1 /*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 * limitations under the License.
14 */
15
16 #include "wifi_starter.h"
17 #include "cmsis_os2.h"
18
19 #include "lwip/netifapi.h"
20
21 static volatile int g_hotspotStarted = 0;
22
OnHotspotStateChanged(int state)23 static void OnHotspotStateChanged(int state)
24 {
25 printf("OnHotspotStateChanged: %d.\r\n", state);
26 if (state == WIFI_HOTSPOT_ACTIVE) {
27 g_hotspotStarted = 1;
28 } else {
29 g_hotspotStarted = 0;
30 }
31 }
32
33 static volatile int g_joinedStations = 0;
34
PrintStationInfo(const StationInfo * info)35 static void PrintStationInfo(const StationInfo* info)
36 {
37 int len = 0;
38
39 if (!info) {
40 return;
41 }
42 static char macAddress[32] = {0};
43 unsigned char* mac = info->macAddress;
44 if (snprintf_s(macAddress, sizeof(macAddress) + 1, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
45 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) < 0) { /* mac地址从0,1,2,3,4,5位 */
46 return;
47 }
48 }
49
OnHotspotStaJoin(const StationInfo * info)50 static void OnHotspotStaJoin(const StationInfo* info)
51 {
52 g_joinedStations++;
53 PrintStationInfo(info);
54 printf("+OnHotspotStaJoin: active stations = %d.\r\n", g_joinedStations);
55 }
56
OnHotspotStaLeave(const StationInfo * info)57 static void OnHotspotStaLeave(const StationInfo* info)
58 {
59 g_joinedStations--;
60 PrintStationInfo(info);
61 printf("-OnHotspotStaLeave: active stations = %d.\r\n", g_joinedStations);
62 }
63
64 WifiEvent g_defaultWifiEventListener = {
65 .OnHotspotStaJoin = OnHotspotStaJoin,
66 .OnHotspotStaLeave = OnHotspotStaLeave,
67 .OnHotspotStateChanged = OnHotspotStateChanged,
68 };
69
70 static struct netif* g_iface = NULL;
71
72 #define SSID_LEN (11)
73
StartHotspot(void)74 int StartHotspot(void)
75 {
76 WifiErrorCode errCode;
77 errCode = WIFI_SUCCESS;
78
79 HotspotConfig config = {0};
80
81 // 准备AP的配置参数
82 strcpy_s(config.ssid, SSID_LEN, "HiSpark-AP");
83 config.securityType = WIFI_SEC_TYPE_OPEN;
84 config.band = HOTSPOT_BAND_TYPE_2G;
85 config.channelNum = 7; /* 7: channel number */
86
87 errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
88 printf("RegisterWifiEvent: %d\r\n", errCode);
89
90 errCode = SetHotspotConfig(&config);
91 printf("SetHotspotConfig: %d\r\n", errCode);
92
93 g_hotspotStarted = 0;
94 errCode = EnableHotspot();
95 printf("EnableHotspot: %d\r\n", errCode);
96
97 while (!g_hotspotStarted) {
98 osDelay(10); /* 10 : OS Sleep 10 ms */
99 }
100 printf("g_hotspotStarted = %d.\r\n", g_hotspotStarted);
101
102 g_iface = netifapi_netif_find("ap1");
103 if (g_iface) {
104 ip4_addr_t ipaddr;
105 ip4_addr_t gateway;
106 ip4_addr_t netmask;
107
108 IP4_ADDR(&ipaddr, 192, 168, 1, 1); /* input your IP for example: 192.168.1.1 */
109 IP4_ADDR(&gateway, 192, 168, 1, 1); /* input your gateway for example: 192.168.1.1 */
110 IP4_ADDR(&netmask, 255, 255, 255, 0); /* input your netmask for example: 255.255.255.0 */
111 err_t ret = netifapi_netif_set_addr(g_iface, &ipaddr, &netmask, &gateway);
112 printf("netifapi_netif_set_addr: %d\r\n", ret);
113
114 ret = netifapi_dhcps_start(g_iface, 0, 0);
115 printf("netifapi_dhcp_start: %d\r\n", ret);
116 }
117 return errCode;
118 }
119
StopHotspot(void)120 void StopHotspot(void)
121 {
122 if (g_iface) {
123 err_t ret = netifapi_dhcps_stop(g_iface);
124 printf("netifapi_dhcps_stop: %d\r\n", ret);
125 }
126
127 WifiErrorCode errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
128 printf("UnRegisterWifiEvent: %d\r\n", errCode);
129
130 errCode = DisableHotspot();
131 printf("EnableHotspot: %d\r\n", errCode);
132 }
133