summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/src/BLEServiceMap.cpp
blob: dd828fae9c7ab75cac4c1ad84aefa4b6822690a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * BLEServiceMap.cpp
 *
 *  Created on: Jun 22, 2017
 *      Author: kolban
 */
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <sstream>
#include <iomanip>
#include "BLEService.h"
 
 
/**
 * @brief Return the service by UUID.
 * @param [in] UUID The UUID to look up the service.
 * @return The characteristic.
 */
BLEService* BLEServiceMap::getByUUID(const char* uuid) {
    return getByUUID(BLEUUID(uuid));
}
/**
 * @brief Return the service by UUID.
 * @param [in] UUID The UUID to look up the service.
 * @return The characteristic.
 */
BLEService* BLEServiceMap::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 service by handle.
 * @param [in] handle The handle to look up the service.
 * @return The service.
 */
BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
return m_handleMap.at(handle);
} // getByHandle 
 
 
/**
 * @brief Set the service by UUID.
 * @param [in] uuid The uuid of the service.
 * @param [in] characteristic The service to cache.
 * @return N/A.
 */
void BLEServiceMap::setByUUID(BLEUUID uuid,
BLEService *service) {
m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString()));
} // setByUUID 
 
 
/**
 * @brief Set the service by handle.
 * @param [in] handle The handle of the service.
 * @param [in] service The service to cache.
 * @return N/A.
 */
void BLEServiceMap::setByHandle(uint16_t handle,
BLEService* service) {
m_handleMap.insert(std::pair<uint16_t, BLEService *>(handle, service));
} // setByHandle 
 
 
/**
 * @brief Return a string representation of the service map.
 * @return A string representation of the service map.
 */
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();
} // toString 
 
void BLEServiceMap::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 Service we have. 
for (auto &myPair : m_uuidMap) {
myPair.first->handleGATTServerEvent(event, gatts_if, param);
}
}
 
/**
 * @brief Get the first service in the map.
 * @return The first service in the map.
 */
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;
} // getFirst 
 
/**
 * @brief Get the next service in the map.
 * @return The next service in the map.
 */
BLEService* BLEServiceMap::getNext() {
if (m_iterator == m_uuidMap.end()) {
return nullptr;
}
BLEService* pRet = m_iterator->first;
m_iterator++;
return pRet;
} // getNext 
 
/**
 * @brief Removes service from maps.
 * @return N/A.
 */
void BLEServiceMap::removeService(BLEService *service){
m_handleMap.erase(service->getHandle());
m_uuidMap.erase(service);
} // removeService 
 
#endif /* CONFIG_BT_ENABLED */