Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Create semaphores dynamically #53

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,29 @@ A fork of the NimBLE stack restructured for compilation in the Ardruino IDE with

Why? Because the Bluedroid library is too bulky.

Initial client code testing has resulted in code size reduction of ~115k and reduced ram consumption of ~37k.

Server code testing results from @beegee-toyo [from the project here](https://github.com/beegee-tokyo/ESP32WiFiBLE-NimBLE):
Initial testing has resulted in code size reduction of ~50% and reduced ram consumption of ~100k.

## BLE_client example comparison (Debug):
#### Arduino BLE Library
Sketch uses 1214909 bytes (57%) of program storage space.
Memory after connection: Free Heap: 171308

#### NimBLE-Arduino library
Sketch uses 622076 bytes (29%) of program storage space.
Memory after connection: Free Heap: 270408

## BLE_notify (server) example comparison (Debug):
#### Arduino BLE Library
Sketch uses 1208409 bytes (57%) of program storage space.
Memory after connection: Free Heap: 173300

#### NimBLE-Arduino library
Sketch uses 609044 bytes (29%) of program storage space.
Memory after connection: Free Heap: 269448


## Server code testing results from @beegee-toyo [from the project here](https://github.com/beegee-tokyo/ESP32WiFiBLE-NimBLE):

### Memory usage (compilation output)
#### Arduino BLE library
```log
Expand All @@ -35,9 +53,8 @@ Flash: [======= ] 69.5% (used 911378 bytes from 1310720 bytes)
#### Arduino BLE library
**`Internal Total heap 259104, internal Free Heap 91660`**
#### NimBLE-Arduino library
**`Internal Total heap 290288, internal Free Heap 182344`**


**`Internal Total heap 290288, internal Free Heap 182344`**

# Installation:

Download as .zip and extract to Arduino/libraries folder, or in Arduino IDE from Sketch menu -> Include library -> Add .Zip library.
Expand Down
1 change: 0 additions & 1 deletion src/NimBLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
/**************************/

#include "NimBLEUUID.h"
#include "FreeRTOS.h"

#include <vector>

Expand Down
2 changes: 1 addition & 1 deletion src/NimBLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void NimBLECharacteristic::notify(bool is_notification) {
om = ble_hs_mbuf_from_flat(data, length);

if(!is_notification) {
m_semaphoreConfEvt.take("indicate");
m_semaphoreConfEvt.take();
rc = ble_gattc_indicate_custom((*it).first, m_handle, om);
if(rc != 0){
m_semaphoreConfEvt.give();
Expand Down
6 changes: 2 additions & 4 deletions src/NimBLECharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef enum {
#include "NimBLEDescriptor.h"
#include "NimBLEUUID.h"
#include "NimBLEValue.h"
#include "FreeRTOS.h"
#include "NimBLESemaphore.h"

#include <string>
#include <map>
Expand Down Expand Up @@ -136,7 +136,6 @@ class NimBLECharacteristic {
//////////////////////////////////////////////////////

private:

friend class NimBLEServer;
friend class NimBLEService;
// friend class NimBLEDescriptor;
Expand All @@ -155,6 +154,7 @@ class NimBLECharacteristic {
NimBLECharacteristicCallbacks* m_pCallbacks;
NimBLEService* m_pService;
NimBLEValue m_value;
NimBLESemaphore m_semaphoreConfEvt = NimBLESemaphore("ConfEvt");
// uint16_t m_permissions;

void addDescriptor(NimBLEDescriptor* pDescriptor);
Expand All @@ -163,8 +163,6 @@ class NimBLECharacteristic {
void setSubscribe(struct ble_gap_event *event);
static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);

FreeRTOS::Semaphore m_semaphoreConfEvt = FreeRTOS::Semaphore("ConfEvt");
}; // NimBLECharacteristic


Expand Down
53 changes: 28 additions & 25 deletions src/NimBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ NimBLEClient::NimBLEClient()
m_conn_id = BLE_HS_CONN_HANDLE_NONE;
m_haveServices = false;
m_isConnected = false;
m_waitingToConnect = false;
m_deleteCallbacks = true;
m_pSemaphore = nullptr;
m_connectTimeout = 30000;

m_pConnParams.scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default)
Expand Down Expand Up @@ -144,7 +147,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
memcpy(&peerAddrt.val, address.getNative(),6);
peerAddrt.type = type;

m_semaphoreOpenEvt.take("connect");
NIMBLE_SEMAPHORE_TAKE(m_pSemaphore, "Connect")

/** Try to connect the the advertiser. Allow 30 seconds (30000 ms) for
* timeout (default value of m_connectTimeout).
Expand All @@ -165,15 +168,15 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
m_peerAddress.toString().c_str(),
rc, NimBLEUtils::returnCodeToString(rc));

m_semaphoreOpenEvt.give();
NIMBLE_SEMAPHORE_DELETE(m_pSemaphore)
m_waitingToConnect = false;
return false;
}

m_waitingToConnect = true;

rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.

rc = NIMBLE_SEMAPHORE_WAIT(m_pSemaphore) //m_pSemaphore->wait(); // Wait for the connection to complete.
NIMBLE_SEMAPHORE_DELETE(m_pSemaphore)
if(rc != 0){
return false;
}
Expand Down Expand Up @@ -208,16 +211,16 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
* @return True on success.
*/
bool NimBLEClient::secureConnection() {

m_semeaphoreSecEvt.take("secureConnection");
NIMBLE_SEMAPHORE_TAKE(m_pSemaphore, "Secure connection")

int rc = NimBLEDevice::startSecurity(m_conn_id);
if(rc != 0){
m_semeaphoreSecEvt.give();
NIMBLE_SEMAPHORE_DELETE(m_pSemaphore)
return false;
}

rc = m_semeaphoreSecEvt.wait("secureConnection");
rc = NIMBLE_SEMAPHORE_WAIT(m_pSemaphore)
NIMBLE_SEMAPHORE_DELETE(m_pSemaphore)
if(rc != 0){
return false;
}
Expand Down Expand Up @@ -412,21 +415,24 @@ bool NimBLEClient::retrieveServices() {
return false;
}

m_semaphoreSearchCmplEvt.take("retrieveServices");
NIMBLE_SEMAPHORE_TAKE(m_pSemaphore, "Retrieve Services")

int rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, this);

if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_svcs: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
m_haveServices = false;
m_semaphoreSearchCmplEvt.give();
NIMBLE_SEMAPHORE_DELETE(m_pSemaphore)
return false;
}

// wait until we have all the services
// If sucessful, remember that we now have services.
m_haveServices = (m_semaphoreSearchCmplEvt.wait("retrieveServices") == 0);
if(m_haveServices){
rc = NIMBLE_SEMAPHORE_WAIT(m_pSemaphore);
NIMBLE_SEMAPHORE_DELETE(m_pSemaphore)

if(rc == 0) {
m_haveServices = true;
for (auto &it: m_servicesVector) {
if(!m_isConnected || !it->retrieveCharacteristics()) {
NIMBLE_LOGE(LOG_TAG, "Disconnected, could not retrieve characteristics -aborting");
Expand Down Expand Up @@ -472,9 +478,7 @@ int NimBLEClient::serviceDiscoveredCB(
}
case BLE_HS_EDONE:{
// All services discovered; start discovering characteristics.

//NIMBLE_LOGD(LOG_TAG,"Giving search semaphore - completed");
peer->m_semaphoreSearchCmplEvt.give(0);
NIMBLE_SEMAPHORE_GIVE(peer->m_pSemaphore, 0)
rc = 0;
break;
}
Expand All @@ -486,7 +490,7 @@ int NimBLEClient::serviceDiscoveredCB(

if (rc != 0) {
// pass non-zero to semaphore on error to indicate an error finding services
peer->m_semaphoreSearchCmplEvt.give(1);
NIMBLE_SEMAPHORE_GIVE(peer->m_pSemaphore, 1)
}
NIMBLE_LOGD(LOG_TAG,"<< Service Discovered. status: %d", rc);
return rc;
Expand Down Expand Up @@ -603,9 +607,7 @@ uint16_t NimBLEClient::getMTU() {
//client->m_conn_id = BLE_HS_CONN_HANDLE_NONE;

// Indicate a non-success return value to any semaphores waiting
client->m_semaphoreOpenEvt.give(1);
client->m_semaphoreSearchCmplEvt.give(1);
client->m_semeaphoreSecEvt.give(1);
NIMBLE_SEMAPHORE_GIVE(client->m_pSemaphore, 1)

client->m_pClientCallbacks->onDisconnect(client);

Expand Down Expand Up @@ -644,15 +646,16 @@ uint16_t NimBLEClient::getMTU() {
NIMBLE_LOGE(LOG_TAG, "ble_gattc_exchange_mtu: rc=%d %s",rc,
NimBLEUtils::returnCodeToString(rc));
// if error getting mtu indicate a connection error.
client->m_semaphoreOpenEvt.give(rc);
NIMBLE_SEMAPHORE_GIVE(client->m_pSemaphore, rc)
}
} else {
// Connection attempt failed
NIMBLE_LOGE(LOG_TAG, "Error: Connection failed; status=%d %s",
event->connect.status,
NimBLEUtils::returnCodeToString(event->connect.status));
NIMBLE_SEMAPHORE_GIVE(client->m_pSemaphore, event->connect.status)
}
client->m_semaphoreOpenEvt.give(event->connect.status);

return 0;
} // BLE_GAP_EVENT_CONNECT

Expand Down Expand Up @@ -748,19 +751,19 @@ uint16_t NimBLEClient::getMTU() {
}
}

client->m_semeaphoreSecEvt.give(event->enc_change.status);
NIMBLE_SEMAPHORE_GIVE(client->m_pSemaphore, event->enc_change.status)
return 0;
} //BLE_GAP_EVENT_ENC_CHANGE

case BLE_GAP_EVENT_MTU: {
if(client->m_conn_id != event->mtu.conn_handle){
return 0; //BLE_HS_ENOTCONN BLE_ATT_ERR_INVALID_HANDLE
return 0;
}
NIMBLE_LOGI(LOG_TAG, "mtu update event; conn_handle=%d mtu=%d",
event->mtu.conn_handle,
event->mtu.value);
client->m_semaphoreOpenEvt.give(0);
//client->m_mtu = event->mtu.value;

NIMBLE_SEMAPHORE_GIVE(client->m_pSemaphore, 0)
return 0;
} // BLE_GAP_EVENT_MTU

Expand Down
26 changes: 11 additions & 15 deletions src/NimBLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,29 @@ class NimBLEClient {
void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout);


private:
NimBLEClient();
~NimBLEClient();
friend class NimBLEDevice;
friend class NimBLERemoteService;
friend class NimBLERemoteCharacteristic;
friend class NimBLERemoteDescriptor;

static int handleGapEvent(struct ble_gap_event *event, void *arg);
static int serviceDiscoveredCB(uint16_t conn_handle, const struct ble_gatt_error *error, const struct ble_gatt_svc *service, void *arg);
void clearServices(); // Clear any existing services.
bool retrieveServices(); //Retrieve services from the server
// void onHostReset();

NimBLEAddress m_peerAddress = NimBLEAddress(""); // The BD address of the remote server.
uint16_t m_conn_id;
bool m_haveServices = false; // Have we previously obtain the set of services from the remote server.
bool m_isConnected = false; // Are we currently connected.
bool m_waitingToConnect =false;
bool m_deleteCallbacks = true;
int32_t m_connectTimeout;
//uint16_t m_mtu = 23;

NimBLEClientCallbacks* m_pClientCallbacks = nullptr;

FreeRTOS::Semaphore m_semaphoreOpenEvt = FreeRTOS::Semaphore("OpenEvt");
FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");
FreeRTOS::Semaphore m_semeaphoreSecEvt = FreeRTOS::Semaphore("Security");
NimBLEAddress m_peerAddress = NimBLEAddress("\0\0\0\0\0\0"); // The BD address of the remote server.
uint16_t m_conn_id;
bool m_haveServices; // Have we previously obtain the set of services from the remote server.
bool m_isConnected; // Are we currently connected.
bool m_waitingToConnect;
bool m_deleteCallbacks;
int32_t m_connectTimeout;
NimBLESemaphore* m_pSemaphore;
NimBLEClientCallbacks* m_pClientCallbacks;

std::vector<NimBLERemoteService*> m_servicesVector;

Expand Down
1 change: 0 additions & 1 deletion src/NimBLEDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include "NimBLECharacteristic.h"
#include "NimBLEUUID.h"
#include "FreeRTOS.h"

#include <string>

Expand Down
Loading