Skip to content

Commit

Permalink
Merge pull request #7 from openbfdev/feat-usb
Browse files Browse the repository at this point in the history
Feat usb
  • Loading branch information
sanpeqf authored Jun 10, 2024
2 parents ae0714e + 66b318a commit 8a39121
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 86 deletions.
9 changes: 9 additions & 0 deletions cmake/xdbd_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
15 changes: 12 additions & 3 deletions include/xdbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <unistd.h>
#include <bfdev/log.h>
#include <errno.h>
#include <xdbd_string.h>

#define XDBD_OK 0
#define XDBD_ERR -1
Expand All @@ -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;
Expand All @@ -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;
};

Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/connection/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/connection/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <xdbd_connection_tcp.h>
#include <xdbd_connection_usb.h>

#endif /*__CONNECTION__H__*/
22 changes: 11 additions & 11 deletions src/connection/tcp/xdbd_connection_tcp.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <xdbd_connection_tcp.h>
#include <xdbd_time.h>

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) {
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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)
}

Expand All @@ -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) {
Expand Down
8 changes: 3 additions & 5 deletions src/connection/tcp/xdbd_connection_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

#include <xdbd.h>

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;
Expand All @@ -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__*/
115 changes: 70 additions & 45 deletions src/connection/usb/xdbd_connection_usb.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
#include <bfdev.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <xdbd_connection_usb.h>

#include <linux/usb/ch9.h>
#include <xdbd_timer.h>

#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 <xdbd_connection_usb.h>

#define ADB_DESCRIPTORS_MAGIC 1
#define ADB_STRINGS_MAGIC 2
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
30 changes: 30 additions & 0 deletions src/connection/usb/xdbd_connection_usb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef __XDBD_CONNECTION_USB__H__
#define __XDBD_CONNECTION_USB__H__

#include <xdbd.h>
#include <xdbd_string.h>
#include <connection.h>

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__*/
7 changes: 7 additions & 0 deletions src/core/xdbd_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading

0 comments on commit 8a39121

Please sign in to comment.