From c415ebe8b42dff7da390d905482da780b0bf79fd Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 28 May 2015 17:31:45 +0200 Subject: [PATCH] add function to get the MAC / BSSID as String --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 41 +++++++++++++++++++++++ libraries/ESP8266WiFi/src/ESP8266WiFi.h | 23 +++++++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 009cb69ef..0f97e48a1 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -203,12 +203,32 @@ uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) return mac; } +String ESP8266WiFiClass::macAddress(void) +{ + uint8_t mac[6]; + char macStr[18] = {0}; + wifi_get_macaddr(STATION_IF, mac); + + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return String(macStr); +} + uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) { wifi_get_macaddr(SOFTAP_IF, mac); return mac; } +String ESP8266WiFiClass::softAPmacAddress(void) +{ + uint8_t mac[6]; + char macStr[18] = {0}; + wifi_get_macaddr(SOFTAP_IF, mac); + + sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + return String(macStr); +} + IPAddress ESP8266WiFiClass::localIP() { struct ip_info ip; @@ -251,6 +271,16 @@ uint8_t* ESP8266WiFiClass::BSSID(void) return reinterpret_cast(conf.bssid); } +String ESP8266WiFiClass::BSSIDstr(void) +{ + static struct station_config conf; + char mac[18] = {0}; + wifi_station_get_config(&conf); + sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", conf.bssid[0], conf.bssid[1], conf.bssid[2], conf.bssid[3], conf.bssid[4], conf.bssid[5]); + return String(mac); +} + + int32_t ESP8266WiFiClass::channel(void) { return wifi_get_channel(); } @@ -358,6 +388,17 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) return it->bssid; } +String ESP8266WiFiClass::BSSIDstr(uint8_t i) +{ + char mac[18] = {0}; + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if (!it) + return String(""); + + sprintf(mac,"%02X:%02X:%02X:%02X:%02X:%02X", it->bssid[0], it->bssid[1], it->bssid[2], it->bssid[3], it->bssid[4], it->bssid[5]); + return String(mac); +} + int32_t ESP8266WiFiClass::channel(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 5eb5c9b0f..0d6def8b3 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -103,16 +103,19 @@ public: * Get the station interface MAC address. * * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * return: String */ uint8_t* macAddress(uint8_t* mac); + String macAddress(void); /* * Get the softAP interface MAC address. * * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + * return: String */ uint8_t* softAPmacAddress(uint8_t* mac); - + String softAPmacAddress(void); /* * Get the station interface IP address. * @@ -151,10 +154,17 @@ public: /* * Return the current bssid / mac associated with the network if configured * - * return: bssid string + * return: bssid uint8_t * */ uint8_t * BSSID(void); + /* + * Return the current bssid / mac associated with the network if configured + * + * return: bssid string + */ + String BSSIDstr(void); + /* * Return the current channel associated with the network * @@ -208,10 +218,17 @@ public: /** * return MAC / BSSID of scanned wifi * @param networkItem specify from which network item want to get the information - * @return uint8_t * to MAC / BSSID of scanned wifi + * @return uint8_t * MAC / BSSID of scanned wifi */ uint8_t * BSSID(uint8_t networkItem); + /** + * return MAC / BSSID of scanned wifi + * @param networkItem specify from which network item want to get the information + * @return String MAC / BSSID of scanned wifi + */ + String BSSIDstr(uint8_t networkItem); + /** * return channel of scanned wifi * @param networkItem specify from which network item want to get the information