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 #ifndef NET_POLICY_QUOTA_POLICY_H
17 #define NET_POLICY_QUOTA_POLICY_H
18 
19 #include <string>
20 
21 #include "parcel.h"
22 
23 #include "net_all_capabilities.h"
24 #include "net_policy_constants.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 static constexpr const char *PERIOD_DAY = "D";
29 static constexpr const char *PERIOD_MONTH = "M";
30 static constexpr const char *PERIOD_YEAR = "Y";
31 static constexpr int32_t PERIOD_START = 1;
32 static constexpr int32_t DAY_MAX = 31;
33 static constexpr int32_t MONTH_MAX = 12;
34 static constexpr int32_t YEAR_MAX = 366;
35 static constexpr int32_t PERIOD_DURATION_SIZE = 2;
36 static constexpr int32_t QUOTA_POLICY_MAX_SIZE = 100;
37 
38 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default")))
39 struct NET_SYMBOL_VISIBLE NetQuotaPolicy final : public Parcelable {
40     struct NetLogotype {
41         /* See {@link NetBearType} */
42         int32_t netType = BEARER_DEFAULT;
43         /* The ID of the target card, valid when netType is BEARER_CELLULAR */
44         std::string simId;
45         /* To specify the identity of network, such as different WLAN */
46         std::string ident;
47     } networkmatchrule;
48     struct QuotaPolicy {
49         // @deprecated
50         int64_t periodStartTime = -1;
51         /* The period and the start time for quota policy, default: "M1" */
52         std::string periodDuration = (PERIOD_MONTH + std::to_string(PERIOD_START));
53         // @deprecated
54         std::string title;
55         // @deprecated
56         std::string summary;
57         /* The warning threshold of traffic, default:  DATA_USAGE_UNKNOWN */
58         int64_t warningBytes = DATA_USAGE_UNKNOWN;
59         /* The limit threshold of traffic, default: DATA_USAGE_UNKNOWN */
60         int64_t limitBytes = DATA_USAGE_UNKNOWN;
61         /* The updated wall time that last warning remind, default: REMIND_NEVER */
62         int64_t lastWarningRemind = REMIND_NEVER;
63         /* The updated wall time that last limit remind, default: REMIND_NEVER */
64         int64_t lastLimitRemind = REMIND_NEVER;
65         /* Is metered network or not */
66         bool metered = false;
67         // @deprecated
68         int32_t source = -1;
69         /* The action while the used bytes reach the limit, see {@link LimitAction} */
70         int32_t limitAction = LimitAction::LIMIT_ACTION_NONE;
71         // @deprecated
72         int64_t usedBytes = -1;
73         // @deprecated
74         int64_t usedTimeDuration = -1;
75         // @deprecated
76         std::string possessor;
77     } quotapolicy;
78 
79     bool Marshalling(Parcel &parcel) const override;
80     static bool Marshalling(Parcel &parcel, const NetQuotaPolicy &quotaPolicy);
81     static bool Marshalling(Parcel &parcel, const std::vector<NetQuotaPolicy> &quotaPolicies);
82     static bool Unmarshalling(Parcel &parcel, NetQuotaPolicy &quotaPolicy);
83     static bool Unmarshalling(Parcel &parcel, std::vector<NetQuotaPolicy> &quotaPolicies);
84     /**
85      * Get the period start, transform the periodDuration to wall time.
86      *
87      * @return int64_t The wall time.of the period start.
88      */
89     int64_t GetPeriodStart();
90 
91     /**
92      * To judge the quota is over the warning threshold.
93      *
94      * @param totalQuota The total quota used.
95      * @return true Over the warning threshold.
96      * @return false
97      */
98     bool IsOverWarning(int64_t totalQuota) const;
99 
100     /**
101      * To judge the quota is over the limit threshold.
102      *
103      * @param totalQuota The total quota used.
104      * @return true Over the limit threshold.
105      * @return false Not over.
106      */
107     bool IsOverLimit(int64_t totalQuota) const;
108 
109     /**
110      * Reset the quota policy to default.
111      *
112      */
113     void Reset();
114 };
115 } // namespace NetManagerStandard
116 } // namespace OHOS
117 #endif // NET_POLICY_QUOTA_POLICY_H
118