• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 NETWORK_PLUGIN_H
17 #define NETWORK_PLUGIN_H
18 
19 #include <algorithm>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <iomanip>
24 #include <string>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <unordered_map>
29 #include <utility>
30 #include <sstream>
31 #include <iostream>
32 #include <fstream>
33 
34 #include <time.h>
35 
36 #include "logging.h"
37 #include "network_plugin_config.pb.h"
38 #include "network_plugin_result.pb.h"
39 
40 struct NetDetails {
41     uint64_t tx;
42     uint64_t rx;
43     std::string type;
44 };
45 
46 struct NetworkCell {
47     int32_t pid;
48     uint64_t tx;
49     uint64_t rx;
50     struct timespec ts;
51     std::vector<NetDetails> details;
52 };
53 
54 enum NetworkNum {
55     IFACE_INDEX = 2,
56     UID_INDEX = 4,
57     RX_INDEX = 6,
58     TX_INDEX = 8,
59     DEC_BASE = 10,
60 };
61 
62 class NetworkPlugin {
63 public:
64     NetworkPlugin();
~NetworkPlugin()65     ~NetworkPlugin() {};
66     int Start(const uint8_t* configData, uint32_t configSize);
67     int Report(uint8_t* configData, uint32_t configSize);
68     int Stop();
69 protected:
70     std::string GetRateNodePath();
71     int32_t GetUid(int32_t pid);
72     bool ReadTxRxBytes(int32_t pid, NetworkCell &cell);
73     void AddNetDetails(NetworkCell& cell, NetDetails& data);
74     // for UT
setPathForTest(std::string path)75     void setPathForTest(std::string path)
76     {
77         fileForTest_ = path;
78     }
getPathForTest()79     std::string getPathForTest()
80     {
81         return fileForTest_;
82     }
83 
84 private:
85     NetworkConfig protoConfig_;
86     std::unique_ptr<uint8_t[]> buffer_;
87     std::unique_ptr<FILE, int (*)(FILE*)> fp_;
88     std::unordered_map<int32_t, int32_t> pidUid_;
89     std::string fileForTest_;
90 };
91 #endif