Skip to content

Commit 0bd09ae

Browse files
committed
little changes for a better handling with the wifi class
1 parent f323d99 commit 0bd09ae

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

cpp_utils/WiFi.cpp

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,38 @@ std::string WiFi::getApSSID() {
292292
return std::string((char *)conf.sta.ssid);
293293
} // getApSSID
294294

295+
/**
296+
* @brief Get the current ESP32 IP form AP.
297+
* @return The ESP32 IP.
298+
*/
299+
std::string WiFi::getApIp(){
300+
tcpip_adapter_ip_info_t ipInfo = getApIpInfo();
301+
char ipAddrStr[30];
302+
inet_ntop(AF_INET, &ipInfo.ip.addr, ipAddrStr, sizeof(ipAddrStr));
303+
return std::string(ipAddrStr);
304+
} // getStaIp
305+
306+
/**
307+
* @brief Get the current AP netmask.
308+
* @return The Netmask IP.
309+
*/
310+
std::string WiFi::getApNetmask(){
311+
tcpip_adapter_ip_info_t ipInfo = getApIpInfo();
312+
char ipAddrStr[30];
313+
inet_ntop(AF_INET, &ipInfo.netmask.addr, ipAddrStr, sizeof(ipAddrStr));
314+
return std::string(ipAddrStr);
315+
} // getStaNetmask
316+
317+
/**
318+
* @brief Get the current AP Gateway IP.
319+
* @return The Gateway IP.
320+
*/
321+
std::string WiFi::getApGateway(){
322+
tcpip_adapter_ip_info_t ipInfo = getApIpInfo();
323+
char ipAddrStr[30];
324+
inet_ntop(AF_INET, &ipInfo.gw.addr, ipAddrStr, sizeof(ipAddrStr));
325+
return std::string(ipAddrStr);
326+
} // getStaGateway
295327

296328
/**
297329
* @brief Lookup an IP address by host name.
@@ -351,6 +383,40 @@ tcpip_adapter_ip_info_t WiFi::getStaIpInfo() {
351383
return ipInfo;
352384
} // getStaIpInfo
353385

386+
/**
387+
* @brief Get the current ESP32 IP form STA.
388+
* @return The ESP32 IP.
389+
*/
390+
std::string WiFi::getStaIp(){
391+
tcpip_adapter_ip_info_t ipInfo = getStaIpInfo();
392+
char ipAddrStr[30];
393+
inet_ntop(AF_INET, &ipInfo.ip.addr, ipAddrStr, sizeof(ipAddrStr));
394+
return std::string(ipAddrStr);
395+
} // getStaIp
396+
397+
398+
/**
399+
* @brief Get the current STA netmask.
400+
* @return The Netmask IP.
401+
*/
402+
std::string WiFi::getStaNetmask(){
403+
tcpip_adapter_ip_info_t ipInfo = getStaIpInfo();
404+
char ipAddrStr[30];
405+
inet_ntop(AF_INET, &ipInfo.netmask.addr, ipAddrStr, sizeof(ipAddrStr));
406+
return std::string(ipAddrStr);
407+
} // getStaNetmask
408+
409+
410+
/**
411+
* @brief Get the current STA Gateway IP.
412+
* @return The Gateway IP.
413+
*/
414+
std::string WiFi::getStaGateway(){
415+
tcpip_adapter_ip_info_t ipInfo = getStaIpInfo();
416+
char ipAddrStr[30];
417+
inet_ntop(AF_INET, &ipInfo.gw.addr, ipAddrStr, sizeof(ipAddrStr));
418+
return std::string(ipAddrStr);
419+
} // getStaGateway
354420

355421
/**
356422
* @brief Get the MAC address of the STA interface.
@@ -501,6 +567,27 @@ std::vector<WiFiAPRecord> WiFi::scan() {
501567
* @return N/A.
502568
*/
503569
void WiFi::startAP(const std::string& ssid, const std::string& password, wifi_auth_mode_t auth) {
570+
startAP(ssid, password, auth, 0, false, 4);
571+
} // startAP
572+
573+
/**
574+
* @brief Start being an access point.
575+
*
576+
* @param[in] ssid The SSID to use to advertize for stations.
577+
* @param[in] password The password to use for station connections.
578+
* @param[in] auth The authorization mode for access to this access point. Options are:
579+
* * WIFI_AUTH_OPEN
580+
* * WIFI_AUTH_WPA_PSK
581+
* * WIFI_AUTH_WPA2_PSK
582+
* * WIFI_AUTH_WPA_WPA2_PSK
583+
* * WIFI_AUTH_WPA2_ENTERPRISE
584+
* * WIFI_AUTH_WEP
585+
* @param[in] channel from the access point.
586+
* @param[in] is the ssid hidden, ore not.
587+
* @param[in] limiting number of clients.
588+
* @return N/A.
589+
*/
590+
void WiFi::startAP(const std::string& ssid, const std::string& password, wifi_auth_mode_t auth, uint8_t channel, bool ssid_hidden, uint8_t max_connection) {
504591
ESP_LOGD(LOG_TAG, ">> startAP: ssid: %s", ssid.c_str());
505592

506593
init();
@@ -517,10 +604,10 @@ void WiFi::startAP(const std::string& ssid, const std::string& password, wifi_au
517604
::memcpy(apConfig.ap.ssid, ssid.data(), ssid.size());
518605
apConfig.ap.ssid_len = ssid.size();
519606
::memcpy(apConfig.ap.password, password.data(), password.size());
520-
apConfig.ap.channel = 0;
607+
apConfig.ap.channel = channel;
521608
apConfig.ap.authmode = auth;
522-
apConfig.ap.ssid_hidden = 0;
523-
apConfig.ap.max_connection = 4;
609+
apConfig.ap.ssid_hidden = (uint8_t) ssid_hidden;
610+
apConfig.ap.max_connection = max_connection;
524611
apConfig.ap.beacon_interval = 100;
525612

526613
errRc = ::esp_wifi_set_config(WIFI_IF_AP, &apConfig);
@@ -543,7 +630,6 @@ void WiFi::startAP(const std::string& ssid, const std::string& password, wifi_au
543630
ESP_LOGD(LOG_TAG, "<< startAP");
544631
} // startAP
545632

546-
547633
/**
548634
* @brief Set the event handler to use to process detected events.
549635
* @param[in] wifiEventHandler The class that will be used to process events.

cpp_utils/WiFi.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,19 @@ class WiFi {
135135
static std::string getApMac();
136136
static tcpip_adapter_ip_info_t getApIpInfo();
137137
static std::string getApSSID();
138+
static std::string getApIp();
139+
static std::string getApNetmask();
140+
static std::string getApGateway();
138141
static std::string getMode();
139142
static tcpip_adapter_ip_info_t getStaIpInfo();
140143
static std::string getStaMac();
141144
static std::string getStaSSID();
145+
static std::string getStaIp();
146+
static std::string getStaNetmask();
147+
static std::string getStaGateway();
142148
std::vector<WiFiAPRecord> scan();
143149
void startAP(const std::string& ssid, const std::string& passwd, wifi_auth_mode_t auth = WIFI_AUTH_OPEN);
150+
void startAP(const std::string& ssid, const std::string& passwd, wifi_auth_mode_t auth, uint8_t channel, bool ssid_hidden, uint8_t max_connection);
144151
void setIPInfo(const std::string& ip, const std::string& gw, const std::string& netmask);
145152
void setIPInfo(const char* ip, const char* gw, const char* netmask);
146153
void setIPInfo(uint32_t ip, uint32_t gw, uint32_t netmask);

0 commit comments

Comments
 (0)