• 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_device_proxy.h"
17 #include "define.h"
18 #include "wifi_common_util.h"
19 #include "wifi_device_callback_stub.h"
20 #include "wifi_hisysevent.h"
21 #include "wifi_logger.h"
22 
23 DEFINE_WIFILOG_LABEL("WifiDeviceProxy");
24 
25 namespace OHOS {
26 namespace Wifi {
27 static sptr<WifiDeviceCallBackStub> g_deviceCallBackStub =
28     sptr<WifiDeviceCallBackStub>(new (std::nothrow) WifiDeviceCallBackStub());
29 
WifiDeviceProxy(const sptr<IRemoteObject> & impl)30 WifiDeviceProxy::WifiDeviceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IWifiDevice>(impl),
31     remote_(nullptr), mRemoteDied(false), deathRecipient_(nullptr)
32 {
33     std::lock_guard<std::mutex> lock(mutex_);
34     if (impl) {
35         if (!impl->IsProxyObject()) {
36             WIFI_LOGW("not proxy object!");
37             return;
38         }
39         deathRecipient_ = new (std::nothrow) WifiDeathRecipient(*this);
40         if (deathRecipient_ == nullptr) {
41             WIFI_LOGW("deathRecipient_ is nullptr!");
42         }
43         if (!impl->AddDeathRecipient(deathRecipient_)) {
44             WIFI_LOGW("AddDeathRecipient failed!");
45             return;
46         }
47         remote_ = impl;
48         WIFI_LOGI("AddDeathRecipient success! deathRecipient_: %{private}p", static_cast<void*>(deathRecipient_));
49     }
50 }
51 
~WifiDeviceProxy()52 WifiDeviceProxy::~WifiDeviceProxy()
53 {
54     WIFI_LOGI("enter ~WifiDeviceProxy!");
55     RemoveDeathRecipient();
56 }
57 
RemoveDeathRecipient(void)58 void WifiDeviceProxy::RemoveDeathRecipient(void)
59 {
60     WIFI_LOGI("enter RemoveDeathRecipient, deathRecipient_: %{private}p!", static_cast<void*>(deathRecipient_));
61     std::lock_guard<std::mutex> lock(mutex_);
62     if (remote_ == nullptr) {
63         WIFI_LOGI("remote_ is nullptr!");
64         return;
65     }
66     if (deathRecipient_ == nullptr) {
67         WIFI_LOGI("deathRecipient_ is nullptr!");
68         return;
69     }
70     remote_->RemoveDeathRecipient(deathRecipient_);
71     remote_ = nullptr;
72 }
73 
EnableWifi()74 ErrCode WifiDeviceProxy::EnableWifi()
75 {
76     if (mRemoteDied) {
77         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
78         return WIFI_OPT_FAILED;
79     }
80     MessageOption option;
81     MessageParcel data;
82     MessageParcel reply;
83     if (!data.WriteInterfaceToken(GetDescriptor())) {
84         WIFI_LOGE("Write interface token error: %{public}s", __func__);
85         return WIFI_OPT_FAILED;
86     }
87     data.WriteInt32(0);
88     int error = Remote()->SendRequest(WIFI_SVR_CMD_ENABLE_WIFI, data, reply, option);
89     if (error != ERR_NONE) {
90         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_ENABLE_WIFI, error);
91         return WIFI_OPT_FAILED;
92     }
93 
94     int exception = reply.ReadInt32();
95     if (exception) {
96         return WIFI_OPT_FAILED;
97     }
98     WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_STA, WifiOperType::ENABLE);
99     return ErrCode(reply.ReadInt32());
100 }
101 
DisableWifi()102 ErrCode WifiDeviceProxy::DisableWifi()
103 {
104     if (mRemoteDied) {
105         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
106         return WIFI_OPT_FAILED;
107     }
108     MessageOption option;
109     MessageParcel data, reply;
110     if (!data.WriteInterfaceToken(GetDescriptor())) {
111         WIFI_LOGE("Write interface token error: %{public}s", __func__);
112         return WIFI_OPT_FAILED;
113     }
114     data.WriteInt32(0);
115     int error = Remote()->SendRequest(WIFI_SVR_CMD_DISABLE_WIFI, data, reply, option);
116     if (error != ERR_NONE) {
117         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_DISABLE_WIFI, error);
118         return WIFI_OPT_FAILED;
119     }
120     int exception = reply.ReadInt32();
121     if (exception) {
122         return WIFI_OPT_FAILED;
123     }
124     WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_STA, WifiOperType::DISABLE);
125     return ErrCode(reply.ReadInt32());
126 }
127 
InitWifiProtect(const WifiProtectType & protectType,const std::string & protectName)128 ErrCode WifiDeviceProxy::InitWifiProtect(const WifiProtectType &protectType, const std::string &protectName)
129 {
130     if (mRemoteDied) {
131         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
132         return WIFI_OPT_FAILED;
133     }
134     MessageOption option;
135     MessageParcel data, reply;
136     if (!data.WriteInterfaceToken(GetDescriptor())) {
137         WIFI_LOGE("Write interface token error: %{public}s", __func__);
138         return WIFI_OPT_FAILED;
139     }
140     data.WriteInt32(0);
141     data.WriteInt32((int)protectType);
142     data.WriteCString(protectName.c_str());
143     int error = Remote()->SendRequest(WIFI_SVR_CMD_INIT_WIFI_PROTECT, data, reply, option);
144     if (error != ERR_NONE) {
145         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_INIT_WIFI_PROTECT, error);
146         return ErrCode(error);
147     }
148     int exception = reply.ReadInt32();
149     if (exception) {
150         return WIFI_OPT_FAILED;
151     }
152     int ret = reply.ReadInt32();
153     if (ret != WIFI_OPT_SUCCESS) {
154         return ErrCode(ret);
155     }
156 
157     return WIFI_OPT_SUCCESS;
158 }
159 
GetWifiProtectRef(const WifiProtectMode & protectMode,const std::string & protectName)160 ErrCode WifiDeviceProxy::GetWifiProtectRef(const WifiProtectMode &protectMode, const std::string &protectName)
161 {
162     if (mRemoteDied) {
163         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
164         return WIFI_OPT_FAILED;
165     }
166     MessageOption option;
167     MessageParcel data, reply;
168     if (!data.WriteInterfaceToken(GetDescriptor())) {
169         WIFI_LOGE("Write interface token error: %{public}s", __func__);
170         return WIFI_OPT_FAILED;
171     }
172     data.WriteInt32(0);
173     data.WriteInt32((int)protectMode);
174     data.WriteCString(protectName.c_str());
175     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_WIFI_PROTECT, data, reply, option);
176     if (error != ERR_NONE) {
177         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_WIFI_PROTECT, error);
178         return ErrCode(error);
179     }
180     int exception = reply.ReadInt32();
181     if (exception) {
182         return WIFI_OPT_FAILED;
183     }
184     int ret = reply.ReadInt32();
185     if (ret != WIFI_OPT_SUCCESS) {
186         return ErrCode(ret);
187     }
188 
189     return WIFI_OPT_SUCCESS;
190 }
191 
PutWifiProtectRef(const std::string & protectName)192 ErrCode WifiDeviceProxy::PutWifiProtectRef(const std::string &protectName)
193 {
194     if (mRemoteDied) {
195         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
196         return WIFI_OPT_FAILED;
197     }
198     MessageOption option;
199     MessageParcel data, reply;
200     if (!data.WriteInterfaceToken(GetDescriptor())) {
201         WIFI_LOGE("Write interface token error: %{public}s", __func__);
202         return WIFI_OPT_FAILED;
203     }
204     data.WriteInt32(0);
205     data.WriteCString(protectName.c_str());
206     int error = Remote()->SendRequest(WIFI_SVR_CMD_PUT_WIFI_PROTECT, data, reply, option);
207     if (error != ERR_NONE) {
208         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_PUT_WIFI_PROTECT, error);
209         return ErrCode(error);
210     }
211     int exception = reply.ReadInt32();
212     if (exception) {
213         return WIFI_OPT_FAILED;
214     }
215     int ret = reply.ReadInt32();
216     if (ret != WIFI_OPT_SUCCESS) {
217         return ErrCode(ret);
218     }
219 
220     return WIFI_OPT_SUCCESS;
221 }
222 
WriteIpAddress(MessageParcel & data,const WifiIpAddress & address)223 void WifiDeviceProxy::WriteIpAddress(MessageParcel &data, const WifiIpAddress &address)
224 {
225     data.WriteInt32(address.family);
226     data.WriteInt32(address.addressIpv4);
227     int size = address.addressIpv6.size();
228     data.WriteInt32(size);
229     for (int i = 0; i < size; i++) {
230         data.WriteInt8(address.addressIpv6[i]);
231     }
232     return;
233 }
234 
WriteDeviceConfig(const WifiDeviceConfig & config,MessageParcel & data)235 void WifiDeviceProxy::WriteDeviceConfig(const WifiDeviceConfig &config, MessageParcel &data)
236 {
237     data.WriteInt32(config.networkId);
238     data.WriteInt32(config.status);
239     data.WriteString(config.bssid);
240     data.WriteString(config.ssid);
241     data.WriteInt32(config.band);
242     data.WriteInt32(config.channel);
243     data.WriteInt32(config.frequency);
244     data.WriteInt32(config.level);
245     data.WriteBool(config.isPasspoint);
246     data.WriteBool(config.isEphemeral);
247     data.WriteString(config.preSharedKey);
248     data.WriteString(config.keyMgmt);
249     for (int i = 0; i < WEPKEYS_SIZE; i++) {
250         data.WriteString(config.wepKeys[i]);
251     }
252     data.WriteInt32(config.wepTxKeyIndex);
253     data.WriteInt32(config.priority);
254     data.WriteBool(config.hiddenSSID);
255     data.WriteInt32((int)config.wifiIpConfig.assignMethod);
256     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.ipAddress.address);
257     data.WriteInt32(config.wifiIpConfig.staticIpAddress.ipAddress.prefixLength);
258     data.WriteInt32(config.wifiIpConfig.staticIpAddress.ipAddress.flags);
259     data.WriteInt32(config.wifiIpConfig.staticIpAddress.ipAddress.scope);
260     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.gateway);
261     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.dnsServer1);
262     WriteIpAddress(data, config.wifiIpConfig.staticIpAddress.dnsServer2);
263     data.WriteString(config.wifiIpConfig.staticIpAddress.domains);
264     data.WriteString(config.wifiEapConfig.eap);
265     data.WriteString(config.wifiEapConfig.identity);
266     data.WriteString(config.wifiEapConfig.password);
267     data.WriteString(config.wifiEapConfig.clientCert);
268     data.WriteString(config.wifiEapConfig.privateKey);
269     data.WriteUInt8Vector(config.wifiEapConfig.certEntry);
270     data.WriteString(config.wifiEapConfig.certPassword);
271     data.WriteInt32(static_cast<int>(config.wifiEapConfig.phase2Method));
272     data.WriteInt32((int)config.wifiProxyconfig.configureMethod);
273     data.WriteString(config.wifiProxyconfig.autoProxyConfig.pacWebAddress);
274     data.WriteString(config.wifiProxyconfig.manualProxyConfig.serverHostName);
275     data.WriteInt32(config.wifiProxyconfig.manualProxyConfig.serverPort);
276     data.WriteString(config.wifiProxyconfig.manualProxyConfig.exclusionObjectList);
277     data.WriteInt32((int)config.wifiPrivacySetting);
278 }
279 
RemoveCandidateConfig(const WifiDeviceConfig & config)280 ErrCode WifiDeviceProxy::RemoveCandidateConfig(const WifiDeviceConfig &config)
281 {
282     if (mRemoteDied) {
283         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
284         return WIFI_OPT_FAILED;
285     }
286     MessageOption option;
287     MessageParcel data, reply;
288     if (!data.WriteInterfaceToken(GetDescriptor())) {
289         WIFI_LOGE("Write interface token error: %{public}s", __func__);
290         return WIFI_OPT_FAILED;
291     }
292     data.WriteInt32(0);
293     /* Write a flag: 1-remove config by networkId, 2-remove config by WifiDeviceConfig */
294     data.WriteInt32(2);
295     WriteDeviceConfig(config, data);
296     int error = Remote()->SendRequest(WIFI_SVR_CMD_REMOVE_CANDIDATE_CONFIG, data, reply, option);
297     if (error != ERR_NONE) {
298         WIFI_LOGE("Set Attr(%{public}d) failed,error=%{public}d", WIFI_SVR_CMD_REMOVE_CANDIDATE_CONFIG, error);
299         return WIFI_OPT_FAILED;
300     }
301     int exception = reply.ReadInt32();
302     if (exception) {
303         return WIFI_OPT_FAILED;
304     }
305     int ret = reply.ReadInt32();
306     if (ret != WIFI_OPT_SUCCESS) {
307         return ErrCode(ret);
308     }
309 
310     return WIFI_OPT_SUCCESS;
311 }
312 
RemoveCandidateConfig(int networkId)313 ErrCode WifiDeviceProxy::RemoveCandidateConfig(int networkId)
314 {
315     if (mRemoteDied) {
316         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
317         return WIFI_OPT_FAILED;
318     }
319     MessageOption option;
320     MessageParcel data, reply;
321     if (!data.WriteInterfaceToken(GetDescriptor())) {
322         WIFI_LOGE("Write interface token error: %{public}s", __func__);
323         return WIFI_OPT_FAILED;
324     }
325     data.WriteInt32(0);
326     /* Write a flag: 1-remove config by networkId, 2-remove config by WifiDeviceConfig */
327     data.WriteInt32(1);
328     data.WriteInt32(networkId);
329     int error = Remote()->SendRequest(WIFI_SVR_CMD_REMOVE_CANDIDATE_CONFIG, data, reply, option);
330     if (error != ERR_NONE) {
331         WIFI_LOGE("Set Attr(%{public}d) failed,error=%{public}d", WIFI_SVR_CMD_REMOVE_CANDIDATE_CONFIG, error);
332         return WIFI_OPT_FAILED;
333     }
334     int exception = reply.ReadInt32();
335     if (exception) {
336         return WIFI_OPT_FAILED;
337     }
338     return ErrCode(reply.ReadInt32());
339 }
340 
AddDeviceConfig(const WifiDeviceConfig & config,int & result,bool isCandidate)341 ErrCode WifiDeviceProxy::AddDeviceConfig(const WifiDeviceConfig &config, int &result, bool isCandidate)
342 {
343     if (mRemoteDied) {
344         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
345         return WIFI_OPT_FAILED;
346     }
347     MessageOption option;
348     MessageParcel data, reply;
349     if (!data.WriteInterfaceToken(GetDescriptor())) {
350         WIFI_LOGE("Write interface token error: %{public}s", __func__);
351         return WIFI_OPT_FAILED;
352     }
353     data.WriteInt32(0);
354     /* true-candidate config, false-normal config */
355     data.WriteBool(isCandidate);
356     WriteDeviceConfig(config, data);
357     int error = Remote()->SendRequest(WIFI_SVR_CMD_ADD_DEVICE_CONFIG, data, reply, option);
358     if (error != ERR_NONE) {
359         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_ADD_DEVICE_CONFIG, error);
360         return WIFI_OPT_FAILED;
361     }
362     int exception = reply.ReadInt32();
363     if (exception) {
364         return WIFI_OPT_FAILED;
365     }
366     int ret = reply.ReadInt32();
367     if (ret != WIFI_OPT_SUCCESS) {
368         return ErrCode(ret);
369     }
370     result = reply.ReadInt32();
371 
372     return WIFI_OPT_SUCCESS;
373 }
374 
UpdateDeviceConfig(const WifiDeviceConfig & config,int & result)375 ErrCode WifiDeviceProxy::UpdateDeviceConfig(const WifiDeviceConfig &config, int &result)
376 {
377     if (mRemoteDied) {
378         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
379         return WIFI_OPT_FAILED;
380     }
381 
382     MessageOption option;
383     MessageParcel data, reply;
384     if (!data.WriteInterfaceToken(GetDescriptor())) {
385         WIFI_LOGE("Write interface token error: %{public}s", __func__);
386         return WIFI_OPT_FAILED;
387     }
388     data.WriteInt32(0);
389     WriteDeviceConfig(config, data);
390     int error = Remote()->SendRequest(WIFI_SVR_CMD_UPDATE_DEVICE_CONFIG, data, reply, option);
391     if (error != ERR_NONE) {
392         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_UPDATE_DEVICE_CONFIG, error);
393         return WIFI_OPT_FAILED;
394     }
395     int exception = reply.ReadInt32();
396     if (exception) {
397         return WIFI_OPT_FAILED;
398     }
399     int ret = reply.ReadInt32();
400     if (ret != WIFI_OPT_SUCCESS) {
401         return ErrCode(ret);
402     }
403     result = reply.ReadInt32();
404     return WIFI_OPT_SUCCESS;
405 }
406 
RemoveDevice(int networkId)407 ErrCode WifiDeviceProxy::RemoveDevice(int networkId)
408 {
409     if (mRemoteDied) {
410         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
411         return WIFI_OPT_FAILED;
412     }
413     MessageOption option;
414     MessageParcel data, reply;
415     if (!data.WriteInterfaceToken(GetDescriptor())) {
416         WIFI_LOGE("Write interface token error: %{public}s", __func__);
417         return WIFI_OPT_FAILED;
418     }
419     data.WriteInt32(0);
420     data.WriteInt32(networkId);
421     int error = Remote()->SendRequest(WIFI_SVR_CMD_REMOVE_DEVICE_CONFIG, data, reply, option);
422     if (error != ERR_NONE) {
423         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_REMOVE_DEVICE_CONFIG, error);
424         return WIFI_OPT_FAILED;
425     }
426     int exception = reply.ReadInt32();
427     if (exception) {
428         return WIFI_OPT_FAILED;
429     }
430     return ErrCode(reply.ReadInt32());
431 }
432 
RemoveAllDevice()433 ErrCode WifiDeviceProxy::RemoveAllDevice()
434 {
435     if (mRemoteDied) {
436         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
437         return WIFI_OPT_FAILED;
438     }
439     MessageOption option;
440     MessageParcel data;
441     MessageParcel reply;
442     if (!data.WriteInterfaceToken(GetDescriptor())) {
443         WIFI_LOGE("Write interface token error: %{public}s", __func__);
444         return WIFI_OPT_FAILED;
445     }
446     data.WriteInt32(0);
447     int error = Remote()->SendRequest(WIFI_SVR_CMD_REMOVE_ALL_DEVICE_CONFIG, data, reply, option);
448     if (error != ERR_NONE) {
449         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_REMOVE_ALL_DEVICE_CONFIG, error);
450         return WIFI_OPT_FAILED;
451     }
452 
453     int exception = reply.ReadInt32();
454     if (exception) {
455         return WIFI_OPT_FAILED;
456     }
457     return ErrCode(reply.ReadInt32());
458 }
459 
ReadIpAddress(MessageParcel & reply,WifiIpAddress & address)460 void WifiDeviceProxy::ReadIpAddress(MessageParcel &reply, WifiIpAddress &address)
461 {
462     constexpr int MAX_SIZE = 256;
463     address.family = reply.ReadInt32();
464     address.addressIpv4 = reply.ReadInt32();
465     int size = reply.ReadInt32();
466     if (size > MAX_SIZE) {
467         WIFI_LOGE("Read IP address size error: %{public}d", size);
468         return;
469     }
470     for (int i = 0; i < size; i++) {
471         address.addressIpv6.push_back(reply.ReadInt8());
472     }
473     return;
474 }
475 
ParseDeviceConfigs(MessageParcel & reply,std::vector<WifiDeviceConfig> & result)476 void WifiDeviceProxy::ParseDeviceConfigs(MessageParcel &reply, std::vector<WifiDeviceConfig> &result)
477 {
478     constexpr int MAX_DEVICE_CONFIG_SIZE = 1024;
479     int retSize = reply.ReadInt32();
480     if (retSize > MAX_DEVICE_CONFIG_SIZE) {
481         WIFI_LOGE("Parse device config size error: %{public}d", retSize);
482         return;
483     }
484     for (int i = 0; i < retSize; ++i) {
485         WifiDeviceConfig config;
486         config.networkId = reply.ReadInt32();
487         config.status = reply.ReadInt32();
488         config.bssid = reply.ReadString();
489         config.ssid = reply.ReadString();
490         config.band = reply.ReadInt32();
491         config.channel = reply.ReadInt32();
492         config.frequency = reply.ReadInt32();
493         config.level = reply.ReadInt32();
494         config.isPasspoint = reply.ReadBool();
495         config.isEphemeral = reply.ReadBool();
496         config.preSharedKey = reply.ReadString();
497         config.keyMgmt = reply.ReadString();
498         for (int j = 0; j < WEPKEYS_SIZE; j++) {
499             config.wepKeys[j] = reply.ReadString();
500         }
501         config.wepTxKeyIndex = reply.ReadInt32();
502         config.priority = reply.ReadInt32();
503         config.hiddenSSID = reply.ReadBool();
504         config.wifiIpConfig.assignMethod = AssignIpMethod(reply.ReadInt32());
505         ReadIpAddress(reply, config.wifiIpConfig.staticIpAddress.ipAddress.address);
506         config.wifiIpConfig.staticIpAddress.ipAddress.prefixLength = reply.ReadInt32();
507         config.wifiIpConfig.staticIpAddress.ipAddress.flags = reply.ReadInt32();
508         config.wifiIpConfig.staticIpAddress.ipAddress.scope = reply.ReadInt32();
509         ReadIpAddress(reply, config.wifiIpConfig.staticIpAddress.gateway);
510         ReadIpAddress(reply, config.wifiIpConfig.staticIpAddress.dnsServer1);
511         ReadIpAddress(reply, config.wifiIpConfig.staticIpAddress.dnsServer2);
512         config.wifiIpConfig.staticIpAddress.domains = reply.ReadString();
513         config.wifiEapConfig.eap = reply.ReadString();
514         config.wifiEapConfig.identity = reply.ReadString();
515         config.wifiEapConfig.password = reply.ReadString();
516         config.wifiEapConfig.clientCert = reply.ReadString();
517         config.wifiEapConfig.privateKey= reply.ReadString();
518         config.wifiEapConfig.phase2Method = Phase2Method(reply.ReadInt32());
519         config.wifiProxyconfig.configureMethod = ConfigureProxyMethod(reply.ReadInt32());
520         config.wifiProxyconfig.autoProxyConfig.pacWebAddress = reply.ReadString();
521         config.wifiProxyconfig.manualProxyConfig.serverHostName = reply.ReadString();
522         config.wifiProxyconfig.manualProxyConfig.serverPort = reply.ReadInt32();
523         config.wifiProxyconfig.manualProxyConfig.exclusionObjectList = reply.ReadString();
524         config.wifiPrivacySetting = WifiPrivacyConfig(reply.ReadInt32());
525         config.uid = reply.ReadInt32();
526 
527         result.emplace_back(config);
528     }
529 }
530 
GetDeviceConfigs(std::vector<WifiDeviceConfig> & result,bool isCandidate)531 ErrCode WifiDeviceProxy::GetDeviceConfigs(std::vector<WifiDeviceConfig> &result, bool isCandidate)
532 {
533     if (mRemoteDied) {
534         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
535         return WIFI_OPT_FAILED;
536     }
537     MessageOption option;
538     MessageParcel data;
539     MessageParcel reply;
540     if (!data.WriteInterfaceToken(GetDescriptor())) {
541         WIFI_LOGE("Write interface token error: %{public}s", __func__);
542         return WIFI_OPT_FAILED;
543     }
544     data.WriteInt32(0);
545     /* true-candidate config, false-normal config */
546     data.WriteBool(isCandidate);
547     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_DEVICE_CONFIGS, data, reply, option);
548     if (error != ERR_NONE) {
549         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_DEVICE_CONFIGS, error);
550         return WIFI_OPT_FAILED;
551     }
552     int exception = reply.ReadInt32();
553     if (exception) {
554         return WIFI_OPT_FAILED;
555     }
556     int ret = reply.ReadInt32();
557     if (ret != WIFI_OPT_SUCCESS) {
558         return ErrCode(ret);
559     }
560 
561     ParseDeviceConfigs(reply, result);
562     return WIFI_OPT_SUCCESS;
563 }
564 
EnableDeviceConfig(int networkId,bool attemptEnable)565 ErrCode WifiDeviceProxy::EnableDeviceConfig(int networkId, bool attemptEnable)
566 {
567     if (mRemoteDied) {
568         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
569         return WIFI_OPT_FAILED;
570     }
571     MessageOption option;
572     MessageParcel data, reply;
573     if (!data.WriteInterfaceToken(GetDescriptor())) {
574         WIFI_LOGE("Write interface token error: %{public}s", __func__);
575         return WIFI_OPT_FAILED;
576     }
577     data.WriteInt32(0);
578     data.WriteInt32(networkId);
579     data.WriteInt32(attemptEnable);
580     int error = Remote()->SendRequest(WIFI_SVR_CMD_ENABLE_DEVICE, data, reply, option);
581     if (error != ERR_NONE) {
582         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_ENABLE_DEVICE, error);
583         return WIFI_OPT_FAILED;
584     }
585     int exception = reply.ReadInt32();
586     if (exception) {
587         return WIFI_OPT_FAILED;
588     }
589     return ErrCode(reply.ReadInt32());
590 }
591 
DisableDeviceConfig(int networkId)592 ErrCode WifiDeviceProxy::DisableDeviceConfig(int networkId)
593 {
594     if (mRemoteDied) {
595         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
596         return WIFI_OPT_FAILED;
597     }
598     MessageOption option;
599     MessageParcel data, reply;
600     if (!data.WriteInterfaceToken(GetDescriptor())) {
601         WIFI_LOGE("Write interface token error: %{public}s", __func__);
602         return WIFI_OPT_FAILED;
603     }
604     data.WriteInt32(0);
605     data.WriteInt32(networkId);
606     int error = Remote()->SendRequest(WIFI_SVR_CMD_DISABLE_DEVICE, data, reply, option);
607     if (error != ERR_NONE) {
608         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_DISABLE_DEVICE, error);
609         return WIFI_OPT_FAILED;
610     }
611     int exception = reply.ReadInt32();
612     if (exception) {
613         return WIFI_OPT_FAILED;
614     }
615     return ErrCode(reply.ReadInt32());
616 }
617 
ConnectToNetwork(int networkId,bool isCandidate)618 ErrCode WifiDeviceProxy::ConnectToNetwork(int networkId, bool isCandidate)
619 {
620     if (mRemoteDied) {
621         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
622         return WIFI_OPT_FAILED;
623     }
624     MessageOption option;
625     MessageParcel data, reply;
626     if (!data.WriteInterfaceToken(GetDescriptor())) {
627         WIFI_LOGE("Write interface token error: %{public}s", __func__);
628         return WIFI_OPT_FAILED;
629     }
630     data.WriteInt32(0);
631     /* true-candidate config, false-normal config */
632     data.WriteBool(isCandidate);
633     data.WriteInt32(networkId);
634     int error = Remote()->SendRequest(WIFI_SVR_CMD_CONNECT_TO, data, reply, option);
635     if (error != ERR_NONE) {
636         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_CONNECT_TO, error);
637         return WIFI_OPT_FAILED;
638     }
639     int exception = reply.ReadInt32();
640     if (exception) {
641         return WIFI_OPT_FAILED;
642     }
643     WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
644     return ErrCode(reply.ReadInt32());
645 }
646 
ConnectToDevice(const WifiDeviceConfig & config)647 ErrCode WifiDeviceProxy::ConnectToDevice(const WifiDeviceConfig &config)
648 {
649     if (mRemoteDied) {
650         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
651         return WIFI_OPT_FAILED;
652     }
653     MessageOption option;
654     MessageParcel data, reply;
655     if (!data.WriteInterfaceToken(GetDescriptor())) {
656         WIFI_LOGE("Write interface token error: %{public}s", __func__);
657         return WIFI_OPT_FAILED;
658     }
659     data.WriteInt32(0);
660     WriteDeviceConfig(config, data);
661     int error = Remote()->SendRequest(WIFI_SVR_CMD_CONNECT2_TO, data, reply, option);
662     if (error != ERR_NONE) {
663         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_CONNECT2_TO, error);
664         return WIFI_OPT_FAILED;
665     }
666     int exception = reply.ReadInt32();
667     if (exception) {
668         return WIFI_OPT_FAILED;
669     }
670     WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
671     return ErrCode(reply.ReadInt32());
672 }
673 
IsConnected(bool & isConnected)674 ErrCode WifiDeviceProxy::IsConnected(bool &isConnected)
675 {
676     if (mRemoteDied) {
677         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
678         return WIFI_OPT_FAILED;
679     }
680     MessageOption option;
681     MessageParcel data, reply;
682     if (!data.WriteInterfaceToken(GetDescriptor())) {
683         WIFI_LOGE("Write interface token error: %{public}s", __func__);
684         return WIFI_OPT_FAILED;
685     }
686     data.WriteInt32(0);
687     int error = Remote()->SendRequest(WIFI_SVR_CMD_IS_WIFI_CONNECTED, data, reply, option);
688     if (error != ERR_NONE) {
689         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_IS_WIFI_CONNECTED, error);
690         return WIFI_OPT_FAILED;
691     }
692     int exception = reply.ReadInt32();
693     if (exception) {
694         return WIFI_OPT_FAILED;
695     }
696     int ret = reply.ReadInt32();
697     if (ret != WIFI_OPT_SUCCESS) {
698         return ErrCode(ret);
699     }
700     isConnected = reply.ReadBool();
701     return WIFI_OPT_SUCCESS;
702 }
703 
ReConnect()704 ErrCode WifiDeviceProxy::ReConnect()
705 {
706     if (mRemoteDied) {
707         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
708         return WIFI_OPT_FAILED;
709     }
710     MessageOption option;
711     MessageParcel data, reply;
712     if (!data.WriteInterfaceToken(GetDescriptor())) {
713         WIFI_LOGE("Write interface token error: %{public}s", __func__);
714         return WIFI_OPT_FAILED;
715     }
716     data.WriteInt32(0);
717     int error = Remote()->SendRequest(WIFI_SVR_CMD_RECONNECT, data, reply, option);
718     if (error != ERR_NONE) {
719         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_RECONNECT, error);
720         return WIFI_OPT_FAILED;
721     }
722     int exception = reply.ReadInt32();
723     if (exception) {
724         return WIFI_OPT_FAILED;
725     }
726     WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
727     return ErrCode(reply.ReadInt32());
728 }
729 
ReAssociate(void)730 ErrCode WifiDeviceProxy::ReAssociate(void)
731 {
732     if (mRemoteDied) {
733         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
734         return WIFI_OPT_FAILED;
735     }
736     MessageOption option;
737     MessageParcel data, reply;
738     if (!data.WriteInterfaceToken(GetDescriptor())) {
739         WIFI_LOGE("Write interface token error: %{public}s", __func__);
740         return WIFI_OPT_FAILED;
741     }
742     data.WriteInt32(0);
743     int error = Remote()->SendRequest(WIFI_SVR_CMD_REASSOCIATE, data, reply, option);
744     if (error != ERR_NONE) {
745         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_REASSOCIATE, error);
746         return WIFI_OPT_FAILED;
747     }
748     int exception = reply.ReadInt32();
749     if (exception) {
750         return WIFI_OPT_FAILED;
751     }
752     WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
753     return ErrCode(reply.ReadInt32());
754 }
755 
Disconnect(void)756 ErrCode WifiDeviceProxy::Disconnect(void)
757 {
758     if (mRemoteDied) {
759         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
760         return WIFI_OPT_FAILED;
761     }
762     MessageOption option;
763     MessageParcel data, reply;
764     if (!data.WriteInterfaceToken(GetDescriptor())) {
765         WIFI_LOGE("Write interface token error: %{public}s", __func__);
766         return WIFI_OPT_FAILED;
767     }
768     data.WriteInt32(0);
769     int error = Remote()->SendRequest(WIFI_SVR_CMD_DISCONNECT, data, reply, option);
770     if (error != ERR_NONE) {
771         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_DISCONNECT, error);
772         return WIFI_OPT_FAILED;
773     }
774     int exception = reply.ReadInt32();
775     if (exception) {
776         return WIFI_OPT_FAILED;
777     }
778     WriteWifiConnectionHiSysEvent(WifiConnectionType::DISCONNECT, GetBundleName());
779     return ErrCode(reply.ReadInt32());
780 }
781 
StartWps(const WpsConfig & config)782 ErrCode WifiDeviceProxy::StartWps(const WpsConfig &config)
783 {
784     if (mRemoteDied) {
785         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
786         return WIFI_OPT_FAILED;
787     }
788     MessageOption option;
789     MessageParcel data, reply;
790     if (!data.WriteInterfaceToken(GetDescriptor())) {
791         WIFI_LOGE("Write interface token error: %{public}s", __func__);
792         return WIFI_OPT_FAILED;
793     }
794     data.WriteInt32(0);
795     data.WriteInt32(static_cast<int>(config.setup));
796     data.WriteCString(config.pin.c_str());
797     data.WriteCString(config.bssid.c_str());
798     int error = Remote()->SendRequest(WIFI_SVR_CMD_START_WPS, data, reply, option);
799     if (error != ERR_NONE) {
800         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_START_WPS, error);
801         return WIFI_OPT_FAILED;
802     }
803     int exception = reply.ReadInt32();
804     if (exception) {
805         return WIFI_OPT_FAILED;
806     }
807     return ErrCode(reply.ReadInt32());
808 }
809 
CancelWps(void)810 ErrCode WifiDeviceProxy::CancelWps(void)
811 {
812     if (mRemoteDied) {
813         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
814         return WIFI_OPT_FAILED;
815     }
816     MessageOption option;
817     MessageParcel data, reply;
818     if (!data.WriteInterfaceToken(GetDescriptor())) {
819         WIFI_LOGE("Write interface token error: %{public}s", __func__);
820         return WIFI_OPT_FAILED;
821     }
822     data.WriteInt32(0);
823     int error = Remote()->SendRequest(WIFI_SVR_CMD_CANCEL_WPS, data, reply, option);
824     if (error != ERR_NONE) {
825         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_CANCEL_WPS, error);
826         return WIFI_OPT_FAILED;
827     }
828     int exception = reply.ReadInt32();
829     if (exception) {
830         return WIFI_OPT_FAILED;
831     }
832     return ErrCode(reply.ReadInt32());
833 }
834 
IsWifiActive(bool & bActive)835 ErrCode WifiDeviceProxy::IsWifiActive(bool &bActive)
836 {
837     if (mRemoteDied) {
838         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
839         return WIFI_OPT_FAILED;
840     }
841     MessageOption option;
842     MessageParcel data, reply;
843     if (!data.WriteInterfaceToken(GetDescriptor())) {
844         WIFI_LOGE("Write interface token error: %{public}s", __func__);
845         return WIFI_OPT_FAILED;
846     }
847     data.WriteInt32(0);
848     int error = Remote()->SendRequest(WIFI_SVR_CMD_IS_WIFI_ACTIVE, data, reply, option);
849     if (error != ERR_NONE) {
850         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_IS_WIFI_ACTIVE, error);
851         return WIFI_OPT_FAILED;
852     }
853     int exception = reply.ReadInt32();
854     if (exception) {
855         return WIFI_OPT_FAILED;
856     }
857     int ret = reply.ReadInt32();
858     if (ret != WIFI_OPT_SUCCESS) {
859         return ErrCode(ret);
860     }
861 
862     bActive = reply.ReadBool();
863     return WIFI_OPT_SUCCESS;
864 }
865 
GetWifiState(int & state)866 ErrCode WifiDeviceProxy::GetWifiState(int &state)
867 {
868     if (mRemoteDied) {
869         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
870         return WIFI_OPT_FAILED;
871     }
872     MessageOption option;
873     MessageParcel data, reply;
874     if (!data.WriteInterfaceToken(GetDescriptor())) {
875         WIFI_LOGE("Write interface token error: %{public}s", __func__);
876         return WIFI_OPT_FAILED;
877     }
878     data.WriteInt32(0);
879     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_WIFI_STATE, data, reply, option);
880     if (error != ERR_NONE) {
881         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_WIFI_STATE, error);
882         return WIFI_OPT_FAILED;
883     }
884     int exception = reply.ReadInt32();
885     if (exception) {
886         return WIFI_OPT_FAILED;
887     }
888     int ret = reply.ReadInt32();
889     if (ret != WIFI_OPT_SUCCESS) {
890         return ErrCode(ret);
891     }
892 
893     state = reply.ReadInt32();
894     return WIFI_OPT_SUCCESS;
895 }
896 
ReadLinkedInfo(MessageParcel & reply,WifiLinkedInfo & info)897 void WifiDeviceProxy::ReadLinkedInfo(MessageParcel &reply, WifiLinkedInfo &info)
898 {
899     info.networkId = reply.ReadInt32();
900     info.ssid = reply.ReadString();
901     info.bssid = reply.ReadString();
902     info.rssi = reply.ReadInt32();
903     info.band = reply.ReadInt32();
904     info.frequency = reply.ReadInt32();
905     info.linkSpeed = reply.ReadInt32();
906     info.macAddress = reply.ReadString();
907     info.macType = reply.ReadInt32();
908     info.ipAddress = reply.ReadInt32();
909     int tmpConnState = reply.ReadInt32();
910     if ((tmpConnState >= 0) && (tmpConnState <= (int)ConnState::UNKNOWN)) {
911         info.connState = ConnState(tmpConnState);
912     } else {
913         info.connState = ConnState::UNKNOWN;
914     }
915     info.ifHiddenSSID = reply.ReadBool();
916     info.rxLinkSpeed = reply.ReadInt32();
917     info.txLinkSpeed = reply.ReadInt32();
918     info.chload = reply.ReadInt32();
919     info.snr = reply.ReadInt32();
920     info.isDataRestricted = reply.ReadInt32();
921     info.portalUrl = reply.ReadString();
922 
923     int tmpState = reply.ReadInt32();
924     if ((tmpState >= 0) && (tmpState <= (int)SupplicantState::INVALID)) {
925         info.supplicantState = (SupplicantState)tmpState;
926     } else {
927         info.supplicantState = SupplicantState::INVALID;
928     }
929 
930     int tmpDetailState = reply.ReadInt32();
931     if ((tmpDetailState >= 0) && (tmpDetailState <= (int)DetailedState::INVALID)) {
932         info.detailedState = (DetailedState)tmpDetailState;
933     } else {
934         info.detailedState = DetailedState::INVALID;
935     }
936 }
937 
GetLinkedInfo(WifiLinkedInfo & info)938 ErrCode WifiDeviceProxy::GetLinkedInfo(WifiLinkedInfo &info)
939 {
940     if (mRemoteDied) {
941         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
942         return WIFI_OPT_FAILED;
943     }
944     MessageOption option;
945     MessageParcel data, reply;
946     if (!data.WriteInterfaceToken(GetDescriptor())) {
947         WIFI_LOGE("Write interface token error: %{public}s", __func__);
948         return WIFI_OPT_FAILED;
949     }
950     data.WriteInt32(0);
951     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_LINKED_INFO, data, reply, option);
952     if (error != ERR_NONE) {
953         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_LINKED_INFO, error);
954         return WIFI_OPT_FAILED;
955     }
956 
957     int exception = reply.ReadInt32();
958     if (exception) {
959         return WIFI_OPT_FAILED;
960     }
961     int ret = reply.ReadInt32();
962     if (ret != WIFI_OPT_SUCCESS) {
963         return ErrCode(ret);
964     }
965 
966     ReadLinkedInfo(reply, info);
967     return WIFI_OPT_SUCCESS;
968 }
969 
GetIpInfo(IpInfo & info)970 ErrCode WifiDeviceProxy::GetIpInfo(IpInfo &info)
971 {
972     if (mRemoteDied) {
973         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
974         return WIFI_OPT_FAILED;
975     }
976     MessageOption option;
977     MessageParcel data, reply;
978     if (!data.WriteInterfaceToken(GetDescriptor())) {
979         WIFI_LOGE("Write interface token error: %{public}s", __func__);
980         return WIFI_OPT_FAILED;
981     }
982     data.WriteInt32(0);
983     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_DHCP_INFO, data, reply, option);
984     if (error != ERR_NONE) {
985         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_DHCP_INFO, error);
986         return WIFI_OPT_FAILED;
987     }
988     int exception = reply.ReadInt32();
989     if (exception) {
990         return WIFI_OPT_FAILED;
991     }
992     int ret = reply.ReadInt32();
993     if (ret != WIFI_OPT_SUCCESS) {
994         return ErrCode(ret);
995     }
996 
997     info.ipAddress = reply.ReadInt32();
998     info.gateway = reply.ReadInt32();
999     info.netmask = reply.ReadInt32();
1000     info.primaryDns = reply.ReadInt32();
1001     info.secondDns = reply.ReadInt32();
1002     info.serverIp = reply.ReadInt32();
1003     info.leaseDuration = reply.ReadInt32();
1004     return WIFI_OPT_SUCCESS;
1005 }
1006 
SetCountryCode(const std::string & countryCode)1007 ErrCode WifiDeviceProxy::SetCountryCode(const std::string &countryCode)
1008 {
1009     if (mRemoteDied) {
1010         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
1011         return WIFI_OPT_FAILED;
1012     }
1013     MessageOption option;
1014     MessageParcel data, reply;
1015     if (!data.WriteInterfaceToken(GetDescriptor())) {
1016         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1017         return WIFI_OPT_FAILED;
1018     }
1019     data.WriteInt32(0);
1020     data.WriteString(countryCode);
1021     int error = Remote()->SendRequest(WIFI_SVR_CMD_SET_COUNTRY_CODE, data, reply, option);
1022     if (error != ERR_NONE) {
1023         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_SET_COUNTRY_CODE, error);
1024         return WIFI_OPT_FAILED;
1025     }
1026     int exception = reply.ReadInt32();
1027     if (exception) {
1028         return WIFI_OPT_FAILED;
1029     }
1030     return ErrCode(reply.ReadInt32());
1031 }
1032 
GetCountryCode(std::string & countryCode)1033 ErrCode WifiDeviceProxy::GetCountryCode(std::string &countryCode)
1034 {
1035     if (mRemoteDied) {
1036         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
1037         return WIFI_OPT_FAILED;
1038     }
1039     MessageOption option;
1040     MessageParcel data, reply;
1041     if (!data.WriteInterfaceToken(GetDescriptor())) {
1042         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1043         return WIFI_OPT_FAILED;
1044     }
1045     data.WriteInt32(0);
1046     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_COUNTRY_CODE, data, reply, option);
1047     if (error != ERR_NONE) {
1048         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_COUNTRY_CODE, error);
1049         return WIFI_OPT_FAILED;
1050     }
1051     int exception = reply.ReadInt32();
1052     if (exception) {
1053         return WIFI_OPT_FAILED;
1054     }
1055     int ret = reply.ReadInt32();
1056     if (ret != WIFI_OPT_SUCCESS) {
1057         return ErrCode(ret);
1058     }
1059     countryCode = reply.ReadString();
1060     return WIFI_OPT_SUCCESS;
1061 }
1062 
RegisterCallBack(const sptr<IWifiDeviceCallBack> & callback)1063 ErrCode WifiDeviceProxy::RegisterCallBack(const sptr<IWifiDeviceCallBack> &callback)
1064 {
1065     if (mRemoteDied) {
1066         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
1067         return WIFI_OPT_FAILED;
1068     }
1069     MessageParcel data, reply;
1070     MessageOption option(MessageOption::TF_ASYNC);
1071     if (!data.WriteInterfaceToken(GetDescriptor())) {
1072         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1073         return WIFI_OPT_FAILED;
1074     }
1075     data.WriteInt32(0);
1076 
1077     if (g_deviceCallBackStub == nullptr) {
1078         WIFI_LOGE("g_deviceCallBackStub is nullptr");
1079         return WIFI_OPT_FAILED;
1080     }
1081     g_deviceCallBackStub->RegisterUserCallBack(callback);
1082 
1083     if (!data.WriteRemoteObject(g_deviceCallBackStub->AsObject())) {
1084         WIFI_LOGE("WifiDeviceProxy::RegisterCallBack WriteRemoteObject failed!");
1085         return WIFI_OPT_FAILED;
1086     }
1087 
1088     int pid = GetCallingPid();
1089     data.WriteInt32(pid);
1090     WIFI_LOGD("%{public}s, calling uid: %{public}d, pid: %{public}d", __func__, GetCallingUid(), pid);
1091     int error = Remote()->SendRequest(WIFI_SVR_CMD_REGISTER_CALLBACK_CLIENT, data, reply, option);
1092     if (error != ERR_NONE) {
1093         WIFI_LOGE("Set Attr(%{public}d) failed, code is %{public}d", WIFI_SVR_CMD_REGISTER_CALLBACK_CLIENT, error);
1094         return WIFI_OPT_FAILED;
1095     }
1096     int exception = reply.ReadInt32();
1097     if (exception) {
1098         return WIFI_OPT_FAILED;
1099     }
1100     return ErrCode(reply.ReadInt32());
1101 }
1102 
GetSignalLevel(const int & rssi,const int & band,int & level)1103 ErrCode WifiDeviceProxy::GetSignalLevel(const int &rssi, const int &band, int &level)
1104 {
1105     if (mRemoteDied) {
1106         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
1107         return WIFI_OPT_FAILED;
1108     }
1109     MessageOption option;
1110     MessageParcel data, reply;
1111     if (!data.WriteInterfaceToken(GetDescriptor())) {
1112         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1113         return WIFI_OPT_FAILED;
1114     }
1115     data.WriteInt32(0);
1116     data.WriteInt32(rssi);
1117     data.WriteInt32(band);
1118     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_SIGNAL_LEVEL, data, reply, option);
1119     if (error != ERR_NONE) {
1120         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_SIGNAL_LEVEL, error);
1121         return WIFI_OPT_FAILED;
1122     }
1123     int exception = reply.ReadInt32();
1124     if (exception) {
1125         return WIFI_OPT_FAILED;
1126     }
1127     int ret = reply.ReadInt32();
1128     if (ret != WIFI_OPT_SUCCESS) {
1129         return ErrCode(ret);
1130     }
1131 
1132     level = reply.ReadInt32();
1133     return WIFI_OPT_SUCCESS;
1134 }
1135 
GetSupportedFeatures(long & features)1136 ErrCode WifiDeviceProxy::GetSupportedFeatures(long &features)
1137 {
1138     if (mRemoteDied) {
1139         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
1140         return WIFI_OPT_FAILED;
1141     }
1142     MessageOption option;
1143     MessageParcel data, reply;
1144     if (!data.WriteInterfaceToken(GetDescriptor())) {
1145         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1146         return WIFI_OPT_FAILED;
1147     }
1148     data.WriteInt32(0);
1149     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_SUPPORTED_FEATURES, data, reply, option);
1150     if (error != ERR_NONE) {
1151         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_SUPPORTED_FEATURES, error);
1152         return WIFI_OPT_FAILED;
1153     }
1154     int exception = reply.ReadInt32();
1155     if (exception) {
1156         return WIFI_OPT_FAILED;
1157     }
1158     int ret = reply.ReadInt32();
1159     if (ret != WIFI_OPT_SUCCESS) {
1160         return ErrCode(ret);
1161     }
1162 
1163     features = reply.ReadInt64();
1164     return WIFI_OPT_SUCCESS;
1165 }
1166 
GetDeviceMacAddress(std::string & result)1167 ErrCode WifiDeviceProxy::GetDeviceMacAddress(std::string &result)
1168 {
1169     if (mRemoteDied) {
1170         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
1171         return WIFI_OPT_FAILED;
1172     }
1173     const char *readStr = nullptr;
1174     MessageOption option;
1175     MessageParcel data;
1176     MessageParcel reply;
1177     if (!data.WriteInterfaceToken(GetDescriptor())) {
1178         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1179         return WIFI_OPT_FAILED;
1180     }
1181     data.WriteInt32(0);
1182     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_DERVICE_MAC_ADD, data, reply, option);
1183     if (error != ERR_NONE) {
1184         WIFI_LOGE("Set Attr(%{public}d) failed", WIFI_SVR_CMD_GET_DERVICE_MAC_ADD);
1185         return WIFI_OPT_FAILED;
1186     }
1187 
1188     int exception = reply.ReadInt32();
1189     if (exception) {
1190         return WIFI_OPT_FAILED;
1191     }
1192     int ret = reply.ReadInt32();
1193     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1194         return ErrCode(ret);
1195     }
1196     readStr = reply.ReadCString();
1197     result = (readStr != nullptr) ? readStr : "";
1198     return WIFI_OPT_SUCCESS;
1199 }
1200 
SetLowLatencyMode(bool enabled)1201 bool WifiDeviceProxy::SetLowLatencyMode(bool enabled)
1202 {
1203     if (mRemoteDied) {
1204         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
1205         return WIFI_OPT_FAILED;
1206     }
1207     MessageOption option;
1208     MessageParcel data;
1209     MessageParcel reply;
1210     if (!data.WriteInterfaceToken(GetDescriptor())) {
1211         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1212         return WIFI_OPT_FAILED;
1213     }
1214     data.WriteInt32(0);
1215     data.WriteBool(enabled);
1216     int error = Remote()->SendRequest(WIFI_SVR_CMD_SET_LOW_LATENCY_MODE, data, reply, option);
1217     if (error != ERR_NONE) {
1218         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_SET_LOW_LATENCY_MODE, error);
1219         return WIFI_OPT_FAILED;
1220     }
1221     int exception = reply.ReadInt32();
1222     if (exception) {
1223         return WIFI_OPT_FAILED;
1224     }
1225     return reply.ReadBool();
1226 }
1227 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)1228 void WifiDeviceProxy::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
1229 {
1230     WIFI_LOGW("Remote service is died! remoteObject: %{private}p", &remoteObject);
1231     mRemoteDied = true;
1232     RemoveDeathRecipient();
1233     if (g_deviceCallBackStub == nullptr) {
1234         WIFI_LOGE("g_deviceCallBackStub is nullptr");
1235         return;
1236     }
1237     if (g_deviceCallBackStub != nullptr) {
1238         g_deviceCallBackStub->SetRemoteDied(true);
1239     }
1240 }
1241 
IsRemoteDied(void)1242 bool WifiDeviceProxy::IsRemoteDied(void)
1243 {
1244     if (mRemoteDied) {
1245         WIFI_LOGW("IsRemoteDied! remote is died now!");
1246     }
1247     return mRemoteDied;
1248 }
1249 }  // namespace Wifi
1250 }  // namespace OHOS
1251