blob: a0bba06f93e0557225494b321ab9b0887c698f38 [file] [log] [blame]
Felipe Lemea9be4f32016-07-19 17:07:22 -07001/*
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
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070020#include <android-base/strings.h>
21
Shaju Mathewbf7da1c2021-10-30 13:47:11 -070022#include <optional>
23
Felipe Lemea9be4f32016-07-19 17:07:22 -070024#include "adb.h"
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070025#include "adb_client.h"
26#include "adb_unique_fd.h"
Shaju Mathewbf7da1c2021-10-30 13:47:11 -070027#include "transport.h"
Felipe Lemea9be4f32016-07-19 17:07:22 -070028
Felipe Lemecc6ebb52016-07-26 12:14:39 -070029// Callback used to handle the standard streams (stdout and stderr) sent by the
30// device's upon receiving a command.
Shaju Mathewbf7da1c2021-10-30 13:47:11 -070031
Felipe Lemecc6ebb52016-07-26 12:14:39 -070032//
33class StandardStreamsCallbackInterface {
34 public:
35 StandardStreamsCallbackInterface() {
36 }
37 // Handles the stdout output from devices supporting the Shell protocol.
Lee Shombertb796cd52022-03-22 12:19:06 -070038 // Returns true on success and false on failure.
39 virtual bool OnStdout(const char* buffer, size_t length) = 0;
Felipe Lemecc6ebb52016-07-26 12:14:39 -070040
41 // Handles the stderr output from devices supporting the Shell protocol.
Lee Shombertb796cd52022-03-22 12:19:06 -070042 // Returns true on success and false on failure.
43 virtual bool OnStderr(const char* buffer, size_t length) = 0;
Felipe Lemecc6ebb52016-07-26 12:14:39 -070044
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:
Lee Shombertb796cd52022-03-22 12:19:06 -070053 static bool OnStream(std::string* string, FILE* stream, const char* buffer, size_t length,
54 bool returnErrors) {
Felipe Lemecc6ebb52016-07-26 12:14:39 -070055 if (string != nullptr) {
56 string->append(buffer, length);
Lee Shombertb796cd52022-03-22 12:19:06 -070057 return true;
Felipe Lemecc6ebb52016-07-26 12:14:39 -070058 } else {
Lee Shombertb796cd52022-03-22 12:19:06 -070059 bool okay = (fwrite(buffer, 1, length, stream) == length);
Felipe Lemecc6ebb52016-07-26 12:14:39 -070060 fflush(stream);
Lee Shombertb796cd52022-03-22 12:19:06 -070061 return returnErrors ? okay : true;
Felipe Lemecc6ebb52016-07-26 12:14:39 -070062 }
63 }
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(StandardStreamsCallbackInterface);
67};
68
Lee Shombertb796cd52022-03-22 12:19:06 -070069// Default implementation that redirects the streams to the equivalent host
70// stream or to a string passed to the constructor.
Felipe Lemecc6ebb52016-07-26 12:14:39 -070071class 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.
75 DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str)
Lee Shombertb796cd52022-03-22 12:19:06 -070076 : stdout_str_(stdout_str), stderr_str_(stderr_str), returnErrors_(false) {
77 }
78 DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str,
79 bool returnErrors)
80 : stdout_str_(stdout_str), stderr_str_(stderr_str), returnErrors_(returnErrors) {
Felipe Lemecc6ebb52016-07-26 12:14:39 -070081 }
82
Lee Shombertb796cd52022-03-22 12:19:06 -070083 bool OnStdout(const char* buffer, size_t length) {
84 return OnStream(stdout_str_, stdout, buffer, length, returnErrors_);
Felipe Lemecc6ebb52016-07-26 12:14:39 -070085 }
86
Lee Shombertb796cd52022-03-22 12:19:06 -070087 bool OnStderr(const char* buffer, size_t length) {
88 return OnStream(stderr_str_, stderr, buffer, length, returnErrors_);
Felipe Lemecc6ebb52016-07-26 12:14:39 -070089 }
90
91 int Done(int status) {
92 return status;
93 }
94
Lee Shombertb796cd52022-03-22 12:19:06 -070095 void ReturnErrors(bool returnErrors) {
96 returnErrors_ = returnErrors;
97 }
98
Felipe Lemecc6ebb52016-07-26 12:14:39 -070099 private:
100 std::string* stdout_str_;
101 std::string* stderr_str_;
Lee Shombertb796cd52022-03-22 12:19:06 -0700102 bool returnErrors_;
Felipe Lemecc6ebb52016-07-26 12:14:39 -0700103
104 DISALLOW_COPY_AND_ASSIGN(DefaultStandardStreamsCallback);
105};
106
Josh Gao37f11f52018-07-09 18:00:48 -0700107class SilentStandardStreamsCallbackInterface : public StandardStreamsCallbackInterface {
108 public:
109 SilentStandardStreamsCallbackInterface() = default;
Lee Shombertb796cd52022-03-22 12:19:06 -0700110 bool OnStdout(const char*, size_t) override final { return true; }
111 bool OnStderr(const char*, size_t) override final { return true; }
Josh Gao37f11f52018-07-09 18:00:48 -0700112 int Done(int status) override final { return status; }
113};
114
Felipe Lemecc6ebb52016-07-26 12:14:39 -0700115// Singleton.
116extern DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK;
117
Felipe Lemea9be4f32016-07-19 17:07:22 -0700118int adb_commandline(int argc, const char** argv);
Felipe Lemea9be4f32016-07-19 17:07:22 -0700119
Shaju Mathewbf7da1c2021-10-30 13:47:11 -0700120// Helper retrieval function.
121const std::optional<FeatureSet>& adb_get_feature_set_or_die(void);
122
Josh Gaod7f1d0b2019-12-03 16:05:54 -0800123bool copy_to_file(int inFd, int outFd);
Idries Hamadi1ecee442018-01-29 16:30:36 +0000124
Felipe Lemea9be4f32016-07-19 17:07:22 -0700125// Connects to the device "shell" service with |command| and prints the
126// resulting output.
Felipe Lemecc6ebb52016-07-26 12:14:39 -0700127// if |callback| is non-null, stdout/stderr output will be handled by it.
Josh Gaob39e4152017-08-16 16:57:01 -0700128int send_shell_command(
Elliott Hughes8fa33512018-06-14 10:46:05 -0700129 const std::string& command, bool disable_shell_protocol = false,
130 StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
Felipe Lemea9be4f32016-07-19 17:07:22 -0700131
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700132// 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.
137int read_and_dump(borrowed_fd fd, bool use_shell_protocol = false,
138 StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
139
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -0700140// Connects to the device "abb" service with |command| and returns the fd.
141template <typename ContainerT>
142unique_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
Felipe Lemea9be4f32016-07-19 17:07:22 -0700153#endif // COMMANDLINE_H