1 /* vi:set et ai sw=2 sts=2 ts=2: */
3 * Copyright (c) 2012 GENIVI.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
23 #include <boot-manager/boot-manager-command-line.h>
24 #include <boot-manager/la-handler-dbus.h>
27 DLT_IMPORT_CONTEXT (la_handler_context);
32 boot_manager_handle_command_line (int argc,
34 GDBusConnection *connection)
36 GOptionContext *context = g_option_context_new (NULL);
37 LAHandler *legacy_app_handler = NULL;
38 gboolean do_register = FALSE;
39 gboolean do_deregister = FALSE;
42 gchar *log_message = NULL;
47 GOptionEntry entries[] = {
48 {"deregister", 0, 0, G_OPTION_ARG_NONE, &do_deregister, NULL, NULL},
49 {"register", 0, 0, G_OPTION_ARG_NONE, &do_register, NULL, NULL},
50 {"unit", 0, 0, G_OPTION_ARG_STRING, &unit, NULL, NULL},
51 {"timeout", 0, 0, G_OPTION_ARG_INT, &timeout, NULL, NULL},
52 {"shutdown-mode", 0, 0, G_OPTION_ARG_STRING, &mode, NULL, NULL},
56 /* set up the option context */
57 g_option_context_set_help_enabled (context, FALSE);
58 g_option_context_add_main_entries (context, entries, NULL);
60 /* parse the arguments into argument data */
61 if (!g_option_context_parse (context, &argc, &argv, &error) || error != NULL)
63 /* an error occurred */
65 g_strdup_printf ("Error occurred parsing arguments: %s\n", error->message);
66 DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
68 exit_status = EXIT_FAILURE;
72 /* validate the argument data */
73 if (unit == NULL || *unit == '\0')
75 DLT_LOG (la_handler_context, DLT_LOG_ERROR,
76 DLT_STRING ("Invalid arguments: unit must be defined"));
77 exit_status = EXIT_FAILURE;
81 if (!(do_register ^ do_deregister))
83 DLT_LOG (la_handler_context, DLT_LOG_ERROR,
84 DLT_STRING ("Invalid arguments: Please select either --register or "
86 exit_status = EXIT_FAILURE;
90 if (do_register && timeout < 0)
92 DLT_LOG (la_handler_context, DLT_LOG_ERROR,
93 DLT_STRING ("Invalid arguments: Timeout must be non-negative"));
94 exit_status = EXIT_FAILURE;
98 /* get a legacy app handler interface */
100 la_handler_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE,
101 "org.genivi.BootManager1",
102 "/org/genivi/BootManager1/LegacyAppHandler", NULL,
106 /* failed to connect to the legacy app handler */
108 g_strdup_printf ("Error occurred connecting to legacy app handler "
109 "interface: %s", error->message);
110 DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
112 g_free (log_message);
113 g_error_free (error);
115 exit_status = EXIT_FAILURE;
121 /* call the legacy app handler's Register() method */
122 la_handler_call_register_sync (legacy_app_handler, unit, mode ? mode : "normal",
123 (guint) timeout, NULL, &error);
127 /* failed to register the legacy app */
128 log_message = g_strdup_printf ("Error occurred registering legacy app: %s",
130 DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
132 exit_status = EXIT_FAILURE;
136 exit_status = EXIT_SUCCESS;
140 else if (do_deregister)
142 /* call the legacy app handler's Deregister() method */
143 la_handler_call_deregister_sync (legacy_app_handler, unit, NULL, &error);
146 /* failed to deregister the legacy app */
147 log_message = g_strdup_printf ("Error occurred deregistering legacy app: %s",
149 DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
151 exit_status = EXIT_FAILURE;
155 exit_status = EXIT_SUCCESS;
161 DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING ("unexpected input"));
162 exit_status = EXIT_FAILURE;
167 g_option_context_free (context);
168 if (legacy_app_handler != NULL)
169 g_object_unref (legacy_app_handler);
171 g_error_free (error);
173 g_free (log_message);