1 /**********************************************************************************************************************
3 * Copyright (C) 2012 Continental Automotive Systems, Inc.
5 * Author: Jean-Pierre.Bogler@continental-corporation.com
7 * Implementation of the NodeStateMachineTest.
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!
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.
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/.
23 * 24.01.2013 Jean-Pierre Bogler CSP_WZ#1194: Initial creation.
25 **********************************************************************************************************************/
28 /**********************************************************************************************************************
32 **********************************************************************************************************************/
34 #include <gio/gio.h> /* Access dbus using glib */
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 */
40 #include "NodeStateMachineTestApi.h" /* Dbus interface offered by NSMC */
43 /**********************************************************************************************************************
45 * Local defines, macros, constants and type definitions.
47 **********************************************************************************************************************/
49 /* There are currently no local defines, macros or types */
52 /**********************************************************************************************************************
56 **********************************************************************************************************************/
58 static NodeStateTest *TSTMSC__pTestMachine = NULL;
59 static GDBusConnection *TSTMSC__pConnection = NULL;
62 /**********************************************************************************************************************
64 * Prototypes for file local functions (see implementation for description)
66 **********************************************************************************************************************/
68 static gboolean NSM__boOnHandleSetNsmData(NodeStateTest *pTestMachine,
69 GDBusMethodInvocation *pInvocation,
70 const gint i32DataType,
72 const guint u32DataLen,
75 static gboolean NSM__boOnHandleGetNsmData(NodeStateTest *pTestMachine,
76 GDBusMethodInvocation *pInvocation,
77 const gint i32DataType,
79 const guint u32DataLen,
82 static gboolean NSM__boOnHandleGetNsmInterfaceVersion(NodeStateTest *pTestMachine,
83 GDBusMethodInvocation *pInvocation,
86 /**********************************************************************************************************************
88 * Local (static) functions
90 **********************************************************************************************************************/
92 /**********************************************************************************************************************
94 * The function is called when a test frame wants to set data to the NSM via the NSMC.
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).
104 * @return TRUE: Dbus message was handled.
106 **********************************************************************************************************************/
107 static gboolean NSM__boOnHandleSetNsmData(NodeStateTest *pTestMachine,
108 GDBusMethodInvocation *pInvocation,
109 const gint i32DataType,
111 const guint u32DataLen,
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;
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);
126 for(u32ArrayIdx = 0; u32ArrayIdx < u32ArraySize; u32ArrayIdx++)
128 pArrayElement = g_variant_get_child_value(pData, u32ArrayIdx);
129 au8Data[u32ArrayIdx] = g_variant_get_byte(pArrayElement);
130 g_variant_unref(pArrayElement);
133 g_variant_unref(pData); /* release the variant. We read it */
135 /* Call the NSM. Pass extracted data and length, originally passed by test frame */
136 enRetVal = NsmSetData(enDateType, (unsigned char*) au8Data, (unsigned int) u32DataLen);
138 /* Send NSMs return value to the test frame */
139 node_state_test_complete_set_nsm_data(pTestMachine, pInvocation, (gint) enRetVal);
144 /**********************************************************************************************************************
146 * The function is called when a test frame wants to get data from the NSM via the NSMC.
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).
156 * @return TRUE: Dbus message was handled.
158 **********************************************************************************************************************/
159 static gboolean NSM__boOnHandleGetNsmData(NodeStateTest *pTestMachine,
160 GDBusMethodInvocation *pInvocation,
161 const gint i32DataType,
163 const guint u32DataLen,
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 */
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.
178 for(u32ArrayIdx = 0; u32ArrayIdx < g_variant_n_children(DataIn); u32ArrayIdx++)
180 pDataInElement = g_variant_get_child_value(DataIn, u32ArrayIdx);
181 ((guchar*) &pData)[u32ArrayIdx] = g_variant_get_byte(pDataInElement);
184 g_variant_unref(DataIn); /* Release pDataIn. we read the data */
187 i32RetVal = NsmGetData((NsmDataType_e) i32DataType, (unsigned char*) &pData, u32DataLen);
189 /* Serialize ougoing data retuurned by NSM, if there is a positive NSM return */
192 pArrayElements = g_new(GVariant*, i32RetVal);
194 for(u32ArrayIdx = 0; u32ArrayIdx < (guint) i32RetVal; u32ArrayIdx++)
196 pArrayElements[u32ArrayIdx] = g_variant_new_byte( ((guchar*) &pData)[u32ArrayIdx] );
199 pRetArray = g_variant_new_array(G_VARIANT_TYPE_BYTE, pArrayElements, (gsize) i32RetVal);
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);
208 /* Send NSM return to test frame */
209 node_state_test_complete_get_nsm_data(pTestMachine, pInvocation, pRetArray, i32RetVal);
211 g_variant_unref(pRetArray); /* Release the returned data, because we send it. */
216 /**********************************************************************************************************************
218 * The function is called when a test frame wants to get the interface version of the NSM via the NSMC.
220 * @param pTestMachine: NodeStateMachineTest object.
221 * @param pInvocation: Invocation for this call.
222 * @param pUserData: Opzional user data (not used).
224 * @return TRUE: Dbus message was handled.
226 **********************************************************************************************************************/
227 static gboolean NSM__boOnHandleGetNsmInterfaceVersion(NodeStateTest *pTestMachine,
228 GDBusMethodInvocation *pInvocation,
231 node_state_test_complete_get_nsm_interface_version(pTestMachine, pInvocation, (guint) NsmGetInterfaceVersion());
237 /**********************************************************************************************************************
239 * Interfaces, exported functions. See header for detailed description.
241 **********************************************************************************************************************/
243 unsigned char NsmcInit(void)
245 unsigned char u8RetVal = 0;
247 TSTMSC__pTestMachine = node_state_test_skeleton_new();
248 TSTMSC__pConnection = g_bus_get_sync(NSM_BUS_TYPE, NULL, NULL);
250 if(g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(TSTMSC__pTestMachine),
252 "/com/contiautomotive/NodeStateMachineTest",
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);
269 unsigned char NsmcLucRequired(void)
275 NsmErrorStatus_e NsmcSetData(NsmDataType_e enData, unsigned char *pData, unsigned int u32DataLen)
277 return NsmErrorStatus_Ok;
281 unsigned char NsmcRequestNodeRestart(void)
287 unsigned int NsmcGetInterfaceVersion(void)
289 return (unsigned int) NSMC_INTERFACE_VERSION;
293 NsmErrorStatus_e NsmcSetTestData(NsmDataType_e enData, unsigned char *pData, unsigned int u32DataLen)
295 if( (enData == NsmDataType_NodeState)
296 && (u32DataLen == sizeof(NsmNodeState_e)
297 && ((NsmNodeState_e) *pData) == NsmNodeState_Shutdown))
299 g_object_unref(TSTMSC__pConnection);
300 g_object_unref(TSTMSC__pTestMachine);
303 return NsmErrorStatus_Ok;