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

Skip to content

Map to vector #37

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 6 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
79 changes: 74 additions & 5 deletions src/NimBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,17 @@ NimBLEClient::~NimBLEClient() {
void NimBLEClient::clearServices() {
NIMBLE_LOGD(LOG_TAG, ">> clearServices");
// Delete all the services.
/*
for (auto &myPair : m_servicesMap) {
delete myPair.second;
}
m_servicesMap.clear();
*/
for(auto &it: m_servicesVector) {
delete it;
}
m_servicesVector.clear();

m_haveServices = false;
NIMBLE_LOGD(LOG_TAG, "<< clearServices");
} // clearServices
Expand Down Expand Up @@ -368,24 +375,38 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
if (!m_haveServices) {
return nullptr;
}
/*
std::string uuidStr = uuid.toString();
for (auto &myPair : m_servicesMap) {
if (myPair.first == uuidStr) {
NIMBLE_LOGD(LOG_TAG, "<< getService: found the service with uuid: %s", uuid.toString().c_str());
return myPair.second;
}
}
}
*/
for(auto &it: m_servicesVector) {
if(it->getUUID() == uuid) {
NIMBLE_LOGD(LOG_TAG, "<< getService: found the service with uuid: %s", uuid.toString().c_str());
return it;
}
}

NIMBLE_LOGD(LOG_TAG, "<< getService: not found");
return nullptr;
} // getService


/**
* @Get a pointer to the map of found services.
*/
* @Get a pointer to the vector of found services.
*/
/*
std::map<std::string, NimBLERemoteService*>* NimBLEClient::getServices() {
return &m_servicesMap;
}
*/
std::vector<NimBLERemoteService*>* NimBLEClient::getServices() {
return &m_servicesVector;
}


/**
Expand Down Expand Up @@ -425,13 +446,21 @@ bool NimBLEClient::retrieveServices() {
// If sucessful, remember that we now have services.
m_haveServices = (m_semaphoreSearchCmplEvt.wait("retrieveServices") == 0);
if(m_haveServices){
/*
for (auto &myPair : m_servicesMap) {
if(!m_isConnected || !myPair.second->retrieveCharacteristics()) {
NIMBLE_LOGE(LOG_TAG, "Disconnected, could not retrieve characteristics -aborting");
return false;
}
}

*/
for (auto &it: m_servicesVector) {
if(!m_isConnected || !it->retrieveCharacteristics()) {
NIMBLE_LOGE(LOG_TAG, "Disconnected, could not retrieve characteristics -aborting");
return false;
}
}

