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> 21#include <stdint.h> 22#include <sys/types.h> 23 24#include <string> 25 26#include <android-base/macros.h> 27 28#include "adb_trace.h" 29#include "fdevent.h" 30#include "socket.h" 31#include "usb.h" 32 33constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024; 34constexpr size_t MAX_PAYLOAD_V2 = 256 * 1024; 35constexpr size_t MAX_PAYLOAD = MAX_PAYLOAD_V2; 36 37constexpr size_t LINUX_MAX_SOCKET_SIZE = 4194304; 38 39#define A_SYNC 0x434e5953 40#define A_CNXN 0x4e584e43 41#define A_OPEN 0x4e45504f 42#define A_OKAY 0x59414b4f 43#define A_CLSE 0x45534c43 44#define A_WRTE 0x45545257 45#define A_AUTH 0x48545541 46 47// ADB protocol version. 48#define A_VERSION 0x01000000 49 50// Used for help/version information. 51#define ADB_VERSION_MAJOR 1 52#define ADB_VERSION_MINOR 0 53 54std::string adb_version(); 55 56// Increment this when we want to force users to start a new adb server. 57#define ADB_SERVER_VERSION 39 58 59using TransportId = uint64_t; 60class atransport; 61 62struct amessage { 63 uint32_t command; /* command identifier constant */ 64 uint32_t arg0; /* first argument */ 65 uint32_t arg1; /* second argument */ 66 uint32_t data_length; /* length of payload (0 is allowed) */ 67 uint32_t data_check; /* checksum of data payload */ 68 uint32_t magic; /* command ^ 0xffffffff */ 69}; 70 71struct apacket 72{ 73 apacket *next; 74 75 size_t len; 76 char* ptr; 77 78 amessage msg; 79 char data[MAX_PAYLOAD]; 80}; 81 82uint32_t calculate_apacket_checksum(const apacket* packet); 83 84/* the adisconnect structure is used to record a callback that 85** will be called whenever a transport is disconnected (e.g. by the user) 86** this should be used to cleanup objects that depend on the 87** transport (e.g. remote sockets, listeners, etc...) 88*/ 89struct adisconnect 90{ 91 void (*func)(void* opaque, atransport* t); 92 void* opaque; 93}; 94 95 96// A transport object models the connection to a remote device or emulator there 97// is one transport per connected device/emulator. A "local transport" connects 98// through TCP (for the emulator), while a "usb transport" through USB (for real 99// devices). 100// 101// Note that kTransportHost doesn't really correspond to a real transport 102// object, it's a special value used to indicate that a client wants to connect 103// to a service implemented within the ADB server itself. 104enum TransportType { 105 kTransportUsb, 106 kTransportLocal, 107 kTransportAny, 108 kTransportHost, 109}; 110 111#define TOKEN_SIZE 20 112 113enum ConnectionState { 114 kCsAny = -1, 115 kCsOffline = 0, 116 kCsBootloader, 117 kCsDevice, 118 kCsHost, 119 kCsRecovery, 120 kCsNoPerm, // Insufficient permissions to communicate with the device. 121 kCsSideload, 122 kCsUnauthorized, 123}; 124 125 126void print_packet(const char *label, apacket *p); 127 128// These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they 129// shouldn't be tagged with ADB_FORMAT_ARCHETYPE. 130void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); 131void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2))); 132 133void handle_packet(apacket *p, atransport *t); 134 135int launch_server(const std::string& socket_spec); 136int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply_fd); 137 138/* initialize a transport object's func pointers and state */ 139#if ADB_HOST 140int get_available_local_transport_index(); 141#endif 142int init_socket_transport(atransport *t, int s, int port, int local); 143void init_usb_transport(atransport* t, usb_handle* usb); 144 145std::string getEmulatorSerialString(int console_port); 146#if ADB_HOST 147atransport* find_emulator_transport_by_adb_port(int adb_port); 148atransport* find_emulator_transport_by_console_port(int console_port); 149#endif 150 151int service_to_fd(const char* name, const atransport* transport); 152#if ADB_HOST 153asocket* host_service_to_socket(const char* name, const char* serial, TransportId transport_id); 154#endif 155 156#if !ADB_HOST 157int init_jdwp(void); 158asocket* create_jdwp_service_socket(); 159asocket* create_jdwp_tracker_service_socket(); 160int create_jdwp_connection_fd(int jdwp_pid); 161#endif 162 163int handle_forward_request(const char* service, TransportType type, const char* serial, 164 TransportId transport_id, int reply_fd); 165 166#if !ADB_HOST 167void framebuffer_service(int fd, void *cookie); 168void set_verity_enabled_state_service(int fd, void* cookie); 169#endif 170 171/* packet allocator */ 172apacket *get_apacket(void); 173void put_apacket(apacket *p); 174 175// Define it if you want to dump packets. 176#define DEBUG_PACKETS 0 177 178#if !DEBUG_PACKETS 179#define print_packet(tag,p) do {} while (0) 180#endif 181 182#if ADB_HOST_ON_TARGET 183/* adb and adbd are coexisting on the target, so use 5038 for adb 184 * to avoid conflicting with adbd's usage of 5037 185 */ 186# define DEFAULT_ADB_PORT 5038 187#else 188# define DEFAULT_ADB_PORT 5037 189#endif 190 191#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 192 193#define ADB_CLASS 0xff 194#define ADB_SUBCLASS 0x42 195#define ADB_PROTOCOL 0x1 196 197 198void local_init(int port); 199bool local_connect(int port); 200int local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error); 201 202ConnectionState connection_state(atransport *t); 203 204extern const char* adb_device_banner; 205 206#if !ADB_HOST 207extern int SHELL_EXIT_NOTIFY_FD; 208#endif // !ADB_HOST 209 210#define CHUNK_SIZE (64*1024) 211 212#if !ADB_HOST 213#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" 214#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x 215 216#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) 217#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) 218#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) 219#endif 220 221int handle_host_request(const char* service, TransportType type, const char* serial, 222 TransportId transport_id, int reply_fd, asocket* s); 223 224void handle_online(atransport *t); 225void handle_offline(atransport *t); 226 227void send_connect(atransport *t); 228 229void parse_banner(const std::string&, atransport* t); 230 231// On startup, the adb server needs to wait until all of the connected devices are ready. 232// To do this, we need to know when the scan has identified all of the potential new transports, and 233// when each transport becomes ready. 234// TODO: Do this for mDNS as well, instead of just USB? 235 236// We've found all of the transports we potentially care about. 237void adb_notify_device_scan_complete(); 238 239// One or more transports have changed status, check to see if we're ready. 240void update_transport_status(); 241 242// Wait until device scan has completed and every transport is ready, or a timeout elapses. 243void adb_wait_for_device_initialization(); 244 245#endif 246