Skip to content
Open
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 @@ -35,6 +35,7 @@ set (ADB_SRCS
adb_banner.c
adb_client.c
adb_frame.c
adb_list.c
hal/hal_uv.c
hal/hal_uv_packet.c
hal/hal_uv_client_tcp.c)
Expand Down
48 changes: 41 additions & 7 deletions adb.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <stdbool.h>
#include <assert.h>

#include "adb_list.h"

#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
Expand Down Expand Up @@ -81,6 +83,13 @@ void adb_reboot_impl(const char *target);
struct adb_client_s;
struct adb_service_s;
struct apacket_s;
#ifdef CONFIG_ADBD_SOCKET_SERVICE
struct adb_reverse_server_s;

typedef struct adb_tcp_socket_s adb_tcp_socket_t;
typedef struct adb_tcp_conn_s adb_tcp_conn_t;
typedef struct adb_tcp_server_s adb_tcp_server_t;
#endif

/****************************************************************************
* Public types
Expand Down Expand Up @@ -116,17 +125,28 @@ typedef struct adb_service_ops_s {
} adb_service_ops_t;

typedef struct adb_service_s {
/* chain pointers for the local/remote list of
** asockets that this asocket lives in
*/
struct adb_service_s *next;
/* Pointer used to keep track of all the services created by a client */
adb_list_entry_t entry;
const adb_service_ops_t *ops;

/* the unique identifier for this service */
int id;
int peer_id;
} adb_service_t;

#ifdef CONFIG_ADBD_SOCKET_SERVICE
typedef struct adb_reverse_server_s {
/* Pointer used to keep track of all the reverse servers
* created by a client.
*/
adb_list_entry_t entry;

/* Reverse servers local and remote tcp ports */
uint16_t local_port;
uint16_t remote_port;
} adb_reverse_server_t;
#endif

