usb_moded 0.86.0+mer64
usb_moded-util.c
Go to the documentation of this file.
1
26
28
29#include <stdio.h>
30#include <getopt.h>
31
32/* ========================================================================= *
33 * Prototypes
34 * ========================================================================= */
35
36/* ------------------------------------------------------------------------- *
37 * UTIL
38 * ------------------------------------------------------------------------- */
39
40static int util_query_mode (void);
41static int util_get_modelist (void);
42static int util_get_mode_configured (void);
43static int util_unset_rescue (void);
44static int util_set_mode (char *mode);
45static int util_set_mode_config (char *mode);
46static int util_set_hide_mode_config (char *mode);
47static int util_set_unhide_mode_config(char *mode);
48static int util_get_hiddenlist (void);
49static int util_handle_network (char *network);
50static int util_clear_user_config (char *uid);
51
52/* ------------------------------------------------------------------------- *
53 * MAIN
54 * ------------------------------------------------------------------------- */
55
56int main(int argc, char *argv[]);
57
58/* ========================================================================= *
59 * Data
60 * ========================================================================= */
61
62static DBusConnection *conn = 0;
63
64/* ========================================================================= *
65 * Functions
66 * ========================================================================= */
67
68static int util_query_mode (void)
69{
70 DBusMessage *req = NULL, *reply = NULL;
71 char *ret = 0;
72
73 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_STATE_REQUEST)) != NULL)
74 {
75 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
76 {
77 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
78 dbus_message_unref(reply);
79 }
80 dbus_message_unref(req);
81 }
82
83 if(ret)
84 {
85 printf("mode = %s\n", ret);
86 return 0;
87 }
88
89 /* not everything went as planned, return error */
90 return 1;
91}
92
93static int util_get_modelist (void)
94{
95 DBusMessage *req = NULL, *reply = NULL;
96 char *ret = 0;
97
98 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_LIST)) != NULL)
99 {
100 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
101 {
102 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
103 dbus_message_unref(reply);
104 }
105 dbus_message_unref(req);
106 }
107
108 if(ret)
109 {
110 printf("modes supported are = %s\n", ret);
111 return 0;
112 }
113
114 /* not everything went as planned, return error */
115 return 1;
116}
117
118static int util_get_mode_configured (void)
119{
120 DBusMessage *req = NULL, *reply = NULL;
121 char *ret = 0;
122
123 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_GET)) != NULL)
124 {
125 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
126 {
127 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
128 dbus_message_unref(reply);
129 }
130 dbus_message_unref(req);
131 }
132
133 if(ret)
134 {
135 printf("On USB connection usb_moded will set the following mode based on the configuration = %s\n", ret);
136 return 0;
137 }
138
139 /* not everything went as planned, return error */
140 return 1;
141}
142
143static int util_unset_rescue (void)
144{
145 DBusMessage *req = NULL, *reply = NULL;
146 int ret = 0;
147
148 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_RESCUE_OFF)) != NULL)
149 {
150 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
151 {
152 if(reply)
153 ret = 1;
154 dbus_message_unref(reply);
155 }
156 dbus_message_unref(req);
157 }
158
159 if(ret)
160 {
161 printf("Rescue mode is off\n");
162 return 0;
163 }
164 else
165 return 1;
166}
167
168static int util_set_mode (char *mode)
169{
170 DBusMessage *req = NULL, *reply = NULL;
171 char *ret = 0;
172
173 printf("Trying to set the following mode %s\n", mode);
174 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_STATE_SET)) != NULL)
175 {
176 dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
177 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
178 {
179 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
180 dbus_message_unref(reply);
181 }
182 dbus_message_unref(req);
183 }
184
185 if(ret)
186 {
187 printf("mode set = %s\n", ret);
188 return 0;
189 }
190
191 /* not everything went as planned, return error */
192 return 1;
193}
194
195static int util_set_mode_config (char *mode)
196{
197 DBusMessage *req = NULL, *reply = NULL;
198 char *ret = 0;
199
200 printf("Trying to set the following mode %s in the config file\n", mode);
201 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_SET)) != NULL)
202 {
203 dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
204 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
205 {
206 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
207 dbus_message_unref(reply);
208 }
209 dbus_message_unref(req);
210 }
211
212 if(ret)
213 {
214 printf("mode set in the configuration file = %s\n", ret);
215 return 0;
216 }
217
218 /* not everything went as planned, return error */
219 return 1;
220}
221
222static int util_set_hide_mode_config (char *mode)
223{
224 DBusMessage *req = NULL, *reply = NULL;
225 char *ret = 0;
226
227 printf("Trying to hide the following mode %s in the config file\n", mode);
228 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_HIDE)) != NULL)
229 {
230 dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
231 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
232 {
233 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
234 dbus_message_unref(reply);
235 }
236 dbus_message_unref(req);
237 }
238
239 if(ret)
240 {
241 printf("mode hidden = %s\n", ret);
242 return 0;
243 }
244
245 /* not everything went as planned, return error */
246 return 1;
247}
248
249static int util_set_unhide_mode_config (char *mode)
250{
251 DBusMessage *req = NULL, *reply = NULL;
252 char *ret = 0;
253
254 printf("Trying to unhide the following mode %s in the config file\n", mode);
255 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_UNHIDE)) != NULL)
256 {
257 dbus_message_append_args (req, DBUS_TYPE_STRING, &mode, DBUS_TYPE_INVALID);
258 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
259 {
260 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
261 dbus_message_unref(reply);
262 }
263 dbus_message_unref(req);
264 }
265
266 if(ret)
267 {
268 printf("mode unhidden = %s\n", ret);
269 return 0;
270 }
271
272 /* not everything went as planned, return error */
273 return 1;
274}
275
276static int util_get_hiddenlist (void)
277{
278 DBusMessage *req = NULL, *reply = NULL;
279 char *ret = 0;
280
281 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_HIDDEN_GET)) != NULL)
282 {
283 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
284 {
285 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
286 dbus_message_unref(reply);
287 }
288 dbus_message_unref(req);
289 }
290
291 if(ret)
292 {
293 printf("hidden modes are = %s\n", ret);
294 return 0;
295 }
296
297 /* not everything went as planned, return error */
298 return 1;
299}
300
301static int util_handle_network(char *network)
302{
303 int result = EXIT_FAILURE;
304 DBusMessage *req = NULL;
305 DBusMessage *reply = NULL;
306 const char *ret = NULL;
307
308 const char *operation = strtok(network, ":");
309 const char *setting = strtok(NULL, ",");
310 printf("Operation = %s\n", operation);
311 printf("Setting = %s\n", setting);
312
313 if(operation == NULL || setting == NULL )
314 {
315 printf("Argument list is wrong. Please use get:$setting or set:$setting,$value\n");
316 }
317 else if(!strcmp(operation, "set"))
318 {
319 const char *value = strtok(NULL, ",");
320 printf("Value = %s\n", value);
321 if(value == NULL)
322 {
323 printf("Argument list is wrong. Please use set:$setting,$value\n");
324 }
325 else if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_SET)) != NULL)
326 {
327 dbus_message_append_args (req, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &value, DBUS_TYPE_INVALID);
328 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
329 {
330 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
331 if(ret)
332 {
333 printf("The following USB network setting %s = %s has been set\n", setting, ret);
334 result = EXIT_SUCCESS;
335 }
336 }
337 }
338 }
339 else if(!strcmp(operation, "get"))
340 {
341 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_GET)) != NULL)
342 {
343 dbus_message_append_args (req, DBUS_TYPE_STRING, &setting, DBUS_TYPE_INVALID);
344 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
345 {
346 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
347 if(ret)
348 {
349 printf("USB network setting %s = %s\n", setting, ret);
350 result = EXIT_SUCCESS;
351 }
352 }
353 }
354
355 }
356 if(reply)
357 dbus_message_unref(reply);
358 if(req)
359 dbus_message_unref(req);
360 return result;
361}
362
363static int util_clear_user_config(char *uid)
364{
365 if (!uid) {
366 fprintf(stderr, "No uid given, try -h for more information\n");
367 return true;
368 }
369 dbus_uint32_t user = atoi(uid);
370
371 DBusMessage *req = NULL;
372 DBusMessage *reply = NULL;
373 int ret = 1;
374
375 printf("Clearing config for user uid %d\n", user);
376 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_USER_CONFIG_CLEAR)) != NULL)
377 {
378 dbus_message_append_args (req, DBUS_TYPE_UINT32, &user, DBUS_TYPE_INVALID);
379 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
380 {
381 dbus_message_unref(reply);
382 ret = 0;
383 }
384 dbus_message_unref(req);
385 }
386
387 return ret;
388}
389
390int main (int argc, char *argv[])
391{
392 int query = 0, network = 0, setmode = 0, config = 0;
393 int modelist = 0, mode_configured = 0, hide = 0, unhide = 0, hiddenlist = 0, clear = 0;
394 int res = 1, opt, rescue = 0;
395 char *option = 0;
396
397 if(argc == 1)
398 {
399 fprintf(stderr, "No options given, try -h for more information\n");
400 exit(1);
401 }
402
403 while ((opt = getopt(argc, argv, "c:dhi:mn:qrs:u:vU:")) != -1)
404 {
405 switch (opt) {
406 case 'c':
407 config = 1;
408 option = optarg;
409 break;
410 case 'd':
411 mode_configured = 1;
412 break;
413 case 'i':
414 hide = 1;
415 option = optarg;
416 break;
417 case 'm':
418 modelist = 1;
419 break;
420 case 'n':
421 network = 1;
422 option = optarg;
423 break;
424 case 'q':
425 query = 1;
426 break;
427 case 'r':
428 rescue = 1;
429 break;
430 case 's':
431 setmode = 1;
432 option = optarg;
433 break;
434 case 'u':
435 unhide = 1;
436 option = optarg;
437 break;
438 case 'v':
439 hiddenlist = 1;
440 break;
441 case 'U':
442 clear = 1;
443 option = optarg;
444 break;
445 case 'h':
446 default:
447 fprintf(stderr, "\nUsage: %s -<option> <args>\n\n \
448 Options are: \n \
449 \t-c to set a mode in the config file,\n \
450 \t-d to get the default mode set in the configuration, \n \
451 \t-h to get this help, \n \
452 \t-i hide a mode,\n \
453 \t-n to get/set network configuration. Use get:${config}/set:${config},${value}\n \
454 \t-m to get the list of supported modes, \n \
455 \t-q to query the current mode,\n \
456 \t-r turn rescue mode off,\n \
457 \t-s to set/activate a mode,\n \
458 \t-u unhide a mode,\n \
459 \t-v to get the list of hidden modes\n \
460 \t-U <uid> to clear config for a user\n",
461 argv[0]);
462 exit(1);
463 }
464 }
465
466 /* init dbus */
467 DBusError error = DBUS_ERROR_INIT;
468
469 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
470 if (!conn)
471 {
472 if (dbus_error_is_set(&error))
473 return 1;
474 }
475
476 /* check which sub-routine to call */
477 if(query)
478 res = util_query_mode();
479 else if (modelist)
480 res = util_get_modelist();
481 else if (mode_configured)
482 res = util_get_mode_configured();
483 else if (setmode)
484 res = util_set_mode(option);
485 else if (config)
486 res = util_set_mode_config(option);
487 else if (network)
488 res = util_handle_network(option);
489 else if (rescue)
490 res = util_unset_rescue();
491 else if (hide)
492 res = util_set_hide_mode_config(option);
493 else if (unhide)
494 res = util_set_unhide_mode_config(option);
495 else if (hiddenlist)
496 res = util_get_hiddenlist();
497 else if (clear)
498 res = util_clear_user_config(option);
499
500 /* subfunctions will return 1 if an error occured, print message */
501 if(res)
502 printf("Sorry an error occured, your request was not processed.\n");
503
504 /* clean-up and exit */
505 dbus_connection_close(conn);
506 dbus_connection_unref(conn);
507 return 0;
508}