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

dt_client_app.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: 20b2549cfd63496f48b100fc5154f89f2f21f52c (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
 ******************************************************************************
 * @file    dt_client_app.c
 * @author  MCD Application Team
 * @brief   data throughput client Application
 ******************************************************************************
 * @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 "app_common.h"

#include "dbg_trace.h"

#include "ble.h"
#include "app_ble.h"    
#include "dt_client_app.h"
#include "dts.h"

#include "stm32_seq.h"

/* Private defines -----------------------------------------------------------*/
#define UNPACK_2_BYTE_PARAMETER(ptr)  \
    (uint16_t)((uint16_t)(*((uint8_t *)ptr))) |   \
    (uint16_t)((((uint16_t)(*((uint8_t *)ptr + 1))) << 8))

#define DEFAULT_TS_MEASUREMENT_INTERVAL   (1000000/CFG_TS_TICK_VAL)  /**< 1s */          
#define TIMEUNIT                          1
#define DATA_THROUGHPUT_MEASUREMENT       (TIMEUNIT*DEFAULT_TS_MEASUREMENT_INTERVAL)

/* Private typedef -----------------------------------------------------------*/
typedef enum
{
  GATT_PROC_MTU_UPDATE,
  GATT_PROC_DISC_ALL_PRIMARY_SERVICES,
  GATT_PROC_DISC_ALL_CHAR_OF_DT_SERVICE,
  GATT_PROC_DISC_TX_CHAR_DESC,
} GattProcId_t;

typedef struct
{
  /**
   * connection handle
   */
  uint16_t connHandle;

  /**
   * handle of the Data Transfer service
   */
  uint16_t DataTransferServiceHandle;

  /**
   * end handle of the Data Transfer service
   */
  uint16_t DataTransferServiceEndHandle;

  /**
   * handle of the Tx characteristic
   *
   */
  uint16_t DataTransferTxCharHdle;

  /**
   * handle of the client configuration
   * descriptor of Tx characteristic
   */
  uint16_t DataTransferTxDescHandle;

} DataTransferContext_t;

/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
DataTransferContext_t DataTransferClientContext;
static uint16_t packet_lost;
static uint32_t DataThroughputValue;
static uint32_t packet_lost_local;
static uint32_t TotalDataReceived = 0;
static uint32_t Nbr_packet = 0;
static DTS_STM_App_Notification_evt_t NotificationData;
static uint8_t TimerDataThroughput_Id;
uint32_t N=0;
uint32_t DataTransfered = 0;
uint16_t Att_Mtu_Exchanged;
//extern DTS_App_Context_t DataTransferServerContext;

/* Global variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static SVCCTL_EvtAckStatus_t DTC_Event_Handler( void *Event );
static void DataThroughputTimer( void );
static void DataThroughput_Calculation(void);
static void GattProcReq(GattProcId_t GattProcId);

/* Functions Definition ------------------------------------------------------*/
/*************************************************************
 *
 * PUBLIC FUNCTIONS
 *
 *************************************************************/
void DTC_App_Init( void )
{
  SVCCTL_RegisterCltHandler(DTC_Event_Handler);

  UTIL_SEQ_RegTask( 1<< CFG_TASK_APP_DATA_THROUGHPUT_ID, UTIL_SEQ_RFU, DataThroughput_Calculation);

  DataTransferClientContext.connHandle = 0xFFFF;
  /* Create timer for Data Throughput */
  HW_TS_Create(CFG_TIM_PROC_ID_ISR, &(TimerDataThroughput_Id), hw_ts_SingleShot, DataThroughputTimer);

  APP_DBG_MSG("-- DT CLIENT INITIALIZED \n");

  return;
}

void DTC_App_LinkReadyNotification(uint16_t ConnectionHandle)
{
#if(CFG_SERVER_ONLY != 1)
  tBleStatus status;
  uint16_t enable_notification = 0x0001;
#endif  

  DataTransferClientContext.connHandle = ConnectionHandle;

#if(CFG_BLE_CENTRAL != 1)
  GattProcReq(GATT_PROC_MTU_UPDATE);
#endif
#if(CFG_SERVER_ONLY != 1)
  GattProcReq(GATT_PROC_DISC_ALL_PRIMARY_SERVICES);
  GattProcReq(GATT_PROC_DISC_ALL_CHAR_OF_DT_SERVICE);
  GattProcReq(GATT_PROC_DISC_TX_CHAR_DESC);

  /**
   * Enable notification on the remote server
   */
  APP_DBG_MSG("Enable notification on the remote server\n");

  status = aci_gatt_write_char_desc(
      DataTransferClientContext.connHandle,
      DataTransferClientContext.DataTransferTxDescHandle,
      2,
      (uint8_t *) &enable_notification);
  if (status != BLE_STATUS_SUCCESS)
  {
    APP_DBG_MSG("Enable notification cmd failure: 0x%x\n", status);
  }
#endif
  
#if(CFG_BLE_CENTRAL != 0)
    UTIL_SEQ_SetTask(1<<CFG_TASK_CONN_UPDATE_ID, CFG_SCH_PRIO_0); 
#endif
  return;
}

/*************************************************************
 *
 * CALLBACK FUNCTIONS
 *
 *************************************************************/
static SVCCTL_EvtAckStatus_t DTC_Event_Handler( void *Event )
{
  SVCCTL_EvtAckStatus_t return_value;
  hci_event_pckt * event_pckt;
  evt_blecore_aci * blecore_evt;
  uint8_t CRC_Result;
  uint8_t CRC_Received;

  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_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE:
        {
          aci_att_read_by_group_type_resp_event_rp0 *pr = (void*) blecore_evt->data;
          uint8_t numServ, i, idx;
          uint16_t uuid, handle;

          handle = pr->Connection_Handle;

          APP_DBG_MSG("DTC_Event_Handler: ACI_ATT_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE\n");

          DataTransferClientContext.connHandle = handle;

          numServ = (pr->Data_Length) / pr->Attribute_Data_Length;

          /* the event data will be
           * 2bytes start handle
           * 2bytes end handle
           * 2 or 16 bytes data
           * we are interested only if the UUID is 16 bit.
           * So check if the data length is 6
           */
#if (UUID_128BIT_FORMAT==1)
          if (pr->Attribute_Data_Length == 20)
          {
            idx = 16;
#else
            if (pr->Attribute_Data_Length == 6)
            {
              idx = 4;
#endif
              for (i = 0; i < numServ; i++)
              {
                uuid = UNPACK_2_BYTE_PARAMETER(&pr->Attribute_Data_List[idx]);
                if (uuid == DATA_TRANSFER_SERVICE_UUID)
                {
#if (UUID_128BIT_FORMAT==1)
                  DataTransferClientContext.DataTransferServiceHandle = UNPACK_2_BYTE_PARAMETER(&pr->Attribute_Data_List[idx-16]);
                  DataTransferClientContext.DataTransferServiceEndHandle = UNPACK_2_BYTE_PARAMETER (&pr->Attribute_Data_List[idx-14]);
#else
                  DataTransferClientContext.DataTransferServiceHandle =
                      UNPACK_2_BYTE_PARAMETER(&pr->Attribute_Data_List[idx - 4]);
                  DataTransferClientContext.DataTransferServiceEndHandle =
                      UNPACK_2_BYTE_PARAMETER(&pr->Attribute_Data_List[idx - 2]);
#endif
                  APP_DBG_MSG("DTC_Event_Handler: DATA_TRANSFER_SERVICE_UUID found !\n");
                }
                idx += 6;
              }
            }
          }
          break;

        case ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE:
        {
          aci_att_read_by_type_resp_event_rp0 *pr = (void*) blecore_evt->data;
          uint8_t idx;
          uint16_t uuid, handle;

          APP_DBG_MSG("DTC_Event_Handler: ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE\n");

          /* the event data will be
           * 2 bytes start handle
           * 1 byte char properties
           * 2 bytes handle
           * 2 or 16 bytes data
           */
#if (UUID_128BIT_FORMAT==1)
          idx = 17;
          if (pr->Handle_Value_Pair_Length == 21)
#else
            idx = 5;
          /* we are interested in only 16 bit UUIDs */
          if (pr->Handle_Value_Pair_Length == 7)
#endif
          {
            pr->Data_Length -= 1;
            while(pr->Data_Length > 0)
            {
              uuid = UNPACK_2_BYTE_PARAMETER(&pr->Handle_Value_Pair_Data[idx]);
              /* store the characteristic handle not the attribute handle */
#if (UUID_128BIT_FORMAT==1)
              handle = UNPACK_2_BYTE_PARAMETER(&pr->Handle_Value_Pair_Data[idx-14]);
#else
              handle = UNPACK_2_BYTE_PARAMETER(&pr->Handle_Value_Pair_Data[idx - 2]);
#endif
              if (uuid == DATA_TRANSFER_TX_CHAR_UUID)
              {
                DataTransferClientContext.DataTransferTxCharHdle = handle;
              }
#if (UUID_128BIT_FORMAT==1)
              pr->Data_Length -= 21;
              idx += 21;
#else
              pr->Data_Length -= 7;
              idx += 7;
#endif
            }
          }
        }
        break;
        case ACI_ATT_FIND_INFO_RESP_VSEVT_CODE:
        {
          aci_att_find_info_resp_event_rp0 *pr = (void*) blecore_evt->data;
          uint8_t numDesc, idx, i;
          uint16_t uuid, handle;

          /*
           * event data will be of the format
           * 2 bytes handle
           * 2 bytes UUID
           */
          numDesc = (pr->Event_Data_Length) / 4;
          /* we are interested only in 16 bit UUIDs */
          idx = 0;
          if (pr->Format == UUID_TYPE_16)
          {
            for (i = 0; i < numDesc; i++)
            {
              handle = UNPACK_2_BYTE_PARAMETER(&pr->Handle_UUID_Pair[idx]);
              uuid = UNPACK_2_BYTE_PARAMETER(&pr->Handle_UUID_Pair[idx + 2]);

              if (uuid == CLIENT_CHAR_CONFIG_DESCRIPTOR_UUID)
              {
                APP_DBG_MSG("DTC_Event_Handler: DTC_DISCOVER_TX_CHAR_DESC ready to enable notification\n");
                DataTransferClientContext.DataTransferTxDescHandle = handle;
              }
              idx += 4;
            }
          }
        }
        break; /*ACI_ATT_FIND_INFO_RESP_VSEVT_CODE*/

        case ACI_GATT_NOTIFICATION_VSEVT_CODE:
        {
          aci_gatt_notification_event_rp0 *pr = (void*) blecore_evt->data;

          if ((pr->Attribute_Handle == DataTransferClientContext.DataTransferTxCharHdle)
              && (pr->Attribute_Value_Length > (2)))
          {
            NotificationData.DataTransfered.Length = pr->Attribute_Value_Length;
            NotificationData.DataTransfered.pPayload = (pr->Attribute_Value);
            NotificationData.DataTransfered.pPayload_n = *((uint32_t*) &(pr->Attribute_Value[0]));
            __disable_irq();
            if (NotificationData.DataTransfered.pPayload_n >= (NotificationData.DataTransfered.pPayload_n_1 + 2))
            {
              packet_lost +=
                  ((NotificationData.DataTransfered.pPayload_n - NotificationData.DataTransfered.pPayload_n_1) - 1);
            }
            __enable_irq();
            /* CRC computation */
            CRC_Result = APP_BLE_ComputeCRC8((uint8_t*) (pr->Attribute_Value), (pr->Attribute_Value_Length) - 1);
            /* get low weight byte */
            CRC_Received = (uint8_t) (pr->Attribute_Value[DATA_NOTIFICATION_MAX_PACKET_SIZE-1]);

            if (CRC_Received != CRC_Result)
            {
              N+=1;
              APP_DBG_MSG("** data error **  N= %d \r\n",(int)N);
            }
            NotificationData.DataTransfered.pPayload_n_1 = NotificationData.DataTransfered.pPayload_n;
            if (DataTransfered == 0)
            {
              HW_TS_Start(TimerDataThroughput_Id, DATA_THROUGHPUT_MEASUREMENT);
            }
            DataTransfered += NotificationData.DataTransfered.Length;
          }
        }
        break;/* end ACI_GATT_NOTIFICATION_VSEVT_CODE */

        case ACI_GATT_PROC_COMPLETE_VSEVT_CODE:
          UTIL_SEQ_SetEvt(1 << CFG_IDLEEVT_GATT_PROC_COMPLETE);
          break; /*ACI_GATT_PROC_COMPLETE_VSEVT_CODE*/

        default:
          break;
        }
        default:
          break;
      }
    }

    return (return_value);
  }

  /*************************************************************
   *
   * LOCAL FUNCTIONS
   *
   *************************************************************/
  static void DataThroughput_Calculation( void )
  {
    uint32_t MoyData;

    if (Nbr_packet < 600)
    {
      Nbr_packet++;
      TotalDataReceived += DataThroughputValue;
    }
    if (Nbr_packet == 600)
    {
      MoyData = TotalDataReceived / 600;
      APP_DBG_MSG("Moy = %ld bytes/s \n",MoyData);
      TotalDataReceived = 0;
      Nbr_packet = 0;
    }

    APP_DBG_MSG("DT = %ld bytes/s lost = %ld \n",DataThroughputValue, packet_lost_local);
  }

  static void DataThroughputTimer( void )
  {
    DataThroughputValue = (uint32_t)(DataTransfered / TIMEUNIT);
    packet_lost_local = (uint32_t)(packet_lost / TIMEUNIT);
    DataTransfered = 0;
    packet_lost = 0;

    UTIL_SEQ_SetTask(1 << CFG_TASK_APP_DATA_THROUGHPUT_ID, CFG_SCH_PRIO_0);
    return;
  }

  static void GattProcReq(GattProcId_t GattProcId)
  {
    tBleStatus status;

    switch(GattProcId)
    {
      case GATT_PROC_MTU_UPDATE:
        APP_DBG_MSG("change ATT MTU size \n");

        status = aci_gatt_exchange_config(DataTransferClientContext.connHandle);
        if (status != BLE_STATUS_SUCCESS)
        {
          APP_DBG_MSG("change MTU cmd failure: 0x%x\n", status);
        }
        UTIL_SEQ_WaitEvt(1 << CFG_IDLEEVT_GATT_PROC_COMPLETE);

        APP_DBG_MSG("GATT_PROC_MTU_UPDATE complete event received \n");
        break;

      case GATT_PROC_DISC_ALL_PRIMARY_SERVICES:
        APP_DBG_MSG("Discover all primary services \n");

        status = aci_gatt_disc_all_primary_services(DataTransferClientContext.connHandle);
        if (status != BLE_STATUS_SUCCESS)
        {
          APP_DBG_MSG("Discover all primary services cmd failure: 0x%x\n", status);
        }
        UTIL_SEQ_WaitEvt(1 << CFG_IDLEEVT_GATT_PROC_COMPLETE);

        APP_DBG_MSG("GATT_PROC_DISC_ALL_PRIMARY_SERVICES complete event received \n");
        break;

      case GATT_PROC_DISC_ALL_CHAR_OF_DT_SERVICE:
        APP_DBG_MSG("Discover all char of service \n");

        status = aci_gatt_disc_all_char_of_service(
            DataTransferClientContext.connHandle,
            DataTransferClientContext.DataTransferServiceHandle,
            DataTransferClientContext.DataTransferServiceEndHandle);
        if (status != BLE_STATUS_SUCCESS)
        {
          APP_DBG_MSG("Discover all char of service cmd failure: 0x%x\n", status);
        }
        UTIL_SEQ_WaitEvt(1 << CFG_IDLEEVT_GATT_PROC_COMPLETE);

        APP_DBG_MSG("GATT_PROC_DISC_ALL_CHAR_OF_DT_SERVICE complete event received \n");
        break;

      case GATT_PROC_DISC_TX_CHAR_DESC:
        APP_DBG_MSG("Discover Tx char descriptors \n");

        status = aci_gatt_disc_all_char_desc(
            DataTransferClientContext.connHandle,
            DataTransferClientContext.DataTransferTxCharHdle,
            DataTransferClientContext.DataTransferTxCharHdle + 2);
        if (status != BLE_STATUS_SUCCESS)
        {
          APP_DBG_MSG("Discover Tx char descriptors cmd failure: 0x%x\n", status);
        }
        UTIL_SEQ_WaitEvt(1 << CFG_IDLEEVT_GATT_PROC_COMPLETE);

        APP_DBG_MSG("GATT_PROC_DISC_TX_CHAR_DESC complete event received \n");
        break;

      default:
        break;
    }
    return;
  }


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