NIMBLE_LOGD(LOG_TAG, "<< retrieveServices");
return true;
}
Expand Down Expand Up @@ -463,9 +492,12 @@ int NimBLEClient::serviceDiscoveredCB(

switch (error->status) {
case 0: {
// Found a service - add it to the map
// Found a service - add it to the vector
NimBLERemoteService* pRemoteService = new NimBLERemoteService(peer, service);
/*
peer->m_servicesMap.insert(std::pair<std::string, NimBLERemoteService*>(pRemoteService->getUUID().toString(), pRemoteService));
*/
peer->m_servicesVector.push_back(pRemoteService);
break;
}
case BLE_HS_EDONE:{
Expand Down Expand Up @@ -662,6 +694,7 @@ uint16_t NimBLEClient::getMTU() {
if(!client->m_haveServices)
return 0;

/*
for(auto &sPair : client->m_servicesMap){
// Dont waste cycles searching services without this handle in their range
if(sPair.second->getEndHandle() < event->notify_rx.attr_handle) {
Expand All @@ -681,6 +714,37 @@ uint16_t NimBLEClient::getMTU() {
break;
}
}
*/
for(auto &it: client->m_servicesVector) {
// Dont waste cycles searching services without this handle in their range
if(it->getEndHandle() < event->notify_rx.attr_handle) {
continue;
}
// auto cMap = it->getCharacteristicsByHandle();
auto cVector = it->getCharacteristics();
NIMBLE_LOGD(LOG_TAG, "checking service %s for handle: %d", it->getUUID().toString().c_str(),event->notify_rx.attr_handle);
// auto characteristic = cMap->find(event->notify_rx.attr_handle);
auto characteristic = cVector->cbegin();
// for(auto &characteristic: *cVector) {
for(; characteristic != cVector->cend(); ++characteristic) {
if((*characteristic)->m_handle == event->notify_rx.attr_handle) break;
}
// if(characteristic != cMap->end()) {
if(characteristic != cVector->cend()) {
// NIMBLE_LOGD(LOG_TAG, "Got Notification for characteristic %s", characteristic->second->toString().c_str());
NIMBLE_LOGD(LOG_TAG, "Got Notification for characteristic %s", (*characteristic)->toString().c_str());

// if (characteristic->second->m_notifyCallback != nullptr) {
if ((*characteristic)->m_notifyCallback != nullptr) {
// NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s", characteristic->second->toString().c_str());
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s", (*characteristic)->toString().c_str());
// characteristic->second->m_notifyCallback(characteristic->second, event->notify_rx.om->om_data, event->notify_rx.om->om_len, !event->notify_rx.indication);
(*characteristic)->m_notifyCallback(*characteristic, event->notify_rx.om->om_data, event->notify_rx.om->om_len, !event->notify_rx.indication);
}

break;
}
}

return 0;
} // BLE_GAP_EVENT_NOTIFY_RX
Expand Down Expand Up @@ -851,9 +915,14 @@ void NimBLEClient::setClientCallbacks(NimBLEClientCallbacks* pClientCallbacks, b
std::string NimBLEClient::toString() {
std::string res = "peer address: " + m_peerAddress.toString();
res += "\nServices:\n";
/*
for (auto &myPair : m_servicesMap) {
res += myPair.second->toString() + "\n";
}
*/
for(auto &it: m_servicesVector) {
res += it->toString() + "\n";
}

return res;
} // toString
Expand Down
9 changes: 8 additions & 1 deletion src/NimBLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#include "NimBLEAdvertisedDevice.h"
#include "NimBLERemoteService.h"

#include <map>
//#include <map>
#include <vector>
#include <string>

class NimBLERemoteService;
Expand All @@ -41,7 +42,10 @@ class NimBLEClient {
int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); // Disconnect from the remote BLE Server
NimBLEAddress getPeerAddress(); // Get the address of the remote BLE Server
int getRssi(); // Get the RSSI of the remote BLE Server
/*
std::map<std::string, NimBLERemoteService*>* getServices(); // Get a map of the services offered by the remote BLE Server
*/
std::vector<NimBLERemoteService*>* getServices(); // Get a vector of the services offered by the remote BLE Server
NimBLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server.
NimBLERemoteService* getService(const NimBLEUUID &uuid); // Get a reference to a specified service offered by the remote BLE server.
std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); // Get the value of a given characteristic at a given service.
Expand Down Expand Up @@ -87,7 +91,10 @@ class NimBLEClient {
FreeRTOS::Semaphore m_semaphoreSearchCmplEvt = FreeRTOS::Semaphore("SearchCmplEvt");
FreeRTOS::Semaphore m_semeaphoreSecEvt = FreeRTOS::Semaphore("Security");

/*
std::map<std::string, NimBLERemoteService*> m_servicesMap;
*/
std::vector<NimBLERemoteService*> m_servicesVector;

private:
friend class NimBLEClientCallbacks;
Expand Down
39 changes: 34 additions & 5 deletions src/NimBLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(uint16_t conn_handle,
case 0: {
// Found a descriptor - add it to the map
NimBLERemoteDescriptor* pNewRemoteDescriptor = new NimBLERemoteDescriptor(characteristic, dsc);
characteristic->m_descriptorMap.insert(std::pair<std::string, NimBLERemoteDescriptor*>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor));

// characteristic->m_descriptorMap.insert(std::pair<std::string, NimBLERemoteDescriptor*>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor));
characteristic->m_descriptorVector.push_back(pNewRemoteDescriptor);
break;
}
case BLE_HS_EDONE:{
Expand Down Expand Up @@ -212,15 +212,20 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(uint16_t endHdl) {
}

return true;
NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): Found %d descriptors.", m_descriptorMap.size());
// NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): Found %d descriptors.", m_descriptorMap.size());
NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): Found %d descriptors.", m_descriptorVector.size());
} // getDescriptors


/**
* @brief Retrieve the map of descriptors keyed by UUID.
*/
*/
std::vector<NimBLERemoteDescriptor*>* NimBLERemoteCharacteristic::getDescriptors() {
return &m_descriptorVector;
/*
std::map<std::string, NimBLERemoteDescriptor*>* NimBLERemoteCharacteristic::getDescriptors() {
return &m_descriptorMap;
*/
} // getDescriptors


Expand Down Expand Up @@ -248,13 +253,21 @@ uint16_t NimBLERemoteCharacteristic::getDefHandle() {
*/
NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUUID &uuid) {
NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
/*
std::string v = uuid.toString();
for (auto &myPair : m_descriptorMap) {
if (myPair.first == v) {
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: found");
return myPair.second;
}
}
*/
for(auto &it: m_descriptorVector) {
if(it->getUUID() == uuid) {
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: found");
return it;
}
}
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: Not found");
return nullptr;
} // getDescriptor
Expand Down Expand Up @@ -451,12 +464,19 @@ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallbac
* @return N/A.
*/
void NimBLERemoteCharacteristic::removeDescriptors() {
/*
// Iterate through all the descriptors releasing their storage and erasing them from the map.
for (auto &myPair : m_descriptorMap) {
m_descriptorMap.erase(myPair.first);
delete myPair.second;
}
m_descriptorMap.clear(); // Technically not neeeded, but just to be sure.
*/
// Iterate through all the descriptors releasing their storage and erasing them from the vector.
for(auto &it: m_descriptorVector) {
delete it;
}
m_descriptorVector.clear();
} // removeCharacteristics


Expand All @@ -477,10 +497,14 @@ std::string NimBLERemoteCharacteristic::toString() {
res += " 0x";
snprintf(val, sizeof(val), "%02x", m_charProp);
res += val;

/*
for (auto &myPair : m_descriptorMap) {
res += "\n" + myPair.second->toString();
}
*/
for(auto &it: m_descriptorVector) {
res += "\n" + it->toString();
}

return res;
} // toString
Expand Down Expand Up @@ -645,9 +669,14 @@ size_t NimBLERemoteCharacteristic::getDataLength() {


void NimBLERemoteCharacteristic::releaseSemaphores() {
/*
for (auto &dPair : m_descriptorMap) {
dPair.second->releaseSemaphores();
}
*/
for (auto &it: m_descriptorVector) {
it->releaseSemaphores();
}
m_semaphoreWriteCharEvt.give(1);
m_semaphoreGetDescEvt.give(1);
m_semaphoreReadCharEvt.give(1);
Expand Down
8 changes: 4 additions & 4 deletions src/NimBLERemoteCharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "NimBLERemoteDescriptor.h"

//#include <string>
#include <map>
#include <vector>

class NimBLERemoteService;
class NimBLERemoteDescriptor;
Expand All @@ -49,7 +49,7 @@ class NimBLERemoteCharacteristic {
bool canWrite();
bool canWriteNoResponse();
NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid);
std::map<std::string, NimBLERemoteDescriptor*>* getDescriptors();
std::vector<NimBLERemoteDescriptor*>* getDescriptors();
uint16_t getHandle();
uint16_t getDefHandle();
NimBLEUUID getUUID();
Expand Down Expand Up @@ -98,8 +98,8 @@ class NimBLERemoteCharacteristic {
size_t m_dataLen;
notify_callback m_notifyCallback;

// We maintain a map of descriptors owned by this characteristic keyed by a string representation of the UUID.
std::map<std::string, NimBLERemoteDescriptor*> m_descriptorMap;
// We maintain a vector of descriptors owned by this characteristic.
std::vector<NimBLERemoteDescriptor*> m_descriptorVector;
}; // BLERemoteCharacteristic

#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
Expand Down
Loading