• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "net_policy_service_proxy.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 #include "net_policy_constants.h"
20 #include "ipc_skeleton.h"
21 #include "bundle_mgr_interface.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
NetPolicyServiceProxy(const sptr<IRemoteObject> & impl)25 NetPolicyServiceProxy::NetPolicyServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<INetPolicyService>(impl) {}
26 
27 NetPolicyServiceProxy::~NetPolicyServiceProxy() = default;
28 
SendRequest(sptr<IRemoteObject> & remote,uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)29 int32_t NetPolicyServiceProxy::SendRequest(sptr<IRemoteObject> &remote, uint32_t code, MessageParcel &data,
30                                            MessageParcel &reply, MessageOption &option)
31 {
32     if (remote == nullptr) {
33         NETMGR_LOG_E("Remote is null");
34         return NETMANAGER_ERR_OPERATION_FAILED;
35     }
36     int32_t retCode = remote->SendRequest(code, data, reply, option);
37     if (retCode != NETMANAGER_SUCCESS) {
38         return NETMANAGER_ERR_OPERATION_FAILED;
39     }
40     int32_t ret = NETMANAGER_SUCCESS;
41     if (!reply.ReadInt32(ret)) {
42         return NETMANAGER_ERR_READ_REPLY_FAIL;
43     }
44 
45     return ret;
46 }
47 
SetPolicyByUid(uint32_t uid,uint32_t policy)48 int32_t NetPolicyServiceProxy::SetPolicyByUid(uint32_t uid, uint32_t policy)
49 {
50     MessageParcel data;
51     if (!WriteInterfaceToken(data)) {
52         NETMGR_LOG_E("WriteInterfaceToken failed");
53         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
54     }
55 
56     if (!data.WriteUint32(uid)) {
57         NETMGR_LOG_E("Write uint32 data failed");
58         return NETMANAGER_ERR_WRITE_DATA_FAIL;
59     }
60     if (!data.WriteUint32(policy)) {
61         NETMGR_LOG_E("Write uint32 data failed");
62         return NETMANAGER_ERR_WRITE_DATA_FAIL;
63     }
64 
65     sptr<IRemoteObject> remote = Remote();
66     if (remote == nullptr) {
67         NETMGR_LOG_E("Remote is null");
68         return NETMANAGER_ERR_LOCAL_PTR_NULL;
69     }
70 
71     MessageParcel reply;
72     MessageOption option;
73     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POLICY_BY_UID), data, reply,
74                        option);
75 }
76 
GetPolicyByUid(uint32_t uid,uint32_t & policy)77 int32_t NetPolicyServiceProxy::GetPolicyByUid(uint32_t uid, uint32_t &policy)
78 {
79     MessageParcel data;
80     if (!WriteInterfaceToken(data)) {
81         NETMGR_LOG_E("WriteInterfaceToken failed");
82         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
83     }
84 
85     if (!data.WriteUint32(uid)) {
86         NETMGR_LOG_E("Write uint32 data failed");
87         return NETMANAGER_ERR_WRITE_DATA_FAIL;
88     }
89 
90     sptr<IRemoteObject> remote = Remote();
91     if (remote == nullptr) {
92         NETMGR_LOG_E("Remote is null");
93         return NETMANAGER_ERR_LOCAL_PTR_NULL;
94     }
95 
96     MessageParcel reply;
97     MessageOption option;
98     int32_t retCode =
99         SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POLICY_BY_UID), data, reply, option);
100     if (retCode != 0) {
101         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
102         return retCode;
103     }
104 
105     if (!reply.ReadUint32(policy)) {
106         NETMGR_LOG_E("Read uint32 reply failed");
107         return NETMANAGER_ERR_READ_REPLY_FAIL;
108     }
109 
110     return retCode;
111 }
112 
GetUidsByPolicy(uint32_t policy,std::vector<uint32_t> & uids)113 int32_t NetPolicyServiceProxy::GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids)
114 {
115     MessageParcel data;
116     if (!WriteInterfaceToken(data)) {
117         NETMGR_LOG_E("WriteInterfaceToken failed");
118         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
119     }
120 
121     if (!data.WriteUint32(policy)) {
122         NETMGR_LOG_E("Write uint32 data failed");
123         return NETMANAGER_ERR_WRITE_DATA_FAIL;
124     }
125 
126     sptr<IRemoteObject> remote = Remote();
127     if (remote == nullptr) {
128         NETMGR_LOG_E("Remote is null");
129         return NETMANAGER_ERR_LOCAL_PTR_NULL;
130     }
131 
132     MessageParcel reply;
133     MessageOption option;
134     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_UIDS_BY_POLICY), data,
135                                   reply, option);
136     if (retCode != 0) {
137         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
138         return retCode;
139     }
140 
141     if (!reply.ReadUInt32Vector(&uids)) {
142         NETMGR_LOG_E("Read uint32 vector reply failed");
143         return NETMANAGER_ERR_READ_REPLY_FAIL;
144     }
145     return retCode;
146 }
147 
IsUidNetAllowed(uint32_t uid,bool metered,bool & isAllowed)148 int32_t NetPolicyServiceProxy::IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed)
149 {
150     MessageParcel data;
151     if (!WriteInterfaceToken(data)) {
152         NETMGR_LOG_E("WriteInterfaceToken failed");
153         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
154     }
155 
156     if (!data.WriteUint32(uid)) {
157         NETMGR_LOG_E("Write uint32 data failed");
158         return NETMANAGER_ERR_WRITE_DATA_FAIL;
159     }
160 
161     if (!data.WriteBool(metered)) {
162         NETMGR_LOG_E("Write Bool data failed");
163         return NETMANAGER_ERR_WRITE_DATA_FAIL;
164     }
165 
166     sptr<IRemoteObject> remote = Remote();
167     if (remote == nullptr) {
168         NETMGR_LOG_E("Remote is null");
169         return NETMANAGER_ERR_LOCAL_PTR_NULL;
170     }
171 
172     MessageParcel reply;
173     MessageOption option;
174     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_METERED),
175                                   data, reply, option);
176     if (retCode != 0) {
177         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
178         return retCode;
179     }
180 
181     if (!reply.ReadBool(isAllowed)) {
182         NETMGR_LOG_E("Read Bool reply failed");
183         return NETMANAGER_ERR_READ_REPLY_FAIL;
184     }
185     return retCode;
186 }
187 
IsUidNetAllowed(uint32_t uid,const std::string & ifaceName,bool & isAllowed)188 int32_t NetPolicyServiceProxy::IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
189 {
190     MessageParcel data;
191     if (!WriteInterfaceToken(data)) {
192         NETMGR_LOG_E("WriteInterfaceToken failed");
193         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
194     }
195 
196     if (!data.WriteUint32(uid)) {
197         NETMGR_LOG_E("Write uint32 data failed");
198         return NETMANAGER_ERR_WRITE_DATA_FAIL;
199     }
200 
201     if (!data.WriteString(ifaceName)) {
202         NETMGR_LOG_E("Write Bool data failed");
203         return NETMANAGER_ERR_WRITE_DATA_FAIL;
204     }
205 
206     sptr<IRemoteObject> remote = Remote();
207     if (remote == nullptr) {
208         NETMGR_LOG_E("Remote is null");
209         return NETMANAGER_ERR_LOCAL_PTR_NULL;
210     }
211 
212     MessageParcel reply;
213     MessageOption option;
214     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_IFACE),
215                                   data, reply, option);
216     if (retCode != 0) {
217         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
218         return retCode;
219     }
220 
221     if (!reply.ReadBool(isAllowed)) {
222         NETMGR_LOG_E("Read Bool reply failed");
223         return NETMANAGER_ERR_READ_REPLY_FAIL;
224     }
225     return retCode;
226 }
227 
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)228 int32_t NetPolicyServiceProxy::RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
229 {
230     if (callback == nullptr) {
231         NETMGR_LOG_E("The parameter of callback is nullptr");
232         return NETMANAGER_ERR_LOCAL_PTR_NULL;
233     }
234 
235     MessageParcel dataParcel;
236     if (!WriteInterfaceToken(dataParcel)) {
237         NETMGR_LOG_E("WriteInterfaceToken failed");
238         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
239     }
240     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
241 
242     sptr<IRemoteObject> remote = Remote();
243     if (remote == nullptr) {
244         NETMGR_LOG_E("Remote is null");
245         return NETMANAGER_ERR_LOCAL_PTR_NULL;
246     }
247 
248     MessageOption option;
249     MessageParcel replyParcel;
250     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_REGISTER_NET_POLICY_CALLBACK),
251                        dataParcel, replyParcel, option);
252 }
253 
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)254 int32_t NetPolicyServiceProxy::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
255 {
256     if (callback == nullptr) {
257         NETMGR_LOG_E("The parameter of callback is nullptr");
258         return NETMANAGER_ERR_LOCAL_PTR_NULL;
259     }
260 
261     MessageParcel dataParcel;
262     if (!WriteInterfaceToken(dataParcel)) {
263         NETMGR_LOG_E("WriteInterfaceToken failed");
264         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
265     }
266     dataParcel.WriteRemoteObject(callback->AsObject().GetRefPtr());
267 
268     sptr<IRemoteObject> remote = Remote();
269     if (remote == nullptr) {
270         NETMGR_LOG_E("Remote is null");
271         return NETMANAGER_ERR_LOCAL_PTR_NULL;
272     }
273 
274     MessageOption option;
275     MessageParcel replyParcel;
276     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_UNREGISTER_NET_POLICY_CALLBACK),
277                        dataParcel, replyParcel, option);
278 }
279 
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)280 int32_t NetPolicyServiceProxy::SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)
281 {
282     MessageParcel data;
283     if (quotaPolicies.empty()) {
284         NETMGR_LOG_E("quotaPolicies is empty");
285         return NetPolicyResultCode::POLICY_ERR_INVALID_QUOTA_POLICY;
286     }
287 
288     if (!WriteInterfaceToken(data)) {
289         NETMGR_LOG_E("WriteInterfaceToken failed");
290         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
291     }
292 
293     sptr<IRemoteObject> remote = Remote();
294     if (remote == nullptr) {
295         NETMGR_LOG_E("Remote is null");
296         return NETMANAGER_ERR_LOCAL_PTR_NULL;
297     }
298 
299     if (!NetQuotaPolicy::Marshalling(data, quotaPolicies)) {
300         NETMGR_LOG_E("Marshalling failed");
301         return NETMANAGER_ERR_WRITE_DATA_FAIL;
302     }
303 
304     MessageParcel reply;
305     MessageOption option;
306     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_NET_QUOTA_POLICIES), data, reply,
307                        option);
308 }
309 
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)310 int32_t NetPolicyServiceProxy::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies)
311 {
312     MessageParcel data;
313     if (!WriteInterfaceToken(data)) {
314         NETMGR_LOG_E("WriteInterfaceToken failed");
315         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
316     }
317 
318     sptr<IRemoteObject> remote = Remote();
319     if (remote == nullptr) {
320         NETMGR_LOG_E("Remote is null");
321         return NETMANAGER_ERR_LOCAL_PTR_NULL;
322     }
323 
324     MessageParcel reply;
325     MessageOption option;
326     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_NET_QUOTA_POLICIES),
327                                   data, reply, option);
328     if (retCode != 0) {
329         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
330         return retCode;
331     }
332 
333     if (!NetQuotaPolicy::Unmarshalling(reply, quotaPolicies)) {
334         NETMGR_LOG_E("Unmarshalling failed.");
335         return NETMANAGER_ERR_READ_REPLY_FAIL;
336     }
337     return retCode;
338 }
339 
ResetPolicies(const std::string & simId)340 int32_t NetPolicyServiceProxy::ResetPolicies(const std::string &simId)
341 {
342     MessageParcel data;
343     if (!WriteInterfaceToken(data)) {
344         NETMGR_LOG_E("WriteInterfaceToken failed");
345         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
346     }
347 
348     if (!data.WriteString(simId)) {
349         NETMGR_LOG_E("Write String data failed");
350         return NETMANAGER_ERR_WRITE_DATA_FAIL;
351     }
352 
353     sptr<IRemoteObject> remote = Remote();
354     if (remote == nullptr) {
355         NETMGR_LOG_E("Remote is null");
356         return NETMANAGER_ERR_LOCAL_PTR_NULL;
357     }
358 
359     MessageParcel reply;
360     MessageOption option;
361     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_RESET_POLICIES), data, reply, option);
362 }
363 
SetBackgroundPolicy(bool isBackgroundPolicyAllow)364 int32_t NetPolicyServiceProxy::SetBackgroundPolicy(bool isBackgroundPolicyAllow)
365 {
366     MessageParcel data;
367     if (!WriteInterfaceToken(data)) {
368         NETMGR_LOG_E("WriteInterfaceToken failed");
369         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
370     }
371 
372     if (!data.WriteBool(isBackgroundPolicyAllow)) {
373         NETMGR_LOG_E("Write Bool data failed");
374         return NETMANAGER_ERR_WRITE_DATA_FAIL;
375     }
376 
377     sptr<IRemoteObject> remote = Remote();
378     if (remote == nullptr) {
379         NETMGR_LOG_E("Remote is null");
380         return NETMANAGER_ERR_LOCAL_PTR_NULL;
381     }
382 
383     MessageParcel reply;
384     MessageOption option;
385     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_BACKGROUND_POLICY), data, reply,
386                        option);
387 }
388 
GetBackgroundPolicy(bool & backgroundPolicy)389 int32_t NetPolicyServiceProxy::GetBackgroundPolicy(bool &backgroundPolicy)
390 {
391     MessageParcel data;
392     if (!WriteInterfaceToken(data)) {
393         NETMGR_LOG_E("WriteInterfaceToken failed");
394         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
395     }
396 
397     sptr<IRemoteObject> remote = Remote();
398     if (remote == nullptr) {
399         NETMGR_LOG_E("Remote is null");
400         return NETMANAGER_ERR_LOCAL_PTR_NULL;
401     }
402 
403     MessageParcel reply;
404     MessageOption option;
405     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY),
406                                   data, reply, option);
407     if (retCode != 0) {
408         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
409         return retCode;
410     }
411 
412     if (!reply.ReadBool(backgroundPolicy)) {
413         NETMGR_LOG_E("Read Bool reply failed");
414         return NETMANAGER_ERR_READ_REPLY_FAIL;
415     }
416     return retCode;
417 }
418 
GetBackgroundPolicyByUid(uint32_t uid,uint32_t & backgroundPolicyOfUid)419 int32_t NetPolicyServiceProxy::GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)
420 {
421     MessageParcel data;
422     if (!WriteInterfaceToken(data)) {
423         NETMGR_LOG_E("WriteInterfaceToken failed");
424         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
425     }
426 
427     if (!data.WriteUint32(uid)) {
428         NETMGR_LOG_E("Write uint32 data failed");
429         return NETMANAGER_ERR_WRITE_DATA_FAIL;
430     }
431 
432     sptr<IRemoteObject> remote = Remote();
433     if (remote == nullptr) {
434         NETMGR_LOG_E("Remote is null");
435         return NETMANAGER_ERR_LOCAL_PTR_NULL;
436     }
437 
438     MessageParcel reply;
439     MessageOption option;
440     int32_t retCode = SendRequest(
441         remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY_BY_UID), data, reply, option);
442     if (retCode != 0) {
443         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
444         return retCode;
445     }
446 
447     if (!reply.ReadUint32(backgroundPolicyOfUid)) {
448         NETMGR_LOG_E("Read uint32 reply failed");
449         return NETMANAGER_ERR_READ_REPLY_FAIL;
450     }
451     return retCode;
452 }
453 
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)454 int32_t NetPolicyServiceProxy::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
455 {
456     MessageParcel data;
457     if (!WriteInterfaceToken(data)) {
458         NETMGR_LOG_E("WriteInterfaceToken failed");
459         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
460     }
461 
462     sptr<IRemoteObject> remote = Remote();
463     if (remote == nullptr) {
464         NETMGR_LOG_E("Remote is null");
465         return NETMANAGER_ERR_LOCAL_PTR_NULL;
466     }
467 
468     if (!data.WriteInt32(netType)) {
469         NETMGR_LOG_E("Write int32 data failed");
470         return NETMANAGER_ERR_WRITE_DATA_FAIL;
471     }
472 
473     if (!data.WriteString(simId)) {
474         NETMGR_LOG_E("Write String data failed");
475         return NETMANAGER_ERR_WRITE_DATA_FAIL;
476     }
477 
478     if (!data.WriteUint32(remindType)) {
479         NETMGR_LOG_E("Write uint32 data failed");
480         return NETMANAGER_ERR_WRITE_DATA_FAIL;
481     }
482 
483     MessageParcel reply;
484     MessageOption option;
485     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_UPDATE_REMIND_POLICY), data, reply,
486                        option);
487 }
488 
SetDeviceIdleTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)489 int32_t NetPolicyServiceProxy::SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
490 {
491     MessageParcel data;
492     if (!WriteInterfaceToken(data)) {
493         NETMGR_LOG_E("WriteInterfaceToken failed");
494         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
495     }
496 
497     if (!data.WriteUInt32Vector(uids)) {
498         NETMGR_LOG_E("Write uint32 data failed");
499         return NETMANAGER_ERR_WRITE_DATA_FAIL;
500     }
501     if (!data.WriteBool(isAllowed)) {
502         NETMGR_LOG_E("Write Bool data failed");
503         return NETMANAGER_ERR_WRITE_DATA_FAIL;
504     }
505 
506     sptr<IRemoteObject> remote = Remote();
507     if (remote == nullptr) {
508         NETMGR_LOG_E("Remote is null");
509         return NETMANAGER_ERR_LOCAL_PTR_NULL;
510     }
511 
512     MessageParcel reply;
513     MessageOption option;
514     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_IDLE_TRUSTLIST), data, reply,
515                        option);
516 }
517 
GetDeviceIdleTrustlist(std::vector<uint32_t> & uids)518 int32_t NetPolicyServiceProxy::GetDeviceIdleTrustlist(std::vector<uint32_t> &uids)
519 {
520     MessageParcel data;
521     if (!WriteInterfaceToken(data)) {
522         NETMGR_LOG_E("WriteInterfaceToken failed");
523         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
524     }
525 
526     sptr<IRemoteObject> remote = Remote();
527     if (remote == nullptr) {
528         NETMGR_LOG_E("Remote is null");
529         return NETMANAGER_ERR_LOCAL_PTR_NULL;
530     }
531 
532     MessageParcel reply;
533     MessageOption option;
534     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_IDLE_TRUSTLIST), data,
535                                   reply, option);
536     if (retCode != 0) {
537         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
538         return retCode;
539     }
540 
541     if (!reply.ReadUInt32Vector(&uids)) {
542         NETMGR_LOG_E("Read reply uint32 Vector failed");
543         return NETMANAGER_ERR_READ_REPLY_FAIL;
544     }
545     return retCode;
546 }
547 
SetDeviceIdlePolicy(bool enable)548 int32_t NetPolicyServiceProxy::SetDeviceIdlePolicy(bool enable)
549 {
550     MessageParcel data;
551     if (!WriteInterfaceToken(data)) {
552         NETMGR_LOG_E("WriteInterfaceToken failed");
553         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
554     }
555 
556     sptr<IRemoteObject> remote = Remote();
557     if (remote == nullptr) {
558         NETMGR_LOG_E("Remote is null");
559         return NETMANAGER_ERR_LOCAL_PTR_NULL;
560     }
561 
562     if (!data.WriteBool(enable)) {
563         NETMGR_LOG_E("WriteBool Bool data failed.");
564         return NETMANAGER_ERR_WRITE_DATA_FAIL;
565     }
566 
567     MessageParcel reply;
568     MessageOption option;
569     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_DEVICE_IDLE_POLICY), data, reply,
570                        option);
571 }
572 
SetPowerSavePolicy(bool enable)573 int32_t NetPolicyServiceProxy::SetPowerSavePolicy(bool enable)
574 {
575     MessageParcel data;
576     if (!WriteInterfaceToken(data)) {
577         NETMGR_LOG_E("WriteInterfaceToken failed");
578         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
579     }
580 
581     sptr<IRemoteObject> remote = Remote();
582     if (remote == nullptr) {
583         NETMGR_LOG_E("Remote is null");
584         return NETMANAGER_ERR_LOCAL_PTR_NULL;
585     }
586 
587     if (!data.WriteBool(enable)) {
588         NETMGR_LOG_E("Write Bool data failed.");
589         return NETMANAGER_ERR_WRITE_DATA_FAIL;
590     }
591 
592     MessageParcel reply;
593     MessageOption option;
594     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POWER_SAVE_POLICY), data, reply,
595                        option);
596 }
597 
GetPowerSaveTrustlist(std::vector<uint32_t> & uids)598 int32_t NetPolicyServiceProxy::GetPowerSaveTrustlist(std::vector<uint32_t> &uids)
599 {
600     MessageParcel data;
601     if (!WriteInterfaceToken(data)) {
602         NETMGR_LOG_E("WriteInterfaceToken failed");
603         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
604     }
605 
606     sptr<IRemoteObject> remote = Remote();
607     if (remote == nullptr) {
608         NETMGR_LOG_E("Remote is null");
609         return NETMANAGER_ERR_LOCAL_PTR_NULL;
610     }
611 
612     MessageParcel reply;
613     MessageOption option;
614     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POWER_SAVE_TRUSTLIST),
615                                   data, reply, option);
616     if (retCode != ERR_NONE) {
617         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
618         return retCode;
619     }
620 
621     if (!reply.ReadUInt32Vector(&uids)) {
622         NETMGR_LOG_E("proxy SendRequest Readuint32Vector failed");
623         return NETMANAGER_ERR_READ_REPLY_FAIL;
624     }
625     return retCode;
626 }
627 
SetPowerSaveTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)628 int32_t NetPolicyServiceProxy::SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
629 {
630     MessageParcel data;
631     if (!WriteInterfaceToken(data)) {
632         NETMGR_LOG_E("WriteInterfaceToken failed");
633         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
634     }
635 
636     sptr<IRemoteObject> remote = Remote();
637     if (remote == nullptr) {
638         NETMGR_LOG_E("Remote is null");
639         return NETMANAGER_ERR_LOCAL_PTR_NULL;
640     }
641 
642     if (!data.WriteUInt32Vector(uids)) {
643         NETMGR_LOG_E("Write uint32 data failed");
644         return NETMANAGER_ERR_WRITE_DATA_FAIL;
645     }
646 
647     if (!data.WriteBool(isAllowed)) {
648         NETMGR_LOG_E("Write Bool data failed");
649         return NETMANAGER_ERR_WRITE_DATA_FAIL;
650     }
651 
652     MessageParcel reply;
653     MessageOption option;
654     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POWER_SAVE_TRUSTLIST), data,
655                        reply, option);
656 }
657 
WriteInterfaceToken(MessageParcel & data)658 bool NetPolicyServiceProxy::WriteInterfaceToken(MessageParcel &data)
659 {
660     if (!data.WriteInterfaceToken(NetPolicyServiceProxy::GetDescriptor())) {
661         NETMGR_LOG_E("WriteInterfaceToken failed");
662         return false;
663     }
664     return true;
665 }
666 
CheckPermission()667 int32_t NetPolicyServiceProxy::CheckPermission()
668 {
669     MessageParcel data;
670     if (!WriteInterfaceToken(data)) {
671         NETMGR_LOG_E("WriteInterfaceToken failed");
672         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
673     }
674 
675     sptr<IRemoteObject> remote = Remote();
676     if (remote == nullptr) {
677         NETMGR_LOG_E("Remote is null");
678         return NETMANAGER_ERR_LOCAL_PTR_NULL;
679     }
680 
681     MessageParcel reply;
682     MessageOption option;
683     int ret = remote->SendRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_CHECK_PERMISSION),
684         data, reply, option);
685     if (ret != ERR_NONE) {
686         NETMGR_LOG_E("proxy SendRequest failed, ret code: [%{public}d]", ret);
687         return NETMANAGER_ERR_OPERATION_FAILED;
688     }
689     int32_t result = 0;
690     if (!reply.ReadInt32(result)) {
691         NETMGR_LOG_E("Read int32 reply failed.");
692         return NETMANAGER_ERR_READ_REPLY_FAIL;
693     }
694     return result;
695 }
696 
FactoryResetPolicies()697 int32_t NetPolicyServiceProxy::FactoryResetPolicies()
698 {
699     return NETMANAGER_SUCCESS;
700 }
701 
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag)702 int32_t NetPolicyServiceProxy::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag)
703 {
704     MessageParcel data;
705     MessageParcel reply;
706     MessageOption option;
707     if (!WriteInterfaceToken(data)) {
708         NETMGR_LOG_E("WriteInterfaceToken failed");
709         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
710     }
711 
712     if (!data.WriteUint32(uid)) {
713         return NETMANAGER_ERR_WRITE_DATA_FAIL;
714     }
715     if (!data.WriteUint8(policy.wifiAllow)) {
716         return NETMANAGER_ERR_WRITE_DATA_FAIL;
717     }
718     if (!data.WriteUint8(policy.cellularAllow)) {
719         return NETMANAGER_ERR_WRITE_DATA_FAIL;
720     }
721     if (!data.WriteBool(reconfirmFlag)) {
722         return NETMANAGER_ERR_WRITE_DATA_FAIL;
723     }
724 
725     sptr<IRemoteObject> remote = Remote();
726     if (remote == nullptr) {
727         NETMGR_LOG_E("Remote is null");
728         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
729     }
730     int32_t error = remote->SendRequest(static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_NETWORK_ACCESS_POLICY),
731                                         data, reply, option);
732     if (error != ERR_NONE) {
733         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", error);
734         return NETMANAGER_ERR_OPERATION_FAILED;
735     }
736 
737     return reply.ReadInt32();
738 }
739 
GetNetworkAccessPolicy(AccessPolicyParameter parameter,AccessPolicySave & policy)740 int32_t NetPolicyServiceProxy::GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave &policy)
741 {
742     MessageParcel data;
743     if (!WriteInterfaceToken(data)) {
744         NETMGR_LOG_E("WriteInterfaceToken failed");
745         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
746     }
747 
748     uint32_t callingUid = static_cast<uint32_t>(IPCSkeleton::GetCallingUid());
749     parameter.userId = callingUid / AppExecFwk::Constants::BASE_USER_RANGE;
750     if (!data.WriteBool(parameter.flag)) {
751         return false;
752     }
753     if (!data.WriteInt32(parameter.uid)) {
754         return false;
755     }
756     if (!data.WriteUint32(parameter.userId)) {
757         return false;
758     }
759 
760     sptr<IRemoteObject> remote = Remote();
761     if (remote == nullptr) {
762         NETMGR_LOG_E("Remote is null");
763         return NETMANAGER_ERR_LOCAL_PTR_NULL;
764     }
765 
766     MessageParcel reply;
767     MessageOption option;
768     int32_t retCode = SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_NETWORK_ACCESS_POLICY),
769                                   data, reply, option);
770     if (retCode != NETMANAGER_SUCCESS) {
771         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
772         return retCode;
773     }
774 
775     retCode = NetworkAccessPolicy::Unmarshalling(reply, policy, parameter.flag);
776     if (retCode != NETMANAGER_SUCCESS) {
777         NETMGR_LOG_E("proxy SendRequest unmarshalling failed, error code: [%{public}d]", retCode);
778         return retCode;
779     }
780 
781     return retCode;
782 }
783 
NotifyNetAccessPolicyDiag(uint32_t uid)784 int32_t NetPolicyServiceProxy::NotifyNetAccessPolicyDiag(uint32_t uid)
785 {
786     MessageParcel data;
787     if (!WriteInterfaceToken(data)) {
788         NETMGR_LOG_E("WriteInterfaceToken failed");
789         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
790     }
791 
792     if (!data.WriteUint32(uid)) {
793         return NETMANAGER_ERR_WRITE_DATA_FAIL;
794     }
795     MessageParcel reply;
796     MessageOption option;
797 
798     sptr<IRemoteObject> remote = Remote();
799     if (remote == nullptr) {
800         NETMGR_LOG_E("Remote is null");
801         return NETMANAGER_ERR_LOCAL_PTR_NULL;
802     }
803 
804     int32_t retCode =
805         SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_NOTIFY_NETWORK_ACCESS_POLICY_DIAG), data,
806                     reply, option);
807     if (retCode != 0) {
808         NETMGR_LOG_E("proxy SendRequest failed, error code: [%{public}d]", retCode);
809         return retCode;
810     }
811 
812     return retCode;
813 }
814 
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)815 int32_t NetPolicyServiceProxy::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
816 {
817     MessageParcel data;
818     if (!WriteInterfaceToken(data)) {
819         return ERR_FLATTEN_OBJECT;
820     }
821     if (!data.WriteBool(status)) {
822         NETMGR_LOG_E("SetNicTrafficAllowed WriteBool func return error");
823         return ERR_FLATTEN_OBJECT;
824     }
825     if (!data.WriteInt32(ifaceNames.size())) {
826         NETMGR_LOG_E("SetNicTrafficAllowed ifaceNames size return error");
827         return ERR_FLATTEN_OBJECT;
828     }
829     for (const std::string& iter : ifaceNames) {
830         if (!data.WriteString(iter)) {
831             NETMGR_LOG_E("SetNicTrafficAllowed write name return error");
832             return ERR_FLATTEN_OBJECT;
833         }
834     }
835 
836     sptr<IRemoteObject> remote = Remote();
837     if (remote == nullptr) {
838         NETMGR_LOG_E("Remote is null");
839         return NETMANAGER_ERR_LOCAL_PTR_NULL;
840     }
841 
842     MessageParcel reply;
843     MessageOption option;
844     return SendRequest(remote, static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_NIC_TRAFFIC_ALLOWED),
845         data, reply, option);
846 }
847 } // namespace NetManagerStandard
848 } // namespace OHOS
849