Skip to content

Commit

Permalink
Merge pull request #75 from iranl/esp-nimble-master-compat
Browse files Browse the repository at this point in the history
Compatibility with latest esp-nimble-cpp
  • Loading branch information
technyon authored Aug 10, 2024
2 parents 16467ec + 43426db commit fcca247
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/NukiBle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ bool NukiBle::connectBle(const BLEAddress bleAddress, bool pairing) {
uint8_t connectRetry = 0;

while (connectRetry < connectRetries) {
if(NimBLEDevice::getClientListSize()) {
#ifdef NUKI_USE_LATEST_NIMBLE
if(NimBLEDevice::getCreatedClientCount())
#else
if(NimBLEDevice::getClientListSize())
#endif
{
pClient = NimBLEDevice::getClientByPeerAddress(bleAddress);
if(pClient){
#ifdef CONFIG_IDF_TARGET_ESP32C6
Expand Down Expand Up @@ -290,7 +295,12 @@ bool NukiBle::connectBle(const BLEAddress bleAddress, bool pairing) {
}

if(!pClient) {
if(NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) {
#ifdef NUKI_USE_LATEST_NIMBLE
if(NimBLEDevice::getCreatedClientCount() >= NIMBLE_MAX_CONNECTIONS)
#else
if(NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS)
#endif
{
#ifdef DEBUG_NUKI_CONNECT
log_d("[%s] Max clients reached - no more connections available", deviceName.c_str());
#endif
Expand Down Expand Up @@ -399,7 +409,12 @@ void NukiBle::updateConnectionState() {
#endif
pClient = nullptr;

if(NimBLEDevice::getClientListSize()) {
#ifdef NUKI_USE_LATEST_NIMBLE
if(NimBLEDevice::getCreatedClientCount())
#else
if(NimBLEDevice::getClientListSize())
#endif
{
pClient = NimBLEDevice::getClientByPeerAddress(bleAddress);
}

Expand Down Expand Up @@ -825,13 +840,21 @@ void NukiBle::saveCredentials() {
unsigned char currentBleAddress[6];
unsigned char storedBleAddress[6];
uint16_t defaultPincode = 0;
#ifdef NUKI_USE_LATEST_NIMBLE
currentBleAddress[0] = bleAddress.getVal()[5];
currentBleAddress[1] = bleAddress.getVal()[4];
currentBleAddress[2] = bleAddress.getVal()[3];
currentBleAddress[3] = bleAddress.getVal()[2];
currentBleAddress[4] = bleAddress.getVal()[1];
currentBleAddress[5] = bleAddress.getVal()[0];
#else
currentBleAddress[0] = bleAddress.getNative()[5];
currentBleAddress[1] = bleAddress.getNative()[4];
currentBleAddress[2] = bleAddress.getNative()[3];
currentBleAddress[3] = bleAddress.getNative()[2];
currentBleAddress[4] = bleAddress.getNative()[1];
currentBleAddress[5] = bleAddress.getNative()[0];

#endif
preferences.getBytes(BLE_ADDRESS_STORE_NAME, storedBleAddress, 6);

if (compareCharArray(currentBleAddress, storedBleAddress, 6)) {
Expand Down Expand Up @@ -1205,7 +1228,11 @@ bool NukiBle::registerOnGdioChar() {
if (pGdioCharacteristic != nullptr) {
if (pGdioCharacteristic->canIndicate()) {
using namespace std::placeholders;
#ifdef NUKI_USE_LATEST_NIMBLE
NimBLERemoteCharacteristic::notify_callback callback = std::bind(&NukiBle::notifyCallback, this, _1, _2, _3, _4);
#else
notify_callback callback = std::bind(&NukiBle::notifyCallback, this, _1, _2, _3, _4);
#endif
if(!pGdioCharacteristic->subscribe(false, callback, true)) {
log_w("Unable to subscribe to GDIO characteristic");
pClient->disconnect();
Expand Down Expand Up @@ -1245,7 +1272,11 @@ bool NukiBle::registerOnUsdioChar() {
if (pUsdioCharacteristic != nullptr) {
if (pUsdioCharacteristic->canIndicate()) {
using namespace std::placeholders;
#ifdef NUKI_USE_LATEST_NIMBLE
NimBLERemoteCharacteristic::notify_callback callback = std::bind(&NukiBle::notifyCallback, this, _1, _2, _3, _4);
#else
notify_callback callback = std::bind(&NukiBle::notifyCallback, this, _1, _2, _3, _4);
#endif
if(!pUsdioCharacteristic->subscribe(false, callback, true)) {
log_w("Unable to subscribe to USDIO characteristic");
pClient->disconnect();
Expand Down
1 change: 1 addition & 0 deletions src/NukiBle.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <BleInterfaces.h>
#include <atomic>
#include <string>
#include <list>
#include "sodium/crypto_secretbox.h"

#define GENERAL_TIMEOUT 3000
Expand Down

0 comments on commit fcca247

Please sign in to comment.