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 #include "include/Dubai.h"
33 const int LARGE_BUFF_MAX_LEN = 256;
34 namespace OHOS {
35 namespace SmartPerf {
ItemData()36 std::map<std::string, std::string> Network::ItemData()
37 {
38 if (hapFlag) {
39 ThreadFunctions();
40 } else {
41 result = Network::GetNetworkInfo();
42 }
43 return result;
44 }
45
GetNetworkInfo()46 std::map<std::string, std::string> Network::GetNetworkInfo()
47 {
48 std::map<std::string, std::string> networkInfo;
49 networkInfo = GetNetworkInfoDev();
50 if (isFirst) {
51 networkInfo["networkUp"] = "0";
52 networkInfo["networkDown"] = "0";
53 isFirst = false;
54 diffRx = 0;
55 diffTx = 0;
56 return networkInfo;
57 }
58 networkInfo["networkUp"] = std::to_string(diffTx);
59 networkInfo["networkDown"] = std::to_string(diffRx);
60 if (!hapFlag || stophapFlag) {
61 diffTx = 0;
62 diffRx = 0;
63 }
64 return networkInfo;
65 }
GetNetworkInfoDev()66 std::map<std::string, std::string> Network::GetNetworkInfoDev()
67 {
68 std::map<std::string, std::string> networkInfo;
69 char buff[LARGE_BUFF_MAX_LEN];
70 FILE *fp = fopen("/proc/net/dev", "r");
71 if (fp == nullptr) {
72 std::cout << "net work node is not accessed" << std::endl;
73 return networkInfo;
74 }
75 while (fgets(buff, LARGE_BUFF_MAX_LEN, fp) != nullptr) {
76 if (strstr(buff, "rmnet0")) {
77 if (sscanf_s(buff, "%*s%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld",
78 &curRx, &curTx) < 0) {
79 (void)fclose(fp);
80 return networkInfo;
81 }
82 GetCurNetwork(rmnetCurRx, rmnetCurTx);
83 }
84 if (strstr(buff, "eth0")) {
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(ethCurRx, ethCurTx);
91 }
92 if (strstr(buff, "wlan0")) {
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(wlanCurRx, wlanCurTx);
99 }
100 }
101 (void)fclose(fp);
102 return networkInfo;
103 }
GetCurNetwork(long long & networkCurRx,long long & networkCurTx)104 void Network::GetCurNetwork(long long &networkCurRx, long long &networkCurTx)
105 {
106 if (curRx > 0) {
107 allRx = curRx - networkCurRx;
108 }
109 networkCurRx = curRx;
110 if (curTx > 0) {
111 allTx = curTx - networkCurTx;
112 }
113 networkCurTx = curTx;
114 if (allRx >= 0) {
115 diffRx += allRx;
116 } else {
117 diffRx += networkCurRx;
118 }
119 if (allTx >= 0) {
120 diffTx += allTx;
121 } else {
122 diffTx += networkCurTx;
123 }
124 curRx = 0;
125 curTx = 0;
126 allRx = 0;
127 allTx = 0;
128 }
IsFindHap()129 void Network::IsFindHap()
130 {
131 hapFlag = true;
132 ClearHapFlag();
133 }
IsStopFindHap()134 void Network::IsStopFindHap()
135 {
136 hapFlag = false;
137 stophapFlag = true;
138 }
139
ThreadFunctions()140 void Network::ThreadFunctions()
141 {
142 auto threadGetHapNetwork = std::thread([this]() { this->ThreadGetHapNetwork(); });
143 threadGetHapNetwork.detach();
144 }
145
ThreadGetHapNetwork()146 void Network::ThreadGetHapNetwork()
147 {
148 while (!stophapFlag) {
149 long long startTime = SPUtils::GetCurTime();
150 result = GetNetworkInfo();
151 std::string hapPid = "";
152 const std::string cmd = "pidof " + Dubai::dubaiPkgName;
153 SPUtils::LoadCmd(cmd, hapPid);
154 if (!hapPid.empty()) {
155 long long stopTime = SPUtils::GetCurTime();
156 long long time = 998;
157 long long costTime = stopTime - startTime;
158 std::this_thread::sleep_for(std::chrono::milliseconds(time - costTime));
159 } else {
160 break;
161 }
162 }
163 }
ClearHapFlag()164 void Network::ClearHapFlag()
165 {
166 isFirst = true;
167 stophapFlag = false;
168 rmnetCurRx = 0;
169 rmnetCurTx = 0;
170 ethCurRx = 0;
171 ethCurTx = 0;
172 wlanCurRx = 0;
173 wlanCurTx = 0;
174 }
175 }
176 }
177