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 printf("removeWatch called @0x%08x\n", (int)watch);
269 static void watchToggled(DBusWatch *watch, void *data)
271 printf("watchToggled called @0x%08x\n", (int)watch);
276 int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtableFallback, void* userData)
279 // lock mutex to make sure dbus main loop is running
280 pthread_mutex_lock(&gDbusInitializedMtx);
282 signal(SIGTERM, sigHandler);
283 signal(SIGQUIT, sigHandler);
284 signal(SIGINT, sigHandler);
286 DBusConnection* conn = (DBusConnection*)userData;
287 dbus_error_init(&err);
289 if (dbus_error_is_set(&err))
291 printf("Connection Error (%s)\n", err.message);
292 dbus_error_free(&err);
294 else if (NULL != conn)
296 dbus_connection_set_exit_on_disconnect (conn, FALSE);
297 printf("connected as '%s'\n", dbus_bus_get_unique_name(conn));
298 if (-1 == (gEfds = eventfd(0, 0)))
300 printf("eventfd() failed w/ errno %d\n", errno);
306 memset(&gPollInfo, 0 , sizeof(gPollInfo));
309 gPollInfo.fds[0].fd = gEfds;
310 gPollInfo.fds[0].events = POLLIN;
312 // register for messages
313 if ( (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/admin", &vtable, userData))
314 && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
316 if (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
318 printf("dbus_connection_set_watch_functions() failed\n");
324 pthread_cond_signal(&gDbusInitializedCond);
325 pthread_mutex_unlock(&gDbusInitializedMtx);
328 bContinue = 0; /* assume error */
330 while(DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
332 while((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, 500)))&&(EINTR==errno));
336 printf("poll() failed w/ errno %d\n", errno);
343 for (i=0; gPollInfo.nfds>i; ++i)
345 if (0!=gPollInfo.fds[i].revents)
347 if (gPollInfo.fds[i].fd==gEfds)
349 if (0!=(gPollInfo.fds[i].revents & POLLIN))
352 while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno));
355 printf("read() failed w/ errno %d | %s\n", errno, strerror(errno));
361 case CMD_REQUEST_NAME:
362 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER !=dbus_bus_request_name(conn, "org.genivi.persistence.admin", DBUS_NAME_FLAG_DO_NOT_QUEUE, &err))
364 fprintf(stderr, "Cannot acquire name 'org.genivi.persistence.admin': \n \"(%s)\". Bailing out!\n", err.message);
365 dbus_error_free(&err);
373 printf("command %d not handled!\n", buf[0]);
382 if (0!=(gPollInfo.fds[i].revents & POLLIN))
384 flags |= DBUS_WATCH_READABLE;
386 if (0!=(gPollInfo.fds[i].revents & POLLOUT))
388 flags |= DBUS_WATCH_WRITABLE;
390 if (0!=(gPollInfo.fds[i].revents & POLLERR))
392 flags |= DBUS_WATCH_ERROR;
394 if (0!=(gPollInfo.fds[i].revents & POLLHUP))
396 flags |= DBUS_WATCH_HANGUP;
398 //printf("handle watch @0x%08x flags: %04x\n", (int)gPollInfo.watches[i], flags);
399 bContinue = dbus_watch_handle(gPollInfo.watches[i], flags);
407 while (0!=bContinue);
409 dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/admin");
410 dbus_connection_unregister_object_path(conn, "/");
414 dbus_connection_unref(conn);
418 pthread_cond_signal(&gDbusInitializedCond);
419 pthread_mutex_unlock(&gDbusInitializedMtx);
424 void* run_mainloop(void* dataPtr)
426 // persistence admin message
427 static const struct DBusObjectPathVTable vtablePersAdmin
428 = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
431 static const struct DBusObjectPathVTable vtableFallback
432 = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
435 mainLoop(vtablePersAdmin, vtableFallback, dataPtr);
437 printf("Exit dbus main loop!!!!\n");
443 int setup_dbus_mainloop(void)
448 const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
449 dbus_error_init(&err);
451 // enable locking of data structures in the D-Bus library for multi threading.
452 dbus_threads_init_default();
454 // Connect to the bus and check for errors
457 printf("Use specific dbus address: %s\n !", pAddress);
458 gDbusConn = dbus_connection_open(pAddress, &err);
460 if(gDbusConn != NULL)
462 if(!dbus_bus_register(gDbusConn, &err))
464 printf("dbus_bus_register() Error %s\n", err.message);
465 dbus_error_free (&err);
470 printf("Registered connection successfully !\n");
475 printf("dbus_connection_open() Error %s\n",err.message);
476 dbus_error_free(&err);
481 printf("Use default dbus bus!!!!!!\n");
482 gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
485 // wain until dbus main loop has been setup and running
486 pthread_mutex_lock(&gDbusInitializedMtx);
488 // create here the dbus connection and pass to main loop
489 rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn);
492 fprintf(stderr, "Server: - ERROR! pthread_create( run_mainloop ) returned: %d\n", rval);
495 // wait for condition variable
496 pthread_cond_wait(&gDbusInitializedCond, &gDbusInitializedMtx);
498 pthread_mutex_unlock(&gDbusInitializedMtx);
504 int main(int argc, char *argv[])
506 setup_dbus_mainloop();
508 printf("Wait, press enter to exit!!\n");
510 printf("Exiting Persistence Admin mockup!!\n");