summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIngo Huerner <ingo.huerner@xse.de>2013-03-01 10:34:02 +0100
committerIngo Huerner <ingo.huerner@xse.de>2013-03-01 10:34:02 +0100
commit0d02860508e9375a046e00dbcf90d40e8c3de21e (patch)
treee1d000f90dee9ececd89c01c500e35c481e26555
parentf89f21a4789c39a88b46ee32b2a2eed46367f98b (diff)
downloadpersistence-client-library-0d02860508e9375a046e00dbcf90d40e8c3de21e.tar.gz
Added timeout to the dbus mainloop; bugfixing dbus messages; bugfixing loading of resource config table; added mockups for persistence admin service and lifecycle dbus simulation
-rw-r--r--src/persistence_client_library.c4
-rw-r--r--src/persistence_client_library_dbus_service.c167
-rw-r--r--src/persistence_client_library_dbus_service.h1
-rw-r--r--src/persistence_client_library_lc_interface.c2
-rw-r--r--src/persistence_client_library_pas_interface.c44
-rw-r--r--src/persistence_client_library_prct_access.c15
-rw-r--r--test/Makefile.am10
-rw-r--r--test/persistence_client_library_dbus_test.c5
8 files changed, 204 insertions, 44 deletions
diff --git a/src/persistence_client_library.c b/src/persistence_client_library.c
index 2f16e43..270d126 100644
--- a/src/persistence_client_library.c
+++ b/src/persistence_client_library.c
@@ -122,10 +122,6 @@ void pers_library_init(void)
}
gPersCustomFuncs[i].custom_plugin_init();
}
-
- /// just testing
- //gPersCustomFuncs[PersCustomLib_early].custom_plugin_open("Hallo", 88, 99);
- //gPersCustomFuncs[PersCustomLib_early].custom_plugin_close(17);
}
//printf("A p p l i c a t i o n n a m e => %s \n", __progname /*program_invocation_short_name*/); // TODO: only temp solution for application name
diff --git a/src/persistence_client_library_dbus_service.c b/src/persistence_client_library_dbus_service.c
index 3d4c652..0ea4fad 100644
--- a/src/persistence_client_library_dbus_service.c
+++ b/src/persistence_client_library_dbus_service.c
@@ -31,12 +31,32 @@
pthread_mutex_t gDbusInitializedMtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gDbusInitializedCond = PTHREAD_COND_INITIALIZER;
+typedef enum EDBusObjectType
+{
+ OT_NONE = 0,
+ OT_WATCH,
+ OT_TIMEOUT
+} tDBusObjectType;
+
+
+typedef struct SObjectEntry
+{
+ tDBusObjectType objtype; /** libdbus' object */
+ union
+ {
+ DBusWatch * watch; /** watch "object" */
+ DBusTimeout * timeout; /** timeout "object" */
+ };
+} tObjectEntry;
+
+
+
/// polling structure
typedef struct SPollInfo
{
int nfds;
struct pollfd fds[10];
- DBusWatch * watches[10];
+ tObjectEntry objects[10];
} tPollInfo;
@@ -216,7 +236,7 @@ int setup_dbus_mainloop(void)
}
else
{
- printf("Use default dbus bus!!!!!!\n");
+ printf("Use default dbus bus (DBUS_BUS_SYSTEM) !!!!!!\n");
gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
}
@@ -245,13 +265,15 @@ static dbus_bool_t addWatch(DBusWatch *watch, void *data)
{
dbus_bool_t result = FALSE;
- //printf("addWatch called @%08x flags: %08x enabled: %c\n", (unsigned int)watch, dbus_watch_get_flags(watch), TRUE==dbus_watch_get_enabled(watch)?'x':'-');
+ //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':'-');
if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
{
int flags = dbus_watch_get_flags(watch);
- gPollInfo.watches[gPollInfo.nfds] = watch;
+ tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
+ pEntry->objtype = OT_WATCH;
+ pEntry->watch = watch;
gPollInfo.fds[gPollInfo.nfds].fd = dbus_watch_get_unix_fd(watch);
@@ -291,6 +313,97 @@ static void watchToggled(DBusWatch *watch, void *data)
+static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
+{
+ dbus_bool_t ret = FALSE;
+
+ if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
+ {
+ const int interval = dbus_timeout_get_interval(timeout);
+ if ((0<interval)&&(TRUE==dbus_timeout_get_enabled(timeout)))
+ {
+ const int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
+ if (-1!=tfd)
+ {
+ const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
+ if (-1!=timerfd_settime(tfd, 0, &its, NULL))
+ {
+ tObjectEntry * const pEntry = &gPollInfo.objects[gPollInfo.nfds];
+ pEntry->objtype = OT_TIMEOUT;
+ pEntry->timeout = timeout;
+ gPollInfo.fds[gPollInfo.nfds].fd = tfd;
+ gPollInfo.fds[gPollInfo.nfds].events |= POLLIN;
+ ++gPollInfo.nfds;
+ ret = TRUE;
+ }
+ else
+ {
+ fprintf(stderr, "timerfd_settime() failed %d '%s'\n", errno, strerror(errno));
+ }
+ }
+ else
+ {
+ fprintf(stderr, "timerfd_create() failed %d '%s'\n", errno, strerror(errno));
+ }
+ }
+ }
+ else
+ {
+ fprintf(stderr, "cannot create another fd to be poll()'ed\n");
+ }
+
+ return ret;
+}
+
+
+
+static void removeTimeout(DBusTimeout *timeout, void *data)
+{
+
+ int i = gPollInfo.nfds;
+ while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
+
+ if (0<i)
+ {
+ if (-1==close(gPollInfo.fds[i].fd))
+ {
+ fprintf(stderr, "close() timerfd #%d failed %d '%s'\n", gPollInfo.fds[i].fd, errno, strerror(errno));
+ }
+
+ --gPollInfo.nfds;
+ while (gPollInfo.nfds>i)
+ {
+ gPollInfo.fds[i] = gPollInfo.fds[i+1];
+ gPollInfo.objects[i] = gPollInfo.objects[i+1];
+ ++i;
+ }
+
+ gPollInfo.fds[gPollInfo.nfds].fd = -1;
+ gPollInfo.objects[gPollInfo.nfds].objtype = OT_NONE;
+ }
+}
+
+
+
+/** callback for libdbus' when timeout changed */
+static void timeoutToggled(DBusTimeout *timeout, void *data)
+{
+ int i = gPollInfo.nfds;
+ while ((0<i--)&&(timeout!=gPollInfo.objects[i].timeout));
+ 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);
+ if (0<i)
+ {
+ const int interval = (TRUE==dbus_timeout_get_enabled(timeout))?dbus_timeout_get_interval(timeout):0;
+ const struct itimerspec its = { .it_value= {interval/1000, interval%1000} };
+ if (-1!=timerfd_settime(gPollInfo.fds[i].fd, 0, &its, NULL))
+ {
+ fprintf(stderr, "timerfd_settime() %d failed %d '%s'\n", interval, errno, strerror(errno));
+ }
+ }
+}
+
+
+
int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
DBusObjectPathVTable vtableFallback, void* userData)
{
@@ -313,7 +426,7 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
else if (NULL != conn)
{
dbus_connection_set_exit_on_disconnect (conn, FALSE);
- printf("connected as '%s'\n", dbus_bus_get_unique_name(conn));
+ //printf("connected as '%s'\n", dbus_bus_get_unique_name(conn));
if (-1 == (gEfds = eventfd(0, 0)))
{
printf("eventfd() failed w/ errno %d\n", errno);
@@ -335,41 +448,63 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
&& (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer", &vtable2, userData))
&& (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
{
- if (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
+ if( (TRUE!=dbus_connection_set_watch_functions(conn, addWatch, removeWatch, watchToggled, NULL, NULL))
+ || (TRUE!=dbus_connection_set_timeout_functions(conn, addTimeout, removeTimeout, timeoutToggled, NULL, NULL)) )
{
printf("dbus_connection_set_watch_functions() failed\n");
}
else
{
- uint16_t buf[64];
-
pthread_cond_signal(&gDbusInitializedCond);
pthread_mutex_unlock(&gDbusInitializedMtx);
do
{
bContinue = 0; /* assume error */
- while(DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
+ while (DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
- while((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, 500)))&&(EINTR==errno));
+ while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, -1)))&&(EINTR==errno));
- if(0>ret)
+ if (0>ret)
+ {
+ fprintf(stderr, "poll() failed w/ errno %d\n", errno);
+ }
+ else if (0==ret)
{
- printf("poll() failed w/ errno %d\n", errno);
+ /* poll time-out */
}
else
{
int i;
- bContinue = 1;
for (i=0; gPollInfo.nfds>i; ++i)
{
+ /* anything to do */
if (0!=gPollInfo.fds[i].revents)
{
- if (gPollInfo.fds[i].fd==gEfds)
+ //fprintf(stderr, "\t[%d] revents 0x%04x\n", i, gPollInfo.fds[i].revents);
+
+ if (OT_TIMEOUT==gPollInfo.objects[i].objtype)
+ {
+ /* time-out occured */
+ unsigned long long nExpCount = 0;
+ if ((ssize_t)sizeof(nExpCount)!=read(gPollInfo.fds[i].fd, &nExpCount, sizeof(nExpCount)))
+ {
+ fprintf(stderr, "read failed!?\n");
+ }
+ fprintf(stderr, "timeout %x #%d!\n", (int)gPollInfo.objects[i].timeout, (int)nExpCount);
+ if (FALSE==dbus_timeout_handle(gPollInfo.objects[i].timeout))
+ {
+ fprintf(stderr, "dbus_timeout_handle() failed!?\n");
+ }
+ bContinue = TRUE;
+ }
+ else if (gPollInfo.fds[i].fd==gEfds)
{
+ /* internal command */
if (0!=(gPollInfo.fds[i].revents & POLLIN))
{
+ uint16_t buf[64];
bContinue = TRUE;
while ((-1==(ret=read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR==errno));
if (0>ret)
@@ -415,8 +550,8 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
{
flags |= DBUS_WATCH_HANGUP;
}
- //printf("handle watch @0x%08x flags: %04x\n", (int)gPollInfo.watches[i], flags);
- bContinue = dbus_watch_handle(gPollInfo.watches[i], flags);
+
+ bContinue = dbus_watch_handle(gPollInfo.objects[i].watch, flags);
}
}
}
diff --git a/src/persistence_client_library_dbus_service.h b/src/persistence_client_library_dbus_service.h
index 328b25f..4e000f2 100644
--- a/src/persistence_client_library_dbus_service.h
+++ b/src/persistence_client_library_dbus_service.h
@@ -23,6 +23,7 @@
#include <poll.h>
#include <pthread.h>
#include <sys/eventfd.h>
+#include <sys/timerfd.h>
/// mutex to make sure main loop is running
extern pthread_mutex_t gDbusInitializedMtx;
diff --git a/src/persistence_client_library_lc_interface.c b/src/persistence_client_library_lc_interface.c
index 22bfae2..203a9e7 100644
--- a/src/persistence_client_library_lc_interface.c
+++ b/src/persistence_client_library_lc_interface.c
@@ -35,7 +35,7 @@
#include <unistd.h>
#include <dlfcn.h>
-static int gTimeoutMs = 500;
+static int gTimeoutMs = 5000; // 5 seconds
int check_lc_request(int request, int requestID)
{
diff --git a/src/persistence_client_library_pas_interface.c b/src/persistence_client_library_pas_interface.c
index 89dab9a..43e18af 100644
--- a/src/persistence_client_library_pas_interface.c
+++ b/src/persistence_client_library_pas_interface.c
@@ -26,7 +26,7 @@
#include <unistd.h>
-static int gTimeoutMs = 500;
+static int gTimeoutMs = 5000; // 5 seconds
/// flag if access is locked
static int gLockAccess = 0;
@@ -105,9 +105,9 @@ DBusHandlerResult msg_persAdminRequest(DBusConnection *connection, DBusMessage *
dbus_error_init (&error);
- if (!dbus_message_get_args (message, &error, DBUS_TYPE_INT32 , &request,
- DBUS_TYPE_INT32 , &requestID,
- DBUS_TYPE_INVALID))
+ if (!dbus_message_get_args(message, &error, DBUS_TYPE_INT32 , &request,
+ DBUS_TYPE_INT32 , &requestID,
+ DBUS_TYPE_INVALID))
{
reply = dbus_message_new_error(message, error.name, error.message);
@@ -123,7 +123,7 @@ DBusHandlerResult msg_persAdminRequest(DBusConnection *connection, DBusMessage *
printf("DBus No memory\n");
}
- dbus_message_unref (reply);
+ dbus_message_unref(reply);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
@@ -292,16 +292,24 @@ int send_pas_register(const char* method, int notificationFlag)
if(conn != NULL)
{
replyMsg = dbus_connection_send_with_reply_and_block(conn, message, gTimeoutMs, &error);
- if(dbus_set_error_from_message(&error, replyMsg))
+
+ if(replyMsg != NULL)
{
- fprintf(stderr, "sendDBusMessage ==> Access denied: %s \n", error.message);
+ if(dbus_set_error_from_message(&error, replyMsg))
+ {
+ fprintf(stderr, "sendDBusMessage ==> Access denied: %s \n", error.message);
+ }
+ else
+ {
+ dbus_message_get_args(replyMsg, &error, DBUS_TYPE_INT32, &rval, DBUS_TYPE_INVALID);
+ printf("send_pas_register ==> REPLY value: %d \n", rval);
+ }
+ dbus_message_unref(replyMsg);
}
else
{
- dbus_message_get_args(replyMsg, &error, DBUS_TYPE_INT32, &rval, DBUS_TYPE_INVALID);
+ printf("send_pas_register ==> reply message is NULL!\n ==> error msg: %s \n", error.message);
}
-
- dbus_message_unref(message);
}
else
{
@@ -346,16 +354,22 @@ int send_pas_request(const char* method, unsigned int requestID, int status)
if(conn != NULL)
{
replyMsg = dbus_connection_send_with_reply_and_block(conn, message, gTimeoutMs, &error);
- if(dbus_set_error_from_message(&error, replyMsg))
+ if(replyMsg != NULL)
{
- fprintf(stderr, "sendDBusMessage ==> Access denied: %s \n", error.message);
+ if(dbus_set_error_from_message(&error, replyMsg))
+ {
+ fprintf(stderr, "sendDBusMessage ==> Access denied: %s \n", error.message);
+ }
+ else
+ {
+ dbus_message_get_args(replyMsg, &error, DBUS_TYPE_INT32, &rval, DBUS_TYPE_INVALID);
+ }
+ dbus_message_unref(replyMsg );
}
else
{
- dbus_message_get_args(replyMsg, &error, DBUS_TYPE_INT32, &rval, DBUS_TYPE_INVALID);
+ fprintf(stderr, "send_pas_request ==> reply messgae is NULL!\n ==> error msg: %s \n", error.message);
}
-
- dbus_message_unref(message);
}
else
{
diff --git a/src/persistence_client_library_prct_access.c b/src/persistence_client_library_prct_access.c
index 8ef8e71..e044f6f 100644
--- a/src/persistence_client_library_prct_access.c
+++ b/src/persistence_client_library_prct_access.c
@@ -103,11 +103,20 @@ itzam_btree* get_resource_cfg_table(PersistenceRCT_e rct, int group)
state = itzam_btree_open(&gResource_table[arrayIdx], filename, itzam_comparator_string, error_handler, 0 , 0);
if(state != ITZAM_OKAY)
{
- fprintf(stderr, "\nget_resource_cfg_table => Itzam problem: %s\n", STATE_MESSAGES[state]);
+ fprintf(stderr, "\n** get_resource_cfg_table => Itzam problem: %s**\n", STATE_MESSAGES[state]);
+ tree = NULL;
+ }
+ else
+ {
+ gResourceOpen[arrayIdx] = 1; // remember the index has an DB entry
+ tree = &gResource_table[arrayIdx];
}
- gResourceOpen[arrayIdx] = 1; // remember the index has an DB entry
}
- tree = &gResource_table[arrayIdx];
+ else
+ {
+ tree = &gResource_table[arrayIdx];
+ }
+
}
return tree;
diff --git a/test/Makefile.am b/test/Makefile.am
index 8977079..79e51bc 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -9,7 +9,7 @@ endif
-noinst_PROGRAMS = persistence_client_library_test persistence_client_library_dbus_test
+noinst_PROGRAMS = persistence_client_library_test persistence_client_library_dbus_test persistence_admin_service_mockup persistence_lifeCycle_mockup
persistence_client_library_dbus_test_SOURCES = persistence_client_library_dbus_test.c
persistence_client_library_dbus_test_LDADD = $(DEPS_LIBS) $(top_srcdir)/src/libpersistence_client_library.la
@@ -17,5 +17,11 @@ persistence_client_library_dbus_test_LDADD = $(DEPS_LIBS) $(top_srcdir)/src/libp
persistence_client_library_test_SOURCES = persistence_client_library_test.c
persistence_client_library_test_LDADD = $(DEPS_LIBS) $(CHECK_LIBS) \
$(top_srcdir)/src/libpersistence_client_library.la
-
+
+persistence_admin_service_mockup_SOURCES = persistence_admin_service_mockup.c
+persistence_admin_service_mockup_LDADD = $(DEPS_LIBS)
+
+persistence_lifeCycle_mockup_SOURCES = persistence_lifeCycle_mockup.c
+persistence_lifeCycle_mockup_LDADD = $(DEPS_LIBS)
+
TESTS=persistence_client_library_test
diff --git a/test/persistence_client_library_dbus_test.c b/test/persistence_client_library_dbus_test.c
index fdad403..f30d5c5 100644
--- a/test/persistence_client_library_dbus_test.c
+++ b/test/persistence_client_library_dbus_test.c
@@ -26,11 +26,10 @@
int main(int argc, char *argv[])
{
int ret = 0;
- unsigned char buffer[1024];
printf("Dbus interface test application\n");
- getchar();
- ret = pclKeyReadData(0xFF, "pos/last_position", 0, 0, buffer, 1024);
+ printf("Press a key to end application\n");
+ ret = pclKeyHandleOpen(0xFF, "posHandle/last_position", 0, 0);
getchar();