-
Notifications
You must be signed in to change notification settings - Fork 1
/
tesla-android-usb-networking-initialiser.c
191 lines (162 loc) · 5.49 KB
/
tesla-android-usb-networking-initialiser.c
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "toys.h"
char* appleVendorId = "05ac";
char* appleInitialisationCommand = "usbmuxd -v -f -X";
char* alcatelIK41XXId = "1bbb:00b6";
char* alcatelIK41XXInitialisationCommand = "echo -e \"AT+USBMODE=1\r\n\" > /dev/ttyUSB2";
char* alcatelIK40VId = "1bbb:f000";
char* alcatelIK40VInitialisationCommand = "/vendor/bin/usb_modeswitch -c /vendor/tesla-android/usb_modeswitch/1bbb-f000.conf";
char* huaweiVendorId = "12d1";
char* huaweiInitialisationCommand = "/vendor/bin/usb_modeswitch -c /vendor/tesla-android/usb_modeswitch/12d1.conf";
char* vodafoneK5161hId = "12d1:1f1d";
char* vodafoneK5161hInitialisationCommand = "/vendor/bin/usb_modeswitch -c /vendor/tesla-android/usb_modeswitch/12d1-1f1d.conf";
// https://www.draisberghof.de/usb_modeswitch/bb/viewtopic.php?f=3&t=3043&p=20026#p20054
char* huawei_E3372_325Id = "3566:2001";
char* huawei_E3372_325InitialisationCommand1 = "/vendor/bin/usb_modeswitch -v 3566 -p 2001 -W -R -w 400";
char* huawei_E3372_325InitialisationCommand2 = "/vendor/bin/usb_modeswitch -v 3566 -p 2001 -W -R";
int connectedDevicesSize = 0;
char **connectedDeviceIds;
GLOBALS(
char *i;
long n;
void *ids, *class;
int count;
)
struct dev_ids {
struct dev_ids *next, *child;
int id;
char name[];
};
struct scanloop {
char *pattern;
void *d1, *d2;
};
void initialiseDevice(char * deviceId) {
printf("New USB device detected: %s\n", deviceId);
if(strstr(deviceId, appleVendorId)) {
printf("Initialising Apple device with usbmuxd");
system(appleInitialisationCommand);
} else if(strstr(deviceId, alcatelIK41XXId)) {
printf("Converting Alcatel IK41 from mbim to rndis");
system(alcatelIK41XXInitialisationCommand);
} else if(strstr(deviceId, alcatelIK40VId)) {
printf("Initialising Alcatel IK40V");
system(alcatelIK40VInitialisationCommand);
} else if(strstr(deviceId, vodafoneK5161hId)) {
printf("Initialising Vodafone device");
system(vodafoneK5161hInitialisationCommand);
} else if(strstr(deviceId, huaweiVendorId)) {
printf("Initialising Huawei device");
system(huaweiInitialisationCommand);
} else if(strstr(deviceId, huawei_E3372_325Id)) {
printf("Initialising Huawei device");
system(huawei_E3372_325InitialisationCommand1);
system(huawei_E3372_325InitialisationCommand2);
}
}
void resetConnectedDeviceIds() {
if(connectedDevicesSize != 0) {
for (int i = 0; i < connectedDevicesSize; i++) {
free(connectedDeviceIds[i]);
}
}
free(connectedDeviceIds);
connectedDevicesSize = 0;
}
void initialiseConnectedDeviceList() {
//FIXME size should be dynamic
connectedDeviceIds = malloc(100 * sizeof(char*));
}
int scan_uevent(struct dirtree *new, int len, struct scanloop *sl) {
int ii, count = 0;
off_t flen = sizeof(toybuf);
char *ss, *yy;
if (*new->name == '.') return 0;
sprintf(toybuf, "%s/uevent", new->name);
if (!readfileat(dirtree_parentfd(new), ss = toybuf, toybuf, &flen)) return 0;
while ((flen = strcspn(ss, "\n"))) {
if (ss[flen]) ss[flen++] = 0;
yy = ss+flen;
for (ii = 0; ii<len; ii++) {
if (strchr(sl[ii].pattern, '%')) {
if (2-!sl[ii].d2==sscanf(ss, sl[ii].pattern, sl[ii].d1, sl[ii].d2))
break;
} else if (strstart(&ss, sl[ii].pattern)) {
*(void **)sl[ii].d1 = ss;
break;
}
}
if (ii!=len) count++;
ss = yy;
}
return count;
}
int processDeviceCallback(struct dirtree *new) {
int busnum = 0, devnum = 0, pid = 0, vid = 0;
if (!new->parent) return DIRTREE_RECURSE;
if (3 == scan_uevent(new, 3, (struct scanloop[]){{"BUSNUM=%u", &busnum, 0},
{"DEVNUM=%u", &devnum, 0}, {"PRODUCT=%x/%x", &pid, &vid}})) {
char *deviceId = malloc(10 * sizeof(char));
snprintf(deviceId, 10, "%04x:%04x", pid, vid);
connectedDeviceIds[connectedDevicesSize] = deviceId;
connectedDevicesSize++;
}
return 0;
}
int processAndInitialiseDeviceCallback(struct dirtree *new) {
int busnum = 0, devnum = 0, pid = 0, vid = 0;
if (!new->parent) return DIRTREE_RECURSE;
if (3 == scan_uevent(new, 3, (struct scanloop[]){{"BUSNUM=%u", &busnum, 0},
{"DEVNUM=%u", &devnum, 0}, {"PRODUCT=%x/%x", &pid, &vid}})) {
char *deviceId = malloc(10 * sizeof(char));
snprintf(deviceId, 10, "%04x:%04x", pid, vid);
connectedDeviceIds[connectedDevicesSize] = deviceId;
connectedDevicesSize++;
initialiseDevice(deviceId);
}
return 0;
}
int initialiseNewlyConnectedDeviceCallback(struct dirtree *new) {
int busnum = 0, devnum = 0, pid = 0, vid = 0;
if (!new->parent) return DIRTREE_RECURSE;
if (3 == scan_uevent(new, 3, (struct scanloop[]){{"BUSNUM=%u", &busnum, 0},
{"DEVNUM=%u", &devnum, 0}, {"PRODUCT=%x/%x", &pid, &vid}})) {
char *deviceId = malloc(10 * sizeof(char));
snprintf(deviceId, 10, "%04x:%04x", pid, vid);
bool isDeviceAlreadyConnected = false;
for (int j = 0; j < connectedDevicesSize; j++) {
if(strcmp(connectedDeviceIds[j], deviceId) == 0) {
isDeviceAlreadyConnected = true;
break;
}
}
if(!isDeviceAlreadyConnected) {
initialiseDevice(deviceId);
}
free(deviceId);
}
return 0;
}
int main(void) {
while(1) {
if(connectedDevicesSize == 0) {
initialiseConnectedDeviceList();
dirtree_read("/sys/bus/usb/devices/", processAndInitialiseDeviceCallback);
} else {
dirtree_read("/sys/bus/usb/devices/", initialiseNewlyConnectedDeviceCallback);
resetConnectedDeviceIds();
initialiseConnectedDeviceList();
dirtree_read("/sys/bus/usb/devices/", processDeviceCallback);
}
if(connectedDevicesSize == 0) {
resetConnectedDeviceIds();
}
sleep(1);
}
resetConnectedDeviceIds();
return 0;
}