Added timeout to poll function in dbus mainloop
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_dbus_service.c
index 0ea4fad..baa1906 100644 (file)
@@ -20,6 +20,9 @@
 #include "persistence_client_library_dbus_service.h"
 #include "persistence_client_library_lc_interface.h"
 #include "persistence_client_library_pas_interface.h"
+#include "persistence_client_library_dbus_cmd.h"
+#include "persistence_client_library_data_organization.h"
+
 
 #include <stdio.h>
 #include <errno.h>
 #include <stdlib.h>
 
 
-pthread_mutex_t gDbusInitializedMtx  = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t  gDbusInitializedCond = PTHREAD_COND_INITIALIZER;
+pthread_mutex_t gDbusInitializedMtx  = PTHREAD_MUTEX_INITIALIZER;
+
+pthread_mutex_t gDbusPendingRegMtx   = PTHREAD_MUTEX_INITIALIZER;
+
+
+pthread_mutex_t gDeliverpMtx         = PTHREAD_MUTEX_INITIALIZER;
+
+pthread_mutex_t gMainCondMtx         = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t  gMainLoopCond        = PTHREAD_COND_INITIALIZER;
+
+pthread_t gMainLoopThread;
+
+
+int gEfds;  // communication channel int dbus mainloop
+
 
 typedef enum EDBusObjectType
 {
@@ -63,29 +80,7 @@ typedef struct SPollInfo
 /// polling information
 static tPollInfo gPollInfo;
 
-
-/// dbus connection
-DBusConnection* gDbusConn = NULL;
-
-
-DBusConnection* get_dbus_connection(void)
-{
-   return gDbusConn;
-}
-
-//------------------------------------------------------------------------
-// debugging only until "correct" exit of main loop is possible!!!!!
-//------------------------------------------------------------------------
-#include "signal.h"
-static int endLoop = 0;
-
-void sigHandler(int signo)
-{
-   endLoop = 1;
-}
-//------------------------------------------------------------------------
-
-
+int bContinue = 0;
 
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
 
@@ -93,37 +88,108 @@ void sigHandler(int signo)
 /* function to unregister ojbect path message handler */
 static void unregisterMessageHandler(DBusConnection *connection, void *user_data)
 {
-   printf("unregisterObjectPath\n");
+   (void)connection;
+   (void)user_data;
+   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPath\n"));
 }
 
 /* catches messages not directed to any registered object path ("garbage collector") */
 static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connection, DBusMessage * message, void * user_data)
 {
    DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
-
-   printf("handleObjectPathMessageFallback Object: '%s' -> Interface: '%s' -> Message: '%s'\n",
-          dbus_message_get_sender(message), dbus_message_get_interface(message), dbus_message_get_member(message) );
+   (void)user_data;
 
    // org.genivi.persistence.admin  S I G N A L
    if((0==strcmp("org.genivi.persistence.admin", dbus_message_get_interface(message))))
    {
-      // printf("checkPersAdminSignalInterface '%s' -> '%s'\n", dbus_message_get_interface(message), dbus_message_get_member(message));
       if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
       {
-         printf("  checkPersAdminSignal signal\n");
          if((0==strcmp("PersistenceModeChanged", dbus_message_get_member(message))))
          {
-            printf("  checkPersAdminSignal message\n");
             // to do handle signal
             result = signal_persModeChange(connection, message);
          }
          else
          {
-            printf("handleObjectPathMessageFallback -> unknown signal '%s'\n", dbus_message_get_interface(message));
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown signal:"), DLT_STRING(dbus_message_get_interface(message)) );
          }
       }
    }
+   // org.genivi.persistence.admin  S I G N A L
+   else if((0==strcmp("org.genivi.persistence.adminconsumer", dbus_message_get_interface(message))))
+   {
+      if(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
+      {
+         pclNotification_s notifyStruct;
+         int validMessage = 0;
 
+         if((0==strcmp("PersistenceResChange", dbus_message_get_member(message))))
+         {
+            notifyStruct.pclKeyNotify_Status = pclNotifyStatus_changed;
+            validMessage = 1;
+         }
+         else if((0==strcmp("PersistenceResDelete", dbus_message_get_member(message))))
+         {
+            notifyStruct.pclKeyNotify_Status = pclNotifyStatus_deleted;
+            validMessage = 1;
+         }
+         else if((0==strcmp("PersistenceRes", dbus_message_get_member(message))))
+         {
+            notifyStruct.pclKeyNotify_Status = pclNotifyStatus_created;
+            validMessage = 1;
+         }
+
+         if(validMessage == 1)
+         {
+            DBusError error;
+            DBusMessage *reply;
+            dbus_error_init (&error);
+            char* ldbid;
+            char* user_no;
+            char* seat_no;
+
+            if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &notifyStruct.resource_id,
+                                                         DBUS_TYPE_STRING, &ldbid,
+                                                         DBUS_TYPE_STRING, &user_no,
+                                                         DBUS_TYPE_STRING, &seat_no,
+                                                         DBUS_TYPE_INVALID))
+            {
+               reply = dbus_message_new_error(message, error.name, error.message);
+
+               if (reply == 0)
+               {
+                  DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
+               }
+
+               if (!dbus_connection_send(connection, reply, 0))
+               {
+                  DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => DBus No memory"), DLT_STRING(dbus_message_get_interface(message)) );
+               }
+
+               result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;;
+               dbus_message_unref(reply);
+            }
+            else
+            {
+               notifyStruct.ldbid       = atoi(ldbid);
+               notifyStruct.user_no     = atoi(user_no);
+               notifyStruct.seat_no     = atoi(seat_no);
+
+               // call the registered callback function
+               if(gChangeNotifyCallback != NULL )
+               {
+                  gChangeNotifyCallback(&notifyStruct);
+               }
+               else
+               {
+                  DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback => gChangeNotifyCallback is not set (possibly NULL)") );
+               }
+               result = DBUS_HANDLER_RESULT_HANDLED;
+            }
+            dbus_connection_flush(connection);
+         }
+      }
+   }
    // org.genivi.persistence.admin  P R O P E R T Y
    else  if((0==strcmp("org.freedesktop.DBus.Properties", dbus_message_get_interface(message))))
    {
@@ -147,21 +213,19 @@ static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connec
             dbus_message_iter_close_container(&dict, &variant);
             dbus_message_iter_close_container(&array, &dict);
 
-            printf("handleObjectPathMessageFallback ==> value: %d \n", value);
             // to do handle signal
             result = DBUS_HANDLER_RESULT_HANDLED;
          }
          else
          {
-            printf("handleObjectPathMessageFallback -> unknown property '%s'\n", dbus_message_get_interface(message));
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> unknown property:"), DLT_STRING(dbus_message_get_interface(message)) );
          }
       }
       else
       {
-         printf("handleObjectPathMessageFallback -> not a signal '%s'\n", dbus_message_get_member(message));
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("handleObjectPathMessageFallback -> not a signal:"), DLT_STRING(dbus_message_get_member(message)) );
       }
    }
