diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp index dbf07abd17accba17faf017b3fe28d945c689b71..942934cabc50c1178c5860c5a202bbe209fcfe30 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp @@ -21,6 +21,7 @@ namespace OHOS { namespace Wifi { DEFINE_WIFILOG_LABEL("WifiNAPIDevice"); +static constexpr int DEFAULT_INVALID_VALUE = -1; std::unique_ptr wifiDevicePtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID); std::unique_ptr wifiScanPtr = WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID); @@ -108,10 +109,12 @@ static ErrCode NativeScanInfosToJsObj(const napi_env& env, SetValueUtf8String(env, "ssid", each.ssid.c_str(), eachObj); SetValueUtf8String(env, "bssid", each.bssid.c_str(), eachObj); + SetValueUtf8String(env, "capabilities", each.capabilities.c_str(), eachObj); SetValueInt32(env, "securityType", static_cast(SecurityTypeNativeToJs(each.securityType)), eachObj); SetValueInt32(env, "rssi", each.rssi, eachObj); SetValueInt32(env, "band", each.band, eachObj); SetValueInt32(env, "frequency", each.frequency, eachObj); + SetValueInt32(env, "channelWidth", static_cast(each.channelWidth), eachObj); SetValueInt64(env, "timestamp", each.timestamp, eachObj); napi_status status = napi_set_element(env, arrayResult, idx++, eachObj); @@ -159,27 +162,33 @@ static void ConvertEncryptionMode(const SecTypeJs& securityType, std::string& ke { switch (securityType) { case SecTypeJs::SEC_TYPE_OPEN: - keyMgmt = "NONE"; - break; - case SecTypeJs::SEC_TYPE_WEP: - keyMgmt = "WEP"; + keyMgmt = KEY_MGMT_NONE; break; case SecTypeJs::SEC_TYPE_PSK: - keyMgmt = "WPA-PSK"; + keyMgmt = KEY_MGMT_WPA_PSK; break; case SecTypeJs::SEC_TYPE_SAE: - keyMgmt = "SAE"; + keyMgmt = KEY_MGMT_SAE; break; default: - keyMgmt = "NONE"; + keyMgmt = KEY_MGMT_NONE; break; } } +static void ProcessPassphrase(const SecTypeJs& securityType, WifiDeviceConfig& cppConfig) +{ + if (securityType == SecTypeJs::SEC_TYPE_WEP) { + cppConfig.wepKeys[0] = cppConfig.preSharedKey; + cppConfig.wepTxKeyIndex = 0; + cppConfig.preSharedKey = ""; + } +} + static void JsObjToDeviceConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig) { JsObjectToString(env, object, "ssid", 33, cppConfig.ssid); /* 33: ssid max length is 32 + '\0' */ @@ -189,6 +198,15 @@ static void JsObjToDeviceConfig(const napi_env& env, const napi_value& object, W int type = static_cast(SecTypeJs::SEC_TYPE_INVALID); JsObjectToInt(env, object, "securityType", type); ConvertEncryptionMode(SecTypeJs(type), cppConfig.keyMgmt); + ProcessPassphrase(SecTypeJs(type), cppConfig); + /* "creatorUid" is not supported currently */ + /* "disableReason" is not supported currently */ + JsObjectToInt(env, object, "netId", cppConfig.networkId); + /* "randomMacType" is not supported currently */ + /* "randomMacAddr" is not supported currently */ + int ipType = static_cast(AssignIpMethod::UNASSIGNED); + JsObjectToInt(env, object, "ipType", ipType); + /* "staticIp" is not supported currently */ } napi_value AddDeviceConfig(napi_env env, napi_callback_info info) @@ -201,7 +219,7 @@ napi_value AddDeviceConfig(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); - + napi_valuetype valueType; napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1."); @@ -242,6 +260,106 @@ napi_value AddDeviceConfig(napi_env env, napi_callback_info info) return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); } +napi_value AddUntrustedConfig(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + size_t argc = 2; + napi_value argv[argc]; + napi_value thisVar = nullptr; + void *data = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1."); + + AddDeviceConfigContext *asyncContext = new AddDeviceConfigContext(env); + NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); + napi_create_string_latin1(env, "AddUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName); + + WifiDeviceConfig *config = new WifiDeviceConfig(); + if (config == nullptr) { + delete asyncContext; + return UndefinedNapiValue(env); + } + JsObjToDeviceConfig(env, argv[0], *config); + asyncContext->config = config; + + asyncContext->executeFunc = [&](void* data) -> void { + AddDeviceConfigContext *context = static_cast(data); + TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddUntrustedConfig"); + /* This interface is not supported currently */ + context->addResult = -1; + context->errorCode = WIFI_OPT_NOT_SUPPORTED; + }; + + asyncContext->completeFunc = [&](void* data) -> void { + AddDeviceConfigContext *context = static_cast(data); + /* This interface is not supported currently */ + napi_get_boolean(context->env, false, &context->result); + if (context->config != nullptr) { + delete context->config; + context->config = nullptr; + } + WIFI_LOGI("Push add untrusted device config result to client"); + }; + + size_t nonCallbackArgNum = 1; + return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); +} + +napi_value RemoveUntrustedConfig(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + size_t argc = 3; + napi_value argv[argc]; + napi_value thisVar = nullptr; + void *data = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1."); + + AddDeviceConfigContext *asyncContext = new AddDeviceConfigContext(env); + NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); + napi_create_string_latin1(env, "RemoveUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName); + + WifiDeviceConfig *config = new WifiDeviceConfig(); + if (config == nullptr) { + delete asyncContext; + return UndefinedNapiValue(env); + } + JsObjToDeviceConfig(env, argv[0], *config); + asyncContext->config = config; + + asyncContext->executeFunc = [&](void* data) -> void { + AddDeviceConfigContext *context = static_cast(data); + TRACE_FUNC_CALL_NAME("wifiDevicePtr->RemoveUntrustedConfig"); + /* This interface is not supported currently */ + context->addResult = -1; + context->errorCode = WIFI_OPT_NOT_SUPPORTED; + }; + + asyncContext->completeFunc = [&](void* data) -> void { + AddDeviceConfigContext *context = static_cast(data); + /* This interface is not supported currently */ + napi_get_boolean(context->env, false, &context->result); + if (context->config != nullptr) { + delete context->config; + context->config = nullptr; + } + WIFI_LOGI("Push remove untrusted device config result to client"); + }; + + size_t nonCallbackArgNum = 1; + return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); +} + napi_value ConnectToNetwork(napi_env env, napi_callback_info info) { TRACE_FUNC_CALL; @@ -349,7 +467,7 @@ napi_value ReConnect(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->ReConnect(), &result); + napi_get_boolean(env, wifiDevicePtr->ReConnect() == WIFI_OPT_SUCCESS, &result); WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, JsAbilityGetBundleName()); return result; } @@ -359,7 +477,7 @@ napi_value ReAssociate(napi_env env, napi_callback_info info) TRACE_FUNC_CALL; NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); napi_value result; - napi_get_boolean(env, wifiDevicePtr->ReAssociate(), &result); + napi_get_boolean(env, wifiDevicePtr->ReAssociate() == WIFI_OPT_SUCCESS, &result); WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, JsAbilityGetBundleName()); return result; } @@ -407,9 +525,7 @@ static void LinkedInfoToJs(const napi_env& env, WifiLinkedInfo& linkedInfo, napi SetValueInt32(env, "snr", linkedInfo.snr, result); SetValueUtf8String(env, "macAddress", linkedInfo.macAddress.c_str(), result); SetValueUnsignedInt32(env, "ipAddress", linkedInfo.ipAddress, result); - /* Check suppState is consistent with HOS */ SetValueInt32(env, "suppState", static_cast(linkedInfo.supplicantState), result); - /* Check connState is consistent with HOS */ SetValueInt32(env, "connState", static_cast(linkedInfo.connState), result); } @@ -514,19 +630,81 @@ napi_value GetCountryCode(napi_env env, napi_callback_info info) static SecTypeJs ConvertKeyMgmtToSecType(const std::string& keyMgmt) { std::map mapKeyMgmtToSecType = { - {"NONE", SecTypeJs::SEC_TYPE_OPEN}, - {"WEP", SecTypeJs::SEC_TYPE_WEP}, - {"WPA-PSK", SecTypeJs::SEC_TYPE_PSK}, - {"SAE", SecTypeJs::SEC_TYPE_SAE}, + {KEY_MGMT_NONE, SecTypeJs::SEC_TYPE_OPEN}, + {KEY_MGMT_WEP, SecTypeJs::SEC_TYPE_WEP}, + {KEY_MGMT_WPA_PSK, SecTypeJs::SEC_TYPE_PSK}, + {KEY_MGMT_SAE, SecTypeJs::SEC_TYPE_SAE}, }; std::map::iterator iter = mapKeyMgmtToSecType.find(keyMgmt); return iter == mapKeyMgmtToSecType.end() ? SecTypeJs::SEC_TYPE_OPEN : iter->second; } -static void DeviceConfigToJsArray(const napi_env& env, const std::vector& vecDeviceConfigs, +static void IpConfigToJs(const napi_env& env, const WifiIpConfig& wifiIpConfig, napi_value& ipCfgObj) +{ + SetValueInt32(env, "ipAddress", wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv4, ipCfgObj); + SetValueInt32(env, "gateway", wifiIpConfig.staticIpAddress.gateway.addressIpv4, ipCfgObj); + + const int DNS_NUM = 2; + napi_value dnsArray; + napi_create_array_with_length(env, DNS_NUM, &dnsArray); + std::vector vecDns = {wifiIpConfig.staticIpAddress.dnsServer1.addressIpv4, + wifiIpConfig.staticIpAddress.dnsServer2.addressIpv4}; + for (int i = 0; i != DNS_NUM; ++i) { + napi_value value; + napi_status status = napi_create_int32(env, vecDns[i], &value); + if (status != napi_ok) { + WIFI_LOGE("Ip config to js create int32 error!"); + return; + } + status = napi_set_element(env, dnsArray, i, value); + if (status != napi_ok) { + WIFI_LOGE("Ip config to js set element error: %{public}d", status); + return; + } + } + if (napi_set_named_property(env, ipCfgObj, "dnsServers", dnsArray) != napi_ok) { + WIFI_LOGE("Set dnsServers named property error!"); + } + + const int DOMAINS_NUM = 1; + napi_value domainsArray; + napi_create_array_with_length(env, DOMAINS_NUM, &domainsArray); + std::vector vecDomains = {wifiIpConfig.staticIpAddress.domains}; + for (int i = 0; i != DOMAINS_NUM; ++i) { + napi_value value; + napi_status status = napi_create_string_utf8(env, vecDomains[i].c_str(), NAPI_AUTO_LENGTH, &value); + if (status != napi_ok) { + WIFI_LOGE("Ip config to js create utf8 string error!"); + return; + } + status = napi_set_element(env, domainsArray, i, value); + if (status != napi_ok) { + WIFI_LOGE("Ip config to js set element error: %{public}d", status); + } + } + if (napi_set_named_property(env, ipCfgObj, "domains", domainsArray) != napi_ok) { + WIFI_LOGE("Set domains named property error!"); + } +} + +static void UpdateSecurityTypeAndPreSharedKey(WifiDeviceConfig& cppConfig) +{ + if (cppConfig.keyMgmt != KEY_MGMT_NONE) { + return; + } + for (int i = 0; i != WEPKEYS_SIZE; ++i) { + if (!cppConfig.wepKeys[i].empty() && cppConfig.wepTxKeyIndex == i) { + cppConfig.keyMgmt = KEY_MGMT_WEP; + cppConfig.preSharedKey = cppConfig.wepKeys[i]; + } + } +} + +static void DeviceConfigToJsArray(const napi_env& env, std::vector& vecDeviceConfigs, const int idx, napi_value& arrayResult) { + UpdateSecurityTypeAndPreSharedKey(vecDeviceConfigs[idx]); napi_value result; napi_create_object(env, &result); SetValueUtf8String(env, "ssid", vecDeviceConfigs[idx].ssid.c_str(), result); @@ -535,8 +713,26 @@ static void DeviceConfigToJsArray(const napi_env& env, const std::vector(ConvertKeyMgmtToSecType(vecDeviceConfigs[idx].keyMgmt)), result); - - napi_status status = napi_set_element(env, arrayResult, idx, result); + /* not supported currently */ + SetValueInt32(env, "creatorUid", DEFAULT_INVALID_VALUE, result); + /* not supported currently */ + SetValueInt32(env, "disableReason", DEFAULT_INVALID_VALUE, result); + SetValueInt32(env, "netId", vecDeviceConfigs[idx].networkId, result); + /* not supported currently */ + SetValueInt32(env, "randomMacType", DEFAULT_INVALID_VALUE, result); + /* not supported currently */ + SetValueUtf8String(env, "randomMacAddr", std::string("").c_str(), result); + /* not fully supported, set as dhcp now */ + SetValueInt32(env, "ipType", static_cast(AssignIpMethod::DHCP), result); + + napi_value ipCfgObj; + napi_create_object(env, &ipCfgObj); + IpConfigToJs(env, vecDeviceConfigs[idx].wifiIpConfig, ipCfgObj); + napi_status status = napi_set_named_property(env, result, "staticIp", ipCfgObj); + if (status != napi_ok) { + WIFI_LOGE("Set staticIp field!"); + } + status = napi_set_element(env, arrayResult, idx, result); if (status != napi_ok) { WIFI_LOGE("Wifi napi set element error: %{public}d", status); } diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h index e9f799af7bcaf8cca30577dd3e8f83e3cc361e86..bf0d12cf2d46eca31c10306d90fff5077b1a2e05 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h @@ -28,6 +28,8 @@ napi_value IsWifiActive(napi_env env, napi_callback_info info); napi_value Scan(napi_env env, napi_callback_info info); napi_value GetScanInfos(napi_env env, napi_callback_info info); napi_value AddDeviceConfig(napi_env env, napi_callback_info info); +napi_value AddUntrustedConfig(napi_env env, napi_callback_info info); +napi_value RemoveUntrustedConfig(napi_env env, napi_callback_info info); napi_value ConnectToNetwork(napi_env env, napi_callback_info info); napi_value ConnectToDevice(napi_env env, napi_callback_info info); napi_value IsConnected(napi_env env, napi_callback_info info); diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp index 9b256fca223a37c26a2b3c5742a4d78864f2b4d6..6a5f35c366a79da355b9a13c1781a7f5c4198255 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp @@ -33,6 +33,8 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_FUNCTION("scan", Scan), DECLARE_NAPI_FUNCTION("getScanInfos", GetScanInfos), DECLARE_NAPI_FUNCTION("addDeviceConfig", AddDeviceConfig), + DECLARE_NAPI_FUNCTION("addUntrustedConfig", AddUntrustedConfig), + DECLARE_NAPI_FUNCTION("removeUntrustedConfig", RemoveUntrustedConfig), DECLARE_NAPI_FUNCTION("connectToNetwork", ConnectToNetwork), DECLARE_NAPI_FUNCTION("connectToDevice", ConnectToDevice), DECLARE_NAPI_FUNCTION("isConnected", IsConnected), @@ -52,6 +54,7 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_FUNCTION("isFeatureSupported", IsFeatureSupported), DECLARE_NAPI_FUNCTION("getDeviceMacAddress", GetDeviceMacAddress), DECLARE_NAPI_FUNCTION("isHotspotActive", IsHotspotActive), + DECLARE_NAPI_FUNCTION("isHotspotDualBandSupported", IsHotspotDualBandSupported), DECLARE_NAPI_FUNCTION("enableHotspot", EnableHotspot), DECLARE_NAPI_FUNCTION("disableHotspot", DisableHotspot), DECLARE_NAPI_FUNCTION("setHotspotConfig", SetHotspotConfig), diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp index e4c9b946e01a863993e8d53c8e7ae870800903a4..e4336a3b232df9d615071712147d160b7b4ff9d3 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp @@ -37,6 +37,7 @@ 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 = "p2pDiscoveryChange"; +const std::string EVENT_STREAM_CHANGE = "streamChange"; static std::set g_supportEventList = { EVENT_STA_POWER_STATE_CHANGE, @@ -175,7 +176,10 @@ public: public: void OnWifiStateChanged(int state) override { WIFI_LOGI("sta received state changed event: %{public}d", state); - CheckAndNotify(EVENT_STA_POWER_STATE_CHANGE, state); + if (m_wifiStateConvertMap.find(state) == m_wifiStateConvertMap.end()) { + return; + } + CheckAndNotify(EVENT_STA_POWER_STATE_CHANGE, m_wifiStateConvertMap[state]); } void OnWifiConnectionChanged(int state, const WifiLinkedInfo &info) override { @@ -183,7 +187,7 @@ public: if (m_connectStateConvertMap.find(state) == m_connectStateConvertMap.end()) { return; } - CheckAndNotify(EVENT_STA_CONN_STATE_CHANGE, state); + CheckAndNotify(EVENT_STA_CONN_STATE_CHANGE, m_connectStateConvertMap[state]); } void OnWifiRssiChanged(int rssi) override { @@ -202,11 +206,25 @@ public: } private: + enum class JsLayerWifiState { + DISABLED = 0, + ENABLED = 1, + ENABLING = 2, + DISABLING = 3 + }; + enum class JsLayerConnectStatus { DISCONNECTED = 0, CONNECTED = 1, }; + std::map m_wifiStateConvertMap = { + { static_cast(WifiState::DISABLING), static_cast(JsLayerWifiState::DISABLING) }, + { static_cast(WifiState::DISABLED), static_cast(JsLayerWifiState::DISABLED) }, + { static_cast(WifiState::ENABLING), static_cast(JsLayerWifiState::ENABLING) }, + { static_cast(WifiState::ENABLED), static_cast(JsLayerWifiState::ENABLED) }, + }; + std::map m_connectStateConvertMap = { { static_cast(ConnState::CONNECTED), static_cast(JsLayerConnectStatus::CONNECTED) }, { static_cast(ConnState::DISCONNECTED), static_cast(JsLayerConnectStatus::DISCONNECTED) }, @@ -243,7 +261,11 @@ public: public: void OnHotspotStateChanged(int state) override { WIFI_LOGI("Hotspot received state changed event: %{public}d", state); - CheckAndNotify(EVENT_HOTSPOT_STATE_CHANGE, state); + if (m_apStateConvertMap.find(state) == m_apStateConvertMap.end()) { + return; + } + + CheckAndNotify(EVENT_HOTSPOT_STATE_CHANGE, m_apStateConvertMap[state]); } void OnHotspotStaJoin(const StationInfo &info) override { @@ -259,6 +281,21 @@ public: OHOS::sptr AsObject() override { return nullptr; } + +private: + enum class JsLayerApState { + DISABLED = 0, + ENABLED = 1, + ENABLING = 2, + DISABLING = 3 + }; + + std::map m_apStateConvertMap = { + { static_cast(ApState::AP_STATE_STARTING), static_cast(JsLayerApState::ENABLING) }, + { static_cast(ApState::AP_STATE_STARTED), static_cast(JsLayerApState::ENABLED) }, + { static_cast(ApState::AP_STATE_CLOSING), static_cast(JsLayerApState::DISABLING) }, + { static_cast(ApState::AP_STATE_CLOSED), static_cast(JsLayerApState::DISABLED) }, + }; }; class WifiNapiP2pEventCallback : public IWifiP2pCallback, 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 bcd68c603e6d5f4d3ec657d054ab77441b332efc..d953812c80130063d96943afd430b238461795ec 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp @@ -66,6 +66,15 @@ napi_value IsHotspotActive(napi_env env, napi_callback_info info) return result; } +napi_value IsHotspotDualBandSupported(napi_env env, napi_callback_info info) +{ + NAPI_ASSERT(env, wifiHotspotPtr != nullptr, "Wifi hotspot instance is null."); + napi_value result; + /* This interface is not supported currently */ + napi_get_boolean(env, false, &result); + return result; +} + static KeyMgmt GetKeyMgmtFromJsSecurityType(int secType) { std::map::iterator iter = g_mapSecTypeToKeyMgmt.find(SecTypeJs(secType)); diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h index 45667c63d8d671bd240795a52fa69e487f2fcc9a..f1bf7e2aed973a5fe66c205307c0504efa28d8ff 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h @@ -23,6 +23,7 @@ namespace Wifi { napi_value EnableHotspot(napi_env env, napi_callback_info info); napi_value DisableHotspot(napi_env env, napi_callback_info info); napi_value IsHotspotActive(napi_env env, napi_callback_info info); +napi_value IsHotspotDualBandSupported(napi_env env, napi_callback_info info); napi_value SetHotspotConfig(napi_env env, napi_callback_info info); napi_value GetHotspotConfig(napi_env env, napi_callback_info info); napi_value GetStations(napi_env env, napi_callback_info info); diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp index 1e8fa91f8dcaa43f487ede882fc6082125ea90cb..81071dd19296b33133c3d9705ca7a5a7a066c88e 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp @@ -26,7 +26,7 @@ TraceFuncCall::TraceFuncCall(std::string funcName): m_funcName(funcName) { if (m_isTrace) { m_startTime = std::chrono::steady_clock::now(); - WIFI_LOGD("Call func: %{public}s (start)", m_funcName.c_str()); + WIFI_LOGD("Call wifi func: %{public}s (start)", m_funcName.c_str()); } } diff --git a/interfaces/innerkits/native_cpp/wifi_standard/interfaces/wifi_msg.h b/interfaces/innerkits/native_cpp/wifi_standard/interfaces/wifi_msg.h index d3efc1dfc74ce19103f4d5e47a2e5f9108f57c3f..4c039ae848f36a5b85367305c6fd9f472a659017 100755 --- a/interfaces/innerkits/native_cpp/wifi_standard/interfaces/wifi_msg.h +++ b/interfaces/innerkits/native_cpp/wifi_standard/interfaces/wifi_msg.h @@ -32,6 +32,11 @@ namespace Wifi { #define IPV4_ADDRESS_TYPE 0 #define IPV6_ADDRESS_TYPE 1 +const std::string KEY_MGMT_NONE = "NONE"; +const std::string KEY_MGMT_WEP = "WEP"; +const std::string KEY_MGMT_WPA_PSK = "WPA-PSK"; +const std::string KEY_MGMT_SAE = "SAE"; + enum class SupplicantState { DISCONNECTED = 0, INTERFACE_DISABLED = 1, 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 10d2d8f089ee0893fab90e8db0642f318b2239d3..86ee75bf55ae10152f819f5a2eb54edae5784cad 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 @@ -36,6 +36,7 @@ int WifiDeviceCallBackStub::OnRemoteRequest( WIFI_LOGD("Failed to `%{public}s`,Remote service is died!", __func__); return -1; } + int exception = data.ReadInt32(); if (exception) { WIFI_LOGE("WifiDeviceCallBackStub::OnRemoteRequest, got exception: %{public}d!", exception); 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 9d623ab3ec52f67af3e11f7fd2d40cfbea58674f..20f2b75b0d56dab6414c8f55f5c31e11c147c464 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 @@ -18,6 +18,7 @@ #include "wifi_hisysevent.h" #include "wifi_logger.h" #include "wifi_msg.h" +#include "wifi_errcode.h" DEFINE_WIFILOG_HOTSPOT_LABEL("WifiHotspotCallbackStub"); namespace OHOS { @@ -36,6 +37,7 @@ int WifiHotspotCallbackStub::OnRemoteRequest( WIFI_LOGD("Failed to `%{public}s`,remote service is died!", __func__); return -1; } + int exception = data.ReadInt32(); if (exception) { return -1; 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 b1e34cfd9f051b4733d8bfc655b2a8a983aeef71..21004e71417b0f3b648f95652aa8aeb39d63bb03 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 @@ -52,6 +52,7 @@ int WifiP2pCallbackStub::OnRemoteRequest( WIFI_LOGD("Failed to `%{public}s`,remote service is died!", __func__); return -1; } + int exception = data.ReadInt32(); if (exception) { WIFI_LOGD("WifiP2pCallbackStub::OnRemoteRequest exception! %{public}d!", exception); 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 faa0a98a6c7f865c42e3d01b6ab80e4b23cdfee0..3d8e82fc0883554b8a5c724b4501ae8306955b56 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 @@ -36,6 +36,7 @@ int WifiScanCallbackStub::OnRemoteRequest( WIFI_LOGD("Failed to `%{public}s`,remote service is died!", __func__); return -1; } + int exception = data.ReadInt32(); if (exception) { WIFI_LOGD("WifiScanCallbackStub::OnRemoteRequest exception! %{public}d!", exception); diff --git a/services/wifi_standard/etc/init/BUILD.gn b/services/wifi_standard/etc/init/BUILD.gn index ee90c6e914ae164168591e8e433057e2ae393e40..26badebad83ef285ebb3c684099f18e6ad5059ed 100755 --- a/services/wifi_standard/etc/init/BUILD.gn +++ b/services/wifi_standard/etc/init/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2021 Huawei Device Co., Ltd. +# 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 @@ -14,15 +14,11 @@ import("//build/ohos.gni") group("etc") { - deps = [ ":wifi_standard.rc" ] + deps = [ ":wifi_standard.cfg" ] } -ohos_prebuilt_etc("wifi_standard.rc") { - if (use_musl) { - source = "wifi_standard.cfg" - } else { - source = "wifi_standard.rc" - } +ohos_prebuilt_etc("wifi_standard.cfg") { + source = "wifi_standard.cfg" relative_install_dir = "init" part_name = "wifi_standard" subsystem_name = "communication" diff --git a/services/wifi_standard/ipc_framework/cRPC/src/server.c b/services/wifi_standard/ipc_framework/cRPC/src/server.c index 03173541ab1cc2154a875d36227b7e98345ccf9c..3b48efa9a398fc7af2af95ad7db360a258aaadb7 100644 --- a/services/wifi_standard/ipc_framework/cRPC/src/server.c +++ b/services/wifi_standard/ipc_framework/cRPC/src/server.c @@ -199,7 +199,7 @@ int RunRpcLoop(RpcServer *server) EventLoop *loop = server->loop; while (!loop->stop) { BeforeLoop(server); - int retval = epoll_wait(loop->epfd, loop->epEvents, loop->setSize, 1); + int retval = epoll_wait(loop->epfd, loop->epEvents, loop->setSize, 5); for (int i = 0; i < retval; ++i) { struct epoll_event *e = loop->epEvents + i; int fd = e->data.fd; diff --git a/services/wifi_standard/wifi_framework/common/config/wifi_settings.cpp b/services/wifi_standard/wifi_framework/common/config/wifi_settings.cpp index 7f4262fa84445b6916664cf6edb38d12e3f295aa..26e3fea61a11478bc8e18dacbf576c0c74139096 100644 --- a/services/wifi_standard/wifi_framework/common/config/wifi_settings.cpp +++ b/services/wifi_standard/wifi_framework/common/config/wifi_settings.cpp @@ -836,7 +836,7 @@ void WifiSettings::InitDefaultHotspotConfig() void WifiSettings::InitDefaultP2pVendorConfig() { mP2pVendorConfig.SetRandomMacSupport(false); - mP2pVendorConfig.SetIsAutoListen(true); + mP2pVendorConfig.SetIsAutoListen(false); mP2pVendorConfig.SetDeviceName(""); mP2pVendorConfig.SetPrimaryDeviceType(""); mP2pVendorConfig.SetSecondaryDeviceType(""); diff --git a/services/wifi_standard/wifi_framework/dhcp_manage/dhcp_server/src/dhcp_server.c b/services/wifi_standard/wifi_framework/dhcp_manage/dhcp_server/src/dhcp_server.c index bc0d0b82eb1a6593b7362e2c1c684070bfe15087..b5d2323c991917920ad680e32a3524d25e83930e 100644 --- a/services/wifi_standard/wifi_framework/dhcp_manage/dhcp_server/src/dhcp_server.c +++ b/services/wifi_standard/wifi_framework/dhcp_manage/dhcp_server/src/dhcp_server.c @@ -881,6 +881,10 @@ AddressBinding *GetBinding(DhcpAddressPool *pool, PDhcpMsgInfo received) AddressBinding *binding = pool->binding(received->packet.chaddr, &received->options); if (!binding) { binding = pool->newBinding(received->packet.chaddr, &received->options); + if (binding == NULL) { + LOGE("new binding is null"); + return NULL; + } if (pool->leaseTime) { binding->leaseTime = pool->leaseTime; } @@ -1014,13 +1018,17 @@ static int NotBindingRequest(DhcpAddressPool *pool, PDhcpMsgInfo received, PDhcp RemoveLease(pool, lease); } AddressBinding *binding = pool->newBinding(received->packet.chaddr, &received->options); + if (binding == NULL) { + LOGE("Not binding request binding is null."); + return REPLY_NONE; + } binding->ipAddress = yourIpAddr; if (pool->leaseTime) { binding->leaseTime = pool->leaseTime; } int replyType = Repending(pool, binding); if (replyType != REPLY_OFFER) { - return replyType; + return replyType; } lease = GetLease(pool, yourIpAddr); if (!lease) { @@ -1111,6 +1119,10 @@ static int OnReceivedRequest(PDhcpServerContext ctx, PDhcpMsgInfo received, PDhc } ServerContext *srvIns = GetServerInstance(ctx); AddressBinding *binding = srvIns->addressPool.binding(received->packet.chaddr, &received->options); + if (binding == NULL) { + LOGE("OnReceivedRequest, binding is null"); + return REPLY_NONE; + } if ((ret = HasNobindgRequest(ctx, received, reply)) != REPLY_ACK) { return ret; } 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 ac06f8584555f8d9f2c391989039dae8d682074f..00f258d73e11244c64efacc1bac4a57a6c06ff24 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 @@ -14,6 +14,7 @@ */ #include "wifi_device_service_impl.h" +#include #include #include #include "wifi_permission_utils.h" @@ -109,6 +110,12 @@ ErrCode WifiDeviceServiceImpl::EnableWifi() } } + 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; + } + if (!WifiConfigCenter::GetInstance().SetWifiMidState(curState, WifiOprMidState::OPENING)) { WIFI_LOGI("set wifi mid state opening failed!"); return WIFI_OPT_OPEN_SUCC_WHEN_OPENED; @@ -143,12 +150,6 @@ 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; } @@ -158,12 +159,6 @@ ErrCode WifiDeviceServiceImpl::DisableWifi() WIFI_LOGE("DisableWifi:VerifySetWifiInfoPermission PERMISSION_DENIED!"); 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)); @@ -173,6 +168,12 @@ ErrCode WifiDeviceServiceImpl::DisableWifi() return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; } } + + sptr p2pService = WifiP2pServiceImpl::GetInstance(); + if (p2pService != nullptr && p2pService->DisableP2p() != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Disable P2p failed!"); + } + if (!WifiConfigCenter::GetInstance().SetWifiMidState(curState, WifiOprMidState::CLOSING)) { WIFI_LOGI("set wifi mid state opening failed! may be other activity has been operated"); return WIFI_OPT_CLOSE_SUCC_WHEN_CLOSED; @@ -216,6 +217,47 @@ ErrCode WifiDeviceServiceImpl::PutWifiProtectRef(const std::string &protectName) return WIFI_OPT_FAILED; } +bool WifiDeviceServiceImpl::CheckConfigPwd(const WifiDeviceConfig &config) +{ + if ((config.ssid.length() <= 0) || (config.keyMgmt.length()) <= 0) { + return false; + } + + if (config.keyMgmt != KEY_MGMT_NONE && config.preSharedKey.empty()) { + WIFI_LOGE("CheckConfigPwd: preSharedKey is empty!"); + return false; + } + + int len = config.preSharedKey.length(); + bool isAllHex = std::all_of(config.preSharedKey.begin(), config.preSharedKey.end(), isxdigit); + WIFI_LOGI("CheckConfigPwd, keyMgmt: %{public}s, len: %{public}d", config.keyMgmt.c_str(), len); + if (config.keyMgmt == KEY_MGMT_NONE) { + for (int i = 0; i != WEPKEYS_SIZE; ++i) { + if (!config.wepKeys[i].empty()) { // wep + int wepLen = config.wepKeys[i].size(); + if (wepLen == WEP_KEY_LEN1 || wepLen == WEP_KEY_LEN2 || wepLen == WEP_KEY_LEN3) { + return true; + } + constexpr int MULTIPLE_HEXT_TO_ASCII = 2; + if (wepLen == (WEP_KEY_LEN1 * MULTIPLE_HEXT_TO_ASCII) || + wepLen == (WEP_KEY_LEN2 * MULTIPLE_HEXT_TO_ASCII) || + wepLen == (WEP_KEY_LEN3 * MULTIPLE_HEXT_TO_ASCII)) { + return isAllHex; + } + return false; + } + } + return config.preSharedKey.empty(); // open + } + int minLen = config.keyMgmt == KEY_MGMT_SAE ? MIN_SAE_LEN : MIN_PSK_LEN; + int maxLen = isAllHex ? MAX_HEX_LEN : MAX_PRESHAREDKEY_LEN; + if (len < minLen || len > maxLen) { + WIFI_LOGE("CheckConfigPwd: preSharedKey length error: %{public}d", len); + return false; + } + return true; +} + ErrCode WifiDeviceServiceImpl::AddDeviceConfig(const WifiDeviceConfig &config, int &result) { if (WifiPermissionUtils::VerifySetWifiInfoPermission() == PERMISSION_DENIED) { @@ -223,6 +265,11 @@ ErrCode WifiDeviceServiceImpl::AddDeviceConfig(const WifiDeviceConfig &config, i return WIFI_OPT_PERMISSION_DENIED; } + if (!CheckConfigPwd(config)) { + WIFI_LOGE("CheckConfigPwd failed!"); + return WIFI_OPT_INVALID_PARAM; + } + if (!IsStaServiceRunning()) { return WIFI_OPT_STA_NOT_OPENED; } @@ -231,15 +278,6 @@ ErrCode WifiDeviceServiceImpl::AddDeviceConfig(const WifiDeviceConfig &config, i if (pService == nullptr) { return WIFI_OPT_STA_NOT_OPENED; } - - if ((config.ssid.length() <= 0) || (config.keyMgmt.length()) <= 0) { - return WIFI_OPT_INVALID_PARAM; - } - - if (config.keyMgmt != "NONE" && config.preSharedKey.length() <= 0) { - return WIFI_OPT_INVALID_PARAM; - } - int retNetworkId = pService->AddDeviceConfig(config); if (retNetworkId < 0) { return WIFI_OPT_FAILED; @@ -395,19 +433,13 @@ ErrCode WifiDeviceServiceImpl::ConnectToDevice(const WifiDeviceConfig &config) WIFI_LOGE("ConnectToDevice with config:VerifySetWifiInfoPermission PERMISSION_DENIED!"); return WIFI_OPT_PERMISSION_DENIED; } - - if (!IsStaServiceRunning()) { - return WIFI_OPT_STA_NOT_OPENED; - } - - if ((config.ssid.length() <= 0) || (config.keyMgmt.length()) <= 0) { + if (!CheckConfigPwd(config)) { + WIFI_LOGE("CheckConfigPwd failed!"); return WIFI_OPT_INVALID_PARAM; } - - if (config.keyMgmt != "NONE" && config.preSharedKey.length() <= 0 ) { - return WIFI_OPT_INVALID_PARAM; + if (!IsStaServiceRunning()) { + return WIFI_OPT_STA_NOT_OPENED; } - IStaService *pService = WifiServiceManager::GetInstance().GetStaServiceInst(); if (pService == nullptr) { return WIFI_OPT_STA_NOT_OPENED; 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 89b1ae75679dc50730e8a7adee04f1197e6e3a33..0f8fe2d4dd653220698b4c85a21ea4ace82e53c0 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 @@ -106,9 +106,18 @@ private: ErrCode CheckCanEnableWifi(void); bool IsStaServiceRunning(); bool IsScanServiceRunning(); + bool CheckConfigPwd(const WifiDeviceConfig &config); static void SaBasicDump(std::string& result); private: + static constexpr int MAX_PRESHAREDKEY_LEN = 63; + static constexpr int MAX_HEX_LEN = 64; + static constexpr int MIN_PSK_LEN = 8; + static constexpr int MIN_SAE_LEN = 1; + static constexpr int WEP_KEY_LEN1 = 5; + static constexpr int WEP_KEY_LEN2 = 13; + static constexpr int WEP_KEY_LEN3 = 16; + static sptr g_instance; static std::mutex g_instanceLock; bool mPublishFlag; 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 cb95d972608330fb7a6f51222872f35dc7764ddd..9c47f213b1b42106611eb31bb7e894281c329c21 100755 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_manager.cpp @@ -183,8 +183,8 @@ int WifiManager::Init() } if (WifiConfigCenter::GetInstance().GetStaLastRunState()) { /* Automatic startup upon startup */ WIFI_LOGE("AutoStartStaApService"); - AutoStartStaService(); AutoStartP2pService(); + AutoStartStaService(); } else { /** * The sta service automatically starts upon startup. After the sta diff --git a/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_enabled_state.cpp b/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_enabled_state.cpp index 5b0f6ee0c395cf62c5f25cd388bb94c396ccda81..9725752fd169489546025e0673e9335614d9153a 100644 --- a/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_enabled_state.cpp +++ b/services/wifi_standard/wifi_framework/wifi_manage/wifi_p2p/p2p_enabled_state.cpp @@ -45,6 +45,7 @@ void P2pEnabledState::GoInState() P2pVendorConfig config; WifiSettings::GetInstance().GetP2pVendorConfig(config); if (config.GetIsAutoListen()) { + WIFI_LOGI("Auto start P2P listen!"); p2pStateMachine.SendMessage( static_cast(P2P_STATE_MACHINE_CMD::CMD_START_LISTEN), defaultPeriodTime, defaultIntervalTime); } 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 e95ea68daf4e55ad0687da07fdef66a4c01a260b..927f0f47deba4c49626a93937bf8564132a58ba0 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 @@ -1959,7 +1959,7 @@ void StaStateMachine::DhcpResultNotify::OnFailed(int status, const std::string & } } else { pStaStateMachine->staCallback.OnStaConnChanged( - OperateResState::CONNECT_OBTAINING_IP_FAILED, pStaStateMachine->linkedInfo); + OperateResState::CONNECT_OBTAINING_IP_FAILED, pStaStateMachine->linkedInfo); if (!pStaStateMachine->isRoam) { pStaStateMachine->DisConnectProcess(); pStaStateMachine->SaveLinkstate(ConnState::DISCONNECTED, DetailedState::OBTAINING_IPADDR_FAIL); diff --git a/services/wifi_standard/wifi_hal/wifi_hal_module/wpa_supplicant_hal/wpa_sta_hal/wifi_supplicant_hal.c b/services/wifi_standard/wifi_hal/wifi_hal_module/wpa_supplicant_hal/wpa_sta_hal/wifi_supplicant_hal.c index 1da66b2da427b4f03dc69409a508e712b1607657..a75d6c37c6b036fb871bbd4d91397e330efe6217 100644 --- a/services/wifi_standard/wifi_hal/wifi_hal_module/wpa_supplicant_hal/wpa_sta_hal/wifi_supplicant_hal.c +++ b/services/wifi_standard/wifi_hal/wifi_hal_module/wpa_supplicant_hal/wpa_sta_hal/wifi_supplicant_hal.c @@ -37,24 +37,27 @@ #define REPLY_BUF_SMALL_LENGTH 64 #define CMD_FREQ_MAX_LEN 8 +const int QUOTATION_MARKS_FLAG_YES = 0; +const int QUOTATION_MARKS_FLAG_NO = 1; + WifiWpaStaInterface *g_wpaStaInterface = NULL; static WpaSsidField g_wpaSsidFields[] = { - {DEVICE_CONFIG_SSID, "ssid", 0}, - {DEVICE_CONFIG_PSK, "psk", 0}, - {DEVICE_CONFIG_KEYMGMT, "key_mgmt", 1}, - {DEVICE_CONFIG_PRIORITY, "priority", 1}, - {DEVICE_CONFIG_SCAN_SSID, "scan_ssid", 1}, - {DEVICE_CONFIG_EAP, "eap", 1}, - {DEVICE_CONFIG_IDENTITY, "identity", 0}, - {DEVICE_CONFIG_PASSWORD, "password", 0}, - {DEVICE_CONFIG_BSSID, "bssid", 1}, - {DEVICE_CONFIG_AUTH_ALGORITHMS, "auth_alg", 1}, - {DEVICE_CONFIG_WEP_KEY_IDX, "wep_tx_keyidx", 1}, - {DEVICE_CONFIG_WEP_KEY_0, "wep_key0", 1}, - {DEVICE_CONFIG_WEP_KEY_1, "wep_key1", 1}, - {DEVICE_CONFIG_WEP_KEY_2, "wep_key2", 1}, - {DEVICE_CONFIG_WEP_KEY_3, "wep_key3", 1} + {DEVICE_CONFIG_SSID, "ssid", QUOTATION_MARKS_FLAG_YES}, + {DEVICE_CONFIG_PSK, "psk", QUOTATION_MARKS_FLAG_YES}, + {DEVICE_CONFIG_KEYMGMT, "key_mgmt", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_PRIORITY, "priority", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_SCAN_SSID, "scan_ssid", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_EAP, "eap", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_IDENTITY, "identity", QUOTATION_MARKS_FLAG_YES}, + {DEVICE_CONFIG_PASSWORD, "password", QUOTATION_MARKS_FLAG_YES}, + {DEVICE_CONFIG_BSSID, "bssid", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_AUTH_ALGORITHMS, "auth_alg", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_WEP_KEY_IDX, "wep_tx_keyidx", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_WEP_KEY_0, "wep_key0", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_WEP_KEY_1, "wep_key1", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_WEP_KEY_2, "wep_key2", QUOTATION_MARKS_FLAG_NO}, + {DEVICE_CONFIG_WEP_KEY_3, "wep_key3", QUOTATION_MARKS_FLAG_NO} }; static int WpaCliCmdStatus(WifiWpaStaInterface *this, struct WpaHalCmdStatus *pcmd) @@ -179,6 +182,30 @@ static int WpaCliCmdSaveConfig(WifiWpaStaInterface *this) return WpaCliCmd(cmd, buf, sizeof(buf)); } +static int CalcQuotationMarksFlag(int pos, const char value[WIFI_NETWORK_CONFIG_VALUE_LENGTH]) +{ + int flag = g_wpaSsidFields[pos].flag; + const int HEX_PSK_MAX_LEN = 64; + int len = strlen(value); + /* if the psk length is 64, it's hex format and don't need quotation marks */ + if (pos == DEVICE_CONFIG_PSK && len >= HEX_PSK_MAX_LEN) { + flag = QUOTATION_MARKS_FLAG_NO; + } + if (pos == DEVICE_CONFIG_WEP_KEY_0 || + pos == DEVICE_CONFIG_WEP_KEY_1 || + pos == DEVICE_CONFIG_WEP_KEY_2 || + pos == DEVICE_CONFIG_WEP_KEY_3) { + const int WEP_KEY_LEN1 = 5; + const int WEP_KEY_LEN2 = 13; + const int WEP_KEY_LEN3 = 16; + /* For wep key, ASCII format need quotation marks, hex format is not required */ + if (len == WEP_KEY_LEN1 || len == WEP_KEY_LEN2 || len == WEP_KEY_LEN3) { + flag = QUOTATION_MARKS_FLAG_YES; + } + } + return flag; +} + static int WpaCliCmdSetNetwork(WifiWpaStaInterface *this, const struct WpaSetNetworkArgv *argv) { if (this == NULL || argv == NULL) { @@ -198,7 +225,7 @@ static int WpaCliCmdSetNetwork(WifiWpaStaInterface *this, const struct WpaSetNet char cmd[CMD_BUFFER_SIZE] = {0}; char buf[REPLY_BUF_SMALL_LENGTH] = {0}; int res; - if (g_wpaSsidFields[pos].flag == 0) { + if (CalcQuotationMarksFlag(pos, argv->value) == QUOTATION_MARKS_FLAG_YES) { res = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SET_NETWORK %d %s \"%s\"", this->ifname, argv->id, g_wpaSsidFields[pos].fieldName, argv->value); } else {