Skip to content

Latest commit

 

History

History
892 lines (763 loc) · 21.4 KB

usb_moded-config.c

File metadata and controls

892 lines (763 loc) · 21.4 KB
 
Mar 22, 2011
Mar 22, 2011
1
2
3
4
/**
@file usb_moded-config.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
May 26, 2015
May 26, 2015
5
Copyright (C) 2012-2015 Jolla. All rights reserved.
Mar 22, 2011
Mar 22, 2011
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@author: Philippe De Swert <philippe.de-swert@nokia.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
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
May 17, 2013
May 17, 2013
28
#include <fcntl.h>
Mar 22, 2011
Mar 22, 2011
29
Nov 21, 2012
Nov 21, 2012
30
31
32
#include <sys/stat.h>
#include <sys/types.h>
Mar 22, 2011
Mar 22, 2011
33
#include <glib.h>
Apr 6, 2016
Apr 6, 2016
34
#include <glob.h>
Aug 15, 2012
Aug 15, 2012
35
/*
Mar 22, 2011
Mar 22, 2011
36
#include <glib/gkeyfile.h>
Sep 11, 2011
Sep 11, 2011
37
#include <glib/gstdio.h>
Aug 15, 2012
Aug 15, 2012
38
*/
Sep 9, 2013
Sep 9, 2013
39
#include "usb_moded.h"
Mar 22, 2011
Mar 22, 2011
40
#include "usb_moded-config.h"
Sep 11, 2011
Sep 11, 2011
41
#include "usb_moded-config-private.h"
Mar 22, 2011
Mar 22, 2011
42
#include "usb_moded-log.h"
Nov 13, 2012
Nov 13, 2012
43
#include "usb_moded-modes.h"
Aug 11, 2017
Aug 11, 2017
44
#include "usb_moded-modesetting.h"
Mar 22, 2011
Mar 22, 2011
45
May 11, 2016
May 11, 2016
46
47
48
49
#ifdef USE_MER_SSU
# include "usb_moded-ssu.h"
#endif
Apr 20, 2011
Apr 20, 2011
50
static int get_conf_int(const gchar *entry, const gchar *key);
Nov 7, 2014
Nov 7, 2014
51
52
static char * get_conf_string(const gchar *entry, const gchar *key);
static char * get_kcmdline_string(const char *entry);
Apr 20, 2011
Apr 20, 2011
53
Apr 20, 2014
Apr 20, 2014
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
static int validate_ip(const char *ipadd)
{
unsigned int b1, b2, b3, b4;
unsigned char c;
if (sscanf(ipadd, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c) != 4)
return(-1);
if ((b1 | b2 | b3 | b4) > 255)
return(-1);
if (strspn(ipadd, "0123456789.") < strlen(ipadd))
return(-1);
/* all ok */
return 0;
}
Jul 7, 2016
Jul 7, 2016
70
char *find_mounts(void)
Mar 22, 2011
Mar 22, 2011
71
{
Apr 20, 2011
Apr 20, 2011
72
Jul 7, 2016
Jul 7, 2016
73
char *ret = NULL;
Mar 22, 2011
Mar 22, 2011
74
Apr 20, 2011
Apr 20, 2011
75
76
ret = get_conf_string(FS_MOUNT_ENTRY, FS_MOUNT_KEY);
if(ret == NULL)
Mar 22, 2011
Mar 22, 2011
77
78
{
ret = g_strdup(FS_MOUNT_DEFAULT);
Apr 20, 2011
Apr 20, 2011
79
log_debug("Default mount = %s\n", ret);
Mar 22, 2011
Mar 22, 2011
80
81
82
83
84
85
86
}
return(ret);
}
int find_sync(void)
{
Apr 20, 2011
Apr 20, 2011
87
return(get_conf_int(FS_SYNC_ENTRY, FS_SYNC_KEY));
Mar 22, 2011
Mar 22, 2011
88
89
}
Jul 7, 2016
Jul 7, 2016
90
char * find_alt_mount(void)
Mar 22, 2011
Mar 22, 2011
91
{
Apr 20, 2011
Apr 20, 2011
92
return(get_conf_string(ALT_MOUNT_ENTRY, ALT_MOUNT_KEY));
Apr 6, 2011
Apr 6, 2011
93
94
}
Nov 7, 2014
Nov 7, 2014
95
char * find_udev_path(void)
Apr 7, 2011
Apr 7, 2011
96
{
Apr 20, 2011
Apr 20, 2011
97
return(get_conf_string(UDEV_PATH_ENTRY, UDEV_PATH_KEY));
Apr 7, 2011
Apr 7, 2011
98
}
Jan 13, 2012
Jan 13, 2012
99
Nov 7, 2014
Nov 7, 2014
100
char * find_udev_subsystem(void)
Jan 13, 2012
Jan 13, 2012
101
102
103
{
return(get_conf_string(UDEV_PATH_ENTRY, UDEV_SUBSYSTEM_KEY));
}
Apr 7, 2011
Apr 7, 2011
104
Nov 11, 2014
Nov 11, 2014
105
char * check_trigger(void)
May 10, 2011
May 10, 2011
106
107
108
109
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_PATH_KEY));
}
Nov 11, 2014
Nov 11, 2014
110
char * get_trigger_subsystem(void)
May 16, 2011
May 16, 2011
111
112
113
114
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_UDEV_SUBSYSTEM));
}
Nov 11, 2014
Nov 11, 2014
115
char * get_trigger_mode(void)
May 10, 2011
May 10, 2011
116
117
118
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_MODE_KEY));
}
May 16, 2011
May 16, 2011
119
Nov 11, 2014
Nov 11, 2014
120
char * get_trigger_property(void)
May 16, 2011
May 16, 2011
121
122
123
124
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_KEY));
}
Nov 11, 2014
Nov 11, 2014
125
char * get_trigger_value(void)
May 16, 2011
May 16, 2011
126
127
128
{
return(get_conf_string(TRIGGER_ENTRY, TRIGGER_PROPERTY_VALUE_KEY));
}
May 10, 2011
May 10, 2011
129
Aug 3, 2015
Aug 3, 2015
130
static char * get_network_ip(void)
Aug 7, 2012
Aug 7, 2012
131
{
Nov 7, 2014
Nov 7, 2014
132
char * ip = get_kcmdline_string(NETWORK_IP_KEY);
May 17, 2013
May 17, 2013
133
if (ip != NULL)
Apr 20, 2014
Apr 20, 2014
134
135
if(!validate_ip(ip))
return(ip);
May 17, 2013
May 17, 2013
136
Aug 7, 2012
Aug 7, 2012
137
138
139
return(get_conf_string(NETWORK_ENTRY, NETWORK_IP_KEY));
}
Aug 3, 2015
Aug 3, 2015
140
static char * get_network_interface(void)
Aug 7, 2012
Aug 7, 2012
141
142
143
144
{
return(get_conf_string(NETWORK_ENTRY, NETWORK_INTERFACE_KEY));
}
Aug 3, 2015
Aug 3, 2015
145
static char * get_network_gateway(void)
Aug 12, 2012
Aug 12, 2012
146
{
Nov 7, 2014
Nov 7, 2014
147
char * gw = get_kcmdline_string(NETWORK_GATEWAY_KEY);
May 17, 2013
May 17, 2013
148
149
150
if (gw != NULL)
return(gw);
Aug 12, 2012
Aug 12, 2012
151
152
153
return(get_conf_string(NETWORK_ENTRY, NETWORK_GATEWAY_KEY));
}
Aug 3, 2015
Aug 3, 2015
154
static char * get_network_netmask(void)
Aug 3, 2015
Aug 3, 2015
155
156
157
158
159
160
161
162
{
char * netmask = get_kcmdline_string(NETWORK_NETMASK_KEY);
if (netmask != NULL)
return(netmask);
return(get_conf_string(NETWORK_ENTRY, NETWORK_NETMASK_KEY));
}
Aug 3, 2015
Aug 3, 2015
163
static char * get_network_nat_interface(void)
Nov 27, 2013
Nov 27, 2013
164
165
166
167
{
return(get_conf_string(NETWORK_ENTRY, NETWORK_NAT_INTERFACE_KEY));
}
Nov 13, 2012
Nov 13, 2012
168
169
170
171
/* create basic conffile with sensible defaults */
static void create_conf_file(void)
{
GKeyFile *settingsfile;
Nov 19, 2012
Nov 19, 2012
172
gchar *keyfile;
Jul 5, 2013
Jul 5, 2013
173
int dir = 1;
Aug 2, 2013
Aug 2, 2013
174
struct stat dir_stat;
Nov 13, 2012
Nov 13, 2012
175
Aug 2, 2013
Aug 2, 2013
176
177
178
/* since this function can also be called when the dir exists we only create
it if it is missing */
if(stat(CONFIG_FILE_DIR, &dir_stat))
Jul 5, 2013
Jul 5, 2013
179
{
Aug 2, 2013
Aug 2, 2013
180
181
182
183
184
185
186
dir = mkdir(CONFIG_FILE_DIR, 0755);
if(dir < 0)
{
log_warning("Could not create confdir, continuing without configuration!\n");
/* no point in trying to generate the config file if the dir cannot be created */
return;
}
Jul 5, 2013
Jul 5, 2013
187
}
Aug 2, 2013
Aug 2, 2013
188
Nov 13, 2012
Nov 13, 2012
189
190
191
192
193
settingsfile = g_key_file_new();
g_key_file_set_string(settingsfile, MODE_SETTING_ENTRY, MODE_SETTING_KEY, MODE_DEVELOPER );
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
if(g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL) == 0)
Mar 31, 2013
Mar 31, 2013
194
log_debug("Conffile creation failed. Continuing without configuration!\n");
Oct 17, 2013
Oct 17, 2013
195
free(keyfile);
Apr 3, 2013
Apr 3, 2013
196
g_key_file_free(settingsfile);
Nov 13, 2012
Nov 13, 2012
197
198
}
Apr 20, 2011
Apr 20, 2011
199
static int get_conf_int(const gchar *entry, const gchar *key)
Apr 6, 2011
Apr 6, 2011
200
201
202
{
GKeyFile *settingsfile;
gboolean test = FALSE;
Nov 30, 2013
Nov 30, 2013
203
gchar **keys, **origkeys;
Apr 20, 2011
Apr 20, 2011
204
int ret = 0;
Mar 22, 2011
Mar 22, 2011
205
Apr 6, 2011
Apr 6, 2011
206
207
208
209
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
{
Nov 13, 2012
Nov 13, 2012
210
211
log_debug("no conffile, Creating\n");
create_conf_file();
Apr 6, 2011
Apr 6, 2011
212
}
Apr 20, 2011
Apr 20, 2011
213
keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL);
Apr 7, 2011
Apr 7, 2011
214
if(keys == NULL)
Apr 20, 2011
Apr 20, 2011
215
return ret;
Nov 30, 2013
Nov 30, 2013
216
origkeys = keys;
Apr 6, 2011
Apr 6, 2011
217
218
while (*keys != NULL)
{
Apr 20, 2011
Apr 20, 2011
219
220
221
222
223
224
if(!strcmp(*keys, key))
{
ret = g_key_file_get_integer(settingsfile, entry, *keys, NULL);
log_debug("%s key value = %d\n", key, ret);
}
keys++;
Apr 6, 2011
Apr 6, 2011
225
}
Nov 30, 2013
Nov 30, 2013
226
g_strfreev(origkeys);
Apr 6, 2011
Apr 6, 2011
227
228
g_key_file_free(settingsfile);
return(ret);
Apr 20, 2011
Apr 20, 2011
229
Mar 22, 2011
Mar 22, 2011
230
}
Apr 15, 2011
Apr 15, 2011
231
Nov 7, 2014
Nov 7, 2014
232
static char * get_conf_string(const gchar *entry, const gchar *key)
Apr 15, 2011
Apr 15, 2011
233
234
235
{
GKeyFile *settingsfile;
gboolean test = FALSE;
Oct 17, 2013
Oct 17, 2013
236
gchar **keys, **origkeys, *tmp_char = NULL;
Apr 15, 2011
Apr 15, 2011
237
238
239
240
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
{
Nov 13, 2012
Nov 13, 2012
241
242
log_debug("No conffile. Creating\n");
create_conf_file();
Oct 17, 2013
Oct 17, 2013
243
244
/* should succeed now */
g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
Apr 15, 2011
Apr 15, 2011
245
}
Apr 20, 2011
Apr 20, 2011
246
keys = g_key_file_get_keys (settingsfile, entry, NULL, NULL);
Apr 15, 2011
Apr 15, 2011
247
if(keys == NULL)
Oct 17, 2013
Oct 17, 2013
248
249
goto end;
origkeys = keys;
Apr 15, 2011
Apr 15, 2011
250
251
while (*keys != NULL)
{
Apr 20, 2011
Apr 20, 2011
252
253
254
255
256
257
258
259
260
if(!strcmp(*keys, key))
{
tmp_char = g_key_file_get_string(settingsfile, entry, *keys, NULL);
if(tmp_char)
{
log_debug("key %s value = %s\n", key, tmp_char);
}
}
keys++;
Apr 15, 2011
Apr 15, 2011
261
}
Oct 17, 2013
Oct 17, 2013
262
263
g_strfreev(origkeys);
end:
Apr 15, 2011
Apr 15, 2011
264
g_key_file_free(settingsfile);
Oct 17, 2013
Oct 17, 2013
265
return(tmp_char);
Apr 20, 2011
Apr 20, 2011
266
Apr 15, 2011
Apr 15, 2011
267
}
Apr 20, 2011
Apr 20, 2011
268
Nov 7, 2014
Nov 7, 2014
269
static char * get_kcmdline_string(const char *entry)
May 17, 2013
May 17, 2013
270
271
272
{
int fd;
char cmdLine[1024];
Nov 7, 2014
Nov 7, 2014
273
char *ret = NULL;
May 17, 2013
May 17, 2013
274
275
276
int len;
gint argc = 0;
gchar **argv = NULL;
May 24, 2013
May 24, 2013
277
gchar **arg_tokens = NULL, **network_tokens = NULL;
May 17, 2013
May 17, 2013
278
279
280
GError *optErr = NULL;
int i;
May 17, 2013
May 17, 2013
281
282
if ((fd = open("/proc/cmdline", O_RDONLY)) < 0)
{
May 17, 2013
May 17, 2013
283
284
285
286
287
288
289
log_debug("could not read /proc/cmdline");
return(ret);
}
len = read(fd, cmdLine, sizeof(cmdLine) - 1);
close(fd);
May 17, 2013
May 17, 2013
290
291
if (len <= 0)
{
May 17, 2013
May 17, 2013
292
293
294
295
296
297
log_debug("kernel command line was empty");
return(ret);
}
cmdLine[len] = '\0';
May 24, 2013
May 24, 2013
298
/* we're looking for a piece of the kernel command line matching this:
May 24, 2013
May 24, 2013
299
ip=192.168.3.100::192.168.3.1:255.255.255.0::usb0:on */
May 17, 2013
May 17, 2013
300
301
if (!g_shell_parse_argv(cmdLine, &argc, &argv, &optErr))
{
May 17, 2013
May 17, 2013
302
303
304
305
g_error_free(optErr);
return(ret);
}
May 24, 2013
May 24, 2013
306
/* find the ip token */
May 17, 2013
May 17, 2013
307
308
for (i=0; i < argc; i++)
{
May 17, 2013
May 17, 2013
309
arg_tokens = g_strsplit(argv[i], "=", 2);
Oct 2, 2013
Oct 2, 2013
310
if (!g_ascii_strcasecmp(arg_tokens[0], "usb_moded_ip"))
May 17, 2013
May 17, 2013
311
{
May 24, 2013
May 24, 2013
312
network_tokens = g_strsplit(arg_tokens[1], ":", 7);
Jul 26, 2013
Jul 26, 2013
313
314
/* check if it is for the usb or rndis interface */
if(g_strrstr(network_tokens[5], "usb")|| (g_strrstr(network_tokens[5], "rndis")))
May 24, 2013
May 24, 2013
315
316
317
{
if(!strcmp(entry, NETWORK_IP_KEY))
{
Apr 29, 2016
Apr 29, 2016
318
g_free(ret), ret = g_strdup(network_tokens[0]);
May 24, 2013
May 24, 2013
319
320
321
322
323
324
325
log_debug("Command line ip = %s\n", ret);
}
if(!strcmp(entry, NETWORK_GATEWAY_KEY))
{
/* gateway might be empty, so we do not want to return an empty string */
if(strlen(network_tokens[2]) > 2)
{
Apr 29, 2016
Apr 29, 2016
326
g_free(ret), ret = g_strdup(network_tokens[2]);
May 24, 2013
May 24, 2013
327
328
329
log_debug("Command line gateway = %s\n", ret);
}
}
Aug 3, 2015
Aug 3, 2015
330
331
if(!strcmp(entry, NETWORK_NETMASK_KEY))
{
Apr 29, 2016
Apr 29, 2016
332
g_free(ret), ret = g_strdup(network_tokens[3]);
Aug 3, 2015
Aug 3, 2015
333
334
log_debug("Command line netmask = %s\n", ret);
}
May 24, 2013
May 24, 2013
335
}
May 17, 2013
May 17, 2013
336
}
Oct 17, 2013
Oct 17, 2013
337
g_strfreev(arg_tokens);
May 17, 2013
May 17, 2013
338
}
Oct 17, 2013
Oct 17, 2013
339
g_strfreev(argv);
May 24, 2013
May 24, 2013
340
g_strfreev(network_tokens);
May 17, 2013
May 17, 2013
341
342
343
344
return(ret);
}
Nov 7, 2014
Nov 7, 2014
345
char * get_mode_setting(void)
Sep 11, 2011
Sep 11, 2011
346
{
Nov 7, 2014
Nov 7, 2014
347
char * mode = get_kcmdline_string(MODE_SETTING_KEY);
May 17, 2013
May 17, 2013
348
349
350
if (mode != NULL)
return(mode);
Sep 11, 2011
Sep 11, 2011
351
352
return(get_conf_string(MODE_SETTING_ENTRY, MODE_SETTING_KEY));
}
May 27, 2015
May 27, 2015
353
354
355
356
357
358
/*
* @param settingsfile: already opened settingsfile we want to read an entry from
* @param entry: entry we want to read
* @param key: key value of the entry we want to read
* @new_value: potentially new value we want to compare against
*
Jul 7, 2016
Jul 7, 2016
359
* @return: 0 when the old value is the same as the new one, 1 otherwise
May 27, 2015
May 27, 2015
360
361
362
*/
int config_value_changed(GKeyFile *settingsfile, const char *entry, const char *key, const char *new_value)
{
Jul 7, 2016
Jul 7, 2016
363
364
365
366
char *old_value = g_key_file_get_string(settingsfile, entry, key, NULL);
int changed = (g_strcmp0(old_value, new_value) != 0);
g_free(old_value);
return changed;
May 27, 2015
May 27, 2015
367
}
Sep 11, 2011
Sep 11, 2011
368
May 26, 2015
May 26, 2015
369
set_config_result_t set_config_setting(const char *entry, const char *key, const char *value)
Sep 11, 2011
Sep 11, 2011
370
371
372
{
GKeyFile *settingsfile;
gboolean test = FALSE;
May 26, 2015
May 26, 2015
373
set_config_result_t ret = SET_CONFIG_ERROR;
Sep 11, 2011
Sep 11, 2011
374
375
376
377
gchar *keyfile;
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
May 26, 2015
May 26, 2015
378
379
if(test)
{
Jul 7, 2016
Jul 7, 2016
380
if(!config_value_changed(settingsfile, entry, key, value))
May 26, 2015
May 26, 2015
381
382
383
384
385
386
{
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
}
}
else
Sep 11, 2011
Sep 11, 2011
387
{
Nov 13, 2012
Nov 13, 2012
388
389
log_debug("No conffile. Creating.\n");
create_conf_file();
Sep 11, 2011
Sep 11, 2011
390
391
}
May 24, 2013
May 24, 2013
392
g_key_file_set_string(settingsfile, entry, key, value);
Sep 11, 2011
Sep 11, 2011
393
394
395
396
397
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
/* free the settingsfile before writing things out to be sure
the contents will be correctly written to file afterwards.
Just a precaution. */
g_key_file_free(settingsfile);
May 26, 2015
May 26, 2015
398
399
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
Oct 29, 2013
Oct 29, 2013
400
g_free(keyfile);
Sep 11, 2011
Sep 11, 2011
401
May 26, 2015
May 26, 2015
402
return (ret);
Sep 11, 2011
Sep 11, 2011
403
}
Nov 13, 2012
Nov 13, 2012
404
May 26, 2015
May 26, 2015
405
set_config_result_t set_mode_setting(const char *mode)
May 24, 2013
May 24, 2013
406
{
Aug 11, 2017
Aug 11, 2017
407
408
if (strcmp(mode, MODE_ASK) && valid_mode(mode))
return SET_CONFIG_ERROR;
May 24, 2013
May 24, 2013
409
410
411
return (set_config_setting(MODE_SETTING_ENTRY, MODE_SETTING_KEY, mode));
}
Dec 26, 2015
Dec 26, 2015
412
413
/* Builds the string used for hidden modes, when hide set to one builds the
new string of hidden modes when adding one, otherwise it will remove one */
Aug 11, 2017
Aug 11, 2017
414
static char * make_modes_string(const char *key, const char *mode_name, int include)
Dec 26, 2015
Dec 26, 2015
415
{
Aug 11, 2017
Aug 11, 2017
416
417
418
419
char *modes_new = 0;
char *modes_old = 0;
gchar **modes_arr = 0;
GString *modes_tmp = 0;
Dec 26, 2015
Dec 26, 2015
420
421
int i;
Jul 7, 2016
Jul 7, 2016
422
/* Get current comma separated list of hidden modes */
Aug 11, 2017
Aug 11, 2017
423
424
modes_old = get_conf_string(MODE_SETTING_ENTRY, key);
if(!modes_old)
Dec 26, 2015
Dec 26, 2015
425
{
Aug 11, 2017
Aug 11, 2017
426
modes_old = g_strdup("");
Dec 26, 2015
Dec 26, 2015
427
428
}
Aug 11, 2017
Aug 11, 2017
429
modes_arr = g_strsplit(modes_old, ",", 0);
Dec 26, 2015
Dec 26, 2015
430
Aug 11, 2017
Aug 11, 2017
431
modes_tmp = g_string_new(NULL);
Jul 7, 2016
Jul 7, 2016
432
Aug 11, 2017
Aug 11, 2017
433
for(i = 0; modes_arr[i] != NULL; i++)
Dec 26, 2015
Dec 26, 2015
434
{
Aug 11, 2017
Aug 11, 2017
435
if(strlen(modes_arr[i]) == 0)
Jul 7, 2016
Jul 7, 2016
436
437
438
439
440
{
/* Skip any empty strings */
continue;
}
Aug 11, 2017
Aug 11, 2017
441
if(!strcmp(modes_arr[i], mode_name))
Jul 7, 2016
Jul 7, 2016
442
443
{
/* When unhiding, just skip all matching entries */
Aug 11, 2017
Aug 11, 2017
444
if(!include)
Jul 7, 2016
Jul 7, 2016
445
446
447
continue;
/* When hiding, keep the 1st match and ignore the rest */
Aug 11, 2017
Aug 11, 2017
448
include = 0;
Jul 7, 2016
Jul 7, 2016
449
450
}
Aug 11, 2017
Aug 11, 2017
451
452
453
if(modes_tmp->len > 0)
modes_tmp = g_string_append(modes_tmp, ",");
modes_tmp = g_string_append(modes_tmp, modes_arr[i]);
Dec 26, 2015
Dec 26, 2015
454
}
Jul 7, 2016
Jul 7, 2016
455
Aug 11, 2017
Aug 11, 2017
456
if(include)
Dec 26, 2015
Dec 26, 2015
457
{
Jul 7, 2016
Jul 7, 2016
458
/* Adding a hidden mode and no matching entry was found */
Aug 11, 2017
Aug 11, 2017
459
460
461
if(modes_tmp->len > 0)
modes_tmp = g_string_append(modes_tmp, ",");
modes_tmp = g_string_append(modes_tmp, mode_name);
Dec 26, 2015
Dec 26, 2015
462
}
Jul 7, 2016
Jul 7, 2016
463
Aug 11, 2017
Aug 11, 2017
464
modes_new = g_string_free(modes_tmp, FALSE), modes_tmp = 0;
Jul 7, 2016
Jul 7, 2016
465
Aug 11, 2017
Aug 11, 2017
466
g_strfreev(modes_arr), modes_arr = 0;
Jul 7, 2016
Jul 7, 2016
467
Aug 11, 2017
Aug 11, 2017
468
g_free(modes_old), modes_old = 0;
Jul 7, 2016
Jul 7, 2016
469
Aug 11, 2017
Aug 11, 2017
470
return modes_new;
Dec 26, 2015
Dec 26, 2015
471
472
}
Dec 15, 2015
Dec 15, 2015
473
474
set_config_result_t set_hide_mode_setting(const char *mode)
{
Jul 7, 2016
Jul 7, 2016
475
476
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Aug 11, 2017
Aug 11, 2017
477
char *hidden_modes = make_modes_string(MODE_HIDE_KEY, mode, 1);
Jul 7, 2016
Jul 7, 2016
478
479
480
481
if( hidden_modes ) {
ret = set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
Dec 15, 2015
Dec 15, 2015
482
Apr 4, 2016
Apr 4, 2016
483
484
485
if(ret == SET_CONFIG_UPDATED) {
send_hidden_modes_signal();
send_supported_modes_signal();
Aug 11, 2017
Aug 11, 2017
486
send_available_modes_signal();
Apr 4, 2016
Apr 4, 2016
487
}
Jul 7, 2016
Jul 7, 2016
488
489
490
g_free(hidden_modes);
Dec 15, 2015
Dec 15, 2015
491
return(ret);
Dec 15, 2015
Dec 15, 2015
492
493
494
495
}
set_config_result_t set_unhide_mode_setting(const char *mode)
{
Jul 7, 2016
Jul 7, 2016
496
497
set_config_result_t ret = SET_CONFIG_UNCHANGED;
Aug 11, 2017
Aug 11, 2017
498
char *hidden_modes = make_modes_string(MODE_HIDE_KEY, mode, 0);
Jul 7, 2016
Jul 7, 2016
499
500
501
502
if( hidden_modes ) {
ret = set_config_setting(MODE_SETTING_ENTRY, MODE_HIDE_KEY, hidden_modes);
}
Dec 15, 2015
Dec 15, 2015
503
Apr 4, 2016
Apr 4, 2016
504
505
506
if(ret == SET_CONFIG_UPDATED) {
send_hidden_modes_signal();
send_supported_modes_signal();
Aug 11, 2017
Aug 11, 2017
507
send_available_modes_signal();
Apr 4, 2016
Apr 4, 2016
508
}
Jul 7, 2016
Jul 7, 2016
509
510
511
g_free(hidden_modes);
Dec 15, 2015
Dec 15, 2015
512
return(ret);
Dec 15, 2015
Dec 15, 2015
513
514
}
Aug 11, 2017
Aug 11, 2017
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
set_config_result_t set_mode_whitelist(const char *whitelist)
{
set_config_result_t ret = set_config_setting(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY, whitelist);
if(ret == SET_CONFIG_UPDATED) {
char *mode_setting;
const char *current_mode;
mode_setting = get_mode_setting();
if (strcmp(mode_setting, MODE_ASK) && valid_mode(mode_setting))
set_mode_setting(MODE_ASK);
g_free(mode_setting);
current_mode = get_usb_mode();
if (strcmp(current_mode, MODE_CHARGING_FALLBACK) && strcmp(current_mode, MODE_ASK) && valid_mode(current_mode)) {
usb_moded_mode_cleanup(get_usb_module());
set_usb_mode(MODE_CHARGING_FALLBACK);
}
usb_moded_send_whitelisted_modes_signal(whitelist);
send_available_modes_signal();
}
return ret;
}
set_config_result_t set_mode_in_whitelist(const char *mode, int allowed)
{
set_config_result_t ret = SET_CONFIG_UNCHANGED;
char *whitelist = make_modes_string(MODE_WHITELIST_KEY, mode, allowed);
if (whitelist) {
ret = set_mode_whitelist(whitelist);
}
g_free(whitelist);
return(ret);
}
Apr 20, 2014
Apr 20, 2014
556
557
558
559
/*
* @param config : the key to be set
* @param setting : The value to be set
*/
May 26, 2015
May 26, 2015
560
set_config_result_t set_network_setting(const char *config, const char *setting)
Nov 19, 2012
Nov 19, 2012
561
562
563
564
565
{
GKeyFile *settingsfile;
gboolean test = FALSE;
gchar *keyfile;
Apr 20, 2014
Apr 20, 2014
566
567
if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
if(validate_ip(setting) != 0)
May 26, 2015
May 26, 2015
568
return SET_CONFIG_ERROR;
Apr 20, 2014
Apr 20, 2014
569
Nov 19, 2012
Nov 19, 2012
570
571
572
573
574
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!strcmp(config, NETWORK_IP_KEY) || !strcmp(config, NETWORK_INTERFACE_KEY) || !strcmp(config, NETWORK_GATEWAY_KEY))
{
May 26, 2015
May 26, 2015
575
576
577
set_config_result_t ret = SET_CONFIG_ERROR;
if (test)
{
Jul 7, 2016
Jul 7, 2016
578
if(!config_value_changed(settingsfile, NETWORK_ENTRY, config, setting))
May 26, 2015
May 26, 2015
579
{
May 27, 2015
May 27, 2015
580
581
g_key_file_free(settingsfile);
return SET_CONFIG_UNCHANGED;
May 26, 2015
May 26, 2015
582
583
584
585
586
587
588
589
}
}
else
{
log_debug("No conffile. Creating.\n");
create_conf_file();
}
Nov 19, 2012
Nov 19, 2012
590
591
592
593
594
595
g_key_file_set_string(settingsfile, NETWORK_ENTRY, config, setting);
keyfile = g_key_file_to_data (settingsfile, NULL, NULL);
/* free the settingsfile before writing things out to be sure
the contents will be correctly written to file afterwards.
Just a precaution. */
g_key_file_free(settingsfile);
May 26, 2015
May 26, 2015
596
597
if (g_file_set_contents(FS_MOUNT_CONFIG_FILE, keyfile, -1, NULL))
ret = SET_CONFIG_UPDATED;
Oct 29, 2013
Oct 29, 2013
598
free(keyfile);
May 26, 2015
May 26, 2015
599
return ret;
Nov 19, 2012
Nov 19, 2012
600
601
602
603
}
else
{
g_key_file_free(settingsfile);
May 26, 2015
May 26, 2015
604
return SET_CONFIG_ERROR;
Nov 19, 2012
Nov 19, 2012
605
606
}
}
Nov 13, 2012
Nov 13, 2012
607
Nov 7, 2014
Nov 7, 2014
608
char * get_network_setting(const char *config)
Aug 31, 2013
Aug 31, 2013
609
{
Nov 7, 2014
Nov 7, 2014
610
char * ret = 0;
Sep 9, 2013
Sep 9, 2013
611
struct mode_list_elem *data;
Aug 31, 2013
Aug 31, 2013
612
613
614
615
616
617
618
619
620
if(!strcmp(config, NETWORK_IP_KEY))
{
ret = get_network_ip();
if(!ret)
ret = strdup("192.168.2.15");
}
else if(!strcmp(config, NETWORK_INTERFACE_KEY))
{
Aug 3, 2015
Aug 3, 2015
621
622
623
624
625
626
627
628
629
/* check main configuration before using
the information from the specific mode */
ret = get_network_interface();
if(ret)
goto end;
/* no interface override specified, let's use the one
from the mode config */
Sep 9, 2013
Sep 9, 2013
630
631
632
633
634
635
636
637
638
data = get_usb_mode_data();
if(data)
{
if(data->network_interface)
{
ret = strdup(data->network_interface);
goto end;
}
}
Aug 3, 2015
Aug 3, 2015
639
ret = strdup("usb0");
Aug 31, 2013
Aug 31, 2013
640
641
642
}
else if(!strcmp(config, NETWORK_GATEWAY_KEY))
return(get_network_gateway());
Aug 3, 2015
Aug 3, 2015
643
else if(!strcmp(config, NETWORK_NETMASK_KEY))
Aug 3, 2015
Aug 3, 2015
644
645
646
647
648
{
ret = get_network_netmask();
if(!ret)
ret = strdup("255.255.255.0");
}
Aug 3, 2015
Aug 3, 2015
649
650
else if(!strcmp(config, NETWORK_NAT_INTERFACE_KEY))
return(get_network_nat_interface());
Aug 31, 2013
Aug 31, 2013
651
652
653
else
/* no matching keys, return error */
return(NULL);
Sep 9, 2013
Sep 9, 2013
654
end:
Aug 31, 2013
Aug 31, 2013
655
656
657
return(ret);
}
Apr 6, 2016
Apr 6, 2016
658
659
660
661
662
663
664
665
666
667
668
669
/**
* Merge value from one keyfile to another
*
* Existing values will be overridden
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
* @param grp value group to merge
* @param key value key to merge
*/
static void merge_key(GKeyFile *dest, GKeyFile *srce,
const char *grp, const char *key)
Mar 30, 2013
Mar 30, 2013
670
{
Apr 6, 2016
Apr 6, 2016
671
672
673
674
675
676
677
gchar *val = g_key_file_get_value(srce, grp, key, 0);
if( val ) {
log_debug("[%s] %s = %s", grp, key, val);
g_key_file_set_value(dest, grp, key, val);
g_free(val);
}
}
Mar 30, 2013
Mar 30, 2013
678
Apr 6, 2016
Apr 6, 2016
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
* Merge group of values from one keyfile to another
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
* @param grp value group to merge
*/
static void merge_group(GKeyFile *dest, GKeyFile *srce,
const char *grp)
{
gchar **key = g_key_file_get_keys(srce, grp, 0, 0);
if( key ) {
for( size_t i = 0; key[i]; ++i )
merge_key(dest, srce, grp, key[i]);
g_strfreev(key);
}
}
Mar 30, 2013
Mar 30, 2013
696
Apr 6, 2016
Apr 6, 2016
697
698
699
700
701
702
703
704
705
/**
* Merge all groups and values from one keyfile to another
*
* @param dest keyfile to modify
* @param srce keyfile to merge from
*/
static void merge_file(GKeyFile *dest, GKeyFile *srce)
{
gchar **grp = g_key_file_get_groups(srce, 0);
Mar 30, 2013
Mar 30, 2013
706
Apr 6, 2016
Apr 6, 2016
707
708
709
710
if( grp ) {
for( size_t i = 0; grp[i]; ++i )
merge_group(dest, srce, grp[i]);
g_strfreev(grp);
Jan 2, 2015
Jan 2, 2015
711
}
Apr 6, 2016
Apr 6, 2016
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
}
/**
* Callback function for logging errors within glob()
*
* @param path path to file/dir where error occurred
* @param err errno that occurred
*
* @return 0 (= do not stop glob)
*/
static int glob_error_cb(const char *path, int err)
{
log_debug("%s: glob: %s", path, g_strerror(err));
return 0;
}
/**
* Read *.ini files on CONFIG_FILE_DIR in the order of [0-9][A-Z][a-z]
*
* @return the in memory value-pair file.
*/
static GKeyFile *read_ini_files(void)
{
static const char pattern[] = CONFIG_FILE_DIR"/*.ini";
GKeyFile *ini = g_key_file_new();
glob_t gb;
memset(&gb, 0, sizeof gb);
if( glob(pattern, 0, glob_error_cb, &gb) != 0 ) {
log_debug("no configuration ini-files found");
g_key_file_free(ini);
ini = NULL;
goto exit;
Mar 31, 2013
Mar 31, 2013
747
}
Apr 6, 2016
Apr 6, 2016
748
749
750
751
752
753
754
755
756
757
758
759
760
761
for( size_t i = 0; i < gb.gl_pathc; ++i ) {
const char *path = gb.gl_pathv[i];
GError *err = 0;
GKeyFile *tmp = g_key_file_new();
if( !g_key_file_load_from_file(tmp, path, 0, &err) ) {
log_debug("%s: can't load: %s", path, err->message);
} else {
log_debug("processing %s ...", path);
merge_file(ini, tmp);
}
g_clear_error(&err);
g_key_file_free(tmp);
Mar 31, 2013
Mar 31, 2013
762
}
Apr 6, 2016
Apr 6, 2016
763
764
765
766
exit:
globfree(&gb);
return ini;
}
Jun 19, 2013
Jun 19, 2013
767
Apr 6, 2016
Apr 6, 2016
768
769
770
771
772
773
774
775
776
777
778
/**
* Read the *.ini files and create/overwrite FS_MOUNT_CONFIG_FILE with
* the merged data.
*
* @return 0 on failure
*/
int conf_file_merge(void)
{
GString *keyfile_string = NULL;
GKeyFile *settingsfile,*tempfile;
int ret = 0;
Mar 30, 2013
Mar 30, 2013
779
Apr 6, 2016
Apr 6, 2016
780
781
settingsfile = read_ini_files();
if (!settingsfile)
Apr 3, 2013
Apr 3, 2013
782
{
Apr 6, 2016
Apr 6, 2016
783
784
785
786
log_debug("No configuration. Creating defaults.");
create_conf_file();
/* There was no configuration so no info to be merged */
return ret;
Apr 3, 2013
Apr 3, 2013
787
}
Apr 6, 2016
Apr 6, 2016
788
789
790
791
792
793
794
tempfile = g_key_file_new();
if (g_key_file_load_from_file(tempfile, FS_MOUNT_CONFIG_FILE,
G_KEY_FILE_NONE,NULL)) {
if (!g_strcmp0(g_key_file_to_data(settingsfile, NULL, NULL),
g_key_file_to_data(tempfile, NULL, NULL)))
goto out;
May 24, 2013
May 24, 2013
795
}
Apr 6, 2016
Apr 6, 2016
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
log_debug("Merging configuration");
keyfile_string = g_string_new(NULL);
keyfile_string = g_string_append(keyfile_string,
g_key_file_to_data(settingsfile,
NULL, NULL));
if (keyfile_string) {
ret = !g_file_set_contents(FS_MOUNT_CONFIG_FILE,
keyfile_string->str,-1, NULL);
g_string_free(keyfile_string, TRUE);
}
out:
g_key_file_free(tempfile);
g_key_file_free(settingsfile);
return ret;
Mar 30, 2013
Mar 30, 2013
811
}
Aug 2, 2013
Aug 2, 2013
812
Nov 11, 2014
Nov 11, 2014
813
char * get_android_manufacturer(void)
Aug 2, 2013
Aug 2, 2013
814
{
May 11, 2016
May 11, 2016
815
816
817
818
819
820
821
822
823
824
825
#ifdef USE_MER_SSU
/* If SSU can provide manufacturer name, use it. Otherwise fall
* back to using the name specified in configuration files. */
char *ssu_name = ssu_get_manufacturer_name();
if( ssu_name )
{
return ssu_name;
}
#endif
return get_conf_string(ANDROID_ENTRY, ANDROID_MANUFACTURER_KEY);
Aug 2, 2013
Aug 2, 2013
826
}
Aug 7, 2013
Aug 7, 2013
827
Nov 11, 2014
Nov 11, 2014
828
char * get_android_vendor_id(void)
Aug 2, 2013
Aug 2, 2013
829
{
Aug 7, 2013
Aug 7, 2013
830
return(get_conf_string(ANDROID_ENTRY, ANDROID_VENDOR_ID_KEY));
Aug 2, 2013
Aug 2, 2013
831
832
}
Nov 11, 2014
Nov 11, 2014
833
char * get_android_product(void)
Aug 2, 2013
Aug 2, 2013
834
{
May 11, 2016
May 11, 2016
835
836
837
838
839
840
841
842
843
844
845
#ifdef USE_MER_SSU
/* If SSU can provide device model name, use it. Otherwise fall
* back to using the name specified in configuration files. */
char *ssu_name = ssu_get_product_name();
if( ssu_name )
{
return ssu_name;
}
#endif
return get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_KEY);
Aug 2, 2013
Aug 2, 2013
846
847
}
Nov 11, 2014
Nov 11, 2014
848
char * get_android_product_id(void)
Aug 7, 2013
Aug 7, 2013
849
850
851
852
{
return(get_conf_string(ANDROID_ENTRY, ANDROID_PRODUCT_ID_KEY));
}
Dec 15, 2015
Dec 15, 2015
853
854
855
856
char * get_hidden_modes(void)
{
return(get_conf_string(MODE_SETTING_ENTRY, MODE_HIDE_KEY));
}
Aug 11, 2017
Aug 11, 2017
857
858
859
860
char * get_mode_whitelist(void)
{
return(get_conf_string(MODE_SETTING_ENTRY, MODE_WHITELIST_KEY));
}
Dec 15, 2015
Dec 15, 2015
861
Aug 2, 2013
Aug 2, 2013
862
863
864
865
866
int check_android_section(void)
{
GKeyFile *settingsfile;
gboolean test = FALSE;
gchar **keys;
Oct 17, 2013
Oct 17, 2013
867
int ret = 1;
Aug 2, 2013
Aug 2, 2013
868
869
870
871
settingsfile = g_key_file_new();
test = g_key_file_load_from_file(settingsfile, FS_MOUNT_CONFIG_FILE, G_KEY_FILE_NONE, NULL);
if(!test)
Oct 17, 2013
Oct 17, 2013
872
873
874
875
{
ret = 0;
goto cleanup;
}
Aug 2, 2013
Aug 2, 2013
876
877
878
keys = g_key_file_get_keys (settingsfile, ANDROID_ENTRY, NULL, NULL);
if(keys == NULL)
{
Oct 17, 2013
Oct 17, 2013
879
880
ret = 0;
goto cleanup;
Aug 2, 2013
Aug 2, 2013
881
882
}
Oct 17, 2013
Oct 17, 2013
883
884
g_strfreev(keys);
cleanup:
Aug 2, 2013
Aug 2, 2013
885
g_key_file_free(settingsfile);
Oct 17, 2013
Oct 17, 2013
886
return(ret);
Aug 2, 2013
Aug 2, 2013
887
888
}
Feb 13, 2014
Feb 13, 2014
889
890
891
892
int is_roaming_not_allowed(void)
{
return(get_conf_int(NETWORK_ENTRY, NO_ROAMING_KEY));
}