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

Implement Smart Config for ESP32 #2474

Merged
merged 10 commits into from
Jan 27, 2022
55 changes: 2 additions & 53 deletions Sming/Arch/Esp32/Components/esp32/src/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,12 @@
#include <driver/uart.h>
#include <Storage.h>

#ifndef DISABLE_NETWORK
#include <esp_netif.h>
#ifndef DISABLE_WIFI
#include <esp_wifi.h>
#include <nvs_flash.h>
#endif
#endif

extern void init();
extern esp_event_loop_handle_t sming_create_event_loop();
extern void esp_network_initialise();

namespace
{
#ifndef DISABLE_WIFI
esp_event_handler_t wifiEventHandler;

/*
* Initialise NVS which IDF WiFi uses to store configuration parameters.
*/
void esp_init_nvs()
{
esp_err_t ret = nvs_flash_init();
if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
}

/*
* Initialise default WiFi stack
*/
void esp_init_wifi()
{
esp_netif_init();
if(wifiEventHandler != nullptr) {
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifiEventHandler, nullptr));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifiEventHandler, nullptr));
}
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
}
#endif

void main(void*)
{
assert(esp_task_wdt_init(CONFIG_ESP_TASK_WDT_TIMEOUT_S, true) == ESP_OK);
Expand All @@ -77,8 +39,7 @@ void main(void*)
auto loop = sming_create_event_loop();

#ifndef DISABLE_WIFI
esp_init_nvs();
esp_init_wifi();
esp_network_initialise();
#endif

System.initialize();
Expand All @@ -94,18 +55,6 @@ void main(void*)

} // namespace

/*
* Called from WiFi event implementation constructor.
* Cannot register directly as event queue hasn't been created yet.
* NOTE: May only be called once.
*/
void wifi_set_event_handler_cb(esp_event_handler_t eventHandler)
{
#ifndef DISABLE_WIFI
wifiEventHandler = eventHandler;
#endif
}

extern void sming_create_task(TaskFunction_t);

extern "C" void app_main(void)
Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Esp32/Components/libc/src/replacements.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ssize_t WRAP(_read_r)(struct _reent* r, int fd, void* dst, size_t size)
return -1;
}

size_t WRAP(putc)(char c)
size_t WRAP(putchar)(char c)
{
return m_putc(c);
}
Expand Down
15 changes: 10 additions & 5 deletions Sming/Components/Network/Arch/Esp32/Platform/AccessPointImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

#include "AccessPointImpl.h"
#include "StationListImpl.h"
#include <esp_netif.h>
#include <esp_wifi.h>
#include <esp_system.h>

static AccessPointImpl accessPoint;
AccessPointClass& WifiAccessPoint = accessPoint;
AccessPointClass& WifiAccessPoint{SmingInternal::Network::accessPoint};

namespace SmingInternal
{
namespace Network
{
AccessPointImpl accessPoint;

void AccessPointImpl::enable(bool enabled, bool save)
{
Expand Down Expand Up @@ -193,3 +195,6 @@ std::unique_ptr<StationList> AccessPointImpl::getStations() const
void AccessPointImpl::onSystemReady()
{
}

} // namespace Network
} // namespace SmingInternal
15 changes: 15 additions & 0 deletions Sming/Components/Network/Arch/Esp32/Platform/AccessPointImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@

#include <Platform/AccessPoint.h>
#include <Platform/System.h>
#include <esp_wifi.h>

struct esp_netif_obj;

namespace SmingInternal
{
namespace Network
{
class AccessPointImpl : public AccessPointClass, protected ISystemReadyHandler
{
public:
Expand All @@ -38,9 +43,19 @@ class AccessPointImpl : public AccessPointClass, protected ISystemReadyHandler
String getPassword() const override;
std::unique_ptr<StationList> getStations() const override;

// Called from WifiEventsImpl
void eventHandler(esp_event_base_t base, int32_t id, void* data)
{
}

protected:
void onSystemReady() override;

private:
esp_netif_obj* apNetworkInterface{nullptr};
};

extern AccessPointImpl accessPoint;

} // namespace Network
} // namespace SmingInternal
Loading