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 "wifi_hotspot_stub.h"
17 #include "wifi_hotspot_callback_proxy.h"
18 #include "wifi_logger.h"
19 #include "string_ex.h"
20 #include "wifi_errcode.h"
21 #include "wifi_internal_event_dispatcher.h"
22 #include "wifi_hotspot_death_recipient.h"
23 #include "wifi_common_def.h"
24 #include "wifi_manager_service_ipc_interface_code.h"
25 #include <algorithm>
26 #include "wifi_device.h"
27 
28 DEFINE_WIFILOG_HOTSPOT_LABEL("WifiHotspotStub");
29 
30 namespace OHOS {
31 namespace Wifi {
32 const std::string DHCP_IP_V4_DEFAULT = "192.168.43.1";
33 std::shared_ptr<WifiDevice> wifiDeviceSharedPtr = OHOS::Wifi::WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
34 
WifiHotspotStub()35 WifiHotspotStub::WifiHotspotStub():mSingleCallback(false), m_id(0)
36 {
37     InitHandleMap();
38     deathRecipient_ = nullptr;
39 }
40 
WifiHotspotStub(int id)41 WifiHotspotStub::WifiHotspotStub(int id):mSingleCallback(false), m_id(id)
42 {
43     InitHandleMap();
44     deathRecipient_ = nullptr;
45 }
46 
~WifiHotspotStub()47 WifiHotspotStub::~WifiHotspotStub()
48 {
49     deathRecipient_ = nullptr;
50 }
51 
InitHandleMap()52 void WifiHotspotStub::InitHandleMap()
53 {
54     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_IS_HOTSPOT_ACTIVE)] =
55         &WifiHotspotStub::OnIsHotspotActive;
56     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GETAPSTATE_WIFI)] =
57         &WifiHotspotStub::OnGetApStateWifi;
58     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_HOTSPOT_CONFIG)] =
59         &WifiHotspotStub::OnGetHotspotConfig;
60     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SETAPCONFIG_WIFI)] =
61         &WifiHotspotStub::OnSetApConfigWifi;
62     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_STATION_LIST)] =
63         &WifiHotspotStub::OnGetStationList;
64     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ENABLE_WIFI_AP)] =
65         &WifiHotspotStub::OnEnableWifiAp;
66     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DISABLE_WIFI_AP)] =
67         &WifiHotspotStub::OnDisableWifiAp;
68     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ADD_BLOCK_LIST)] =
69         &WifiHotspotStub::OnAddBlockList;
70     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DEL_BLOCK_LIST)] =
71         &WifiHotspotStub::OnDelBlockList;
72     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_BLOCK_LISTS)] =
73         &WifiHotspotStub::OnGetBlockLists;
74     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DISCONNECT_STA)] =
75         &WifiHotspotStub::OnDisassociateSta;
76     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_VALID_BANDS)] =
77         &WifiHotspotStub::OnGetValidBands;
78     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_VALID_CHANNELS)] =
79         &WifiHotspotStub::OnGetValidChannels;
80     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_REGISTER_HOTSPOT_CALLBACK)] =
81         &WifiHotspotStub::OnRegisterCallBack;
82     handleFuncMap[static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES)] =
83         &WifiHotspotStub::OnGetSupportedFeatures;
84     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_POWER_MODEL)] =
85         &WifiHotspotStub::OnGetSupportedPowerModel;
86     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_POWER_MODEL)] =
87         &WifiHotspotStub::OnGetPowerModel;
88     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SET_POWER_MODEL)] =
89         &WifiHotspotStub::OnSetPowerModel;
90     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_IS_HOTSPOT_DUAL_BAND_SUPPORTED)] =
91         &WifiHotspotStub::OnIsHotspotDualBandSupported;
92     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SETTIMEOUT_AP)] =
93         &WifiHotspotStub::OnSetApIdleTimeout;
94     handleFuncMap[static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_IFACE_NAME)] =
95         &WifiHotspotStub::OnGetApIfaceName;
96     return;
97 }
98 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)99 int WifiHotspotStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
100 {
101     if (data.ReadInterfaceToken() != GetDescriptor()) {
102         WIFI_LOGE("Hotspot stub token verification error: %{public}d", code);
103         return WIFI_OPT_FAILED;
104     }
105 
106     int exception = data.ReadInt32();
107     if (exception) {
108         return WIFI_OPT_FAILED;
109     }
110 
111     HandleFuncMap::iterator iter = handleFuncMap.find(code);
112     if (iter == handleFuncMap.end()) {
113         WIFI_LOGW("not find function to deal, code %{public}u", code);
114         reply.WriteInt32(0);
115         reply.WriteInt32(WIFI_OPT_NOT_SUPPORTED);
116     } else {
117         (this->*(iter->second))(code, data, reply, option);
118     }
119     return 0;
120 }
121 
OnIsHotspotActive(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)122 void WifiHotspotStub::OnIsHotspotActive(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
123 {
124     WIFI_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
125     bool bActive = false;
126     ErrCode ret = IsHotspotActive(bActive);
127     reply.WriteInt32(0);
128     reply.WriteInt32(ret);
129     if (ret == WIFI_OPT_SUCCESS) {
130         reply.WriteInt32(bActive ? 1 : 0);
131     }
132     return;
133 }
134 
OnIsHotspotDualBandSupported(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)135 void WifiHotspotStub::OnIsHotspotDualBandSupported(uint32_t code, MessageParcel &data,
136     MessageParcel &reply, MessageOption &option)
137 {
138     WIFI_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
139     bool isSupported = false;
140     ErrCode ret = IsHotspotDualBandSupported(isSupported);
141     reply.WriteInt32(0);
142     reply.WriteInt32(ret);
143     if (ret == WIFI_OPT_SUCCESS) {
144         reply.WriteInt32(isSupported ? 1 : 0);
145     }
146     return;
147 }
148 
OnGetApStateWifi(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)149 void WifiHotspotStub::OnGetApStateWifi(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
150 {
151     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
152     int state = 0;
153     ErrCode ret = GetHotspotState(state);
154     reply.WriteInt32(0);
155     reply.WriteInt32(ret);
156     if (ret == WIFI_OPT_SUCCESS) {
157         reply.WriteInt32(state);
158     }
159     return;
160 }
161 
OnGetHotspotConfig(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)162 void WifiHotspotStub::OnGetHotspotConfig(
163     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
164 {
165     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
166     HotspotConfig hotspotConfig;
167 
168     ErrCode ret = GetHotspotConfig(hotspotConfig);
169     reply.WriteInt32(0);
170     reply.WriteInt32(ret);
171     if (ret == WIFI_OPT_SUCCESS) {
172         reply.WriteCString(hotspotConfig.GetSsid().c_str());
173         reply.WriteInt32(static_cast<int>(hotspotConfig.GetSecurityType()));
174         reply.WriteInt32(static_cast<int>(hotspotConfig.GetBand()));
175         reply.WriteInt32(hotspotConfig.GetChannel());
176         reply.WriteCString(hotspotConfig.GetPreSharedKey().c_str());
177         reply.WriteInt32(hotspotConfig.GetMaxConn());
178         if (hotspotConfig.GetIpAddress().empty()) {
179             reply.WriteString(DHCP_IP_V4_DEFAULT);
180         } else {
181             reply.WriteString(hotspotConfig.GetIpAddress());
182         }
183         reply.WriteInt32(hotspotConfig.GetLeaseTime());
184     }
185 
186     return;
187 }
188 
CheckHotspot160MParam(BandType band,int bandwidth,int channel)189 bool WifiHotspotStub::CheckHotspot160MParam(BandType band, int bandwidth, int channel)
190 {
191     if ((band != BandType::BAND_5GHZ && bandwidth == AP_BANDWIDTH_160) ||
192         (bandwidth != AP_BANDWIDTH_160 && bandwidth != AP_BANDWIDTH_DEFAULT) ||
193         (band == BandType::BAND_5GHZ && bandwidth == AP_BANDWIDTH_160 &&
194         ((channel < AP_CHANNEL_5G_160M_SET_BEGIN) || (channel > AP_CHANNEL_5G_160M_SET_END)))) {
195         return false;
196     } else {
197         return true;
198     }
199 }
200 
CheckHostspot160MCountryCode()201 bool WifiHotspotStub::CheckHostspot160MCountryCode()
202 {
203     std::string countryCode;
204     ErrCode ret = wifiDeviceSharedPtr->GetCountryCode(countryCode);
205     if (ret != WIFI_OPT_SUCCESS) {
206         WIFI_LOGE("CheckHostspot160MCountryCode GetcountryCode fail");
207         return false;
208     }
209     transform(countryCode.begin(), countryCode.end(), countryCode.begin(), ::toupper);
210     if (countryCode == "CN" || countryCode == "TW" || countryCode == "SG" || countryCode == "KR") {
211         WIFI_LOGD("CheckHostspot160MCountryCode countryCode %{public}s", countryCode.c_str());
212         return true;
213     } else {
214         WIFI_LOGE("CheckHostspot160MCountryCode Error countryCode %{public}s", countryCode.c_str());
215         return false;
216     }
217 }
218 
OnSetApConfigWifi(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)219 void WifiHotspotStub::OnSetApConfigWifi(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
220 {
221     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
222     ErrCode ret = WIFI_OPT_FAILED;
223     HotspotConfig config;
224     const char *ssidRead = data.ReadCString();
225     config.SetSecurityType(static_cast<KeyMgmt>(data.ReadInt32()));
226     config.SetBand(static_cast<BandType>(data.ReadInt32()));
227     int dataRead = data.ReadInt32();
228     int channel = dataRead & 0x000000FF;
229     int bandwidth = (dataRead & 0x00FF0000) >> 16;
230 
231     BandType band = config.GetBand();
232     config.SetBandWidth(bandwidth);
233     config.SetChannel(channel);
234     WIFI_LOGI("run %{public}s channel %{public}d bandwidth %{public}d band %{public}d",
235         __func__, config.GetChannel(), config.GetBandWidth(), config.GetBand());
236     const char *preSharedKeyRead = data.ReadCString();
237     config.SetMaxConn(data.ReadInt32());
238     config.SetIpAddress(data.ReadString());
239     config.SetLeaseTime(data.ReadInt32());
240     if (ssidRead == nullptr || preSharedKeyRead == nullptr || !CheckHotspot160MParam(band, bandwidth, channel)) {
241         ret = WIFI_OPT_INVALID_PARAM;
242     } else if ((!CheckHostspot160MCountryCode()) && bandwidth == AP_BANDWIDTH_160) {
243         ret = WIFI_OPT_INVALID_PARAM;
244     } else {
245         config.SetSsid(ssidRead);
246         config.SetPreSharedKey(preSharedKeyRead);
247         ret = SetHotspotConfig(config);
248     }
249     reply.WriteInt32(0);
250     reply.WriteInt32(ret);
251 
252     return;
253 }
254 
OnGetStationList(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)255 void WifiHotspotStub::OnGetStationList(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
256 {
257     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
258     std::vector<StationInfo> result;
259     ErrCode ret = GetStationList(result);
260 
261     reply.WriteInt32(0);
262     reply.WriteInt32(ret);
263     if (ret == WIFI_OPT_SUCCESS) {
264         int size = static_cast<int>(result.size());
265         reply.WriteInt32(size);
266         for (int i = 0; i < size; i++) {
267             reply.WriteCString(result[i].deviceName.c_str());
268             reply.WriteCString(result[i].bssid.c_str());
269             reply.WriteInt32(result[i].bssidType);
270             reply.WriteCString(result[i].ipAddr.c_str());
271         }
272     }
273 
274     return;
275 }
276 
OnDisassociateSta(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)277 void WifiHotspotStub::OnDisassociateSta(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
278 {
279     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
280     ErrCode ret = WIFI_OPT_FAILED;
281     StationInfo info;
282     const char *deviceNameRead = data.ReadCString();
283     const char *bssidRead = data.ReadCString();
284     const int bssidTypeRead = data.ReadInt32();
285     const char *ipAddrRead = data.ReadCString();
286     if (deviceNameRead == nullptr || bssidRead == nullptr || ipAddrRead == nullptr) {
287         WIFI_LOGE("failed to read data, %{public}s", __func__);
288         ret = WIFI_OPT_INVALID_PARAM;
289     } else {
290         info.deviceName = deviceNameRead;
291         info.bssid = bssidRead;
292         info.bssidType = bssidTypeRead;
293         info.ipAddr = ipAddrRead;
294         ret = DisassociateSta(info);
295     }
296     reply.WriteInt32(0);
297     reply.WriteInt32(ret);
298     return;
299 }
300 
OnGetValidBands(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)301 void WifiHotspotStub::OnGetValidBands(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
302 {
303     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
304     std::vector<BandType> bands;
305     ErrCode ret = GetValidBands(bands);
306 
307     reply.WriteInt32(0);
308     reply.WriteInt32(ret);
309     if (ret == WIFI_OPT_SUCCESS) {
310         int count = static_cast<int>(bands.size());
311         reply.WriteInt32(count);
312         for (int i = 0; i < count; i++) {
313             reply.WriteInt32((int)bands[i]);
314         }
315     }
316     return;
317 }
318 
OnGetValidChannels(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)319 void WifiHotspotStub::OnGetValidChannels(
320     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
321 {
322     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
323     std::vector<int32_t> channels;
324     int32_t band = data.ReadInt32();
325     ErrCode ret = GetValidChannels(static_cast<BandType>(band), channels);
326 
327     reply.WriteInt32(0);
328     reply.WriteInt32(ret);
329     if (ret == WIFI_OPT_SUCCESS) {
330         int count = static_cast<int>(channels.size());
331         reply.WriteInt32(count);
332         for (int i = 0; i < count; i++) {
333             reply.WriteInt32(channels[i]);
334         }
335     }
336     return;
337 }
338 
OnEnableWifiAp(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)339 void WifiHotspotStub::OnEnableWifiAp(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
340 {
341     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
342     int32_t serviceType = data.ReadInt32();
343     ErrCode ret = EnableHotspot(ServiceType(serviceType));
344     reply.WriteInt32(0);
345     reply.WriteInt32(ret);
346 
347     return;
348 }
349 
OnDisableWifiAp(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)350 void WifiHotspotStub::OnDisableWifiAp(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
351 {
352     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
353     int32_t serviceType = data.ReadInt32();
354     ErrCode ret = DisableHotspot(ServiceType(serviceType));
355     reply.WriteInt32(0);
356     reply.WriteInt32(ret);
357 
358     return;
359 }
360 
OnAddBlockList(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)361 void WifiHotspotStub::OnAddBlockList(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
362 {
363     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
364     ErrCode ret = WIFI_OPT_FAILED;
365     StationInfo info;
366     const char *deviceNameRead = data.ReadCString();
367     const char *bssidRead = data.ReadCString();
368     const int bssidTypeRead = data.ReadInt32();
369     const char *ipAddrRead = data.ReadCString();
370     if (deviceNameRead == nullptr || bssidRead == nullptr || ipAddrRead == nullptr) {
371         WIFI_LOGE("failed to read data, %{public}s", __func__);
372         ret = WIFI_OPT_INVALID_PARAM;
373     } else {
374         info.deviceName = deviceNameRead;
375         info.bssid = bssidRead;
376         info.bssidType = bssidTypeRead;
377         info.ipAddr = ipAddrRead;
378         ret = AddBlockList(info);
379     }
380     reply.WriteInt32(0);
381     reply.WriteInt32(ret);
382 
383     return;
384 }
385 
OnDelBlockList(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)386 void WifiHotspotStub::OnDelBlockList(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
387 {
388     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
389     ErrCode ret = WIFI_OPT_FAILED;
390     StationInfo info;
391     const char *deviceNameRead = data.ReadCString();
392     const char *bssidRead = data.ReadCString();
393     const int bssidTypeRead = data.ReadInt32();
394     const char *ipAddrRead = data.ReadCString();
395     if (deviceNameRead == nullptr || bssidRead == nullptr || ipAddrRead == nullptr) {
396         WIFI_LOGE("failed to read data, %{public}s", __func__);
397         ret = WIFI_OPT_INVALID_PARAM;
398     } else {
399         info.deviceName = deviceNameRead;
400         info.bssid = bssidRead;
401         info.bssidType = bssidTypeRead;
402         info.ipAddr = ipAddrRead;
403         ret = DelBlockList(info);
404     }
405     reply.WriteInt32(0);
406     reply.WriteInt32(ret);
407 
408     return;
409 }
410 
OnGetBlockLists(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)411 void WifiHotspotStub::OnGetBlockLists(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
412 {
413     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
414     std::vector<StationInfo> infos;
415     ErrCode ret = GetBlockLists(infos);
416     reply.WriteInt32(0);
417     reply.WriteInt32(ret);
418     if (ret == WIFI_OPT_SUCCESS) {
419         int size = static_cast<int>(infos.size());
420         reply.WriteInt32(size);
421         for (int i = 0; i < size; i++) {
422             reply.WriteCString(infos[i].deviceName.c_str());
423             reply.WriteCString(infos[i].bssid.c_str());
424             reply.WriteInt32(infos[i].bssidType);
425             reply.WriteCString(infos[i].ipAddr.c_str());
426         }
427     }
428 
429     return;
430 }
431 
OnRegisterCallBack(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)432 void WifiHotspotStub::OnRegisterCallBack(
433     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
434 {
435     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
436     ErrCode ret = WIFI_OPT_FAILED;
437     do {
438         sptr<IRemoteObject> remote = data.ReadRemoteObject();
439         if (remote == nullptr) {
440             WIFI_LOGE("Failed to ReadRemoteObject!");
441             break;
442         }
443         sptr<IWifiHotspotCallback> callback_ = iface_cast<IWifiHotspotCallback>(remote);
444         if (callback_ == nullptr) {
445             callback_ = new (std::nothrow) WifiHotspotCallbackProxy(remote);
446             WIFI_LOGI("create new WifiHotspotCallbackProxy!");
447         }
448 
449         int eventNum = data.ReadInt32();
450         std::vector<std::string> event;
451         if (eventNum > 0 && eventNum <= MAX_READ_EVENT_SIZE) {
452             for (int i = 0; i < eventNum; ++i) {
453                 event.emplace_back(data.ReadString());
454             }
455         }
456 
457         if (mSingleCallback) {
458             ret = RegisterCallBack(callback_, event);
459         } else {
460             std::unique_lock<std::mutex> lock(deathRecipientMutex);
461             if (deathRecipient_ == nullptr) {
462                 deathRecipient_ = new (std::nothrow) WifiHotspotDeathRecipient();
463             }
464             // Add death recipient to remote object if this is the first time to register callback.
465             if (remote->IsProxyObject() &&
466                 !WifiInternalEventDispatcher::GetInstance().HasHotspotRemote(remote, m_id)) {
467                 remote->AddDeathRecipient(deathRecipient_);
468             }
469 
470             if (callback_ != nullptr) {
471                 for (const auto &eventName : event) {
472                     ret = WifiInternalEventDispatcher::GetInstance().AddHotspotCallback(remote, callback_, eventName,
473                         m_id);
474                 }
475             }
476         }
477     } while (0);
478     reply.WriteInt32(0);
479     reply.WriteInt32(ret);
480     return;
481 }
482 
OnGetSupportedFeatures(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)483 void WifiHotspotStub::OnGetSupportedFeatures(
484     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
485 {
486     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
487     long features = 0;
488     int ret = GetSupportedFeatures(features);
489     reply.WriteInt32(0);
490     reply.WriteInt32(ret);
491 
492     if (ret == WIFI_OPT_SUCCESS) {
493         reply.WriteInt64(features);
494     }
495 
496     return;
497 }
498 
OnGetSupportedPowerModel(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)499 void WifiHotspotStub::OnGetSupportedPowerModel(uint32_t code, MessageParcel &data,
500     MessageParcel &reply, MessageOption &option)
501 {
502     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
503     std::set<PowerModel> setPowerModelList;
504     ErrCode ret = GetSupportedPowerModel(setPowerModelList);
505     reply.WriteInt32(0);
506     reply.WriteInt32(ret);
507     if (ret == WIFI_OPT_SUCCESS) {
508         int size = (int)setPowerModelList.size();
509         reply.WriteInt32(size);
510         for (auto &powerModel : setPowerModelList) {
511             reply.WriteInt32(static_cast<int>(powerModel));
512         }
513     }
514     return;
515 }
516 
OnGetPowerModel(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)517 void WifiHotspotStub::OnGetPowerModel(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
518 {
519     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
520     PowerModel model;
521     ErrCode ret = GetPowerModel(model);
522     reply.WriteInt32(0);
523     reply.WriteInt32(ret);
524     reply.WriteInt32(static_cast<int>(model));
525     return;
526 }
527 
OnSetPowerModel(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)528 void WifiHotspotStub::OnSetPowerModel(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
529 {
530     WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
531     PowerModel model = PowerModel(data.ReadInt32());
532     ErrCode ret = SetPowerModel(model);
533     reply.WriteInt32(0);
534     reply.WriteInt32(ret);
535     return;
536 }
537 
OnSetApIdleTimeout(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)538 void WifiHotspotStub::OnSetApIdleTimeout(uint32_t code, MessageParcel &data,
539     MessageParcel &reply, MessageOption &option)
540 {
541     WIFI_LOGD("run %{private}s code %{private}u, datasize %{private}zu", __func__, code, data.GetRawDataSize());
542     int time = data.ReadInt32();
543     int ret = SetHotspotIdleTimeout(time);
544     reply.WriteInt32(0);
545     reply.WriteInt32(ret);
546 
547     return;
548 }
549 
OnGetApIfaceName(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)550 void WifiHotspotStub::OnGetApIfaceName(uint32_t code, MessageParcel &data,
551     MessageParcel &reply, MessageOption &option)
552 {
553     WIFI_LOGD("run %{private}s code %{private}u, datasize %{private}zu", __func__, code, data.GetRawDataSize());
554     std::string ifaceName;
555     ErrCode ret = GetApIfaceName(ifaceName);
556     reply.WriteInt32(0);
557     reply.WriteInt32(ret);
558     if (ret == WIFI_OPT_SUCCESS) {
559         reply.WriteString(ifaceName);
560     }
561     return;
562 }
563 }  // namespace Wifi
564 }  // namespace OHOS
565