• 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 #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 #include <thread>
25 
26 namespace OHOS {
27     namespace SmartPerf {
28 #define OH_DATA_PORT 12567
29 #define OH_DATA_PORT_TRY_NUM 3
30 #define MSG_MAX_LEN 256
31 #define PARA_NAME_MAX_LEN 16
32 #define SOCKET_PORT_NUM_PER_TYPE 10
33 #define OH_SOCKET_MAX 10
34 
35         typedef struct {
36             char name[PARA_NAME_MAX_LEN];
37             int isEvent;
38             int value;
39         } ParaStatus;
40 
41         struct ServerParams {
42             time_t startTime;
43             int serverFd;
44             int pipFd[2];
45             int receiveFd[OH_SOCKET_MAX];
46         };
47         class SdkDataRecv : public SpProfiler {
48         public:
49             std::map<std::string, std::string> ItemData() override;
50             void StartExecutionOnce(bool isPause) override;
51             void FinishtExecutionOnce(bool isPause) override;
GetInstance()52             static SdkDataRecv &GetInstance()
53             {
54                 static SdkDataRecv instance;
55                 return instance;
56             }
57             static int CreateOhSocketServer(int basePort);
58             std::string OhDataReceive(int index, ServerParams &params);
59             std::string ProcessData(std::string& message, ServerParams &params);
60             void ServerThread();
61 
62             void RunServerThread(ServerParams &params);
63             void HandleReceiveFd(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 SocketCommandVerification(std::string &resStr, ServerParams &params);
69             void GetSdkFpsAndSystime(const std::string &sdkStr);
70             std::string GetSdkFps() const;
71             std::string GetSdkSystime() const;
72             void SetFilePath(const std::string& path);
73 
74             std::vector<std::string> sdkVec_;
75         private:
76             SdkDataRecv();
77             SdkDataRecv(const SdkDataRecv &);
78             SdkDataRecv &operator = (const SdkDataRecv &);
79             int listenFd = -1;
80             int sendSocket = -1;
81             std::string userTpye = "";
82             bool collectRunring  = false;
83             int maxFd = 0;
84             fd_set readFds;
85             std::string receiveBuffer = "";
86             std::string sdkDataRealtimeData = "";
87             ServerParams sdkParams;
88             std::mutex realtimeDataLock;
89             std::string sdkFps;
90             std::string sdkSystime;
91             std::thread th_;
92             std::string filePath_;
93         };
94     }
95 }
96 #endif