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.
19 #include "persistence_client_library_dbus_cmd.h"
21 #include "persistence_client_library_handle.h"
22 #include "persistence_client_library_custom_loader.h"
23 #include "persistence_client_library_pas_interface.h"
25 #include "persistence_client_library_data_organization.h"
26 #include "persistence_client_library_db_access.h"
33 void msg_pending_func(DBusPendingCall *call, void *data);
37 void process_reg_notification_signal(DBusConnection* conn)
39 char ruleChanged[DbusMatchRuleSize] = {0};
40 char ruleDeleted[DbusMatchRuleSize] = {0};
41 char ruleCreated[DbusMatchRuleSize] = {0};
43 // add match for c h a n g e
44 snprintf(ruleChanged, DbusMatchRuleSize,
45 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResChange',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
46 gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
48 // add match for d e l e t e
49 snprintf(ruleDeleted, DbusMatchRuleSize,
50 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResDelete',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
51 gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
53 // add match for c r e a t e
54 snprintf(ruleCreated, DbusMatchRuleSize,
55 "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResCreate',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
56 gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
58 if(gNotifyPolicy == Notify_register)
60 dbus_bus_add_match(conn, ruleChanged, NULL);
61 dbus_bus_add_match(conn, ruleDeleted, NULL);
62 dbus_bus_add_match(conn, ruleCreated, NULL);
64 else if(gNotifyPolicy == Notify_unregister)
66 dbus_bus_remove_match(conn, ruleChanged, NULL);
67 dbus_bus_remove_match(conn, ruleDeleted, NULL);
68 dbus_bus_remove_match(conn, ruleCreated, NULL);
71 dbus_connection_flush(conn); // flush the connection to add the match
76 void process_send_notification_signal(DBusConnection* conn)
80 const char* notifyReason = NULL;
82 char ldbidArray[DbusSubMatchSize] = {0};
83 char userArray[DbusSubMatchSize] = {0};
84 char seatArray[DbusSubMatchSize] = {0};
85 char* pldbidArra = ldbidArray;
86 char* puserArray = userArray;
87 char* pseatArray = seatArray;
88 char* pnotifyKey = gNotifykey;
92 case pclNotifyStatus_deleted:
93 notifyReason = gDeleteSignal;
95 case pclNotifyStatus_created:
96 notifyReason = gCreateSignal;
98 case pclNotifyStatus_changed:
99 notifyReason = gChangeSignal;
106 if(notifyReason != NULL)
108 // dbus_bus_add_match is used for the notification mechanism,
109 // and this works only for type DBUS_TYPE_STRING as message arguments
110 // this is the reason to use string instead of integer types directly
111 snprintf(ldbidArray, DbusSubMatchSize, "%u", gNotifyLdbid);
112 snprintf(userArray, DbusSubMatchSize, "%u", gNotifyUserNo);
113 snprintf(seatArray, DbusSubMatchSize, "%u", gNotifySeatNo);
115 //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", gNotifykey, gLdbid, gUserNo, gSeatNo, gReason);
116 message = dbus_message_new_signal("/org/genivi/persistence/adminconsumer", // const char *path,
117 "org.genivi.persistence.adminconsumer", // const char *interface,
118 notifyReason); // const char *name
120 ret = dbus_message_append_args(message, DBUS_TYPE_STRING, &pnotifyKey,
121 DBUS_TYPE_STRING, &pldbidArra,
122 DBUS_TYPE_STRING, &puserArray,
123 DBUS_TYPE_STRING, &pseatArray,
130 if(dbus_connection_send(conn, message, 0) == TRUE)
132 // Free the signal now we have finished with it
133 dbus_message_unref(message);
137 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> failed to send dbus message!!"));
142 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!!"));
147 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR dbus_message_append_args"));
152 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR invalid notification reason"));
158 void process_block_and_write_data_back(unsigned int requestID, unsigned int status)
162 // lock persistence data access
164 // sync data back to memory device
166 // send complete notification
167 //pers_admin_service_data_sync_complete(requestID, status);
172 void process_prepare_shutdown(int complete)
179 // flush open files to disk
180 for(i=0; i<MaxPersHandle; i++)
182 if(gOpenFdArray[i] == FileOpen)
187 if(complete == Shutdown_Full)
189 rval = pfcCloseFile(i);
191 else if(complete == Shutdown_Partial)
193 pfcWriteBackAndSync(i);
196 if(complete == Shutdown_Full)
200 else if(complete == Shutdown_Partial)
207 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => failed to close file: "), DLT_STRING(strerror(errno)) );
213 // close all opend rct
214 pers_rct_close_all();
216 // close opend database
217 database_close_all();
221 close_all_persistence_handle();
227 // unload custom client libraries
228 for(i=0; i<PersCustomLib_LastEntry; i++)
230 if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
232 // deinitialize plugin
233 gPersCustomFuncs[i].custom_plugin_deinit();
234 // close library handle
235 dlclose(gPersCustomFuncs[i].handle);
237 invalidate_custom_plugin(i);
245 void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int status)
248 dbus_error_init (&error);
250 DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin", // destination
251 "/org/genivi/persistence/admin", // path
252 "org.genivi.persistence.admin", // interface
253 "PersistenceAdminRequestCompleted"); // method
258 dbus_message_append_args(message, DBUS_TYPE_UINT32, &requestID,
259 DBUS_TYPE_INT32, &status,
262 if(!dbus_connection_send(conn, message, 0))
264 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
267 dbus_connection_flush(conn);
268 dbus_message_unref(message);
272 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid message") );
277 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid connection") );
282 void process_send_pas_register(DBusConnection* conn, int regType, int notificationFlag)
285 dbus_error_init (&error);
286 DBusPendingCall* pending = NULL;
291 method = "UnRegisterPersAdminNotification";
292 else if(regType == 1)
293 method = "RegisterPersAdminNotification";
297 const char* objName = "/org/genivi/persistence/adminconsumer";
298 const char* busName = dbus_bus_get_unique_name(conn);
302 DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin", // destination
303 "/org/genivi/persistence/admin", // path
304 "org.genivi.persistence.admin", // interface
309 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName, // bus name
310 DBUS_TYPE_STRING, &objName,
311 DBUS_TYPE_INT32, ¬ificationFlag,
312 DBUS_TYPE_UINT32, &gTimeoutMs,
315 dbus_connection_send_with_reply(conn, // the connection
316 message, // the message to write
318 gTimeoutMs); // timeout in milliseconds or -1 for default
320 dbus_connection_flush(conn);
322 if(!dbus_pending_call_set_notify(pending, msg_pending_func, method, NULL))
324 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_pas_register => dbus_pending_call_set_notify: FAILED\n") );
326 dbus_pending_call_unref(pending);
330 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid message") );
332 dbus_message_unref(message);
336 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid busname") );
341 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register => ERROR: Invalid connection") );
346 void process_send_lifecycle_register(DBusConnection* conn, int regType, int shutdownMode)
349 dbus_error_init (&error);
354 method = "RegisterShutdownClient";
355 else if(regType == 0)
356 method = "UnRegisterShutdownClient";
360 const char* objName = "/org/genivi/NodeStateManager/LifeCycleConsumer";
361 const char* busName = dbus_bus_get_unique_name(conn);
363 DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager", // destination
364 "/org/genivi/NodeStateManager/Consumer", // path
365 "org.genivi.NodeStateManager.Consumer", // interface
369 if(regType == 1) // register
371 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
372 DBUS_TYPE_STRING, &objName,
373 DBUS_TYPE_UINT32, &shutdownMode,
374 DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);
378 dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
379 DBUS_TYPE_STRING, &objName,
380 DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
383 if(!dbus_connection_send(conn, message, 0))
385 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
387 dbus_connection_flush(conn);
388 dbus_message_unref(message);
392 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: Invalid message"));
397 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: connection isn NULL"));
403 void process_send_lifecycle_request(DBusConnection* conn, int requestId, int status)
406 dbus_error_init (&error);
410 DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager", // destination
411 "/org/genivi/NodeStateManager/Consumer", // path
412 "org.genivi.NodeStateManager.Consumer", // interface
413 "LifecycleRequestComplete"); // method
416 dbus_message_append_args(message, DBUS_TYPE_INT32, &requestId,
417 DBUS_TYPE_INT32, &status,
421 if(!dbus_connection_send(conn, message, 0))
423 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => Access denied"), DLT_STRING(error.message) );
426 dbus_connection_flush(conn);
427 dbus_message_unref(message);
431 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: Invalid message"));
436 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: connection isn NULL"));
442 void msg_pending_func(DBusPendingCall *call, void *data)
446 dbus_error_init(&err);
450 DBusMessage *message = dbus_pending_call_steal_reply(call);
452 if (dbus_set_error_from_message(&err, message))
454 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func ==> Access denied") );
458 //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("msg_pending_func ==> UNlock mutex") );
459 dbus_message_get_args(message, &err, DBUS_TYPE_INT32, &replyArg, DBUS_TYPE_INVALID);
462 gDbusPendingRvalue = replyArg; // set the return value
463 dbus_message_unref(message);
465 // unlock the mutex because we have received the reply to the dbus message
466 pthread_mutex_unlock(&gDbusPendingRegMtx);