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_admin_service_mockup.c
13 * @ingroup Persistence client library test
14 * @author Ingo Huerner
15 * @brief Persistence Administration Serivce mockup
23 #include <unistd.h> /* exit */
28 #include <dbus/dbus.h>
31 #include <sys/eventfd.h>
34 /// command definitions for main loop
37 CMD_NONE = 0, /// command none
38 CMD_PAS_BLOCK_AND_WRITE_BACK, /// command block access and write data back
39 CMD_LC_PREPARE_SHUTDOWN, /// command to prepare shutdown
40 CMD_QUIT, /// quit command
45 /// pipe file descriptors
50 pthread_mutex_t gDbusInitializedMtx = PTHREAD_MUTEX_INITIALIZER;
51 pthread_cond_t gDbusInitializedCond = PTHREAD_COND_INITIALIZER;
54 typedef struct SPollInfo
57 struct pollfd fds[10];
58 DBusWatch * watches[10];
62 /// polling information
63 static tPollInfo gPollInfo;
67 DBusConnection* gDbusConn = NULL;
70 DBusConnection* get_dbus_connection(void)
75 //------------------------------------------------------------------------
76 // debugging only until "correct" exit of main loop is possible!!!!!
77 //------------------------------------------------------------------------
79 static int endLoop = 0;
81 void sigHandler(int signo)
85 //------------------------------------------------------------------------
89 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
93 int checkAdminMsg(DBusConnection *connection, DBusMessage *message)
97 int32_t notificationFlag = 0;
98 uint32_t gTimeoutMs = 0;
99 int msgReturn = 123321;
103 dbus_error_init (&error);
105 if (!dbus_message_get_args (message, &error, DBUS_TYPE_STRING, &busName, // bus name
106 DBUS_TYPE_STRING, &objName,
107 DBUS_TYPE_INT32, ¬ificationFlag,
108 DBUS_TYPE_UINT32, &gTimeoutMs,
111 reply = dbus_message_new_error(message, error.name, error.message);
115 //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory"));
116 printf("DBus No memory\n");
119 if (!dbus_connection_send(connection, reply, 0))
121 //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory"));
122 printf("DBus No memory\n");
125 dbus_message_unref(reply);
127 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
131 printf(" checkAdminMsg ==> busName: %s | objName: %s | notificationFlag: %u | gTimeoutMs: %d\n\n", busName, objName, notificationFlag, gTimeoutMs);
132 reply = dbus_message_new_method_return(message);
136 //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory"));
137 printf("DBus No memory\n");
140 if (!dbus_message_append_args(reply, DBUS_TYPE_INT32, &msgReturn, DBUS_TYPE_INVALID))
142 //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory"));
143 printf("DBus No memory\n");
146 if (!dbus_connection_send(connection, reply, NULL))
148 //DLT_LOG(mgrContext, DLT_LOG_ERROR, DLT_STRING("DBus No memory"));
149 printf("DBus No memory\n");
152 dbus_connection_flush(connection);
153 dbus_message_unref(reply);
155 return DBUS_HANDLER_RESULT_HANDLED;
160 DBusHandlerResult checkPersAdminMsg(DBusConnection * connection, DBusMessage * message, void * user_data)
162 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
164 //printf("checkPersAdminMsg '%s' -> '%s'\n", dbus_message_get_interface(message), dbus_message_get_member(message));
165 if((0==strcmp("org.genivi.persistence.admin", dbus_message_get_interface(message))))
167 if((0==strcmp("RegisterPersAdminNotification", dbus_message_get_member(message))))
169 printf(" ==> org.genivi.persistence.admin - received - ==> RegisterPersAdminNotification \n");
171 result = checkAdminMsg(connection, message);
173 else if((0==strcmp("UnRegisterPersAdminNotification", dbus_message_get_member(message))))
175 printf(" ==> org.genivi.persistence.admin - received - ==> UnRegisterPersAdminNotification \n");
177 result = checkAdminMsg(connection, message);
181 printf(" ==> org.genivi.persistence.admin - received U N KN O W N-'%s'\n", dbus_message_get_interface(message));
186 printf(" ==> org.genivi.persistence - received U N KN O W N-'%s'\n", dbus_message_get_interface(message));
192 /* function to unregister ojbect path message handler */
193 static void unregisterMessageHandler(DBusConnection *connection, void *user_data)
195 printf("unregisterObjectPath\n");
198 /* catches messages not directed to any registered object path ("garbage collector") */
199 static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data)
201 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
203 printf("handleObjectPathMessageFallback Object: '%s' -> Interface: '%s' -> Message: '%s'\n",
204 dbus_message_get_sender(message), dbus_message_get_interface(message), dbus_message_get_member(message) );
211 static void unregisterObjectPathFallback(DBusConnection *connection, void *user_data)
213 printf("unregisterObjectPathFallback\n");
221 static dbus_bool_t addWatch(DBusWatch *watch, void *data)
223 dbus_bool_t result = FALSE;
225 //printf("addWatch called @%08x flags: %08x enabled: %c\n", (unsigned int)watch, dbus_watch_get_flags(watch), TRUE==dbus_watch_get_enabled(watch)?'x':'-');
227 if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
229 int flags = dbus_watch_get_flags(watch);
231 gPollInfo.watches[gPollInfo.nfds] = watch;
233 gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch);
235 if (TRUE==dbus_watch_get_enabled(watch))
237 if (flags&DBUS_WATCH_READABLE)
239 gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
241 if (flags&DBUS_WATCH_WRITABLE)
243 gPollInfo.fds[gPollInfo.nfds].events |= POLLOUT;
247 /* wakeup main-loop, just in case */
248 static const uint64_t cmd = CMD_REQUEST_NAME;
249 if (sizeof(uint64_t)!=write(gEfds, &cmd, sizeof(uint64_t)))
251 fprintf(stderr, "write failed w/ errno %d\n", errno);
262 static void removeWatch(DBusWatch *watch, void *data)
264 void* w_data = dbus_watch_get_data(watch);
266 printf("removeWatch called @0x%08x\n", (int)watch);
271 dbus_watch_set_data(watch, NULL, NULL);
275 static void watchToggled(DBusWatch *watch, void *data)
277 printf("watchToggled called @0x%08x\n", (int)watch);
279 if(dbus_watch_get_enabled(watch))
280 addWatch(watch, data);
282 removeWatch(watch, data);
286 int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtableFallback, void* userData)
289 // lock mutex to make sure dbus main loop is running
290 pthread_mutex_lock(&gDbusInitializedMtx);
292 signal(SIGTERM, sigHandler);
293 signal(SIGQUIT, sigHandler);
294 signal(SIGINT, sigHandler);
296 DBusConnection* conn = (DBusConnection*)userData;
297 dbus_error_init(&err);
299 if (dbus_error_is_set(&err))
301 printf("Connection Error (%s)\n", err.message);
302 dbus_error_free(&err);
304 else if (NULL != conn)
306 dbus_connection_set_exit_on_disconnect (conn, FALSE);
307 printf("connected as '%s'\n", dbus_bus_get_unique_name(conn));
308 if (-1 == (gEfds = eventfd(0, 0)))
310 printf("eventfd() failed w/ errno %d\n", errno);
316 memset(&gPollInfo, 0 , sizeof(gPollInfo));
319 gPollInfo.fds[0].fd = gEfds;
320 gPollInfo.fds[0].events = POLLIN;
322 // register for messages
323 if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/admin", &vtable, userData))
324 && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
326 if (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
328 printf("dbus_connection_set_watch_functions() failed\n");
334 pthread_cond_signal(&gDbusInitializedCond);
335 pthread_mutex_unlock(&gDbusInitializedMtx);
338 bContinue = 0; /* assume error */
340 while(DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
342 while((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, 500)))&&(EINTR==errno));
346 printf("poll() failed w/ errno %d\n", errno);
353 for (i=0; gPollInfo.nfds>i; ++i)
355 if (0!=gPollInfo.fds[i].revents)
357 if (gPollInfo.fds[i].fd==gEfds)
359 if (0!=(gPollInfo.fds[i].revents & POLLIN))
362 while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno));
365 printf("read() failed w/ errno %d | %s\n", errno, strerror(errno));
371 case CMD_REQUEST_NAME:
372 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER !=dbus_bus_request_name(conn, "org.genivi.persistence.admin", DBUS_NAME_FLAG_DO_NOT_QUEUE, &err))
374 fprintf(stderr, "Cannot acquire name 'org.genivi.persistence.admin': \n \"(%s)\". Bailing out!\n", err.message);
375 dbus_error_free(&err);
383 printf("command %d not handled!\n", buf[0]);
392 if (0!=(gPollInfo.fds[i].revents & POLLIN))
394 flags |= DBUS_WATCH_READABLE;
396 if (0!=(gPollInfo.fds[i].revents & POLLOUT))
398 flags |= DBUS_WATCH_WRITABLE;
400 if (0!=(gPollInfo.fds[i].revents & POLLERR))
402 flags |= DBUS_WATCH_ERROR;
404 if (0!=(gPollInfo.fds[i].revents & POLLHUP))
406 flags |= DBUS_WATCH_HANGUP;
408 //printf("handle watch @0x%08x flags: %04x\n", (int)gPollInfo.watches[i], flags);
409 bContinue = dbus_watch_handle(gPollInfo.watches[i], flags);
417 while (0!=bContinue);
419 dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/admin");
420 dbus_connection_unregister_object_path(conn, "/");
424 //dbus_connection_close(conn);
425 dbus_connection_unref(conn);
429 pthread_cond_signal(&gDbusInitializedCond);
430 pthread_mutex_unlock(&gDbusInitializedMtx);
435 void* run_mainloop(void* dataPtr)
437 // persistence admin message
438 static const struct DBusObjectPathVTable vtablePersAdmin
439 = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
442 static const struct DBusObjectPathVTable vtableFallback
443 = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
446 mainLoop(vtablePersAdmin, vtableFallback, dataPtr);
448 printf("Exit dbus main loop!!!!\n");
454 int setup_dbus_mainloop(void)
459 const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
460 dbus_error_init(&err);
462 // enable locking of data structures in the D-Bus library for multi threading.
463 dbus_threads_init_default();
465 // Connect to the bus and check for errors
468 printf("Use specific dbus address: %s\n !", pAddress);
469 gDbusConn = dbus_connection_open_private(pAddress, &err);
471 if(gDbusConn != NULL)
473 if(!dbus_bus_register(gDbusConn, &err))
475 printf("dbus_bus_register() Error %s\n", err.message);
476 dbus_error_free (&err);
481 printf("Registered connection successfully !\n");
486 printf("dbus_connection_open() Error %s\n",err.message);
487 dbus_error_free(&err);
492 printf("Use default dbus bus!!!!!!\n");
493 gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
496 // wain until dbus main loop has been setup and running
497 pthread_mutex_lock(&gDbusInitializedMtx);
499 // create here the dbus connection and pass to main loop
500 rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn);
503 fprintf(stderr, "Server: - ERROR! pthread_create( run_mainloop ) returned: %d\n", rval);
506 // wait for condition variable
507 pthread_cond_wait(&gDbusInitializedCond, &gDbusInitializedMtx);
509 pthread_mutex_unlock(&gDbusInitializedMtx);
515 int main(int argc, char *argv[])
517 setup_dbus_mainloop();
519 printf("Wait, press enter to exit!!\n");
521 printf("Exiting Persistence Admin mockup!!\n");