8000 Sync 2017-07-06 · copercini/esp32-snippets@39a79f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39a79f6

Browse files
committed
Sync 2017-07-06
1 parent 340a505 commit 39a79f6

16 files changed

+460
-139
lines changed

cpp_utils/BLE.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <iomanip>
2222

2323
#include "BLE.h"
24-
#include "BLERemoteDevice.h"
24+
#include "BLEClient.h"
2525
#include "BLEUtils.h"
2626
#include "BLEXXXCharacteristic.h"
2727
#include "GeneralUtils.h"
@@ -40,10 +40,11 @@ static EventGroupHandle_t g_eventGroup;
4040
* Note that this address is binary and should not be directly printed. The value of the map is
4141
* the BLEDevice object that this device represents.
4242
*/
43-
static std::map<std::string, BLERemoteDevice> g_devices;
43+
static std::map<std::string, BLEClient> g_devices;
4444

4545
BLEServer *BLE::m_bleServer = nullptr;
4646
BLEScan *BLE::m_pScan = nullptr;
47+
BLEClient *BLE::m_pClient = nullptr;
4748

4849
BLE::BLE() {
4950
}
@@ -53,6 +54,12 @@ BLE::~BLE() {
5354
}
5455

5556

57+
BLEClient* BLE::createClient() {
58+
m_pClient = new BLEClient();
59+
return m_pClient;
60+
}
61+
62+
5663
/**
5764
* @brief Dump all the devices.
5865
*
@@ -74,7 +81,7 @@ void BLE::dumpDevices() {
7481
* @param [in] gatts_if
7582
* @param [in] param
7683
*/
77-
void gatt_server_event_handler(
84+
static void gatt_server_event_handler(
7885
esp_gatts_cb_event_t event,
7986
esp_gatt_if_t gatts_if,
8087
esp_ble_gatts_cb_param_t *param
@@ -112,13 +119,15 @@ static void gatt_client_event_handler(
112119
BLEUtils::dumpGattClientEvent(event, gattc_if, param);
113120

114121
switch(event) {
122+
/*
115123
case ESP_GATTC_OPEN_EVT: {
116-
BLERemoteDevice *pDevice = BLEUtils::findByAddress(std::string((char *)param->open.remote_bda, 6));
124+
BLEClient *pDevice = BLEUtils::findByAddress(std::string((char *)param->open.remote_bda, 6));
117125
BLEUtils::registerByConnId(param->open.conn_id, pDevice);
118126
pDevice->dump();
119127
pDevice->onConnected(param->open.status);
120128
break;
121129
}
130+
*/
122131

123132
/*
124133
* The search_res field of the parameter has been populated. It contains:
@@ -129,17 +138,20 @@ static void gatt_client_event_handler(
129138
* * uint8_t inst_id
130139
* * bool is_primary
131140
*/
141+
142+
/*
132143
case ESP_GATTC_SEARCH_RES_EVT: {
133-
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->search_res.conn_id);
144+
BLEClient *pDevice = BLEUtils::findByConnId(param->search_res.conn_id);
134145
pDevice->addService(param->search_res.srvc_id);
135146
break;
136147
}
137148
138149
case ESP_GATTC_SEARCH_CMPL_EVT: {
139-
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->search_cmpl.conn_id);
150+
BLEClient *pDevice = BLEUtils::findByConnId(param->search_cmpl.conn_id);
140151
pDevice->onSearchComplete();
141152
break;
142153
}
154+
*/
143155

144156
/*
145157
* The `get_char` field of the parameters has been populated. It contains:
@@ -149,15 +161,17 @@ static void gatt_client_event_handler(
149161
* * esp_gatt_id_t char_id
150162
* * esp_gatt_char_prop_t char_prop
151163
*/
164+
/*
152165
case ESP_GATTC_GET_CHAR_EVT: {
153166
if (param->get_char.status == ESP_GATT_OK) {
154-
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->get_char.conn_id);
167+
BLEClient *pDevice = BLEUtils::findByConnId(param->get_char.conn_id);
155168
BLECharacteristicXXX characteristic(param->get_char.conn_id,
156169
param->get_char.srvc_id, param->get_char.char_id, param->get_char.char_prop);
157170
pDevice->onCharacteristic(characteristic);
158171
}
159172
break;
160173
}
174+
*/
161175

162176

163177
/*
@@ -171,17 +185,25 @@ static void gatt_client_event_handler(
171185
* * uint16_t value_type
172186
* * uint16_t value_len
173187
*/
188+
/*
174189
case ESP_GATTC_READ_CHAR_EVT: {
175190
if (param->read.status == ESP_GATT_OK) {
176-
BLERemoteDevice *pDevice = BLEUtils::findByConnId(param->read.conn_id);
191+
BLEClient *pDevice = BLEUtils::findByConnId(param->read.conn_id);
177192
std::string data = std::string((char *)param->read.value, param->read.value_len);
178193
pDevice->onRead(data);
179194
}
180195
break;
181196
}
197+
*/
182198

183-
default:
184-
break;
199+
default: {
200+
break;
201+
}
202+
} // switch
203+
204+
// If we have a client registered, call it.
205+
if (BLE::m_pClient != nullptr) {
206+
BLE::m_pClient->gattClientEventHandler(event, gattc_if, param);
185207
}
186208

187209
} // gatt_event_handler
@@ -223,7 +245,7 @@ static void gap_event_handler(
223245
/**
224246
* @brief Get the current set of known devices.
225247
*/
226-
std::map<std::string, BLERemoteDevice> getDevices() {
248+
std::map<std::string, BLEClient> getDevices() {
227249
return g_devices;
228250
} // getDevices
229251

@@ -346,4 +368,6 @@ BLEScan* BLE::getScan() {
346368
return m_pScan;
347369
} // getScan
348370

371+
372+
349373
#endif // CONFIG_BT_ENABLED

cpp_utils/BLE.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
#include <string>
1616

1717
#include "BLEServer.h"
18-
#include "BLERemoteDevice.h"
18+
#include "BLEClient.h"
1919
#include "BLEUtils.h"
2020
#include "BLEScan.h"
21+
#include "BLEAddress.h"
2122
/**
2223
* @brief %BLE functions.
2324
*/
@@ -26,7 +27,8 @@ class BLE {
2627
BLE();
2728
virtual ~BLE();
2829
static void dumpDevices();
29-
static std::map<std::string, BLERemoteDevice> getDevices();
30+
static std::map<std::string, BLEClient> getDevices();
31+
static BLEClient *createClient();
3032

3133
static void initClient();
3234
static void initServer(std::string deviceName);
@@ -35,6 +37,7 @@ class BLE {
3537
static esp_gatt_if_t getGattcIF();
3638
static BLEServer *m_bleServer;
3739
static BLEScan *m_pScan;
40+
static BLEClient *m_pClient;
3841
}; // class BLE
3942

4043
#endif // CONFIG_BT_ENABLED

cpp_utils/BLEAddress.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,26 @@
1212
#include <string.h>
1313
#include <stdio.h>
1414

15+
16+
/**
17+
* @brief Create an address from the native representation.
18+
* @param [in] address The native representation.
19+
*/
1520
BLEAddress::BLEAddress(esp_bd_addr_t address) {
1621
memcpy(m_address, address, ESP_BD_ADDR_LEN);
17-
}
22+
} // BLEAddress
1823

19-
BLEAddress::~BLEAddress() {
20-
}
2124

25+
/**
26+
* @brief Create an address from a hex string
27+
* A hex string is of the format:
28+
* ```
29+
* 00:00:00:00:00:00
30+
* ```
31+
* which is 17 characters in length.
32+
*
33+
* @param [in] stringAddress The hex representation of the address.
34+
*/
2235
BLEAddress::BLEAddress(std::string stringAddress) {
2336
if (stringAddress.length() != 17) {
2437
return;
@@ -31,7 +44,12 @@ BLEAddress::BLEAddress(std::string stringAddress) {
3144
m_address[3] = (uint8_t)data[3];
3245
m_address[4] = (uint8_t)data[4];
3346
m_address[5] = (uint8_t)data[5];
34-
}
47+
} // BLEAddress
48+
49+
50+
BLEAddress::~BLEAddress() {
51+
} // ~BLEAddress
52+
3553

3654
/**
3755
* @brief Convert a BLE address to a string.
@@ -49,6 +67,11 @@ std::string BLEAddress::toString() {
4967
return stream.str();
5068
} // toString
5169

70+
71+
/**
72+
* @brief Return the native representation of the address.
73+
* @return The native representation of the address.
74+
*/
5275
esp_bd_addr_t *BLEAddress::getNative() {
5376
return &m_address;
54-
}
77+
} // getNative

cpp_utils/BLEAddress.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ class BLEAddress {
1515
BLEAddress(esp_bd_addr_t address);
1616
BLEAddress(std::string stringAddress);
1717
virtual ~BLEAddress();
18-
std::string toString();
1918
esp_bd_addr_t *getNative();
19+
std::string toString();
20+
2021
private:
2122
esp_bd_addr_t m_address;
2223
};

0 commit comments

Comments
 (0)
0