From 1faa9b566c4e6e5789841c9fae715ca5fbbd55bc Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Tue, 30 Apr 2024 20:17:04 +0800 Subject: [PATCH] Improve:wifi hal. --- hal/src/WifiHal.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++- hal/src/WifiHal.h | 3 +++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/hal/src/WifiHal.cpp b/hal/src/WifiHal.cpp index 59274f2..235a9df 100644 --- a/hal/src/WifiHal.cpp +++ b/hal/src/WifiHal.cpp @@ -13,14 +13,58 @@ * limitations under the License. */ #include "WifiHal.h" -#include "LinuxApi.h" #include "ILog.h" +#include "LinuxApi.h" +#include +#include +#include +#include +#include StatusCode WifiHal::OpenApMode(void) { LogInfo("OpenApMode. \n"); + constexpr int SLEEP_TIME_MS = 5; + constexpr int WAITING_TIME_MS = 1000 * 10; + unsigned int sleepingTime_ms = 0; + while (CheckWlan0IfExist() == false) { + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); + sleepingTime_ms += SLEEP_TIME_MS; + if (sleepingTime_ms > WAITING_TIME_MS) { + LogError("wlan0 not found. \n"); + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + } + LogInfo("wlan0 ok. \n"); fx_system("ifconfig wlan0 192.168.169.1 netmask 255.255.255.0"); fx_system("hostapd -d /etc/hostapd.conf &"); fx_system("touch /var/lib/misc/udhcpd.leases"); fx_system("udhcpd -f /etc/udhcpd.conf &"); return CreateStatusCode(STATUS_CODE_OK); +} +bool WifiHal::CheckWlan0IfExist(void) +{ + DIR *dir; + struct dirent *entry; + int wlan0_found = 0; + + dir = opendir("/sys/class/net"); + if (dir == NULL) { + perror("opendir"); + return false; + } + + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, "wlan0") == 0) { + wlan0_found = 1; + break; + } + } + + closedir(dir); + if (wlan0_found) { + return true; + } + else { + return false; + } } \ No newline at end of file diff --git a/hal/src/WifiHal.h b/hal/src/WifiHal.h index 7cb3884..4183c41 100644 --- a/hal/src/WifiHal.h +++ b/hal/src/WifiHal.h @@ -21,5 +21,8 @@ public: WifiHal() = default; virtual ~WifiHal() = default; StatusCode OpenApMode(void) override; + +private: + bool CheckWlan0IfExist(void); }; #endif \ No newline at end of file