Improve:wifi hal.

This commit is contained in:
Fancy code 2024-04-30 20:17:04 +08:00
parent e2d6455746
commit 1faa9b566c
2 changed files with 48 additions and 1 deletions

View File

@ -13,14 +13,58 @@
* limitations under the License.
*/
#include "WifiHal.h"
#include "LinuxApi.h"
#include "ILog.h"
#include "LinuxApi.h"
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
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;
}
}

View File

@ -21,5 +21,8 @@ public:
WifiHal() = default;
virtual ~WifiHal() = default;
StatusCode OpenApMode(void) override;
private:
bool CheckWlan0IfExist(void);
};
#endif