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_service.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of the persistence client library dbus service.
20 #include "persistence_client_library_dbus_service.h"
21 #include "persistence_client_library_lc_interface.h"
22 #include "persistence_client_library_pas_interface.h"
23 #include "../include_protected/persistence_client_library_data_organization.h"
31 //#define DEBUG_MODE 1
33 pthread_mutex_t gDbusInitializedMtx = PTHREAD_MUTEX_INITIALIZER;
34 pthread_cond_t gDbusInitializedCond = PTHREAD_COND_INITIALIZER;
36 typedef enum EDBusObjectType
44 typedef struct SObjectEntry
46 tDBusObjectType objtype; /** libdbus' object */
49 DBusWatch * watch; /** watch "object" */
50 DBusTimeout * timeout; /** timeout "object" */
57 typedef struct SPollInfo
60 struct pollfd fds[10];
61 tObjectEntry objects[10];
65 /// polling information
66 static tPollInfo gPollInfo;
69 DBusConnection* gDbusConn = NULL;
72 DBusConnection* get_dbus_connection(void)
79 //------------------------------------------------------------------------
80 // debugging only until "correct" exit of main loop is possible!!!!!
81 //------------------------------------------------------------------------
86 void sigHandler(int signo)
91 //------------------------------------------------------------------------
95 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
98 /* function to unregister ojbect path message handler */
99 static void unregisterMessageHandler(DBusConnection *connection, void *user_data)
101 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPath\n"));
104 /* catches messages not directed to any registered object path ("garbage collector") */
105 static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data)
107 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
109 // org.genivi.persistence.admin S I G N A L
110 if((0==strcmp("org.genivi.persistence.admin", dbus_message_get_interface(message))))
112 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
114 if((0==strcmp("PersistenceModeChanged", dbus_message_get_member(message))))
116 // to do handle signal
117 result = signal_persModeChange(connection, message);
121 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown signal:"), DLT_STRING(dbus_message_get_interface(message)) );
125 // org.genivi.persistence.admin S I G N A L
126 else if((0==strcmp("org.genivi.persistence.adminconsumer", dbus_message_get_interface(message))))
128 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
130 pclNotification_s notifyStruct;
131 int validMessage = 0;
133 if((0==strcmp("PersistenceResChange", dbus_message_get_member(message))))
135 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_changed;
138 else if((0==strcmp("PersistenceResDelete", dbus_message_get_member(message))))
140 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_deleted;
143 else if((0==strcmp("PersistenceRes", dbus_message_get_member(message))))
145 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_created;
149 if(validMessage == 1)
153 dbus_error_init (&error);
158 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING, ¬ifyStruct.resource_id,
159 DBUS_TYPE_STRING, &ldbid,
160 DBUS_TYPE_STRING, &user_no,
161 DBUS_TYPE_STRING, &seat_no,
164 reply = dbus_message_new_error(message, error.name, error.message);
168 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
171 if (!dbus_connection_send(connection, reply, 0))
173 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
176 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;;
177 dbus_message_unref(reply);
181 notifyStruct.ldbid = atoi(ldbid);
182 notifyStruct.user_no = atoi(user_no);
183 notifyStruct.seat_no = atoi(seat_no);
185 // call the registered callback function
186 gChangeNotifyCallback(¬ifyStruct);
188 result = DBUS_HANDLER_RESULT_HANDLED;
190 dbus_connection_flush(connection);
194 // org.genivi.persistence.admin P R O P E R T Y
195 else if((0==strcmp("org.freedesktop.DBus.Properties", dbus_message_get_interface(message))))
197 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
199 if((0==strcmp("EggDBusChanged", dbus_message_get_member(message))))
201 DBusMessageIter array;
202 DBusMessageIter dict;
203 DBusMessageIter variant;
205 char* dictString = NULL;
208 dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, 0, &dict);
209 dbus_message_iter_get_basic(&dict, &dictString);
211 dbus_message_iter_open_container(&dict,DBUS_TYPE_VARIANT, NULL, &variant);
212 dbus_message_iter_get_basic(&dict, &value);
214 dbus_message_iter_close_container(&dict, &variant);
215 dbus_message_iter_close_container(&array, &dict);
217 // to do handle signal
218 result = DBUS_HANDLER_RESULT_HANDLED;
222 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown property:"), DLT_STRING(dbus_message_get_interface(message)) );
227 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> not a signal:"), DLT_STRING(dbus_message_get_member(message)) );
236 static void unregisterObjectPathFallback(DBusConnection *connection, void *user_data)
238 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPathFallback\n"));
243 void* run_mainloop(void* dataPtr)
245 // persistence admin message
246 static const struct DBusObjectPathVTable vtablePersAdmin
247 = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
250 static const struct DBusObjectPathVTable vtableLifecycle
251 = {unregisterMessageHandler, checkLifecycleMsg, NULL, };
254 static const struct DBusObjectPathVTable vtableFallback
255 = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
258 mainLoop(vtablePersAdmin, vtableLifecycle, vtableFallback, dataPtr);
265 int setup_dbus_mainloop(void)
270 const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
272 // enable locking of data structures in the D-Bus library for multi threading.
273 dbus_threads_init_default();
275 dbus_error_init(&err);
277 // wain until dbus main loop has been setup and running
278 pthread_mutex_lock(&gDbusInitializedMtx);
280 // Connect to the bus and check for errors
283 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("setup_dbus_mainloop -> Use specific dbus address:"), DLT_STRING(pAddress) );
285 gDbusConn = dbus_connection_open(pAddress, &err);
287 if(gDbusConn != NULL)
289 if(!dbus_bus_register(gDbusConn, &err))
291 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_bus_register() Error :"), DLT_STRING(err.message) );
292 dbus_error_free (&err);
298 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_connection_open() Error :"), DLT_STRING(err.message) );
299 dbus_error_free(&err);
305 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("Use default dbus bus (DBUS_BUS_SYSTEM)"));
307 gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
310 // create here the dbus connection and pass to main loop
311 rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn);
314 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pthread_create( DBUS run_mainloop ) returned an error:"), DLT_INT(rval) );
318 // wait for condition variable
319 pthread_cond_wait(&gDbusInitializedCond, &gDbusInitializedMtx);
321 pthread_mutex_unlock(&gDbusInitializedMtx);
329 static dbus_bool_t addWatch(DBusWatch *watch, void *data)
331 dbus_bool_t result = FALSE;
333 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
335 int flags = dbus_watch_get_flags(watch);
337 tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
338 pEntry->objtype = OT_WATCH;
339 pEntry->watch = watch;
341 gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch);
343 if (TRUE==dbus_watch_get_enabled(watch))
345 if (flags&DBUS_WATCH_READABLE)
347 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
349 if (flags&DBUS_WATCH_WRITABLE)
351 gPollInfo.fds[gPollInfo.nfds].events |= POLLOUT;
365 static void removeWatch(DBusWatch *watch, void *data)
367 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("removeWatch called "), DLT_INT( (int)watch) );
372 static void watchToggled(DBusWatch *watch, void *data)
374 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("watchToggled called "), DLT_INT( (int)watch) );
379 static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
381 dbus_bool_t ret = FALSE;
383 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
385 const int interval = dbus_timeout_get_interval(timeout);
386 if ((0<interval)&&(TRUE==dbus_timeout_get_enabled(timeout)))
388 const int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
391 const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
392 if (-1!=timerfd_settime(tfd, 0, &its, NULL))
394 tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
395 pEntry->objtype = OT_TIMEOUT;
396 pEntry->timeout = timeout;
397 gPollInfo.fds[gPollInfo.nfds].fd = tfd;
398 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
404 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => timerfd_settime() failed"), DLT_STRING(strerror(errno)) );
409 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => timerfd_create() failed"), DLT_STRING(strerror(errno)) );
415 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => cannot create another fd to be poll()'ed"));
423 static void removeTimeout(DBusTimeout *timeout, void *data)
426 int i = gPollInfo.nfds;
427 while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
431 if (-1==close(gPollInfo.fds[i].fd))
433 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("removeTimeout => close() timerfd"), DLT_STRING(strerror(errno)) );
437 while (gPollInfo.nfds>i)
439 gPollInfo.fds[i] = gPollInfo.fds[i+1];
440 gPollInfo.objects[i] = gPollInfo.objects[i+1];
444 gPollInfo.fds[gPollInfo.nfds].fd = -1;
445 gPollInfo.objects[gPollInfo.nfds].objtype = OT_NONE;
451 /** callback for libdbus' when timeout changed */
452 static void timeoutToggled(DBusTimeout *timeout, void *data)
454 int i = gPollInfo.nfds;
455 while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
456 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("timeoutToggled") );
459 const int interval = (TRUE==dbus_timeout_get_enabled(timeout))?dbus_timeout_get_interval(timeout):0;
460 const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
461 if (-1!=timerfd_settime(gPollInfo.fds[i].fd, 0, &its, NULL))
463 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("timeoutToggled => timerfd_settime()"), DLT_STRING(strerror(errno)) );
470 int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
471 DBusObjectPathVTable vtableFallback, void* userData)
474 // lock mutex to make sure dbus main loop is running
475 pthread_mutex_lock(&gDbusInitializedMtx);
478 signal(SIGINT, sigHandler);
482 DBusConnection* conn = (DBusConnection*)userData;
483 dbus_error_init(&err);
485 if (dbus_error_is_set(&err))
487 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => Connection Error:"), DLT_STRING(err.message) );
488 dbus_error_free(&err);
490 else if (NULL != conn)
492 dbus_connection_set_exit_on_disconnect (conn, FALSE);
493 if (-1 == (gEfds = eventfd(0, 0)))
495 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => eventfd() failed w/ errno:"), DLT_INT(errno) );
500 memset(&gPollInfo, 0 , sizeof(gPollInfo));
503 gPollInfo.fds[0].fd = gEfds;
504 gPollInfo.fds[0].events = POLLIN;
506 dbus_bus_add_match(conn, "type='signal',interface='org.genivi.persistence.admin',member='PersistenceModeChanged',path='/org/genivi/persistence/admin'", &err);
508 // register for messages
509 if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData))
510 && (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer", &vtable2, userData))
511 && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
513 if( (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
514 || (TRUE!=dbus_connection_set_timeout_functions(conn, addTimeout, removeTimeout, timeoutToggled, NULL, NULL)) )
516 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_connection_set_watch_functions() failed"));
520 pthread_cond_signal(&gDbusInitializedCond);
521 pthread_mutex_unlock(&gDbusInitializedMtx);
524 bContinue = 0; /* assume error */
526 while (DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
528 while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, -1)))&&(EINTR==errno));
532 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => poll() failed w/ errno "), DLT_INT(errno) );
542 for (i=0; gPollInfo.nfds>i; ++i)
545 if (0!=gPollInfo.fds[i].revents)
547 if (OT_TIMEOUT==gPollInfo.objects[i].objtype)
549 /* time-out occured */
550 unsigned long long nExpCount = 0;
551 if ((ssize_t)sizeof(nExpCount)!=read(gPollInfo.fds[i].fd, &nExpCount, sizeof(nExpCount)))
553 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read failed"));
555 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => timeout"));
557 if (FALSE==dbus_timeout_handle(gPollInfo.objects[i].timeout))
559 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_timeout_handle() failed!?"));
563 else if (gPollInfo.fds[i].fd==gEfds)
565 /* internal command */
566 if (0!=(gPollInfo.fds[i].revents & POLLIN))
570 while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno));
573 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read() failed"), DLT_STRING(strerror(errno)) );
579 case CMD_PAS_BLOCK_AND_WRITE_BACK:
580 process_block_and_write_data_back((buf[2]), buf[1]);
582 case CMD_LC_PREPARE_SHUTDOWN:
583 process_prepare_shutdown((buf[2]), buf[1]);
589 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => command not handled"), DLT_INT(buf[0]) );
598 if (0!=(gPollInfo.fds[i].revents & POLLIN))
600 flags |= DBUS_WATCH_READABLE;
602 if (0!=(gPollInfo.fds[i].revents & POLLOUT))
604 flags |= DBUS_WATCH_WRITABLE;
606 if (0!=(gPollInfo.fds[i].revents & POLLERR))
608 flags |= DBUS_WATCH_ERROR;
610 if (0!=(gPollInfo.fds[i].revents & POLLHUP))
612 flags |= DBUS_WATCH_HANGUP;
615 bContinue = dbus_watch_handle(gPollInfo.objects[i].watch, flags);
621 while (0!=bContinue);
623 dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/adminconsumer");
624 dbus_connection_unregister_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer");
625 dbus_connection_unregister_object_path(conn, "/");
629 dbus_connection_unref(conn);
633 pthread_cond_signal(&gDbusInitializedCond);
634 pthread_mutex_unlock(&gDbusInitializedMtx);