Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Origin/feat usb #8

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/xdbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
173 changes: 80 additions & 93 deletions src/connection/usb/xdbd_connection_usb.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@

#include <linux/usb/ch9.h>
#include "connection.h"
#include "xdbd.h"
#include <xdbd_event.h>

#include <xdbd_timer.h>
#include <xdbd_connection_usb.h>

#if defined(XDBD_CONFIG_LINUX)
#include <linux/usb/ch9.h>
#endif

#define ADB_DESCRIPTORS_MAGIC 1
#define ADB_STRINGS_MAGIC 2
#define ADB_CLASS 0xff
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
16 changes: 11 additions & 5 deletions src/connection/usb/xdbd_connection_usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <xdbd_string.h>
#include <connection.h>

#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;
Expand All @@ -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__*/
19 changes: 9 additions & 10 deletions src/event/xdbd_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -387,23 +388,21 @@ 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;

rev->accepted = 1;

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;
Expand Down
21 changes: 12 additions & 9 deletions src/event/xdbd_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Copyright(c) 2024 John Sanpe <[email protected]>
*/

#include "bfdev/container.h"
#include "bfdev/log.h"
#include "xdbd.h"
#include <xdbd_event.h>
#include <xdbd_timer.h>
#include <xdbd_times.h>
Expand Down Expand Up @@ -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 (;;) {
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/event/xdbd_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
Loading
Loading