• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 #include "include/Network.h"
16 #include <sstream>
17 #include <fstream>
18 #include <iostream>
19 #include <string>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <memory>
25 #include <climits>
26 #include <cctype>
27 #include <thread>
28 #include "sys/time.h"
29 #include "securec.h"
30 #include "include/sp_utils.h"
31 #include "include/sp_log.h"
32 const int LARGE_BUFF_MAX_LEN = 256;
33 namespace OHOS {
34 namespace SmartPerf {
ItemData()35 std::map<std::string, std::string> Network::ItemData()
36 {
37     if (hapFlag) {
38         ThreadFunctions();
39         return result;
40     } else {
41         result = GetNetworkInfo();
42         return result;
43     }
44     if (result.find("networkUp") != result.end() && result["networkUp"].empty()) {
45         result["networkUp"] = "NA";
46     }
47     if (result.find("networkDown") != result.end() && result["networkDown"].empty()) {
48         result["networkDown"] = "NA";
49     }
50     return result;
51 }
52 
GetNetworkInfo()53 std::map<std::string, std::string> Network::GetNetworkInfo()
54 {
55     std::map<std::string, std::string> networkInfo;
56     networkInfo = GetNetworkInfoDev();
57     if (isFirst) {
58         networkInfo["networkUp"] = "0";
59         networkInfo["networkDown"] = "0";
60         isFirst = false;
61         diffRx = 0;
62         diffTx = 0;
63         return networkInfo;
64     }
65     networkInfo["networkUp"] = std::to_string(diffTx);
66     networkInfo["networkDown"] = std::to_string(diffRx);
67     LOGD("networkUp: %s, networkDown: %s", networkInfo["networkUp"].c_str(), networkInfo["networkDown"].c_str());
68     if (!hapFlag || stophapFlag) {
69         diffTx = 0;
70         diffRx = 0;
71     }
72     return networkInfo;
73 }
GetNetworkInfoDev()74 std::map<std::string, std::string> Network::GetNetworkInfoDev()
75 {
76     std::map<std::string, std::string> networkInfo;
77     char buff[LARGE_BUFF_MAX_LEN];
78     FILE *fp = fopen("/proc/net/dev", "r");
79     if (fp == nullptr) {
80         std::cout << "net work node is not accessed" << std::endl;
81         return networkInfo;
82     }
83     while (fgets(buff, LARGE_BUFF_MAX_LEN, fp) != nullptr) {
84         if (strstr(buff, "rmnet0")) {
85             if (sscanf_s(buff, "%*s%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld",
86                 &curRx, &curTx) < 0) {
87                 (void)fclose(fp);
88                 return networkInfo;
89             }
90             GetCurNetwork(rmnetCurRx, rmnetCurTx);
91         }
92         if (strstr(buff, "eth0")) {
93             if (sscanf_s(buff, "%*s%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld",
94                 &curRx, &curTx) < 0) {
95                 (void)fclose(fp);
96                 return networkInfo;
97             }
98             GetCurNetwork(ethCurRx, ethCurTx);
99         }
100         if (strstr(buff, "wlan0")) {
101             if (sscanf_s(buff, "%*s%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld",
102                 &curRx, &curTx) < 0) {
103                 (void)fclose(fp);
104                 return networkInfo;
105             }
106             GetCurNetwork(wlanCurRx, wlanCurTx);
107         }
108     }
109     (void)fclose(fp);
110     return networkInfo;
111 }
GetCurNetwork(long long & networkCurRx,long long & networkCurTx)112 void Network::GetCurNetwork(long long &networkCurRx, long long &networkCurTx)
113 {
114     if (curRx > 0) {
115         allRx = curRx - networkCurRx;
116     }
117     networkCurRx = curRx;
118     if (curTx > 0) {
119         allTx = curTx - networkCurTx;
120     }
121     networkCurTx = curTx;
122     if (allRx >= 0) {
123         diffRx += allRx;
124     } else {
125         diffRx += networkCurRx;
126     }
127     if (allTx >= 0) {
128         diffTx += allTx;
129     } else {
130         diffTx += networkCurTx;
131     }
132     curRx = 0;
133     curTx = 0;
134     allRx = 0;
135     allTx = 0;
136 }
IsFindHap()137 void Network::IsFindHap()
138 {
139     hapFlag = true;
140     ClearHapFlag();
141 }
IsStopFindHap()142 void Network::IsStopFindHap()
143 {
144     hapFlag = false;
145     stophapFlag = true;
146 }
147 
ThreadFunctions()148 void Network::ThreadFunctions()
149 {
150     auto threadGetHapNetwork = std::thread([this]() { this->ThreadGetHapNetwork(); });
151     threadGetHapNetwork.detach();
152 }
153 
ThreadGetHapNetwork()154 void Network::ThreadGetHapNetwork()
155 {
156     while (!stophapFlag) {
157         long long startTime = SPUtils::GetCurTime();
158         result = GetNetworkInfo();
159         long long stopTime = SPUtils::GetCurTime();
160         long long time = 998;
161         long long costTime = stopTime - startTime;
162         std::this_thread::sleep_for(std::chrono::milliseconds(time - costTime));
163     }
164 }
ClearHapFlag()165 void Network::ClearHapFlag()
166 {
167     isFirst = true;
168     stophapFlag = false;
169     rmnetCurRx = 0;
170     rmnetCurTx = 0;
171     ethCurRx = 0;
172     ethCurTx = 0;
173     wlanCurRx = 0;
174     wlanCurTx = 0;
175 }
176 }
177 }
178