• 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     hotspotConfig.SetMaxConn(config->maxConn);
110     int value = config->leaseTime;
111     if (value < static_cast<int>(DHCP_LEASE_TIME_MIN)) {
112         value = static_cast<int>(DHCP_LEASE_TIME);
113     }
114     hotspotConfig.SetLeaseTime(value);
115     hotspotConfig.SetBandWidth(config->apBandWidth);
116     return WIFI_SUCCESS;
117 }
118 
GetHotspotConfigFromCpp(const OHOS::Wifi::HotspotConfig & hotspotConfig,HotspotConfig * result)119 static WifiErrorCode GetHotspotConfigFromCpp(const OHOS::Wifi::HotspotConfig& hotspotConfig, HotspotConfig *result)
120 {
121     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
122     if (memcpy_s(result->ssid, WIFI_MAX_SSID_LEN,
123         hotspotConfig.GetSsid().c_str(), hotspotConfig.GetSsid().size() + 1) != EOK) {
124         return ERROR_WIFI_UNKNOWN;
125     }
126     result->securityType = GetSecurityTypeFromKeyMgmt(hotspotConfig.GetSecurityType());
127     result->band = static_cast<int>(hotspotConfig.GetBand());
128     result->channelNum = hotspotConfig.GetChannel();
129     if (memcpy_s(result->preSharedKey, WIFI_MAX_KEY_LEN,
130         hotspotConfig.GetPreSharedKey().c_str(), hotspotConfig.GetPreSharedKey().size() + 1) != EOK) {
131         return ERROR_WIFI_UNKNOWN;
132     }
133     if (memcpy_s(result->ipAddress, WIFI_MAX_IPV4_LEN,
134         hotspotConfig.GetIpAddress().c_str(), hotspotConfig.GetIpAddress().size() + 1) != EOK) {
135         return ERROR_WIFI_UNKNOWN;
136     }
137     return WIFI_SUCCESS;
138 }
139 
SetHotspotConfig(const HotspotConfig * config)140 NO_SANITIZE("cfi") WifiErrorCode SetHotspotConfig(const HotspotConfig *config)
141 {
142     CHECK_PTR_RETURN(config, ERROR_WIFI_INVALID_ARGS);
143     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
144     OHOS::Wifi::HotspotConfig hotspotConfig;
145     WifiErrorCode ret = GetHotspotConfigFromC(config, hotspotConfig);
146     if (ret != WIFI_SUCCESS) {
147         return ret;
148     }
149     return GetCErrorCode(hotspotPtr->SetHotspotConfig(hotspotConfig));
150 }
151 
GetHotspotConfig(HotspotConfig * result)152 NO_SANITIZE("cfi") WifiErrorCode GetHotspotConfig(HotspotConfig *result)
153 {
154     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
155     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
156     OHOS::Wifi::HotspotConfig hotspotConfig;
157     OHOS::Wifi::ErrCode ret = hotspotPtr->GetHotspotConfig(hotspotConfig);
158     if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
159         WifiErrorCode retValue = GetHotspotConfigFromCpp(hotspotConfig, result);
160         if (retValue != WIFI_SUCCESS) {
161             WIFI_LOGE("Get hotspot config from cpp error!");
162             return retValue;
163         }
164     }
165     return GetCErrorCode(ret);
166 }
167 
GetStaListFromCpp(const std::vector<OHOS::Wifi::StationInfo> & vecStaList,StationInfo * result)168 static WifiErrorCode GetStaListFromCpp(const std::vector<OHOS::Wifi::StationInfo>& vecStaList, StationInfo *result)
169 {
170     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
171     for (auto& each : vecStaList) {
172         if (result->name != nullptr) {
173             if (memcpy_s(result->name, DEVICE_NAME_LEN, each.deviceName.c_str(), each.deviceName.size() + 1) != EOK) {
174                 return ERROR_WIFI_UNKNOWN;
175             }
176         } else {
177             WIFI_LOGE("WARN: device name is not pre-allocate memory!");
178         }
179 
180         if (OHOS::Wifi::MacStrToArray(each.bssid, result->macAddress) != EOK) {
181             WIFI_LOGE("Get sta list convert bssid error!");
182             return ERROR_WIFI_UNKNOWN;
183         }
184         result->ipAddress = OHOS::Wifi::Ip2Number(each.ipAddr);
185     }
186     return WIFI_SUCCESS;
187 }
188 
GetStationList(StationInfo * result,unsigned int * size)189 NO_SANITIZE("cfi") WifiErrorCode GetStationList(StationInfo *result, unsigned int *size)
190 {
191     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
192     CHECK_PTR_RETURN(result, ERROR_WIFI_INVALID_ARGS);
193     CHECK_PTR_RETURN(size, ERROR_WIFI_INVALID_ARGS);
194     std::vector<OHOS::Wifi::StationInfo> vecStaList;
195     OHOS::Wifi::ErrCode ret = hotspotPtr->GetStationList(vecStaList);
196     *size = (int)vecStaList.size();
197     if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
198         WifiErrorCode retValue = GetStaListFromCpp(vecStaList, result);
199         if (retValue != WIFI_SUCCESS) {
200             WIFI_LOGE("Get station list from cpp error!");
201             return retValue;
202         }
203     }
204     return GetCErrorCode(ret);
205 }
206 
DisassociateSta(unsigned char * mac,int macLen)207 WifiErrorCode DisassociateSta(unsigned char *mac, int macLen)
208 {
209     CHECK_PTR_RETURN(mac, ERROR_WIFI_INVALID_ARGS);
210     return GetCErrorCode(OHOS::Wifi::WIFI_OPT_NOT_SUPPORTED);
211 }
212 
AddTxPowerInfo(int power)213 WifiErrorCode AddTxPowerInfo(int power)
214 {
215     return GetCErrorCode(OHOS::Wifi::WIFI_OPT_NOT_SUPPORTED);
216 }
217 
GetApIfaceName(char * ifaceName,int nameLen)218 WifiErrorCode GetApIfaceName(char *ifaceName, int nameLen)
219 {
220     CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
221     CHECK_PTR_RETURN(ifaceName, ERROR_WIFI_INVALID_ARGS);
222     if (nameLen < IFACENAME_MIN_LEN) {
223         return ERROR_WIFI_INVALID_ARGS;
224     }
225     std::string iface;
226     OHOS::Wifi::ErrCode ret = hotspotPtr->GetApIfaceName(iface);
227     if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
228         if (iface.size() > static_cast<unsigned long>(nameLen)) {
229             return ERROR_WIFI_INVALID_ARGS;
230         }
231         if (memcpy_s(ifaceName, nameLen, iface.c_str(), iface.size()) != EOK) {
232             WIFI_LOGE("memcpy iface name failed");
233             return ERROR_WIFI_UNKNOWN;
234         }
235     }
236     return GetCErrorCode(ret);
237 }