Browse Source

update

3lead_uart_test_ok_version
zhaohe 1 year ago
parent
commit
096782e6c4
  1. 160
      .vscode/c_cpp_properties.json
  2. 9
      app/src/basic/zdatachannel_service.c
  3. 3
      app/src/one_conduction/ble_cmd_process_service.c
  4. 20
      app/src/one_conduction/one_conduction_main.c
  5. 65
      ble_app_uart_c/ble_app_uart_c_pca10100_s122.uvguix.h_zha
  6. 49
      ble_app_uart_c/main.c
  7. 375
      ble_app_uart_c/zble_nus_c.c
  8. 19
      ble_app_uart_c/zble_nus_c.h

160
.vscode/c_cpp_properties.json

@ -3,18 +3,20 @@
{
"name": "nrf52833_xxaa",
"includePath": [
"libznordic\\include",
"sdk/external/fatfs/src/",
"sdk/external/fatfs/src/",
"sdk/external/fatfs/port/",
"sdk/components/libraries/block_dev/sdc/",
"sdk/components/libraries/block_dev/",
"sdk\\config",
"app\\src",
"sdk\\components\\ble\\nrf_ble_gq\\",
"sdk\\components\\softdevice\\mbr\\headers\\",
"sdk\\components\\ble\\common\\",
"sdk\\components\\ble\\ble_db_discovery\\",
"sdk\\components\\toolchain\\cmsis\\include\\",
"ble_app_uart_c\\config",
"sdk\\components\\libraries\\util\\",
"sdk\\modules\\nrfx\\mdk\\",
"sdk\\components\\softdevice\\s140\\headers",
"ble_app_uart_c\\config",
"sdk\\components",
"sdk\\components\\ble\\ble_advertising",
"sdk\\components\\ble\\ble_db_discovery",
"sdk\\components\\ble\\ble_dtm",
"sdk\\components\\ble\\ble_link_ctx_manager",
"sdk\\components\\ble\\ble_racp",
"sdk\\components\\ble\\ble_services\\ble_ancs_c",
"sdk\\components\\ble\\ble_services\\ble_ans_c",
@ -41,12 +43,13 @@
"sdk\\components\\ble\\ble_services\\ble_tps",
"sdk\\components\\ble\\common",
"sdk\\components\\ble\\nrf_ble_gatt",
"sdk\\components\\ble\\nrf_ble_gq",
"sdk\\components\\ble\\nrf_ble_qwr",
"sdk\\components\\ble\\nrf_ble_scan",
"sdk\\components\\ble\\peer_manager",
"sdk\\components\\boards",
"sdk\\components\\libraries\\atomic",
"sdk\\components\\libraries\\atomic_fifo",
"sdk\\components\\libraries\\atomic_flags",
"sdk\\components\\libraries\\balloc",
"sdk\\components\\libraries\\bootloader\\ble_dfu",
"sdk\\components\\libraries\\bsp",
@ -130,8 +133,8 @@
"sdk\\components\\nfc\\t4t_parser\\hl_detection_procedure",
"sdk\\components\\nfc\\t4t_parser\\tlv",
"sdk\\components\\softdevice\\common",
"sdk\\components\\softdevice\\s140\\headers",
"sdk\\components\\softdevice\\s140\\headers\\nrf52",
"sdk\\components\\softdevice\\s122\\headers",
"sdk\\components\\softdevice\\s122\\headers\\nrf52",
"sdk\\external\\fprintf",
"sdk\\external\\segger_rtt",
"sdk\\external\\utf_converter",
@ -140,24 +143,21 @@
"sdk\\modules\\nrfx",
"sdk\\modules\\nrfx\\drivers\\include",
"sdk\\modules\\nrfx\\hal",
"app\\config\\",
"app\\config",
"app",
"config",
"ble_app_uart_c",
"sdk\\modules\\nrfx\\soc",
"sdk\\modules\\nrfx\\drivers\\src",
"sdk\\modules\\nrfx\\drivers\\src\\prs",
"sdk\\modules\\nrfx\\mdk\\",
"sdk\\components\\toolchain\\cmsis\\include\\"
"sdk\\modules\\nrfx\\drivers\\src\\prs"
],
"defines": [
"APP_TIMER_V2",
"APP_TIMER_V2_RTC1_ENABLED",
"BOARD_PCA10100",
"BOARD_QING_FENG_NRF52833",
"CONFIG_GPIO_AS_PINRESET",
"FLOAT_ABI_HARD",
"NRF52833_XXAA",
"NRF_SD_BLE_API_VERSION=7",
"S140",
"NRF_SD_BLE_API_VERSION=8",
"S122",
"SOFTDEVICE_PRESENT",
"__HEAP_SIZE=2048",
"__STACK_SIZE=2048",
@ -235,6 +235,124 @@
"__vfp_status(x,y)=0"
],
"intelliSenseMode": "${default}"
},
{
"name": "flash_s122_nrf52_8.0.0_softdevice",
"includePath": [
"ble_app_uart_c\\config",
"config",
"ble_app_uart_c",
"sdk\\components\\boards",
"sdk\\components\\libraries\\bsp",
"sdk\\external\\utf_converter",
"sdk\\components\\ble\\common",
"sdk\\components\\ble\\ble_db_discovery",
"sdk\\components\\ble\\nrf_ble_gatt",
"sdk\\components\\ble\\nrf_ble_gq",
"sdk\\components\\ble\\nrf_ble_scan",
"sdk\\integration\\nrfx\\legacy",
"sdk\\modules\\nrfx\\soc",
"sdk\\modules\\nrfx\\drivers\\src",
"sdk\\modules\\nrfx\\drivers\\src\\prs",
"sdk\\components\\libraries\\button",
"sdk\\components\\libraries\\util",
"sdk\\components\\libraries\\fifo",
"sdk\\components\\libraries\\scheduler",
"sdk\\components\\libraries\\timer",
"sdk\\components\\libraries\\uart",
"sdk\\components\\libraries\\hardfault",
"sdk\\components\\libraries\\atomic_fifo",
"sdk\\components\\libraries\\atomic",
"sdk\\components\\libraries\\balloc",
"sdk\\external\\fprintf",
"sdk\\components\\libraries\\memobj",
"sdk\\components\\libraries\\pwr_mgmt",
"sdk\\components\\libraries\\queue",
"sdk\\components\\libraries\\ringbuf",
"sdk\\components\\libraries\\experimental_section_vars",
"sdk\\components\\libraries\\sortlist",
"sdk\\components\\libraries\\strerror",
"sdk\\components\\libraries\\log\\src",
"sdk\\external\\segger_rtt",
"sdk\\components\\softdevice\\common"
],
"defines": [
"__HEAP_SIZE=2048",
"__STACK_SIZE=2048",
"__CC_ARM",
"__arm__",
"__align(x)=",
"__ALIGNOF__(x)=",
"__alignof__(x)=",
"__asm(x)=",
"__forceinline=",
"__restrict=",
"__global_reg(n)=",
"__inline=",
"__int64=long long",
"__INTADDR__(expr)=0",
"__irq=",
"__packed=",
"__pure=",
"__smc(n)=",
"__svc(n)=",
"__svc_indirect(n)=",
"__svc_indirect_r7(n)=",
"__value_in_regs=",
"__weak=",
"__writeonly=",
"__declspec(x)=",
"__attribute__(x)=",
"__nonnull__(x)=",
"__register=",
"__breakpoint(x)=",
"__cdp(x,y,z)=",
"__clrex()=",
"__clz(x)=0U",
"__current_pc()=0U",
"__current_sp()=0U",
"__disable_fiq()=",
"__disable_irq()=",
"__dmb(x)=",
"__dsb(x)=",
"__enable_fiq()=",
"__enable_irq()=",
"__fabs(x)=0.0",
"__fabsf(x)=0.0f",
"__force_loads()=",
"__force_stores()=",
"__isb(x)=",
"__ldrex(x)=0U",
"__ldrexd(x)=0U",
"__ldrt(x)=0U",
"__memory_changed()=",
"__nop()=",
"__pld(...)=",
"__pli(...)=",
"__qadd(x,y)=0",
"__qdbl(x)=0",
"__qsub(x,y)=0",
"__rbit(x)=0U",
"__rev(x)=0U",
"__return_address()=0U",
"__ror(x,y)=0U",
"__schedule_barrier()=",
"__semihost(x,y)=0",
"__sev()=",
"__sqrt(x)=0.0",
"__sqrtf(x)=0.0f",
"__ssat(x,y)=0",
"__strex(x,y)=0U",
"__strexd(x,y)=0",
"__strt(x,y)=",
"__swp(x,y)=0U",
"__usat(x,y)=0U",
"__wfe()=",
"__wfi()=",
"__yield()=",
"__vfp_status(x,y)=0"
],
"intelliSenseMode": "${default}"
}
],
"version": 4

