Introduced a new event which can be passed to the calling application : BLE_SERVICE_MODE_UNSUBSCRIBED, added a function to the API allowing to set the battery service level (still need to check if a device is subscribed to the characteristic to send the new value ot it).

This commit is contained in:
anschrammh 2023-04-20 13:08:23 +02:00
parent 03f86ea483
commit 5f7ac5a1d1
2 changed files with 30 additions and 6 deletions

View File

@ -38,7 +38,8 @@ static struct ble_gap_event_listener ble_gap_event_listener;
static int battery_level_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); static int battery_level_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
static uint16_t battery_level_char_handle = 0; static uint16_t battery_level_char_handle = 0;
static uint8_t battery_level_value = 42; // Default battery value set to 100%
static uint8_t _battery_level_value = 100;
static int gatt_nus_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg); static int gatt_nus_char_access_cb(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg);
@ -304,7 +305,7 @@ bool ble_service_update_connection_parameters(
gap_params_to_apply.min_ce_len = min_ce_len; gap_params_to_apply.min_ce_len = min_ce_len;
gap_params_to_apply.max_ce_len = max_ce_len; gap_params_to_apply.max_ce_len = max_ce_len;
if ((status = ble_gap_update_params(ble_device_conn_handle, &gap_params_to_apply)) != BLE_HS_ENOERR) if((status = ble_gap_update_params(ble_device_conn_handle, &gap_params_to_apply)) != BLE_HS_ENOERR)
{ {
TLS_BT_APPL_TRACE_ERROR("%s, ble_gap_update_params failed %s" NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); TLS_BT_APPL_TRACE_ERROR("%s, ble_gap_update_params failed %s" NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status));
return false; return false;
@ -352,6 +353,12 @@ 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; _ble_service_nus_data_rx_cb = nus_data_rx_cb;
} }
void ble_service_set_battery_value(uint8_t value)
{
_battery_level_value = value > 100 ? 100 : value;
}
/** /**
* PRIVATE FUNCTION DEFINITION * PRIVATE FUNCTION DEFINITION
* Used for the internal workings of the service * Used for the internal workings of the service
@ -661,10 +668,19 @@ static int ble_gap_event_cb(struct ble_gap_event *event, void *arg)
event->subscribe.prev_notify event->subscribe.prev_notify
); );
if(gatt_nus_char_tx_handle == event->subscribe.attr_handle && (event->subscribe.cur_notify || event->subscribe.cur_indicate)) if(gatt_nus_char_tx_handle == event->subscribe.attr_handle)
{ {
//We call the state change callback if registered if(event->subscribe.cur_notify || event->subscribe.cur_indicate)
if(_ble_service_state_change_cb)_ble_service_state_change_cb(BLE_SERVICE_MODE_SUBSCRIBED); {
//We call the state change callback if registered
if(_ble_service_state_change_cb)_ble_service_state_change_cb(BLE_SERVICE_MODE_SUBSCRIBED);
}
else if(!event->subscribe.cur_notify && !event->subscribe.cur_indicate)
{
//We call the state change callback if registered
if(_ble_service_state_change_cb)_ble_service_state_change_cb(BLE_SERVICE_MODE_UNSUBSCRIBED);
}
} }
break; break;
case BLE_GAP_EVENT_MTU: case BLE_GAP_EVENT_MTU:
@ -733,7 +749,7 @@ static int battery_level_char_access_cb(uint16_t conn_handle, uint16_t attr_hand
if(attr_handle == battery_level_char_handle) if(attr_handle == battery_level_char_handle)
{ {
TLS_BT_APPL_TRACE_VERBOSE("battery level reading"NEW_LINE); TLS_BT_APPL_TRACE_VERBOSE("battery level reading"NEW_LINE);
if((status = os_mbuf_append(ctxt->om, &battery_level_value, sizeof(battery_level_value))) != BLE_HS_ENOERR) if((status = os_mbuf_append(ctxt->om, &_battery_level_value, sizeof(_battery_level_value))) != BLE_HS_ENOERR)
{ {
TLS_BT_APPL_TRACE_ERROR("%s, battery level os_mbuf : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status)); TLS_BT_APPL_TRACE_ERROR("%s, battery level os_mbuf : %s"NEW_LINE, __FUNCTION__, tls_bt_rc_2_str(status));
} }

View File

@ -16,6 +16,7 @@ typedef enum
BLE_SERVICE_MODE_ADVERTISING, BLE_SERVICE_MODE_ADVERTISING,
BLE_SERVICE_MODE_CONNECTED, BLE_SERVICE_MODE_CONNECTED,
BLE_SERVICE_MODE_SUBSCRIBED, BLE_SERVICE_MODE_SUBSCRIBED,
BLE_SERVICE_MODE_UNSUBSCRIBED,
BLE_SERVICE_MODE_INDICATING, BLE_SERVICE_MODE_INDICATING,
BLE_SERVICE_MODE_EXITING BLE_SERVICE_MODE_EXITING
} ble_service_state_e; } ble_service_state_e;
@ -124,4 +125,11 @@ bool ble_service_send_nus_data(const uint8_t *data, uint16_t length);
*/ */
void ble_service_register_nus_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);
/**
* @brief Sets the battery level in percents sent by the BLE battery service
*
* @param value the battery level to set in percents
*/
void ble_service_set_battery_value(uint8_t value);
#endif //BLE_APP_H #endif //BLE_APP_H