1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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_event.h"
17 #include "station_info.h"
18 #include "wifi_scan_info.h"
19 #include "wifi_error_code.h"
20 #include "wifi_linked_info.h"
21 #include "wifi_device_config.h"
22 #include "bwifi_interface.h"
23 #include "wifi_hotspot_config.h"
24 #include "bwifi_event.h"
25
26 #define RSSI_LEVEL_4_2_G (-65)
27 #define RSSI_LEVEL_3_2_G (-75)
28 #define RSSI_LEVEL_2_2_G (-82)
29 #define RSSI_LEVEL_1_2_G (-88)
30 #define RSSI_LEVEL_4_5_G (-65)
31 #define RSSI_LEVEL_3_5_G (-72)
32 #define RSSI_LEVEL_2_5_G (-79)
33 #define RSSI_LEVEL_1_5_G (-85)
34
35
36 static int g_HalHmosWifiApStatus = WIFI_HOTSPOT_NOT_ACTIVE;
37 static HotspotConfig g_HalHmosWifiApConfig = {0};
38 extern WifiErrorCode HalHmosWifiEventInit();
39 extern void HalHmosWifiLock();
40 extern void HalHmosWifiUnLock();
41
HalBesSecTypeConvert(WifiSecurityType security_type)42 static int HalBesSecTypeConvert(WifiSecurityType security_type)
43 {
44 switch (security_type) {
45 case WIFI_SEC_TYPE_WEP:
46 return BWIFI_SECURITY_WEP104;
47 case WIFI_SEC_TYPE_PSK:
48 return BWIFI_SECURITY_WPA_WPA2;
49 case WIFI_SEC_TYPE_SAE:
50 return BWIFI_SECURITY_WPA3_SAE_WPA2;
51 default:
52 return WIFI_SEC_TYPE_OPEN;
53 }
54 }
55
EnableHotspot(void)56 WifiErrorCode EnableHotspot(void)
57 {
58 WifiErrorCode ret = ERROR_WIFI_UNKNOWN;
59 BWIFI_SEC_TYPE_T sectype = HalBesSecTypeConvert(g_HalHmosWifiApConfig.securityType);
60 HalHmosWifiLock();
61 if (BWIFI_R_OK == bwifi_set_softap_config((char *)g_HalHmosWifiApConfig.ssid,
62 g_HalHmosWifiApConfig.channelNum, 0, 0,
63 sectype, (char *)g_HalHmosWifiApConfig.preSharedKey)) {
64 if (g_HalHmosWifiApStatus != WIFI_HOTSPOT_ACTIVE
65 &&HalHmosWifiEventInit() == WIFI_SUCCESS) {
66 if (bwifi_softap_start() == BWIFI_R_OK) {
67 g_HalHmosWifiApStatus = WIFI_HOTSPOT_ACTIVE;
68 ret = WIFI_SUCCESS;
69 }
70 }
71 }
72 HalHmosWifiUnLock();
73 return ret;
74 }
75
DisableHotspot(void)76 WifiErrorCode DisableHotspot(void)
77 {
78 HalHmosWifiLock();
79 if(g_HalHmosWifiApStatus == WIFI_HOTSPOT_NOT_ACTIVE)
80 return ERROR_WIFI_NOT_STARTED;
81 bwifi_softap_stop();
82 g_HalHmosWifiApStatus = WIFI_HOTSPOT_NOT_ACTIVE;
83 HalHmosWifiUnLock();
84 return WIFI_SUCCESS;
85 }
86
SetHotspotConfig(const HotspotConfig * config)87 WifiErrorCode SetHotspotConfig(const HotspotConfig *config)
88 {
89 if (config == NULL)
90 return ERROR_WIFI_INVALID_ARGS;
91 HalHmosWifiLock();
92 memcpy(&g_HalHmosWifiApConfig, config, sizeof(HotspotConfig));
93 HalHmosWifiUnLock();
94 return WIFI_SUCCESS;
95 }
96
GetHotspotConfig(HotspotConfig * result)97 WifiErrorCode GetHotspotConfig(HotspotConfig *result)
98 {
99 if (result == NULL)
100 return ERROR_WIFI_INVALID_ARGS;
101 HalHmosWifiLock();
102 memcpy(result, &g_HalHmosWifiApConfig, sizeof(HotspotConfig));
103 HalHmosWifiUnLock();
104 return WIFI_SUCCESS;
105 }
106
IsHotspotActive(void)107 int IsHotspotActive(void)
108 {
109 return g_HalHmosWifiApStatus;
110 }
111
GetStationList(StationInfo * result,unsigned int * size)112 WifiErrorCode GetStationList(StationInfo *result, unsigned int *size)
113 {
114
115 int i = 0;
116 int count = *size;
117 WifiErrorCode ret = ERROR_WIFI_INVALID_ARGS;
118 struct bwifi_mac_addr *mac_list = NULL;
119
120 if (result == NULL)
121 return ret;
122
123 mac_list = (struct bwifi_mac_addr *)malloc(count * sizeof(struct bwifi_mac_addr));
124 if (mac_list != NULL) {
125 HalHmosWifiLock();
126 if (bwifi_softap_get_station_list(mac_list, &count) == BWIFI_R_OK) {
127 for (i = 0; i < count; i++) {
128 memcpy((uint8_t *)result->macAddress, mac_list++, WIFI_MAC_LEN);
129 result->disconnectedReason = 0;
130 result++;
131 }
132 *size = count;
133 } else
134 *size = 0;
135
136 HalHmosWifiUnLock();
137 free(mac_list);
138 }
139
140 return WIFI_SUCCESS;
141
142 }
143
144
GetSignalLevel(int rssi,int band)145 int GetSignalLevel(int rssi, int band)
146 {
147 if (band == HOTSPOT_BAND_TYPE_2G) {
148 if (rssi >= RSSI_LEVEL_4_2_G)
149 return RSSI_LEVEL_4;
150 if (rssi >= RSSI_LEVEL_3_2_G)
151 return RSSI_LEVEL_3;
152 if (rssi >= RSSI_LEVEL_2_2_G)
153 return RSSI_LEVEL_2;
154 if (rssi >= RSSI_LEVEL_1_2_G)
155 return RSSI_LEVEL_1;
156 }
157
158 if (band == HOTSPOT_BAND_TYPE_5G) {
159 if (rssi >= RSSI_LEVEL_4_5_G)
160 return RSSI_LEVEL_4;
161 if (rssi >= RSSI_LEVEL_3_5_G)
162 return RSSI_LEVEL_3;
163 if (rssi >= RSSI_LEVEL_2_5_G)
164 return RSSI_LEVEL_2;
165 if (rssi >= RSSI_LEVEL_1_5_G)
166 return RSSI_LEVEL_1;
167 }
168 return ERROR_WIFI_INVALID_ARGS;
169 }
170
SetBand(int band)171 WifiErrorCode SetBand(int band)
172 {
173 if(band != 1 && band != 2)
174 return ERROR_WIFI_NOT_SUPPORTED;
175 g_HalHmosWifiApConfig.band = band;
176 return WIFI_SUCCESS;
177 }
178
GetBand(int * result)179 WifiErrorCode GetBand(int *result)
180 {
181 if(g_HalHmosWifiApConfig.band == 0)
182 if(*result != 1 && *result != 2)
183 return ERROR_WIFI_NOT_SUPPORTED;
184 *result = g_HalHmosWifiApConfig.band;
185 return WIFI_SUCCESS;
186 }
187
188
189