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

dts.c « App « STM32_WPAN « BLE_DataThroughput « BLE « Applications « NUCLEO-WB15CC « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d6d37f0f00b9c9cfded17e5901c7cbadff4ab3c (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
 ******************************************************************************
 * @file    dts.c
 * @author  MCD Application Team
 * @brief   Data Transfer Service (Custom)
 ******************************************************************************
 * @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 "ble_common.h"
#include "ble.h"
#include "dbg_trace.h"
#include "dts.h"   

#define UUID_128_SUPPORTED 1

#if (UUID_128_SUPPORTED == 1)
#define DT_UUID_LENGTH  UUID_TYPE_128
#else
#define DT_UUID_LENGTH  UUID_TYPE_16
#endif

#if (UUID_128_SUPPORTED == 1)
const uint8_t DT_REQ_CHAR_UUID[16] =
{ 0x19, 0xed, 0x82, 0xae,
  0xed, 0x21, 0x4c, 0x9d,
  0x41, 0x45, 0x22, 0x8e,
  0x81, 0xFE, 0x00, 0x00};
#else
const uint8_t DT_REQ_CHAR_UUID[2] = { 0x81, 0xFE };
#endif

#if (UUID_128_SUPPORTED == 1)
const uint8_t DT_REQ_CHAR2_UUID[16] =
{ 0x19, 0xed, 0x82, 0xae,
  0xed, 0x21, 0x4c, 0x9d,
  0x41, 0x45, 0x22, 0x8e,
  0x82, 0xFE, 0x00, 0x00};
#else
const uint8_t DT_REQ_CHAR2_UUID[2] = { 0x82, 0xFE };
#endif

#if (UUID_128_SUPPORTED == 1)
const uint8_t DT_REQ_CHAR3_UUID[16] =
{ 0x19, 0xed, 0x82, 0xae,
  0xed, 0x21, 0x4c, 0x9d,
  0x41, 0x45, 0x22, 0x8e,
  0x83, 0xFE, 0x00, 0x00};
#else
const uint8_t DT_REQ_CHAR3_UUID[2] = { 0x83, 0xFE };
#endif

#if (UUID_128_SUPPORTED == 1)
const uint8_t DT_REQ_SERV_UUID[16] =
{ 0x19, 0xed, 0x82, 0xae,
  0xed, 0x21, 0x4c, 0x9d,
  0x41, 0x45, 0x22, 0x8e,
  0x80, 0xFE, 0x00, 0x00};
#else
const uint8_t DT_REQ_SERV_UUID[2] = { 0x80, 0xFE };
#endif

/* Private typedef -----------------------------------------------------------*/
typedef enum
{
  DTS_STM_NOTIFICATION_MASK = (1 << 0),
  DTS_STM_INDICATION_MASK = (1 << 1),
} ClientCharConfMask_t;

typedef struct
{
uint16_t DataTransferSvcHdle; /**< Service handle */
uint16_t DataTransferTxCharHdle; /**< Characteristic handle */
uint16_t DataTransferRxCharHdle; /**< Characteristic handle */
uint16_t DataTransferTxChar3Hdle; /**< Characteristic handle */
} DataTransferSvcContext_t;

/* Private defines -----------------------------------------------------------*/
#define DATA_TRANSFER_NOTIFICATION_LEN_MAX                                 (255)

/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static tBleStatus TX_Update_Char( DTS_STM_Payload_t *pDataValue );
static SVCCTL_EvtAckStatus_t DTS_Event_Handler( void *pckt );
static DataTransferSvcContext_t aDataTransferContext;
extern uint16_t Att_Mtu_Exchanged;
extern void BLE_SVC_L2CAP_Conn_Update_7_5(void);

/* 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 DTS_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;
  aci_att_exchange_mtu_resp_event_rp0 * exchange_mtu_resp;
  aci_gatt_write_permit_req_event_rp0 * write_permit_req ;

  DTS_STM_App_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_ATT_EXCHANGE_MTU_RESP_VSEVT_CODE:
          exchange_mtu_resp = (aci_att_exchange_mtu_resp_event_rp0 *)blecore_evt->data;
          APP_DBG_MSG("**MTU_size = %d \n",exchange_mtu_resp->Server_RX_MTU );
          APP_DBG_MSG("\r\n\r");
          Att_Mtu_Exchanged = exchange_mtu_resp->Server_RX_MTU;
          break;
        /* server */
        case ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE:
        {
          attribute_modified = (aci_gatt_attribute_modified_event_rp0*) blecore_evt->data;
          if (attribute_modified->Attr_Handle == (aDataTransferContext.DataTransferTxCharHdle + 2))
          {
            /**
             * Notify to application to start measurement
             */
            if (attribute_modified->Attr_Data[0] & DTS_STM_NOTIFICATION_MASK)
            {
              APP_DBG_MSG("**NOTIFICATION ENABLED \n");
              APP_DBG_MSG("\r\n\r");
              Notification.Evt_Opcode = DTS_STM__NOTIFICATION_ENABLED;
              DTS_Notification(&Notification);
            }
            else
            {
              APP_DBG_MSG("**NOTIFICATION DISABLED \n");
              APP_DBG_MSG("\r\n\r");
              Notification.Evt_Opcode = DTS_STM_NOTIFICATION_DISABLED;
              DTS_Notification(&Notification);
            }
          }
          if (attribute_modified->Attr_Handle == (aDataTransferContext.DataTransferTxChar3Hdle + 2))
          {
            /**
            * Notify to application to start measurement
            */
            if (attribute_modified->Attr_Data[0] & DTS_STM_NOTIFICATION_MASK)
            {
              APP_DBG_MSG("**WRITE RESULT NOTIFICATION ENABLED \n");
              APP_DBG_MSG("\r\n\r");
              BLE_SVC_L2CAP_Conn_Update_7_5();
              Notification.Evt_Opcode = DTC_NOTIFICATION_ENABLED;
              DTS_Notification(&Notification);
            }
            else
            {
              APP_DBG_MSG("**WRITE RESULT NOTIFICATION DISABLED \n");
              APP_DBG_MSG("\r\n\r");
              Notification.Evt_Opcode = DTC_NOTIFICATION_DISABLED;
              DTS_Notification(&Notification);
            }
          }
          if(attribute_modified->Attr_Handle == (aDataTransferContext.DataTransferRxCharHdle + 1))
          {
            return_value = SVCCTL_EvtAckFlowEnable;
            
            Notification.Evt_Opcode = DTS_STM_DATA_RECEIVED;
            Notification.DataTransfered.Length=attribute_modified->Attr_Data_Length;
            DTS_Notification(&Notification); 
          }
          }
          break;
        case ACI_GATT_TX_POOL_AVAILABLE_VSEVT_CODE:
          Resume_Notification();
          break; 
          
      case ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE:
        //APP_DBG_MSG("WRITE PERMIT RESP \r\n");
        write_permit_req = (aci_gatt_write_permit_req_event_rp0 *) blecore_evt->data;
        aci_gatt_write_resp( write_permit_req->Connection_Handle, write_permit_req->Attribute_Handle, 0, 0, write_permit_req->Data_Length, write_permit_req->Data);        
        break;

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

    default:
      break;
  }

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

