8000 Work for #98 · headcloudmonkey/esp32-snippets@cb84cd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb84cd9

Browse files
committed
Work for nkolban#98
1 parent 7d97376 commit cb84cd9

File tree

6 files changed

+54
-20
lines changed

6 files changed

+54
-20
lines changed

cpp_utils/BLEClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ std::map<std::string, BLERemoteService*>* BLEClient::getServices() {
281281
* and will culminate with an ESP_GATTC_SEARCH_CMPL_EVT when all have been received.
282282
*/
283283
ESP_LOGD(LOG_TAG, ">> getServices");
284-
m_servicesMap.empty();
284+
m_servicesMap.clear();
285285
esp_err_t errRc = esp_ble_gattc_search_service(
286286
getGattcIf(),
287287
getConnId(),

cpp_utils/BLERemoteService.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) {
140140
// asked the device about its characteristics, then we do that now. Once we get the results we can then
141141
// examine the characteristics map to see if it has the characteristic we are looking for.
142142
if (!m_haveCharacteristics) {
143-
getCharacteristics();
143+
retrieveCharacteristics();
144144
}
145145
std::string v = uuid.toString();
146146
for (auto &myPair : m_characteristicMap) {
@@ -156,7 +156,7 @@ BLERemoteCharacteristic* BLERemoteService::getCharacteristic(BLEUUID uuid) {
156156
* @brief Retrieve all the characteristics for this service.
157157
* @return N/A
158158
*/
159-
void BLERemoteService::getCharacteristics() {
159+
void BLERemoteService::retrieveCharacteristics() {
160160

161161
ESP_LOGD(LOG_TAG, ">> getCharacteristics() for service: %s", getUUID().toString().c_str());
162162

@@ -262,6 +262,15 @@ void BLERemoteService::getCharacteristics() {
262262
} // getCharacteristics
263263

264264

265+
/**
266+
* @brief Retrieve a map of all the characteristics of this service.
267+
* @return A map of all the characteristics of this service.
268+
*/
269+
std::map<std::string, BLERemoteCharacteristic*>* BLERemoteService::getCharacteristics() {
270+
return &m_characteristicMap;
271+
} // getCharacteristics
272+
273+
265274
BLEClient* BLERemoteService::getClient() {
266275
return m_pClient;
267276
}

cpp_utils/BLERemoteService.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class BLERemoteService {
3232
// Public methods
3333
BLERemoteCharacteristic* getCharacteristic(const char* uuid);
3434
BLERemoteCharacteristic* getCharacteristic(BLEUUID uuid);
35+
std::map<std::string, BLERemoteCharacteristic*>* getCharacteristics();
3536

3637
BLEClient* getClient(void);
3738
BLEUUID getUUID(void);
@@ -46,7 +47,7 @@ class BLERemoteService {
4647
friend class BLERemoteCharacteristic;
4748

4849
// Private methods
49-
void getCharacteristics(void);
50+
void retrieveCharacteristics(void);
5051
uint16_t getHandle();
5152
esp_gatt_id_t* getSrvcId(void);
5253
uint16_t getStartHandle();

cpp_utils/WiFi.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,30 @@ static void setDNSServer(char *ip) {
4141
}
4242
*/
4343

44+
45+
46+
4447
/**
4548
* @brief Creates and uses a default event handler
4649
*/
4750
WiFi::WiFi()
4851
: ip(0)
4952
, gw(0)
5053
, netmask(0)
51-
, m_wifiEventHandler(nullptr)
54+
, m_pWifiEventHandler(nullptr)
5255
{
5356
::nvs_flash_init();
5457
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
5558
esp_wifi_init(&config);
5659
::tcpip_adapter_init();
57-
m_wifiEventHandler = new WiFiEventHandler();
60+
m_pWifiEventHandler = new WiFiEventHandler();
5861
} // WiFi
5962

6063
/**
6164
* @brief Deletes the event handler that was used by the class
6265
*/
6366
WiFi::~WiFi() {
64-
delete m_wifiEventHandler;
67+
delete m_pWifiEventHandler;
6568
}
6669

6770
/**
@@ -134,11 +137,12 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
134137
*
135138
* The event handler will be called back with the outcome of the connection.
136139
*
137-
* @param[in] ssid The network SSID of the access point to which we wish to connect.
138-
* @param[in] password The password of the access point to which we wish to connect.
140+
* @param [in] ssid The network SSID of the access point to which we wish to connect.
141+
* @param [in] password The password of the access point to which we wish to connect.
142+
* @param [in] waitForConnection Block until the connection has an outcome.
139143
* @return N/A.
140144
*/
141-
void WiFi::connectAP(const std::string& ssid, const std::string& password){
145+
void WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
142146
ESP_LOGD(LOG_TAG, ">> connectAP");
143147

144148
if (ip != 0 && gw != 0 && netmask != 0) {
@@ -153,7 +157,8 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password){
153157
}
154158

155159

156-
ESP_ERROR_CHECK( esp_event_loop_init(m_wifiEventHandler->getEventHandler(), m_wifiEventHandler));
160+
ESP_ERROR_CHECK(esp_event_loop_init(WiFi::eventHandler, this));
161+
//ESP_ERROR_CHECK(esp_event_loop_init(m_pWifiEventHandler->getEventHandler(), m_pWifiEventHandler));
157162
ESP_ERROR_CHECK(::esp_wifi_set_storage(WIFI_STORAGE_RAM));
158163
ESP_ERROR_CHECK(::esp_wifi_set_mode(WIFI_MODE_STA));
159164
wifi_config_t sta_config;
@@ -164,7 +169,9 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password){
164169
ESP_ERROR_CHECK(::esp_wifi_set_config(WIFI_IF_STA, &sta_config));
165170
ESP_ERROR_CHECK(::esp_wifi_start());
166171

172+
m_gotIpEvt.take("connectAP");
167173
ESP_ERROR_CHECK(::esp_wifi_connect());
174+
m_gotIpEvt.wait("connectAP");
168175
ESP_LOGD(LOG_TAG, "<< connectAP");
169176
} // connectAP
170177

@@ -180,6 +187,22 @@ void WiFi::dump() {
180187
ESP_LOGD(LOG_TAG, "DNS Server[0]: %s", ipAddrStr);
181188
} // dump
182189

190+
191+
/**
192+
* @brief Primary event handler interface.
193+
*/
194+
esp_err_t WiFi::eventHandler(void* ctx, system_event_t* event) {
195+
WiFi *pWiFi = (WiFi *)ctx;
196+
esp_err_t rc = pWiFi->m_pWifiEventHandler->getEventHandler()(pWiFi->m_pWifiEventHandler, event);
197+
// If the event we received indicates that we now have an IP address then unlock the mutex that
198+
// indicates we are waiting for an IP.
199+
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
200+
pWiFi->m_gotIpEvt.give();
201+
}
202+
return rc;
203+
} // eventHandler
204+
205+
183206
/**
184207
* @brief Get the AP IP Info.
185208
* @return The AP IP Info.
@@ -311,7 +334,7 @@ std::string WiFi::getStaSSID() {
311334
std::vector<WiFiAPRecord> WiFi::scan() {
312335
::nvs_flash_init();
313336
::tcpip_adapter_init();
314-
ESP_ERROR_CHECK(esp_event_loop_init(m_wifiEventHandler->getEventHandler(), m_wifiEventHandler));
337+
ESP_ERROR_CHECK(esp_event_loop_init(m_pWifiEventHandler->getEventHandler(), m_pWifiEventHandler));
315338
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
316339
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
317340
ESP_ERROR_CHECK(::esp_wifi_set_storage(WIFI_STORAGE_RAM));
@@ -353,7 +376,7 @@ std::vector<WiFiAPRecord> WiFi::scan() {
353376
void WiFi::startAP(const std::string& ssid, const std::string& password) {
354377
::nvs_flash_init();
355378
::tcpip_adapter_init();
356-
ESP_ERROR_CHECK(esp_event_loop_init(m_wifiEventHandler->getEventHandler(), m_wifiEventHandler));
379+
ESP_ERROR_CHECK(esp_event_loop_init(m_pWifiEventHandler->getEventHandler(), m_pWifiEventHandler));
357380
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
358381
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
359382
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
@@ -378,7 +401,7 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {
378401
* @param[in] wifiEventHandler The class that will be used to process events.
379402
*/
380403
void WiFi::setWifiEventHandler(WiFiEventHandler *wifiEventHandler) {
381-
this->m_wifiEventHandler = wifiEventHandler;
404+
this->m_pWifiEventHandler = wifiEventHandler;
382405
} // setWifiEventHandler
383406

384407

cpp_utils/WiFi.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string>
1313
#include <vector>
1414
#include <mdns.h>
15+
#include "FreeRTOS.h"
1516
#include "WiFiEventHandler.h"
1617

1718
/**
@@ -103,10 +104,13 @@ class WiFiAPRecord {
103104
*/
104105
class WiFi {
105106
private:
107+
static esp_err_t eventHandler(void* ctx, system_event_t* event);
106108
uint32_t ip;
107109
uint32_t gw;
108110
uint32_t netmask;
109-
WiFiEventHandler *m_wifiEventHandler;
111+
WiFiEventHandler* m_pWifiEventHandler;
112+
uint8_t m_dnsCount=0;
113+
FreeRTOS::Semaphore m_gotIpEvt = FreeRTOS::Semaphore("GotIpEvt");
110114

111115
public:
112116
WiFi();
@@ -119,7 +123,7 @@ class WiFi {
119123
void setDNSServer(int numdns, ip_addr_t ip);
120124
struct in_addr getHostByName(const std::string& hostName);
121125
struct in_addr getHostByName(const char* hostName);
122-
void connectAP(const std::string& ssid, const std::string& password);
126+
void connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true);
123127
void dump();
124128
static std::string getApMac();
125129
static tcpip_adapter_ip_info_t getApIpInfo();
@@ -134,10 +138,6 @@ class WiFi {
134138
void setIPInfo(const char* ip, const char* gw, const char* netmask);
135139
void setIPInfo(uint32_t ip, uint32_t gw, uint32_t netmask);
136140
void setWifiEventHandler(WiFiEventHandler *wifiEventHandler);
137-
private:
138-
uint8_t m_dnsCount=0;
139-
//char *m_dnsServer = nullptr;
140-
141141
};
142142

143143
#endif /* MAIN_WIFI_H_ */

cpp_utils/WiFiEventHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class WiFiEventHandler {
110110
}
111111

112112
private:
113+
friend class WiFi;
113114
WiFiEventHandler *m_nextHandler;
114115
static esp_err_t eventHandler(void* ctx, system_event_t* event);
115116
};

0 commit comments

Comments
 (0)
0