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.
23 #include "transport.h" /* readx(), writex() */
26 #include "commandline_sdbd.h"
28 #include <tzplatform_config.h>
30 #define MAX_PAYLOAD 4096
32 #define A_SYNC 0x434e5953
33 #define A_CNXN 0x4e584e43
34 #define A_OPEN 0x4e45504f
35 #define A_OKAY 0x59414b4f
36 #define A_CLSE 0x45534c43
37 #define A_WRTE 0x45545257
39 #define A_VERSION 0x01000000 // SDB protocol version
41 #define SDB_VERSION_MAJOR 2 // Used for help/version information
42 #define SDB_VERSION_MINOR 1 // Used for help/version information
44 #define SDB_SERVER_VERSION 0 // Increment this when we want to force users to start a new sdb server
46 typedef struct amessage amessage;
47 typedef struct apacket apacket;
48 typedef struct asocket asocket;
49 typedef struct alistener alistener;
50 typedef struct aservice aservice;
51 typedef struct atransport atransport;
52 typedef struct adisconnect adisconnect;
53 typedef struct usb_handle usb_handle;
56 unsigned command; /* command identifier constant */
57 unsigned arg0; /* first argument */
58 unsigned arg1; /* second argument */
59 unsigned data_length; /* length of payload (0 is allowed) */
60 unsigned data_check; /* checksum of data payload */
61 unsigned magic; /* command ^ 0xffffffff */
72 unsigned char data[MAX_PAYLOAD];
75 /* An asocket represents one half of a connection between a local and
76 ** remote entity. A local asocket is bound to a file descriptor. A
77 ** remote asocket is bound to the protocol engine.
80 /* chain pointers for the local/remote list of
81 ** asockets that this asocket lives in
86 /* the unique identifier for this asocket
90 /* flag: set when the socket's peer has closed
91 ** but packets are still queued for delivery
95 /* flag: quit adbd when both ends close the
96 ** local service socket
100 /* the asocket we are connected to
105 /* For local asockets, the fde is used to bind
106 ** us to our fd event system. For remote asockets
107 ** these fields are not used.
112 /* queue of apackets waiting to be written
117 /* enqueue is called by our peer when it has data
118 ** for us. It should return 0 if we can accept more
119 ** data or 1 if not. If we return 1, we must call
120 ** peer->ready() when we once again are ready to
123 int (*enqueue)(asocket *s, apacket *pkt);
125 /* ready is called by the peer when it is ready for
126 ** us to send data via enqueue again
128 void (*ready)(asocket *s);
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.
134 void (*close)(asocket *s);
136 /* socket-type-specific extradata */
139 /* A socket is bound to atransport */
140 atransport *transport;
144 /* the adisconnect structure is used to record a callback that
145 ** will be called whenever a transport is disconnected (e.g. by the user)
146 ** this should be used to cleanup objects that depend on the
147 ** transport (e.g. remote sockets, listeners, etc...)
151 void (*func)(void* opaque, atransport* t);
158 /* a transport object models the connection to a remote device or emulator
159 ** there is one transport per connected device/emulator. a "local transport"
160 ** connects through TCP (for the emulator), while a "usb transport" through
161 ** USB (for real devices)
163 ** note that kTransportHost doesn't really correspond to a real transport
164 ** object, it's a special value used to indicate that a client wants to
165 ** connect to a service implemented within the SDB server itself.
167 typedef enum transport_type {
179 int (*read_from_remote)(apacket *p, atransport *t);
180 int (*write_to_remote)(apacket *p, atransport *t);
181 void (*close)(atransport *t);
182 void (*kick)(atransport *t);
185 int transport_socket;
186 fdevent transport_fde;
189 int connection_state;
192 /* usb handle or socket fd as needed */
196 /* used to identify transports for clients */
199 int sdb_port; // Use for emulators (local transport)
200 char *device_name; // for connection explorer
202 /* a list of adisconnect callbacks called when the transport is kicked */
204 adisconnect disconnects;
208 /* A listener is an entity which binds to a local port
209 ** and, upon receiving a connection on that port, creates
210 ** an asocket to connect the new local connection to a
211 ** specific remote service.
213 ** TODO: some listeners read from the new connection to
214 ** determine what exact service to connect to on the far
225 const char *local_name;
226 const char *connect_to;
227 atransport *transport;
228 adisconnect disconnect;
232 void print_packet(const char *label, apacket *p);
234 asocket *find_local_socket(unsigned id);
235 void install_local_socket(asocket *s);
236 void remove_socket(asocket *s);
237 void close_all_sockets(atransport *t);
239 #define LOCAL_CLIENT_PREFIX "emulator-"
241 asocket *create_local_socket(int fd);
242 asocket *create_local_service_socket(const char *destination);
244 asocket *create_remote_socket(unsigned id, atransport *t);
245 void connect_to_remote(asocket *s, const char *destination);
246 void connect_to_smartsocket(asocket *s);
248 void fatal(const char *fmt, ...);
249 void fatal_errno(const char *fmt, ...);
251 void handle_packet(apacket *p, atransport *t);
252 void send_packet(apacket *p, atransport *t);
254 void get_my_path(char *s, size_t maxLen);
255 int launch_server(int server_port);
256 int sdb_main(int is_daemon, int server_port);
259 /* transports are ref-counted
260 ** get_device_transport does an acquire on your behalf before returning
262 void init_transport_registration(void);
263 int list_transports(char *buf, size_t bufsize);
264 void update_transports(void);
266 asocket* create_device_tracker(void);
268 /* Obtain a transport from the available transports.
269 ** If state is != CS_ANY, only transports in that state are considered.
270 ** If serial is non-NULL then only the device with that serial will be chosen.
271 ** If no suitable transport is found, error is set.
273 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
274 void add_transport_disconnect( atransport* t, adisconnect* dis );
275 void remove_transport_disconnect( atransport* t, adisconnect* dis );
276 void run_transport_disconnects( atransport* t );
277 void kick_transport( atransport* t );
279 /* initialize a transport object's func pointers and state */
281 int get_available_local_transport_index();
283 int init_socket_transport(atransport *t, int s, int port, int local);
284 void init_usb_transport(atransport *t, usb_handle *usb, int state);
286 /* for MacOS X cleanup */
287 void close_usb_devices();
289 /* cause new transports to be init'd and added to the list */
290 void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name);
292 /* these should only be used for the "sdb disconnect" command */
293 void unregister_transport(atransport *t);
294 void unregister_all_tcp_transports();
296 void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
298 /* this should only be used for transports with connection_state == CS_NOPERM */
299 void unregister_usb_transport(usb_handle *usb);
301 atransport *find_transport(const char *serial);
303 atransport* find_emulator_transport_by_sdb_port(int sdb_port);
306 int service_to_fd(const char *name);
308 asocket *host_service_to_socket(const char* name, const char *serial);
313 asocket* create_jdwp_service_socket();
314 asocket* create_jdwp_tracker_service_socket();
315 int create_jdwp_connection_fd(int jdwp_pid);
323 int backup_service(BackupOperation operation, char* args);
324 void framebuffer_service(int fd, void *cookie);
325 void log_service(int fd, void *cookie);
326 void remount_service(int fd, void *cookie);
327 char * get_log_file_path(const char * log_name);
329 int rootshell_mode;// 0: developer, 1: root
331 // This is the users and groups config for the platform
333 #define SID_ROOT 0 /* traditional unix root user */
334 #define SID_TTY 5 /* group for /dev/ptmx */
335 #define SID_APP tzplatform_getuid(TZ_USER_NAME) /* application */
336 #define SID_DEVELOPER tzplatform_getuid(TZ_SDK_USER_NAME) /* developer with SDK */
337 #define SID_APP_LOGGING 6509
338 #define SID_SYS_LOGGING 6527
339 #define SID_INPUT 1004
343 int should_drop_privileges(void);
344 int set_developer_privileges();
345 void set_root_privileges();
347 int get_emulator_forward_port(void);
348 int get_emulator_name(char str[], int str_size);
349 int get_device_name(char str[], int str_size);
350 /* packet allocator */
351 apacket *get_apacket(void);
352 void put_apacket(apacket *p);
354 int check_header(apacket *p);
355 int check_data(apacket *p);
357 /* define SDB_TRACE to 1 to enable tracing support, or 0 to disable it */
361 /* IMPORTANT: if you change the following list, don't
362 * forget to update the corresponding 'tags' table in
363 * the sdb_trace_init() function implemented in sdb.c
384 * When running inside the emulator, guest's sdbd can connect to 'sdb-debug'
385 * qemud service that can display sdb trace messages (on condition that emulator
386 * has been started with '-debug sdb' option).
389 /* Delivers a trace message to the emulator via QEMU pipe. */
390 void sdb_qemu_trace(const char* fmt, ...);
391 /* Macro to use to send SDB trace messages to the emulator. */
392 #define DQ(...) sdb_qemu_trace(__VA_ARGS__)
394 #define DQ(...) ((void)0)
395 #endif /* !SDB_HOST */
397 extern int sdb_trace_mask;
398 extern unsigned char sdb_trace_output_count;
399 void sdb_trace_init(void);
401 # define SDB_TRACING ((sdb_trace_mask & (1 << TRACE_TAG)) != 0)
403 /* you must define TRACE_TAG before using this macro */
407 int save_errno = errno; \
408 sdb_mutex_lock(&D_lock); \
409 fprintf(stderr, "%s::%s():", \
410 __FILE__, __FUNCTION__); \
411 errno = save_errno; \
412 fprintf(stderr, __VA_ARGS__ ); \
414 sdb_mutex_unlock(&D_lock); \
415 errno = save_errno; \
421 int save_errno = errno; \
422 sdb_mutex_lock(&D_lock); \
423 errno = save_errno; \
424 fprintf(stderr, __VA_ARGS__ ); \
426 sdb_mutex_unlock(&D_lock); \
427 errno = save_errno; \
431 # define D(...) ((void)0)
432 # define DR(...) ((void)0)
433 # define SDB_TRACING 0
438 #define print_packet(tag,p) do {} while (0)
441 #if SDB_HOST_ON_TARGET
442 /* sdb and sdbd are coexisting on the target, so use 26099 for sdb
443 * to avoid conflicting with sdbd's usage of 26098
445 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
447 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
450 # define QEMU_FORWARD_IP "10.0.2.2"
452 #define DEFAULT_SDB_LOCAL_TRANSPORT_PORT 26101 /* tizen specific */
453 #define DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT 26103 /* tizen specific */
455 #define SDB_CLASS 0xff
456 #define SDB_SUBCLASS 0x20 //0x42 /* tizen specific */
457 #define SDB_PROTOCOL 0x02 //0x01 /* tizen specific */
460 void local_init(int port);
461 int local_connect(int port, const char *device_name);
462 int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
464 /* usb host/client interface */
468 int usb_write(usb_handle *h, const void *data, int len);
469 int usb_read(usb_handle *h, void *data, int len);
470 int usb_close(usb_handle *h);
471 void usb_kick(usb_handle *h);
474 extern void (*usb_init)();
475 extern void (*usb_cleanup)();
476 extern int (*usb_write)(usb_handle *h, const void *data, int len);
477 extern int (*usb_read)(usb_handle *h, void *data, int len);
478 extern int (*usb_close)(usb_handle *h);
479 extern void (*usb_kick)(usb_handle *h);
481 /* functionfs backend */
483 void ffs_usb_cleanup();
484 int ffs_usb_write(usb_handle *h, const void *data, int len);
485 int ffs_usb_read(usb_handle *h, void *data, int len);
486 int ffs_usb_close(usb_handle *h);
487 void ffs_usb_kick(usb_handle *h);
489 /* kernel sdb gadget backend */
490 void linux_usb_init();
491 void linux_usb_cleanup();
492 int linux_usb_write(usb_handle *h, const void *data, int len);
493 int linux_usb_read(usb_handle *h, void *data, int len);
494 int linux_usb_close(usb_handle *h);
495 void linux_usb_kick(usb_handle *h);
499 /* used for USB device detection */
501 int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
504 unsigned host_to_le32(unsigned n);
505 int sdb_commandline(int argc, char **argv);
507 int connection_state(atransport *t);
511 #define CS_BOOTLOADER 1
514 #define CS_RECOVERY 4
515 #define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
516 #define CS_SIDELOAD 6
519 extern int SHELL_EXIT_NOTIFY_FD;
521 extern SdbdCommandlineArgs sdbd_commandline_args;
524 #define CHUNK_SIZE (64*1024)
526 int sendfailmsg(int fd, const char *reason);
527 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
529 int is_emulator(void);
530 #define DEFAULT_DEVICENAME "unknown"
532 #if SDB_HOST /* tizen-specific */
533 #define DEVICEMAP_SEPARATOR ":"
534 #define DEVICENAME_MAX 256
535 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above
537 void register_device_name(const char *device_type, const char *device_name, int port);
538 int get_devicename_from_shdmem(int port, char *device_name);
539 int read_line(const int fd, char* ptr, const size_t maxlen);
543 #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb"
544 #define USB_NODE_FILE "/dev/samsung_sdb"