9
app/src/basic/zdatachannel_service.c

@ -88,6 +88,7 @@ void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
case BLE_GAP_EVT_DISCONNECTED:
ZLOGI("BLE_GAP_EVT_DISCONNECTED");
p_nus->conn_handle = BLE_CONN_HANDLE_INVALID;
// ZLOGI("-----%d %d", p_datachannel->conn_handle, BLE_CONN_HANDLE_INVALID);
break;
case BLE_GATTS_EVT_WRITE:
on_write(p_nus, p_ble_evt);
@ -98,10 +99,14 @@ void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
}
}
bool zdatachannel_is_connected() { return p_datachannel->conn_handle != BLE_CONN_HANDLE_INVALID; }
bool zdatachannel_is_connected() {
// ZLOGI("%d %d", p_datachannel->conn_handle, BLE_CONN_HANDLE_INVALID);
return p_datachannel->conn_handle != (int16_t)BLE_CONN_HANDLE_INVALID;
}
uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_nus_init) {
p_datachannel = p_nus;
p_datachannel = p_nus;
p_datachannel->conn_handle = BLE_CONN_HANDLE_INVALID;
ret_code_t err_code;
ble_uuid_t ble_uuid;
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;

3
app/src/one_conduction/ble_cmd_process_service.c

@ -56,6 +56,9 @@ void ble_cmder_process_rx(uint8_t* rx, int len) {
txheader->frame_index = rxheader->frame_index;
txheader->frame_type = kifyhrs_pt_cmd_receipt;
ZLOGI("rx cmd:%d index:%d datalen:%d", cmd, rxheader->frame_index, len - sizeof(ify_hrs_packet_t));
NRF_LOG_HEXDUMP_INFO(rxheader->data, len - sizeof(ify_hrs_packet_t));
memset(txbuf, 0, sizeof(txbuf));
if (cmd == ify_hrs_cmd_read_device_version) {

20
app/src/one_conduction/one_conduction_main.c

@ -45,11 +45,9 @@ static bool plod_state_is_connected() { return m_plod_state_connected_state;
static void m_plod_state_event_detect_tmr_cb(void* p_context) { //
static uint32_t connecte_state_trigger_cnt = 0;
static uint32_t connecte_state_trigger_cnt_last_val = 0;
static app_event_t appevent;
memset(&appevent, 0, sizeof(appevent));
connecte_state_trigger_cnt_last_val = connecte_state_trigger_cnt;
if (SingleLeadECG_ecg_plod_get_connected_state()) {
connecte_state_trigger_cnt++;
m_connecte_state_last_trigger_timepoint = znordic_getpower_on_ms();
@ -221,18 +219,22 @@ void app_event_process_cb(void* p_event_data, uint16_t event_size) {
* *
*******************************************************************************/
//
if (!zdatachannel_is_connected() && //
!plod_state_is_connected() && //
plod_state_has_disconnected_ms() >= 30000 && //
ds_cur_state_haspassed_ms() >= 30000) {
if (!zdatachannel_is_connected() && //
!plod_state_is_connected() && //
plod_state_has_disconnected_ms() >= 3000 && //
ds_cur_state_haspassed_ms() >= 15000) {
state_machine__change_to_standby_state();
}
//
else if (plod_state_is_connected() && ds_cur_state_haspassed_ms() > 1500) {
// dsp_mgr_change_to_preparePage
ds_change_to_state(kdevice_state_keep_still);
dsp_mgr_change_to_preparePage();
}
zdatachannel_data_send2((uint8_t*)"hello", 5);
// ZLOGI("bt:%d plod:%d has_disc:%d state:%d", zdatachannel_is_connected(), plod_state_is_connected(), plod_state_has_disconnected_ms(), ds_cur_state_haspassed_ms());
} else if (ds_now_state() == kdevice_state_keep_still) {
/*******************************************************************************
* *
@ -363,7 +365,9 @@ void zdatachannel_data_handler(zdatachannel_evt_t* p_evt) {
* @brief
*/
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:%d", hex2str(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length), p_evt->params.rx_data.length);
// NRF_LOG_HEXDUMP_INFO(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
ble_cmder_process_rx((uint8_t*)p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
}
}
@ -406,5 +410,7 @@ void one_conduction_main() {
ZERROR_CHECK(app_timer_start(m_plod_state_event_detect_tmr, APP_TIMER_TICKS(30), NULL));
ZERROR_CHECK(app_timer_start(m_charge_event_detect_tmr, APP_TIMER_TICKS(100), NULL));
state_machine__change_to_poweroning_state();
znordic_loop();
}

65
ble_app_uart_c/ble_app_uart_c_pca10100_s122.uvguix.h_zha
File diff suppressed because it is too large
View File

49
ble_app_uart_c/main.c

@ -260,36 +260,37 @@ static void ble_nus_chars_received_uart_print(uint8_t * p_data, uint16_t data_le
* a string. The string is sent over BLE when the last character received is a
* 'new line' '\n' (hex 0x0A) or if the string reaches the maximum data length.
*/
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
static uint16_t data_array_index = 0;
// static uint32_t
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
static uint16_t index = 0;
uint32_t ret_val;
switch (p_event->evt_type)
{
/**@snippet [Handling data from UART] */
case APP_UART_DATA_READY:
UNUSED_VARIABLE(app_uart_get(&data_array[index]));
index++;
UNUSED_VARIABLE(app_uart_get(&data_array[data_array_index]));
data_array_index++;
if ((data_array[index - 1] == '\n') ||
(data_array[index - 1] == '\r') ||
(index >= (m_ble_nus_max_data_len)))
if (
data_array_index >= (m_ble_nus_max_data_len))
{
NRF_LOG_DEBUG("Ready to send data over BLE NUS");
NRF_LOG_HEXDUMP_DEBUG(data_array, index);
NRF_LOG_HEXDUMP_DEBUG(data_array, data_array_index);
do
{
ret_val = ble_nus_c_string_send(&m_ble_nus_c, data_array, index);
ret_val = ble_nus_c_string_send(&m_ble_nus_c, data_array, data_array_index);
if ( (ret_val != NRF_ERROR_INVALID_STATE) && (ret_val != NRF_ERROR_RESOURCES) )
{
APP_ERROR_CHECK(ret_val);
}
} while (ret_val == NRF_ERROR_RESOURCES);
index = 0;
data_array_index = 0;
}
break;
@ -338,9 +339,9 @@ static void ble_nus_c_evt_handler(ble_nus_c_t * p_ble_nus_c, ble_nus_c_evt_t con
case BLE_NUS_C_EVT_NUS_TX_EVT:
ble_nus_chars_received_uart_print(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
break;
case BLE_NUS_C_EVT_NUS_TX_BLOCK_EVT:
ble_nus_chars_received_uart_print(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
break;
// case BLE_NUS_C_EVT_NUS_TX_BLOCK_EVT:
// ble_nus_chars_received_uart_print(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
// break;
case BLE_NUS_C_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected.");
@ -655,8 +656,23 @@ static void idle_state_handle(void)
nrf_pwr_mgmt_run();
}
}
APP_TIMER_DEF(m_uart_send_tmr); // ״̬»úÇý¯¨Ê±Æ÷
static void uart_send_tmr_cb(void* p_context) { //
static uint32_t last_data_array_index;
ret_code_t ret_val;
if (data_array_index != 0 && data_array_index == last_data_array_index) {
do {
ret_val = ble_nus_c_string_send(&m_ble_nus_c, data_array, data_array_index);
if ((ret_val != NRF_ERROR_INVALID_STATE) && (ret_val != NRF_ERROR_RESOURCES)) {
APP_ERROR_CHECK(ret_val);
}
} while (ret_val == NRF_ERROR_RESOURCES);
data_array_index = 0;
last_data_array_index = 0;
}
last_data_array_index = data_array_index;
}
int main(void)
{
// Initialize.
@ -671,6 +687,11 @@ int main(void)
nus_c_init();
scan_init();
app_timer_create(&m_uart_send_tmr, APP_TIMER_MODE_REPEATED, uart_send_tmr_cb);
app_timer_start(m_uart_send_tmr, APP_TIMER_TICKS(2), NULL);
app_uart_put(1);
// Start execution.
printf("BLE UART central example started.\r\n");
NRF_LOG_INFO("BLE UART central example started.");

375
ble_app_uart_c/zble_nus_c.c

@ -37,84 +37,65 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "sdk_common.h"
#if 1
#if NRF_MODULE_ENABLED(BLE_NUS_C)
#include <stdlib.h>
#include "app_error.h"
#include "ble.h"
#include "zble_nus_c.h"
#include "ble_gattc.h"
#include "ble_nus_c.h"
#include "ble_srv_common.h"
#include "app_error.h"
#define NRF_LOG_MODULE_NAME ble_nus_c
#include "nrf_log.h"
NRF_LOG_MODULE_REGISTER();
/**@brief Function for intercepting the errors of GATTC and the BLE GATT Queue.
*
* @param[in] nrf_error Error code.
* @param[in] p_ctx Parameter from the event handler.
* @param[in] conn_handle Connection handle.
*/
static void gatt_error_handler(uint32_t nrf_error,
void * p_ctx,
uint16_t conn_handle)
{
ble_nus_c_t * p_ble_nus_c = (ble_nus_c_t *)p_ctx;
static void gatt_error_handler(uint32_t nrf_error, void *p_ctx, uint16_t conn_handle) {
ble_nus_c_t *p_ble_nus_c = (ble_nus_c_t *)p_ctx;
NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
if (p_ble_nus_c->error_handler != NULL)
{
p_ble_nus_c->error_handler(nrf_error);
}
if (p_ble_nus_c->error_handler != NULL) {
p_ble_nus_c->error_handler(nrf_error);
}
}
void ble_nus_c_on_db_disc_evt(ble_nus_c_t *p_ble_nus_c, ble_db_discovery_evt_t *p_evt) {
ble_nus_c_evt_t nus_c_evt;
memset(&nus_c_evt, 0, sizeof(ble_nus_c_evt_t));
ble_gatt_db_char_t *p_chars = p_evt->params.discovered_db.charateristics;
// Check if the NUS was discovered.
if ((p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE) && (p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_NUS_SERVICE) && (p_evt->params.discovered_db.srv_uuid.type == p_ble_nus_c->uuid_type)) {
for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++) {
switch (p_chars[i].characteristic.uuid.uuid) {
case BLE_UUID_NUS_RX_CHARACTERISTIC:
nus_c_evt.handles.nus_rx_handle = p_chars[i].characteristic.handle_value;
break;
case BLE_UUID_NUS_TX_CHARACTERISTIC:
nus_c_evt.handles.nus_tx_handle = p_chars[i].characteristic.handle_value;
nus_c_evt.handles.nus_tx_cccd_handle = p_chars[i].cccd_handle;
break;
void ble_nus_c_on_db_disc_evt(ble_nus_c_t * p_ble_nus_c, ble_db_discovery_evt_t * p_evt)
{
ble_nus_c_evt_t nus_c_evt;
memset(&nus_c_evt,0,sizeof(ble_nus_c_evt_t));
ble_gatt_db_char_t * p_chars = p_evt->params.discovered_db.charateristics;
// Check if the NUS was discovered.
if ( (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE)
&& (p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_NUS_SERVICE)
&& (p_evt->params.discovered_db.srv_uuid.type == p_ble_nus_c->uuid_type))
{
for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
{
switch (p_chars[i].characteristic.uuid.uuid)
{
case BLE_UUID_NUS_RX_CHARACTERISTIC:
nus_c_evt.handles.nus_rx_handle = p_chars[i].characteristic.handle_value;
break;
case BLE_UUID_NUS_TX_CHARACTERISTIC:
nus_c_evt.handles.nus_tx_handle = p_chars[i].characteristic.handle_value;
nus_c_evt.handles.nus_tx_cccd_handle = p_chars[i].cccd_handle;
break;
case BLE_UUID_NUS_TX_BLOCK_CHARACTERISTIC:
nus_c_evt.handles.nus_tx_block_handle = p_chars[i].characteristic.handle_value;
nus_c_evt.handles.nus_tx_cccd_handle = p_chars[i].cccd_handle;
break;
default:
break;
}
}
if (p_ble_nus_c->evt_handler != NULL)
{
nus_c_evt.conn_handle = p_evt->conn_handle;
nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCOVERY_COMPLETE;
p_ble_nus_c->evt_handler(p_ble_nus_c, &nus_c_evt);
}
default:
break;
}
}
if (p_ble_nus_c->evt_handler != NULL) {
nus_c_evt.conn_handle = p_evt->conn_handle;
nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCOVERY_COMPLETE;
p_ble_nus_c->evt_handler(p_ble_nus_c, &nus_c_evt);
}
}
}
/**@brief Function for handling Handle Value Notification received from the SoftDevice.
@ -122,198 +103,154 @@ void ble_nus_c_on_db_disc_evt(ble_nus_c_t * p_ble_nus_c, ble_db_discovery_evt_t
* @details This function uses the Handle Value Notification received from the SoftDevice
* and checks if it is a notification of the NUS TX characteristic from the peer.
* If it is, this function decodes the data and sends it to the application.
*
*
* @param[in] p_ble_nus_c Pointer to the NUS Client structure.
* @param[in] p_ble_evt Pointer to the BLE event received.
*/
static void on_hvx(ble_nus_c_t * p_ble_nus_c, ble_evt_t const * p_ble_evt)
{
// HVX can only occur from client sending.
if ( (p_ble_nus_c->handles.nus_tx_handle != BLE_GATT_HANDLE_INVALID)
&& (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_nus_c->handles.nus_tx_handle)
&& (p_ble_nus_c->evt_handler != NULL))
{
ble_nus_c_evt_t ble_nus_c_evt;
ble_nus_c_evt.evt_type = BLE_NUS_C_EVT_NUS_TX_EVT;
ble_nus_c_evt.p_data = (uint8_t *)p_ble_evt->evt.gattc_evt.params.hvx.data;
ble_nus_c_evt.data_len = p_ble_evt->evt.gattc_evt.params.hvx.len;
p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt);
NRF_LOG_DEBUG("Client sending data.");
}
if ( (p_ble_nus_c->handles.nus_tx_block_handle != BLE_GATT_HANDLE_INVALID)
&& (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_nus_c->handles.nus_tx_block_handle)
&& (p_ble_nus_c->evt_handler != NULL))
{
ble_nus_c_evt_t ble_nus_c_evt;
ble_nus_c_evt.evt_type = BLE_NUS_C_EVT_NUS_TX_BLOCK_EVT;
ble_nus_c_evt.p_data = (uint8_t *)p_ble_evt->evt.gattc_evt.params.hvx.data;
ble_nus_c_evt.data_len = p_ble_evt->evt.gattc_evt.params.hvx.len;
p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt);
NRF_LOG_DEBUG("Client sending data.");
}
static void on_hvx(ble_nus_c_t *p_ble_nus_c, ble_evt_t const *p_ble_evt) {
// HVX can only occur from client sending.
if ((p_ble_nus_c->handles.nus_tx_handle != BLE_GATT_HANDLE_INVALID) && (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_nus_c->handles.nus_tx_handle) && (p_ble_nus_c->evt_handler != NULL)) {
ble_nus_c_evt_t ble_nus_c_evt;
ble_nus_c_evt.evt_type = BLE_NUS_C_EVT_NUS_TX_EVT;
ble_nus_c_evt.p_data = (uint8_t *)p_ble_evt->evt.gattc_evt.params.hvx.data;
ble_nus_c_evt.data_len = p_ble_evt->evt.gattc_evt.params.hvx.len;
p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt);
NRF_LOG_DEBUG("Client sending data.");
}
}
uint32_t ble_nus_c_init(ble_nus_c_t * p_ble_nus_c, ble_nus_c_init_t * p_ble_nus_c_init)
{
uint32_t err_code;
ble_uuid_t uart_uuid;
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
uint32_t ble_nus_c_init(ble_nus_c_t *p_ble_nus_c, ble_nus_c_init_t *p_ble_nus_c_init) {
uint32_t err_code;
ble_uuid_t uart_uuid;
ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init);
VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init->p_gatt_queue);
VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init);
VERIFY_PARAM_NOT_NULL(p_ble_nus_c_init->p_gatt_queue);
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_ble_nus_c->uuid_type);
VERIFY_SUCCESS(err_code);
err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_ble_nus_c->uuid_type);
VERIFY_SUCCESS(err_code);
uart_uuid.type = p_ble_nus_c->uuid_type;
uart_uuid.uuid = BLE_UUID_NUS_SERVICE;
uart_uuid.type = p_ble_nus_c->uuid_type;
uart_uuid.uuid = BLE_UUID_NUS_SERVICE;
p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID;
p_ble_nus_c->evt_handler = p_ble_nus_c_init->evt_handler;
p_ble_nus_c->error_handler = p_ble_nus_c_init->error_handler;
p_ble_nus_c->handles.nus_tx_handle = BLE_GATT_HANDLE_INVALID;
p_ble_nus_c->handles.nus_tx_block_handle = BLE_GATT_HANDLE_INVALID;
p_ble_nus_c->handles.nus_rx_handle = BLE_GATT_HANDLE_INVALID;
p_ble_nus_c->p_gatt_queue = p_ble_nus_c_init->p_gatt_queue;
p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID;
p_ble_nus_c->evt_handler = p_ble_nus_c_init->evt_handler;
p_ble_nus_c->error_handler = p_ble_nus_c_init->error_handler;
p_ble_nus_c->handles.nus_tx_handle = BLE_GATT_HANDLE_INVALID;
p_ble_nus_c->handles.nus_rx_handle = BLE_GATT_HANDLE_INVALID;
p_ble_nus_c->p_gatt_queue = p_ble_nus_c_init->p_gatt_queue;
return ble_db_discovery_evt_register(&uart_uuid);
return ble_db_discovery_evt_register(&uart_uuid);
}
void ble_nus_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
ble_nus_c_t * p_ble_nus_c = (ble_nus_c_t *)p_context;
void ble_nus_c_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
ble_nus_c_t *p_ble_nus_c = (ble_nus_c_t *)p_context;
if ((p_ble_nus_c == NULL) || (p_ble_evt == NULL))
{
return;
}
if ((p_ble_nus_c == NULL) || (p_ble_evt == NULL)) {
return;
}
if ( (p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID)
||(p_ble_nus_c->conn_handle != p_ble_evt->evt.gap_evt.conn_handle)
)
{
return;
}
if ((p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID) || (p_ble_nus_c->conn_handle != p_ble_evt->evt.gap_evt.conn_handle)) {
return;
}
switch (p_ble_evt->header.evt_id)
{
case BLE_GATTC_EVT_HVX:
on_hvx(p_ble_nus_c, p_ble_evt);
break;
switch (p_ble_evt->header.evt_id) {
case BLE_GATTC_EVT_HVX:
on_hvx(p_ble_nus_c, p_ble_evt);
break;
case BLE_GAP_EVT_DISCONNECTED:
if (p_ble_evt->evt.gap_evt.conn_handle == p_ble_nus_c->conn_handle
&& p_ble_nus_c->evt_handler != NULL)
{
ble_nus_c_evt_t nus_c_evt;
case BLE_GAP_EVT_DISCONNECTED:
if (p_ble_evt->evt.gap_evt.conn_handle == p_ble_nus_c->conn_handle && p_ble_nus_c->evt_handler != NULL) {
ble_nus_c_evt_t nus_c_evt;
nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCONNECTED;
nus_c_evt.evt_type = BLE_NUS_C_EVT_DISCONNECTED;
p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID;
p_ble_nus_c->evt_handler(p_ble_nus_c, &nus_c_evt);
}
break;
p_ble_nus_c->conn_handle = BLE_CONN_HANDLE_INVALID;
p_ble_nus_c->evt_handler(p_ble_nus_c, &nus_c_evt);
}
break;
default:
// No implementation needed.
break;
}
default:
// No implementation needed.
break;
}
}
/**@brief Function for creating a message for writing to the CCCD. */
static uint32_t cccd_configure(ble_nus_c_t * p_ble_nus_c, bool notification_enable)
{
nrf_ble_gq_req_t cccd_req;
uint8_t cccd[BLE_CCCD_VALUE_LEN];
uint16_t cccd_val = notification_enable ? BLE_GATT_HVX_NOTIFICATION : 0;
memset(&cccd_req, 0, sizeof(nrf_ble_gq_req_t));
cccd[0] = LSB_16(cccd_val);
cccd[1] = MSB_16(cccd_val);
cccd_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
cccd_req.error_handler.cb = gatt_error_handler;
cccd_req.error_handler.p_ctx = p_ble_nus_c;
cccd_req.params.gattc_write.handle = p_ble_nus_c->handles.nus_tx_cccd_handle;
cccd_req.params.gattc_write.len = BLE_CCCD_VALUE_LEN;
cccd_req.params.gattc_write.offset = 0;
cccd_req.params.gattc_write.p_value = cccd;
cccd_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_REQ;
cccd_req.params.gattc_write.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
return nrf_ble_gq_item_add(p_ble_nus_c->p_gatt_queue, &cccd_req, p_ble_nus_c->conn_handle);
static uint32_t cccd_configure(ble_nus_c_t *p_ble_nus_c, bool notification_enable) {
nrf_ble_gq_req_t cccd_req;
uint8_t cccd[BLE_CCCD_VALUE_LEN];
uint16_t cccd_val = notification_enable ? BLE_GATT_HVX_NOTIFICATION : 0;
memset(&cccd_req, 0, sizeof(nrf_ble_gq_req_t));
cccd[0] = LSB_16(cccd_val);
cccd[1] = MSB_16(cccd_val);
cccd_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
cccd_req.error_handler.cb = gatt_error_handler;
cccd_req.error_handler.p_ctx = p_ble_nus_c;
cccd_req.params.gattc_write.handle = p_ble_nus_c->handles.nus_tx_cccd_handle;
cccd_req.params.gattc_write.len = BLE_CCCD_VALUE_LEN;
cccd_req.params.gattc_write.offset = 0;
cccd_req.params.gattc_write.p_value = cccd;
cccd_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_REQ;
cccd_req.params.gattc_write.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
return nrf_ble_gq_item_add(p_ble_nus_c->p_gatt_queue, &cccd_req, p_ble_nus_c->conn_handle);
}
uint32_t ble_nus_c_tx_notif_enable(ble_nus_c_t *p_ble_nus_c) {
VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
uint32_t ble_nus_c_tx_notif_enable(ble_nus_c_t * p_ble_nus_c)
{
VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
if ( (p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID)
||(p_ble_nus_c->handles.nus_tx_cccd_handle == BLE_GATT_HANDLE_INVALID)
)
{
return NRF_ERROR_INVALID_STATE;
}
return cccd_configure(p_ble_nus_c, true);
if ((p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID) || (p_ble_nus_c->handles.nus_tx_cccd_handle == BLE_GATT_HANDLE_INVALID)) {
return NRF_ERROR_INVALID_STATE;
}
return cccd_configure(p_ble_nus_c, true);
}
uint32_t ble_nus_c_string_send(ble_nus_c_t * p_ble_nus_c, uint8_t * p_string, uint16_t length)
{
VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
nrf_ble_gq_req_t write_req;
memset(&write_req, 0, sizeof(nrf_ble_gq_req_t));
if (length > BLE_NUS_MAX_DATA_LEN)
{
NRF_LOG_WARNING("Content too long.");
return NRF_ERROR_INVALID_PARAM;
}
if (p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID)
{
NRF_LOG_WARNING("Connection handle invalid.");
return NRF_ERROR_INVALID_STATE;
}
write_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
write_req.error_handler.cb = gatt_error_handler;
write_req.error_handler.p_ctx = p_ble_nus_c;
write_req.params.gattc_write.handle = p_ble_nus_c->handles.nus_rx_handle;
write_req.params.gattc_write.len = length;
write_req.params.gattc_write.offset = 0;
write_req.params.gattc_write.p_value = p_string;
write_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_CMD;
write_req.params.gattc_write.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
return nrf_ble_gq_item_add(p_ble_nus_c->p_gatt_queue, &write_req, p_ble_nus_c->conn_handle);
uint32_t ble_nus_c_string_send(ble_nus_c_t *p_ble_nus_c, uint8_t *p_string, uint16_t length) {
VERIFY_PARAM_NOT_NULL(p_ble_nus_c);
nrf_ble_gq_req_t write_req;
memset(&write_req, 0, sizeof(nrf_ble_gq_req_t));
if (length > BLE_NUS_MAX_DATA_LEN) {
NRF_LOG_WARNING("Content too long.");
return NRF_ERROR_INVALID_PARAM;
}
if (p_ble_nus_c->conn_handle == BLE_CONN_HANDLE_INVALID) {
NRF_LOG_WARNING("Connection handle invalid.");
return NRF_ERROR_INVALID_STATE;
}
write_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
write_req.error_handler.cb = gatt_error_handler;
write_req.error_handler.p_ctx = p_ble_nus_c;
write_req.params.gattc_write.handle = p_ble_nus_c->handles.nus_rx_handle;
write_req.params.gattc_write.len = length;
write_req.params.gattc_write.offset = 0;
write_req.params.gattc_write.p_value = p_string;
write_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_CMD;
write_req.params.gattc_write.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE;
return nrf_ble_gq_item_add(p_ble_nus_c->p_gatt_queue, &write_req, p_ble_nus_c->conn_handle);
}
uint32_t ble_nus_c_handles_assign(ble_nus_c_t *p_ble_nus, uint16_t conn_handle, ble_nus_c_handles_t const *p_peer_handles) {
VERIFY_PARAM_NOT_NULL(p_ble_nus);
uint32_t ble_nus_c_handles_assign(ble_nus_c_t * p_ble_nus,
uint16_t conn_handle,
ble_nus_c_handles_t const * p_peer_handles)
{
VERIFY_PARAM_NOT_NULL(p_ble_nus);
p_ble_nus->conn_handle = conn_handle;
if (p_peer_handles != NULL)
{
p_ble_nus->handles.nus_tx_cccd_handle = p_peer_handles->nus_tx_cccd_handle;
p_ble_nus->handles.nus_tx_handle = p_peer_handles->nus_tx_handle;
p_ble_nus->handles.nus_tx_block_handle = p_peer_handles->nus_tx_block_handle;
p_ble_nus->handles.nus_tx_block_cccd_handle = p_peer_handles->nus_tx_block_cccd_handle;
p_ble_nus->handles.nus_rx_handle = p_peer_handles->nus_rx_handle;
}
return nrf_ble_gq_conn_handle_register(p_ble_nus->p_gatt_queue, conn_handle);
p_ble_nus->conn_handle = conn_handle;
if (p_peer_handles != NULL) {
p_ble_nus->handles.nus_tx_cccd_handle = p_peer_handles->nus_tx_cccd_handle;
p_ble_nus->handles.nus_tx_handle = p_peer_handles->nus_tx_handle;
p_ble_nus->handles.nus_rx_handle = p_peer_handles->nus_rx_handle;
}
return nrf_ble_gq_conn_handle_register(p_ble_nus->p_gatt_queue, conn_handle);
}
#endif // NRF_MODULE_ENABLED(BLE_NUS_C)
#endif

19
ble_app_uart_c/zble_nus_c.h

@ -100,10 +100,9 @@ extern "C" {
{ 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E } \
} /**< Used vendor-specific UUID. */
#define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
#define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
#define BLE_UUID_NUS_TX_BLOCK_CHARACTERISTIC 0x0004 /**< The UUID of the TX Characteristic. */
#define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
#define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
#define OPCODE_LENGTH 1
#define HANDLE_LENGTH 2
@ -120,7 +119,6 @@ extern "C" {
typedef enum {
BLE_NUS_C_EVT_DISCOVERY_COMPLETE, /**< Event indicating that the NUS service and its characteristics were found. */
BLE_NUS_C_EVT_NUS_TX_EVT, /**< Event indicating that the central received something from a peer. */
BLE_NUS_C_EVT_NUS_TX_BLOCK_EVT, /**< Event indicating that the central received something from a peer. */
BLE_NUS_C_EVT_DISCONNECTED /**< Event indicating that the NUS server disconnected. */
} ble_nus_c_evt_type_t;
@ -128,11 +126,7 @@ typedef enum {
typedef struct {
uint16_t nus_tx_handle; /**< Handle of the NUS TX characteristic, as provided by a discovery. */
uint16_t nus_tx_cccd_handle; /**< Handle of the CCCD of the NUS TX characteristic, as provided by a discovery. */
uint16_t nus_tx_block_handle; /**< Handle of the NUS TX characteristic, as provided by a discovery. */
uint16_t nus_tx_block_cccd_handle; /**< Handle of the CCCD of the NUS TX characteristic, as provided by a discovery. */
uint16_t nus_rx_handle; /**< Handle of the NUS RX characteristic, as provided by a discovery. */
uint16_t nus_rx_handle; /**< Handle of the NUS RX characteristic, as provided by a discovery. */
} ble_nus_c_handles_t;
/**@brief Structure containing the NUS event data received from the peer. */
@ -262,7 +256,4 @@ uint32_t ble_nus_c_handles_assign(ble_nus_c_t* p_ble_nus_c, uint16_t conn_handle
#ifdef __cplusplus
}
#endif
#endif // BLE_NUS_C_H__
/** @} */
#endif
Loading…
Cancel
Save