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

crs_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: fed6b5062c0ee8c98a1152c8e14cc1c17f8359b6 (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
296
297
298
299
300
301
302
303
304
305
306
307
/**
  ******************************************************************************
  * @file    crs_stm.c
  * @author  MCD Application Team
  * @brief   Cable Replacement Service (Custom STM)
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2018-2021 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */


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

/* Private typedef -----------------------------------------------------------*/
typedef struct
{
  uint16_t	SvcHdle;				  /**< Service handle */
  uint16_t	CRSTXCharHdle;	                   /**< Characteristic handle */
  uint16_t	CRSRXCharHdle;                     /**< Characteristic handle */
}CRSContext_t;

/* Private defines -----------------------------------------------------------*/
#define BM_UUID_LENGTH  UUID_TYPE_128

#define BM_REQ_CHAR_SIZE    (3)


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

/* Private variables ---------------------------------------------------------*/
/**
 * Request Characteristic UUID
 * 0000fe11-8e22-4541-9d4c-21edae82ed19
 */
const uint8_t BM_REQ_CHAR_UUID[16] = {0x19, 0xed, 0x82, 0xae,
                                       0xed, 0x21, 0x4c, 0x9d,
                                       0x41, 0x45, 0x22, 0x8e,
                                       0x11, 0xFE, 0x00, 0x00};

/**
 * START of Section BLE_DRIVER_CONTEXT
 */
PLACE_IN_SECTION("BLE_DRIVER_CONTEXT") static CRSContext_t CRSContext;
/**
 * END of Section BLE_DRIVER_CONTEXT
 */
/* Private function prototypes -----------------------------------------------*/
static SVCCTL_EvtAckStatus_t CRS_Event_Handler(void *Event);


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

#define COPY_UUID_128(uuid_struct, uuid_15, uuid_14, uuid_13, uuid_12, uuid_11, uuid_10, uuid_9, uuid_8, uuid_7, uuid_6, uuid_5, uuid_4, uuid_3, uuid_2, uuid_1, uuid_0) \
do {\
    uuid_struct[0] = uuid_0; uuid_struct[1] = uuid_1; uuid_struct[2] = uuid_2; uuid_struct[3] = uuid_3; \
        uuid_struct[4] = uuid_4; uuid_struct[5] = uuid_5; uuid_struct[6] = uuid_6; uuid_struct[7] = uuid_7; \
            uuid_struct[8] = uuid_8; uuid_struct[9] = uuid_9; uuid_struct[10] = uuid_10; uuid_struct[11] = uuid_11; \
                uuid_struct[12] = uuid_12; uuid_struct[13] = uuid_13; uuid_struct[14] = uuid_14; uuid_struct[15] = uuid_15; \
}while(0)

/* Hardware Characteristics Service */
/*
 The following 128bits UUIDs have been generated from the random UUID
 generator:
 daf4562c-1dfc-11e7-93ae-92361f002671
 daf458b6-1dfc-11e7-93ae-92361f002671
 daf45d16-1dfc-11e7-93ae-92361f002671
 D973F2E0-B19E-11E2-9E96-0800200C9A66: Service 128bits UUID
 D973F2E1-B19E-11E2-9E96-0800200C9A66: Characteristic_1 128bits UUID
 D973F2E2-B19E-11E2-9E96-0800200C9A66: Characteristic_2 128bits UUID
 */
#define COPY_CRS_UUID(uuid_struct, uuid) \
  COPY_UUID_128(uuid_struct, \
    uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], \
    uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15])

#define CRS_MAX_RX_CHAR_LEN                                     CRS_MAX_DATA_LEN        /**< Maximum length of the RX Characteristic (in bytes). */
#define CRS_MAX_TX_CHAR_LEN                                     CRS_MAX_DATA_LEN        /**< Maximum length of the TX Characteristic (in bytes). */


/**
 * @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 CRS_Event_Handler(void *Event)
{
  SVCCTL_EvtAckStatus_t return_value;
  hci_event_pckt *event_pckt;
  evt_blecore_aci *blecore_evt;
  aci_gatt_attribute_modified_event_rp0    * attribute_modified;
  CRSAPP_Notification_evt_t Notification;

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

  switch(event_pckt->evt)
  {
    case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
      {
        blecore_evt = (evt_blecore_aci*)event_pckt->data;
        switch(blecore_evt->ecode)
        {
          case ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE:
           {
              attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
              if(attribute_modified->Attr_Handle == (CRSContext.CRSRXCharHdle + 2))
              {
                /**
                 * Descriptor handle
                 */
                return_value = SVCCTL_EvtAckFlowEnable;
                /**
                 * Notify to application
                 */
                if(attribute_modified->Attr_Data[0] & COMSVC_Notification)
                {
                  Notification.CRS_Evt_Opcode = CRS_NOTIFY_ENABLED_EVT;
                  CRSAPP_Notification(&Notification);

                }
                else
                {
                  Notification.CRS_Evt_Opcode = CRS_NOTIFY_DISABLED_EVT;

                  CRSAPP_Notification(&Notification);

                }
              }
              
              else if(attribute_modified->Attr_Handle == (CRSContext.CRSTXCharHdle + 1))
              {
                /*value handle */
                BLE_DBG_CRS_STM_MSG("-- GATT : RX\n");
                Notification.CRS_Evt_Opcode = CRS_WRITE_EVT;
                Notification.DataTransfered.Length = attribute_modified->Attr_Data_Length;
                Notification.DataTransfered.pPayload = attribute_modified->Attr_Data;
                CRSAPP_Notification(&Notification);  
              }            
            }
            break;

          default:
            break;
        }
      }
      break; /* HCI_HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE_SPECIFIC */

    default:
      break;
  }

  return(return_value);
}/* end CRS_Event_Handler */


