Browse Source

update

master
zhaohe 1 year ago
parent
commit
ce9c1b7d17
  1. 4
      .vscode/settings.json
  2. 18
      app/main.c
  3. 193
      app/src/zdatachannel_service.c
  4. 48
      app/src/zdatachannel_service.h

4
.vscode/settings.json

@ -39,6 +39,8 @@
"nrf_log_default_backends.h": "c", "nrf_log_default_backends.h": "c",
"nrf_delay.h": "c", "nrf_delay.h": "c",
"zhrs_service.h": "c", "zhrs_service.h": "c",
"ble_srv_common.h": "c"
"ble_srv_common.h": "c",
"zdatachannel_service.h": "c",
"ble_link_ctx_manager.h": "c"
} }
} }

18
app/main.c

@ -21,7 +21,8 @@ void qingfengboard_test(void) {
} }
#endif #endif
ZDATACANNEL_DEF(m_zhrs, 2 /*回调事件优先级*/, 1 /*client num*/);
ZDATACHANNEL_DEF(m_zhrs, 2 /*回调事件优先级*/, 1 /*client num*/);
APP_TIMER_DEF(m_test_tx_timer);
static const char* hex2str(const uint8_t* data, int32_t len) { static const char* hex2str(const uint8_t* data, int32_t len) {
static char rx[64] = {0}; static char rx[64] = {0};
@ -36,13 +37,12 @@ void zdatachannel_data_handler(zdatachannel_evt_t* p_evt) {
/** /**
* @brief * @brief
*/ */
if (p_evt->type == ZDATACANNEL_EVT_RX_DATA) {
if (p_evt->type == ZDATACHANNEL_EVT_RX_DATA) {
ZLOGI("rx:%s", hex2str(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length)); ZLOGI("rx:%s", hex2str(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length));
} }
} }
static void ble_evt_handler(ble_evt_t const* p_ble_evt, void* p_context) { static void ble_evt_handler(ble_evt_t const* p_ble_evt, void* p_context) {
uint32_t err_code;
switch (p_ble_evt->header.evt_id) { switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED: case BLE_GAP_EVT_CONNECTED:
ZLOGI("Connected"); ZLOGI("Connected");
@ -66,6 +66,13 @@ void on_service_init(void) {
ZERROR_CHECK(zdatachannel_init(&m_zhrs, &zdatachannle_init)); ZERROR_CHECK(zdatachannel_init(&m_zhrs, &zdatachannle_init));
} }
static void test_tx_timer_cb(void* p_context) {
static uint32_t data;
uint16_t txlen = 4;
data++;
zdatachannel_data_send((uint8_t*)&data, &txlen);
}
int main(void) { int main(void) {
zsys_init(); zsys_init();
NRF_LOG_INFO("compile time :%s", __TIME__); NRF_LOG_INFO("compile time :%s", __TIME__);
@ -74,12 +81,15 @@ int main(void) {
static zble_module_cfg_t cfg = // static zble_module_cfg_t cfg = //
{ {
.deviceName = "iflytop",
.deviceName = "iflytop2",
.on_service_init = on_service_init, .on_service_init = on_service_init,
}; };
zble_module_init(&cfg); zble_module_init(&cfg);
NRF_SDH_BLE_OBSERVER(m_ble_observer, 3, ble_evt_handler, NULL); NRF_SDH_BLE_OBSERVER(m_ble_observer, 3, ble_evt_handler, NULL);
ZERROR_CHECK(app_timer_create(&m_test_tx_timer, APP_TIMER_MODE_REPEATED, test_tx_timer_cb));
ZERROR_CHECK(app_timer_start(m_test_tx_timer, APP_TIMER_TICKS(5), NULL));
zble_module_start_adv(); zble_module_start_adv();
zsys_loop(); zsys_loop();
} }

193
app/src/zdatachannel_service.c

@ -5,55 +5,35 @@
#include "nrf_log.h" #include "nrf_log.h"
#include "sdk_common.h" #include "sdk_common.h"
#define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
#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 ZDATACANNEL_MAX_RX_CHAR_LEN ZDATACANNEL_MAX_DATA_LEN /**< Maximum length of the RX Characteristic (in bytes). */
#define ZDATACANNEL_MAX_TX_CHAR_LEN ZDATACANNEL_MAX_DATA_LEN /**< Maximum length of the TX Characteristic (in bytes). */
#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 \ #define NUS_BASE_UUID \
{ \ { \
{ 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E } \ { 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E } \
} /**< Used vendor specific UUID. */ } /**< Used vendor specific UUID. */
/**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED 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_connect(zdatachannel_t *p_nus, ble_evt_t const *p_ble_evt) {
static zdatachannel_t *p_datachannel;
static bool notification_enable(uint16_t conn_handle, uint16_t cccd_handle) {
ret_code_t err_code; ret_code_t err_code;
zdatachannel_evt_t evt;
ble_gatts_value_t gatts_val; ble_gatts_value_t gatts_val;
uint8_t cccd_value[2]; uint8_t cccd_value[2];
zdatachannel_client_context_t *p_client = NULL;
err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage, p_ble_evt->evt.gap_evt.conn_handle, (void *)&p_client);
if (err_code != NRF_SUCCESS) {
NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.", p_ble_evt->evt.gap_evt.conn_handle);
}
/* Check the hosts CCCD value to inform of readiness to send data using the RX characteristic */
memset(&gatts_val, 0, sizeof(ble_gatts_value_t)); memset(&gatts_val, 0, sizeof(ble_gatts_value_t));
gatts_val.p_value = cccd_value; gatts_val.p_value = cccd_value;
gatts_val.len = sizeof(cccd_value); gatts_val.len = sizeof(cccd_value);
gatts_val.offset = 0; gatts_val.offset = 0;
err_code = sd_ble_gatts_value_get(p_ble_evt->evt.gap_evt.conn_handle, p_nus->tx_handles.cccd_handle, &gatts_val);
if ((err_code == NRF_SUCCESS) && (p_nus->data_handler != NULL) && ble_srv_is_notification_enabled(gatts_val.p_value)) {
if (p_client != NULL) {
p_client->is_notification_enabled = true;
}
memset(&evt, 0, sizeof(zdatachannel_evt_t));
evt.type = ZDATACANNEL_EVT_COMM_STARTED;
evt.p_nus = p_nus;
evt.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
evt.p_link_ctx = p_client;
p_nus->data_handler(&evt);
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. /**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the SoftDevice.
@ -62,37 +42,25 @@ static void on_connect(zdatachannel_t *p_nus, ble_evt_t const *p_ble_evt) {
* @param[in] p_ble_evt Pointer to the event received from BLE stack. * @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) { static void on_write(zdatachannel_t *p_nus, ble_evt_t const *p_ble_evt) {
ret_code_t err_code;
zdatachannel_evt_t evt; zdatachannel_evt_t evt;
zdatachannel_client_context_t *p_client;
ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage, p_ble_evt->evt.gatts_evt.conn_handle, (void *)&p_client);
if (err_code != NRF_SUCCESS) {
NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.", p_ble_evt->evt.gatts_evt.conn_handle);
}
memset(&evt, 0, sizeof(zdatachannel_evt_t)); memset(&evt, 0, sizeof(zdatachannel_evt_t));
evt.p_nus = p_nus;
evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
evt.p_link_ctx = p_client;
if ((p_evt_write->handle == p_nus->tx_handles.cccd_handle) && (p_evt_write->len == 2)) {
if (p_client != NULL) {
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)) { if (ble_srv_is_notification_enabled(p_evt_write->data)) {
p_client->is_notification_enabled = true;
evt.type = ZDATACANNEL_EVT_COMM_STARTED;
p_nus->cmd_tx_channel_is_notification_enabled = true;
} else { } else {
p_client->is_notification_enabled = false;
evt.type = ZDATACANNEL_EVT_COMM_STOPPED;
}
if (p_nus->data_handler != NULL) {
p_nus->data_handler(&evt);
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->rx_handles.value_handle) && (p_nus->data_handler != NULL)) {
evt.type = ZDATACANNEL_EVT_RX_DATA;
} else if ((p_evt_write->handle == p_nus->cmd_tx_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.p_data = p_evt_write->data;
evt.params.rx_data.length = p_evt_write->len; evt.params.rx_data.length = p_evt_write->len;
@ -102,33 +70,6 @@ static void on_write(zdatachannel_t *p_nus, ble_evt_t const *p_ble_evt) {
} }
} }
/**@brief Function for handling the @ref BLE_GATTS_EVT_HVN_TX_COMPLETE 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_hvx_tx_complete(zdatachannel_t *p_nus, ble_evt_t const *p_ble_evt) {
ret_code_t err_code;
zdatachannel_evt_t evt;
zdatachannel_client_context_t *p_client;
err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage, p_ble_evt->evt.gatts_evt.conn_handle, (void *)&p_client);
if (err_code != NRF_SUCCESS) {
NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.", p_ble_evt->evt.gatts_evt.conn_handle);
return;
}
if ((p_client->is_notification_enabled) && (p_nus->data_handler != NULL)) {
memset(&evt, 0, sizeof(zdatachannel_evt_t));
evt.type = ZDATACANNEL_EVT_TX_RDY;
evt.p_nus = p_nus;
evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
evt.p_link_ctx = p_client;
p_nus->data_handler(&evt);
}
}
void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) { void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
if ((p_context == NULL) || (p_ble_evt == NULL)) { if ((p_context == NULL) || (p_ble_evt == NULL)) {
return; return;
@ -138,17 +79,16 @@ void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
switch (p_ble_evt->header.evt_id) { switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED: case BLE_GAP_EVT_CONNECTED:
on_connect(p_nus, p_ble_evt);
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:
p_nus->conn_handle = BLE_CONN_HANDLE_INVALID;
break; break;
case BLE_GATTS_EVT_WRITE: case BLE_GATTS_EVT_WRITE:
on_write(p_nus, p_ble_evt); on_write(p_nus, p_ble_evt);
break; break;
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
on_hvx_tx_complete(p_nus, p_ble_evt);
break;
default: default:
// No implementation needed. // No implementation needed.
break; break;
@ -156,6 +96,7 @@ void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
} }
uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_nus_init) { uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_nus_init) {
p_datachannel = p_nus;
ret_code_t err_code; ret_code_t err_code;
ble_uuid_t ble_uuid; ble_uuid_t ble_uuid;
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID; ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
@ -173,7 +114,7 @@ uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_n
VERIFY_SUCCESS(err_code); VERIFY_SUCCESS(err_code);
ble_uuid.type = p_nus->uuid_type; ble_uuid.type = p_nus->uuid_type;
ble_uuid.uuid = BLE_UUID_NUS_SERVICE;
ble_uuid.uuid = ZDATACHANNEL_UUID_NUS_SERVICE;
// Add the service. // Add the service.
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_nus->service_handle); err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_nus->service_handle);
@ -182,28 +123,41 @@ uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_n
// Add the RX Characteristic. // Add the RX Characteristic.
memset(&add_char_params, 0, sizeof(add_char_params)); memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
add_char_params.uuid = ZDATACHANNEL_RX_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type; add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = ZDATACANNEL_MAX_RX_CHAR_LEN;
add_char_params.max_len = ZDATACHANNEL_MAX_RX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t); add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true; add_char_params.is_var_len = true;
add_char_params.char_props.write = 1; add_char_params.char_props.write = 1;
add_char_params.char_props.write_wo_resp = 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.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN; add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;
err_code = characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->rx_handles);
if (err_code != NRF_SUCCESS) {
return err_code;
}
VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->cmd_tx_handles));
// Add the TX Characteristic. // Add the TX Characteristic.
/**@snippet [Adding proprietary characteristic to the SoftDevice] */ /**@snippet [Adding proprietary characteristic to the SoftDevice] */
memset(&add_char_params, 0, sizeof(add_char_params)); memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_NUS_TX_CHARACTERISTIC;
add_char_params.uuid = ZDATACHANNEL_DATABLOCK_TX_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type; add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = ZDATACANNEL_MAX_TX_CHAR_LEN;
add_char_params.max_len = ZDATACHANNEL_DATABLOCK_TX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t); add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true; add_char_params.is_var_len = true;
add_char_params.char_props.notify = 1; add_char_params.char_props.notify = 1;
@ -212,38 +166,51 @@ uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_n
add_char_params.write_access = SEC_OPEN; add_char_params.write_access = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN; add_char_params.cccd_write_access = SEC_OPEN;
return characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->tx_handles);
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] */ /**@snippet [Adding proprietary characteristic to the SoftDevice] */
} }
uint32_t zdatachannel_data_send(zdatachannel_t *p_nus, uint8_t *p_data, uint16_t *p_length, uint16_t conn_handle) {
uint32_t zdatachannel_data_send(uint8_t *p_data, uint16_t *p_length) {
ret_code_t err_code; ret_code_t err_code;
ble_gatts_hvx_params_t hvx_params; ble_gatts_hvx_params_t hvx_params;
zdatachannel_client_context_t *p_client;
VERIFY_PARAM_NOT_NULL(p_nus);
err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage, conn_handle, (void *)&p_client);
VERIFY_SUCCESS(err_code);
if (!p_datachannel->cmd_tx_channel_is_notification_enabled) {
return NRF_ERROR_INVALID_STATE;
}
if ((conn_handle == BLE_CONN_HANDLE_INVALID) || (p_client == NULL)) {
return NRF_ERROR_NOT_FOUND;
if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
return NRF_ERROR_INVALID_PARAM;
} }
if (!p_client->is_notification_enabled) {
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) {
ret_code_t err_code;
ble_gatts_hvx_params_t hvx_params;
if (!p_datachannel->datablock_tx_channel_is_notification_enabled) {
return NRF_ERROR_INVALID_STATE; return NRF_ERROR_INVALID_STATE;
} }
if (*p_length > ZDATACANNEL_MAX_DATA_LEN) {
if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
return NRF_ERROR_INVALID_PARAM; return NRF_ERROR_INVALID_PARAM;
} }
memset(&hvx_params, 0, sizeof(hvx_params)); memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = p_nus->tx_handles.value_handle;
hvx_params.handle = p_datachannel->datablock_tx_handles.value_handle;
hvx_params.p_data = p_data; hvx_params.p_data = p_data;
hvx_params.p_len = p_length; hvx_params.p_len = p_length;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION; hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
return sd_ble_gatts_hvx(conn_handle, &hvx_params);
return sd_ble_gatts_hvx(p_datachannel->conn_handle, &hvx_params);
} }

