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_cond_t gDbusInitializedCond = PTHREAD_COND_INITIALIZER;
32 pthread_mutex_t gDbusInitializedMtx = PTHREAD_MUTEX_INITIALIZER;
34 pthread_mutex_t gDbusPendingRegMtx = PTHREAD_MUTEX_INITIALIZER;
36 pthread_mutex_t gMainLoopMtx = PTHREAD_MUTEX_INITIALIZER;
37 pthread_cond_t gMainLoopCond = PTHREAD_COND_INITIALIZER;
42 typedef enum EDBusObjectType
50 typedef struct SObjectEntry
52 tDBusObjectType objtype; /** libdbus' object */
55 DBusWatch * watch; /** watch "object" */
56 DBusTimeout * timeout; /** timeout "object" */
63 typedef struct SPollInfo
66 struct pollfd fds[10];
67 tObjectEntry objects[10];
71 /// polling information
72 static tPollInfo gPollInfo;
76 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
79 /* function to unregister ojbect path message handler */
80 static void unregisterMessageHandler(DBusConnection *connection, void *user_data)
82 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPath\n"));
85 /* catches messages not directed to any registered object path ("garbage collector") */
86 static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data)
88 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
90 // org.genivi.persistence.admin S I G N A L
91 if((0==strcmp("org.genivi.persistence.admin", dbus_message_get_interface(message))))
93 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
95 if((0==strcmp("PersistenceModeChanged", dbus_message_get_member(message))))
97 // to do handle signal
98 result = signal_persModeChange(connection, message);
102 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown signal:"), DLT_STRING(dbus_message_get_interface(message)) );
106 // org.genivi.persistence.admin S I G N A L
107 else if((0==strcmp("org.genivi.persistence.adminconsumer", dbus_message_get_interface(message))))
109 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
111 pclNotification_s notifyStruct;
112 int validMessage = 0;
114 if((0==strcmp("PersistenceResChange", dbus_message_get_member(message))))
116 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_changed;
119 else if((0==strcmp("PersistenceResDelete", dbus_message_get_member(message))))
121 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_deleted;
124 else if((0==strcmp("PersistenceRes", dbus_message_get_member(message))))
126 notifyStruct.pclKeyNotify_Status = pclNotifyStatus_created;
130 if(validMessage == 1)
134 dbus_error_init (&error);
139 if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, ¬ifyStruct.resource_id,
140 DBUS_TYPE_STRING, &ldbid,
141 DBUS_TYPE_STRING, &user_no,
142 DBUS_TYPE_STRING, &seat_no,
145 reply = dbus_message_new_error(message, error.name, error.message);
149 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
152 if (!dbus_connection_send(connection, reply, 0))
154 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
157 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;;
158 dbus_message_unref(reply);
162 notifyStruct.ldbid = atoi(ldbid);
163 notifyStruct.user_no = atoi(user_no);
164 notifyStruct.seat_no = atoi(seat_no);
166 // call the registered callback function
167 if(gChangeNotifyCallback != NULL )
169 gChangeNotifyCallback(¬ifyStruct);
173 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => gChangeNotifyCallback is not set (possibly NULL)") );
175 result = DBUS_HANDLER_RESULT_HANDLED;
177 dbus_connection_flush(connection);
181 // org.genivi.persistence.admin P R O P E R T Y
182 else if((0==strcmp("org.freedesktop.DBus.Properties", dbus_message_get_interface(message))))
184 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
186 if((0==strcmp("EggDBusChanged", dbus_message_get_member(message))))
188 DBusMessageIter array;
189 DBusMessageIter dict;
190 DBusMessageIter variant;
192 char* dictString = NULL;
195 dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, 0, &dict);
196 dbus_message_iter_get_basic(&dict, &dictString);
198 dbus_message_iter_open_container(&dict,DBUS_TYPE_VARIANT, NULL, &variant);
199 dbus_message_iter_get_basic(&dict, &value);
201 dbus_message_iter_close_container(&dict, &variant);
202 dbus_message_iter_close_container(&array, &dict);
204 // to do handle signal
205 result = DBUS_HANDLER_RESULT_HANDLED;
209 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown property:"), DLT_STRING(dbus_message_get_interface(message)) );
214 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> not a signal:"), DLT_STRING(dbus_message_get_member(message)) );
222 static void unregisterObjectPathFallback(DBusConnection *connection, void *user_data)
224 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPathFallback\n"));
229 void* run_mainloop(void* dataPtr)
231 // persistence admin message
232 static const struct DBusObjectPathVTable vtablePersAdmin
233 = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
236 static const struct DBusObjectPathVTable vtableLifecycle
237 = {unregisterMessageHandler, checkLifecycleMsg, NULL, };
240 static const struct DBusObjectPathVTable vtableFallback
241 = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
244 mainLoop(vtablePersAdmin, vtableLifecycle, vtableFallback, dataPtr);
251 int setup_dbus_mainloop(void)
256 DBusConnection* conn = NULL;
258 const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
260 dbus_error_init(&err);
262 // wain until dbus main loop has been setup and running
263 pthread_mutex_lock(&gDbusInitializedMtx);
265 // Connect to the bus and check for errors
268 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("setup_dbus_mainloop -> Use specific dbus address:"), DLT_STRING(pAddress) );
270 conn = dbus_connection_open_private(pAddress, &err);
274 if(!dbus_bus_register(conn, &err))
276 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_bus_register() Error :"), DLT_STRING(err.message) );
277 dbus_error_free (&err);
278 pthread_mutex_unlock(&gDbusInitializedMtx);
284 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_connection_open_private() Error :"), DLT_STRING(err.message) );
285 dbus_error_free(&err);
286 pthread_mutex_unlock(&gDbusInitializedMtx);
292 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("Use default dbus bus (DBUS_BUS_SYSTEM)"));
294 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
297 // create here the dbus connection and pass to main loop
298 rval = pthread_create(&thread, NULL, run_mainloop, conn);
301 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pthread_create( DBUS run_mainloop ) returned an error:"), DLT_INT(rval) );
302 pthread_mutex_unlock(&gDbusInitializedMtx);
306 // wait for condition variable
307 pthread_cond_wait(&gDbusInitializedCond, &gDbusInitializedMtx);
309 pthread_mutex_unlock(&gDbusInitializedMtx);
317 static dbus_bool_t addWatch(DBusWatch *watch, void *data)
319 dbus_bool_t result = FALSE;
321 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
323 int flags = dbus_watch_get_flags(watch);
325 tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
326 pEntry->objtype = OT_WATCH;
327 pEntry->watch = watch;
329 gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch);
331 if (TRUE==dbus_watch_get_enabled(watch))
333 if (flags&DBUS_WATCH_READABLE)
335 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
337 if (flags&DBUS_WATCH_WRITABLE)
339 gPollInfo.fds[gPollInfo.nfds].events |= POLLOUT;
353 static void removeWatch(DBusWatch *watch, void *data)
355 void* w_data = dbus_watch_get_data(watch);
357 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("removeWatch called "), DLT_INT( (int)watch) );
362 dbus_watch_set_data(watch, NULL, NULL);
367 static void watchToggled(DBusWatch *watch, void *data)
369 DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("watchToggled called "), DLT_INT( (int)watch) );
371 if(dbus_watch_get_enabled(watch))
372 addWatch(watch, data);
374 removeWatch(watch, data);
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);
477 DBusConnection* conn = (DBusConnection*)userData;
478 dbus_error_init(&err);
480 if (dbus_error_is_set(&err))
482 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => Connection Error:"), DLT_STRING(err.message) );
483 dbus_error_free(&err);
485 else if (NULL != conn)
487 dbus_connection_set_exit_on_disconnect(conn, FALSE);
488 if (-1 == (gEfds = eventfd(0, 0)))
490 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => eventfd() failed w/ errno:"), DLT_INT(errno) );
495 memset(&gPollInfo, 0 , sizeof(gPollInfo));
498 gPollInfo.fds[0].fd = gEfds;
499 gPollInfo.fds[0].events = POLLIN;
501 dbus_bus_add_match(conn, "type='signal',interface='org.genivi.persistence.admin',member='PersistenceModeChanged',path='/org/genivi/persistence/admin'", &err);
503 // register for messages
504 if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData))
505 && (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer", &vtable2, userData))
506 && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
508 if( (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
509 || (TRUE!=dbus_connection_set_timeout_functions(conn, addTimeout, removeTimeout, timeoutToggled, NULL, NULL)) )
511 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_connection_set_watch_functions() failed"));
515 pthread_cond_signal(&gDbusInitializedCond);
516 pthread_mutex_unlock(&gDbusInitializedMtx);
519 bContinue = 0; /* assume error */
521 while (DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
523 while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, -1)))&&(EINTR==errno));
527 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => poll() failed w/ errno "), DLT_INT(errno) );
537 for (i=0; gPollInfo.nfds>i; ++i)
540 if (0!=gPollInfo.fds[i].revents)
542 if (OT_TIMEOUT==gPollInfo.objects[i].objtype)
544 /* time-out occured */
545 unsigned long long nExpCount = 0;
546 if ((ssize_t)sizeof(nExpCount)!=read(gPollInfo.fds[i].fd, &nExpCount, sizeof(nExpCount)))
548 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read failed"));
550 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => timeout"));
552 if (FALSE==dbus_timeout_handle(gPollInfo.objects[i].timeout))
554 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_timeout_handle() failed!?"));
558 else if (gPollInfo.fds[i].fd == gEfds)
560 /* internal command */
561 if (0!=(gPollInfo.fds[i].revents & POLLIN))
565 while ((-1==(ret = read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR == errno));
568 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read() failed"), DLT_STRING(strerror(errno)) );
572 pthread_mutex_lock(&gMainLoopMtx);
575 case CMD_PAS_BLOCK_AND_WRITE_BACK:
576 process_block_and_write_data_back((buf[2]), buf[1]);
578 case CMD_LC_PREPARE_SHUTDOWN:
579 process_prepare_shutdown((buf[2]), buf[1]);
581 case CMD_SEND_LC_REQUEST:
582 process_send_lifecycle_request(conn, (buf[2]), buf[1]);
584 case CMD_SEND_NOTIFY_SIGNAL:
585 process_send_notification_signal(conn);
587 case CMD_REG_NOTIFY_SIGNAL:
588 process_reg_notification_signal(conn);
590 case CMD_SEND_PAS_REQUEST:
591 process_send_pas_request(conn, (buf[2]), buf[1]);
593 case CMD_SEND_PAS_REGISTER:
594 process_send_pas_register(conn, (buf[1]), buf[2]);
596 case CMD_SEND_LC_REGISTER:
597 process_send_lifecycle_register(conn, (buf[1]), buf[2]);
603 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => command not handled"), DLT_INT(buf[0]) );
606 pthread_cond_signal(&gMainLoopCond);
607 pthread_mutex_unlock(&gMainLoopMtx);
614 if (0!=(gPollInfo.fds[i].revents & POLLIN))
616 flags |= DBUS_WATCH_READABLE;
618 if (0!=(gPollInfo.fds[i].revents & POLLOUT))
620 flags |= DBUS_WATCH_WRITABLE;
622 if (0!=(gPollInfo.fds[i].revents & POLLERR))
624 flags |= DBUS_WATCH_ERROR;
626 if (0!=(gPollInfo.fds[i].revents & POLLHUP))
628 flags |= DBUS_WATCH_HANGUP;
631 bContinue = dbus_watch_handle(gPollInfo.objects[i].watch, flags);
637 while (0!=bContinue);
639 dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/adminconsumer");
640 dbus_connection_unregister_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer");
641 dbus_connection_unregister_object_path(conn, "/");
645 dbus_connection_close(conn);
646 dbus_connection_unref(conn);
650 pthread_cond_signal(&gDbusInitializedCond);
651 pthread_mutex_unlock(&gDbusInitializedMtx);
657 int deliverToMainloop(tCmd mainloopCmd, unsigned int param1, unsigned int param2)
661 pthread_mutex_lock(&gMainLoopMtx);
663 deliverToMainloop_NM(mainloopCmd, param1, param2);
665 // wait for condition variable
666 pthread_cond_wait(&gMainLoopCond, &gMainLoopMtx);
667 pthread_mutex_unlock(&gMainLoopMtx);
672 int deliverToMainloop_NM(tCmd mainloopCmd, unsigned int param1, unsigned int param2)
677 cmd = ( ((uint64_t)param2 << 32) | ((uint64_t)param1 << 16) | mainloopCmd);
679 if(-1 == write(gEfds, &cmd, (sizeof(uint64_t))))
681 DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("deliverToMainloop => failed to write to pipe"), DLT_INT(errno));