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 <sys/types.h>
     22 
     23 #include <string>
     24 
     25 #include <android-base/macros.h>
     26 
     27 #include "adb_trace.h"
     28 #include "fdevent.h"
     29 #include "socket.h"
     30 
     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;
     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
     41 #define A_AUTH 0x48545541
     42 
     43 // ADB protocol version.
     44 #define A_VERSION 0x01000000
     45 
     46 // Used for help/version information.
     47 #define ADB_VERSION_MAJOR 1
     48 #define ADB_VERSION_MINOR 0
     49 
     50 std::string adb_version();
     51 
     52 // Increment this when we want to force users to start a new adb server.
     53 #define ADB_SERVER_VERSION 36
     54 
     55 class atransport;
     56 struct usb_handle;
     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 
     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;
     87 };
     88 
     89 
     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.
     98 enum TransportType {
     99     kTransportUsb,
    100     kTransportLocal,
    101     kTransportAny,
    102     kTransportHost,
    103 };
    104 
    105 #define TOKEN_SIZE 20
    106 
    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 
    119 /* A listener is an entity which binds to a local port
    120 ** and, upon receiving a connection on that port, creates
    121 ** an asocket to connect the new local connection to a
    122 ** specific remote service.
    123 **
    124 ** TODO: some listeners read from the new connection to
    125 ** determine what exact service to connect to on the far
    126 ** side.
    127 */
    128 struct alistener
    129 {
    130     alistener *next;
    131     alistener *prev;
    132 
    133     fdevent fde;
    134     int fd;
    135 
    136     char *local_name;
    137     char *connect_to;
    138     atransport *transport;
    139     adisconnect  disconnect;
    140 };
    141 
    142 
    143 void print_packet(const char *label, apacket *p);
    144 
    145 // These use the system (v)fprintf, not the adb prefixed ones defined in sysdeps.h, so they
    146 // shouldn't be tagged with ADB_FORMAT_ARCHETYPE.
    147 void fatal(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
    148 void fatal_errno(const char* fmt, ...) __attribute__((noreturn, format(__printf__, 1, 2)));
    149 
    150 void handle_packet(apacket *p, atransport *t);
    151 
    152 void get_my_path(char *s, size_t maxLen);
    153 int launch_server(int server_port);
    154 int adb_server_main(int is_daemon, int server_port, int ack_reply_fd);
    155 
    156 /* initialize a transport object's func pointers and state */
    157 #if ADB_HOST
    158 int get_available_local_transport_index();
    159 #endif
    160 int  init_socket_transport(atransport *t, int s, int port, int local);
    161 void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state);
    162 
    163 std::string getEmulatorSerialString(int console_port);
    164 #if ADB_HOST
    165 atransport* find_emulator_transport_by_adb_port(int adb_port);
    166 atransport* find_emulator_transport_by_console_port(int console_port);
    167 #endif
    168 
    169 int service_to_fd(const char* name, const atransport* transport);
    170 #if ADB_HOST
    171 asocket *host_service_to_socket(const char*  name, const char *serial);
    172 #endif
    173 
    174 #if !ADB_HOST
    175 int       init_jdwp(void);
    176 asocket*  create_jdwp_service_socket();
    177 asocket*  create_jdwp_tracker_service_socket();
    178 int       create_jdwp_connection_fd(int  jdwp_pid);
    179 #endif
    180 
    181 int handle_forward_request(const char* service, TransportType type, const char* serial, int reply_fd);
    182 
    183 #if !ADB_HOST
    184 void framebuffer_service(int fd, void *cookie);
    185 void set_verity_enabled_state_service(int fd, void* cookie);
    186 #endif
    187 
    188 /* packet allocator */
    189 apacket *get_apacket(void);
    190 void put_apacket(apacket *p);
    191 
    192 // Define it if you want to dump packets.
    193 #define DEBUG_PACKETS 0
    194 
    195 #if !DEBUG_PACKETS
    196 #define print_packet(tag,p) do {} while (0)
    197 #endif
    198 
    199 #if ADB_HOST_ON_TARGET
    200 /* adb and adbd are coexisting on the target, so use 5038 for adb
    201  * to avoid conflicting with adbd's usage of 5037
    202  */
    203 #  define DEFAULT_ADB_PORT 5038
    204 #else
    205 #  define DEFAULT_ADB_PORT 5037
    206 #endif
    207 
    208 #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
    209 
    210 #define ADB_CLASS              0xff
    211 #define ADB_SUBCLASS           0x42
    212 #define ADB_PROTOCOL           0x1
    213 
    214 
    215 void local_init(int port);
    216 void local_connect(int port);
    217 int  local_connect_arbitrary_ports(int console_port, int adb_port, std::string* error);
    218 
    219 // USB host/client interface.
    220 void usb_init();
    221 int usb_write(usb_handle *h, const void *data, int len);
    222 int usb_read(usb_handle *h, void *data, int len);
    223 int usb_close(usb_handle *h);
    224 void usb_kick(usb_handle *h);
    225 
    226 // USB device detection.
    227 #if ADB_HOST
    228 int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
    229 #endif
    230 
    231 ConnectionState connection_state(atransport *t);
    232 
    233 extern const char* adb_device_banner;
    234 
    235 #if !ADB_HOST
    236 extern int SHELL_EXIT_NOTIFY_FD;
    237 #endif // !ADB_HOST
    238 
    239 #define CHUNK_SIZE (64*1024)
    240 
    241 #if !ADB_HOST
    242 #define USB_ADB_PATH     "/dev/android_adb"
    243 
    244 #define USB_FFS_ADB_PATH  "/dev/usb-ffs/adb/"
    245 #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
    246 
    247 #define USB_FFS_ADB_EP0   USB_FFS_ADB_EP(ep0)
    248 #define USB_FFS_ADB_OUT   USB_FFS_ADB_EP(ep1)
    249 #define USB_FFS_ADB_IN    USB_FFS_ADB_EP(ep2)
    250 #endif
    251 
    252 int handle_host_request(const char* service, TransportType type, const char* serial, int reply_fd, asocket *s);
    253 
    254 void handle_online(atransport *t);
    255 void handle_offline(atransport *t);
    256 
    257 void send_connect(atransport *t);
    258 
    259 void parse_banner(const std::string&, atransport* t);
    260 
    261 #endif
    262