summaryrefslogtreecommitdiff
path: root/nsm-dummy/nsm-lifecycle-control-service.c
blob: 0b3077878e48227049a0bf9e538310af63781d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* vi:set et ai sw=2 sts=2 ts=2: */
/* -
 * Copyright (c) 2012 GENIVI.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib-object.h>
#include <gio/gio.h>

#include <dlt/dlt.h>

#include <common/nsm-enum-types.h>
#include <common/nsm-lifecycle-control-dbus.h>

#include <nsm-dummy/nsm-lifecycle-control-service.h>



DLT_IMPORT_CONTEXT (nsm_dummy_context);



/* property identifiers */
enum
{
  PROP_0,
  PROP_CONNECTION,
};



static void     nsm_lifecycle_control_service_finalize                  (GObject                    *object);
static void     nsm_lifecycle_control_service_get_property              (GObject                    *object,
                                                                         guint                       prop_id,
                                                                         GValue                     *value,
                                                                         GParamSpec                 *pspec);
static void     nsm_lifecycle_control_service_set_property              (GObject                    *object,
                                                                         guint                       prop_id,
                                                                         const GValue               *value,
                                                                         GParamSpec                 *pspec);
static gboolean nsm_lifecycle_control_service_handle_set_node_state     (NSMLifecycleControl        *object,
                                                                         GDBusMethodInvocation      *invocation,
                                                                         gint                        node_state_id,
                                                                         NSMLifecycleControlService *service);
static gboolean nsm_lifecycle_control_service_handle_check_luc_required (NSMLifecycleControl        *object,
                                                                         GDBusMethodInvocation      *invocation,
                                                                         NSMLifecycleControlService *service);



struct _NSMLifecycleControlServiceClass
{
  GObjectClass __parent__;
};

struct _NSMLifecycleControlService
{
  GObject              __parent__;

  NSMLifecycleControl *interface;
  GDBusConnection     *connection;

  /* this variable show if the node state manager mode has been set */
  gboolean             accept_state;

  /* this variable show if LUC should be restored */
  gboolean             luc_required;
};



G_DEFINE_TYPE (NSMLifecycleControlService, nsm_lifecycle_control_service, G_TYPE_OBJECT);



static void
nsm_lifecycle_control_service_class_init (NSMLifecycleControlServiceClass *klass)
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = nsm_lifecycle_control_service_finalize;
  gobject_class->get_property = nsm_lifecycle_control_service_get_property;
  gobject_class->set_property = nsm_lifecycle_control_service_set_property;

  g_object_class_install_property (gobject_class,
                                   PROP_CONNECTION,
                                   g_param_spec_object ("connection",
                                                        "connection",
                                                        "connection",
                                                        G_TYPE_DBUS_CONNECTION,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));
}



static void
nsm_lifecycle_control_service_init (NSMLifecycleControlService *service)
{
  service->interface = nsm_lifecycle_control_skeleton_new ();
  service->accept_state = TRUE;
  service->luc_required = TRUE;

  /* implement the SetNodeState() handler */
  g_signal_connect (service->interface, "handle-set-node-state",
                    G_CALLBACK (nsm_lifecycle_control_service_handle_set_node_state),
                    service);

  /* implement the CheckLucRequired() handler */
  g_signal_connect (service->interface, "handle-check-luc-required",
                    G_CALLBACK (nsm_lifecycle_control_service_handle_check_luc_required),
                    service);
}



static void
nsm_lifecycle_control_service_finalize (GObject *object)
{
  NSMLifecycleControlService *service = NSM_LIFECYCLE_CONTROL_SERVICE (object);

  /* release the D-Bus connection object */
  if (service->connection != NULL)
    g_object_unref (service->connection);

  /* release the interface skeleton */
  g_signal_handlers_disconnect_matched (service->interface,
                                        G_SIGNAL_MATCH_DATA,
                                        0, 0, NULL, NULL, service);
  g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (service->interface));
  g_object_unref (service->interface);

  (*G_OBJECT_CLASS (nsm_lifecycle_control_service_parent_class)->finalize) (object);
}



