#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <sstream>
#include <iomanip>
#include "BLEService.h"
BLEService* BLEServiceMap::getByUUID(const char* uuid) {
return getByUUID(BLEUUID(uuid));
}
BLEService* BLEServiceMap::getByUUID(BLEUUID uuid) {
for (auto &myPair : m_uuidMap) {
if (myPair.first->getUUID().equals(uuid)) {
return myPair.first;
}
}
return nullptr;
}
BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
return m_handleMap.at(handle);
}
void BLEServiceMap::setByUUID(BLEUUID uuid,
BLEService *service) {
m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString()));
}
void BLEServiceMap::setByHandle(uint16_t handle,
BLEService* service) {
m_handleMap.insert(std::pair<uint16_t, BLEService *>(handle, service));
}
std::string BLEServiceMap::toString() {
std::stringstream stringStream;
stringStream << std::hex << std::setfill('0');
for (auto &myPair: m_handleMap) {
stringStream << "handle: 0x" << std::setw(2) << myPair.first << ", uuid: " + myPair.second->getUUID().toString() << "\n";
}
return stringStream.str();
}
void BLEServiceMap::handleGATTServerEvent(
esp_gatts_cb_event_t event,
esp_gatt_if_t gatts_if,
esp_ble_gatts_cb_param_t *param) {
for (auto &myPair : m_uuidMap) {
myPair.first->handleGATTServerEvent(event, gatts_if, param);
}
}
BLEService* BLEServiceMap::getFirst() {
m_iterator = m_uuidMap.begin();
if (m_iterator == m_uuidMap.end()) {
return nullptr;
}
BLEService* pRet = m_iterator->first;
m_iterator++;
return pRet;
}
BLEService* BLEServiceMap::getNext() {
if (m_iterator == m_uuidMap.end()) {
return nullptr;
}
BLEService* pRet = m_iterator->first;
m_iterator++;
return pRet;
}
#endif