-
    return result;
 }
 
@@ -169,7 +233,9 @@ static DBusHandlerResult handleObjectPathMessageFallback(DBusConnection * connec
 
 static void  unregisterObjectPathFallback(DBusConnection *connection, void *user_data)
 {
-   printf("unregisterObjectPathFallback\n");
+   (void)connection;
+   (void)user_data;
+   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("unregisterObjectPathFallback\n"));
 }
 
 
@@ -178,21 +244,19 @@ void* run_mainloop(void* dataPtr)
 {
    // persistence admin message
    static const struct DBusObjectPathVTable vtablePersAdmin
-      = {unregisterMessageHandler, checkPersAdminMsg, NULL, };
+      = {unregisterMessageHandler, checkPersAdminMsg, NULL, NULL, NULL, NULL};
 
    // lifecycle message
    static const struct DBusObjectPathVTable vtableLifecycle
-      = {unregisterMessageHandler, checkLifecycleMsg, NULL, };
+      = {unregisterMessageHandler, checkLifecycleMsg, NULL, NULL, NULL, NULL};
 
    // fallback
    static const struct DBusObjectPathVTable vtableFallback
-      = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, };
+      = {unregisterObjectPathFallback, handleObjectPathMessageFallback, NULL, NULL, NULL, NULL};
 
    // setup the dbus
    mainLoop(vtablePersAdmin, vtableLifecycle, vtableFallback, dataPtr);
 
-   printf("Exit dbus main loop!!!!\n");
-
    return NULL;
 }
 
