90 lines
2.4 KiB
C++
90 lines
2.4 KiB
C++
/*
|
|
* Copyright (c) 2023 Fancy Code.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
#include "WifiHal.h"
|
|
#include "ILog.h"
|
|
#include "LinuxApi.h"
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <thread>
|
|
WifiHal::WifiHal() : mInitRunning(false)
|
|
{
|
|
}
|
|
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;
|
|
mInitRunning = true;
|
|
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);
|
|
}
|
|
if (false == mInitRunning) {
|
|
LogError("Open ap mode stop. \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);
|
|
}
|
|
StatusCode WifiHal::CloseApMode(void)
|
|
{
|
|
mInitRunning = false;
|
|
return CreateStatusCode(STATUS_CODE_OK);
|
|
}
|
|
void WifiHal::Init(void)
|
|
{
|
|
PowerOff();
|
|
}
|
|
void WifiHal::UnInit(void)
|
|
{
|
|
}
|
|
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;
|
|
}
|
|
} |