Welcome to mirror list, hosted at ThFree Co, Russian Federation.

otas_stm.c « Src « svc « ble « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 67241d48732780b3b978098f67f6cbeeef835541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
 ******************************************************************************
 * @file    otas_stm.c
 * @author  MCD Application Team
 * @brief   OTA Service
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/
#include "common_blesvc.h"

/* Private typedef -----------------------------------------------------------*/
typedef enum
{
  OTAS_Conf_Not_Pending,
  OTAS_Conf_Pending,
} OTAS_Conf_Status_t;

typedef struct
{
  uint16_t	OTAS_SvcHdle;				          /**< Service handle */
  uint16_t  OTAS_Base_Addr_CharHdle;      /**< Characteristic handle */
  uint16_t  OTAS_Conf_CharHdle;           /**< Characteristic handle */
  uint16_t  OTAS_Raw_Data_CharHdle;       /**< Characteristic handle */
  OTAS_Conf_Status_t    OTAS_Conf_Status; /**< Indication Status */
}OTAS_Context_t;


/* Private defines -----------------------------------------------------------*/
#define UUID_128_SUPPORTED  1
#if (UUID_128_SUPPORTED == 1)
#define OTA_UUID_LENGTH  UUID_TYPE_128
#else
#define OTA_UUID_LENGTH  UUID_TYPE_16
#endif

#define OTA_BASE_ADR_CHAR_SIZE    (4)
#define OTA_CONF_CHAR_SIZE        (1)
#define OTA_RAW_DATA_CHAR_SIZE    OTAS_STM_RAW_DATA_SIZE

/**
* Service UUID
* 0000fe20-cc7a-482a-984a-7f2ed5b3e58f
*/
#if (UUID_128_SUPPORTED == 1)
const uint8_t OTAS_SVC_UUID[16] = {0x8f, 0xe5, 0xb3, 0xd5,
        0x2e, 0x7f, 0x4a, 0x98,
        0x2a, 0x48, 0x7a, 0xcc,
        0x20, 0xFE, 0x00, 0x00};
#else
const uint8_t OTAS_SVC_UUID[2] = {0x20, 0xFE};
#endif


/**
* Base Address Characteristic UUID
* 0000fe22-8e22-4541-9d4c-21edae82ed19
*/
#if (UUID_128_SUPPORTED == 1)
const uint8_t OTA_BASE_ADR_CHAR_UUID[16] = {0x19, 0xed, 0x82, 0xae,
        0xed, 0x21, 0x4c, 0x9d,
        0x41, 0x45, 0x22, 0x8e,
        0x22, 0xFE, 0x00, 0x00};
#else
const uint8_t OTA_BASE_ADR_CHAR_UUID[2] = {0x22, 0xFE};
#endif

/**
* Confirmation Characteristic UUID
* 0000fe23-8e22-4541-9d4c-21edae82ed19
*/
#if (UUID_128_SUPPORTED == 1)
const uint8_t OTA_CONF_CHAR_UUID[16] = {0x19, 0xed, 0x82, 0xae,
        0xed, 0x21, 0x4c, 0x9d,
        0x41, 0x45, 0x22, 0x8e,
        0x23, 0xFE, 0x00, 0x00};
#else
const uint8_t OTA_CONF_CHAR_UUID[2] = {0x23, 0xfe};
#endif

/**
* Raw Data Characteristic UUID
* 0000fe24-8e22-4541-9d4c-21edae82ed19
*/
#if (UUID_128_SUPPORTED == 1)
const uint8_t OTA_RAW_DATA_CHAR_UUID[16] = {0x19, 0xed, 0x82, 0xae,
        0xed, 0x21, 0x4c, 0x9d,
       0x41, 0x45, 0x22, 0x8e,
        0x24, 0xFE, 0x00, 0x00};
#else
const uint8_t OTA_RAW_DATA_CHAR_UUID[2] = {0x24, 0xfe};
#endif


/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/**
 * START of Section BLE_DRIVER_CONTEXT
 */
PLACE_IN_SECTION("BLE_DRIVER_CONTEXT") static OTAS_Context_t OTAS_Context;
/**
 * END of Section BLE_DRIVER_CONTEXT
 */

/* Private function prototypes -----------------------------------------------*/
static SVCCTL_EvtAckStatus_t OTAS_Event_Handler(void *pckt);


/* Functions Definition ------------------------------------------------------*/
/* Private functions ----------------------------------------------------------*/

/**
 * @brief  Event handler
 * @param  Event: Address of the buffer holding the Event
 * @retval Ack: Return whether the Event has been managed or not
 */
