diff --git a/cmake/xdbd_config.h.in b/cmake/xdbd_config.h.in index 0c4e952..0c7dcc6 100644 --- a/cmake/xdbd_config.h.in +++ b/cmake/xdbd_config.h.in @@ -11,5 +11,14 @@ #cmakedefine XDBD_CONFIG_WIN #cmakedefine XDBD_CONFIG_DEBUG_POOL +#define USB_ADB_PATH "/dev/adb/usb" +#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" + +#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x +#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) +#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) +#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) + #define XDBD_HAVE_SELECT 1 + #endif /* __XDBD_CONFIG__H__ */ diff --git a/include/xdbd.h b/include/xdbd.h index 1b31e0e..028f78e 100644 --- a/include/xdbd.h +++ b/include/xdbd.h @@ -12,6 +12,7 @@ #include #include #include +#include #define XDBD_OK 0 #define XDBD_ERR -1 @@ -28,12 +29,16 @@ typedef struct xdbd_connection_s xdbd_connection_t; typedef struct xdbd_peer_connection_s xdbd_peer_connection_t; typedef struct xdbd_adb_connection_s xdbd_adb_connection_t; typedef struct xdbd_adb_request_s xdbd_adb_request_t; -typedef struct xdbd_listening_s xdbd_listening_t; +typedef struct xdbd_tcp_listening_s xdbd_tcp_listening_t; +typedef struct xdbd_usb_listening_s xdbd_usb_listening_t; +typedef struct xdbd_usb_s xdbd_usb_t; typedef struct xdbd_event_s xdbd_event_t; typedef struct xdbd_buf_s xdbd_buf_t; typedef struct xdbd_timer_s xdbd_timer_t; typedef struct xdbd_s xdbd_t; +typedef void (*xdbd_connection_handler_pt)(xdbd_connection_t *c); + struct xdbd_s { unsigned connection_n; xdbd_connection_t *connections; @@ -43,7 +48,9 @@ struct xdbd_s { xdbd_event_t *write_events; xdbd_event_t *read_events; - bfdev_array_t listening; + bfdev_array_t tcp_listening; + bfdev_array_t usb_listening; + // xdbd_usb_t usb; xdbd_pool_t *pool; }; @@ -55,7 +62,9 @@ struct xdbd_s { /*socket*/ #define xdbd_socket socket -#define xdbd_close_socket close +#define xdbd_open open +#define xdbd_close close +#define xdbd_access access typedef int xdbd_socket_t; diff --git a/src/connection/connection.c b/src/connection/connection.c index 689b3ef..5579b05 100644 --- a/src/connection/connection.c +++ b/src/connection/connection.c @@ -80,10 +80,10 @@ void xdbd_close_connection(xdbd_t *xdbd, xdbd_connection_t *c) { fd = c->fd; c->fd = (xdbd_socket_t) -1; - if (xdbd_close_socket(fd) == -1) { + if (xdbd_close(fd) == -1) { err = xdbd_socket_errno; - bfdev_log_debug("xdbd_close_socket error"); + bfdev_log_debug("xdbd_close error"); } } diff --git a/src/connection/connection.h b/src/connection/connection.h index d2c8d50..88272ca 100644 --- a/src/connection/connection.h +++ b/src/connection/connection.h @@ -23,7 +23,8 @@ struct xdbd_connection_s { xdbd_adb_connection_t *ac; void *data; - xdbd_listening_t *listening; + xdbd_tcp_listening_t *listening; + xdbd_usb_listening_t *ulistening; off_t sent; @@ -51,5 +52,6 @@ ssize_t xdbd_unix_read(xdbd_connection_t *c, u_char *buf, size_t size); ssize_t xdbd_unix_write(xdbd_connection_t *c, u_char *buf, size_t size); #include +#include #endif /*__CONNECTION__H__*/ diff --git a/src/connection/tcp/xdbd_connection_tcp.c b/src/connection/tcp/xdbd_connection_tcp.c index c5aaec9..cb91fb4 100644 --- a/src/connection/tcp/xdbd_connection_tcp.c +++ b/src/connection/tcp/xdbd_connection_tcp.c @@ -1,16 +1,16 @@ #include #include -xdbd_listening_t *xdbd_create_listening(xdbd_t *xdbd, struct sockaddr *sockaddr, socklen_t socklen) { - xdbd_listening_t *ls; +xdbd_tcp_listening_t *xdbd_create_tcp_listening(xdbd_t *xdbd, struct sockaddr *sockaddr, socklen_t socklen) { + xdbd_tcp_listening_t *ls; struct sockaddr *sa; - ls = bfdev_array_push(&xdbd->listening, 1); + ls = bfdev_array_push(&xdbd->tcp_listening, 1); if (ls == NULL) { return NULL; } - xdbd_memzero(ls, sizeof(xdbd_listening_t)); + xdbd_memzero(ls, sizeof(xdbd_tcp_listening_t)); sa = xdbd_palloc(xdbd->pool, socklen); if (sa == NULL) { @@ -30,13 +30,13 @@ xdbd_listening_t *xdbd_create_listening(xdbd_t *xdbd, struct sockaddr *sockaddr, int _xdbd_open_listening_sockets(xdbd_t *xdbd) { int i; - xdbd_listening_t *ls; + xdbd_tcp_listening_t *ls; xdbd_socket_t s; xdbd_err_t err; - ls = bfdev_array_data(&xdbd->listening, 0); + ls = bfdev_array_data(&xdbd->tcp_listening, 0); - for (i = 0; i < bfdev_array_index(&xdbd->listening); i++) { + for (i = 0; i < bfdev_array_index(&xdbd->tcp_listening); i++) { if (ls[i].fd != (xdbd_socket_t) -1) { continue; } @@ -51,8 +51,8 @@ int _xdbd_open_listening_sockets(xdbd_t *xdbd) { if (xdbd_nonblocking(s) == -1) { bfdev_log_err("xdbd_nonblocking error\n"); - if (xdbd_close_socket(s) == -1) { - bfdev_log_err("xdbd_close_socket error"); + if (xdbd_close(s) == -1) { + bfdev_log_err("xdbd_close error"); } return XDBD_ERR; @@ -65,7 +65,7 @@ int _xdbd_open_listening_sockets(xdbd_t *xdbd) { // bfdev_log_err("bind() to %V failed", &ls[i].addr_text) } - if (xdbd_close_socket(s) == -1) { + if (xdbd_close(s) == -1) { // bfdev_log_err("close() %s failed", &ls[i].addr_text) } @@ -90,7 +90,7 @@ int _xdbd_open_listening_sockets(xdbd_t *xdbd) { return XDBD_OK; } -int xdbd_open_listening_sockets(xdbd_t *xdbd) { +int xdbd_open_tcp_listening_sockets(xdbd_t *xdbd) { int tries, rc; for (tries = 0; tries < 90; ++tries) { diff --git a/src/connection/tcp/xdbd_connection_tcp.h b/src/connection/tcp/xdbd_connection_tcp.h index d4ff650..cdccd19 100644 --- a/src/connection/tcp/xdbd_connection_tcp.h +++ b/src/connection/tcp/xdbd_connection_tcp.h @@ -3,9 +3,7 @@ #include -typedef void (*xdbd_connection_handler_pt)(xdbd_connection_t *c); - -struct xdbd_listening_s { +struct xdbd_tcp_listening_s { xdbd_socket_t fd; unsigned type; struct sockaddr *sockaddr; @@ -22,8 +20,8 @@ struct xdbd_listening_s { }; -xdbd_listening_t *xdbd_create_listening(xdbd_t *xdbd, struct sockaddr *sockaddr, socklen_t socklen); -int xdbd_open_listening_sockets(xdbd_t *xdbd); +xdbd_tcp_listening_t *xdbd_create_tcp_listening(xdbd_t *xdbd, struct sockaddr *sockaddr, socklen_t socklen); +int xdbd_open_tcp_listening_sockets(xdbd_t *xdbd); #endif /*__XDBD_CONNECTION_TCP__H__*/ diff --git a/src/connection/usb/xdbd_connection_usb.c b/src/connection/usb/xdbd_connection_usb.c index 3b2b17b..f7fbf6d 100644 --- a/src/connection/usb/xdbd_connection_usb.c +++ b/src/connection/usb/xdbd_connection_usb.c @@ -1,17 +1,7 @@ -#include -#include -#include -#include + #include #include - -#define USB_ADB_PATH "/dev/adb/usb" -#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" - -#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x -#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) -#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) -#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) +#include #define ADB_DESCRIPTORS_MAGIC 1 #define ADB_STRINGS_MAGIC 2 @@ -130,17 +120,17 @@ struct adb_connection { }; static int -usb_init_send(struct adb_connection *conn) +usb_init_send(xdbd_connection_t *ctl) { int retval; - retval = write(conn->control, &adb_descriptors, sizeof(adb_descriptors)); + retval = write(ctl->fd, &adb_descriptors, sizeof(adb_descriptors)); if (retval != sizeof(adb_descriptors)) { bfdev_log_err("adbd: %s write descriptors failed: %m\n", USB_FFS_ADB_EP0); return retval; } - retval = write(conn->control, &adb_strings, sizeof(adb_strings)); + retval = write(ctl->fd, &adb_strings, sizeof(adb_strings)); if (retval != sizeof(adb_strings)) { bfdev_log_err("adbd: %s write strings failed: %m\n", USB_FFS_ADB_EP0); return retval; @@ -150,32 +140,38 @@ usb_init_send(struct adb_connection *conn) } static int -usb_init_connection(struct adb_connection *conn) +usb_init_connection(xdbd_usb_listening_t *uls) { + xdbd_connection_t *ctl, *in, *out; int retval; - retval = open(USB_FFS_ADB_EP0, O_RDWR); + ctl = uls->conn_ctl; + in = uls->conn_in; + out = uls->conn_out; + + + retval = xdbd_open((char *)uls->ctr.data, O_RDWR); if (retval < 0) { bfdev_log_err("adbd: %s open failed: %m\n", USB_FFS_ADB_EP0); goto failed; } - conn->control = retval; + ctl->fd = retval; - retval = open(USB_FFS_ADB_OUT, O_RDWR); + retval = xdbd_open((char *)uls->out.data, O_RDWR); if (retval < 0) { bfdev_log_err("adbd: %s open failed: %m\n", USB_FFS_ADB_OUT); goto failed; } - conn->bulk_out = retval; + in->fd = retval; - retval = open(USB_FFS_ADB_IN, O_RDWR); + retval = xdbd_open((char *)uls->in.data, O_RDWR); if (retval < 0) { bfdev_log_err("adbd: %s open failed: %m\n", USB_FFS_ADB_IN); goto failed; } - conn->bulk_in = retval; + out->fd = retval; - retval = usb_init_send(conn); + retval = usb_init_send(ctl); if (retval) { bfdev_log_err("adbd: init send failed\n"); goto failed; @@ -184,33 +180,62 @@ usb_init_connection(struct adb_connection *conn) return 0; failed: - if (conn->control >= 0) - close(conn->control); - if (conn->bulk_out >= 0) - close(conn->bulk_out); - if (conn->bulk_in >= 0) - close(conn->bulk_in); - - conn->control = -1; - conn->bulk_out = -1; - conn->bulk_in = -1; + if (ctl->fd >= 0) + xdbd_close(ctl->fd); + if (in->fd >= 0) + xdbd_close(in->fd); + if (out->fd >= 0) + xdbd_close(out->fd); return retval; } -static int -usb_kick_timer(xdbd_timer_t *timer, void *pdata) -{ - struct adb_connection *conn; - int retval; +// static int +// usb_kick_timer(xdbd_timer_t *timer, void *pdata) +// { +// struct adb_connection *conn; +// int retval; - conn = pdata; - retval = usb_init_connection(conn); - if (!retval) - return 0; +// conn = pdata; +// retval = usb_init_connection(conn); +// if (!retval) +// return 0; - bfdev_log_warn("adbd: usb kick timer retry\n"); - xdbd_add_timer(timer, 1000); +// bfdev_log_warn("adbd: usb kick timer retry\n"); +// xdbd_add_timer(timer, 1000); - return 0; +// return 0; +// } + +xdbd_usb_listening_t *xdbd_create_usb_listening(xdbd_t *xdbd) { + xdbd_usb_listening_t *uls; + + uls = bfdev_array_push(&xdbd->usb_listening, 1); + if (uls == NULL) { + return uls; + } + + xdbd_memzero(uls, sizeof(*uls)); + + return uls; +} + +int xdbd_open_usb_listening_sockets(xdbd_t *xdbd) { + xdbd_usb_listening_t *uls; + int i; + + uls = bfdev_array_data(&xdbd->tcp_listening, 0); + + for (i = 0; i < bfdev_array_index(&xdbd->usb_listening); i++) { + if (xdbd_access((const char *)uls[i].name.data, F_OK) == 0) { + return XDBD_ERR; + } + + if (xdbd_access((const char *)uls[i].ctr.data, F_OK) == 0) { + return XDBD_ERR; + } + + } + + return XDBD_OK; } diff --git a/src/connection/usb/xdbd_connection_usb.h b/src/connection/usb/xdbd_connection_usb.h index e69de29..15fba82 100644 --- a/src/connection/usb/xdbd_connection_usb.h +++ b/src/connection/usb/xdbd_connection_usb.h @@ -0,0 +1,30 @@ +#ifndef __XDBD_CONNECTION_USB__H__ +#define __XDBD_CONNECTION_USB__H__ + +#include +#include +#include + +struct xdbd_usb_listening_s { + xdbd_str_t name; + xdbd_str_t ctr; + xdbd_str_t in; + xdbd_str_t out; + + xdbd_connection_handler_pt handler; + + xdbd_socket_t event_fd; + + xdbd_socket_t fd_ctr; + xdbd_socket_t fd_in; + xdbd_socket_t fd_out; + + xdbd_connection_t *conn_ctl; + xdbd_connection_t *conn_in; + xdbd_connection_t *conn_out; + +}; + +xdbd_usb_listening_t *xdbd_create_usb_listening(xdbd_t *xdbd); +int xdbd_open_usb_listening_sockets(xdbd_t *xdbd); +#endif /*__XDBD_CONNECTION_USB__H__*/ diff --git a/src/core/xdbd_pool.c b/src/core/xdbd_pool.c index e82ed57..4868516 100644 --- a/src/core/xdbd_pool.c +++ b/src/core/xdbd_pool.c @@ -154,3 +154,10 @@ void xdbd_destroy_pool(xdbd_pool_t *pool) { xdbd_release_pool(pool); bfdev_free(NULL, pool); } + +void xdbd_cleanup_array(void *pdata) { + bfdev_array_t *a; + a = pdata; + + bfdev_array_release(a); +} diff --git a/src/core/xdbd_pool.h b/src/core/xdbd_pool.h index 2ebaded..04d1c22 100644 --- a/src/core/xdbd_pool.h +++ b/src/core/xdbd_pool.h @@ -55,4 +55,5 @@ xdbd_pool_t *xdbd_create_pool(); void xdbd_release_pool(xdbd_pool_t *pool); void xdbd_destroy_pool(xdbd_pool_t *pool); +void xdbd_cleanup_array(void *pdata); #endif /*__XDBD_POOL__H__*/ diff --git a/src/core/xdbd_string.h b/src/core/xdbd_string.h index 2b3c1e1..5269a77 100644 --- a/src/core/xdbd_string.h +++ b/src/core/xdbd_string.h @@ -1,6 +1,8 @@ #ifndef __XDBD_STRING__H__ #define __XDBD_STRING__H__ -#include + +#include +#include typedef struct { ssize_t size; diff --git a/src/event/xdbd_event.c b/src/event/xdbd_event.c index 482e57b..b29ac71 100644 --- a/src/event/xdbd_event.c +++ b/src/event/xdbd_event.c @@ -1,3 +1,4 @@ +#include "xdbd_string.h" #include "xdbd_timer.h" #include #include @@ -13,6 +14,7 @@ #include #include #include +#include extern xdbd_event_module_t xdbd_select_module; @@ -92,8 +94,8 @@ xdbd_close_accepted_connection(xdbd_connection_t *c) { s = c->fd; c->fd = (xdbd_socket_t)-1; - if (xdbd_close_socket(s) == (xdbd_socket_t) -1) { - bfdev_log_err("xdbd_close_socket close error"); + if (xdbd_close(s) == (xdbd_socket_t) -1) { + bfdev_log_err("xdbd_close close error"); } if (c->pool) { @@ -105,9 +107,19 @@ xdbd_close_accepted_connection(xdbd_connection_t *c) { } } +static void xdbd_event_usb_accept(xdbd_event_t *ev) { + +} + +static void xdbd_event_try_usb_accept(xdbd_event_t *ev) { + + xdbd_event_usb_accept(ev); +} + + static void xdbd_event_accept(xdbd_event_t *ev) { xdbd_connection_t *lc, *c; - xdbd_listening_t *ls; + xdbd_tcp_listening_t *ls; xdbd_sockaddr_t sa; socklen_t socklen; xdbd_socket_t s; @@ -202,8 +214,8 @@ void xdbd_adb_init_connection(xdbd_connection_t *c) { } } -static xdbd_listening_t *xdbd_push_adb_default_listen(xdbd_t *xdbd) { - xdbd_listening_t *ls; +static xdbd_tcp_listening_t *xdbd_push_adb_default_tcp_listen(xdbd_t *xdbd) { + xdbd_tcp_listening_t *ls; struct sockaddr_in *sin; sin = xdbd_palloc(xdbd->pool, sizeof(struct sockaddr_in)); @@ -215,7 +227,7 @@ static xdbd_listening_t *xdbd_push_adb_default_listen(xdbd_t *xdbd) { sin->sin_port = htons(ADB_CONNECTION_DEFAULT_PORT); sin->sin_addr.s_addr = INADDR_ANY; - ls = xdbd_create_listening(xdbd, (struct sockaddr *)sin, sizeof(struct sockaddr_in)); + ls = xdbd_create_tcp_listening(xdbd, (struct sockaddr *)sin, sizeof(struct sockaddr_in)); if (ls == NULL) { return NULL; } @@ -225,23 +237,46 @@ static xdbd_listening_t *xdbd_push_adb_default_listen(xdbd_t *xdbd) { return ls; } +static xdbd_usb_listening_t *xdbd_push_adb_default_usb_listen(xdbd_t *xdbd) { + xdbd_usb_listening_t *uls; + + uls = xdbd_create_usb_listening(xdbd); + if (uls == NULL) { + return NULL; + } -static void xdbd_cleanup_listening_handler(void *pdata) { - bfdev_array_t *a; - a = pdata; + uls->name = (xdbd_str_t)xdbd_string(USB_ADB_PATH); + uls->ctr = (xdbd_str_t)xdbd_string(USB_FFS_ADB_EP0); + uls->out = (xdbd_str_t)xdbd_string(USB_FFS_ADB_OUT); + uls->in = (xdbd_str_t)xdbd_string(USB_FFS_ADB_IN); - bfdev_array_release(a); + uls->handler = xdbd_adb_init_connection; + return uls; } static int xdbd_parse_listen_conf(xdbd_t *xdbd) { - xdbd_listening_t *ls; + xdbd_tcp_listening_t *tls; + xdbd_usb_listening_t *uls; - bfdev_array_init(&xdbd->listening, NULL, sizeof(xdbd_listening_t)); + bfdev_array_init(&xdbd->tcp_listening, NULL, sizeof(xdbd_tcp_listening_t)); - xdbd_pool_cleanup_add(xdbd->pool, xdbd_cleanup_listening_handler, &xdbd->listening); + if (xdbd_pool_cleanup_add(xdbd->pool, xdbd_cleanup_array, &xdbd->tcp_listening) != XDBD_OK) { + return XDBD_ERR; + } - ls = xdbd_push_adb_default_listen(xdbd); - if (ls == NULL) { + bfdev_array_init(&xdbd->usb_listening, NULL, sizeof(xdbd_usb_listening_t)); + + if (xdbd_pool_cleanup_add(xdbd->pool, xdbd_cleanup_array, &xdbd->tcp_listening) != XDBD_OK) { + return XDBD_ERR; + } + + tls = xdbd_push_adb_default_tcp_listen(xdbd); + if (tls == NULL) { + return XDBD_ERR; + } + + uls = xdbd_push_adb_default_usb_listen(xdbd); + if (uls == NULL) { return XDBD_ERR; } @@ -265,7 +300,8 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { xdbd_event_t *rev, *wev; int i; xdbd_connection_t *next, *c; - xdbd_listening_t *ls; + xdbd_tcp_listening_t *ls; + xdbd_usb_listening_t *uls; #if (XDBD_HAVE_SELECT) emodule = &xdbd_select_module; @@ -324,10 +360,10 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { xdbd->free_connections = next; xdbd->free_connection_n = xdbd->connection_n; - ls = bfdev_array_data(&xdbd->listening, 0); + ls = bfdev_array_data(&xdbd->tcp_listening, 0); assert(ls != NULL); - for (i = 0; i < bfdev_array_index(&xdbd->listening); i++) { + for (i = 0; i < bfdev_array_index(&xdbd->tcp_listening); i++) { c = xdbd_get_connection(xdbd, ls[i].fd); if (c == NULL) { return XDBD_ERR; @@ -347,6 +383,41 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { } } + uls = bfdev_array_data(&xdbd->usb_listening, 0); + assert(uls != NULL); + + for (i = 0; i < bfdev_array_index(&xdbd->usb_listening); i++) { + c = xdbd_get_connection(xdbd, uls[i].fd_ctr); + if (c == NULL) { + return XDBD_ERR; + } + + c->ulistening = &uls[i]; + uls[i].conn_ctl = c; + + rev = c->read; + + rev->accepted = 1; + + rev->handler = xdbd_event_try_usb_accept; + + if (xdbd_add_event(rev, XDBD_READ_EVENT, 0) == XDBD_ERR) { + return XDBD_ERR; + } + } + + return XDBD_OK; +} + +static int xdbd_open_listening_sockets(xdbd_t *xdbd) { + if (xdbd_open_tcp_listening_sockets(xdbd) != XDBD_OK) { + return XDBD_ERR; + } + + if (xdbd_open_usb_listening_sockets(xdbd) != XDBD_OK) { + return XDBD_ERR; + } + return XDBD_OK; }