@@ -201,53 +265,55 @@ void* run_mainloop(void* dataPtr)
 int setup_dbus_mainloop(void)
 {
    int rval = 0;
-   pthread_t thread;
    DBusError err;
+   DBusConnection* conn = NULL;
+
    const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS");
+
    dbus_error_init(&err);
 
-   // enable locking of data structures in the D-Bus library for multi threading.
-   dbus_threads_init_default();
+   // wait until dbus main loop has been setup and running
+   pthread_mutex_lock(&gDbusInitializedMtx);
 
    // Connect to the bus and check for errors
    if(pAddress != NULL)
    {
-      printf("Use specific dbus address: %s\n !", pAddress);
-      gDbusConn = dbus_connection_open(pAddress, &err);
+      DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("setup_dbus_mainloop -> Use specific dbus address:"), DLT_STRING(pAddress) );
+
+      conn = dbus_connection_open_private(pAddress, &err);
 
-      if(gDbusConn != NULL)
+      if(conn != NULL)
       {
-         if(!dbus_bus_register(gDbusConn, &err))
+         if(!dbus_bus_register(conn, &err))
          {
-            printf("dbus_bus_register() Error %s\n", err.message);
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_bus_register() Error :"), DLT_STRING(err.message) );
             dbus_error_free (&err);
+            pthread_mutex_unlock(&gDbusInitializedMtx);
             return -1;
          }
-         else
-         {
-            printf("Registered connection successfully !\n");
-         }
       }
       else
       {
-         printf("dbus_connection_open() Error %s\n",err.message);
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("dbus_connection_open_private() Error :"), DLT_STRING(err.message) );
          dbus_error_free(&err);
+         pthread_mutex_unlock(&gDbusInitializedMtx);
+         return -1;
       }
    }
    else
    {
-      printf("Use default dbus bus (DBUS_BUS_SYSTEM) !!!!!!\n");
-      gDbusConn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
-   }
+      DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("Use default dbus bus (DBUS_BUS_SYSTEM)"));
 
-   // wain until dbus main loop has been setup and running
-   pthread_mutex_lock(&gDbusInitializedMtx);
+      conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
+   }
 
    // create here the dbus connection and pass to main loop
-   rval = pthread_create(&thread, NULL, run_mainloop, gDbusConn);
+   rval = pthread_create(&gMainLoopThread, NULL, run_mainloop, conn);
    if(rval)
    {
-     fprintf(stderr, "Server: - ERROR! pthread_create( run_mainloop ) returned: %d\n", rval);
+     DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("pthread_create( DBUS run_mainloop ) returned an error:"), DLT_INT(rval) );
+     pthread_mutex_unlock(&gDbusInitializedMtx);
+     return -1;
    }
 
    // wait for condition variable
@@ -264,8 +330,7 @@ int setup_dbus_mainloop(void)
 static dbus_bool_t addWatch(DBusWatch *watch, void *data)
 {
    dbus_bool_t result = FALSE;
-
-   //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':'-');
+   (void)data;
 
    if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
    {
@@ -301,20 +366,36 @@ static dbus_bool_t addWatch(DBusWatch *watch, void *data)
 
 static void removeWatch(DBusWatch *watch, void *data)
 {
-   printf("removeWatch called @0x%08x\n", (int)watch);
+   void* w_data = dbus_watch_get_data(watch);
+
+   (void)data;
+
+   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("removeWatch called "), DLT_INT( (int)watch) );
+
+   if(w_data)
+      free(w_data);
+
+   dbus_watch_set_data(watch, NULL, NULL);
 }
 
 
 
 static void watchToggled(DBusWatch *watch, void *data)
 {
-   printf("watchToggled called @0x%08x\n", (int)watch);
+   (void)data;
+   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("watchToggled called "), DLT_INT( (int)watch) );
+
+   if(dbus_watch_get_enabled(watch))
+      addWatch(watch, data);
+   else
+      removeWatch(watch, data);
 }
 
 
 
 static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
 {
+   (void)data;
    dbus_bool_t ret = FALSE;
 
    if (ARRAY_SIZE(gPollInfo.fds)>gPollInfo.nfds)
@@ -338,18 +419,18 @@ static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
             }
             else
             {
-               fprintf(stderr, "timerfd_settime() failed %d '%s'\n", errno, strerror(errno));
+               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => timerfd_settime() failed"), DLT_STRING(strerror(errno)) );
             }
          }
          else
          {
-            fprintf(stderr, "timerfd_create() failed %d '%s'\n", errno, strerror(errno));
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => timerfd_create() failed"), DLT_STRING(strerror(errno)) );
          }
       }
    }
    else
    {
-      fprintf(stderr, "cannot create another fd to be poll()'ed\n");
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("addTimeout => cannot create another fd to be poll()'ed"));
    }
 
    return ret;
