• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiHope Community.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "wifi_hotspot.h"
31 #include "wifi_device_util.h"
32 
33 #include <string.h>
34 #include <securec.h>
35 #include "wm_type_def.h"
36 #include "wm_wifi.h"
37 
38 #define RSSI_LEVEL_4_2_G (-65)
39 #define RSSI_LEVEL_3_2_G (-75)
40 #define RSSI_LEVEL_2_2_G (-82)
41 #define RSSI_LEVEL_1_2_G (-88)
42 
43 #define RSSI_LEVEL_4_5_G (-65)
44 #define RSSI_LEVEL_3_5_G (-72)
45 #define RSSI_LEVEL_2_5_G (-79)
46 #define RSSI_LEVEL_1_5_G (-85)
47 
48 #define W800_MAX_STA_NUM 8
49 
50 static int g_wifiApStatus = WIFI_HOTSPOT_NOT_ACTIVE;
51 static HotspotConfig g_wifiApConfig = {0};
52 
SetHotspotConfig(const HotspotConfig * config)53 WifiErrorCode SetHotspotConfig(const HotspotConfig* config)
54 {
55     if (config == NULL) {
56         return ERROR_WIFI_INVALID_ARGS;
57     }
58 
59     if (LockWifiGlobalLock() != WIFI_SUCCESS) {
60         return ERROR_WIFI_UNKNOWN;
61     }
62     errno_t err = memcpy_s(&g_wifiApConfig, sizeof(g_wifiApConfig), config, sizeof(*config));
63     if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
64         return ERROR_WIFI_UNKNOWN;
65     }
66     if (err != EOK) {
67         printf("[wifi_service]:SetHotspotConfig memcpy fail, err = %d\n", err);
68         return ERROR_WIFI_UNKNOWN;
69     }
70     return WIFI_SUCCESS;
71 }
72 
GetHotspotConfig(HotspotConfig * result)73 WifiErrorCode GetHotspotConfig(HotspotConfig* result)
74 {
75     if (result == NULL) {
76         return ERROR_WIFI_INVALID_ARGS;
77     }
78 
79     if (LockWifiGlobalLock() != WIFI_SUCCESS) {
80         return ERROR_WIFI_UNKNOWN;
81     }
82     errno_t err = memcpy_s(result, sizeof(*result), &g_wifiApConfig, sizeof(g_wifiApConfig));
83     if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
84         return ERROR_WIFI_UNKNOWN;
85     }
86     if (err != EOK) {
87         printf("[wifi_service]:SetHotspotConfig memcpy fail, err = %d\n", err);
88         return ERROR_WIFI_UNKNOWN;
89     }
90     return WIFI_SUCCESS;
91 }
92 
EnableHotspot()93 WifiErrorCode EnableHotspot()
94 {
95     if (LockWifiGlobalLock() != WIFI_SUCCESS) {
96         return ERROR_WIFI_UNKNOWN;
97     }
98     if (g_wifiApStatus == WIFI_HOTSPOT_ACTIVE) {
99         if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
100             return ERROR_WIFI_UNKNOWN;
101         }
102         return ERROR_WIFI_BUSY;
103     }
104     tls_wifi_init();
105     struct tls_softap_info_t apinfo = {0};
106     strcpy_s(apinfo.ssid, sizeof(apinfo.ssid), g_wifiApConfig.ssid);
107     apinfo.encrypt = HoSec2WmSec(g_wifiApConfig.securityType);
108     apinfo.channel = g_wifiApConfig.channelNum;
109     strcpy_s(apinfo.keyinfo.key, sizeof(apinfo.keyinfo.key), g_wifiApConfig.preSharedKey);
110     apinfo.keyinfo.key_len = strlen(g_wifiApConfig.preSharedKey);
111     apinfo.keyinfo.format = 1;  // 0-hex, 1-ascii
112     if (g_wifiApConfig.securityType == WIFI_SEC_TYPE_WEP) {
113         apinfo.keyinfo.index = 1; // 1-4 (only wep)
114     }
115     struct tls_ip_info_t ipinfo = {0};
116     ipinfo.ip_addr[0] = 192; // 192:byte alignment
117     ipinfo.ip_addr[1] = 168; // 168:byte alignment
118     ipinfo.ip_addr[2] = 1; // 2:array element
119     ipinfo.ip_addr[3] = 1; // 3:array element
120     ipinfo.netmask[0] = 255; // 255:byte alignment
121     ipinfo.netmask[1] = 255; // 255:byte alignment
122     ipinfo.netmask[2] = 255; // 255:byte alignment, 2:array element
123     ipinfo.netmask[3] = 0; // 3:array element
124     int retval = tls_wifi_softap_create(&apinfo, &ipinfo);
125     if (retval != WM_SUCCESS) {
126         printf("[wifi_service]:EnableHotspot tls_wifi_softap_create fail, err = %d\n", retval);
127         if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
128             return ERROR_WIFI_UNKNOWN;
129         }
130     }
131 
132     g_wifiApStatus = WIFI_HOTSPOT_ACTIVE;
133     if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
134         return ERROR_WIFI_UNKNOWN;
135     }
136     return WIFI_SUCCESS;
137 }
138 
DisableHotspot()139 WifiErrorCode DisableHotspot()
140 {
141     if (LockWifiGlobalLock() != WIFI_SUCCESS) {
142         return ERROR_WIFI_UNKNOWN;
143     }
144     if (g_wifiApStatus == WIFI_HOTSPOT_NOT_ACTIVE) {
145         if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
146             return ERROR_WIFI_UNKNOWN;
147         }
148         return ERROR_WIFI_NOT_STARTED;
149     }
150 
151     tls_wifi_softap_destroy();
152     g_wifiApStatus = WIFI_HOTSPOT_NOT_ACTIVE;
153 
154     if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
155         return ERROR_WIFI_UNKNOWN;
156     }
157     return WIFI_SUCCESS;
158 }
159 
IsHotspotActive(void)160 int IsHotspotActive(void)
161 {
162     if (LockWifiGlobalLock() != WIFI_SUCCESS) {
163         return ERROR_WIFI_UNKNOWN;
164     }
165     int ret = g_wifiApStatus;
166     if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
167         return ERROR_WIFI_UNKNOWN;
168     }
169 
170     return ret;
171 }
172 
GetStationList(StationInfo * result,unsigned int * size)173 WifiErrorCode GetStationList(StationInfo* result, unsigned int* size)
174 {
175     if (result == NULL || size == NULL || *size == 0) {
176         return ERROR_WIFI_INVALID_ARGS;
177     }
178 
179     struct tls_sta_info_t staList[W800_MAX_STA_NUM] = {0};
180     unsigned int staNum = 0;
181 
182     tls_wifi_get_authed_sta_info(&staNum, staList, sizeof(staList));
183 
184     if (*size < staNum) {
185         return ERROR_WIFI_INVALID_ARGS;
186     }
187 
188     for (unsigned int i = 0; i < staNum; i++) {
189         errno_t err = memcpy_s(result[i].macAddress, WIFI_MAC_LEN, staList[i].mac_addr, ETH_ALEN);
190         if (err != EOK) {
191             printf("[wifi_service]:GetStationList memcpy fail, err = %d\n", err);
192             return ERROR_WIFI_UNKNOWN;
193         }
194     }
195 
196     *size = staNum;
197     return WIFI_SUCCESS;
198 }
199 
GetSignalLevel(int rssi,int band)200 int GetSignalLevel(int rssi, int band)
201 {
202     if (band == HOTSPOT_BAND_TYPE_2G) {
203         if (rssi >= RSSI_LEVEL_4_2_G) {
204             return RSSI_LEVEL_4;
205         }
206         if (rssi >= RSSI_LEVEL_3_2_G) {
207             return RSSI_LEVEL_3;
208         }
209         if (rssi >= RSSI_LEVEL_2_2_G) {
210             return RSSI_LEVEL_2;
211         }
212         if (rssi >= RSSI_LEVEL_1_2_G) {
213             return RSSI_LEVEL_1;
214         }
215     }
216 
217     if (band == HOTSPOT_BAND_TYPE_5G) {
218         if (rssi >= RSSI_LEVEL_4_5_G) {
219             return RSSI_LEVEL_4;
220         }
221         if (rssi >= RSSI_LEVEL_3_5_G) {
222             return RSSI_LEVEL_3;
223         }
224         if (rssi >= RSSI_LEVEL_2_5_G) {
225             return RSSI_LEVEL_2;
226         }
227         if (rssi >= RSSI_LEVEL_1_5_G) {
228             return RSSI_LEVEL_1;
229         }
230     }
231 
232     return ERROR_WIFI_INVALID_ARGS;
233 }
234 
SetBand(int band)235 WifiErrorCode SetBand(int band)
236 {
237     if (band != HOTSPOT_BAND_TYPE_2G) {
238         return ERROR_WIFI_NOT_SUPPORTED;
239     }
240 
241     if (LockWifiGlobalLock() != WIFI_SUCCESS) {
242         return ERROR_WIFI_UNKNOWN;
243     }
244     g_wifiApConfig.band = band;
245     if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
246         return ERROR_WIFI_UNKNOWN;
247     }
248     return WIFI_SUCCESS;
249 }
250 
GetBand(int * result)251 WifiErrorCode GetBand(int* result)
252 {
253     if (result == NULL) {
254         return ERROR_WIFI_INVALID_ARGS;
255     }
256 
257     if (LockWifiGlobalLock() != WIFI_SUCCESS) {
258         return ERROR_WIFI_UNKNOWN;
259     }
260     if (g_wifiApConfig.band == 0) {
261         if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
262             return ERROR_WIFI_UNKNOWN;
263         }
264         return ERROR_WIFI_NOT_AVAILABLE;
265     }
266     if (UnlockWifiGlobalLock() != WIFI_SUCCESS) {
267         return ERROR_WIFI_UNKNOWN;
268     }
269     *result = HOTSPOT_BAND_TYPE_2G;
270     return WIFI_SUCCESS;
271 }
272 
DisassociateSta(unsigned char * mac,int macLen)273 WifiErrorCode DisassociateSta(unsigned char* mac, int macLen)
274 {
275     if (mac == NULL) {
276         printf("[wifi_service]: MAC is NULL\r\n");
277         return ERROR_WIFI_INVALID_ARGS;
278     }
279     int ret = tls_wifi_softap_del_station(mac);
280     if (ret != WIFI_SUCCESS) {
281         printf("[wifi_service]: remove station device failed.\r\n");
282         return ERROR_WIFI_UNKNOWN;
283     }
284     return WIFI_SUCCESS;
285 }
286 
AddTxPowerInfo(int power)287 WifiErrorCode AddTxPowerInfo(int power)
288 {
289     printf("Neptune not support.\r\n");
290     return ERROR_WIFI_UNKNOWN;
291 }