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     } else {
39         if (!parcel.WriteUint32(policies.uid_policies.size())) {
40             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
41         }
42 
43         for (const auto &policies : policies.uid_policies) {
44             if (!parcel.WriteInt32(policies.first)) {
45                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
46             }
47             if (!parcel.WriteBool(policies.second.wifiAllow)) {
48                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
49             }
50             if (!parcel.WriteBool(policies.second.cellularAllow)) {
51                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
52             }
53         }
54     }
55 
56     return NETMANAGER_SUCCESS;
57 }
58 
Unmarshalling(Parcel & parcel,AccessPolicySave & policies,bool flag)59 int32_t NetworkAccessPolicy::Unmarshalling(Parcel &parcel, AccessPolicySave& policies, bool flag)
60 {
61     if (flag) {
62         NetworkAccessPolicy accessPolicyTmp;
63         if (!parcel.ReadBool(accessPolicyTmp.wifiAllow)) {
64             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
65         }
66 
67         if (!parcel.ReadBool(accessPolicyTmp.cellularAllow)) {
68             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
69         }
70         policies.policy = accessPolicyTmp;
71     } else {
72         uint32_t size = 0;
73         if (!parcel.ReadUint32(size)) {
74             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
75         }
76 
77         for (uint32_t i = 0; i < size; ++i) {
78             NetworkAccessPolicy tmp_policy;
79             uint32_t uid;
80             if (!parcel.ReadUint32(uid)) {
81                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
82             }
83             if (!parcel.ReadBool(tmp_policy.wifiAllow)) {
84                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
85             }
86 
87             if (!parcel.ReadBool(tmp_policy.cellularAllow)) {
88                 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
89             }
90             policies.uid_policies.insert(std::pair<int32_t, NetworkAccessPolicy>(uid, tmp_policy));
91         }
92     }
93 
94     return NETMANAGER_SUCCESS;
95 }
96 } // namespace NetManagerStandard
97 } // namespace OHOS
98