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