usb_moded  0.86.0+mer57
usb_moded-util.c
Go to the documentation of this file.
1 
27 #include "usb_moded-dbus-private.h"
28 
29 #include <stdio.h>
30 #include <getopt.h>
31 
32 /* ========================================================================= *
33  * Prototypes
34  * ========================================================================= */
35 
36 /* ------------------------------------------------------------------------- *
37  * UTIL
38  * ------------------------------------------------------------------------- */
39 
40 static int util_query_mode (void);
41 static int util_get_modelist (void);
42 static int util_get_mode_configured (void);
43 static int util_unset_rescue (void);
44 static int util_set_mode (char *mode);
45 static int util_set_mode_config (char *mode);
46 static int util_set_hide_mode_config (char *mode);
47 static int util_set_unhide_mode_config(char *mode);
48 static int util_get_hiddenlist (void);
49 static int util_handle_network (char *network);
50 static int util_clear_user_config (char *uid);
51 
52 /* ------------------------------------------------------------------------- *
53  * MAIN
54  * ------------------------------------------------------------------------- */
55 
56 int main(int argc, char *argv[]);
57 
58 /* ========================================================================= *
59  * Data
60  * ========================================================================= */
61 
62 static DBusConnection *conn = 0;
63 
64 /* ========================================================================= *
65  * Functions
66  * ========================================================================= */
67 
68 static 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 
93 static 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 
118 static 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 
143 static 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 
168 static 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 
195 static 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 
222 static 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 
249 static 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 
276 static 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 
301 static int util_handle_network(char *network)
302 {
303  char *operation = 0, *setting = 0, *value = 0;
304  DBusMessage *req = NULL, *reply = NULL;
305  char *ret = 0;
306 
307  operation = strtok(network, ":");
308  printf("Operation = %s\n", operation);
309  setting = strtok(NULL, ",");
310  printf("Setting = %s\n", setting);
311  value = strtok(NULL, ",");
312  printf("Value = %s\n", value);
313  if(operation == NULL || setting == NULL )
314  {
315  printf("Argument list is wrong. Please use get:$setting or set:$setting,$value\n");
316  return 1;
317  }
318  if(!strcmp(operation, "set"))
319  {
320  if(value == NULL)
321  {
322  printf("Argument list is wrong. Please use set:$setting,$value\n");
323  return 1;
324  }
325  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  dbus_message_unref(reply);
332  }
333  dbus_message_unref(req);
334  }
335 
336  if(ret)
337  {
338  printf("The following USB network setting %s = %s has been set\n", setting, ret);
339  return 0;
340  }
341  else
342  return 1;
343  }
344  else if(!strcmp(operation, "get"))
345  {
346 
347  if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_GET)) != NULL)
348  {
349  dbus_message_append_args (req, DBUS_TYPE_STRING, &setting, DBUS_TYPE_INVALID);
350  if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
351  {
352  dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
353  dbus_message_unref(reply);
354  }
355  dbus_message_unref(req);
356  }
357 
358  if(ret)
359  {
360  printf("USB network setting %s = %s\n", setting, ret);
361  return 0;
362  }
363  else
364  return 1;
365  }
366  else
367  /* unknown operation */
368  return 1;
369 }
370 
371 static int util_clear_user_config(char *uid)
372 {
373  if (!uid) {
374  fprintf(stderr, "No uid given, try -h for more information\n");
375  return true;
376  }
377  dbus_uint32_t user = atoi(uid);
378 
379  DBusMessage *req = NULL;
380  DBusMessage *reply = NULL;
381  int ret = 1;
382 
383  printf("Clearing config for user uid %d\n", user);
384  if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_USER_CONFIG_CLEAR)) != NULL)
385  {
386  dbus_message_append_args (req, DBUS_TYPE_UINT32, &user, DBUS_TYPE_INVALID);
387  if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
388  {
389  dbus_message_unref(reply);
390  ret = 0;
391  }
392  dbus_message_unref(req);
393  }
394 
395  return ret;
396 }
397 
398 int main (int argc, char *argv[])
399 {
400  int query = 0, network = 0, setmode = 0, config = 0;
401  int modelist = 0, mode_configured = 0, hide = 0, unhide = 0, hiddenlist = 0, clear = 0;
402  int res = 1, opt, rescue = 0;
403  char *option = 0;
404 
405  if(argc == 1)
406  {
407  fprintf(stderr, "No options given, try -h for more information\n");
408  exit(1);
409  }
410 
411  while ((opt = getopt(argc, argv, "c:dhi:mn:qrs:u:vU:")) != -1)
412  {
413  switch (opt) {
414  case 'c':
415  config = 1;
416  option = optarg;
417  break;
418  case 'd':
419  mode_configured = 1;
420  break;
421  case 'i':
422  hide = 1;
423  option = optarg;
424  break;
425  case 'm':
426  modelist = 1;
427  break;
428  case 'n':
429  network = 1;
430  option = optarg;
431  break;
432  case 'q':
433  query = 1;
434  break;
435  case 'r':
436  rescue = 1;
437  break;
438  case 's':
439  setmode = 1;
440  option = optarg;
441  break;
442  case 'u':
443  unhide = 1;
444  option = optarg;
445  break;
446  case 'v':
447  hiddenlist = 1;
448  break;
449  case 'U':
450  clear = 1;
451  option = optarg;
452  break;
453  case 'h':
454  default:
455  fprintf(stderr, "\nUsage: %s -<option> <args>\n\n \
456  Options are: \n \
457  \t-c to set a mode in the config file,\n \
458  \t-d to get the default mode set in the configuration, \n \
459  \t-h to get this help, \n \
460  \t-i hide a mode,\n \
461  \t-n to get/set network configuration. Use get:${config}/set:${config},${value}\n \
462  \t-m to get the list of supported modes, \n \
463  \t-q to query the current mode,\n \
464  \t-r turn rescue mode off,\n \
465  \t-s to set/activate a mode,\n \
466  \t-u unhide a mode,\n \
467  \t-v to get the list of hidden modes\n \
468  \t-U <uid> to clear config for a user\n",
469  argv[0]);
470  exit(1);
471  }
472  }
473 
474  /* init dbus */
475  DBusError error = DBUS_ERROR_INIT;
476 
477  conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
478  if (!conn)
479  {
480  if (dbus_error_is_set(&error))
481  return 1;
482  }
483 
484  /* check which sub-routine to call */
485  if(query)
486  res = util_query_mode();
487  else if (modelist)
488  res = util_get_modelist();
489  else if (mode_configured)
490  res = util_get_mode_configured();
491  else if (setmode)
492  res = util_set_mode(option);
493  else if (config)
494  res = util_set_mode_config(option);
495  else if (network)
496  res = util_handle_network(option);
497  else if (rescue)
498  res = util_unset_rescue();
499  else if (hide)
500  res = util_set_hide_mode_config(option);
501  else if (unhide)
502  res = util_set_unhide_mode_config(option);
503  else if (hiddenlist)
504  res = util_get_hiddenlist();
505  else if (clear)
506  res = util_clear_user_config(option);
507 
508  /* subfunctions will return 1 if an error occured, print message */
509  if(res)
510  printf("Sorry an error occured, your request was not processed.\n");
511 
512  /* clean-up and exit */
513  dbus_connection_close(conn);
514  dbus_connection_unref(conn);
515  return 0;
516 }
USB_MODE_SERVICE
#define USB_MODE_SERVICE
Definition: usb_moded-dbus.h:42
usb_moded-dbus-private.h