1
2 /*
3 * Copyright (c) 2024 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "net_access_policy.h"
18
19 #include <ctime>
20
21 #include "parcel.h"
22 #include "net_mgr_log_wrapper.h"
23 #include "netmanager_base_common_utils.h"
24 #include "net_manager_constants.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
Marshalling(Parcel & parcel,AccessPolicySave & policies,bool flag)28 int32_t NetworkAccessPolicy::Marshalling(Parcel &parcel, AccessPolicySave& policies, bool flag)
29 {
30 if (flag) {
31 if (!parcel.WriteBool(policies.policy.wifiAllow)) {
32 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
33 }
34
35 if (!parcel.WriteBool(policies.policy.cellularAllow)) {
36 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
37 }
38
39 if (!parcel.WriteBool(policies.policy.wifiSwitchDisable)) {
40 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
41 }
42
43 if (!parcel.WriteBool(policies.policy.cellularSwitchDisable)) {
44 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
45 }
46 } else {
47 if (!parcel.WriteUint32(policies.uid_policies.size())) {
48 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
49 }
50
51 for (const auto &policies : policies.uid_policies) {
52 if (!parcel.WriteInt32(policies.first)) {
53 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
54 }
55 if (!parcel.WriteBool(policies.second.wifiAllow)) {
56 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
57 }
58 if (!parcel.WriteBool(policies.second.cellularAllow)) {
59 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
60 }
61 if (!parcel.WriteBool(policies.second.wifiSwitchDisable)) {
62 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
63 }
64 if (!parcel.WriteBool(policies.second.cellularSwitchDisable)) {
65 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
66 }
67 }
68 }
69
70 return NETMANAGER_SUCCESS;
71 }
72
Unmarshalling(Parcel & parcel,AccessPolicySave & policies,bool flag)73 int32_t NetworkAccessPolicy::Unmarshalling(Parcel &parcel, AccessPolicySave& policies, bool flag)
74 {
75 if (flag) {
76 NetworkAccessPolicy accessPolicyTmp;
77 if (!parcel.ReadBool(accessPolicyTmp.wifiAllow)) {
78 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
79 }
80
81 if (!parcel.ReadBool(accessPolicyTmp.cellularAllow)) {
82 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
83 }
84
85 if (!parcel.ReadBool(accessPolicyTmp.wifiSwitchDisable)) {
86 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
87 }
88
89 if (!parcel.ReadBool(accessPolicyTmp.cellularSwitchDisable)) {
90 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
91 }
92 policies.policy = accessPolicyTmp;
93 } else {
94 uint32_t size = 0;
95 if (!parcel.ReadUint32(size)) {
96 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
97 }
98
99 for (uint32_t i = 0; i < size; ++i) {
100 NetworkAccessPolicy tmp_policy;
101 uint32_t uid;
102 if (!parcel.ReadUint32(uid)) {
103 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
104 }
105 if (!parcel.ReadBool(tmp_policy.wifiAllow)) {
106 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
107 }
108
109 if (!parcel.ReadBool(tmp_policy.cellularAllow)) {
110 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
111 }
112 if (!parcel.ReadBool(tmp_policy.wifiSwitchDisable)) {
113 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
114 }
115
116 if (!parcel.ReadBool(tmp_policy.cellularSwitchDisable)) {
117 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
118 }
119 policies.uid_policies.insert(std::pair<int32_t, NetworkAccessPolicy>(uid, tmp_policy));
120 }
121 }
122
123 return NETMANAGER_SUCCESS;
124 }
125 } // namespace NetManagerStandard
126 } // namespace OHOS
127