diff --git a/bundle.json b/bundle.json index bfcc7273cb34535746d872d6cdb982640d82cb9c..322bb08709b8ecc721d5f151b2980ce43d4cb7b7 100755 --- a/bundle.json +++ b/bundle.json @@ -51,6 +51,7 @@ "ipc", "ces_standard", "hiviewdfx_hilog_native", + "hisysevent_native", "netmanager_base" ], "third_party": [ @@ -129,6 +130,9 @@ "//foundation/communication/wifi/tests/wifi_standard/wifi_framework/wifi_manage/idl_client/unittest:unittest", "//foundation/communication/wifi/tests/wifi_standard/wifi_hal/unittest:unittest" ] - } + }, + "hisysevent_config": [ + "//foundation/communication/wifi/hisysevent.yaml" + ] } } \ No newline at end of file diff --git a/hisysevent.yaml b/hisysevent.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1bd9fd4733283860f98bf24b12b6605aca242045 --- /dev/null +++ b/hisysevent.yaml @@ -0,0 +1,38 @@ +# Copyright (C) 2022 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. + +domain: COMMUNICATION + +# Start or stop Wi-Fi switch +WIFI_STATE: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: start or stop wifi AP message} + TYPE: {type: STRING, desc: sta or ap} + OPER_TYPE: {type: INT32, desc: operation type, enable or disable} + +# Connect or disconnect Wi-Fi +WIFI_CONNECTION: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: wifi connection or disconnection message} + TYPE: {type: INT32, desc: connect or disconnect operation type} + PACKAGE_NAME: {type: STRING, desc: package name} + +# Scans Wi-Fi hotspots +WIFI_SCAN: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: wifi scan message} + EXECUTE_RESULT: {type: INT32, desc: wifi scan execute result} + PACKAGE_NAME: {type: STRING, desc: package name} + +# Wi-Fi event received +WIFI_EVENT_RECEIVED: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: wifi event for int value} + EVENT_TYPE: {type: STRING, desc: event type} + VALUE: {type: INT32, desc: event value} diff --git a/interfaces/innerkits/native_cpp/napi/BUILD.gn b/interfaces/innerkits/native_cpp/napi/BUILD.gn index 2bcf486db6b1acf5294c1ca410b4b71b2b373be1..e3692359e57e2a7b6ef43e437ac3835302053dae 100755 --- a/interfaces/innerkits/native_cpp/napi/BUILD.gn +++ b/interfaces/innerkits/native_cpp/napi/BUILD.gn @@ -24,6 +24,9 @@ ohos_shared_library("wifi") { "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", "//foundation/communication/wifi/interfaces/innerkits/native_cpp/wifi_standard/include", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/common/net_helper", + "//foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime/context", + "//foundation/communication/wifi/utils/inc", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", ] sources = [ @@ -36,8 +39,10 @@ ohos_shared_library("wifi") { ] deps = [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//foundation/aafwk/standard/frameworks/kits/appkit:app_context", "//foundation/ace/napi:ace_napi", "//foundation/communication/wifi/interfaces/innerkits/native_cpp/wifi_standard:wifi_sdk", + "//foundation/communication/wifi/utils/src:wifi_utils", "//utils/native/base:utils", ] @@ -59,6 +64,9 @@ ohos_shared_library("wifi_native_js") { "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", "//foundation/communication/wifi/interfaces/innerkits/native_cpp/wifi_standard/include", "//foundation/communication/wifi/services/wifi_standard/wifi_framework/common/net_helper", + "//foundation/aafwk/standard/frameworks/kits/appkit/native/ability_runtime/context", + "//foundation/communication/wifi/utils/inc", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", ] sources = [ @@ -71,8 +79,10 @@ ohos_shared_library("wifi_native_js") { ] deps = [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//foundation/aafwk/standard/frameworks/kits/appkit:app_context", "//foundation/ace/napi:ace_napi", "//foundation/communication/wifi/interfaces/innerkits/native_cpp/wifi_standard:wifi_sdk", + "//foundation/communication/wifi/utils/src:wifi_utils", "//utils/native/base:utils", ] diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp index 4354ae7727a276d0d2ad01ec37ef84a5211be903..dbf07abd17accba17faf017b3fe28d945c689b71 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp @@ -29,6 +29,7 @@ napi_value EnableWifi(napi_env env, napi_callback_info info) TRACE_FUNC_CALL; NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->EnableWifi(); + WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_STA, WifiOperType::ENABLE); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; @@ -39,6 +40,7 @@ napi_value DisableWifi(napi_env env, napi_callback_info info) TRACE_FUNC_CALL; NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->DisableWifi(); + WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_STA, WifiOperType::DISABLE); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; @@ -63,7 +65,7 @@ napi_value Scan(napi_env env, napi_callback_info info) TRACE_FUNC_CALL; NAPI_ASSERT(env, wifiScanPtr != nullptr, "Wifi scan instance is null."); ErrCode ret = wifiScanPtr->Scan(); - + WriteWifiScanHiSysEvent(static_cast(ret), JsAbilityGetBundleName()); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; @@ -258,6 +260,7 @@ napi_value ConnectToNetwork(napi_env env, napi_callback_info info) NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId); + WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, JsAbilityGetBundleName()); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; @@ -282,7 +285,7 @@ napi_value ConnectToDevice(napi_env env, napi_callback_info info) if (ret != WIFI_OPT_SUCCESS) { WIFI_LOGE("Connect to device fail: %{public}d", ret); } - + WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, JsAbilityGetBundleName()); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; @@ -302,6 +305,7 @@ napi_value Disconnect(napi_env env, napi_callback_info info) TRACE_FUNC_CALL; NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->Disconnect(); + WriteWifiConnectionHiSysEvent(WifiConnectionType::DISCONNECT, JsAbilityGetBundleName()); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; @@ -346,6 +350,7 @@ napi_value ReConnect(napi_env env, napi_callback_info info) napi_value result; napi_get_boolean(env, wifiDevicePtr->ReConnect(), &result); + WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, JsAbilityGetBundleName()); return result; } @@ -355,6 +360,7 @@ napi_value ReAssociate(napi_env env, napi_callback_info info) NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); napi_value result; napi_get_boolean(env, wifiDevicePtr->ReAssociate(), &result); + WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, JsAbilityGetBundleName()); return result; } diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp index 0afcac047d45ff59c0a1bbc67ab4bd270648d12d..9b256fca223a37c26a2b3c5742a4d78864f2b4d6 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp @@ -57,21 +57,17 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_FUNCTION("setHotspotConfig", SetHotspotConfig), DECLARE_NAPI_FUNCTION("getHotspotConfig", GetHotspotConfig), DECLARE_NAPI_FUNCTION("getStations", GetStations), - DECLARE_NAPI_FUNCTION("enableP2p", EnableP2p), - DECLARE_NAPI_FUNCTION("disableP2p", DisableP2p), DECLARE_NAPI_FUNCTION("getP2pLinkedInfo", GetP2pLinkedInfo), DECLARE_NAPI_FUNCTION("getCurrentGroup", GetCurrentGroup), DECLARE_NAPI_FUNCTION("getP2pDevices", GetP2pDevices), DECLARE_NAPI_FUNCTION("createGroup", CreateGroup), DECLARE_NAPI_FUNCTION("removeGroup", RemoveGroup), DECLARE_NAPI_FUNCTION("p2pConnect", P2pConnect), - DECLARE_NAPI_FUNCTION("p2pDisConnect", P2pDisConnect), + DECLARE_NAPI_FUNCTION("p2pCancelConnect", P2pCancelConnect), DECLARE_NAPI_FUNCTION("startDiscoverDevices", StartDiscoverDevices), DECLARE_NAPI_FUNCTION("stopDiscoverDevices", StopDiscoverDevices), - DECLARE_NAPI_FUNCTION("startP2pListen", StartP2pListen), - DECLARE_NAPI_FUNCTION("stopP2pListen", StopP2pListen), DECLARE_NAPI_FUNCTION("deletePersistentGroup", DeletePersistentGroup), - DECLARE_NAPI_FUNCTION("setP2pDeviceName", SetP2pDeviceName), + DECLARE_NAPI_FUNCTION("setDeviceName", SetDeviceName), DECLARE_NAPI_FUNCTION("on", On), DECLARE_NAPI_FUNCTION("off", Off), }; diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp index 1492e653381c74e3b1ba4fc61ee805120fc2a4fe..e4c9b946e01a863993e8d53c8e7ae870800903a4 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp @@ -36,7 +36,7 @@ const std::string EVENT_P2P_CONN_STATE_CHANGE = "p2pConnectionChange"; const std::string EVENT_P2P_DEVICE_STATE_CHANGE = "p2pDeviceChange"; const std::string EVENT_P2P_PERSISTENT_GROUP_CHANGE = "p2pPersistentGroupChange"; const std::string EVENT_P2P_PEER_DEVICE_CHANGE = "p2pPeerDeviceChange"; -const std::string EVENT_P2P_DISCOVERY_CHANGE = "p2pPeerDeviceChange"; +const std::string EVENT_P2P_DISCOVERY_CHANGE = "p2pDiscoveryChange"; static std::set g_supportEventList = { EVENT_STA_POWER_STATE_CHANGE, @@ -180,6 +180,9 @@ public: void OnWifiConnectionChanged(int state, const WifiLinkedInfo &info) override { WIFI_LOGI("sta received connection changed event: %{public}d", state); + if (m_connectStateConvertMap.find(state) == m_connectStateConvertMap.end()) { + return; + } CheckAndNotify(EVENT_STA_CONN_STATE_CHANGE, state); } @@ -197,6 +200,17 @@ public: OHOS::sptr AsObject() override { return nullptr; } + +private: + enum class JsLayerConnectStatus { + DISCONNECTED = 0, + CONNECTED = 1, + }; + + std::map m_connectStateConvertMap = { + { static_cast(ConnState::CONNECTED), static_cast(JsLayerConnectStatus::CONNECTED) }, + { static_cast(ConnState::DISCONNECTED), static_cast(JsLayerConnectStatus::DISCONNECTED) }, + }; }; class WifiNapiScanEventCallback : public IWifiScanCallback, public NapiEvent { diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp index 185a7d6faf9d940efb92b5158e7755e39b3c5339..bcd68c603e6d5f4d3ec657d054ab77441b332efc 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 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 @@ -38,6 +38,7 @@ napi_value EnableHotspot(napi_env env, napi_callback_info info) if (ret != WIFI_OPT_SUCCESS) { WIFI_LOGE("Enable hotspot error: %{public}d", ret); } + WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_AP, WifiOperType::ENABLE); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; @@ -51,6 +52,7 @@ napi_value DisableHotspot(napi_env env, napi_callback_info info) if (ret != WIFI_OPT_SUCCESS) { WIFI_LOGE("Disable hotspot error: %{public}d", ret); } + WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_AP, WifiOperType::DISABLE); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); return result; diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.cpp index 574662b538abc85ee7a88ad7fb76ea640a1a265a..0bb825d6da813f5eb882059048ddd3e16dc2346f 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.cpp @@ -21,50 +21,16 @@ namespace Wifi { DEFINE_WIFILOG_LABEL("WifiNAPIP2p"); std::unique_ptr wifiP2pPtr = WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID); - -napi_value EnableP2p(napi_env env, napi_callback_info info) -{ - TRACE_FUNC_CALL; - NAPI_ASSERT(env, wifiP2pPtr != nullptr, "Wifi p2p instance is null."); - - ErrCode ret = wifiP2pPtr->EnableP2p(); - napi_value result; - napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); - return result; -} - -napi_value DisableP2p(napi_env env, napi_callback_info info) -{ - TRACE_FUNC_CALL; - NAPI_ASSERT(env, wifiP2pPtr != nullptr, "Wifi p2p instance is null."); - - ErrCode ret = wifiP2pPtr->DisableP2p(); - napi_value result; - napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); - return result; -} - static void DeviceInfoToJs(const napi_env& env, const WifiP2pDevice& device, napi_value& result) { SetValueUtf8String(env, "deviceName", device.GetDeviceName().c_str(), result); - SetValueUtf8String(env, "macAddress", device.GetDeviceAddress().c_str(), result); + SetValueUtf8String(env, "deviceAddress", device.GetDeviceAddress().c_str(), result); SetValueUtf8String(env, "primaryDeviceType", device.GetPrimaryDeviceType().c_str(), result); - SetValueUtf8String(env, "secondaryDeviceType", device.GetSecondaryDeviceType().c_str(), result); - SetValueInt32(env, "status", static_cast(device.GetP2pDeviceStatus()), result); - SetValueUnsignedInt32(env, "supportWpsConfigMethods", device.GetWpsConfigMethod(), result); - SetValueInt32(env, "deviceCapabilitys", device.GetDeviceCapabilitys(), result); + SetValueInt32(env, "deviceStatus", static_cast(device.GetP2pDeviceStatus()), result); SetValueInt32(env, "groupCapabilitys", device.GetGroupCapabilitys(), result); } -static void WfdInfoToJs(const napi_env& env, const WifiP2pWfdInfo& wfdInfo, napi_value& result) -{ - SetValueBool(env, "wfdEnabled", wfdInfo.GetWfdEnabled(), result); - SetValueInt32(env, "deviceInfo", wfdInfo.GetDeviceInfo(), result); - SetValueInt32(env, "ctrlPort", wfdInfo.GetCtrlPort(), result); - SetValueInt32(env, "maxThroughput", wfdInfo.GetMaxThroughput(), result); -} - -static ErrCode DeviceInfosToJs(const napi_env& env, +static ErrCode DevicesToJsArray(const napi_env& env, const std::vector& vecDevices, napi_value& arrayResult) { uint32_t idx = 0; @@ -72,11 +38,6 @@ static ErrCode DeviceInfosToJs(const napi_env& env, napi_value eachObj; napi_create_object(env, &eachObj); DeviceInfoToJs(env, each, eachObj); - WifiP2pWfdInfo info = each.GetWfdInfo(); - napi_value wfdInfo; - napi_create_object(env, &wfdInfo); - WfdInfoToJs(env, info, wfdInfo); - napi_set_named_property(env, eachObj, "wfdInfo", wfdInfo); napi_status status = napi_set_element(env, arrayResult, idx++, eachObj); if (status != napi_ok) { WIFI_LOGE("wifi napi set element error: %{public}d, idx: %{public}d", status, idx - 1); @@ -88,30 +49,29 @@ static ErrCode DeviceInfosToJs(const napi_env& env, static ErrCode GroupInfosToJs(const napi_env& env, WifiP2pGroupInfo& groupInfo, napi_value& result) { - SetValueBool(env, "isP2pGroupOwner", groupInfo.IsGroupOwner(), result); - SetValueUtf8String(env, "passphrase", groupInfo.GetPassphrase().c_str(), result); - SetValueUtf8String(env, "interface", groupInfo.GetInterface().c_str(), result); - SetValueUtf8String(env, "groupName", groupInfo.GetGroupName().c_str(), result); - SetValueInt32(env, "networkId", groupInfo.GetNetworkId(), result); - SetValueInt32(env, "frequency", groupInfo.GetFrequency(), result); - SetValueBool(env, "isP2pPersistent", groupInfo.IsPersistent(), result); - SetValueInt32(env, "groupStatus", static_cast(groupInfo.GetP2pGroupStatus()), result); - SetValueUtf8String(env, "goIpAddress", groupInfo.GetGoIpAddress().c_str(), result); + SetValueBool(env, "isP2pGo", groupInfo.IsGroupOwner(), result); WifiP2pDevice ownerDevice = groupInfo.GetOwner(); napi_value owner; napi_create_object(env, &owner); DeviceInfoToJs(env, ownerDevice, owner); - napi_status status = napi_set_named_property(env, result, "owner", owner); + napi_status status = napi_set_named_property(env, result, "ownerInfo", owner); if (status != napi_ok) { - WIFI_LOGE("napi_set_named_property owner fail"); + WIFI_LOGE("napi_set_named_property ownerInfo fail"); return WIFI_OPT_FAILED; } + + SetValueUtf8String(env, "passphrase", groupInfo.GetPassphrase().c_str(), result); + SetValueUtf8String(env, "interface", groupInfo.GetInterface().c_str(), result); + SetValueUtf8String(env, "groupName", groupInfo.GetGroupName().c_str(), result); + SetValueInt32(env, "networkId", groupInfo.GetNetworkId(), result); + SetValueInt32(env, "frequency", groupInfo.GetFrequency(), result); + if (!groupInfo.IsClientDevicesEmpty()) { const std::vector& vecDevices = groupInfo.GetClientDevices(); napi_value devices; napi_create_array_with_length(env, vecDevices.size(), &devices); - if (DeviceInfosToJs(env, vecDevices, devices) != WIFI_OPT_SUCCESS) { + if (DevicesToJsArray(env, vecDevices, devices) != WIFI_OPT_SUCCESS) { return WIFI_OPT_FAILED; } status = napi_set_named_property(env, result, "clientDevices", devices); @@ -120,13 +80,14 @@ static ErrCode GroupInfosToJs(const napi_env& env, WifiP2pGroupInfo& groupInfo, return WIFI_OPT_FAILED; } } + SetValueUtf8String(env, "goIpAddress", groupInfo.GetGoIpAddress().c_str(), result); return WIFI_OPT_SUCCESS; } napi_value GetCurrentGroup(napi_env env, napi_callback_info info) { TRACE_FUNC_CALL; - size_t argc = 2; + size_t argc = 1; napi_value argv[argc]; napi_value thisVar = nullptr; void *data = nullptr; @@ -153,43 +114,6 @@ napi_value GetCurrentGroup(napi_env env, napi_callback_info info) return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); } -napi_value StartP2pListen(napi_env env, napi_callback_info info) -{ - TRACE_FUNC_CALL; - size_t argc = 2; - napi_value argv[argc]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - - napi_valuetype valueType; - napi_valuetype value2Type; - napi_typeof(env, argv[0], &valueType); - napi_typeof(env, argv[1], &value2Type); - NAPI_ASSERT(env, valueType == napi_number, "Wrong argument 1 type. napi_number expected."); - NAPI_ASSERT(env, value2Type == napi_object, "Wrong argument 2 type. napi_number expected."); - - NAPI_ASSERT(env, wifiP2pPtr != nullptr, "Wifi p2p instance is null."); - int period; - int interval; - napi_get_value_int32(env, argv[0], &period); - napi_get_value_int32(env, argv[1], &interval); - ErrCode ret = wifiP2pPtr->StartP2pListen(period, interval); - napi_value result; - napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); - return result; -} - -napi_value StopP2pListen(napi_env env, napi_callback_info info) -{ - TRACE_FUNC_CALL; - NAPI_ASSERT(env, wifiP2pPtr != nullptr, "Wifi p2p instance is null."); - - ErrCode ret = wifiP2pPtr->StopP2pListen(); - napi_value result; - napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); - return result; -} - napi_value DeletePersistentGroup(napi_env env, napi_callback_info info) { TRACE_FUNC_CALL; @@ -237,7 +161,7 @@ napi_value StopDiscoverDevices(napi_env env, napi_callback_info info) napi_value GetP2pDevices(napi_env env, napi_callback_info info) { - size_t argc = 2; + size_t argc = 1; napi_value argv[argc]; napi_value thisVar = nullptr; void *data = nullptr; @@ -246,7 +170,7 @@ napi_value GetP2pDevices(napi_env env, napi_callback_info info) QueryP2pDeviceAsyncContext *asyncContext = new QueryP2pDeviceAsyncContext(env); NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); - napi_create_string_latin1(env, "queryP2pDevices", NAPI_AUTO_LENGTH, &asyncContext->resourceName); + napi_create_string_latin1(env, "GetP2pDevices", NAPI_AUTO_LENGTH, &asyncContext->resourceName); asyncContext->executeFunc = [&](void* data) -> void { QueryP2pDeviceAsyncContext *context = static_cast(data); @@ -257,7 +181,7 @@ napi_value GetP2pDevices(napi_env env, napi_callback_info info) asyncContext->completeFunc = [&](void* data) -> void { QueryP2pDeviceAsyncContext *context = static_cast(data); napi_create_array_with_length(context->env, context->vecP2pDevices.size(), &context->result); - context->errorCode = DeviceInfosToJs(context->env, context->vecP2pDevices, context->result); + context->errorCode = DevicesToJsArray(context->env, context->vecP2pDevices, context->result); WIFI_LOGI("Push P2p Device List to client"); }; @@ -265,7 +189,7 @@ napi_value GetP2pDevices(napi_env env, napi_callback_info info) return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); } -napi_value SetP2pDeviceName(napi_env env, napi_callback_info info) +napi_value SetDeviceName(napi_env env, napi_callback_info info) { TRACE_FUNC_CALL; size_t argc = 1; @@ -294,21 +218,18 @@ static void JsObjToP2pConfig(const napi_env& env, const napi_value& object, Wifi std::string address = ""; int netId = -1; std::string passphrase = ""; - int groupOwnerIntent = -1; std::string groupName = ""; int band = static_cast(GroupOwnerBand::GO_BAND_AUTO); - JsObjectToString(env, object, "macAddress", WIFI_STR_MAC_LENGTH + 1, address); - JsObjectToInt(env, object, "goBand", band); + JsObjectToString(env, object, "deviceAddress", WIFI_STR_MAC_LENGTH + 1, address); JsObjectToInt(env, object, "netId", netId); JsObjectToString(env, object, "passphrase", MAX_PASSPHRASE_LENGTH + 1, passphrase); - JsObjectToInt(env, object, "groupOwnerIntent", groupOwnerIntent); JsObjectToString(env, object, "groupName", DEVICE_NAME_LENGTH + 1, groupName); + JsObjectToInt(env, object, "goBand", band); config.SetDeviceAddress(address); - config.SetGoBand(static_cast(band)); config.SetNetId(netId); config.SetPassphrase(passphrase); - config.SetGroupOwnerIntent(groupOwnerIntent); config.SetGroupName(groupName); + config.SetGoBand(static_cast(band)); } napi_value P2pConnect(napi_env env, napi_callback_info info) @@ -335,7 +256,7 @@ napi_value P2pConnect(napi_env env, napi_callback_info info) return result; } -napi_value P2pDisConnect(napi_env env, napi_callback_info info) +napi_value P2pCancelConnect(napi_env env, napi_callback_info info) { TRACE_FUNC_CALL; NAPI_ASSERT(env, wifiP2pPtr != nullptr, "Wifi p2p instance is null."); @@ -361,7 +282,6 @@ napi_value CreateGroup(napi_env env, napi_callback_info info) NAPI_ASSERT(env, wifiP2pPtr != nullptr, "Wifi p2p instance is null."); WifiP2pConfig config; JsObjToP2pConfig(env, argv[0], config); - ErrCode ret = wifiP2pPtr->FormGroup(config); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); @@ -382,14 +302,14 @@ napi_value RemoveGroup(napi_env env, napi_callback_info info) static void LinkedInfoToJs(const napi_env& env, WifiP2pLinkedInfo& linkedInfo, napi_value& result) { SetValueInt32(env, "connectState", static_cast(linkedInfo.GetConnectState()), result); - SetValueBool(env, "isP2pGroupOwner", linkedInfo.IsGroupOwner(), result); - SetValueUtf8String(env, "groupOwnerAddress", linkedInfo.GetGroupOwnerAddress().c_str(), result); + SetValueBool(env, "isGroupOwner", linkedInfo.IsGroupOwner(), result); + SetValueUtf8String(env, "groupOwnerAddr", linkedInfo.GetGroupOwnerAddress().c_str(), result); } napi_value GetP2pLinkedInfo(napi_env env, napi_callback_info info) { TRACE_FUNC_CALL; - size_t argc = 2; + size_t argc = 1; napi_value argv[argc]; napi_value thisVar = nullptr; void *data = nullptr; diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.h b/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.h index db359f4c6a155f3a6030b01782cc526f93a0c01b..d030febfc55d031e5e5aab56913ff9e7a4d9b765 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.h +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_p2p.h @@ -21,21 +21,17 @@ namespace OHOS { namespace Wifi { -napi_value EnableP2p(napi_env env, napi_callback_info info); -napi_value DisableP2p(napi_env env, napi_callback_info info); napi_value GetP2pLinkedInfo(napi_env env, napi_callback_info info); napi_value GetCurrentGroup(napi_env env, napi_callback_info info); napi_value GetP2pDevices(napi_env env, napi_callback_info info); napi_value CreateGroup(napi_env env, napi_callback_info info); napi_value RemoveGroup(napi_env env, napi_callback_info info); napi_value P2pConnect(napi_env env, napi_callback_info info); -napi_value P2pDisConnect(napi_env env, napi_callback_info info); +napi_value P2pCancelConnect(napi_env env, napi_callback_info info); napi_value StartDiscoverDevices(napi_env env, napi_callback_info info); napi_value StopDiscoverDevices(napi_env env, napi_callback_info info); -napi_value StartP2pListen(napi_env env, napi_callback_info info); -napi_value StopP2pListen(napi_env env, napi_callback_info info); napi_value DeletePersistentGroup(napi_env env, napi_callback_info info); -napi_value SetP2pDeviceName(napi_env env, napi_callback_info info); +napi_value SetDeviceName(napi_env env, napi_callback_info info); class QueryP2pDeviceAsyncContext : public AsyncContext { public: diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp index 3677e26effc65f3cc7a7fe515d906aa0741c9a93..1e8fa91f8dcaa43f487ede882fc6082125ea90cb 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp @@ -16,6 +16,7 @@ #include "wifi_napi_utils.h" #include "securec.h" #include "wifi_logger.h" +#include "context.h" namespace OHOS { namespace Wifi { @@ -314,5 +315,15 @@ napi_value DoAsyncWork(const napi_env& env, AsyncContext *asyncContext, return promise; } } + +std::string JsAbilityGetBundleName() +{ + std::shared_ptr appContext = AbilityRuntime::Context::GetApplicationContext(); + if (appContext == nullptr) { + WIFI_LOGE("Get boundle name context is null"); + return ""; + } + return appContext->GetBundleName(); +} } // namespace Wifi } // namespace OHOS diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h index e5d23b1f0fa27bbe8e95d7cdb53739298666c579..355129048961df764410236a02ff97208ce9909f 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h @@ -20,6 +20,7 @@ #include #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "wifi_hisysevent.h" namespace OHOS { namespace Wifi { @@ -92,6 +93,8 @@ enum class SecTypeJs { SEC_TYPE_PSK = 3, /* Pre-shared key (PSK) */ SEC_TYPE_SAE = 4, /* Simultaneous Authentication of Equals (SAE) */ }; + +std::string JsAbilityGetBundleName(); } // namespace Wifi } // namespace OHOS diff --git a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_device_callback_stub.cpp b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_device_callback_stub.cpp index 151f1ddeab9fb6e8b5b00b9b2e76f995d86049d8..10d2d8f089ee0893fab90e8db0642f318b2239d3 100644 --- a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_device_callback_stub.cpp +++ b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_device_callback_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 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 @@ -13,10 +13,11 @@ * limitations under the License. */ #include "wifi_device_callback_stub.h" +#include "define.h" +#include "wifi_hisysevent.h" #include "wifi_logger.h" #include "wifi_msg.h" #include "wifi_errcode.h" -#include "define.h" DEFINE_WIFILOG_LABEL("WifiDeviceCallBackStub"); namespace OHOS { @@ -95,6 +96,7 @@ void WifiDeviceCallBackStub::OnWifiStateChanged(int state) if (callback_) { callback_->OnWifiStateChanged(state); } + WriteWifiEventReceivedHiSysEvent(HISYS_STA_POWER_STATE_CHANGE, state); } void WifiDeviceCallBackStub::OnWifiConnectionChanged(int state, const WifiLinkedInfo &info) @@ -103,6 +105,7 @@ void WifiDeviceCallBackStub::OnWifiConnectionChanged(int state, const WifiLinked if (callback_) { callback_->OnWifiConnectionChanged(state, info); } + WriteWifiEventReceivedHiSysEvent(HISYS_STA_CONN_STATE_CHANGE, state); } void WifiDeviceCallBackStub::OnWifiRssiChanged(int rssi) @@ -111,6 +114,7 @@ void WifiDeviceCallBackStub::OnWifiRssiChanged(int rssi) if (callback_) { callback_->OnWifiRssiChanged(rssi); } + WriteWifiEventReceivedHiSysEvent(HISYS_STA_RSSI_STATE_CHANGE, rssi); } void WifiDeviceCallBackStub::OnWifiWpsStateChanged(int state, const std::string &pinCode) diff --git a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_hotspot_callback_stub.cpp b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_hotspot_callback_stub.cpp index 1de0a6eca4d1aba78c3221f0337277191f0e8342..9d623ab3ec52f67af3e11f7fd2d40cfbea58674f 100644 --- a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_hotspot_callback_stub.cpp +++ b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_hotspot_callback_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 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 @@ -14,9 +14,10 @@ */ #include "wifi_hotspot_callback_stub.h" +#include "define.h" +#include "wifi_hisysevent.h" #include "wifi_logger.h" #include "wifi_msg.h" -#include "define.h" DEFINE_WIFILOG_HOTSPOT_LABEL("WifiHotspotCallbackStub"); namespace OHOS { @@ -100,7 +101,6 @@ void WifiHotspotCallbackStub::RegisterCallBack(const sptr WIFI_LOGD("RegisterCallBack:callBack is nullptr!"); } userCallback_ = callBack; - return; } void WifiHotspotCallbackStub::OnHotspotStateChanged(int state) @@ -109,6 +109,7 @@ void WifiHotspotCallbackStub::OnHotspotStateChanged(int state) if (userCallback_) { userCallback_->OnHotspotStateChanged(state); } + WriteWifiEventReceivedHiSysEvent(HISYS_HOTSPOT_STATE_CHANGE, state); } void WifiHotspotCallbackStub::OnHotspotStaJoin(const StationInfo &info) @@ -117,6 +118,7 @@ void WifiHotspotCallbackStub::OnHotspotStaJoin(const StationInfo &info) if (userCallback_) { userCallback_->OnHotspotStaJoin(info); } + WriteWifiEventReceivedHiSysEvent(HISYS_HOTSPOT_STA_JOIN, HISYS_EVENT_DEFAULT_VALUE); } void WifiHotspotCallbackStub::OnHotspotStaLeave(const StationInfo &info) @@ -125,6 +127,7 @@ void WifiHotspotCallbackStub::OnHotspotStaLeave(const StationInfo &info) if (userCallback_) { userCallback_->OnHotspotStaLeave(info); } + WriteWifiEventReceivedHiSysEvent(HISYS_HOTSPOT_STA_LEAVE, HISYS_EVENT_DEFAULT_VALUE); } bool WifiHotspotCallbackStub::IsRemoteDied() const diff --git a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_p2p_callback_stub.cpp b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_p2p_callback_stub.cpp index 430bfc37e2a0cef06e2c1af72a4a4de1fb0f4c80..b1e34cfd9f051b4733d8bfc655b2a8a983aeef71 100644 --- a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_p2p_callback_stub.cpp +++ b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_p2p_callback_stub.cpp @@ -15,12 +15,12 @@ #include "wifi_p2p_callback_stub.h" #include "define.h" -#include "wifi_p2p_msg.h" #include "wifi_errcode.h" +#include "wifi_hisysevent.h" #include "wifi_logger.h" +#include "wifi_p2p_msg.h" DEFINE_WIFILOG_P2P_LABEL("WifiP2pCallbackStub"); - namespace OHOS { namespace Wifi { WifiP2pCallbackStub::WifiP2pCallbackStub() : userCallback_(nullptr), mRemoteDied(false) @@ -93,7 +93,7 @@ void WifiP2pCallbackStub::OnP2pStateChanged(int state) if (userCallback_) { userCallback_->OnP2pStateChanged(state); } - return; + WriteWifiEventReceivedHiSysEvent(HISYS_P2P_STATE_CHANGE, state); } void WifiP2pCallbackStub::OnP2pPersistentGroupsChanged(void) @@ -102,7 +102,7 @@ void WifiP2pCallbackStub::OnP2pPersistentGroupsChanged(void) if (userCallback_) { userCallback_->OnP2pPersistentGroupsChanged(); } - return; + WriteWifiEventReceivedHiSysEvent(HISYS_P2P_PERSISTENT_GROUP_CHANGE, HISYS_EVENT_DEFAULT_VALUE); } void WifiP2pCallbackStub::OnP2pThisDeviceChanged(const WifiP2pDevice &device) @@ -111,7 +111,7 @@ void WifiP2pCallbackStub::OnP2pThisDeviceChanged(const WifiP2pDevice &device) if (userCallback_) { userCallback_->OnP2pThisDeviceChanged(device); } - return; + WriteWifiEventReceivedHiSysEvent(HISYS_P2P_DEVICE_STATE_CHANGE, HISYS_EVENT_DEFAULT_VALUE); } void WifiP2pCallbackStub::OnP2pPeersChanged(const std::vector &device) @@ -120,7 +120,7 @@ void WifiP2pCallbackStub::OnP2pPeersChanged(const std::vector &de if (userCallback_) { userCallback_->OnP2pPeersChanged(device); } - return; + WriteWifiEventReceivedHiSysEvent(HISYS_P2P_PEER_DEVICE_CHANGE, HISYS_EVENT_DEFAULT_VALUE); } void WifiP2pCallbackStub::OnP2pServicesChanged(const std::vector &srvInfo) @@ -129,7 +129,6 @@ void WifiP2pCallbackStub::OnP2pServicesChanged(const std::vectorOnP2pServicesChanged(srvInfo); } - return; } void WifiP2pCallbackStub::OnP2pConnectionChanged(const WifiP2pLinkedInfo &info) @@ -138,7 +137,7 @@ void WifiP2pCallbackStub::OnP2pConnectionChanged(const WifiP2pLinkedInfo &info) if (userCallback_) { userCallback_->OnP2pConnectionChanged(info); } - return; + WriteWifiEventReceivedHiSysEvent(HISYS_P2P_CONN_STATE_CHANGE, static_cast(info.GetConnectState())); } void WifiP2pCallbackStub::OnP2pDiscoveryChanged(bool isChange) @@ -147,7 +146,7 @@ void WifiP2pCallbackStub::OnP2pDiscoveryChanged(bool isChange) if (userCallback_) { userCallback_->OnP2pDiscoveryChanged(isChange); } - return; + WriteWifiEventReceivedHiSysEvent(HISYS_P2P_DISCOVERY_CHANGE, isChange); } void WifiP2pCallbackStub::OnP2pActionResult(P2pActionCallback action, ErrCode code) @@ -156,7 +155,6 @@ void WifiP2pCallbackStub::OnP2pActionResult(P2pActionCallback action, ErrCode co if (userCallback_) { userCallback_->OnP2pActionResult(action, code); } - return; } void WifiP2pCallbackStub::RemoteOnP2pStateChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -165,7 +163,6 @@ void WifiP2pCallbackStub::RemoteOnP2pStateChanged(uint32_t code, MessageParcel & int state = data.ReadInt32(); OnP2pStateChanged(state); reply.WriteInt32(0); - return; } void WifiP2pCallbackStub::RemoteOnP2pPersistentGroupsChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -173,7 +170,6 @@ void WifiP2pCallbackStub::RemoteOnP2pPersistentGroupsChanged(uint32_t code, Mess WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize()); OnP2pPersistentGroupsChanged(); reply.WriteInt32(0); - return; } void WifiP2pCallbackStub::ReadWifiP2pDeviceData(MessageParcel &data, WifiP2pDevice &device) @@ -192,7 +188,6 @@ void WifiP2pCallbackStub::ReadWifiP2pDeviceData(MessageParcel &data, WifiP2pDevi device.SetWpsConfigMethod(data.ReadInt32()); device.SetDeviceCapabilitys(data.ReadInt32()); device.SetGroupCapabilitys(data.ReadInt32()); - return; } void WifiP2pCallbackStub::RemoteOnP2pThisDeviceChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -202,7 +197,6 @@ void WifiP2pCallbackStub::RemoteOnP2pThisDeviceChanged(uint32_t code, MessagePar ReadWifiP2pDeviceData(data, config); OnP2pThisDeviceChanged(config); reply.WriteInt32(0); - return; } void WifiP2pCallbackStub::RemoteOnP2pPeersChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -217,7 +211,6 @@ void WifiP2pCallbackStub::RemoteOnP2pPeersChanged(uint32_t code, MessageParcel & } OnP2pPeersChanged(device); reply.WriteInt32(0); - return; } void WifiP2pCallbackStub::RemoteOnP2pServicesChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -240,7 +233,6 @@ void WifiP2pCallbackStub::RemoteOnP2pServicesChanged(uint32_t code, MessageParce } OnP2pServicesChanged(srvInfo); reply.WriteInt32(0); - return; } void WifiP2pCallbackStub::RemoteOnP2pConnectionChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -252,7 +244,6 @@ void WifiP2pCallbackStub::RemoteOnP2pConnectionChanged(uint32_t code, MessagePar info.SetIsGroupOwnerAddress(data.ReadCString()); OnP2pConnectionChanged(info); reply.WriteInt32(0); - return; } void WifiP2pCallbackStub::RemoteOnP2pDiscoveryChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -261,7 +252,6 @@ void WifiP2pCallbackStub::RemoteOnP2pDiscoveryChanged(uint32_t code, MessageParc bool isChange = data.ReadBool(); OnP2pDiscoveryChanged(isChange); reply.WriteInt32(0); - return; } void WifiP2pCallbackStub::RemoteOnP2pActionResult(uint32_t code, MessageParcel &data, MessageParcel &reply) @@ -271,7 +261,6 @@ void WifiP2pCallbackStub::RemoteOnP2pActionResult(uint32_t code, MessageParcel & ErrCode errCode = static_cast(data.ReadInt32()); OnP2pActionResult(action, errCode); reply.WriteInt32(0); - return; } } // namespace Wifi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_scan_callback_stub.cpp b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_scan_callback_stub.cpp index cd1df8c40180f0c6c7678f4f6ecc1573f7f547ca..faa0a98a6c7f865c42e3d01b6ab80e4b23cdfee0 100644 --- a/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_scan_callback_stub.cpp +++ b/interfaces/innerkits/native_cpp/wifi_standard/src/wifi_scan_callback_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Huawei Device Co., Ltd. + * Copyright (C) 2021-2022 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 @@ -12,13 +12,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "wifi_scan_callback_stub.h" +#include "define.h" +#include "wifi_hisysevent.h" #include "wifi_logger.h" #include "wifi_msg.h" -#include "define.h" DEFINE_WIFILOG_SCAN_LABEL("WifiScanCallbackStub"); - namespace OHOS { namespace Wifi { WifiScanCallbackStub::WifiScanCallbackStub() : userCallback_(nullptr), mRemoteDied(false) @@ -80,6 +81,7 @@ void WifiScanCallbackStub::OnWifiScanStateChanged(int state) if (userCallback_) { userCallback_->OnWifiScanStateChanged(state); } + WriteWifiEventReceivedHiSysEvent(HISYS_STA_SCAN_STATE_CHANGE, state); } int WifiScanCallbackStub::RemoteOnWifiScanStateChanged(uint32_t code, MessageParcel &data, MessageParcel &reply) diff --git a/interfaces/kits/jskits/@ohos.wifi.d.ts b/interfaces/kits/jskits/@ohos.wifi.d.ts index a503f80f55ceb8519a489c0b87231ec4b48d6e7a..b0301b8f433411c6c7bc8fe8a078799707127184 100755 --- a/interfaces/kits/jskits/@ohos.wifi.d.ts +++ b/interfaces/kits/jskits/@ohos.wifi.d.ts @@ -19,8 +19,6 @@ import { AsyncCallback, Callback } from "./basic"; * Provides methods to operate or manage Wi-Fi. * * @since 6 - * @SysCap SystemCapability.Communication.WiFi - * @devices phone, tablet, tv, wearable, car * @import import wifi from '@ohos.wifi'; */ declare namespace wifi { @@ -30,6 +28,7 @@ declare namespace wifi { * @return Returns {@code true} if the operation is successful; returns {@code false} otherwise. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function enableWifi(): boolean; @@ -40,6 +39,7 @@ declare namespace wifi { * @return Returns {@code true} if the operation is successful; returns {@code false} otherwise. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function disableWifi(): boolean; @@ -50,17 +50,19 @@ declare namespace wifi { * @return Returns {@code true} if the Wi-Fi is active; returns {@code false} otherwise. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA */ function isWifiActive(): boolean; /** - * Scans Wi-Fi hotspots with parameters. + * Scans Wi-Fi hotspots. * *

