From 226c99ab0c1670664c98928791fd605565d8d9c7 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 8 Jul 2017 00:29:38 +0700 Subject: [PATCH] implement #41 read peer Gap Device Name --- libraries/Bluefruit52Lib/src/BLEGap.cpp | 10 ++++++++++ libraries/Bluefruit52Lib/src/BLEGap.h | 3 +++ libraries/Bluefruit52Lib/src/BLEGatt.cpp | 22 ++++++++++++---------- libraries/Bluefruit52Lib/src/BLEGatt.h | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index 2fadcecb..352cb149 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -53,6 +53,16 @@ bool BLEGap::getTxPacket(uint16_t conn_handle) return xSemaphoreTake(_txpacket_sem[conn_handle], ms2tick(BLE_GENERIC_TIMEOUT)); } +uint16_t BLEGap::getPeerDevName(char* buf, uint16_t bufsize) +{ + return getPeerDevName(Bluefruit.connHandle(), buf, bufsize); +} + +uint16_t BLEGap::getPeerDevName(uint16_t conn_handle, char* buf, uint16_t bufsize) +{ + return Bluefruit.Gatt.readCharByUuid(conn_handle, BLEUuid(BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME), buf, bufsize); +} + void BLEGap::_eventHandler(ble_evt_t* evt) { // conn handle has fixed offset regardless of event type diff --git a/libraries/Bluefruit52Lib/src/BLEGap.h b/libraries/Bluefruit52Lib/src/BLEGap.h index 6d7c9256..7ee424f2 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.h +++ b/libraries/Bluefruit52Lib/src/BLEGap.h @@ -50,6 +50,9 @@ class BLEGap bool getTxPacket(void); bool getTxPacket(uint16_t conn_handle); + uint16_t getPeerDevName(char* buf, uint16_t bufsize); + uint16_t getPeerDevName(uint16_t conn_handle, char* buf, uint16_t bufsize); + /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY * Although declare as public, it is meant to be invoked by internal diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 9f86e616..a2a57032 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -38,7 +38,7 @@ BLEGatt::BLEGatt(void) - //: _adamsg() + : _adamsg() { varclr(&_server); varclr(&_client); @@ -46,17 +46,20 @@ BLEGatt::BLEGatt(void) uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffer, uint16_t bufsize, uint16_t start_hdl, uint16_t end_hdl) { -#if 0 + int32_t count = 0; ble_gattc_handle_range_t hdl_range = { .start_handle = start_hdl, .end_handle = end_hdl }; + _adamsg.begin(true); _adamsg.prepare(buffer, bufsize); - VERIFY_STATUS( sd_ble_gattc_char_value_by_uuid_read(conn_hdl, &bleuuid._uuid, &hdl_range), 0); - int32_t count = _adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT); + if( NRF_SUCCESS == sd_ble_gattc_char_value_by_uuid_read(conn_hdl, &bleuuid._uuid, &hdl_range) ) + { + count = _adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT); + } + + _adamsg.stop(); + return (count < 0) ? 0 : count; -#endif - - return 0; } void BLEGatt::_eventHandler(ble_evt_t* evt) @@ -94,7 +97,6 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) for(int i=0; i<_client.svc_count; i++) _client.svc_list[i]->disconnect(); } -#if 0 // GATTC Read Characteristic by UUID procedure if ( evt->header.evt_id == BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP ) { @@ -104,10 +106,10 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) PRINT_INT(rd_rsp->value_len); if (rd_rsp->count) { - _adamsg.feed(rd_rsp->handle_value[0].p_value, rd_rsp->value_len) + _adamsg.feed(rd_rsp->handle_value[0].p_value, rd_rsp->value_len); + _adamsg.complete(); } } -#endif } /*------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.h b/libraries/Bluefruit52Lib/src/BLEGatt.h index cb62f21c..b7396ab2 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.h +++ b/libraries/Bluefruit52Lib/src/BLEGatt.h @@ -89,7 +89,7 @@ class BLEGatt BLEClientCharacteristic* chr_list[BLE_GATT_MAX_CLIENT_CHARS]; }_client; -// AdaMsg _adamsg; + AdaMsg _adamsg; }; #endif /* BLEGATT_H_ */