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

dt_server_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: 80f418cabfec0aec7a035157dccc3e56a65259de (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
/**
  ******************************************************************************
 * @file    dt_server_app.c
 * @author  MCD Application Team
 * @brief   data throughput server Application
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2019-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 "app_common.h"
#include "dbg_trace.h"

#include "ble.h"
#include "app_ble.h"

#include "dt_server_app.h"
#include "dt_client_app.h"
#include "dts.h"

#include "stm32_seq.h"
#include "stm32_lpm.h"

#include "ble_common.h"
#include "ble_clock.h"

typedef enum
{
  DTS_APP_FLOW_OFF,
  DTS_APP_FLOW_ON
} DTS_App_Flow_Status_t;

typedef enum
{
  DTS_APP_TRANSFER_REQ_OFF,
  DTS_APP_TRANSFER_REQ_ON
} DTS_App_Transfer_Req_Status_t;

typedef struct
{
  DTS_STM_Payload_t TxData;
  DTS_App_Transfer_Req_Status_t NotificationTransferReq;
  DTS_App_Transfer_Req_Status_t ButtonTransferReq;
  DTS_App_Flow_Status_t DtFlowStatus;
} DTS_App_Context_t;

/* Private defines -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
DTS_App_Context_t DataTransferServerContext;
static uint8_t Notification_Data_Buffer[DATA_NOTIFICATION_MAX_PACKET_SIZE]; /* DATA_NOTIFICATION_MAX_PACKET_SIZE data + CRC */
uint32_t DataReceived;

/* Global variables ----------------------------------------------------------*/
/* Functions Definition ------------------------------------------------------*/
/* Private functions ----------------------------------------------------------*/
static void ButtonTriggerReceived(void);
static void DT_App_Button2_Trigger_Received( void );
static void DT_App_Button3_Trigger_Received( void );
static void SendData(void);
static void BLE_App_Delay_DataThroughput( void );
extern uint16_t Att_Mtu_Exchanged;
extern uint8_t TimerDataThroughputWrite_Id;

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

#define BOUNCE_THRESHOLD                20U
#define LONG_PRESS_THRESHOLD            1000U
/*************************************************************
 *
 * PUBLIC FUNCTIONS
 *
 *************************************************************/
void DTS_App_Init(void)
{
  uint8_t i;

  UTIL_SEQ_RegTask( 1<<CFG_TASK_BUTTON_ID, UTIL_SEQ_RFU, ButtonTriggerReceived);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_SW2_BUTTON_PUSHED_ID, UTIL_SEQ_RFU, DT_App_Button2_Trigger_Received);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_SW3_BUTTON_PUSHED_ID, UTIL_SEQ_RFU, DT_App_Button3_Trigger_Received);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_DATA_TRANSFER_UPDATE_ID, UTIL_SEQ_RFU, SendData);
  UTIL_SEQ_RegTask( 1<<CFG_TASK_DATA_WRITE_ID, UTIL_SEQ_RFU, BLE_App_Delay_DataThroughput);

  /**
   * Initialize data buffer
   */
  for (i=0 ; i<(DATA_NOTIFICATION_MAX_PACKET_SIZE-1) ; i++)
  {
    Notification_Data_Buffer[i] = i;
  }

  DataTransferServerContext.NotificationTransferReq = DTS_APP_TRANSFER_REQ_OFF;
  DataTransferServerContext.ButtonTransferReq = DTS_APP_TRANSFER_REQ_OFF;
  DataTransferServerContext.DtFlowStatus = DTS_APP_FLOW_ON;
}

void DTS_App_KeyButtonAction( void )
{
  UTIL_SEQ_SetTask(1 << CFG_TASK_BUTTON_ID, CFG_SCH_PRIO_0);
}

void DTS_App_KeyButton2Action( void )
{
    UTIL_SEQ_SetTask(1 << CFG_TASK_SW2_BUTTON_PUSHED_ID, CFG_SCH_PRIO_0);
}

void DTS_App_KeyButton3Action( void )
{
    UTIL_SEQ_SetTask(1 << CFG_TASK_SW3_BUTTON_PUSHED_ID, CFG_SCH_PRIO_0);
}

void DTS_App_TxPoolAvailableNotification(void)
{
  DataTransferServerContext.DtFlowStatus = DTS_APP_FLOW_ON;
  UTIL_SEQ_SetTask(1 << CFG_TASK_DATA_TRANSFER_UPDATE_ID, CFG_SCH_PRIO_0);

  return;
}

/*************************************************************
 *
 * CALLBACK FUNCTIONS
 *
 *************************************************************/
void DTS_Notification( DTS_STM_App_Notification_evt_t *pNotification )
{
  switch (pNotification->Evt_Opcode)
  {
    case DTS_STM__NOTIFICATION_ENABLED:
      DataTransferServerContext.NotificationTransferReq = DTS_APP_TRANSFER_REQ_ON;
      UTIL_SEQ_SetTask(1 << CFG_TASK_DATA_TRANSFER_UPDATE_ID, CFG_SCH_PRIO_0);
      break;

    case DTS_STM_NOTIFICATION_DISABLED:
      DataTransferServerContext.NotificationTransferReq = DTS_APP_TRANSFER_REQ_OFF;
      break;
      
    case DTC_NOTIFICATION_ENABLED:
      BLE_SVC_L2CAP_Conn_Update_7_5();
      //DataTransferServerContext.NotificationClientTransferFlag = 0x01;
      break;
      
    case DTC_NOTIFICATION_DISABLED:
      //DataTransferServerContext.NotificationClientTransferFlag = 0x00;
      APP_DBG_MSG("write data notification disabled \n");
      break;
      
    case DTS_STM_DATA_RECEIVED:
      if (DataReceived == 0)
      {
        /* start timer */
        DataReceived += pNotification->DataTransfered.Length;
        HW_TS_Start(TimerDataThroughputWrite_Id, DELAY_1s);
      }
      else
      {
        DataReceived += pNotification->DataTransfered.Length;
      }
      break;

    case DTS_STM_GATT_TX_POOL_AVAILABLE:
      DataTransferServerContext.DtFlowStatus = DTS_APP_FLOW_ON;
      UTIL_SEQ_SetTask(1 << CFG_TASK_DATA_TRANSFER_UPDATE_ID, CFG_SCH_PRIO_0);
      break;

    default:
      break;
  }

  return;
}

