Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix and additional functionality for Wifi.cpp #114

Merged
merged 9 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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)|
47 changes: 45 additions & 2 deletions lib/smooth/core/network/Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace smooth::core::network
{
Wifi::Wifi()
{
this->ip.addr = 0;
PerMalmberg marked this conversation as resolved.
Show resolved Hide resolved
tcpip_adapter_init();
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &Wifi::wifi_event_callback, this);
esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &Wifi::wifi_event_callback, this);
Expand Down Expand Up @@ -128,6 +129,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 +141,12 @@ namespace smooth::core::network
}
else if (event_id == WIFI_EVENT_AP_START)
{
wifi->ip.addr = 0xC0A80401; // 192.168.4.1
PerMalmberg marked this conversation as resolved.
Show resolved Hide 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,9 +185,11 @@ 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);
}
}
Expand All @@ -193,9 +199,10 @@ namespace smooth::core::network
{
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 +218,40 @@ 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;
}

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 Expand Up @@ -244,4 +285,6 @@ namespace smooth::core::network
ip_changed);
core::ipc::Publisher<network::NetworkStatus>::publish(status);
}

ip4_addr_t Wifi::ip;
}
5 changes: 5 additions & 0 deletions lib/smooth/include/smooth/core/network/Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ namespace smooth::core::network

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

[[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
void start_softap(uint8_t max_conn = 1);
Expand All @@ -89,5 +93,6 @@ namespace smooth::core::network
std::string ssid{};

std::string password{};
static ip4_addr_t ip;
PerMalmberg marked this conversation as resolved.
Show resolved Hide 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);