@@ -359,15 +440,16 @@ static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data)
 
 static void removeTimeout(DBusTimeout *timeout, void *data)
 {
-
    int i = gPollInfo.nfds;
+   (void)data;
+
    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));
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("removeTimeout => close() timerfd"), DLT_STRING(strerror(errno)) );
       }
 
       --gPollInfo.nfds;
@@ -389,15 +471,17 @@ static void removeTimeout(DBusTimeout *timeout, void *data)
 static void timeoutToggled(DBusTimeout *timeout, void *data)
 {
    int i = gPollInfo.nfds;
+   (void)data;
+
    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);
+   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("timeoutToggled") );
    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));
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("timeoutToggled => timerfd_settime()"), DLT_STRING(strerror(errno)) );
       }
    }
 }
@@ -411,30 +495,24 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
    // lock mutex to make sure dbus main loop is running
    pthread_mutex_lock(&gDbusInitializedMtx);
 
-   signal(SIGTERM, sigHandler);
-   signal(SIGQUIT, sigHandler);
-   signal(SIGINT,  sigHandler);
-
    DBusConnection* conn = (DBusConnection*)userData;
    dbus_error_init(&err);
 
    if (dbus_error_is_set(&err))
    {
-      printf("Connection Error (%s)\n", err.message);
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => Connection Error:"), DLT_STRING(err.message) );
       dbus_error_free(&err);
    }
    else if (NULL != conn)
    {
-      dbus_connection_set_exit_on_disconnect (conn, FALSE);
-      //printf("connected as '%s'\n", dbus_bus_get_unique_name(conn));
+      dbus_connection_set_exit_on_disconnect(conn, FALSE);
       if (-1 == (gEfds = eventfd(0, 0)))
       {
-         printf("eventfd() failed w/ errno %d\n", errno);
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => eventfd() failed w/ errno:"), DLT_INT(errno) );
       }
       else
       {
          int ret;
-         int bContinue = 0;
          memset(&gPollInfo, 0 , sizeof(gPollInfo));
 
          gPollInfo.nfds = 1;
@@ -444,14 +522,16 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
          dbus_bus_add_match(conn, "type='signal',interface='org.genivi.persistence.admin',member='PersistenceModeChanged',path='/org/genivi/persistence/admin'", &err);
 
          // register for messages
-         if (   (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData))
-             && (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer", &vtable2, userData))
+         if (   (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer", &vtable2, userData))
+#if USE_PASINTERFACE == 1
+             && (TRUE==dbus_connection_register_object_path(conn, "/org/genivi/persistence/adminconsumer", &vtable, userData))
+#endif
              && (TRUE==dbus_connection_register_fallback(conn, "/", &vtableFallback, userData)) )
          {
             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");
+               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_connection_set_watch_functions() failed"));
             }
             else
             {
@@ -463,11 +543,11 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
 
                   while (DBUS_DISPATCH_DATA_REMAINS==dbus_connection_dispatch(conn));
 
-                  while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, -1)))&&(EINTR==errno));
+                  while ((-1==(ret=poll(gPollInfo.fds, gPollInfo.nfds, 750)))&&(EINTR==errno));
 
                   if (0>ret)
                   {
-                     fprintf(stderr, "poll() failed w/ errno %d\n", errno);
+                     DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => poll() failed w/ errno "), DLT_INT(errno) );
                   }
                   else if (0==ret)
                   {
@@ -482,52 +562,79 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
                         /* anything to do */
                         if (0!=gPollInfo.fds[i].revents)
                         {
-                           //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");
+                                 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read failed"));
                               }
-                              fprintf(stderr, "timeout %x #%d!\n", (int)gPollInfo.objects[i].timeout, (int)nExpCount);
+                              DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => timeout"));
+
                               if (FALSE==dbus_timeout_handle(gPollInfo.objects[i].timeout))
                               {
-                                 fprintf(stderr, "dbus_timeout_handle() failed!?\n");
+                                 DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => dbus_timeout_handle() failed!?"));
                               }
                               bContinue = TRUE;
                            }
-                           else if (gPollInfo.fds[i].fd==gEfds)
+                           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)
+                                 while ((-1==(ret = read(gPollInfo.fds[i].fd, buf, 64)))&&(EINTR == errno));
+                                 if(ret < 0)
                                  {
-                                    printf("read() failed w/ errno %d | %s\n", errno, strerror(errno));
+                                    DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => read() failed"), DLT_STRING(strerror(errno)) );
                                  }
