• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef NETWORKSHARE_TRAFFICLIMIT_H
16 #define NETWORKSHARE_TRAFFICLIMIT_H
17 
18 #include "ffrt.h"
19 #include "singleton.h"
20 #include "data_ability_observer_stub.h"
21 #include "network_sharing.h"
22 #include "network_state.h"
23 #include "event_handler.h"
24 #include "event_runner.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 
29 constexpr int64_t NO_LIMIT = -1;
30 constexpr int64_t DEFAULT_INTERVAL_MINIMUM = 100;
31 constexpr int64_t STATS_INTERVAL_MINIMUM = 500;
32 constexpr int64_t STATS_INTERVAL_MAXIMUM = 30000;
33 constexpr int64_t STATS_INTERVAL_DEFAULT = 5000;
34 constexpr int64_t WRITE_DB_INTERVAL_MINIMUM = 2000;
35 constexpr int64_t KB_IN_BYTES = 1024;
36 constexpr int64_t MB_IN_BYTES = KB_IN_BYTES * 1024;
37 constexpr int64_t WIFI_AP_STATS_DEFAULT_VALUE = 0;
38 
39 struct TetherTrafficInfos {
40     int64_t mStartSize = 0;
41     int64_t mLastStatsMills = 0;
42     int64_t mLastStatsSize = 0;
43     int64_t mLimitSize = NO_LIMIT;
44     int64_t mRemainSize = NO_LIMIT;
45     int64_t mMaxSpeed = 0;
46     int64_t mNetSpeed = 0;
47     int64_t SharingTrafficValue = 0;
48 };
49 
50 enum class NetworkSpeed {
51     NETWORK_SPEED_2G = 100 * KB_IN_BYTES,
52     NETWORK_SPEED_3G = 10 * MB_IN_BYTES,
53     NETWORK_SPEED_4G = 100 * MB_IN_BYTES,
54 };
55 
56 class TrafficEventHandler;
57 class NetworkShareTrafficLimit {
58 public:
59     NetworkShareTrafficLimit();
60     ~NetworkShareTrafficLimit() = default;
61     static NetworkShareTrafficLimit &GetInstance(void);
62     void InitTetherStatsInfo();
63     void UpdataSharingSettingdata(int64_t &tetherInt);
64     void SaveSharingTrafficToCachedData(nmd::NetworkSharingTraffic &traffic);
65     int64_t GetMaxNetworkSpeed();
66     void CheckSharingStatsData();
67     int64_t GetNextUpdataDelay();
68     void StartHandleSharingLimitEvent();
69     void EndHandleSharingLimitEvent();
70     void AddSharingTrafficBeforeConnChanged(nmd::NetworkSharingTraffic &traffic);
71     bool IsCellularDataConnection();
72     void SaveSharingTrafficToSettingsDB(nmd::NetworkSharingTraffic &traffic);
73     std::shared_ptr<TrafficEventHandler> eventHandler_ = nullptr;
74 
75 private:
76     void SendSharingTrafficToCachedData(const nmd::NetworkSharingTraffic &traffic,  const std::string &upIface);
77     void UpdataSharingTrafficStats();
78     int64_t GetNetSpeedForRadioTech(int32_t radioTech);
79     void SetTetherLimit(int64_t &tetherInt);
80     void sendMsgDelayed(const std::string &name, int64_t delayTime);
81     int32_t GetDefaultSlotId();
82     bool IsValidSlotId(int32_t slotId);
83     void WriteSharingTrafficToDB(const int64_t &traffic);
84     void InitEventHandler();
85 
86 private:
87     TetherTrafficInfos tetherTrafficInfos;
88     std::unique_ptr<Telephony::NetworkState> networkState_ = nullptr;
89     int64_t lastSharingStatsSize = 0;
90     int64_t tmpMills = 0;
91     ffrt::mutex lock_;
92 };
93 
94 class TetherSingleValueObserver : public AAFwk::DataAbilityObserverStub {
95 public:
96     TetherSingleValueObserver() = default;
97     ~TetherSingleValueObserver() = default;
98     void OnChange() override;
99 };
100 
101 class SharingTrafficDataObserver {
102 public:
103     SharingTrafficDataObserver();
104     ~SharingTrafficDataObserver() = default;
105     void RegisterTetherDataSettingObserver();
106     void UnregisterTetherDataSettingObserver();
107     void ReadTetherTrafficSetting();
108 
109 public:
110     sptr<TetherSingleValueObserver> mTetherSingleValueObserver_ = nullptr;
111 };
112 
113 class TrafficEventHandler : public AppExecFwk::EventHandler {
114 public:
115     explicit TrafficEventHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
116     ~TrafficEventHandler() override;
117     bool HandlePostTask(const Callback &callback, int64_t delayTime = 0);
118     bool HandlePostTask(const Callback &callback, const std::string &name = std::string(), int64_t delayTime = 0);
119     void HandleRemoveTask(const std::string &name);
120 };
121 
GetCurrentMilliseconds()122 inline int64_t GetCurrentMilliseconds()
123 {
124     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
125         .count();
126 }
127 
128 } // namespace NetManagerStandard
129 } // namespace OHOS
130 
131 #endif
132