Skip to content

Latest commit

 

History

History
354 lines (294 loc) · 8.31 KB

usb_moded-android.c

File metadata and controls

354 lines (294 loc) · 8.31 KB
 
Aug 24, 2018
Aug 24, 2018
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
* @file usb_moded-android.c
*
* Copyright (C) 2013-2018 Jolla. All rights reserved.
*
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
23
24
25
26
#include <stdio.h>
#include <glib.h>
Jun 5, 2018
Jun 5, 2018
27
#include "usb_moded.h"
28
29
30
#include "usb_moded-android.h"
#include "usb_moded-log.h"
#include "usb_moded-modesetting.h"
Aug 24, 2018
Aug 24, 2018
31
#include "usb_moded-config-private.h"
Aug 14, 2013
Aug 14, 2013
32
#include "usb_moded-mac.h"
Aug 24, 2018
Aug 24, 2018
34
35
36
37
38
39
40
41
42
43
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/* ========================================================================= *
* Functions
* ========================================================================= */
/* -- android -- */
Aug 24, 2018
Aug 24, 2018
44
bool android_in_use (void);
Aug 24, 2018
Aug 24, 2018
45
46
47
static bool android_probe (void);
gchar *android_get_serial (void);
bool android_init_values (void);
Aug 24, 2018
Aug 24, 2018
48
bool android_set_enabled (bool enable);
Aug 24, 2018
Aug 24, 2018
49
bool android_set_charging_mode(void);
Aug 24, 2018
Aug 24, 2018
50
bool android_set_function (const char *function);
Aug 24, 2018
Aug 24, 2018
51
52
bool android_set_productid (const char *id);
bool android_set_vendorid (const char *id);
Aug 24, 2018
Aug 24, 2018
53
bool android_set_attr (const char *function, const char *attr, const char *value);
Aug 24, 2018
Aug 24, 2018
54
55
56
57
58
59
60
61
62
63
64
/* ========================================================================= *
* Data
* ========================================================================= */
static int android_probed = -1;
/* ========================================================================= *
* Functions
* ========================================================================= */
Aug 24, 2018
Aug 24, 2018
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
static bool
android_write_file(const char *path, const char *text)
{
bool ack = false;
if( !path || !text )
goto EXIT;
log_debug("WRITE %s '%s'", path, text);
char buff[64];
snprintf(buff, sizeof buff, "%s\n", text);
if( write_to_file(path, buff) == -1 )
goto EXIT;
ack = true;
EXIT:
return ack;
}
Aug 24, 2018
Aug 24, 2018
88
89
bool
android_in_use(void)
Aug 24, 2018
Aug 24, 2018
90
91
92
93
94
95
96
{
if( android_probed < 0 )
log_debug("android_in_use() called before android_probe()");
return android_probed > 0;
}
Aug 24, 2018
Aug 24, 2018
97
98
static bool
android_probe(void)
Aug 24, 2018
Aug 24, 2018
99
100
101
102
103
104
105
106
{
if( android_probed <= 0 ) {
android_probed = access(ANDROID0_ENABLE, F_OK) == 0;
log_warning("ANDROID0 %sdetected", android_probed ? "" : "not ");
}
return android_in_use();
}
Apr 26, 2017
Apr 26, 2017
108
109
/** Read android serial number from kernel command line
*/
Aug 24, 2018
Aug 24, 2018
110
111
gchar *
android_get_serial(void)
Apr 26, 2017
Apr 26, 2017
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
{
static const char path[] = "/proc/cmdline";
static const char find[] = "androidboot.serialno=";
static const char pbrk[] = " \t\r\n,";
char *res = 0;
FILE *file = 0;
size_t size = 0;
char *data = 0;
if( !(file = fopen(path, "r")) ) {
log_warning("%s: %s: %m", path, "can't open");
goto EXIT;
}
if( getline(&data, &size, file) < 0 ) {
log_warning("%s: %s: %m", path, "can't read");
goto EXIT;
}
char *beg = strstr(data, find);
if( !beg ) {
log_warning("%s: no serial found", path);
goto EXIT;
}
beg += sizeof find - 1;
size_t len = strcspn(beg, pbrk);
if( len < 1 ) {
log_warning("%s: empty serial found", path);
goto EXIT;
}
res = g_strndup(beg, len);
EXIT:
free(data);
if( file )
fclose(file);
return res;
}
157
158
/** initialize the basic android values
*/
Aug 24, 2018
Aug 24, 2018
159
160
bool
android_init_values(void)
Aug 24, 2018
Aug 24, 2018
162
163
164
165
166
167
gchar *text;
if( !android_probe() )
goto EXIT;
/* Disable */
Aug 24, 2018
Aug 24, 2018
168
android_set_enabled(false);
Aug 24, 2018
Aug 24, 2018
169
170
171
172
/* Configure */
if( (text = android_get_serial()) )
{
Aug 24, 2018
Aug 24, 2018
173
android_write_file(ANDROID0_SERIAL, text);
Aug 24, 2018
Aug 24, 2018
174
g_free(text);
Jun 5, 2018
Jun 5, 2018
175
176
}
Aug 24, 2018
Aug 24, 2018
177
178
179
text = config_get_android_manufacturer();
if(text)
{
Aug 24, 2018
Aug 24, 2018
180
android_write_file(ANDROID0_MANUFACTURER, text);
Aug 24, 2018
Aug 24, 2018
181
182
183
184
185
g_free(text);
}
text = config_get_android_vendor_id();
if(text)
{
Aug 24, 2018
Aug 24, 2018
186
android_set_vendorid(text);
Aug 24, 2018
Aug 24, 2018
187
188
189
190
191
g_free(text);
}
text = config_get_android_product();
if(text)
{
Aug 24, 2018
Aug 24, 2018
192
android_write_file(ANDROID0_PRODUCT, text);
Aug 24, 2018
Aug 24, 2018
193
194
195
196
197
g_free(text);
}
text = config_get_android_product_id();
if(text)
{
Aug 24, 2018
Aug 24, 2018
198
android_set_productid(text);
Aug 24, 2018
Aug 24, 2018
199
200
201
202
203
g_free(text);
}
text = mac_read_mac();
if(text)
{
Aug 24, 2018
Aug 24, 2018
204
android_set_attr("f_rndis", "ethaddr", text);
Aug 24, 2018
Aug 24, 2018
205
206
207
g_free(text);
}
/* For rndis to be discovered correctly in M$ Windows (vista and later) */
Aug 24, 2018
Aug 24, 2018
208
209
210
211
212
213
android_set_attr("f_rndis", "wceis", "1");
/* Make sure remnants off mass-storage mode do not cause
* issues for charging_fallback & co */
android_set_attr("f_mass_storage", "lun/nofua", "0");
android_set_attr("f_mass_storage", "lun/file", "");
Aug 24, 2018
Aug 24, 2018
214
Mar 8, 2018
Mar 8, 2018
215
EXIT:
Aug 24, 2018
Aug 24, 2018
216
return android_in_use();
Aug 14, 2013
Aug 14, 2013
217
218
}
Aug 24, 2018
Aug 24, 2018
219
220
221
222
223
224
bool
android_set_enabled(bool enable)
{
bool ack = false;
if( android_in_use() ) {
const char *val = enable ? "1" : "0";
Aug 24, 2018
Aug 24, 2018
225
ack = android_write_file(ANDROID0_ENABLE, val) != -1;
Aug 24, 2018
Aug 24, 2018
226
227
228
229
230
}
log_debug("ANDROID %s(%d) -> %d", __func__, enable, ack);
return ack;
}
Aug 14, 2013
Aug 14, 2013
231
232
/* Set a charging mode for the android gadget
*
Aug 24, 2018
Aug 24, 2018
233
* @return true if successful, false on failure
Aug 14, 2013
Aug 14, 2013
234
*/
Aug 24, 2018
Aug 24, 2018
235
236
bool
android_set_charging_mode(void)
Aug 14, 2013
Aug 14, 2013
237
{
Aug 24, 2018
Aug 24, 2018
238
bool ack = false;
Aug 24, 2018
Aug 24, 2018
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
if( !android_in_use() )
goto EXIT;
if( !android_set_function("mass_storage") )
goto EXIT;
/* TODO: make configurable */
if( !android_set_productid("0AFE") )
goto EXIT;
if( !android_set_enabled(true) )
goto EXIT;
ack = true;
EXIT:
Aug 24, 2018
Aug 24, 2018
256
257
log_debug("ANDROID %s() -> %d", __func__, ack);
return ack;
Aug 23, 2013
Aug 23, 2013
259
Aug 24, 2018
Aug 24, 2018
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Set a function for the android gadget
*
* @return true if successful, false on failure
*/
bool
android_set_function(const char *function)
{
bool ack = false;
if( !function )
goto EXIT;
if( !android_in_use() )
goto EXIT;
if( !android_set_enabled(false) )
goto EXIT;
Aug 24, 2018
Aug 24, 2018
278
if( android_write_file(ANDROID0_FUNCTIONS, function) == -1 )
Aug 24, 2018
Aug 24, 2018
279
280
281
282
283
284
285
286
287
288
289
290
goto EXIT;
/* Leave disabled, so that caller can adjust attributes
* etc before enabling */
ack = true;
EXIT:
log_debug("ANDROID %s(%s) -> %d", __func__, function, ack);
return ack;
}
Aug 23, 2013
Aug 23, 2013
291
292
/* Set a product id for the android gadget
*
Aug 24, 2018
Aug 24, 2018
293
* @return true if successful, false on failure
Aug 23, 2013
Aug 23, 2013
294
*/
Aug 24, 2018
Aug 24, 2018
295
296
bool
android_set_productid(const char *id)
Aug 23, 2013
Aug 23, 2013
297
{
Aug 24, 2018
Aug 24, 2018
298
bool ack = false;
Aug 24, 2018
Aug 24, 2018
299
Aug 24, 2018
Aug 24, 2018
300
if( id && android_in_use() ) {
Aug 24, 2018
Aug 24, 2018
301
302
303
304
305
306
307
char str[16];
char *end = 0;
unsigned num = strtol(id, &end, 16);
if( end > id && *end == 0 ) {
snprintf(str, sizeof str, "%04x", num);
id = str;
}
Aug 24, 2018
Aug 24, 2018
308
ack = android_write_file(ANDROID0_ID_PRODUCT, id) != -1;
Aug 24, 2018
Aug 24, 2018
309
310
311
}
log_debug("ANDROID %s(%s) -> %d", __func__, id, ack);
return ack;
Aug 23, 2013
Aug 23, 2013
312
}
Nov 5, 2015
Nov 5, 2015
313
314
315
/* Set a vendor id for the android gadget
*
Aug 24, 2018
Aug 24, 2018
316
* @return true if successful, false on failure
Nov 5, 2015
Nov 5, 2015
317
*/
Aug 24, 2018
Aug 24, 2018
318
319
bool
android_set_vendorid(const char *id)
Nov 5, 2015
Nov 5, 2015
320
{
Aug 24, 2018
Aug 24, 2018
321
bool ack = false;
Aug 24, 2018
Aug 24, 2018
322
if( id && android_in_use() ) {
Aug 24, 2018
Aug 24, 2018
323
324
325
326
327
328
329
char str[16];
char *end = 0;
unsigned num = strtol(id, &end, 16);
if( end > id && *end == 0 ) {
snprintf(str, sizeof str, "%04x", num);
id = str;
}
Aug 24, 2018
Aug 24, 2018
330
ack = android_write_file(ANDROID0_ID_VENDOR, id) != -1;
Aug 24, 2018
Aug 24, 2018
331
332
333
}
log_debug("ANDROID %s(%s) -> %d", __func__, id, ack);
return ack;
Nov 5, 2015
Nov 5, 2015
334
}
Aug 24, 2018
Aug 24, 2018
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/** Set function attribute
*
* @return true if successful, false on failure
*/
bool
android_set_attr(const char *function, const char *attr, const char *value)
{
bool ack = false;
if( function && attr && value && android_in_use() ) {
char path[256];
snprintf(path, sizeof path, "%s/%s/%s",
ANDROID0_DIRECTORY, function, attr);
ack = android_write_file(path, value);
}
log_debug("ANDROID %s(%s, %s, %s) -> %d", __func__,
function, attr, value, ack);
return ack;
}