Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ble/develop #1740

Merged
merged 120 commits into from May 31, 2019
Merged
Changes from 1 commit
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
59409e0
BLE: Implement some of the BLE HAL APIs.
XuGuohui Nov 28, 2018
04d8ed7
BLE: finalize the BLE peripheral role.
XuGuohui Nov 30, 2018
1d78879
BLE: fixes some compilation issues.
XuGuohui Dec 1, 2018
1b73927
BLE: do not include TX power for the default advertising type
XuGuohui Jan 7, 2019
f08f2e3
BLE: fix the BLE event callback definition.
XuGuohui Jan 7, 2019
f2b662d
BLE: add more trace log.
XuGuohui Jan 7, 2019
1952daf
BLE: restart advertising on disconnected.
XuGuohui Jan 7, 2019
de4d8f3
BLE: resort GATT server APIs.
XuGuohui Jan 8, 2019
d874c0c
BLE: Remove char_handle parameter from the add descriptor API.
XuGuohui Jan 8, 2019
6471783
BLE: don't need to pass in the connection handle in ble_publish().
XuGuohui Jan 8, 2019
01daea4
BLE: use BLE_CONN_HANDLE_INVALID when set/get characteristic value lo…
XuGuohui Jan 8, 2019
816d4a0
BLE: optionally specify the user description when add a characteristic
XuGuohui Jan 8, 2019
4f84c25
BLE: Implement ble_connected().
XuGuohui Jan 9, 2019
e9083e6
BLE: conditionally exclude legacy hal driver and add test application
XuGuohui Jan 9, 2019
3bd4c00
BLE: collect BLE status infomations.
XuGuohui Jan 9, 2019
ceb341b
BLE: implement BLE scanner and add test app.
XuGuohui Jan 10, 2019
f8cd5b3
BLE: organize all the BLE informations in an object.
XuGuohui Jan 10, 2019
4889378
BLE: add function to decode advertising data.
XuGuohui Jan 11, 2019
2d34d23
BLE: implement BLE connect APIs.
XuGuohui Jan 12, 2019
a7cf4df
BLE: ble_disconnect behave accordingly.
XuGuohui Jan 16, 2019
de01de8
BLE: increase the count of vendor base UUID.
XuGuohui Jan 16, 2019
5eb082d
BLE: fix ble_connect() minor issue.
XuGuohui Jan 16, 2019
dbda6f1
BLE: add default connection parameters and BLE Central example.
XuGuohui Jan 16, 2019
f31d1c8
BLE: config advertising or scan response data snippet without refresh…
XuGuohui Jan 17, 2019
d6e6070
BLE: implement BLE database discovery APIs.
XuGuohui Jan 24, 2019
aa9352b
BLE: implement ble_is_discovering() and increase database discovery r…
XuGuohui Jan 25, 2019
c58e35c
BLE: implement GATT client notification/indication functionality.
XuGuohui Jan 25, 2019
8c096b8
BLE: implement GATT client read/write functionalities.
XuGuohui Jan 25, 2019
dc3832b
BLE: coding style and function name fixes.
XuGuohui Feb 14, 2019
29f6dc4
BLE: minor fixes and add BLE Peripheral unit test.
XuGuohui Feb 18, 2019
bc1bf3a
BLE: rename files.
XuGuohui Feb 18, 2019
513a42f
BLE: implement ble_select_antenna().
XuGuohui Feb 18, 2019
0d24b25
BLE: optimize BLE events routing.
XuGuohui Feb 20, 2019
5c40e6f
BLE: add an reserved argument for some of the BLE APIs.
XuGuohui Feb 20, 2019
8987d02
BLE: minor fix and add BLE Central unit test.
XuGuohui Feb 21, 2019
7790b9d
BLE: Wait until some of the BLE procedure is completed.
XuGuohui Feb 23, 2019
1edd4b3
BLE: single callback for handling all BLE events.
XuGuohui Feb 27, 2019
89fe928
BLE: set characteristic value if notify/indicate successfully.
XuGuohui Feb 27, 2019
48b9a61
BLE: distinguish data received event.
XuGuohui Mar 5, 2019
d2166c0
BLE: add an argument for ble_gap_connect()
XuGuohui Mar 5, 2019
5f04956
BLE: re-name types.
XuGuohui Mar 5, 2019
5d7aebe
BLE: update HAL APIs and add more examples.
XuGuohui Mar 11, 2019
2732c8d
BLE: exports BLE HAL APIs.
XuGuohui Mar 11, 2019
af58eaa
BLE: SoM EVBs doesn't have antenna switch.
XuGuohui Mar 11, 2019
6ba9540
BLE: update HAL module.
XuGuohui Mar 15, 2019
348af23
BLE: first wiring implementation.
XuGuohui Mar 15, 2019
1cc19de
BLE: add function stopAdvertise()
XuGuohui Mar 15, 2019
ed3609c
BLE: update wiring examples.
XuGuohui Mar 15, 2019
1832c9e
BLE: refactor Broadcaster and Observer role.
XuGuohui Mar 17, 2019
7599cf1
BLE: wait until BLE scan stopped.
XuGuohui Mar 17, 2019
3d1d549
BLE: fix BLE Observer issue.
XuGuohui Mar 17, 2019
4a30980
BLE: refactor Peripheral and Central role.
XuGuohui Mar 19, 2019
fe63017
BLE: hide the implementation of characteristic.
XuGuohui Mar 21, 2019
2ad0ead
BLE: hide the implementation of GATT server and client
XuGuohui Mar 25, 2019
28c04f5
BLE: add an example.
XuGuohui Mar 25, 2019
4d65537
BLE: hide the implementation of GAP roles
XuGuohui Mar 25, 2019
09fa39b
BLE: change the initialization sequence and fix broadcaster issue
XuGuohui Mar 25, 2019
473d826
BLE: fixes Scanner issue and update the scanner example.
XuGuohui Mar 25, 2019
5fb54ac
BLE: update the Peripheral, Oberser and Broadcaster
XuGuohui Mar 26, 2019
a768424
BLE: set the default BLE device name in HAL
XuGuohui Mar 26, 2019
0730342
BLE: add a parameter which default to false to configure an iBeacon t…
XuGuohui Mar 27, 2019
a7285e7
BLE: use template when constructing or adding characteristic. Delete …
XuGuohui Mar 28, 2019
f6914cc
BLE: template BleAdvData.appendServiceUuid()
XuGuohui Mar 28, 2019
56a4b6a
BLE: refactoring BLE control request channel.
XuGuohui Mar 30, 2019
a716454
BLE: fixes BLE HAL issue.
XuGuohui Apr 2, 2019
02a273f
BLE: Update wiring APIs and temporarily disable BLE control request c…
XuGuohui Apr 2, 2019
da74a6c
BLE: fixes the issue that device cannot send notification.
XuGuohui Apr 7, 2019
15e22a9
BLE: set the characteristic value length to maximum.
XuGuohui Apr 8, 2019
d3c55a9
BLE: SoftDevice has no more memory to add characteristics.
XuGuohui Apr 8, 2019
95387c8
BLE control request channel: remove timeout after connected.
XuGuohui Apr 8, 2019
b585dcf
BLE: refactor Broadcaster HAL and wiring APIs.
XuGuohui Apr 10, 2019
9cbb40e
BLE: refactor BLE Observer HAL and wiring APIs.
XuGuohui Apr 10, 2019
49edd98
BLE: refactor BLE central and peripheral HAL and wiring APIs.
XuGuohui Apr 11, 2019
5caecec
BLE: refactor GATT server HAL and wiring APIs.
XuGuohui Apr 11, 2019
fad3afe
BLE: refactor GATT client HAL APIs.
XuGuohui Apr 12, 2019
6a81985
BLE: refactor BLE HAL event dispatcher.
XuGuohui Apr 13, 2019
579b4a8
BLE: refactor HAL event dispatching.
XuGuohui Apr 14, 2019
dc454b0
BLE: finalize the ATT_MTU exchange protocol.
XuGuohui Apr 15, 2019
8b2fc7a
BLE: callback on connected/disconnected in user application.
XuGuohui Apr 16, 2019
4308b78
BLE: type fixes.
XuGuohui Apr 17, 2019
3a18ed6
BLE: increase the reserved RAM space for SoftDevice and some bug fixes.
XuGuohui Apr 18, 2019
ac7cf08
BLE: remove unused test applications and update BLE HAL unit test app…
XuGuohui Apr 18, 2019
7d4157f
BLE: fixes the insufficient heap issue during linkage.
XuGuohui Apr 19, 2019
56be941
BLE: filters out duplicated scanned results and fixes the issue that …
XuGuohui Apr 23, 2019
e76a0a7
BLE: Add characteristic context and peer device as characteristic dat…
XuGuohui Apr 23, 2019
8454246
BLE: connect to peer initially and discover all services and characte…
XuGuohui Apr 25, 2019
6ecd14f
BLE: Read characteristic user description if presented.
XuGuohui Apr 26, 2019
db56f6a
BLE: wiring GATT Client now read/write peer characteristic.
XuGuohui Apr 26, 2019
ae12a6e
BLE: rc.1 reworking stash.
XuGuohui May 1, 2019
69762a3
BLE: rc.1 reworking stash.
XuGuohui May 7, 2019
d525ece
BLE: rc.1 reworking stash.
XuGuohui May 7, 2019
be5e964
BLE: rc.1 reworking stash.
XuGuohui May 8, 2019
119ec45
BLE: rc.1 reworking stash, solve the vector issue in ISR.
XuGuohui May 9, 2019
dc46598
BLE: rc.1 reworking stash. Fixes the wiring thread-safty issue.
XuGuohui May 10, 2019
899a5ab
BLE: rc.1 reworking stash.
XuGuohui May 12, 2019
3284cb5
BLE: rc.1 reworking stash.
XuGuohui May 13, 2019
185cf6f
BLE: rc.1 reworking stash.
XuGuohui May 13, 2019
3dcdfc6
BLE: supports broadcasting when connected as Peripheral.
XuGuohui May 14, 2019
72f6fbf
BLE: implement the behaviour on entering/exiting the listening mode.
XuGuohui May 15, 2019
84855a8
BLE: simplify the wiring implementation.
XuGuohui May 20, 2019
f5bcec6
BLE: add reserved argument for all HAL APIs.
XuGuohui May 23, 2019
9402663
BLE: move the ATT MTU exchanging logic from GATT to connection manager.
XuGuohui May 23, 2019
316a0fc
BLE: nrf error to system error can be used by other Nordic APIs.
XuGuohui May 23, 2019
14d4ff2
BLE: check memory allocation result.
XuGuohui May 23, 2019
0a07aff
BLE: check-return fixes.
XuGuohui May 23, 2019
a3f7f32
BLE: avoid copy garbage data.
XuGuohui May 23, 2019
f81f9e2
BLE: resume advertising even if setting advertising parameters or adv…
XuGuohui May 23, 2019
b11e8c5
BLE: nullptring semaphore before destroying it.
XuGuohui May 23, 2019
9f94ae7
BLE: add code to check the return values.
XuGuohui May 24, 2019
b9ee46a
BLE: wiring thread safe.
XuGuohui May 24, 2019
8db7884
BLE: set to defaut advertising parameters if nullptr provided.
XuGuohui May 24, 2019
d09af1e
BLE: buildable for SoMs.
XuGuohui May 24, 2019
4d9b5f0
BLE: fixes the issue that when build with DEBUG_BUILD=y, device will …
XuGuohui May 27, 2019
37b27f9
BLE: optimize the semaphores' usage in BLE HAL.
XuGuohui May 28, 2019
c817f70
BLE: to not pollute system listening mode handler class.
XuGuohui May 28, 2019
5402a00
BLE: minor wiring fixes.
XuGuohui May 29, 2019
f15802b
BLE: decouple the BLE listening mode handler.
XuGuohui May 29, 2019
3084867
BLE: fixes race condition.
XuGuohui May 29, 2019
8150f18
BLE: fixes semaphore might be given twice.
XuGuohui May 29, 2019
e59c2f1
BLE: fixes the issue that the scan response data is wrong in a scan r…
XuGuohui May 30, 2019
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

