1 /*******************************************************************************
2  * @file  rsi_bt_ble.c
3  *******************************************************************************
4  * # License
5  * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
6  *******************************************************************************
7  *
8  * SPDX-License-Identifier: Zlib
9  *
10  * The licensor of this software is Silicon Laboratories Inc.
11  *
12  * This software is provided 'as-is', without any express or implied
13  * warranty. In no event will the authors be held liable for any damages
14  * arising from the use of this software.
15  *
16  * Permission is granted to anyone to use this software for any purpose,
17  * including commercial applications, and to alter it and redistribute it
18  * freely, subject to the following restrictions:
19  *
20  * 1. The origin of this software must not be misrepresented; you must not
21  *    claim that you wrote the original software. If you use this software
22  *    in a product, an acknowledgment in the product documentation would be
23  *    appreciated but is not required.
24  * 2. Altered source versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.
26  * 3. This notice may not be removed or altered from any source distribution.
27  *
28  ******************************************************************************/
29 #include "rsi_bt_common_config.h"
30 
31 #include "rsi_common.h"
32 #include "cmsis_os2.h"
33 
34 #include "sl_wifi_host_interface.h"
35 #include "sl_si91x_driver.h"
36 
37 #include "rsi_bt_common.h"
38 #include "rsi_ble.h"
39 #include "stdio.h"
40 
41 #include "sl_si91x_host_interface.h"
42 
43 sl_status_t sli_si91x_allocate_command_buffer(sl_wifi_buffer_t **host_buffer,
44                                               void **buffer,
45                                               uint32_t requested_buffer_size,
46                                               uint32_t wait_duration_ms);
47 uint32_t rsi_get_bt_state(const rsi_bt_cb_t *bt_cb);
48 
49 #define BT_SEM     0x1
50 #define BT_CMD_SEM 0x2
51 
52 /*
53  * Global Variables
54  * */
55 
56 // rsi_bt_ble.c function declarations
57 void rsi_bt_common_register_callbacks(rsi_bt_get_ber_pkt_t rsi_bt_get_ber_pkt_from_app);
58 uint32_t rsi_bt_get_timeout(uint16_t cmd_type, uint16_t protocol_type);
59 uint32_t rsi_bt_get_status(const rsi_bt_cb_t *bt_cb);
60 void rsi_ble_update_le_dev_buf(const rsi_ble_event_le_dev_buf_ind_t *rsi_ble_event_le_dev_buf_ind);
61 void rsi_add_remote_ble_dev_info(const rsi_ble_event_enhance_conn_status_t *remote_dev_info);
62 void rsi_remove_remote_ble_dev_info(const rsi_ble_event_disconnect_t *remote_dev_info);
63 int32_t rsi_driver_process_bt_resp(
64   rsi_bt_cb_t *bt_cb,
65   sl_si91x_packet_t *pkt,
66   void (*rsi_bt_async_callback_handler)(rsi_bt_cb_t *cb, uint16_t type, uint8_t *data, uint16_t length),
67   uint16_t protocol_type);
68 void rsi_ble_on_chip_memory_status_callbacks_register(chip_ble_buffers_stats_handler_t ble_on_chip_memory_status_event);
69 uint16_t rsi_bt_prepare_common_pkt(uint16_t cmd_type, void *cmd_struct, sl_si91x_packet_t *pkt);
70 uint16_t rsi_bt_prepare_le_pkt(uint16_t cmd_type, void *cmd_struct, sl_si91x_packet_t *pkt);
71 
72 /*
73  Global Variables
74  */
75 /** @addtogroup DRIVER14
76 * @{
77 */
78 /**
79  * @brief       Register the bt-common callbacks
80  * @param[in]   rsi_bt_get_ber_pkt_from_app - BER Call back
81  * @return      void
82  *
83  *
84  */
85 
rsi_bt_common_register_callbacks(rsi_bt_get_ber_pkt_t rsi_bt_get_ber_pkt_from_app)86 void rsi_bt_common_register_callbacks(rsi_bt_get_ber_pkt_t rsi_bt_get_ber_pkt_from_app)
87 {
88   // Get bt cb struct pointer
89   rsi_bt_common_specific_cb_t *bt_common_specific_cb = rsi_driver_cb->bt_global_cb->bt_common_specific_cb;
90 
91   // Assign the call backs to the respective call back
92   bt_common_specific_cb->rsi_bt_get_ber_pkt = rsi_bt_get_ber_pkt_from_app;
93 }
94 
95 /**
96  * @brief       Determine the BT protocol (BT COMMON / BT classic / BLE ) using the packet type
97  * @param[in]   rsp_type - Packet type
98  * @param[in]   bt_cb    - BT control back
99  * @return      0              - Success \n
100  *              Non-Zero Value - Failure
101  *
102  *
103  */
104 
rsi_bt_get_proto_type(uint16_t rsp_type,rsi_bt_cb_t ** bt_cb)105 uint16_t rsi_bt_get_proto_type(uint16_t rsp_type, rsi_bt_cb_t **bt_cb)
106 {
107   SL_PRINTF(SL_RSI_BT_GET_PROTO_TYPE_TRIGGER, BLUETOOTH, LOG_INFO, "RESPONSE_TYPE: %2x", rsp_type);
108   uint16_t return_value = 0xFF;
109 
110   if (rsp_type == RSI_BLE_EVENT_DISCONNECT) {
111     if (rsi_driver_cb->bt_common_cb->dev_type == RSI_BT_LE_DEVICE) {
112       return_value = RSI_PROTO_BLE;
113       *bt_cb       = rsi_driver_cb->ble_cb;
114     }
115 
116     return return_value;
117   }
118   /** @} */
119   // Determine the protocol type by looking at the packet type
120   if ((rsp_type == RSI_BT_EVENT_CARD_READY)
121       || ((rsp_type >= RSI_BT_SET_LOCAL_NAME) && (rsp_type <= RSI_BT_GET_LOCAL_DEV_ADDR))
122       || ((rsp_type >= RSI_BT_REQ_INIT) && (rsp_type <= RSI_BT_SET_ANTENNA_SELECT))
123       || (rsp_type == RSI_BT_SET_FEATURES_BITMAP) || (rsp_type == RSI_BT_SET_ANTENNA_TX_POWER_LEVEL)
124       || (rsp_type == RSI_BT_SET_BD_ADDR_REQ) || (rsp_type == RSI_BLE_ONLY_OPER_MODE)
125       || (rsp_type == RSI_BLE_REQ_PWRMODE) || (rsp_type == RSI_BLE_REQ_SOFTRESET) || (rsp_type == RSI_BT_REQ_PER_CMD)
126       || (rsp_type == RSI_BT_VENDOR_SPECIFIC) || (rsp_type == RSI_BT_GET_BT_STACK_VERSION)
127       || (rsp_type == RSI_BT_SET_GAIN_TABLE_OFFSET_OR_MAX_POWER_UPDATE)) {
128     return_value = RSI_PROTO_BT_COMMON;
129     *bt_cb       = rsi_driver_cb->bt_common_cb;
130   } else if (((rsp_type >= RSI_BLE_REQ_ADV) && (rsp_type <= RSI_BLE_REQ_EXECUTE_WRITE))
131              || ((rsp_type >= RSI_BLE_ADD_SERVICE) && (rsp_type <= RSI_BLE_CMD_INDICATE))
132              || (rsp_type == RSI_BLE_SET_ADVERTISE_DATA)
133              || ((rsp_type >= RSI_BLE_GET_LE_PING) && (rsp_type <= RSI_BLE_CMD_READ_RESP))
134              || (rsp_type == RSI_BLE_SET_SCAN_RESPONSE_DATA)
135              || ((rsp_type >= RSI_BLE_LE_ACCEPT_LIST) && (rsp_type <= RSI_BLE_CBFC_DISCONN))
136              || ((rsp_type >= RSI_BLE_LE_LTK_REQ_REPLY) && (rsp_type <= RSI_BLE_PER_RX_MODE))
137              || (rsp_type == RSI_BLE_CMD_ATT_ERROR) || (rsp_type == RSI_BLE_CMD_SET_BLE_TX_POWER)
138              || (rsp_type == RSI_BLE_CMD_INDICATE_SYNC) || (rsp_type == RSI_BLE_CMD_AE)
139              || ((rsp_type >= RSI_BLE_REQ_PROFILES_ASYNC) && (rsp_type <= RSI_BLE_EXECUTE_LONGDESCWRITE_ASYNC))
140              || (rsp_type == RSI_BLE_SET_SMP_PAIRING_CAPABILITY_DATA) || (rsp_type == RSI_BLE_REQ_SMP_PAIRING_FAILED)
141              || (rsp_type == RSI_BLE_REQ_HCI_RAW) || (rsp_type == RSI_BLE_EVENT_RCP_DATA_RCVD)
142              || ((rsp_type >= RSI_BLE_CONN_PARAM_RESP_CMD) && (rsp_type <= RSI_BLE_CMD_MTU_EXCHANGE_RESP))
143              || ((rsp_type >= RSI_BLE_EVENT_GATT_ERROR_RESPONSE) && (rsp_type <= RSI_BLE_EVENT_SCAN_REQ_RECVD))
144              || (rsp_type == RSI_BLE_REQ_CONN_ENHANCE) || (rsp_type == RSI_BLE_EVENT_REMOTE_DEVICE_INFORMATION)
145              || ((rsp_type >= RSI_BLE_CMD_READ_TRANSMIT_POWER) && (rsp_type <= RSI_BLE_CMD_WRITE_RF_PATH_COMP))) {
146 
147     return_value = RSI_PROTO_BLE;
148     *bt_cb       = rsi_driver_cb->ble_cb;
149   }
150 
151   return return_value;
152 }
153 /** @addtogroup DRIVER14
154 * @{
155 */
156 
157 /**
158  * @brief      Calculate semaphore wait time for a protocol (BT COMMON / BT classic / BLE )
159  * using the packet type
160  * @param[in]  cmd_type      - Command Type
161  * @param[in]  protocol_type - Protocol type , whether it is BT Common/BT Classic/BLE
162  * @return     0              - Success \n
163  *             Non-Zero Value - Failure
164  *
165  *
166  */
167 
rsi_bt_get_timeout(uint16_t cmd_type,uint16_t protocol_type)168 uint32_t rsi_bt_get_timeout(uint16_t cmd_type, uint16_t protocol_type)
169 {
170 
171   SL_PRINTF(SL_RSI_BT_GET_TIMEOUT_TRIGGER,
172             BLUETOOTH,
173             LOG_INFO,
174             "COMMAND_TYPE: %2x, PROTOCOL_TYPE: %2x",
175             cmd_type,
176             protocol_type);
177   uint32_t return_value = 0; // 0 means RSI_WAIT_FOREVER
178 
179   switch (protocol_type) {
180     case RSI_PROTO_BT_COMMON: {
181       return_value = RSI_BT_COMMON_CMD_RESP_WAIT_TIME;
182     } break;
183     case RSI_PROTO_BLE: {
184       if (((cmd_type >= RSI_BLE_REQ_ADV) && (cmd_type <= RSI_BLE_SMP_PASSKEY))
185           || ((cmd_type >= RSI_BLE_SET_ADVERTISE_DATA) && (cmd_type <= RSI_BLE_PER_RX_MODE))
186           || (cmd_type == RSI_BLE_CONN_PARAM_RESP_CMD)
187           || ((cmd_type == RSI_BLE_MTU_EXCHANGE_REQUEST) || (cmd_type == RSI_BLE_CMD_MTU_EXCHANGE_RESP))) {
188         return_value = RSI_BLE_GAP_CMD_RESP_WAIT_TIME;
189       } else if (((cmd_type >= RSI_BLE_REQ_PROFILES) && (cmd_type <= RSI_BLE_CMD_INDICATE))
190                  || ((cmd_type >= RSI_BLE_CMD_ATT_ERROR) && (cmd_type <= RSI_BLE_SET_SMP_PAIRING_CAPABILITY_DATA))
191                  || (cmd_type == RSI_BLE_CMD_INDICATE_CONFIRMATION) || (cmd_type == RSI_BLE_CMD_INDICATE_SYNC)) {
192         return_value = RSI_BLE_GATT_CMD_RESP_WAIT_TIME;
193       } else {
194         return_value = RSI_BT_BLE_CMD_MAX_RESP_WAIT_TIME;
195       }
196     } break;
197 
198     default: {
199       return_value = RSI_BT_BLE_CMD_MAX_RESP_WAIT_TIME;
200     } break;
201   }
202   return return_value;
203 }
204 
205 /**
206  * @brief       Handle BT data transfer completion.
207  * @param[in]   pkt - Pointer to packet
208  * @return      void
209  */
210 
rsi_bt_common_tx_done(sl_si91x_packet_t * pkt)211 void rsi_bt_common_tx_done(sl_si91x_packet_t *pkt)
212 {
213 
214   SL_PRINTF(SL_RSI_BT_COMMON_TX_DONE, BLUETOOTH, LOG_INFO);
215   const uint8_t *host_desc = NULL;
216   uint8_t protocol_type    = 0;
217   uint16_t rsp_type        = 0;
218   rsi_bt_cb_t *bt_cb       = NULL;
219 
220   // Get Host Descriptor
221   host_desc = pkt->desc;
222 
223   // Get Command response Type
224   rsp_type = rsi_bytes2R_to_uint16(host_desc + RSI_BT_RSP_TYPE_OFFSET);
225 
226   // Get the protocol Type
227   protocol_type = (uint8_t)rsi_bt_get_proto_type(rsp_type, &bt_cb);
228 
229   if (protocol_type == 0xFF) {
230     return;
231   }
232 
233   // If the command is not a synchronous/blocking one
234   if (!bt_cb->sync_rsp) {
235     // Set bt_common status as success
236     rsi_bt_set_status(bt_cb, RSI_SUCCESS);
237 
238     // Post the semaphore which is waiting on driver_send API
239     osSemaphoreRelease(bt_cb->bt_sem);
240   }
241 }
242 /** @} */
243 /** @addtogroup DRIVER14
244 * @{
245 */
246 /**
247  *
248  * @brief      Return BT status
249  * @param[in]  bt_cb - BT control block
250  * @return     0              - Success \n
251  *             Non-Zero Value - Failure
252  */
253 
rsi_get_bt_state(const rsi_bt_cb_t * bt_cb)254 uint32_t rsi_get_bt_state(const rsi_bt_cb_t *bt_cb)
255 {
256   SL_PRINTF(SL_RSI_BT_STATE_TRIGGER, BLUETOOTH, LOG_INFO);
257   return bt_cb->state;
258 }
259 
260 /*==============================================*/
261 /**
262  * @brief       Set BT status
263  * @param[in]   bt_cb  - BT control block
264  * @param[in]   status - Status value to be set
265  * @param[out]  None
266  * @return      void
267  */
268 
rsi_bt_set_status(rsi_bt_cb_t * bt_cb,int32_t status)269 void rsi_bt_set_status(rsi_bt_cb_t *bt_cb, int32_t status)
270 {
271 
272   SL_PRINTF(SL_RSI_BT_SET_STATUS_TRIGGER, BLUETOOTH, LOG_INFO, "STATUS: %4x", status);
273   bt_cb->status = status;
274 }
275 
276 /*==============================================*/
277 /**
278  * @brief       Get bt status
279  * @param[in]   bt_cb - BT control block
280  * @return      0              - Success \n
281  *              Non-Zero Value - Failure
282  */
rsi_bt_get_status(const rsi_bt_cb_t * bt_cb)283 uint32_t rsi_bt_get_status(const rsi_bt_cb_t *bt_cb)
284 {
285   return bt_cb->status;
286 }
287 
288 /**
289  * @brief       Update local Device buffer availability per peripheral in global ble cb structure
290  * @param[in]   void
291  * @return      void
292  *
293  */
294 
rsi_ble_update_le_dev_buf(const rsi_ble_event_le_dev_buf_ind_t * rsi_ble_event_le_dev_buf_ind)295 void rsi_ble_update_le_dev_buf(const rsi_ble_event_le_dev_buf_ind_t *rsi_ble_event_le_dev_buf_ind)
296 {
297 
298   SL_PRINTF(SL_RSI_BT_UPDATE_LE_DEV_BUF_TRIGGER, BLUETOOTH, LOG_INFO);
299   rsi_bt_cb_t *le_cb = rsi_driver_cb->ble_cb;
300 
301   for (uint8_t inx = 0; inx < (RSI_BLE_MAX_NBR_PERIPHERALS + RSI_BLE_MAX_NBR_CENTRALS); inx++) {
302     if (!memcmp(rsi_ble_event_le_dev_buf_ind->remote_dev_bd_addr,
303                 le_cb->remote_ble_info[inx].remote_dev_bd_addr,
304                 RSI_DEV_ADDR_LEN)) {
305       if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
306         osMutexAcquire(le_cb->remote_ble_info[inx].ble_buff_mutex, 0xFFFFFFFFUL);
307       }
308 
309       le_cb->remote_ble_info[inx].avail_buf_cnt += rsi_ble_event_le_dev_buf_ind->avail_buf_cnt;
310       if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
311         osMutexRelease(le_cb->remote_ble_info[inx].ble_buff_mutex);
312       }
313       break;
314     }
315   }
316 }
317 
318 /**
319  * @brief       Update Remote BLE Device info in global ble cb structure
320  * @param[in]   void
321  * @return      void
322  *
323  *
324  */
325 
rsi_add_remote_ble_dev_info(const rsi_ble_event_enhance_conn_status_t * remote_dev_info)326 void rsi_add_remote_ble_dev_info(const rsi_ble_event_enhance_conn_status_t *remote_dev_info)
327 {
328 
329   SL_PRINTF(SL_RSI_ADD_REMOTE_BLE_DEV_INFO_TRIGGER, BLUETOOTH, LOG_INFO);
330   rsi_bt_cb_t *le_cb = rsi_driver_cb->ble_cb;
331 
332   for (uint8_t inx = 0; inx < (RSI_BLE_MAX_NBR_PERIPHERALS + RSI_BLE_MAX_NBR_CENTRALS); inx++) {
333     if (!le_cb->remote_ble_info[inx].used) {
334       memcpy(le_cb->remote_ble_info[inx].remote_dev_bd_addr, remote_dev_info->dev_addr, RSI_DEV_ADDR_LEN);
335       le_cb->remote_ble_info[inx].used = 1;
336       /* On connection default values are set as follows*/
337       le_cb->remote_ble_info[inx].max_buf_cnt    = 1;
338       le_cb->remote_ble_info[inx].avail_buf_cnt  = 1;
339       le_cb->remote_ble_info[inx].mode           = 1;
340       le_cb->remote_ble_info[inx].ble_buff_mutex = osMutexNew(NULL);
341       break;
342     }
343   }
344 }
345 
346 /**
347  * @brief       Remove Remote BLE Device info in global ble cb structure
348  * @param[in]   remote_dev_info - Remote device information
349  * @return      void
350  *
351  *
352  */
353 
rsi_remove_remote_ble_dev_info(const rsi_ble_event_disconnect_t * remote_dev_info)354 void rsi_remove_remote_ble_dev_info(const rsi_ble_event_disconnect_t *remote_dev_info)
355 {
356 
357   SL_PRINTF(SL_RSI_REMOVE_REMOTE_BLE_DEV_INFO_TRIGGER, BLUETOOTH, LOG_INFO);
358   rsi_bt_cb_t *le_cb = rsi_driver_cb->ble_cb;
359 
360   for (uint8_t inx = 0; inx < (RSI_BLE_MAX_NBR_PERIPHERALS + RSI_BLE_MAX_NBR_CENTRALS); inx++) {
361     if (!memcmp(remote_dev_info->dev_addr, le_cb->remote_ble_info[inx].remote_dev_bd_addr, RSI_DEV_ADDR_LEN)) {
362       memset(le_cb->remote_ble_info[inx].remote_dev_bd_addr, 0, RSI_DEV_ADDR_LEN);
363       le_cb->remote_ble_info[inx].used                 = 0;
364       le_cb->remote_ble_info[inx].avail_buf_cnt        = 0;
365       le_cb->remote_ble_info[inx].cmd_in_use           = 0;
366       le_cb->remote_ble_info[inx].max_buf_cnt          = 0;
367       le_cb->remote_ble_info[inx].expected_resp        = 0;
368       le_cb->remote_ble_info[inx].mode                 = 0;
369       le_cb->remote_ble_info[inx].remote_dev_addr_type = 0;
370       le_cb->remote_ble_info[inx].max_buf_len          = 0;
371       if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
372         osMutexDelete(le_cb->remote_ble_info[inx].ble_buff_mutex);
373       }
374       break;
375     }
376   }
377 }
378 
379 /**
380  * @brief       Process BT RX packets
381  * @param[in ]  bt_cb    - BT control block
382  * @param[in]   pkt      - Pointer to received RX packet
383  * @param[in]   bt_aync_callback_handler
384  * @return      0              - Success \n
385  *              Non-Zero Value - Failure
386  *
387  */
388 
rsi_driver_process_bt_resp(rsi_bt_cb_t * bt_cb,sl_si91x_packet_t * pkt,void (* rsi_bt_async_callback_handler)(rsi_bt_cb_t * cb,uint16_t type,uint8_t * data,uint16_t length),uint16_t protocol_type)389 int32_t rsi_driver_process_bt_resp(
390   rsi_bt_cb_t *bt_cb,
391   sl_si91x_packet_t *pkt,
392   void (*rsi_bt_async_callback_handler)(rsi_bt_cb_t *cb, uint16_t type, uint8_t *data, uint16_t length),
393   uint16_t protocol_type)
394 {
395   UNUSED_PARAMETER(protocol_type);
396 
397   SL_PRINTF(SL_RSI_DRIVER_PROCESS_BT_RESPONSE_TRIGGER, BLUETOOTH, LOG_INFO, "PROTOCOL_TYPE: %2x", protocol_type);
398   uint16_t rsp_type        = 0;
399   int16_t status           = RSI_SUCCESS;
400   const uint8_t *host_desc = NULL;
401   uint8_t *payload;
402   uint16_t payload_length;
403   uint16_t expected_resp = 0;
404 
405   // Get Host Descriptor
406   host_desc = pkt->desc;
407 
408   // Get Command response Type
409   rsp_type = rsi_bytes2R_to_uint16(host_desc + RSI_BT_RSP_TYPE_OFFSET);
410 
411   // Get Payload start pointer
412   payload = pkt->data;
413 
414   // Get Payload length
415   payload_length = (rsi_bytes2R_to_uint16(host_desc) & 0xFFF);
416 
417   // Get Status
418   status = rsi_bytes2R_to_uint16(host_desc + RSI_BT_STATUS_OFFSET);
419 
420   // Check bt_cb for any task is waiting for response
421   if (bt_cb->expected_response_type == rsp_type) {
422     // Update the status in bt_cb
423     rsi_bt_set_status(bt_cb, status);
424     if (bt_cb->expected_response_type == RSI_BT_EVENT_CARD_READY) {
425       bt_cb->state = RSI_BT_STATE_OPERMODE_DONE;
426     }
427 
428     //To not allow BT SetAddress after these states are triggered
429     if ((status == RSI_SUCCESS)
430         && (bt_cb->expected_response_type == RSI_BLE_REQ_ADV || bt_cb->expected_response_type == RSI_BLE_REQ_SCAN
431             || bt_cb->expected_response_type == RSI_BLE_REQ_CONN)) {
432       rsi_driver_cb->bt_common_cb->state = RSI_BT_STATE_NONE;
433     }
434     expected_resp = bt_cb->expected_response_type;
435     // Clear expected response type
436     bt_cb->expected_response_type = 0;
437 
438     // Copy the expected response to response structure/buffer, if any, passed in API
439     if (bt_cb->expected_response_buffer != NULL) {
440       memcpy(bt_cb->expected_response_buffer, payload, payload_length);
441 
442       // Save expected_response pointer to a local variable, since it is being cleared below
443       payload = bt_cb->expected_response_buffer;
444 
445       // Clear the expected response pointer
446       bt_cb->expected_response_buffer = NULL;
447     }
448 
449     // Check if it is sync response
450     if (bt_cb->sync_rsp) {
451 
452       /* handling this for the new buf configuration */
453       if ((expected_resp == RSI_BLE_RSP_SET_WWO_RESP_NOTIFY_BUF_INFO) && (status == RSI_SUCCESS)) {
454         const rsi_ble_set_wo_resp_notify_buf_info_t *buf_info = (rsi_ble_set_wo_resp_notify_buf_info_t *)payload;
455 
456         bt_cb->remote_ble_info[bt_cb->remote_ble_index].mode = buf_info->buf_mode;
457 
458         if (buf_info->buf_mode == 0) /* small buf cnt */
459         {
460           bt_cb->remote_ble_info[bt_cb->remote_ble_index].max_buf_cnt   = (buf_info->buf_count * 10);
461           bt_cb->remote_ble_info[bt_cb->remote_ble_index].avail_buf_cnt = (buf_info->buf_count * 10);
462         } else /* big buf cnt */
463         {
464           bt_cb->remote_ble_info[bt_cb->remote_ble_index].max_buf_cnt   = buf_info->buf_count;
465           bt_cb->remote_ble_info[bt_cb->remote_ble_index].avail_buf_cnt = buf_info->buf_count;
466         }
467         bt_cb->remote_ble_index = 0; /* assigning value to 0 after successful response */
468       }
469       // Signal the bt semaphore
470       osSemaphoreRelease(bt_cb->bt_sem);
471     } else if (rsi_bt_async_callback_handler != NULL) {
472 
473       bt_cb->async_status = status;
474       // Call callbacks handler
475       rsi_bt_async_callback_handler(bt_cb, rsp_type, payload, payload_length);
476     }
477   } else if (rsi_bt_async_callback_handler != NULL) {
478     bt_cb->async_status = status;
479     // Call callbacks handler
480     rsi_bt_async_callback_handler(bt_cb, rsp_type, payload, payload_length);
481   }
482 
483   return status;
484 }
485 
486 /**
487  * @brief       Process BT RX packets
488  * @param[in]   pkt      - Pointer to received RX packet
489  * @return      0              - Success \n
490  *              Non-Zero Value - Failure
491  */
492 
rsi_driver_process_bt_resp_handler(void * rx_pkt)493 uint16_t rsi_driver_process_bt_resp_handler(void *rx_pkt)
494 {
495 
496   SL_PRINTF(SL_RSI_DRIVER_PROCESS_BT_RESP_HANDLER_TRIGGER, BLUETOOTH, LOG_INFO);
497   sl_si91x_packet_t *pkt                      = (sl_si91x_packet_t *)rx_pkt;
498   const uint8_t *host_desc                    = NULL;
499   uint8_t protocol_type                       = 0;
500   uint16_t rsp_type                           = 0;
501   int16_t status                              = RSI_SUCCESS;
502   rsi_bt_cb_t *bt_cb                          = NULL;
503   const rsi_ble_event_disconnect_t *temp_data = NULL;
504 
505   // Get Host Descriptor
506   host_desc = pkt->desc;
507 
508   // Get Command response Type
509   rsp_type = rsi_bytes2R_to_uint16(host_desc + RSI_BT_RSP_TYPE_OFFSET);
510 
511   if (rsp_type == RSI_BLE_EVENT_DISCONNECT) {
512 
513     // rsi_driver_cb->bt_common_cb->dev_type = ((rsi_ble_event_disconnect_t *)pkt->data)->dev_type;
514     temp_data = (rsi_ble_event_disconnect_t *)pkt->data;
515     rsi_driver_cb->bt_common_cb->dev_type =
516       ((temp_data->dev_type) & LOWERNIBBLE); //Getting the dev_type from lower nibble
517   }
518 
519   // Get the protocol Type
520   protocol_type = (uint8_t)rsi_bt_get_proto_type(rsp_type, &bt_cb);
521 
522   SL_PRINTF(SL_RSI_BT_DRIVER_PROCESS_BT_RESP_HANDLER_TRIGGER, BLUETOOTH, LOG_INFO, "PROTOCOL_TYPE: %1x", protocol_type);
523   if (protocol_type == 0xFF) {
524     return 0;
525   }
526   // Call the corresponding protocol process rsp handler
527   if (protocol_type == RSI_PROTO_BT_COMMON) {
528     // Call BT common process rsp handler
529     status = (int16_t)rsi_driver_process_bt_resp(bt_cb, pkt, NULL, protocol_type);
530   } else {
531     // Call BLE process response handler
532     status = (int16_t)rsi_driver_process_bt_resp(bt_cb, pkt, rsi_ble_callbacks_handler, protocol_type);
533   }
534   return status;
535 }
536 
537 /**
538  * @brief      Initialize bt control block structure
539  * @param[in]  bt_cb - Pointer to bt_cb structure
540  * @param[in]  protocol_type - Protocol type
541  * @return     0              - Success \n
542  *             Non-Zero Value - Failure
543  *
544  */
545 
rsi_bt_cb_init(rsi_bt_cb_t * bt_cb,uint16_t protocol_type)546 int8_t rsi_bt_cb_init(rsi_bt_cb_t *bt_cb, uint16_t protocol_type)
547 {
548   UNUSED_PARAMETER(protocol_type);
549 
550   SL_PRINTF(SL_RSI_BT_CB_INIT_TRIGGER, BLUETOOTH, LOG_INFO, "PROTOCOL_TYPE: %2x", protocol_type);
551 
552   int8_t retval = 0;
553 
554   // validate input parameter
555   if (bt_cb == NULL) {
556     return RSI_ERROR_INVALID_PARAM;
557   }
558 
559   // Initialize bt control block with default values
560   bt_cb->state  = 0;
561   bt_cb->status = 0;
562 
563   // Create bt mutex
564   bt_cb->expected_response_type   = 0;
565   bt_cb->expected_response_buffer = NULL;
566 
567   // Create common/bt/ble sync semaphore
568   bt_cb->bt_sem = osSemaphoreNew(1, 0, NULL);
569   if (bt_cb->bt_sem == NULL) {
570     retval = RSI_ERROR_SEMAPHORE_CREATE_FAILED;
571   }
572 
573   // Create common/bt/ble command semaphore
574   bt_cb->bt_cmd_sem = osSemaphoreNew(1, 0, NULL);
575   if (bt_cb->bt_cmd_sem == NULL) {
576     retval = RSI_ERROR_SEMAPHORE_CREATE_FAILED;
577   }
578 
579   osSemaphoreRelease(bt_cb->bt_cmd_sem);
580   bt_cb->app_buffer = 0;
581 
582   return retval;
583 }
584 
585 /**
586  * @brief      Initialize bt global control block
587  * @param[in]  driver_cb - Pointer to bt_cb structure
588  * @param[in]  buffer - Buffer
589  * @return     0              - Success \n
590  *             Non-Zero Value - Failure
591  *
592  */
rsi_bt_global_cb_init(rsi_driver_cb_t * driver_cb,uint8_t * buffer)593 uint16_t rsi_bt_global_cb_init(rsi_driver_cb_t *driver_cb, uint8_t *buffer)
594 {
595 
596   SL_PRINTF(SL_RSI_BT_GLOBAL_CB_INIT_TRIGGER, BLUETOOTH, LOG_INFO);
597   rsi_bt_global_cb_t *bt_global_cb = driver_cb->bt_global_cb;
598   uint16_t total_size              = 0;
599 
600   bt_global_cb->ble_specific_cb = (rsi_ble_cb_t *)buffer;
601 
602   driver_cb->ble_cb->bt_global_cb = bt_global_cb;
603   total_size += sizeof(rsi_ble_cb_t);
604 
605   return total_size;
606 }
607 /** @} */
608 /** @addtogroup DRIVER14
609 * @{
610 */
611 /**
612  * @brief      Wait for BT card ready
613  * @param[in]  void
614  * @return     void
615  * */
616 
rsi_bt_common_init(void)617 void rsi_bt_common_init(void)
618 {
619   // Get bt_common_cb structure pointer
620   rsi_bt_cb_t *bt_common_cb = rsi_driver_cb->bt_common_cb;
621 
622   // Save expected response type
623   bt_common_cb->expected_response_type = RSI_BT_EVENT_CARD_READY;
624   bt_common_cb->sync_rsp               = 1;
625 
626   // Wait on BLE semaphore
627   if (bt_common_cb->bt_sem) {
628     osSemaphoreAcquire(bt_common_cb->bt_sem, osWaitForever);
629   }
630 
631   // BT card ready is received
632 }
633 /** @} */
634 
635 /** @addtogroup BT-LOW-ENERGY7
636 * @{
637 */
638 /*==============================================*/
639 /**
640  * @brief      Register GAP callbacks.
641  * @param[in]  ble_on_adv_report_event                  - Callback function for Advertise events
642  * @param[in]  ble_on_connect                           - Callback function for Connect events
643  * @param[in]  ble_on_disconnect                        - Callback function for Disconnect events
644  * @param[in]  timeout_expired_event                    - Callback function for le ping timeout events
645  * @param[in]  ble_on_phy_update_complete_event         - Callback function for phy update complete events
646  * @param[in]  ble_on_data_length_update_complete_event - Callback function for data length update events
647  * @param[in]  ble_on_enhance_conn_status_event         - Callback function for enhanced connection status events
648  * @param[in]  ble_on_directed_adv_report_event         - Callback function for directed advertiseing report events
649  * @param[in]  ble_on_conn_update_complete_event        - Callback function for conn update complete events
650  * @param[in]  ble_on_remote_conn_params_request_event  - Callback function to remote conn params request events
651  * @return      void
652  */
653 
rsi_ble_gap_register_callbacks(rsi_ble_on_adv_report_event_t ble_on_adv_report_event,rsi_ble_on_connect_t ble_on_conn_status_event,rsi_ble_on_disconnect_t ble_on_disconnect_event,rsi_ble_on_le_ping_payload_timeout_t ble_on_le_ping_time_expired_event,rsi_ble_on_phy_update_complete_t ble_on_phy_update_complete_event,rsi_ble_on_data_length_update_t rsi_ble_on_data_length_update_event,rsi_ble_on_enhance_connect_t ble_on_enhance_conn_status_event,rsi_ble_on_directed_adv_report_event_t ble_on_directed_adv_report_event,rsi_ble_on_conn_update_complete_t ble_on_conn_update_complete_event,rsi_ble_on_remote_conn_params_request_t ble_on_remote_conn_params_request_event)654 void rsi_ble_gap_register_callbacks(rsi_ble_on_adv_report_event_t ble_on_adv_report_event,
655                                     rsi_ble_on_connect_t ble_on_conn_status_event,
656                                     rsi_ble_on_disconnect_t ble_on_disconnect_event,
657                                     rsi_ble_on_le_ping_payload_timeout_t ble_on_le_ping_time_expired_event,
658                                     rsi_ble_on_phy_update_complete_t ble_on_phy_update_complete_event,
659                                     rsi_ble_on_data_length_update_t rsi_ble_on_data_length_update_event,
660                                     rsi_ble_on_enhance_connect_t ble_on_enhance_conn_status_event,
661                                     rsi_ble_on_directed_adv_report_event_t ble_on_directed_adv_report_event,
662                                     rsi_ble_on_conn_update_complete_t ble_on_conn_update_complete_event,
663                                     rsi_ble_on_remote_conn_params_request_t ble_on_remote_conn_params_request_event)
664 {
665 
666   SL_PRINTF(SL_RSI_BLE_GAP_REGISTER_CALLBACKS_TRIGGER, BLE, LOG_INFO);
667   // Get ble cb struct pointer
668   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
669 
670   // Assign the call backs to the respective call back
671   ble_specific_cb->ble_on_adv_report_event                 = ble_on_adv_report_event;
672   ble_specific_cb->ble_on_conn_status_event                = ble_on_conn_status_event;
673   ble_specific_cb->ble_on_disconnect_event                 = ble_on_disconnect_event;
674   ble_specific_cb->ble_on_le_ping_time_expired_event       = ble_on_le_ping_time_expired_event;
675   ble_specific_cb->ble_on_phy_update_complete_event        = ble_on_phy_update_complete_event;
676   ble_specific_cb->rsi_ble_on_data_length_update_event     = rsi_ble_on_data_length_update_event;
677   ble_specific_cb->ble_on_enhance_conn_status_event        = ble_on_enhance_conn_status_event;
678   ble_specific_cb->ble_on_directed_adv_report_event        = ble_on_directed_adv_report_event;
679   ble_specific_cb->ble_on_conn_update_complete_event       = ble_on_conn_update_complete_event;
680   ble_specific_cb->ble_on_remote_conn_params_request_event = ble_on_remote_conn_params_request_event;
681 }
682 /*==============================================*/
683 /**
684  * @brief      Register GAP Extended responses/events callbacks.
685  * @pre        Device should be initialized before calling this API.
686  * @param[in]  ble_on_remote_features_event  - Call back function for Remote feature request
687  * @param[in]  ble_on_le_more_data_req_event - Call back function for LE More data request
688  * @note        For more information about each callback, please refer to GAP Extended callbacks description section.
689  * @return      void
690  */
691 
rsi_ble_gap_extended_register_callbacks(rsi_ble_on_remote_features_t ble_on_remote_features_event,rsi_ble_on_le_more_data_req_t ble_on_le_more_data_req_event)692 void rsi_ble_gap_extended_register_callbacks(rsi_ble_on_remote_features_t ble_on_remote_features_event,
693                                              rsi_ble_on_le_more_data_req_t ble_on_le_more_data_req_event)
694 {
695 
696   SL_PRINTF(SL_RSI_BLE_GAP_EXTENDED_REGISTER_CALLBACKS_TRIGGER, BLE, LOG_INFO);
697   // Get ble cb struct pointer
698   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
699 
700   // Assign the call backs to the respective call back
701   ble_specific_cb->ble_on_remote_features_event  = ble_on_remote_features_event;
702   ble_specific_cb->ble_on_le_more_data_req_event = ble_on_le_more_data_req_event;
703 }
704 /** @} */
705 
706 /** @addtogroup BT-LOW-ENERGY7
707 * @{
708 */
709 /**
710  * @brief         Register the SMP callbacks
711  * @param[in]     ble_on_smp_request_event           - smp request callback
712  * @param[in]     ble_on_smp_response_event          - smp response callback
713  * @param[in]	  ble_on_smp_passkey_event           - smp passkey callback
714  * @param[in]	  ble_on_smp_failed_event            - smp failed callback
715  * @param[in]	  ble_on_smp_encryptrd               - encription enabled callback
716  * @param[in]	  ble_on_smp_passkey_display_event   - smp passkey display callback
717  * @param[in]	  ble_sc_passkey_event               - sc passkey display callback
718  * @param[in]     ble_on_le_ltk_req_event            - This is the SMP ltk request callback
719  * @param[in]     ble_on_le_security_keys_event      -  This is the SMP security keys callback
720  * @param[in]	  ble_on_sc_method_event             - sc method display callback
721  * @return      void
722  *
723  */
724 
rsi_ble_smp_register_callbacks(rsi_ble_on_smp_request_t ble_on_smp_request_event,rsi_ble_on_smp_response_t ble_on_smp_response_event,rsi_ble_on_smp_passkey_t ble_on_smp_passkey_event,rsi_ble_on_smp_failed_t ble_on_smp_failed_event,rsi_ble_on_encrypt_started_t ble_on_smp_encryptrd,rsi_ble_on_smp_passkey_display_t ble_on_smp_passkey_display_event,rsi_ble_on_sc_passkey_t ble_sc_passkey_event,rsi_ble_on_le_ltk_req_event_t ble_on_le_ltk_req_event,rsi_ble_on_le_security_keys_t ble_on_le_security_keys_event,rsi_ble_on_smp_response_t ble_on_cli_smp_response_event,rsi_ble_on_sc_method_t ble_on_sc_method_event)725 void rsi_ble_smp_register_callbacks(rsi_ble_on_smp_request_t ble_on_smp_request_event,
726                                     rsi_ble_on_smp_response_t ble_on_smp_response_event,
727                                     rsi_ble_on_smp_passkey_t ble_on_smp_passkey_event,
728                                     rsi_ble_on_smp_failed_t ble_on_smp_failed_event,
729                                     rsi_ble_on_encrypt_started_t ble_on_smp_encryptrd,
730                                     rsi_ble_on_smp_passkey_display_t ble_on_smp_passkey_display_event,
731                                     rsi_ble_on_sc_passkey_t ble_sc_passkey_event,
732                                     rsi_ble_on_le_ltk_req_event_t ble_on_le_ltk_req_event,
733                                     rsi_ble_on_le_security_keys_t ble_on_le_security_keys_event,
734                                     rsi_ble_on_smp_response_t ble_on_cli_smp_response_event,
735                                     rsi_ble_on_sc_method_t ble_on_sc_method_event)
736 {
737 
738   SL_PRINTF(SL_RSI_BLE_SMP_REGISTER_CALLBACKS_TRIGGER, BLE, LOG_INFO);
739   // Get ble cb struct pointer
740   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
741 
742   // Assign the call backs to the respective call back
743   ble_specific_cb->ble_on_smp_request_event      = ble_on_smp_request_event;
744   ble_specific_cb->ble_on_smp_response_event     = ble_on_smp_response_event;
745   ble_specific_cb->ble_on_smp_passkey_event      = ble_on_smp_passkey_event;
746   ble_specific_cb->ble_on_smp_fail_event         = ble_on_smp_failed_event;
747   ble_specific_cb->ble_on_smp_encrypt_started    = ble_on_smp_encryptrd;
748   ble_specific_cb->ble_on_smp_passkey_display    = ble_on_smp_passkey_display_event;
749   ble_specific_cb->ble_on_sc_passkey             = ble_sc_passkey_event;
750   ble_specific_cb->ble_on_le_ltk_req_event       = ble_on_le_ltk_req_event;
751   ble_specific_cb->ble_on_le_security_keys_event = ble_on_le_security_keys_event;
752   ble_specific_cb->ble_on_cli_smp_response_event = ble_on_cli_smp_response_event;
753   ble_specific_cb->ble_on_sc_method_event        = ble_on_sc_method_event;
754 }
755 
756 /**
757  * @brief       Register the GATT callbacks.
758  * @param[in]   rsi_ble_on_profiles_list_resp_t         ble_on_profiles_list_resp         - Callback for rsi_ble_get_profiles command
759  * @param[in]   rsi_ble_on_profile_resp_t               ble_on_profile_resp               - Callback for rsi_ble_get_profile command
760  * @param[in]   rsi_ble_on_char_services_resp_t         ble_on_char_services_resp         - Callback for rsi_ble_get_char_services command
761  * @param[in]   rsi_ble_on_inc_services_resp_t          ble_on_inc_services_resp          - Callback for rsi_ble_get_inc_services command
762  * @param[in]   rsi_ble_on_att_desc_resp_t              ble_on_att_desc_resp              - Callback for rsi_ble_get_att_descriptors command
763  * @param[in]   rsi_ble_on_read_resp_t                  ble_on_read_resp                  - Callback for all read requests command
764  * @param[in]   rsi_ble_on_write_resp_t                 ble_on_write_resp                 - Callback for all write commands
765  * @param[in]   rsi_ble_on_gatt_write_event_t           blw_on_gatt_event                 - Callback for all GATT events
766  * @param[in]   rsi_ble_on_gatt_error_resp_t            ble_on_gatt_error_resp_event      - Callback for GATT error events
767  * @param[in]   rsi_ble_on_gatt_desc_val_event_t        ble_on_gatt_desc_val_resp_event   - Callback for GATT descriptor value event
768  * @param[in]   rsi_ble_on_event_profiles_list_t        ble_on_profiles_list_event        - Callback function for profiles list event
769  * @param[in]   rsi_ble_on_event_profile_by_uuid_t      ble_on_profile_by_uuid_event      - Callback function for profile event
770  * @param[in]  rsi_ble_on_event_read_by_char_services_t ble_on_read_by_char_services_event- Callback function for char services event
771  * @param[in]  rsi_ble_on_event_read_by_inc_services_t  ble_on_read_by_inc_services_event - Callback function for inc services event
772  * @param[in]  rsi_ble_on_event_read_att_value_t        ble_on_read_att_value_event       - Callback function for read att value event
773  * @param[in]  rsi_ble_on_event_read_resp_t             ble_on_read_resp_event            - Callback function for read att event
774  * @param[in]  rsi_ble_on_event_write_resp_t            ble_on_write_resp_event           - Callback function for write event
775  * @param[in]  rsi_ble_on_event_indicate_confirmation_t ble_on_indicate_confirmation_event- Callback function for indicate confirmation event
776  * @param[in]  rsi_ble_on_event_prepare_write_resp_t    ble_on_prepare_write_resp_event   - Callback function for prepare write event
777  * @return  void
778  *
779  */
780 
rsi_ble_gatt_register_callbacks(rsi_ble_on_profiles_list_resp_t ble_on_profiles_list_resp,rsi_ble_on_profile_resp_t ble_on_profile_resp,rsi_ble_on_char_services_resp_t ble_on_char_services_resp,rsi_ble_on_inc_services_resp_t ble_on_inc_services_resp,rsi_ble_on_att_desc_resp_t ble_on_att_desc_resp,rsi_ble_on_read_resp_t ble_on_read_resp,rsi_ble_on_write_resp_t ble_on_write_resp,rsi_ble_on_gatt_write_event_t ble_on_gatt_event,rsi_ble_on_gatt_prepare_write_event_t ble_on_gatt_prepare_write_event,rsi_ble_on_execute_write_event_t ble_on_execute_write_event,rsi_ble_on_read_req_event_t ble_on_read_req_event,rsi_ble_on_mtu_event_t ble_on_mtu_event,rsi_ble_on_gatt_error_resp_t ble_on_gatt_error_resp_event,rsi_ble_on_gatt_desc_val_event_t ble_on_gatt_desc_val_resp_event,rsi_ble_on_event_profiles_list_t ble_on_profiles_list_event,rsi_ble_on_event_profile_by_uuid_t ble_on_profile_by_uuid_event,rsi_ble_on_event_read_by_char_services_t ble_on_read_by_char_services_event,rsi_ble_on_event_read_by_inc_services_t ble_on_read_by_inc_services_event,rsi_ble_on_event_read_att_value_t ble_on_read_att_value_event,rsi_ble_on_event_read_resp_t ble_on_read_resp_event,rsi_ble_on_event_write_resp_t ble_on_write_resp_event,rsi_ble_on_event_indicate_confirmation_t ble_on_indicate_confirmation_event,rsi_ble_on_event_prepare_write_resp_t ble_on_prepare_write_resp_event)781 void rsi_ble_gatt_register_callbacks(rsi_ble_on_profiles_list_resp_t ble_on_profiles_list_resp,
782                                      rsi_ble_on_profile_resp_t ble_on_profile_resp,
783                                      rsi_ble_on_char_services_resp_t ble_on_char_services_resp,
784                                      rsi_ble_on_inc_services_resp_t ble_on_inc_services_resp,
785                                      rsi_ble_on_att_desc_resp_t ble_on_att_desc_resp,
786                                      rsi_ble_on_read_resp_t ble_on_read_resp,
787                                      rsi_ble_on_write_resp_t ble_on_write_resp,
788                                      rsi_ble_on_gatt_write_event_t ble_on_gatt_event,
789                                      rsi_ble_on_gatt_prepare_write_event_t ble_on_gatt_prepare_write_event,
790                                      rsi_ble_on_execute_write_event_t ble_on_execute_write_event,
791                                      rsi_ble_on_read_req_event_t ble_on_read_req_event,
792                                      rsi_ble_on_mtu_event_t ble_on_mtu_event,
793                                      rsi_ble_on_gatt_error_resp_t ble_on_gatt_error_resp_event,
794                                      rsi_ble_on_gatt_desc_val_event_t ble_on_gatt_desc_val_resp_event,
795                                      rsi_ble_on_event_profiles_list_t ble_on_profiles_list_event,
796                                      rsi_ble_on_event_profile_by_uuid_t ble_on_profile_by_uuid_event,
797                                      rsi_ble_on_event_read_by_char_services_t ble_on_read_by_char_services_event,
798                                      rsi_ble_on_event_read_by_inc_services_t ble_on_read_by_inc_services_event,
799                                      rsi_ble_on_event_read_att_value_t ble_on_read_att_value_event,
800                                      rsi_ble_on_event_read_resp_t ble_on_read_resp_event,
801                                      rsi_ble_on_event_write_resp_t ble_on_write_resp_event,
802                                      rsi_ble_on_event_indicate_confirmation_t ble_on_indicate_confirmation_event,
803                                      rsi_ble_on_event_prepare_write_resp_t ble_on_prepare_write_resp_event)
804 {
805 
806   SL_PRINTF(SL_RSI_BLE_GATT_REGISTER_CALLBACKS_TRIGGER, BLE, LOG_INFO);
807   // Get ble specific cb struct pointer
808   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
809 
810   // Assign the call backs to the respective call back
811   ble_specific_cb->ble_on_profiles_list_resp          = ble_on_profiles_list_resp;
812   ble_specific_cb->ble_on_profile_resp                = ble_on_profile_resp;
813   ble_specific_cb->ble_on_char_services_resp          = ble_on_char_services_resp;
814   ble_specific_cb->ble_on_inc_services_resp           = ble_on_inc_services_resp;
815   ble_specific_cb->ble_on_att_desc_resp               = ble_on_att_desc_resp;
816   ble_specific_cb->ble_on_read_resp                   = ble_on_read_resp;
817   ble_specific_cb->ble_on_write_resp                  = ble_on_write_resp;
818   ble_specific_cb->ble_on_gatt_events                 = ble_on_gatt_event;
819   ble_specific_cb->ble_on_prepare_write_event         = ble_on_gatt_prepare_write_event;
820   ble_specific_cb->ble_on_execute_write_event         = ble_on_execute_write_event;
821   ble_specific_cb->ble_on_read_req_event              = ble_on_read_req_event;
822   ble_specific_cb->ble_on_mtu_event                   = ble_on_mtu_event;
823   ble_specific_cb->ble_on_gatt_error_resp_event       = ble_on_gatt_error_resp_event;
824   ble_specific_cb->ble_on_profiles_list_event         = ble_on_profiles_list_event;
825   ble_specific_cb->ble_on_gatt_desc_val_resp_event    = ble_on_gatt_desc_val_resp_event;
826   ble_specific_cb->ble_on_profile_by_uuid_event       = ble_on_profile_by_uuid_event;
827   ble_specific_cb->ble_on_read_by_char_services_event = ble_on_read_by_char_services_event;
828   ble_specific_cb->ble_on_read_by_inc_services_event  = ble_on_read_by_inc_services_event;
829   ble_specific_cb->ble_on_read_att_value_event        = ble_on_read_att_value_event;
830   ble_specific_cb->ble_on_read_resp_event             = ble_on_read_resp_event;
831   ble_specific_cb->ble_on_write_resp_event            = ble_on_write_resp_event;
832   ble_specific_cb->ble_on_indicate_confirmation_event = ble_on_indicate_confirmation_event;
833   ble_specific_cb->ble_on_prepare_write_resp_event    = ble_on_prepare_write_resp_event;
834 
835   return;
836 }
837 /**
838  * @brief       Register the GATT Extended responses/events callbacks.
839  * @param[in]   rsi_ble_on_mtu_exchange_info_t         ble_on_mtu_exchange_info_event         - Call back function for MTU Exchange information Event
840  * @return      void
841  *
842  */
843 
rsi_ble_gatt_extended_register_callbacks(rsi_ble_on_mtu_exchange_info_t ble_on_mtu_exchange_info_event)844 void rsi_ble_gatt_extended_register_callbacks(rsi_ble_on_mtu_exchange_info_t ble_on_mtu_exchange_info_event)
845 {
846   // Get ble cb struct pointer
847   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
848 
849   // Assign the call backs to the respective call back
850   ble_specific_cb->ble_on_mtu_exchange_info_event = ble_on_mtu_exchange_info_event;
851 }
852 
853 /**
854  * @fn       uint16_t rsi_ble_enhanced_gap_extended_register_callbacks(uint16_t callback_id, void (*callback_handler_ptr)(uint16_t status,
855  *                                                uint8_t *buffer))
856  * @brief    Register the BLE call back functions.
857  * @param[in]  callback_id                       - This is the Id of the call back function following ids are supported:
858  * @param[in]  void (*callback_handler_ptr)(void - This is the Call back handler
859  * @param[in]  status                            - status of the asynchronous response
860  * @param[in]  buffer                            - payload of the asynchronous response
861  * @return      0 - Success \n
862  *              -53 - Failure \n
863  *             If call_back_id is greater than the maximum callbacks to register, returns ref/ RSI_ERROR_BLE_INVALID_CALLBACK_CNT.
864  * @note        In callbacks, application should not initiate any TX operation to the module.
865  */
rsi_ble_enhanced_gap_extended_register_callbacks(uint16_t callback_id,void (* callback_handler_ptr)(uint16_t status,uint8_t * buffer))866 uint32_t rsi_ble_enhanced_gap_extended_register_callbacks(uint16_t callback_id,
867                                                           void (*callback_handler_ptr)(uint16_t status,
868                                                                                        uint8_t *buffer))
869 {
870   // Get BLE cb struct pointer
871   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
872 
873   if (callback_id > RSI_BLE_MAX_NUM_GAP_EXT_CALLBACKS) {
874 
875     /*Return , if the callback number exceeds the RSI_BT_COMMON_MAX_NUM_EXT_CALLBACKS */
876 
877     return RSI_ERROR_BLE_INVALID_CALLBACK_CNT;
878   }
879   switch (callback_id) {
880     case RSI_BLE_ON_REMOTE_DEVICE_INFORMATION: {
881       ble_specific_cb->ble_on_remote_device_info_event = (rsi_ble_on_remote_device_info_t)callback_handler_ptr;
882     } break;
883     case RSI_BLE_ON_RCP_EVENT: {
884       ble_specific_cb->ble_on_rcp_resp_rcvd_event = (rsi_ble_on_rcp_resp_rcvd_t)callback_handler_ptr;
885     } break;
886     default:
887       LOG_PRINT("\nInvalid Callback ID\n");
888       return RSI_ERROR_BLE_INVALID_CALLBACK_CNT;
889   }
890   return RSI_SUCCESS;
891 }
892 
893 /**
894  * @fn       uint16_t  rsi_ble_adv_ext_events_register_callbacks  (uint16_t callback_id, void (*callback_handler_ptr)(uint16_t status,
895  *                                                uint8_t *buffer))
896  * @brief    Register the BLE call back functions.
897  * @param[in]  callback_id                       - This is the Id of the call back function following ids are supported:
898  * @param[in]  void (*callback_handler_ptr)(void - This is the Call back handler
899  * @param[in]  status                            - status of the asynchronous response
900  * @param[in]  buffer                            - payload of the asynchronous response
901  * @return      0 - Success \n
902  *              -53 - Failure \n
903  *             If call_back_id is greater than the maximum callbacks to register, returns ref/ RSI_ERROR_BLE_INVALID_CALLBACK_CNT.
904  * @note        In callbacks, application should not initiate any TX operation to the module.
905  */
906 
rsi_ble_adv_ext_events_register_callbacks(uint16_t callback_id,void (* callback_handler_ptr)(uint16_t status,uint8_t * buffer))907 int32_t rsi_ble_adv_ext_events_register_callbacks(uint16_t callback_id,
908                                                   void (*callback_handler_ptr)(uint16_t status, uint8_t *buffer))
909 {
910   //Get ble cb struct pointer
911   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
912 
913   if (callback_id > RSI_BLE_MAX_NUM_ADV_EXT_EVENT_CALLBACKS) {
914     /*
915      *Return , if the callback number exceeds the RSI_BLE_MAX_NUM_CALLBACKS ,or
916      * the callback is already registered
917      */
918     return RSI_ERROR_BLE_INVALID_CALLBACK_CNT;
919   }
920 
921   switch (callback_id) {
922     case RSI_BLE_ON_ADV_EXT_ADVERTISE_REPORT_EVENT: {
923       ble_specific_cb->ble_ae_report_complete_event = (rsi_ble_ae_report_complete_t)callback_handler_ptr;
924     } break;
925     case RSI_BLE_ON_ADV_EXT_PERIODIC_ADV_SYNC_ESTBL_EVENT: {
926       ble_specific_cb->ble_ae_per_adv_sync_estbl_event = (rsi_ble_ae_per_adv_sync_estbl_t)callback_handler_ptr;
927     } break;
928     case RSI_BLE_ON_ADV_EXT_PERIODIC_ADVERTISE_REPORT_EVENT: {
929       ble_specific_cb->ble_ae_per_adv_report_event = (rsi_ble_ae_per_adv_report_t)callback_handler_ptr;
930     } break;
931     case RSI_BLE_ON_ADV_EXT_PERIODIC_ADV_SYNC_LOST_EVENT: {
932       ble_specific_cb->ble_ae_per_adv_sync_lost_event = (rsi_ble_ae_per_adv_sync_lost_t)callback_handler_ptr;
933     } break;
934     case RSI_BLE_ON_ADV_EXT_SCAN_TIMEOUT_EVENT: {
935       ble_specific_cb->ble_ae_scan_timeout_event = (rsi_ble_ae_scan_timeout_t)callback_handler_ptr;
936     } break;
937     case RSI_BLE_ON_ADV_EXT_ADVERTISE_SET_TERMINATED_EVENT: {
938       ble_specific_cb->ble_ae_adv_set_terminated_event = (rsi_ble_ae_adv_set_terminated_t)callback_handler_ptr;
939     } break;
940     case RSI_BLE_ON_ADV_EXT_SCAN_REQUEST_RECEIVED_EVENT: {
941       ble_specific_cb->ble_ae_scan_req_recvd_event = (rsi_ble_ae_scan_req_recvd_t)callback_handler_ptr;
942     } break;
943     default:
944       return RSI_ERROR_BLE_INVALID_CALLBACK_CNT;
945   }
946 
947   return RSI_SUCCESS;
948 }
949 
950 /** @} */
951 
952 /*==============================================*/
953 /**
954  * @brief      Register the function pointers for GATT responses
955  * @param[in]  ble_on_cbsc_conn_req      - Callback function for CBFC connection request event
956  * @param[in]  ble_on_cbsc_conn_complete - Callback function for CBFC connection complete status event
957  * @param[in]  ble_on_cbsc_rx_data       - Callback function for CBFC data receive event
958  * @param[in]  ble_on_cbsc_disconn       - Callback function for CBFC disconnect event
959  * @return     void
960  */
961 
rsi_ble_l2cap_cbsc_register_callbacks(rsi_ble_on_cbfc_conn_req_event_t ble_on_cbsc_conn_req,rsi_ble_on_cbfc_conn_complete_event_t ble_on_cbsc_conn_complete,rsi_ble_on_cbfc_rx_data_event_t ble_on_cbsc_rx_data,rsi_ble_on_cbfc_disconn_event_t ble_on_cbsc_disconn)962 void rsi_ble_l2cap_cbsc_register_callbacks(rsi_ble_on_cbfc_conn_req_event_t ble_on_cbsc_conn_req,
963                                            rsi_ble_on_cbfc_conn_complete_event_t ble_on_cbsc_conn_complete,
964                                            rsi_ble_on_cbfc_rx_data_event_t ble_on_cbsc_rx_data,
965                                            rsi_ble_on_cbfc_disconn_event_t ble_on_cbsc_disconn)
966 {
967 
968   SL_PRINTF(SL_RSI_BLE_L2CAP_CBSC_REGISTER_CALLBACKS_TRIGGER, BLE, LOG_INFO);
969   // Get ble specific cb struct pointer
970   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
971 
972   // Assign the call backs to the respective call back
973   ble_specific_cb->ble_on_cbfc_conn_req_event      = ble_on_cbsc_conn_req;
974   ble_specific_cb->ble_on_cbfc_conn_complete_event = ble_on_cbsc_conn_complete;
975   ble_specific_cb->ble_on_cbfc_rx_data_event       = ble_on_cbsc_rx_data;
976   ble_specific_cb->ble_on_cbfc_disconn_event       = ble_on_cbsc_disconn;
977 
978   return;
979 }
980 
981 /** @addtogroup DRIVER14
982 * @{
983 */
984 /**
985  * @brief      Initailize the BT callbacks register.
986  * @param[in]  ble_cb   - BLE control back
987  * @param[in]  rsp_type - BLE Packet type
988  * @param[in]  payload - Payload
989  * @param[in]  payload_length - Payload length
990  * @return     void
991  *
992  */
rsi_ble_callbacks_handler(rsi_bt_cb_t * ble_cb,uint16_t rsp_type,uint8_t * payload,uint16_t payload_length)993 void rsi_ble_callbacks_handler(rsi_bt_cb_t *ble_cb, uint16_t rsp_type, uint8_t *payload, uint16_t payload_length)
994 {
995   SL_PRINTF(SL_RSI_BLE_CALLBACKS_HANDLER_TRIGGER, BLE, LOG_INFO, "RESPONSE_TYPE: %2x", rsp_type);
996   // This statement is added only to resolve compilation warning, value is unchanged
997   UNUSED_PARAMETER(payload_length);
998   // Get ble cb struct pointer
999   const rsi_ble_cb_t *ble_specific_cb = ble_cb->bt_global_cb->ble_specific_cb;
1000   uint16_t status                     = 0;
1001   uint16_t sync_status                = 0;
1002   uint8_t le_cmd_inuse_check          = 0;
1003 
1004   // updating the response status;
1005   status = (uint16_t)ble_cb->async_status;
1006 
1007   sync_status = (uint16_t)rsi_bt_get_status(ble_cb);
1008 
1009   SL_PRINTF(SL_RSI_BLE_CALLBACKS_HANDLER_STATUS, BLE, LOG_INFO, "STATUS: %2x", status);
1010 
1011   // Check each cmd_type like decode_resp_handler and call the respective callback
1012   switch (rsp_type) {
1013     case RSI_BLE_EVENT_ADV_REPORT: {
1014       if (ble_specific_cb->ble_on_adv_report_event != NULL) {
1015         ble_specific_cb->ble_on_adv_report_event((rsi_ble_event_adv_report_t *)payload);
1016       }
1017     } break;
1018     case RSI_BLE_EVENT_CONN_STATUS: {
1019       if (ble_specific_cb->ble_on_conn_status_event != NULL) {
1020         ((rsi_ble_event_conn_status_t *)payload)->status = status;
1021         ble_specific_cb->ble_on_conn_status_event((rsi_ble_event_conn_status_t *)payload);
1022       }
1023       rsi_add_remote_ble_dev_info((rsi_ble_event_enhance_conn_status_t *)payload);
1024     } break;
1025 
1026     case RSI_BLE_EVENT_ENHANCE_CONN_STATUS: {
1027       if (ble_specific_cb->ble_on_enhance_conn_status_event != NULL) {
1028         ((rsi_ble_event_enhance_conn_status_t *)payload)->status = status;
1029         ble_specific_cb->ble_on_enhance_conn_status_event((rsi_ble_event_enhance_conn_status_t *)payload);
1030       }
1031       rsi_add_remote_ble_dev_info((rsi_ble_event_enhance_conn_status_t *)payload);
1032     } break;
1033 
1034     case RSI_BLE_EVENT_DISCONNECT: {
1035       if (ble_specific_cb->ble_on_disconnect_event != NULL) {
1036         ble_specific_cb->ble_on_disconnect_event((rsi_ble_event_disconnect_t *)payload, status);
1037       }
1038       rsi_remove_remote_ble_dev_info((rsi_ble_event_disconnect_t *)payload);
1039     } break;
1040     case RSI_BLE_EVENT_GATT_ERROR_RESPONSE: {
1041       if (ble_specific_cb->ble_on_gatt_error_resp_event != NULL) {
1042         ble_specific_cb->ble_on_gatt_error_resp_event(status, (rsi_ble_event_error_resp_t *)payload);
1043       }
1044       le_cmd_inuse_check = 1;
1045     } break;
1046     case RSI_BLE_EVENT_GATT_DESC_VAL_RESPONSE: {
1047       if (ble_specific_cb->ble_on_gatt_desc_val_resp_event != NULL) {
1048         ble_specific_cb->ble_on_gatt_desc_val_resp_event(status, (rsi_ble_event_gatt_desc_t *)payload);
1049       }
1050       le_cmd_inuse_check = 1;
1051     } break;
1052 
1053     case RSI_BLE_EVENT_GATT_PRIMARY_SERVICE_LIST: {
1054       if (ble_specific_cb->ble_on_profiles_list_event != NULL) {
1055         ble_specific_cb->ble_on_profiles_list_event(status, (rsi_ble_event_profiles_list_t *)payload);
1056       }
1057       le_cmd_inuse_check = 1;
1058     } break;
1059     case RSI_BLE_EVENT_GATT_PRIMARY_SERVICE_BY_UUID: {
1060       if (ble_specific_cb->ble_on_profile_by_uuid_event != NULL) {
1061         ble_specific_cb->ble_on_profile_by_uuid_event(status, (rsi_ble_event_profile_by_uuid_t *)payload);
1062       }
1063       le_cmd_inuse_check = 1;
1064     } break;
1065     case RSI_BLE_EVENT_GATT_READ_CHAR_SERVS: {
1066       if (ble_specific_cb->ble_on_read_by_char_services_event != NULL) {
1067         ble_specific_cb->ble_on_read_by_char_services_event(status, (rsi_ble_event_read_by_type1_t *)payload);
1068       }
1069       le_cmd_inuse_check = 1;
1070     } break;
1071     case RSI_BLE_EVENT_GATT_READ_INC_SERVS: {
1072       if (ble_specific_cb->ble_on_read_by_inc_services_event != NULL) {
1073         ble_specific_cb->ble_on_read_by_inc_services_event(status, (rsi_ble_event_read_by_type2_t *)payload);
1074       }
1075       le_cmd_inuse_check = 1;
1076     } break;
1077     case RSI_BLE_EVENT_GATT_READ_VAL_BY_UUID: {
1078       if (ble_specific_cb->ble_on_read_att_value_event != NULL) {
1079         ble_specific_cb->ble_on_read_att_value_event(status, (rsi_ble_event_read_by_type3_t *)payload);
1080       }
1081       le_cmd_inuse_check = 1;
1082     } break;
1083     case RSI_BLE_EVENT_GATT_READ_RESP:
1084     case RSI_BLE_EVENT_GATT_READ_BLOB_RESP:
1085     case RSI_BLE_EVENT_GATT_READ_MULTIPLE_RESP: {
1086       if (ble_specific_cb->ble_on_read_resp_event != NULL) {
1087         ble_specific_cb->ble_on_read_resp_event(status, (rsi_ble_event_att_value_t *)payload);
1088       }
1089       le_cmd_inuse_check = 1;
1090     } break;
1091     case RSI_BLE_EVENT_GATT_WRITE_RESP:
1092     case RSI_BLE_EVENT_GATT_EXECUTE_WRITE_RESP: {
1093       if (ble_specific_cb->ble_on_write_resp_event != NULL) {
1094         ble_specific_cb->ble_on_write_resp_event(status, (rsi_ble_set_att_resp_t *)payload);
1095       }
1096       le_cmd_inuse_check = 1;
1097     } break;
1098     case RSI_BLE_EVENT_GATT_INDICATE_CONFIRMATION: {
1099       if (ble_specific_cb->ble_on_indicate_confirmation_event != NULL) {
1100         ble_specific_cb->ble_on_indicate_confirmation_event(status, (rsi_ble_set_att_resp_t *)payload);
1101       }
1102       le_cmd_inuse_check = 1;
1103     } break;
1104     case RSI_BLE_EVENT_GATT_PREPARE_WRITE_RESP: {
1105       if (ble_specific_cb->ble_on_prepare_write_resp_event != NULL) {
1106         ble_specific_cb->ble_on_prepare_write_resp_event(status, (rsi_ble_prepare_write_resp_t *)payload);
1107       }
1108       le_cmd_inuse_check = 1;
1109     } break;
1110     case RSI_BLE_EVENT_SMP_REQUEST: {
1111       if (ble_specific_cb->ble_on_smp_request_event != NULL) {
1112         ble_specific_cb->ble_on_smp_request_event((rsi_bt_event_smp_req_t *)payload);
1113       }
1114     } break;
1115 
1116     case RSI_BLE_EVENT_SMP_RESPONSE: {
1117       if (ble_specific_cb->ble_on_smp_response_event != NULL) {
1118         ble_specific_cb->ble_on_smp_response_event((rsi_bt_event_smp_resp_t *)payload);
1119       }
1120     } break;
1121 
1122     case RSI_BLE_EVENT_CLI_SMP_RESPONSE: {
1123       if (ble_specific_cb->ble_on_cli_smp_response_event != NULL) {
1124         ble_specific_cb->ble_on_cli_smp_response_event((rsi_bt_event_smp_resp_t *)payload);
1125       }
1126     } break;
1127     case RSI_BLE_EVENT_CHIP_MEMORY_STATS: {
1128       if (ble_specific_cb->ble_on_chip_memory_status_event != NULL) {
1129         ble_specific_cb->ble_on_chip_memory_status_event((chip_ble_buffers_stats_t *)payload);
1130       }
1131     } break;
1132 
1133     case RSI_BLE_EVENT_SMP_PASSKEY: {
1134       if (ble_specific_cb->ble_on_smp_passkey_event != NULL) {
1135         ble_specific_cb->ble_on_smp_passkey_event((rsi_bt_event_smp_passkey_t *)payload);
1136       }
1137     } break;
1138 
1139     case RSI_BLE_EVENT_SMP_FAILED: {
1140       if (ble_specific_cb->ble_on_smp_fail_event != NULL) {
1141         ble_specific_cb->ble_on_smp_fail_event(status, (rsi_bt_event_smp_failed_t *)payload);
1142       }
1143     } break;
1144 
1145     case RSI_BLE_EVENT_SC_METHOD: {
1146       if (ble_specific_cb->ble_on_sc_method_event != NULL) {
1147         ble_specific_cb->ble_on_sc_method_event((rsi_bt_event_sc_method_t *)payload);
1148       }
1149     } break;
1150 
1151     case RSI_BLE_EVENT_ENCRYPT_STARTED: {
1152       if (ble_specific_cb->ble_on_smp_encrypt_started != NULL) {
1153         ble_specific_cb->ble_on_smp_encrypt_started(status, (rsi_bt_event_encryption_enabled_t *)payload);
1154       }
1155     } break;
1156     case RSI_BLE_EVENT_SMP_PASSKEY_DISPLAY_EVENT: {
1157       if (ble_specific_cb->ble_on_smp_passkey_display != NULL) {
1158         ble_specific_cb->ble_on_smp_passkey_display((rsi_bt_event_smp_passkey_display_t *)payload);
1159       }
1160     } break;
1161     case RSI_BLE_RSP_PROFILES: {
1162       if (ble_specific_cb->ble_on_profiles_list_resp != NULL) {
1163         ble_specific_cb->ble_on_profiles_list_resp(sync_status, (rsi_ble_resp_profiles_list_t *)payload);
1164       }
1165     } break;
1166     case RSI_BLE_RSP_PROFILE: {
1167       if (ble_specific_cb->ble_on_profile_resp != NULL) {
1168         ble_specific_cb->ble_on_profile_resp(sync_status, (profile_descriptors_t *)payload);
1169       }
1170     } break;
1171     case RSI_BLE_RSP_CHAR_SERVICES: {
1172       if (ble_specific_cb->ble_on_char_services_resp != NULL) {
1173         ble_specific_cb->ble_on_char_services_resp(sync_status, (rsi_ble_resp_char_services_t *)payload);
1174       }
1175     } break;
1176     case RSI_BLE_RSP_INC_SERVICES: {
1177       if (ble_specific_cb->ble_on_inc_services_resp != NULL) {
1178         ble_specific_cb->ble_on_inc_services_resp(sync_status, (rsi_ble_resp_inc_services_t *)payload);
1179       }
1180     } break;
1181     case RSI_BLE_RSP_DESC: {
1182       if (ble_specific_cb->ble_on_att_desc_resp != NULL) {
1183         ble_specific_cb->ble_on_att_desc_resp(sync_status, (rsi_ble_resp_att_descs_t *)payload);
1184       }
1185     } break;
1186     case RSI_BLE_RSP_READ_BY_UUID:
1187     case RSI_BLE_RSP_READ_VAL:
1188     case RSI_BLE_RSP_MULTIPLE_READ:
1189     case RSI_BLE_RSP_LONG_READ: {
1190       if (ble_specific_cb->ble_on_read_resp != NULL) {
1191         ble_specific_cb->ble_on_read_resp(sync_status, rsp_type, (rsi_ble_resp_att_value_t *)payload);
1192       }
1193     } break;
1194     case RSI_BLE_RSP_WRITE:
1195     case RSI_BLE_RSP_WRITE_NO_ACK:
1196     case RSI_BLE_RSP_LONG_WRITE:
1197     case RSI_BLE_RSP_PREPARE_WRITE:
1198     case RSI_BLE_RSP_EXECUTE_WRITE: {
1199       if (ble_specific_cb->ble_on_write_resp != NULL) {
1200         ble_specific_cb->ble_on_write_resp(sync_status, rsp_type);
1201       }
1202     } break;
1203     case RSI_BLE_EVENT_GATT_NOTIFICATION:
1204     case RSI_BLE_EVENT_GATT_INDICATION:
1205     case RSI_BLE_EVENT_GATT_WRITE:
1206       if (ble_specific_cb->ble_on_gatt_events != NULL) {
1207         ble_specific_cb->ble_on_gatt_events(rsp_type, (rsi_ble_event_write_t *)payload);
1208       }
1209       break;
1210     case RSI_BLE_EVENT_MTU:
1211       if (ble_specific_cb->ble_on_mtu_event != NULL) {
1212         ble_specific_cb->ble_on_mtu_event((rsi_ble_event_mtu_t *)payload);
1213       }
1214       le_cmd_inuse_check = 1;
1215       break;
1216     case RSI_BLE_EVENT_MTU_EXCHANGE_INFORMATION:
1217       if (ble_specific_cb->ble_on_mtu_exchange_info_event != NULL) {
1218         ble_specific_cb->ble_on_mtu_exchange_info_event((rsi_ble_event_mtu_exchange_information_t *)payload);
1219       }
1220       le_cmd_inuse_check = 1;
1221       break;
1222     case RSI_BLE_EVENT_LE_PING_TIME_EXPIRED:
1223       if (ble_specific_cb->ble_on_le_ping_time_expired_event != NULL) {
1224         ble_specific_cb->ble_on_le_ping_time_expired_event((rsi_ble_event_le_ping_time_expired_t *)payload);
1225       }
1226       break;
1227 
1228     case RSI_BLE_EVENT_PREPARE_WRITE:
1229       if (ble_specific_cb->ble_on_prepare_write_event != NULL) {
1230         ble_specific_cb->ble_on_prepare_write_event(rsp_type, (rsi_ble_event_prepare_write_t *)payload);
1231       }
1232       break;
1233 
1234     case RSI_BLE_EVENT_EXECUTE_WRITE:
1235       if (ble_specific_cb->ble_on_execute_write_event != NULL) {
1236         ble_specific_cb->ble_on_execute_write_event(rsp_type, (rsi_ble_execute_write_t *)payload);
1237       }
1238       break;
1239 
1240     case RSI_BLE_EVENT_READ_REQ:
1241       if (ble_specific_cb->ble_on_read_req_event != NULL) {
1242         ble_specific_cb->ble_on_read_req_event(rsp_type, (rsi_ble_read_req_t *)payload);
1243       }
1244       break;
1245 
1246     case RSI_BLE_EVENT_PHY_UPDATE_COMPLETE: {
1247       if (ble_specific_cb->ble_on_phy_update_complete_event != NULL) {
1248         ble_specific_cb->ble_on_phy_update_complete_event((rsi_ble_event_phy_update_t *)payload);
1249       }
1250     } break;
1251 
1252     case RSI_BLE_EVENT_DATA_LENGTH_UPDATE_COMPLETE:
1253       if (ble_specific_cb->rsi_ble_on_data_length_update_event != NULL) {
1254         ble_specific_cb->rsi_ble_on_data_length_update_event((rsi_ble_event_data_length_update_t *)payload);
1255       }
1256       break;
1257 
1258     case RSI_BLE_EVENT_SC_PASSKEY: {
1259       if (ble_specific_cb->ble_on_sc_passkey != NULL) {
1260         ble_specific_cb->ble_on_sc_passkey((rsi_bt_event_sc_passkey_t *)payload);
1261       }
1262     } break;
1263 
1264     case RSI_BLE_EVENT_DIRECTED_ADV_REPORT: {
1265       if (ble_specific_cb->ble_on_directed_adv_report_event != NULL) {
1266         ble_specific_cb->ble_on_directed_adv_report_event((rsi_ble_event_directedadv_report_t *)payload);
1267       }
1268     } break;
1269 
1270     case RSI_BLE_EVENT_LE_LTK_REQUEST:
1271       if (ble_specific_cb->ble_on_le_ltk_req_event != NULL) {
1272         ble_specific_cb->ble_on_le_ltk_req_event((rsi_bt_event_le_ltk_request_t *)payload);
1273       }
1274       break;
1275 
1276     case RSI_BLE_EVENT_SECURITY_KEYS: {
1277       if (ble_specific_cb->ble_on_le_security_keys_event != NULL) {
1278         ble_specific_cb->ble_on_le_security_keys_event((rsi_bt_event_le_security_keys_t *)payload);
1279       }
1280     } break;
1281 
1282     case RSI_BLE_EVENT_PSM_CONN_REQ: {
1283       if (ble_specific_cb->ble_on_cbfc_conn_req_event != NULL) {
1284         ble_specific_cb->ble_on_cbfc_conn_req_event((rsi_ble_event_cbfc_conn_req_t *)payload);
1285       }
1286     } break;
1287 
1288     case RSI_BLE_EVENT_PSM_CONN_COMPLETE: {
1289       if (ble_specific_cb->ble_on_cbfc_conn_complete_event != NULL) {
1290         ble_specific_cb->ble_on_cbfc_conn_complete_event((rsi_ble_event_cbfc_conn_complete_t *)payload, status);
1291       }
1292     } break;
1293 
1294     case RSI_BLE_EVENT_PSM_RX_DATA: {
1295       if (ble_specific_cb->ble_on_cbfc_rx_data_event != NULL) {
1296         ble_specific_cb->ble_on_cbfc_rx_data_event((rsi_ble_event_cbfc_rx_data_t *)payload);
1297       }
1298     } break;
1299 
1300     case RSI_BLE_EVENT_PSM_DISCONNECT: {
1301       if (ble_specific_cb->ble_on_cbfc_disconn_event != NULL) {
1302         ble_specific_cb->ble_on_cbfc_disconn_event((rsi_ble_event_cbfc_disconn_t *)payload);
1303       }
1304     } break;
1305 
1306     case RSI_BLE_EVENT_CONN_UPDATE_COMPLETE: {
1307       if (ble_specific_cb->ble_on_conn_update_complete_event != NULL) {
1308         ble_specific_cb->ble_on_conn_update_complete_event((rsi_ble_event_conn_update_t *)payload, status);
1309       }
1310     } break;
1311     case RSI_BLE_EVENT_REMOTE_FEATURES: {
1312       if (ble_specific_cb->ble_on_remote_features_event != NULL) {
1313         ble_specific_cb->ble_on_remote_features_event((rsi_ble_event_remote_features_t *)payload);
1314       }
1315     } break;
1316     case RSI_BLE_EVENT_LE_MORE_DATA_REQ: {
1317       rsi_ble_update_le_dev_buf((rsi_ble_event_le_dev_buf_ind_t *)payload);
1318       if (ble_specific_cb->ble_on_le_more_data_req_event != NULL) {
1319         ble_specific_cb->ble_on_le_more_data_req_event((rsi_ble_event_le_dev_buf_ind_t *)payload);
1320       }
1321     } break;
1322     case RSI_BLE_EVENT_REMOTE_CONN_PARAMS_REQUEST: {
1323       if (ble_specific_cb->ble_on_remote_conn_params_request_event != NULL) {
1324         ble_specific_cb->ble_on_remote_conn_params_request_event((rsi_ble_event_remote_conn_param_req_t *)payload,
1325                                                                  status);
1326       }
1327     } break;
1328     case RSI_BLE_EVENT_REMOTE_DEVICE_INFORMATION: {
1329       if (ble_specific_cb->ble_on_remote_device_info_event != NULL) {
1330         ble_specific_cb->ble_on_remote_device_info_event(status, (rsi_ble_event_remote_device_info_t *)payload);
1331       }
1332     } break;
1333 
1334     case RSI_BLE_EVENT_AE_ADVERTISING_REPORT: {
1335       if (ble_specific_cb->ble_ae_report_complete_event != NULL) {
1336         ble_specific_cb->ble_ae_report_complete_event(status, (rsi_ble_ae_adv_report_t *)payload);
1337       }
1338     } break;
1339     case RSI_BLE_EVENT_PER_ADV_SYNC_ESTBL: {
1340       if (ble_specific_cb->ble_ae_per_adv_sync_estbl_event != NULL) {
1341         ble_specific_cb->ble_ae_per_adv_sync_estbl_event(status, (rsi_ble_per_adv_sync_estbl_t *)payload);
1342       }
1343     } break;
1344     case RSI_BLE_EVENT_PER_ADV_REPORT: {
1345       if (ble_specific_cb->ble_ae_per_adv_report_event != NULL) {
1346         ble_specific_cb->ble_ae_per_adv_report_event(status, (rsi_ble_per_adv_report_t *)payload);
1347       }
1348     } break;
1349     case RSI_BLE_EVENT_PER_ADV_SYNC_LOST: {
1350       if (ble_specific_cb->ble_ae_per_adv_sync_lost_event != NULL) {
1351         ble_specific_cb->ble_ae_per_adv_sync_lost_event(status, (rsi_ble_per_adv_sync_lost_t *)payload);
1352       }
1353     } break;
1354     case RSI_BLE_EVENT_SCAN_TIMEOUT: {
1355       if (ble_specific_cb->ble_ae_scan_timeout_event != NULL) {
1356         ble_specific_cb->ble_ae_scan_timeout_event(status, (rsi_ble_scan_timeout_t *)payload);
1357       }
1358     } break;
1359     case RSI_BLE_EVENT_ADV_SET_TERMINATED: {
1360       if (ble_specific_cb->ble_ae_adv_set_terminated_event != NULL) {
1361         ble_specific_cb->ble_ae_adv_set_terminated_event(status, (rsi_ble_adv_set_terminated_t *)payload);
1362       }
1363     } break;
1364     case RSI_BLE_EVENT_SCAN_REQ_RECVD: {
1365       if (ble_specific_cb->ble_ae_scan_req_recvd_event != NULL) {
1366         ble_specific_cb->ble_ae_scan_req_recvd_event(status, (rsi_ble_scan_req_recvd_t *)payload);
1367       }
1368     } break;
1369     case RSI_BLE_EVENT_RCP_DATA_RCVD: {
1370       if (ble_specific_cb->ble_on_rcp_resp_rcvd_event != NULL) {
1371         ble_specific_cb->ble_on_rcp_resp_rcvd_event(status, (rsi_ble_event_rcp_rcvd_info_t *)payload);
1372       }
1373     }
1374     default:
1375       break;
1376   }
1377 
1378   if (le_cmd_inuse_check) {
1379     const uint8_t *remote_dev_bd_addr = payload;
1380     for (uint8_t inx = 0; inx < (RSI_BLE_MAX_NBR_PERIPHERALS + RSI_BLE_MAX_NBR_CENTRALS); inx++) {
1381       if (!memcmp(ble_cb->remote_ble_info[inx].remote_dev_bd_addr, remote_dev_bd_addr, RSI_DEV_ADDR_LEN)) {
1382         if ((ble_cb->remote_ble_info[inx].cmd_in_use)
1383             && ((rsp_type == RSI_BLE_EVENT_GATT_ERROR_RESPONSE)
1384                 || (rsp_type == ble_cb->remote_ble_info[inx].expected_resp))) {
1385           ble_cb->remote_ble_info[inx].cmd_in_use    = 0;
1386           ble_cb->remote_ble_info[inx].expected_resp = 0;
1387         }
1388         break;
1389       }
1390     }
1391   }
1392 }
1393 /**
1394  * @brief       Chip memory status
1395  * @param[in]   ble_on_chip_memory_status_event     - Memory status
1396  * @return      0              - Success \n
1397  *              Non-Zero Value - Failure
1398  *
1399  */
rsi_ble_on_chip_memory_status_callbacks_register(chip_ble_buffers_stats_handler_t ble_on_chip_memory_status_event)1400 void rsi_ble_on_chip_memory_status_callbacks_register(chip_ble_buffers_stats_handler_t ble_on_chip_memory_status_event)
1401 {
1402 
1403   SL_PRINTF(SL_RSI_BLE_CHIP_MEMORY_STATUS_CALLBACKS_REGISTER, BLE, LOG_INFO);
1404   // Get ble cb struct pointer
1405   rsi_ble_cb_t *ble_specific_cb = rsi_driver_cb->ble_cb->bt_global_cb->ble_specific_cb;
1406 
1407   // Assign the call backs to the respective call back
1408   ble_specific_cb->ble_on_chip_memory_status_event = ble_on_chip_memory_status_event;
1409 }
1410 
1411 /**
1412  * @brief       Form the payload of the BT command packet
1413  * @param[in]   cmd_type     - Type of the command
1414  * @param[in]   cmd_stuct    - Pointer of the command structure
1415  * @param[out]  pkt          - Pointer of the packet to fill the contents of the payload
1416  * @return      0              - Success \n
1417  *              Non-Zero Value - Failure
1418  *
1419  */
1420 
rsi_bt_prepare_common_pkt(uint16_t cmd_type,void * cmd_struct,sl_si91x_packet_t * pkt)1421 uint16_t rsi_bt_prepare_common_pkt(uint16_t cmd_type, void *cmd_struct, sl_si91x_packet_t *pkt)
1422 {
1423 
1424   SL_PRINTF(SL_RSI_BT_PREPARE_COMMON_PACKET_TRIGGER, BLUETOOTH, LOG_INFO, "COMMAND_TYPE: %2x", cmd_type);
1425   uint16_t payload_size = 0;
1426 
1427   switch (cmd_type) {
1428     case RSI_BT_SET_LOCAL_NAME: {
1429       payload_size = sizeof(rsi_bt_req_set_local_name_t);
1430       memcpy(pkt->data, cmd_struct, payload_size);
1431     } break;
1432     case RSI_BT_SET_BD_ADDR_REQ: {
1433       rsi_bt_cb_t *bt_cb = rsi_driver_cb->bt_common_cb;
1434       if (bt_cb->state == RSI_BT_STATE_OPERMODE_DONE) {
1435         bt_cb->state = RSI_BT_STATE_NONE;
1436         payload_size = sizeof(rsi_bt_set_local_bd_addr_t);
1437         memcpy(pkt->data, cmd_struct, payload_size);
1438       }
1439     } break;
1440 
1441     case RSI_BT_GET_RSSI: {
1442       payload_size = sizeof(rsi_bt_get_rssi_t);
1443       memcpy(pkt->data, cmd_struct, payload_size);
1444     } break;
1445     case RSI_BT_GET_LOCAL_DEV_ADDR:
1446     case RSI_BT_GET_LOCAL_NAME:
1447     case RSI_BT_REQ_INIT:
1448     case RSI_BT_REQ_DEINIT:
1449     case RSI_BT_GET_BT_STACK_VERSION:
1450       break;
1451     case RSI_BT_SET_ANTENNA_SELECT: {
1452       payload_size = sizeof(rsi_ble_set_antenna_t);
1453       memcpy(pkt->data, cmd_struct, payload_size);
1454     } break;
1455     case RSI_BT_SET_FEATURES_BITMAP: {
1456       payload_size = sizeof(rsi_bt_set_feature_bitmap_t);
1457       memcpy(pkt->data, cmd_struct, payload_size);
1458     } break;
1459     case RSI_BT_SET_ANTENNA_TX_POWER_LEVEL: {
1460       payload_size = sizeof(rsi_bt_set_antenna_tx_power_level_t);
1461       memcpy(pkt->data, cmd_struct, payload_size);
1462     } break;
1463 
1464     case RSI_BLE_ONLY_OPER_MODE: {
1465       payload_size = sizeof(rsi_ble_oper_mode_t);
1466       memcpy(pkt->data, cmd_struct, payload_size);
1467     } break;
1468 
1469     case RSI_BLE_REQ_PWRMODE: {
1470       payload_size = sizeof(rsi_ble_power_mode_t);
1471       memcpy(pkt->data, cmd_struct, payload_size);
1472     } break;
1473     case RSI_BLE_REQ_SOFTRESET:
1474       break;
1475     case RSI_BT_REQ_PER_CMD: {
1476       pkt->data[0] = *(uint8_t *)cmd_struct;
1477       switch (pkt->data[0]) {
1478         case HCI_BT_PER_STATS_CMD_ID:
1479           payload_size = 1;
1480           pkt->data[0] = *(uint8_t *)cmd_struct;
1481           break;
1482         case HCI_BLE_TRANSMIT_CMD_ID:
1483           payload_size = sizeof(rsi_ble_per_transmit_t);
1484           memcpy(pkt->data, cmd_struct, payload_size);
1485           break;
1486         case HCI_BLE_RECEIVE_CMD_ID:
1487           payload_size = sizeof(rsi_ble_per_receive_t);
1488           memcpy(pkt->data, cmd_struct, payload_size);
1489           break;
1490         default:
1491           return RSI_ERROR_INVALID_PARAM;
1492       }
1493     } break;
1494     case RSI_BT_VENDOR_SPECIFIC: {
1495       pkt->data[0] = ((uint8_t *)cmd_struct)[0];
1496       pkt->data[1] = ((uint8_t *)cmd_struct)[1];
1497       switch (pkt->data[0] | (pkt->data[1] << 8)) {
1498         case BLE_VENDOR_RF_TYPE_CMD_OPCODE:
1499           payload_size = sizeof(rsi_ble_vendor_rf_type_t);
1500           memcpy(pkt->data, cmd_struct, payload_size);
1501           break;
1502         case BLE_VENDOR_ACCEPTLIST_USING_ADV_DATA_PAYLOAD:
1503           payload_size = sizeof(rsi_ble_req_acceptlist_using_payload_t);
1504           memcpy(pkt->data, cmd_struct, payload_size);
1505           break;
1506         default:
1507           break;
1508       }
1509     } break;
1510     case RSI_BT_SET_GAIN_TABLE_OFFSET_OR_MAX_POWER_UPDATE: {
1511       payload_size = sizeof(rsi_bt_cmd_update_gain_table_offset_or_maxpower_t);
1512       memcpy(pkt->data, cmd_struct, payload_size);
1513     } break;
1514     default:
1515       break;
1516   }
1517 
1518   // Return payload_size
1519   return payload_size;
1520 }
1521 
1522 /** @} */
1523 
1524 /**
1525  * @brief       Form the payload of the BT Classic command packet.
1526  * @param[in]   cmd_type     - Type of the command
1527  * @param[in]   cmd_stuct    - Pointer of the command structure
1528  * @param[out]  pkt          - Pointer of the packet to fill the contents of the payload
1529  * @return      0              - Success \n
1530  *              Non-Zero Value - Failure
1531  */
1532 
rsi_bt_prepare_le_pkt(uint16_t cmd_type,void * cmd_struct,sl_si91x_packet_t * pkt)1533 uint16_t rsi_bt_prepare_le_pkt(uint16_t cmd_type, void *cmd_struct, sl_si91x_packet_t *pkt)
1534 {
1535 
1536   SL_PRINTF(SL_RSI_BT_PREPARE_LE_PKT_TRIGGER, BLUETOOTH, LOG_INFO, "COMMAND_TYPE: %2x", cmd_type);
1537   uint16_t payload_size       = 0;
1538   uint8_t le_buf_check        = 0;
1539   uint8_t le_cmd_inuse_check  = 0;
1540   uint8_t le_buf_in_use_check = 0;
1541   uint8_t le_buf_config_check = 0;
1542   uint8_t pkt_type            = 0;
1543   uint16_t expected_resp      = 0;
1544   uint8_t is_it_legacy_cmd    = 0;
1545   rsi_bt_cb_t *le_cb          = rsi_driver_cb->ble_cb;
1546 
1547   switch (cmd_type) {
1548     case RSI_BLE_REQ_ADV: {
1549       payload_size = sizeof(rsi_ble_req_adv_t);
1550       memcpy(pkt->data, cmd_struct, payload_size);
1551       is_it_legacy_cmd = 1;
1552     } break;
1553     case RSI_BLE_SET_ADVERTISE_DATA: {
1554       payload_size = sizeof(rsi_ble_req_adv_data_t);
1555       memcpy(pkt->data, cmd_struct, payload_size);
1556       is_it_legacy_cmd = 1;
1557     } break;
1558     case RSI_BLE_SET_SCAN_RESPONSE_DATA: {
1559       payload_size = sizeof(rsi_ble_req_scanrsp_data_t);
1560       memcpy(pkt->data, cmd_struct, payload_size);
1561       is_it_legacy_cmd = 1;
1562     } break;
1563     case RSI_BLE_REQ_SCAN: {
1564       payload_size = sizeof(rsi_ble_req_scan_t);
1565       memcpy(pkt->data, cmd_struct, payload_size);
1566       is_it_legacy_cmd = 1;
1567     } break;
1568     case RSI_BLE_REQ_CONN: {
1569       payload_size = sizeof(rsi_ble_req_conn_t);
1570       memcpy(pkt->data, cmd_struct, payload_size);
1571       is_it_legacy_cmd = 1;
1572     } break;
1573     case RSI_BLE_REQ_CONN_ENHANCE: {
1574       payload_size = sizeof(rsi_ble_req_enhance_conn_t);
1575       memcpy(pkt->data, cmd_struct, payload_size);
1576       is_it_legacy_cmd = 1;
1577     } break;
1578     case RSI_BLE_CMD_CONN_PARAMS_UPDATE: {
1579       payload_size = sizeof(rsi_ble_cmd_conn_params_update_t);
1580       memcpy(pkt->data, cmd_struct, payload_size);
1581     } break;
1582 
1583     case RSI_BLE_REQ_DISCONNECT: {
1584       payload_size = sizeof(rsi_ble_req_disconnect_t);
1585       memcpy(pkt->data, cmd_struct, payload_size);
1586     } break;
1587     case RSI_BLE_REQ_START_ENCRYPTION: {
1588       payload_size = sizeof(rsi_ble_strat_encryption_t);
1589       memcpy(pkt->data, cmd_struct, payload_size);
1590     } break;
1591 
1592     case RSI_BLE_REQ_SMP_PAIR: {
1593       payload_size = sizeof(rsi_ble_req_smp_pair_t);
1594       memcpy(pkt->data, cmd_struct, payload_size);
1595     } break;
1596 
1597     case RSI_BLE_SMP_PAIR_RESPONSE: {
1598       payload_size = sizeof(rsi_ble_smp_response_t);
1599       memcpy(pkt->data, cmd_struct, payload_size);
1600     } break;
1601 
1602     case RSI_BLE_SMP_PASSKEY: {
1603       payload_size = sizeof(rsi_ble_smp_passkey_t);
1604       memcpy(pkt->data, cmd_struct, payload_size);
1605     } break;
1606 
1607     case RSI_BLE_REQ_SMP_PAIRING_FAILED: {
1608       payload_size = sizeof(rsi_ble_req_smp_pair_failed_t);
1609       memcpy(pkt->data, cmd_struct, payload_size);
1610     } break;
1611 
1612     case RSI_BLE_REQ_HCI_RAW: {
1613       payload_size = *((uint8_t *)cmd_struct + 3);
1614       pkt_type     = *(uint8_t *)cmd_struct;
1615       if (pkt_type == 0x01) {
1616         payload_size += 3;
1617       } else {
1618         payload_size |= ((uint16_t)(*((uint8_t *)cmd_struct + 4)) << 8);
1619         payload_size += 4;
1620       }
1621       memcpy(pkt->data, ((uint8_t *)cmd_struct + 1), payload_size);
1622       le_cb->sync_rsp = 0;
1623     } break;
1624 
1625       //GATT cases
1626     case RSI_BLE_REQ_PROFILES: {
1627       payload_size = sizeof(rsi_ble_req_profiles_list_t);
1628       memcpy(pkt->data, cmd_struct, payload_size);
1629       le_cb->sync_rsp = 0;
1630     } break;
1631 
1632     case RSI_BLE_REQ_PROFILE: {
1633       payload_size = sizeof(rsi_ble_req_profile_t);
1634       memcpy(pkt->data, cmd_struct, payload_size);
1635       le_cb->sync_rsp = 0;
1636     } break;
1637 
1638     case RSI_BLE_REQ_CHAR_SERVICES: {
1639       payload_size = sizeof(rsi_ble_req_char_services_t);
1640       memcpy(pkt->data, cmd_struct, payload_size);
1641       le_cb->sync_rsp = 0;
1642     } break;
1643 
1644     case RSI_BLE_REQ_INC_SERVICES: {
1645       payload_size = sizeof(rsi_ble_req_inc_services_t);
1646       memcpy(pkt->data, cmd_struct, payload_size);
1647       le_cb->sync_rsp = 0;
1648     } break;
1649 
1650     case RSI_BLE_REQ_READ_BY_UUID: {
1651       payload_size = sizeof(rsi_ble_req_char_val_by_uuid_t);
1652       memcpy(pkt->data, cmd_struct, payload_size);
1653       le_cb->sync_rsp = 0;
1654     } break;
1655 
1656     case RSI_BLE_REQ_DESC: {
1657       payload_size = sizeof(rsi_ble_req_att_descs_t);
1658       memcpy(pkt->data, cmd_struct, payload_size);
1659       le_cb->sync_rsp = 0;
1660     } break;
1661 
1662     case RSI_BLE_REQ_READ_VAL: {
1663       payload_size = sizeof(rsi_ble_req_att_value_t);
1664       memcpy(pkt->data, cmd_struct, payload_size);
1665       le_cb->sync_rsp = 0;
1666     } break;
1667 
1668     case RSI_BLE_REQ_MULTIPLE_READ: {
1669       payload_size = sizeof(rsi_ble_req_multi_att_values_t);
1670       memcpy(pkt->data, cmd_struct, payload_size);
1671       le_cb->sync_rsp = 0;
1672     } break;
1673 
1674     case RSI_BLE_REQ_LONG_READ: {
1675       payload_size = sizeof(rsi_ble_req_long_att_value_t);
1676       memcpy(pkt->data, cmd_struct, payload_size);
1677       le_cb->sync_rsp = 0;
1678     } break;
1679 
1680     case RSI_BLE_REQ_WRITE: {
1681       payload_size = sizeof(rsi_ble_set_att_value_t);
1682       memcpy(pkt->data, cmd_struct, payload_size);
1683       le_cb->sync_rsp = 0;
1684     } break;
1685 
1686     case RSI_BLE_REQ_WRITE_NO_ACK: {
1687       payload_size = sizeof(rsi_ble_set_att_cmd_t);
1688       memcpy(pkt->data, cmd_struct, payload_size);
1689       le_buf_check = 1;
1690     } break;
1691 
1692     case RSI_BLE_REQ_LONG_WRITE: {
1693       payload_size = sizeof(rsi_ble_set_long_att_value_t);
1694       memcpy(pkt->data, cmd_struct, payload_size);
1695       le_cb->sync_rsp = 0;
1696     } break;
1697 
1698     case RSI_BLE_REQ_PREPARE_WRITE: {
1699       payload_size = sizeof(rsi_ble_req_prepare_write_t);
1700       memcpy(pkt->data, cmd_struct, payload_size);
1701       le_cb->sync_rsp = 0;
1702     } break;
1703 
1704     case RSI_BLE_REQ_EXECUTE_WRITE: {
1705       payload_size = sizeof(rsi_ble_req_execute_write_t);
1706       memcpy(pkt->data, cmd_struct, payload_size);
1707       le_cb->sync_rsp = 0;
1708     } break;
1709     case RSI_BLE_ADD_SERVICE: {
1710       payload_size = sizeof(rsi_ble_req_add_serv_t);
1711       memcpy(pkt->data, cmd_struct, payload_size);
1712     } break;
1713     case RSI_BLE_ADD_ATTRIBUTE: {
1714       payload_size = sizeof(rsi_ble_req_add_att_t);
1715       memcpy(pkt->data, cmd_struct, payload_size);
1716     } break;
1717     case RSI_BLE_SET_LOCAL_ATT_VALUE: {
1718       payload_size = sizeof(rsi_ble_set_local_att_value_t);
1719       memcpy(pkt->data, cmd_struct, payload_size);
1720     } break;
1721     case RSI_BLE_CMD_SET_WWO_RESP_NOTIFY_BUF_INFO: {
1722       payload_size = sizeof(rsi_ble_set_wo_resp_notify_buf_info_t);
1723       memcpy(pkt->data, cmd_struct, payload_size);
1724       le_buf_in_use_check = 1;
1725       le_buf_config_check = 1;
1726     } break;
1727     case RSI_BLE_CMD_NOTIFY: {
1728       payload_size = sizeof(rsi_ble_notify_att_value_t);
1729       memcpy(pkt->data, cmd_struct, payload_size);
1730       le_buf_check = 1;
1731     } break;
1732     case RSI_BLE_CMD_INDICATE: {
1733       payload_size = sizeof(rsi_ble_notify_att_value_t);
1734       memcpy(pkt->data, cmd_struct, payload_size);
1735       le_cmd_inuse_check = 1;
1736       expected_resp      = RSI_BLE_EVENT_GATT_INDICATE_CONFIRMATION;
1737     } break;
1738     case RSI_BLE_CMD_INDICATE_CONFIRMATION: {
1739       payload_size = sizeof(rsi_ble_indicate_confirm_t);
1740       memcpy(pkt->data, cmd_struct, payload_size);
1741     } break;
1742 
1743     case RSI_BLE_GET_LOCAL_ATT_VALUE: {
1744       payload_size = sizeof(rsi_ble_get_local_att_value_t);
1745       memcpy(pkt->data, cmd_struct, payload_size);
1746     } break;
1747     case RSI_BLE_GET_LE_PING: {
1748       payload_size = sizeof(rsi_ble_get_le_ping_timeout_t);
1749       memcpy(pkt->data, cmd_struct, payload_size);
1750     } break;
1751 
1752     case RSI_BLE_SET_LE_PING: {
1753       payload_size = sizeof(rsi_ble_set_le_ping_timeout_t);
1754       memcpy(pkt->data, cmd_struct, payload_size);
1755     } break;
1756 
1757     case RSI_BLE_ENCRYPT: {
1758       payload_size = sizeof(rsi_ble_encrypt_t);
1759       memcpy(pkt->data, cmd_struct, payload_size);
1760     } break;
1761 
1762     case RSI_BLE_SET_RANDOM_ADDRESS: {
1763       uint8_t dummy_rand_addr[6]       = { 0 };
1764       rsi_ble_req_rand_t *rsi_ble_rand = (rsi_ble_req_rand_t *)pkt->data;
1765       memcpy(rsi_ble_rand, cmd_struct, sizeof(rsi_ble_req_rand_t));
1766       if (memcmp(rsi_ble_rand->rand_addr, dummy_rand_addr, 6) == 0) {
1767         rsi_ascii_dev_address_to_6bytes_rev(rsi_ble_rand->rand_addr, (int8_t *)RSI_BLE_SET_RAND_ADDR);
1768       }
1769       // fill payload size
1770       payload_size = sizeof(rsi_ble_req_rand_t);
1771     } break;
1772 
1773     case RSI_BLE_CMD_READ_RESP: {
1774       payload_size = sizeof(rsi_ble_gatt_read_response_t);
1775       memcpy(pkt->data, cmd_struct, payload_size);
1776     } break;
1777 
1778     case RSI_BLE_LE_ACCEPT_LIST: {
1779       payload_size = sizeof(rsi_ble_accept_list_t);
1780       memcpy(pkt->data, cmd_struct, payload_size);
1781     } break;
1782 
1783     case RSI_BLE_RSP_REMOVE_SERVICE: {
1784       payload_size = sizeof(rsi_ble_gatt_remove_serv_t);
1785       memcpy(pkt->data, cmd_struct, payload_size);
1786     } break;
1787 
1788     case RSI_BLE_RSP_REMOVE_ATTRIBUTE: {
1789       payload_size = sizeof(rsi_ble_gatt_remove_att_t);
1790       memcpy(pkt->data, cmd_struct, payload_size);
1791     } break;
1792 
1793     case RSI_BLE_PROCESS_RESOLV_LIST: {
1794       payload_size = sizeof(rsi_ble_resolvlist_t);
1795       memcpy(pkt->data, cmd_struct, payload_size);
1796     } break;
1797 
1798     case RSI_BLE_GET_RESOLVING_LIST_SIZE:
1799       break;
1800 
1801     case RSI_BLE_SET_ADDRESS_RESOLUTION_ENABLE: {
1802       payload_size = sizeof(rsi_ble_set_addr_resolution_enable_t);
1803       memcpy(pkt->data, cmd_struct, payload_size);
1804     } break;
1805 
1806     case RSI_BLE_SET_PRIVACY_MODE: {
1807       payload_size = sizeof(rsi_ble_set_privacy_mode_t);
1808       memcpy(pkt->data, cmd_struct, payload_size);
1809     } break;
1810 
1811     case RSI_BLE_REQ_READ_PHY: {
1812       payload_size = sizeof(rsi_ble_req_read_phy_t);
1813       memcpy(pkt->data, cmd_struct, payload_size);
1814     } break;
1815 
1816     case RSI_BLE_REQ_SET_PHY: {
1817       payload_size = sizeof(rsi_ble_set_phy_t);
1818       memcpy(pkt->data, cmd_struct, payload_size);
1819     } break;
1820 
1821     case RSI_BLE_SET_DATA_LEN: {
1822       payload_size = sizeof(rsi_ble_setdatalength_t);
1823       memcpy(pkt->data, cmd_struct, payload_size);
1824     } break;
1825 
1826     case RSI_BLE_CBFC_CONN_REQ: {
1827       payload_size = sizeof(rsi_ble_cbfc_conn_req_t);
1828       memcpy(pkt->data, cmd_struct, payload_size);
1829     } break;
1830 
1831     case RSI_BLE_CBFC_CONN_RESP: {
1832       payload_size = sizeof(rsi_ble_cbfc_conn_resp_t);
1833       memcpy(pkt->data, cmd_struct, payload_size);
1834     } break;
1835 
1836     case RSI_BLE_CBFC_TX_DATA: {
1837       payload_size = sizeof(rsi_ble_cbfc_data_tx_t);
1838       memcpy(pkt->data, cmd_struct, payload_size);
1839     } break;
1840 
1841     case RSI_BLE_CBFC_DISCONN: {
1842       payload_size = sizeof(rsi_ble_cbfc_disconn_t);
1843       memcpy(pkt->data, cmd_struct, payload_size);
1844     } break;
1845 
1846     case RSI_BLE_CMD_ATT_ERROR: {
1847       payload_size = sizeof(rsi_ble_att_error_response_t);
1848       memcpy(pkt->data, cmd_struct, payload_size);
1849     } break;
1850 
1851     case RSI_BLE_LE_LTK_REQ_REPLY: {
1852       payload_size = sizeof(rsi_ble_set_le_ltkreqreply_t);
1853       memcpy(pkt->data, cmd_struct, payload_size);
1854     } break;
1855 
1856     case RSI_BLE_RX_TEST_MODE: {
1857       payload_size = sizeof(rsi_ble_rx_test_mode_t);
1858       memcpy(pkt->data, cmd_struct, payload_size);
1859     } break;
1860 
1861     case RSI_BLE_TX_TEST_MODE: {
1862       payload_size = sizeof(rsi_ble_tx_test_mode_t);
1863       memcpy(pkt->data, cmd_struct, payload_size);
1864     } break;
1865     case RSI_BLE_REQ_PROFILES_ASYNC: {
1866       payload_size = sizeof(rsi_ble_req_profiles_list_t);
1867       memcpy(pkt->data, cmd_struct, payload_size);
1868       le_cmd_inuse_check = 1;
1869       expected_resp      = RSI_BLE_EVENT_GATT_PRIMARY_SERVICE_LIST;
1870     } break;
1871 
1872     case RSI_BLE_REQ_PROFILE_ASYNC: {
1873       payload_size = sizeof(rsi_ble_req_profile_t);
1874       memcpy(pkt->data, cmd_struct, payload_size);
1875       le_cmd_inuse_check = 1;
1876       expected_resp      = RSI_BLE_EVENT_GATT_PRIMARY_SERVICE_BY_UUID;
1877     } break;
1878     case RSI_BLE_GET_CHARSERVICES_ASYNC: {
1879       payload_size = sizeof(rsi_ble_req_char_services_t);
1880       memcpy(pkt->data, cmd_struct, payload_size);
1881       le_cmd_inuse_check = 1;
1882       expected_resp      = RSI_BLE_EVENT_GATT_READ_CHAR_SERVS;
1883     } break;
1884 
1885     case RSI_BLE_GET_INCLUDESERVICES_ASYNC: {
1886       payload_size = sizeof(rsi_ble_req_inc_services_t);
1887       memcpy(pkt->data, cmd_struct, payload_size);
1888       le_cmd_inuse_check = 1;
1889       expected_resp      = RSI_BLE_EVENT_GATT_READ_INC_SERVS;
1890     } break;
1891 
1892     case RSI_BLE_READCHARVALUEBYUUID_ASYNC: {
1893       payload_size = sizeof(rsi_ble_req_char_val_by_uuid_t);
1894       memcpy(pkt->data, cmd_struct, payload_size);
1895       le_cmd_inuse_check = 1;
1896       expected_resp      = RSI_BLE_EVENT_GATT_READ_VAL_BY_UUID;
1897     } break;
1898 
1899     case RSI_BLE_GET_ATTRIBUTE_ASYNC: {
1900       payload_size = sizeof(rsi_ble_req_att_descs_t);
1901       memcpy(pkt->data, cmd_struct, payload_size);
1902       le_cmd_inuse_check = 1;
1903       expected_resp      = RSI_BLE_EVENT_GATT_DESC_VAL_RESPONSE;
1904     } break;
1905 
1906     case RSI_BLE_GET_DESCRIPTORVALUE_ASYNC: {
1907       payload_size = sizeof(rsi_ble_req_att_value_t);
1908       memcpy(pkt->data, cmd_struct, payload_size);
1909       le_cmd_inuse_check = 1;
1910       expected_resp      = RSI_BLE_EVENT_GATT_READ_RESP;
1911     } break;
1912 
1913     case RSI_BLE_GET_MULTIPLEVALUES_ASYNC: {
1914       payload_size = sizeof(rsi_ble_req_multi_att_values_t);
1915       memcpy(pkt->data, cmd_struct, payload_size);
1916       le_cmd_inuse_check = 1;
1917       expected_resp      = RSI_BLE_EVENT_GATT_READ_MULTIPLE_RESP;
1918     } break;
1919 
1920     case RSI_BLE_GET_LONGDESCVALUES_ASYNC: {
1921       payload_size = sizeof(rsi_ble_req_long_att_value_t);
1922       memcpy(pkt->data, cmd_struct, payload_size);
1923       le_cmd_inuse_check = 1;
1924       expected_resp      = RSI_BLE_EVENT_GATT_READ_BLOB_RESP;
1925     } break;
1926     case RSI_BLE_SET_DESCVALUE_ASYNC: {
1927       payload_size = sizeof(rsi_ble_set_att_value_t);
1928       memcpy(pkt->data, cmd_struct, payload_size);
1929       le_cmd_inuse_check = 1;
1930       expected_resp      = RSI_BLE_EVENT_GATT_WRITE_RESP;
1931     } break;
1932     case RSI_BLE_SET_PREPAREWRITE_ASYNC: {
1933       payload_size = sizeof(rsi_ble_req_prepare_write_t);
1934       memcpy(pkt->data, cmd_struct, payload_size);
1935       le_cmd_inuse_check = 1;
1936       expected_resp      = RSI_BLE_EVENT_GATT_PREPARE_WRITE_RESP;
1937     } break;
1938     case RSI_BLE_EXECUTE_LONGDESCWRITE_ASYNC: {
1939       payload_size = sizeof(rsi_ble_req_execute_write_t);
1940       memcpy(pkt->data, cmd_struct, payload_size);
1941       le_cmd_inuse_check = 1;
1942       expected_resp      = RSI_BLE_EVENT_GATT_EXECUTE_WRITE_RESP;
1943     } break;
1944     case RSI_BLE_SET_SMP_PAIRING_CAPABILITY_DATA: {
1945       payload_size = sizeof(rsi_ble_set_smp_pairing_capabilty_data_t);
1946       memcpy(pkt->data, cmd_struct, payload_size);
1947     } break;
1948     case RSI_BLE_CONN_PARAM_RESP_CMD: {
1949       payload_size = sizeof(rsi_ble_cmd_conn_param_resp_t);
1950       memcpy(pkt->data, cmd_struct, payload_size);
1951     } break;
1952 
1953     case RSI_BLE_END_TEST_MODE:
1954       break;
1955     case RSI_BLE_MTU_EXCHANGE_REQUEST: {
1956       payload_size = sizeof(rsi_ble_mtu_exchange_t);
1957       memcpy(pkt->data, cmd_struct, payload_size);
1958       le_cmd_inuse_check = 1;
1959       expected_resp      = RSI_BLE_EVENT_MTU;
1960     } break;
1961     case RSI_BLE_CMD_MTU_EXCHANGE_RESP: {
1962       payload_size = sizeof(rsi_ble_mtu_exchange_resp_t);
1963       memcpy(pkt->data, cmd_struct, payload_size);
1964     } break;
1965     case RSI_BLE_CMD_WRITE_RESP: {
1966       payload_size = sizeof(rsi_ble_gatt_write_response_t);
1967       memcpy(pkt->data, cmd_struct, payload_size);
1968     } break;
1969 
1970     case RSI_BLE_CMD_PREPARE_WRITE_RESP: {
1971       payload_size = sizeof(rsi_ble_gatt_prepare_write_response_t);
1972       memcpy(pkt->data, cmd_struct, payload_size);
1973     } break;
1974 
1975     case RSI_BLE_CMD_SET_LOCAL_IRK: {
1976       payload_size = sizeof(rsi_ble_set_local_irk_t);
1977       memcpy(pkt->data, cmd_struct, payload_size);
1978     } break;
1979     case RSI_BLE_CMD_SET_BLE_TX_POWER: {
1980       payload_size = sizeof(rsi_ble_set_ble_tx_power_t);
1981       memcpy(pkt->data, cmd_struct, payload_size);
1982     } break;
1983     case RSI_BLE_CMD_INDICATE_SYNC: {
1984       payload_size = sizeof(rsi_ble_notify_att_value_t);
1985       memcpy(pkt->data, cmd_struct, payload_size);
1986       le_cb->sync_rsp = 0;
1987     } break;
1988 
1989     //AE ENABLE
1990     case RSI_BLE_CMD_AE: {
1991       payload_size = sizeof(rsi_ble_ae_pdu_t);
1992       memcpy(pkt->data, cmd_struct, payload_size);
1993 
1994       if (le_cb->state & RSI_BLE_LEGACY_CMD_USED) {
1995         le_cb->state |= RSI_BLE_CHECK_CMD;
1996       } else {
1997         le_cb->state |= RSI_BLE_ADV_EXT_CMD_USED;
1998       }
1999     } break;
2000 
2001     case RSI_BLE_CMD_READ_TRANSMIT_POWER: {
2002       // No TX payload
2003     } break;
2004 
2005     case RSI_BLE_CMD_READ_RF_PATH_COMP: {
2006       // No Tx Payload
2007     } break;
2008 
2009     case RSI_BLE_CMD_WRITE_RF_PATH_COMP: {
2010       payload_size = sizeof(rsi_ble_write_rf_path_comp_t);
2011       memcpy(pkt->data, cmd_struct, payload_size);
2012     } break;
2013 
2014     default:
2015       break;
2016   }
2017 
2018   if (le_buf_check || le_cmd_inuse_check || le_buf_in_use_check) {
2019     uint8_t *remote_dev_bd_addr = (uint8_t *)cmd_struct;
2020     for (uint8_t inx = 0; inx < (RSI_BLE_MAX_NBR_PERIPHERALS + RSI_BLE_MAX_NBR_CENTRALS); inx++) {
2021       if (!memcmp(le_cb->remote_ble_info[inx].remote_dev_bd_addr, remote_dev_bd_addr, RSI_DEV_ADDR_LEN)) {
2022 
2023         /* ERROR PRONE : Do not changes if else checks order */
2024         if (le_buf_config_check) {
2025           if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
2026             osMutexAcquire(le_cb->remote_ble_info[inx].ble_buff_mutex, 0xFFFFFFFFUL);
2027           }
2028           if ((le_cb->remote_ble_info[inx].avail_buf_cnt) != (le_cb->remote_ble_info[inx].max_buf_cnt)) {
2029             le_cb->buf_status = 2; //return error based on the status
2030             if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
2031               osMutexRelease(le_cb->remote_ble_info[inx].ble_buff_mutex);
2032             }
2033             break;
2034           }
2035           if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
2036             osMutexRelease(le_cb->remote_ble_info[inx].ble_buff_mutex);
2037           }
2038         }
2039 
2040         if (le_buf_in_use_check) {
2041           le_cb->remote_ble_index = inx;
2042           le_buf_in_use_check     = RSI_FALSE;
2043           break;
2044         } else if (le_buf_check) {
2045           if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
2046             osMutexAcquire(le_cb->remote_ble_info[inx].ble_buff_mutex, 0xFFFFFFFFUL);
2047           }
2048           if (le_cb->remote_ble_info[inx].avail_buf_cnt == 0) {
2049             le_cb->buf_status = SI_LE_BUFFER_FULL;
2050             if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
2051               osMutexRelease(le_cb->remote_ble_info[inx].ble_buff_mutex);
2052             }
2053             break;
2054           } else {
2055             le_cb->buf_status = SI_LE_BUFFER_AVL;
2056             le_cb->remote_ble_info[inx].avail_buf_cnt -= 1;
2057             if (le_cb->remote_ble_info[inx].ble_buff_mutex) {
2058               osMutexRelease(le_cb->remote_ble_info[inx].ble_buff_mutex);
2059             }
2060             break;
2061           }
2062         } else if (le_cmd_inuse_check) {
2063           if (le_cb->remote_ble_info[inx].cmd_in_use) {
2064             le_cb->cmd_status = RSI_TRUE;
2065           } else {
2066             le_cb->cmd_status                         = RSI_FALSE;
2067             le_cb->remote_ble_info[inx].cmd_in_use    = RSI_TRUE;
2068             le_cb->remote_ble_info[inx].expected_resp = expected_resp;
2069           }
2070         }
2071         break;
2072       }
2073     }
2074   }
2075 
2076   if (is_it_legacy_cmd) {
2077     if (le_cb->state & RSI_BLE_ADV_EXT_CMD_USED) {
2078       le_cb->state |= RSI_BLE_CHECK_CMD; //bt_cb->state
2079     } else {
2080       le_cb->state |= RSI_BLE_LEGACY_CMD_USED; //bt_cb->state
2081     }
2082   }
2083   // Return payload_size
2084   return payload_size;
2085 }
2086 
2087 /**
2088  * @brief       Fill commands and places into Bt TX queue
2089  * @param[in]   cmd          - Type of the command to send
2090  * @param[in]   cmd_stuct    - Pointer of the packet structure to send
2091  * @param[in]   resp         - Pointer of the packet to fill the contents of the payload
2092  * @return      0              - Success \n
2093  *              Non-Zero Value - Failure
2094  */
2095 
rsi_bt_driver_send_cmd(uint16_t cmd,void * cmd_struct,void * resp)2096 int32_t rsi_bt_driver_send_cmd(uint16_t cmd, void *cmd_struct, void *resp)
2097 {
2098 
2099   SL_PRINTF(SL_RSI_BT_SEND_CMD_TRIGGER, BLUETOOTH, LOG_INFO, "COMMAND: %2x", cmd);
2100   uint16_t payload_size         = 0;
2101   uint16_t protocol_type        = 0;
2102   int32_t status                = RSI_SUCCESS;
2103   sl_si91x_packet_t *pkt        = NULL;
2104   uint8_t *host_desc            = NULL;
2105   rsi_bt_cb_t *bt_cb            = NULL;
2106   uint32_t calculate_timeout_ms = 0;
2107 
2108   sl_wifi_buffer_t *buffer = NULL;
2109 
2110   protocol_type = rsi_bt_get_proto_type(cmd, &bt_cb);
2111 
2112   SL_PRINTF(SL_RSI_BT_SEND_CMD_PROTOCOL_TYPE, BLUETOOTH, LOG_INFO, "PROTOCOL_TYPE: %2x", protocol_type);
2113   if (protocol_type == 0xFF) {
2114     // Return packet allocation failure error
2115     SL_PRINTF(SL_RSI_ERROR_PACKET_ALLOCATION_FAILURE,
2116               BLUETOOTH,
2117               LOG_ERROR,
2118               "COMMAND: %2x , PROTOCOL_TYPE: %2x",
2119               protocol_type,
2120               cmd);
2121 
2122     return RSI_ERROR_PKT_ALLOCATION_FAILURE;
2123   }
2124 
2125   if ((cmd == RSI_BT_SET_BD_ADDR_REQ) && (rsi_driver_cb->bt_common_cb->state != RSI_BT_STATE_OPERMODE_DONE)) {
2126     SL_PRINTF(SL_RSI_ERROR_COMMAND_GIVEN_IN_WORNG_STATE, BLUETOOTH, LOG_ERROR, "COMMAND: %2x", cmd);
2127 
2128     return RSI_ERROR_COMMAND_GIVEN_IN_WRONG_STATE;
2129   }
2130   // Get timeout based on cmd
2131   calculate_timeout_ms = rsi_bt_get_timeout(cmd, protocol_type);
2132   if (bt_cb->bt_cmd_sem == NULL || (osSemaphoreAcquire(bt_cb->bt_cmd_sem, calculate_timeout_ms) != osOK)) {
2133     // LOG_PRINT("%s: Command ID:0x%x Command timed-out with:%d\n",__func__, cmd, calculate_timeout_ms);
2134     SL_PRINTF(SL_RSI_ERROR_BT_BLE_CMD_IN_PROGRESS,
2135               BLUETOOTH,
2136               LOG_ERROR,
2137               "COMMAND: %2x, Calculate_timeout_ms: %4x",
2138               cmd,
2139               calculate_timeout_ms);
2140 
2141     return RSI_ERROR_BT_BLE_CMD_IN_PROGRESS;
2142   }
2143 
2144   // Allocate command buffer from ble pool
2145   status = sli_si91x_allocate_command_buffer(&buffer,
2146                                              (void **)&pkt,
2147                                              sizeof(sl_si91x_packet_t) + RSI_BT_COMMON_CMD_LEN,
2148                                              calculate_timeout_ms);
2149   // If allocation of packet fails
2150   if (pkt == NULL) {
2151     osSemaphoreRelease(bt_cb->bt_cmd_sem);
2152 
2153     // Return packet allocation failure error
2154     SL_PRINTF(SL_RSI_ERROR_PKT_ALLOCATION_FAILURE, BLUETOOTH, LOG_ERROR, "COMMAND: %2x", cmd);
2155 
2156     return RSI_ERROR_PKT_ALLOCATION_FAILURE;
2157   }
2158 
2159   // Memset host descriptor
2160   memset(pkt->desc, 0, sizeof(pkt->desc));
2161 
2162   // Get host descriptor pointer
2163   host_desc = (pkt->desc);
2164 
2165   bt_cb->sync_rsp = 1;
2166   payload_size    = 0;
2167 
2168   if (protocol_type == RSI_PROTO_BT_COMMON) {
2169     // Memset data
2170     memset(pkt->data, 0, (RSI_BT_COMMON_CMD_LEN - sizeof(sl_si91x_packet_t)));
2171     payload_size = rsi_bt_prepare_common_pkt(cmd, cmd_struct, pkt);
2172   } else if (protocol_type == RSI_PROTO_BLE) {
2173     // Memset data
2174     memset(pkt->data, 0, (RSI_BLE_CMD_LEN - sizeof(sl_si91x_packet_t)));
2175     payload_size = rsi_bt_prepare_le_pkt(cmd, cmd_struct, pkt);
2176     if (cmd == RSI_BLE_REQ_CONN_ENHANCE) {
2177       cmd = RSI_BLE_REQ_CONN;
2178     }
2179   }
2180 
2181   if (bt_cb->buf_status || bt_cb->cmd_status || (bt_cb->state & RSI_BLE_CHECK_CMD)) {
2182     sl_si91x_host_free_buffer(buffer);
2183 
2184     if (bt_cb->buf_status == SI_LE_BUFFER_IN_PROGRESS) {
2185       status = RSI_ERROR_BLE_DEV_BUF_IS_IN_PROGRESS;
2186     } else if (bt_cb->buf_status == SI_LE_BUFFER_FULL) {
2187       status = RSI_ERROR_BLE_DEV_BUF_FULL;
2188     } else if (bt_cb->cmd_status) {
2189       status = RSI_ERROR_BLE_ATT_CMD_IN_PROGRESS;
2190     }
2191 
2192     if (bt_cb->state & RSI_BLE_CHECK_CMD) {
2193       if (bt_cb->state & RSI_BLE_LEGACY_CMD_USED) {
2194         status = RSI_ERROR_BLE_ADV_EXT_COMMAND_NOT_ALLOWED;
2195       } else if (bt_cb->state & RSI_BLE_ADV_EXT_CMD_USED) {
2196         status = RSI_ERROR_BLE_LEGACY_COMMAND_NOT_ALLOWED;
2197       }
2198     }
2199 
2200     bt_cb->buf_status = SI_LE_BUFFER_AVL;
2201     bt_cb->cmd_status = 0;
2202     osSemaphoreRelease(bt_cb->bt_cmd_sem);
2203     SL_PRINTF(SL_RSI_BLE_ERROR, BLUETOOTH, LOG_ERROR, "Status: %4x", status);
2204 
2205     return status;
2206   }
2207   if (cmd == RSI_BLE_REQ_HCI_RAW) {
2208     host_desc[14] = *(uint8_t *)cmd_struct;
2209   }
2210 
2211   rsi_uint16_to_2bytes(host_desc, (payload_size & 0xFFF));
2212   rsi_uint16_to_2bytes(&host_desc[2], cmd);
2213 
2214   // Save expected response type
2215   bt_cb->expected_response_type = cmd;
2216 
2217   // Save expected response type
2218   bt_cb->expected_response_buffer = resp;
2219 
2220   if (cmd == RSI_BLE_ONLY_OPER_MODE) {
2221     // Save expected response type
2222     bt_cb->expected_response_type = RSI_BT_EVENT_CARD_READY;
2223     bt_cb->sync_rsp               = 1;
2224   }
2225 
2226   sl_si91x_driver_send_bt_command(cmd, SI91X_BT_CMD, buffer, bt_cb->sync_rsp);
2227 
2228   if (bt_cb->bt_sem == NULL || (osSemaphoreAcquire(bt_cb->bt_sem, calculate_timeout_ms) != osOK)) {
2229     rsi_bt_set_status(bt_cb, RSI_ERROR_RESPONSE_TIMEOUT);
2230     SL_PRINTF(SL_RSI_SEMAPHORE_TIMEOUT,
2231               BLUETOOTH,
2232               LOG_ERROR,
2233               " Command: %2x , Calculate_timeout_ms: %4x",
2234               cmd,
2235               calculate_timeout_ms);
2236   }
2237   // Get command response status
2238   status = rsi_bt_get_status(bt_cb);
2239 
2240   SL_PRINTF(SL_RSI_BT_COMMAND_RESPONSE_STATUS, BLUETOOTH, LOG_INFO, "STATUS: %4x", status);
2241   // Clear sync rsp variable
2242   bt_cb->sync_rsp = 0;
2243 
2244   // Post the semaphore which is waiting on driver_send API
2245   osSemaphoreRelease(bt_cb->bt_cmd_sem);
2246 
2247   // Return status
2248   return status;
2249 }
2250 /** @} */
2251 
2252 /*==============================================*/
2253