typedef struct adb_client_ops_s {
int (*write)(struct adb_client_s *client, apacket *p);
void (*kick)(struct adb_client_s *client);
Expand All @@ -136,11 +156,15 @@ typedef struct adb_client_ops_s {
typedef struct adb_client_s {
const adb_client_ops_t *ops;
int next_service_id;
adb_service_t *services;
adb_list_entry_t services;
uint8_t is_connected;
#ifdef CONFIG_ADBD_AUTHENTICATION
uint8_t token[CONFIG_ADBD_TOKEN_SIZE];
#endif
#ifdef CONFIG_ADBD_SOCKET_SERVICE
/* Keep track of reverse servers attached to a client */
adb_list_entry_t reverse_servers;
#endif
} adb_client_t;

typedef struct adb_context_s {
Expand Down Expand Up @@ -198,8 +222,11 @@ extern const unsigned char *g_adb_public_keys[];
/* TCP services */

#ifdef CONFIG_ADBD_SOCKET_SERVICE
typedef struct adb_tcp_socket_s adb_tcp_socket_t;
typedef struct adb_tcp_conn_s adb_tcp_conn_t;
void adb_register_reverse_server(adb_client_t *client,
adb_reverse_server_t *server);
void adb_reverse_server_close(adb_client_t *client,
adb_reverse_server_t *server);
void adb_hal_destroy_reverse_server(adb_reverse_server_t *server);

int adb_hal_socket_connect(struct adb_client_s *client, adb_tcp_socket_t *socket,
int port, adb_tcp_conn_t *conn,
Expand All @@ -208,6 +235,13 @@ int adb_hal_socket_connect(struct adb_client_s *client, adb_tcp_socket_t *socket
void adb_hal_socket_close(adb_tcp_socket_t *socket,
void (*close_cb)(adb_tcp_socket_t*));

int adb_hal_server_listen(struct adb_client_s *client, adb_tcp_server_t *server,
int port,
void (*on_connect_cb)(adb_tcp_server_t*, adb_tcp_socket_t*));

void adb_hal_server_close(adb_tcp_server_t *server,
void (*close_cb)(adb_tcp_server_t*));

int adb_hal_socket_write(adb_tcp_socket_t *socket, struct apacket_s *p,
void (*cb)(struct adb_client_s*, adb_tcp_socket_t*, struct apacket_s*, bool fail));

Expand Down
75 changes: 44 additions & 31 deletions adb_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ static void handle_close_frame(adb_client_t *client, apacket *p) {

svc = adb_client_find_service(client, p->msg.arg1, p->msg.arg0);
if (svc != NULL) {
/* Service cannot send anything after CLOSE packet */
adb_service_close(client, svc, NULL);
}
adb_hal_apacket_release(client, p);
Expand Down Expand Up @@ -334,9 +335,9 @@ void adb_send_data_frame(adb_client_t *client, apacket *p)

void adb_register_service(adb_service_t *svc, adb_client_t *client) {
svc->id = client->next_service_id++;
svc->next = client->services;

adb_log("id=%d, peer=%d\n", svc->id, svc->peer_id);
client->services = svc;
adb_list_insert(&client->services, &svc->entry);
}

static adb_service_t *adb_service_open(adb_client_t *client, const char *name, apacket *p)
Expand All @@ -363,6 +364,10 @@ static adb_service_t *adb_service_open(adb_client_t *client, const char *name, a
svc = tcp_forward_service(client, name, p);
break;
}
if (!strncmp(name, "reverse:", 8)) {
svc = tcp_reverse_service(client, name, p);
break;
}
#endif

#if defined(CONFIG_ADBD_SHELL_SERVICE) || defined(CONFIG_ADBD_LOGCAT_SERVICE)
Expand Down Expand Up @@ -391,7 +396,7 @@ static adb_service_t *adb_service_open(adb_client_t *client, const char *name, a
} while (0);

if (svc == NULL) {
adb_log("fail to init service %s\n", name);
/* Either service init failed or service completed in init function */
return NULL;
}

Expand All @@ -401,58 +406,58 @@ static adb_service_t *adb_service_open(adb_client_t *client, const char *name, a
}

void adb_service_close(adb_client_t *client, adb_service_t *svc, apacket *p) {
adb_service_t *cur_svc = client->services;

if (cur_svc == svc) {
client->services = svc->next;
goto exit_free_service;
}

while (cur_svc->next) {
if (cur_svc->next == svc) {
cur_svc->next = svc->next;
goto exit_free_service;
}
}

adb_log("service %p not found\n", svc);
return;
adb_list_remove(&client->services, &svc->entry);

exit_free_service:
if (p) {
send_close_frame(client, p, svc->id, svc->peer_id);
}

svc->ops->on_close(svc);
}

#ifdef CONFIG_ADBD_SOCKET_SERVICE
void adb_register_reverse_server(adb_client_t *client,
adb_reverse_server_t *server) {
adb_log("register tcp:%d <-> tcp:%d\n",
server->local_port, server->remote_port);
adb_list_insert(&client->reverse_servers, &server->entry);
}

void adb_reverse_server_close(adb_client_t *client,
adb_reverse_server_t *server) {
adb_list_remove(&client->reverse_servers, &server->entry);
adb_hal_destroy_reverse_server(server);
}
#endif

static adb_service_t* adb_client_find_service(adb_client_t *client, int id, int peer_id) {
adb_service_t *svc;

svc = client->services;
while (svc != NULL) {
adb_list_foreach(&client->services, svc, adb_service_t, entry) {
if (svc->id == id && (peer_id == 0 || svc->peer_id == peer_id)) {
return svc;
}
svc = svc->next;
}

return NULL;
}

void adb_client_kick_services(adb_client_t *client) {
adb_service_t *service = client->services;
while (service != NULL) {
adb_service_t *service;
adb_list_foreach(&client->services, service, adb_service_t, entry) {
if (service->ops->on_kick) {
service->ops->on_kick(service);
}
service = service->next;
}
}

static void adb_init_client(adb_client_t *client) {
/* setup adb_client */
client->next_service_id = 1;
client->services = NULL;
adb_list_init(&client->services);
#ifdef CONFIG_ADBD_SOCKET_SERVICE
adb_list_init(&client->reverse_servers);
#endif
client->is_connected = 0;
}

Expand All @@ -467,13 +472,21 @@ adb_client_t *adb_create_client(size_t size) {
}

void adb_destroy_client(adb_client_t *client) {
adb_service_t *service = client->services;
while (service != NULL) {
while (!adb_list_empty(&client->services)) {
adb_service_t *service = container_of(adb_list_next(&client->services),
adb_service_t, entry);
adb_log("stop service %d <-> %d\n", service->id, service->peer_id);
// FIXME send close frame ?
adb_service_close(client, service, NULL);
service = service->next;
}
#ifdef CONFIG_ADBD_SOCKET_SERVICE
while (!adb_list_empty(&client->reverse_servers)) {
adb_reverse_server_t *server =
container_of(adb_list_next(&client->reverse_servers),
adb_reverse_server_t, entry);

adb_reverse_server_close(client, server);
}
#endif
adb_hal_destroy_client(client);
}

Expand Down
41 changes: 41 additions & 0 deletions adb_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2022 Simon Piriou. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "adb_list.h"

void adb_list_remove(adb_list_entry_t *head, adb_list_entry_t *entry)
{
adb_list_entry_t *current = head->next;
if (head->next == NULL) {
/* List empty */
return;
}

if (current == entry) {
head->next = entry->next;
return;
}

while (current->next) {
if (current->next == entry) {
current->next = entry->next;
return;
}
}

/* Entry not found */
}
45 changes: 45 additions & 0 deletions adb_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2022 Simon Piriou. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#ifndef __ADB_LIST_H
#define __ADB_LIST_H

#include <stddef.h>

typedef struct adb_list_entry_s {
struct adb_list_entry_s *next;
} adb_list_entry_t;

#define adb_list_init(entry) (entry)->next = NULL
#define adb_list_empty(head) ((head)->next == NULL)
#define adb_list_next(entry) (entry)->next

#define adb_list_foreach(head, entry, type, member) \
for((entry) = container_of((head)->next, type, member);\
(entry); \
(entry) = container_of((entry)->member.next, type, member))


static inline void adb_list_insert(adb_list_entry_t *head,
adb_list_entry_t *entry) {
entry->next = head->next;
head->next = entry;
}

void adb_list_remove(adb_list_entry_t *head, adb_list_entry_t *entry);

#endif
6 changes: 6 additions & 0 deletions hal/hal_uv_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ struct adb_tcp_conn_s {
uv_connect_t connect_req;
void (*on_connect_cb)(adb_tcp_socket_t*, int);
};

typedef struct adb_tcp_server_s {
uv_tcp_t handle;
void (*close_cb)(struct adb_tcp_server_s*);
void (*on_connect_cb)(struct adb_tcp_server_s*, adb_tcp_socket_t*);
} adb_tcp_server_t;
#endif

/****************************************************************************
Expand Down
Loading