Missing files for prevoius commitID; updated header documentation
[profile/ivi/persistence-client-library.git] / src / persistence_client_library_dbus_cmd.c
index c3f69af..89c019c 100644 (file)
  * @see
  */
 
+#include <errno.h>
+#include <dlfcn.h>                                                                                                                             /* For dlclose() */
 #include "persistence_client_library_dbus_cmd.h"
 
 #include "persistence_client_library_handle.h"
-#include "persistence_client_library_itzam_errors.h"
 #include "persistence_client_library_custom_loader.h"
 #include "persistence_client_library_prct_access.h"
+#include "persistence_client_library_pas_interface.h"
+#include "persistence_client_library_data_organization.h"
+#include "persistence_client_library_db_access.h"
 
-#include "../include_protected/persistence_client_library_data_organization.h"
-#include "../include_protected/persistence_client_library_db_access.h"
+#if USE_FILECACHE
+   #include <persistence_file_cache.h>
 
-#include <itzam.h>
+
+       /**
+        * write back from cache to non volatile memory device
+        * ATTENTION:
+        * THIS FUNCTION IS NOT INTENDED TO BE USED BY A NORMAL APPLICATION.
+        * ONLY SPECIAL APPLICATION ARE ALLOWED TO USING USE THIS FUNCTION
+        **/
+        extern int pfcWriteBackAndSync(int handle);
+#endif
 
 
 // function prototype
@@ -34,38 +46,41 @@ void msg_pending_func(DBusPendingCall *call, void *data);
 
 
 
