const GValue *value,
GParamSpec *pspec);
static void la_handler_application_startup (GApplication *application);
-static int la_handler_application_command_line (GApplication *application,
- GApplicationCommandLine *cmdline);
gapplication_class = G_APPLICATION_CLASS (klass);
gapplication_class->startup = la_handler_application_startup;
- gapplication_class->command_line = la_handler_application_command_line;
g_object_class_install_property (gobject_class,
PROP_LA_HANDLER_SERVICE,
-static int
-la_handler_application_command_line (GApplication *application,
- GApplicationCommandLine *cmdline)
-{
- GOptionContext *context;
- gboolean do_register;
- GError *error = NULL;
- gchar **args;
- gchar **argv;
- gchar *mode = NULL;
- gchar *unit = NULL;
- gint argc;
- gint timeout = 0;
- gint i;
-
- GOptionEntry entries[] = {
- {"register", 0, 0, G_OPTION_ARG_NONE, &do_register, NULL, NULL},
- {"unit", 0, 0, G_OPTION_ARG_STRING, &unit, NULL, NULL},
- {"timeout", 0, 0, G_OPTION_ARG_INT, &timeout, NULL, NULL},
- {"shutdown-mode", 0, 0, G_OPTION_ARG_STRING, &mode, NULL, NULL},
- {NULL},
- };
-
- /* keep the application running until we have finished */
- g_application_hold (application);
-
- /* retrieve the command-line arguments */
- args = g_application_command_line_get_arguments (cmdline, &argc);
-
- /* copy the args array, because g_option_context_parse() removes elements without
- * freeing them */
- argv = g_new (gchar *, argc + 1);
- for (i = 0; i <= argc; i++)
- argv[i] = args[i];
-
- /* set up the option context */
- context = g_option_context_new (NULL);
- g_option_context_set_help_enabled (context, FALSE);
- g_option_context_add_main_entries (context, entries, NULL);
-
- /* parse the arguments into the argument data */
- if (!g_option_context_parse (context, &argc, &argv, &error) || error != NULL)
- {
- /* an error occurred */
- g_application_command_line_printerr (cmdline, "%s\n", error->message);
- g_error_free (error);
- g_application_command_line_set_exit_status (cmdline, EXIT_FAILURE);
- }
- else if (do_register)
- {
- if (unit != NULL && *unit != '\0' && timeout >= 0)
- {
- /* register was called correctly */
- la_handler_service_register (LA_HANDLER_APPLICATION (application)->service,
- unit, mode ? mode : "normal", (guint) timeout);
- }
- else
- {
- /* register was called incorrectly */
- g_application_command_line_printerr (cmdline,
- "Invalid arguments. A unit must be "
- "specified and the timeout must be "
- "positive.\n");
- }
- }
-
- /* clean up */
- g_free (argv);
- g_strfreev (args);
- g_option_context_free (context);
-
- g_free (mode);
- g_free (unit);
-
- /* allow the application to stop */
- g_application_release (application);
-
- return EXIT_SUCCESS;
-}
-
-
-
LAHandlerApplication *
la_handler_application_new (LAHandlerService *service,
GApplicationFlags flags)
#include <dlt/dlt.h>
#include <legacy-app-handler/la-handler-application.h>
+#include <legacy-app-handler/la-handler-dbus.h>
#include <legacy-app-handler/la-handler-service.h>
+static gboolean
+handle_command_line (int argc,
+ char **argv,
+ GDBusConnection *connection)
+{
+ GOptionContext *context = g_option_context_new (NULL);
+ LAHandler *legacy_app_handler;
+ gboolean do_register = FALSE;
+ gboolean do_deregister = FALSE;
+ GError *error = NULL;
+ gchar *unit = NULL;
+ gchar *log_message = NULL;
+ gchar *mode = NULL;
+ gint timeout = 0;
+
+ GOptionEntry entries[] = {
+ {"deregister", 0, 0, G_OPTION_ARG_NONE, &do_deregister, NULL, NULL},
+ {"register", 0, 0, G_OPTION_ARG_NONE, &do_register, NULL, NULL},
+ {"unit", 0, 0, G_OPTION_ARG_STRING, &unit, NULL, NULL},
+ {"timeout", 0, 0, G_OPTION_ARG_INT, &timeout, NULL, NULL},
+ {"shutdown-mode", 0, 0, G_OPTION_ARG_STRING, &mode, NULL, NULL},
+ {NULL},
+ };
+
+ /* set up the option context */
+ g_option_context_set_help_enabled (context, FALSE);
+ g_option_context_add_main_entries (context, entries, NULL);
+
+ /* parse the arguments into argument data */
+ if (!g_option_context_parse (context, &argc, &argv, &error) || error != NULL)
+ {
+ /* an error occurred */
+ log_message =
+ g_strdup_printf ("Error occurred parsing arguments: %s\n", error->message);
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_error_free (error);
+ g_free (log_message);
+
+ return FALSE;
+ }
+ else if (do_register && !do_deregister)
+ {
+ if (unit == NULL || *unit == '\0' || timeout < 0)
+ {
+ /* register was called incorrectly */
+ log_message =
+ g_strdup_printf ("Invalid arguments for --register. A unit must be specified"
+ " and the timeout must be positive.");
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_free (log_message);
+
+ return FALSE;
+ }
+
+ /* get a legacy app handler interface */
+ legacy_app_handler =
+ la_handler_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE,
+ "org.genivi.LegacyAppHandler1",
+ "/org/genivi/LegacyAppHandler1", NULL, &error);
+ if (error != NULL)
+ {
+ /* failed to connect to the legacy app handler */
+ log_message =
+ g_strdup_printf ("Error occurred connecting to legacy app handler "
+ "interface: %s", error->message);
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_free (log_message);
+ g_error_free (error);
+
+ return FALSE;
+ }
+
+ /* call the legacy app handler's Register() method */
+ la_handler_call_register_sync (legacy_app_handler, unit,
+ mode ? mode : "normal", (guint) timeout, NULL,
+ &error);
+ if (error != NULL)
+ {
+ /* failed to register the legacy app */
+ log_message = g_strdup_printf ("Error occurred registering legacy app: %s",
+ error->message);
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_object_unref (legacy_app_handler);
+ g_free (log_message);
+ g_error_free (error);
+
+ return FALSE;
+ }
+
+ g_object_unref (legacy_app_handler);
+
+ return TRUE;
+
+ }
+ else if (do_deregister && !do_register)
+ {
+ if (unit == NULL || *unit == '\0')
+ {
+ /* deregister was called incorrectly */
+ log_message =
+ g_strdup_printf ("Invalid arguments for --deregister. A unit must be "
+ "specified.");
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_free (log_message);
+
+ return FALSE;
+ }
+
+ /* get a legacy app handler interface */
+ legacy_app_handler =
+ la_handler_proxy_new_sync (connection, G_DBUS_PROXY_FLAGS_NONE,
+ "org.genivi.LegacyAppHandler1",
+ "/org/genivi/LegacyAppHandler1", NULL, &error);
+ if (error != NULL)
+ {
+ log_message =
+ g_strdup_printf ("Error occurred connecting to legacy app handler "
+ "interface: %s", error->message);
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_free (log_message);
+ g_error_free (error);
+
+ return FALSE;
+ }
+
+ /* call the legacy app handler's Deregister() method */
+ la_handler_call_deregister_sync (legacy_app_handler, unit, NULL, &error);
+ if (error != NULL)
+ {
+ log_message = g_strdup_printf ("Error occurred deregistering legacy "
+ "app: %s", error->message);
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_object_unref (legacy_app_handler);
+ g_free (log_message);
+ g_error_free (error);
+
+ return FALSE;
+ }
+
+ g_object_unref (legacy_app_handler);
+
+ return TRUE;
+ }
+ else if (do_register && do_deregister)
+ {
+ log_message =
+ g_strdup_printf ("Invalid arguments. Please choose either --register or "
+ "--deregister.");
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_message));
+
+ g_free (log_message);
+
+ return FALSE;
+ }
+ else
+ {
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR,
+ DLT_STRING ("No arguments recognised"));
+ return FALSE;
+ }
+}
+
+
+
int
main (int argc,
char **argv)
return EXIT_FAILURE;
}
- /* instantiate the LegacyAppHandler service implementation */
- service = la_handler_service_new (connection);
- if (!la_handler_service_start (service, &error))
- {
- log_text = g_strdup_printf ("Failed to start the legacy app handler service: %s",
- error->message);
- DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_text));
-
- /* clean up */
- g_free (log_text);
- g_error_free (error);
- g_object_unref (service);
- g_object_unref (connection);
-
- return EXIT_FAILURE;
- }
-
- /* create and run the main application */
if (is_remote)
{
- /* an application with the flag G_APPLICATION_IS_SERVICE tries to be the primary
- * instance of the application, and fails if another instance already exists.
- * setting G_APPLICATION_IS_LAUNCHER indicates that it shouldn't try to be the
- * primary instance */
- application =
- la_handler_application_new (service, G_APPLICATION_HANDLES_COMMAND_LINE |
- G_APPLICATION_IS_LAUNCHER);
+ if (!handle_command_line (argc, argv, connection))
+ exit_status = EXIT_FAILURE;
+ else
+ exit_status = EXIT_SUCCESS;
}
else
{
- /* this application is meant to be the primary instance, so
- * G_APPLICATION_IS_LAUNCHER is not set */
+ /* instantiate the LegacyAppHandler service implementation */
+ service = la_handler_service_new (connection);
+ if (!la_handler_service_start (service, &error))
+ {
+ log_text = g_strdup_printf ("Failed to start the legacy app handler service: %s",
+ error->message);
+ DLT_LOG (la_handler_context, DLT_LOG_ERROR, DLT_STRING (log_text));
+
+ /* clean up */
+ g_free (log_text);
+ g_error_free (error);
+ g_object_unref (service);
+ g_object_unref (connection);
+
+ return EXIT_FAILURE;
+ }
+
+ /* create and run the main application */
application =
la_handler_application_new (service, G_APPLICATION_IS_SERVICE);
- }
- /* run the application */
- exit_status = g_application_run (G_APPLICATION (application), argc, argv);
- g_object_unref (application);
+ exit_status = g_application_run (G_APPLICATION (application), argc, argv);
+ g_object_unref (application);
+
+ /* release allocated objects */
+ g_object_unref (service);
+ }
- /* release allocated objects */
- g_object_unref (service);
g_object_unref (connection);
return exit_status;