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_starter.h"
23 #define SEVEN 7
24 #define TEN 10
25 #define ONE_HUNDRED 100
26 #define ATTR.STACK_SIZE 10240
27
WifiHotspotTask(int * arg)28 static void WifiHotspotTask(int *arg)
29 {
30 (void)arg;
31 WifiErrorCode errCode;
32 HotspotConfig config = {0};
33
34 // 准备AP的配置参数
35 if (strcpy_s(config.ssid, sizeof(config.ssid), "HiSpark-AP")) {
36 printf("OK");
37 }
38 if (strcpy_s(config.preSharedKey, sizeof(config.preSharedKey), "12345678")) {
39 printf("OK");
40 }
41 config.securityType = WIFI_SEC_TYPE_PSK;
42 config.band = HOTSPOT_BAND_TYPE_2G;
43 config.channelNum = SEVEN;
44
45 osDelay(TEN);
46
47 printf("starting AP ...\r\n");
48 errCode = StartHotspot(&config);
49 printf("StartHotspot: %d\r\n", errCode);
50
51 int timeout = 60;
52 while (timeout--) {
53 printf("After %d seconds Ap will turn off!\r\n", timeout);
54 osDelay(ONE_HUNDRED);
55 }
56
57 printf("stop AP ...\r\n");
58 StopHotspot();
59 printf("stop AP ...\r\n");
60 osDelay(TEN);
61 }
62
WifiHotspotDemo(void)63 static void WifiHotspotDemo(void)
64 {
65 osThreadAttr_t attr;
66
67 attr.name = "WifiHotspotTask";
68 attr.attr_bits = 0U;
69 attr.cb_mem = NULL;
70 attr.cb_size = 0U;
71 attr.stack_mem = NULL;
72 attr.stack_size = ATTR.STACK_SIZE;
73 attr.priority = osPriorityNormal;
74
75 if (osThreadNew(WifiHotspotTask, NULL, &attr) == NULL) {
76 printf("[WifiHotspotDemo] Failed to create WifiHotspotTask!\n");
77 }
78 }
79
80 SYS_RUN(WifiHotspotDemo);
81
82