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