You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

219 lines
8.7 KiB

#include "zdatachannel_service.h"
#include "ble.h"
#include "ble_srv_common.h"
#include "nrf_log.h"
#include "sdk_common.h"
#include "znordic.h"
#define ZDATACHANNEL_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
#define ZDATACHANNEL_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
#define ZDATACHANNEL_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
#define ZDATACHANNEL_DATABLOCK_TX_CHARACTERISTIC 0x0004 /**< The UUID of the RX Characteristic. */
#define ZDATACHANNEL_MAX_RX_CHAR_LEN ZDATACHANNEL_MAX_DATA_LEN /**< Maximum length of the RX Characteristic (in bytes). */
#define ZDATACHANNEL_MAX_TX_CHAR_LEN ZDATACHANNEL_MAX_DATA_LEN /**< Maximum length of the TX Characteristic (in bytes). */
#define ZDATACHANNEL_DATABLOCK_TX_CHAR_LEN ZDATACHANNEL_MAX_DATA_LEN /**< Maximum length of the TX Characteristic (in bytes). */
#define NUS_BASE_UUID \
{ \
{ 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E } \
} /**< Used vendor specific UUID. */
static zdatachannel_t *p_datachannel;
static bool notification_enable(uint16_t conn_handle, uint16_t cccd_handle) {
ret_code_t err_code;
ble_gatts_value_t gatts_val;
uint8_t cccd_value[2];
memset(&gatts_val, 0, sizeof(ble_gatts_value_t));
gatts_val.p_value = cccd_value;
gatts_val.len = sizeof(cccd_value);
gatts_val.offset = 0;
err_code = sd_ble_gatts_value_get(conn_handle, cccd_handle, &gatts_val);
if ((err_code == NRF_SUCCESS) && ble_srv_is_notification_enabled(gatts_val.p_value)) {
return true;
}
return false;
}
/**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the SoftDevice.
*
* @param[in] p_nus Nordic UART Service structure.
* @param[in] p_ble_evt Pointer to the event received from BLE stack.
*/
static void on_write(zdatachannel_t *p_nus, ble_evt_t const *p_ble_evt) {
zdatachannel_evt_t evt;
ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
memset(&evt, 0, sizeof(zdatachannel_evt_t));
if ((p_evt_write->handle == p_nus->cmd_tx_handles.cccd_handle) && (p_evt_write->len == 2)) {
if (ble_srv_is_notification_enabled(p_evt_write->data)) {
p_nus->cmd_tx_channel_is_notification_enabled = true;
} else {
p_nus->cmd_tx_channel_is_notification_enabled = false;
}
} else if ((p_evt_write->handle == p_nus->datablock_tx_handles.cccd_handle) && (p_evt_write->len == 2)) {
if (ble_srv_is_notification_enabled(p_evt_write->data)) {
p_nus->datablock_tx_channel_is_notification_enabled = true;
} else {
p_nus->datablock_tx_channel_is_notification_enabled = false;
}
} else if ((p_evt_write->handle == p_nus->cmd_rx_handles.value_handle) && (p_nus->data_handler != NULL)) {
evt.type = ZDATACHANNEL_EVT_RX_DATA;
evt.params.rx_data.p_data = p_evt_write->data;
evt.params.rx_data.length = p_evt_write->len;
p_nus->data_handler(&evt);
} else {
// Do Nothing. This event is not relevant for this service.
}
}
void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
if ((p_context == NULL) || (p_ble_evt == NULL)) {
return;
}
zdatachannel_t *p_nus = (zdatachannel_t *)p_context;
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED:
ZLOGI("BLE_GAP_EVT_CONNECTED");
p_nus->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
p_nus->cmd_tx_channel_is_notification_enabled = notification_enable(p_ble_evt->evt.gap_evt.conn_handle, p_nus->cmd_tx_handles.cccd_handle);
p_nus->datablock_tx_channel_is_notification_enabled = notification_enable(p_ble_evt->evt.gap_evt.conn_handle, p_nus->cmd_tx_handles.cccd_handle);
break;
case BLE_GAP_EVT_DISCONNECTED:
ZLOGI("BLE_GAP_EVT_DISCONNECTED");
p_nus->conn_handle = BLE_CONN_HANDLE_INVALID;
break;
case BLE_GATTS_EVT_WRITE:
on_write(p_nus, p_ble_evt);
break;
default:
// No implementation needed.
break;
}
}
bool zdatachannel_is_connected() { return p_datachannel->conn_handle != BLE_CONN_HANDLE_INVALID; }
uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_nus_init) {
p_datachannel = p_nus;
ret_code_t err_code;
ble_uuid_t ble_uuid;
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
ble_add_char_params_t add_char_params;
VERIFY_PARAM_NOT_NULL(p_nus);
VERIFY_PARAM_NOT_NULL(p_nus_init);
// Initialize the service structure.
p_nus->data_handler = p_nus_init->data_handler;
/**@snippet [Adding proprietary Service to the SoftDevice] */
// Add a custom base UUID.
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
VERIFY_SUCCESS(err_code);
ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = ZDATACHANNEL_UUID_NUS_SERVICE;
// Add the service.
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_nus->service_handle);
/**@snippet [Adding proprietary Service to the SoftDevice] */
VERIFY_SUCCESS(err_code);
// Add the RX Characteristic.
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = ZDATACHANNEL_RX_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = ZDATACHANNEL_MAX_RX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.write = 1;
add_char_params.char_props.write_wo_resp = 1;
add_char_params.read_access = SEC_NO_ACCESS;
add_char_params.write_access = SEC_OPEN;
VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->cmd_rx_handles));
// Add the TX Characteristic.
/**@snippet [Adding proprietary characteristic to the SoftDevice] */
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = ZDATACHANNEL_TX_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = ZDATACHANNEL_MAX_TX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.notify = 1;
add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->cmd_tx_handles));
// Add the TX Characteristic.
/**@snippet [Adding proprietary characteristic to the SoftDevice] */
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = ZDATACHANNEL_DATABLOCK_TX_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = ZDATACHANNEL_DATABLOCK_TX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.notify = 1;
add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->datablock_tx_handles));
return NRF_SUCCESS;
/**@snippet [Adding proprietary characteristic to the SoftDevice] */
}
uint32_t zdatachannel_data_send(uint8_t *p_data, uint16_t *p_length) {
ble_gatts_hvx_params_t hvx_params;
if (!p_datachannel->cmd_tx_channel_is_notification_enabled) {
return NRF_ERROR_INVALID_STATE;
}
if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
return NRF_ERROR_INVALID_PARAM;
}
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = p_datachannel->cmd_tx_handles.value_handle;
hvx_params.p_data = p_data;
hvx_params.p_len = p_length;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
return sd_ble_gatts_hvx(p_datachannel->conn_handle, &hvx_params);
}
uint32_t zdatachannel_block_data_send(uint8_t *p_data, uint16_t *p_length) {
ble_gatts_hvx_params_t hvx_params;
if (!p_datachannel->datablock_tx_channel_is_notification_enabled) {
return NRF_ERROR_INVALID_STATE;
}
if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
return NRF_ERROR_INVALID_PARAM;
}
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = p_datachannel->datablock_tx_handles.value_handle;
hvx_params.p_data = p_data;
hvx_params.p_len = p_length;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
return sd_ble_gatts_hvx(p_datachannel->conn_handle, &hvx_params);
}