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 #ifndef SDK_DATA_RECV_H
16 #define SDK_DATA_RECV_H
17 #include <pthread.h>
18 #include <vector>
19 #include <string>
20 #include <arpa/inet.h>
21 #include "sp_profiler.h"
22 #include "sp_data.h"
23 #include <sys/select.h>
24 
25 namespace OHOS {
26     namespace SmartPerf {
27 #define OH_DATA_PORT 12567
28 #define OH_DATA_PORT_TRY_NUM 3
29 #define MSG_MAX_LEN 256
30 #define PARA_NAME_MAX_LEN 16
31 #define SOCKET_PORT_NUM_PER_TYPE 10
32 #define OH_SOCKET_MAX 10
33 
34         typedef struct {
35             char name[PARA_NAME_MAX_LEN];
36             int isEvent;
37             int value;
38         } ParaStatus;
39 
40         struct ServerParams {
41             time_t startTime;
42             int serverFd;
43             int pipFd[2];
44             int receiveFd[OH_SOCKET_MAX];
45         };
46         class SdkDataRecv : public SpProfiler {
47         public:
48             std::map<std::string, std::string> ItemData() override;
GetInstance()49             static SdkDataRecv &GetInstance()
50             {
51                 static SdkDataRecv instance;
52                 return instance;
53             }
54             static int CreateOhSocketServer(int basePort);
55             std::string OhDataReceive(int index, ServerParams &params);
56             std::string ProcessData(std::string message, ServerParams &params);
57             void ServerThread(std::vector<std::string> &dataVec);
58 
59             void SetRunningState(bool state);
60             int GetListenFd();
61             void SetListenFd(int fd);
62             void RunServerThread(std::vector<std::string> &dataVec, ServerParams &params);
63             void HandleReceiveFd(std::vector<std::string> &dataVec, int i, ServerParams &params);
64             void HandleServerFd(ServerParams &params);
65             void SetUpFdSet(ServerParams &params);
66             void CleanUpResources(ServerParams &params);
67             void GetSdkDataRealtimeData(std::map<std::string, std::string> &dataMap);
68             void SetStartRecordTime();
69         private:
70             SdkDataRecv();
71             SdkDataRecv(const SdkDataRecv &);
72             SdkDataRecv &operator = (const SdkDataRecv &);
73             int listenFd = -1;
74             int sendSocket = -1;
75             std::string userTpye = "";
76             bool collectRunring  = false;
77             int maxFd = 0;
78             fd_set readFds;
79             std::string receiveBuffer = "";
80             std::string sdkDataRealtimeData = "";
81             ServerParams sdkParams;
82             std::mutex realtimeDataLock;
83         };
84     }
85 }
86 #endif