This API works in asynchronous mode.

* * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA */ function scan(): boolean; @@ -70,6 +72,7 @@ declare namespace wifi { * @return Returns information about scanned Wi-Fi hotspots if any. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA */ function getScanInfos(): Promise>; function getScanInfos(callback: AsyncCallback>): void; @@ -79,10 +82,11 @@ declare namespace wifi { * *

The configuration will be updated when the configuration is added.

* + * @param config Indicates the device configuration for connection to the Wi-Fi network. * @return Returns {@code networkId} if the configuration is added; returns {@code -1} otherwise. * - * @devices phone, tablet * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function addDeviceConfig(config: WifiDeviceConfig): Promise; @@ -95,6 +99,7 @@ declare namespace wifi { * @return Returns {@code true} if the network connection is successful; returns {@code false} otherwise. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function connectToNetwork(networkId: number): boolean; @@ -105,8 +110,8 @@ declare namespace wifi { * @param config Indicates the device configuration for connection to the Wi-Fi network. * @return Returns {@code true} if the network connection is successful; returns {@code false} otherwise. * - * @devices phone, tablet * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function connectToDevice(config: WifiDeviceConfig): boolean; @@ -117,6 +122,7 @@ declare namespace wifi { * @return Returns {@code true} for disconnecting network success, returns {@code false} otherwise. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function disconnect(): boolean; @@ -129,6 +135,7 @@ declare namespace wifi { * @return Returns Wi-Fi signal level ranging from 0 to 4. * * @since 6 + * @syscap SystemCapability.Communication.WiFi.STA */ function getSignalLevel(rssi: number, band: number): number; @@ -137,6 +144,7 @@ declare namespace wifi { * * @return Returns the Wi-Fi connection information. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function getLinkedInfo(): Promise; function getLinkedInfo(callback: AsyncCallback): void; @@ -146,6 +154,7 @@ declare namespace wifi { * * @return Returns {@code true} if a Wi-Fi connection has been set up; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function isConnected(): boolean; @@ -156,6 +165,7 @@ declare namespace wifi { * * @return Returns the features supported by this device. * @since 7 + * @syscap SystemCapability.Communication.WiFi.Core * @systemapi Hide this for inner system use. */ function getSupportedFeatures(): number; @@ -166,6 +176,7 @@ declare namespace wifi { * @param featureId Indicates the ID of the feature. * @return Returns {@code true} if this device supports the specified feature; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.Core */ function isFeatureSupported(featureId: number): boolean; @@ -176,6 +187,7 @@ declare namespace wifi { * * @return Returns the MAC address of the Wi-Fi device. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function getDeviceMacAddress(): string[]; @@ -187,6 +199,7 @@ declare namespace wifi { * * @return Returns the IP information of the Wi-Fi connection. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function getIpInfo(): IpInfo; @@ -195,6 +208,7 @@ declare namespace wifi { * * @return Returns the country code of this device. * @since 7 + * @syscap SystemCapability.Communication.WiFi.Core */ function getCountryCode(): string; @@ -203,6 +217,7 @@ declare namespace wifi { * * @return {@code true} if the Wi-Fi network is re-associate successfully. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function reassociate(): boolean; @@ -212,6 +227,7 @@ declare namespace wifi { * * @return {@code true} if the Wi-Fi network is re-connect successfully. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function reconnect(): boolean; @@ -223,6 +239,7 @@ declare namespace wifi { * * @return Returns the list of all existing Wi-Fi configurations you created on your application. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function getDeviceConfigs(): Array; @@ -235,6 +252,7 @@ declare namespace wifi { * @return Returns the network ID in the updated Wi-Fi configuration if the update is successful; * returns {@code -1} if the specified Wi-Fi configuration is not contained in the list. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function updateNetwork(config: WifiDeviceConfig): number; @@ -247,6 +265,7 @@ declare namespace wifi { * @param netId Identifies the network to disable. * @return Returns {@code true} if the specified network is disabled; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function disableNetwork(netId: number): boolean; @@ -257,6 +276,7 @@ declare namespace wifi { * @return Returns {@code true} if all the saved Wi-Fi configurations are removed; * returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function removeAllNetwork(): boolean; @@ -272,6 +292,7 @@ declare namespace wifi { * which can be obtained using the {@link #addDeviceConfig} or {@link #getLinkedInfo} method. * @return Returns {@code true} if the Wi-Fi network is deleted successfully; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA * @systemapi Hide this for inner system use. */ function removeDevice(id: number): boolean; @@ -283,6 +304,7 @@ declare namespace wifi { * * @return Returns {@code true} if this method is called successfully; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function enableHotspot(): boolean; @@ -294,6 +316,7 @@ declare namespace wifi { * * @return Returns {@code true} if this method is called successfully; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function disableHotspot(): boolean; @@ -303,6 +326,7 @@ declare namespace wifi { * * @return Returns {@code true} if Wi-Fi hotspot is enabled; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function isHotspotActive(): boolean; @@ -317,6 +341,7 @@ declare namespace wifi { * If {@code securityType} is not {@code open}, {@code preSharedKey} must be available and correct. * @return Returns {@code true} if the method is called successfully; returns {@code false} otherwise. * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function setHotspotConfig(config: HotspotConfig): boolean; @@ -326,6 +351,7 @@ declare namespace wifi { * * @return Returns the configuration of an existing or enabled Wi-Fi hotspot. * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function getHotspotConfig(): HotspotConfig; @@ -337,6 +363,7 @@ declare namespace wifi { * * @return Returns the list of clients that are connected to the Wi-Fi hotspot. * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function getStations(): Array; @@ -346,6 +373,7 @@ declare namespace wifi { * * @return Returns the P2P connection information. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function getP2pLinkedInfo(): Promise; function getP2pLinkedInfo(callback: AsyncCallback): void; @@ -355,6 +383,7 @@ declare namespace wifi { * * @return Returns the current group information. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function getCurrentGroup(): Promise; function getCurrentGroup(callback: AsyncCallback): void; @@ -364,6 +393,7 @@ declare namespace wifi { * * @return Returns the found devices list. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function getP2pDevices(): Promise; function getP2pDevices(callback: AsyncCallback): void; @@ -371,8 +401,10 @@ declare namespace wifi { /** * Creates a P2P group. * + * @param config Indicates the configuration for creating a group. * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function createGroup(config: WifiP2PConfig): boolean; @@ -381,14 +413,17 @@ declare namespace wifi { * * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function removeGroup(): boolean; /** * Initiates a P2P connection to a device with the specified configuration. * + * @param config Indicates the configuration for connecting to a specific group. * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function p2pConnect(config: WifiP2PConfig): boolean; @@ -397,6 +432,7 @@ declare namespace wifi { * * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function p2pCancelConnect(): boolean; @@ -405,6 +441,7 @@ declare namespace wifi { * * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function startDiscoverDevices(): boolean; @@ -413,22 +450,27 @@ declare namespace wifi { * * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ - function stopDiscoveryDevices(): boolean; + function stopDiscoverDevices(): boolean; /** * Deletes the persistent P2P group with the specified network ID. * + * @param netId Indicates the network ID of the group to be deleted. * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function deletePersistentGroup(netId: number): boolean; /** * Sets the name of the Wi-Fi P2P device. * + * @param devName Indicates the name to be set. * @return Returns {@code true} if the scanning is successful; returns {@code false} otherwise. * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function setDeviceName(devName: string): boolean; @@ -437,6 +479,7 @@ declare namespace wifi { * * @return Returns 0: inactive, 1: active, 2: activating, 3: deactivating * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function on(type: "wifiStateChange", callback: Callback): void; @@ -446,6 +489,7 @@ declare namespace wifi { *

All callback functions will be deregistered If there is no specific callback parameter.

* * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function off(type: "wifiStateChange", callback?: Callback): void; @@ -454,6 +498,7 @@ declare namespace wifi { * * @return Returns 0: disconnected, 1: connected * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function on(type: "wifiConnectionChange", callback: Callback): void; @@ -463,6 +508,7 @@ declare namespace wifi { *

All callback functions will be deregistered If there is no specific callback parameter.

* * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function off(type: "wifiConnectionChange", callback?: Callback): void; @@ -471,6 +517,7 @@ declare namespace wifi { * * @return Returns 0: scan fail, 1: scan success * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function on(type: "wifiScanStateChange", callback: Callback): void; @@ -480,6 +527,7 @@ declare namespace wifi { *

All callback functions will be deregistered If there is no specific callback parameter.

* * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function off(type: "wifiScanStateChange", callback?: Callback): void; @@ -488,6 +536,7 @@ declare namespace wifi { * * @return Returns RSSI value in dBm * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function on(type: "wifiRssiChange", callback: Callback): void; @@ -497,6 +546,7 @@ declare namespace wifi { *

All callback functions will be deregistered If there is no specific callback parameter.

* * @since 7 + * @syscap SystemCapability.Communication.WiFi.STA */ function off(type: "wifiRssiChange", callback?: Callback): void; @@ -505,6 +555,7 @@ declare namespace wifi { * * @return Returns 0: inactive, 1: active, 2: activating, 3: deactivating * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP */ function on(type: "hotspotStateChange", callback: Callback): void; @@ -514,6 +565,7 @@ declare namespace wifi { *

All callback functions will be deregistered If there is no specific callback parameter.

* * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP */ function off(type: "hotspotStateChange", callback?: Callback): void; @@ -522,6 +574,7 @@ declare namespace wifi { * * @return Returns StationInfo * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function on(type: "hotspotStaJoin", callback: Callback): void; @@ -532,6 +585,7 @@ declare namespace wifi { *

All callback functions will be deregistered If there is no specific callback parameter.

* * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function off(type: "hotspotStaJoin", callback?: Callback): void; @@ -541,6 +595,7 @@ declare namespace wifi { * * @return Returns {@link #StationInfo} object * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function on(type: "hotspotStaLeave", callback: Callback): void; @@ -550,6 +605,7 @@ declare namespace wifi { * * @return Returns {@link #StationInfo} object * @since 7 + * @syscap SystemCapability.Communication.WiFi.AP * @systemapi Hide this for inner system use. */ function off(type: "hotspotStaLeave", callback?: Callback): void; @@ -559,6 +615,7 @@ declare namespace wifi { * * @return Returns 1: idle, 2: starting, 3:started, 4: closing, 5: closed * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function on(type: "p2pStateChange", callback: Callback): void; @@ -566,6 +623,7 @@ declare namespace wifi { * Unsubscribe P2P status change events. * * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function off(type: "p2pStateChange", callback?: Callback): void; @@ -574,6 +632,7 @@ declare namespace wifi { * * @return Returns WifiP2pLinkedInfo * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function on(type: "p2pConnectionChange", callback: AsyncCallback): void; @@ -581,6 +640,7 @@ declare namespace wifi { * Unsubscribe P2P connection change events. * * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function off(type: "p2pConnectionChange", callback?: AsyncCallback): void; @@ -589,6 +649,7 @@ declare namespace wifi { * * @return Returns WifiP2pDevice * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function on(type: "p2pDeviceChange", callback: AsyncCallback): void; @@ -597,6 +658,7 @@ declare namespace wifi { * * @return Returns WifiP2pDevice * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function off(type: "p2pDeviceChange", callback?: AsyncCallback): void; @@ -605,6 +667,7 @@ declare namespace wifi { * * @return Returns WifiP2pDevice[] * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function on(type: "p2pPeerDeviceChange", callback: AsyncCallback): void; @@ -612,6 +675,7 @@ declare namespace wifi { * Unsubscribe P2P peer device change events. * * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function off(type: "p2pPeerDeviceChange", callback?: AsyncCallback): void; @@ -620,6 +684,7 @@ declare namespace wifi { * * @return Returns void * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function on(type: "p2pPersistentGroupChange", callback: Callback): void; @@ -627,6 +692,7 @@ declare namespace wifi { * Unsubscribe P2P persistent group change events. * * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function off(type: "p2pPersistentGroupChange", callback?: Callback): void; @@ -635,6 +701,7 @@ declare namespace wifi { * * @return Returns 0: initial state, 1: discovery succeeded * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function on(type: "p2pDiscoveryChange", callback: Callback): void; @@ -642,6 +709,7 @@ declare namespace wifi { * Unsubscribe P2P discovery events. * * @since 8 + * @syscap SystemCapability.Communication.WiFi.P2P */ function off(type: "p2pDiscoveryChange", callback?: Callback): void; @@ -933,10 +1001,10 @@ declare namespace wifi { primaryDeviceType: string; /** Device status */ - devStatus: P2pDeviceStatus; + deviceStatus: P2pDeviceStatus; - /** Device group capability */ - groupCapability: number; + /** Device group capabilitys */ + groupCapabilitys: number; } /** diff --git a/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn b/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn index 5c2924f2e0c35a36958685ab1b4a6420d443df35..34d55120dcdcd9d8b119adf26605573b8236945b 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn +++ b/services/wifi_standard/wifi_framework/wifi_manage/BUILD.gn @@ -95,6 +95,7 @@ ohos_source_set("wifi_device_service_impl") { ":wifi_manager_service_header", ] + deps = [ ":wifi_p2p_service_impl" ] external_deps = [ "ipc:ipc_core" ] } ohos_source_set("wifi_hotspot_service_impl") { diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp index a79d56553cdfc50240cab8d287e86c2bb44eb39a..ac06f8584555f8d9f2c391989039dae8d682074f 100644 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.cpp @@ -110,14 +110,13 @@ ErrCode WifiDeviceServiceImpl::EnableWifi() } if (!WifiConfigCenter::GetInstance().SetWifiMidState(curState, WifiOprMidState::OPENING)) { - WIFI_LOGI("set wifi mid state opening failed! may be other activity has been operated"); + WIFI_LOGI("set wifi mid state opening failed!"); return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; } errCode = WIFI_OPT_FAILED; do { if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_STA) < 0) { - WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_STA); break; } IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(); @@ -144,6 +143,12 @@ ErrCode WifiDeviceServiceImpl::EnableWifi() return errCode; } WifiSettings::GetInstance().SyncWifiConfig(); + + sptr p2pService = WifiP2pServiceImpl::GetInstance(); + if (p2pService != nullptr && p2pService->EnableP2p() != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Enable P2p failed, ret %{public}d!", static_cast(errCode)); + return WIFI_OPT_FAILED; + } return WIFI_OPT_SUCCESS; } @@ -154,6 +159,11 @@ ErrCode WifiDeviceServiceImpl::DisableWifi() return WIFI_OPT_PERMISSION_DENIED; } + sptr p2pService = WifiP2pServiceImpl::GetInstance(); + if (p2pService != nullptr && p2pService->DisableP2p() != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Disable P2p failed!"); + } + WifiOprMidState curState = WifiConfigCenter::GetInstance().GetWifiMidState(); if (curState != WifiOprMidState::RUNNING) { WIFI_LOGI("current wifi state is %{public}d", static_cast(curState)); diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.h b/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.h index 4429e5302c36fbb4927376b0e9b2c26af99b3680..89b1ae75679dc50730e8a7adee04f1197e6e3a33 100644 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.h +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_device_service_impl.h @@ -22,13 +22,10 @@ #include "system_ability.h" #include "wifi_device_stub.h" #include "iremote_object.h" +#include "wifi_p2p_service_impl.h" namespace OHOS { namespace Wifi { -enum ServiceRunningState { - STATE_NOT_START, - STATE_RUNNING -}; class WifiDeviceServiceImpl : public SystemAbility, public WifiDeviceStub { DECLARE_SYSTEM_ABILITY(WifiDeviceServiceImpl); public: diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp index ca65170295ad1baed4856547e37e23461b9fa98d..cb95d972608330fb7a6f51222872f35dc7764ddd 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp @@ -51,7 +51,6 @@ WifiManager::WifiManager() : mInitStatus(INIT_UNKNOWN), mSupportedFeatures(0) WifiManager::~WifiManager() {} - void WifiManager::AutoStartStaService(void) { WifiOprMidState staState = WifiConfigCenter::GetInstance().GetWifiMidState(); @@ -90,6 +89,44 @@ void WifiManager::AutoStartStaService(void) return; } +void WifiManager::AutoStartP2pService(void) +{ + WifiOprMidState p2pState = WifiConfigCenter::GetInstance().GetP2pMidState(); + if (p2pState == WifiOprMidState::CLOSED) { + if (!WifiConfigCenter::GetInstance().SetP2pMidState(p2pState, WifiOprMidState::OPENING)) { + WIFI_LOGD("set p2p mid state opening failed!"); + return; + } + } + ErrCode ret = WIFI_OPT_FAILED; + do { + if (WifiServiceManager::GetInstance().CheckAndEnforceService(WIFI_SERVICE_P2P) < 0) { + WIFI_LOGE("Load %{public}s service failed!", WIFI_SERVICE_P2P); + break; + } + IP2pService *pService = WifiServiceManager::GetInstance().GetP2pServiceInst(); + if (pService == nullptr) { + WIFI_LOGE("Create %{public}s service failed!", WIFI_SERVICE_P2P); + break; + } + ret = pService->RegisterP2pServiceCallbacks(WifiManager::GetInstance().GetP2pCallback()); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Register p2p service callback failed!"); + break; + } + ret = pService->EnableP2p(); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("service EnableP2p failed, ret %{public}d!", static_cast(ret)); + break; + } + } while (false); + if (ret != WIFI_OPT_SUCCESS) { + WifiConfigCenter::GetInstance().SetP2pMidState(WifiOprMidState::OPENING, WifiOprMidState::CLOSED); + WifiServiceManager::GetInstance().UnloadService(WIFI_SERVICE_P2P); + } + return; +} + void WifiManager::AutoStartScanService(void) { if (!WifiConfigCenter::GetInstance().IsScanAlwaysActive()) { @@ -145,8 +182,9 @@ int WifiManager::Init() return -1; } if (WifiConfigCenter::GetInstance().GetStaLastRunState()) { /* Automatic startup upon startup */ - WIFI_LOGE("AutoStartStaService"); + WIFI_LOGE("AutoStartStaApService"); AutoStartStaService(); + AutoStartP2pService(); } else { /** * The sta service automatically starts upon startup. After the sta diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h b/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h index 8bfe4d688d1bceb260f9d061a1b4ce8b588a17fe..25f390e0523f087627b7147f9809295da73eb751 100644 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.h @@ -156,8 +156,9 @@ private: static void DealP2pDiscoveryChanged(bool bState); static void DealP2pGroupsChanged(void); static void DealP2pActionResult(P2pActionCallback action, ErrCode code); - static void AutoStartStaService(); - static void AutoStartScanService(); + static void AutoStartStaService(void); + static void AutoStartP2pService(void); + static void AutoStartScanService(void); private: std::thread mCloseServiceThread; diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp index 68c12751dea2faa481fc2730ab3d8191ae94d74e..e95ea68daf4e55ad0687da07fdef66a4c01a260b 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp @@ -388,6 +388,13 @@ void StaStateMachine::StartWifiProcess() /* callback the InterfaceService that wifi is enabled successfully. */ WifiSettings::GetInstance().SetWifiState(static_cast(WifiState::ENABLED)); staCallback.OnStaOpenRes(OperateResState::OPEN_WIFI_SUCCEED); + /* Sets the MAC address of WifiSettings. */ + std::string mac; + if ((WifiStaHalInterface::GetInstance().GetStaDeviceMacAddress(mac)) == WIFI_IDL_OPT_OK) { + WifiSettings::GetInstance().SetMacAddress(mac); + } else { + WIFI_LOGI("GetStaDeviceMacAddress failed!"); + } WifiNetAgent::GetInstance().RegisterNetSupplier(); WifiNetAgent::GetInstance().RegisterNetSupplierCallback(staCallback); /* Initialize Connection Information. */ diff --git a/utils/inc/wifi_hisysevent.h b/utils/inc/wifi_hisysevent.h new file mode 100644 index 0000000000000000000000000000000000000000..253812776b8b881fab742cd187451958fde363ff --- /dev/null +++ b/utils/inc/wifi_hisysevent.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2022 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_HISYSEVENT_H +#define OHOS_WIFI_HISYSEVENT_H + +#include + +namespace OHOS { +namespace Wifi { +enum class WifiOperType { + ENABLE, + DISABLE +}; + +enum class WifiConnectionType { + CONNECT, + DISCONNECT +}; + +const int HISYS_EVENT_DEFAULT_VALUE = -1; + +const std::string HISYS_STA_POWER_STATE_CHANGE = "wifiStateChange"; +const std::string HISYS_STA_CONN_STATE_CHANGE = "wifiConnectionChange"; +const std::string HISYS_STA_SCAN_STATE_CHANGE = "wifiScanStateChange"; +const std::string HISYS_STA_RSSI_STATE_CHANGE = "wifiRssiChange"; +const std::string HISYS_HOTSPOT_STATE_CHANGE = "hotspotStateChange"; +const std::string HISYS_HOTSPOT_STA_JOIN = "hotspotStaJoin"; +const std::string HISYS_HOTSPOT_STA_LEAVE = "hotspotStaLeave"; +const std::string HISYS_P2P_STATE_CHANGE = "p2pStateChange"; +const std::string HISYS_P2P_CONN_STATE_CHANGE = "p2pConnectionChange"; +const std::string HISYS_P2P_DEVICE_STATE_CHANGE = "p2pDeviceChange"; +const std::string HISYS_P2P_PERSISTENT_GROUP_CHANGE = "p2pPersistentGroupChange"; +const std::string HISYS_P2P_PEER_DEVICE_CHANGE = "p2pPeerDeviceChange"; +const std::string HISYS_P2P_DISCOVERY_CHANGE = "p2pDiscoveryChange"; + +const std::string HISYS_SERVICE_TYPE_STA = "STA"; +const std::string HISYS_SERVICE_TYPE_AP = "AP"; +const std::string HISYS_SERVICE_TYPE_P2P = "P2P"; + +void WriteWifiStateHiSysEvent(const std::string& serviceType, WifiOperType operType); + +void WriteWifiConnectionHiSysEvent(const WifiConnectionType& type, const std::string& pkgName); + +void WriteWifiScanHiSysEvent(const int result, const std::string& pkgName); + +void WriteWifiEventReceivedHiSysEvent(const std::string& eventType, int value); +} // namespace Wifi +} // namespace OHOS +#endif \ No newline at end of file diff --git a/utils/src/BUILD.gn b/utils/src/BUILD.gn index fa472cc53198ad313990c6ff1e4aa2ebad74eb2a..fb69f397a5a2fb78645528b3bef458595c629122 100755 --- a/utils/src/BUILD.gn +++ b/utils/src/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2021 Huawei Device Co., Ltd. +# Copyright (C) 2021-2022 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 @@ -17,11 +17,21 @@ ohos_shared_library("wifi_utils") { install_enable = true include_dirs = [ "//foundation/communication/wifi/utils/inc", - "//utils/native/base/include", + "//base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include", + "//foundation/communication/wifi/services/wifi_standard/wifi_framework/common/log", ] - sources = [ "wifi_common_util.cpp" ] - deps = [ "//utils/native/base:utils" ] + sources = [ + "wifi_common_util.cpp", + "wifi_hisysevent.cpp", + ] + + deps = [ + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//utils/native/base:utils", + ] + + external_deps = [ "hisysevent_native:libhisysevent" ] cflags_cc = [ "-std=c++17", diff --git a/utils/src/wifi_hisysevent.cpp b/utils/src/wifi_hisysevent.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55eff627f8ca8855f7fc243b9c3061d9dc8582e9 --- /dev/null +++ b/utils/src/wifi_hisysevent.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2022 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_hisysevent.h" +#include "hisysevent.h" +#include "wifi_logger.h" + +namespace OHOS { +namespace Wifi { +DEFINE_WIFILOG_LABEL("WifiHiSysEvent"); + +template +static void WriteEvent(const std::string& eventType, Types... args) +{ + int ret = HiviewDFX::HiSysEvent::Write(HiviewDFX::HiSysEvent::Domain::COMMUNICATION, eventType, + HiviewDFX::HiSysEvent::EventType::STATISTIC, args...); + if (ret != 0) { + WIFI_LOGE("Write event fail: %{public}s", eventType.c_str()); + } +} + +void WriteWifiStateHiSysEvent(const std::string& serviceType, WifiOperType operType) +{ + WriteEvent("WIFI_STATE", "TYPE", serviceType, "OPER_TYPE", static_cast(operType)); +} + +void WriteWifiConnectionHiSysEvent(const WifiConnectionType& type, const std::string& pkgName) +{ + WriteEvent("WIFI_CONNECTION", "TYPE", static_cast(type), "PACKAGE_NAME", pkgName); +} + +void WriteWifiScanHiSysEvent(const int result, const std::string& pkgName) +{ + WriteEvent("WIFI_SCAN", "EXECUTE_RESULT", result, "PACKAGE_NAME", pkgName); +} + +void WriteWifiEventReceivedHiSysEvent(const std::string& eventType, int value) +{ + WriteEvent("WIFI_EVENT_RECEIVED", "EVENT_TYPE", eventType, "VALUE", value); +} +} // namespace Wifi +} // namespace OHOS \ No newline at end of file