Merge branch 'tizen_3.0' into tizen
[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 #if !SDB_HOST
27 #include "commandline_sdbd.h"
28 #endif
29 #include <tzplatform_config.h>
30
31 #define MAX_PAYLOAD_V1  (4*1024)
32 #define MAX_PAYLOAD_V2  (256*1024)
33 #define MAX_PAYLOAD     MAX_PAYLOAD_V2
34
35 #define A_SYNC 0x434e5953
36 #define A_CNXN 0x4e584e43
37 #define A_OPEN 0x4e45504f
38 #define A_OKAY 0x59414b4f
39 #define A_CLSE 0x45534c43
40 #define A_WRTE 0x45545257
41 #define A_STAT 0x54415453
42 #define A_ENCR 0x40682018 // encryption 메시지
43
44 #ifdef SUPPORT_ENCRYPT
45  #define ENCR_SET_ON_REQ 0 // encryption hello 메시지
46  #define ENCR_SET_ON_OK 1 // encryption ack 메시지
47  #define ENCR_SET_OFF 2 // encryption mode off 메시지
48  #define ENCR_GET 3 // encryption status get 메시지
49  #define ENCR_ON_FAIL 4 // encryption on 실패 메시지
50  #define ENCR_OFF_FAIL 5 // encryption off 실패 메시지
51  #define ENCR_ON 1 // encryption on 상태
52  #define ENCR_OFF 0 // encryption off 상태
53 #endif
54
55 #define A_VERSION 0x02000000        // SDB protocol version
56
57 #define SDB_VERSION_MAJOR 2         // Used for help/version information
58 #define SDB_VERSION_MINOR 2         // Used for help/version information
59 #define SDB_VERSION_PATCH 31        // Used for help/version information
60
61 #define SDB_SERVER_VERSION 0        // Increment this when we want to force users to start a new sdb server
62
63 typedef struct amessage amessage;
64 typedef struct apacket apacket;
65 typedef struct asocket asocket;
66 typedef struct alistener alistener;
67 typedef struct aservice aservice;
68 typedef struct atransport atransport;
69 typedef struct adisconnect  adisconnect;
70 typedef struct usb_handle usb_handle;
71
72 struct amessage {
73     unsigned command;       /* command identifier constant      */
74     unsigned arg0;          /* first argument                   */
75     unsigned arg1;          /* second argument                  */
76     unsigned data_length;   /* length of payload (0 is allowed) */
77     unsigned data_check;    /* checksum of data payload         */
78     unsigned magic;         /* command ^ 0xffffffff             */
79 };
80
81 struct apacket
82 {
83     apacket *next;
84
85     unsigned len;
86     unsigned char *ptr;
87
88     amessage msg;
89     unsigned char data[MAX_PAYLOAD];
90 };
91
92 /* An asocket represents one half of a connection between a local and
93 ** remote entity.  A local asocket is bound to a file descriptor.  A
94 ** remote asocket is bound to the protocol engine.
95 */
96 struct asocket {
97         /* chain pointers for the local/remote list of
98         ** asockets that this asocket lives in
99         */
100     asocket *next;
101     asocket *prev;
102
103         /* the unique identifier for this asocket
104         */
105     unsigned id;
106
107         /* flag: set when the socket's peer has closed
108         ** but packets are still queued for delivery
109         */
110     int    closing;
111
112         /* the asocket we are connected to
113         */
114
115     asocket *peer;
116
117         /* For local asockets, the fde is used to bind
118         ** us to our fd event system.  For remote asockets
119         ** these fields are not used.
120         */
121     fdevent fde;
122     int fd;
123
124         /* queue of apackets waiting to be written
125         */
126     apacket *pkt_first;
127     apacket *pkt_last;
128
129         /* enqueue is called by our peer when it has data
130         ** for us.  It should return 0 if we can accept more
131         ** data or 1 if not.  If we return 1, we must call
132         ** peer->ready() when we once again are ready to
133         ** receive data.
134         */
135     int (*enqueue)(asocket *s, apacket *pkt);
136
137         /* ready is called by the peer when it is ready for
138         ** us to send data via enqueue again
139         */
140     void (*ready)(asocket *s);
141
142         /* close is called by the peer when it has gone away.
143         ** we are not allowed to make any further calls on the
144         ** peer once our close method is called.
145         */
146     void (*close)(asocket *s);
147
148         /* socket-type-specific extradata */
149     void *extra;
150
151         /* A socket is bound to atransport */
152     atransport *transport;
153 };
154
155
156 /* the adisconnect structure is used to record a callback that
157 ** will be called whenever a transport is disconnected (e.g. by the user)
158 ** this should be used to cleanup objects that depend on the
159 ** transport (e.g. remote sockets, listeners, etc...)
160 */
161 struct  adisconnect
162 {
163     void        (*func)(void*  opaque, atransport*  t);
164     void*         opaque;
165     adisconnect*  next;
166     adisconnect*  prev;
167 };
168
169
170 /* a transport object models the connection to a remote device or emulator
171 ** there is one transport per connected device/emulator. a "local transport"
172 ** connects through TCP (for the emulator), while a "usb transport" through
173 ** USB (for real devices)
174 **
175 ** note that kTransportHost doesn't really correspond to a real transport
176 ** object, it's a special value used to indicate that a client wants to
177 ** connect to a service implemented within the SDB server itself.
178 */
179 typedef enum transport_type {
180         kTransportUsb,
181         kTransportLocal,
182         kTransportAny,
183         kTransportHost,
184 } transport_type;
185
186 struct atransport
187 {
188     atransport *next;
189     atransport *prev;
190
191     int (*read_from_remote)(apacket *p, atransport *t);
192     int (*write_to_remote)(apacket *p, atransport *t);
193     void (*close)(atransport *t);
194     void (*kick)(atransport *t);
195
196     int fd;
197     int transport_socket;
198     fdevent transport_fde;
199     int ref_count;
200     unsigned sync_token;
201     int connection_state;
202     transport_type type;
203
204         /* usb handle or socket fd as needed */
205     usb_handle *usb;
206     int sfd;
207
208         /* used to identify transports for clients */
209     char *serial;
210     char *product;
211     int sdb_port; // Use for emulators (local transport)
212     char *device_name; // for connection explorer
213
214         /* a list of adisconnect callbacks called when the transport is kicked */
215     int          kicked;
216     adisconnect  disconnects;
217     int protocol_version;
218     size_t max_payload;
219
220 #ifdef SUPPORT_ENCRYPT
221         unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption
222         int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key
223 #endif
224 };
225
226
227 /* A listener is an entity which binds to a local port
228 ** and, upon receiving a connection on that port, creates
229 ** an asocket to connect the new local connection to a
230 ** specific remote service.
231 **
232 ** TODO: some listeners read from the new connection to
233 ** determine what exact service to connect to on the far
234 ** side.
235 */
236 struct alistener
237 {
238     alistener *next;
239     alistener *prev;
240
241     fdevent fde;
242     int fd;
243
244     const char *local_name;
245     const char *connect_to;
246     atransport *transport;
247     adisconnect  disconnect;
248 };
249
250 #define UNKNOWN "unknown"
251 #define INFOBUF_MAXLEN 64
252 #define INFO_VERSION "2.2.0"
253 typedef struct platform_info {
254     char platform_info_version[INFOBUF_MAXLEN];
255     char model_name[INFOBUF_MAXLEN]; // Emulator
256     char platform_name[INFOBUF_MAXLEN]; // Tizen
257     char platform_version[INFOBUF_MAXLEN]; // 2.2.1
258     char profile_name[INFOBUF_MAXLEN]; // 2.2.1
259 } pinfo;
260
261 #define ENABLED "enabled"
262 #define DISABLED "disabled"
263 #define CAPBUF_SIZE 4096
264 #define CAPBUF_ITEMSIZE 32
265 #define CAPBUF_L_ITEMSIZE 256
266 #define CAPBUF_LL_ITEMSIZE PATH_MAX
267 #define SDBD_CAP_VERSION_MAJOR 1
268 #define SDBD_CAP_VERSION_MINOR 0
269 typedef struct platform_capabilities
270 {
271     char secure_protocol[CAPBUF_ITEMSIZE];      // enabled or disabled
272     char intershell_support[CAPBUF_ITEMSIZE];   // enabled or disabled
273     char filesync_support[CAPBUF_ITEMSIZE];     // push or pull or pushpull or disabled
274     char rootonoff_support[CAPBUF_ITEMSIZE];    // enabled or disabled
275     char zone_support[CAPBUF_ITEMSIZE];         // enabled or disabled
276     char multiuser_support[CAPBUF_ITEMSIZE];    // enabled or disabled
277     char syncwinsz_support[CAPBUF_ITEMSIZE];    // enabled or disabled
278     char usbproto_support[CAPBUF_ITEMSIZE];     // enabled or disabled
279     char sockproto_support[CAPBUF_ITEMSIZE];    // enabled or disabled
280     char appcmd_support[CAPBUF_ITEMSIZE];       // enabled or disabled
281     char encryption_support[CAPBUF_ITEMSIZE];   // enabled or disabled
282     char appid2pid_support[CAPBUF_ITEMSIZE];    // enabled or disabled
283     char pkgcmd_debugmode[CAPBUF_ITEMSIZE];     // enabled or disabled
284     char root_permission[CAPBUF_ITEMSIZE];      // enabled or disabled
285
286     char log_enable[CAPBUF_ITEMSIZE];           // enabled or disabled
287     char log_path[CAPBUF_LL_ITEMSIZE];          // path of sdbd log
288
289     char cpu_arch[CAPBUF_ITEMSIZE];             // cpu architecture (ex. x86)
290     char profile_name[CAPBUF_ITEMSIZE];         // profile name (ex. mobile)
291     char vendor_name[CAPBUF_ITEMSIZE];          // vendor name (ex. Tizen)
292     char sdk_toolpath[CAPBUF_L_ITEMSIZE];       // sdk tool path
293     char can_launch[CAPBUF_L_ITEMSIZE];         // target name
294
295     char platform_version[CAPBUF_ITEMSIZE];     // platform version (ex. 2.3.0)
296     char product_version[CAPBUF_ITEMSIZE];      // product version (ex. 1.0)
297     char sdbd_version[CAPBUF_ITEMSIZE];         // sdbd version
298     char sdbd_plugin_version[CAPBUF_ITEMSIZE];  // sdbd plugin version
299     char sdbd_cap_version[CAPBUF_ITEMSIZE];     // capability version
300 } pcap;
301 extern pcap g_capabilities;
302
303 void print_packet(const char *label, apacket *p);
304
305 asocket *find_local_socket(unsigned id);
306 void install_local_socket(asocket *s);
307 void remove_socket(asocket *s);
308 void close_all_sockets(atransport *t);
309
310 #define  LOCAL_CLIENT_PREFIX  "emulator-"
311
312 asocket *create_local_socket(int fd);
313 asocket *create_local_service_socket(const char *destination);
314
315 asocket *create_remote_socket(unsigned id, atransport *t);
316 void connect_to_remote(asocket *s, const char *destination);
317 void connect_to_smartsocket(asocket *s);
318 size_t asock_get_max_payload(asocket *s);
319
320 void fatal(const char *fmt, ...);
321 void fatal_errno(const char *fmt, ...);
322
323 void handle_packet(apacket *p, atransport *t);
324 void send_packet(apacket *p, atransport *t);
325
326 void get_my_path(char *s, size_t maxLen);
327 int launch_server(int server_port);
328 int sdb_main(int is_daemon, int server_port);
329
330
331 /* transports are ref-counted
332 ** get_device_transport does an acquire on your behalf before returning
333 */
334 void init_transport_registration(void);
335 int  list_transports(char *buf, size_t  bufsize);
336 void update_transports(void);
337 void broadcast_transport(apacket *p);
338 int get_connected_count(transport_type type);
339
340 asocket*  create_device_tracker(void);
341
342 /* Obtain a transport from the available transports.
343 ** If state is != CS_ANY, only transports in that state are considered.
344 ** If serial is non-NULL then only the device with that serial will be chosen.
345 ** If no suitable transport is found, error is set.
346 */
347 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
348 void   add_transport_disconnect( atransport*  t, adisconnect*  dis );
349 void   remove_transport_disconnect( atransport*  t, adisconnect*  dis );
350 void   run_transport_disconnects( atransport*  t );
351 void   kick_transport( atransport*  t );
352
353 /* initialize a transport object's func pointers and state */
354 #if SDB_HOST
355 int get_available_local_transport_index();
356 #endif
357 int  init_socket_transport(atransport *t, int s, int port, int local);
358 void init_usb_transport(atransport *t, usb_handle *usb, int state);
359
360 /* for MacOS X cleanup */
361 void close_usb_devices();
362
363 /* cause new transports to be init'd and added to the list */
364 void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name);
365
366 /* these should only be used for the "sdb disconnect" command */
367 void unregister_transport(atransport *t);
368 void unregister_all_tcp_transports();
369
370 void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
371
372 /* this should only be used for transports with connection_state == CS_NOPERM */
373 void unregister_usb_transport(usb_handle *usb);
374
375 atransport *find_transport(const char *serial);
376 #if SDB_HOST
377 atransport* find_emulator_transport_by_sdb_port(int sdb_port);
378 #endif
379
380 int service_to_fd(const char *name);
381 #if SDB_HOST
382 asocket *host_service_to_socket(const char*  name, const char *serial);
383 #endif
384
385 #if !SDB_HOST
386 int       init_jdwp(void);
387 asocket*  create_jdwp_service_socket();
388 asocket*  create_jdwp_tracker_service_socket();
389 int       create_jdwp_connection_fd(int  jdwp_pid);
390 #endif
391
392 #if !SDB_HOST
393 typedef enum {
394     BACKUP,
395     RESTORE
396 } BackupOperation;
397 int backup_service(BackupOperation operation, char* args);
398 void framebuffer_service(int fd, void *cookie);
399 void log_service(int fd, void *cookie);
400 void remount_service(int fd, void *cookie);
401 char * get_log_file_path(const char * log_name);
402
403 extern int rootshell_mode; // 0: sdk user, 1: root
404 extern int booting_done; // 0: platform booting is in progess 1: platform booting is done
405
406 // 1 if locked, 0 if unlocked
407 extern int is_pwlocked;
408
409 // This is the users and groups config for the platform
410
411 #define SID_ROOT        0    /* traditional unix root user */
412
413 #define SDK_USER_NAME   tzplatform_getenv(TZ_SDK_USER_NAME)
414 #define SDK_TOOL_PATH   tzplatform_getenv(TZ_SDK_TOOLS)
415 #define STATIC_SDK_USER_ID      5001
416 #define STATIC_SDK_GROUP_ID     100
417 #define STATIC_SDK_HOME_DIR     "/home/owner"
418 extern uid_t g_sdk_user_id;
419 extern gid_t g_sdk_group_id;
420 extern char* g_sdk_home_dir;
421 extern char* g_sdk_home_dir_env;
422
423 #define ROOT_USER_NAME          "root"
424 #define STATIC_ROOT_USER_ID       0
425 #define STATIC_ROOT_GROUP_ID     0
426 #define STATIC_ROOT_HOME_DIR     "/root"
427 extern uid_t g_root_user_id;
428 extern gid_t g_root_group_id;
429 extern char* g_root_home_dir;
430 extern char* g_root_home_dir_env;
431
432 #endif
433
434 int should_drop_privileges(void);
435 void send_device_status();
436 int set_sdk_user_privileges(int is_drop_capability_after_fork);
437 int set_root_privileges();
438
439 int get_emulator_forward_port(void);
440 int get_emulator_name(char str[], int str_size);
441 int get_device_name(char str[], int str_size);
442 int get_emulator_hostip(char str[], int str_size);
443 int get_emulator_guestip(char str[], int str_size);
444
445 /* packet allocator */
446 apacket *get_apacket(void);
447 void put_apacket(apacket *p);
448
449 int check_header(apacket *p, atransport *t);
450 int check_data(apacket *p);
451
452 #if !TRACE_PACKETS
453 #define print_packet(tag,p) do {} while (0)
454 #endif
455
456 #if SDB_HOST_ON_TARGET
457 /* sdb and sdbd are coexisting on the target, so use 26099 for sdb
458  * to avoid conflicting with sdbd's usage of 26098
459  */
460 #  define DEFAULT_SDB_PORT 26099 /* tizen specific */
461 #else
462 #  define DEFAULT_SDB_PORT 26099 /* tizen specific */
463 #endif
464
465 #  define QEMU_FORWARD_IP "10.0.2.2"
466
467 #define DEFAULT_SDB_LOCAL_TRANSPORT_PORT 26101 /* tizen specific */
468 #define DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT 26103 /* tizen specific */
469
470 #define SDB_CLASS              0xff
471 #define SDB_SUBCLASS           0x20 //0x42 /* tizen specific */
472 #define SDB_PROTOCOL           0x02 //0x01 /* tizen specific */
473
474
475 void local_init(int port);
476 int  local_connect(int  port, const char *device_name);
477 int  local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
478
479 /* usb host/client interface */
480 #if SDB_HOST
481 void usb_init();
482 void usb_cleanup();
483 int usb_write(usb_handle *h, const void *data, int len);
484 int usb_read(usb_handle *h, void *data, size_t len);
485 int usb_close(usb_handle *h);
486 void usb_kick(usb_handle *h);
487 #else
488
489 extern void (*usb_init)();
490 extern void (*usb_cleanup)();
491 extern int (*usb_write)(usb_handle *h, const void *data, int len);
492 extern int (*usb_read)(usb_handle *h, void *data, size_t len);
493 extern int (*usb_close)(usb_handle *h);
494 extern void (*usb_kick)(usb_handle *h);
495
496 /* functionfs backend */
497 void ffs_usb_init();
498 void ffs_usb_cleanup();
499 int ffs_usb_write(usb_handle *h, const void *data, int len);
500 int ffs_usb_read(usb_handle *h, void *data, size_t len);
501 int ffs_usb_close(usb_handle *h);
502 void ffs_usb_kick(usb_handle *h);
503
504 /* kernel sdb gadget backend */
505 void linux_usb_init();
506 void linux_usb_cleanup();
507 int linux_usb_write(usb_handle *h, const void *data, int len);
508 int linux_usb_read(usb_handle *h, void *data, size_t len);
509 int linux_usb_close(usb_handle *h);
510 void linux_usb_kick(usb_handle *h);
511
512 #endif
513
514 /* used for USB device detection */
515 #if SDB_HOST
516 int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
517 #endif
518
519 unsigned host_to_le32(unsigned n);
520 int sdb_commandline(int argc, char **argv);
521
522 int connection_state(atransport *t);
523
524 #define CS_ANY       -1
525 #define CS_OFFLINE    0
526 #define CS_BOOTLOADER 1
527 #define CS_DEVICE     2
528 #define CS_HOST       3
529 #define CS_RECOVERY   4
530 #define CS_NOPERM     5 /* Insufficient permissions to communicate with the device */
531 #define CS_SIDELOAD   6
532 #define CS_PWLOCK     10
533
534 extern int HOST;
535 extern int SHELL_EXIT_NOTIFY_FD;
536 #if !SDB_HOST
537 extern SdbdCommandlineArgs sdbd_commandline_args;
538 #endif
539
540 #define CHUNK_SIZE (64*1024)
541 #define SDBD_SHELL_CMD_MAX 4096
542
543 int sendfailmsg(int fd, const char *reason);
544 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
545 int copy_packet(apacket* dest, apacket* src);
546
547 int is_emulator(void);
548 #define DEFAULT_DEVICENAME "unknown"
549
550 #if SDB_HOST /* tizen-specific */
551 #define DEVICEMAP_SEPARATOR ":"
552 #define DEVICENAME_MAX 256
553 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above
554
555 void register_device_name(const char *device_type, const char *device_name, int port);
556 int get_devicename_from_shdmem(int port, char *device_name);
557 int read_line(const int fd, char* ptr, const size_t maxlen);
558 #endif
559 #endif
560
561 #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb"
562 #define USB_NODE_FILE "/dev/samsung_sdb"
563 int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[]);
564 void get_env(char *key, char **env);
565
566 #define RESERVE_CAPABILITIES_AFTER_FORK 0
567 #define DROP_CAPABILITIES_AFTER_FORK 1
568