Merge branch 'master' of gitee.com:bjtu_wujun_transportation/communication_wifi

This commit is contained in:
wujun 2023-12-06 19:48:53 +08:00
commit 3faae6af32
5 changed files with 399 additions and 0 deletions

View File

@ -0,0 +1,108 @@
/*
* Copyright (C) 2023-2023 Huawei Device Co., Ltd.
* 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.
*/
if (defined(ohos_lite)) {
import("//build/lite/config/component/lite_component.gni")
import("//foundation/communication/wifi/wifi/wifi_lite.gni")
} else {
import("//build/ohos.gni")
import("//foundation/communication/wifi/wifi/wifi.gni")
}
local_base_sources = [
"wifi_power_cmd_client.cpp",
"rx_listen/wifi_rx_listen_arbitration.cpp",
]
local_base_include_dirs = [
"$WIFI_ROOT_DIR/interfaces/inner_api",
"$WIFI_ROOT_DIR/frameworks/native/interfaces",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_power",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_manage/wifi_power/rx_listen",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/net_helper",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/include",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/utils",
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit/log",
"//foundation/appexecfwk/adapter/interfaces/innerkits/appexecfwk_base/include",
]
if (defined(ohos_lite)) {
shared_library("wifi_power_service") {
sources = local_base_sources
include_dirs = local_base_include_dirs
deps = [
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit:wifi_toolkit",
"$WIFI_ROOT_DIR/utils:wifi_utils",
]
external_deps = [
"hilog:libhilog",
"init:libbegetutil",
"c_utils:utils",
"ability_runtime:app_manager",
]
configs -= [ "//build/lite/config:language_cpp" ]
cflags_cc = [
"-std=c++17",
"-fno-rtti",
]
defines = [ "OHOS_ARCH_LITE" ]
ldflags = [
"-fPIC",
"-Wl,-E",
]
}
} else {
ohos_shared_library("wifi_power_service") {
branch_protector_ret = "pac_ret"
sanitize = {
cfi = true
boundary_sanitize = true
cfi_cross_dso = true
debug = false
}
install_enable = true
sources = local_base_sources
include_dirs = local_base_include_dirs
cflags_cc = [
"-std=c++17",
"-fno-rtti",
]
ldflags = [
"-fPIC",
"-Wl,-E",
]
deps = [
"$WIFI_ROOT_DIR/services/wifi_standard/wifi_framework/wifi_toolkit:wifi_toolkit",
"$WIFI_ROOT_DIR/utils:wifi_utils",
]
external_deps = [
"hilog:libhilog",
"init:libbegetutil",
"c_utils:utils",
"ability_runtime:app_manager",
]
part_name = "wifi"
subsystem_name = "communication"
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2023-2023 Huawei Device Co., Ltd.
* 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 "wifi_rx_listen_arbitration.h"
#include "wifi_logger.h"
#include "wifi_power_cmd_client.h"
#include "app_parser.h"
namespace OHOS {
namespace Wifi {
DEFINE_WIFILOG_LABEL("WifiRxListen");
static const int SHIFT_BIT_UTIL = 1;
static const int GAME_APP_SHIFT = 0;
static const int RX_LISTEN_SWITCH_LEN = 1;
static const auto ENABLE_RX_LISTEN = "Y";
static const auto DISABLE_RX_LISTEN = "N";
RxListenArbitration::RxListenArbitration()
{
WIFI_LOGI("%{public}s enter", __FUNCTION__);
}
RxListenArbitration::~RxListenArbitration()
{
WIFI_LOGI("%{public}s enter", __FUNCTION__);
}
RxListenArbitration &RxListenArbitration::GetInstance()
{
static RxListenArbitration instance;
return instance;
}
void RxListenArbitration::OnForegroundAppChanged(const AppExecFwk::AppStateData &appStateData)
{
if (appStateData.state == static_cast<int>(AppExecFwk::ApplicationState::APP_STATE_FOREGROUND) &&
appStateData.isFocused) {
WIFI_LOGD("%{public}s enter rx_listen arbitration", __FUNCTION__);
if (AppParser::GetInstance().IsGameApp(appStateData.bundleName)) {
// game scene: set m_arbitrationCond to zero to enable rx_listen
m_arbitrationCond = m_arbitrationCond | (SHIFT_BIT_UTIL << GAME_APP_SHIFT);
} else {
// not game scene: set m_arbitrationCond to one to disable rx_listen
m_arbitrationCond = m_arbitrationCond & (~(SHIFT_BIT_UTIL << GAME_APP_SHIFT));
}
CheckRxListenSwitch();
}
}
void RxListenArbitration::CheckRxListenSwitch()
{
if ((m_arbitrationCond == 0) && !m_isRxListenOn) {
// TODO open rx_listen
} else if ((m_arbitrationCond != 0) && m_isRxListenOn) {
// TODO close
} else {
WIFI_LOGD("%{public}s no switch", __FUNCTION__);
}
}
} // namespace Wifi
} // namespace OHOS

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2023-2023 Huawei Device Co., Ltd.
* 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 <string>
#include "app_mgr_interface.h"
#include "app_mgr_constants.h"
#ifndef OHOS_WIFI_POWER_RX_LISTEN_H
#define OHOS_WIFI_POWER_RX_LISTEN_H
namespace OHOS {
namespace Wifi {
class RxListenArbitration {
public:
RxListenArbitration();
~RxListenArbitration();
static RxListenArbitration &GetInstance();
void OnForegroundAppChanged(const AppExecFwk::AppStateData &appStateData);
private:
void CheckRxListenSwitch();
private:
unsigned int m_arbitrationCond = 0x00;
bool m_isRxListenOn = false;
};
} // namespace Wifi
} // namespace OHOS
#endif /* OHOS_WIFI_POWER_RX_LISTEN_H */

View File

@ -0,0 +1,132 @@
#include "wifi_power_cmd_client.h"
/*
* Copyright (C) 2023-2023 Huawei Device Co., Ltd.
* 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 "wifi_power_cmd_client.h"
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <cerrno>
#include "wifi_logger.h"
namespace OHOS {
namespace Wifi {
DEFINE_WIFILOG_LABEL("WifiPowerCmdClient");
const auto WiFI_IFNAME = "wlan0";
const int CMD_SET_RX_LISTEN_POWER_SAVING_SWITCH = 125;
static const int MAX_PRIV_CMD_SIZE = 4096;
static const int TINY_BUFF_SIZE = 64;
static const char RX_LISTEN_ON = 'Y';
static const char RX_LISTEN_OFF = 'N';
static const auto CMD_SET_RX_LISTEN_ON = "SET_RX_LISTEN_PS_SWITCH 1";
static const auto CMD_SET_RX_LISTEN_OFF = "SET_RX_LISTEN_PS_SWITCH 0";
WifiPowerCmdClient &Wifi::WifiPowerCmdClient::GetInstance()
{
static WifiPowerCmdClient instance;
return instance;
}
int Wifi::WifiPowerCmdClient::SendCmdToDriver(const char *iface, int commandId, const char *paramBuf,
unsigned int paramSize) const
{
int ret = -1;
if (iface == nullptr || paramBuf == nullptr) {
WIFI_LOGE("%{public}s invalid params", __FUNCTION__);
return ret;
}
if (commandId == CMD_SET_RX_LISTEN_POWER_SAVING_SWITCH) {
ret = SetRxListen(paramBuf);
}
return ret;
}
int Wifi::WifiPowerCmdClient::SendCommandToDriverByInterfaceName(char *cmdBuf, int cmdSize,
const char *interfaceName) const
{
if (cmdBuf > MAX_PRIV_CMD_SIZE) {
WIFI_LOGE("%{public}s cmdSize too large", __FUNCTION__);
return ret;
}
if (cmdBuf == nullptr) {
WIFI_LOGE("%{public}s cmdBuf is null", __FUNCTION__);
return ret;
}
struct ifreq ifr;
int ret = -1;
WifiPrivCmd privCmd = { 0 };
unint8_t buf[MAX_PRIV_CMD_SIZE] = {0};
(void)memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr));
if (memcpy_s(buf, MAX_PRIV_CMD_SIZE, cmdBuf, cmdSize) != EOK) {
WIFI_LOGE("%{public}s memcpy_s privCmd buf error", __FUNCTION__);
return ret;
}
privCmd.buf = buf;
privCmd.size = sizeof(buf);
privCmd.len = cmdSize;
ifr.ifr_data = reinterpret_cast<char *>(&privCmd);
if (strcpy_s(ifr.ifr_name, IFNAMSIZ, interfaceName) != EOK) {
WIFI_LOGE("%{public}s strcpy_s ifr fail", __FUNCTION__);
return ret;
}
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
WIFI_LOGE("%{public}s socked fail", __FUNCTION__);
return ret;
}
ret = ioctl(sock, SIOCDEVPRIVATE + 1, &ifr);
if (ret < 0) {
WIFI_LOGE("%{public}s ioctl failed, error is: %{public}d.", __FUNCTION__, errno);
return ret;
}
(void)memset_s(cmdBuf, cmdSize, 0, cmdSize);
if (memcpy_s(cmdBuf, cmdSize, privCmd.buf, cmdSize - 1) != 0) {
WIFI_LOGE("%{public}s memcpy_s cmd fail", __FUNCTION__);
}
close(sock);
return ret;
}
int Wifi::WifiPowerCmdClient::SetRxListen(const char *paramBuf) const
{
int ret = -1;
size_t cmdLen;
char cmdBuf[TINY_BUFF_SIZE] = {0};
if (*paramBuf == RX_LISTEN_ON) {
cmdLen = strlen(CMD_SET_RX_LISTEN_ON);
if (memcpy_s(cmdBuf, TINY_BUFF_SIZE - 1, CMD_SET_RX_LISTEN_ON, cmdLen) != EOK) {
WIFI_LOGE("%{public}s memcpy_s cmdBuf fail", __FUNCTION__);
return ret;
}
} else if (*paramBuf == RX_LISTEN_OFF) {
cmdLen = strlen(CMD_SET_RX_LISTEN_OFF);
if (memcpy_s(cmdBuf, TINY_BUFF_SIZE - 1, CMD_SET_RX_LISTEN_OFF, cmdLen) != EOK) {
WIFI_LOGE("%{public}s memcpy_s cmdBuf fail", __FUNCTION__);
return ret;
}
} else {
WIFI_LOGE("%{public}s invalid param", __FUNCTION__);
return ret;
}
ret = SendCommandToDriverByInterfaceName(cmdBuf, TINY_BUFF_SIZE, WiFI_IFNAME);
return ret;
}
} // namespace Wifi
} // namespace OHOS

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) 2023-2023 Huawei Device Co., Ltd.
* 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.
*/
#ifndef OHOS_WIFI_POWER_CMD_CLIENT_H
#define OHOS_WIFI_POWER_CMD_CLIENT_H
#include <unistd.h>
namespace OHOS {
namespace Wifi {
typedef struct {
unit8_t *buf;
unint32_t size;
unint32_t len;
} WifiPrivCmd;
class WifiPowerCmdClient {
public:
static WifiPowerCmdClient &GetInstance();
int SendCmdToDriver(const char *iface, int commandId, const char *paramBuf, unsigned int paramSize) const;
private:
int SendCommandToDriverByInterfaceName(char *cmdBuf, int cmdSize, const char *interfaceName) const;
int SetRxListen(const char *paramBuf) const;
};
} // namespace Wifi
} // namespace OHOS
#endif /* OHOS_WIFI_POWER_CMD_CLIENT_H */