69 lines
2.3 KiB
C++
69 lines
2.3 KiB
C++
#ifndef _WIFI_STATION_H_
|
|
#define _WIFI_STATION_H_
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <functional>
|
|
|
|
#include <esp_event.h>
|
|
#include <esp_timer.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);
|
|
void OnNoCandidates(std::function<void()> on_no_candidates);// 没有候选AP时调用
|
|
void OnReconnectTimeout(std::function<void()> on_reconnect_timeout);
|
|
|
|
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;
|
|
std::string ssid_;
|
|
std::string password_;
|
|
std::string ip_address_;
|
|
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::function<void()> on_no_candidates_;
|
|
std::function<void()> on_reconnect_timeout_;
|
|
std::vector<WifiApRecord> connect_queue_;
|
|
esp_timer_handle_t reconnect_timer_handle_ = nullptr;
|
|
|
|
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_
|