-                                 else if (ret != -1)
+                                 else
                                  {
+                                    pthread_mutex_lock(&gMainCondMtx);
                                     switch (buf[0])
                                     {
                                        case CMD_PAS_BLOCK_AND_WRITE_BACK:
                                           process_block_and_write_data_back((buf[2]), buf[1]);
+                                          process_send_pas_request(conn, (buf[2]), buf[1]);
                                           break;
                                        case CMD_LC_PREPARE_SHUTDOWN:
-                                          process_prepare_shutdown((buf[2]), buf[1]);
+                                          process_prepare_shutdown(Shutdown_Full);
+                                          process_send_lifecycle_request(conn, (buf[2]), buf[1]);
+                                          break;
+                                       case CMD_SEND_NOTIFY_SIGNAL:
+                                          process_send_notification_signal(conn);
+                                          break;
+                                       case CMD_REG_NOTIFY_SIGNAL:
+                                          process_reg_notification_signal(conn);
+                                          break;
+                                       case CMD_SEND_PAS_REGISTER:
+                                          process_send_pas_register(conn, (buf[1]), buf[2]);
+                                          break;
+                                       case CMD_SEND_LC_REGISTER:
+                                          process_send_lifecycle_register(conn, (buf[1]), buf[2]);
                                           break;
                                        case CMD_QUIT:
-                                          bContinue = FALSE;
+                                          bContinue = 0;
                                           break;
+
+                                       // ******************************************************
+                                       /*
+                                       case CMD_SEND_LC_REQUEST:  // remove
+                                         process_send_lifecycle_request(conn, (buf[2]), buf[1]);
+                                         break;
+                                      case CMD_SEND_PAS_REQUEST: /// remove
+                                         process_send_pas_request(conn, (buf[2]), buf[1]);
+                                         break;
+                                       */
+                                       // ******************************************************
                                        default:
-                                          printf("command %d not handled!\n", buf[0]);
+                                          DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("mainLoop => command not handled"), DLT_INT(buf[0]) );
                                           break;
                                     }
+                                    pthread_cond_signal(&gMainLoopCond);
+                                    pthread_mutex_unlock(&gMainCondMtx);
                                  }
                               }
                            }
@@ -556,17 +663,18 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
                         }
                      }
                   }
-                  if(endLoop == 1)
-                     break;
                }
                while (0!=bContinue);
             }
+#if USE_PASINTERFACE == 1
             dbus_connection_unregister_object_path(conn, "/org/genivi/persistence/adminconsumer");
+#endif
             dbus_connection_unregister_object_path(conn, "/org/genivi/NodeStateManager/LifeCycleConsumer");
             dbus_connection_unregister_object_path(conn, "/");
          }
          close(gEfds);
       }
+      dbus_connection_close(conn);
       dbus_connection_unref(conn);
       dbus_shutdown();
    }
@@ -576,3 +684,41 @@ int mainLoop(DBusObjectPathVTable vtable, DBusObjectPathVTable vtable2,
    return 0;
 }
 
+
+
+int deliverToMainloop(tCmd mainloopCmd, unsigned int param1, unsigned int param2)
+{
+   int rval = 0;
+
+   pthread_mutex_lock(&gDeliverpMtx);
+
+
+   pthread_mutex_lock(&gMainCondMtx);
+
+   deliverToMainloop_NM(mainloopCmd, param1, param2);
+
+   pthread_cond_wait(&gMainLoopCond, &gMainCondMtx);
+   pthread_mutex_unlock(&gMainCondMtx);
+
+
+   pthread_mutex_unlock(&gDeliverpMtx);
+
+   return rval;
+}
+
+int deliverToMainloop_NM(tCmd mainloopCmd, unsigned int param1, unsigned int param2)
+{
+   int rval = 0;
+   uint64_t cmd;
+
+   cmd = ( ((uint64_t)param2 << 32) | ((uint64_t)param1 << 16) | mainloopCmd);
+
+   if(-1 == write(gEfds, &cmd, (sizeof(uint64_t))))
+   {
+     DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("deliverToMainloop => failed to write to pipe"), DLT_INT(errno));
+     rval = -1;
+   }
+
+   return rval;
+}
+