291 lines
10 KiB
C
291 lines
10 KiB
C
#include "sdk_common.h"
|
|
#if NRF_MODULE_ENABLED(BLE_EDC)
|
|
|
|
#include "app_error.h"
|
|
#include "ble_conn_state.h"
|
|
#include "ble_gatts.h"
|
|
#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>
|
|
|
|
/**< Used vendor specific UUID. */
|
|
#define BLE_EDC_BASE_UUID \
|
|
{ \
|
|
{ \
|
|
0x4D, 0x3C, 0x56, 0x45, 0x12, 0x8B, 0x44, 0x1D, 0x8D, 0x6F, 0xC5, 0x95, 0x00, 0x00, 0x9B, 0xD8 \
|
|
} \
|
|
}
|
|
#define BLE_UUID_EDC_SERVICE 0x0001
|
|
#define BLE_UUID_EDC_BAT_VOLT_CHAR 0x0002
|
|
#define BLE_UUID_EDC_LOW_FREQ_DATA_CHAR 0x0003
|
|
#define BLE_UUID_EDC_AUXILIARY_DATA_CHAR 0x0004
|
|
#define BLE_UUID_EDC_STATUS_CHAR 0x0005
|
|
#define BLE_UUID_EDC_EVENT_CHAR 0x0006
|
|
|
|
typedef struct
|
|
{
|
|
uint16_t conn_handle;
|
|
uint16_t service_handle;
|
|
|
|
ble_gatts_char_handles_t regular_data_handles;
|
|
ble_gatts_char_handles_t low_freq_data_handles;
|
|
ble_gatts_char_handles_t auxiliary_data_handles;
|
|
ble_gatts_char_handles_t status_handles;
|
|
ble_gatts_char_handles_t event_handles;
|
|
bool regular_data_notify_enable;
|
|
bool low_freq_data_notify_enable;
|
|
bool auxiliary_data_notify_enable;
|
|
bool status_notify_enable;
|
|
bool event_notify_enable;
|
|
} ble_edc_t;
|
|
|
|
typedef struct
|
|
{
|
|
uint16_t dummy;
|
|
} ble_edc_init_t;
|
|
|
|
static ble_edc_t ble_edc = {
|
|
.conn_handle = BLE_CONN_HANDLE_INVALID,
|
|
.regular_data_notify_enable = false,
|
|
.low_freq_data_notify_enable = false,
|
|
.auxiliary_data_notify_enable = false,
|
|
.status_notify_enable = false,
|
|
.event_notify_enable = false,
|
|
};
|
|
|
|
static void on_connect(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt)
|
|
{
|
|
p_edc->conn_handle = p_ble_evt->evt.common_evt.conn_handle;
|
|
}
|
|
|
|
static void on_disconnect(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt)
|
|
{
|
|
p_edc->conn_handle = BLE_CONN_HANDLE_INVALID;
|
|
p_edc->regular_data_notify_enable = false;
|
|
p_edc->low_freq_data_notify_enable = false;
|
|
p_edc->auxiliary_data_notify_enable = false;
|
|
p_edc->status_notify_enable = false;
|
|
p_edc->event_notify_enable = false;
|
|
}
|
|
|
|
static void edc_ccc_update(ble_edc_t *p_edc, 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_edc->regular_data_handles.cccd_handle)
|
|
{
|
|
p_edc->regular_data_notify_enable = ble_srv_is_notification_enabled(p_evt_write->data);
|
|
NRF_LOG_INFO("regular_data_notify:%s", p_edc->regular_data_notify_enable ? "enable" : "disable");
|
|
return;
|
|
}
|
|
else if (p_evt_write->handle == p_edc->low_freq_data_handles.cccd_handle)
|
|
{
|
|
p_edc->low_freq_data_notify_enable = ble_srv_is_notification_enabled(p_evt_write->data);
|
|
NRF_LOG_INFO("low_freq_data_notify:%s", p_edc->low_freq_data_notify_enable ? "enable" : "disable");
|
|
return;
|
|
}
|
|
else if (p_evt_write->handle == p_edc->auxiliary_data_handles.cccd_handle)
|
|
{
|
|
p_edc->auxiliary_data_notify_enable = ble_srv_is_notification_enabled(p_evt_write->data);
|
|
NRF_LOG_INFO("auxiliary_data_notify:%s", p_edc->auxiliary_data_notify_enable ? "enable" : "disable");
|
|
return;
|
|
}
|
|
else if (p_evt_write->handle == p_edc->status_handles.cccd_handle)
|
|
{
|
|
p_edc->status_notify_enable = ble_srv_is_notification_enabled(p_evt_write->data);
|
|
NRF_LOG_INFO("status_notify:%s", p_edc->status_notify_enable ? "enable" : "disable");
|
|
return;
|
|
}
|
|
else if (p_evt_write->handle == p_edc->event_handles.cccd_handle)
|
|
{
|
|
p_edc->event_notify_enable = ble_srv_is_notification_enabled(p_evt_write->data);
|
|
NRF_LOG_INFO("event_notify:%s", p_edc->event_notify_enable ? "enable" : "disable");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void on_write(ble_edc_t *p_edc, 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:
|
|
edc_ccc_update(p_edc, p_ble_evt);
|
|
break;
|
|
case BLE_UUID_CHARACTERISTIC:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void on_hvx_tx_complete(ble_edc_t *p_edc, ble_evt_t const *p_ble_evt)
|
|
{
|
|
}
|
|
|
|
static void ble_edc_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
|
|
{
|
|
ble_edc_t *p_edc = (ble_edc_t *)p_context;
|
|
|
|
switch (p_ble_evt->header.evt_id)
|
|
{
|
|
case BLE_GAP_EVT_CONNECTED:
|
|
on_connect(p_edc, p_ble_evt);
|
|
break;
|
|
case BLE_GAP_EVT_DISCONNECTED:
|
|
on_disconnect(p_edc, p_ble_evt);
|
|
break;
|
|
case BLE_GATTS_EVT_WRITE:
|
|
on_write(p_edc, p_ble_evt);
|
|
break;
|
|
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
|
|
on_hvx_tx_complete(p_edc, p_ble_evt);
|
|
break;
|
|
default:
|
|
// No implementation needed.
|
|
break;
|
|
}
|
|
}
|
|
|
|
static ret_code_t add_characteristic(uint16_t uuid, uint8_t uuid_type, uint32_t val_len, void *p_val, ble_gatts_char_handles_t *p_handles)
|
|
{
|
|
|
|
ble_add_char_params_t add_char_params;
|
|
memset(&add_char_params, 0, sizeof(add_char_params));
|
|
add_char_params.uuid = uuid;
|
|
add_char_params.uuid_type = uuid_type;
|
|
add_char_params.max_len = val_len;
|
|
add_char_params.init_len = 0;
|
|
add_char_params.p_init_value = p_val;
|
|
add_char_params.is_value_user = false;
|
|
add_char_params.is_var_len = true;
|
|
add_char_params.char_props.read = 1;
|
|
add_char_params.char_props.notify = 1;
|
|
add_char_params.read_access = SEC_OPEN;
|
|
add_char_params.cccd_write_access = SEC_OPEN;
|
|
ret_code_t err_code = characteristic_add(ble_edc.service_handle, &add_char_params, p_handles);
|
|
return err_code;
|
|
}
|
|
|
|
static uint32_t ble_edc_init(ble_edc_init_t const *p_edc_init)
|
|
{
|
|
ret_code_t err_code;
|
|
ble_uuid128_t base_uuid = BLE_EDC_BASE_UUID;
|
|
ble_uuid_t ble_uuid = {
|
|
.type = BLE_UUID_TYPE_UNKNOWN,
|
|
.uuid = BLE_UUID_EDC_SERVICE,
|
|
};
|
|
|
|
// Adding vendor specific UUID to the SoftDevice
|
|
err_code = sd_ble_uuid_vs_add(&base_uuid, &ble_uuid.type);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
// Adding proprietary service to the SoftDevice
|
|
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &ble_edc.service_handle);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
// Adding regular data characteristic to the SoftDevice
|
|
err_code = add_characteristic(BLE_UUID_EDC_BAT_VOLT_CHAR,
|
|
ble_uuid.type,
|
|
NRF_SDH_BLE_GATT_MAX_MTU_SIZE - 3,
|
|
NULL,
|
|
&ble_edc.regular_data_handles);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
// Adding low freq data characteristic to the SoftDevice
|
|
err_code = add_characteristic(BLE_UUID_EDC_LOW_FREQ_DATA_CHAR,
|
|
ble_uuid.type,
|
|
32,
|
|
NULL,
|
|
&ble_edc.low_freq_data_handles);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
// Adding auxiliary data characteristic to the SoftDevice
|
|
err_code = add_characteristic(BLE_UUID_EDC_AUXILIARY_DATA_CHAR,
|
|
ble_uuid.type,
|
|
32,
|
|
NULL,
|
|
&ble_edc.auxiliary_data_handles);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
// Adding status characteristic to the SoftDevice
|
|
err_code = add_characteristic(BLE_UUID_EDC_STATUS_CHAR,
|
|
ble_uuid.type,
|
|
32,
|
|
NULL,
|
|
&ble_edc.status_handles);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
// Adding event characteristic to the SoftDevice
|
|
err_code = add_characteristic(BLE_UUID_EDC_EVENT_CHAR,
|
|
ble_uuid.type,
|
|
32,
|
|
NULL,
|
|
&ble_edc.event_handles);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
// Register a handler for BLE events.
|
|
NRF_SDH_BLE_OBSERVER(m_edc_observer, EDC_BLE_OBSERVER_PRIO, ble_edc_evt_handler, &ble_edc);
|
|
|
|
return NRF_SUCCESS;
|
|
}
|
|
|
|
void le_edc_init(void)
|
|
{
|
|
ble_edc_init_t edc_init;
|
|
memset(&edc_init, 0x00, sizeof(edc_init));
|
|
|
|
ret_code_t err_code = ble_edc_init(&edc_init);
|
|
APP_ERROR_CHECK(err_code);
|
|
|
|
extern void edc_regular_data_init(void);
|
|
edc_regular_data_init();
|
|
}
|
|
|
|
ret_code_t le_regular_data_notify(uint8_t *p_value, uint16_t len)
|
|
{
|
|
ret_code_t err_code = NRF_SUCCESS;
|
|
|
|
// update database.
|
|
ble_gatts_value_t gatts_value = {
|
|
.offset = 0,
|
|
.len = len,
|
|
.p_value = p_value,
|
|
};
|
|
err_code = sd_ble_gatts_value_set(BLE_CONN_HANDLE_INVALID, ble_edc.regular_data_handles.value_handle, &gatts_value);
|
|
if (err_code != NRF_SUCCESS)
|
|
{
|
|
return err_code;
|
|
}
|
|
|
|
// send notify
|
|
if (ble_edc.regular_data_notify_enable)
|
|
{
|
|
ble_gatts_hvx_params_t hvx_params = {
|
|
.handle = ble_edc.regular_data_handles.value_handle,
|
|
.type = BLE_GATT_HVX_NOTIFICATION,
|
|
.offset = gatts_value.offset,
|
|
.p_len = &gatts_value.len,
|
|
.p_data = gatts_value.p_value,
|
|
};
|
|
if (ble_conn_state_status(ble_edc.conn_handle) == BLE_CONN_STATUS_CONNECTED)
|
|
{
|
|
err_code = sd_ble_gatts_hvx(ble_edc.conn_handle, &hvx_params);
|
|
}
|
|
}
|
|
|
|
return err_code;
|
|
}
|
|
|
|
bool le_regular_data_notify_is_enable(void)
|
|
{
|
|
return ble_edc.regular_data_notify_enable;
|
|
}
|
|
|
|
#endif // NRF_MODULE_ENABLED(BLE_EDC)
|