1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef COMMANDLINE_H
18 #define COMMANDLINE_H
19
20 #include <android-base/strings.h>
21
22 #include <optional>
23
24 #include "adb.h"
25 #include "adb_client.h"
26 #include "adb_unique_fd.h"
27 #include "transport.h"
28
29 // Callback used to handle the standard streams (stdout and stderr) sent by the
30 // device's upon receiving a command.
31
32 //
33 class StandardStreamsCallbackInterface {
34 public:
StandardStreamsCallbackInterface()35 StandardStreamsCallbackInterface() {
36 }
37 // Handles the stdout output from devices supporting the Shell protocol.
38 // Returns true on success and false on failure.
39 virtual bool OnStdout(const char* buffer, size_t length) = 0;
40
41 // Handles the stderr output from devices supporting the Shell protocol.
42 // Returns true on success and false on failure.
43 virtual bool OnStderr(const char* buffer, size_t length) = 0;
44
45 // Indicates the communication is finished and returns the appropriate error
46 // code.
47 //
48 // |status| has the status code returning by the underlying communication
49 // channels
50 virtual int Done(int status) = 0;
51
52 protected:
OnStream(std::string * string,FILE * stream,const char * buffer,size_t length,bool returnErrors)53 static bool OnStream(std::string* string, FILE* stream, const char* buffer, size_t length,
54 bool returnErrors) {
55 if (string != nullptr) {
56 string->append(buffer, length);
57 return true;
58 } else {
59 bool okay = (fwrite(buffer, 1, length, stream) == length);
60 fflush(stream);
61 return returnErrors ? okay : true;
62 }
63 }
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(StandardStreamsCallbackInterface);
67 };
68
69 // Default implementation that redirects the streams to the equivalent host
70 // stream or to a string passed to the constructor.
71 class DefaultStandardStreamsCallback : public StandardStreamsCallbackInterface {
72 public:
73 // If |stdout_str| is non-null, OnStdout will append to it.
74 // If |stderr_str| is non-null, OnStderr will append to it.
DefaultStandardStreamsCallback(std::string * stdout_str,std::string * stderr_str)75 DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str)
76 : stdout_str_(stdout_str), stderr_str_(stderr_str), returnErrors_(false) {
77 }
DefaultStandardStreamsCallback(std::string * stdout_str,std::string * stderr_str,bool returnErrors)78 DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str,
79 bool returnErrors)
80 : stdout_str_(stdout_str), stderr_str_(stderr_str), returnErrors_(returnErrors) {
81 }
82
OnStdout(const char * buffer,size_t length)83 bool OnStdout(const char* buffer, size_t length) {
84 return OnStream(stdout_str_, stdout, buffer, length, returnErrors_);
85 }
86
OnStderr(const char * buffer,size_t length)87 bool OnStderr(const char* buffer, size_t length) {
88 return OnStream(stderr_str_, stderr, buffer, length, returnErrors_);
89 }
90
Done(int status)91 int Done(int status) {
92 return status;
93 }
94
ReturnErrors(bool returnErrors)95 void ReturnErrors(bool returnErrors) {
96 returnErrors_ = returnErrors;
97 }
98
99 private:
100 std::string* stdout_str_;
101 std::string* stderr_str_;
102 bool returnErrors_;
103
104 DISALLOW_COPY_AND_ASSIGN(DefaultStandardStreamsCallback);
105 };
106
107 class SilentStandardStreamsCallbackInterface : public StandardStreamsCallbackInterface {
108 public:
109 SilentStandardStreamsCallbackInterface() = default;
OnStdout(const char *,size_t)110 bool OnStdout(const char*, size_t) override final { return true; }
OnStderr(const char *,size_t)111 bool OnStderr(const char*, size_t) override final { return true; }
Done(int status)112 int Done(int status) override final { return status; }
113 };
114
115 // Singleton.
116 extern DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK;
117
118 int adb_commandline(int argc, const char** argv);
119
120 // Helper retrieval function.
121 const std::optional<FeatureSet>& adb_get_feature_set_or_die(void);
122
123 bool copy_to_file(int inFd, int outFd);
124
125 // Connects to the device "shell" service with |command| and prints the
126 // resulting output.
127 // if |callback| is non-null, stdout/stderr output will be handled by it.
128 int send_shell_command(
129 const std::string& command, bool disable_shell_protocol = false,
130 StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
131
132 // Reads from |fd| and prints received data. If |use_shell_protocol| is true
133 // this expects that incoming data will use the shell protocol, in which case
134 // stdout/stderr are routed independently and the remote exit code will be
135 // returned.
136 // if |callback| is non-null, stdout/stderr output will be handled by it.
137 int read_and_dump(borrowed_fd fd, bool use_shell_protocol = false,
138 StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
139
140 // Connects to the device "abb" service with |command| and returns the fd.
141 template <typename ContainerT>
send_abb_exec_command(const ContainerT & command_args,std::string * error)142 unique_fd send_abb_exec_command(const ContainerT& command_args, std::string* error) {
143 std::string service_string = "abb_exec:" + android::base::Join(command_args, ABB_ARG_DELIMETER);
144
145 unique_fd fd(adb_connect(service_string, error));
146 if (fd < 0) {
147 fprintf(stderr, "adb: failed to run abb_exec. Error: %s\n", error->c_str());
148 return unique_fd{};
149 }
150 return fd;
151 }
152
153 #endif // COMMANDLINE_H
154