!1553 添加手机热点识别接口

Merge pull request !1553 from 郭园/master
This commit is contained in:
openharmony_ci 2023-12-11 14:18:13 +00:00 committed by Gitee
commit e00f8ff177
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
16 changed files with 174 additions and 2 deletions

View File

@ -49,6 +49,7 @@ napi_value GetIpInfo(napi_env env, napi_callback_info info);
napi_value GetIpv6Info(napi_env env, napi_callback_info info);
napi_value GetLinkedInfo(napi_env env, napi_callback_info info);
napi_value GetDisconnectedReason(napi_env env, napi_callback_info info);
napi_value IsMeteredHotspot(napi_env env, napi_callback_info info);
napi_value RemoveDevice(napi_env env, napi_callback_info info);
napi_value RemoveAllNetwork(napi_env env, napi_callback_info info);
napi_value DisableNetwork(napi_env env, napi_callback_info info);

View File

@ -976,8 +976,7 @@ static void LinkedInfoToJs(const napi_env& env, WifiLinkedInfo& linkedInfo, napi
SetValueInt32(env, "linkSpeed", linkedInfo.linkSpeed, result);
SetValueInt32(env, "frequency", linkedInfo.frequency, result);
SetValueBool(env, "isHidden", linkedInfo.ifHiddenSSID, result);
/* isRestricted not support now, set as default value */
SetValueBool(env, "isRestricted", false, result);
SetValueBool(env, "isRestricted", linkedInfo.isDataRestricted, result);
SetValueInt32(env, "chload", linkedInfo.chload, result);
SetValueInt32(env, "snr", linkedInfo.snr, result);
SetValueUtf8String(env, "macAddress", linkedInfo.macAddress.c_str(), result);
@ -1041,6 +1040,20 @@ NO_SANITIZE("cfi") napi_value GetDisconnectedReason(napi_env env, napi_callback_
return value;
}
NO_SANITIZE("cfi") napi_value IsMeteredHotspot(napi_env env, napi_callback_info info)
{
WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
bool isMeteredHotspot = false;
ErrCode ret = wifiDevicePtr->IsMeteredHotspot(isMeteredHotspot);
if (ret != WIFI_OPT_SUCCESS) {
WIFI_LOGE("Get isMeteredHotspot value fail: %{public}d", ret);
WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
}
napi_value result;
napi_get_boolean(env, isMeteredHotspot, &result);
return result;
}
NO_SANITIZE("cfi") napi_value RemoveDevice(napi_env env, napi_callback_info info)
{
TRACE_FUNC_CALL;

View File

@ -319,6 +319,7 @@ static napi_value Init(napi_env env, napi_value exports) {
DECLARE_NAPI_FUNCTION("getIpv6Info", GetIpv6Info),
DECLARE_NAPI_FUNCTION("getLinkedInfo", GetLinkedInfo),
DECLARE_NAPI_FUNCTION("getDisconnectedReason", GetDisconnectedReason),
DECLARE_NAPI_FUNCTION("isMeteredHotspot", IsMeteredHotspot),
DECLARE_NAPI_FUNCTION("removeDevice", RemoveDevice),
DECLARE_NAPI_FUNCTION("removeDeviceConfig", RemoveDevice),
DECLARE_NAPI_FUNCTION("removeAllNetwork", RemoveAllNetwork),

View File

@ -58,6 +58,17 @@ NO_SANITIZE("cfi") int IsWifiActive()
return (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) && isActive;
}
NO_SANITIZE("cfi") int IsMeteredHotspot()
{
if (wifiDevicePtr == nullptr) {
return false;
}
bool isMeteredHotspot = false;
OHOS::Wifi::ErrCode ret = wifiDevicePtr->IsMeteredHotspot(isMeteredHotspot);
return (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) && isMeteredHotspot;
}
NO_SANITIZE("cfi") WifiErrorCode Scan()
{
CHECK_PTR_RETURN(wifiScanPtr, ERROR_WIFI_NOT_AVAILABLE);

View File

@ -228,6 +228,14 @@ public:
*/
virtual ErrCode IsWifiActive(bool &bActive) = 0;
/**
* @Description Check whether Wi-Fi is metered hotspot
*
* @param bMeteredHotspot - isMeteredHotspot / notMeteredHotspot
* @return ErrCode - operation result
*/
virtual ErrCode IsMeteredHotspot(bool &bMeteredHotspot) = 0;
/**
* @Description Get the Wifi State
*

View File

@ -62,6 +62,7 @@ enum class DevInterfaceCode {
WIFI_SVR_CMD_IS_HELD_WIFI_PROTECT = 0x1036, /* is held the Wi-Fi protect. */
WIFI_SVR_CMD_IS_SET_FACTORY_RESET = 0x1037, /* factory reset */
/* 新增WIFI_SVR_CMD_类code请在此下方添加 */
WIFI_SVR_CMD_IS_METERED_HOTSPOT = 0x1037, /* whether current link is metered hotspot */
/* 以下CALL BACK类code不需要进行权限校验 */
WIFI_CBK_CMD_STATE_CHANGE = 0x3000, /* STA state change event */

View File

@ -326,6 +326,13 @@ ErrCode WifiDeviceImpl::GetDisconnectedReason(DisconnectedReason &reason)
return client_->GetDisconnectedReason(reason);
}
ErrCode WifiDeviceImpl::IsMeteredHotspot(bool &bMeteredHotspot)
{
std::lock_guard<std::mutex> lock(mutex_);
RETURN_IF_FAIL(GetWifiDeviceProxy());
return client_->IsMeteredHotspot(bMeteredHotspot);
}
ErrCode WifiDeviceImpl::GetIpInfo(IpInfo &info)
{
std::lock_guard<std::mutex> lock(mutex_);

View File

@ -197,6 +197,13 @@ public:
*/
ErrCode IsWifiActive(bool &bActive) override;
/**
* @Description Check whether Wi-Fi is metered hotspot
*
* @param bMeteredHotspot - isMeteredHotspot / notMeteredHotspot
* @return ErrCode - operation result
*/
ErrCode IsMeteredHotspot(bool &bMeteredHotspot) override;
/**
* @Description Get the Wifi State
*

View File

@ -1069,6 +1069,39 @@ ErrCode WifiDeviceProxy::GetDisconnectedReason(DisconnectedReason &reason)
return WIFI_OPT_SUCCESS;
}
ErrCode WifiDeviceProxy::IsMeteredHotspot(bool &bMeteredHotspot)
{
if (mRemoteDied) {
WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
return WIFI_OPT_FAILED;
}
MessageOption option;
MessageParcel data, reply;
if (!data.WriteInterfaceToken(GetDescriptor())) {
WIFI_LOGE("Write interface token error: %{public}s", __func__);
return WIFI_OPT_FAILED;
}
data.WriteInt32(0);
int error = Remote()->SendRequest(static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_IS_METERED_HOTSPOT),
data, reply, option);
if (error != ERR_NONE) {
WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
static_cast<int32_t>(DevInterfaceCode::WIFI_SVR_CMD_IS_METERED_HOTSPOT), error);
return WIFI_OPT_FAILED;
}
int exception = reply.ReadInt32();
if (exception) {
return WIFI_OPT_FAILED;
}
int ret = reply.ReadInt32();
if (ret != WIFI_OPT_SUCCESS) {
return ErrCode(ret);
}
bMeteredHotspot = reply.ReadBool();
return WIFI_OPT_SUCCESS;
}
ErrCode WifiDeviceProxy::GetLinkedInfo(WifiLinkedInfo &info)
{
if (mRemoteDied) {

View File

@ -234,6 +234,14 @@ public:
*/
ErrCode IsWifiActive(bool &bActive) override;
/**
* @Description Check whether Wi-Fi is metered hotspot
*
* @param bMeteredHotspot - isMeteredHotspot / notMeteredHotspot
* @return ErrCode - operation result
*/
ErrCode IsMeteredHotspot(bool &bMeteredHotspot) override;
/**
* @Description Get the Wifi State
*

View File

@ -1106,6 +1106,40 @@ ErrCode WifiDeviceProxy::IsWifiActive(bool &bActive)
return ErrCode(owner.retCode);
}
ErrCode WifiDeviceProxy::IsMeteredHotspot(bool &bMeteredHotspot)
{
if (remoteDied_ || remote_ == nullptr) {
WIFI_LOGE("failed to %{public}s, remoteDied_: %{public}d, remote_: %{public}d",
__func__, remoteDied_, remote_ == nullptr);
return WIFI_OPT_FAILED;
}
IpcIo req;
char data[IPC_DATA_SIZE_SMALL];
struct IpcOwner owner = {.exception = -1, .retCode = 0, .variable = nullptr};
IpcIoInit(&req, data, IPC_DATA_SIZE_SMALL, MAX_IPC_OBJ_COUNT);
if (!WriteInterfaceToken(&req, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
WIFI_LOGE("Write interface token error: %{public}s", __func__);
return WIFI_OPT_FAILED;
}
(void)WriteInt32(&req, 0);
owner.variable = &bMeteredHotspot;
owner.funcId = static_cast<int32_t>(DevInterfaceCode::WIFI_SVR_CMD_IS_METERED_HOTSPOT);
int error = remote_->Invoke(remote_, static_cast<int32_t>(DevInterfaceCode::WIFI_SVR_CMD_IS_METERED_HOTSPOT), &req,
&owner, IpcCallback);
if (error != EC_SUCCESS) {
WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
static_cast<int32_t>(DevInterfaceCode::WIFI_SVR_CMD_IS_METERED_HOTSPOT), error);
return WIFI_OPT_FAILED;
}
if (owner.exception) {
return WIFI_OPT_FAILED;
}
return ErrCode(owner.retCode);
}
ErrCode WifiDeviceProxy::GetWifiState(int &state)
{
if (remoteDied_ || remote_ == nullptr) {

View File

@ -191,6 +191,14 @@ public:
*/
virtual ErrCode IsWifiActive(bool &bActive) = 0;
/**
* @Description Check whether Wi-Fi is metered hotspot.
*
* @param bMeteredHotspot - isMeteredHotspot / notMeteredHotspot
* @return ErrCode - operation result
*/
virtual ErrCode IsMeteredHotspot(bool &bMeteredHotspot) = 0;
/**
* @Description Get the Wi-Fi State.
*

View File

@ -1130,6 +1130,28 @@ ErrCode WifiDeviceServiceImpl::GetWifiState(int &state)
return WIFI_OPT_SUCCESS;
}
ErrCode WifiDeviceServiceImpl::IsMeteredHotspot(bool &bMeteredHotspot)
{
if (WifiPermissionUtils::VerifyGetWifiInfoPermission() == PERMISSION_DENIED) {
WIFI_LOGE("IsMeteredHotspot:VerifyGetWifiInfoPermission() PERMISSION_DENIED!");
return WIFI_OPT_PERMISSION_DENIED;
}
if (!IsStaServiceRunning()) {
return WIFI_OPT_STA_NOT_OPENED;
}
WifiLinkedInfo info;
WifiConfigCenter::GetInstance().GetLinkedInfo(info, m_instId);
WIFI_LOGI("%{public}s, connState=%{public}d, detailedState=%{public}d",
__func__, info.connState, info.detailedState);
if (info.connState != ConnState::CONNECTED) {
return WIFI_OPT_FAILED;
}
bMeteredHotspot = info.isDataRestricted;
return WIFI_OPT_SUCCESS;
}
ErrCode WifiDeviceServiceImpl::GetLinkedInfo(WifiLinkedInfo &info)
{
if (WifiPermissionUtils::VerifyGetWifiInfoPermission() == PERMISSION_DENIED) {

View File

@ -111,6 +111,8 @@ public:
ErrCode GetWifiState(int &state) override;
ErrCode IsMeteredHotspot(bool &bMeteredHotspot) override;
ErrCode GetLinkedInfo(WifiLinkedInfo &info) override;
ErrCode GetDisconnectedReason(DisconnectedReason &reason) override;

View File

@ -114,6 +114,8 @@ void WifiDeviceStub::InitHandleMap()
&WifiDeviceStub::OnIsWifiActive;
handleFuncMap[static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_WIFI_STATE)] =
&WifiDeviceStub::OnGetWifiState;
handleFuncMap[static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_IS_METERED_HOTSPOT)] =
&WifiDeviceStub::OnIsMeteredHotspot;
handleFuncMap[static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_LINKED_INFO)] =
&WifiDeviceStub::OnGetLinkedInfo;
handleFuncMap[static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_DHCP_INFO)] = &WifiDeviceStub::OnGetIpInfo;
@ -654,6 +656,19 @@ void WifiDeviceStub::OnGetWifiState(uint32_t code, MessageParcel &data, MessageP
return;
}
void WifiDeviceStub::OnIsMeteredHotspot(uint32_t code, MessageParcel &data, MessageParcel &reply)
{
WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
bool bMeteredHotspot = false;
ErrCode ret = IsMeteredHotspot(bMeteredHotspot);
reply.WriteInt32(0);
reply.WriteInt32(ret);
if (ret == WIFI_OPT_SUCCESS) {
reply.WriteBool(bMeteredHotspot);
}
return;
}
void WifiDeviceStub::OnGetLinkedInfo(uint32_t code, MessageParcel &data, MessageParcel &reply)
{
WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());

View File

@ -61,6 +61,7 @@ private:
void OnStartWps(uint32_t code, MessageParcel &data, MessageParcel &reply);
void OnCancelWps(uint32_t code, MessageParcel &data, MessageParcel &reply);
void OnIsWifiActive(uint32_t code, MessageParcel &data, MessageParcel &reply);
void OnIsMeteredHotspot(uint32_t code, MessageParcel &data, MessageParcel &reply);
void OnGetWifiState(uint32_t code, MessageParcel &data, MessageParcel &reply);
void OnGetLinkedInfo(uint32_t code, MessageParcel &data, MessageParcel &reply);
void OnGetIpInfo(uint32_t code, MessageParcel &data, MessageParcel &reply);