|
@ -44,8 +44,8 @@ |
|
|
#include "app_error.h" |
|
|
#include "app_error.h" |
|
|
#include "ble.h" |
|
|
#include "ble.h" |
|
|
#include "ble_gattc.h" |
|
|
#include "ble_gattc.h" |
|
|
#include "ble_nus_c.h" |
|
|
|
|
|
#include "ble_srv_common.h" |
|
|
#include "ble_srv_common.h" |
|
|
|
|
|
#include "zble_nus_c.h" |
|
|
|
|
|
|
|
|
#define NRF_LOG_MODULE_NAME ble_nus_c |
|
|
#define NRF_LOG_MODULE_NAME ble_nus_c |
|
|
#include "nrf_log.h" |
|
|
#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: |
|
|
case BLE_UUID_NUS_RX_CHARACTERISTIC: |
|
|
nus_c_evt.handles.nus_rx_handle = p_chars[i].characteristic.handle_value; |
|
|
nus_c_evt.handles.nus_rx_handle = p_chars[i].characteristic.handle_value; |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case BLE_UUID_NUS_TX_CHARACTERISTIC: |
|
|
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_handle = p_chars[i].characteristic.handle_value; |
|
|
nus_c_evt.handles.nus_tx_cccd_handle = p_chars[i].cccd_handle; |
|
|
nus_c_evt.handles.nus_tx_cccd_handle = p_chars[i].cccd_handle; |
|
|
break; |
|
|
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: |
|
|
default: |
|
|
break; |
|
|
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); |
|
|
p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt); |
|
|
NRF_LOG_DEBUG("Client sending data."); |
|
|
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) { |
|
|
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.type = p_ble_nus_c->uuid_type; |
|
|
uart_uuid.uuid = BLE_UUID_NUS_SERVICE; |
|
|
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); |
|
|
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); |
|
|
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) { |
|
|
uint32_t ble_nus_c_tx_notif_enable(ble_nus_c_t *p_ble_nus_c) { |
|
|
VERIFY_PARAM_NOT_NULL(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)) { |
|
|
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 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) { |
|
|
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; |
|
|
p_ble_nus->conn_handle = conn_handle; |
|
|
if (p_peer_handles != NULL) { |
|
|
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); |
|
|
return nrf_ble_gq_conn_handle_register(p_ble_nus->p_gatt_queue, conn_handle); |
|
|
} |
|
|
} |
|
|