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"
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;
68 DBusConnection* gDbusConn = NULL;
71 DBusConnection* get_dbus_connection(void)
76 //------------------------------------------------------------------------
77 // debugging only until "correct" exit of main loop is possible!!!!!
78 //------------------------------------------------------------------------
80 static int endLoop = 0;
82 void sigHandler(int signo)
86 //------------------------------------------------------------------------
90 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
93 /* function to unregister ojbect path message handler */
94 static void unregisterMessageHandler(DBusConnection *connection, void *user_data)
96 printf("unregisterObjectPath\n");
99 /* catches messages not directed to any registered object path ("garbage collector") */
100 static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data)
102 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
104 printf("handleObjectPathMessageFallback Object: '%s' -> Interface: '%s' -> Message: '%s'\n",
105 dbus_message_get_sender(message), dbus_message_get_interface(message), dbus_message_get_member(message) );
107 // org.genivi.persistence.admin S I G N A L
108 if((0==strcmp("org.genivi.persistence.admin", dbus_message_get_interface(message))))
110 // printf("checkPersAdminSignalInterface '%s' -> '%s'\n", dbus_message_get_interface(message), dbus_message_get_member(message));
111 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
113 printf(" checkPersAdminSignal signal\n");
114 if((0==strcmp("PersistenceModeChanged", dbus_message_get_member(message))))
116 printf(" checkPersAdminSignal message\n");
117 // to do handle signal
118 result = signal_persModeChange(connection, message);
122 printf("handleObjectPathMessageFallback -> unknown signal '%s'\n", dbus_message_get_interface(message));
127 // org.genivi.persistence.admin P R O P E R T Y
128 else if((0==strcmp("org.freedesktop.DBus.Properties", dbus_message_get_interface(message))))
130 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
132 if((0==strcmp("EggDBusChanged", dbus_message_get_member(message))))
134 DBusMessageIter array;
135 DBusMessageIter dict;
136 DBusMessageIter variant;
138 char* dictString = NULL;
141 dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, 0, &dict);
142 dbus_message_iter_get_basic(&dict, &dictString);
144 dbus_message_iter_open_container(&dict,DBUS_TYPE_VARIANT, NULL, &variant);
145 dbus_message_iter_get_basic(&dict, &value);
147 dbus_message_iter_close_container(&dict, &variant);
148 dbus_message_iter_close_container(&array, &dict);
150 printf("handleObjectPathMessageFallback ==> value: %d \n", value);
151 // to do handle signal
152 result = DBUS_HANDLER_RESULT_HANDLED;
156 printf("handleObjectPathMessageFallback -> unknown property '%s'\n", dbus_message_get_interface(message));
161 printf("handleObjectPathMessageFallback -> not a signal '%s'\n", dbus_message_get_member(message));
170 static void unregisterObjectPathFallback(DBusConnection *connection, void *user_data)
172 printf("unregisterObjectPathFallback\n");
177 void* run_mainloop(void* dataPtr)
179 // persistence admin message
180 static const struct DBusObjectPathVTable vtablePersAdmin
181 = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
184 static const struct DBusObjectPathVTable vtableLifecycle
185 = {unregisterMessageHandler, checkLifecycleMsg, NULL, };
188 static const struct DBusObjectPathVTable vtableFallback
189 = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
192 mainLoop(vtablePersAdmin, vtableLifecycle, vtableFallback, dataPtr);
194 printf("Exit dbus main loop!!!!\n");
201 int setup_dbus_mainloop(void)
206 const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
208 // enable locking of data structures in the D-Bus library for multi threading.
209 dbus_threads_init_default();
211 dbus_error_init(&err);
213 // Connect to the bus and check for errors
216 printf("Use specific dbus address: %s\n !", pAddress);
217 gDbusConn = dbus_connection_open(pAddress, &err);
219 if(gDbusConn != NULL)
221 if(!dbus_bus_register(gDbusConn, &err))
223 printf("dbus_bus_register() Error %s\n", err.message);
224 dbus_error_free (&err);
229 printf("Registered connection successfully !\n");
234 printf("dbus_connection_open() Error %s\n",err.message);
235 dbus_error_free(&err);
240 printf("Use default dbus bus (DBUS_BUS_SYSTEM) !!!!!!\n");
241 gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
244 // wain until dbus main loop has been setup and running
245 pthread_mutex_lock(&gDbusInitializedMtx);
247 // create here the dbus connection and pass to main loop
248 rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn);
251 fprintf(stderr, "Server: - ERROR! pthread_create( run_mainloop ) returned: %d\n", rval);
254 // wait for condition variable
255 pthread_cond_wait(&gDbusInitializedCond, &gDbusInitializedMtx);
257 pthread_mutex_unlock(&gDbusInitializedMtx);
265 static dbus_bool_t addWatch(DBusWatch *watch, void *data)
267 dbus_bool_t result = FALSE;
269 //fprintf(stderr, "addWatch called @%08x flags: %08x enabled: %c\n", (unsigned int)watch, dbus_watch_get_flags(watch), TRUE==dbus_watch_get_enabled(watch)?'x':'-');
271 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
273 int flags = dbus_watch_get_flags(watch);
275 tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
276 pEntry->objtype = OT_WATCH;
277 pEntry->watch = watch;
279 gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch);
281 if (TRUE==dbus_watch_get_enabled(watch))
283 if (flags&DBUS_WATCH_READABLE)
285 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
287 if (flags&DBUS_WATCH_WRITABLE)
289 gPollInfo.fds[gPollInfo.nfds].events |= POLLOUT;
303 static void removeWatch(DBusWatch *watch, void *data)
305 printf("removeWatch called @0x%08x\n", (int)watch);
310 static void watchToggled(DBusWatch *watch, void *data)
312 printf("watchToggled called @0x%08x\n", (int)watch);
317 static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
319 dbus_bool_t ret = FALSE;
321 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
323 const int interval = dbus_timeout_get_interval(timeout);
324 if ((0<interval)&&(TRUE==dbus_timeout_get_enabled(timeout)))
326 const int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
329 const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
330 if (-1!=timerfd_settime(tfd, 0, &its, NULL))
332 tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
333 pEntry->objtype = OT_TIMEOUT;
334 pEntry->timeout = timeout;
335 gPollInfo.fds[gPollInfo.nfds].fd = tfd;
336 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
342 fprintf(stderr, "timerfd_settime() failed %d '%s'\n", errno, strerror(errno));
347 fprintf(stderr, "timerfd_create() failed %d '%s'\n", errno, strerror(errno));
353 fprintf(stderr, "cannot create another fd to be poll()'ed\n");
361 static void removeTimeout(DBusTimeout *timeout, void *data)
364 int i = gPollInfo.nfds;
365 while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
369 if (-1==close(gPollInfo.fds[i].fd))
371 fprintf(stderr, "close() timerfd #%d failed %d '%s'\n", gPollInfo.fds[i].fd, errno, strerror(errno));
375 while (gPollInfo.nfds>i)
377 gPollInfo.fds[i] = gPollInfo.fds[i+1];
378 gPollInfo.objects[i] = gPollInfo.objects[i+1];
382 gPollInfo.fds[gPollInfo.nfds].fd = -1;
383 gPollInfo.objects[gPollInfo.nfds].objtype = OT_NONE;
389 /** callback for libdbus' when timeout changed */
390 static void timeoutToggled(DBusTimeout *timeout, void *data)
392 int i = gPollInfo.nfds;
393 while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
394 fprintf(stderr, "timeoutToggled @%x %c %dms @%d [%d]\n", (int)timeout, dbus_timeout_get_enabled(timeout)?'x':'-', dbus_timeout_get_interval(timeout), i, gPollInfo.nfds);
397 const int interval = (TRUE==dbus_timeout_get_enabled(timeout))?dbus_timeout_get_interval(timeout):0;
398 const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
399 if (-1!=timerfd_settime(gPollInfo.fds[i].fd, 0, &its, NULL))
401 fprintf(stderr, "timerfd_settime() %d failed %d '%s'\n", interval, errno, strerror(errno));
408 int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
409 DBusObjectPathVTable vtableFallback, void* userData)
412 // lock mutex to make sure dbus main loop is running
413 pthread_mutex_lock(&gDbusInitializedMtx);
415 signal(SIGTERM, sigHandler);
416 signal(SIGQUIT, sigHandler);
417 signal(SIGINT, sigHandler);
419 DBusConnection* conn = (DBusConnection*)userData;
420 dbus_error_init(&err);
422 if (dbus_error_is_set(&err))
424 printf("Connection Error (%s)\n", err.message);
425 dbus_error_free(&err);
427 else if (NULL != conn)
429 dbus_connection_set_exit_on_disconnect (conn, FALSE);
430 //printf("connected as '%s'\n", dbus_bus_get_unique_name(conn));
431 if (-1 == (gEfds = eventfd(0, 0)))
433 printf("eventfd() failed w/ errno %d\n", errno);
439 memset(&gPollInfo, 0 , sizeof(gPollInfo));
442 gPollInfo.fds[0].fd = gEfds;
443 gPollInfo.fds[0].events = POLLIN;
445 dbus_bus_add_match(conn, "type='signal',interface='org.genivi.persistence.admin',member='PersistenceModeChanged',path='/org/genivi/persistence/admin'", &err);
447 // register for messages
448 if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData))
449 && (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer", &vtable2, userData))
450 && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
452 if( (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
453 || (TRUE!=dbus_connection_set_timeout_functions(conn, addTimeout, removeTimeout, timeoutToggled, NULL, NULL)) )
455 printf("dbus_connection_set_watch_functions() failed\n");
459 pthread_cond_signal(&gDbusInitializedCond);
460 pthread_mutex_unlock(&gDbusInitializedMtx);
463 bContinue = 0; /* assume error */
465 while (DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
467 while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, -1)))&&(EINTR==errno));
471 fprintf(stderr, "poll() failed w/ errno %d\n", errno);
481 for (i=0; gPollInfo.nfds>i; ++i)
484 if (0!=gPollInfo.fds[i].revents)
486 //fprintf(stderr, "\t[%d] revents 0x%04x\n", i, gPollInfo.fds[i].revents);
488 if (OT_TIMEOUT==gPollInfo.objects[i].objtype)
490 /* time-out occured */
491 unsigned long long nExpCount = 0;
492 if ((ssize_t)sizeof(nExpCount)!=read(gPollInfo.fds[i].fd, &nExpCount, sizeof(nExpCount)))
494 fprintf(stderr, "read failed!?\n");
496 fprintf(stderr, "timeout %x #%d!\n", (int)gPollInfo.objects[i].timeout, (int)nExpCount);
497 if (FALSE==dbus_timeout_handle(gPollInfo.objects[i].timeout))
499 fprintf(stderr, "dbus_timeout_handle() failed!?\n");
503 else if (gPollInfo.fds[i].fd==gEfds)
505 /* internal command */
506 if (0!=(gPollInfo.fds[i].revents & POLLIN))
510 while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno));
513 printf("read() failed w/ errno %d | %s\n", errno, strerror(errno));
519 case CMD_PAS_BLOCK_AND_WRITE_BACK:
520 process_block_and_write_data_back((buf[2]), buf[1]);
522 case CMD_LC_PREPARE_SHUTDOWN:
523 process_prepare_shutdown((buf[2]), buf[1]);
529 printf("command %d not handled!\n", buf[0]);
538 if (0!=(gPollInfo.fds[i].revents & POLLIN))
540 flags |= DBUS_WATCH_READABLE;
542 if (0!=(gPollInfo.fds[i].revents & POLLOUT))
544 flags |= DBUS_WATCH_WRITABLE;
546 if (0!=(gPollInfo.fds[i].revents & POLLERR))
548 flags |= DBUS_WATCH_ERROR;
550 if (0!=(gPollInfo.fds[i].revents & POLLHUP))
552 flags |= DBUS_WATCH_HANGUP;
555 bContinue = dbus_watch_handle(gPollInfo.objects[i].watch, flags);
563 while (0!=bContinue);
565 dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/adminconsumer");
566 dbus_connection_unregister_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer");
567 dbus_connection_unregister_object_path(conn, "/");
571 dbus_connection_unref(conn);
575 pthread_cond_signal(&gDbusInitializedCond);
576 pthread_mutex_unlock(&gDbusInitializedMtx);