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