1580a87ec1d5466b0a6cd42f6d464cedf897dbe3
[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   GError                     *error = NULL;
42   int                         exit_status;
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 and run the main application */
93   application = nsm_dummy_application_new (connection, 
94                                            consumer_service, 
95                                            lifecycle_control_service);
96   exit_status = g_application_run (G_APPLICATION (application), 0, NULL);
97
98   /* release allocated objects */
99   g_object_unref (application);
100   g_object_unref (lifecycle_control_service);
101   g_object_unref (consumer_service);
102   g_object_unref (connection);
103
104   /* unregister the application and context with DLT */
105   DLT_UNREGISTER_CONTEXT (nsm_dummy_context);
106   DLT_UNREGISTER_APP ();
107
108   return exit_status;
109 }