• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "adb_trace.h"
24 #include "fdevent.h"
25 
26 #define MAX_PAYLOAD 4096
27 
28 #define A_SYNC 0x434e5953
29 #define A_CNXN 0x4e584e43
30 #define A_OPEN 0x4e45504f
31 #define A_OKAY 0x59414b4f
32 #define A_CLSE 0x45534c43
33 #define A_WRTE 0x45545257
34 #define A_AUTH 0x48545541
35 
36 // ADB protocol version.
37 #define A_VERSION 0x01000000
38 
39 // Used for help/version information.
40 #define ADB_VERSION_MAJOR 1
41 #define ADB_VERSION_MINOR 0
42 
43 // Increment this when we want to force users to start a new adb server.
44 #define ADB_SERVER_VERSION 32
45 
46 struct atransport;
47 struct usb_handle;
48 
49 struct amessage {
50     unsigned command;       /* command identifier constant      */
51     unsigned arg0;          /* first argument                   */
52     unsigned arg1;          /* second argument                  */
53     unsigned data_length;   /* length of payload (0 is allowed) */
54     unsigned data_check;    /* checksum of data payload         */
55     unsigned magic;         /* command ^ 0xffffffff             */
56 };
57 
58 struct apacket
59 {
60     apacket *next;
61 
62     unsigned len;
63     unsigned char *ptr;
64 
65     amessage msg;
66     unsigned char data[MAX_PAYLOAD];
67 };
68 
69 /* An asocket represents one half of a connection between a local and
70 ** remote entity.  A local asocket is bound to a file descriptor.  A
71 ** remote asocket is bound to the protocol engine.
72 */
73 struct asocket {
74         /* chain pointers for the local/remote list of
75         ** asockets that this asocket lives in
76         */
77     asocket *next;
78     asocket *prev;
79 
80         /* the unique identifier for this asocket
81         */
82     unsigned id;
83 
84         /* flag: set when the socket's peer has closed
85         ** but packets are still queued for delivery
86         */
87     int    closing;
88 
89         /* flag: quit adbd when both ends close the
90         ** local service socket
91         */
92     int    exit_on_close;
93 
94         /* the asocket we are connected to
95         */
96 
97     asocket *peer;
98 
99         /* For local asockets, the fde is used to bind
100         ** us to our fd event system.  For remote asockets
101         ** these fields are not used.
102         */
103     fdevent fde;
104     int fd;
105 
106         /* queue of apackets waiting to be written
107         */
108     apacket *pkt_first;
109     apacket *pkt_last;
110 
111         /* enqueue is called by our peer when it has data
112         ** for us.  It should return 0 if we can accept more
113         ** data or 1 if not.  If we return 1, we must call
114         ** peer->ready() when we once again are ready to
115         ** receive data.
116         */
117     int (*enqueue)(asocket *s, apacket *pkt);
118 
119         /* ready is called by the peer when it is ready for
120         ** us to send data via enqueue again
121         */
122     void (*ready)(asocket *s);
123 
124         /* shutdown is called by the peer before it goes away.
125         ** the socket should not do any further calls on its peer.
126         ** Always followed by a call to close. Optional, i.e. can be NULL.
127         */
128     void (*shutdown)(asocket *s);
129 
130         /* close is called by the peer when it has gone away.
131         ** we are not allowed to make any further calls on the
132         ** peer once our close method is called.
133         */
134     void (*close)(asocket *s);
135 
136         /* A socket is bound to atransport */
137     atransport *transport;
138 };
139 
140 
141 /* the adisconnect structure is used to record a callback that
142 ** will be called whenever a transport is disconnected (e.g. by the user)
143 ** this should be used to cleanup objects that depend on the
144 ** transport (e.g. remote sockets, listeners, etc...)
145 */
146 struct  adisconnect
147 {
148     void        (*func)(void*  opaque, atransport*  t);
149     void*         opaque;
150     adisconnect*  next;
151     adisconnect*  prev;
152 };
153 
154 
155 /* a transport object models the connection to a remote device or emulator
156 ** there is one transport per connected device/emulator. a "local transport"
157 ** connects through TCP (for the emulator), while a "usb transport" through
158 ** USB (for real devices)
159 **
160 ** note that kTransportHost doesn't really correspond to a real transport
161 ** object, it's a special value used to indicate that a client wants to
162 ** connect to a service implemented within the ADB server itself.
163 */
164 enum transport_type {
165         kTransportUsb,
166         kTransportLocal,
167         kTransportAny,
168         kTransportHost,
169 };
170 
171 #define TOKEN_SIZE 20
172 
173 struct atransport
174 {
175     atransport *next;
176     atransport *prev;
177 
178     int (*read_from_remote)(apacket *p, atransport *t);
179     int (*write_to_remote)(apacket *p, atransport *t);
180     void (*close)(atransport *t);
181     void (*kick)(atransport *t);
182 
183     int fd;
184     int transport_socket;
185     fdevent transport_fde;
186     int ref_count;
187     unsigned sync_token;
188     int connection_state;
189     int online;
190     transport_type type;
191 
192         /* usb handle or socket fd as needed */
193     usb_handle *usb;
194     int sfd;
195 
196         /* used to identify transports for clients */
197     char *serial;
198     char *product;
199     char *model;
200     char *device;
201     char *devpath;
202     int adb_port; // Use for emulators (local transport)
203 
204         /* a list of adisconnect callbacks called when the transport is kicked */
205     int          kicked;
206     adisconnect  disconnects;
207 
208     void *key;
209     unsigned char token[TOKEN_SIZE];
210     fdevent auth_fde;
211     unsigned failed_auth_attempts;
212 
213     const char* connection_state_name() const;
214 };
215 
216 
217 /* A listener is an entity which binds to a local port
218 ** and, upon receiving a connection on that port, creates
219 ** an asocket to connect the new local connection to a
220 ** specific remote service.
221 **
222 ** TODO: some listeners read from the new connection to
223 ** determine what exact service to connect to on the far
224 ** side.
225 */
226 struct alistener
227 {
228     alistener *next;
229     alistener *prev;
230 
231     fdevent fde;
232     int fd;
233 
234     char *local_name;
235     char *connect_to;
236     atransport *transport;
237     adisconnect  disconnect;
238 };
239 
240 
241 void print_packet(const char *label, apacket *p);
242 
243 asocket *find_local_socket(unsigned local_id, unsigned remote_id);
244 void install_local_socket(asocket *s);
245 void remove_socket(asocket *s);
246 void close_all_sockets(atransport *t);
247 
248 asocket *create_local_socket(int fd);
249 asocket *create_local_service_socket(const char *destination);
250 
251 asocket *create_remote_socket(unsigned id, atransport *t);
252 void connect_to_remote(asocket *s, const char *destination);
253 void connect_to_smartsocket(asocket *s);
254 
255 void fatal(const char *fmt, ...);
256 void fatal_errno(const char *fmt, ...);
257 
258 void handle_packet(apacket *p, atransport *t);
259 
260 void get_my_path(char *s, size_t maxLen);
261 int launch_server(int server_port);
262 int adb_main(int is_daemon, int server_port);
263 
264 /* initialize a transport object's func pointers and state */
265 #if ADB_HOST
266 int get_available_local_transport_index();
267 #endif
268 int  init_socket_transport(atransport *t, int s, int port, int local);
269 void init_usb_transport(atransport *t, usb_handle *usb, int state);
270 
271 #if ADB_HOST
272 atransport* find_emulator_transport_by_adb_port(int adb_port);
273 #endif
274 
275 int service_to_fd(const char *name);
276 #if ADB_HOST
277 asocket *host_service_to_socket(const char*  name, const char *serial);
278 #endif
279 
280 #if !ADB_HOST
281 int       init_jdwp(void);
282 asocket*  create_jdwp_service_socket();
283 asocket*  create_jdwp_tracker_service_socket();
284 int       create_jdwp_connection_fd(int  jdwp_pid);
285 #endif
286 
287 int handle_forward_request(const char* service, transport_type ttype, char* serial, int reply_fd);
288 
289 #if !ADB_HOST
290 void framebuffer_service(int fd, void *cookie);
291 void set_verity_enabled_state_service(int fd, void* cookie);
292 #endif
293 
294 /* packet allocator */
295 apacket *get_apacket(void);
296 void put_apacket(apacket *p);
297 
298 // Define it if you want to dump packets.
299 #define DEBUG_PACKETS 0
300 
301 #if !DEBUG_PACKETS
302 #define print_packet(tag,p) do {} while (0)
303 #endif
304 
305 #if ADB_HOST_ON_TARGET
306 /* adb and adbd are coexisting on the target, so use 5038 for adb
307  * to avoid conflicting with adbd's usage of 5037
308  */
309 #  define DEFAULT_ADB_PORT 5038
310 #else
311 #  define DEFAULT_ADB_PORT 5037
312 #endif
313 
314 #define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555
315 
316 #define ADB_CLASS              0xff
317 #define ADB_SUBCLASS           0x42
318 #define ADB_PROTOCOL           0x1
319 
320 
321 void local_init(int port);
322 int  local_connect(int  port);
323 int  local_connect_arbitrary_ports(int console_port, int adb_port);
324 
325 /* usb host/client interface */
326 void usb_init();
327 void usb_cleanup();
328 int usb_write(usb_handle *h, const void *data, int len);
329 int usb_read(usb_handle *h, void *data, int len);
330 int usb_close(usb_handle *h);
331 void usb_kick(usb_handle *h);
332 
333 /* used for USB device detection */
334 #if ADB_HOST
335 int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
336 #endif
337 
338 int adb_commandline(int argc, const char **argv);
339 
340 int connection_state(atransport *t);
341 
342 #define CS_ANY       -1
343 #define CS_OFFLINE    0
344 #define CS_BOOTLOADER 1
345 #define CS_DEVICE     2
346 #define CS_HOST       3
347 #define CS_RECOVERY   4
348 #define CS_NOPERM     5 /* Insufficient permissions to communicate with the device */
349 #define CS_SIDELOAD   6
350 #define CS_UNAUTHORIZED 7
351 
352 extern const char *adb_device_banner;
353 extern int HOST;
354 extern int SHELL_EXIT_NOTIFY_FD;
355 
356 enum subproc_mode {
357     SUBPROC_PTY = 0,
358     SUBPROC_RAW = 1,
359 } ;
360 
361 #define CHUNK_SIZE (64*1024)
362 
363 #if !ADB_HOST
364 #define USB_ADB_PATH     "/dev/android_adb"
365 
366 #define USB_FFS_ADB_PATH  "/dev/usb-ffs/adb/"
367 #define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x
368 
369 #define USB_FFS_ADB_EP0   USB_FFS_ADB_EP(ep0)
370 #define USB_FFS_ADB_OUT   USB_FFS_ADB_EP(ep1)
371 #define USB_FFS_ADB_IN    USB_FFS_ADB_EP(ep2)
372 #endif
373 
374 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
375 
376 void handle_online(atransport *t);
377 void handle_offline(atransport *t);
378 
379 void send_connect(atransport *t);
380 
381 #endif
382