The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 __ADB_H |
| 18 | #define __ADB_H |
| 19 | |
| 20 | #include <limits.h> |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 27 | |
leozwang | f25a6a4 | 2014-07-29 12:50:02 -0700 | [diff] [blame] | 28 | #include "adb_trace.h" |
Dan Albert | bbbbea6 | 2014-11-24 23:34:35 -0800 | [diff] [blame] | 29 | #include "fdevent.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 30 | #include "socket.h" |
Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame^] | 31 | #include "usb.h" |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 32 | |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 33 | constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024; |
| 34 | constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024; |
| 35 | constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | |
| 37 | #define A_SYNC 0x434e5953 |
| 38 | #define A_CNXN 0x4e584e43 |
| 39 | #define A_OPEN 0x4e45504f |
| 40 | #define A_OKAY 0x59414b4f |
| 41 | #define A_CLSE 0x45534c43 |
| 42 | #define A_WRTE 0x45545257 |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 43 | #define A_AUTH 0x48545541 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 45 | // ADB protocol version. |
| 46 | #define A_VERSION 0x01000000 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 48 | // Used for help/version information. |
| 49 | #define ADB_VERSION_MAJOR 1 |
| 50 | #define ADB_VERSION_MINOR 0 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | |
Elliott Hughes | b9f0164 | 2015-08-12 08:32:10 -0700 | [diff] [blame] | 52 | std::string adb_version(); |
| 53 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 54 | // Increment this when we want to force users to start a new adb server. |
Josh Gao | a2cf375 | 2016-12-05 17:11:34 -0800 | [diff] [blame] | 55 | #define ADB_SERVER_VERSION 38 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 57 | class atransport; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | |
| 59 | struct amessage { |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 60 | uint32_t command; /* command identifier constant */ |
| 61 | uint32_t arg0; /* first argument */ |
| 62 | uint32_t arg1; /* second argument */ |
| 63 | uint32_t data_length; /* length of payload (0 is allowed) */ |
| 64 | uint32_t data_check; /* checksum of data payload */ |
| 65 | uint32_t magic; /* command ^ 0xffffffff */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | struct apacket |
| 69 | { |
| 70 | apacket *next; |
| 71 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 72 | size_t len; |
| 73 | char* ptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | |
| 75 | amessage msg; |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 76 | char data[MAX_PAYLOAD]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Josh Gao | 67ac379 | 2016-10-06 13:31:44 -0700 | [diff] [blame] | 79 | uint32_t calculate_apacket_checksum(const apacket* packet); |
| 80 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | /* the adisconnect structure is used to record a callback that |
| 82 | ** will be called whenever a transport is disconnected (e.g. by the user) |
| 83 | ** this should be used to cleanup objects that depend on the |
| 84 | ** transport (e.g. remote sockets, listeners, etc...) |
| 85 | */ |
| 86 | struct adisconnect |
| 87 | { |
| 88 | void (*func)(void* opaque, atransport* t); |
| 89 | void* opaque; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 93 | // A transport object models the connection to a remote device or emulator there |
| 94 | // is one transport per connected device/emulator. A "local transport" connects |
| 95 | // through TCP (for the emulator), while a "usb transport" through USB (for real |
| 96 | // devices). |
| 97 | // |
| 98 | // Note that kTransportHost doesn't really correspond to a real transport |
| 99 | // object, it's a special value used to indicate that a client wants to connect |
| 100 | // to a service implemented within the ADB server itself. |
Elliott Hughes | 3aec2ba | 2015-05-05 13:10:43 -0700 | [diff] [blame] | 101 | enum TransportType { |
Dan Albert | ecce503 | 2015-05-18 16:46:31 -0700 | [diff] [blame] | 102 | kTransportUsb, |
| 103 | kTransportLocal, |
| 104 | kTransportAny, |
| 105 | kTransportHost, |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 106 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 108 | #define TOKEN_SIZE 20 |
| 109 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 110 | enum ConnectionState { |
| 111 | kCsAny = -1, |
| 112 | kCsOffline = 0, |
| 113 | kCsBootloader, |
| 114 | kCsDevice, |
| 115 | kCsHost, |
| 116 | kCsRecovery, |
| 117 | kCsNoPerm, // Insufficient permissions to communicate with the device. |
| 118 | kCsSideload, |
| 119 | kCsUnauthorized, |
| 120 | }; |
| 121 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 122 | |
| 123 | void print_packet(const char *label, apacket *p); |
| 124 | |
Josh Gao | 2930cdc | 2016-01-15 15:17:37 -0800 | [diff] [blame] | 125 | // These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they |
| 126 | // shouldn't be tagged with ADB_FORMAT_ARCHETYPE. |
| 127 | void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); |
| 128 | void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
| 130 | void handle_packet(apacket *p, atransport *t); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 131 | |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 132 | int launch_server(const std::string& socket_spec); |
| 133 | int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | /* initialize a transport object's func pointers and state */ |
Stefan Hilzinger | 1ec0342 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 136 | #if ADB_HOST |
| 137 | int get_available_local_transport_index(); |
| 138 | #endif |
Mike Lockwood | b51ae57 | 2009-08-24 15:58:40 -0700 | [diff] [blame] | 139 | int init_socket_transport(atransport *t, int s, int port, int local); |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 140 | void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | |
Lingfeng Yang | 5edb12b | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 142 | std::string getEmulatorSerialString(int console_port); |
Stefan Hilzinger | 1ec0342 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 143 | #if ADB_HOST |
| 144 | atransport* find_emulator_transport_by_adb_port(int adb_port); |
Lingfeng Yang | 5edb12b | 2016-10-06 12:22:55 -0700 | [diff] [blame] | 145 | atransport* find_emulator_transport_by_console_port(int console_port); |
Stefan Hilzinger | 1ec0342 | 2010-04-26 10:17:43 +0100 | [diff] [blame] | 146 | #endif |
Mike Lockwood | 5f2c4a1 | 2009-10-11 23:04:18 -0400 | [diff] [blame] | 147 | |
David Pursell | 8da19a4 | 2015-08-31 10:42:13 -0700 | [diff] [blame] | 148 | int service_to_fd(const char* name, const atransport* transport); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | #if ADB_HOST |
| 150 | asocket *host_service_to_socket(const char* name, const char *serial); |
| 151 | #endif |
| 152 | |
| 153 | #if !ADB_HOST |
| 154 | int init_jdwp(void); |
| 155 | asocket* create_jdwp_service_socket(); |
| 156 | asocket* create_jdwp_tracker_service_socket(); |
| 157 | int create_jdwp_connection_fd(int jdwp_pid); |
| 158 | #endif |
| 159 | |
Elliott Hughes | 9d07fee | 2015-05-07 21:38:41 -0700 | [diff] [blame] | 160 | int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd); |
David 'Digit' Turner | 963a449 | 2013-03-21 21:07:42 +0100 | [diff] [blame] | 161 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 162 | #if !ADB_HOST |
| 163 | void framebuffer_service(int fd, void *cookie); |
Paul Lawrence | 8ba2b02 | 2014-12-03 15:31:57 -0800 | [diff] [blame] | 164 | void set_verity_enabled_state_service(int fd, void* cookie); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | #endif |
| 166 | |
| 167 | /* packet allocator */ |
| 168 | apacket *get_apacket(void); |
| 169 | void put_apacket(apacket *p); |
| 170 | |
leozwang | 1be5462 | 2014-08-15 09:51:27 -0700 | [diff] [blame] | 171 | // Define it if you want to dump packets. |
| 172 | #define DEBUG_PACKETS 0 |
| 173 | |
Benoit Goby | 2cc19e4 | 2012-04-12 12:23:49 -0700 | [diff] [blame] | 174 | #if !DEBUG_PACKETS |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | #define print_packet(tag,p) do {} while (0) |
| 176 | #endif |
| 177 | |
John Michelau | 8733752 | 2010-09-23 17:08:34 -0500 | [diff] [blame] | 178 | #if ADB_HOST_ON_TARGET |
| 179 | /* adb and adbd are coexisting on the target, so use 5038 for adb |
| 180 | * to avoid conflicting with adbd's usage of 5037 |
| 181 | */ |
| 182 | # define DEFAULT_ADB_PORT 5038 |
| 183 | #else |
| 184 | # define DEFAULT_ADB_PORT 5037 |
| 185 | #endif |
| 186 | |
Stefan Hilzinger | 92ca4fa | 2010-04-19 12:21:12 +0100 | [diff] [blame] | 187 | #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 188 | |
Xavier Ducrohet | 30a4033 | 2009-05-21 17:47:43 -0700 | [diff] [blame] | 189 | #define ADB_CLASS 0xff |
| 190 | #define ADB_SUBCLASS 0x42 |
| 191 | #define ADB_PROTOCOL 0x1 |
Dima Zavin | 3e82491 | 2009-05-08 18:25:58 -0700 | [diff] [blame] | 192 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | |
Mike Lockwood | a8b3875 | 2009-08-26 12:50:22 -0700 | [diff] [blame] | 194 | void local_init(int port); |
Yabin Cui | f401ead | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 195 | bool local_connect(int port); |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 196 | int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 197 | |
Dan Albert | 9a50f4c | 2015-05-18 16:43:57 -0700 | [diff] [blame] | 198 | ConnectionState connection_state(atransport *t); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 200 | extern const char* adb_device_banner; |
| 201 | |
Alex Vallée | 436fa6b | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 202 | #if !ADB_HOST |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 203 | extern int SHELL_EXIT_NOTIFY_FD; |
Alex Vallée | 436fa6b | 2015-07-17 15:30:59 -0400 | [diff] [blame] | 204 | #endif // !ADB_HOST |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | |
| 206 | #define CHUNK_SIZE (64*1024) |
| 207 | |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 208 | #if !ADB_HOST |
Andrzej Pietrasiewicz | d7270f2 | 2012-01-13 15:13:46 +0100 | [diff] [blame] | 209 | #define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" |
| 210 | #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x |
| 211 | |
| 212 | #define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) |
| 213 | #define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) |
| 214 | #define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) |
| 215 | #endif |
| 216 | |
Elliott Hughes | 9d07fee | 2015-05-07 21:38:41 -0700 | [diff] [blame] | 217 | int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 218 | |
Dan Albert | 056ad0e | 2015-02-18 17:47:33 -0800 | [diff] [blame] | 219 | void handle_online(atransport *t); |
| 220 | void handle_offline(atransport *t); |
| 221 | |
| 222 | void send_connect(atransport *t); |
| 223 | |
Dan Albert | be8e54b | 2015-05-18 13:06:53 -0700 | [diff] [blame] | 224 | void parse_banner(const std::string&, atransport* t); |
| 225 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | #endif |