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 pthread_mutex_t gDbusInitializedMtx = PTHREAD_MUTEX_INITIALIZER;
32 pthread_cond_t gDbusInitializedCond = PTHREAD_COND_INITIALIZER;
34 typedef enum EDBusObjectType
42 typedef struct SObjectEntry
44 tDBusObjectType objtype; /** libdbus' object */
47 DBusWatch * watch; /** watch "object" */
48 DBusTimeout * timeout; /** timeout "object" */
55 typedef struct SPollInfo
58 struct pollfd fds[10];
59 tObjectEntry objects[10];
63 /// polling information
64 static tPollInfo gPollInfo;
67 DBusConnection* gDbusConn = NULL;
70 DBusConnection* get_dbus_connection(void)
77 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
80 /* function to unregister ojbect path message handler */
81 static void unregisterMessageHandler(DBusConnection *connection, void *user_data)
83 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPath\n"));
86 /* catches messages not directed to any registered object path ("garbage collector") */
87 static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data)
89 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
91 // org.genivi.persistence.admin S I G N A L
92 if((0==strcmp("org.genivi.persistence.admin", dbus_message_get_interface(message))))
94 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
96 if((0==strcmp("PersistenceModeChanged", dbus_message_get_member(message))))
98 // to do handle signal
99 result = signal_persModeChange(connection, message);
103 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown signal:"), DLT_STRING(dbus_message_get_interface(message)) );
107 // org.genivi.persistence.admin S I G N A L
108 else if((0==strcmp("org.genivi.persistence.adminconsumer", dbus_message_get_interface(message))))
110 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
112 pclNotification_s notifyStruct;
113 int validMessage = 0;
115 if((0==strcmp("PersistenceResChange", dbus_message_get_member(message))))
117 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_changed;
120 else if((0==strcmp("PersistenceResDelete", dbus_message_get_member(message))))
122 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_deleted;
125 else if((0==strcmp("PersistenceRes", dbus_message_get_member(message))))
127 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_created;
131 if(validMessage == 1)
135 dbus_error_init (&error);
140 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING, ¬ifyStruct.resource_id,
141 DBUS_TYPE_STRING, &ldbid,
142 DBUS_TYPE_STRING, &user_no,
143 DBUS_TYPE_STRING, &seat_no,
146 reply = dbus_message_new_error(message, error.name, error.message);
150 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
153 if (!dbus_connection_send(connection, reply, 0))
155 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
158 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;;
159 dbus_message_unref(reply);
163 notifyStruct.ldbid = atoi(ldbid);
164 notifyStruct.user_no = atoi(user_no);
165 notifyStruct.seat_no = atoi(seat_no);
167 // call the registered callback function
168 gChangeNotifyCallback(¬ifyStruct);
170 result = DBUS_HANDLER_RESULT_HANDLED;
172 dbus_connection_flush(connection);
176 // org.genivi.persistence.admin P R O P E R T Y
177 else if((0==strcmp("org.freedesktop.DBus.Properties", dbus_message_get_interface(message))))
179 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
181 if((0==strcmp("EggDBusChanged", dbus_message_get_member(message))))
183 DBusMessageIter array;
184 DBusMessageIter dict;
185 DBusMessageIter variant;
187 char* dictString = NULL;
190 dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, 0, &dict);
191 dbus_message_iter_get_basic(&dict, &dictString);
193 dbus_message_iter_open_container(&dict,DBUS_TYPE_VARIANT, NULL, &variant);
194 dbus_message_iter_get_basic(&dict, &value);
196 dbus_message_iter_close_container(&dict, &variant);
197 dbus_message_iter_close_container(&array, &dict);
199 // to do handle signal
200 result = DBUS_HANDLER_RESULT_HANDLED;
204 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown property:"), DLT_STRING(dbus_message_get_interface(message)) );
209 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> not a signal:"), DLT_STRING(dbus_message_get_member(message)) );
218 static void unregisterObjectPathFallback(DBusConnection *connection, void *user_data)
220 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPathFallback\n"));
225 void* run_mainloop(void* dataPtr)
227 // persistence admin message
228 static const struct DBusObjectPathVTable vtablePersAdmin
229 = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
232 static const struct DBusObjectPathVTable vtableLifecycle
233 = {unregisterMessageHandler, checkLifecycleMsg, NULL, };
236 static const struct DBusObjectPathVTable vtableFallback
237 = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
240 mainLoop(vtablePersAdmin, vtableLifecycle, vtableFallback, dataPtr);
247 int setup_dbus_mainloop(void)
252 const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
254 // enable locking of data structures in the D-Bus library for multi threading.
255 dbus_threads_init_default();
257 dbus_error_init(&err);
259 // wain until dbus main loop has been setup and running
260 pthread_mutex_lock(&gDbusInitializedMtx);
262 // Connect to the bus and check for errors
265 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("setup_dbus_mainloop -> Use specific dbus address:"), DLT_STRING(pAddress) );
267 gDbusConn = dbus_connection_open_private(pAddress, &err);
269 if(gDbusConn != NULL)
271 if(!dbus_bus_register(gDbusConn, &err))
273 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_bus_register() Error :"), DLT_STRING(err.message) );
274 dbus_error_free (&err);
280 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_connection_open_private() Error :"), DLT_STRING(err.message) );
281 dbus_error_free(&err);
287 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("Use default dbus bus (DBUS_BUS_SYSTEM)"));
289 gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
292 // create here the dbus connection and pass to main loop
293 rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn);
296 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pthread_create( DBUS run_mainloop ) returned an error:"), DLT_INT(rval) );
300 // wait for condition variable
301 pthread_cond_wait(&gDbusInitializedCond, &gDbusInitializedMtx);
303 pthread_mutex_unlock(&gDbusInitializedMtx);
311 static dbus_bool_t addWatch(DBusWatch *watch, void *data)
313 dbus_bool_t result = FALSE;
315 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
317 int flags = dbus_watch_get_flags(watch);
319 tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
320 pEntry->objtype = OT_WATCH;
321 pEntry->watch = watch;
323 gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch);
325 if (TRUE==dbus_watch_get_enabled(watch))
327 if (flags&DBUS_WATCH_READABLE)
329 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
331 if (flags&DBUS_WATCH_WRITABLE)
333 gPollInfo.fds[gPollInfo.nfds].events |= POLLOUT;
347 static void removeWatch(DBusWatch *watch, void *data)
349 void* w_data = dbus_watch_get_data(watch);
351 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("removeWatch called "), DLT_INT( (int)watch) );
356 dbus_watch_set_data(watch, NULL, NULL);
361 static void watchToggled(DBusWatch *watch, void *data)
363 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("watchToggled called "), DLT_INT( (int)watch) );
365 if(dbus_watch_get_enabled(watch))
366 addWatch(watch, data);
368 removeWatch(watch, data);
373 static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
375 dbus_bool_t ret = FALSE;
377 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
379 const int interval = dbus_timeout_get_interval(timeout);
380 if ((0<interval)&&(TRUE==dbus_timeout_get_enabled(timeout)))
382 const int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
385 const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
386 if (-1!=timerfd_settime(tfd, 0, &its, NULL))
388 tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
389 pEntry->objtype = OT_TIMEOUT;
390 pEntry->timeout = timeout;
391 gPollInfo.fds[gPollInfo.nfds].fd = tfd;
392 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
398 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => timerfd_settime() failed"), DLT_STRING(strerror(errno)) );
403 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => timerfd_create() failed"), DLT_STRING(strerror(errno)) );
409 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => cannot create another fd to be poll()'ed"));
417 static void removeTimeout(DBusTimeout *timeout, void *data)
420 int i = gPollInfo.nfds;
421 while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
425 if (-1==close(gPollInfo.fds[i].fd))
427 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("removeTimeout => close() timerfd"), DLT_STRING(strerror(errno)) );
431 while (gPollInfo.nfds>i)
433 gPollInfo.fds[i] = gPollInfo.fds[i+1];
434 gPollInfo.objects[i] = gPollInfo.objects[i+1];
438 gPollInfo.fds[gPollInfo.nfds].fd = -1;
439 gPollInfo.objects[gPollInfo.nfds].objtype = OT_NONE;
445 /** callback for libdbus' when timeout changed */
446 static void timeoutToggled(DBusTimeout *timeout, void *data)
448 int i = gPollInfo.nfds;
449 while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
450 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("timeoutToggled") );
453 const int interval = (TRUE==dbus_timeout_get_enabled(timeout))?dbus_timeout_get_interval(timeout):0;
454 const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
455 if (-1!=timerfd_settime(gPollInfo.fds[i].fd, 0, &its, NULL))
457 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("timeoutToggled => timerfd_settime()"), DLT_STRING(strerror(errno)) );
464 int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
465 DBusObjectPathVTable vtableFallback, void* userData)
468 // lock mutex to make sure dbus main loop is running
469 pthread_mutex_lock(&gDbusInitializedMtx);
472 DBusConnection* conn = (DBusConnection*)userData;
473 dbus_error_init(&err);
475 if (dbus_error_is_set(&err))
477 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => Connection Error:"), DLT_STRING(err.message) );
478 dbus_error_free(&err);
480 else if (NULL != conn)
482 dbus_connection_set_exit_on_disconnect (conn, FALSE);
483 if (-1 == (gEfds = eventfd(0, 0)))
485 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => eventfd() failed w/ errno:"), DLT_INT(errno) );
490 memset(&gPollInfo, 0 , sizeof(gPollInfo));
493 gPollInfo.fds[0].fd = gEfds;
494 gPollInfo.fds[0].events = POLLIN;
496 dbus_bus_add_match(conn, "type='signal',interface='org.genivi.persistence.admin',member='PersistenceModeChanged',path='/org/genivi/persistence/admin'", &err);
498 // register for messages
499 if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData))
500 && (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer", &vtable2, userData))
501 && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
503 if( (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
504 || (TRUE!=dbus_connection_set_timeout_functions(conn, addTimeout, removeTimeout, timeoutToggled, NULL, NULL)) )
506 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_connection_set_watch_functions() failed"));
510 pthread_cond_signal(&gDbusInitializedCond);
511 pthread_mutex_unlock(&gDbusInitializedMtx);
514 bContinue = 0; /* assume error */
516 while (DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
518 while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, -1)))&&(EINTR==errno));
522 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => poll() failed w/ errno "), DLT_INT(errno) );
532 for (i=0; gPollInfo.nfds>i; ++i)
535 if (0!=gPollInfo.fds[i].revents)
537 if (OT_TIMEOUT==gPollInfo.objects[i].objtype)
539 /* time-out occured */
540 unsigned long long nExpCount = 0;
541 if ((ssize_t)sizeof(nExpCount)!=read(gPollInfo.fds[i].fd, &nExpCount, sizeof(nExpCount)))
543 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read failed"));
545 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => timeout"));
547 if (FALSE==dbus_timeout_handle(gPollInfo.objects[i].timeout))
549 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_timeout_handle() failed!?"));
553 else if (gPollInfo.fds[i].fd==gEfds)
555 /* internal command */
556 if (0!=(gPollInfo.fds[i].revents & POLLIN))
560 while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno));
563 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read() failed"), DLT_STRING(strerror(errno)) );
569 case CMD_PAS_BLOCK_AND_WRITE_BACK:
570 process_block_and_write_data_back((buf[2]), buf[1]);
572 case CMD_LC_PREPARE_SHUTDOWN:
573 process_prepare_shutdown((buf[2]), buf[1]);
579 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => command not handled"), DLT_INT(buf[0]) );
588 if (0!=(gPollInfo.fds[i].revents & POLLIN))
590 flags |= DBUS_WATCH_READABLE;
592 if (0!=(gPollInfo.fds[i].revents & POLLOUT))
594 flags |= DBUS_WATCH_WRITABLE;
596 if (0!=(gPollInfo.fds[i].revents & POLLERR))
598 flags |= DBUS_WATCH_ERROR;
600 if (0!=(gPollInfo.fds[i].revents & POLLHUP))
602 flags |= DBUS_WATCH_HANGUP;
605 bContinue = dbus_watch_handle(gPollInfo.objects[i].watch, flags);
611 while (0!=bContinue);
613 dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/adminconsumer");
614 dbus_connection_unregister_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer");
615 dbus_connection_unregister_object_path(conn, "/");
619 //dbus_connection_close(conn);
620 dbus_connection_unref(conn);
624 pthread_cond_signal(&gDbusInitializedCond);
625 pthread_mutex_unlock(&gDbusInitializedMtx);