Add the can_launch member to capability structure.
[sdk/target/sdbd.git] / src / sdb.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 __SDB_H
18 #define __SDB_H
19
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23
24 #include "transport.h"  /* readx(), writex() */
25 #include "fdevent.h"
26 #include "sdbd_plugin.h"
27 #if !SDB_HOST
28 #include "commandline_sdbd.h"
29 #endif
30 #include <tzplatform_config.h>
31
32 #define MAX_PAYLOAD 4096
33
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
41
42 #define A_VERSION 0x02000000        // SDB protocol version
43
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
47
48 #define SDB_SERVER_VERSION 0        // Increment this when we want to force users to start a new sdb server
49
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;
58
59 struct amessage {
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             */
66 };
67
68 struct apacket
69 {
70     apacket *next;
71
72     unsigned len;
73     unsigned char *ptr;
74
75     amessage msg;
76     unsigned char data[MAX_PAYLOAD];
77 };
78
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.
82 */
83 struct asocket {
84         /* chain pointers for the local/remote list of
85         ** asockets that this asocket lives in
86         */
87     asocket *next;
88     asocket *prev;
89
90         /* the unique identifier for this asocket
91         */
92     unsigned id;
93
94         /* flag: set when the socket's peer has closed
95         ** but packets are still queued for delivery
96         */
97     int    closing;
98
99         /* the asocket we are connected to
100         */
101
102     asocket *peer;
103
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.
107         */
108     fdevent fde;
109     int fd;
110
111         /* queue of apackets waiting to be written
112         */
113     apacket *pkt_first;
114     apacket *pkt_last;
115
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
120         ** receive data.
121         */
122     int (*enqueue)(asocket *s, apacket *pkt);
123
124         /* ready is called by the peer when it is ready for
125         ** us to send data via enqueue again
126         */
127     void (*ready)(asocket *s);
128
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.
132         */
133     void (*close)(asocket *s);
134
135         /* socket-type-specific extradata */
136     void *extra;
137
138         /* A socket is bound to atransport */
139     atransport *transport;
140 };
141
142
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...)
147 */
148 struct  adisconnect
149 {
150     void        (*func)(void*  opaque, atransport*  t);
151     void*         opaque;
152     adisconnect*  next;
153     adisconnect*  prev;
154 };
155
156
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)
161 **
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.
165 */
166 typedef enum transport_type {
167         kTransportUsb,
168         kTransportLocal,
169         kTransportAny,
170         kTransportHost,
171 } transport_type;
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     transport_type type;
190
191         /* usb handle or socket fd as needed */
192     usb_handle *usb;
193     int sfd;
194
195         /* used to identify transports for clients */
196     char *serial;
197     char *product;
198     int sdb_port; // Use for emulators (local transport)
199     char *device_name; // for connection explorer
200
201         /* a list of adisconnect callbacks called when the transport is kicked */
202     int          kicked;
203     adisconnect  disconnects;
204 };
205
206
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.
211 **
212 ** TODO: some listeners read from the new connection to
213 ** determine what exact service to connect to on the far
214 ** side.
215 */
216 struct alistener
217 {
218     alistener *next;
219     alistener *prev;
220
221     fdevent fde;
222     int fd;
223
224     const char *local_name;
225     const char *connect_to;
226     atransport *transport;
227     adisconnect  disconnect;
228 };
229
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
239 } pinfo;
240
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 #define SDBD_CAP_VERSION_MAJOR 1
251 #define SDBD_CAP_VERSION_MINOR 0
252 typedef struct platform_capabilities
253 {
254     char secure_protocol[CAPBUF_ITEMSIZE];      // enabled or disabled
255     char intershell_support[CAPBUF_ITEMSIZE];   // enabled or disabled
256     char filesync_support[CAPBUF_ITEMSIZE];     // push or pull or pushpull or disabled
257     char rootonoff_support[CAPBUF_ITEMSIZE];    // enabled or disabled
258     char zone_support[CAPBUF_ITEMSIZE];         // enabled or disabled
259     char multiuser_support[CAPBUF_ITEMSIZE];    // enabled or disabled
260     char syncwinsz_support[CAPBUF_ITEMSIZE];    // enabled or disabled
261     char usbproto_support[CAPBUF_ITEMSIZE];     // enabled or disabled
262     char sockproto_support[CAPBUF_ITEMSIZE];    // enabled or disabled
263
264     char log_enable[CAPBUF_ITEMSIZE];    // enabled or disabled
265     char log_path[CAPBUF_LL_ITEMSIZE];    // path of sdbd log
266
267     char cpu_arch[CAPBUF_ITEMSIZE];             // cpu architecture (ex. x86)
268     char profile_name[CAPBUF_ITEMSIZE];         // profile name (ex. mobile)
269     char vendor_name[CAPBUF_ITEMSIZE];          // vendor name (ex. Tizen)
270     char sdk_toolpath[CAPBUF_L_ITEMSIZE];       // sdk tool path
271     char can_launch[CAPBUF_L_ITEMSIZE];         // target name
272
273     char platform_version[CAPBUF_ITEMSIZE];     // platform version (ex. 2.3.0)
274     char product_version[CAPBUF_ITEMSIZE];      // product version (ex. 1.0)
275     char sdbd_version[CAPBUF_ITEMSIZE];         // sdbd version
276     char sdbd_plugin_version[CAPBUF_ITEMSIZE];  // sdbd plugin version
277     char sdbd_cap_version[CAPBUF_ITEMSIZE];     // capability version
278 } pcap;
279 pcap g_capabilities;
280
281 #define SDBD_PLUGIN_PATH    "/usr/lib/libsdbd_plugin.so"
282 #define SDBD_PLUGIN_INTF    "sdbd_plugin_cmd_proc"
283 typedef int (*SDBD_PLUGIN_CMD_PROC_PTR)(const char*, const char*, sdbd_plugin_param);
284 extern SDBD_PLUGIN_CMD_PROC_PTR sdbd_plugin_cmd_proc;
285 int request_plugin_cmd(const char* cmd, const char* in_buf, char *out_buf, unsigned int out_len);
286 int request_plugin_verification(const char* cmd, const char* in_buf);
287
288 void print_packet(const char *label, apacket *p);
289
290 asocket *find_local_socket(unsigned id);
291 void install_local_socket(asocket *s);
292 void remove_socket(asocket *s);
293 void close_all_sockets(atransport *t);
294
295 #define  LOCAL_CLIENT_PREFIX  "emulator-"
296
297 asocket *create_local_socket(int fd);
298 asocket *create_local_service_socket(const char *destination);
299
300 asocket *create_remote_socket(unsigned id, atransport *t);
301 void connect_to_remote(asocket *s, const char *destination);
302 void connect_to_smartsocket(asocket *s);
303
304 void fatal(const char *fmt, ...);
305 void fatal_errno(const char *fmt, ...);
306
307 void handle_packet(apacket *p, atransport *t);
308 void send_packet(apacket *p, atransport *t);
309
310 void get_my_path(char *s, size_t maxLen);
311 int launch_server(int server_port);
312 int sdb_main(int is_daemon, int server_port);
313
314
315 /* transports are ref-counted
316 ** get_device_transport does an acquire on your behalf before returning
317 */
318 void init_transport_registration(void);
319 int  list_transports(char *buf, size_t  bufsize);
320 void update_transports(void);
321 void broadcast_transport(apacket *p);
322 int get_connected_count(transport_type type);
323
324 asocket*  create_device_tracker(void);
325
326 /* Obtain a transport from the available transports.
327 ** If state is != CS_ANY, only transports in that state are considered.
328 ** If serial is non-NULL then only the device with that serial will be chosen.
329 ** If no suitable transport is found, error is set.
330 */
331 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
332 void   add_transport_disconnect( atransport*  t, adisconnect*  dis );
333 void   remove_transport_disconnect( atransport*  t, adisconnect*  dis );
334 void   run_transport_disconnects( atransport*  t );
335 void   kick_transport( atransport*  t );
336
337 /* initialize a transport object's func pointers and state */
338 #if SDB_HOST
339 int get_available_local_transport_index();
340 #endif
341 int  init_socket_transport(atransport *t, int s, int port, int local);
342 void init_usb_transport(atransport *t, usb_handle *usb, int state);
343
344 /* for MacOS X cleanup */
345 void close_usb_devices();
346
347 /* cause new transports to be init'd and added to the list */
348 void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name);
349
350 /* these should only be used for the "sdb disconnect" command */
351 void unregister_transport(atransport *t);
352 void unregister_all_tcp_transports();
353
354 void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
355
356 /* this should only be used for transports with connection_state == CS_NOPERM */
357 void unregister_usb_transport(usb_handle *usb);
358
359 atransport *find_transport(const char *serial);
360 #if SDB_HOST
361 atransport* find_emulator_transport_by_sdb_port(int sdb_port);
362 #endif
363
364 int service_to_fd(const char *name);
365 #if SDB_HOST
366 asocket *host_service_to_socket(const char*  name, const char *serial);
367 #endif
368
369 #if !SDB_HOST
370 int       init_jdwp(void);
371 asocket*  create_jdwp_service_socket();
372 asocket*  create_jdwp_tracker_service_socket();
373 int       create_jdwp_connection_fd(int  jdwp_pid);
374 #endif
375
376 #if !SDB_HOST
377 typedef enum {
378     BACKUP,
379     RESTORE
380 } BackupOperation;
381 int backup_service(BackupOperation operation, char* args);
382 void framebuffer_service(int fd, void *cookie);
383 void log_service(int fd, void *cookie);
384 void remount_service(int fd, void *cookie);
385 char * get_log_file_path(const char * log_name);
386
387 int rootshell_mode; // 0: sdk user, 1: root
388 int booting_done; // 0: platform booting is in progess 1: platform booting is done
389
390 // This is the users and groups config for the platform
391
392 #define SID_ROOT        0    /* traditional unix root user */
393
394 #define SDK_USER_NAME   tzplatform_getenv(TZ_SDK_USER_NAME)
395 #define SDK_TOOL_PATH   tzplatform_getenv(TZ_SDK_TOOLS)
396 #define STATIC_SDK_USER_ID      5001
397 #define STATIC_SDK_GROUP_ID     100
398 #define STATIC_SDK_HOME_DIR     "/home/owner"
399 extern uid_t g_sdk_user_id;
400 extern gid_t g_sdk_group_id;
401 extern char* g_sdk_home_dir;
402 extern char* g_sdk_home_dir_env;
403 #endif
404
405 int is_pwlocked(void);
406 int should_drop_privileges(void);
407 int set_sdk_user_privileges();
408 void set_root_privileges();
409
410 int get_emulator_forward_port(void);
411 int get_emulator_name(char str[], int str_size);
412 int get_device_name(char str[], int str_size);
413 int get_emulator_hostip(char str[], int str_size);
414 int get_emulator_guestip(char str[], int str_size);
415
416 /* packet allocator */
417 apacket *get_apacket(void);
418 void put_apacket(apacket *p);
419
420 int check_header(apacket *p);
421 int check_data(apacket *p);
422
423 /* define SDB_TRACE to 1 to enable tracing support, or 0 to disable it */
424
425 #define  SDB_TRACE    1
426
427 /* IMPORTANT: if you change the following list, don't
428  * forget to update the corresponding 'tags' table in
429  * the sdb_trace_init() function implemented in sdb.c
430  */
431 typedef enum {
432     TRACE_SDB = 0,
433     TRACE_SOCKETS,
434     TRACE_PACKETS,
435     TRACE_TRANSPORT,
436     TRACE_RWX,
437     TRACE_USB,
438     TRACE_SYNC,
439     TRACE_SYSDEPS,
440     TRACE_JDWP,
441     TRACE_SERVICES,
442     TRACE_PROPERTIES,
443     TRACE_SDKTOOLS
444 } SdbTrace;
445
446 #if SDB_TRACE
447
448 #if !SDB_HOST
449 /*
450  * When running inside the emulator, guest's sdbd can connect to 'sdb-debug'
451  * qemud service that can display sdb trace messages (on condition that emulator
452  * has been started with '-debug sdb' option).
453  */
454
455 /* Delivers a trace message to the emulator via QEMU pipe. */
456 void sdb_qemu_trace(const char* fmt, ...);
457 /* Macro to use to send SDB trace messages to the emulator. */
458 #define DQ(...)    sdb_qemu_trace(__VA_ARGS__)
459 #else
460 #define DQ(...) ((void)0)
461 #endif  /* !SDB_HOST */
462
463   extern int     sdb_trace_mask;
464   extern unsigned char    sdb_trace_output_count;
465   void    sdb_trace_init(void);
466
467 #  define SDB_TRACING  ((sdb_trace_mask & (1 << TRACE_TAG)) != 0)
468
469   /* you must define TRACE_TAG before using this macro */
470 #  define  D(...)                                      \
471         do {                                           \
472             if (SDB_TRACING) {                         \
473                 int save_errno = errno;                \
474                 sdb_mutex_lock(&D_lock);               \
475                 fprintf(stderr, "%s::%s():",           \
476                         __FILE__, __FUNCTION__);       \
477                 errno = save_errno;                    \
478                 fprintf(stderr, __VA_ARGS__ );         \
479                 fflush(stderr);                        \
480                 sdb_mutex_unlock(&D_lock);             \
481                 errno = save_errno;                    \
482            }                                           \
483         } while (0)
484 #  define  DR(...)                                     \
485         do {                                           \
486             if (SDB_TRACING) {                         \
487                 int save_errno = errno;                \
488                 sdb_mutex_lock(&D_lock);               \
489                 errno = save_errno;                    \
490                 fprintf(stderr, __VA_ARGS__ );         \
491                 fflush(stderr);                        \
492                 sdb_mutex_unlock(&D_lock);             \
493                 errno = save_errno;                    \
494            }                                           \
495         } while (0)
496 #else
497 #  define  D(...)          ((void)0)
498 #  define  DR(...)         ((void)0)
499 #  define  SDB_TRACING     0
500 #endif
501
502
503 #if !TRACE_PACKETS
504 #define print_packet(tag,p) do {} while (0)
505 #endif
506
507 #if SDB_HOST_ON_TARGET
508 /* sdb and sdbd are coexisting on the target, so use 26099 for sdb
509  * to avoid conflicting with sdbd's usage of 26098
510  */
511 #  define DEFAULT_SDB_PORT 26099 /* tizen specific */
512 #else
513 #  define DEFAULT_SDB_PORT 26099 /* tizen specific */
514 #endif
515
516 #  define QEMU_FORWARD_IP "10.0.2.2"
517
518 #define DEFAULT_SDB_LOCAL_TRANSPORT_PORT 26101 /* tizen specific */
519 #define DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT 26103 /* tizen specific */
520
521 #define SDB_CLASS              0xff
522 #define SDB_SUBCLASS           0x20 //0x42 /* tizen specific */
523 #define SDB_PROTOCOL           0x02 //0x01 /* tizen specific */
524
525
526 void local_init(int port);
527 int  local_connect(int  port, const char *device_name);
528 int  local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
529
530 /* usb host/client interface */
531 #if SDB_HOST
532 void usb_init();
533 void usb_cleanup();
534 int usb_write(usb_handle *h, const void *data, int len);
535 int usb_read(usb_handle *h, void *data, size_t len);
536 int usb_close(usb_handle *h);
537 void usb_kick(usb_handle *h);
538 #else
539
540 extern void (*usb_init)();
541 extern void (*usb_cleanup)();
542 extern int (*usb_write)(usb_handle *h, const void *data, int len);
543 extern int (*usb_read)(usb_handle *h, void *data, size_t len);
544 extern int (*usb_close)(usb_handle *h);
545 extern void (*usb_kick)(usb_handle *h);
546
547 /* functionfs backend */
548 void ffs_usb_init();
549 void ffs_usb_cleanup();
550 int ffs_usb_write(usb_handle *h, const void *data, int len);
551 int ffs_usb_read(usb_handle *h, void *data, size_t len);
552 int ffs_usb_close(usb_handle *h);
553 void ffs_usb_kick(usb_handle *h);
554
555 /* kernel sdb gadget backend */
556 void linux_usb_init();
557 void linux_usb_cleanup();
558 int linux_usb_write(usb_handle *h, const void *data, int len);
559 int linux_usb_read(usb_handle *h, void *data, size_t len);
560 int linux_usb_close(usb_handle *h);
561 void linux_usb_kick(usb_handle *h);
562
563 #endif
564
565 /* used for USB device detection */
566 #if SDB_HOST
567 int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
568 #endif
569
570 unsigned host_to_le32(unsigned n);
571 int sdb_commandline(int argc, char **argv);
572
573 int connection_state(atransport *t);
574
575 #define CS_ANY       -1
576 #define CS_OFFLINE    0
577 #define CS_BOOTLOADER 1
578 #define CS_DEVICE     2
579 #define CS_HOST       3
580 #define CS_RECOVERY   4
581 #define CS_NOPERM     5 /* Insufficient permissions to communicate with the device */
582 #define CS_SIDELOAD   6
583 #define CS_PWLOCK     10
584
585 extern int HOST;
586 extern int SHELL_EXIT_NOTIFY_FD;
587 #if !SDB_HOST
588 extern SdbdCommandlineArgs sdbd_commandline_args;
589 #endif
590
591 #define CHUNK_SIZE (64*1024)
592
593 int sendfailmsg(int fd, const char *reason);
594 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
595 int copy_packet(apacket* dest, apacket* src);
596
597 int is_emulator(void);
598 #define DEFAULT_DEVICENAME "unknown"
599
600 #if SDB_HOST /* tizen-specific */
601 #define DEVICEMAP_SEPARATOR ":"
602 #define DEVICENAME_MAX 256
603 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above
604
605 void register_device_name(const char *device_type, const char *device_name, int port);
606 int get_devicename_from_shdmem(int port, char *device_name);
607 int read_line(const int fd, char* ptr, const size_t maxlen);
608 #endif
609 #endif
610
611 #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb"
612 #define USB_NODE_FILE "/dev/samsung_sdb"