提交 63b4c13f authored 作者: blu's avatar blu

new feature: evwifi

上级 2f33bdea
...@@ -76,4 +76,4 @@ add_executable(evmlmotion evmlmotion.cpp) ...@@ -76,4 +76,4 @@ add_executable(evmlmotion evmlmotion.cpp)
target_link_libraries(evmlmotion PUBLIC ${COMM_LIBS} ${AV_LIBS} ${CV_LIBRARIES} ${EXTRA_LIBS}) target_link_libraries(evmlmotion PUBLIC ${COMM_LIBS} ${AV_LIBS} ${CV_LIBRARIES} ${EXTRA_LIBS})
add_executable(evwifi evwifi.cpp) add_executable(evwifi evwifi.cpp)
target_link_libraries(evwifi PUBLIC database leveldb) target_link_libraries(evwifi PUBLIC database leveldb pthread)
\ No newline at end of file \ No newline at end of file
...@@ -38,9 +38,7 @@ class WifiMgr { ...@@ -38,9 +38,7 @@ class WifiMgr {
Server srv; Server srv;
promise<int> p; promise<int> p;
thread monitor; thread monitor;
vector<string> ssids; json wifiData;
string wifiSSID;
string wifiPasswd;
int mode, lastMode; // 1: ap; 2: ste int mode, lastMode; // 1: ap; 2: ste
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";
...@@ -68,30 +66,33 @@ class WifiMgr { ...@@ -68,30 +66,33 @@ class WifiMgr {
/// scan wifis /// scan wifis
string res = exec("iwlist wlan1 scan|grep ESSID"); string res = exec("iwlist wlan1 scan|grep ESSID");
ssids.clear(); wifiData["wifi"]["ssids"].clear();
httplib::detail::split(&res[0], &res[res.size()], '\n', [&](const char *b, const char *e) { httplib::detail::split(&res[0], &res[res.size()], '\n', [&](const char *b, const char *e) {
string ssid; string ssid;
ssid.assign(b,e); ssid.assign(b,e);
ssids.push_back(ssid); wifiData["wifi"]["ssids"].push_back(ssid);
}); });
//
}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(msg);
ret["msg"] = msg; ret["msg"] = msg;
return ret;
} }
}else if(mode == 2) { }else if(mode == 2) {
// station mode // station mode
spdlog::info("prepare to enter Station mode"); spdlog::info("prepare to enter Station mode");
if( wifiData["wifi"].count("ssid") == 0 || wifiData["wifi"]["ssid"].size() == 0 ||
wifiData["wifi"].count("password") == 0 || wifiData["wifi"]["password"].size() == 0) {
string msg = fmt::format("no valid ssid/password provided");
spdlog::error(msg);
ret["msg"] = msg;
ret["code"] = 3;
}
else{
// stop hostapd // stop hostapd
exec("pkill hostapd"); exec("pkill hostapd");
string wpaContent = fmt::format("ctrl_interface=/run/wpa_supplicant\nupdate_config=1\nap_scan=1\n" string wpaContent = fmt::format("ctrl_interface=/run/wpa_supplicant\nupdate_config=1\nap_scan=1\n"
"network={{\nssid=\"{}\"\npsk=\"{}\"\n}}\n", this->wifiSSID, this->wifiPasswd); "network={{\nssid=\"{}\"\npsk=\"{}\"\n}}\n", this->wifiData["wifi"]["ssid"].get<string>(), this->wifiData["wifi"]["password"].get<string>());
ofstream wpaFile(wpaCfgPath, ios::out|ios::trunc); ofstream wpaFile(wpaCfgPath, ios::out|ios::trunc);
if(wpaFile.is_open()){ if(wpaFile.is_open()){
wpaFile << wpaContent; wpaFile << wpaContent;
...@@ -105,10 +106,11 @@ class WifiMgr { ...@@ -105,10 +106,11 @@ class WifiMgr {
ret["code"] = 2; ret["code"] = 2;
ret["msg"] = msg; ret["msg"] = msg;
spdlog::error(msg); spdlog::error(msg);
return ret;
} }
} }
}
ret["wifiData"] = wifiData;
return ret; return ret;
} }
...@@ -116,6 +118,12 @@ class WifiMgr { ...@@ -116,6 +118,12 @@ class WifiMgr {
public: public:
WifiMgr(){ WifiMgr(){
LVDB::getSn(this->info); LVDB::getSn(this->info);
wifiData["sn"] = this->info;
wifiData["wifi"] = json();
wifiData["wifi"]["ssids"] = json();
//wifiData["wifi"]["ssid"] = string;
//wifiData["wifi"]["password"] = string;
monitor = thread([this](){ monitor = thread([this](){
// check /etc/systemd/wpa_supplicant@wlan1.service // check /etc/systemd/wpa_supplicant@wlan1.service
...@@ -126,12 +134,12 @@ class WifiMgr { ...@@ -126,12 +134,12 @@ class WifiMgr {
// default is AP mode // default is AP mode
this->lastMode = 0; this->lastMode = 0;
while(1){ while(1){
// check modes // // check modes
this->mode = 1; // this->mode = 1;
if(this->lastMode != this->mode) { // if(this->lastMode != this->mode) {
enableMode(this->mode); // enableMode(this->mode);
} // }
this->lastMode = this->mode; // this->lastMode = this->mode;
this_thread::sleep_for(chrono::seconds(10)); this_thread::sleep_for(chrono::seconds(10));
} }
}); });
...@@ -147,7 +155,24 @@ class WifiMgr { ...@@ -147,7 +155,24 @@ class WifiMgr {
if(!mode.empty()){ if(!mode.empty()){
try{ try{
auto i = stoi(mode); auto i = stoi(mode);
if(i == 2) {
string ssid = req.get_param_value("ssid");
string password = req.get_param_value("password");
if(ssid.empty()||password.empty()){
string msg = fmt::format("no valid ssid/password provided");
spdlog::error(msg);
ret["msg"] = msg;
ret["code"] = 3;
}else{
this->wifiData["wifi"]["ssid"] = ssid;
this->wifiData["wifi"]["password"] = password;
}
}
if(ret["coce"] == 0) {
ret = this->enableMode(i); ret = this->enableMode(i);
}
}catch(exception &e){ }catch(exception &e){
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;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论