提交 1736ffdf authored 作者: blu's avatar blu

new feature: evwifi

上级 4bc7ffe5
...@@ -35,11 +35,13 @@ using namespace httplib; ...@@ -35,11 +35,13 @@ using namespace httplib;
class WifiMgr { class WifiMgr {
private: private:
json info; json info;
string devSn;
Server srv; Server srv;
promise<int> p; promise<int> p;
thread monitor; thread monitor;
json wifiData; json wifiData;
int mode, lastMode; // 1: ap; 2: ste int mode, lastMode; // 0:no; network 1: ap; 2: ste
mutex mutMode;
const string apdCfgPath = "/etc/apd.conf"; const string apdCfgPath = "/etc/apd.conf";
const string wpaCfgPath = "/etc/wpa_supplicant/wpa_supplicant-wlan1.conf"; const string wpaCfgPath = "/etc/wpa_supplicant/wpa_supplicant-wlan1.conf";
...@@ -60,8 +62,7 @@ class WifiMgr { ...@@ -60,8 +62,7 @@ class WifiMgr {
if( mode == 1) { if( mode == 1) {
// ap // ap
// stop all spdlog::info("evwifi {} prepare to enter AP mode", devSn);
spdlog::info("prepare to enter AP mode");
// exec("systemctl dsiable wpa_supplicant@wlan1 ") // exec("systemctl dsiable wpa_supplicant@wlan1 ")
string apdContent = fmt::format("interface=wlan1\ndriver=nl80211\nssid=EVB-{}\nhw_mode=g\n" string apdContent = fmt::format("interface=wlan1\ndriver=nl80211\nssid=EVB-{}\nhw_mode=g\n"
"channel=6\nmacaddr_acl=0\nignore_broadcast_ssid=0\nwpa=0\n", this->info["sn"].get<string>()); "channel=6\nmacaddr_acl=0\nignore_broadcast_ssid=0\nwpa=0\n", this->info["sn"].get<string>());
...@@ -72,23 +73,24 @@ class WifiMgr { ...@@ -72,23 +73,24 @@ class WifiMgr {
// start hostapd // start hostapd
auto t = thread([](){ auto t = thread([](){
system("pkill hostapd;systemctl stop wpa_supplicant@wlan1;ifconfig wlan1 down;" system("pkill hostapd;systemctl stop wpa_supplicant@wlan1;ifconfig wlan1 down;"
"ifconfig wlan1 up;ifconfig wlan1 192.168.0.1;hostapd /etc/apd.conf -B"); "ifconfig wlan1 up;ifconfig wlan1 192.168.0.1;hostapd /etc/apd.conf -B");
// TODO: check result // TODO: check result
}); });
t.detach(); t.detach();
}else{ }else{
ret["code"] = 1; ret["code"] = 1;
string msg = fmt::format("failed to write ap config file to {}", apdCfgPath); string msg = fmt::format("failed to write ap config file to {}", apdCfgPath);
spdlog::error(msg); spdlog::error("evwifi {} {}", devSn,msg);
ret["msg"] = msg; ret["msg"] = msg;
} }
}else if(mode == 2) { }else if(mode == 2) {
// station mode // station mode
spdlog::info("prepare to enter Station mode"); spdlog::info("evwifi {} prepare to enter Station mode", devSn);
if( wifiData["wifi"].count("ssid") == 0 || wifiData["wifi"]["ssid"].size() == 0 || if( wifiData["wifi"].count("ssid") == 0 || wifiData["wifi"]["ssid"].size() == 0 ||
wifiData["wifi"].count("password") == 0 || wifiData["wifi"]["password"].size() == 0) { wifiData["wifi"].count("password") == 0 || wifiData["wifi"]["password"].size() == 0) {
string msg = fmt::format("no valid ssid/password provided"); string msg = fmt::format("no valid ssid/password provided");
spdlog::error(msg); spdlog::error("evwifi {} {}", devSn, msg);
ret["msg"] = msg; ret["msg"] = msg;
ret["code"] = 3; ret["code"] = 3;
} }
...@@ -100,18 +102,21 @@ class WifiMgr { ...@@ -100,18 +102,21 @@ class WifiMgr {
wpaFile << wpaContent; wpaFile << wpaContent;
wpaFile.close(); wpaFile.close();
// TODO: verify // TODO: verify
auto t = thread([](){ auto t = thread([this](){
// delay for rest return (ifdown caused no networking available) // delay for rest return (ifdown caused no networking available)
this_thread::sleep_for(chrono::seconds(1)); this_thread::sleep_for(chrono::seconds(1));
system("pkill hostapd; pkill dhclient;systemctl enable wpa_supplicant@wlan1;systemctl restart wpa_supplicant@wlan1;" system("pkill hostapd; pkill dhclient;systemctl enable wpa_supplicant@wlan1;systemctl restart wpa_supplicant@wlan1;"
"/sbin/ifdown -a --read-environment;/sbin/ifup -a --read-environment;systemctl restart evdaemon"); "/sbin/ifdown -a --read-environment");
auto s = exec("/sbin/ifup -a --read-environment");
spdlog::info("evwifi {} ifup: \n{}", this->devSn, s);
system("systemctl restart evdaemon");
}); });
t.detach(); t.detach();
}else{ }else{
string msg = fmt::format("failed write wpa config to {}", wpaCfgPath); string msg = fmt::format("failed write wpa config to {}", wpaCfgPath);
ret["code"] = 2; ret["code"] = 2;
ret["msg"] = msg; ret["msg"] = msg;
spdlog::error(msg); spdlog::error("evwifi {} {}", devSn, msg);
} }
} }
} }
...@@ -122,6 +127,8 @@ class WifiMgr { ...@@ -122,6 +127,8 @@ class WifiMgr {
public: public:
WifiMgr(){ WifiMgr(){
LVDB::getSn(this->info); LVDB::getSn(this->info);
devSn = this->info["sn"];
mode = 0;
wifiData["info"] = this->info; wifiData["info"] = this->info;
wifiData["wifi"] = json(); wifiData["wifi"] = json();
wifiData["wifi"]["ssids"] = json(); wifiData["wifi"]["ssids"] = json();
...@@ -134,15 +141,23 @@ class WifiMgr { ...@@ -134,15 +141,23 @@ class WifiMgr {
// get wlan1 ip // get wlan1 ip
// ping outside address // ping outside address
// default is AP mode // this->lastMode = 0;
this->lastMode = 0;
while(1){ while(1){
// // check modes // check wifi interface
// this->mode = 1; {
// if(this->lastMode != this->mode) { lock_guard<mutex> lk(mutMode);
// enableMode(this->mode); auto s = exec("ifconfig wlan1|grep -v inet6|grep inet");
// } if(s.empty() && this->mode != 1) {
// this->lastMode = this->mode; spdlog::info("evwifi {} detects no wifi connection, enabling AP mode", this->devSn);
this->enableMode(1);
this->mode = 1;
this->scanWifi();
}
// TODO: flash light
}
this_thread::sleep_for(chrono::seconds(10)); this_thread::sleep_for(chrono::seconds(10));
} }
}); });
...@@ -168,7 +183,7 @@ class WifiMgr { ...@@ -168,7 +183,7 @@ class WifiMgr {
string password = req.get_param_value("password"); string password = req.get_param_value("password");
if(ssid.empty()||password.empty()){ if(ssid.empty()||password.empty()){
string msg = fmt::format("no valid ssid/password provided"); string msg = fmt::format("no valid ssid/password provided");
spdlog::error(msg); spdlog::error("evwifi {} {}", devSn, msg);
ret["msg"] = msg; ret["msg"] = msg;
ret["code"] = 3; ret["code"] = 3;
}else{ }else{
...@@ -185,7 +200,7 @@ class WifiMgr { ...@@ -185,7 +200,7 @@ class WifiMgr {
string msg = fmt::format("exception in convert mode {} to int:{}", mode, e.what()); string msg = fmt::format("exception in convert mode {} to int:{}", mode, e.what());
ret["code"] = -1; ret["code"] = -1;
ret["msg"] = msg; ret["msg"] = msg;
spdlog::error(msg); spdlog::error("evwifi {} {}", devSn, msg);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论