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);
56 int main(
int argc,
char *argv[]);
62 static DBusConnection *conn = 0;
68 static int util_query_mode (
void)
70 DBusMessage *req = NULL, *reply = NULL;
73 if ((req = dbus_message_new_method_call(
USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_STATE_REQUEST)) != NULL)
75 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
77 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
78 dbus_message_unref(reply);
80 dbus_message_unref(req);
85 printf(
"mode = %s\n", ret);
93 static int util_get_modelist (
void)
95 DBusMessage *req = NULL, *reply = NULL;
98 if ((req = dbus_message_new_method_call(
USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_LIST)) != NULL)
100 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
102 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
103 dbus_message_unref(reply);
105 dbus_message_unref(req);
110 printf(
"modes supported are = %s\n", ret);
118 static int util_get_mode_configured (
void)
120 DBusMessage *req = NULL, *reply = NULL;
123 if ((req = dbus_message_new_method_call(
USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_CONFIG_GET)) != NULL)
125 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
127 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
128 dbus_message_unref(reply);
130 dbus_message_unref(req);
135 printf(
"On USB connection usb_moded will set the following mode based on the configuration = %s\n", ret);
143 static int util_unset_rescue (
void)
145 DBusMessage *req = NULL, *reply = NULL;
148 if ((req = dbus_message_new_method_call(
USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_RESCUE_OFF)) != NULL)
150 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
154 dbus_message_unref(reply);
156 dbus_message_unref(req);
161 printf(
"Rescue mode is off\n");
168 static int util_set_mode (
char *mode)
170 DBusMessage *req = NULL, *reply = NULL;
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)
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)
179 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
180 dbus_message_unref(reply);
182 dbus_message_unref(req);
187 printf(
"mode set = %s\n", ret);
195 static int util_set_mode_config (
char *mode)
197 DBusMessage *req = NULL, *reply = NULL;
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)
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)
206 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
207 dbus_message_unref(reply);
209 dbus_message_unref(req);
214 printf(
"mode set in the configuration file = %s\n", ret);
222 static int util_set_hide_mode_config (
char *mode)
224 DBusMessage *req = NULL, *reply = NULL;
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)
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)
233 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
234 dbus_message_unref(reply);
236 dbus_message_unref(req);
241 printf(
"mode hidden = %s\n", ret);
249 static int util_set_unhide_mode_config (
char *mode)
251 DBusMessage *req = NULL, *reply = NULL;
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)
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)
260 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
261 dbus_message_unref(reply);
263 dbus_message_unref(req);
268 printf(
"mode unhidden = %s\n", ret);
276 static int util_get_hiddenlist (
void)
278 DBusMessage *req = NULL, *reply = NULL;
281 if ((req = dbus_message_new_method_call(
USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_HIDDEN_GET)) != NULL)
283 if ((reply = dbus_connection_send_with_reply_and_block(conn, req, -1, NULL)) != NULL)
285 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
286 dbus_message_unref(reply);
288 dbus_message_unref(req);
293 printf(
"hidden modes are = %s\n", ret);
301 static int util_handle_network(
char *network)
303 char *operation = 0, *setting = 0, *value = 0;
304 DBusMessage *req = NULL, *reply = NULL;
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 )
315 printf(
"Argument list is wrong. Please use get:$setting or set:$setting,$value\n");
318 if(!strcmp(operation,
"set"))
322 printf(
"Argument list is wrong. Please use set:$setting,$value\n");
325 if ((req = dbus_message_new_method_call(
USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_SET)) != NULL)
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)
330 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
331 dbus_message_unref(reply);
333 dbus_message_unref(req);
338 printf(
"The following USB network setting %s = %s has been set\n", setting, ret);
344 else if(!strcmp(operation,
"get"))
347 if ((req = dbus_message_new_method_call(
USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_GET)) != NULL)
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)
352 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
353 dbus_message_unref(reply);
355 dbus_message_unref(req);
360 printf(
"USB network setting %s = %s\n", setting, ret);
371 static int util_clear_user_config(
char *uid)
374 fprintf(stderr,
"No uid given, try -h for more information\n");
377 dbus_uint32_t user = atoi(uid);
379 DBusMessage *req = NULL;
380 DBusMessage *reply = NULL;
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)
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)
389 dbus_message_unref(reply);
392 dbus_message_unref(req);
398 int main (
int argc,
char *argv[])
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;
407 fprintf(stderr,
"No options given, try -h for more information\n");
411 while ((opt = getopt(argc, argv,
"c:dhi:mn:qrs:u:vU:")) != -1)
455 fprintf(stderr,
"\nUsage: %s -<option> <args>\n\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",
475 DBusError error = DBUS_ERROR_INIT;
477 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
480 if (dbus_error_is_set(&error))
486 res = util_query_mode();
488 res = util_get_modelist();
489 else if (mode_configured)
490 res = util_get_mode_configured();
492 res = util_set_mode(option);
494 res = util_set_mode_config(option);
496 res = util_handle_network(option);
498 res = util_unset_rescue();
500 res = util_set_hide_mode_config(option);
502 res = util_set_unhide_mode_config(option);
504 res = util_get_hiddenlist();
506 res = util_clear_user_config(option);
510 printf(
"Sorry an error occured, your request was not processed.\n");
513 dbus_connection_close(conn);
514 dbus_connection_unref(conn);