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 typedef struct amessage amessage;
51 typedef struct apacket apacket;
52 typedef struct asocket asocket;
53 typedef struct alistener alistener;
54 typedef struct aservice aservice;
55 typedef struct atransport atransport;
56 typedef struct adisconnect adisconnect;
57 typedef struct usb_handle usb_handle;
60 unsigned command; /* command identifier constant */
61 unsigned arg0; /* first argument */
62 unsigned arg1; /* second argument */
63 unsigned data_length; /* length of payload (0 is allowed) */
64 unsigned data_check; /* checksum of data payload */
65 unsigned magic; /* command ^ 0xffffffff */
76 unsigned char data[MAX_PAYLOAD];
79 /* An asocket represents one half of a connection between a local and
80 ** remote entity. A local asocket is bound to a file descriptor. A
81 ** remote asocket is bound to the protocol engine.
84 /* chain pointers for the local/remote list of
85 ** asockets that this asocket lives in
90 /* the unique identifier for this asocket
94 /* flag: set when the socket's peer has closed
95 ** but packets are still queued for delivery
99 /* the asocket we are connected to
104 /* For local asockets, the fde is used to bind
105 ** us to our fd event system. For remote asockets
106 ** these fields are not used.
111 /* queue of apackets waiting to be written
116 /* enqueue is called by our peer when it has data
117 ** for us. It should return 0 if we can accept more
118 ** data or 1 if not. If we return 1, we must call
119 ** peer->ready() when we once again are ready to
122 int (*enqueue)(asocket *s, apacket *pkt);
124 /* ready is called by the peer when it is ready for
125 ** us to send data via enqueue again
127 void (*ready)(asocket *s);
129 /* close is called by the peer when it has gone away.
130 ** we are not allowed to make any further calls on the
131 ** peer once our close method is called.
133 void (*close)(asocket *s);
135 /* socket-type-specific extradata */
138 /* A socket is bound to atransport */
139 atransport *transport;
143 /* the adisconnect structure is used to record a callback that
144 ** will be called whenever a transport is disconnected (e.g. by the user)
145 ** this should be used to cleanup objects that depend on the
146 ** transport (e.g. remote sockets, listeners, etc...)
150 void (*func)(void* opaque, atransport* t);
157 /* a transport object models the connection to a remote device or emulator
158 ** there is one transport per connected device/emulator. a "local transport"
159 ** connects through TCP (for the emulator), while a "usb transport" through
160 ** USB (for real devices)
162 ** note that kTransportHost doesn't really correspond to a real transport
163 ** object, it's a special value used to indicate that a client wants to
164 ** connect to a service implemented within the SDB server itself.
166 typedef enum transport_type {
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);
184 int transport_socket;
185 fdevent transport_fde;
188 int connection_state;
191 /* usb handle or socket fd as needed */
195 /* used to identify transports for clients */
198 int sdb_port; // Use for emulators (local transport)
199 char *device_name; // for connection explorer
201 /* a list of adisconnect callbacks called when the transport is kicked */
203 adisconnect disconnects;
207 /* A listener is an entity which binds to a local port
208 ** and, upon receiving a connection on that port, creates
209 ** an asocket to connect the new local connection to a
210 ** specific remote service.
212 ** TODO: some listeners read from the new connection to
213 ** determine what exact service to connect to on the far
224 const char *local_name;
225 const char *connect_to;
226 atransport *transport;
227 adisconnect disconnect;
230 #define UNKNOWN "unknown"
231 #define INFOBUF_MAXLEN 64
232 #define INFO_VERSION "2.2.0"
233 typedef struct platform_info {
234 char platform_info_version[INFOBUF_MAXLEN];
235 char model_name[INFOBUF_MAXLEN]; // Emulator
236 char platform_name[INFOBUF_MAXLEN]; // Tizen
237 char platform_version[INFOBUF_MAXLEN]; // 2.2.1
238 char profile_name[INFOBUF_MAXLEN]; // 2.2.1
241 #define ENABLED "enabled"
242 #define DISABLED "disabled"
243 #define CPUARCH_ARMV6 "armv6"
244 #define CPUARCH_ARMV7 "armv7"
245 #define CPUARCH_X86 "x86"
246 #define CAPBUF_SIZE 4096
247 #define CAPBUF_ITEMSIZE 32
248 #define CAPBUF_L_ITEMSIZE 256
249 #define CAPBUF_LL_ITEMSIZE PATH_MAX
250 typedef struct platform_capabilities
252 char secure_protocol[CAPBUF_ITEMSIZE]; // enabled or disabled
253 char intershell_support[CAPBUF_ITEMSIZE]; // enabled or disabled
254 char filesync_support[CAPBUF_ITEMSIZE]; // push or pull or pushpull or disabled
255 char rootonoff_support[CAPBUF_ITEMSIZE]; // enabled or disabled
256 char zone_support[CAPBUF_ITEMSIZE]; // enabled or disabled
257 char multiuser_support[CAPBUF_ITEMSIZE]; // enabled or disabled
258 char syncwinsz_support[CAPBUF_ITEMSIZE]; // enabled or disabled
259 char usbproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled
260 char sockproto_support[CAPBUF_ITEMSIZE]; // enabled or disabled
262 char log_enable[CAPBUF_ITEMSIZE]; // enabled or disabled
263 char log_path[CAPBUF_LL_ITEMSIZE]; // path of sdbd log
265 char cpu_arch[CAPBUF_ITEMSIZE]; // cpu architecture (ex. x86)
266 char profile_name[CAPBUF_ITEMSIZE]; // profile name (ex. mobile)
267 char vendor_name[CAPBUF_ITEMSIZE]; // vendor name (ex. Tizen)
268 char sdk_toolpath[CAPBUF_L_ITEMSIZE]; // sdk tool path
270 char platform_version[CAPBUF_ITEMSIZE]; // platform version (ex. 2.3.0)
271 char product_version[CAPBUF_ITEMSIZE]; // product version (ex. 1.0)
272 char sdbd_version[CAPBUF_ITEMSIZE]; // sdbd version
273 char sdbd_plugin_version[CAPBUF_ITEMSIZE]; // sdbd plugin version
277 #define SDBD_PLUGIN_PATH "/usr/lib/libsdbd_plugin.so"
278 #define SDBD_PLUGIN_INTF "sdbd_plugin_cmd_proc"
279 typedef int (*SDBD_PLUGIN_CMD_PROC_PTR)(const char*, const char*, sdbd_plugin_param);
280 extern SDBD_PLUGIN_CMD_PROC_PTR sdbd_plugin_cmd_proc;
281 int request_plugin_cmd(const char* cmd, const char* in_buf, char *out_buf, unsigned int out_len);
282 int request_plugin_verification(const char* cmd, const char* in_buf);
284 void print_packet(const char *label, apacket *p);
286 asocket *find_local_socket(unsigned id);
287 void install_local_socket(asocket *s);
288 void remove_socket(asocket *s);
289 void close_all_sockets(atransport *t);
291 #define LOCAL_CLIENT_PREFIX "emulator-"
293 asocket *create_local_socket(int fd);
294 asocket *create_local_service_socket(const char *destination);
296 asocket *create_remote_socket(unsigned id, atransport *t);
297 void connect_to_remote(asocket *s, const char *destination);
298 void connect_to_smartsocket(asocket *s);
300 void fatal(const char *fmt, ...);
301 void fatal_errno(const char *fmt, ...);
303 void handle_packet(apacket *p, atransport *t);
304 void send_packet(apacket *p, atransport *t);
306 void get_my_path(char *s, size_t maxLen);
307 int launch_server(int server_port);
308 int sdb_main(int is_daemon, int server_port);
311 /* transports are ref-counted
312 ** get_device_transport does an acquire on your behalf before returning
314 void init_transport_registration(void);
315 int list_transports(char *buf, size_t bufsize);
316 void update_transports(void);
317 void broadcast_transport(apacket *p);
318 int get_connected_count(transport_type type);
320 asocket* create_device_tracker(void);
322 /* Obtain a transport from the available transports.
323 ** If state is != CS_ANY, only transports in that state are considered.
324 ** If serial is non-NULL then only the device with that serial will be chosen.
325 ** If no suitable transport is found, error is set.
327 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
328 void add_transport_disconnect( atransport* t, adisconnect* dis );
329 void remove_transport_disconnect( atransport* t, adisconnect* dis );
330 void run_transport_disconnects( atransport* t );
331 void kick_transport( atransport* t );
333 /* initialize a transport object's func pointers and state */
335 int get_available_local_transport_index();
337 int init_socket_transport(atransport *t, int s, int port, int local);
338 void init_usb_transport(atransport *t, usb_handle *usb, int state);
340 /* for MacOS X cleanup */
341 void close_usb_devices();
343 /* cause new transports to be init'd and added to the list */
344 void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name);
346 /* these should only be used for the "sdb disconnect" command */
347 void unregister_transport(atransport *t);
348 void unregister_all_tcp_transports();
350 void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
352 /* this should only be used for transports with connection_state == CS_NOPERM */
353 void unregister_usb_transport(usb_handle *usb);
355 atransport *find_transport(const char *serial);
357 atransport* find_emulator_transport_by_sdb_port(int sdb_port);
360 int service_to_fd(const char *name);
362 asocket *host_service_to_socket(const char* name, const char *serial);
367 asocket* create_jdwp_service_socket();
368 asocket* create_jdwp_tracker_service_socket();
369 int create_jdwp_connection_fd(int jdwp_pid);
377 int backup_service(BackupOperation operation, char* args);
378 void framebuffer_service(int fd, void *cookie);
379 void log_service(int fd, void *cookie);
380 void remount_service(int fd, void *cookie);
381 char * get_log_file_path(const char * log_name);
383 int rootshell_mode; // 0: sdk user, 1: root
384 int booting_done; // 0: platform booting is in progess 1: platform booting is done
386 // This is the users and groups config for the platform
388 #define SID_ROOT 0 /* traditional unix root user */
390 #define SDK_USER_NAME tzplatform_getenv(TZ_SDK_USER_NAME)
391 #define SDK_TOOL_PATH tzplatform_getenv(TZ_SDK_TOOLS)
392 #define STATIC_SDK_USER_ID 5001
393 #define STATIC_SDK_GROUP_ID 100
394 #define STATIC_SDK_HOME_DIR "/home/owner"
395 extern uid_t g_sdk_user_id;
396 extern gid_t g_sdk_group_id;
397 extern char* g_sdk_home_dir;
398 extern char* g_sdk_home_dir_env;
401 int is_pwlocked(void);
402 int should_drop_privileges(void);
403 int set_sdk_user_privileges();
404 void set_root_privileges();
406 int get_emulator_forward_port(void);
407 int get_emulator_name(char str[], int str_size);
408 int get_device_name(char str[], int str_size);
409 int get_emulator_hostip(char str[], int str_size);
410 int get_emulator_guestip(char str[], int str_size);
412 /* packet allocator */
413 apacket *get_apacket(void);
414 void put_apacket(apacket *p);
416 int check_header(apacket *p);
417 int check_data(apacket *p);
419 /* define SDB_TRACE to 1 to enable tracing support, or 0 to disable it */
423 /* IMPORTANT: if you change the following list, don't
424 * forget to update the corresponding 'tags' table in
425 * the sdb_trace_init() function implemented in sdb.c
446 * When running inside the emulator, guest's sdbd can connect to 'sdb-debug'
447 * qemud service that can display sdb trace messages (on condition that emulator
448 * has been started with '-debug sdb' option).
451 /* Delivers a trace message to the emulator via QEMU pipe. */
452 void sdb_qemu_trace(const char* fmt, ...);
453 /* Macro to use to send SDB trace messages to the emulator. */
454 #define DQ(...) sdb_qemu_trace(__VA_ARGS__)
456 #define DQ(...) ((void)0)
457 #endif /* !SDB_HOST */
459 extern int sdb_trace_mask;
460 extern unsigned char sdb_trace_output_count;
461 void sdb_trace_init(void);
463 # define SDB_TRACING ((sdb_trace_mask & (1 << TRACE_TAG)) != 0)
465 /* you must define TRACE_TAG before using this macro */
469 int save_errno = errno; \
470 sdb_mutex_lock(&D_lock); \
471 fprintf(stderr, "%s::%s():", \
472 __FILE__, __FUNCTION__); \
473 errno = save_errno; \
474 fprintf(stderr, __VA_ARGS__ ); \
476 sdb_mutex_unlock(&D_lock); \
477 errno = save_errno; \
483 int save_errno = errno; \
484 sdb_mutex_lock(&D_lock); \
485 errno = save_errno; \
486 fprintf(stderr, __VA_ARGS__ ); \
488 sdb_mutex_unlock(&D_lock); \
489 errno = save_errno; \
493 # define D(...) ((void)0)
494 # define DR(...) ((void)0)
495 # define SDB_TRACING 0
500 #define print_packet(tag,p) do {} while (0)
503 #if SDB_HOST_ON_TARGET
504 /* sdb and sdbd are coexisting on the target, so use 26099 for sdb
505 * to avoid conflicting with sdbd's usage of 26098
507 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
509 # define DEFAULT_SDB_PORT 26099 /* tizen specific */
512 # define QEMU_FORWARD_IP "10.0.2.2"
514 #define DEFAULT_SDB_LOCAL_TRANSPORT_PORT 26101 /* tizen specific */
515 #define DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT 26103 /* tizen specific */
517 #define SDB_CLASS 0xff
518 #define SDB_SUBCLASS 0x20 //0x42 /* tizen specific */
519 #define SDB_PROTOCOL 0x02 //0x01 /* tizen specific */
522 void local_init(int port);
523 int local_connect(int port, const char *device_name);
524 int local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
526 /* usb host/client interface */
530 int usb_write(usb_handle *h, const void *data, int len);
531 int usb_read(usb_handle *h, void *data, size_t len);
532 int usb_close(usb_handle *h);
533 void usb_kick(usb_handle *h);
536 extern void (*usb_init)();
537 extern void (*usb_cleanup)();
538 extern int (*usb_write)(usb_handle *h, const void *data, int len);
539 extern int (*usb_read)(usb_handle *h, void *data, size_t len);
540 extern int (*usb_close)(usb_handle *h);
541 extern void (*usb_kick)(usb_handle *h);
543 /* functionfs backend */
545 void ffs_usb_cleanup();
546 int ffs_usb_write(usb_handle *h, const void *data, int len);
547 int ffs_usb_read(usb_handle *h, void *data, size_t len);
548 int ffs_usb_close(usb_handle *h);
549 void ffs_usb_kick(usb_handle *h);
551 /* kernel sdb gadget backend */
552 void linux_usb_init();
553 void linux_usb_cleanup();
554 int linux_usb_write(usb_handle *h, const void *data, int len);
555 int linux_usb_read(usb_handle *h, void *data, size_t len);
556 int linux_usb_close(usb_handle *h);
557 void linux_usb_kick(usb_handle *h);
561 /* used for USB device detection */
563 int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
566 unsigned host_to_le32(unsigned n);
567 int sdb_commandline(int argc, char **argv);
569 int connection_state(atransport *t);
573 #define CS_BOOTLOADER 1
576 #define CS_RECOVERY 4
577 #define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
578 #define CS_SIDELOAD 6
582 extern int SHELL_EXIT_NOTIFY_FD;
584 extern SdbdCommandlineArgs sdbd_commandline_args;
587 #define CHUNK_SIZE (64*1024)
589 int sendfailmsg(int fd, const char *reason);
590 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
591 int copy_packet(apacket* dest, apacket* src);
593 int is_emulator(void);
594 #define DEFAULT_DEVICENAME "unknown"
596 #if SDB_HOST /* tizen-specific */
597 #define DEVICEMAP_SEPARATOR ":"
598 #define DEVICENAME_MAX 256
599 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above
601 void register_device_name(const char *device_type, const char *device_name, int port);
602 int get_devicename_from_shdmem(int port, char *device_name);
603 int read_line(const int fd, char* ptr, const size_t maxlen);
607 #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb"
608 #define USB_NODE_FILE "/dev/samsung_sdb"