From 698320fb8f0d0a4b42a3c0819ffd62181ae55d04 Mon Sep 17 00:00:00 2001 From: ffashion Date: Tue, 11 Jun 2024 14:47:10 +0800 Subject: [PATCH 1/2] fix timer: make timer use event handler cb and fix unsigned overflow --- include/xdbd.h | 1 + src/event/xdbd_timer.c | 21 ++++++++++++--------- src/event/xdbd_timer.h | 2 +- src/xdbd.c | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/xdbd.h b/include/xdbd.h index 028f78e..34f9fe8 100644 --- a/include/xdbd.h +++ b/include/xdbd.h @@ -24,6 +24,7 @@ #define xdbd_msec_t unsigned +#define xdbd_msec_int_t int typedef struct xdbd_connection_s xdbd_connection_t; typedef struct xdbd_peer_connection_s xdbd_peer_connection_t; diff --git a/src/event/xdbd_timer.c b/src/event/xdbd_timer.c index 6cb4dfe..16f5a2b 100644 --- a/src/event/xdbd_timer.c +++ b/src/event/xdbd_timer.c @@ -3,6 +3,9 @@ * Copyright(c) 2024 John Sanpe */ +#include "bfdev/container.h" +#include "bfdev/log.h" +#include "xdbd.h" #include #include #include @@ -54,23 +57,24 @@ xdbd_msec_t xdbd_first_timeout(void) { xdbd_timer_t *timer; - xdbd_msec_t timeout; + xdbd_msec_int_t timeout; timer = xdbd_first_timer(); if (timer == NULL) { return XDBD_TIMER_INFINITE; } - timeout = timer->time - xdbd_current_msec; + timeout = (xdbd_msec_int_t)(timer->time - xdbd_current_msec); - return (xdbd_msec_t) (timeout > 0 ? timeout : 0); + return (xdbd_msec_int_t) (timeout > 0 ? timeout : 0); } -int +void xdbd_expire_timers(void) { xdbd_timer_t *timer; xdbd_msec_t timeout; + xdbd_event_t *ev; int retval; for (;;) { @@ -81,10 +85,9 @@ xdbd_expire_timers(void) timer = xdbd_first_timer(); xdbd_remove_timer(timer); - retval = timer->func(timer, timer->pdata); - if (retval) - return retval; - } + ev = bfdev_container_of(timer, xdbd_event_t, timer); - return 0; + ev->timeout = 1; + ev->handler(ev); + } } diff --git a/src/event/xdbd_timer.h b/src/event/xdbd_timer.h index a5a87f4..76542cc 100644 --- a/src/event/xdbd_timer.h +++ b/src/event/xdbd_timer.h @@ -35,7 +35,7 @@ xdbd_remove_timer(xdbd_timer_t *timer); extern xdbd_msec_t xdbd_first_timeout(void); -extern int +extern void xdbd_expire_timers(void); #endif /* __XDBD_TIMER__H__ */ diff --git a/src/xdbd.c b/src/xdbd.c index 6bc5595..d6d8bcd 100644 --- a/src/xdbd.c +++ b/src/xdbd.c @@ -5,6 +5,7 @@ #include #include #include +#include static void usage() { @@ -23,13 +24,12 @@ static xdbd_t *xdbd_init() { xdbd_t *xdbd = NULL; xdbd_pool_t *pool; + xdbd_time_update(); pool = xdbd_create_pool(); if (pool == NULL) { return NULL; } - // xdbd_destroy_pool(pool); - xdbd = xdbd_palloc(pool, sizeof(xdbd_t)); if (xdbd == NULL) { xdbd_destroy_pool(pool); From 6796718b434f71581c7cf7ade274ddd7850ccf83 Mon Sep 17 00:00:00 2001 From: ffashion Date: Tue, 11 Jun 2024 15:33:10 +0800 Subject: [PATCH 2/2] fix usb: support usb to use timer to trigger the event --- CMakeLists.txt | 1 + src/connection/usb/xdbd_connection_usb.c | 173 +++++++++++------------ src/connection/usb/xdbd_connection_usb.h | 16 ++- src/event/xdbd_event.c | 19 ++- 4 files changed, 101 insertions(+), 108 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d20ae15..3a83ae0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set(SRCS ${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb_request.c ${PROJECT_SOURCE_DIR}/src/connection/connection.c ${PROJECT_SOURCE_DIR}/src/connection/tcp/xdbd_connection_tcp.c + ${PROJECT_SOURCE_DIR}/src/connection/usb/xdbd_connection_usb.c ${PROJECT_SOURCE_DIR}/src/core/xdbd_buf.c ${PROJECT_SOURCE_DIR}/src/core/xdbd_pool.c ${PROJECT_SOURCE_DIR}/src/core/xdbd_times.c diff --git a/src/connection/usb/xdbd_connection_usb.c b/src/connection/usb/xdbd_connection_usb.c index f7fbf6d..91e6bf3 100644 --- a/src/connection/usb/xdbd_connection_usb.c +++ b/src/connection/usb/xdbd_connection_usb.c @@ -1,8 +1,15 @@ -#include +#include "connection.h" +#include "xdbd.h" +#include + #include #include +#if defined(XDBD_CONFIG_LINUX) + #include +#endif + #define ADB_DESCRIPTORS_MAGIC 1 #define ADB_STRINGS_MAGIC 2 #define ADB_CLASS 0xff @@ -13,6 +20,8 @@ #define MAX_PACKET_SIZE_FS 64 #define MAX_PACKET_SIZE_HS 512 +#if defined(XDBD_CONFIG_LINUX) + struct adb_functionfs_descs_head { bfdev_le32 magic; bfdev_le32 length; @@ -113,99 +122,23 @@ static const struct { }, }; -struct adb_connection { - int control; - int bulk_out; - int bulk_in; -}; - -static int -usb_init_send(xdbd_connection_t *ctl) -{ - int retval; - - 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(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; - } - - return 0; -} static int -usb_init_connection(xdbd_usb_listening_t *uls) +usb_init_send(xdbd_usb_listening_t *uls) { - xdbd_connection_t *ctl, *in, *out; - int retval; - - 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; - } - ctl->fd = retval; - - 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; + if (write(uls->fd_ctr, &adb_descriptors, sizeof(adb_descriptors)) != sizeof(adb_descriptors)) { + return XDBD_ERR; } - in->fd = retval; - 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; + if (write(uls->fd_ctr, &adb_strings, sizeof(adb_strings)) != sizeof(adb_strings)) { + return XDBD_ERR; } - out->fd = retval; - retval = usb_init_send(ctl); - if (retval) { - bfdev_log_err("adbd: init send failed\n"); - goto failed; - } - - return 0; - -failed: - 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; + return XDBD_OK; } -// 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; - -// bfdev_log_warn("adbd: usb kick timer retry\n"); -// xdbd_add_timer(timer, 1000); +#endif -// return 0; -// } xdbd_usb_listening_t *xdbd_create_usb_listening(xdbd_t *xdbd) { xdbd_usb_listening_t *uls; @@ -224,18 +157,72 @@ int xdbd_open_usb_listening_sockets(xdbd_t *xdbd) { xdbd_usb_listening_t *uls; int i; - uls = bfdev_array_data(&xdbd->tcp_listening, 0); + uls = bfdev_array_data(&xdbd->usb_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; - } - + if (uls->name.size == 0 || uls->ctr.size == 0 || uls->in.size == 0 || uls->out.size == 0) { + return XDBD_ERR; + } } return XDBD_OK; } + +int xdbd_event_open_usb_connection(xdbd_usb_listening_t *uls) { + + uls->fd_ctr = xdbd_open((char *)uls->ctr.data, 0); + if (uls->fd_ctr < 0) { + return XDBD_ERR; + } + + uls->fd_in = xdbd_open((char *)uls->in.data, 0); + if (uls->fd_in < 0) { + return XDBD_ERR; + } + + uls->fd_out = xdbd_open((char *)uls->out.data, 0); + if (uls->fd_out < 0) { + return XDBD_ERR; + } + +#if defined(XDBD_CONFIG_LINUX) + return usb_init_send(uls); +#endif + + return XDBD_OK; +} + +void xdbd_event_usb_accept(xdbd_event_t *ev) { + xdbd_usb_listening_t *uls; + xdbd_connection_t *c; +#if defined(XDBD_CONFIG_OSX) + bfdev_log_err("OSX not support yet\r\n"); + return; +#endif + + c = ev->data; + uls = c->ulistening; + + if (xdbd_event_open_usb_connection(uls) != XDBD_OK) { + return; + } + // FIXME: use ulistening + uls->conn_ctl = xdbd_get_connection(c->listening->xdbd, uls->fd_ctr); + if (uls->conn_ctl == NULL) { + return; + } + + uls->conn_in = xdbd_get_connection(c->listening->xdbd, uls->fd_in); + if (uls->conn_ctl == NULL) { + return; + } + + uls->conn_out = xdbd_get_connection(c->listening->xdbd, uls->fd_out); + if (uls->conn_ctl == NULL) { + return; + } + + //FIXME: how to process ctl, in and out??? + + ev->accepted = 1; +} diff --git a/src/connection/usb/xdbd_connection_usb.h b/src/connection/usb/xdbd_connection_usb.h index 15fba82..ef0ace9 100644 --- a/src/connection/usb/xdbd_connection_usb.h +++ b/src/connection/usb/xdbd_connection_usb.h @@ -5,6 +5,8 @@ #include #include +#define XDBD_CONNECTION_USB_TIMER_FD 0xFFFF +#define XDBD_CONNECTION_USB_TIMER_TIMEOUT 1000 struct xdbd_usb_listening_s { xdbd_str_t name; xdbd_str_t ctr; @@ -13,18 +15,22 @@ struct xdbd_usb_listening_s { 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; + //listeing connection + xdbd_connection_t *conn; + //process connection xdbd_connection_t *conn_ctl; xdbd_connection_t *conn_in; xdbd_connection_t *conn_out; + xdbd_socket_t fd_ctr; + xdbd_socket_t fd_in; + xdbd_socket_t fd_out; + }; xdbd_usb_listening_t *xdbd_create_usb_listening(xdbd_t *xdbd); int xdbd_open_usb_listening_sockets(xdbd_t *xdbd); + +void xdbd_event_usb_accept(xdbd_event_t *ev); #endif /*__XDBD_CONNECTION_USB__H__*/ diff --git a/src/event/xdbd_event.c b/src/event/xdbd_event.c index b29ac71..7f49f83 100644 --- a/src/event/xdbd_event.c +++ b/src/event/xdbd_event.c @@ -107,13 +107,14 @@ 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) { + if (ev->accepted == 0) { + //this usb already connected + return; + } - xdbd_event_usb_accept(ev); + xdbd_add_timer(&ev->timer, XDBD_CONNECTION_USB_TIMER_TIMEOUT); + return xdbd_event_usb_accept(ev); } @@ -387,13 +388,13 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { assert(uls != NULL); for (i = 0; i < bfdev_array_index(&xdbd->usb_listening); i++) { - c = xdbd_get_connection(xdbd, uls[i].fd_ctr); + c = xdbd_get_connection(xdbd, XDBD_CONNECTION_USB_TIMER_FD); if (c == NULL) { return XDBD_ERR; } c->ulistening = &uls[i]; - uls[i].conn_ctl = c; + uls[i].conn = c; rev = c->read; @@ -401,9 +402,7 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { rev->handler = xdbd_event_try_usb_accept; - if (xdbd_add_event(rev, XDBD_READ_EVENT, 0) == XDBD_ERR) { - return XDBD_ERR; - } + xdbd_add_timer(&rev->timer, XDBD_CONNECTION_USB_TIMER_TIMEOUT); } return XDBD_OK;