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);
44 void msg_pending_func(DBusPendingCall *call, void *data);
48 void process_reg_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
49 unsigned int notifySeatNo, unsigned int notifyPolicy, const char* notifyKey)
51 char ruleChanged[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
52 char ruleDeleted[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
53 char ruleCreated[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
55 // add match for c h a n g e
56 snprintf(ruleChanged, DbusMatchRuleSize,
57 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResChange',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
58 notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
60 // add match for d e l e t e
61 snprintf(ruleDeleted, DbusMatchRuleSize,
62 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResDelete',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
63 notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
65 // add match for c r e a t e
66 snprintf(ruleCreated, DbusMatchRuleSize,
67 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResCreate',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
68 notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
70 if(notifyPolicy == Notify_register)
72 dbus_bus_add_match(conn, ruleChanged, NULL);
73 dbus_bus_add_match(conn, ruleDeleted, NULL);
74 dbus_bus_add_match(conn, ruleCreated, NULL);
75 DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("Registered for change notifications:"), DLT_STRING(ruleChanged));
77 else if(notifyPolicy == Notify_unregister)
79 dbus_bus_remove_match(conn, ruleChanged, NULL);
80 dbus_bus_remove_match(conn, ruleDeleted, NULL);
81 dbus_bus_remove_match(conn, ruleCreated, NULL);
82 DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("Unregistered for change notifications:"), DLT_STRING(ruleChanged));
85 dbus_connection_flush(conn); // flush the connection to add the match
90 void process_send_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
91 unsigned int notifySeatNo, unsigned int notifyReason, const char* notifyKey)
95 const char* notifyReasonString = NULL;
97 char ldbidArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
98 char userArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
99 char seatArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
100 char* pldbidArra = ldbidArray;
101 char* puserArray = userArray;
102 char* pseatArray = seatArray;
103 char* pnotifyKey = (char*)notifyKey;
107 case pclNotifyStatus_deleted:
108 notifyReasonString = gDeleteSignal;
110 case pclNotifyStatus_created:
111 notifyReasonString = gCreateSignal;
113 case pclNotifyStatus_changed:
114 notifyReasonString = gChangeSignal;
117 notifyReasonString = NULL;
121 if(notifyReasonString != NULL)
123 // dbus_bus_add_match is used for the notification mechanism,
124 // and this works only for type DBUS_TYPE_STRING as message arguments
125 // this is the reason to use string instead of integer types directly
126 snprintf(ldbidArray, DbusSubMatchSize, "%u", notifyLdbid);
127 snprintf(userArray, DbusSubMatchSize, "%u", notifyUserNo);
128 snprintf(seatArray, DbusSubMatchSize, "%u", notifySeatNo);
130 //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", gNotifykey, gLdbid, gUserNo, gSeatNo, gReason);
131 message = dbus_message_new_signal(gPersAdminConsumerPath,
132 gDbusPersAdminConsInterface,
135 ret = dbus_message_append_args(message, DBUS_TYPE_STRING, &pnotifyKey,
136 DBUS_TYPE_STRING, &pldbidArra,
137 DBUS_TYPE_STRING, &puserArray,
138 DBUS_TYPE_STRING, &pseatArray,
145 if(dbus_connection_send(conn, message, 0) == TRUE)
147 // Free the signal now we have finished with it
148 dbus_message_unref(message);
152 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> failed to send dbus message!!"));
157 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!!"));
162 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR dbus_message_append_args"));
167 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR invalid notification reason"));
173 void process_block_and_write_data_back(unsigned int requestID, unsigned int status)
177 // lock persistence data access
179 // sync data back to memory device
184 void process_prepare_shutdown(int complete)
191 // flush open files to disk
192 for(i=0; i<MaxPersHandle; i++)
194 if(gOpenFdArray[i] == FileOpen)
198 if(complete == Shutdown_Full)
200 rval = pfcCloseFile(i);
202 else if(complete == Shutdown_Partial)
204 pfcWriteBackAndSync(i);
207 if(complete == Shutdown_Full)
211 else if(complete == Shutdown_Partial)
218 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => failed to close file: "), DLT_STRING(strerror(errno)) );
224 // close all opend rct
225 pers_rct_close_all();
227 // close opend database
228 database_close_all();
232 close_all_persistence_handle();
238 // unload custom client libraries
239 for(i=0; i<PersCustomLib_LastEntry; i++)
241 if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
243 // deinitialize plugin
244 gPersCustomFuncs[i].custom_plugin_deinit();
245 // close library handle
246 dlclose(gPersCustomFuncs[i].handle);
248 invalidate_custom_plugin(i);
256 void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int status)
259 dbus_error_init (&error);
261 DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface, // destination
262 gDbusPersAdminPath, // path
263 gDbusPersAdminInterface, // interface
264 "PersistenceAdminRequestCompleted"); // method
269 dbus_message_append_args(message, DBUS_TYPE_UINT32, &requestID,
270 DBUS_TYPE_INT32, &status,
273 if(!dbus_connection_send(conn, message, 0))
275 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
278 dbus_connection_flush(conn);
279 dbus_message_unref(message);
283 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid message") );
288 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid connection") );
293 void process_send_pas_register(DBusConnection* conn, int regType, int notificationFlag)
296 dbus_error_init (&error);
297 DBusPendingCall* pending = NULL;
302 method = "UnRegisterPersAdminNotification";
303 else if(regType == 1)
304 method = "RegisterPersAdminNotification";
308 const char* busName = dbus_bus_get_unique_name(conn);
312 DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface, // destination
313 gDbusPersAdminPath, // path
314 gDbusPersAdminInterface, // interface
319 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName, // bus name
320 DBUS_TYPE_STRING, &gPersAdminConsumerPath,
321 DBUS_TYPE_INT32, ¬ificationFlag,
322 DBUS_TYPE_UINT32, &gTimeoutMs,
325 dbus_connection_send_with_reply(conn, // the connection
326 message, // the message to write
328 gTimeoutMs); // timeout in milliseconds or -1 for default
330 dbus_connection_flush(conn);
332 if(!dbus_pending_call_set_notify(pending, msg_pending_func, method, NULL))
334 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_pas_register => dbus_pending_call_set_notify: FAILED\n") );
336 dbus_pending_call_unref(pending);
340 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid message") );
342 dbus_message_unref(message);
346 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid busname") );
351 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid connection") );
356 void process_send_lifecycle_register(DBusConnection* conn, int regType, int shutdownMode)
359 dbus_error_init (&error);
364 method = "RegisterShutdownClient";
365 else if(regType == 0)
366 method = "UnRegisterShutdownClient";
370 const char* busName = dbus_bus_get_unique_name(conn);
372 DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest, // destination
374 gDbusLcInterface, // interface
378 if(regType == 1) // register
380 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
381 DBUS_TYPE_STRING, &gDbusLcConsPath,
382 DBUS_TYPE_UINT32, &shutdownMode,
383 DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);
387 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
388 DBUS_TYPE_STRING, &gDbusLcConsPath,
389 DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
392 if(!dbus_connection_send(conn, message, 0))
394 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
396 dbus_connection_flush(conn);
397 dbus_message_unref(message);
401 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: Invalid message"));
406 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: connection isn NULL"));
412 void process_send_lifecycle_request(DBusConnection* conn, unsigned int requestId, unsigned int status)
415 dbus_error_init (&error);
419 DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest, // destination
421 gDbusLcInterface, // interface
422 "LifecycleRequestComplete"); // method
425 dbus_message_append_args(message, DBUS_TYPE_INT32, &requestId,
426 DBUS_TYPE_INT32, &status,
430 if(!dbus_connection_send(conn, message, 0))
432 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => Access denied"), DLT_STRING(error.message) );
435 dbus_connection_flush(conn);
436 dbus_message_unref(message);
440 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: Invalid message"));
445 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: connection isn NULL"));
451 void msg_pending_func(DBusPendingCall *call, void *data)
455 dbus_error_init(&err);
459 DBusMessage *message = dbus_pending_call_steal_reply(call);
461 if (dbus_set_error_from_message(&err, message))
463 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func ==> Access denied") );
467 //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("msg_pending_func ==> UNlock mutex") );
468 dbus_message_get_args(message, &err, DBUS_TYPE_INT32, &replyArg, DBUS_TYPE_INVALID);
471 gDbusPendingRvalue = replyArg; // set the return value
472 dbus_message_unref(message);
474 // unlock the mutex because we have received the reply to the dbus message
475 pthread_mutex_unlock(&gDbusPendingRegMtx);