-void process_reg_notification_signal(DBusConnection* conn)
+void process_reg_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
+                                                           unsigned int notifySeatNo, unsigned int notifyPolicy, const char* notifyKey)
 {
-   char ruleChanged[DbusMatchRuleSize] = {0};
-   char ruleDeleted[DbusMatchRuleSize] = {0};
-   char ruleCreated[DbusMatchRuleSize] = {0};
+   char ruleChanged[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
+   char ruleDeleted[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
+   char ruleCreated[DbusMatchRuleSize] = {[0 ... DbusMatchRuleSize-1] = 0};
 
    // add match for  c h a n g e
    snprintf(ruleChanged, DbusMatchRuleSize,
             "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResChange',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
-            gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
+            notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
 
    // add match for  d e l e t e
    snprintf(ruleDeleted, DbusMatchRuleSize,
             "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResDelete',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
-            gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
+            notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
 
    // add match for  c r e a t e
    snprintf(ruleCreated, DbusMatchRuleSize,
             "type='signal',interface='org.genivi.persistence.adminconsumer',member='PersistenceResCreate',path='/org/genivi/persistence/adminconsumer',arg0='%s',arg1='%u',arg2='%u',arg3='%u'",
-            gNotifykey, gNotifyLdbid, gNotifyUserNo, gNotifySeatNo);
+            notifyKey, notifyLdbid, notifyUserNo, notifySeatNo);
 
-   if(gNotifyPolicy == Notify_register)
+   if(notifyPolicy == Notify_register)
    {
       dbus_bus_add_match(conn, ruleChanged, NULL);
       dbus_bus_add_match(conn, ruleDeleted, NULL);
       dbus_bus_add_match(conn, ruleCreated, NULL);
+      DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("Registered for change notifications:"), DLT_STRING(ruleChanged));
    }
-   else if(gNotifyPolicy == Notify_unregister)
+   else if(notifyPolicy == Notify_unregister)
    {
       dbus_bus_remove_match(conn, ruleChanged, NULL);
       dbus_bus_remove_match(conn, ruleDeleted, NULL);
       dbus_bus_remove_match(conn, ruleCreated, NULL);
+      DLT_LOG(gPclDLTContext, DLT_LOG_VERBOSE, DLT_STRING("Unregistered for change notifications:"), DLT_STRING(ruleChanged));
    }
 
    dbus_connection_flush(conn);  // flush the connection to add the match
@@ -73,49 +88,50 @@ void process_reg_notification_signal(DBusConnection* conn)
 
 
 
-void process_send_notification_signal(DBusConnection* conn)
+void process_send_notification_signal(DBusConnection* conn, unsigned int notifyLdbid, unsigned int notifyUserNo,
+                                                            unsigned int notifySeatNo, unsigned int notifyReason, const char* notifyKey)
 {
    dbus_bool_t ret;
    DBusMessage* message;
-   const char* notifyReason = NULL;
+   const char* notifyReasonString = NULL;
 
-   char ldbidArray[DbusSubMatchSize] = {0};
-   char userArray[DbusSubMatchSize]  = {0};
-   char seatArray[DbusSubMatchSize]  = {0};
+   char ldbidArray[DbusSubMatchSize] = {[0 ... DbusSubMatchSize-1] = 0};
+   char userArray[DbusSubMatchSize]  = {[0 ... DbusSubMatchSize-1] = 0};
+   char seatArray[DbusSubMatchSize]  = {[0 ... DbusSubMatchSize-1] = 0};
    char* pldbidArra = ldbidArray;
    char* puserArray = userArray;
    char* pseatArray = seatArray;
-   char* pnotifyKey = gNotifykey;
+   char* pnotifyKey = (char*)notifyKey;
 
-   switch(gNotifyReason)
+   switch(notifyReason)
    {
       case pclNotifyStatus_deleted:
-         notifyReason = gDeleteSignal;
+       notifyReasonString = gDeleteSignal;
          break;
       case  pclNotifyStatus_created:
-         notifyReason = gCreateSignal;
+       notifyReasonString = gCreateSignal;
          break;
       case pclNotifyStatus_changed:
-         notifyReason = gChangeSignal;
+       notifyReasonString = gChangeSignal;
          break;
       default:
-         notifyReason = NULL;
+       notifyReasonString = NULL;
          break;
    }
 
-   if(notifyReason != NULL)
+   if(notifyReasonString != NULL)
    {
       // dbus_bus_add_match is used for the notification mechanism,
       // and this works only for type DBUS_TYPE_STRING as message arguments
       // this is the reason to use string instead of integer types directly
-      snprintf(ldbidArray, DbusSubMatchSize, "%u", gNotifyLdbid);
-      snprintf(userArray,  DbusSubMatchSize, "%u", gNotifyUserNo);
-      snprintf(seatArray,  DbusSubMatchSize, "%u", gNotifySeatNo);
+      snprintf(ldbidArray, DbusSubMatchSize, "%u", notifyLdbid);
+      snprintf(userArray,  DbusSubMatchSize, "%u", notifyUserNo);
+      snprintf(seatArray,  DbusSubMatchSize, "%u", notifySeatNo);
 
-      //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", gNotifykey, gLdbid, gUserNo, gSeatNo, gReason);
-      message = dbus_message_new_signal("/org/genivi/persistence/adminconsumer",    // const char *path,
-                                        "org.genivi.persistence.adminconsumer",     // const char *interface,
-                                        notifyReason);                                 // const char *name
+      //printf("process_send_Notification_Signal => key: %s | lbid: %d | gUserNo: %d | gSeatNo: %d | gReason: %d \n", notifyKey, notifyLdbid, notifyUserNo, notifySeatNo, notifyReason);
+      message = dbus_message_new_signal(gPersAdminConsumerPath,
+                                           gDbusPersAdminConsInterface,
+                                        notifyReasonString);
 
       ret = dbus_message_append_args(message, DBUS_TYPE_STRING, &pnotifyKey,
                                               DBUS_TYPE_STRING, &pldbidArra,
@@ -134,22 +150,22 @@ void process_send_notification_signal(DBusConnection* conn)
             }
             else
             {
-               DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> failed to send dbus message!!"));
+               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal - failed to send dbus message!!"));
             }
          }
          else
          {
-            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> E R R O R  C O N E C T I O N  NULL!!"));
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal - C O N E C T I O N  NULL!!"));
          }
       }
       else
       {
-         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR dbus_message_append_args"));
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal - dbus_message_append_args"));
       }
    }
    else
    {
-      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal ==> ERROR invalid notification reason"));
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_Notification_Signal - invalid notification reason"));
    }
 }
 
@@ -157,21 +173,20 @@ void process_send_notification_signal(DBusConnection* conn)
 
 void process_block_and_write_data_back(unsigned int requestID, unsigned int status)
 {
+   (void)requestID;
+   (void)status;
    // lock persistence data access
    pers_lock_access();
    // sync data back to memory device
-   pers_data_sync();
-   // send complete notification
-   //pers_admin_service_data_sync_complete(requestID, status);
 }
 
 
 
-void process_prepare_shutdown(unsigned char requestId, unsigned int status)
+void process_prepare_shutdown(int complete)
 {
-   int i = 0;
-   itzam_btree* resourceTable = NULL;
-   itzam_state  state = ITZAM_FAILED;
+   int i = 0, rval = 0;
+
+   DLT_LOG(gPclDLTContext, DLT_LOG_INFO, DLT_STRING("process_prepare_shutdown - writing down all changed data and closing all handles"));
 
    // block write
    pers_lock_access();
@@ -179,46 +194,63 @@ void process_prepare_shutdown(unsigned char requestId, unsigned int status)
    // flush open files to disk
    for(i=0; i<MaxPersHandle; i++)
    {
-      int tmp = i;
-      if(gOpenFdArray[tmp] == FileOpen)
+      if(gOpenFdArray[i] == FileOpen)
       {
-         fsync(tmp);
-         close(tmp);
+
+#if USE_FILECACHE
+         if(complete == Shutdown_Full)
+         {
+               rval = pfcCloseFile(i);
+         }
+         else if(complete == Shutdown_Partial)
+         {
+               pfcWriteBackAndSync(i);
+         }
+#else
+         if(complete == Shutdown_Full)
+         {
+               rval = close(i);
+         }
+         else if(complete == Shutdown_Partial)
+         {
+               fsync(i);
+         }
+#endif
+         if(rval == -1)
+         {
+               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown - failed to close file: "), DLT_STRING(strerror(errno)) );
+         }
+
       }
    }
 
-   // close open gvdb persistence resource configuration table
-   for(i=0; i< PrctDbTableSize; i++)
+   // close all opend rct
+   pers_rct_close_all();
+
+   // close opend database
+   database_close_all();
+
+   if(complete > 0)
    {
-     resourceTable = get_resource_cfg_table_by_idx(i);
-     // dereference opend database
-     if(resourceTable != NULL &&  get_resource_cfg_table_status(i) == 1)
-     {
-        state = itzam_btree_close(resourceTable);
-        invalidate_resource_cfg_table(i);
-        if (state != ITZAM_OKAY)
-        {
-           DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("process_prepare_shutdown => itzam_btree_close: Itzam problem"), DLT_STRING(STATE_MESSAGES[state]));
-        }
-     }
+       close_all_persistence_handle();
    }
 
-   //close opend database
-   pers_db_close_all();
-
 
-   // unload custom client libraries
-   for(i=0; i<PersCustomLib_LastEntry; i++)
+   if(complete > 0)
    {
-      if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
-      {
-         // deinitialize plugin
-         gPersCustomFuncs[i].custom_plugin_deinit();
-         // close library handle
-         dlclose(gPersCustomFuncs[i].handle);
-
-         invalidate_custom_plugin(i);
-      }
+               // unload custom client libraries
+               for(i=0; i<PersCustomLib_LastEntry; i++)
+               {
+                       if(gPersCustomFuncs[i].custom_plugin_deinit != NULL)
+                       {
+                               // deinitialize plugin
+                               gPersCustomFuncs[i].custom_plugin_deinit();
+                               // close library handle
+                               dlclose(gPersCustomFuncs[i].handle);
+
+                               invalidate_custom_plugin(i);
+                       }
+               }
    }
 }
 
