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;
35 typedef struct SPollInfo
38 struct pollfd fds[10];
39 DBusWatch * watches[10];
43 /// polling information
44 static tPollInfo gPollInfo;
48 DBusConnection* gDbusConn = NULL;
51 DBusConnection* get_dbus_connection(void)
56 //------------------------------------------------------------------------
57 // debugging only until "correct" exit of main loop is possible!!!!!
58 //------------------------------------------------------------------------
60 static int endLoop = 0;
62 void sigHandler(int signo)
66 //------------------------------------------------------------------------
70 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
73 /* function to unregister ojbect path message handler */
74 static void unregisterMessageHandler(DBusConnection *connection, void *user_data)
76 printf("unregisterObjectPath\n");
79 /* catches messages not directed to any registered object path ("garbage collector") */
80 static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data)
82 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
84 printf("handleObjectPathMessageFallback Object: '%s' -> Interface: '%s' -> Message: '%s'\n",
85 dbus_message_get_sender(message), dbus_message_get_interface(message), dbus_message_get_member(message) );
87 // org.genivi.persistence.admin S I G N A L
88 if((0==strcmp("org.genivi.persistence.admin", dbus_message_get_interface(message))))
90 // printf("checkPersAdminSignalInterface '%s' -> '%s'\n", dbus_message_get_interface(message), dbus_message_get_member(message));
91 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
93 printf(" checkPersAdminSignal signal\n");
94 if((0==strcmp("PersistenceModeChanged", dbus_message_get_member(message))))
96 printf(" checkPersAdminSignal message\n");
97 // to do handle signal
98 result = signal_persModeChange(connection, message);
102 printf("handleObjectPathMessageFallback -> unknown signal '%s'\n", dbus_message_get_interface(message));
107 // org.genivi.persistence.admin P R O P E R T Y
108 else if((0==strcmp("org.freedesktop.DBus.Properties", dbus_message_get_interface(message))))
110 if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
112 if((0==strcmp("EggDBusChanged", dbus_message_get_member(message))))
114 DBusMessageIter array;
115 DBusMessageIter dict;
116 DBusMessageIter variant;
118 char* dictString = NULL;
121 dbus_message_iter_open_container(&array, DBUS_TYPE_DICT_ENTRY, 0, &dict);
122 dbus_message_iter_get_basic(&dict, &dictString);
124 dbus_message_iter_open_container(&dict,DBUS_TYPE_VARIANT, NULL, &variant);
125 dbus_message_iter_get_basic(&dict, &value);
127 dbus_message_iter_close_container(&dict, &variant);
128 dbus_message_iter_close_container(&array, &dict);
130 printf("handleObjectPathMessageFallback ==> value: %d \n", value);
131 // to do handle signal
132 result = DBUS_HANDLER_RESULT_HANDLED;
136 printf("handleObjectPathMessageFallback -> unknown property '%s'\n", dbus_message_get_interface(message));
141 printf("handleObjectPathMessageFallback -> not a signal '%s'\n", dbus_message_get_member(message));
150 static void unregisterObjectPathFallback(DBusConnection *connection, void *user_data)
152 printf("unregisterObjectPathFallback\n");
157 void* run_mainloop(void* dataPtr)
159 // persistence admin message
160 static const struct DBusObjectPathVTable vtablePersAdmin
161 = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
164 static const struct DBusObjectPathVTable vtableLifecycle
165 = {unregisterMessageHandler, checkLifecycleMsg, NULL, };
168 static const struct DBusObjectPathVTable vtableFallback
169 = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
172 mainLoop(vtablePersAdmin, vtableLifecycle, vtableFallback, dataPtr);
174 printf("Exit dbus main loop!!!!\n");
181 int setup_dbus_mainloop(void)
186 const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
187 dbus_error_init(&err);
189 // enable locking of data structures in the D-Bus library for multi threading.
190 dbus_threads_init_default();
192 // Connect to the bus and check for errors
195 printf("Use specific dbus address: %s\n !", pAddress);
196 gDbusConn = dbus_connection_open(pAddress, &err);
198 if(gDbusConn != NULL)
200 if(!dbus_bus_register(gDbusConn, &err))
202 printf("dbus_bus_register() Error %s\n", err.message);
203 dbus_error_free (&err);
208 printf("Registered connection successfully !\n");
213 printf("dbus_connection_open() Error %s\n",err.message);
214 dbus_error_free(&err);
219 printf("Use default dbus bus!!!!!!\n");
220 gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
223 // wain until dbus main loop has been setup and running
224 pthread_mutex_lock(&gDbusInitializedMtx);
226 // create here the dbus connection and pass to main loop
227 rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn);
230 fprintf(stderr, "Server: - ERROR! pthread_create( run_mainloop ) returned: %d\n", rval);
233 // wait for condition variable
234 pthread_cond_wait(&gDbusInitializedCond, &gDbusInitializedMtx);
236 pthread_mutex_unlock(&gDbusInitializedMtx);
244 static dbus_bool_t addWatch(DBusWatch *watch, void *data)
246 dbus_bool_t result = FALSE;
248 //printf("addWatch called @%08x flags: %08x enabled: %c\n", (unsigned int)watch, dbus_watch_get_flags(watch), TRUE==dbus_watch_get_enabled(watch)?'x':'-');
250 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
252 int flags = dbus_watch_get_flags(watch);
254 gPollInfo.watches[gPollInfo.nfds] = watch;
256 gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch);
258 if (TRUE==dbus_watch_get_enabled(watch))
260 if (flags&DBUS_WATCH_READABLE)
262 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
264 if (flags&DBUS_WATCH_WRITABLE)
266 gPollInfo.fds[gPollInfo.nfds].events |= POLLOUT;
280 static void removeWatch(DBusWatch *watch, void *data)
282 printf("removeWatch called @0x%08x\n", (int)watch);
287 static void watchToggled(DBusWatch *watch, void *data)
289 printf("watchToggled called @0x%08x\n", (int)watch);
294 int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
295 DBusObjectPathVTable vtableFallback, void* userData)
298 // lock mutex to make sure dbus main loop is running
299 pthread_mutex_lock(&gDbusInitializedMtx);
301 signal(SIGTERM, sigHandler);
302 signal(SIGQUIT, sigHandler);
303 signal(SIGINT, sigHandler);
305 DBusConnection* conn = (DBusConnection*)userData;
306 dbus_error_init(&err);
308 if (dbus_error_is_set(&err))
310 printf("Connection Error (%s)\n", err.message);
311 dbus_error_free(&err);
313 else if (NULL != conn)
315 dbus_connection_set_exit_on_disconnect (conn, FALSE);
316 printf("connected as '%s'\n", dbus_bus_get_unique_name(conn));
317 if (-1 == (gEfds = eventfd(0, 0)))
319 printf("eventfd() failed w/ errno %d\n", errno);
325 memset(&gPollInfo, 0 , sizeof(gPollInfo));
328 gPollInfo.fds[0].fd = gEfds;
329 gPollInfo.fds[0].events = POLLIN;
331 dbus_bus_add_match(conn, "type='signal',interface='org.genivi.persistence.admin',member='PersistenceModeChanged',path='/org/genivi/persistence/admin'", &err);
333 // register for messages
334 if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData))
335 && (TRUE==dbus_connection_register_object_path(conn, "/com/contiautomotive/NodeStateManager/LifecycleConsumer", &vtable2, userData))
336 && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
338 if (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
340 printf("dbus_connection_set_watch_functions() failed\n");
346 pthread_cond_signal(&gDbusInitializedCond);
347 pthread_mutex_unlock(&gDbusInitializedMtx);
350 bContinue = 0; /* assume error */
352 while(DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
354 while((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, 500)))&&(EINTR==errno));
358 printf("poll() failed w/ errno %d\n", errno);
365 for (i=0; gPollInfo.nfds>i; ++i)
367 if (0!=gPollInfo.fds[i].revents)
369 if (gPollInfo.fds[i].fd==gEfds)
371 if (0!=(gPollInfo.fds[i].revents & POLLIN))
374 while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno));
377 printf("read() failed w/ errno %d | %s\n", errno, strerror(errno));
383 case CMD_PAS_BLOCK_AND_WRITE_BACK:
384 process_block_and_write_data_back((buf[2]), buf[1]);
386 case CMD_LC_PREPARE_SHUTDOWN:
387 process_prepare_shutdown((buf[2]), buf[1]);
393 printf("command %d not handled!\n", buf[0]);
399 printf("read() returned %d \n", ret);
406 if (0!=(gPollInfo.fds[i].revents & POLLIN))
408 flags |= DBUS_WATCH_READABLE;
410 if (0!=(gPollInfo.fds[i].revents & POLLOUT))
412 flags |= DBUS_WATCH_WRITABLE;
414 if (0!=(gPollInfo.fds[i].revents & POLLERR))
416 flags |= DBUS_WATCH_ERROR;
418 if (0!=(gPollInfo.fds[i].revents & POLLHUP))
420 flags |= DBUS_WATCH_HANGUP;
422 //printf("handle watch @0x%08x flags: %04x\n", (int)gPollInfo.watches[i], flags);
423 bContinue = dbus_watch_handle(gPollInfo.watches[i], flags);
431 while (0!=bContinue);
433 dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/adminconsumer");
434 dbus_connection_unregister_object_path(conn, "/com/contiautomotive/NodeStateManager/LifecycleConsumer");
435 dbus_connection_unregister_object_path(conn, "/");
439 dbus_connection_unref(conn);
443 pthread_cond_signal(&gDbusInitializedCond);
444 pthread_mutex_unlock(&gDbusInitializedMtx);