1 /*
2 * Copyright (C) 2021 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 "native_c/wifi_hotspot.h"
17 #include "native_c/wifi_hotspot_config.h"
18 #include "native_c/wifi_device_config.h"
19 #include "native_cpp/wifi_standard/include/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_WIFILOG_LABEL("WifiCHotspot");
26
27 std::unique_ptr<OHOS::Wifi::WifiHotspot> hotspotPtr = OHOS::Wifi::WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID);
28
EnableHotspot()29 WifiErrorCode EnableHotspot()
30 {
31 CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
32 return GetCErrorCode(hotspotPtr->EnableHotspot());
33 }
34
DisableHotspot()35 WifiErrorCode DisableHotspot()
36 {
37 CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
38 return GetCErrorCode(hotspotPtr->DisableHotspot());
39 }
40
IsHotspotActive(void)41 int IsHotspotActive(void)
42 {
43 if (hotspotPtr == nullptr) {
44 return false;
45 }
46 return hotspotPtr->IsHotspotActive();
47 }
48
49 /* Others type is not support for AP */
50 static std::map<WifiSecurityType, OHOS::Wifi::KeyMgmt> g_mapSecTypeToKeyMgmt = {
51 {WifiSecurityType::WIFI_SEC_TYPE_OPEN, OHOS::Wifi::KeyMgmt::NONE},
52 {WifiSecurityType::WIFI_SEC_TYPE_PSK, OHOS::Wifi::KeyMgmt::WPA_PSK},
53 };
54
GetKeyMgmtFromSecurityType(int secType)55 static OHOS::Wifi::KeyMgmt GetKeyMgmtFromSecurityType(int secType)
56 {
57 WifiSecurityType key = WifiSecurityType(secType);
58 std::map<WifiSecurityType, OHOS::Wifi::KeyMgmt>::iterator iter = g_mapSecTypeToKeyMgmt.find(key);
59 return iter == g_mapSecTypeToKeyMgmt.end() ? OHOS::Wifi::KeyMgmt::NONE : iter->second;
60 }
61
GetSecurityTypeFromKeyMgmt(OHOS::Wifi::KeyMgmt keyMgmt)62 static int GetSecurityTypeFromKeyMgmt(OHOS::Wifi::KeyMgmt keyMgmt)
63 {
64 for (auto& each : g_mapSecTypeToKeyMgmt) {
65 if (each.second == keyMgmt) {
66 return static_cast<int>(each.first);
67 }
68 }
69 return static_cast<int>(WifiSecurityType::WIFI_SEC_TYPE_OPEN);
70 }
71
IsSecurityTypeSupported(int secType)72 static bool IsSecurityTypeSupported(int secType)
73 {
74 WifiSecurityType key = WifiSecurityType(secType);
75 std::map<WifiSecurityType, OHOS::Wifi::KeyMgmt>::iterator iter = g_mapSecTypeToKeyMgmt.find(key);
76 return iter != g_mapSecTypeToKeyMgmt.end();
77 }
78
GetHotspotConfigFromC(const HotspotConfig * config,OHOS::Wifi::HotspotConfig & hotspotConfig)79 static WifiErrorCode GetHotspotConfigFromC(const HotspotConfig *config, OHOS::Wifi::HotspotConfig& hotspotConfig)
80 {
81 hotspotConfig.SetSsid(config->ssid);
82 if (!IsSecurityTypeSupported(config->securityType)) {
83 WIFI_LOGE("Ap security is not supported!");
84 return ERROR_WIFI_NOT_SUPPORTED;
85 }
86 hotspotConfig.SetSecurityType(GetKeyMgmtFromSecurityType(config->securityType));
87 hotspotConfig.SetBand(OHOS::Wifi::BandType(config->band));
88 hotspotConfig.SetChannel(config->channelNum);
89 hotspotConfig.SetPreSharedKey(config->preSharedKey);
90 return WIFI_SUCCESS;
91 }
92
GetHotspotConfigFromCpp(const OHOS::Wifi::HotspotConfig & hotspotConfig,HotspotConfig * result)93 static WifiErrorCode GetHotspotConfigFromCpp(const OHOS::Wifi::HotspotConfig& hotspotConfig, HotspotConfig *result)
94 {
95 if (memcpy_s(result->ssid, WIFI_MAX_SSID_LEN,
96 hotspotConfig.GetSsid().c_str(), hotspotConfig.GetSsid().size() + 1) != EOK) {
97 return ERROR_WIFI_UNKNOWN;
98 }
99 result->securityType = GetSecurityTypeFromKeyMgmt(hotspotConfig.GetSecurityType());
100 result->band = static_cast<int>(hotspotConfig.GetBand());
101 result->channelNum = hotspotConfig.GetChannel();
102 if (memcpy_s(result->preSharedKey, WIFI_MAX_KEY_LEN,
103 hotspotConfig.GetPreSharedKey().c_str(), hotspotConfig.GetPreSharedKey().size() + 1) != EOK) {
104 return ERROR_WIFI_UNKNOWN;
105 }
106 return WIFI_SUCCESS;
107 }
108
SetHotspotConfig(const HotspotConfig * config)109 WifiErrorCode SetHotspotConfig(const HotspotConfig *config)
110 {
111 CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
112 OHOS::Wifi::HotspotConfig hotspotConfig;
113 WifiErrorCode ret = GetHotspotConfigFromC(config, hotspotConfig);
114 if (ret != WIFI_SUCCESS) {
115 return ret;
116 }
117 return GetCErrorCode(hotspotPtr->SetHotspotConfig(hotspotConfig));
118 }
119
GetHotspotConfig(HotspotConfig * result)120 WifiErrorCode GetHotspotConfig(HotspotConfig *result)
121 {
122 CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
123 OHOS::Wifi::HotspotConfig hotspotConfig;
124 OHOS::Wifi::ErrCode ret = hotspotPtr->GetHotspotConfig(hotspotConfig);
125 if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
126 WifiErrorCode retValue = GetHotspotConfigFromCpp(hotspotConfig, result);
127 if (retValue != WIFI_SUCCESS) {
128 WIFI_LOGE("Get hotspot config from cpp error!");
129 return retValue;
130 }
131 }
132 return GetCErrorCode(ret);
133 }
134
GetStaListFromCpp(const std::vector<OHOS::Wifi::StationInfo> & vecStaList,StationInfo * result)135 static WifiErrorCode GetStaListFromCpp(const std::vector<OHOS::Wifi::StationInfo>& vecStaList, StationInfo *result)
136 {
137 for (auto& each : vecStaList) {
138 if (result->name != nullptr) {
139 if (memcpy_s(result->name, DEVICE_NAME_LEN, each.deviceName.c_str(), each.deviceName.size() + 1) != EOK) {
140 return ERROR_WIFI_UNKNOWN;
141 }
142 } else {
143 WIFI_LOGE("WARN: device name is not pre-allocate memory!");
144 }
145
146 if (OHOS::Wifi::MacStrToArray(each.bssid, result->macAddress) != EOK) {
147 WIFI_LOGE("Get sta list convert bssid error!");
148 return ERROR_WIFI_UNKNOWN;
149 }
150 result->ipAddress = OHOS::Wifi::IpTools::ConvertIpv4Address(each.ipAddr);
151 }
152 return WIFI_SUCCESS;
153 }
154
GetStationList(StationInfo * result,unsigned int * size)155 WifiErrorCode GetStationList(StationInfo *result, unsigned int *size)
156 {
157 if (result == nullptr) {
158 WIFI_LOGE("Station info list receive addr is null!");
159 return ERROR_WIFI_UNKNOWN;
160 }
161 CHECK_PTR_RETURN(hotspotPtr, ERROR_WIFI_NOT_AVAILABLE);
162
163 std::vector<OHOS::Wifi::StationInfo> vecStaList;
164 OHOS::Wifi::ErrCode ret = hotspotPtr->GetStationList(vecStaList);
165 *size = (int)vecStaList.size();
166 if (ret == OHOS::Wifi::WIFI_OPT_SUCCESS) {
167 WifiErrorCode retValue = GetStaListFromCpp(vecStaList, result);
168 if (retValue != WIFI_SUCCESS) {
169 WIFI_LOGE("Get station list from cpp error!");
170 return retValue;
171 }
172 }
173 return GetCErrorCode(ret);
174 }
175
DisassociateSta(unsigned char * mac,int macLen)176 WifiErrorCode DisassociateSta(unsigned char *mac, int macLen)
177 {
178 return GetCErrorCode(OHOS::Wifi::WIFI_OPT_NOT_SUPPORTED);
179 }
180
AddTxPowerInfo(int power)181 WifiErrorCode AddTxPowerInfo(int power)
182 {
183 return GetCErrorCode(OHOS::Wifi::WIFI_OPT_NOT_SUPPORTED);
184 }
185