blob: b9dee5625938cd1beeca8968aea034a26a6ee1e7 [file] [log] [blame]
Felipe Leme78e09632016-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 Zubrytskyi6eca0e62019-06-27 13:47:34 -070020#include <android-base/strings.h>
21
Felipe Leme78e09632016-07-19 17:07:22 -070022#include "adb.h"
Yurii Zubrytskyi6eca0e62019-06-27 13:47:34 -070023#include "adb_client.h"
24#include "adb_unique_fd.h"
Felipe Leme78e09632016-07-19 17:07:22 -070025
Felipe Leme07ac8552016-07-26 12:14:39 -070026// Callback used to handle the standard streams (stdout and stderr) sent by the
27// device's upon receiving a command.
28//
29class StandardStreamsCallbackInterface {
30 public:
31 StandardStreamsCallbackInterface() {
32 }
33 // Handles the stdout output from devices supporting the Shell protocol.
34 virtual void OnStdout(const char* buffer, int length) = 0;
35
36 // Handles the stderr output from devices supporting the Shell protocol.
37 virtual void OnStderr(const char* buffer, int length) = 0;
38
39 // Indicates the communication is finished and returns the appropriate error
40 // code.
41 //
42 // |status| has the status code returning by the underlying communication
43 // channels
44 virtual int Done(int status) = 0;
45
46 protected:
47 static void OnStream(std::string* string, FILE* stream, const char* buffer, int length) {
48 if (string != nullptr) {
49 string->append(buffer, length);
50 } else {
51 fwrite(buffer, 1, length, stream);
52 fflush(stream);
53 }
54 }
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(StandardStreamsCallbackInterface);
58};
59
60// Default implementation that redirects the streams to the equilavent host
61// stream or to a string
62// passed to the constructor.
63class DefaultStandardStreamsCallback : public StandardStreamsCallbackInterface {
64 public:
65 // If |stdout_str| is non-null, OnStdout will append to it.
66 // If |stderr_str| is non-null, OnStderr will append to it.
67 DefaultStandardStreamsCallback(std::string* stdout_str, std::string* stderr_str)
68 : stdout_str_(stdout_str), stderr_str_(stderr_str) {
69 }
70
71 void OnStdout(const char* buffer, int length) {
72 OnStream(stdout_str_, stdout, buffer, length);
73 }
74
75 void OnStderr(const char* buffer, int length) {
76 OnStream(stderr_str_, stderr, buffer, length);
77 }
78
79 int Done(int status) {
80 return status;
81 }
82
83 private:
84 std::string* stdout_str_;
85 std::string* stderr_str_;
86
87 DISALLOW_COPY_AND_ASSIGN(DefaultStandardStreamsCallback);
88};
89
Josh Gao0cee7cc2018-07-09 18:00:48 -070090class SilentStandardStreamsCallbackInterface : public StandardStreamsCallbackInterface {
91 public:
92 SilentStandardStreamsCallbackInterface() = default;
93 void OnStdout(const char*, int) override final {}
94 void OnStderr(const char*, int) override final {}
95 int Done(int status) override final { return status; }
96};
97
Felipe Leme07ac8552016-07-26 12:14:39 -070098// Singleton.
99extern DefaultStandardStreamsCallback DEFAULT_STANDARD_STREAMS_CALLBACK;
100
Felipe Leme78e09632016-07-19 17:07:22 -0700101int adb_commandline(int argc, const char** argv);
Felipe Leme78e09632016-07-19 17:07:22 -0700102
Josh Gao3828ebc2019-12-03 16:05:54 -0800103bool copy_to_file(int inFd, int outFd);
Idries Hamadied409ea2018-01-29 16:30:36 +0000104
Felipe Leme78e09632016-07-19 17:07:22 -0700105// Connects to the device "shell" service with |command| and prints the
106// resulting output.
Felipe Leme07ac8552016-07-26 12:14:39 -0700107// if |callback| is non-null, stdout/stderr output will be handled by it.
Josh Gaob122b172017-08-16 16:57:01 -0700108int send_shell_command(
Elliott Hughes31f0f1b2018-06-14 10:46:05 -0700109 const std::string& command, bool disable_shell_protocol = false,
110 StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
Felipe Leme78e09632016-07-19 17:07:22 -0700111
Alex Buynytskyy665f3ff2019-09-16 12:10:54 -0700112// Reads from |fd| and prints received data. If |use_shell_protocol| is true
113// this expects that incoming data will use the shell protocol, in which case
114// stdout/stderr are routed independently and the remote exit code will be
115// returned.
116// if |callback| is non-null, stdout/stderr output will be handled by it.
117int read_and_dump(borrowed_fd fd, bool use_shell_protocol = false,
118 StandardStreamsCallbackInterface* callback = &DEFAULT_STANDARD_STREAMS_CALLBACK);
119
Yurii Zubrytskyi6eca0e62019-06-27 13:47:34 -0700120// Connects to the device "abb" service with |command| and returns the fd.
121template <typename ContainerT>
122unique_fd send_abb_exec_command(const ContainerT& command_args, std::string* error) {
123 std::string service_string = "abb_exec:" + android::base::Join(command_args, ABB_ARG_DELIMETER);
124
125 unique_fd fd(adb_connect(service_string, error));
126 if (fd < 0) {
127 fprintf(stderr, "adb: failed to run abb_exec. Error: %s\n", error->c_str());
128 return unique_fd{};
129 }
130 return fd;
131}
132
Felipe Leme78e09632016-07-19 17:07:22 -0700133#endif // COMMANDLINE_H