From 5f7ac5a1d19aef901670b72c8adc4ac545d6fc8b Mon Sep 17 00:00:00 2001 From: anschrammh Date: Thu, 20 Apr 2023 13:08:23 +0200 Subject: [PATCH] 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). --- src/W800_SDK_v1.00.10/app/ble/ble_service.c | 28 ++++++++++++++++----- src/W800_SDK_v1.00.10/app/ble/ble_service.h | 8 ++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/W800_SDK_v1.00.10/app/ble/ble_service.c b/src/W800_SDK_v1.00.10/app/ble/ble_service.c index c198f90..83a489e 100644 --- a/src/W800_SDK_v1.00.10/app/ble/ble_service.c +++ b/src/W800_SDK_v1.00.10/app/ble/ble_service.c @@ -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 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); @@ -304,7 +305,7 @@ bool ble_service_update_connection_parameters( gap_params_to_apply.min_ce_len = min_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)); 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; } + +void ble_service_set_battery_value(uint8_t value) +{ + _battery_level_value = value > 100 ? 100 : value; +} + /** * PRIVATE FUNCTION DEFINITION * 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 ); - 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(_ble_service_state_change_cb)_ble_service_state_change_cb(BLE_SERVICE_MODE_SUBSCRIBED); + 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_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; 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) { 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)); } diff --git a/src/W800_SDK_v1.00.10/app/ble/ble_service.h b/src/W800_SDK_v1.00.10/app/ble/ble_service.h index 85308ec..8cfe4a4 100644 --- a/src/W800_SDK_v1.00.10/app/ble/ble_service.h +++ b/src/W800_SDK_v1.00.10/app/ble/ble_service.h @@ -16,6 +16,7 @@ typedef enum BLE_SERVICE_MODE_ADVERTISING, BLE_SERVICE_MODE_CONNECTED, BLE_SERVICE_MODE_SUBSCRIBED, + BLE_SERVICE_MODE_UNSUBSCRIBED, BLE_SERVICE_MODE_INDICATING, BLE_SERVICE_MODE_EXITING } 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); +/** + * @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