-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathadb.h
228 lines (180 loc) · 6.7 KB
/
adb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (C) 2020 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_H
#define __ADB_H
#include <limits.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
#define container_of(ptr, type, member) \
((type *)((uintptr_t)(ptr) - offsetof(type, member)))
#ifndef fatal
#include <assert.h>
#define fatal(...) assert(0)
#endif
/* Logging methods */
#define ADB_ERR 0
#define ADB_WARN 1
#define ADB_INFO 2
void adb_log_impl(int priority, const char *func, int line,
const char *fmt, ...);
#define adb_log(...) \
adb_log_impl(ADB_INFO, __func__, __LINE__, __VA_ARGS__)
#define adb_warn(...) \
adb_log_impl(ADB_WARN, __func__, __LINE__, __VA_ARGS__)
#define adb_err(...) \
adb_log_impl(ADB_ERR, __func__, __LINE__, __VA_ARGS__)
/* Platform support */
void adb_reboot_impl(const char *target);
/* ADB protocol definitions */
#define A_SYNC 0x434e5953
#define A_CNXN 0x4e584e43
#define A_OPEN 0x4e45504f
#define A_OKAY 0x59414b4f
#define A_CLSE 0x45534c43
#define A_WRTE 0x45545257
#define A_AUTH 0x48545541
/* AUTH packets first argument */
/* Request */
#define ADB_AUTH_TOKEN 1
/* Response */
#define ADB_AUTH_SIGNATURE 2
#define ADB_AUTH_RSAPUBLICKEY 3
#define CS_ANY -1
#define CS_OFFLINE 0
#define CS_BOOTLOADER 1
#define CS_DEVICE 2
#define CS_HOST 3
#define CS_RECOVERY 4
#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */
#define CS_SIDELOAD 6
#define CS_UNAUTHORIZED 7
/* ADB protocol version */
#define A_VERSION 0x01000000
struct adb_client_s;
struct adb_service_s;
struct apacket_s;
/****************************************************************************
* Public types
****************************************************************************/
typedef struct amessage_s {
unsigned command; /* command identifier constant */
unsigned arg0; /* first argument */
unsigned arg1; /* second argument */
unsigned data_length; /* length of payload (0 is allowed) */
unsigned data_check; /* checksum of data payload */
unsigned magic; /* command ^ 0xffffffff */
} amessage;
/* Service init can force packet write_len to APACKET_SERVICE_INIT_ASYNC to
* notify adb client that the service init is asynchronous. No OKAY frame is
* sent and apacket is released.
*/
#define APACKET_SERVICE_INIT_ASYNC ((uint32_t)(~0))
typedef struct apacket_s
{
unsigned int write_len;
amessage msg;
uint8_t data[CONFIG_ADBD_PAYLOAD_SIZE];
} apacket;
typedef struct adb_service_ops_s {
int (*on_write_frame)(struct adb_service_s *service, apacket *p);
int (*on_ack_frame)(struct adb_service_s *service, apacket *p);
void (*on_kick)(struct adb_service_s *service);
void (*on_close)(struct adb_service_s *service);
} 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;
const adb_service_ops_t *ops;
/* the unique identifier for this service */
int id;
int peer_id;
} adb_service_t;
typedef struct adb_client_ops_s {
int (*write)(struct adb_client_s *client, apacket *p);
void (*kick)(struct adb_client_s *client);
void (*close)(struct adb_client_s *client);
} adb_client_ops_t;
typedef struct adb_client_s {
const adb_client_ops_t *ops;
int next_service_id;
adb_service_t *services;
uint8_t is_connected;
#ifdef CONFIG_ADBD_AUTHENTICATION
uint8_t token[CONFIG_ADBD_TOKEN_SIZE];
#endif
} adb_client_t;
typedef struct adb_context_s {
} adb_context_t;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* adb HAL */
adb_context_t* adb_hal_create_context(void);
void adb_hal_destroy_context(adb_context_t *context);
int adb_hal_run(adb_context_t *context);
int adb_fill_connect_data(char *buf, size_t bufsize);
int adb_hal_random(void *buf, size_t len);
/* Client */
adb_client_t* adb_create_client(size_t size);
void adb_destroy_client(adb_client_t *client);
void adb_client_kick_services(adb_client_t *client);
adb_client_t* adb_hal_create_client(size_t size);
void adb_hal_destroy_client(adb_client_t *client);
/* Frames */
void adb_send_okay_frame(adb_client_t *client, apacket *p,
unsigned local, unsigned remote);
void adb_send_okay_frame_with_data(adb_client_t *client, apacket *p,
unsigned local, unsigned remote);
void adb_send_open_frame(adb_client_t *client, apacket *p,
unsigned local, unsigned remote, int size);
void adb_send_data_frame(adb_client_t *client, apacket *p);
int adb_check_frame_data(apacket *p);
int adb_check_frame_header(apacket *p);
int adb_check_auth_frame_header(apacket *p);
void adb_process_packet(adb_client_t *client, apacket *p);
apacket* adb_hal_apacket_allocate(adb_client_t *c);
void adb_hal_apacket_release(adb_client_t *client, apacket *p);
/* Services */
void adb_register_service(adb_service_t *svc, adb_client_t *client);
void adb_service_close(adb_client_t *client, adb_service_t *svc, apacket *p);
#ifdef CONFIG_ADBD_AUTHENTICATION
extern const unsigned char *g_adb_public_keys[];
#endif
/* 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;
int adb_hal_socket_connect(struct adb_client_s *client, adb_tcp_socket_t *socket,
int port, adb_tcp_conn_t *conn,
void (*on_connect_cb)(adb_tcp_socket_t*, int));
void adb_hal_socket_close(adb_tcp_socket_t *socket,
void (*close_cb)(adb_tcp_socket_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));
int adb_hal_socket_start(adb_tcp_socket_t *socket,
void (*on_data_cb)(adb_tcp_socket_t*, struct apacket_s*));
int adb_hal_socket_stop(adb_tcp_socket_t *socket);
#endif
#endif /* __ADB_H */