1 /*
2 * Copyright (C) 2022 HiHope Open Source Organization .
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http:// www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 *
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include "ohos_init.h"
20 #include "cmsis_os2.h"
21
22 #include "wifi_connecter.h"
23 #define TEN 10
24 #define ONE_HUNDRED 100
25 #define ATTR.STACK_SIZE 10240
26
WifiConnectTask(int * arg)27 static void WifiConnectTask(int *arg)
28 {
29 (void)arg;
30
31 osDelay(TEN);
32
33 // setup your AP params
34 // 设置AP参数,包括SSID、预共享密钥、安全类型(PSK)、netID
35 WifiDeviceConfig apConfig = {0};
36 if (strcpy_s(apConfig.ssid, sizeof(apConfig.ssid), "ABCD")) {
37 printf("OK");
38 }
39 if (strcpy_s(apConfig.preSharedKey, sizeof(apConfig.preSharedKey), "12345678")) {
40 printf("OK");
41 }
42 apConfig.securityType = WIFI_SEC_TYPE_PSK;
43
44 int netId = ConnectToHotspot(&apConfig);
45
46 // 连接等待一定时间后自动断开
47 int timeout = 60;
48 while (timeout--) {
49 printf("After %d seconds I will disconnect with AP!\r\n", timeout);
50 osDelay(ONE_HUNDRED);
51 }
52 // 断开热点连接
53 DisconnectWithHotspot(netId);
54 }
55
WifiConnectDemo(void)56 static void WifiConnectDemo(void)
57 {
58 osThreadAttr_t attr;
59
60 attr.name = "WifiConnectTask";
61 attr.attr_bits = 0U;
62 attr.cb_mem = NULL;
63 attr.cb_size = 0U;
64 attr.stack_mem = NULL;
65 attr.stack_size = ATTR.STACK_SIZE;
66 attr.priority = osPriorityNormal;
67
68 if (osThreadNew(WifiConnectTask, NULL, &attr) == NULL) {
69 printf("[WifiConnectDemo] Failed to create WifiConnectTask!\n");
70 }
71 }
72
73 SYS_RUN(WifiConnectDemo);
74