/
usb_moded-android.c
229 lines (196 loc) · 5.79 KB
1
/**
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>
27
#include "usb_moded.h"
28
29
30
#include "usb_moded-android.h"
#include "usb_moded-log.h"
#include "usb_moded-modesetting.h"
31
#include "usb_moded-config-private.h"
32
#include "usb_moded-mac.h"
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
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/* ========================================================================= *
* Functions
* ========================================================================= */
/* -- android -- */
bool android_in_use (void);
static bool android_probe (void);
gchar *android_get_serial (void);
bool android_init_values (void);
bool android_set_charging_mode(void);
bool android_set_productid (const char *id);
bool android_set_vendorid (const char *id);
/* ========================================================================= *
* Data
* ========================================================================= */
static int android_probed = -1;
/* ========================================================================= *
* Functions
* ========================================================================= */
bool android_in_use(void)
{
if( android_probed < 0 )
log_debug("android_in_use() called before android_probe()");
return android_probed > 0;
}
static bool android_probe(void)
{
if( android_probed <= 0 ) {
android_probed = access(ANDROID0_ENABLE, F_OK) == 0;
log_warning("ANDROID0 %sdetected", android_probed ? "" : "not ");
}
return android_in_use();
}
79
80
81
/** Read android serial number from kernel command line
*/
82
83
gchar *
android_get_serial(void)
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
{
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;
}
129
130
/** initialize the basic android values
*/
131
bool android_init_values(void)
132
{
133
134
135
136
137
138
139
140
141
142
143
144
145
gchar *text;
if( !android_probe() )
goto EXIT;
/* Disable */
write_to_file(ANDROID0_ENABLE, "0");
/* Configure */
if( (text = android_get_serial()) )
{
write_to_file(ANDROID0_SERIAL, text);
g_free(text);
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
text = config_get_android_manufacturer();
if(text)
{
write_to_file(ANDROID0_MANUFACTURER, text);
g_free(text);
}
text = config_get_android_vendor_id();
if(text)
{
write_to_file(ANDROID0_ID_VENDOR, text);
g_free(text);
}
text = config_get_android_product();
if(text)
{
write_to_file(ANDROID0_PRODUCT, text);
g_free(text);
}
text = config_get_android_product_id();
if(text)
{
write_to_file(ANDROID0_ID_PRODUCT, text);
g_free(text);
}
text = mac_read_mac();
if(text)
{
write_to_file("/sys/class/android_usb/f_rndis/ethaddr", text);
g_free(text);
}
/* For rndis to be discovered correctly in M$ Windows (vista and later) */
write_to_file("/sys/class/android_usb/f_rndis/wceis", "1");
181
EXIT:
182
return android_in_use();
183
184
}
185
186
187
188
/* Set a charging mode for the android gadget
*
* @return 0 if successful, 1 on failure
*/
189
bool android_set_charging_mode(void)
190
{
191
192
193
194
195
196
197
198
199
200
bool ack = false;
if( android_in_use() ) {
/* disable, set functions to "mass_storage", re-enable */
write_to_file(ANDROID0_ENABLE, "0");
write_to_file(ANDROID0_ID_PRODUCT, "0AFE"); /* TODO: make configurable */
write_to_file(ANDROID0_FUNCTIONS, "mass_storage");
ack = write_to_file(ANDROID0_ENABLE, "1") != -1;
}
log_debug("ANDROID %s() -> %d", __func__, ack);
return ack;
201
}
202
203
204
205
206
/* Set a product id for the android gadget
*
* @return 0 if successful, 1 on failure
*/
207
bool android_set_productid(const char *id)
208
{
209
210
211
212
213
214
bool ack = false;
if( android_in_use() ) {
ack = write_to_file(ANDROID0_ID_PRODUCT, id) != -1;
}
log_debug("ANDROID %s(%s) -> %d", __func__, id, ack);
return ack;
215
}
216
217
218
219
220
/* Set a vendor id for the android gadget
*
* @return 0 if successful, 1 on failure
*/
221
bool android_set_vendorid(const char *id)
222
{
223
224
225
226
227
228
bool ack = false;
if( android_in_use() ) {
ack = write_to_file(ANDROID0_ID_VENDOR, id) != -1;
}
log_debug("ANDROID %s(%s) -> %d", __func__, id, ack);
return ack;
229
}