From 7afb8aedd6f3553aa0ec138373aea18068abd2f8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 30 May 2022 20:27:44 +0200 Subject: feat(minion): add print MAC address --- minion/src/gymnasiearbete.cpp | 5 ++++ minion/src/wifi_module.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++ minion/src/wifi_module.hpp | 9 ++++++++ 3 files changed, 68 insertions(+) (limited to 'minion/src') diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp index 54e8d50..d2a970b 100644 --- a/minion/src/gymnasiearbete.cpp +++ b/minion/src/gymnasiearbete.cpp @@ -80,6 +80,11 @@ void setup() Serial.print(F("IP address: ")); Serial.println(wifi_module.get_local_ip(local_ip)); + char mac_address[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = ""; + + Serial.print(F("MAC address: ")); + Serial.println(wifi_module.get_mac_address(mac_address)); + wifi_module.set_multiple_connections_enabled(true); delay(MILLIS_IN_SECOND); diff --git a/minion/src/wifi_module.cpp b/minion/src/wifi_module.cpp index 953e9fa..8a2dd6a 100644 --- a/minion/src/wifi_module.cpp +++ b/minion/src/wifi_module.cpp @@ -254,6 +254,60 @@ auto WiFiModule::get_local_ip(char *local_ip_out) noexcept -> const char * return local_ip_out; } +auto WiFiModule::get_mac_address(char *mac_address_out) noexcept -> const char * +{ + const auto send_success = _send_serial("AT+CIFSR"); + + if (!send_success) + { + return mac_address_out; + } + + auto *buf = util::malloc(strlen(mac_address_out) + 1U); + + if (buf == nullptr) + { + strcpy(mac_address_out, "Memory allocation failure"); + return mac_address_out; + } + + strcpy(buf, ""); + + const auto response_status = _read(10000U, buf); + + if (response_status != WiFiModuleResponseStatus::OK) + { + free(buf); + + sprintf( + mac_address_out, + "Response status was not OK. Was %d", + static_cast(response_status) + ); + + return mac_address_out; + } + + const auto stamac_title = "CIFSR:STAMAC,\""; + + auto mac_address_start = strstr(buf, stamac_title); + + if (mac_address_start == nullptr) + { + free(buf); + strcpy(mac_address_out, "Response parsing error"); + return mac_address_out; + } + + mac_address_start += strlen(stamac_title); + + util::substr(mac_address_start, mac_address_start + 17U, mac_address_out); + + free(buf); + + return mac_address_out; +} + auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest { char request_prefix[] = "+IPD,"; diff --git a/minion/src/wifi_module.hpp b/minion/src/wifi_module.hpp index 9bc2c20..da4a487 100644 --- a/minion/src/wifi_module.hpp +++ b/minion/src/wifi_module.hpp @@ -97,6 +97,15 @@ public: */ auto get_local_ip(char *local_ip_out) noexcept -> const char *; + /** + * Gets the MAC address of the wifi module. + * + * @param mac_address_out MAC address output buffer. + * + * @returns A pointer to the MAC address output buffer. + */ + auto get_mac_address(char *mac_address_out) noexcept -> const char *; + /** * Reads a incoming HTTP request. * -- cgit v1.2.3-18-g5258