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);
56int main(
int argc,
char *argv[]);
62static DBusConnection *conn = 0;
68static 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);
93static 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);
118static 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);
143static 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");
168static 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);
195static 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);
222static 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);
249static 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);
276static 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);
301static int util_handle_network(
char *network)
303 int result = EXIT_FAILURE;
304 DBusMessage *req = NULL;
305 DBusMessage *reply = NULL;
306 const char *ret = NULL;
308 const char *operation = strtok(network,
":");
309 const char *setting = strtok(NULL,
",");
310 printf(
"Operation = %s\n", operation);
311 printf(
"Setting = %s\n", setting);
313 if(operation == NULL || setting == NULL )
315 printf(
"Argument list is wrong. Please use get:$setting or set:$setting,$value\n");
317 else if(!strcmp(operation,
"set"))
319 const char *value = strtok(NULL,
",");
320 printf(
"Value = %s\n", value);
323 printf(
"Argument list is wrong. Please use set:$setting,$value\n");
325 else 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);
333 printf(
"The following USB network setting %s = %s has been set\n", setting, ret);
334 result = EXIT_SUCCESS;
339 else if(!strcmp(operation,
"get"))
341 if ((req = dbus_message_new_method_call(USB_MODE_SERVICE, USB_MODE_OBJECT, USB_MODE_INTERFACE, USB_MODE_NETWORK_GET)) != NULL)
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)
346 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &setting, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
349 printf(
"USB network setting %s = %s\n", setting, ret);
350 result = EXIT_SUCCESS;
357 dbus_message_unref(reply);
359 dbus_message_unref(req);
363static int util_clear_user_config(
char *uid)
366 fprintf(stderr,
"No uid given, try -h for more information\n");
369 dbus_uint32_t user = atoi(uid);
371 DBusMessage *req = NULL;
372 DBusMessage *reply = NULL;
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)
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)
381 dbus_message_unref(reply);
384 dbus_message_unref(req);
390int main (
int argc,
char *argv[])
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;
399 fprintf(stderr,
"No options given, try -h for more information\n");
403 while ((opt = getopt(argc, argv,
"c:dhi:mn:qrs:u:vU:")) != -1)
447 fprintf(stderr,
"\nUsage: %s -<option> <args>\n\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",
467 DBusError error = DBUS_ERROR_INIT;
469 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
472 if (dbus_error_is_set(&error))
478 res = util_query_mode();
480 res = util_get_modelist();
481 else if (mode_configured)
482 res = util_get_mode_configured();
484 res = util_set_mode(option);
486 res = util_set_mode_config(option);
488 res = util_handle_network(option);
490 res = util_unset_rescue();
492 res = util_set_hide_mode_config(option);
494 res = util_set_unhide_mode_config(option);
496 res = util_get_hiddenlist();
498 res = util_clear_user_config(option);
502 printf(
"Sorry an error occured, your request was not processed.\n");
505 dbus_connection_close(conn);
506 dbus_connection_unref(conn);