/*************************************************************
 *
 * LOCAL FUNCTIONS
 *
 *************************************************************/
static void SendData( void )
{
  tBleStatus status = BLE_STATUS_INVALID_PARAMS;
  uint8_t crc_result;

  if( (DataTransferServerContext.ButtonTransferReq != DTS_APP_TRANSFER_REQ_OFF)
      && (DataTransferServerContext.NotificationTransferReq != DTS_APP_TRANSFER_REQ_OFF)
      && (DataTransferServerContext.DtFlowStatus != DTS_APP_FLOW_OFF) )
  {   
    /*Data Packet to send to remote*/
    Notification_Data_Buffer[0] += 1;
    /* compute CRC */
    crc_result = APP_BLE_ComputeCRC8((uint8_t*) Notification_Data_Buffer, (DATA_NOTIFICATION_MAX_PACKET_SIZE - 1));
    Notification_Data_Buffer[DATA_NOTIFICATION_MAX_PACKET_SIZE - 1] = crc_result;

    DataTransferServerContext.TxData.pPayload = Notification_Data_Buffer;
    //DataTransferServerContext.TxData.Length = DATA_NOTIFICATION_MAX_PACKET_SIZE; /* DATA_NOTIFICATION_MAX_PACKET_SIZE */
    DataTransferServerContext.TxData.Length =  DATA_NOTIFICATION_MAX_PACKET_SIZE; //Att_Mtu_Exchanged-10;

    status = DTS_STM_UpdateChar(DATA_TRANSFER_TX_CHAR_UUID, (uint8_t *) &DataTransferServerContext.TxData);
    if (status == BLE_STATUS_INSUFFICIENT_RESOURCES)
    {
      DataTransferServerContext.DtFlowStatus = DTS_APP_FLOW_OFF;
      (Notification_Data_Buffer[0])-=1;
    }
    else
    {
      UTIL_SEQ_SetTask(1 << CFG_TASK_DATA_TRANSFER_UPDATE_ID, CFG_SCH_PRIO_0);
    }
  }
  return;
}
void Resume_Notification(void)
{
  DataTransferServerContext.DtFlowStatus = DTS_APP_FLOW_ON;
}
static void ButtonTriggerReceived( void )
{
  if(DataTransferServerContext.ButtonTransferReq != DTS_APP_TRANSFER_REQ_OFF)
  {
    BSP_LED_Off(LED_BLUE);
    DataTransferServerContext.ButtonTransferReq = DTS_APP_TRANSFER_REQ_OFF;
  }
  else
  {
    BSP_LED_On(LED_BLUE);
    DataTransferServerContext.ButtonTransferReq = DTS_APP_TRANSFER_REQ_ON;
    UTIL_SEQ_SetTask(1 << CFG_TASK_DATA_TRANSFER_UPDATE_ID, CFG_SCH_PRIO_0);
  }

  return;
}

static void DT_App_Button2_Trigger_Received( void )
{
  APP_DBG_MSG("**CHANGE PHY \n");
  BLE_SVC_GAP_Change_PHY();
  return;
}

static void Appli_UpdateButtonState(int isPressed)
{
  uint32_t t0 = 0,t1 = 1;

  t0 = Clock_Time(); /* SW3 press timing */
  
  while(BSP_PB_GetState(BUTTON_SW3) == BUTTON_PRESSED);
  t1 = Clock_Time(); /* SW3 release timing */
  
  if((t1 - t0) > LONG_PRESS_THRESHOLD)
  {
    /* Button 3 long press action */
    APP_DBG_MSG("clear database \n");
    BLE_SVC_GAP_Clear_DataBase();
  }
  else if((t1 - t0) > BOUNCE_THRESHOLD)
  {
    /* Button 3 short press action */
    APP_DBG_MSG("slave security request \n");
    BLE_SVC_GAP_Security_Req();
  }
}

static void DT_App_Button3_Trigger_Received(void)
{
  Appli_UpdateButtonState(BSP_PB_GetState(BUTTON_SW3) == BUTTON_PRESSED);
  APP_DBG_MSG("SW3 \n");
  return;
}

static void BLE_App_Delay_DataThroughput(void)
{
  uint32_t DataThroughput;
  DTS_STM_Payload_t ThroughputToSend; 
  
  DataThroughput = (uint32_t)(DataReceived/TIMEUNIT);
  APP_DBG_MSG("DataThroughput = %ld  bytes/s\n", DataThroughput);
  
  ThroughputToSend.Length = 4;
  ThroughputToSend.pPayload = (uint8_t*)&DataThroughput;
  
  DTS_STM_UpdateCharThroughput( (DTS_STM_Payload_t*) &ThroughputToSend);  
  DataReceived = 0;
}