diff --git a/setting/BUILD.gn b/setting/BUILD.gn index c4b5cc22f0bca608d5ced5b4408898a1a618ec19..5a6a9ae62a37fb651bf83467cced80d1475ae510 100644 --- a/setting/BUILD.gn +++ b/setting/BUILD.gn @@ -24,7 +24,7 @@ shared_library("setting") { "setting/src/main/cpp/setting_utils.cpp", "setting/src/main/cpp/setting_wifi_ability_slice.cpp", "setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp", - "setting/src/main/cpp/wpa_work.c", + "setting/src/main/cpp/setting_client.c", ] deps = [ @@ -37,7 +37,6 @@ shared_library("setting") { "//foundation/graphic/graphic_utils_lite:utils_lite", "//foundation/graphic/surface_lite", "//foundation/systemabilitymgr/samgr_lite/samgr:samgr", - "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant", ] include_dirs = [ @@ -47,12 +46,11 @@ shared_library("setting") { "${aafwk_lite_path}/interfaces/kits/want_lite", "//base/startup/init/interfaces/innerkits/include/syspara", "//base/security/permission_lite/interfaces/kits", - "//third_party/wpa_supplicant/wpa_supplicant-2.9/src/common", + "//foundation/communication/ipc/ipc/native/c/manager/include", ] + configs -= [ "//build/lite/config:clang_opt" ] ldflags = [ - "-lwpa", - "-lwpa_client", "-lbegetutil", "-lpms_client", ] @@ -66,6 +64,9 @@ shared_library("setting") { hap_pack("setting_hap") { deps = [ ":setting" ] + if (defined(ohos_lite)) { + deps += [ "setting/src/main/cpp/setting_server_lite:setting_service_lite" ] + } mode = "hap" json_path = "setting/src/main/config.json" ability_so_path = "$root_out_dir/libsetting.so" diff --git a/setting/setting/src/main/cpp/main_ability_slice.cpp b/setting/setting/src/main/cpp/main_ability_slice.cpp old mode 100755 new mode 100644 index f59d47bfb2f9ef2f8021bda95fa66a3a10bb9ef9..6ebda00a170a7734db9291b3f876b0654d09194b --- a/setting/setting/src/main/cpp/main_ability_slice.cpp +++ b/setting/setting/src/main/cpp/main_ability_slice.cpp @@ -22,7 +22,7 @@ #include "bundle_manager.h" #include "module_info.h" #include "element_name.h" -#include "wpa_work.h" +#include "setting_client.h" #include "gfx_utils/style.h" #include #include @@ -176,7 +176,7 @@ void MainAbilitySlice::SetWifiButtonView(void) char buff[64] = {0}; // 64 is the longest in this sample int myX = WIFI_BUTTON_TEXT_SSID_X; int myY = WIFI_BUTTON_TEXT_SSID_Y; - int ret = GetCurrentConnInfo(buff, sizeof(buff)); + int ret = GetCurrentWifiConnInfo(buff, sizeof(buff)); lablelFontSsid_ = new UILabel(); lablelFontSsid_->SetPosition(myX, myY, DE_SUBTITLE_TEXT_WIDTH, DE_SUBTITLE_TEXT_HEIGHT); if (ret == 0) { @@ -315,6 +315,9 @@ void MainAbilitySlice::SetScrollView() void MainAbilitySlice::OnStart(const Want& want) { + if (ServerProxyInit() == 0) { + WifiWpaStart(); + } AbilitySlice::OnStart(want); SetButtonListenerWifi(); SetButtonListenerApp(); @@ -337,7 +340,7 @@ void MainAbilitySlice::OnInactive() void MainAbilitySlice::OnActive(const Want& want) { char buff[64] = {0}; - int ret = GetCurrentConnInfo(buff, sizeof(buff)); + int ret = GetCurrentWifiConnInfo(buff, sizeof(buff)); if (ret == 0) { printf("##### SetText -> %s \n", buff); lablelFontSsid_->SetText(buff); diff --git a/setting/setting/src/main/cpp/main_ability_slice.h b/setting/setting/src/main/cpp/main_ability_slice.h old mode 100755 new mode 100644 index 75aee452b50c4050df5f667d9af8649370e2f787..9ecaa16bcd0e14eeef6e19996d573ee018ac673c --- a/setting/setting/src/main/cpp/main_ability_slice.h +++ b/setting/setting/src/main/cpp/main_ability_slice.h @@ -37,7 +37,6 @@ #include "pthread.h" #include "setting_utils.h" #include "want.h" -#include "wpa_work.h" namespace OHOS { class MainAbilitySlice : public AbilitySlice { diff --git a/setting/setting/src/main/cpp/setting_client.c b/setting/setting/src/main/cpp/setting_client.c new file mode 100644 index 0000000000000000000000000000000000000000..a881a2bf27b15d0e6de7ce26805d892f91959212 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_client.c @@ -0,0 +1,263 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "setting_stub.h" +#include "ipc_skeleton.h" +#include "iproxy_client.h" +#include "rpc_errno.h" +#include "rpc_log.h" +#include "serializer.h" +#include "samgr_lite.h" + +#define MAX_DATA_LEN 64 +#define WAIT_SERVER_READY_INTERVAL_COUNT 3 + +static IClientProxy *g_serverProxy = NULL; + +static int WifiScanCallback(IOwner owner, int code, IpcIo *reply) +{ + printf("WifiScanCallback start.\n"); + if (code != 0) { + printf("server add callback error[%d].\n", code); + return -1; + } + + ReadInt32(reply, (int *)owner); + printf("WifiScanCallback return[%d].\n", *(int*)owner); + return 0; +} + +void WifiWpaStart(void) +{ + if (g_serverProxy == NULL) { + printf("g_serverProxy is null\n"); + return; + } + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_START, NULL, NULL, NULL); + if (ret != 0) { + printf("WifiStart failed ret [%d]\n", ret); + } +} + +void WifiScan(void) +{ + printf("====== WifiScan start ======\n"); + + if (g_serverProxy != NULL) { + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_SCAN, NULL, NULL, NULL); + printf("SERVER_OP_ADD callback ret=[%d] ret=[%d]\n", ret, ret); + if (ret != 0) { + printf("SERVER_OP_ADD callback ret [%d]\n", ret); + } + } else { + printf("g_serverProxy is null\n"); + } + + printf("====== WifiScan end ======\n"); +} + +void WifiConnect(const char* ssid, const char* pwd) +{ + if (g_serverProxy == NULL || ssid == NULL || pwd == NULL) { + printf("g_serverProxy is null\n"); + return; + } + + IpcIo request; + uint8_t dataAdd[IPC_MAX_SIZE]; + IpcIoInit(&request, dataAdd, IPC_MAX_SIZE, 0); + WriteString(&request, ssid); + WriteString(&request, pwd); + + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_CONNECT, &request, NULL, NULL); + if (ret != 0) { + printf("WifiConnect failed\n"); + } +} + +static int GetWifiInfoCB(IOwner owner, int code, IpcIo *reply) +{ + if (code != 0) { + printf("NULLWifiInfoCB error[%d].\n", code); + return -1; + } + + size_t len; + const char *ssidInfo = (const char*)ReadString(reply, &len); + if (ssidInfo == NULL) { + printf("read inputPwd failed!\n"); + return -1; + } + + return strcpy_s(owner, MAX_DATA_LEN, ssidInfo); +} + +int GetCurrentWifiConnInfo(char *ssid, int len) +{ + if (g_serverProxy == NULL || ssid == NULL) { + printf("g_serverProxy is null\n"); + return -1; + } + if (len > MAX_DATA_LEN) { + printf("ssid len more than 64\n"); + return -1; + } + + IpcIo request; + IpcIoInit(&request, ssid, IPC_MAX_SIZE, 0); + + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_GETINFO, &request, ssid, GetWifiInfoCB); + if (ret != 0) { + printf("GetCurrentWifiConnInfo failed \n"); + } + return 0; +} + +static int GetWifiIdNumCB(IOwner owner, int code, IpcIo *reply) +{ + printf("GetWifiIdNumCB start.\n"); + if (code != 0) { + printf("GetWifiIdNumCB error[%d].\n", code); + return -1; + } + + ReadInt32(reply, owner); + return 0; +} + +int GetWifiIdNum(void) +{ + int idNum = 0; + IpcIo request; + char data[MAX_DATA_LEN]; + IpcIoInit(&request, data, IPC_MAX_SIZE, 0); + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_GET_IDNUM, &request, &idNum, GetWifiIdNumCB); + if (ret != 0) { + printf("GetCurrentWifiConnInfo failed ret [%d]\n", ret); + } + return idNum; +} + +static int SetWifiEnableCB(IOwner owner, int code, IpcIo *reply) +{ + printf("SetWifiEnableCB start.\n"); + if (code != 0) { + printf("SetWifiEnableCB error[%d].\n", code); + return -1; + } + + ReadInt32(reply, owner); + return 0; +} + +int SetWifiEnable(int enable) +{ + int respRet = 0; + IpcIo request; + char data[MAX_DATA_LEN]; + IpcIoInit(&request, data, IPC_MAX_SIZE, 0); + WriteInt32(&request, enable); + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_SET_WIFI_SWITCH, &request, &respRet, SetWifiEnableCB); + if (ret != 0) { + printf("GetCurrentWifiConnInfo failed ret [%d]\n", ret); + } + printf("GetCurrentWifiConnInfo respRet [%d]\n", respRet); + return respRet; +} + +static int GetWifiScanStatCB(IOwner owner, int code, IpcIo *reply) +{ + printf("GetWifiScanStatCB start.\n"); + if (code != 0) { + printf("GetWifiScanStatCB error[%d].\n", code); + return -1; + } + + ReadInt32(reply, owner); + return 0; +} + +int GetAndResetWifiScanStat(void) +{ + int status = 0; + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_GET_WIFISTAT, NULL, &status, GetWifiScanStatCB); + if (ret != 0) { + printf("GetAndResetWifiScanStat failed ret [%d]\n", ret); + } + + return status; +} + +static int GetWifiSsidCB(IOwner owner, int code, IpcIo *reply) +{ + if (code != 0) { + printf("GetWifiSsidCB error[%d].\n", code); + return -1; + } + + size_t len; + const char *ssid = (const char*)ReadString(reply, &len); + if (ssid == NULL) { + printf("read inputPwd failed!\n"); + return -1; + } + + return strcpy_s(owner, MAX_DATA_LEN, ssid); +} + +int GetWifiSsid(const int ssidNum, char *ssid) +{ + IpcIo request; + char data[MAX_DATA_LEN]; + IpcIoInit(&request, data, IPC_MAX_SIZE, 0); + WriteInt32(&request, ssidNum); + int ret = g_serverProxy->Invoke(g_serverProxy, SERVER_OP_SETTING_GET_WIFISSID, &request, ssid, GetWifiSsidCB); + if (ret != 0) { + printf("GetAndResetWifiScanStat failed ret [%d]\n", ret); + } + return ret; +} + +int ServerProxyInit(void) +{ + int proxyInitCount = 0; + while (g_serverProxy == NULL) { + proxyInitCount++; + if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) { + printf("[ServerProxyInit] get server proxy error\n"); + return -1; + } + IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SETTING_SERVICE); + if (iUnknown == NULL) { + printf("iUnknown is null\n"); + sleep(1); + continue; + } + printf("[ServerProxyInit] GetDefaultFeatureApi success\n"); + + int ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&g_serverProxy); + if (ret != EC_SUCCESS || g_serverProxy == NULL) { + printf("QueryInterface failed [%d]\n", ret); + sleep(1); + continue; + } + } + + printf("[ServerProxyInit] get client proxy ok\n"); + return 0; +} \ No newline at end of file diff --git a/setting/setting/src/main/cpp/setting_client.h b/setting/setting/src/main/cpp/setting_client.h new file mode 100644 index 0000000000000000000000000000000000000000..2a3b2c24eda0490c80e6b4411e7eeac159a7f3e8 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_client.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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_SETTING_CLIENT_H +#define OHOS_SETTING_CLIENT_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +#define SETTING_OK 0 +#define SETTING_ERR -1 + +int GetAndResetWifiScanStat(void); +int GetCurrentWifiConnInfo(char *ssid, int len); +int GetWifiIdNum(void); +int GetWifiSsid(const int ssidNum, char *ssid); +void WifiConnect(const char* ssid, const char* pwd); +int ServerProxyInit(void); +void WifiScan(void); +int SetWifiEnable(int enable); +void WifiWpaStart(void); + + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ +#endif // OHOS_SETTING_CLIENT_H \ No newline at end of file diff --git a/setting/setting/src/main/cpp/setting_server_lite/BUILD.gn b/setting/setting/src/main/cpp/setting_server_lite/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..f0a227f613848df6ef839efc7759d7f4aca7dcc6 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_server_lite/BUILD.gn @@ -0,0 +1,49 @@ +# 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 +# +# 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. + +import("//build/lite/config/component/lite_component.gni") + +lite_component("setting_service_lite") { + features = [ ":setting_service" ] +} + +executable("setting_service") { + sources = [ + "setting_server.c", + "wpa_work.c" + ] + include_dirs = [ + "//third_party/bounds_checking_function/include", + "//commonlibrary/utils_lite/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", + "//foundation/communication/ipc/ipc/native/c/manager/include", + "//applications/sample/camera/setting/setting/src/main/cpp", + "//third_party/wpa_supplicant/wpa_supplicant-2.9/src/common", + "//base/startup/init/interfaces/innerkits/include/syspara", + "//base/security/permission_lite/interfaces/kits", + ] + ldflags = [ + "-lstdc++", + "-lpthread", + "-lwpa", + "-lwpa_client", + ] + + deps = [ + "//foundation/communication/ipc/interfaces/innerkits/c/ipc:ipc_single", + "//foundation/systemabilitymgr/samgr_lite/samgr:samgr", + "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant", + ] + + configs -= [ "//build/lite/config:clang_opt" ] +} diff --git a/setting/setting/src/main/cpp/setting_server_lite/setting_server.c b/setting/setting/src/main/cpp/setting_server_lite/setting_server.c new file mode 100644 index 0000000000000000000000000000000000000000..d42d3b553ccae32dcf13f512b4e6272215d66c9d --- /dev/null +++ b/setting/setting/src/main/cpp/setting_server_lite/setting_server.c @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "ipc_skeleton.h" +#include "iproxy_server.h" +#include "iproxy_client.h" +#include "rpc_errno.h" +#include "rpc_log.h" +#include "serializer.h" +#include "samgr_lite.h" +#include "setting_stub.h" +#include "wpa_work.h" + +#define STACK_SIZE 0x800 +#define QUEUE_SIZE 20 + +typedef struct SettingSaInterface { + INHERIT_SERVER_IPROXY; +} SettingSaInterface; + +typedef struct SettingSaService { + INHERIT_SERVICE; + INHERIT_IUNKNOWNENTRY(SettingSaInterface); + Identity identity; +} SettingSaService; + +static const char *GetName(Service *service) +{ + (void)service; + return SETTING_SERVICE; +} + +static BOOL MessageHandle(Service *service, Request *msg) +{ + return TRUE; +} + +static TaskConfig GetTaskConfig(Service *service) +{ + (void)service; + TaskConfig config = { LEVEL_HIGH, PRI_BELOW_NORMAL, STACK_SIZE, QUEUE_SIZE, SHARED_TASK }; + return config; +} + +typedef struct { + enum ServerFuncId id; + int (*func)(IpcIo *req, IpcIo *reply); +} ServerInvokeCmd; + +static int ServerOpSettingStart() +{ + WpaClientStart(); + WpaScanReconnect(NULL, NULL, HIDDEN_CLOSE); + return 0; +} + +static int ServerOpSettingScan(IpcIo *req, IpcIo *reply) +{ + WpaScanReconnect(NULL, NULL, HIDDEN_CLOSE); + return 0; +} + +static const int g_maxPassword = 10; // Maximum length of a password. +static char* g_inputSsid = NULL; + +static int ServerOpSettingConnect(IpcIo *req, IpcIo *reply) +{ + size_t len; + const char *inputSsid = (const char*)ReadString(req, &len); + if (inputSsid == NULL) { + printf("read inputSsid failed!\n"); + return -1; + } + const char *inputPwd = (const char*)ReadString(req, &len); + if (inputPwd == NULL) { + printf("read inputPwd failed!\n"); + return -1; + } + WpaScanReconnect(inputSsid, inputPwd, HIDDEN_OPEN); + return 0; +} + +static int ServerOpSettingGetConnInfo(IpcIo *req, IpcIo *reply) +{ + char buff[64] = {0}; + if (GetCurrentConnInfo(buff, sizeof(buff)) != 0) { + printf("ServerOpSettingGetConnInfo failed\n"); + return -1; + } + WriteString(reply, buff); + return 0; +} + +static int ServerOpGetWifiIdNum(IpcIo *req, IpcIo *reply) +{ + WriteInt32(reply, GetIdNum()); + return 0; +} + +static int ServerOpGetAndResetWifiScanStat(IpcIo *req, IpcIo *reply) +{ + WriteInt32(reply, GetAndResetScanStat()); + return 0; +} + +static int ServerOpGetWifiSsid(IpcIo *req, IpcIo *reply) +{ + int ssidNum = 0; + ReadInt32(req, &ssidNum); + char *ssid = GetSsid(ssidNum); + WriteString(reply, GetSsid(ssidNum)); + return 0; +} + +static int ServerOpSetWifiSwtch(IpcIo *req, IpcIo *reply) +{ + int wifiEnable = 0; + ReadInt32(req, &wifiEnable); + int ret = setWifiSwitch(wifiEnable); + WriteInt32(reply, ret); + return 0; +} + +static BOOL Initialize(Service *service, Identity identity) +{ + if (service == NULL) { + RPC_LOG_ERROR("invalid param"); + return FALSE; + } + SettingSaService *settingSaService = (SettingSaService *)service; + settingSaService->identity = identity; + return TRUE; +} + +ServerInvokeCmd g_serverInvokeCmd[] = { + { SERVER_OP_SETTING_START, ServerOpSettingStart }, + { SERVER_OP_SETTING_SCAN, ServerOpSettingScan }, + { SERVER_OP_SETTING_CONNECT, ServerOpSettingConnect }, + { SERVER_OP_SETTING_GETINFO, ServerOpSettingGetConnInfo }, + { SERVER_OP_SETTING_GET_IDNUM, ServerOpGetWifiIdNum }, + { SERVER_OP_SETTING_GET_WIFISTAT, ServerOpGetAndResetWifiScanStat }, + { SERVER_OP_SETTING_GET_WIFISSID, ServerOpGetWifiSsid }, + { SERVER_OP_SETTING_SET_WIFI_SWITCH, ServerOpSetWifiSwtch }, +}; + +static int Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply) +{ + RPC_LOG_INFO("[wifi_settting_server]Invoke:RECEIVE FUNCID:%d", funcId); + int tblSize = sizeof(g_serverInvokeCmd) / sizeof(ServerInvokeCmd); + for (int i = 0; i < tblSize; i++) { + if (funcId == g_serverInvokeCmd[i].id) { + return g_serverInvokeCmd[i].func(req, reply); + } + } + RPC_LOG_INFO("[wifi_settting_server]not support func[%d]", funcId); + return -1; +} + +static SettingSaService g_SettingSaService = { + .GetName = GetName, + .Initialize = Initialize, + .MessageHandle = MessageHandle, + .GetTaskConfig = GetTaskConfig, + SERVER_IPROXY_IMPL_BEGIN, + .Invoke = Invoke, + IPROXY_END, +}; + +void __attribute__((weak)) HOS_SystemInit(void) +{ + SAMGR_Bootstrap(); + return; +} + +int main(int argc, char *argv[]) +{ + RPC_LOG_INFO("[wifi_settting_server] Enter System Ability Server"); + HOS_SystemInit(); + + while (1) { + pause(); + } + RPC_LOG_INFO("[wifi_settting_server] return"); + return -1; +} + +static void Init(void) +{ + RPC_LOG_INFO("[wifi_settting_server] Init start"); + if (SAMGR_GetInstance()->RegisterService((Service *)&g_SettingSaService) != TRUE) { + printf("[wifisettingservice] register service failed!\n"); + return; + } + printf("[wifi_settting_server] register service succeed.\n"); + if (SAMGR_GetInstance()->RegisterDefaultFeatureApi(SETTING_SERVICE, \ + GET_IUNKNOWN(g_SettingSaService)) != TRUE) { + (void)SAMGR_GetInstance()->UnregisterService(SETTING_SERVICE); + printf("[wifisettingservice] register featureapi failed!\n"); + return; + } +} + +SYSEX_SERVICE_INIT(Init); diff --git a/setting/setting/src/main/cpp/wpa_work.c b/setting/setting/src/main/cpp/setting_server_lite/wpa_work.c old mode 100755 new mode 100644 similarity index 90% rename from setting/setting/src/main/cpp/wpa_work.c rename to setting/setting/src/main/cpp/setting_server_lite/wpa_work.c index 0d617d97959e9d20bbfefde5e8f0fb7149299a0c..dae57b8ab1a4654514c8ce40eea648dae722dc2e --- a/setting/setting/src/main/cpp/wpa_work.c +++ b/setting/setting/src/main/cpp/setting_server_lite/wpa_work.c @@ -23,6 +23,9 @@ #define WPA_CTRL_REQUEST_FAIL "FAIL" #define SSID_LINE_MAX_LEN 256 #define WPA_SCAN_RESULT_SSID_COLUMN 4 +#define CMD_BUFFER_SIZE 1024 +#define REPLY_BUF_SMALL_LENGTH 64 +#define PATH "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/element/wpa_supplicant.conf" #define SAMPLE_INFO(format, args...) \ do { \ @@ -51,6 +54,7 @@ static pthread_t g_wpaThreadId = 0; static pthread_t g_scanThreadId = 0; static int g_scanAvailable = 0; static int g_Connect = 0; +static char g_networkId[20] = {0}; static void DumpString(const char *buf, int len, const char *tag) { @@ -294,7 +298,10 @@ int GetAndResetScanStat(void) char *GetSsid(const int ssidNum) { - return g_useSsidD[ssidNum]; + pthread_mutex_lock(&g_monitorConnMutex); + char *ssid = g_useSsidD[ssidNum]; + pthread_mutex_unlock(&g_monitorConnMutex); + return ssid; } void LockWifiData() @@ -383,7 +390,7 @@ static void CliRecvPending(void) { int pendingResult = -1; pthread_mutex_lock(&g_monitorConnMutex); - if (g_monitorConn!= NULL) { + if (g_monitorConn != NULL) { pendingResult = wpa_ctrl_pending(g_monitorConn); } while (pendingResult > 0) { @@ -432,35 +439,64 @@ static void* MonitorTask(void *args) return NULL; } +int setWifiSwitch(int enable) +{ + char cmd[200] = {0}; + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + size_t networkIdLen = sizeof(g_networkId); + + if (enable) { + WpaScanReconnect(NULL, NULL, HIDDEN_CLOSE); + return 0; + } + if (strlen(g_networkId) == 0) { + printf("g_networkId is null\n"); + return 0; + } + int err = sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, g_networkId); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return -1; + } + + SendCtrlCommand(cmd, reply, &replyLen); + /* disable wifi clear g_networkId */ + memset_s(g_networkId, sizeof(g_networkId), 0, sizeof(g_networkId)); + return 0; +} + static void TestNetworkConfig(const char *gSsid, const char *gPassWord) { - char networkId[20] = {0}; - size_t networkIdLen = sizeof(networkId); - int ret = SendCtrlCommand("DISCONNECT", networkId, &networkIdLen); - ret += SendCtrlCommand("ADD_NETWORK", networkId, &networkIdLen); + if (strlen(g_networkId) != 0) { + memset_s(g_networkId, sizeof(g_networkId), 0, sizeof(g_networkId)); + } + size_t networkIdLen = sizeof(g_networkId); + int ret = SendCtrlCommand("DISCONNECT", g_networkId, &networkIdLen); + ret += SendCtrlCommand("ADD_NETWORK", g_networkId, &networkIdLen); if (ret != 0) { SAMPLE_ERROR("add network failed."); return; } - SAMPLE_INFO("add network success, network id [%.*s]", networkIdLen, networkId); + SAMPLE_INFO("add network success, network id [%.*s]", networkIdLen, g_networkId); char reply[100] = {0}; size_t replyLen = sizeof(reply); char cmd[200] = {0}; - int err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s ssid \"%s\"", networkIdLen, networkId, gSsid); + int err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s ssid \"%s\"", networkIdLen, g_networkId, gSsid); if (err < 0) { printf("[ERROR]sprintf_s failed, err = %d\n", err); return; } ret += SendCtrlCommand(cmd, reply, &replyLen); replyLen = sizeof(reply); - err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s psk \"%s\"", networkIdLen, networkId, gPassWord); + err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s psk \"%s\"", networkIdLen, g_networkId, gPassWord); if (err < 0) { printf("[ERROR]sprintf_s failed, err = %d\n", err); return; } ret += SendCtrlCommand(cmd, reply, &replyLen); replyLen = sizeof(reply); - err = sprintf_s(cmd, sizeof(cmd), "ENABLE_NETWORK %.*s", networkIdLen, networkId); + err = sprintf_s(cmd, sizeof(cmd), "ENABLE_NETWORK %.*s", networkIdLen, g_networkId); if (err < 0) { printf("[ERROR]sprintf_s failed, err = %d\n", err); return; @@ -473,13 +509,13 @@ static void TestNetworkConfig(const char *gSsid, const char *gPassWord) SAMPLE_INFO("network config success."); return; } - err = sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, networkId); + err = sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, g_networkId); if (err < 0) { printf("[ERROR]sprintf_s failed, err = %d\n", err); return; } SendCtrlCommand(cmd, reply, &replyLen); - SAMPLE_ERROR("network config failed remove network [%.*s].", networkIdLen, networkId); + SAMPLE_ERROR("network config failed remove network [%.*s].", networkIdLen, g_networkId); } int InitControlInterface() @@ -501,7 +537,7 @@ int InitControlInterface() ret = -1; pthread_mutex_lock(&g_monitorConnMutex); - if (g_monitorConn!= NULL) { + if (g_monitorConn != NULL) { ret = wpa_ctrl_attach(g_monitorConn); } pthread_mutex_unlock(&g_monitorConnMutex); @@ -559,7 +595,7 @@ static void *ThreadMain() arg[++i] = (char *)"-i"; arg[++i] = (char *)"wlan0"; arg[++i] = (char *)"-c"; - arg[++i] = (char *)"/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/element/wpa_supplicant.conf"; + arg[++i] = (char *)PATH; for (i = 0; i < myfor; i++) { printf("[LOG]arg[%d]->%s \n", i, arg[i]); diff --git a/setting/setting/src/main/cpp/wpa_work.h b/setting/setting/src/main/cpp/setting_server_lite/wpa_work.h old mode 100755 new mode 100644 similarity index 100% rename from setting/setting/src/main/cpp/wpa_work.h rename to setting/setting/src/main/cpp/setting_server_lite/wpa_work.h diff --git a/setting/setting/src/main/cpp/setting_stub.h b/setting/setting/src/main/cpp/setting_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..0182258d9bbf1fc88d62d15aa1432d21711b408f --- /dev/null +++ b/setting/setting/src/main/cpp/setting_stub.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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_SETTING_STUB_H +#define OHOS_SETTING_STUB_H + +#define SETTING_SERVICE "settingservice" +#define IPC_MAX_SIZE 128 + +enum ServerFuncId { + SERVER_OP_SETTING_START = 1, + SERVER_OP_SETTING_SCAN, + SERVER_OP_SETTING_CONNECT, + SERVER_OP_SETTING_GETINFO, + SERVER_OP_SETTING_GET_IDNUM, + SERVER_OP_SETTING_GET_WIFISTAT, + SERVER_OP_SETTING_GET_WIFISSID, + SERVER_OP_SETTING_SET_WIFI_SWITCH +}; + +#endif // OHOS_SETTING_STUB_H \ No newline at end of file diff --git a/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp b/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp old mode 100755 new mode 100644 index c37b18fa1ef51e08f534a3d18f065799ee412e66..715ddca5acb32ce377790c5863b0c198d1bb1379 --- a/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp +++ b/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp @@ -17,6 +17,7 @@ #include #include #include "gfx_utils/style.h" +#include "setting_client.h" namespace OHOS { REGISTER_AS(SettingWifiAbilitySlice) @@ -24,7 +25,7 @@ static int g_wifiStatus = 0; SettingWifiAbilitySlice::SettingWifiAbilitySlice() : headView_(nullptr), toggleButtonView_(nullptr), scrollView_(nullptr), rootView_(nullptr), - changeListener_(nullptr), buttonBackListener_(nullptr), buttonInputListener_(nullptr), wpaCount_(0) + changeListener_(nullptr), buttonBackListener_(nullptr), buttonInputListener_(nullptr) { int taskPeriod = 5000; Task::Init(); @@ -33,13 +34,6 @@ SettingWifiAbilitySlice::SettingWifiAbilitySlice() SettingWifiAbilitySlice::~SettingWifiAbilitySlice() { - if (wpaCount_ == 1) { - printf("%s:%d call ExitWpa\n", __FUNCTION__, __LINE__); - ExitWpa(); - ExitWpaScan(); - wpaCount_ = 0; - } - if (toggleButtonView_) { DeleteChildren(toggleButtonView_); toggleButtonView_ = nullptr; @@ -165,11 +159,13 @@ void SettingWifiAbilitySlice::SetUseWifi(void) rootView_->Add(lablelFont); } +static char g_ssidBuff[40][40] = {0}; + void SettingWifiAbilitySlice::AddWifi(void) { int ssidIndex, ssidCount; - ssidCount = GetIdNum(); + ssidCount = GetWifiIdNum(); if (ssidCount == 0) { printf("[LOG]SettingWifiAbilitySlice::AddWifi ssidCount == 0 \n"); return; @@ -182,6 +178,10 @@ void SettingWifiAbilitySlice::AddWifi(void) printf("[LOG]SettingWifiAbilitySlice::AddWifi wifiStatus == 1 \n"); return; } + if (memset_s(g_ssidBuff, sizeof(g_ssidBuff), 0, sizeof(g_ssidBuff)) != EOK) { + printf("[ERROR]g_ssidBuff memset_s failed\n"); + return; + } for (ssidIndex = 0; ssidIndex < ssidCount; ssidIndex++) { UIViewGroup *useWifiView = new UIViewGroup(); useWifiView->SetPosition(ADD_WIFI_X, DE_ITEM_INTERVAL * ssidIndex, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); @@ -189,14 +189,14 @@ void SettingWifiAbilitySlice::AddWifi(void) useWifiView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); useWifiView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); useWifiView->SetTouchable(true); - char* buff = GetSsid(ssidIndex); // GetSsid need return point - SetWifiButtonListener(buff); + GetWifiSsid(ssidIndex, g_ssidBuff[ssidIndex]); // GetSsid need return point + SetWifiButtonListener(g_ssidBuff[ssidIndex]); useWifiView->SetOnClickListener(buttonInputListener_); scrollView_->Add(useWifiView); auto lablelFont = new UILabel(); lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); - lablelFont->SetText(buff); + lablelFont->SetText(g_ssidBuff[ssidIndex]); lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); useWifiView->Add(lablelFont); @@ -220,21 +220,15 @@ void SettingWifiAbilitySlice::SetScrollWifi(void) void SettingWifiAbilitySlice::Callback() { - if (GetAndResetScanStat() == 1) { - LockWifiData(); + if (GetAndResetWifiScanStat() == 1) { AddWifi(); - UnLockWifiData(); } } void SettingWifiAbilitySlice::OnStart(const Want& want) { - if (wpaCount_ == 0) { - printf("%s:%d call WpaClientStart\n", __FUNCTION__, __LINE__); - WpaClientStart(); - WpaScanReconnect(nullptr, nullptr, HIDDEN_CLOSE); - wpaCount_ = 1; - } + WifiWpaStart(); + WifiScan(); AbilitySlice::OnStart(want); rootView_ = RootView::GetWindowRootView(); @@ -268,11 +262,5 @@ void SettingWifiAbilitySlice::OnBackground() void SettingWifiAbilitySlice::OnStop() { AbilitySlice::OnStop(); - if (wpaCount_ == 1) { - printf("%s:%d call ExitWpa,\n", __FUNCTION__, __LINE__); - ExitWpa(); - ExitWpaScan(); - wpaCount_ = 0; - } } } // namespace OHOS diff --git a/setting/setting/src/main/cpp/setting_wifi_ability_slice.h b/setting/setting/src/main/cpp/setting_wifi_ability_slice.h old mode 100755 new mode 100644 index 2ebf871474657b443ac341f2a3ad8d723a0225b7..d42e9e25c8666b347017ac3b179eb28b392264dd --- a/setting/setting/src/main/cpp/setting_wifi_ability_slice.h +++ b/setting/setting/src/main/cpp/setting_wifi_ability_slice.h @@ -32,7 +32,7 @@ #include "parameter.h" #include "pthread.h" #include "setting_utils.h" -#include "wpa_work.h" +#include "setting_client.h" namespace OHOS { class TestBtnOnStateChangeListener : public OHOS::UICheckBox::OnChangeListener, public OHOS::UIView::OnClickListener { @@ -48,8 +48,10 @@ public: { bool ret = myUiView->IsVisible(); if (ret == false) { + SetWifiEnable(true); myUiView->SetVisible(true); } else { + SetWifiEnable(false); myUiView->SetVisible(false); } myUiView->Invalidate(); @@ -90,7 +92,6 @@ private: TestBtnOnStateChangeListener* changeListener_; EventListener* buttonBackListener_; EventListener* buttonInputListener_; - int wpaCount_; constexpr static int TOGGLE_X = 36; constexpr static int TOGGLE_Y = 72; diff --git a/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp old mode 100755 new mode 100644 index 4b8ecba114280089a3c92abeed1986bbfc4858fa..4ffcaee8bd67e159be6a9a96314786a95fad5cd1 --- a/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp +++ b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp @@ -15,6 +15,7 @@ #include #include "gfx_utils/style.h" +#include "setting_client.h" namespace OHOS { REGISTER_AS(SettingWifiInputPasswordAbilitySlice) @@ -83,7 +84,7 @@ public: ~TestBtnOnClickEnterChangeListener() {} bool OnClick(UIView& view, const ClickEvent& event) override { - WpaScanReconnect(g_inputSsid, g_inputPassword, HIDDEN_OPEN); + WifiConnect(g_inputSsid, g_inputPassword); g_inputSsid = nullptr; int err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword)); if (err != EOK) { diff --git a/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h old mode 100755 new mode 100644 index 03f86e371b2a6f9cb0b8b96ca3a92f44c93f728b..f091ea7b762577d41d23d67252130690260bc55c --- a/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h +++ b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h @@ -29,7 +29,6 @@ #include "parameter.h" #include "setting_utils.h" #include "setting_wifi_input_password_ability_slice.h" -#include "wpa_work.h" namespace OHOS { class SettingWifiInputPasswordAbilitySlice : public AbilitySlice {