/**
 * @brief  Feature Characteristic update
 * @param  Service_Instance: Instance of the service to which the characteristic belongs
 * @param  pFeatureValue: The address of the new value to be written
 * @retval None
 */
static tBleStatus TX_Update_Char( DTS_STM_Payload_t *pDataValue )
{
  tBleStatus ret;

  /**
   *  Notification Data Transfer Packet
   */
  ret = aci_gatt_update_char_value(
                                   aDataTransferContext.DataTransferSvcHdle,
                                   aDataTransferContext.DataTransferTxCharHdle,
                                   0, /* charValOffset */
                                   pDataValue->Length, /* charValueLen */
                                   (uint8_t *) pDataValue->pPayload);

  return ret;
}/* end TX_Update_Char() */

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

/**
 * @brief  Service initialization
 * @param  None
 * @retval None
 */
void DTS_STM_Init( void )
{
  tBleStatus hciCmdResult = BLE_STATUS_FAILED;

  /**
   *	Register the event handler to the BLE controller
   */
  SVCCTL_RegisterSvcHandler(DTS_Event_Handler);

  /* DT service and characteristics */
  hciCmdResult = aci_gatt_add_service(DT_UUID_LENGTH, (Service_UUID_t *) DT_REQ_SERV_UUID,
  PRIMARY_SERVICE,
                                      10, &(aDataTransferContext.DataTransferSvcHdle));
  if (hciCmdResult != 0)
  {
    APP_DBG_MSG("error add service 0x%x\n", hciCmdResult);
  }

  /**
   *  Add Data Transfer TX Characteristic
   */
  hciCmdResult = aci_gatt_add_char(aDataTransferContext.DataTransferSvcHdle,
  DT_UUID_LENGTH,
                    (Char_UUID_t *) DT_REQ_CHAR_UUID,
                    DATA_TRANSFER_NOTIFICATION_LEN_MAX,
                    CHAR_PROP_NOTIFY,
                    ATTR_PERMISSION_NONE,
                    GATT_DONT_NOTIFY_EVENTS, /* gattEvtMask */
                    10, /* encryKeySize */
                    1, /* isVariable */
                    &(aDataTransferContext.DataTransferTxCharHdle));
  if (hciCmdResult != 0)
  {
    APP_DBG_MSG("error add char Tx 0x%x\n", hciCmdResult);
  }

  /**
   *  Add Data Transfer RX Characteristic
   */
  hciCmdResult = aci_gatt_add_char(aDataTransferContext.DataTransferSvcHdle,
  DT_UUID_LENGTH,
                    (Char_UUID_t *) DT_REQ_CHAR2_UUID,
                    255, /* DATA_TRANSFER_NOTIFICATION_LEN_MAX, */
                    CHAR_PROP_WRITE_WITHOUT_RESP,
                    ATTR_PERMISSION_NONE,
                    GATT_NOTIFY_ATTRIBUTE_WRITE, //GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP,/* gattEvtMask */
                    10, /* encryKeySize */
                    1, /* isVariable */
                    &(aDataTransferContext.DataTransferRxCharHdle));
  if (hciCmdResult != 0)
  {
    APP_DBG_MSG("error add char Tx\n");
  }
  
  /**
   *  Add Data Transfer TX Characteristic
   */
  hciCmdResult = aci_gatt_add_char(aDataTransferContext.DataTransferSvcHdle,
  DT_UUID_LENGTH,
                    (Char_UUID_t *) DT_REQ_CHAR3_UUID,
                    255, /* DATA_TRANSFER_NOTIFICATION_LEN_MAX, */
                    CHAR_PROP_NOTIFY,
                    ATTR_PERMISSION_NONE,
                    GATT_DONT_NOTIFY_EVENTS, /* gattEvtMask */
                    10, /* encryKeySize */
                    1, /* isVariable */
                    &(aDataTransferContext.DataTransferTxChar3Hdle));
  if (hciCmdResult != 0)
  {
    APP_DBG_MSG("error add char Tx\n");
  }
  
  return;
}

/**
 * @brief  Characteristic update
 * @param  UUID: UUID of the characteristic
 * @param  Service_Instance: Instance of the service to which the characteristic belongs
 * 
 */
tBleStatus DTS_STM_UpdateChar( uint16_t UUID , uint8_t *pPayload )
{
  tBleStatus result = BLE_STATUS_INVALID_PARAMS;
  switch (UUID)
  {
    case DATA_TRANSFER_TX_CHAR_UUID:
      result = TX_Update_Char((DTS_STM_Payload_t*) pPayload);
      break;

    default:
      break;
  }
  return result;
}/* end DTS_STM_UpdateChar() */

tBleStatus DTS_STM_UpdateCharThroughput(DTS_STM_Payload_t *pDataValue )
{
  tBleStatus result = BLE_STATUS_INVALID_PARAMS;
  /**
   *  Notification Data Transfer Packet
   */
  result = aci_gatt_update_char_value(
                                   aDataTransferContext.DataTransferSvcHdle,
                                   aDataTransferContext.DataTransferTxChar3Hdle,
                                   0, /* charValOffset */
                                   pDataValue->Length, /* charValueLen */
                                   (uint8_t *) pDataValue->pPayload);
  return result;
}/* end DTS_STM_UpdateChar() */

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