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