1 /******************************************************************************
4 * Company XS Embedded GmbH
5 *****************************************************************************/
6 /******************************************************************************
7 * This Source Code Form is subject to the terms of the
8 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
9 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
12 * @file persistence_client_library_dbus_cmd.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of the persistence client library dbus commands.
20 #include <dlfcn.h> /* For dlclose() */
21 #include "persistence_client_library_dbus_cmd.h"
23 #include "persistence_client_library_handle.h"
24 #include "persistence_client_library_custom_loader.h"
25 #include "persistence_client_library_prct_access.h"
26 #include "persistence_client_library_pas_interface.h"
27 #include "persistence_client_library_data_organization.h"
28 #include "persistence_client_library_db_access.h"
31 #include <persistence_file_cache.h>
35 * write back from cache to non volatile memory device
37 * THIS FUNCTION IS NOT INTENDED TO BE USED BY A NORMAL APPLICATION.
38 * ONLY SPECIAL APPLICATION ARE ALLOWED TO USING USE THIS FUNCTION
40 extern int pfcWriteBackAndSync(int handle);
45 void msg_pending_func(DBusPendingCall *call, void *data);
49 void process_reg_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
50 unsigned int notifySeatNo, unsigned int notifyPolicy, const char* notifyKey)
52 char ruleChanged[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
53 char ruleDeleted[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
54 char ruleCreated[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
56 // add match for c h a n g e
57 snprintf(ruleChanged, DbusMatchRuleSize,
58 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResChange',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
59 notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
61 // add match for d e l e t e
62 snprintf(ruleDeleted, DbusMatchRuleSize,
63 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResDelete',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
64 notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
66 // add match for c r e a t e
67 snprintf(ruleCreated, DbusMatchRuleSize,
68 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResCreate',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
69 notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
71 if(notifyPolicy == Notify_register)
73 dbus_bus_add_match(conn, ruleChanged, NULL);
74 dbus_bus_add_match(conn, ruleDeleted, NULL);
75 dbus_bus_add_match(conn, ruleCreated, NULL);
76 DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("Registered for change notifications:"), DLT_STRING(ruleChanged));
78 else if(notifyPolicy == Notify_unregister)
80 dbus_bus_remove_match(conn, ruleChanged, NULL);
81 dbus_bus_remove_match(conn, ruleDeleted, NULL);
82 dbus_bus_remove_match(conn, ruleCreated, NULL);
83 DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("Unregistered for change notifications:"), DLT_STRING(ruleChanged));
86 dbus_connection_flush(conn); // flush the connection to add the match
91 void process_send_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
92 unsigned int notifySeatNo, unsigned int notifyReason, const char* notifyKey)
96 const char* notifyReasonString = NULL;
98 char ldbidArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
99 char userArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
100 char seatArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
101 char* pldbidArra = ldbidArray;
102 char* puserArray = userArray;
103 char* pseatArray = seatArray;
104 char* pnotifyKey = (char*)notifyKey;
108 case pclNotifyStatus_deleted:
109 notifyReasonString = gDeleteSignal;
111 case pclNotifyStatus_created:
112 notifyReasonString = gCreateSignal;
114 case pclNotifyStatus_changed:
115 notifyReasonString = gChangeSignal;
118 notifyReasonString = NULL;
122 if(notifyReasonString != NULL)
124 // dbus_bus_add_match is used for the notification mechanism,
125 // and this works only for type DBUS_TYPE_STRING as message arguments
126 // this is the reason to use string instead of integer types directly
127 snprintf(ldbidArray, DbusSubMatchSize, "%u", notifyLdbid);
128 snprintf(userArray, DbusSubMatchSize, "%u", notifyUserNo);
129 snprintf(seatArray, DbusSubMatchSize, "%u", notifySeatNo);
131 //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", gNotifykey, gLdbid, gUserNo, gSeatNo, gReason);
132 message = dbus_message_new_signal(gPersAdminConsumerPath,
133 gDbusPersAdminConsInterface,
136 ret = dbus_message_append_args(message, DBUS_TYPE_STRING, &pnotifyKey,
137 DBUS_TYPE_STRING, &pldbidArra,
138 DBUS_TYPE_STRING, &puserArray,
139 DBUS_TYPE_STRING, &pseatArray,
146 if(dbus_connection_send(conn, message, 0) == TRUE)
148 // Free the signal now we have finished with it
149 dbus_message_unref(message);
153 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> failed to send dbus message!!"));
158 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> E R R O R C O N E C T I O N NULL!!"));
163 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR dbus_message_append_args"));
168 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR invalid notification reason"));
174 void process_block_and_write_data_back(unsigned int requestID, unsigned int status)
178 // lock persistence data access
180 // sync data back to memory device
185 void process_prepare_shutdown(int complete)
192 // flush open files to disk
193 for(i=0; i<MaxPersHandle; i++)
195 if(gOpenFdArray[i] == FileOpen)
199 if(complete == Shutdown_Full)
201 rval = pfcCloseFile(i);
203 else if(complete == Shutdown_Partial)
205 pfcWriteBackAndSync(i);
208 if(complete == Shutdown_Full)
212 else if(complete == Shutdown_Partial)
219 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => failed to close file: "), DLT_STRING(strerror(errno)) );
225 // close all opend rct
226 pers_rct_close_all();
228 // close opend database
229 database_close_all();
233 close_all_persistence_handle();
239 // unload custom client libraries
240 for(i=0; i<PersCustomLib_LastEntry; i++)
242 if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
244 // deinitialize plugin
245 gPersCustomFuncs[i].custom_plugin_deinit();
246 // close library handle
247 dlclose(gPersCustomFuncs[i].handle);
249 invalidate_custom_plugin(i);
257 void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int status)
260 dbus_error_init (&error);
262 DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface, // destination
263 gDbusPersAdminPath, // path
264 gDbusPersAdminInterface, // interface
265 "PersistenceAdminRequestCompleted"); // method
270 dbus_message_append_args(message, DBUS_TYPE_UINT32, &requestID,
271 DBUS_TYPE_INT32, &status,
274 if(!dbus_connection_send(conn, message, 0))
276 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
279 dbus_connection_flush(conn);
280 dbus_message_unref(message);
284 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid message") );
289 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid connection") );
294 void process_send_pas_register(DBusConnection* conn, int regType, int notificationFlag)
297 dbus_error_init (&error);
298 DBusPendingCall* pending = NULL;
303 method = "UnRegisterPersAdminNotification";
304 else if(regType == 1)
305 method = "RegisterPersAdminNotification";
309 const char* busName = dbus_bus_get_unique_name(conn);
313 DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface, // destination
314 gDbusPersAdminPath, // path
315 gDbusPersAdminInterface, // interface
320 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName, // bus name
321 DBUS_TYPE_STRING, &gPersAdminConsumerPath,
322 DBUS_TYPE_INT32, ¬ificationFlag,
323 DBUS_TYPE_UINT32, &gTimeoutMs,
326 dbus_connection_send_with_reply(conn, // the connection
327 message, // the message to write
329 gTimeoutMs); // timeout in milliseconds or -1 for default
331 dbus_connection_flush(conn);
333 if(!dbus_pending_call_set_notify(pending, msg_pending_func, method, NULL))
335 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_pas_register => dbus_pending_call_set_notify: FAILED\n") );
337 dbus_pending_call_unref(pending);
341 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid message") );
343 dbus_message_unref(message);
347 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid busname") );
352 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid connection") );
357 void process_send_lifecycle_register(DBusConnection* conn, int regType, int shutdownMode)
360 dbus_error_init (&error);
365 method = "RegisterShutdownClient";
366 else if(regType == 0)
367 method = "UnRegisterShutdownClient";
371 const char* busName = dbus_bus_get_unique_name(conn);
373 DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest, // destination
375 gDbusLcInterface, // interface
379 if(regType == 1) // register
381 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
382 DBUS_TYPE_STRING, &gDbusLcConsPath,
383 DBUS_TYPE_UINT32, &shutdownMode,
384 DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);
388 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
389 DBUS_TYPE_STRING, &gDbusLcConsPath,
390 DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
393 if(!dbus_connection_send(conn, message, 0))
395 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
397 dbus_connection_flush(conn);
398 dbus_message_unref(message);
402 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: Invalid message"));
407 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: connection isn NULL"));
413 void process_send_lifecycle_request(DBusConnection* conn, unsigned int requestId, unsigned int status)
416 dbus_error_init (&error);
420 DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest, // destination
422 gDbusLcInterface, // interface
423 "LifecycleRequestComplete"); // method
426 dbus_message_append_args(message, DBUS_TYPE_INT32, &requestId,
427 DBUS_TYPE_INT32, &status,
431 if(!dbus_connection_send(conn, message, 0))
433 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => Access denied"), DLT_STRING(error.message) );
436 dbus_connection_flush(conn);
437 dbus_message_unref(message);
441 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: Invalid message"));
446 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: connection isn NULL"));
452 void msg_pending_func(DBusPendingCall *call, void *data)
456 dbus_error_init(&err);
460 DBusMessage *message = dbus_pending_call_steal_reply(call);
462 if (dbus_set_error_from_message(&err, message))
464 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func ==> Access denied") );
468 //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("msg_pending_func ==> UNlock mutex") );
469 dbus_message_get_args(message, &err, DBUS_TYPE_INT32, &replyArg, DBUS_TYPE_INVALID);
472 gDbusPendingRvalue = replyArg; // set the return value
473 dbus_message_unref(message);
475 // unlock the mutex because we have received the reply to the dbus message
476 pthread_mutex_unlock(&gDbusPendingRegMtx);