From ec1efd2656534f3eb7109508bd8f690c64af159c Mon Sep 17 00:00:00 2001 From: zhaohe Date: Mon, 29 Jan 2024 23:40:29 +0800 Subject: [PATCH] update --- app/src/one_conduction/one_conduction_main.c | 2 - .../ble_app_uart_c_pca10100_s122.uvguix.h_zha | 2 +- ble_app_uart_c/zble_nus_c.c | 70 ++++++++++++++++++---- ble_app_uart_c/zble_nus_c.h | 11 +++- 4 files changed, 67 insertions(+), 18 deletions(-) diff --git a/app/src/one_conduction/one_conduction_main.c b/app/src/one_conduction/one_conduction_main.c index 070d79a..3b38dbb 100644 --- a/app/src/one_conduction/one_conduction_main.c +++ b/app/src/one_conduction/one_conduction_main.c @@ -232,8 +232,6 @@ void app_event_process_cb(void* p_event_data, uint16_t event_size) { 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) { /******************************************************************************* diff --git a/ble_app_uart_c/ble_app_uart_c_pca10100_s122.uvguix.h_zha b/ble_app_uart_c/ble_app_uart_c_pca10100_s122.uvguix.h_zha index abe62ad..71e962e 100644 --- a/ble_app_uart_c/ble_app_uart_c_pca10100_s122.uvguix.h_zha +++ b/ble_app_uart_c/ble_app_uart_c_pca10100_s122.uvguix.h_zha @@ -1822,7 +1822,7 @@ Build 1024 - 00200000010000001000FFFF01001100434D4643546F6F6C426172427574746F6ECF7F0000000000001C0000000000000000000000000000000001000000010000000180D07F0000000002001D000000000000000000000000000000000100000001000000018030800000000000001E000000000000000000000000000000000100000001000000FFFF01001500434D4643546F6F6C4261724D656E75427574746F6EC7040000000000006A0000000C4261746368204275696C2664000000000000000000000000010000000100000000000000000000000100000004000580C7040000000000006A0000000C4261746368204275696C266400000000000000000000000001000000010000000000000000000000010000000000058046070000000000006B0000000D42617463682052656275696C640000000000000000000000000100000001000000000000000000000001000000000005804707000000000000FFFFFFFF0B426174636820436C65616E0100000000000000000000000100000001000000000000000000000001000000000005809E8A0000000000001F0000000F4261746326682053657475702E2E2E000000000000000000000000010000000100000000000000000000000100000000000180D17F0000000004002000000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF00000000000000000000000000010000000100000001804C8A0000000000002100000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF000000000000000000000000000100000001000000FFFF01001900434D4643546F6F6C426172436F6D626F426F78427574746F6EBA00000000000000000000000000000000000000000000000001000000010000009600000003002050000000000D6E726635323833335F78786161960000000000000002000D6E726635323833335F7878616121666C6173685F733132325F6E726635325F382E302E305F736F667464657669636500000000000000000180EB880000000000002200000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF0000000000000000000000000001000000010000000180C07F000000000000230000000000000000000000000000000001000000010000000180B08A000000000400240000000000000000000000000000000001000000010000000180A8010000000000004E00000000000000000000000000000000010000000100000001807202000000000000530000000000000000000000000000000001000000010000000180BE010000000000005000000000000000000000000000000000010000000100000000000000054275696C64DC010000 + 00200000010000001000FFFF01001100434D4643546F6F6C426172427574746F6ECF7F0000000000001C0000000000000000000000000000000001000000010000000180D07F0000000000001D000000000000000000000000000000000100000001000000018030800000000000001E000000000000000000000000000000000100000001000000FFFF01001500434D4643546F6F6C4261724D656E75427574746F6EC7040000000000006A0000000C4261746368204275696C2664000000000000000000000000010000000100000000000000000000000100000004000580C7040000000000006A0000000C4261746368204275696C266400000000000000000000000001000000010000000000000000000000010000000000058046070000000000006B0000000D42617463682052656275696C640000000000000000000000000100000001000000000000000000000001000000000005804707000000000000FFFFFFFF0B426174636820436C65616E0100000000000000000000000100000001000000000000000000000001000000000005809E8A0000000000001F0000000F4261746326682053657475702E2E2E000000000000000000000000010000000100000000000000000000000100000000000180D17F0000000004002000000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF00000000000000000000000000010000000100000001804C8A0000000000002100000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF000000000000000000000000000100000001000000FFFF01001900434D4643546F6F6C426172436F6D626F426F78427574746F6EBA00000000000000000000000000000000000000000000000001000000010000009600000003002050000000000D6E726635323833335F78786161960000000000000002000D6E726635323833335F7878616121666C6173685F733132325F6E726635325F382E302E305F736F667464657669636500000000000000000180EB880000000000002200000000000000000000000000000000010000000100000001800000000001000000FFFFFFFF0000000000000000000000000001000000010000000180C07F000000000000230000000000000000000000000000000001000000010000000180B08A000000000400240000000000000000000000000000000001000000010000000180A8010000000000004E00000000000000000000000000000000010000000100000001807202000000000000530000000000000000000000000000000001000000010000000180BE010000000000005000000000000000000000000000000000010000000100000000000000054275696C64DC010000 583 diff --git a/ble_app_uart_c/zble_nus_c.c b/ble_app_uart_c/zble_nus_c.c index 539fab9..16673b6 100644 --- a/ble_app_uart_c/zble_nus_c.c +++ b/ble_app_uart_c/zble_nus_c.c @@ -44,8 +44,8 @@ #include "app_error.h" #include "ble.h" #include "ble_gattc.h" -#include "ble_nus_c.h" #include "ble_srv_common.h" +#include "zble_nus_c.h" #define NRF_LOG_MODULE_NAME ble_nus_c #include "nrf_log.h" @@ -80,11 +80,14 @@ void ble_nus_c_on_db_disc_evt(ble_nus_c_t *p_ble_nus_c, ble_db_discovery_evt_t * 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_CHARACTERISTIC_2: + nus_c_evt.handles.nus_tx_handle_2 = p_chars[i].characteristic.handle_value; + nus_c_evt.handles.nus_tx_cccd_handle_2 = p_chars[i].cccd_handle; + break; default: break; @@ -119,6 +122,18 @@ static void on_hvx(ble_nus_c_t *p_ble_nus_c, ble_evt_t const *p_ble_evt) { 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_handle_2 != BLE_GATT_HANDLE_INVALID) && (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_nus_c->handles.nus_tx_handle_2) && (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_2; + 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) { @@ -136,12 +151,13 @@ uint32_t ble_nus_c_init(ble_nus_c_t *p_ble_nus_c, ble_nus_c_init_t *p_ble_nus_c_ 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_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_tx_handle_2 = 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); } @@ -203,13 +219,40 @@ static uint32_t cccd_configure(ble_nus_c_t *p_ble_nus_c, bool notification_enabl 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_2(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_2; + 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); 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_2 == BLE_GATT_HANDLE_INVALID)) { + return NRF_ERROR_INVALID_STATE; + } + cccd_configure(p_ble_nus_c, true); + return cccd_configure_2(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) { @@ -246,9 +289,12 @@ uint32_t ble_nus_c_handles_assign(ble_nus_c_t *p_ble_nus, uint16_t 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; + 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_cccd_handle_2 = p_peer_handles->nus_tx_cccd_handle_2; + p_ble_nus->handles.nus_tx_handle_2 = p_peer_handles->nus_tx_handle_2; + + 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); } diff --git a/ble_app_uart_c/zble_nus_c.h b/ble_app_uart_c/zble_nus_c.h index 8ca332b..680845b 100644 --- a/ble_app_uart_c/zble_nus_c.h +++ b/ble_app_uart_c/zble_nus_c.h @@ -100,9 +100,10 @@ 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_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_CHARACTERISTIC_2 0x0004 /**< The UUID of the TX Characteristic. */ #define OPCODE_LENGTH 1 #define HANDLE_LENGTH 2 @@ -119,6 +120,7 @@ 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_EVT_2, /**< 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; @@ -126,6 +128,9 @@ 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_handle_2; /**< Handle of the NUS RX characteristic, as provided by a discovery. */ + uint16_t nus_tx_cccd_handle_2; /**< 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. */ } ble_nus_c_handles_t;