/* Public functions ----------------------------------------------------------*/

/**
 * @brief  Service initialization
 * @param  None
 * @retval None
 */
void CRS_STM_Init(void)
{
  Char_UUID_t  uuid;
  tBleStatus hciCmdResult = BLE_STATUS_SUCCESS;
  uint8_t service_uuid[] = { CRS_STM_UUID128 }; 
  uint8_t rx_uuid[]      = { CRS_STM_RX_UUID128 }; 
  uint8_t tx_uuid[]      = { CRS_STM_TX_UUID128 }; 

  /**
   *	Register the event handler to the BLE controller
   */
  SVCCTL_RegisterSvcHandler(CRS_Event_Handler);
  
  /**
   *  Cable Replacement Service
   *
   * Max_Attribute_Records = 2*no_of_char + 1
   * service_max_attribute_record = 1 for Cable Replacement service +
   *                                2 for CRS TX characteristic +
   *                                2 for CRS RX characteristic +
   *                                1 for client char configuration descriptor
   *                                
   */
  COPY_CRS_UUID(uuid.Char_UUID_128, service_uuid);
  hciCmdResult = aci_gatt_add_service(
                                      UUID_TYPE_128,
                                      (Service_UUID_t *) &uuid,
                                      PRIMARY_SERVICE,
                                      6,
                                      &(CRSContext.SvcHdle));

  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    BLE_DBG_CRS_STM_MSG("Cable Replacement (CR) Service is added Successfully 0x%02X\n", 
                 CRSContext.SvcHdle);
  }
  else 
  {
    BLE_DBG_CRS_STM_MSG ("FAILED to add Cable Replacement (CR) Service, Error: 0x%02X !!\n", 
                         hciCmdResult);
  }

  /**
   *  Add TX Characteristic
   */
  COPY_CRS_UUID(uuid.Char_UUID_128, tx_uuid);
  hciCmdResult = aci_gatt_add_char(
                                   CRSContext.SvcHdle,
                                   UUID_TYPE_128, 
                                   &uuid ,
                                   CRS_MAX_TX_CHAR_LEN,                                   
                                   CHAR_PROP_WRITE_WITHOUT_RESP|CHAR_PROP_READ,
                                   ATTR_PERMISSION_NONE,
                                   GATT_NOTIFY_ATTRIBUTE_WRITE, /* gattEvtMask */
                                   10, /* encryKeySize */
                                   1, /* isVariable */
                                   &(CRSContext.CRSTXCharHdle));

  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    BLE_DBG_CRS_STM_MSG("TX Characteristic is added Successfully 0x%02X\n", 
                        CRSContext.CRSTXCharHdle);
  }
  else 
  {
    BLE_DBG_CRS_STM_MSG ("FAILED to add TX Characteristic, Error: 0x%02X !!\n", 
                         hciCmdResult);
  }

  /**
   *   Add RX Characteristic
   */
  COPY_CRS_UUID(uuid.Char_UUID_128, rx_uuid);
  hciCmdResult = aci_gatt_add_char(CRSContext.SvcHdle,
                                   UUID_TYPE_128,
                                   &uuid,
                                   CRS_MAX_RX_CHAR_LEN,
                                   CHAR_PROP_READ|CHAR_PROP_NOTIFY,
                                   ATTR_PERMISSION_NONE,
                                   GATT_NOTIFY_ATTRIBUTE_WRITE, /* gattEvtMask */
                                   10, /* encryKeySize */
                                   1, /* isVariable: 1 */
                                   &(CRSContext.CRSRXCharHdle));    
  if (hciCmdResult == BLE_STATUS_SUCCESS)
  {
    BLE_DBG_CRS_STM_MSG("RX Characteristic is added Successfully 0x%02X\n", 
                        CRSContext.CRSRXCharHdle);
  }
  else 
  {
    BLE_DBG_CRS_STM_MSG ("FAILED to add RX Characteristic, Error: 0x%02X !!\n", 
                 hciCmdResult);
  }

  return;
}

/**
 * @brief  Characteristic update
 * @param  UUID: UUID of the characteristic
 * @param  Service_Instance: Instance of the service to which the characteristic belongs
 * 
 */
tBleStatus CRSAPP_Update_Char(uint16_t UUID, uint8_t *pPayload) 
{
  tBleStatus result = BLE_STATUS_INVALID_PARAMS;
  switch(UUID)
  {
    case CRS_RX_CHAR_UUID:
    {
      uint8_t size;
      
      size = 0;
      while(pPayload[size] != '\0')
      {
        size++;
      }
      result = aci_gatt_update_char_value(CRSContext.SvcHdle,
                                          CRSContext.CRSRXCharHdle,
                                          0, /* charValOffset */
                                          size/*CRS_MAX_RX_CHAR_LEN*/, /* charValueLen */
                                          (uint8_t *)  pPayload);
    }
    break;

    default:
      break;
  }

  return result;
}/* end CRSAPP_Update_Char() */