BLE: check-return fixes.

  • Loading branch information...
XuGuohui authored and technobly committed May 23, 2019
commit 0a07affc0dda1d4eb3c788e52e753bcb77df9e5f
@@ -359,7 +359,7 @@ int hal_ble_set_callback_on_events(on_ble_evt_cb_t callback, void* context, void
/**
* Set local BLE identity address, which type must be either public or random.
*
* @param[in] address Pointer to the BLE local identity address to be set.
* @param[in] address Pointer to the BLE local identity address to be set. If nullptr, it restores the default BLE address.
*
* @returns 0 on success, system_error_t on error.
*/
@@ -377,7 +377,7 @@ int hal_ble_gap_get_device_address(hal_ble_addr_t* address, void* reserved);
/**
* Set the BLE device name.
*
* @param[in] device_name Pointer to a UTF-8 encoded, <b>non NULL-terminated</b> string.
* @param[in] device_name Pointer to a UTF-8 encoded, <b>non NULL-terminated</b> string. If nullptr, it resets to the default device name.
* @param[in] len Length of the UTF-8, <b>non NULL-terminated</b> string pointed to by device_name
* in octets (must be smaller or equal than @ref BLE_GAP_DEVNAME_MAX_LEN).
*
@@ -115,6 +115,20 @@ bool addressEqual(const hal_ble_addr_t& srcAddr, const hal_ble_addr_t& destAddr)
return (srcAddr.addr_type == destAddr.addr_type && !memcmp(srcAddr.addr, destAddr.addr, BLE_SIG_ADDR_LEN));
}

hal_ble_addr_t chipDefaultAddress() {
uint32_t addrMsb = NRF_FICR->DEVICEID[1];
uint32_t addrLsb = NRF_FICR->DEVICEID[0];
hal_ble_addr_t localAddr = {};
localAddr.addr_type = BLE_SIG_ADDR_TYPE_RANDOM_STATIC;
localAddr.addr[0] = (uint8_t)((addrMsb >> 24) & 0x000000FF);
localAddr.addr[1] = (uint8_t)((addrMsb >> 16) & 0x000000FF);
localAddr.addr[2] = (uint8_t)((addrMsb >> 8) & 0x000000FF);
localAddr.addr[3] = (uint8_t)(addrMsb & 0x000000FF);
localAddr.addr[4] = (uint8_t)((addrLsb >> 24) & 0x000000FF);
localAddr.addr[5] = (uint8_t)((addrLsb >> 16) & 0x000000FF);
return localAddr;
}

} //anonymous namespace

class GattBase {
@@ -632,20 +646,25 @@ static BleGapImpl bleGapImpl;
int BleObject::BleGap::init() {
// Set the default device name
char devName[32] = {};
get_device_name(devName, sizeof(devName));
setDeviceName(devName, strlen(devName));
CHECK(get_device_name(devName, sizeof(devName)));
CHECK(setDeviceName(devName, strlen(devName)));
bleGapImpl.instance = this;
This conversation was marked as resolved by avtolstoy

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy Apr 28, 2019

Member

Why do we need this object? Can't we simply pass this? And even that is probably not necessary because BleObject::instance()->xxx can be used at all times.

This comment has been minimized.

Copy link
@XuGuohui

XuGuohui Apr 30, 2019

Author Contributor

This is somehow the section conflict between c and c++ code:

src/nRF52840/ble_hal.cpp:2166:26: error: bleGattClient causes a section type conflict with bleGap
     NRF_SDH_BLE_OBSERVER(bleGattClient, 1, processGattClientEvents, &gattcImpl);
                          ^
../third_party/nrf5_sdk/nrf5_sdk/components/libraries/experimental_section_vars/nrf_section.h:149:5: note: in definition of macro 'NRF_SECTION_ITEM_REGISTER'
     section_var __attribute__ ((section("." STRINGIFY(section_name)))) __attribute__((used))
     ^
../third_party/nrf5_sdk/nrf5_sdk/components/softdevice/common/nrf_sdh_ble.h:85:1: note: in expansion of macro 'NRF_SECTION_SET_ITEM_REGISTER'
 NRF_SECTION_SET_ITEM_REGISTER(sdh_ble_observers, _prio, static nrf_sdh_ble_evt_observer_t _name) =  \
 ^
src/nRF52840/ble_hal.cpp:2166:5: note: in expansion of macro 'NRF_SDH_BLE_OBSERVER'
     NRF_SDH_BLE_OBSERVER(bleGattClient, 1, processGattClientEvents, &gattcImpl);
     ^
src/nRF52840/ble_hal.cpp:639:26: note: 'bleGap' was declared here
     NRF_SDH_BLE_OBSERVER(bleGap, 1, processBleGapEvents, this);
                          ^
../third_party/nrf5_sdk/nrf5_sdk/components/libraries/experimental_section_vars/nrf_section.h:149:5: note: in definition of macro 'NRF_SECTION_ITEM_REGISTER'
     section_var __attribute__ ((section("." STRINGIFY(section_name)))) __attribute__((used))
     ^
../third_party/nrf5_sdk/nrf5_sdk/components/softdevice/common/nrf_sdh_ble.h:85:1: note: in expansion of macro 'NRF_SECTION_SET_ITEM_REGISTER'
 NRF_SECTION_SET_ITEM_REGISTER(sdh_ble_observers, _prio, static nrf_sdh_ble_evt_observer_t _name) =  \
 ^
src/nRF52840/ble_hal.cpp:639:5: note: in expansion of macro 'NRF_SDH_BLE_OBSERVER'
     NRF_SDH_BLE_OBSERVER(bleGap, 1, processBleGapEvents, this);

This comment has been minimized.

Copy link
@sergeuz

sergeuz Apr 30, 2019

Member

AFAICR, you can't use this with the Nordic's NRF_SDH_BLE_OBSERVER() macro. Although a similar code was compiling fine in my tests, at runtime this was always set to NULL.

This comment has been minimized.

Copy link
@XuGuohui

XuGuohui May 7, 2019

Author Contributor

Can this be marked as resolved?

NRF_SDH_BLE_OBSERVER(bleGap, 1, processBleGapEvents, &bleGapImpl);
return SYSTEM_ERROR_NONE;
}

int BleObject::BleGap::setDeviceName(const char* deviceName, size_t len) {
char name[32] = {};
if (deviceName == nullptr || len == 0) {
This conversation was marked as resolved by XuGuohui

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy May 22, 2019

Member

nullptr device name to reset to default perhaps?

This comment has been minimized.

Copy link
@XuGuohui

XuGuohui May 22, 2019

Author Contributor

Then what should be the default device name? The device name in DCT?

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy May 22, 2019

Member

The same what we have now e.g. Boron-XXXXXX?

return SYSTEM_ERROR_INVALID_ARGUMENT;
CHECK(get_device_name(name, sizeof(name)));
len = strlen(name);
} else {
len = std::min(len, sizeof(name));
memcpy(name, deviceName, len);
}
ble_gap_conn_sec_mode_t secMode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&secMode);
int ret = sd_ble_gap_device_name_set(&secMode, (const uint8_t*)deviceName, len);
int ret = sd_ble_gap_device_name_set(&secMode, (const uint8_t*)name, len);
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
return SYSTEM_ERROR_NONE;
}
@@ -657,9 +676,9 @@ int BleObject::BleGap::getDeviceName(char* deviceName, size_t len) {
// non NULL-terminated string returned.
uint16_t nameLen = len - 1;
int ret = sd_ble_gap_device_name_get((uint8_t*)deviceName, &nameLen);
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
nameLen = std::min(len - 1, (size_t)nameLen);
deviceName[nameLen] = '\0';
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
return SYSTEM_ERROR_NONE;
}

@@ -670,16 +689,19 @@ int BleObject::BleGap::setDeviceAddress(const hal_ble_addr_t* address) {
// The identity address cannot be changed while advertising, scanning or creating a connection.
return SYSTEM_ERROR_INVALID_STATE;
}
hal_ble_addr_t newAddr = {};
if (address == nullptr) {
This conversation was marked as resolved by XuGuohui

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy May 22, 2019

Member

Perhaps reset to default instead?

This comment has been minimized.

Copy link
@XuGuohui

XuGuohui May 22, 2019

Author Contributor

The address programmed into nRF52840 during chip manufacturing?

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy May 22, 2019

Member

If that is the default, yes.

return SYSTEM_ERROR_INVALID_ARGUMENT;
newAddr = chipDefaultAddress();
} else {
newAddr = *address;
}
if (address->addr_type != BLE_SIG_ADDR_TYPE_PUBLIC && address->addr_type != BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
if (newAddr.addr_type != BLE_SIG_ADDR_TYPE_PUBLIC && newAddr.addr_type != BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
ble_gap_addr_t localAddr;
localAddr.addr_id_peer = false;
localAddr.addr_type = address->addr_type;
memcpy(localAddr.addr, address->addr, BLE_SIG_ADDR_LEN);
localAddr.addr_type = newAddr.addr_type;
memcpy(localAddr.addr, newAddr.addr, BLE_SIG_ADDR_LEN);
int ret = sd_ble_gap_addr_set(&localAddr);
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
return SYSTEM_ERROR_NONE;
@@ -1161,6 +1183,9 @@ int BleObject::Observer::startScanning(on_ble_scan_result_cb_t callback, void* c
return SYSTEM_ERROR_INTERNAL;
}
SCOPE_GUARD ({
isScanning_ = false;
clearCachedDevice();
clearPendingResult();
os_semaphore_destroy(scanSemaphore_);

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy May 22, 2019

Member

I see the pattern of checking these semaphores to be not null in various BLE event handlers, just in case, let's change (here and in any other places that follow the same pattern) the destruction to first nullptr scanSemaphore_:

Suggested change
LOG(ERROR, "nrf_sd
auto sem = scanSemaphore_;
scanSemaphore_ = nullptr;
os_semaphore_destroy(sem);
scanSemaphore_ = nullptr;
});
@@ -1173,14 +1198,10 @@ int BleObject::Observer::startScanning(on_ble_scan_result_cb_t callback, void* c
int ret = sd_ble_gap_scan_start(&bleGapScanParams, &bleScanData_);
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
isScanning_ = true;
ret = SYSTEM_ERROR_NONE;
if (os_semaphore_take(scanSemaphore_, CONCURRENT_WAIT_FOREVER, false)) {
ret = SYSTEM_ERROR_TIMEOUT;
return SYSTEM_ERROR_TIMEOUT;
}
isScanning_ = false;
clearCachedDevice();
clearPendingResult();
return ret;
return SYSTEM_ERROR_NONE;
}

int BleObject::Observer::stopScanning() {
@@ -1532,9 +1553,7 @@ int BleObject::ConnectionsManager::connect(const hal_ble_addr_t* address) {
ret = sd_ble_gap_ppcp_get(&bleGapConnParams);
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
ret = sd_ble_gap_connect(&bleDevAddr, &bleGapScanParams, &bleGapConnParams, BLE_CONN_CFG_TAG);
if (ret != NRF_SUCCESS) {
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
isConnecting_ = true;
memcpy(&connectingAddr_, address, sizeof(hal_ble_addr_t));
ret = SYSTEM_ERROR_NONE;
@@ -1570,9 +1589,7 @@ int BleObject::ConnectionsManager::disconnect(hal_ble_conn_handle_t connHandle)
disconnectSemaphore_ = nullptr;
});
int ret = sd_ble_gap_disconnect(connHandle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (ret != NRF_SUCCESS) {
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
disconnectingHandle_ = connHandle;
ret = SYSTEM_ERROR_NONE;
if (os_semaphore_take(disconnectSemaphore_, CONNECTION_OPERATION_TIMEOUT_MS, false)) {
@@ -1606,9 +1623,7 @@ int BleObject::ConnectionsManager::updateConnectionParams(hal_ble_conn_handle_t
// For Central role, this will initiate the connection parameter update procedure.
// For Peripheral role, this will use the passed in parameters and send the request to central.
ret = sd_ble_gap_conn_param_update(connHandle, &bleGapConnParams);
if (ret != NRF_SUCCESS) {
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
connParamUpdateHandle_ = connHandle;
ret = SYSTEM_ERROR_NONE;
if (os_semaphore_take(connParamsUpdateSemaphore_, CONNECTION_OPERATION_TIMEOUT_MS, false)) {
@@ -2211,10 +2226,7 @@ ssize_t BleObject::GattServer::getValue(hal_ble_attr_handle_t attrHandle, uint8_
gattValue.offset = 0;
gattValue.p_value = buf;
int ret = sd_ble_gatts_value_get(BLE_CONN_HANDLE_INVALID, attrHandle, &gattValue);
if (ret != NRF_SUCCESS) {
LOG(ERROR, "sd_ble_gatts_value_get() failed: %u", (unsigned)ret);
return 0;
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
return gattValue.len;
} else {
return SYSTEM_ERROR_NOT_FOUND;
@@ -2388,6 +2400,7 @@ int BleObject::GattClient::discoverServices(hal_ble_conn_handle_t connHandle, co
return SYSTEM_ERROR_INTERNAL;
}
SCOPE_GUARD ({
resetDiscoveryState();
os_semaphore_destroy(discoverySemaphore_);
discoverySemaphore_ = nullptr;
});
@@ -2401,10 +2414,7 @@ int BleObject::GattClient::discoverServices(hal_ble_conn_handle_t connHandle, co
BleObject::toPlatformUUID(uuid, &svcUUID);
ret = sd_ble_gattc_primary_services_discover(connHandle, SERVICES_BASE_START_HANDLE, &svcUUID);
}
if (ret != NRF_SUCCESS) {
resetDiscoveryState();
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
isDiscovering_ = true;
ret = SYSTEM_ERROR_NONE;
if (os_semaphore_take(discoverySemaphore_, BLE_DICOVERY_PROCEDURE_TIMEOUT_MS, false)) {
@@ -2435,12 +2445,12 @@ int BleObject::GattClient::discoverServices(hal_ble_conn_handle_t connHandle, co
msg.context = context;
msg.hook = gattcEventProcessedHook;
msg.hookContext = this;
BleObject::getInstance().dispatcher()->enqueue(msg);
return BleObject::getInstance().dispatcher()->enqueue(msg);
} else {
LOG(ERROR, "Allocate memory for discovered services failed.");
return SYSTEM_ERROR_NO_MEMORY;
}
resetDiscoveryState();
return ret;
return SYSTEM_ERROR_NONE;
}

int BleObject::GattClient::discoverCharacteristics(hal_ble_conn_handle_t connHandle, const hal_ble_svc_t* service, on_ble_disc_char_cb_t callback, void* context) {
@@ -2456,6 +2466,7 @@ int BleObject::GattClient::discoverCharacteristics(hal_ble_conn_handle_t connHan
return SYSTEM_ERROR_INTERNAL;
}
SCOPE_GUARD ({
resetDiscoveryState();
os_semaphore_destroy(discoverySemaphore_);
discoverySemaphore_ = nullptr;
});
@@ -2466,27 +2477,18 @@ int BleObject::GattClient::discoverCharacteristics(hal_ble_conn_handle_t connHan
currDiscSvc_ = *service;
currDiscProcedure_ = DiscoveryProcedure::BLE_DISCOVERY_PROCEDURE_CHARACTERISTICS;
int ret = sd_ble_gattc_characteristics_discover(connHandle, &handleRange);
if (ret != NRF_SUCCESS) {
resetDiscoveryState();
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
isDiscovering_ = true;
ret = SYSTEM_ERROR_NONE;
os_semaphore_take(discoverySemaphore_, BLE_DICOVERY_PROCEDURE_TIMEOUT_MS, false);
// Now discover characteristic descriptors
if (!BleObject::getInstance().connMgr()->valid(connHandle)) {
LOG(ERROR, "Connection invalid.");
resetDiscoveryState();
return SYSTEM_ERROR_NOT_FOUND;
if (os_semaphore_take(discoverySemaphore_, BLE_DICOVERY_PROCEDURE_TIMEOUT_MS, false)) {
return SYSTEM_ERROR_TIMEOUT;
}
// Now discover characteristic descriptors
CHECK_TRUE(BleObject::getInstance().connMgr()->valid(connHandle), SYSTEM_ERROR_NOT_FOUND);
currDiscProcedure_ = DiscoveryProcedure::BLE_DISCOVERY_PROCEDURE_DESCRIPTORS;
if (sd_ble_gattc_descriptors_discover(currDiscConnHandle_, &handleRange) != NRF_SUCCESS) {
LOG(ERROR, "sd_ble_gattc_descriptors_discover() failed: %u", (unsigned)ret);
} else {
ret = SYSTEM_ERROR_NONE;
if (os_semaphore_take(discoverySemaphore_, BLE_DICOVERY_PROCEDURE_TIMEOUT_MS, false)) {
ret = SYSTEM_ERROR_TIMEOUT;
}
ret = sd_ble_gattc_descriptors_discover(currDiscConnHandle_, &handleRange);
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
if (os_semaphore_take(discoverySemaphore_, BLE_DICOVERY_PROCEDURE_TIMEOUT_MS, false)) {
return SYSTEM_ERROR_TIMEOUT;
}
hal_ble_char_t* characteristics = (hal_ble_char_t*)malloc(discCharacteristicCount_ * sizeof(hal_ble_char_t));
if (characteristics) {
@@ -2513,12 +2515,12 @@ int BleObject::GattClient::discoverCharacteristics(hal_ble_conn_handle_t connHan
msg.context = context;
msg.hook = gattcEventProcessedHook;
msg.hookContext = this;
BleObject::getInstance().dispatcher()->enqueue(msg);
return BleObject::getInstance().dispatcher()->enqueue(msg);
} else {
LOG(ERROR, "Allocate memory for discovered characteristics failed.");
return SYSTEM_ERROR_NO_MEMORY;
}
resetDiscoveryState();
return ret;
return SYSTEM_ERROR_NONE;
}

ssize_t BleObject::GattClient::writeAttribute(hal_ble_conn_handle_t connHandle, hal_ble_attr_handle_t attrHandle, const uint8_t* buf, size_t len, bool response) {
@@ -2550,10 +2552,7 @@ ssize_t BleObject::GattClient::writeAttribute(hal_ble_conn_handle_t connHandle,
writeParams.len = len;
writeParams.p_value = buf;
int ret = sd_ble_gattc_write(connHandle, &writeParams);
if (ret != NRF_SUCCESS) {
LOG(ERROR, "sd_ble_gattc_write() failed: %u", (unsigned)ret);
return nrf_system_error(ret);
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
if (os_semaphore_take(readWriteSemaphore_, BLE_READ_WRITE_PROCEDURE_TIMEOUT_MS, false)) {
return SYSTEM_ERROR_TIMEOUT;
}
@@ -2574,19 +2573,16 @@ ssize_t BleObject::GattClient::readAttribute(hal_ble_conn_handle_t connHandle, h
return SYSTEM_ERROR_INTERNAL;
}
SCOPE_GUARD ({
readBuf_ = nullptr;
readAttrHandle_ = BLE_INVALID_ATTR_HANDLE;
os_semaphore_destroy(readWriteSemaphore_);
readWriteSemaphore_ = nullptr;
});
readAttrHandle_ = attrHandle;
readBuf_ = buf;
readLen_ = std::min(len, (size_t)BLE_ATTR_VALUE_PACKET_SIZE(BleObject::getInstance().connMgr()->getAttMtu(connHandle)));
int ret = sd_ble_gattc_read(connHandle, attrHandle, 0);
if (ret != NRF_SUCCESS) {
readBuf_ = nullptr;
readAttrHandle_ = BLE_INVALID_ATTR_HANDLE;
LOG(ERROR, "sd_ble_gattc_read() failed: %u", (unsigned)ret);
return nrf_system_error(ret);
}
CHECK_NRF_RETURN(ret, nrf_system_error(ret));
if (os_semaphore_take(readWriteSemaphore_, BLE_READ_WRITE_PROCEDURE_TIMEOUT_MS, false)) {
return SYSTEM_ERROR_TIMEOUT;
}
@@ -2600,6 +2596,22 @@ void BleObject::GattClient::resetDiscoveryState() {
currDiscProcedure_ = DiscoveryProcedure::BLE_DISCOVERY_PROCEDURE_IDLE;
discServiceCount_ = 0;
discCharacteristicCount_ = 0;
DiscoveredService* discService = nullptr;
DiscoveredCharacteristic* discChar = nullptr;
do {
discService = discServicesList_.popFront();
if (discService) {
DiscoveredService* curr = discService;
gattcDiscPool_.free(curr);
}
} while (discService);
do {
discChar = discCharacteristicsList_.popFront();
if (discChar) {
DiscoveredCharacteristic* curr = discChar;
gattcDiscPool_.free(curr);
}
} while (discChar);
}

int BleObject::GattClient::addDiscoveredService(const hal_ble_svc_t& service) {
@@ -2735,9 +2747,11 @@ void BleObject::GattClient::processGattClientEvents(const ble_evt_t* event, void
return;
}
LOG(ERROR, "sd_ble_gattc_primary_services_discover() failed: %u", (unsigned)ret);
// Falls down to read 128-bits UUID if needed.
}
} else {
LOG(ERROR, "BLE service discovery failed: %d, handle: %d.", event->evt.gattc_evt.gatt_status, event->evt.gattc_evt.error_handle);
// Falls down to read 128-bits UUID if needed.
}
if (gattc->readServiceUUID128IfNeeded()) {
return;
@@ -2779,9 +2793,11 @@ void BleObject::GattClient::processGattClientEvents(const ble_evt_t* event, void
return;
}
LOG(ERROR, "sd_ble_gattc_characteristics_discover() failed: %u", (unsigned)ret);
// Falls down to read 128-bits UUID if needed.
}
} else {
LOG(ERROR, "BLE characteristic discovery failed: %d, handle: %d.", event->evt.gattc_evt.gatt_status, event->evt.gattc_evt.error_handle);
// Falls down to read 128-bits UUID if needed.
}
if (gattc->readCharacteristicUUID128IfNeeded()) {
return;
@@ -2829,6 +2845,7 @@ void BleObject::GattClient::processGattClientEvents(const ble_evt_t* event, void
return;
}
LOG(ERROR, "sd_ble_gattc_descriptors_discover() failed: %u", (unsigned)ret);
// Falls down to finish the discovery procedure.
}
}
if (gattc->discoverySemaphore_) {
@@ -2902,6 +2919,7 @@ void BleObject::GattClient::processGattClientEvents(const ble_evt_t* event, void
int ret = sd_ble_gattc_hv_confirm(event->evt.gattc_evt.conn_handle, hvx.handle);
if (ret != NRF_SUCCESS) {
LOG(ERROR, "sd_ble_gattc_hv_confirm() failed: %u", (unsigned)ret);
return;
}
}
uint8_t* data = (uint8_t*)gattc->gattcDataRecPool_.alloc(hvx.len);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.