Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Below is the list of indiviuals that have contributed to the project.
|----|----|
|[Per Malmberg](https://github.com/PerMalmberg)|Author and maintainer|
|[squonk11](https://github.com/squonk11)|[PR#65](https://github.com/PerMalmberg/Smooth/pull/65)|
|[squonk11](https://github.com/squonk11)|[PR#114](https://github.com/PerMalmberg/Smooth/pull/114)|
|[KerryRJ](https://github.com/KerryRJ)|[PR#71](https://github.com/PerMalmberg/Smooth/pull/71)|
|[jeremyjh](https://github.com/jeremyjh)|[PR#87](https://github.com/PerMalmberg/Smooth/pull/87)|
|[COM8](https://github.com/COM8)|[PR#98](https://github.com/PerMalmberg/Smooth/pull/98)|
50 changes: 47 additions & 3 deletions lib/smooth/core/network/Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ using namespace smooth::core;

namespace smooth::core::network
{
ip4_addr_t Wifi::ip = { 0 };

Wifi::Wifi()
{
tcpip_adapter_init();
Expand Down Expand Up @@ -128,6 +130,7 @@ namespace smooth::core::network
}
else if (event_id == WIFI_EVENT_STA_DISCONNECTED)
{
wifi->ip.addr = 0;
wifi->connected_to_ap = false;
publish_status(wifi->connected_to_ap, true);

Expand All @@ -139,10 +142,12 @@ namespace smooth::core::network
}
else if (event_id == WIFI_EVENT_AP_START)
{
wifi->ip.addr = 0xC0A80401; // 192.168.4.1
Comment thread
PerMalmberg marked this conversation as resolved.
publish_status(true, true);
}
else if (event_id == WIFI_EVENT_AP_STOP)
{
wifi->ip.addr = 0;
Log::info("SoftAP", "AP stopped");
publish_status(false, true);
}
Expand Down Expand Up @@ -181,21 +186,24 @@ namespace smooth::core::network
auto ip_changed = event_id == IP_EVENT_STA_GOT_IP ?
reinterpret_cast<ip_event_got_ip_t*>(event_data)->ip_changed : true;
publish_status(true, ip_changed);
wifi->ip.addr = reinterpret_cast<ip_event_got_ip_t*>(event_data)->ip_info.ip.addr;
}
else if (event_id == IP_EVENT_STA_LOST_IP)
{
wifi->ip.addr = 0;
publish_status(false, true);
}
}
}

std::string Wifi::get_mac_address() const
std::string Wifi::get_mac_address()
{
std::stringstream mac;

uint8_t m[6];
std::array<uint8_t, 6> m;
bool ret = get_local_mac_address(m);

if (esp_wifi_get_mac(WIFI_IF_STA, m) == ESP_OK)
if (ret)
{
for (const auto& v : m)
{
Expand All @@ -211,6 +219,42 @@ namespace smooth::core::network
return mac.str();
}

bool Wifi::get_local_mac_address(std::array<uint8_t, 6>& m)
{
wifi_mode_t mode;
esp_err_t err = esp_wifi_get_mode(&mode);

if (err == ESP_OK)
{
if (mode == WIFI_MODE_STA)
{
err = esp_wifi_get_mac(WIFI_IF_STA, m.data());
}
else if (mode == WIFI_MODE_AP)
{
err = esp_wifi_get_mac(WIFI_IF_AP, m.data());
}
else
{
err = ESP_FAIL;
}
}

if (err != ESP_OK)
{
Log::error("Wifi", "get_local_mac_address(): {}", esp_err_to_name(err));
}

return err == ESP_OK;
}

// attention: access to this function might have a threading issue.
// It should be called from the main thread only!
ip4_addr_t Wifi::get_local_ip()
{
return ip;
}

void Wifi::start_softap(uint8_t max_conn)
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
Expand Down
7 changes: 6 additions & 1 deletion lib/smooth/include/smooth/core/network/Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ namespace smooth::core::network
int32_t event_id,
void* event_data);

[[nodiscard]] std::string get_mac_address() const;
[[nodiscard]] static std::string get_mac_address();

[[nodiscard]] static bool get_local_mac_address(std::array<uint8_t, 6>& m);

[[nodiscard]] static ip4_addr_t get_local_ip();

/// Start providing an access point
/// \param max_conn maximum number of clients to connect to this AP
Expand All @@ -89,5 +93,6 @@ namespace smooth::core::network
std::string ssid{};

std::string password{};
static ip4_addr_t ip;
Comment thread
PerMalmberg marked this conversation as resolved.
};
}
5 changes: 5 additions & 0 deletions mock-idf/components/esp_common/esp_err.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#include <esp_err.h>

const char *esp_err_to_name(esp_err_t code)
{
return nullptr;
}
2 changes: 2 additions & 0 deletions mock-idf/components/esp_common/include/esp_err.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ typedef int32_t esp_err_t;
#define ESP_ERR_FLASH_BASE 0x6000 /*!< Starting number of flash error codes */

#define ESP_ERROR_CHECK(x) (x)

const char *esp_err_to_name(esp_err_t code);
6 changes: 6 additions & 0 deletions mock-idf/components/esp_wifi/esp_wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ esp_err_t esp_wifi_get_mac(wifi_interface_t /*ifx*/, uint8_t mac[6])

return ESP_OK;
}

esp_err_t esp_wifi_get_mode(wifi_mode_t * mode)
{
*mode = WIFI_MODE_STA;
return ESP_OK;
}
2 changes: 2 additions & 0 deletions mock-idf/components/esp_wifi/include/esp_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ esp_err_t esp_wifi_set_config(wifi_interface_t interface, wifi_config_t* conf);
esp_err_t esp_wifi_connect();

esp_err_t esp_wifi_get_mac(wifi_interface_t ifx, uint8_t mac[6]);

esp_err_t esp_wifi_get_mode(wifi_mode_t * mode);
35 changes: 33 additions & 2 deletions test/access_point/access_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.

#include "access_point.h"
#include <memory>
#include <string>
#include <fmt/format.h>
#include <smooth/core/logging/log.h>
#include <smooth/core/task_priorities.h>
#include <smooth/core/network/IPv4.h>
Expand Down Expand Up @@ -80,7 +82,36 @@ namespace access_point

insecure_server->start(max_client_count, listen_backlog, std::make_shared<IPv4>("0.0.0.0", 8080));

auto hello_world = [](
std::string str_response = "";

std::array<uint8_t, 6> mac;

if (wifi.get_local_mac_address(mac))
{
Log::info("Tag", "loacal MAC: {:X}:{:X}:{:X}:{:X}:{:X}:{:X}", mac[0], mac[1], mac[2], mac[3], mac[4],
mac[5]);
}

uint32_t ip = static_cast<uint32_t>(wifi.get_local_ip().addr);

if (ip)
{
Log::info("Tag",
"loacal IP: {}.{}.{}.{}",
ip >> 24,
(ip & 0x00FF0000) >> 16,
(ip & 0x0000FF00) >> 8,
ip & 0xFF);
}

std::string MAC = fmt::format("{:X}:{:X}:{:X}:{:X}:{:X}:{:X}", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
std::string IP =
fmt::format("{}.{}.{}.{}", ip >> 24, (ip & 0x00FF0000) >> 16, (ip & 0x0000FF00) >> 8, ip & 0xFF);
str_response = "<HTML><HEAD><TITLE>Hello World!</TITLE></HEAD><BODY><H1>Hello World!</H1>";
str_response += "Access point IP is: " + IP + "<br>";
str_response += "Access point MAC is:" + MAC + "</BODY></HTML>";

auto hello_world = [str_response](
IServerResponse& response,
IConnectionTimeoutModifier& /*timeout_modifier*/,
const std::string& /*url*/,
Expand All @@ -94,7 +125,7 @@ namespace access_point
{
response.reply(std::make_unique<responses::StringResponse>(
ResponseCode::OK,
"<HTML><HEAD><TITLE>Hello World!</TITLE></HEAD><BODY><H1>Hello World!</H1></BODY></HTML>"),
str_response),
false);
}
};
Expand Down