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 int32_t userId_ = 0; 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_) + "," + std::to_string(userId_); 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 info.flag_ = flag_; 75 info.userId_ = userId_; 76 return info; 77 } 78 79 NetStatsInfo &operator+=(const NetStatsInfo &other) 80 { 81 rxPackets_ += other.rxPackets_; 82 rxBytes_ += other.rxBytes_; 83 txPackets_ += other.txPackets_; 84 txBytes_ += other.txBytes_; 85 return *this; 86 } 87 88 bool Marshalling(Parcel &parcel) const override; 89 static bool Marshalling(Parcel &parcel, const NetStatsInfo &stats); 90 static bool Marshalling(Parcel &parcel, const std::vector<NetStatsInfo> &statsInfos); 91 static bool Marshalling(Parcel &parcel, const std::unordered_map<uint32_t, NetStatsInfo> &statsInfos); 92 static NetStatsInfo* Unmarshalling(Parcel &parcel); 93 static bool Unmarshalling(Parcel &parcel, NetStatsInfo &stats); 94 static bool Unmarshalling(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos); 95 static bool Unmarshalling(Parcel &parcel, std::unordered_map<uint32_t, NetStatsInfo> &statsInfos); 96 }; 97 } // namespace NetManagerStandard 98 } // namespace OHOS 99 #endif // NET_STATS_INFO_H 100