43static bool android_write_file (
const char *path,
const char *text);
44bool android_in_use (
void);
45static bool android_probe (
void);
49bool android_set_enabled (
bool enable);
50bool android_set_charging_mode(
void);
51bool android_set_function (
const char *function);
52bool android_set_productid (
const char *
id);
53bool android_set_vendorid (
const char *
id);
54bool android_set_attr (
const char *function,
const char *attr,
const char *value);
60static int android_probed = -1;
67android_write_file(
const char *path,
const char *text)
76 log_debug(
"WRITE %s '%s'", path, text);
79 snprintf(buff,
sizeof buff,
"%s\n", text);
81 if( write_to_file(path, buff) == -1 )
96 if( android_probed < 0 )
97 log_debug(
"android_in_use() called before android_probe()");
99 return android_probed > 0;
105 LOG_REGISTER_CONTEXT;
107 if( android_probed <= 0 ) {
108 android_probed = access(ANDROID0_ENABLE, F_OK) == 0;
109 log_warning(
"ANDROID0 %sdetected", android_probed ?
"" :
"not ");
112 return android_in_use();
120 LOG_REGISTER_CONTEXT;
122 static const char path[] =
"/proc/cmdline";
123 static const char find[] =
"androidboot.serialno=";
124 static const char pbrk[] =
" \t\r\n,";
131 if( !(file = fopen(path,
"r")) ) {
132 log_warning(
"%s: %s: %m", path,
"can't open");
136 if( getline(&data, &size, file) < 0 ) {
137 log_warning(
"%s: %s: %m", path,
"can't read");
141 char *beg = strstr(data, find);
143 log_warning(
"%s: no serial found", path);
147 beg +=
sizeof find - 1;
148 size_t len = strcspn(beg, pbrk);
150 log_warning(
"%s: empty serial found", path);
154 res = g_strndup(beg, len);
173 LOG_REGISTER_CONTEXT;
177 if( !android_probe() )
181 android_set_enabled(
false);
186 android_write_file(ANDROID0_SERIAL, text);
190 text = config_get_android_manufacturer();
193 android_write_file(ANDROID0_MANUFACTURER, text);
196 text = config_get_android_vendor_id();
199 android_set_vendorid(text);
202 text = config_get_android_product();
205 android_write_file(ANDROID0_PRODUCT, text);
208 text = config_get_android_product_id();
211 android_set_productid(text);
214 text = mac_read_mac();
229 return android_in_use();
241android_set_enabled(
bool enable)
243 LOG_REGISTER_CONTEXT;
246 if( android_in_use() ) {
247 const char *val = enable ?
"1" :
"0";
248 ack = android_write_file(ANDROID0_ENABLE, val);
250 log_debug(
"ANDROID %s(%d) -> %d", __func__, enable, ack);
259android_set_charging_mode(
void)
261 LOG_REGISTER_CONTEXT;
265 if( !android_in_use() )
268 if( !android_set_function(
"mass_storage") )
272 if( !android_set_productid(
"0AFE") )
275 if( !android_set_enabled(
true) )
281 log_debug(
"ANDROID %s() -> %d", __func__, ack);
290android_set_function(
const char *function)
292 LOG_REGISTER_CONTEXT;
299 if( !android_in_use() )
302 if( !android_set_enabled(
false) )
305 if( !android_write_file(ANDROID0_FUNCTIONS, function) )
314 log_debug(
"ANDROID %s(%s) -> %d", __func__, function, ack);
323android_set_productid(
const char *
id)
325 LOG_REGISTER_CONTEXT;
329 if(
id && android_in_use() ) {
332 unsigned num = strtol(
id, &end, 16);
333 if( end >
id && *end == 0 ) {
334 snprintf(str,
sizeof str,
"%04x", num);
337 ack = android_write_file(ANDROID0_ID_PRODUCT,
id);
339 log_debug(
"ANDROID %s(%s) -> %d", __func__,
id, ack);
348android_set_vendorid(
const char *
id)
350 LOG_REGISTER_CONTEXT;
353 if(
id && android_in_use() ) {
356 unsigned num = strtol(
id, &end, 16);
357 if( end >
id && *end == 0 ) {
358 snprintf(str,
sizeof str,
"%04x", num);
361 ack = android_write_file(ANDROID0_ID_VENDOR,
id);
363 log_debug(
"ANDROID %s(%s) -> %d", __func__,
id, ack);
374 LOG_REGISTER_CONTEXT;
378 if( function && attr && value && android_in_use() ) {
380 snprintf(path,
sizeof path,
"%s/%s/%s",
381 ANDROID0_DIRECTORY, function, attr);
382 ack = android_write_file(path, value);
384 log_debug(
"ANDROID %s(%s, %s, %s) -> %d", __func__,
385 function, attr, value, ack);
gchar * android_get_serial(void)
bool android_set_attr(const char *function, const char *attr, const char *value)