Skip to content

Latest commit

 

History

History
451 lines (393 loc) · 11.9 KB

usb_moded-util.c

File metadata and controls

451 lines (393 loc) · 11.9 KB
 
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
/**
@file usb_moded-util.c
Copyright (C) 2013 Jolla. All rights reserved.
@author: Philippe De Swert <philippe.deswert@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
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
Aug 31, 2013
Aug 31, 2013
27
#include <string.h>
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
#include <dbus/dbus.h>
#include "usb_moded-dbus.h"
DBusError error;
DBusConnection *conn = 0;
static int query_mode (void)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_STATE_REQUEST)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("mode = %s\n", ret);
return 0;
}
/* not everything went as planned, return error */
return 1;
}
static int get_modelist (void)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_LIST)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("modes supported are = %s\n", ret);
return 0;
}
/* not everything went as planned, return error */
return 1;
}
static int get_mode_configured (void)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_GET)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("On USB connection usb_moded will set the following mode based on the configuration = %s\n", ret);
return 0;
}
/* not everything went as planned, return error */
return 1;
}
Oct 10, 2013
Oct 10, 2013
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
static int unset_rescue (void)
{
DBusMessage *req = NULL, *reply = NULL;
int ret = 0;
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_RESCUE_OFF)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
if(reply)
ret = 1;
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("Rescue mode is off\n");
return 0;
}
else
return 1;
}
May 16, 2013
May 16, 2013
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
static int set_mode (char *mode)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
printf("Trying to set the following mode %s\n", mode);
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_STATE_SET)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("mode set = %s\n", ret);
return 0;
}
/* not everything went as planned, return error */
return 1;
}
static int set_mode_config (char *mode)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
Aug 6, 2013
Aug 6, 2013
167
printf("Trying to set the following mode %s in the config file\n", mode);
May 16, 2013
May 16, 2013
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_SET)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("mode set in the configuration file = %s\n", ret);
return 0;
}
Dec 15, 2015
Dec 15, 2015
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* not everything went as planned, return error */
return 1;
}
static int set_hide_mode_config (char *mode)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
printf("Trying to hide the following mode %s in the config file\n", mode);
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_HIDE)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("mode hidden = %s\n", ret);
return 0;
}
/* not everything went as planned, return error */
return 1;
}
static int set_unhide_mode_config (char *mode)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
printf("Trying to unhide the following mode %s in the config file\n", mode);
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_UNHIDE)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("mode unhidden = %s\n", ret);
return 0;
}
May 16, 2013
May 16, 2013
239
240
241
242
/* not everything went as planned, return error */
return 1;
}
Apr 4, 2016
Apr 4, 2016
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
static int get_hiddenlist (void)
{
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_HIDDEN_GET)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("hidden modes are = %s\n", ret);
return 0;
}
/* not everything went as planned, return error */
return 1;
}
Aug 31, 2013
Aug 31, 2013
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
static int handle_network(char *network)
{
char *operation = 0, *setting = 0, *value = 0;
DBusMessage *req = NULL, *reply = NULL;
char *ret = 0;
operation = strtok(network, ":");
printf("Operation = %s\n", operation);
setting = strtok(NULL, ",");
printf("Setting = %s\n", setting);
value = strtok(NULL, ",");
printf("Value = %s\n", value);
if(operation == NULL || setting == NULL )
{
printf("Argument list is wrong. Please use get:$setting or set:$setting,$value\n");
Sep 9, 2013
Sep 9, 2013
283
return 1;
Aug 31, 2013
Aug 31, 2013
284
285
286
287
288
289
}
if(!strcmp(operation, "set"))
{
if(value == NULL)
{
printf("Argument list is wrong. Please use set:$setting,$value\n");
Sep 9, 2013
Sep 9, 2013
290
return 1;
Aug 31, 2013
Aug 31, 2013
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
}
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_SET)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &value, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("The following USB network setting %s = %s has been set\n", setting, ret);
return 0;
}
Sep 9, 2013
Sep 9, 2013
308
309
else
return 1;
Aug 31, 2013
Aug 31, 2013
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
}
else if(!strcmp(operation, "get"))
{
if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_GET)) != NULL)
{
dbus_message_append_args (req, DBUS_TYPE_STRING, &setting, DBUS_TYPE_INVALID);
if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(req);
}
if(ret)
{
printf("USB network setting %s = %s\n", setting, ret);
return 0;
}
Sep 9, 2013
Sep 9, 2013
330
331
else
return 1;
Aug 31, 2013
Aug 31, 2013
332
333
334
}
else
/* unknown operation */
Sep 9, 2013
Sep 9, 2013
335
return 1;
Aug 31, 2013
Aug 31, 2013
336
337
338
}
339
340
341
int main (int argc, char *argv[])
{
int query = 0, network = 0, setmode = 0, config = 0;
Apr 4, 2016
Apr 4, 2016
342
int modelist = 0, mode_configured = 0, hide = 0, unhide = 0, hiddenlist = 0;
Oct 10, 2013
Oct 10, 2013
343
int res = 1, opt, rescue = 0;
May 16, 2013
May 16, 2013
344
char *option = 0;
345
346
347
348
349
350
351
if(argc == 1)
{
fprintf(stderr, "No options given, try -h for more information\n");
exit(1);
}
Apr 4, 2016
Apr 4, 2016
352
while ((opt = getopt(argc, argv, "c:dhi:mn:qrs:u:v")) != -1)
353
354
355
356
{
switch (opt) {
case 'c':
config = 1;
May 16, 2013
May 16, 2013
357
option = optarg;
358
359
360
361
break;
case 'd':
mode_configured = 1;
break;
Dec 15, 2015
Dec 15, 2015
362
363
364
365
case 'i':
hide = 1;
option = optarg;
break;
366
367
368
369
370
case 'm':
modelist = 1;
break;
case 'n':
network = 1;
May 16, 2013
May 16, 2013
371
option = optarg;
372
373
374
375
break;
case 'q':
query = 1;
break;
Oct 10, 2013
Oct 10, 2013
376
377
378
case 'r':
rescue = 1;
break;
379
380
case 's':
setmode = 1;
May 16, 2013
May 16, 2013
381
option = optarg;
Dec 15, 2015
Dec 15, 2015
383
384
385
386
case 'u':
unhide = 1;
option = optarg;
break;
Apr 4, 2016
Apr 4, 2016
387
388
389
case 'v':
hiddenlist = 1;
break;
390
391
392
393
394
395
396
case 'h':
default:
fprintf(stderr, "\nUsage: %s -<option> <args>\n\n \
Options are: \n \
\t-c to set a mode in the config file,\n \
\t-d to get the default mode set in the configuration, \n \
\t-h to get this help, \n \
Dec 15, 2015
Dec 15, 2015
397
\t-i hide a mode,\n \
Sep 9, 2013
Sep 9, 2013
398
\t-n to get/set network configuration. Use get:${config}/set:${config},${value}\n \
399
400
\t-m to get the list of supported modes, \n \
\t-q to query the current mode,\n \
Oct 10, 2013
Oct 10, 2013
401
\t-r turn rescue mode off,\n \
Apr 4, 2016
Apr 4, 2016
402
403
404
\t-s to set/activate a mode,\n \
\t-u unhide a mode,\n \
\t-v to get the list of hidden modes\n",
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
argv[0]);
exit(1);
}
}
/* init dbus */
dbus_error_init(&error);
conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
if (!conn)
{
if (dbus_error_is_set(&error))
return 1;
}
/* check which sub-routine to call */
if(query)
res = query_mode();
else if (modelist)
res = get_modelist();
else if (mode_configured)
res = get_mode_configured();
May 16, 2013
May 16, 2013
427
428
429
430
431
else if (setmode)
res = set_mode(option);
else if (config)
res = set_mode_config(option);
else if (network)
Aug 31, 2013
Aug 31, 2013
432
res = handle_network(option);
Oct 10, 2013
Oct 10, 2013
433
434
else if (rescue)
res = unset_rescue();
Dec 15, 2015
Dec 15, 2015
435
436
437
438
else if (hide)
res = set_hide_mode_config(option);
else if (unhide)
res = set_unhide_mode_config(option);
Apr 4, 2016
Apr 4, 2016
439
440
else if (hiddenlist)
res = get_hiddenlist();
441
442
443
444
445
446
447
448
449
450
/* subfunctions will return 1 if an error occured, print message */
if(res)
printf("Sorry an error occured, your request was not processed.\n");
/* clean-up and exit */
dbus_connection_close(conn);
dbus_connection_unref(conn);
return 0;
}