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 <iostream>
16 #include <sstream>
17 #include <cstring>
18 #include <arpa/inet.h>
19 #include <unistd.h>
20 #include <sys/socket.h>
21 #include "include/client_control.h"
22 #include "include/sp_utils.h"
23 #include "include/startup_delay.h"
24 #include "include/sp_log.h"
25 #include "include/common.h"
26 namespace OHOS {
27 namespace SmartPerf {
SocketStart(const std::string &args)28 int ClientControl::SocketStart(const std::string &args)
29 {
30 std::string messageInit = message + args;
31 int resultId = OHOS::SmartPerf::ClientControl::InitSocket();
32 if (resultId == 1) {
33 LOGE("ClientControl::InitSocket() error(%d)", resultId);
34 return resultId;
35 }
36 send(clientSocket, messageInit.c_str(), strlen(messageInit.c_str()), 0);
37 read(clientSocket, buffer, numBuff);
38 send(clientSocket, message1, strlen(message1), 0);
39 read(clientSocket, buffer, numBuff);
40 char dest[arraySize] = {0};
41 size_t i;
42 for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) {
43 dest[i] = buffer[i];
44 }
45 dest[i] = '\0';
46 if (strcmp(dest, "start::True") == 0) {
47 std::cout << "SP_daemon Collection begins" << std::endl;
48 OHOS::SmartPerf::ClientControl::CloseSocket();
49 } else {
50 std::cout << "SP_daemon Collection begins failed" << std::endl;
51 OHOS::SmartPerf::StartUpDelay sd;
52 sd.GetSpClear();
53 }
54 LOGI("ClientControl::SocketStart() OK");
55 return 0;
56 }
SocketStop()57 int ClientControl::SocketStop()
58 {
59 OHOS::SmartPerf::ClientControl::InitSocket();
60 send(clientSocket, message2, strlen(message2), 0);
61 read(clientSocket, buffer, numBuff);
62 char dest[arraySize] = {0};
63 size_t i;
64 for (i = 0; buffer[i] != '\0' && i < arraySize - 1; i++) {
65 dest[i] = buffer[i];
66 }
67 dest[i] = '\0';
68 if (strcmp(dest, "stop::True") == 0) {
69 std::cout << "SP_daemon Collection ended" << std::endl;
70 std::cout << "Output Path: data/local/tmp/smartperf/1/t_index_info.csv" << std::endl;
71 OHOS::SmartPerf::StartUpDelay sd;
72 OHOS::SmartPerf::ClientControl::CloseSocket();
73 sd.GetSpClear();
74 } else {
75 std::cout << "SP_daemon Collection ended failed" << std::endl;
76 }
77 LOGI("ClientControl::SocketStop() OK");
78 return 0;
79 }
InitSocket()80 int ClientControl::InitSocket()
81 {
82 clientSocket = socket(AF_INET, SOCK_STREAM, 0);
83 if (clientSocket == -1) {
84 LOGE("Faild to create socket");
85 return -1;
86 }
87 serverAddress.sin_family = AF_INET;
88 serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
89 serverAddress.sin_port = htons(protNumber);
90 if (connect(clientSocket, reinterpret_cast<struct sockaddr *>(&serverAddress), sizeof(serverAddress)) < 0) {
91 OHOS::SmartPerf::StartUpDelay sd;
92 sd.GetSpClear();
93 LOGE("Failed to connect to server");
94 return 1;
95 }
96 LOGI("ClientControl::InitSocket() OK");
97 return 0;
98 }
StartSPDaemon() const99 void ClientControl::StartSPDaemon() const
100 {
101 std::string result = "";
102 std::string server = CMD_COMMAND_MAP.at(CmdCommand::SERVER);
103 SPUtils::LoadCmd(server, result);
104 sleep(1);
105 LOGI("ClientControl::StartSPDaemon() OK");
106 }
CloseSocket()107 int ClientControl::CloseSocket()
108 {
109 shutdown(clientSocket, SHUT_RD);
110 close(clientSocket);
111 clientSocket = -1;
112 LOGI("ClientControl::CloseSocket() OK");
113 return 0;
114 }
115 }
116 }