#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <sstream>
#include <iomanip>
#include "BLECharacteristic.h"
#include "BLEDescriptor.h"
#include <esp_gatts_api.h>
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
BLEDescriptor* BLEDescriptorMap::getByUUID(const char* uuid) {
return getByUUID(BLEUUID(uuid));
}
BLEDescriptor* BLEDescriptorMap::getByUUID(BLEUUID uuid) {
for (auto &myPair : m_uuidMap) {
if (myPair.second->getUUID().equals(uuid)) {
return myPair.second;
}
}
return nullptr;
}
BLEDescriptor* BLEDescriptorMap::getByHandle(uint16_t handle) {
return m_handleMap.at(handle);
}
void BLEDescriptorMap::setByUUID(const char* uuid, BLEDescriptor *pDescriptor){
m_uuidMap.insert(std::pair<std::string, BLEDescriptor *>(uuid, pDescriptor));
}
void BLEDescriptorMap::setByUUID(BLEUUID uuid, BLEDescriptor *pDescriptor) {
m_uuidMap.insert(std::pair<std::string, BLEDescriptor *>(uuid.toString(), pDescriptor));
}
void BLEDescriptorMap::setByHandle(uint16_t handle,
BLEDescriptor *pDescriptor) {
m_handleMap.insert(std::pair<uint16_t, BLEDescriptor *>(handle, pDescriptor));
}
std::string BLEDescriptorMap::toString() {
std::stringstream stringStream;
stringStream << std::hex << std::setfill('0');
int count=0;
for (auto &myPair: m_uuidMap) {
if (count > 0) {
stringStream << "\n";
}
count++;
stringStream << "handle: 0x" << std::setw(2) << myPair.second->getHandle() << ", uuid: " + myPair.second->getUUID().toString();
}
return stringStream.str();
}
void BLEDescriptorMap::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.second->handleGATTServerEvent(event, gatts_if, param);
}
}
BLEDescriptor* BLEDescriptorMap::getFirst() {
m_iterator = m_uuidMap.begin();
if (m_iterator == m_uuidMap.end()) {
return nullptr;
}
BLEDescriptor *pRet = m_iterator->second;
m_iterator++;
return pRet;
}
BLEDescriptor* BLEDescriptorMap::getNext() {
if (m_iterator == m_uuidMap.end()) {
return nullptr;
}
BLEDescriptor *pRet = m_iterator->second;
m_iterator++;
return pRet;
}
#endif