-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathtermux-usb.c
More file actions
259 lines (223 loc) · 7.94 KB
/
termux-usb.c
File metadata and controls
259 lines (223 loc) · 7.94 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <libusb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "termux-api.h"
#include "termux-usb.h"
#include "UsbAPI.pb-c.h"
#define BUF_SIZE 1024
#define TERMUX_PREFIX "/data/data/com.termux/files/usr"
static size_t read_buffer (unsigned int buf_size, uint8_t *out, FILE *fp)
{
size_t cur_len = 0;
size_t nread;
while ((nread = fread(out + cur_len, 1, buf_size - cur_len, fp)) != 0)
{
cur_len += nread;
if (cur_len >= buf_size)
{
fprintf(stderr, "max message length exceeded\n");
exit(EXIT_FAILURE);
}
}
return cur_len;
}
ssize_t termux_usb_get_device_list(struct termux_usb_device ***list)
{
Usbapi__TermuxUsb *container;
struct termux_usb_device **ret;
uint8_t buf[BUF_SIZE];
FILE *fp;
unsigned int num_devices;
fp = popen(TERMUX_PREFIX "/libexec/termux-api Usb -a getDevices", "r");
if (fp == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
size_t msg_len = read_buffer(BUF_SIZE, buf, fp);
if (pclose(fp)) {
fprintf(stderr, "Error closing protobuf stream\n");
return -1;
}
container = usbapi__termux_usb__unpack(NULL, msg_len, buf);
if (container == NULL)
{
fprintf(stderr, "Error unpacking incoming message\n");
return -1;
}
ret = (struct termux_usb_device **) calloc(container->n_device + 1, sizeof(struct termux_usb_device *));
if (!ret) {
fprintf(stderr, "Failed to allocate memory\n");
usbapi__termux_usb__free_unpacked(container, NULL);
return -1;
}
ret[container->n_device] = NULL;
for (size_t i = 0; i < container->n_device; i++) {
Usbapi__TermuxUsbDevice *msg = container->device[i];
Usbapi__TermuxUsbDeviceDescriptor *desc = msg->device;
struct termux_usb_device *dev = (struct termux_usb_device *) malloc(sizeof(struct termux_usb_device));
struct termux_usb_device_descriptor *dev_desc = (struct termux_usb_device_descriptor *) malloc(sizeof(struct termux_usb_device_descriptor));
dev->bus_number = msg->busnumber;
dev->port_number = msg->portnumber;
dev->device_address = (char *) malloc(strlen(msg->deviceaddress)+1);
strcpy(dev->device_address, msg->deviceaddress);
dev_desc->bDeviceClass = desc->deviceclass;
dev_desc->bDeviceSubClass = desc->devicesubclass;
dev_desc->bDeviceProtocol = desc->deviceprotocol;
dev_desc->idVendor = desc->vendorid;
dev_desc->idProduct = desc->productid;
dev_desc->manufacturer = (char *) malloc(strlen(desc->manufacturername)+1);
strcpy(dev_desc->manufacturer, desc->manufacturername);
dev_desc->product = (char *) malloc(strlen(desc->productname)+1);
strcpy(dev_desc->product, desc->productname);
dev_desc->serialNumber = (char *) malloc(strlen(desc->serialnumber)+1);
strcpy(dev_desc->serialNumber, desc->serialnumber);
dev_desc->bNumConfigurations = desc->configurationcount;
dev->device_descriptor = dev_desc;
ret[i] = dev;
}
*list = ret;
num_devices = container->n_device;
usbapi__termux_usb__free_unpacked(container, NULL);
return num_devices;
}
void termux_usb_free_device_list(struct termux_usb_device **list)
{
int i = 0;
struct termux_usb_device *dev;
while ((dev = list[i++]) != NULL) {
free(dev->device_descriptor->manufacturer);
free(dev->device_descriptor->product);
free(dev->device_descriptor->serialNumber);
free(dev->device_descriptor);
free(dev->device_address);
free(dev);
}
free(list);
}
intptr_t termux_usb_open_address(char *address)
{
int argc = 10;
char *argv[argc];
argv[0] = "termux-usb";
argv[1] = "Usb";
argv[2] = "-a";
argv[3] = "open";
argv[4] = "--ez";
argv[5] = "request";
argv[6] = "true";
argv[7] = "--es";
argv[8] = "device";
argv[9] = address;
int fd = run_api_command(argc, argv);
return (intptr_t) fd;
}
intptr_t termux_usb_open(struct termux_usb_device *dev)
{
return termux_usb_open_address(dev->device_address);
}
int termux_usb_get_config_descriptor(struct termux_usb_device *dev, int config_index,
struct termux_usb_config_descriptor **conf_desc)
{
Usbapi__TermuxUsbConfigDescriptor *confDesc;
uint8_t buf[BUF_SIZE];
FILE *fp;
char cmd[256];
char *base_cmd = TERMUX_PREFIX "/libexec/termux-api Usb -a getConfigDescriptor --es device %s --ei config %d";
snprintf(cmd, sizeof(cmd)-1, base_cmd, dev->device_address, config_index);
fp = popen(cmd, "r");
if (fp == NULL) {
perror("popen");
return 1;
}
size_t msg_len = read_buffer(BUF_SIZE, buf, fp);
if (pclose(fp)) {
fprintf(stderr, "Error closing protobuf stream\n");
return -1;
}
confDesc = usbapi__termux_usb_config_descriptor__unpack(NULL, msg_len, buf);
if (confDesc == NULL) {
fprintf(stderr, "Error unpacking incoming config descriptor message\n");
return -1;
}
struct termux_usb_config_descriptor *conf;
conf = (struct termux_usb_config_descriptor *) malloc(sizeof(struct termux_usb_config_descriptor));
if (conf == NULL) {
fprintf(stderr, "Memory allocation failed\n");
usbapi__termux_usb_config_descriptor__free_unpacked(confDesc, NULL);
return -1;
}
conf->bConfigurationValue = confDesc->configurationvalue;
conf->configuration = (char *) malloc(strlen(confDesc->configuration)+1);
if (conf->configuration == NULL) {
fprintf(stderr, "Memory allocation failed\n");
free(conf);
usbapi__termux_usb_config_descriptor__free_unpacked(confDesc, NULL);
return -1;
}
strcpy(conf->configuration, confDesc->configuration);
conf->MaxPower = confDesc->maxpower;
conf->bNumInterfaces = confDesc->n_interface;
conf->interface = (struct termux_usb_interface_descriptor *) calloc(confDesc->n_interface, sizeof(struct termux_usb_interface_descriptor));
if (conf->interface == NULL) {
fprintf(stderr, "Memory allocation failed\n");
free(conf->configuration);
free(conf);
usbapi__termux_usb_config_descriptor__free_unpacked(confDesc, NULL);
return -1;
}
for (size_t i = 0; i < confDesc->n_interface; i++) {
Usbapi__TermuxUsbInterfaceDescriptor *intfDesc = confDesc->interface[i];
conf->interface[i].bAlternateSetting = intfDesc->alternatesetting;
conf->interface[i].bNumEndpoints = intfDesc->n_endpoint;
conf->interface[i].bInterfaceClass = intfDesc->interfaceclass;
conf->interface[i].bInterfaceSubClass = intfDesc->interfacesubclass;
conf->interface[i].bInterfaceProtocol = intfDesc->interfaceprotocol;
conf->interface[i].interface = (char *) malloc(strlen(intfDesc->interface)+1);
if (conf->interface[i].interface == NULL) {
fprintf(stderr, "Memory allocation failed\n");
for (size_t j = 0; j < i; i++) {
free(conf->interface[i].endpoint);
free(conf->interface[i].interface);
}
free(conf->interface);
free(conf->configuration);
free(conf);
usbapi__termux_usb_config_descriptor__free_unpacked(confDesc, NULL);
return -1;
}
strcpy(conf->interface[i].interface, intfDesc->interface);
conf->interface[i].endpoint = (struct termux_usb_endpoint_descriptor *) calloc(intfDesc->n_endpoint, sizeof(struct termux_usb_endpoint_descriptor));
if (conf->interface[i].endpoint == NULL) {
fprintf(stderr, "Memory allocation failed\n");
for (size_t j = 0; j < i; i++) {
free(conf->interface[i].endpoint);
free(conf->interface[i].interface);
}
free(conf->interface);
free(conf->configuration);
free(conf);
usbapi__termux_usb_config_descriptor__free_unpacked(confDesc, NULL);
}
for (size_t j = 0; j < intfDesc->n_endpoint; j++) {
Usbapi__TermuxUsbEndpointDescriptor *endpointDesc = intfDesc->endpoint[j];
conf->interface[i].endpoint[j].bEndpointAddress = endpointDesc->endpointaddress;
conf->interface[i].endpoint[j].bmAttributes = endpointDesc->attributes;
conf->interface[i].endpoint[j].wMaxPacketSize = endpointDesc->maxpacketsize;
conf->interface[i].endpoint[j].bInterval = endpointDesc->interval;
}
}
*conf_desc = conf;
usbapi__termux_usb_config_descriptor__free_unpacked(confDesc, NULL);
return 0;
}
void termux_usb_free_config_descriptor(struct termux_usb_config_descriptor *conf_desc)
{
for (int i = 0; i < conf_desc->bNumInterfaces; i++) {
free(conf_desc->interface[i].endpoint);
free(conf_desc->interface[i].interface);
}
free(conf_desc->interface);
free(conf_desc->configuration);
free(conf_desc);
}