mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 09:37:21 +02:00
Add basic wifi support
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
idf_component_register(SRCS "rpc.cpp" "TCPClient.cpp"
|
||||
PRIV_REQUIRES esp_netif
|
||||
INCLUDE_DIRS "include")
|
||||
idf_component_register(SRCS "rpc.cpp" "TCPServer.cpp" "WifiManager.cpp"
|
||||
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi
|
||||
INCLUDE_DIRS "include")
|
||||
|
||||
3
components/rpc/DiscoveryService.cpp
Normal file
3
components/rpc/DiscoveryService.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
4
components/rpc/MessagingInterface.cpp
Normal file
4
components/rpc/MessagingInterface.cpp
Normal file
@@ -0,0 +1,4 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
3
components/rpc/RPCFactory.cpp
Normal file
3
components/rpc/RPCFactory.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
164
components/rpc/WifiManager.cpp
Normal file
164
components/rpc/WifiManager.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#include "WifiManager.h"
|
||||
|
||||
#include <esp_netif.h>
|
||||
#include <esp_event.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_wifi.h"
|
||||
|
||||
int WifiManager::init() {
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
printf("finished esp init");
|
||||
|
||||
this->m_mutex = xSemaphoreCreateMutex();
|
||||
this->m_state = wifi_state::connect;
|
||||
this->m_attempts = 0;
|
||||
|
||||
printf("finished variable init");
|
||||
|
||||
printf("lambda");
|
||||
|
||||
xTaskCreate(reinterpret_cast<TaskFunction_t>(s_manage), "wifi_task", 4096, this, 5, &m_task);
|
||||
|
||||
printf("task create");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WifiManager::connect() {
|
||||
this->update_state(wifi_state::connect);
|
||||
vTaskResume(this->m_task);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WifiManager::disconnect() {
|
||||
this->update_state(wifi_state::disconnect);
|
||||
vTaskResume(this->m_task);
|
||||
return 0;
|
||||
}
|
||||
|
||||
[[noreturn]] void WifiManager::manage() {
|
||||
while (true) {
|
||||
xSemaphoreTake(this->m_mutex, portMAX_DELAY);
|
||||
this->m_attempts++;
|
||||
const auto state = this->m_state;
|
||||
xSemaphoreGive(this->m_mutex);
|
||||
|
||||
switch (state) {
|
||||
case wifi_state::connect:
|
||||
printf("connect state\n");
|
||||
init_connection();
|
||||
update_state(wifi_state::connecting);
|
||||
break;
|
||||
case wifi_state::connecting:
|
||||
printf("connecting state\n");
|
||||
handle_connecting();
|
||||
break;
|
||||
case wifi_state::searching:
|
||||
printf("searching state\n");
|
||||
handle_search();
|
||||
break;
|
||||
case wifi_state::disconnect:
|
||||
printf("disconnect state\n");
|
||||
handle_disconnect();
|
||||
update_state(wifi_state::disconnected);
|
||||
break;
|
||||
default:
|
||||
printf("sleep state\n");
|
||||
vTaskSuspend(nullptr);
|
||||
break;
|
||||
}
|
||||
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
[[noreturn]] void WifiManager::s_manage(WifiManager *that) {
|
||||
that->manage();
|
||||
}
|
||||
|
||||
int WifiManager::init_connection() {
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_wifi_init(&cfg);
|
||||
|
||||
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, this);
|
||||
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, this);
|
||||
|
||||
// TEMP: get config from nvs key value store
|
||||
wifi_config_t wifi_configuration;
|
||||
wifi_configuration = {
|
||||
.sta = {
|
||||
.ssid = "dlink-C32D",
|
||||
.password = "",
|
||||
}
|
||||
};
|
||||
|
||||
esp_wifi_set_config(static_cast<wifi_interface_t>(ESP_IF_WIFI_STA), &wifi_configuration);
|
||||
|
||||
esp_wifi_start();
|
||||
esp_wifi_set_mode(WIFI_MODE_STA);
|
||||
esp_wifi_connect();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WifiManager::handle_connecting() {
|
||||
if (m_attempts > 10) {
|
||||
handle_disconnect();
|
||||
update_state(wifi_state::searching);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WifiManager::handle_disconnect() {
|
||||
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler);
|
||||
|
||||
esp_wifi_scan_stop();
|
||||
esp_wifi_disconnect();
|
||||
esp_wifi_stop();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WifiManager::handle_search() {
|
||||
printf("Waiting for configuration");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WifiManager::update_state(wifi_state state) {
|
||||
xSemaphoreTake(this->m_mutex, portMAX_DELAY);
|
||||
this->m_attempts = 0;
|
||||
this->m_state = state;
|
||||
xSemaphoreGive(this->m_mutex);
|
||||
}
|
||||
|
||||
void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
|
||||
// Passed in as a parameter since c (freertos) cannot call the member function directly.
|
||||
const auto that = static_cast<WifiManager *>(event_handler_arg);
|
||||
|
||||
if (WIFI_EVENT_STA_START == event_id) {
|
||||
printf("Connecting to wifi\n");
|
||||
} else if (WIFI_EVENT_STA_CONNECTED == event_id) {
|
||||
printf("Connected to wifi\n");
|
||||
that->update_state(wifi_state::connected);
|
||||
} else if (WIFI_EVENT_STA_DISCONNECTED == event_id) {
|
||||
printf("Disconnected from wifi\n");
|
||||
xSemaphoreTake(that->m_mutex, portMAX_DELAY);
|
||||
if (that->m_state == wifi_state::connected) {
|
||||
xSemaphoreGive(that->m_mutex);
|
||||
that->handle_disconnect();
|
||||
that->update_state(wifi_state::connect);
|
||||
} else {
|
||||
xSemaphoreGive(that->m_mutex);
|
||||
}
|
||||
} else if (IP_EVENT_STA_GOT_IP == event_id) {
|
||||
printf("Got IP \n");
|
||||
}
|
||||
}
|
||||
16
components/rpc/include/CommunicationRouter.h
Normal file
16
components/rpc/include/CommunicationRouter.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef COMMUNICATIONROUTER_H
|
||||
#define COMMUNICATIONROUTER_H
|
||||
|
||||
|
||||
|
||||
class CommunicationRouter {
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //COMMUNICATIONROUTER_H
|
||||
8
components/rpc/include/DiscoveryService.h
Normal file
8
components/rpc/include/DiscoveryService.h
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef DISCOVERYSERVICE_H
|
||||
#define DISCOVERYSERVICE_H
|
||||
|
||||
#endif //DISCOVERYSERVICE_H
|
||||
8
components/rpc/include/ICommunicationRouter.h
Normal file
8
components/rpc/include/ICommunicationRouter.h
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef ICOMMUNICATIONROUTER_H
|
||||
#define ICOMMUNICATIONROUTER_H
|
||||
|
||||
#endif //ICOMMUNICATIONROUTER_H
|
||||
8
components/rpc/include/IMessagingInterface.h
Normal file
8
components/rpc/include/IMessagingInterface.h
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef IMESSAGINGINTERFACE_H
|
||||
#define IMESSAGINGINTERFACE_H
|
||||
|
||||
#endif //IMESSAGINGINTERFACE_H
|
||||
8
components/rpc/include/IRPCServer.h
Normal file
8
components/rpc/include/IRPCServer.h
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef IRPCSERVER_H
|
||||
#define IRPCSERVER_H
|
||||
|
||||
#endif //IRPCSERVER_H
|
||||
16
components/rpc/include/MessagingInterface.h
Normal file
16
components/rpc/include/MessagingInterface.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef MESSAGINGINTERFACE_H
|
||||
#define MESSAGINGINTERFACE_H
|
||||
|
||||
|
||||
|
||||
class MessagingInterface {
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //MESSAGINGINTERFACE_H
|
||||
8
components/rpc/include/RPCFactory.h
Normal file
8
components/rpc/include/RPCFactory.h
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef RPCFACTORY_H
|
||||
#define RPCFACTORY_H
|
||||
|
||||
#endif //RPCFACTORY_H
|
||||
8
components/rpc/include/TCPServer.h
Normal file
8
components/rpc/include/TCPServer.h
Normal file
@@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-05-25.
|
||||
//
|
||||
|
||||
#ifndef TCPSERVER_H
|
||||
#define TCPSERVER_H
|
||||
|
||||
#endif //TCPSERVER_H
|
||||
39
components/rpc/include/WifiManager.h
Normal file
39
components/rpc/include/WifiManager.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef NETWORKMANAGER_H
|
||||
#define NETWORKMANAGER_H
|
||||
|
||||
#include "esp_event.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
class WifiManager {
|
||||
public:
|
||||
WifiManager() = default;
|
||||
~WifiManager() = default;
|
||||
int init();
|
||||
int connect();
|
||||
int disconnect();
|
||||
|
||||
[[noreturn]] void manage();
|
||||
|
||||
enum class wifi_state { connect, connected, disconnected, connecting, disconnect, searching };
|
||||
|
||||
private:
|
||||
void update_state(wifi_state state);
|
||||
[[noreturn]] static void s_manage(WifiManager *that);
|
||||
int init_connection();
|
||||
int handle_connecting();
|
||||
int handle_disconnect();
|
||||
int handle_search();
|
||||
|
||||
static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
|
||||
|
||||
SemaphoreHandle_t m_mutex;
|
||||
wifi_state m_state;
|
||||
TaskHandle_t m_task;
|
||||
unsigned int m_attempts;
|
||||
|
||||
};
|
||||
|
||||
#endif //NETWORKMANAGER_H
|
||||
Reference in New Issue
Block a user