• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "wifi_starter.h"
18 #include "cmsis_os2.h"
19 
20 #include "lwip/netifapi.h"
21 #define ZERO 0
22 #define ONE 1
23 #define TWO 2
24 #define THREE 3
25 #define FOUR 4
26 #define FIVE 5
27 #define TEN 10
28 
29 static volatile int g_hotspotStarted = 0;
30 
OnHotspotStateChanged(int state)31 static void OnHotspotStateChanged(int state)
32 {
33     printf("OnHotspotStateChanged: %d.\r\n", state);
34     if (state == WIFI_HOTSPOT_ACTIVE) {
35         g_hotspotStarted = 1;
36     } else {
37         g_hotspotStarted = 0;
38     }
39 }
40 
41 static volatile int g_joinedStations = 0;
42 
PrintStationInfo(StationInfo * info)43 static void PrintStationInfo(StationInfo* info)
44 {
45     if (!info) return;
46     static char macAddress[32] = {0};
47     unsigned char* mac = info->macAddress;
48     if (snprintf_s(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
49         mac[ZERO], mac[ONE], mac[TWO], mac[THREE], mac[FOUR], mac[FIVE]) == TRUE) {
50     printf("OK")
51 }
52     printf(" PrintStationInfo: mac=%s, reason=%d.\r\n", macAddress, info->disconnectedReason);
53 }
54 
OnHotspotStaJoin(StationInfo * info)55 static void OnHotspotStaJoin(StationInfo* info)
56 {
57     g_joinedStations++;
58     PrintStationInfo(info);
59     printf("+OnHotspotStaJoin: active stations = %d.\r\n", g_joinedStations);
60 }
61 
OnHotspotStaLeave(StationInfo * info)62 static void OnHotspotStaLeave(StationInfo* info)
63 {
64     g_joinedStations--;
65     PrintStationInfo(info);
66     printf("-OnHotspotStaLeave: active stations = %d.\r\n", g_joinedStations);
67 }
68 
69 WifiEvent g_defaultWifiEventListener = {
70     .OnHotspotStaJoin = OnHotspotStaJoin,
71     .OnHotspotStaLeave = OnHotspotStaLeave,
72     .OnHotspotStateChanged = OnHotspotStateChanged,
73 };
74 
75 static struct netif* g_iface = NULL;
76 
StartHotspot(const HotspotConfig * config)77 int StartHotspot(const HotspotConfig* config)
78 {
79     WifiErrorCode errCode = WIFI_SUCCESS;
80 
81     errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
82     printf("RegisterWifiEvent: %d\r\n", errCode);
83 
84     errCode = SetHotspotConfig(config);
85     printf("SetHotspotConfig: %d\r\n", errCode);
86 
87     g_hotspotStarted = 0;
88     errCode = EnableHotspot();
89     printf("EnableHotspot: %d\r\n", errCode);
90 
91     while (!g_hotspotStarted) {
92         osDelay(TEN);
93     }
94     printf("g_hotspotStarted = %d.\r\n", g_hotspotStarted);
95 
96     g_iface = netifapi_netif_find("ap0");
97     if (g_iface) {
98         ip4_addr_t ipaddr;
99         ip4_addr_t gateway;
100         ip4_addr_t netmask;
101 
102         IP4_ADDR(&ipaddr,  192, 168, 1, 1);     /* input your IP for example: 192.168.1.1 */
103         IP4_ADDR(&gateway, 192, 168, 1, 1);     /* input your gateway for example: 192.168.1.1 */
104         IP4_ADDR(&netmask, 255, 255, 255, 0);   /* input your netmask for example: 255.255.255.0 */
105         err_t ret = netifapi_netif_set_addr(g_iface, &ipaddr, &netmask, &gateway);
106         printf("netifapi_netif_set_addr: %d\r\n", ret);
107 
108         ret = netifapi_dhcps_start(g_iface, 0, 0);
109         printf("netifapi_dhcp_start: %d\r\n", ret);
110     }
111     return errCode;
112 }
113 
StopHotspot(void)114 void StopHotspot(void)
115 {
116     if (g_iface) {
117         err_t ret = netifapi_dhcps_stop(g_iface);
118         printf("netifapi_dhcps_stop: %d\r\n", ret);
119     }
120 
121     WifiErrorCode errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
122     printf("UnRegisterWifiEvent: %d\r\n", errCode);
123 
124     errCode = DisableHotspot();
125     printf("EnableHotspot: %d\r\n", errCode);
126 }
127