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 #include "data_flow_statistics.h"
17
18 #include "netsys_controller.h"
19 #include "net_mgr_log_wrapper.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
GetCellularRxBytes()23 int64_t DataFlowStatistics::GetCellularRxBytes()
24 {
25 return NetsysController::GetInstance().GetCellularRxBytes();
26 }
27
GetCellularTxBytes()28 int64_t DataFlowStatistics::GetCellularTxBytes()
29 {
30 return NetsysController::GetInstance().GetCellularTxBytes();
31 }
32
GetAllRxBytes()33 int64_t DataFlowStatistics::GetAllRxBytes()
34 {
35 return NetsysController::GetInstance().GetAllRxBytes();
36 }
37
GetAllTxBytes()38 int64_t DataFlowStatistics::GetAllTxBytes()
39 {
40 return NetsysController::GetInstance().GetAllTxBytes();
41 }
42
GetUidRxBytes(uint32_t uid)43 int64_t DataFlowStatistics::GetUidRxBytes(uint32_t uid)
44 {
45 return NetsysController::GetInstance().GetUidRxBytes(uid);
46 }
47
GetUidTxBytes(uint32_t uid)48 int64_t DataFlowStatistics::GetUidTxBytes(uint32_t uid)
49 {
50 return NetsysController::GetInstance().GetUidTxBytes(uid);
51 }
52
GetIfaceRxBytes(const std::string & interfaceName)53 int64_t DataFlowStatistics::GetIfaceRxBytes(const std::string &interfaceName)
54 {
55 uint64_t rxBytes = 0;
56 int32_t result = NetsysController::GetInstance().GetIfaceStats(
57 rxBytes, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_BYTES), interfaceName);
58 if (result != 0) {
59 NETMGR_LOG_E("Failed to get %{public}s RX bytes, result: %{public}d", interfaceName.c_str(), result);
60 return -1;
61 }
62 return static_cast<int64_t>(rxBytes);
63 }
64
GetIfaceTxBytes(const std::string & interfaceName)65 int64_t DataFlowStatistics::GetIfaceTxBytes(const std::string &interfaceName)
66 {
67 uint64_t txBytes = 0;
68 int32_t result = NetsysController::GetInstance().GetIfaceStats(
69 txBytes, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_BYTES), interfaceName);
70 if (result != 0) {
71 NETMGR_LOG_E("Failed to get %{public}s TX bytes, result: %{public}d", interfaceName.c_str(), result);
72 return -1;
73 }
74 return static_cast<int64_t>(txBytes);
75 }
76
GetIfaceRxPackets(const std::string & interfaceName)77 int64_t DataFlowStatistics::GetIfaceRxPackets(const std::string &interfaceName)
78 {
79 uint64_t rxPackets = 0;
80 int32_t result = NetsysController::GetInstance().GetIfaceStats(
81 rxPackets, static_cast<uint32_t>(StatsType::STATS_TYPE_RX_PACKETS), interfaceName);
82 if (result != 0) {
83 NETMGR_LOG_E("Failed to get %{public}s RX packets, result: %{public}d", interfaceName.c_str(), result);
84 return -1;
85 }
86 return static_cast<int64_t>(rxPackets);
87 }
88
GetIfaceTxPackets(const std::string & interfaceName)89 int64_t DataFlowStatistics::GetIfaceTxPackets(const std::string &interfaceName)
90 {
91 uint64_t txPackets = 0;
92 int32_t result = NetsysController::GetInstance().GetIfaceStats(
93 txPackets, static_cast<uint32_t>(StatsType::STATS_TYPE_TX_PACKETS), interfaceName);
94 if (result != 0) {
95 NETMGR_LOG_E("Failed to get %{public}s TX packets, result: %{public}d", interfaceName.c_str(), result);
96 return -1;
97 }
98 return static_cast<int64_t>(txPackets);
99 }
100 } // namespace NetManagerStandard
101 } // namespace OHOS
102