@@ -228,11 +260,10 @@ void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int
 {
    DBusError error;
    dbus_error_init (&error);
-   int rval = 0;
 
-   DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin",       // destination
-                                                      "/org/genivi/persistence/admin",       // path
-                                                       "org.genivi.persistence.admin",       // interface
+   DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface,                        // destination
+                                                             gDbusPersAdminPath,               // path
+                                                             gDbusPersAdminInterface,          // interface
                                                        "PersistenceAdminRequestCompleted");  // method
    if(conn != NULL)
    {
@@ -244,8 +275,7 @@ void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int
 
          if(!dbus_connection_send(conn, message, 0))
          {
-            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
-            rval = -1;
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register - Access denied"), DLT_STRING(error.message) );
          }
 
          dbus_connection_flush(conn);
@@ -253,12 +283,12 @@ void process_send_pas_request(DBusConnection* conn, unsigned int requestID, int
       }
       else
       {
-         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid message") );
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request - Invalid message") );
       }
    }
    else
    {
-      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request => ERROR: Invalid connection") );
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_request - Invalid connection") );
    }
 }
 
@@ -278,20 +308,19 @@ void process_send_pas_register(DBusConnection* conn, int regType, int notificati
 
    if(conn != NULL)
    {
-      const char* objName = "/org/genivi/persistence/adminconsumer";
       const char* busName = dbus_bus_get_unique_name(conn);
 
       if(busName != NULL)
       {
-         DBusMessage* message = dbus_message_new_method_call("org.genivi.persistence.admin",    // destination
-                                                            "/org/genivi/persistence/admin",    // path
-                                                             "org.genivi.persistence.admin",    // interface
-                                                             method);                           // method
+         DBusMessage* message = dbus_message_new_method_call(gDbusPersAdminInterface,   // destination
+                                                                     gDbusPersAdminPath,               // path
+                                                                     gDbusPersAdminInterface,   // interface
+                                                             method);               // method
 
          if(message != NULL)
          {
             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,  // bus name
-                                              DBUS_TYPE_STRING, &objName,
+                                              DBUS_TYPE_STRING, &gPersAdminConsumerPath,
                                               DBUS_TYPE_INT32,  &notificationFlag,
                                               DBUS_TYPE_UINT32, &gTimeoutMs,
                                               DBUS_TYPE_INVALID);
@@ -305,32 +334,30 @@ void process_send_pas_register(DBusConnection* conn, int regType, int notificati
 
             if(!dbus_pending_call_set_notify(pending, msg_pending_func, method, NULL))
             {
-               printf("process_send_pas_register => dbus_pending_call_set_notify: FAILED\n");
+               DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("process_send_pas_register - dbus_pending_call_set_notify: FAILED\n") );
             }
             dbus_pending_call_unref(pending);
          }
          else
          {
-            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid message") );
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register - Invalid message") );
          }
          dbus_message_unref(message);
       }
       else
       {
-         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid busname") );
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register - Invalid busname") );
       }
    }
    else
    {
-      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register =>  ERROR: Invalid connection") );
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_pas_register - Invalid connection") );
    }
 }
 
 
 void process_send_lifecycle_register(DBusConnection* conn, int regType, int shutdownMode)
 {
-   int rval = 0;
-
    DBusError error;
    dbus_error_init (&error);
 
@@ -343,61 +370,58 @@ void process_send_lifecycle_register(DBusConnection* conn, int regType, int shut
 
    if(conn != NULL)
    {
-      const char* objName = "/org/genivi/NodeStateManager/LifeCycleConsumer";
       const char* busName = dbus_bus_get_unique_name(conn);
 
-      DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
-                                                          "/org/genivi/NodeStateManager/Consumer", // path
-                                                          "org.genivi.NodeStateManager.Consumer",  // interface
+      DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest,           // destination
+                                                             gDbusLcCons, // path
+                                                             gDbusLcInterface,  // interface
                                                           method);                                 // method
       if(message != NULL)
       {
          if(regType == 1)   // register
          {
             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
-                                              DBUS_TYPE_STRING, &objName,
+                                              DBUS_TYPE_STRING, &gDbusLcConsPath,
                                               DBUS_TYPE_UINT32, &shutdownMode,
                                               DBUS_TYPE_UINT32, &gTimeoutMs, DBUS_TYPE_INVALID);
          }
          else           // unregister
          {
             dbus_message_append_args(message, DBUS_TYPE_STRING, &busName,
-                                              DBUS_TYPE_STRING, &objName,
+                                              DBUS_TYPE_STRING, &gDbusLcConsPath,
                                               DBUS_TYPE_UINT32, &shutdownMode, DBUS_TYPE_INVALID);
          }
 
                   if(!dbus_connection_send(conn, message, 0))
                   {
-                     DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => Access denied"), DLT_STRING(error.message) );
-                     rval = -1;
+                     DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register - Access denied"), DLT_STRING(error.message) );
                   }
                   dbus_connection_flush(conn);
          dbus_message_unref(message);
       }
       else
       {
-         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: Invalid message"));
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register - Invalid message"));
       }
    }
    else
    {
-      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register => ERROR: connection isn NULL"));
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_register - connection isn NULL"));
    }
 }
 
 
 
