Skip to content

Commit

Permalink
Merge branch 'feat-usb' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpeqf committed Jun 11, 2024
2 parents a4ae57d + 8a8ce97 commit b7fd245
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 120 deletions.
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
4 changes: 4 additions & 0 deletions cmake/xdbd_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#cmakedefine XDBD_CONFIG_WIN
#cmakedefine XDBD_CONFIG_DEBUG_POOL

#if !defined(XDBD_CONFIG_LINUX) && !defined(XDBD_CONFIG_OSX) && !defined(XDBD_CONFIG_WIN)
# error "Unknow platform"
#endif

#define USB_ADB_PATH "/dev/adb/usb"
#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/"

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
193 changes: 99 additions & 94 deletions src/connection/usb/xdbd_connection_usb.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@

#include <linux/usb/ch9.h>

#define MODULE_NAME "xdbd_connection_usb"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#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 +24,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 All @@ -27,11 +40,20 @@ struct adb_functionfs_strings_head {
bfdev_le32 lang_count;
} __bfdev_packed;

struct adb_endpoint_descriptor_no_audio {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
bfdev_le16 wMaxPacketSize;
uint8_t bInterval;
} __bfdev_packed;

static const struct {
struct adb_functionfs_descs_head header;
struct {
struct usb_interface_descriptor intf;
struct usb_endpoint_descriptor source, sink;
struct adb_endpoint_descriptor_no_audio source, sink;
} __bfdev_packed fs_descs, hs_descs;
} __bfdev_packed adb_descriptors = {
.header = {
Expand Down Expand Up @@ -113,99 +135,25 @@ 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;
if (write(uls->fd_ctr, &adb_descriptors, sizeof(adb_descriptors)) != sizeof(adb_descriptors)) {
bfdev_log_warn("send adb descriptors failed\n");
return XDBD_ERR;
}
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_strings, sizeof(adb_strings)) != sizeof(adb_strings)) {
bfdev_log_warn("send adb strings failed\n");
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;
}
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;
#endif

// 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);

// return 0;
// }

xdbd_usb_listening_t *xdbd_create_usb_listening(xdbd_t *xdbd) {
xdbd_usb_listening_t *uls;
Expand All @@ -224,18 +172,75 @@ 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, O_RDWR);
if (uls->fd_ctr < 0) {
bfdev_log_err("%s open failed: %m\n", uls->ctr.data);
return XDBD_ERR;
}

#if defined(XDBD_CONFIG_LINUX)
return usb_init_send(uls);
#endif

uls->fd_out = xdbd_open((char *)uls->out.data, O_RDWR);
if (uls->fd_out < 0) {
bfdev_log_err("%s open failed: %m\n", uls->out.data);
return XDBD_ERR;
}

uls->fd_in = xdbd_open((char *)uls->in.data, O_RDWR);
if (uls->fd_in < 0) {
bfdev_log_err("%s open failed: %m\n", uls->in.data);
return XDBD_ERR;
}

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
Loading

0 comments on commit b7fd245

Please sign in to comment.