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_STATS_INFO_H 17 #define NET_STATS_INFO_H 18 19 #include "parcel.h" 20 #include "net_stats_constants.h" 21 22 namespace OHOS { 23 namespace NetManagerStandard { 24 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default"))) 25 struct NET_SYMBOL_VISIBLE NetStatsInfo final : public Parcelable { 26 uint32_t uid_ = 0; 27 std::string iface_; 28 std::string ident_; 29 uint64_t date_ = 0; 30 uint64_t rxBytes_ = 0; 31 uint64_t txBytes_ = 0; 32 uint64_t rxPackets_ = 0; 33 uint64_t txPackets_ = 0; 34 uint32_t flag_ = STATS_DATA_FLAG_DEFAULT; 35 UidDatafinal36 inline const std::string UidData() const 37 { 38 return std::to_string(uid_) + "," + ident_ + "," + iface_ + "," + std::to_string(date_) + "," + 39 std::to_string(rxBytes_) + "," + std::to_string(rxPackets_) + "," + std::to_string(txBytes_) + "," + 40 std::to_string(txPackets_) + "," + std::to_string(flag_); 41 } 42 IfaceDatafinal43 inline const std::string IfaceData() const 44 { 45 return "'" + iface_ + "'," + std::to_string(date_) + "," + std::to_string(rxBytes_) + "," + 46 std::to_string(rxPackets_) + "," + std::to_string(txBytes_) + "," + std::to_string(txPackets_); 47 } 48 GetStatsfinal49 inline uint64_t GetStats() const 50 { 51 return rxBytes_ + txBytes_; 52 } 53 Equalsfinal54 inline bool Equals(const NetStatsInfo &info) const 55 { 56 return info.uid_ == uid_ && info.iface_ == iface_; 57 } 58 HasNoDatafinal59 inline bool HasNoData() const 60 { 61 return rxBytes_ == 0 && rxPackets_ == 0 && txBytes_ == 0 && txPackets_ == 0; 62 } 63 64 const NetStatsInfo operator-(const NetStatsInfo &other) const 65 { 66 NetStatsInfo info; 67 info.uid_ = other.uid_; 68 info.iface_ = other.iface_; 69 info.ident_ = ident_; 70 info.rxPackets_ = (rxPackets_ > other.rxPackets_) ? rxPackets_ - other.rxPackets_ : 0; 71 info.rxBytes_ = (rxBytes_ > other.rxBytes_) ? rxBytes_ - other.rxBytes_ : 0; 72 info.txPackets_ = (txPackets_ > other.txPackets_) ? txPackets_ - other.txPackets_ : 0; 73 info.txBytes_ = (txBytes_ > other.txBytes_) ? txBytes_ - other.txBytes_ : 0; 74 return info; 75 } 76 77 NetStatsInfo &operator+=(const NetStatsInfo &other) 78 { 79 rxPackets_ += other.rxPackets_; 80 rxBytes_ += other.rxBytes_; 81 txPackets_ += other.txPackets_; 82 txBytes_ += other.txBytes_; 83 return *this; 84 } 85 86 bool Marshalling(Parcel &parcel) const override; 87 static bool Marshalling(Parcel &parcel, const NetStatsInfo &stats); 88 static bool Marshalling(Parcel &parcel, const std::vector<NetStatsInfo> &statsInfos); 89 static bool Marshalling(Parcel &parcel, const std::unordered_map<uint32_t, NetStatsInfo> &statsInfos); 90 static bool Unmarshalling(Parcel &parcel, NetStatsInfo &stats); 91 static bool Unmarshalling(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos); 92 static bool Unmarshalling(Parcel &parcel, std::unordered_map<uint32_t, NetStatsInfo> &statsInfos); 93 }; 94 } // namespace NetManagerStandard 95 } // namespace OHOS 96 #endif // NET_STATS_INFO_H 97