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 21 namespace OHOS { 22 namespace NetManagerStandard { 23 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default"))) 24 struct NET_SYMBOL_VISIBLE NetStatsInfo final : public Parcelable { 25 uint32_t uid_ = 0; 26 std::string iface_; 27 std::string ident_; 28 uint64_t date_ = 0; 29 uint64_t rxBytes_ = 0; 30 uint64_t txBytes_ = 0; 31 uint64_t rxPackets_ = 0; 32 uint64_t txPackets_ = 0; 33 uint32_t flag_ = 0; 34 UidDatafinal35 inline const std::string UidData() const 36 { 37 return std::to_string(uid_) + "," + ident_ + "," + iface_ + "," + std::to_string(date_) + "," + 38 std::to_string(rxBytes_) + "," + std::to_string(rxPackets_) + "," + std::to_string(txBytes_) + "," + 39 std::to_string(txPackets_) + "," + std::to_string(flag_); 40 } 41 IfaceDatafinal42 inline const std::string IfaceData() const 43 { 44 return "'" + iface_ + "'," + std::to_string(date_) + "," + std::to_string(rxBytes_) + "," + 45 std::to_string(rxPackets_) + "," + std::to_string(txBytes_) + "," + std::to_string(txPackets_); 46 } 47 GetStatsfinal48 inline uint64_t GetStats() const 49 { 50 return rxBytes_ + txBytes_; 51 } 52 Equalsfinal53 inline bool Equals(const NetStatsInfo &info) const 54 { 55 return info.uid_ == uid_ && info.iface_ == iface_; 56 } 57 HasNoDatafinal58 inline bool HasNoData() const 59 { 60 return rxBytes_ == 0 && rxPackets_ == 0 && txBytes_ == 0 && txPackets_ == 0; 61 } 62 63 const NetStatsInfo operator-(const NetStatsInfo &other) const 64 { 65 NetStatsInfo info; 66 info.uid_ = other.uid_; 67 info.iface_ = other.iface_; 68 info.ident_ = ident_; 69 info.rxPackets_ = (rxPackets_ > other.rxPackets_) ? rxPackets_ - other.rxPackets_ : 0; 70 info.rxBytes_ = (rxBytes_ > other.rxBytes_) ? rxBytes_ - other.rxBytes_ : 0; 71 info.txPackets_ = (txPackets_ > other.txPackets_) ? txPackets_ - other.txPackets_ : 0; 72 info.txBytes_ = (txBytes_ > other.txBytes_) ? txBytes_ - other.txBytes_ : 0; 73 info.flag_ = flag_; 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