5502879a5b
This is very much still work in progress and much more will change before the final 2.0.0 Some APIs have changed. New libraries have been added. LittleFS included. Co-authored-by: Seon Rozenblum <seonr@3sprockets.com> Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com> Co-authored-by: geeksville <kevinh@geeksville.com> Co-authored-by: Mike Dunston <m_dunston@comcast.net> Co-authored-by: Unexpected Maker <seon@unexpectedmaker.com> Co-authored-by: Seon Rozenblum <seonr@3sprockets.com> Co-authored-by: microDev <70126934+microDev1@users.noreply.github.com> Co-authored-by: tobozo <tobozo@users.noreply.github.com> Co-authored-by: bobobo1618 <bobobo1618@users.noreply.github.com> Co-authored-by: lorol <lorolouis@gmail.com> Co-authored-by: geeksville <kevinh@geeksville.com> Co-authored-by: Limor "Ladyada" Fried <limor@ladyada.net> Co-authored-by: Sweety <switi.mhaiske@espressif.com> Co-authored-by: Loick MAHIEUX <loick111@gmail.com> Co-authored-by: Larry Bernstone <lbernstone@gmail.com> Co-authored-by: Valerii Koval <valeros@users.noreply.github.com> Co-authored-by: 快乐的我531 <2302004040@qq.com> Co-authored-by: chegewara <imperiaonline4@gmail.com> Co-authored-by: Clemens Kirchgatterer <clemens@1541.org> Co-authored-by: Aron Rubin <aronrubin@gmail.com> Co-authored-by: Pete Lewis <601236+lewispg228@users.noreply.github.com>
149 lines
3.9 KiB
C++
149 lines
3.9 KiB
C++
/*
|
|
* BLEDescriptorMap.cpp
|
|
*
|
|
* Created on: Jun 22, 2017
|
|
* Author: kolban
|
|
*/
|
|
#include "sdkconfig.h"
|
|
#if defined(CONFIG_BLUEDROID_ENABLED)
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
#include "BLECharacteristic.h"
|
|
#include "BLEDescriptor.h"
|
|
#include <esp_gatts_api.h> // ESP32 BLE
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
#include "esp32-hal-log.h"
|
|
#endif
|
|
|
|
/**
|
|
* @brief Return the descriptor by UUID.
|
|
* @param [in] UUID The UUID to look up the descriptor.
|
|
* @return The descriptor. If not present, then nullptr is returned.
|
|
*/
|
|
BLEDescriptor* BLEDescriptorMap::getByUUID(const char* uuid) {
|
|
return getByUUID(BLEUUID(uuid));
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Return the descriptor by UUID.
|
|
* @param [in] UUID The UUID to look up the descriptor.
|
|
* @return The descriptor. If not present, then nullptr is returned.
|
|
*/
|
|
BLEDescriptor* BLEDescriptorMap::getByUUID(BLEUUID uuid) {
|
|
for (auto &myPair : m_uuidMap) {
|
|
if (myPair.first->getUUID().equals(uuid)) {
|
|
return myPair.first;
|
|
}
|
|
}
|
|
//return m_uuidMap.at(uuid.toString());
|
|
return nullptr;
|
|
} // getByUUID
|
|
|
|
|
|
/**
|
|
* @brief Return the descriptor by handle.
|
|
* @param [in] handle The handle to look up the descriptor.
|
|
* @return The descriptor.
|
|
*/
|
|
BLEDescriptor* BLEDescriptorMap::getByHandle(uint16_t handle) {
|
|
return m_handleMap.at(handle);
|
|
} // getByHandle
|
|
|
|
|
|
/**
|
|
* @brief Set the descriptor by UUID.
|
|
* @param [in] uuid The uuid of the descriptor.
|
|
* @param [in] characteristic The descriptor to cache.
|
|
* @return N/A.
|
|
*/
|
|
void BLEDescriptorMap::setByUUID(const char* uuid, BLEDescriptor* pDescriptor){
|
|
m_uuidMap.insert(std::pair<BLEDescriptor*, std::string>(pDescriptor, uuid));
|
|
} // setByUUID
|
|
|
|
|
|
|
|
/**
|
|
* @brief Set the descriptor by UUID.
|
|
* @param [in] uuid The uuid of the descriptor.
|
|
* @param [in] characteristic The descriptor to cache.
|
|
* @return N/A.
|
|
*/
|
|
void BLEDescriptorMap::setByUUID(BLEUUID uuid, BLEDescriptor* pDescriptor) {
|
|
m_uuidMap.insert(std::pair<BLEDescriptor*, std::string>(pDescriptor, uuid.toString()));
|
|
} // setByUUID
|
|
|
|
|
|
/**
|
|
* @brief Set the descriptor by handle.
|
|
* @param [in] handle The handle of the descriptor.
|
|
* @param [in] descriptor The descriptor to cache.
|
|
* @return N/A.
|
|
*/
|
|
void BLEDescriptorMap::setByHandle(uint16_t handle, BLEDescriptor* pDescriptor) {
|
|
m_handleMap.insert(std::pair<uint16_t, BLEDescriptor*>(handle, pDescriptor));
|
|
} // setByHandle
|
|
|
|
|
|
/**
|
|
* @brief Return a string representation of the descriptor map.
|
|
* @return A string representation of the descriptor map.
|
|
*/
|
|
std::string BLEDescriptorMap::toString() {
|
|
std::string res;
|
|
char hex[5];
|
|
int count = 0;
|
|
for (auto &myPair : m_uuidMap) {
|
|
if (count > 0) {res += "\n";}
|
|
snprintf(hex, sizeof(hex), "%04x", myPair.first->getHandle());
|
|
count++;
|
|
res += "handle: 0x";
|
|
res += hex;
|
|
res += ", uuid: " + myPair.first->getUUID().toString();
|
|
}
|
|
return res;
|
|
} // toString
|
|
|
|
|
|
/**
|
|
* @breif Pass the GATT server event onwards to each of the descriptors found in the mapping
|
|
* @param [in] event
|
|
* @param [in] gatts_if
|
|
* @param [in] param
|
|
*/
|
|
void BLEDescriptorMap::handleGATTServerEvent(
|
|
esp_gatts_cb_event_t event,
|
|
esp_gatt_if_t gatts_if,
|
|
esp_ble_gatts_cb_param_t* param) {
|
|
// Invoke the handler for every descriptor we have.
|
|
for (auto &myPair : m_uuidMap) {
|
|
myPair.first->handleGATTServerEvent(event, gatts_if, param);
|
|
}
|
|
} // handleGATTServerEvent
|
|
|
|
|
|
/**
|
|
* @brief Get the first descriptor in the map.
|
|
* @return The first descriptor in the map.
|
|
*/
|
|
BLEDescriptor* BLEDescriptorMap::getFirst() {
|
|
m_iterator = m_uuidMap.begin();
|
|
if (m_iterator == m_uuidMap.end()) return nullptr;
|
|
BLEDescriptor* pRet = m_iterator->first;
|
|
m_iterator++;
|
|
return pRet;
|
|
} // getFirst
|
|
|
|
|
|
/**
|
|
* @brief Get the next descriptor in the map.
|
|
* @return The next descriptor in the map.
|
|
*/
|
|
BLEDescriptor* BLEDescriptorMap::getNext() {
|
|
if (m_iterator == m_uuidMap.end()) return nullptr;
|
|
BLEDescriptor* pRet = m_iterator->first;
|
|
m_iterator++;
|
|
return pRet;
|
|
} // getNext
|
|
#endif /* CONFIG_BLUEDROID_ENABLED */
|