Drop GApplication, fix D-Bus activation, further NSM integration work
[profile/ivi/node-startup-controller.git] / nsm-dummy / main.c
1 /* vi:set et ai sw=2 sts=2 ts=2: */
2 /* -
3  * Copyright (c) 2012 GENIVI.
4  *
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/.
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #ifdef HAVE_STDLIB_H
15 #include <stdlib.h>
16 #endif
17
18 #include <glib.h>
19 #include <gio/gio.h>
20
21 #include <dlt/dlt.h>
22
23 #include <nsm-dummy/nsm-consumer-service.h>
24 #include <nsm-dummy/nsm-dummy-application.h>
25 #include <nsm-dummy/nsm-lifecycle-control-service.h>
26
27
28
29 DLT_DECLARE_CONTEXT (nsm_dummy_context);
30
31
32
33 int
34 main (int    argc,
35       char **argv)
36 {
37   NSMLifecycleControlService *lifecycle_control_service;
38   NSMDummyApplication        *application;
39   NSMConsumerService         *consumer_service;
40   GDBusConnection            *connection;
41   GMainLoop                  *main_loop;
42   GError                     *error = NULL;
43
44   /* register the application and context in DLT */
45   DLT_REGISTER_APP ("NSMD", "GENIVI Node State Manager Dummy");
46   DLT_REGISTER_CONTEXT (nsm_dummy_context, "NSMC",
47                         "Context of the node state manager dummy itself");
48
49   /* initialize the GType type system */
50   g_type_init ();
51
52   /* attempt to connect to D-Bus */
53   connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
54   if (connection == NULL)
55     {
56       g_warning ("Failed to connect to D-Bus: %s", error->message);
57
58       /* clean up */
59       g_error_free (error);
60
61       return EXIT_FAILURE;
62     }
63
64   /* instantiate the NSMLifecycleControlService implementation */
65   lifecycle_control_service = nsm_lifecycle_control_service_new (connection);
66   if (!nsm_lifecycle_control_service_start (lifecycle_control_service, &error))
67     {
68       g_warning ("Failed to start the LifecycleControl service: %s", error->message);
69
70       /* clean up */
71       g_error_free (error);
72       g_object_unref (lifecycle_control_service);
73       g_object_unref (connection);
74
75       return EXIT_FAILURE;
76     }
77
78   /* instantiate the NSMConsumerService implementation */
79   consumer_service = nsm_consumer_service_new (connection);
80   if (!nsm_consumer_service_start (consumer_service, &error))
81     {
82       g_warning ("Failed to start the Consumer service: %s", error->message);
83
84       /* clean up */
85       g_error_free (error);
86       g_object_unref (consumer_service);
87       g_object_unref (connection);
88
89       return EXIT_FAILURE;
90     }
91
92   /* create the main loop */
93   main_loop = g_main_loop_new (NULL, FALSE);
94
95   /* create the main application */
96   application = nsm_dummy_application_new (main_loop, connection, consumer_service,
97                                            lifecycle_control_service);
98
99   /* run the main loop */
100   g_main_loop_run (main_loop);
101   g_main_loop_unref (main_loop);
102
103   /* release allocated objects */
104   g_object_unref (application);
105   g_object_unref (lifecycle_control_service);
106   g_object_unref (consumer_service);
107   g_object_unref (connection);
108
109   /* unregister the application and context with DLT */
110   DLT_UNREGISTER_CONTEXT (nsm_dummy_context);
111   DLT_UNREGISTER_APP ();
112
113   return EXIT_SUCCESS;
114 }