static void
nsm_lifecycle_control_service_get_property (GObject    *object,
                                            guint       prop_id,
                                            GValue     *value,
                                            GParamSpec *pspec)
{
  NSMLifecycleControlService *service = NSM_LIFECYCLE_CONTROL_SERVICE (object);

  switch (prop_id)
    {
    case PROP_CONNECTION:
      g_value_set_object (value, service->connection);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
nsm_lifecycle_control_service_set_property (GObject      *object,
                                            guint         prop_id,
                                            const GValue *value,
                                            GParamSpec   *pspec)
{
  NSMLifecycleControlService *service = NSM_LIFECYCLE_CONTROL_SERVICE (object);

  switch (prop_id)
    {
    case PROP_CONNECTION:
      service->connection = g_value_dup_object (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static gboolean
nsm_lifecycle_control_service_handle_set_node_state (NSMLifecycleControl        *object,
                                                     GDBusMethodInvocation      *invocation,
                                                     gint                        node_state_id,
                                                     NSMLifecycleControlService *service)
{
  gint error_code;

  g_return_val_if_fail (IS_NSM_LIFECYCLE_CONTROL (object), FALSE);
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);
  g_return_val_if_fail (NSM_LIFECYCLE_CONTROL_IS_SERVICE (service), FALSE);

  /* check whether this is a valid node state */
  if (node_state_id >= NSM_NODE_STATE_NOT_SET && node_state_id <= NSM_NODE_STATE_LAST)
    {
      /* log how we handled the node state */
      DLT_LOG (nsm_dummy_context, DLT_LOG_INFO,
               DLT_STRING ("Node state"), DLT_INT (node_state_id),
               DLT_STRING ("applied:"),
               DLT_STRING (service->accept_state ? "yes" : "no"));

      /* alternate return value between successful(0) and fail(-1) with every handled call.
       * We are temporarily assuming that 0 is success and -1 is failure */
      if (service->accept_state)
        error_code = NSM_ERROR_STATUS_OK;
      else
        error_code = NSM_ERROR_STATUS_ERROR;
      service->accept_state = !service->accept_state;
    }
  else
    {
      /* log how we handled the node state */
      DLT_LOG (nsm_dummy_context, DLT_LOG_INFO,
               DLT_STRING ("Received an invalid node state:"), DLT_INT (node_state_id));

      /* let the caller know that it sent an invalid parameter */
      error_code = NSM_ERROR_STATUS_PARAMETER;
    }

  /* notify the caller that we have handled the register request */
  nsm_lifecycle_control_complete_set_node_state (object, invocation, error_code);
  return TRUE;
}



static gboolean
nsm_lifecycle_control_service_handle_check_luc_required (NSMLifecycleControl        *object,
                                                         GDBusMethodInvocation      *invocation,
                                                         NSMLifecycleControlService *service)
{
  g_return_val_if_fail (IS_NSM_LIFECYCLE_CONTROL (object), FALSE);
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);
  g_return_val_if_fail (NSM_LIFECYCLE_CONTROL_IS_SERVICE (service), FALSE);

  /* alternate return value between true and false with every handled call */
  service->luc_required = !service->luc_required;

  /* notify the caller that we have handled the register request */
  nsm_lifecycle_control_complete_check_luc_required (object,
                                                     invocation,
                                                     service->luc_required);
  return TRUE;
}



NSMLifecycleControlService *
nsm_lifecycle_control_service_new (GDBusConnection *connection)
{
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
  return g_object_new (NSM_LIFECYCLE_CONTROL_TYPE_SERVICE, "connection", connection, NULL);
}



gboolean
nsm_lifecycle_control_service_start (NSMLifecycleControlService *service,
                                     GError                    **error)
{
  g_return_val_if_fail (NSM_LIFECYCLE_CONTROL_IS_SERVICE (service), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  /* announce the LifecycleControl service on the bus */
  return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (service->interface),
                                           service->connection,
                                           "/com/contiautomotive/NodeStateManager/LifecycleControl",
                                           error);
}