From d34d7e2b340037ec9bffec2032c1716f4098fc3a Mon Sep 17 00:00:00 2001 From: mamingshuai Date: Thu, 11 Mar 2021 18:36:58 +0800 Subject: [PATCH] update openharmony 1.0.1 --- BUILD.gn | 27 +++ LICENSE | 177 ++++++++++++++++ README.en.md | 36 ---- README.md | 50 +---- hostapd/BUILD.gn | 40 ++++ hostapd/config/hostapd.conf | 7 + hostapd/src/hostapd_sample.c | 70 +++++++ wpa_cli/BUILD.gn | 44 ++++ wpa_cli/src/wpa_cli_sample.c | 242 ++++++++++++++++++++++ wpa_supplicant/BUILD.gn | 40 ++++ wpa_supplicant/config/wpa_supplicant.conf | 4 + wpa_supplicant/src/wpa_sample.c | 70 +++++++ 12 files changed, 732 insertions(+), 75 deletions(-) create mode 100755 BUILD.gn create mode 100755 LICENSE delete mode 100644 README.en.md mode change 100644 => 100755 README.md create mode 100755 hostapd/BUILD.gn create mode 100755 hostapd/config/hostapd.conf create mode 100755 hostapd/src/hostapd_sample.c create mode 100755 wpa_cli/BUILD.gn create mode 100755 wpa_cli/src/wpa_cli_sample.c create mode 100755 wpa_supplicant/BUILD.gn create mode 100755 wpa_supplicant/config/wpa_supplicant.conf create mode 100755 wpa_supplicant/src/wpa_sample.c diff --git a/BUILD.gn b/BUILD.gn new file mode 100755 index 0000000..18a1ad9 --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,27 @@ +# Copyright (c) 2020 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("sample") { + features = [ + ] +} + +generate_notice_file("communication_sample") { + module_name = "communication_sample" + module_source_dir_list = [ + "//third_party/wpa_supplicant/wpa_supplicant-2.9/hostapd", + "//third_party/wpa_supplicant/wpa_supplicant-2.9/wpa_supplicant", + ] +} diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..4a45986 --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.en.md b/README.en.md deleted file mode 100644 index a89209a..0000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# applications_camera_sample_communication - -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 9c97c97..519f9bc --- a/README.md +++ b/README.md @@ -1,39 +1,11 @@ -# applications_camera_sample_communication - -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} - -#### 软件架构 -软件架构说明 - - -#### 安装教程 - -1. xxxx -2. xxxx -3. xxxx - -#### 使用说明 - -1. xxxx -2. xxxx -3. xxxx - -#### 参与贡献 - -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request - - -#### 特技 - -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +## Introduction +This directory is the sample code of the communication subsystem. For now, it contains the usage example of wpa_supplicant. +## Directions + +``` +. +├── hostapd +├── wpa_cli +└── wpa_supplicant + +``` diff --git a/hostapd/BUILD.gn b/hostapd/BUILD.gn new file mode 100755 index 0000000..3fe3aa4 --- /dev/null +++ b/hostapd/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2020 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") + +sample_sources = [ + "src/hostapd_sample.c", +] + +config_file = [ + "config/hostapd.conf" +] + +executable("hostapd_sample_exe") { + output_name = "hostapd" + sources = sample_sources +} + +group("hostapd_sample") { + deps = [ + ":hostapd_sample_exe", + ] +} + +copy("config") { + sources = config_file + outputs = [ + "$root_out_dir/etc/hostapd.conf" + ] +} diff --git a/hostapd/config/hostapd.conf b/hostapd/config/hostapd.conf new file mode 100755 index 0000000..bc6057c --- /dev/null +++ b/hostapd/config/hostapd.conf @@ -0,0 +1,7 @@ +interface=wlan0 +driver=hdf wifi +ctrl_interface=udp +ssid=testap +hw_mode=g +channel=1 +ignore_broadcast_ssid=0 diff --git a/hostapd/src/hostapd_sample.c b/hostapd/src/hostapd_sample.c new file mode 100755 index 0000000..fa1975b --- /dev/null +++ b/hostapd/src/hostapd_sample.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 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 + +pthread_t g_apThread; + +char* g_apArg[20] = {0}; +int g_apArgc = 0; + +static void* ThreadMain() +{ + printf("[HostapdSample]init hostapd.\n"); + + void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL); + if (handleLibWpa == NULL) { + printf("[HostapdSample]dlopen libwpa failed.\n"); + return NULL; + } + int (*func)(int, char **) = NULL; + func = dlsym(handleLibWpa, "ap_main"); + if (func == NULL) { + dlclose(handleLibWpa); + printf("[HostapdSample]dlsym ap_main failed.\n"); + return NULL; + } + int ret = func(g_apArgc, g_apArg); + + printf("[HostapdSample]run ap_main failed, ret:%d.\n", ret); + for (int i = 0; i < g_apArgc; i++) { + printf("[HostapdSample]arg %d:%s.\n", i, g_apArg[i]); + } + + if (dlclose(handleLibWpa) != 0) { + printf("[HostapdSample]dlclose libwpa failed.\n"); + return NULL; + } + return NULL; +} + +int main(int argc, char *argv[]) +{ + g_apArgc = argc; + for (int i = 0; i < g_apArgc; i++) { + g_apArg[i] = argv[i]; + } + + int ret = pthread_create(&g_apThread, NULL, ThreadMain, NULL); + if (ret != 0) { + printf("[HostapdSample]create thread failed error:%s.\n", strerror(ret)); + return 1; + } + pthread_join(g_apThread, NULL); + return 0; +} diff --git a/wpa_cli/BUILD.gn b/wpa_cli/BUILD.gn new file mode 100755 index 0000000..f29be47 --- /dev/null +++ b/wpa_cli/BUILD.gn @@ -0,0 +1,44 @@ +# Copyright (c) 2020 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") + +sample_sources = [ + "src/wpa_cli_sample.c", +] + +sample_include_dirs = [ + "//third_party/wpa_supplicant/wpa_supplicant-2.9/src/", + "//third_party/bounds_checking_function:libsec_shared/include/" +] + +executable("wpa_cli_exe") { + output_name = "wpa_cli" + sources = sample_sources + include_dirs = sample_include_dirs + out_dir = rebase_path(root_build_dir) + deps = [ + "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant", + "//third_party/bounds_checking_function:libsec_shared" + ] + ldflags = [ + "-L${out_dir}", + "-lwpa_client" + ] +} + +group("wpa_cli_sample") { + deps = [ + ":wpa_cli_exe", + ] +} diff --git a/wpa_cli/src/wpa_cli_sample.c b/wpa_cli/src/wpa_cli_sample.c new file mode 100755 index 0000000..0895f79 --- /dev/null +++ b/wpa_cli/src/wpa_cli_sample.c @@ -0,0 +1,242 @@ +/* + * Copyright (c) 2020 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 "utils/includes.h" +#include "pthread.h" +#include "common/wpa_ctrl.h" +#include "securec.h" + + +#define WPA_IFACE_NAME "wlan0" +#define WIFI_AUTH_FAILED_REASON_STR "WRONG_KEY" +#define WIFI_AUTH_FAILED_REASON_CODE "reason=2" +#define WPA_CTRL_REQUEST_OK "OK" +#define WPA_CTRL_REQUEST_FAIL "FAIL" + +#define SAMPLE_INFO(format, args...) \ + do { \ + fprintf(stderr, "\033[1;32m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + +#define SAMPLE_ERROR(format, args...) \ + do { \ + fprintf(stderr, "\033[1;31m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + + +static struct wpa_ctrl *g_monitorConn; +static struct wpa_ctrl *g_ctrlConn; +static pthread_t g_wpaThreadId; +static int g_scanAvailable = 0; + +static void DumpString(const char *buf, int len, const char *tag) +{ + SAMPLE_INFO("%s dump start.", tag); + for (int i = 0; i < len; i++) { + printf("%c", buf[i]); + } + printf("\n"); + SAMPLE_INFO("%s dump end.", tag); +} + +static int StrMatch(const char *a, const char *b) +{ + return strncmp(a, b, strlen(b)) == 0; +} + +static void WifiEventHandler(char *rawEvent, int len) +{ + char *pos = rawEvent; + if (*pos == '<') { + pos = strchr(pos, '>'); + if (pos) { + pos++; + } else { + pos = rawEvent; + } + } + if (StrMatch(pos, WPA_EVENT_CONNECTED)) { + SAMPLE_INFO("WIFI_EVENT_CONNECTED"); + return; + } + if (StrMatch(pos, WPA_EVENT_SCAN_RESULTS)) { + SAMPLE_INFO("WIFI_EVENT_SCAN_DONE"); + g_scanAvailable = 1; + return; + } + if (StrMatch(pos, WPA_EVENT_TEMP_DISABLED) && strstr(pos, WIFI_AUTH_FAILED_REASON_STR)) { + SAMPLE_INFO("WIFI_EVENT_WRONG_KEY"); + return; + } + if (StrMatch(pos, WPA_EVENT_DISCONNECTED) && !strstr(pos, WIFI_AUTH_FAILED_REASON_CODE)) { + SAMPLE_INFO("WIFI_EVENT_DISCONNECTED"); + return; + } +} + +static void CliRecvPending(void) +{ + while (wpa_ctrl_pending(g_monitorConn)) { + char buf[4096]; + size_t len = sizeof(buf) - 1; + if (wpa_ctrl_recv(g_monitorConn, buf, &len) == 0) { + buf[len] = '\0'; + SAMPLE_INFO("event received %s", buf); + WifiEventHandler(buf, len); + } else { + SAMPLE_INFO("could not read pending message."); + break; + } + } +} + +static void* MonitorTask(void *args) +{ + (void)args; + int fd, ret; + fd_set rfd; + while (1) { + fd = wpa_ctrl_get_fd(g_monitorConn); + FD_ZERO(&rfd); + FD_SET(fd, &rfd); + ret = select(fd + 1, &rfd, NULL, NULL, NULL); + if (ret <= 0) { + SAMPLE_INFO("select failed ret = %d\n", ret); + break; + } + CliRecvPending(); + sleep(1); + } + return NULL; +} + +static int SendCtrlCommand(const char *cmd, char *reply, size_t *replyLen) +{ + size_t len = *replyLen - 1; + wpa_ctrl_request(g_ctrlConn, cmd, strlen(cmd), reply, &len, 0); + DumpString(reply, len, "SendCtrlCommand raw return"); + if (len != 0 && !StrMatch(reply, WPA_CTRL_REQUEST_FAIL)) { + *replyLen = len; + return 0; + } + SAMPLE_ERROR("send ctrl request [%s] failed.", cmd); + return -1; +} + +static void TestNetworkConfig(void) +{ + char networkId[20] = {0}; + size_t networkIdLen = sizeof(networkId); + int ret = SendCtrlCommand("DISCONNECT", networkId, &networkIdLen); + ret += SendCtrlCommand("ADD_NETWORK", networkId, &networkIdLen); + if (ret != 0) { + SAMPLE_ERROR("add network failed."); + return; + } + SAMPLE_INFO("add network success, network id [%.*s]", networkIdLen, networkId); + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + char cmd[200] = {0}; + sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s ssid \"example\"", networkIdLen, networkId); + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s psk \"012345678\"", networkIdLen, networkId); + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + sprintf_s(cmd, sizeof(cmd), "ENABLE_NETWORK %.*s", networkIdLen, networkId); + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + ret += SendCtrlCommand("RECONNECT", reply, &replyLen); + replyLen = sizeof(reply); + if (ret == 0) { + SAMPLE_INFO("network config success."); + return; + } + sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, networkId); + SendCtrlCommand(cmd, reply, &replyLen); + SAMPLE_ERROR("network config failed remove network [%.*s].", networkIdLen, networkId); +} + +static void TestCliConnection(void) +{ + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + int ret = SendCtrlCommand("PING", reply, &replyLen); + if (ret == 0 && StrMatch(reply, "PONG")) { + SAMPLE_INFO("connect to wpa success."); + return; + } + SAMPLE_INFO("connect to wpa failed, err = %s.", reply); +} + +static void TestScan() +{ + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + g_scanAvailable = 0; + SendCtrlCommand("SCAN", reply, &replyLen); + while (1) { + sleep(1); + if (g_scanAvailable == 1) { + SAMPLE_INFO("scan result received."); + break; + } + SAMPLE_INFO("waiting scan result."); + } + char scanResult[4096] = {0}; + size_t scanLen = sizeof(scanResult); + int ret = SendCtrlCommand("SCAN_RESULTS", scanResult, &scanLen); + if (ret != 0) { + SAMPLE_ERROR("request scan results failed."); + return; + } + DumpString(scanResult, scanLen, "scan results"); +} + +static void StartTest() +{ + TestCliConnection(); // test if wpa control interface connected successfully + TestScan(); // test scan and get scan results + TestNetworkConfig(); // test config network and connect +} + +int InitControlInterface() +{ + g_ctrlConn = wpa_ctrl_open(WPA_IFACE_NAME); // create control interface for send cmd + g_monitorConn = wpa_ctrl_open(WPA_IFACE_NAME); // create control interface for event monitor + if (!g_ctrlConn || !g_monitorConn) { + SAMPLE_ERROR("open wpa control interface failed."); + return -1; + } + if (wpa_ctrl_attach(g_monitorConn) == 0) { // start monitor + pthread_create(&g_wpaThreadId, NULL, MonitorTask, NULL); // create thread for read event + return 0; + } + return -1; +} + +int main() +{ + if (InitControlInterface() != 0) { + SAMPLE_ERROR("control interface init failed, exit client."); + return -1; + } + SAMPLE_INFO("control interface init success."); + StartTest(); + pthread_join(g_wpaThreadId, NULL); + SAMPLE_INFO("test finished, exit client."); +} diff --git a/wpa_supplicant/BUILD.gn b/wpa_supplicant/BUILD.gn new file mode 100755 index 0000000..81d52a4 --- /dev/null +++ b/wpa_supplicant/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2020 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") + +sample_sources = [ + "src/wpa_sample.c", +] + +config_file = [ + "config/wpa_supplicant.conf" +] + +executable("wpa_sample_exe") { + output_name = "wpa_supplicant" + sources = sample_sources +} + +group("wpa_sample") { + deps = [ + ":wpa_sample_exe", + ] +} + +copy("config") { + sources = config_file + outputs = [ + "$root_out_dir/etc/wpa_supplicant.conf" + ] +} diff --git a/wpa_supplicant/config/wpa_supplicant.conf b/wpa_supplicant/config/wpa_supplicant.conf new file mode 100755 index 0000000..f4a3a12 --- /dev/null +++ b/wpa_supplicant/config/wpa_supplicant.conf @@ -0,0 +1,4 @@ +country=GB +ctrl_interface=udp +network={ +} diff --git a/wpa_supplicant/src/wpa_sample.c b/wpa_supplicant/src/wpa_sample.c new file mode 100755 index 0000000..a30d8f6 --- /dev/null +++ b/wpa_supplicant/src/wpa_sample.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 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 + +pthread_t g_wpaThread; + +char* g_wpaArg[20] = {0}; +int g_wpaArgc = 0; + +static void* ThreadMain() +{ + printf("[WpaSample]init wpa_supplicant.\n"); + + void *handleLibWpa = dlopen("/usr/lib/libwpa.so", RTLD_NOW | RTLD_LOCAL); + if (handleLibWpa == NULL) { + printf("[WpaSample]dlopen libwpa failed.\n"); + return NULL; + } + int (*func)(int, char **) = NULL; + func = dlsym(handleLibWpa, "wpa_main"); + if (func == NULL) { + dlclose(handleLibWpa); + printf("[WpaSample]dlsym wpa_main failed.\n"); + return NULL; + } + int ret = func(g_wpaArgc, g_wpaArg); + + printf("[WpaSample]run wpa_main failed, ret:%d.\n", ret); + for (int i = 0; i < g_wpaArgc; i++) { + printf("[WpaSample]arg %d:%s.\n", i, g_wpaArg[i]); + } + + if (dlclose(handleLibWpa) != 0) { + printf("[WpaSample]dlclose libwpa failed.\n"); + return NULL; + } + return NULL; +} + +int main(int argc, char *argv[]) +{ + g_wpaArgc = argc; + for (int i = 0; i < g_wpaArgc; i++) { + g_wpaArg[i] = argv[i]; + } + + int ret = pthread_create(&g_wpaThread, NULL, ThreadMain, NULL); + if (ret != 0) { + printf("[WpaSample]create thread failed error:%s.\n", strerror(ret)); + return 1; + } + pthread_join(g_wpaThread, NULL); + return 0; +}