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