• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
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 "kits/c/wifi_hotspot.h"
17 #include "kits/c/wifi_hotspot_config.h"
18 #include "kits/c/wifi_device_config.h"
19 #include "inner_api/wifi_hotspot.h"
20 #include "wifi_logger.h"
21 #include "wifi_c_utils.h"
22 #include "ip_tools.h"
23 #include "wifi_common_util.h"
24 
25 #define IFACENAME_MIN_LEN 6
26 
27 DEFINE_WIFILOG_LABEL("WifiCHotspot");
28 
29 std::shared_ptr<OHOS::Wifi::WifiHotspot> hotspotPtr = OHOS::Wifi::WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID);
30 
EnableHotspot()31 NO_SANITIZE("cfi") WifiErrorCode EnableHotspot()
32 {
33     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
34     return GetCErrorCode(hotspotPtr->EnableHotspot());
35 }
36 
DisableHotspot()37 NO_SANITIZE("cfi") WifiErrorCode DisableHotspot()
38 {
39     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
40     return GetCErrorCode(hotspotPtr->DisableHotspot());
41 }
42 
IsHotspotActive(void)43 NO_SANITIZE("cfi") int IsHotspotActive(void)
44 {
45     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
46     bool isActive = false;
47     OHOS::Wifi::ErrCode ret = hotspotPtr->IsHotspotActive(isActive);
48     if (ret != OHOS::Wifi::WIFI_OPT_SUCCESS) {
49         WIFI_LOGE("IsHotspotActive return error: %{public}d!", ret);
50     }
51     return (ret == OHOS::Wifi::WIFI_OPT_SUCCESS && isActive) ? 1 : 0;
52 }
53 
IsHotspotDualBandSupported(bool & isSupported)54 NO_SANITIZE("cfi") WifiErrorCode IsHotspotDualBandSupported(bool &isSupported)
55 {
56     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
57     return GetCErrorCode(hotspotPtr->IsHotspotDualBandSupported(isSupported));
58 }
59 
60 /* Others type is not support for AP */
61 static std::map<WifiSecurityType, OHOS::Wifi::KeyMgmt> g_mapSecTypeToKeyMgmt = {
62     {WifiSecurityType::WIFI_SEC_TYPE_OPEN, OHOS::Wifi::KeyMgmt::NONE},
63     {WifiSecurityType::WIFI_SEC_TYPE_PSK, OHOS::Wifi::KeyMgmt::WPA2_PSK},
64 };
65 
GetKeyMgmtFromSecurityType(int secType)66 static OHOS::Wifi::KeyMgmt GetKeyMgmtFromSecurityType(int secType)
67 {
68     WifiSecurityType key = WifiSecurityType(secType);
69     std::map<WifiSecurityType, OHOS::Wifi::KeyMgmt>::iterator iter = g_mapSecTypeToKeyMgmt.find(key);
70     return iter == g_mapSecTypeToKeyMgmt.end() ? OHOS::Wifi::KeyMgmt::NONE : iter->second;
71 }
72 
GetSecurityTypeFromKeyMgmt(OHOS::Wifi::KeyMgmt keyMgmt)73 static int GetSecurityTypeFromKeyMgmt(OHOS::Wifi::KeyMgmt keyMgmt)
74 {
75     for (auto& each : g_mapSecTypeToKeyMgmt) {
76         if (each.second == keyMgmt) {
77             return static_cast<int>(each.first);
78         }
79     }
80     return static_cast<int>(WifiSecurityType::WIFI_SEC_TYPE_OPEN);
81 }
82 
IsSecurityTypeSupported(int secType)83 static bool IsSecurityTypeSupported(int secType)
84 {
85     WifiSecurityType key = WifiSecurityType(secType);
86     std::map<WifiSecurityType, OHOS::Wifi::KeyMgmt>::iterator iter = g_mapSecTypeToKeyMgmt.find(key);
87     return iter != g_mapSecTypeToKeyMgmt.end();
88 }
89 
GetHotspotConfigFromC(const HotspotConfig * config,OHOS::Wifi::HotspotConfig & hotspotConfig)90 static WifiErrorCode GetHotspotConfigFromC(const HotspotConfig *config, OHOS::Wifi::HotspotConfig& hotspotConfig)
91 {
92     CHECK_PTR_RETURN(config, ERROR_WIFI_INVALID_ARGS);
93     hotspotConfig.SetSsid(config->ssid);
94     if (!IsSecurityTypeSupported(config->securityType)) {
95         WIFI_LOGE("Ap security is not supported!");
96         return ERROR_WIFI_NOT_SUPPORTED;
97     }
98     hotspotConfig.SetSecurityType(GetKeyMgmtFromSecurityType(config->securityType));
99     hotspotConfig.SetBand(OHOS::Wifi::BandType(config->band));
100     hotspotConfig.SetChannel(config->channelNum);
101     if (strnlen(config->preSharedKey, WIFI_MAX_KEY_LEN) == WIFI_MAX_KEY_LEN) {
102         return ERROR_WIFI_INVALID_ARGS;
103     }
104     hotspotConfig.SetPreSharedKey(config->preSharedKey);
105     if (strnlen(config->ipAddress, WIFI_MAX_IPV4_LEN) == WIFI_MAX_IPV4_LEN) {
106         return ERROR_WIFI_INVALID_ARGS;
107     }
108     hotspotConfig.SetIpAddress(config->ipAddress);
109     return WIFI_SUCCESS;
110 }
111 
GetHotspotConfigFromCpp(const OHOS::Wifi::HotspotConfig & hotspotConfig,HotspotConfig * result)112 static WifiErrorCode GetHotspotConfigFromCpp(const OHOS::Wifi::HotspotConfig& hotspotConfig, HotspotConfig *result)
113 {
114     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
115     if (memcpy_s(result->ssid, WIFI_MAX_SSID_LEN,
116         hotspotConfig.GetSsid().c_str(), hotspotConfig.GetSsid().size() + 1) != EOK) {
117         return ERROR_WIFI_UNKNOWN;
118     }
119     result->securityType = GetSecurityTypeFromKeyMgmt(hotspotConfig.GetSecurityType());
120     result->band = static_cast<int>(hotspotConfig.GetBand());
121     result->channelNum = hotspotConfig.GetChannel();
122     if (memcpy_s(result->preSharedKey, WIFI_MAX_KEY_LEN,
123         hotspotConfig.GetPreSharedKey().c_str(), hotspotConfig.GetPreSharedKey().size() + 1) != EOK) {
124         return ERROR_WIFI_UNKNOWN;
125     }
126     if (memcpy_s(result->ipAddress, WIFI_MAX_IPV4_LEN,
127         hotspotConfig.GetIpAddress().c_str(), hotspotConfig.GetIpAddress().size() + 1) != EOK) {
128         return ERROR_WIFI_UNKNOWN;
129     }
130     return WIFI_SUCCESS;
131 }
132 
SetHotspotConfig(const HotspotConfig * config)133 NO_SANITIZE("cfi") WifiErrorCode SetHotspotConfig(const HotspotConfig *config)
134 {
135     CHECK_PTR_RETURN(config, ERROR_WIFI_INVALID_ARGS);
136     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
137     OHOS::Wifi::HotspotConfig hotspotConfig;
138     WifiErrorCode ret = GetHotspotConfigFromC(config, hotspotConfig);
139     if (ret != WIFI_SUCCESS) {
140         return ret;
141     }
142     return GetCErrorCode(hotspotPtr->SetHotspotConfig(hotspotConfig));
143 }
144 
GetHotspotConfig(HotspotConfig * result)145 NO_SANITIZE("cfi") WifiErrorCode GetHotspotConfig(HotspotConfig *result)
146 {
147     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
148     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
149     OHOS::Wifi::HotspotConfig hotspotConfig;
150     OHOS::Wifi::ErrCode ret = hotspotPtr->GetHotspotConfig(hotspotConfig);
151     if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
152         WifiErrorCode retValue = GetHotspotConfigFromCpp(hotspotConfig, result);
153         if (retValue != WIFI_SUCCESS) {
154             WIFI_LOGE("Get hotspot config from cpp error!");
155             return retValue;
156         }
157     }
158     return GetCErrorCode(ret);
159 }
160 
GetStaListFromCpp(const std::vector<OHOS::Wifi::StationInfo> & vecStaList,StationInfo * result)161 static WifiErrorCode GetStaListFromCpp(const std::vector<OHOS::Wifi::StationInfo>& vecStaList, StationInfo *result)
162 {
163     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
164     for (auto& each : vecStaList) {
165         if (result->name != nullptr) {
166             if (memcpy_s(result->name, DEVICE_NAME_LEN, each.deviceName.c_str(), each.deviceName.size() + 1) != EOK) {
167                 return ERROR_WIFI_UNKNOWN;
168             }
169         } else {
170             WIFI_LOGE("WARN: device name is not pre-allocate memory!");
171         }
172 
173         if (OHOS::Wifi::MacStrToArray(each.bssid, result->macAddress) != EOK) {
174             WIFI_LOGE("Get sta list convert bssid error!");
175             return ERROR_WIFI_UNKNOWN;
176         }
177         result->ipAddress = OHOS::Wifi::Ip2Number(each.ipAddr);
178     }
179     return WIFI_SUCCESS;
180 }
181 
GetStationList(StationInfo * result,unsigned int * size)182 NO_SANITIZE("cfi") WifiErrorCode GetStationList(StationInfo *result, unsigned int *size)
183 {
184     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
185     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
186     CHECK_PTR_RETURN(size, ERROR_WIFI_INVALID_ARGS);
187     std::vector<OHOS::Wifi::StationInfo> vecStaList;
188     OHOS::Wifi::ErrCode ret = hotspotPtr->GetStationList(vecStaList);
189     *size = (int)vecStaList.size();
190     if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
191         WifiErrorCode retValue = GetStaListFromCpp(vecStaList, result);
192         if (retValue != WIFI_SUCCESS) {
193             WIFI_LOGE("Get station list from cpp error!");
194             return retValue;
195         }
196     }
197     return GetCErrorCode(ret);
198 }
199 
DisassociateSta(unsigned char * mac,int macLen)200 WifiErrorCode DisassociateSta(unsigned char *mac, int macLen)
201 {
202     CHECK_PTR_RETURN(mac, ERROR_WIFI_INVALID_ARGS);
203     return GetCErrorCode(OHOS::Wifi::WIFI_OPT_NOT_SUPPORTED);
204 }
205 
AddTxPowerInfo(int power)206 WifiErrorCode AddTxPowerInfo(int power)
207 {
208     return GetCErrorCode(OHOS::Wifi::WIFI_OPT_NOT_SUPPORTED);
209 }
210 
GetApIfaceName(char * ifaceName,int nameLen)211 WifiErrorCode GetApIfaceName(char *ifaceName, int nameLen)
212 {
213     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
214     CHECK_PTR_RETURN(ifaceName, ERROR_WIFI_INVALID_ARGS);
215     if (nameLen < IFACENAME_MIN_LEN) {
216         return ERROR_WIFI_INVALID_ARGS;
217     }
218     std::string iface;
219     OHOS::Wifi::ErrCode ret = hotspotPtr->GetApIfaceName(iface);
220     if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
221         if (iface.size() > static_cast<unsigned long>(nameLen)) {
222             return ERROR_WIFI_INVALID_ARGS;
223         }
224         if (memcpy_s(ifaceName, nameLen, iface.c_str(), iface.size()) != EOK) {
225             WIFI_LOGE("memcpy iface name failed");
226             return ERROR_WIFI_UNKNOWN;
227         }
228     }
229     return GetCErrorCode(ret);
230 }