2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "transport.h" /* readx(), writex() */
24 #define MAX_PAYLOAD 4096
26 #define A_SYNC 0x434e5953
27 #define A_CNXN 0x4e584e43
28 #define A_OPEN 0x4e45504f
29 #define A_OKAY 0x59414b4f
30 #define A_CLSE 0x45534c43
31 #define A_WRTE 0x45545257
33 #define A_VERSION 0x01000000 // SDB protocol version
35 #define SDB_VERSION_MAJOR 2 // Used for help/version information
36 #define SDB_VERSION_MINOR 0 // Used for help/version information
38 #define SDB_SERVER_VERSION 2 // Increment this when we want to force users to start a new sdb server
40 typedef struct amessage amessage;
41 typedef struct apacket apacket;
42 typedef struct asocket asocket;
43 typedef struct alistener alistener;
44 typedef struct aservice aservice;
45 typedef struct atransport atransport;
46 typedef struct adisconnect adisconnect;
47 typedef struct usb_handle usb_handle;
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 */
66 unsigned char data[MAX_PAYLOAD];
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.
74 /* chain pointers for the local/remote list of
75 ** asockets that this asocket lives in
80 /* the unique identifier for this asocket
84 /* flag: set when the socket's peer has closed
85 ** but packets are still queued for delivery
89 /* flag: quit adbd when both ends close the
90 ** local service socket
94 /* the asocket we are connected to
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.
106 /* queue of apackets waiting to be written
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
117 int (*enqueue)(asocket *s, apacket *pkt);
119 /* ready is called by the peer when it is ready for
120 ** us to send data via enqueue again
122 void (*ready)(asocket *s);
124 /* close is called by the peer when it has gone away.
125 ** we are not allowed to make any further calls on the
126 ** peer once our close method is called.
128 void (*close)(asocket *s);
130 /* socket-type-specific extradata */
133 /* A socket is bound to atransport */
134 atransport *transport;
138 /* the adisconnect structure is used to record a callback that
139 ** will be called whenever a transport is disconnected (e.g. by the user)
140 ** this should be used to cleanup objects that depend on the
141 ** transport (e.g. remote sockets, listeners, etc...)
145 void (*func)(void* opaque, atransport* t);
152 /* a transport object models the connection to a remote device or emulator
153 ** there is one transport per connected device/emulator. a "local transport"
154 ** connects through TCP (for the emulator), while a "usb transport" through
155 ** USB (for real devices)
157 ** note that kTransportHost doesn't really correspond to a real transport
158 ** object, it's a special value used to indicate that a client wants to
159 ** connect to a service implemented within the SDB server itself.
161 typedef enum transport_type {
173 int (*read_from_remote)(apacket *p, atransport *t);
174 int (*write_to_remote)(apacket *p, atransport *t);
175 void (*close)(atransport *t);
176 void (*kick)(atransport *t);
179 int transport_socket;
180 fdevent transport_fde;
183 int connection_state;
186 /* usb handle or socket fd as needed */
190 /* used to identify transports for clients */
193 int sdb_port; // Use for emulators (local transport)
194 char *device_name; // for connection explorer
196 /* a list of adisconnect callbacks called when the transport is kicked */
198 adisconnect disconnects;
202 /* A listener is an entity which binds to a local port
203 ** and, upon receiving a connection on that port, creates
204 ** an asocket to connect the new local connection to a
205 ** specific remote service.
207 ** TODO: some listeners read from the new connection to
208 ** determine what exact service to connect to on the far
219 const char *local_name;
220 const char *connect_to;
221 atransport *transport;
222 adisconnect disconnect;
226 void print_packet(const char *label, apacket *p);
228 asocket *find_local_socket(unsigned id);
229 void install_local_socket(asocket *s);
230 void remove_socket(asocket *s);
231 void close_all_sockets(atransport *t);
233 #define LOCAL_CLIENT_PREFIX "emulator-"
235 asocket *create_local_socket(int fd);
236 asocket *create_local_service_socket(const char *destination);
238 asocket *create_remote_socket(unsigned id, atransport *t);
239 void connect_to_remote(asocket *s, const char *destination);
240 void connect_to_smartsocket(asocket *s);
242 void fatal(const char *fmt, ...);
243 void fatal_errno(const char *fmt, ...);
245 void handle_packet(apacket *p, atransport *t);
246 void send_packet(apacket *p, atransport *t);
248 void get_my_path(char *s, size_t maxLen);
249 int launch_server(int server_port);
250 int sdb_main(int is_daemon, int server_port);
253 /* transports are ref-counted
254 ** get_device_transport does an acquire on your behalf before returning
256 void init_transport_registration(void);
257 int list_transports(char *buf, size_t bufsize);
258 void update_transports(void);
260 asocket* create_device_tracker(void);
262 /* Obtain a transport from the available transports.
263 ** If state is != CS_ANY, only transports in that state are considered.
264 ** If serial is non-NULL then only the device with that serial will be chosen.
265 ** If no suitable transport is found, error is set.
267 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
268 void add_transport_disconnect( atransport* t, adisconnect* dis );
269 void remove_transport_disconnect( atransport* t, adisconnect* dis );
270 void run_transport_disconnects( atransport* t );
271 void kick_transport( atransport* t );
273 /* initialize a transport object's func pointers and state */
275 int get_available_local_transport_index();
277 int init_socket_transport(atransport *t, int s, int port, int local);
278 void init_usb_transport(atransport *t, usb_handle *usb, int state);
280 /* for MacOS X cleanup */
281 void close_usb_devices();
283 /* cause new transports to be init'd and added to the list */
284 void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name);
286 /* these should only be used for the "sdb disconnect" command */
287 void unregister_transport(atransport *t);
288 void unregister_all_tcp_transports();
290 void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
292 /* this should only be used for transports with connection_state == CS_NOPERM */
293 void unregister_usb_transport(usb_handle *usb);
295 atransport *find_transport(const char *serial);
297 atransport* find_emulator_transport_by_sdb_port(int sdb_port);
300 int service_to_fd(const char *name);
302 asocket *host_service_to_socket(const char* name, const char *serial);
307 asocket* create_jdwp_service_socket();
308 asocket* create_jdwp_tracker_service_socket();
309 int create_jdwp_connection_fd(int jdwp_pid);
317 int backup_service(BackupOperation operation, char* args);
318 void framebuffer_service(int fd, void *cookie);
319 void log_service(int fd, void *cookie);
320 void remount_service(int fd, void *cookie);
321 char * get_log_file_path(const char * log_name);
324 /* packet allocator */
325 apacket *get_apacket(void);
326 void put_apacket(apacket *p);
328 int check_header(apacket *p);
329 int check_data(apacket *p);
331 /* define SDB_TRACE to 1 to enable tracing support, or 0 to disable it */
335 /* IMPORTANT: if you change the following list, don't
336 * forget to update the corresponding 'tags' table in
337 * the sdb_trace_init() function implemented in sdb.c
357 * When running inside the emulator, guest's sdbd can connect to 'sdb-debug'
358 * qemud service that can display sdb trace messages (on condition that emulator
359 * has been started with '-debug sdb' option).
362 /* Delivers a trace message to the emulator via QEMU pipe. */
363 void sdb_qemu_trace(const char* fmt, ...);
364 /* Macro to use to send SDB trace messages to the emulator. */
365 #define DQ(...) sdb_qemu_trace(__VA_ARGS__)
367 #define DQ(...) ((void)0)
368 #endif /* !SDB_HOST */
370 extern int sdb_trace_mask;
371 extern unsigned char sdb_trace_output_count;
372 void sdb_trace_init(void);
374 # define SDB_TRACING ((sdb_trace_mask & (1 << TRACE_TAG)) != 0)
376 /* you must define TRACE_TAG before using this macro */
380 int save_errno = errno; \
381 sdb_mutex_lock(&D_lock); \
382 fprintf(stderr, "%s::%s():", \
383 __FILE__, __FUNCTION__); \
384 errno = save_errno; \
385 fprintf(stderr, __VA_ARGS__ ); \
387 sdb_mutex_unlock(&D_lock); \
388 errno = save_errno; \
394 int save_errno = errno; \
395 sdb_mutex_lock(&D_lock); \
396 errno = save_errno; \
397 fprintf(stderr, __VA_ARGS__ ); \
399 sdb_mutex_unlock(&D_lock); \
400 errno = save_errno; \
404 # define D(...) ((void)0)
405 # define DR(...) ((void)0)
406 # define SDB_TRACING 0
411 #define print_packet(tag,p) do {} while (0)
414 #if SDB_HOST_ON_TARGET
415 /* sdb and sdbd are coexisting on the target, so use 26099 for sdb
416 * to avoid conflicting with sdbd's usage of 26098
418 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
420 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
423 #define DEFAULT_SDB_LOCAL_TRANSPORT_PORT 26101 /* tizen specific */
425 #define SDB_CLASS 0xff
426 #define SDB_SUBCLASS 0x20 //0x42 /* tizen specific */
427 #define SDB_PROTOCOL 0x02 //0x01 /* tizen specific */
430 void local_init(int port);
431 int local_connect(int port, const char *device_name);
432 int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
434 /* usb host/client interface */
437 int usb_write(usb_handle *h, const void *data, int len);
438 int usb_read(usb_handle *h, void *data, int len);
439 int usb_close(usb_handle *h);
440 void usb_kick(usb_handle *h);
442 /* used for USB device detection */
444 int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
447 unsigned host_to_le32(unsigned n);
448 int sdb_commandline(int argc, char **argv);
450 int connection_state(atransport *t);
454 #define CS_BOOTLOADER 1
457 #define CS_RECOVERY 4
458 #define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
459 #define CS_SIDELOAD 6
462 extern int SHELL_EXIT_NOTIFY_FD;
464 #define CHUNK_SIZE (64*1024)
466 int sendfailmsg(int fd, const char *reason);
467 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
469 #if SDB_HOST /* tizen-specific */
470 #define DEVICEMAP_SEPARATOR ":"
471 #define DEVICENAME_MAX 256
472 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above
473 #define DEFAULT_DEVICENAME "<unknown>"
474 void register_device_name(const char *device_type, const char *device_name, int port);
475 int get_devicename_from_shdmem(int port, char *device_name);
476 int read_line(const int fd, char* ptr, const size_t maxlen);