xref: /aosp_15_r20/external/parameter-framework/upstream/remote-process/main.cpp (revision c33452fb792a5495ec310a9626f2638b053af5dd)
1*c33452fbSAndroid Build Coastguard Worker /*
2*c33452fbSAndroid Build Coastguard Worker  * Copyright (c) 2011-2014, Intel Corporation
3*c33452fbSAndroid Build Coastguard Worker  * All rights reserved.
4*c33452fbSAndroid Build Coastguard Worker  *
5*c33452fbSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without modification,
6*c33452fbSAndroid Build Coastguard Worker  * are permitted provided that the following conditions are met:
7*c33452fbSAndroid Build Coastguard Worker  *
8*c33452fbSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright notice, this
9*c33452fbSAndroid Build Coastguard Worker  * list of conditions and the following disclaimer.
10*c33452fbSAndroid Build Coastguard Worker  *
11*c33452fbSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*c33452fbSAndroid Build Coastguard Worker  * this list of conditions and the following disclaimer in the documentation and/or
13*c33452fbSAndroid Build Coastguard Worker  * other materials provided with the distribution.
14*c33452fbSAndroid Build Coastguard Worker  *
15*c33452fbSAndroid Build Coastguard Worker  * 3. Neither the name of the copyright holder nor the names of its contributors
16*c33452fbSAndroid Build Coastguard Worker  * may be used to endorse or promote products derived from this software without
17*c33452fbSAndroid Build Coastguard Worker  * specific prior written permission.
18*c33452fbSAndroid Build Coastguard Worker  *
19*c33452fbSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20*c33452fbSAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21*c33452fbSAndroid Build Coastguard Worker  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22*c33452fbSAndroid Build Coastguard Worker  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23*c33452fbSAndroid Build Coastguard Worker  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*c33452fbSAndroid Build Coastguard Worker  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26*c33452fbSAndroid Build Coastguard Worker  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*c33452fbSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*c33452fbSAndroid Build Coastguard Worker  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c33452fbSAndroid Build Coastguard Worker  */
30*c33452fbSAndroid Build Coastguard Worker 
31*c33452fbSAndroid Build Coastguard Worker #include <asio.hpp>
32*c33452fbSAndroid Build Coastguard Worker 
33*c33452fbSAndroid Build Coastguard Worker #include <iostream>
34*c33452fbSAndroid Build Coastguard Worker #include <string>
35*c33452fbSAndroid Build Coastguard Worker #include <cstring>
36*c33452fbSAndroid Build Coastguard Worker #include <stdlib.h>
37*c33452fbSAndroid Build Coastguard Worker #include "RequestMessage.h"
38*c33452fbSAndroid Build Coastguard Worker #include "AnswerMessage.h"
39*c33452fbSAndroid Build Coastguard Worker #include "Socket.h"
40*c33452fbSAndroid Build Coastguard Worker #include "convert.hpp"
41*c33452fbSAndroid Build Coastguard Worker 
42*c33452fbSAndroid Build Coastguard Worker using namespace std;
43*c33452fbSAndroid Build Coastguard Worker 
sendAndDisplayCommand(asio::generic::stream_protocol::socket & socket,CRequestMessage & requestMessage)44*c33452fbSAndroid Build Coastguard Worker bool sendAndDisplayCommand(asio::generic::stream_protocol::socket &socket,
45*c33452fbSAndroid Build Coastguard Worker                            CRequestMessage &requestMessage)
46*c33452fbSAndroid Build Coastguard Worker {
47*c33452fbSAndroid Build Coastguard Worker     string strError;
48*c33452fbSAndroid Build Coastguard Worker 
49*c33452fbSAndroid Build Coastguard Worker     if (requestMessage.serialize(Socket(socket), true, strError) != CRequestMessage::success) {
50*c33452fbSAndroid Build Coastguard Worker 
51*c33452fbSAndroid Build Coastguard Worker         cerr << "Unable to send command to target: " << strError << endl;
52*c33452fbSAndroid Build Coastguard Worker         return false;
53*c33452fbSAndroid Build Coastguard Worker     }
54*c33452fbSAndroid Build Coastguard Worker 
55*c33452fbSAndroid Build Coastguard Worker     ///// Get answer
56*c33452fbSAndroid Build Coastguard Worker     CAnswerMessage answerMessage;
57*c33452fbSAndroid Build Coastguard Worker     if (answerMessage.serialize(Socket(socket), false, strError) != CRequestMessage::success) {
58*c33452fbSAndroid Build Coastguard Worker 
59*c33452fbSAndroid Build Coastguard Worker         cerr << "Unable to received answer from target: " << strError << endl;
60*c33452fbSAndroid Build Coastguard Worker         return false;
61*c33452fbSAndroid Build Coastguard Worker     }
62*c33452fbSAndroid Build Coastguard Worker 
63*c33452fbSAndroid Build Coastguard Worker     // Success?
64*c33452fbSAndroid Build Coastguard Worker     if (!answerMessage.success()) {
65*c33452fbSAndroid Build Coastguard Worker 
66*c33452fbSAndroid Build Coastguard Worker         // Display error answer
67*c33452fbSAndroid Build Coastguard Worker         cerr << answerMessage.getAnswer() << endl;
68*c33452fbSAndroid Build Coastguard Worker         return false;
69*c33452fbSAndroid Build Coastguard Worker     }
70*c33452fbSAndroid Build Coastguard Worker 
71*c33452fbSAndroid Build Coastguard Worker     // Display success answer
72*c33452fbSAndroid Build Coastguard Worker     cout << answerMessage.getAnswer() << endl;
73*c33452fbSAndroid Build Coastguard Worker 
74*c33452fbSAndroid Build Coastguard Worker     return true;
75*c33452fbSAndroid Build Coastguard Worker }
76*c33452fbSAndroid Build Coastguard Worker 
usage(const std::string & command,const std::string & error)77*c33452fbSAndroid Build Coastguard Worker int usage(const std::string &command, const std::string &error)
78*c33452fbSAndroid Build Coastguard Worker {
79*c33452fbSAndroid Build Coastguard Worker     if (not error.empty()) {
80*c33452fbSAndroid Build Coastguard Worker         cerr << error << endl;
81*c33452fbSAndroid Build Coastguard Worker     }
82*c33452fbSAndroid Build Coastguard Worker     cerr << "Usage: " << endl;
83*c33452fbSAndroid Build Coastguard Worker     cerr << "Send a single command:" << endl;
84*c33452fbSAndroid Build Coastguard Worker     cerr << "\t" << command
85*c33452fbSAndroid Build Coastguard Worker          << " <hostname port|tcp://[host]:port|unix://path> <command> [argument[s]]" << endl;
86*c33452fbSAndroid Build Coastguard Worker 
87*c33452fbSAndroid Build Coastguard Worker     return 1;
88*c33452fbSAndroid Build Coastguard Worker }
89*c33452fbSAndroid Build Coastguard Worker 
main(int argc,char * argv[])90*c33452fbSAndroid Build Coastguard Worker int main(int argc, char *argv[])
91*c33452fbSAndroid Build Coastguard Worker {
92*c33452fbSAndroid Build Coastguard Worker     int commandPos;
93*c33452fbSAndroid Build Coastguard Worker 
94*c33452fbSAndroid Build Coastguard Worker     // Enough args?
95*c33452fbSAndroid Build Coastguard Worker     if (argc < 3) {
96*c33452fbSAndroid Build Coastguard Worker         return usage(argv[0], "Missing arguments");
97*c33452fbSAndroid Build Coastguard Worker     }
98*c33452fbSAndroid Build Coastguard Worker     asio::io_service io_service;
99*c33452fbSAndroid Build Coastguard Worker     asio::generic::stream_protocol::socket connectionSocket(io_service);
100*c33452fbSAndroid Build Coastguard Worker 
101*c33452fbSAndroid Build Coastguard Worker     bool isInet = false;
102*c33452fbSAndroid Build Coastguard Worker     string port;
103*c33452fbSAndroid Build Coastguard Worker     string host;
104*c33452fbSAndroid Build Coastguard Worker     try {
105*c33452fbSAndroid Build Coastguard Worker         // backward compatibility: tcp port only refered by its value
106*c33452fbSAndroid Build Coastguard Worker         uint16_t testConverter;
107*c33452fbSAndroid Build Coastguard Worker         if (convertTo({argv[2]}, testConverter)) {
108*c33452fbSAndroid Build Coastguard Worker             isInet = true;
109*c33452fbSAndroid Build Coastguard Worker             port = argv[2];
110*c33452fbSAndroid Build Coastguard Worker             host = argv[1];
111*c33452fbSAndroid Build Coastguard Worker             if (argc <= 3) {
112*c33452fbSAndroid Build Coastguard Worker                 return usage(argv[0], "Missing arguments");
113*c33452fbSAndroid Build Coastguard Worker             }
114*c33452fbSAndroid Build Coastguard Worker             commandPos = 3;
115*c33452fbSAndroid Build Coastguard Worker         } else {
116*c33452fbSAndroid Build Coastguard Worker             commandPos = 2;
117*c33452fbSAndroid Build Coastguard Worker             string endPortArg{argv[1]};
118*c33452fbSAndroid Build Coastguard Worker             std::string protocol;
119*c33452fbSAndroid Build Coastguard Worker 
120*c33452fbSAndroid Build Coastguard Worker             const std::string tcpProtocol{"tcp"};
121*c33452fbSAndroid Build Coastguard Worker             const std::string unixProtocol{"unix"};
122*c33452fbSAndroid Build Coastguard Worker             const std::vector<std::string> supportedProtocols{tcpProtocol, unixProtocol};
123*c33452fbSAndroid Build Coastguard Worker             const std::string protocolDelimiter{"://"};
124*c33452fbSAndroid Build Coastguard Worker 
125*c33452fbSAndroid Build Coastguard Worker             size_t protocolDelPos = endPortArg.find(protocolDelimiter);
126*c33452fbSAndroid Build Coastguard Worker             if (protocolDelPos == std::string::npos) {
127*c33452fbSAndroid Build Coastguard Worker                 return usage(argv[0], "Invalid endpoint " + endPortArg);
128*c33452fbSAndroid Build Coastguard Worker             }
129*c33452fbSAndroid Build Coastguard Worker             protocol = endPortArg.substr(0, protocolDelPos);
130*c33452fbSAndroid Build Coastguard Worker 
131*c33452fbSAndroid Build Coastguard Worker             if (std::find(begin(supportedProtocols), end(supportedProtocols), protocol) ==
132*c33452fbSAndroid Build Coastguard Worker                 end(supportedProtocols)) {
133*c33452fbSAndroid Build Coastguard Worker                 return usage(argv[0], "Invalid endpoint " + endPortArg);
134*c33452fbSAndroid Build Coastguard Worker             }
135*c33452fbSAndroid Build Coastguard Worker             isInet = (endPortArg.find(tcpProtocol) != std::string::npos);
136*c33452fbSAndroid Build Coastguard Worker             if (isInet) {
137*c33452fbSAndroid Build Coastguard Worker                 size_t portDelPos = endPortArg.rfind(':');
138*c33452fbSAndroid Build Coastguard Worker                 if (portDelPos == std::string::npos) {
139*c33452fbSAndroid Build Coastguard Worker                     return usage(argv[0], "Invalid endpoint " + endPortArg);
140*c33452fbSAndroid Build Coastguard Worker                 }
141*c33452fbSAndroid Build Coastguard Worker                 host = endPortArg.substr(protocolDelPos + protocolDelimiter.size(),
142*c33452fbSAndroid Build Coastguard Worker                                          portDelPos - (protocolDelPos + protocolDelimiter.size()));
143*c33452fbSAndroid Build Coastguard Worker                 port = endPortArg.substr(portDelPos + 1);
144*c33452fbSAndroid Build Coastguard Worker             } else {
145*c33452fbSAndroid Build Coastguard Worker                 port = endPortArg.substr(protocolDelPos + protocolDelimiter.size());
146*c33452fbSAndroid Build Coastguard Worker             }
147*c33452fbSAndroid Build Coastguard Worker         }
148*c33452fbSAndroid Build Coastguard Worker         if (isInet) {
149*c33452fbSAndroid Build Coastguard Worker             asio::ip::tcp::resolver resolver(io_service);
150*c33452fbSAndroid Build Coastguard Worker             asio::ip::tcp::socket tcpSocket(io_service);
151*c33452fbSAndroid Build Coastguard Worker 
152*c33452fbSAndroid Build Coastguard Worker             asio::connect(tcpSocket, resolver.resolve(asio::ip::tcp::resolver::query(host, port)));
153*c33452fbSAndroid Build Coastguard Worker             connectionSocket = std::move(tcpSocket);
154*c33452fbSAndroid Build Coastguard Worker         } else {
155*c33452fbSAndroid Build Coastguard Worker             asio::generic::stream_protocol::socket socket(io_service);
156*c33452fbSAndroid Build Coastguard Worker             asio::generic::stream_protocol::endpoint endpoint =
157*c33452fbSAndroid Build Coastguard Worker                 asio::local::stream_protocol::endpoint(port);
158*c33452fbSAndroid Build Coastguard Worker             socket.connect(endpoint);
159*c33452fbSAndroid Build Coastguard Worker             connectionSocket = std::move(socket);
160*c33452fbSAndroid Build Coastguard Worker         }
161*c33452fbSAndroid Build Coastguard Worker 
162*c33452fbSAndroid Build Coastguard Worker     } catch (const asio::system_error &e) {
163*c33452fbSAndroid Build Coastguard Worker         string endpoint;
164*c33452fbSAndroid Build Coastguard Worker 
165*c33452fbSAndroid Build Coastguard Worker         if (isInet) {
166*c33452fbSAndroid Build Coastguard Worker             endpoint = string("tcp://") + host + ":" + port;
167*c33452fbSAndroid Build Coastguard Worker         } else { /* other supported protocols */
168*c33452fbSAndroid Build Coastguard Worker             endpoint = argv[1];
169*c33452fbSAndroid Build Coastguard Worker         }
170*c33452fbSAndroid Build Coastguard Worker         cerr << "Connection to '" << endpoint << "' failed: " << e.what() << endl;
171*c33452fbSAndroid Build Coastguard Worker         return 1;
172*c33452fbSAndroid Build Coastguard Worker     }
173*c33452fbSAndroid Build Coastguard Worker 
174*c33452fbSAndroid Build Coastguard Worker     // Create command message
175*c33452fbSAndroid Build Coastguard Worker     CRequestMessage requestMessage(argv[commandPos]);
176*c33452fbSAndroid Build Coastguard Worker 
177*c33452fbSAndroid Build Coastguard Worker     // Add arguments
178*c33452fbSAndroid Build Coastguard Worker     for (int arg = commandPos + 1; arg < argc; arg++) {
179*c33452fbSAndroid Build Coastguard Worker 
180*c33452fbSAndroid Build Coastguard Worker         requestMessage.addArgument(argv[arg]);
181*c33452fbSAndroid Build Coastguard Worker     }
182*c33452fbSAndroid Build Coastguard Worker 
183*c33452fbSAndroid Build Coastguard Worker     if (!sendAndDisplayCommand(connectionSocket, requestMessage)) {
184*c33452fbSAndroid Build Coastguard Worker         return 1;
185*c33452fbSAndroid Build Coastguard Worker     }
186*c33452fbSAndroid Build Coastguard Worker 
187*c33452fbSAndroid Build Coastguard Worker     // Program status
188*c33452fbSAndroid Build Coastguard Worker     return 0;
189*c33452fbSAndroid Build Coastguard Worker }
190