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