48
app/src/zdatachannel_service.h

@ -1,5 +1,5 @@
#ifndef ZDATACANNEL_H__
#define ZDATACANNEL_H__
#ifndef ZDATACHANNEL_H__
#define ZDATACHANNEL_H__
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -20,30 +20,24 @@ extern "C" {
* @param[in] _nus_max_clients Maximum number of NUS clients connected at a time. * @param[in] _nus_max_clients Maximum number of NUS clients connected at a time.
* @hideinitializer * @hideinitializer
*/ */
#define ZDATACANNEL_DEF(_name, _ble_observer_prio,_nus_max_clients) \
BLE_LINK_CTX_MANAGER_DEF(CONCAT_2(_name, _link_ctx_storage), (_nus_max_clients), sizeof(zdatachannel_client_context_t)); \
static zdatachannel_t _name = {.p_link_ctx_storage = &CONCAT_2(_name, _link_ctx_storage)}; \
#define ZDATACHANNEL_DEF(_name, _ble_observer_prio, _nus_max_clients) \
static zdatachannel_t _name; \
NRF_SDH_BLE_OBSERVER(_name##_obs, _ble_observer_prio, zdatachannel_on_ble_evt, &_name) NRF_SDH_BLE_OBSERVER(_name##_obs, _ble_observer_prio, zdatachannel_on_ble_evt, &_name)
#define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
#define OPCODE_LENGTH 1 #define OPCODE_LENGTH 1
#define HANDLE_LENGTH 2 #define HANDLE_LENGTH 2
/**@brief Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */ /**@brief Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
#if defined(NRF_SDH_BLE_GATT_MAX_MTU_SIZE) && (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 0) #if defined(NRF_SDH_BLE_GATT_MAX_MTU_SIZE) && (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 0)
#define ZDATACANNEL_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
#define ZDATACHANNEL_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
#else #else
#define ZDATACANNEL_MAX_DATA_LEN (BLE_GATT_MTU_SIZE_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH)
#define ZDATACHANNEL_MAX_DATA_LEN (BLE_GATT_MTU_SIZE_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH)
#warning NRF_SDH_BLE_GATT_MAX_MTU_SIZE is not defined. #warning NRF_SDH_BLE_GATT_MAX_MTU_SIZE is not defined.
#endif #endif
/**@brief Nordic UART Service event types. */ /**@brief Nordic UART Service event types. */
typedef enum { typedef enum {
ZDATACANNEL_EVT_RX_DATA, /**< Data received. */
ZDATACANNEL_EVT_TX_RDY, /**< Service is ready to accept new data to be transmitted. */
ZDATACANNEL_EVT_COMM_STARTED, /**< Notification has been enabled. */
ZDATACANNEL_EVT_COMM_STOPPED, /**< Notification has been disabled. */
ZDATACHANNEL_EVT_RX_DATA, /**< Data received. */
} zdatachannel_evt_type_t; } zdatachannel_evt_type_t;
typedef struct zdatachannel_s zdatachannel_t; typedef struct zdatachannel_s zdatachannel_t;
@ -54,42 +48,42 @@ typedef struct {
} zdatachannel_evt_rx_data_t; } zdatachannel_evt_rx_data_t;
typedef struct { typedef struct {
bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/
} zdatachannel_client_context_t;
typedef struct {
zdatachannel_evt_type_t type; /**< Event type. */ zdatachannel_evt_type_t type; /**< Event type. */
zdatachannel_t* p_nus; /**< A pointer to the instance. */
uint16_t conn_handle; /**< Connection handle. */
zdatachannel_client_context_t* p_link_ctx; /**< A pointer to the link context. */
union { union {
zdatachannel_evt_rx_data_t rx_data; /**< @ref ZDATACANNEL_EVT_RX_DATA event data. */
zdatachannel_evt_rx_data_t rx_data; /**< @ref ZDATACHANNEL_EVT_RX_DATA event data. */
} params; } params;
} zdatachannel_evt_t; } zdatachannel_evt_t;
typedef void (*zdatachannel_data_handler_t)(zdatachannel_evt_t* p_evt); typedef void (*zdatachannel_data_handler_t)(zdatachannel_evt_t* p_evt);
typedef struct { typedef struct {
zdatachannel_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
zdatachannel_data_handler_t data_handler;
} zdatachannel_init_t; } zdatachannel_init_t;
struct zdatachannel_s { struct zdatachannel_s {
uint8_t uuid_type; /**< UUID type for Nordic UART Service Base UUID. */ uint8_t uuid_type; /**< UUID type for Nordic UART Service Base UUID. */
uint16_t service_handle; /**< Handle of Nordic UART Service (as provided by the SoftDevice). */ uint16_t service_handle; /**< Handle of Nordic UART Service (as provided by the SoftDevice). */
ble_gatts_char_handles_t tx_handles; /**< Handles related to the TX characteristic (as provided by the SoftDevice). */
ble_gatts_char_handles_t rx_handles; /**< Handles related to the RX characteristic (as provided by the SoftDevice). */
blcm_link_ctx_storage_t* const p_link_ctx_storage; /**< Pointer to link context storage with handles of all current connections and its context. */
ble_gatts_char_handles_t cmd_tx_handles; /**< 指令发送句柄 */
ble_gatts_char_handles_t cmd_rx_handles; /**< 指令接收句柄 */
ble_gatts_char_handles_t datablock_tx_handles; /**< 数据块发送句柄 */
zdatachannel_data_handler_t data_handler; /**< Event handler to be called for handling received data. */ zdatachannel_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
//
int16_t conn_handle;
bool cmd_tx_channel_is_notification_enabled;
bool datablock_tx_channel_is_notification_enabled;
}; };
uint32_t zdatachannel_init(zdatachannel_t* p_nus, zdatachannel_init_t const* p_nus_init); uint32_t zdatachannel_init(zdatachannel_t* p_nus, zdatachannel_init_t const* p_nus_init);
void zdatachannel_on_ble_evt(ble_evt_t const* p_ble_evt, void* p_context); void zdatachannel_on_ble_evt(ble_evt_t const* p_ble_evt, void* p_context);
uint32_t zdatachannel_data_send(zdatachannel_t* p_nus, uint8_t* p_data, uint16_t* p_length, uint16_t conn_handle);
uint32_t zdatachannel_data_send(uint8_t* p_data, uint16_t* p_length);
uint32_t zdatachannel_block_data_send(uint8_t* p_data, uint16_t* p_length);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif // ZDATACANNEL_H__
#endif // ZDATACHANNEL_H__
/** @} */ /** @} */
Loading…
Cancel
Save