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.
24 #include "transport.h" /* readx(), writex() */
26 #include "sdbd_plugin.h"
28 #include "commandline_sdbd.h"
30 #include <tzplatform_config.h>
32 #define MAX_PAYLOAD 4096
34 #define A_SYNC 0x434e5953
35 #define A_CNXN 0x4e584e43
36 #define A_OPEN 0x4e45504f
37 #define A_OKAY 0x59414b4f
38 #define A_CLSE 0x45534c43
39 #define A_WRTE 0x45545257
40 #define A_STAT 0x54415453
42 #define A_VERSION 0x02000000 // SDB protocol version
44 #define SDB_VERSION_MAJOR 2 // Used for help/version information
45 #define SDB_VERSION_MINOR 2 // Used for help/version information
46 #define SDB_VERSION_PATCH 31 // Used for help/version information
48 #define SDB_SERVER_VERSION 0 // Increment this when we want to force users to start a new sdb server
50 #define SDBD_UMASK 0022 // default file creation mask of sdbd
52 typedef struct amessage amessage;
53 typedef struct apacket apacket;
54 typedef struct asocket asocket;
55 typedef struct alistener alistener;
56 typedef struct aservice aservice;
57 typedef struct atransport atransport;
58 typedef struct adisconnect adisconnect;
59 typedef struct usb_handle usb_handle;
62 unsigned command; /* command identifier constant */
63 unsigned arg0; /* first argument */
64 unsigned arg1; /* second argument */
65 unsigned data_length; /* length of payload (0 is allowed) */
66 unsigned data_check; /* checksum of data payload */
67 unsigned magic; /* command ^ 0xffffffff */
78 unsigned char data[MAX_PAYLOAD];
81 /* An asocket represents one half of a connection between a local and
82 ** remote entity. A local asocket is bound to a file descriptor. A
83 ** remote asocket is bound to the protocol engine.
86 /* chain pointers for the local/remote list of
87 ** asockets that this asocket lives in
92 /* the unique identifier for this asocket
96 /* flag: set when the socket's peer has closed
97 ** but packets are still queued for delivery
101 /* the asocket we are connected to
106 /* For local asockets, the fde is used to bind
107 ** us to our fd event system. For remote asockets
108 ** these fields are not used.
113 /* queue of apackets waiting to be written
118 /* enqueue is called by our peer when it has data
119 ** for us. It should return 0 if we can accept more
120 ** data or 1 if not. If we return 1, we must call
121 ** peer->ready() when we once again are ready to
124 int (*enqueue)(asocket *s, apacket *pkt);
126 /* ready is called by the peer when it is ready for
127 ** us to send data via enqueue again
129 void (*ready)(asocket *s);
131 /* close is called by the peer when it has gone away.
132 ** we are not allowed to make any further calls on the
133 ** peer once our close method is called.
135 void (*close)(asocket *s);
137 /* socket-type-specific extradata */
140 /* A socket is bound to atransport */
141 atransport *transport;
145 /* the adisconnect structure is used to record a callback that
146 ** will be called whenever a transport is disconnected (e.g. by the user)
147 ** this should be used to cleanup objects that depend on the
148 ** transport (e.g. remote sockets, listeners, etc...)
152 void (*func)(void* opaque, atransport* t);
159 /* a transport object models the connection to a remote device or emulator
160 ** there is one transport per connected device/emulator. a "local transport"
161 ** connects through TCP (for the emulator), while a "usb transport" through
162 ** USB (for real devices)
164 ** note that kTransportHost doesn't really correspond to a real transport
165 ** object, it's a special value used to indicate that a client wants to
166 ** connect to a service implemented within the SDB server itself.
168 typedef enum transport_type {
180 int (*read_from_remote)(apacket *p, atransport *t);
181 int (*write_to_remote)(apacket *p, atransport *t);
182 void (*close)(atransport *t);
183 void (*kick)(atransport *t);
186 int transport_socket;
187 fdevent transport_fde;
190 int connection_state;
193 /* usb handle or socket fd as needed */
197 /* used to identify transports for clients */
200 int sdb_port; // Use for emulators (local transport)
201 char *device_name; // for connection explorer
203 /* a list of adisconnect callbacks called when the transport is kicked */
205 adisconnect disconnects;
209 /* A listener is an entity which binds to a local port
210 ** and, upon receiving a connection on that port, creates
211 ** an asocket to connect the new local connection to a
212 ** specific remote service.
214 ** TODO: some listeners read from the new connection to
215 ** determine what exact service to connect to on the far
226 const char *local_name;
227 const char *connect_to;
228 atransport *transport;
229 adisconnect disconnect;
232 #define UNKNOWN "unknown"
233 #define INFOBUF_MAXLEN 64
234 #define INFO_VERSION "2.2.0"
235 typedef struct platform_info {
236 char platform_info_version[INFOBUF_MAXLEN];
237 char model_name[INFOBUF_MAXLEN]; // Emulator
238 char platform_name[INFOBUF_MAXLEN]; // Tizen
239 char platform_version[INFOBUF_MAXLEN]; // 2.2.1
240 char profile_name[INFOBUF_MAXLEN]; // 2.2.1
243 #define ENABLED "enabled"
244 #define DISABLED "disabled"
245 #define CPUARCH_ARMV6 "armv6"
246 #define CPUARCH_ARMV7 "armv7"
247 #define CPUARCH_X86 "x86"
248 #define CAPBUF_SIZE 4096
249 #define CAPBUF_ITEMSIZE 32
250 #define CAPBUF_L_ITEMSIZE 256
251 #define CAPBUF_LL_ITEMSIZE PATH_MAX
252 #define SDBD_CAP_VERSION_MAJOR 1
253 #define SDBD_CAP_VERSION_MINOR 0
254 typedef struct platform_capabilities
256 char secure_protocol[CAPBUF_ITEMSIZE]; // enabled or disabled
257 char intershell_support[CAPBUF_ITEMSIZE]; // enabled or disabled
258 char filesync_support[CAPBUF_ITEMSIZE]; // push or pull or pushpull or disabled
259 char rootonoff_support[CAPBUF_ITEMSIZE]; // enabled or disabled
260 char zone_support[CAPBUF_ITEMSIZE]; // enabled or disabled
261 char multiuser_support[CAPBUF_ITEMSIZE]; // enabled or disabled
262 char syncwinsz_support[CAPBUF_ITEMSIZE]; // enabled or disabled
263 char usbproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled
264 char sockproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled
266 char log_enable[CAPBUF_ITEMSIZE]; // enabled or disabled
267 char log_path[CAPBUF_LL_ITEMSIZE]; // path of sdbd log
269 char cpu_arch[CAPBUF_ITEMSIZE]; // cpu architecture (ex. x86)
270 char profile_name[CAPBUF_ITEMSIZE]; // profile name (ex. mobile)
271 char vendor_name[CAPBUF_ITEMSIZE]; // vendor name (ex. Tizen)
272 char sdk_toolpath[CAPBUF_L_ITEMSIZE]; // sdk tool path
273 char can_launch[CAPBUF_L_ITEMSIZE]; // target name
275 char platform_version[CAPBUF_ITEMSIZE]; // platform version (ex. 2.3.0)
276 char product_version[CAPBUF_ITEMSIZE]; // product version (ex. 1.0)
277 char sdbd_version[CAPBUF_ITEMSIZE]; // sdbd version
278 char sdbd_plugin_version[CAPBUF_ITEMSIZE]; // sdbd plugin version
279 char sdbd_cap_version[CAPBUF_ITEMSIZE]; // capability version
281 extern pcap g_capabilities;
283 #define SDBD_PLUGIN_PATH "/usr/lib/libsdbd_plugin.so"
284 #define SDBD_PLUGIN_INTF "sdbd_plugin_cmd_proc"
285 typedef int (*SDBD_PLUGIN_CMD_PROC_PTR)(const char*, const char*, sdbd_plugin_param);
286 extern SDBD_PLUGIN_CMD_PROC_PTR sdbd_plugin_cmd_proc;
287 int request_plugin_cmd(const char* cmd, const char* in_buf, char *out_buf, unsigned int out_len);
288 int request_plugin_verification(const char* cmd, const char* in_buf);
290 void print_packet(const char *label, apacket *p);
292 asocket *find_local_socket(unsigned id);
293 void install_local_socket(asocket *s);
294 void remove_socket(asocket *s);
295 void close_all_sockets(atransport *t);
297 #define LOCAL_CLIENT_PREFIX "emulator-"
299 asocket *create_local_socket(int fd);
300 asocket *create_local_service_socket(const char *destination);
302 asocket *create_remote_socket(unsigned id, atransport *t);
303 void connect_to_remote(asocket *s, const char *destination);
304 void connect_to_smartsocket(asocket *s);
306 void fatal(const char *fmt, ...);
307 void fatal_errno(const char *fmt, ...);
309 void handle_packet(apacket *p, atransport *t);
310 void send_packet(apacket *p, atransport *t);
312 void get_my_path(char *s, size_t maxLen);
313 int launch_server(int server_port);
314 int sdb_main(int is_daemon, int server_port);
317 /* transports are ref-counted
318 ** get_device_transport does an acquire on your behalf before returning
320 void init_transport_registration(void);
321 int list_transports(char *buf, size_t bufsize);
322 void update_transports(void);
323 void broadcast_transport(apacket *p);
324 int get_connected_count(transport_type type);
326 asocket* create_device_tracker(void);
328 /* Obtain a transport from the available transports.
329 ** If state is != CS_ANY, only transports in that state are considered.
330 ** If serial is non-NULL then only the device with that serial will be chosen.
331 ** If no suitable transport is found, error is set.
333 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
334 void add_transport_disconnect( atransport* t, adisconnect* dis );
335 void remove_transport_disconnect( atransport* t, adisconnect* dis );
336 void run_transport_disconnects( atransport* t );
337 void kick_transport( atransport* t );
339 /* initialize a transport object's func pointers and state */
341 int get_available_local_transport_index();
343 int init_socket_transport(atransport *t, int s, int port, int local);
344 void init_usb_transport(atransport *t, usb_handle *usb, int state);
346 /* for MacOS X cleanup */
347 void close_usb_devices();
349 /* cause new transports to be init'd and added to the list */
350 void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name);
352 /* these should only be used for the "sdb disconnect" command */
353 void unregister_transport(atransport *t);
354 void unregister_all_tcp_transports();
356 void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
358 /* this should only be used for transports with connection_state == CS_NOPERM */
359 void unregister_usb_transport(usb_handle *usb);
361 atransport *find_transport(const char *serial);
363 atransport* find_emulator_transport_by_sdb_port(int sdb_port);
366 int service_to_fd(const char *name);
368 asocket *host_service_to_socket(const char* name, const char *serial);
373 asocket* create_jdwp_service_socket();
374 asocket* create_jdwp_tracker_service_socket();
375 int create_jdwp_connection_fd(int jdwp_pid);
383 int backup_service(BackupOperation operation, char* args);
384 void framebuffer_service(int fd, void *cookie);
385 void log_service(int fd, void *cookie);
386 void remount_service(int fd, void *cookie);
387 char * get_log_file_path(const char * log_name);
389 extern int rootshell_mode; // 0: sdk user, 1: root
390 extern int booting_done; // 0: platform booting is in progess 1: platform booting is done
392 // This is the users and groups config for the platform
394 #define SID_ROOT 0 /* traditional unix root user */
396 #define SDK_USER_NAME tzplatform_getenv(TZ_SDK_USER_NAME)
397 #define SDK_TOOL_PATH tzplatform_getenv(TZ_SDK_TOOLS)
398 #define STATIC_SDK_USER_ID 5001
399 #define STATIC_SDK_GROUP_ID 100
400 #define STATIC_SDK_HOME_DIR "/home/owner"
401 extern uid_t g_sdk_user_id;
402 extern gid_t g_sdk_group_id;
403 extern char* g_sdk_home_dir;
404 extern char* g_sdk_home_dir_env;
407 int is_pwlocked(void);
408 int should_drop_privileges(void);
409 int set_sdk_user_privileges();
410 void set_root_privileges();
412 int get_emulator_forward_port(void);
413 int get_emulator_name(char str[], int str_size);
414 int get_device_name(char str[], int str_size);
415 int get_emulator_hostip(char str[], int str_size);
416 int get_emulator_guestip(char str[], int str_size);
418 /* packet allocator */
419 apacket *get_apacket(void);
420 void put_apacket(apacket *p);
422 int check_header(apacket *p);
423 int check_data(apacket *p);
425 /* define SDB_TRACE to 1 to enable tracing support, or 0 to disable it */
429 /* IMPORTANT: if you change the following list, don't
430 * forget to update the corresponding 'tags' table in
431 * the sdb_trace_init() function implemented in sdb.c
452 * When running inside the emulator, guest's sdbd can connect to 'sdb-debug'
453 * qemud service that can display sdb trace messages (on condition that emulator
454 * has been started with '-debug sdb' option).
457 /* Delivers a trace message to the emulator via QEMU pipe. */
458 void sdb_qemu_trace(const char* fmt, ...);
459 /* Macro to use to send SDB trace messages to the emulator. */
460 #define DQ(...) sdb_qemu_trace(__VA_ARGS__)
462 #define DQ(...) ((void)0)
463 #endif /* !SDB_HOST */
465 extern int sdb_trace_mask;
466 extern unsigned char sdb_trace_output_count;
467 void sdb_trace_init(void);
469 # define SDB_TRACING ((sdb_trace_mask & (1 << TRACE_TAG)) != 0)
471 /* you must define TRACE_TAG before using this macro */
475 int save_errno = errno; \
476 sdb_mutex_lock(&D_lock); \
477 fprintf(stderr, "%s::%s():", \
478 __FILE__, __FUNCTION__); \
479 errno = save_errno; \
480 fprintf(stderr, __VA_ARGS__ ); \
482 sdb_mutex_unlock(&D_lock); \
483 errno = save_errno; \
489 int save_errno = errno; \
490 sdb_mutex_lock(&D_lock); \
491 errno = save_errno; \
492 fprintf(stderr, __VA_ARGS__ ); \
494 sdb_mutex_unlock(&D_lock); \
495 errno = save_errno; \
499 # define D(...) ((void)0)
500 # define DR(...) ((void)0)
501 # define SDB_TRACING 0
506 #define print_packet(tag,p) do {} while (0)
509 #if SDB_HOST_ON_TARGET
510 /* sdb and sdbd are coexisting on the target, so use 26099 for sdb
511 * to avoid conflicting with sdbd's usage of 26098
513 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
515 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
518 # define QEMU_FORWARD_IP "10.0.2.2"
520 #define DEFAULT_SDB_LOCAL_TRANSPORT_PORT 26101 /* tizen specific */
521 #define DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT 26103 /* tizen specific */
523 #define SDB_CLASS 0xff
524 #define SDB_SUBCLASS 0x20 //0x42 /* tizen specific */
525 #define SDB_PROTOCOL 0x02 //0x01 /* tizen specific */
528 void local_init(int port);
529 int local_connect(int port, const char *device_name);
530 int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
532 /* usb host/client interface */
536 int usb_write(usb_handle *h, const void *data, int len);
537 int usb_read(usb_handle *h, void *data, size_t len);
538 int usb_close(usb_handle *h);
539 void usb_kick(usb_handle *h);
542 extern void (*usb_init)();
543 extern void (*usb_cleanup)();
544 extern int (*usb_write)(usb_handle *h, const void *data, int len);
545 extern int (*usb_read)(usb_handle *h, void *data, size_t len);
546 extern int (*usb_close)(usb_handle *h);
547 extern void (*usb_kick)(usb_handle *h);
549 /* functionfs backend */
551 void ffs_usb_cleanup();
552 int ffs_usb_write(usb_handle *h, const void *data, int len);
553 int ffs_usb_read(usb_handle *h, void *data, size_t len);
554 int ffs_usb_close(usb_handle *h);
555 void ffs_usb_kick(usb_handle *h);
557 /* kernel sdb gadget backend */
558 void linux_usb_init();
559 void linux_usb_cleanup();
560 int linux_usb_write(usb_handle *h, const void *data, int len);
561 int linux_usb_read(usb_handle *h, void *data, size_t len);
562 int linux_usb_close(usb_handle *h);
563 void linux_usb_kick(usb_handle *h);
567 /* used for USB device detection */
569 int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
572 unsigned host_to_le32(unsigned n);
573 int sdb_commandline(int argc, char **argv);
575 int connection_state(atransport *t);
579 #define CS_BOOTLOADER 1
582 #define CS_RECOVERY 4
583 #define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
584 #define CS_SIDELOAD 6
588 extern int SHELL_EXIT_NOTIFY_FD;
590 extern SdbdCommandlineArgs sdbd_commandline_args;
593 #define CHUNK_SIZE (64*1024)
595 int sendfailmsg(int fd, const char *reason);
596 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
597 int copy_packet(apacket* dest, apacket* src);
599 int is_emulator(void);
600 #define DEFAULT_DEVICENAME "unknown"
602 #if SDB_HOST /* tizen-specific */
603 #define DEVICEMAP_SEPARATOR ":"
604 #define DEVICENAME_MAX 256
605 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above
607 void register_device_name(const char *device_type, const char *device_name, int port);
608 int get_devicename_from_shdmem(int port, char *device_name);
609 int read_line(const int fd, char* ptr, const size_t maxlen);
613 #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb"
614 #define USB_NODE_FILE "/dev/samsung_sdb"