add some code
This commit is contained in:
22
managed_components/78__esp-wifi-connect/include/dns_server.h
Normal file
22
managed_components/78__esp-wifi-connect/include/dns_server.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef _DNS_SERVER_H_
|
||||
#define _DNS_SERVER_H_
|
||||
|
||||
#include <string>
|
||||
#include <esp_netif_ip_addr.h>
|
||||
|
||||
class DnsServer {
|
||||
public:
|
||||
DnsServer();
|
||||
~DnsServer();
|
||||
|
||||
void Start(esp_ip4_addr_t gateway);
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
int port_ = 53;
|
||||
int fd_ = -1;
|
||||
esp_ip4_addr_t gateway_;
|
||||
void Run();
|
||||
};
|
||||
|
||||
#endif // _DNS_SERVER_H_
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef SSID_MANAGER_H
|
||||
#define SSID_MANAGER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct SsidItem {
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
};
|
||||
|
||||
class SsidManager {
|
||||
public:
|
||||
static SsidManager& GetInstance() {
|
||||
static SsidManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void AddSsid(const std::string& ssid, const std::string& password);
|
||||
void RemoveSsid(int index);
|
||||
void SetDefaultSsid(int index);
|
||||
void Clear();
|
||||
const std::vector<SsidItem>& GetSsidList() const { return ssid_list_; }
|
||||
|
||||
private:
|
||||
SsidManager();
|
||||
~SsidManager();
|
||||
|
||||
void LoadFromNvs();
|
||||
void SaveToNvs();
|
||||
|
||||
std::vector<SsidItem> ssid_list_;
|
||||
};
|
||||
|
||||
#endif // SSID_MANAGER_H
|
||||
@@ -0,0 +1,69 @@
|
||||
#ifndef _WIFI_CONFIGURATION_AP_H_
|
||||
#define _WIFI_CONFIGURATION_AP_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#include <esp_http_server.h>
|
||||
#include <esp_event.h>
|
||||
#include <esp_timer.h>
|
||||
#include <esp_netif.h>
|
||||
#include <esp_wifi_types_generic.h>
|
||||
|
||||
#include "dns_server.h"
|
||||
|
||||
class WifiConfigurationAp {
|
||||
public:
|
||||
static WifiConfigurationAp& GetInstance();
|
||||
void SetSsidPrefix(const std::string &&ssid_prefix);
|
||||
void SetLanguage(const std::string &&language);
|
||||
void Start();
|
||||
void Stop();
|
||||
void StartSmartConfig();
|
||||
bool ConnectToWifi(const std::string &ssid, const std::string &password);
|
||||
void Save(const std::string &ssid, const std::string &password);
|
||||
std::vector<wifi_ap_record_t> GetAccessPoints();
|
||||
std::string GetSsid();
|
||||
std::string GetWebServerUrl();
|
||||
|
||||
// Delete copy constructor and assignment operator
|
||||
WifiConfigurationAp(const WifiConfigurationAp&) = delete;
|
||||
WifiConfigurationAp& operator=(const WifiConfigurationAp&) = delete;
|
||||
|
||||
private:
|
||||
// Private constructor
|
||||
WifiConfigurationAp();
|
||||
~WifiConfigurationAp();
|
||||
|
||||
std::mutex mutex_;
|
||||
DnsServer dns_server_;
|
||||
httpd_handle_t server_ = NULL;
|
||||
EventGroupHandle_t event_group_;
|
||||
std::string ssid_prefix_;
|
||||
std::string language_;
|
||||
esp_event_handler_instance_t instance_any_id_;
|
||||
esp_event_handler_instance_t instance_got_ip_;
|
||||
esp_timer_handle_t scan_timer_ = nullptr;
|
||||
bool is_connecting_ = false;
|
||||
esp_netif_t* ap_netif_ = nullptr;
|
||||
std::vector<wifi_ap_record_t> ap_records_;
|
||||
|
||||
// 高级配置项
|
||||
std::string ota_url_;
|
||||
int8_t max_tx_power_;
|
||||
bool remember_bssid_;
|
||||
bool sleep_mode_;
|
||||
|
||||
void StartAccessPoint();
|
||||
void StartWebServer();
|
||||
|
||||
// Event handlers
|
||||
static void WifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
static void IpEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
static void SmartConfigEventHandler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data);
|
||||
esp_event_handler_instance_t sc_event_instance_ = nullptr;
|
||||
};
|
||||
|
||||
#endif // _WIFI_CONFIGURATION_AP_H_
|
||||
@@ -0,0 +1,67 @@
|
||||
#ifndef _WIFI_STATION_H_
|
||||
#define _WIFI_STATION_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <esp_event.h>
|
||||
#include <esp_timer.h>
|
||||
#include <esp_netif.h>
|
||||
#include <esp_wifi_types_generic.h>
|
||||
|
||||
struct WifiApRecord {
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
int channel;
|
||||
wifi_auth_mode_t authmode;
|
||||
uint8_t bssid[6];
|
||||
};
|
||||
|
||||
class WifiStation {
|
||||
public:
|
||||
static WifiStation& GetInstance();
|
||||
void AddAuth(const std::string &&ssid, const std::string &&password);
|
||||
void Start();
|
||||
void Stop();
|
||||
bool IsConnected();
|
||||
bool WaitForConnected(int timeout_ms = 10000);
|
||||
int8_t GetRssi();
|
||||
std::string GetSsid() const { return ssid_; }
|
||||
std::string GetIpAddress() const { return ip_address_; }
|
||||
uint8_t GetChannel();
|
||||
void SetPowerSaveMode(bool enabled);
|
||||
|
||||
void OnConnect(std::function<void(const std::string& ssid)> on_connect);
|
||||
void OnConnected(std::function<void(const std::string& ssid)> on_connected);
|
||||
void OnScanBegin(std::function<void()> on_scan_begin);
|
||||
|
||||
private:
|
||||
WifiStation();
|
||||
~WifiStation();
|
||||
WifiStation(const WifiStation&) = delete;
|
||||
WifiStation& operator=(const WifiStation&) = delete;
|
||||
|
||||
EventGroupHandle_t event_group_;
|
||||
esp_timer_handle_t timer_handle_ = nullptr;
|
||||
esp_event_handler_instance_t instance_any_id_ = nullptr;
|
||||
esp_event_handler_instance_t instance_got_ip_ = nullptr;
|
||||
esp_netif_t* station_netif_ = nullptr;
|
||||
std::string ssid_;
|
||||
std::string password_;
|
||||
std::string ip_address_;
|
||||
int8_t max_tx_power_;
|
||||
uint8_t remember_bssid_;
|
||||
int reconnect_count_ = 0;
|
||||
std::function<void(const std::string& ssid)> on_connect_;
|
||||
std::function<void(const std::string& ssid)> on_connected_;
|
||||
std::function<void()> on_scan_begin_;
|
||||
std::vector<WifiApRecord> connect_queue_;
|
||||
|
||||
void HandleScanResult();
|
||||
void StartConnect();
|
||||
static void WifiEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
static void IpEventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
|
||||
};
|
||||
|
||||
#endif // _WIFI_STATION_H_
|
||||
Reference in New Issue
Block a user