Skip to content

Commit

Permalink
feat usb: init support use framwork
Browse files Browse the repository at this point in the history
  • Loading branch information
ffashion committed Jun 9, 2024
1 parent df49c40 commit 034c4f7
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 33 deletions.
13 changes: 11 additions & 2 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 @@ -56,6 +63,8 @@ struct xdbd_s {
/*socket*/
#define xdbd_socket socket
#define xdbd_close_socket close
#define xdbd_open open
#define xdbd_access access

typedef int xdbd_socket_t;

Expand Down
12 changes: 12 additions & 0 deletions mycompile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
rm -rf build
mkdir -p build
cd build

VERBOSE_MAKEFILE=OFF

cmake ../ -DCMAKE_VERBOSE_MAKEFILE=${VERBOSE_MAKEFILE} -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DXDBD_CONFIG_DEBUG_POOL=1 -DCMAKE_INSTALL_PREFIX=`pwd`/install

make -j10
make install

cp compile_commands.json ../
3 changes: 2 additions & 1 deletion src/connection/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct xdbd_connection_s {

xdbd_adb_connection_t *ac;
void *data;
xdbd_listening_t *listening;
xdbd_tcp_listening_t *listening;

off_t sent;

Expand Down Expand Up @@ -51,5 +51,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__*/
16 changes: 8 additions & 8 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 Down Expand Up @@ -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__*/
37 changes: 37 additions & 0 deletions src/connection/usb/xdbd_connection_usb.c
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
#include <xdbd_connection_usb.h>

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;
}

uls->fd = (xdbd_socket_t) -1;

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 (uls[i].fd != (xdbd_socket_t) -1) {
continue;
}

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

#include <xdbd.h>
#include <xdbd_string.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 fd;
xdbd_connection_t *connection;
};

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);
}
1 change: 1 addition & 0 deletions src/core/xdbd_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__*/
4 changes: 3 additions & 1 deletion src/core/xdbd_string.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __XDBD_STRING__H__
#define __XDBD_STRING__H__
#include <xdbd.h>

#include <unistd.h>
#include <sys/types.h>

typedef struct {
ssize_t size;
Expand Down
68 changes: 52 additions & 16 deletions src/event/xdbd_event.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "xdbd_string.h"
#include "xdbd_timer.h"
#include <bfdev/log.h>
#include <bfdev/list.h>
Expand All @@ -13,6 +14,7 @@
#include <string.h>
#include <xdbd_adb.h>
#include <xdbd_times.h>
#include <string.h>

extern xdbd_event_module_t xdbd_select_module;

Expand Down Expand Up @@ -107,7 +109,7 @@ xdbd_close_accepted_connection(xdbd_connection_t *c) {

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;
Expand Down Expand Up @@ -202,8 +204,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));
Expand All @@ -215,7 +217,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;
}
Expand All @@ -225,23 +227,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("/dev/adb/usb");
uls->ctr = (xdbd_str_t)xdbd_string("/dev/usb-ffs/adb/ep0");
uls->out = (xdbd_str_t)xdbd_string("/dev/usb-ffs/adb/ep1");
uls->in = (xdbd_str_t)xdbd_string("/dev/usb-ffs/adb/ep2");

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;
}

Expand All @@ -265,7 +290,7 @@ 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;

#if (XDBD_HAVE_SELECT)
emodule = &xdbd_select_module;
Expand Down Expand Up @@ -324,10 +349,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;
Expand All @@ -350,6 +375,17 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) {
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;
}

static int xdbd_event_post_configration(xdbd_t *xdbd) {
if (xdbd_open_listening_sockets(xdbd) != XDBD_OK) {
return XDBD_ERR;
Expand Down

0 comments on commit 034c4f7

Please sign in to comment.