• 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 "wifi_hotspot_proxy.h"
17 #include "string_ex.h"
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 #include "wifi_hisysevent.h"
21 #include "wifi_hotspot_callback_stub.h"
22 #include "wifi_logger.h"
23 #include "wifi_manager_service_ipc_interface_code.h"
24 
25 DEFINE_WIFILOG_HOTSPOT_LABEL("WifiHotspotProxy");
26 namespace OHOS {
27 namespace Wifi {
28 static sptr<WifiHotspotCallbackStub> g_wifiHotspotCallbackStub =
29     sptr<WifiHotspotCallbackStub>(new (std::nothrow) WifiHotspotCallbackStub());
30 
WifiHotspotProxy(const sptr<IRemoteObject> & impl)31 WifiHotspotProxy::WifiHotspotProxy(const sptr<IRemoteObject> &impl)
32     : IRemoteProxy<IWifiHotspot>(impl), mRemoteDied(false), remote_(nullptr), deathRecipient_(nullptr)
33 {
34     std::lock_guard<std::mutex> lock(mutex_);
35     if (impl) {
36         if (!impl->IsProxyObject()) {
37             WIFI_LOGW("not proxy object!");
38             return;
39         }
40         deathRecipient_ = new (std::nothrow) WifiDeathRecipient(*this);
41         if (deathRecipient_ == nullptr) {
42             WIFI_LOGW("deathRecipient_ is nullptr!");
43         }
44         if (!impl->AddDeathRecipient(deathRecipient_)) {
45             WIFI_LOGW("AddDeathRecipient failed!");
46             return;
47         }
48         remote_ = impl;
49         WIFI_LOGI("AddDeathRecipient success! deathRecipient_");
50     }
51 }
52 
~WifiHotspotProxy()53 WifiHotspotProxy::~WifiHotspotProxy()
54 {
55     WIFI_LOGI("enter ~WifiHotspotProxy!");
56     RemoveDeathRecipient();
57 }
58 
RemoveDeathRecipient(void)59 void WifiHotspotProxy::RemoveDeathRecipient(void)
60 {
61     WIFI_LOGI("enter RemoveDeathRecipient, deathRecipient_!");
62     std::lock_guard<std::mutex> lock(mutex_);
63     if (remote_ == nullptr) {
64         WIFI_LOGI("remote_ is nullptr!");
65         return;
66     }
67     if (deathRecipient_ == nullptr) {
68         WIFI_LOGI("deathRecipient_ is nullptr!");
69         return;
70     }
71     remote_->RemoveDeathRecipient(deathRecipient_);
72     remote_ = nullptr;
73 }
74 
IsHotspotActive(bool & isActive)75 ErrCode WifiHotspotProxy::IsHotspotActive(bool &isActive)
76 {
77     if (mRemoteDied) {
78         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
79         return WIFI_OPT_FAILED;
80     }
81     MessageOption option;
82     MessageParcel data;
83     MessageParcel reply;
84     if (!data.WriteInterfaceToken(GetDescriptor())) {
85         WIFI_LOGE("Write interface token error: %{public}s", __func__);
86         return WIFI_OPT_FAILED;
87     }
88     data.WriteInt32(0);
89     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_IS_HOTSPOT_ACTIVE), data,
90         reply, option);
91     if (error != ERR_NONE) {
92         WIFI_LOGE("Set Attr(%{public}d) failed",
93             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_IS_HOTSPOT_ACTIVE));
94         return WIFI_OPT_FAILED;
95     }
96     int exception = reply.ReadInt32();
97     if (exception) {
98         return WIFI_OPT_FAILED;
99     }
100     int ret = reply.ReadInt32();
101     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
102         return ErrCode(ret);
103     }
104     isActive = ((reply.ReadInt32() == 1) ? true : false);
105     return WIFI_OPT_SUCCESS;
106 }
107 
IsHotspotDualBandSupported(bool & isSupported)108 ErrCode WifiHotspotProxy::IsHotspotDualBandSupported(bool &isSupported)
109 {
110     if (mRemoteDied) {
111         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
112         return WIFI_OPT_FAILED;
113     }
114     MessageOption option;
115     MessageParcel data;
116     MessageParcel reply;
117     if (!data.WriteInterfaceToken(GetDescriptor())) {
118         WIFI_LOGE("Write interface token error: %{public}s", __func__);
119         return WIFI_OPT_FAILED;
120     }
121     data.WriteInt32(0);
122     int error = Remote()->SendRequest(
123         static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_IS_HOTSPOT_DUAL_BAND_SUPPORTED), data, reply, option);
124     if (error != ERR_NONE) {
125         WIFI_LOGE("Set Attr(%{public}d) failed",
126             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_IS_HOTSPOT_DUAL_BAND_SUPPORTED));
127         return WIFI_OPT_FAILED;
128     }
129     int exception = reply.ReadInt32();
130     if (exception) {
131         return WIFI_OPT_FAILED;
132     }
133     int ret = reply.ReadInt32();
134     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
135         WIFI_LOGE("reply failed: %d", ret);
136         return ErrCode(ret);
137     }
138     isSupported = ((reply.ReadInt32() == 1) ? true : false);
139     return WIFI_OPT_SUCCESS;
140 }
141 
GetHotspotState(int & state)142 ErrCode WifiHotspotProxy::GetHotspotState(int &state)
143 {
144     if (mRemoteDied) {
145         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
146         return WIFI_OPT_FAILED;
147     }
148     MessageOption option;
149     MessageParcel data;
150     MessageParcel reply;
151     if (!data.WriteInterfaceToken(GetDescriptor())) {
152         WIFI_LOGE("Write interface token error: %{public}s", __func__);
153         return WIFI_OPT_FAILED;
154     }
155     data.WriteInt32(0);
156     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GETAPSTATE_WIFI), data,
157         reply, option);
158     if (error != ERR_NONE) {
159         WIFI_LOGE("Set Attr(%{public}d) failed",
160             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GETAPSTATE_WIFI));
161         return WIFI_OPT_FAILED;
162     }
163 
164     int exception = reply.ReadInt32();
165     if (exception) {
166         return WIFI_OPT_FAILED;
167     }
168     int ret = reply.ReadInt32();
169     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
170         return ErrCode(ret);
171     }
172     state = reply.ReadInt32();
173     return WIFI_OPT_SUCCESS;
174 }
175 
GetHotspotConfig(HotspotConfig & result)176 ErrCode WifiHotspotProxy::GetHotspotConfig(HotspotConfig &result)
177 {
178     if (mRemoteDied) {
179         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
180         return WIFI_OPT_FAILED;
181     }
182     const char *readStr = nullptr;
183     MessageOption option;
184     MessageParcel data;
185     MessageParcel reply;
186     if (!data.WriteInterfaceToken(GetDescriptor())) {
187         WIFI_LOGE("Write interface token error: %{public}s", __func__);
188         return WIFI_OPT_FAILED;
189     }
190     data.WriteInt32(0);
191     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_HOTSPOT_CONFIG),
192         data, reply, option);
193     if (error != ERR_NONE) {
194         WIFI_LOGE("Set Attr(%{public}d) failed",
195             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_HOTSPOT_CONFIG));
196         return WIFI_OPT_FAILED;
197     }
198 
199     int exception = reply.ReadInt32();
200     if (exception) {
201         return WIFI_OPT_FAILED;
202     }
203     int ret = reply.ReadInt32();
204     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
205         return ErrCode(ret);
206     }
207 
208     readStr = reply.ReadCString();
209     result.SetSsid((readStr != nullptr) ? readStr : "");
210     result.SetSecurityType(static_cast<KeyMgmt>(reply.ReadInt32()));
211     result.SetBand(static_cast<BandType>(reply.ReadInt32()));
212     result.SetChannel(reply.ReadInt32());
213     readStr = reply.ReadCString();
214     result.SetPreSharedKey((readStr != nullptr) ? readStr : "");
215     result.SetMaxConn(reply.ReadInt32());
216     result.SetIpAddress(reply.ReadString());
217     result.SetLeaseTime(reply.ReadInt32());
218 
219     return WIFI_OPT_SUCCESS;
220 }
221 
SetHotspotConfig(const HotspotConfig & config)222 ErrCode WifiHotspotProxy::SetHotspotConfig(const HotspotConfig &config)
223 {
224     if (mRemoteDied) {
225         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
226         return WIFI_OPT_FAILED;
227     }
228     MessageOption option;
229     MessageParcel data;
230     MessageParcel reply;
231     if (!data.WriteInterfaceToken(GetDescriptor())) {
232         WIFI_LOGE("Write interface token error: %{public}s", __func__);
233         return WIFI_OPT_FAILED;
234     }
235     data.WriteInt32(0);
236     data.WriteCString(config.GetSsid().c_str());
237     data.WriteInt32(static_cast<int>(config.GetSecurityType()));
238     data.WriteInt32(static_cast<int>(config.GetBand()));
239     data.WriteInt32(config.GetChannel());
240     data.WriteCString(config.GetPreSharedKey().c_str());
241     data.WriteInt32(config.GetMaxConn());
242     data.WriteString(config.GetIpAddress());
243     data.WriteInt32(config.GetLeaseTime());
244     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SETAPCONFIG_WIFI),
245         data, reply, option);
246     if (error != ERR_NONE) {
247         WIFI_LOGE("Set Attr(%{public}d) failed",
248             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SETAPCONFIG_WIFI));
249         return WIFI_OPT_FAILED;
250     }
251 
252     int exception = reply.ReadInt32();
253     if (exception) {
254         return WIFI_OPT_FAILED;
255     }
256     return ErrCode(reply.ReadInt32());
257 }
258 
SetHotspotIdleTimeout(int time)259 ErrCode WifiHotspotProxy::SetHotspotIdleTimeout(int time)
260 {
261     if (mRemoteDied) {
262         WIFI_LOGE("failed to `%{private}s`,remote service is died!", __func__);
263         return WIFI_OPT_FAILED;
264     }
265     MessageOption option;
266     MessageParcel data;
267     MessageParcel reply;
268     if (!data.WriteInterfaceToken(GetDescriptor())) {
269         WIFI_LOGE("Write interface token error: %{private}s", __func__);
270         return WIFI_OPT_FAILED;
271     }
272     data.WriteInt32(0);
273     data.WriteInt32(time);
274     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SETTIMEOUT_AP),
275         data, reply, option);
276     if (error != ERR_NONE) {
277         WIFI_LOGE("Set Attr(%{private}d) failed,error code is %{private}d",
278             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SETTIMEOUT_AP), error);
279         return WIFI_OPT_FAILED;
280     }
281 
282     int exception = reply.ReadInt32();
283     if (exception) {
284         return WIFI_OPT_FAILED;
285     }
286     int ret = reply.ReadInt32();
287     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
288         return ErrCode(ret);
289     }
290     return WIFI_OPT_SUCCESS;
291 }
292 
GetStationList(std::vector<StationInfo> & result)293 ErrCode WifiHotspotProxy::GetStationList(std::vector<StationInfo> &result)
294 {
295     if (mRemoteDied) {
296         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
297         return WIFI_OPT_FAILED;
298     }
299     const char *readStr = nullptr;
300     MessageOption option;
301     MessageParcel data;
302     MessageParcel reply;
303     if (!data.WriteInterfaceToken(GetDescriptor())) {
304         WIFI_LOGE("Write interface token error: %{public}s", __func__);
305         return WIFI_OPT_FAILED;
306     }
307     data.WriteInt32(0);
308     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_STATION_LIST), data,
309         reply, option);
310     if (error != ERR_NONE) {
311         WIFI_LOGE("Set Attr(%{public}d) failed",
312             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_STATION_LIST));
313         return WIFI_OPT_FAILED;
314     }
315 
316     int exception = reply.ReadInt32();
317     if (exception) {
318         return WIFI_OPT_FAILED;
319     }
320     int ret = reply.ReadInt32();
321     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
322         return ErrCode(ret);
323     }
324     constexpr int MAX_SIZE = 512;
325     int size = reply.ReadInt32();
326     if (size > MAX_SIZE) {
327         WIFI_LOGE("Station list size error: %{public}d", size);
328         return WIFI_OPT_FAILED;
329     }
330     for (int i = 0; i < size; i++) {
331         StationInfo info;
332         readStr = reply.ReadCString();
333         info.deviceName = (readStr != nullptr) ? readStr : "";
334         readStr = reply.ReadCString();
335         info.bssid = (readStr != nullptr) ? readStr : "";
336         info.bssidType = reply.ReadInt32();
337         readStr = reply.ReadCString();
338         info.ipAddr = (readStr != nullptr) ? readStr : "";
339         result.emplace_back(info);
340     }
341 
342     return WIFI_OPT_SUCCESS;
343 }
344 
DisassociateSta(const StationInfo & info)345 ErrCode WifiHotspotProxy::DisassociateSta(const StationInfo &info)
346 {
347     if (mRemoteDied) {
348         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
349         return WIFI_OPT_FAILED;
350     }
351     MessageOption option;
352     MessageParcel data;
353     MessageParcel reply;
354     if (!data.WriteInterfaceToken(GetDescriptor())) {
355         WIFI_LOGE("Write interface token error: %{public}s", __func__);
356         return WIFI_OPT_FAILED;
357     }
358     data.WriteInt32(0);
359     data.WriteCString(info.deviceName.c_str());
360     data.WriteCString(info.bssid.c_str());
361     data.WriteInt32(info.bssidType);
362     data.WriteCString(info.ipAddr.c_str());
363     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DISCONNECT_STA), data,
364         reply, option);
365     if (error != ERR_NONE) {
366         WIFI_LOGE("Set Attr(%{public}d) failed",
367             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DISCONNECT_STA));
368         return WIFI_OPT_FAILED;
369     }
370 
371     int exception = reply.ReadInt32();
372     if (exception) {
373         return ErrCode(exception);
374     }
375     return ErrCode(reply.ReadInt32());
376 }
377 
EnableHotspot(const ServiceType type)378 ErrCode WifiHotspotProxy::EnableHotspot(const ServiceType type)
379 {
380     MessageOption option;
381     MessageParcel data;
382     MessageParcel reply;
383     if (!data.WriteInterfaceToken(GetDescriptor())) {
384         WIFI_LOGE("Write interface token error: %{public}s", __func__);
385         return WIFI_OPT_FAILED;
386     }
387     data.WriteInt32(0);
388     data.WriteInt32(static_cast<int>(type));
389     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ENABLE_WIFI_AP), data,
390         reply, option);
391     if (error != ERR_NONE) {
392         WIFI_LOGE("Set Attr(%{public}d) failed",
393             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ENABLE_WIFI_AP));
394         return WIFI_OPT_FAILED;
395     }
396 
397     int exception = reply.ReadInt32();
398     if (exception) {
399         return WIFI_OPT_FAILED;
400     }
401     WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_AP, WifiOperType::ENABLE);
402     return ErrCode(reply.ReadInt32());
403 }
404 
DisableHotspot(const ServiceType type)405 ErrCode WifiHotspotProxy::DisableHotspot(const ServiceType type)
406 {
407     if (mRemoteDied) {
408         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
409         return WIFI_OPT_FAILED;
410     }
411     MessageOption option;
412     MessageParcel data;
413     MessageParcel reply;
414     if (!data.WriteInterfaceToken(GetDescriptor())) {
415         WIFI_LOGE("Write interface token error: %{public}s", __func__);
416         return WIFI_OPT_FAILED;
417     }
418     data.WriteInt32(0);
419     data.WriteInt32(static_cast<int>(type));
420     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DISABLE_WIFI_AP), data,
421         reply, option);
422     if (error != ERR_NONE) {
423         WIFI_LOGE("Set Attr(%{public}d) failed",
424             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DISABLE_WIFI_AP));
425         return WIFI_OPT_FAILED;
426     }
427 
428     int exception = reply.ReadInt32();
429     if (exception) {
430         return WIFI_OPT_FAILED;
431     }
432     WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_AP, WifiOperType::DISABLE);
433     return ErrCode(reply.ReadInt32());
434 }
435 
GetBlockLists(std::vector<StationInfo> & infos)436 ErrCode WifiHotspotProxy::GetBlockLists(std::vector<StationInfo> &infos)
437 {
438     if (mRemoteDied) {
439         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
440         return WIFI_OPT_FAILED;
441     }
442     const char *readStr = nullptr;
443     MessageOption option;
444     MessageParcel data;
445     MessageParcel reply;
446     if (!data.WriteInterfaceToken(GetDescriptor())) {
447         WIFI_LOGE("Write interface token error: %{public}s", __func__);
448         return WIFI_OPT_FAILED;
449     }
450     data.WriteInt32(0);
451     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_BLOCK_LISTS), data,
452         reply, option);
453     if (error != ERR_NONE) {
454         WIFI_LOGE("Set Attr(%{public}d) failed",
455             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_BLOCK_LISTS));
456         return WIFI_OPT_FAILED;
457     }
458 
459     int exception = reply.ReadInt32();
460     if (exception) {
461         return WIFI_OPT_FAILED;
462     }
463     int err = reply.ReadInt32();
464     if (err != WIFI_OPT_SUCCESS) {
465         return ErrCode(err);
466     }
467 
468     constexpr int MAX_SIZE = 512;
469     int size = reply.ReadInt32();
470     if (size > MAX_SIZE) {
471         WIFI_LOGE("Get block size error: %{public}d", size);
472         return WIFI_OPT_FAILED;
473     }
474 
475     for (int i = 0; i < size; i++) {
476         StationInfo info;
477         readStr = reply.ReadCString();
478         info.deviceName = (readStr != nullptr) ? readStr : "";
479         readStr = reply.ReadCString();
480         info.bssid = (readStr != nullptr) ? readStr : "";
481         info.bssidType = reply.ReadInt32();
482         readStr = reply.ReadCString();
483         info.ipAddr = (readStr != nullptr) ? readStr : "";
484         infos.push_back(info);
485     }
486     return WIFI_OPT_SUCCESS;
487 }
488 
AddBlockList(const StationInfo & info)489 ErrCode WifiHotspotProxy::AddBlockList(const StationInfo &info)
490 {
491     if (mRemoteDied) {
492         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
493         return WIFI_OPT_FAILED;
494     }
495     MessageOption option;
496     MessageParcel data;
497     MessageParcel reply;
498     if (!data.WriteInterfaceToken(GetDescriptor())) {
499         WIFI_LOGE("Write interface token error: %{public}s", __func__);
500         return WIFI_OPT_FAILED;
501     }
502     data.WriteInt32(0);
503     data.WriteCString(info.deviceName.c_str());
504     data.WriteCString(info.bssid.c_str());
505     data.WriteInt32(info.bssidType);
506     data.WriteCString(info.ipAddr.c_str());
507     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ADD_BLOCK_LIST), data,
508         reply, option);
509     if (error != ERR_NONE) {
510         WIFI_LOGE("Set Attr(%{public}d) failed",
511             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_ADD_BLOCK_LIST));
512         return WIFI_OPT_FAILED;
513     }
514 
515     int exception = reply.ReadInt32();
516     if (exception) {
517         return WIFI_OPT_FAILED;
518     }
519     return ErrCode(reply.ReadInt32());
520 }
521 
DelBlockList(const StationInfo & info)522 ErrCode WifiHotspotProxy::DelBlockList(const StationInfo &info)
523 {
524     if (mRemoteDied) {
525         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
526         return WIFI_OPT_FAILED;
527     }
528     MessageOption option;
529     MessageParcel data;
530     MessageParcel reply;
531     if (!data.WriteInterfaceToken(GetDescriptor())) {
532         WIFI_LOGE("Write interface token error: %{public}s", __func__);
533         return WIFI_OPT_FAILED;
534     }
535     data.WriteInt32(0);
536     data.WriteCString(info.deviceName.c_str());
537     data.WriteCString(info.bssid.c_str());
538     data.WriteInt32(info.bssidType);
539     data.WriteCString(info.ipAddr.c_str());
540     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DEL_BLOCK_LIST), data,
541         reply, option);
542     if (error != ERR_NONE) {
543         WIFI_LOGE("Set Attr(%{public}d) failed",
544             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_DEL_BLOCK_LIST));
545         return WIFI_OPT_FAILED;
546     }
547 
548     int exception = reply.ReadInt32();
549     if (exception) {
550         return WIFI_OPT_FAILED;
551     }
552     return ErrCode(reply.ReadInt32());
553 }
554 
GetValidBands(std::vector<BandType> & bands)555 ErrCode WifiHotspotProxy::GetValidBands(std::vector<BandType> &bands)
556 {
557     if (mRemoteDied) {
558         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
559         return WIFI_OPT_FAILED;
560     }
561     MessageOption option;
562     MessageParcel data;
563     MessageParcel reply;
564     if (!data.WriteInterfaceToken(GetDescriptor())) {
565         WIFI_LOGE("Write interface token error: %{public}s", __func__);
566         return WIFI_OPT_FAILED;
567     }
568     data.WriteInt32(0);
569     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_VALID_BANDS), data,
570         reply, option);
571     if (error != ERR_NONE) {
572         WIFI_LOGE("Set Attr(%{public}d) failed",
573             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_VALID_BANDS));
574         return WIFI_OPT_FAILED;
575     }
576 
577     int exception = reply.ReadInt32();
578     if (exception) {
579         return WIFI_OPT_FAILED;
580     }
581     int err = reply.ReadInt32();
582     if (err != WIFI_OPT_SUCCESS) {
583         return ErrCode(err);
584     }
585 
586     constexpr int MAX_BAND_SIZE = 512;
587     int count = reply.ReadInt32();
588     if (count > MAX_BAND_SIZE) {
589         WIFI_LOGE("Band size error: %{public}d", count);
590         return WIFI_OPT_FAILED;
591     }
592     for (int i = 0; i < count; i++) {
593         int val = reply.ReadInt32();
594         bands.push_back(BandType(val));
595     }
596     return WIFI_OPT_SUCCESS;
597 }
598 
GetValidChannels(BandType band,std::vector<int32_t> & validchannels)599 ErrCode WifiHotspotProxy::GetValidChannels(BandType band, std::vector<int32_t> &validchannels)
600 {
601     if (mRemoteDied) {
602         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
603         return WIFI_OPT_FAILED;
604     }
605     MessageOption option;
606     MessageParcel data;
607     MessageParcel reply;
608     if (!data.WriteInterfaceToken(GetDescriptor())) {
609         WIFI_LOGE("Write interface token error: %{public}s", __func__);
610         return WIFI_OPT_FAILED;
611     }
612     data.WriteInt32(0);
613     data.WriteInt32((int)band);
614     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_VALID_CHANNELS),
615         data, reply, option);
616     if (error != ERR_NONE) {
617         WIFI_LOGE("Set Attr(%{public}d) failed",
618             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_VALID_CHANNELS));
619         return WIFI_OPT_FAILED;
620     }
621 
622     int exception = reply.ReadInt32();
623     if (exception) {
624         return WIFI_OPT_FAILED;
625     }
626     int err = reply.ReadInt32();
627     if (err != WIFI_OPT_SUCCESS) {
628         return ErrCode(err);
629     }
630 
631     constexpr int MAX_CHANNELS_SIZE = 512;
632     int count = reply.ReadInt32();
633     if (count > MAX_CHANNELS_SIZE) {
634         WIFI_LOGE("Channel size error: %{public}d", count);
635         return WIFI_OPT_FAILED;
636     }
637     for (int i = 0; i < count; i++) {
638         int val = reply.ReadInt32();
639         validchannels.push_back(val);
640     }
641     return WIFI_OPT_SUCCESS;
642 }
643 
RegisterCallBack(const sptr<IWifiHotspotCallback> & callback,const std::vector<std::string> & event)644 ErrCode WifiHotspotProxy::RegisterCallBack(const sptr<IWifiHotspotCallback> &callback,
645     const std::vector<std::string> &event)
646 {
647     WIFI_LOGD("WifiHotspotProxy::RegisterCallBack!");
648     MessageParcel data;
649     MessageParcel reply;
650     MessageOption option(MessageOption::TF_ASYNC);
651 
652     g_wifiHotspotCallbackStub->RegisterCallBack(callback);
653     if (!data.WriteInterfaceToken(GetDescriptor())) {
654         WIFI_LOGE("Write interface token error: %{public}s", __func__);
655         return WIFI_OPT_FAILED;
656     }
657     data.WriteInt32(0);
658     if (!data.WriteRemoteObject(g_wifiHotspotCallbackStub->AsObject())) {
659         WIFI_LOGE("WifiHotspotProxy::RegisterCallBack WriteDate fail, write callback.");
660         return WIFI_OPT_FAILED;
661     }
662     int eventNum = static_cast<int>(event.size());
663     data.WriteInt32(eventNum);
664     if (eventNum > 0) {
665         for (auto &eventName : event) {
666             data.WriteString(eventName);
667         }
668     }
669     int error = Remote()->SendRequest(
670         static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_REGISTER_HOTSPOT_CALLBACK), data, reply, option);
671     if (error != ERR_NONE) {
672         WIFI_LOGE("WifiHotspotProxy::RegisterCallBack failed, error code is %{public}d ", error);
673         return WIFI_OPT_FAILED;
674     }
675     int exception = reply.ReadInt32();
676     if (exception) {
677         return WIFI_OPT_FAILED;
678     }
679     int ret = reply.ReadInt32();
680     return ErrCode(ret);
681 }
682 
GetSupportedFeatures(long & features)683 ErrCode WifiHotspotProxy::GetSupportedFeatures(long &features)
684 {
685     if (mRemoteDied) {
686         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
687         return WIFI_OPT_FAILED;
688     }
689     MessageOption option;
690     MessageParcel data, reply;
691     if (!data.WriteInterfaceToken(GetDescriptor())) {
692         WIFI_LOGE("Write interface token error: %{public}s", __func__);
693         return WIFI_OPT_FAILED;
694     }
695     data.WriteInt32(0);
696     int error = Remote()->SendRequest(static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES),
697         data, reply, option);
698     if (error != ERR_NONE) {
699         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
700             static_cast<int32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES), error);
701         return ErrCode(error);
702     }
703     int exception = reply.ReadInt32();
704     if (exception) {
705         return WIFI_OPT_FAILED;
706     }
707     int ret = reply.ReadInt32();
708     if (ret != WIFI_OPT_SUCCESS) {
709         return ErrCode(ret);
710     }
711 
712     features = reply.ReadInt64();
713     return WIFI_OPT_SUCCESS;
714 }
715 
GetSupportedPowerModel(std::set<PowerModel> & setPowerModelList)716 ErrCode WifiHotspotProxy::GetSupportedPowerModel(std::set<PowerModel>& setPowerModelList)
717 {
718     if (mRemoteDied) {
719         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
720         return WIFI_OPT_FAILED;
721     }
722     MessageOption option;
723     MessageParcel data;
724     MessageParcel reply;
725     if (!data.WriteInterfaceToken(GetDescriptor())) {
726         WIFI_LOGE("Write interface token error: %{public}s", __func__);
727         return WIFI_OPT_FAILED;
728     }
729     data.WriteInt32(0);
730     int error = Remote()->SendRequest(
731         static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_POWER_MODEL), data, reply, option);
732     if (error != ERR_NONE) {
733         WIFI_LOGE("Set Attr(%{public}d) failed",
734             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_POWER_MODEL));
735         return WIFI_OPT_FAILED;
736     }
737 
738     int exception = reply.ReadInt32();
739     if (exception) {
740         return WIFI_OPT_FAILED;
741     }
742     int err = reply.ReadInt32();
743     if (err != WIFI_OPT_SUCCESS) {
744         return ErrCode(err);
745     }
746 
747     constexpr int MAX_SIZE = 32;
748     int size = reply.ReadInt32();
749     if (size > MAX_SIZE) {
750         WIFI_LOGE("size error: %{public}d", size);
751         return WIFI_OPT_FAILED;
752     }
753     for (int i = 0; i < size; i++) {
754         int val = reply.ReadInt32();
755         setPowerModelList.insert(PowerModel(val));
756     }
757     return WIFI_OPT_SUCCESS;
758 }
759 
GetPowerModel(PowerModel & model)760 ErrCode WifiHotspotProxy::GetPowerModel(PowerModel& model)
761 {
762     if (mRemoteDied) {
763         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
764         return WIFI_OPT_FAILED;
765     }
766     MessageOption option;
767     MessageParcel data;
768     MessageParcel reply;
769     if (!data.WriteInterfaceToken(GetDescriptor())) {
770         WIFI_LOGE("Write interface token error: %{public}s", __func__);
771         return WIFI_OPT_FAILED;
772     }
773     data.WriteInt32(0);
774     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_POWER_MODEL), data,
775         reply, option);
776     if (error != ERR_NONE) {
777         WIFI_LOGE("Set Attr(%{public}d) failed",
778             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_POWER_MODEL));
779         return WIFI_OPT_FAILED;
780     }
781 
782     int exception = reply.ReadInt32();
783     if (exception) {
784         return WIFI_OPT_FAILED;
785     }
786     int err = reply.ReadInt32();
787     if (err != WIFI_OPT_SUCCESS) {
788         return ErrCode(err);
789     }
790     model = PowerModel(reply.ReadInt32());
791     return WIFI_OPT_SUCCESS;
792 }
793 
SetPowerModel(const PowerModel & model)794 ErrCode WifiHotspotProxy::SetPowerModel(const PowerModel& model)
795 {
796     if (mRemoteDied) {
797         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
798         return WIFI_OPT_FAILED;
799     }
800     MessageOption option;
801     MessageParcel data;
802     MessageParcel reply;
803     if (!data.WriteInterfaceToken(GetDescriptor())) {
804         WIFI_LOGE("Write interface token error: %{public}s", __func__);
805         return WIFI_OPT_FAILED;
806     }
807     data.WriteInt32(0);
808     data.WriteInt32(static_cast<int>(model));
809     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SET_POWER_MODEL), data,
810         reply, option);
811     if (error != ERR_NONE) {
812         WIFI_LOGE("Set Attr(%{public}d) failed",
813             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_SET_POWER_MODEL));
814         return WIFI_OPT_FAILED;
815     }
816 
817     int exception = reply.ReadInt32();
818     if (exception) {
819         return WIFI_OPT_FAILED;
820     }
821     return ErrCode(reply.ReadInt32());
822 }
823 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)824 void WifiHotspotProxy::OnRemoteDied(const wptr<IRemoteObject>& remoteObject)
825 {
826     WIFI_LOGW("Remote service is died!");
827     mRemoteDied = true;
828     RemoveDeathRecipient();
829     if (g_wifiHotspotCallbackStub != nullptr) {
830         g_wifiHotspotCallbackStub->SetRemoteDied(true);
831     }
832 }
833 
IsRemoteDied(void)834 bool WifiHotspotProxy::IsRemoteDied(void)
835 {
836     if (mRemoteDied) {
837         WIFI_LOGW("IsRemoteDied! remote is died now!");
838     }
839     return mRemoteDied;
840 }
841 
GetApIfaceName(std::string & ifaceName)842 ErrCode WifiHotspotProxy::GetApIfaceName(std::string& ifaceName)
843 {
844     if (mRemoteDied) {
845         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
846         return WIFI_OPT_FAILED;
847     }
848     MessageOption option;
849     MessageParcel data;
850     MessageParcel reply;
851     if (!data.WriteInterfaceToken(GetDescriptor())) {
852         WIFI_LOGE("Write interface token error: %{public}s", __func__);
853         return WIFI_OPT_FAILED;
854     }
855     data.WriteInt32(0);
856     int error = Remote()->SendRequest(static_cast<uint32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_IFACE_NAME), data,
857         reply, option);
858     if (error != ERR_NONE) {
859         WIFI_LOGE("Set Attr(%{public}d) failed",
860             static_cast<int32_t>(HotspotInterfaceCode::WIFI_SVR_CMD_GET_IFACE_NAME));
861         return WIFI_OPT_FAILED;
862     }
863 
864     int exception = reply.ReadInt32();
865     if (exception) {
866         return WIFI_OPT_FAILED;
867     }
868     int ret = reply.ReadInt32();
869     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
870         return ErrCode(ret);
871     }
872     ifaceName = reply.ReadString();
873     return WIFI_OPT_SUCCESS;
874 }
875 }  // namespace Wifi
876 }  // namespace OHOS
877