static SVCCTL_EvtAckStatus_t OTAS_Event_Handler(void *Event)
{
  SVCCTL_EvtAckStatus_t return_value;
  hci_event_pckt *event_pckt;
  evt_blue_aci *blue_evt;
  aci_gatt_attribute_modified_event_rp0    * attribute_modified;
  OTA_STM_Notification_t notification;

  return_value = SVCCTL_EvtNotAck;

  event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)Event)->data);

  switch(event_pckt->evt)
  {
    case EVT_VENDOR:
    {
      blue_evt = (evt_blue_aci*)event_pckt->data;
      attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blue_evt->data;
      switch(blue_evt->ecode)
      {
        case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED:
        {
          if(attribute_modified->Attr_Handle == (OTAS_Context.OTAS_Base_Addr_CharHdle + 1))
          {
            /**
             * Base Address
             */
            return_value = SVCCTL_EvtAckFlowEnable;

            notification.ChardId = OTAS_STM_BASE_ADDR_ID;
            notification.pPayload = (uint8_t*)&attribute_modified->Attr_Data[0];
            notification.ValueLength = attribute_modified->Attr_Data_Length;
            OTAS_STM_Notification( &notification );
          }
          else if(attribute_modified->Attr_Handle == (OTAS_Context.OTAS_Raw_Data_CharHdle  + 1))
          {
            /**
             * Raw Data
             */
            return_value = SVCCTL_EvtAckFlowEnable;

            notification.ChardId = OTAS_STM_RAW_DATA_ID;
            notification.pPayload = (uint8_t*)&attribute_modified->Attr_Data[0];
            notification.ValueLength = attribute_modified->Attr_Data_Length;
            OTAS_STM_Notification( &notification );
          }
        }
        break;

        case EVT_BLUE_GATT_SERVER_CONFIRMATION_EVENT:
        {
          if( OTAS_Context.OTAS_Conf_Status != OTAS_Conf_Not_Pending)
          {
            /**
             * Confirmation Event
             */
            OTAS_Context.OTAS_Conf_Status = OTAS_Conf_Not_Pending;

            return_value = SVCCTL_EvtAckFlowEnable;

            notification.ChardId = OTAS_STM_CONF_EVENT_ID;
            notification.pPayload = (uint8_t*)&attribute_modified->Attr_Data[0];
            notification.ValueLength = attribute_modified->Attr_Data_Length;
            OTAS_STM_Notification( &notification );
          }
        }
        break;

        default:
          break;
      }
    }
    break;

    default:
      break;
  }

  return(return_value);
}

/* Public functions ----------------------------------------------------------*/
void OTAS_STM_Init(void)
{
  /**
   *	Register the event handler to the BLE controller
   */
  SVCCTL_RegisterSvcHandler(OTAS_Event_Handler);

  /**
   *  Add OTA Service
   */
  aci_gatt_add_service(OTA_UUID_LENGTH,
                       (Service_UUID_t *)OTAS_SVC_UUID,
                       PRIMARY_SERVICE,
                       1
                       + 2  /**< OTA_BASE CHAR */
                       + 3  /**< OTA_CONF CHAR */
                       + 2,  /**< OTA_RAW_DATA CHAR */
                       &(OTAS_Context.OTAS_SvcHdle));


  /**
   *  Add Base Address Characteristic
   */
  aci_gatt_add_char(OTAS_Context.OTAS_SvcHdle,
                    OTA_UUID_LENGTH,
                    (Char_UUID_t *)OTA_BASE_ADR_CHAR_UUID,
                    OTA_BASE_ADR_CHAR_SIZE,
                    CHAR_PROP_WRITE_WITHOUT_RESP,
                    ATTR_PERMISSION_NONE,
                    GATT_NOTIFY_ATTRIBUTE_WRITE,
                    10,
                    0,
                    &(OTAS_Context.OTAS_Base_Addr_CharHdle));

  /**
   *  Add Confirmation Characteristic
   */
  aci_gatt_add_char(OTAS_Context.OTAS_SvcHdle,
                    OTA_UUID_LENGTH,
                    (Char_UUID_t *)OTA_CONF_CHAR_UUID,
                    OTA_CONF_CHAR_SIZE,
                    CHAR_PROP_INDICATE,
                    ATTR_PERMISSION_NONE,
                    GATT_DONT_NOTIFY_EVENTS,
                    10,
                    0,
                    &(OTAS_Context.OTAS_Conf_CharHdle));

  /**
   *  Add Raw Data Characteristic
   */
  aci_gatt_add_char(OTAS_Context.OTAS_SvcHdle,
                    OTA_UUID_LENGTH,
                    (Char_UUID_t *)OTA_RAW_DATA_CHAR_UUID,
                    OTA_RAW_DATA_CHAR_SIZE,
                    CHAR_PROP_WRITE_WITHOUT_RESP,
                    ATTR_PERMISSION_NONE,
                    GATT_NOTIFY_ATTRIBUTE_WRITE,
                    10,
                    1,
                    &(OTAS_Context.OTAS_Raw_Data_CharHdle));

  OTAS_Context.OTAS_Conf_Status = OTAS_Conf_Not_Pending;

  return;
}

tBleStatus OTAS_STM_UpdateChar(OTAS_STM_ChardId_t  ChardId, uint8_t *p_payload)
{
  tBleStatus return_value;

  OTAS_Context.OTAS_Conf_Status = OTAS_Conf_Pending;

  return_value = aci_gatt_update_char_value(OTAS_Context.OTAS_SvcHdle,
                                            OTAS_Context.OTAS_Conf_CharHdle,
                                            0,                  /**< charValOffset */
                                            OTA_CONF_CHAR_SIZE, /**< charValueLen */
                                            p_payload);

  return return_value;
}


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/