Release NodeStateManager 1.2.0.0
[profile/ivi/node-state-manager.git] / NodeStateTest / NodeStateMachineTest.c
1 /**********************************************************************************************************************
2 *
3 * Copyright (C) 2012 Continental Automotive Systems, Inc.
4 *
5 * Author: Jean-Pierre.Bogler@continental-corporation.com
6 *
7 * Implementation of the NodeStateMachineTest.
8 *
9 * The NodeStateMachineTest compiles to a shared object that is loaded by the NodeStateManager.
10 * This special test version of the NodeStateMachine implements additonal functionality.
11 * Beside of the internal interfaces that are directly called by the NSM, who links with the lib,
12 * the test NodeStateMachine offers an own dbus interface!
13 *
14 * A test frame can use this dbus interface to stimulate the NSMC to make calls to the NSM.
15 * The return values that the NSMC receives are passed back to the test frame, where it can be
16 * checked if they are valid.
17 *
18 * This Source Code Form is subject to the terms of the Mozilla Public
19 * License, v. 2.0. If a copy of the MPL was not distributed with this
20 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
21 *
22 * Date             Author              Reason
23 * 24.01.2013       Jean-Pierre Bogler  CSP_WZ#1194: Initial creation.
24 *
25 **********************************************************************************************************************/
26
27
28 /**********************************************************************************************************************
29 *
30 * Header includes
31 *
32 **********************************************************************************************************************/
33
34 #include <gio/gio.h>                 /* Access dbus using glib          */
35
36 #include "NodeStateMachineTest.h"    /* Own header file                 */
37 #include "NodeStateTypes.h"          /* Know the types of the NSM       */
38 #include "NodeStateManager.h"        /* Access inhternal NSM interfaces */
39
40 #include "NodeStateMachineTestApi.h" /* Dbus interface offered by NSMC  */
41
42
43 /**********************************************************************************************************************
44 *
45 * Local defines, macros, constants and type definitions.
46 *
47 **********************************************************************************************************************/
48
49 /* There are currently no local defines, macros or types */
50
51
52 /**********************************************************************************************************************
53 *
54 * Local variables
55 *
56 **********************************************************************************************************************/
57
58 static NodeStateTest   *TSTMSC__pTestMachine = NULL;
59 static GDBusConnection *TSTMSC__pConnection  = NULL;
60
61
62 /**********************************************************************************************************************
63 *
64 * Prototypes for file local functions (see implementation for description)
65 *
66 **********************************************************************************************************************/
67
68 static gboolean NSM__boOnHandleSetNsmData(NodeStateTest         *pTestMachine,
69                                           GDBusMethodInvocation *pInvocation,
70                                           const gint             i32DataType,
71                                           GVariant              *pData,
72                                           const guint            u32DataLen,
73                                           gpointer               pUserData);
74
75 static gboolean NSM__boOnHandleGetNsmData(NodeStateTest         *pTestMachine,
76                                           GDBusMethodInvocation *pInvocation,
77                                           const gint             i32DataType,
78                                           GVariant              *DataIn,
79                                           const guint            u32DataLen,
80                                           gpointer               pUserData);
81
82 static gboolean NSM__boOnHandleGetNsmInterfaceVersion(NodeStateTest         *pTestMachine,
83                                                       GDBusMethodInvocation *pInvocation,
84                                                       gpointer               pUserData);
85
86 /**********************************************************************************************************************
87 *
88 * Local (static) functions
89 *
90 **********************************************************************************************************************/
91
92 /**********************************************************************************************************************
93 *
94 * The function is called when a test frame wants to set data to the NSM via the NSMC.
95 *
96 * @param pTestMachine: NodeStateMachineTest object.
97 * @param pInvocation:  Invocation for this call.
98 * @param i32DataType:  DataType (possible valid values see NsmDataType_e)
99 * @param pData:        GVariant of type "ay". Includes an array of byte containing the data to be set.
100 * @param u32DataLen:   Length of the data, which is directly passed to the NSM. Can contain invalid values,
101 *                      because it is not usef for the serialization of the content of pData.
102 * @param pUserData:    Opzional user data (not used).
103 *
104 * @return TRUE: Dbus message was handled.
105 *
106 **********************************************************************************************************************/
107 static gboolean NSM__boOnHandleSetNsmData(NodeStateTest         *pTestMachine,
108                                           GDBusMethodInvocation *pInvocation,
109                                           const gint             i32DataType,
110                                           GVariant              *pData,
111                                           const guint            u32DataLen,
112                                           gpointer               pUserData)
113 {
114   /* Function local variables                                           */
115   guint             u32ArraySize  = 0;    /* Children of pData          */
116   guint8           *au8Data       = NULL; /* Pointer to byte array      */
117   guint             u32ArrayIdx   = 0;    /* Index to loop through data */
118   GVariant         *pArrayElement = NULL; /* Pointer to child of pData  */
119   NsmErrorStatus_e  enRetVal      = NsmErrorStatus_NotSet;
120   NsmDataType_e     enDateType    = (NsmDataType_e) i32DataType;
121
122   /* Create a new byte array based on the length of pData and fill it with the values */
123   u32ArraySize = g_variant_n_children(pData);
124   au8Data = g_new(guint8, u32ArraySize);
125
126   for(u32ArrayIdx = 0; u32ArrayIdx < u32ArraySize; u32ArrayIdx++)
127   {
128     pArrayElement = g_variant_get_child_value(pData, u32ArrayIdx);
129     au8Data[u32ArrayIdx] = g_variant_get_byte(pArrayElement);
130     g_variant_unref(pArrayElement);
131   }
132
133   g_variant_unref(pData); /* release the variant. We read it */
134
135   /* Call the NSM. Pass extracted data and length, originally passed by test frame */
136   enRetVal = NsmSetData(enDateType, (unsigned char*) au8Data, (unsigned int) u32DataLen);
137
138   /* Send NSMs return value to the test frame */
139   node_state_test_complete_set_nsm_data(pTestMachine, pInvocation, (gint) enRetVal);
140
141   return TRUE;
142 }
143
144 /**********************************************************************************************************************
145 *
146 * The function is called when a test frame wants to get data from the NSM via the NSMC.
147 *
148 * @param pTestMachine: NodeStateMachineTest object.
149 * @param pInvocation:  Invocation for this call.
150 * @param i32DataType:  DataType (possible valid values see NsmDataType_e)
151 * @param pDataIn:      Parameters passed by test frame fopr getting data (currently only used for sesion states)
152 * @param u32DataLen:   Length of the expected data, which is directly passed to the NSM. Can contain invalid values,
153 *                      because it is not used for the serialization of pDataOut.
154 * @param pUserData:    Opzional user data (not used).
155 *
156 * @return TRUE: Dbus message was handled.
157 *
158 **********************************************************************************************************************/
159 static gboolean NSM__boOnHandleGetNsmData(NodeStateTest         *pTestMachine,
160                                           GDBusMethodInvocation *pInvocation,
161                                           const gint             i32DataType,
162                                           GVariant              *DataIn,
163                                           const guint            u32DataLen,
164                                           gpointer               pUserData)
165 {
166   /* Function local variables                                                                        */
167   NsmSession_s       pData;                 /* Contains data from pDataIn and is popassed to the NSM */
168   int                i32RetVal      = 0;    /* Return value of the NSM                               */
169   guint              u32ArrayIdx    = 0;    /* Index to loop through data                            */
170   GVariant         **pArrayElements = NULL; /* Helper to serailize data                              */
171   GVariant          *pRetArray      = NULL; /* Outgoing data returned to NSM                         */
172   GVariant          *pDataInElement = NULL; /* Pointer to child of pDataIn                           */
173
174   /* 
175    * The NSM has a read write interface for getting data. The largest data frame that can be
176    * exchanged is a NsmSession_s. Therefore, pDataIn is translated into this kind of variable.
177    */
178   for(u32ArrayIdx = 0; u32ArrayIdx < g_variant_n_children(DataIn); u32ArrayIdx++)
179   {
180     pDataInElement = g_variant_get_child_value(DataIn, u32ArrayIdx);
181     ((guchar*) &pData)[u32ArrayIdx] = g_variant_get_byte(pDataInElement);
182   }
183
184   g_variant_unref(DataIn); /* Release pDataIn. we read the data */
185
186   /* Call the NSM */
187   i32RetVal = NsmGetData((NsmDataType_e) i32DataType, (unsigned char*) &pData, u32DataLen);
188
189   /* Serialize ougoing data retuurned by NSM, if there is a positive NSM return */
190   if(i32RetVal > 0)
191   {
192     pArrayElements = g_new(GVariant*, i32RetVal);
193
194     for(u32ArrayIdx = 0; u32ArrayIdx < (guint) i32RetVal; u32ArrayIdx++)
195     {
196       pArrayElements[u32ArrayIdx] = g_variant_new_byte( ((guchar*) &pData)[u32ArrayIdx] );
197     }
198
199     pRetArray = g_variant_new_array(G_VARIANT_TYPE_BYTE, pArrayElements, (gsize) i32RetVal);
200   }
201   else
202   {
203     pArrayElements    = g_new(GVariant*, 1);
204     pArrayElements[0] = g_variant_new_byte(0);
205     pRetArray = g_variant_new_array(G_VARIANT_TYPE_BYTE, pArrayElements, 1);
206   }
207
208   /* Send NSM return to test frame */
209   node_state_test_complete_get_nsm_data(pTestMachine, pInvocation, pRetArray, i32RetVal);
210
211   g_variant_unref(pRetArray); /* Release the returned data, because we send it. */
212
213   return TRUE;
214 }
215
216 /**********************************************************************************************************************
217 *
218 * The function is called when a test frame wants to get the interface version of the NSM via the NSMC.
219 *
220 * @param pTestMachine: NodeStateMachineTest object.
221 * @param pInvocation:  Invocation for this call.
222 * @param pUserData:    Opzional user data (not used).
223 *
224 * @return TRUE: Dbus message was handled.
225 *
226 **********************************************************************************************************************/
227 static gboolean NSM__boOnHandleGetNsmInterfaceVersion(NodeStateTest         *pTestMachine,
228                                                       GDBusMethodInvocation *pInvocation,
229                                                       gpointer               pUserData)
230 {
231   node_state_test_complete_get_nsm_interface_version(pTestMachine, pInvocation, (guint) NsmGetInterfaceVersion());
232
233   return TRUE;
234 }
235
236
237 /**********************************************************************************************************************
238 *
239 * Interfaces, exported functions. See header for detailed description.
240 *
241 **********************************************************************************************************************/
242
243 unsigned char NsmcInit(void)
244 {
245   unsigned char u8RetVal = 0;
246
247   TSTMSC__pTestMachine = node_state_test_skeleton_new();
248   TSTMSC__pConnection  = g_bus_get_sync(NSM_BUS_TYPE, NULL, NULL);
249
250   if(g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(TSTMSC__pTestMachine),
251                                       TSTMSC__pConnection,
252                                       "/com/contiautomotive/NodeStateMachineTest",
253                                       NULL) == TRUE)
254   {
255     u8RetVal = 1;
256     (void) g_signal_connect(TSTMSC__pTestMachine, "handle-set-nsm-data",              G_CALLBACK(NSM__boOnHandleSetNsmData),             NULL);
257     (void) g_signal_connect(TSTMSC__pTestMachine, "handle-get-nsm-data",              G_CALLBACK(NSM__boOnHandleGetNsmData),             NULL);
258     (void) g_signal_connect(TSTMSC__pTestMachine, "handle-get-nsm-interface-version", G_CALLBACK(NSM__boOnHandleGetNsmInterfaceVersion), NULL);
259   }
260   else
261   {
262     u8RetVal = 0;
263   }
264
265   return u8RetVal;
266 }
267
268
269 unsigned char NsmcLucRequired(void)
270 {
271   return 1;
272 }
273
274
275 NsmErrorStatus_e NsmcSetData(NsmDataType_e enData, unsigned char *pData, unsigned int u32DataLen)
276 {
277   return NsmErrorStatus_Ok;
278 }
279
280
281 unsigned char NsmcRequestNodeRestart(void)
282 {
283   return 1;
284 }
285
286
287 unsigned int NsmcGetInterfaceVersion(void)
288 {
289   return (unsigned int) NSMC_INTERFACE_VERSION;
290 }
291
292
293 NsmErrorStatus_e NsmcSetTestData(NsmDataType_e enData, unsigned char *pData, unsigned int u32DataLen)
294 {
295   if(   (enData                   == NsmDataType_NodeState)
296      && (u32DataLen               == sizeof(NsmNodeState_e)
297      && ((NsmNodeState_e) *pData) == NsmNodeState_Shutdown))
298   {
299     g_object_unref(TSTMSC__pConnection);
300     g_object_unref(TSTMSC__pTestMachine);
301   }
302
303   return NsmErrorStatus_Ok;
304 }
305