set the battery voltage characteristic as a notification characteristic Part 1.

1. register handler for ble events
2. add eis_ccc_update() for CCCD (client characteristic configuration descriptor)
This commit is contained in:
chain40
2023-05-11 08:04:31 +08:00
parent 2f708e0113
commit 672ccaaa02
2 changed files with 92 additions and 15 deletions
+14 -12
View File
@@ -123,19 +123,21 @@ extern "C"
#define MAJOR_VERSION_NUMBER 1
#define MINOR_VERSION_NUMBER 8
#elif (DEF_ELITE_MODEL == DEF_ELITE_EIS_10)
#define ELITE_DEVICE_NAME "EIS"
#define MAJOR_PRODUCT_NUMBER 0
#define MINOR_PRODUCT_NUMBER 4
#define MAJOR_VERSION_NUMBER 1
#define MINOR_VERSION_NUMBER 0
#define BLE_EIS_ENABLED 1
#define ELITE_DEVICE_NAME "EIS"
#define MAJOR_PRODUCT_NUMBER 0
#define MINOR_PRODUCT_NUMBER 4
#define MAJOR_VERSION_NUMBER 1
#define MINOR_VERSION_NUMBER 0
#define BLE_EIS_ENABLED 1
#define EIS_BLE_OBSERVER_PRIO 3
#elif (DEF_ELITE_MODEL == DEF_ELITE_EIS_11)
#define ELITE_DEVICE_NAME "EIS"
#define MAJOR_PRODUCT_NUMBER 0
#define MINOR_PRODUCT_NUMBER 4
#define MAJOR_VERSION_NUMBER 1
#define MINOR_VERSION_NUMBER 1
#define BLE_EIS_ENABLED 1
#define ELITE_DEVICE_NAME "EIS"
#define MAJOR_PRODUCT_NUMBER 0
#define MINOR_PRODUCT_NUMBER 4
#define MAJOR_VERSION_NUMBER 1
#define MINOR_VERSION_NUMBER 1
#define BLE_EIS_ENABLED 1
#define EIS_BLE_OBSERVER_PRIO 3
#elif (DEF_ELITE_MODEL == DEF_ELITE_EIS_MINI_10)
#define ELITE_DEVICE_NAME "EIS"
#define MAJOR_PRODUCT_NUMBER 0
+78 -3
View File
@@ -6,6 +6,10 @@
#include "ble_srv_common.h"
#include "nrf_sdh_ble.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include <stdlib.h>
#include <string.h>
@@ -24,6 +28,8 @@ typedef struct
uint16_t conn_handle;
uint16_t service_handle;
ble_gatts_char_handles_t bat_volt_handles;
bool bat_volt_notify_en;
uint16_t bat_volt;
} ble_eis_t;
typedef struct
@@ -33,6 +39,69 @@ typedef struct
static ble_eis_t ble_eis;
static void on_connect(ble_eis_t *p_eis, ble_evt_t const *p_ble_evt)
{
// TODO...
}
static void on_disconnect(ble_eis_t *p_eis, ble_evt_t const *p_ble_evt)
{
// TODO...
}
static void eis_ccc_update(ble_eis_t *p_eis, ble_evt_t const *p_ble_evt)
{
ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
if (p_evt_write->handle == p_eis->bat_volt_handles.cccd_handle)
{
p_eis->bat_volt_notify_en = ble_srv_is_notification_enabled(p_evt_write->data);
NRF_LOG_INFO("bat_volt_notify:%s", p_eis->bat_volt_notify_en ? "enable" : "disable");
return;
}
}
static void on_write(ble_eis_t *p_eis, ble_evt_t const *p_ble_evt)
{
ble_uuid_t uuid = p_ble_evt->evt.gatts_evt.params.write.uuid;
switch (uuid.uuid)
{
case BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG:
eis_ccc_update(p_eis, p_ble_evt);
break;
default:
break;
}
}
static void on_hvx_tx_complete(ble_eis_t *p_eis, ble_evt_t const *p_ble_evt)
{
// TODO...
}
static void ble_eis_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
{
ble_eis_t *p_eis = (ble_eis_t *)p_context;
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
on_connect(p_eis, p_ble_evt);
break;
case BLE_GAP_EVT_DISCONNECTED:
on_disconnect(p_eis, p_ble_evt);
break;
case BLE_GATTS_EVT_WRITE:
on_write(p_eis, p_ble_evt);
break;
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
on_hvx_tx_complete(p_eis, p_ble_evt);
break;
default:
// No implementation needed.
break;
}
}
static uint32_t ble_eis_init(ble_eis_init_t const *p_eis_init)
{
uint32_t err_code;
@@ -42,6 +111,8 @@ static uint32_t ble_eis_init(ble_eis_init_t const *p_eis_init)
.uuid = BLE_UUID_EIS_SERVICE,
};
ble_eis.bat_volt = p_eis_init->battery_volt;
// Adding vendor specific UUID to the SoftDevice
err_code = sd_ble_uuid_vs_add(&base_uuid, &ble_uuid.type);
APP_ERROR_CHECK(err_code);
@@ -57,15 +128,19 @@ static uint32_t ble_eis_init(ble_eis_init_t const *p_eis_init)
add_char_params.uuid_type = ble_uuid.type;
add_char_params.max_len = sizeof(p_eis_init->battery_volt);
add_char_params.init_len = sizeof(p_eis_init->battery_volt);
add_char_params.p_init_value = (void *)&p_eis_init->battery_volt;
add_char_params.p_init_value = (void *)&ble_eis.bat_volt;
add_char_params.is_value_user = false;
add_char_params.is_var_len = false;
add_char_params.char_props.read = 1;
add_char_params.read_access = SEC_OPEN;
err_code = characteristic_add(ble_eis.service_handle, &add_char_params, &ble_eis.bat_volt_handles);
add_char_params.char_props.notify = 1;
add_char_params.cccd_write_access = SEC_OPEN;
err_code = characteristic_add(ble_eis.service_handle, &add_char_params, &ble_eis.bat_volt_handles);
APP_ERROR_CHECK(err_code);
// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(m_eis_observer, EIS_BLE_OBSERVER_PRIO, ble_eis_evt_handler, &ble_eis);
return NRF_SUCCESS;
}