28
28
#include < Task.h>
29
29
30
30
31
-
32
31
static char tag[]= " WiFi" ;
33
32
34
33
@@ -68,14 +67,46 @@ WiFi::WiFi() {
68
67
* @param [in] ip The IP address of the DNS Server.
69
68
* @return N/A.
70
69
*/
71
- void WiFi::addDNSServer (std::string ip) {
72
- ip_addr_t dnsserver;
73
- ESP_LOGD (tag, " Setting DNS[%d] to %s" , m_dnsCount, ip.c_str ());
74
- inet_pton (AF_INET, ip.c_str (), &dnsserver);
75
- ::dns_setserver (m_dnsCount, &dnsserver);
76
- m_dnsCount++;
70
+ void WiFi::addDNSServer (const std::string& ip) {
71
+ addDNSServer (ip.c_str ());
72
+ } // addDNSServer
73
+
74
+ void WiFi::addDNSServer (const char * ip) {
75
+ ip_addr_t dnsserver;
76
+ ESP_LOGD (tag, " Setting DNS[%d] to %s" , m_dnsCount, ip);
77
+ inet_pton (AF_INET, ip, &dnsserver);
78
+ ::dns_setserver (m_dnsCount, &dnsserver);
79
+ m_dnsCount++;
80
+ m_dnsCount %= 2 ;
77
81
} // addDNSServer
78
82
83
+ /* *
84
+ * @brief Set a reference to a DNS server.
85
+ *
86
+ * Here we define a server that will act as a DNS server. We use numdns to specify which DNS server to set
87
+ *
88
+ * For example:
89
+ *
90
+ * @code{.cpp}
91
+ * wifi.setDNSServer(0, "8.8.8.8");
92
+ * wifi.setDNSServer(1, "8.8.4.4");
93
+ * @endcode
94
+ *
95
+ * @param [in] numdns The DNS number we wish to set
96
+ * @param [in] ip The IP address of the DNS Server.
97
+ * @return N/A.
98
+ */
99
+ void WiFi::setDNSServer (int numdns, const std::string& ip) {
100
+ setDNSServer (numdns, ip.c_str ());
101
+ } // setDNSServer
102
+
103
+ void WiFi::setDNSServer (int numdns, const char * ip) {
104
+ ip_addr_t dnsserver;
105
+ ESP_LOGD (tag, " Setting DNS[%d] to %s" , numdns, ip);
106
+ inet_pton (AF_INET, ip, &dnsserver);
107
+ ::dns_setserver (numdns, &dnsserver);
108
+ } // setDNSServer
109
+
79
110
/* *
80
111
* @brief Connect to an external access point.
81
112
*
@@ -85,7 +116,7 @@ void WiFi::addDNSServer(std::string ip) {
85
116
* @param[in] password The password of the access point to which we wish to connect.
86
117
* @return N/A.
87
118
*/
88
- void WiFi::connectAP (std::string ssid, std::string password){
119
+ void WiFi::connectAP (const std::string& ssid, const std::string& password){
89
120
::nvs_flash_init ();
90
121
::tcpip_adapter_init ();
91
122
if (ip.length () > 0 && gw.length () > 0 && netmask.length () > 0 ) {
@@ -115,7 +146,6 @@ void WiFi::connectAP(std::string ssid, std::string password){
115
146
ESP_ERROR_CHECK (::esp_wifi_connect ());
116
147
} // connectAP
117
148
118
-
119
149
/* *
120
150
* @brief Dump diagnostics to the log.
121
151
*/
@@ -147,9 +177,9 @@ tcpip_adapter_ip_info_t WiFi::getApIpInfo() {
147
177
std::string WiFi::getApMac () {
148
178
uint8_t mac[6 ];
149
179
esp_wifi_get_mac (WIFI_IF_AP, mac);
150
- std::stringstream s ;
151
- s << std::hex << std::setfill ( ' 0 ' ) << std::setw ( 2 ) << ( int ) mac[0 ] << ' : ' << ( int ) mac[1 ] << ' : ' << ( int ) mac[2 ] << ' : ' << ( int ) mac[3 ] << ' : ' << ( int ) mac[4 ] << ' : ' << ( int ) mac[5 ];
152
- return s. str ( );
180
+ auto mac_str = ( char *) malloc ( 18 ) ;
181
+ sprintf (mac_str, " %02X:%02X:%02X:%02X:%02X:%02X " , mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]) ;
182
+ return std::string ( std::move (mac_str) );
153
183
} // getApMac
154
184
155
185
@@ -171,18 +201,21 @@ std::string WiFi::getApSSID() {
171
201
*
172
202
* @return The IP address of the host or 0.0.0.0 if not found.
173
203
*/
174
- struct in_addr WiFi::getHostByName (std::string hostName) {
175
- struct in_addr retAddr;
176
- struct hostent *he = gethostbyname (hostName.c_str ());
177
- if (he == nullptr ) {
178
- retAddr.s_addr = 0 ;
179
- ESP_LOGD (tag, " Unable to resolve %s - %d" , hostName.c_str (), h_errno);
180
- } else {
181
- retAddr = *(struct in_addr *)(he->h_addr_list [0 ]);
182
- // ESP_LOGD(tag, "resolved %s to %.8x", hostName, *(uint32_t *)&retAddr);
204
+ struct in_addr WiFi::getHostByName (const std::string& hostName) {
205
+ return getHostByName (hostName.c_str ());
206
+ } // getHostByName
183
207
184
- }
185
- return retAddr;
208
+ struct in_addr WiFi::getHostByName (const char * hostName) {
209
+ struct in_addr retAddr;
210
+ struct hostent *he = gethostbyname (hostName);
211
+ if (he == nullptr ) {
212
+ retAddr.s_addr = 0 ;
213
+ ESP_LOGD (tag, " Unable to resolve %s - %d" , hostName, h_errno);
214
+ } else {
215
+ retAddr = *(struct in_addr *)(he->h_addr_list [0 ]);
216
+ ESP_LOGD (tag, " resolved %s to %.8x" , hostName, *(uint32_t *)&retAddr);
217
+ }
218
+ return retAddr;
186
219
} // getHostByName
187
220
188
221
@@ -226,9 +259,9 @@ tcpip_adapter_ip_info_t WiFi::getStaIpInfo() {
226
259
std::string WiFi::getStaMac () {
227
260
uint8_t mac[6 ];
228
261
esp_wifi_get_mac (WIFI_IF_STA, mac);
229
- std::stringstream s ;
230
- s << std::hex << std::setfill ( ' 0 ' ) << std::setw ( 2 ) << ( int ) mac[0 ] << ' : ' << ( int ) mac[1 ] << ' : ' << ( int ) mac[2 ] << ' : ' << ( int ) mac[3 ] << ' : ' << ( int ) mac[4 ] << ' : ' << ( int ) mac[5 ];
231
- return s. str ( );
262
+ auto mac_str = ( char *) malloc ( 18 ) ;
263
+ sprintf (mac_str, " %02X:%02X:%02X:%02X:%02X:%02X " , mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]) ;
264
+ return std::string ( std::move (mac_str) );
232
265
} // getStaMac
233
266
234
267
@@ -295,7 +328,7 @@ std::vector<WiFiAPRecord> WiFi::scan() {
295
328
* @param[in] password The password to use for station connections.
296
329
* @return N/A.
297
330
*/
298
- void WiFi::startAP (std::string ssid, std::string password) {
331
+ void WiFi::startAP (const std::string& ssid, const std::string& password) {
299
332
::nvs_flash_init ();
300
333
::tcpip_adapter_init ();
301
334
ESP_ERROR_CHECK (esp_event_loop_init (wifiEventHandler->getEventHandler (), wifiEventHandler));
@@ -306,7 +339,7 @@ void WiFi::startAP(std::string ssid, std::string password) {
306
339
wifi_config_t apConfig;
307
340
::memset (&apConfig, 0 , sizeof (apConfig));
308
341
::memcpy (apConfig.ap.ssid, ssid.data(), ssid.size());
309
- apConfig.ap .ssid_len = 0 ;
342
+ apConfig.ap .ssid_len = ssid. size () ;
310
343
::memcpy (apConfig.ap.password, password.data(), password.size());
311
344
apConfig.ap .channel = 0 ;
312
345
apConfig.ap .authmode = WIFI_AUTH_OPEN;
@@ -334,12 +367,18 @@ void WiFi::startAP(std::string ssid, std::string password) {
334
367
* @param [in] netmask Netmask value.
335
368
* @return N/A.
336
369
*/
337
- void WiFi::setIPInfo (std::string ip, std::string gw, std::string netmask) {
370
+ void WiFi::setIPInfo (const std::string& ip, const std::string& gw, const std::string& netmask) {
338
371
this ->ip = ip;
339
372
this ->gw = gw;
340
373
this ->netmask = netmask;
341
374
} // setIPInfo
342
375
376
+ void WiFi::setIPInfo (std::string&& ip, std::string&& gw, std::string&& netmask) {
377
+ this ->ip = std::move (ip);
378
+ this ->gw = std::move (gw);
379
+ this ->netmask = std::move (netmask);
380
+ } // setIPInfo
381
+
343
382
344
383
/* *
345
384
* @brief Return a string representation of the WiFi access point record.
@@ -368,9 +407,11 @@ std::string WiFiAPRecord::toString() {
368
407
auth = " <unknown>" ;
369
408
break ;
370
409
}
371
- std::stringstream s;
372
- s<< " ssid: " << m_ssid << " , auth: " << auth << " , rssi: " << m_rssi;
373
- return s.str ();
410
+ // std::stringstream s;
411
+ // s<< "ssid: " << m_ssid << ", auth: " << auth << ", rssi: " << m_rssi;
412
+ auto info_str = (char *) malloc (6 + 32 + 8 + 22 + 8 + 3 + 1 );
413
+ sprintf (info_str, " ssid: %s, auth: %s, rssi: %d" , m_ssid.c_str (), auth.c_str (), (int ) m_rssi);
414
+ return std::string (std::move (info_str));
374
415
} // toString
375
416
376
417
MDNS::MDNS () {
@@ -392,23 +433,72 @@ MDNS::~MDNS() {
392
433
* @param [in] port
393
434
* @return N/A.
394
435
*/
395
- void MDNS::serviceAdd (std::string service, std::string proto, uint16_t port) {
396
- ESP_ERROR_CHECK (mdns_service_add (m_mdns_server, service.c_str (), proto.c_str (), port));
436
+ void MDNS::serviceAdd (const std::string& service, const std::string& proto, uint16_t port) {
437
+ serviceAdd (service.c_str (), proto.c_str (), port);
438
+ } // serviceAdd
439
+
440
+
441
+ void MDNS::serviceInstanceSet (const std::string& service, const std::string& proto, const std::string& instance) {
442
+ serviceInstanceSet (service.c_str (), proto.c_str (), instance.c_str ());
443
+ } // serviceInstanceSet
444
+
445
+
446
+ void MDNS::servicePortSet (const std::string& service, const std::string& proto, uint16_t port) {
447
+ servicePortSet (service.c_str (), proto.c_str (), port);
448
+ } // servicePortSet
449
+
450
+
451
+ void MDNS::serviceRemove (const std::string& service, const std::string& proto) {
452
+ serviceRemove (service.c_str (), proto.c_str ());
453
+ } // serviceRemove
454
+
455
+
456
+ /* *
457
+ * @brief Set the mDNS hostname.
458
+ *
459
+ * @param [in] hostname The host name to set against the mDNS.
460
+ * @return N/A.
461
+ */
462
+ void MDNS::setHostname (const std::string& hostname) {
463
+ setHostname (hostname.c_str ());
464
+ } // setHostname
465
+
466
+
467
+ /* *
468
+ * @brief Set the mDNS instance.
469
+ *
470
+ * @param [in] instance The instance name to set against the mDNS.
471
+ * @return N/A.
472
+ */
473
+ void MDNS::setInstance (const std::string& instance) {
474
+ setInstance (instance.c_str ());
475
+ } // setInstance
476
+
477
+ /* *
478
+ * @brief Define the service for mDNS.
479
+ *
480
+ * @param [in] service
481
+ * @param [in] proto
482
+ * @param [in] port
483
+ * @return N/A.
484
+ */
485
+ void MDNS::serviceAdd (const char * service, const char * proto, uint16_t port) {
486
+ ESP_ERROR_CHECK (mdns_service_add (m_mdns_server, service, proto, port));
397
487
} // serviceAdd
398
488
399
489
400
- void MDNS::serviceInstanceSet (std::string service, std::string proto, std::string instance) {
401
- ESP_ERROR_CHECK (mdns_service_instance_set (m_mdns_server, service. c_str () , proto. c_str () , instance. c_str () ));
490
+ void MDNS::serviceInstanceSet (const char * service, const char * proto, const char * instance) {
491
+ ESP_ERROR_CHECK (mdns_service_instance_set (m_mdns_server, service, proto, instance));
402
492
} // serviceInstanceSet
403
493
404
494
405
- void MDNS::servicePortSet (std::string service, std::string proto, uint16_t port) {
406
- ESP_ERROR_CHECK (mdns_service_port_set (m_mdns_server, service. c_str () , proto. c_str () , port));
495
+ void MDNS::servicePortSet (const char * service, const char * proto, uint16_t port) {
496
+ ESP_ERROR_CHECK (mdns_service_port_set (m_mdns_server, service, proto, port));
407
497
} // servicePortSet
408
498
409
499
410
- void MDNS::serviceRemove (std::string service, std::string proto) {
411
- ESP_ERROR_CHECK (mdns_service_remove (m_mdns_server, service. c_str () , proto. c_str () ));
500
+ void MDNS::serviceRemove (const char * service, const char * proto) {
501
+ ESP_ERROR_CHECK (mdns_service_remove (m_mdns_server, service, proto));
412
502
} // serviceRemove
413
503
414
504
@@ -418,8 +508,8 @@ void MDNS::serviceRemove(std::string service, std::string proto) {
418
508
* @param [in] hostname The host name to set against the mDNS.
419
509
* @return N/A.
420
510
*/
421
- void MDNS::setHostname (std::string hostname) {
422
- ESP_ERROR_CHECK (mdns_set_hostname (m_mdns_server,hostname. c_str () ));
511
+ void MDNS::setHostname (const char * hostname) {
512
+ ESP_ERROR_CHECK (mdns_set_hostname (m_mdns_server,hostname));
423
513
} // setHostname
424
514
425
515
@@ -429,6 +519,6 @@ void MDNS::setHostname(std::string hostname) {
429
519
* @param [in] instance The instance name to set against the mDNS.
430
520
* @return N/A.
431
521
*/
432
- void MDNS::setInstance (std::string instance) {
433
- ESP_ERROR_CHECK (mdns_set_instance (m_mdns_server, instance. c_str () ));
522
+ void MDNS::setInstance (const char * instance) {
523
+ ESP_ERROR_CHECK (mdns_set_instance (m_mdns_server, instance));
434
524
} // setInstance
0 commit comments