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 uint64_t date_ = 0; 28 uint64_t rxBytes_ = 0; 29 uint64_t txBytes_ = 0; 30 uint64_t rxPackets_ = 0; 31 uint64_t txPackets_ = 0; 32 UidDatafinal33 inline const std::string UidData() const 34 { 35 return std::to_string(uid_) + ",'" + iface_ + "'," + std::to_string(date_) + "," + std::to_string(rxBytes_) + 36 "," + std::to_string(rxPackets_) + "," + std::to_string(txBytes_) + "," + std::to_string(txPackets_); 37 } 38 IfaceDatafinal39 inline const std::string IfaceData() const 40 { 41 return "'" + iface_ + "'," + std::to_string(date_) + "," + std::to_string(rxBytes_) + "," + 42 std::to_string(rxPackets_) + "," + std::to_string(txBytes_) + "," + std::to_string(txPackets_); 43 } 44 GetStatsfinal45 inline uint64_t GetStats() const 46 { 47 return rxBytes_ + txBytes_; 48 } 49 Equalsfinal50 inline bool Equals(const NetStatsInfo &info) const 51 { 52 return info.uid_ == uid_ && info.iface_ == iface_; 53 } 54 HasNoDatafinal55 inline bool HasNoData() const 56 { 57 return rxBytes_ == 0 && rxPackets_ == 0 && txBytes_ == 0 && txPackets_ == 0; 58 } 59 60 const NetStatsInfo operator-(const NetStatsInfo &other) const 61 { 62 NetStatsInfo info; 63 info.uid_ = other.uid_; 64 info.iface_ = other.iface_; 65 info.rxPackets_ = (rxPackets_ > other.rxPackets_) ? rxPackets_ - other.rxPackets_ : 0; 66 info.rxBytes_ = (rxBytes_ > other.rxBytes_) ? rxBytes_ - other.rxBytes_ : 0; 67 info.txPackets_ = (txPackets_ > other.txPackets_) ? txPackets_ - other.txPackets_ : 0; 68 info.txBytes_ = (txBytes_ > other.txBytes_) ? txBytes_ - other.txBytes_ : 0; 69 return info; 70 } 71 72 NetStatsInfo &operator+=(const NetStatsInfo &other) 73 { 74 rxPackets_ += other.rxPackets_; 75 rxBytes_ += other.rxBytes_; 76 txPackets_ += other.txPackets_; 77 txBytes_ += other.txBytes_; 78 return *this; 79 } 80 81 bool Marshalling(Parcel &parcel) const override; 82 static bool Marshalling(Parcel &parcel, const NetStatsInfo &stats); 83 static bool Marshalling(Parcel &parcel, const std::vector<NetStatsInfo> &statsInfos); 84 static bool Unmarshalling(Parcel &parcel, NetStatsInfo &stats); 85 static bool Unmarshalling(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos); 86 }; 87 } // namespace NetManagerStandard 88 } // namespace OHOS 89 #endif // NET_STATS_INFO_H 90