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 <climits>
25#include <cctype>
26#include "sys/time.h"
27#include "securec.h"
28#include "include/sp_utils.h"
29#include "include/sp_log.h"
30const int LARGE_BUFF_MAX_LEN = 256;
31namespace OHOS {
32namespace SmartPerf {
33std::map<std::string, std::string> Network::ItemData()
34{
35    std::map<std::string, std::string> result = Network::GetNetworkInfo();
36    LOGI("Network ItemData map siez=%u", result.size());
37    if (result.find("networkUp") != result.end() && result["networkUp"].empty()) {
38        result["networkUp"] = "NA";
39    }
40    if (result.find("networkDown") != result.end() && result["networkDown"].empty()) {
41        result["networkDown"] = "NA";
42    }
43    return result;
44}
45
46std::map<std::string, std::string> Network::GetNetworkInfo()
47{
48    std::map<std::string, std::string> networkInfo;
49    char buff[LARGE_BUFF_MAX_LEN];
50    FILE *fp = fopen("/proc/net/dev", "r");
51    if (fp == nullptr) {
52        std::cout << "net work node is not accessed" << std::endl;
53        return networkInfo;
54    }
55    while (fgets(buff, LARGE_BUFF_MAX_LEN, fp) != nullptr) {
56        if (strstr(buff, "rmnet") || strstr(buff, "eth") || strstr(buff, "wlan")) {
57            if (sscanf_s(buff, "%*s%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld%lld%*lld%*lld%*lld%*lld%*lld%*lld%*lld",
58                &curRx, &curTx) < 0) {
59                (void)fclose(fp);
60                return networkInfo;
61            }
62            allTx += curTx;
63            allRx += curRx;
64        }
65    }
66    (void)fclose(fp);
67    if (isFirst) {
68        networkInfo["networkUp"] = std::to_string(prevTx);
69        networkInfo["networkDown"] = std::to_string(prevRx);
70        isFirst = false;
71        prevTx = allTx;
72        prevRx = allRx;
73        allTx = 0;
74        allRx = 0;
75        return networkInfo;
76    }
77    if ((allTx == 0 && allRx == 0) || (allTx <= prevTx && allRx <= prevRx)) {
78        networkInfo["networkUp"] = "0";
79        networkInfo["networkDown"] = "0";
80        prevTx = allTx;
81        prevRx = allRx;
82        allTx = 0;
83        allRx = 0;
84        return networkInfo;
85    }
86    diffTx = allTx - prevTx;
87    prevTx = allTx;
88    diffRx = allRx - prevRx;
89    prevRx = allRx;
90    allTx = 0;
91    allRx = 0;
92    networkInfo["networkUp"] = std::to_string(diffTx);
93    networkInfo["networkDown"] = std::to_string(diffRx);
94    return networkInfo;
95}
96}
97}
98