-void process_send_lifecycle_request(DBusConnection* conn, int requestId, int status)
+void process_send_lifecycle_request(DBusConnection* conn, unsigned int requestId, unsigned int status)
 {
-   int rval = 0;
    DBusError error;
    dbus_error_init (&error);
 
    if(conn != NULL)
    {
-      DBusMessage* message = dbus_message_new_method_call("org.genivi.NodeStateManager",           // destination
-                                                         "/org/genivi/NodeStateManager/Consumer",  // path
-                                                          "org.genivi.NodeStateManager.Consumer",  // interface
+      DBusMessage* message = dbus_message_new_method_call(gDbusLcConsDest,           // destination
+                                                             gDbusLcCons,  // path
+                                                             gDbusLcInterface,  // interface
                                                           "LifecycleRequestComplete");             // method
       if(message != NULL)
       {
@@ -408,8 +432,7 @@ void process_send_lifecycle_request(DBusConnection* conn, int requestId, int sta
 
          if(!dbus_connection_send(conn, message, 0))
          {
-            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => Access denied"), DLT_STRING(error.message) );
-            rval = -1;
+            DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request - Access denied"), DLT_STRING(error.message) );
           }
 
           dbus_connection_flush(conn);
@@ -417,14 +440,12 @@ void process_send_lifecycle_request(DBusConnection* conn, int requestId, int sta
       }
       else
       {
-         DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: Invalid message"));
-         rval = -1;
+         DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request - Invalid message"));
       }
    }
    else
    {
-      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request => ERROR: connection isn NULL"));
-      rval = -1;
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("send_lifecycle_request - connection isn NULL"));
    }
 }
 
@@ -436,11 +457,13 @@ void msg_pending_func(DBusPendingCall *call, void *data)
    DBusError err;
    dbus_error_init(&err);
 
+   (void)data;
+
    DBusMessage *message = dbus_pending_call_steal_reply(call);
 
    if (dbus_set_error_from_message(&err, message))
    {
-      DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func ==> Access denied") );
+      DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("msg_pending_func - Access denied") );
    }
    else
    {