Added the possibility to set the battery service's battery value and started to add a descriptor to play around with nimble

This commit is contained in:
Th3maz1ng 2023-04-10 21:34:14 +02:00
parent 5206181a32
commit 338c7e18e6
2 changed files with 24 additions and 3 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 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);
@ -61,7 +62,18 @@ static struct ble_gatt_svc_def gatt_svc[] =
.uuid = BLE_UUID16_DECLARE(0x2A19),
.val_handle = &battery_level_char_handle,
.access_cb = &(battery_level_char_access_cb),
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
/*.descriptors = (struct ble_gatt_dsc_def[])
{
{
.uuid = BLE_UUID,
.access_cb = ,
.att_flags = BLE_ATT_F_READ;
},
{
.uuid = NULL
}
}*/
},
{
.uuid = NULL
@ -352,6 +364,13 @@ 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)
{
if(value > 100) value = 100;
_battery_level_value = value;
}
/**
* PRIVATE FUNCTION DEFINITION
* Used for the internal workings of the service
@ -727,7 +746,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));
}

View File

@ -123,4 +123,6 @@ 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_set_battery_value(uint8_t value);
#endif //BLE_APP_H