Added corrections to the BLE module

This commit is contained in:
Th3maz1ng 2023-03-31 15:53:30 +02:00
parent 5b5032ec2f
commit cb4a9d77e3
4 changed files with 43 additions and 19 deletions

View File

@ -58,13 +58,13 @@ bool ble_modem_off(void)
uint8_t timeout = 0;
while(ble_service_is_started())
{
tls_os_time_delay(pdMS_TO_TICKS(1));
tls_os_time_delay(pdMS_TO_TICKS(5));
// Service is stuck ?
if(++timeout > 10)
// Service is stuck ? waiting up to 300 ms for it to stop
if(++timeout > 60)
{
serviceStopSuccess = false;
break;
TLS_BT_APPL_TRACE_ERROR("%s, ble_service_stop timeout "NEW_LINE, __FUNCTION__);
return serviceStopSuccess;
}
};
@ -88,4 +88,4 @@ bool is_ble_modem_on(void)
if(bt_adapter_state == WM_BT_STATE_OFF || bt_system_action != WM_BT_SYSTEM_ACTION_IDLE)
return false;
return true;
}
}

View File

@ -11,6 +11,7 @@
/* ble service internal workings attributes */
static volatile ble_service_state_e _ble_service_state = BLE_SERVICE_MODE_STOPPED;
static nus_data_rx_fn_t _ble_service_nus_data_rx_cb = NULL;
static ble_service_state_change_fn_t _ble_service_state_change_cb = NULL;
/* Connection handle to the connected device : only one simultaneous connection */
static uint16_t ble_device_conn_handle = BLE_HS_CONN_HANDLE_NONE;
@ -134,7 +135,7 @@ bool ble_service_start(void)
// 1 We first check if the BLE service is stopped
if(_ble_service_state != BLE_SERVICE_MODE_STOPPED)
{
TLS_BT_APPL_TRACE_WARNING("%s, ble service already running"NEW_LINE, __FUNCTION__);
TLS_BT_APPL_TRACE_WARNING("%s, ble service already running (%s)"NEW_LINE, __FUNCTION__, ble_service_state_2_str(_ble_service_state));
return true;
}
@ -268,6 +269,11 @@ bool ble_service_is_device_connected(void)
return _ble_service_state == BLE_SERVICE_MODE_CONNECTED;
}
void ble_service_register_state_change_cb(ble_service_state_change_fn_t ble_service_state_change_cb)
{
_ble_service_state_change_cb = ble_service_state_change_cb;
}
ble_service_state_e ble_service_get_state(void)
{
return _ble_service_state;
@ -325,9 +331,9 @@ bool ble_service_request_mtu_exchange(void)
return true;
}
bool ble_service_nus_send_data(const uint8_t *data, uint16_t length)
bool ble_service_send_nus_data(const uint8_t *data, uint16_t length)
{
// The NUS is TX is using notification
// The NUS is TX is using notifications
if(notification_data.transfer_in_progress)
{
TLS_BT_APPL_TRACE_WARNING("%s, a transfer is already in progress"NEW_LINE, __FUNCTION__);
@ -341,7 +347,7 @@ bool ble_service_nus_send_data(const uint8_t *data, uint16_t length)
return ble_service_send_custom_notification(gatt_nus_char_tx_handle, &notification_data);
}
void ble_service_nus_register_data_rx_cb(nus_data_rx_fn_t nus_data_rx_cb)
void ble_service_register_nus_data_rx_cb(nus_data_rx_fn_t nus_data_rx_cb)
{
_ble_service_nus_data_rx_cb = nus_data_rx_cb;
}
@ -487,9 +493,12 @@ static bool ble_service_advertise(bool enable)
extern int tls_get_bt_mac_addr(u8 *mac);
tls_get_bt_mac_addr(device_addr);
// Make sure the the device address is compliant with the random address specification :
device_addr[5] |= 0xC0;
if((status = ble_hs_id_set_rnd(device_addr)) != BLE_HS_ENOERR)
{
TLS_BT_APPL_TRACE_ERROR("%s, ble_hs_id_infer_auto failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status));
TLS_BT_APPL_TRACE_ERROR("%s, ble_hs_id_set_rnd failed : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status));
return false;
}
else
@ -557,6 +566,9 @@ static int ble_gap_event_cb(struct ble_gap_event *event, void *arg)
_ble_service_state = BLE_SERVICE_MODE_CONNECTED;
usable_mtu = USABLE_DEFAULT_MTU;
ble_device_conn_handle = event->connect.conn_handle;
//We call the state change callback if registered
if(_ble_service_state_change_cb)_ble_service_state_change_cb(_ble_service_state);
}
else
{
@ -620,6 +632,9 @@ static int ble_gap_event_cb(struct ble_gap_event *event, void *arg)
}
_ble_service_state = BLE_SERVICE_MODE_ADVERTISING;
}
//We call the state change callback if registered
if(_ble_service_state_change_cb)_ble_service_state_change_cb(_ble_service_state);
break;
case BLE_GAP_EVENT_CONN_UPDATE:
TLS_BT_APPL_TRACE_DEBUG("Conn update status : %d"NEW_LINE, event->conn_update.status);
@ -680,7 +695,7 @@ static int ble_gap_event_cb(struct ble_gap_event *event, void *arg)
else
{
TLS_BT_APPL_TRACE_VERBOSE("last data chunk sent, end of the transfer"NEW_LINE);
// All data has been sent, end of the transfer
// All data have been sent, end of the transfer
reset_data_being_sent(&notification_data);
}
}
@ -726,8 +741,7 @@ static int battery_level_char_access_cb(uint16_t conn_handle, uint16_t attr_hand
static int gatt_nus_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
{
TLS_BT_APPL_TRACE_EVENT("gatt_nus_char_access_cb op : %s"NEW_LINE, tls_bt_access_opt_2_str(ctxt->op));
//TLS_BT_APPL_TRACE_EVENT("gatt_nus_char_access_cb op : %s"NEW_LINE, tls_bt_access_opt_2_str(ctxt->op));
switch(ctxt->op)
{
case BLE_GATT_ACCESS_OP_WRITE_CHR:
@ -771,4 +785,4 @@ static void reset_data_being_sent(data_being_sent_t * const data)
{
memset(data, 0, sizeof(data_being_sent_t));
}
}
}

View File

@ -19,6 +19,8 @@ typedef enum
BLE_SERVICE_MODE_EXITING
} ble_service_state_e;
typedef void (*ble_service_state_change_fn_t)(ble_service_state_e service_state);
/**
* @brief Resturns the corresponding enum name as a string
*
@ -59,6 +61,14 @@ bool ble_service_is_started(void);
*/
bool ble_service_is_device_connected(void);
/**
* @brief Registers a callback function called every time the state of the BLE service changes.
* For example, you can register a callback to know if a device connected to the service, or disconnected
*
* @param ble_service_state_change_cb the function to register as the callback. The ble_service_state parameter is set to the new state.
*/
void ble_service_register_state_change_cb(ble_service_state_change_fn_t ble_service_state_change_cb);
/**
* @brief Returns the current state of the ble service
*
@ -103,7 +113,7 @@ bool ble_service_request_mtu_exchange(void);
* @return true on success
* @return false on failure
*/
bool ble_service_nus_send_data(const uint8_t *data, uint16_t length);
bool ble_service_send_nus_data(const uint8_t *data, uint16_t length);
/**
* @brief Registers a function which will be called every time data is received by the nus rx
@ -111,6 +121,6 @@ bool ble_service_nus_send_data(const uint8_t *data, uint16_t length);
*
* @param nus_data_rx_cb a pointer to the function to call of type nus_data_rx_fn_t
*/
void ble_service_nus_register_data_rx_cb(nus_data_rx_fn_t nus_data_rx_cb);
void ble_service_register_nus_data_rx_cb(nus_data_rx_fn_t nus_data_rx_cb);
#endif //BLE_APP_H
#endif //BLE_APP_H

View File

@ -2,7 +2,7 @@
#define BLUETOOTH_SIG_VALUES_H
#define BLE_DEVICE_APPEARANCE (0x00C2) //Smart Watch
#define BLE_DEVICE_NAME "W800SmartWatch"
#define BLE_DEVICE_NAME "MDBT42Q_DEV"
#define BLE_DEVICE_ADV_SERVICE (0x180F) //Battery Service