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

custom.c « App « STM32_WPAN « BLE_AT_Server « BLE « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63fee2ee98765c9408d18a535e3966e1782d06c1 (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
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : App/custom.c
  * Description        : Application file for BLE Middleware.
  *
  *****************************************************************************
  * @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.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

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

#include "app_common.h"

#include "dbg_trace.h"
#include "ble.h"
#include "tl.h"
#include "app_ble.h"

#include "stm32_seq.h"
#include "shci.h"
#include "stm32_lpm.h"
#include "otp.h"
#include "p2p_server_app.h"
#include "stm32wb_at_ble.h"
#include "ble_at_server.h"
#include "custom_app.h"
#include "custom.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */
typedef enum
{
  CUSTOMS_NOTIFICATION_MASK = (1 << 0),
  CUSTOMS_INDICATION_MASK = (1 << 1),
} ClientCharConfMask_t;

/* USER CODE END PTD */

/* Private defines -----------------------------------------------------------*/
#define APPBLE_GAP_DEVICE_NAME_LENGTH 7
#define FAST_ADV_TIMEOUT               (30*1000*1000/CFG_TS_TICK_VAL) /**< 30s */
#define INITIAL_ADV_TIMEOUT            (60*1000*1000/CFG_TS_TICK_VAL) /**< 60s */

#define BD_ADDR_SIZE_LOCAL    6

/* USER CODE BEGIN PD */
#define LED_ON_TIMEOUT                 (0.005*1000*1000/CFG_TS_TICK_VAL) /**< 5ms */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
extern BleApplicationContext_t BleApplicationContext;
extern Custom_Context_t Custom_Context;
/* USER CODE END PM */

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

/* USER CODE BEGIN PV */

/* USER CODE END PV */

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

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Functions Definition ------------------------------------------------------*/
 
static SVCCTL_EvtAckStatus_t Custom_Event_Handler(void *Event)
{
  hci_event_pckt *event_pckt;
  evt_blecore_aci *blecore_evt;
  aci_att_exchange_mtu_resp_event_rp0 *exchange_mtu_resp;
  aci_gatt_attribute_modified_event_rp0 *attribute_modified;
  tBleStatus result;
  SVCCTL_EvtAckStatus_t return_value;
  
  CUSTOMS_App_Notification_evt_t Notification;

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

  /* USER CODE BEGIN Custom_Event_Handler */

  /* USER CODE END Custom_Event_Handler */

  switch (event_pckt->evt)
  {
    case HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE:
      blecore_evt = (evt_blecore_aci*) event_pckt->data;
      /* USER CODE BEGIN EVT_VENDOR */

      /* USER CODE END EVT_VENDOR */
      switch (blecore_evt->ecode)
      {
      /* USER CODE BEGIN ecode */

        case EVT_BLUE_ATT_EXCHANGE_MTU_RESP:
        {
          printf("EVT_BLUE_ATT_EXCHANGE_MTU_RESP \n");
          exchange_mtu_resp = (aci_att_exchange_mtu_resp_event_rp0 *)blecore_evt->data;
          printf("MTU_size = %d \n",exchange_mtu_resp->Server_RX_MTU );
          result = hci_le_set_data_length(BleApplicationContext.BleApplicationContext_legacy.connectionHandle,251,2120);
          if (result != BLE_STATUS_SUCCESS)
          {
            printf("set data length command error %x \n", result);
          }
          else
          {
            printf("set data length command success \n");
          }
        }
        break;
        case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED:
        {
          uint8_t i;
          uint8_t id_svc;
          uint8_t id_char;
          
          attribute_modified = (aci_gatt_attribute_modified_event_rp0*) blecore_evt->data;
          
          for (id_svc=3;id_svc<6;id_svc++)
          {
            for (id_char=0;id_char<5;id_char++)
            {
              if (attribute_modified->Attr_Handle == (Custom_Context.CharHdle[id_svc-3][id_char-1] + 2))
              {
                return_value = SVCCTL_EvtAckFlowEnable;
                /**
                * Notify to application to start measurement
                */
                if (attribute_modified->Attr_Data[0] & CUSTOMS_NOTIFICATION_MASK)
                {
                  APP_DBG_MSG("svc: %d char:%d NOTIFICATION ENABLED \n", id_svc, id_char);
                  Notification.Evt_Opcode = CUSTOM_NOTIFICATION_ENABLED;
                  CUSTOMS_Notification(&Notification);
                }
                else if (attribute_modified->Attr_Data[0] & CUSTOMS_INDICATION_MASK)
                {
                  APP_DBG_MSG("svc: %d char:%d INDICATION ENABLED \n", id_svc, id_char);
                  Notification.Evt_Opcode = CUSTOM_INDICATION_ENABLED;
                  CUSTOMS_Notification(&Notification);
                }
                else
                {
                  Notification.Evt_Opcode = CUSTOM_INDICATION_DISABLED;
                  CUSTOMS_Notification(&Notification);
                }
              }
            }
          }
                   
          for (id_svc=3;id_svc<6;id_svc++)
          {
            for (id_char=0;id_char<5;id_char++)
            {
              if(attribute_modified->Attr_Handle == (Custom_Context.CharHdle[id_svc-3][id_char-1] + 1))
              {       
                return_value = SVCCTL_EvtAckFlowEnable;
                printf("WRITE CHARAC svc: %d char: %d - LENGTH = %d \n", id_svc, id_char, attribute_modified->Attr_Data_Length);
                printf("Reception : ");
                for (i = 0; i < attribute_modified->Attr_Data_Length; i++)
                {
                  printf("Data[%x] = 0x%x ", i, attribute_modified->Attr_Data[i]);
                }
                printf(" \r\n");
                Notification.service_index = id_svc;
                Notification.charac_index = id_char;
                Notification.length = attribute_modified->Attr_Data_Length;
                for (i = 0; i < attribute_modified->Attr_Data_Length; i++)
                {
                  Notification.charac_value[i] = attribute_modified->Attr_Data[i];
                }
                Notification.Evt_Opcode = CUSTOM_WRITE_EVT;
                CUSTOMS_Notification(&Notification);
              }
            }
          }
        }
        break;
        case ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE:
        {
            uint8_t i;
            uint8_t id_svc;
            uint8_t id_char;
            
            aci_gatt_write_permit_req_event_rp0 * write_perm_req;
            write_perm_req = (aci_gatt_write_permit_req_event_rp0*)blecore_evt->data;
            for (id_svc=3;id_svc<6;id_svc++)
            {
              for (id_char=0;id_char<5;id_char++)
              {
                if(write_perm_req->Attribute_Handle == (Custom_Context.CharHdle[id_svc-3][id_char-1] + 1))
                {     
                  return_value = SVCCTL_EvtAckFlowEnable;
                  printf("WRITE PERMIT REQ CHARAC svc: %d char: %d - LENGTH = %d \n", id_svc, id_char, write_perm_req->Data_Length);
                  printf("Reception : ");
                  for (i = 0; i < write_perm_req->Data_Length; i++)
                  {
                    printf("Data[%x] = 0x%x ", i, write_perm_req->Data[i]);
                  }
                  printf(" \r\n");
                  Notification.service_index = id_svc;
                  Notification.charac_index = id_char;
                  Notification.length = write_perm_req->Data_Length;
                  for (i = 0; i < write_perm_req->Data_Length; i++)
                  {
                    Notification.charac_value[i] = write_perm_req->Data[i];
                  }
                  return_value = SVCCTL_EvtAckFlowEnable;          
                  aci_gatt_write_resp(write_perm_req->Connection_Handle,
                                      write_perm_req->Attribute_Handle,
                                      0x00, /* write_status = 0 (no error))*/
                                      0x00, /* err_code */
                                      write_perm_req->Data_Length,
                                      (uint8_t *)&(write_perm_req->Data[0]));
                  Notification.Evt_Opcode = CUSTOM_WRITE_EVT;
                  CUSTOMS_Notification(&Notification);
                }
              }
            }
          }
          break;  
          
      /* USER CODE END ecode */
#if(RADIO_ACTIVITY_EVENT != 0)
        case ACI_HAL_END_OF_RADIO_ACTIVITY_VSEVT_CODE:
        /* USER CODE BEGIN RADIO_ACTIVITY_EVENT*/
          BSP_LED_On(LED_GREEN);
          HW_TS_Start(BleApplicationContext.SwitchOffGPIO_timer_Id, (uint32_t)LED_ON_TIMEOUT);
        /* USER CODE END RADIO_ACTIVITY_EVENT*/
          break; /* ACI_HAL_END_OF_RADIO_ACTIVITY_VSEVT_CODE */
#endif

      /* USER CODE BEGIN BLUE_EVT */

      /* USER CODE END BLUE_EVT */
      }
      break; /* HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE */

      /* USER CODE BEGIN EVENT_PCKT */

      /* USER CODE END EVENT_PCKT */

      default:
      /* USER CODE BEGIN ECODE_DEFAULT*/

      /* USER CODE END ECODE_DEFAULT*/
      break;
  }

  return(return_value);
}

/**
 * @brief  Service initialization
 * @param  None
 * @retval None
 */
void Custom_SVC_Init(void)
{
  /**
   *	Register the event handler to the BLE controller
   */
  SVCCTL_RegisterSvcHandler(Custom_Event_Handler);
        
  return;
}
/* USER CODE END FD*/

/* USER CODE END FD_SPECIFIC_FUNCTIONS */

/* USER CODE BEGIN FD_WRAP_FUNCTIONS */

/* USER CODE END FD_WRAP_FUNCTIONS */