Skip to content
Snippets Groups Projects
Commit a5c62307 authored by Francesco Pham's avatar Francesco Pham
Browse files

openharmony-standard: update wifi-napi-networkmanager-integration.patch


Signed-off-by: default avatarFrancesco Pham <francesco.pham@huawei.com>
Signed-off-by: default avatarKareem Zarka <kareem.zarka@huawei.com>
parent 93b5c666
Branches wifi-support
No related tags found
No related merge requests found
Pipeline #17198 passed
......@@ -26,10 +26,10 @@ index ea63e51..5b6f90d 100755
relative_install_dir = "module"
diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp
index 3afaf93..9c00654 100755
index 3afaf93..05ecca3 100755
--- a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp
+++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp
@@ -18,13 +18,44 @@
@@ -18,28 +18,74 @@
#include "wifi_device.h"
#include "wifi_scan.h"
#include <vector>
......@@ -42,10 +42,32 @@ index 3afaf93..9c00654 100755
-std::unique_ptr<WifiDevice> wifiDevicePtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
-std::unique_ptr<WifiScan> wifiScanPtr = WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
+bool enableWifi() {
+ NMClient *client = NULL;
+struct WifiStatus
+{
+ bool wifi_enabled;
+ NMClient *client;
+ GMainLoop *loop;
+ int attempts;
+};
+static gboolean check_wifi_status(gpointer user_data)
+{
+ const int ENABLE_WIFI_MAX_ATTEMPTS = 10;
+ WifiStatus *wifi_status = (WifiStatus *)user_data;
+ wifi_status->wifi_enabled = nm_client_wireless_get_enabled(wifi_status->client);
+ wifi_status->attempts++;
+ if (wifi_status->wifi_enabled || wifi_status->attempts >= ENABLE_WIFI_MAX_ATTEMPTS)
+ {
+ g_main_loop_quit(wifi_status->loop);
+ }
+ return !wifi_status->wifi_enabled && wifi_status->attempts < ENABLE_WIFI_MAX_ATTEMPTS;
+}
+
+bool enable_wifi_with_attempts()
+{
+ GError *error = NULL;
+ // Initialize the NMClient
+ NMClient *client;
+ GMainLoop *loop = g_main_loop_new(NULL, FALSE);
+ client = nm_client_new(NULL, &error);
+ if (!client)
+ {
......@@ -53,30 +75,18 @@ index 3afaf93..9c00654 100755
+ g_error_free(error);
+ return false;
+ }
+ if (nm_client_wireless_get_enabled(client))
+ {
+ WIFI_LOGI("Wi-Fi is already enabled.\n");
+ g_object_unref(client);
+ return true;
+ }
+ WifiStatus wifi_status = {false, client, loop, 0};
+ nm_client_wireless_set_enabled(client, TRUE);
+ if (nm_client_wireless_get_enabled(client))
+ {
+ WIFI_LOGI("Wi-Fi has been enabled.\n");
+ g_object_unref(client);
+ return true;
+ }
+ else
+ {
+ WIFI_LOGE("Failed to enable Wi-Fi.\n");
+ g_object_unref(client);
+ return false;
+ }
+ g_timeout_add_seconds(1, check_wifi_status, &wifi_status);
+ g_main_loop_run(loop);
+ bool result = wifi_status.wifi_enabled;
+ g_object_unref(client);
+ g_main_loop_unref(loop);
+ return result;
+}
napi_value EnableWifi(napi_env env, napi_callback_info info)
{
@@ -32,11 +63,15 @@ napi_value EnableWifi(napi_env env, napi_callback_info info)
size_t argc = 1;
napi_value argv[1];
napi_value thisVar;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
......@@ -85,18 +95,23 @@ index 3afaf93..9c00654 100755
- ErrCode ret = wifiDevicePtr->EnableWifi();
napi_value result;
- napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
+ if (enableWifi())
+ if (enable_wifi_with_attempts())
+ {
+ WIFI_LOGI("Wi-Fi has been enabled.\n");
+ napi_get_boolean(env, true, &result);
+ }
+ else
+ {
+ WIFI_LOGI("Failed to enable Wi-Fi.\n");
+ napi_get_boolean(env, false, &result);
+ }
return result;
}
@@ -47,29 +82,52 @@ napi_value DisableWifi(napi_env env, napi_callback_info info)
-
napi_value DisableWifi(napi_env env, napi_callback_info info)
{
size_t argc = 1;
@@ -47,13 +93,33 @@ napi_value DisableWifi(napi_env env, napi_callback_info info)
napi_value thisVar;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
......@@ -108,38 +123,34 @@ index 3afaf93..9c00654 100755
return result;
}
+bool isWifiActive()
+bool isWifiActive()
+{
+ NMClient *client = NULL;
+ GError *error = NULL;
+ client = nm_client_new(NULL, &error);
+ if (!client)
+ {
+ WIFI_LOGE("Error: %s\n", error->message);
+ g_error_free(error);
+ return false;
+ }
+ if (nm_client_wireless_get_enabled(client))
+ {
+ WIFI_LOGI("Wi-Fi is enabled.\n");
+ g_object_unref(client);
+ return true;
+ }
+ else
+ {
+ WIFI_LOGI("Wi-Fi is not enabled.\n");
+ g_object_unref(client);
+ return false;
+ }
+ NMClient * client = NULL;
+ GError * error = NULL;
+ client = nm_client_new(NULL, & error);
+ if(!client) {
+ WIFI_LOGE("Error: %s\n", error -> message);
+ g_error_free(error);
+ return false;
+ }
+ if(nm_client_wireless_get_enabled(client)) {
+ WIFI_LOGI("Wi-Fi is active.\n");
+ g_object_unref(client);
+ return true;
+ } else {
+ WIFI_LOGI("Wi-Fi is not active.\n");
+ g_object_unref(client);
+ return false;
+ }
+}
+
napi_value IsWifiActive(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1];
@@ -61,15 +127,13 @@ napi_value IsWifiActive(napi_env env, napi_callback_info info)
napi_value thisVar;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
-
- NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null.");
- bool activeStatus = true;
- ErrCode ret = wifiDevicePtr->IsWifiActive(activeStatus);
......@@ -149,18 +160,16 @@ index 3afaf93..9c00654 100755
-
napi_value result;
- napi_get_boolean(env, activeStatus, &result);
+ if (isWifiActive())
+ {
+ napi_get_boolean(env, true, &result);
+ if(isWifiActive()) {
+ napi_get_boolean(env,true, &result);
+ }
+ else
+ {
+ napi_get_boolean(env, false, &result);
+ else {
+ napi_get_boolean(env,false, &result);
+ }
return result;
}
@@ -80,9 +138,7 @@ napi_value Scan(napi_env env, napi_callback_info info)
@@ -80,9 +144,7 @@ napi_value Scan(napi_env env, napi_callback_info info)
napi_value thisVar;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
......@@ -171,7 +180,7 @@ index 3afaf93..9c00654 100755
napi_value result;
napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
return result;
@@ -143,10 +199,7 @@ static bool NativeScanInfosToJsObj(const napi_env& env, napi_value& arrayResult,
@@ -143,10 +205,6 @@ static bool NativeScanInfosToJsObj(const napi_env& env, napi_value& arrayResult,
static bool GetWifiScanInfoList(const napi_env& env, napi_value& arrayResult)
{
std::vector<WifiScanInfo> vecCppScanInfos;
......@@ -179,11 +188,10 @@ index 3afaf93..9c00654 100755
- WIFI_LOGE("[Napi Device] Get Scaninf list error");
- return false;
- }
+
WIFI_LOGI("[Napi Device] GetScanInfoList, size: %{public}zu", vecCppScanInfos.size());
napi_create_array_with_length(env, vecCppScanInfos.size(), &arrayResult);
@@ -293,10 +346,9 @@ static void JsObjToDeviceConfig(const napi_env& env, const napi_value& object, W
@@ -293,10 +351,9 @@ static void JsObjToDeviceConfig(const napi_env& env, const napi_value& object, W
static napi_value AddDeviceConfigImpl(const napi_env& env, AsyncCallbackInfo *asCallbackInfo)
{
......@@ -195,17 +203,18 @@ index 3afaf93..9c00654 100755
if (addResult < 0 || ret != WIFI_OPT_SUCCESS) {
retValue = -1;
} else {
@@ -441,8 +493,7 @@ napi_value ConnectToNetwork(napi_env env, napi_callback_info info)
@@ -440,9 +497,7 @@ napi_value ConnectToNetwork(napi_env env, napi_callback_info info)
int networkId = -1;
napi_get_value_int32(env, argv[0], &networkId);
-
- NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null.");
- ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId);
+ ErrCode ret = WIFI_OPT_SUCCESS;
+ ErrCode ret = WIFI_OPT_SUCCESS;
napi_value result;
napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
return result;
@@ -459,11 +510,9 @@ napi_value ConnectToDevice(napi_env env, napi_callback_info info)
@@ -459,11 +514,9 @@ napi_value ConnectToDevice(napi_env env, napi_callback_info info)
napi_typeof(env, argv[0], &valueType);
NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected.");
......@@ -218,17 +227,17 @@ index 3afaf93..9c00654 100755
napi_value result;
napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
return result;
@@ -476,8 +525,7 @@ napi_value Disconnect(napi_env env, napi_callback_info info)
@@ -476,8 +529,7 @@ napi_value Disconnect(napi_env env, napi_callback_info info)
napi_value thisVar;
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
- NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null.");
- ErrCode ret = wifiDevicePtr->Disconnect();
+ ErrCode ret = WIFI_OPT_SUCCESS;
+ ErrCode ret = WIFI_OPT_SUCCESS;
napi_value result;
napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
return result;
@@ -503,9 +551,8 @@ napi_value GetSignalLevel(napi_env env, napi_callback_info info)
@@ -503,11 +555,10 @@ napi_value GetSignalLevel(napi_env env, napi_callback_info info)
napi_get_value_int32(env, argv[0], &rssi);
napi_get_value_int32(env, argv[1], &band);
......@@ -237,5 +246,8 @@ index 3afaf93..9c00654 100755
- ErrCode ret = wifiDevicePtr->GetSignalLevel(rssi, band, level);
+ ErrCode ret = WIFI_OPT_SUCCESS;
if (ret != WIFI_OPT_SUCCESS) {
WIFI_LOGW("[Napi Device] Get wifi signal level fail: %{public}d", ret);
- WIFI_LOGW("[Napi Device] Get wifi signal level fail: %{public}d", ret);
+ WIFI_LOGE("[Napi Device] Get wifi signal level fail: %{public}d", ret);
}
napi_value result;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment