WLG/wifi/wpa_client.h

82 lines
2.9 KiB
C
Raw Normal View History

2024-10-19 16:02:41 +08:00
/*
#
# wifi连接接口
# 说明通过进程间通信与wpa_supplicant进行交互实现wifi连接
# 注意使用该接口的应用运行前要确保wpa_supplicant应用已经启动
# 并且能明确进程间通信地址。
#
*/
#ifndef __WPA_CLIENT_H__
#define __WPA_CLIENT_H__
#include <string>
#include <sys/un.h>
#include "../common/SH_global.h"
2024-10-22 19:04:25 +08:00
namespace wifi {
2024-10-19 16:02:41 +08:00
#ifdef G2UL_GATEWAY
2024-10-22 19:04:25 +08:00
const std::string WPA_PATH = "/var/run/wpa_supplicant/wlan0"; //进程间通信地址加上网络接口额名称
2024-10-19 16:02:41 +08:00
#endif
#ifdef IMX6UL_GATEWAY
2024-10-22 19:04:25 +08:00
const std::string WPA_PATH = "/var/run/wpa_supplicant/wlan2"; //进程间通信地址加上网络接口额名称
2024-10-19 16:02:41 +08:00
#endif
2024-10-22 19:04:25 +08:00
struct WPAContext {
2024-10-19 16:02:41 +08:00
int s;
struct sockaddr_un local;
struct sockaddr_un dest;
};
2024-10-22 19:04:25 +08:00
enum ConnectStatus { STEP_SCAN = 0, STEP_CONNECT_AP_OK, STEP_DHCP_IP, STEP_SUCCESS };
class MXDHCP { // dhcp ip地址的工具类实现方法不算特别合理。可以根据具体情况进行更改
2024-10-19 16:02:41 +08:00
public:
MXDHCP();
~MXDHCP();
bool Start(const std::string& net_interface);
bool GetDHCPStatus();
2024-10-22 19:04:25 +08:00
2024-10-19 16:02:41 +08:00
private:
2024-10-22 19:04:25 +08:00
bool CheckString(char* buf, int len);
FILE* pstream;
2024-10-19 16:02:41 +08:00
};
2024-10-22 19:04:25 +08:00
class WPAClient {
2024-10-19 16:02:41 +08:00
public:
WPAClient(const std::string& wpa_control_path = WPA_PATH);
~WPAClient();
2024-10-22 19:04:25 +08:00
bool GetInitStatus() { return wpa_context_ != NULL; } //获取wpa进程间通信是否建立连接
int GetWiFiRssi(); //获取wifi信号强度需要在连接成功之后调用
std::string GetCurrentSSID(); //获取当前连接的wifi的名称
bool ConnectWiFi(const std::string& ssid, const std::string& password); //连接加密wifi传入wifi名称和密码
bool ConnectWiFiWithNoPassword(const std::string& ssid); //连接无加密的wifi传入wifi名称即可
bool ConnectWiFiWithLast(); //直接连接上次已保存的wifi
bool GetConnectStatus(); //获取wifi连接状态
2024-10-19 16:02:41 +08:00
std::string GetNetSsid();
2024-10-22 19:04:25 +08:00
void wifiup(); // mlan0 up
2024-10-19 16:02:41 +08:00
bool CleanWifi();
bool ReconnectWiFi(); //重新连接WIFI
protected:
bool Init();
struct WPAContext* Open(const std::string& path);
void Close(struct WPAContext* context);
2024-10-22 19:04:25 +08:00
bool Request(struct WPAContext* context, const std::string& cmd, std::string& reply);
2024-10-19 16:02:41 +08:00
bool CheckCommandWithOk(const std::string cmd);
bool AddWiFi(int& id);
bool SetScanSSID(int id);
bool SetSSID(const std::string& ssid, int id);
bool SetPassword(const std::string& password, int id);
bool SetProtocol(int id, int en_crypt);
bool CleanAllWiFi();
bool EnableWiFi(int id);
void SetConnStatus(ConnectStatus status);
2024-10-22 19:04:25 +08:00
int GetConnStatus();
2024-10-19 16:02:41 +08:00
protected:
2024-10-22 19:04:25 +08:00
struct WPAContext* wpa_context_;
2024-10-19 16:02:41 +08:00
std::string wpa_control_path_;
MXDHCP dhcp_;
2024-10-22 19:04:25 +08:00
2024-10-19 16:02:41 +08:00
private:
int step_;
};
}
#endif // __WPA_CLIENT_H__