* [ GAM-6 ] enhace routing algorithm: changed the way the routing algorithm gets...
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / DBusWrapper.cpp
index 5b81792..ac073a5 100644 (file)
 #include <fstream>
 #include <sstream>
 #include <string>
-#include <dlt/dlt.h>
-#include <assert.h>
-#include <stdlib.h>
+#include <cassert>
+#include <cstdlib>
+#include "DLTWrapper.h"
 
 using namespace am;
 
-DLT_IMPORT_CONTEXT(AudioManager)
-
 #define ROOT_INTROSPECT_XML                                                                                            \
 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                                                              \
 "<node>"                                                                                                                               \
@@ -58,16 +56,16 @@ DBusWrapper::DBusWrapper() :
         mListTimerhandlePointer()
 {
     dbus_error_init(&mDBusError);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::DBusWrapper Opening DBus connection"));
+    logInfo("DBusWrapper::DBusWrapper Opening DBus connection");
     mDbusConnection = dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
     if (dbus_error_is_set(&mDBusError))
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Error while getting the DBus"));
+        logError("DBusWrapper::DBusWrapper Error while getting the DBus");
         dbus_error_free(&mDBusError);
     }
     if (NULL == mDbusConnection)
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper DBus Connection is null"));
+        logError("DBusWrapper::DBusWrapper DBus Connection is null");
     }
 
     //first, we are old enought to live longer then the connection:
@@ -78,12 +76,12 @@ DBusWrapper::DBusWrapper() :
     int ret = dbus_bus_request_name(mDbusConnection, DBUS_SERVICE_PREFIX, DBUS_NAME_FLAG_DO_NOT_QUEUE, &mDBusError);
     if (dbus_error_is_set(&mDBusError))
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Name Error"), DLT_STRING(mDBusError.message));
+        logError("DBusWrapper::DBusWrapper Name Error",mDBusError.message);
         dbus_error_free(&mDBusError);
     }
     if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner"), DLT_INT(ret));
+        logError("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner",ret);
         exit(1);
     }
 }
@@ -102,16 +100,16 @@ DBusWrapper::DBusWrapper(SocketHandler* socketHandler) :
     assert(mSocketHandler!=0);
 
     dbus_error_init(&mDBusError);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::DBusWrapper Opening DBus connection"));
+    logInfo("DBusWrapper::DBusWrapper Opening DBus connection");
     mDbusConnection = dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
     if (dbus_error_is_set(&mDBusError))
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Error while getting the DBus"));
+        logError("DBusWrapper::DBusWrapper Error while getting the DBus");
         dbus_error_free(&mDBusError);
     }
     if (NULL == mDbusConnection)
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper DBus Connection is null"));
+        logError("DBusWrapper::DBusWrapper DBus Connection is null");
     }
 
     //then we need to adopt the dbus to our mainloop:
@@ -125,14 +123,14 @@ DBusWrapper::DBusWrapper(SocketHandler* socketHandler) :
     dbus_bool_t watch = dbus_connection_set_watch_functions(mDbusConnection, addWatch, removeWatch, toogleWatch, this, NULL);
     if (!watch)
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Registering of watch functions failed"));
+        logError("DBusWrapper::DBusWrapper Registering of watch functions failed");
     }
 
     //add timer functions:
     dbus_bool_t timer = dbus_connection_set_timeout_functions(mDbusConnection, addTimeout, removeTimeout, toggleTimeout, this, NULL);
     if (!timer)
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Registering of timer functions failed"));
+        logError("DBusWrapper::DBusWrapper Registering of timer functions failed");
     }
 
     //register callback for Introspectio
@@ -141,12 +139,12 @@ DBusWrapper::DBusWrapper(SocketHandler* socketHandler) :
     int ret = dbus_bus_request_name(mDbusConnection, DBUS_SERVICE_PREFIX, DBUS_NAME_FLAG_DO_NOT_QUEUE, &mDBusError);
     if (dbus_error_is_set(&mDBusError))
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Name Error"), DLT_STRING(mDBusError.message));
+        logError("DBusWrapper::DBusWrapper Name Error",mDBusError.message);
         dbus_error_free(&mDBusError);
     }
     if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner"), DLT_INT(ret));
+        logError("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner",ret);
         exit(1);
     }
 }
@@ -154,7 +152,7 @@ DBusWrapper::DBusWrapper(SocketHandler* socketHandler) :
 DBusWrapper::~DBusWrapper()
 {
     //close the connection again
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::~DBusWrapper Closing DBus connection"));
+    logInfo("DBusWrapper::~DBusWrapper Closing DBus connection");
     dbus_connection_unref(mDbusConnection);
 
     //clean up all timerhandles we created but did not delete before
@@ -167,7 +165,7 @@ DBusWrapper::~DBusWrapper()
 
 void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata)
 {
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::~registerCallback register callback:"), DLT_STRING(path.c_str()));
+    logInfo("DBusWrapper::~registerCallback register callback:",path);
 
     std::string completePath = std::string(DBUS_SERVICE_OBJECT_PATH) + "/" + path;
     dbus_error_init(&mDBusError);
@@ -175,7 +173,7 @@ void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std
     dbus_connection_register_object_path(mDbusConnection, completePath.c_str(), vtable, userdata);
     if (dbus_error_is_set(&mDBusError))
     {
-        DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::registerCallack error: "), DLT_STRING(mDBusError.message));
+        logError("DBusWrapper::registerCallack error: ",mDBusError.message);
         dbus_error_free(&mDBusError);
     }
     mNodesList.push_back(path);
@@ -183,7 +181,7 @@ void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std
 
 DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference)
 {
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::~cbRootIntrospection called:"));
+    logInfo("DBusWrapper::~cbRootIntrospection called:");
 
     mReference = (DBusWrapper*) reference;
     std::list<std::string> nodesList = mReference->mNodesList;
@@ -210,13 +208,13 @@ DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMes
         dbus_message_iter_init_append(reply, &args);
         if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))
         {
-            DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
+            logError("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!");
         }
 
         // send the reply && flush the connection
         if (!dbus_connection_send(conn, reply, &serial))
         {
-            DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
+            logError("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!");
         }
         dbus_connection_flush(conn);
         // free the reply
@@ -232,7 +230,7 @@ DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMes
 
 void DBusWrapper::dbusMainLoop()
 {
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::dbusMainLoop Entering MainLoop"));
+    logInfo("DBusWrapper::dbusMainLoop Entering MainLoop");
 
     while (dbus_connection_read_write_dispatch(mDbusConnection, -1))
     {
@@ -254,6 +252,7 @@ dbus_bool_t DBusWrapper::addWatch(DBusWatch *watch, void *userData)
 
 dbus_bool_t DBusWrapper::addWatchDelegate(DBusWatch * watch, void* userData)
 {
+    (void) userData;
     int16_t event = 0;
     sh_pollHandle_t handle = 0;
     uint flags = dbus_watch_get_flags(watch);
@@ -261,11 +260,13 @@ dbus_bool_t DBusWrapper::addWatchDelegate(DBusWatch * watch, void* userData)
     /* no watch flags for disabled watches */
     if (dbus_watch_get_enabled(watch))
     {
-        if (flags & DBUS_WATCH_READABLE) event |= POLLIN;
-        if (flags & DBUS_WATCH_WRITABLE) event |= POLLOUT;
+        if (flags & DBUS_WATCH_READABLE)
+            event |= POLLIN;
+        if (flags & DBUS_WATCH_WRITABLE)
+            event |= POLLOUT;
     }
 
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::addWatchDelegate entered new watch, fd="), DLT_INT(dbus_watch_get_unix_fd(watch)), DLT_STRING("event flag="), DLT_INT(event));
+    logInfo("DBusWrapper::addWatchDelegate entered new watch, fd=",dbus_watch_get_unix_fd(watch),"event flag=",event);
     am_Error_e error = mSocketHandler->addFDPoll(dbus_watch_get_unix_fd(watch), event, NULL, &pDbusFireCallback, &pDbusCheckCallback, &pDbusDispatchCallback, watch, handle);
 
     //if everything is alright, add the watch and the handle to our map so we know this relationship
@@ -273,7 +274,9 @@ dbus_bool_t DBusWrapper::addWatchDelegate(DBusWatch * watch, void* userData)
     {
         mMapHandleWatch.insert(std::make_pair(watch, handle));
         return true;
-    }DLT_LOG(AudioManager, DLT_LOG_ERROR, DLT_STRING("DBusWrapper::addWatchDelegate entering watch failed"));
+    }
+    logError("DBusWrapper::addWatchDelegate entering watch failed");
+    return (true);
 }
 
 void DBusWrapper::removeWatch(DBusWatch *watch, void *userData)
@@ -285,10 +288,12 @@ void DBusWrapper::removeWatch(DBusWatch *watch, void *userData)
 
 void DBusWrapper::removeWatchDelegate(DBusWatch *watch, void *userData)
 {
+    (void) userData;
     std::map<DBusWatch*, sh_pollHandle_t>::iterator iterator = mMapHandleWatch.begin();
     iterator = mMapHandleWatch.find(watch);
-    if (iterator != mMapHandleWatch.end()) mSocketHandler->removeFDPoll(iterator->second);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::removeWatch removed watch with handle"), DLT_INT(iterator->second));
+    if (iterator != mMapHandleWatch.end())
+        mSocketHandler->removeFDPoll(iterator->second);
+    logInfo("DBusWrapper::removeWatch removed watch with handle",iterator->second);
     mMapHandleWatch.erase(iterator);
 }
 
@@ -301,19 +306,23 @@ void DBusWrapper::toogleWatch(DBusWatch *watch, void *userData)
 
 void DBusWrapper::toogleWatchDelegate(DBusWatch *watch, void *userData)
 {
+    (void) userData;
     int16_t event = 0;
-    int watchFD = dbus_watch_get_unix_fd(watch);
+    dbus_watch_get_unix_fd(watch);
     uint flags = dbus_watch_get_flags(watch);
     /* no watch flags for disabled watches */
     if (dbus_watch_get_enabled(watch))
     {
-        if (flags & DBUS_WATCH_READABLE) event |= POLLIN;
-        if (flags & DBUS_WATCH_WRITABLE) event |= POLLOUT;
+        if (flags & DBUS_WATCH_READABLE)
+            event |= POLLIN;
+        if (flags & DBUS_WATCH_WRITABLE)
+            event |= POLLOUT;
     }
     std::map<DBusWatch*, sh_pollHandle_t>::iterator iterator = mMapHandleWatch.begin();
     iterator = mMapHandleWatch.find(watch);
-    if (iterator != mMapHandleWatch.end()) mSocketHandler->updateEventFlags(iterator->second, event);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::toogleWatchDelegate watch was toggeled"));
+    if (iterator != mMapHandleWatch.end())
+        mSocketHandler->updateEventFlags(iterator->second, event);
+    logInfo("DBusWrapper::toogleWatchDelegate watch was toggeled");
 }
 
 dbus_bool_t DBusWrapper::addTimeout(DBusTimeout *timeout, void* userData)
@@ -325,7 +334,8 @@ dbus_bool_t DBusWrapper::addTimeout(DBusTimeout *timeout, void* userData)
 
 dbus_bool_t DBusWrapper::addTimeoutDelegate(DBusTimeout *timeout, void* userData)
 {
-    if (!dbus_timeout_get_enabled(timeout)) return false;
+    if (!dbus_timeout_get_enabled(timeout))
+        return false;
 
     //calculate the timeout in timeval
     timespec pollTimeout;
@@ -346,7 +356,7 @@ dbus_bool_t DBusWrapper::addTimeoutDelegate(DBusTimeout *timeout, void* userData
 
     //save timeout in Socket context
     userData = timeout;
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::addTimeoutDelegate a timeout was added"));
+    logInfo("DBusWrapper::addTimeoutDelegate a timeout was added");
     return true;
 }
 
@@ -359,6 +369,7 @@ void DBusWrapper::removeTimeout(DBusTimeout *timeout, void* userData)
 
 void DBusWrapper::removeTimeoutDelegate(DBusTimeout *timeout, void* userData)
 {
+    (void) userData;
     //get the pointer to the handle and remove the timer
     sh_timerHandle_t* handle = (sh_timerHandle_t*) dbus_timeout_get_data(timeout);
     mSocketHandler->removeTimer(*handle);
@@ -374,7 +385,7 @@ void DBusWrapper::removeTimeoutDelegate(DBusTimeout *timeout, void* userData)
         }
     }
     delete handle;
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::removeTimeoutDelegate a timeout was removed"));
+    logInfo("DBusWrapper::removeTimeoutDelegate a timeout was removed");
 }
 
 void DBusWrapper::toggleTimeout(DBusTimeout *timeout, void* userData)
@@ -386,44 +397,57 @@ void DBusWrapper::toggleTimeout(DBusTimeout *timeout, void* userData)
 
 bool am::DBusWrapper::dbusDispatchCallback(const sh_pollHandle_t handle, void *userData)
 {
+    (void) handle;
+    (void) userData;
     bool returnVal = true;
     dbus_connection_ref(mDbusConnection);
-    if (dbus_connection_dispatch(mDbusConnection) == DBUS_DISPATCH_COMPLETE) returnVal = false;
+    if (dbus_connection_dispatch(mDbusConnection) == DBUS_DISPATCH_COMPLETE)
+        returnVal = false;
     dbus_connection_unref(mDbusConnection);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::dbusDispatchCallback was called"));
+//    logInfo("DBusWrapper::dbusDispatchCallback was called");
     return returnVal;
 }
 
 bool am::DBusWrapper::dbusCheckCallback(const sh_pollHandle_t handle, void *userData)
 {
+    (void) handle;
+    (void) userData;
     bool returnVal = false;
     dbus_connection_ref(mDbusConnection);
-    if (dbus_connection_get_dispatch_status(mDbusConnection) == DBUS_DISPATCH_DATA_REMAINS) returnVal = true;
+    if (dbus_connection_get_dispatch_status(mDbusConnection) == DBUS_DISPATCH_DATA_REMAINS)
+        returnVal = true;
     dbus_connection_unref(mDbusConnection);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::dbusCheckCallback was called"));
+//    logInfo("DBusWrapper::dbusCheckCallback was called");
     return returnVal;
 }
 
 void am::DBusWrapper::dbusFireCallback(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
 {
+    (void) handle;
+    (void) userData;
     assert(userData!=NULL);
     uint flags = 0;
 
-    if (pollfd.revents & POLLIN) flags |= DBUS_WATCH_READABLE;
-    if (pollfd.revents & POLLOUT) flags |= DBUS_WATCH_WRITABLE;
-    if (pollfd.revents & POLLHUP) flags |= DBUS_WATCH_HANGUP;
-    if (pollfd.revents & POLLERR) flags |= DBUS_WATCH_ERROR;
+    if (pollfd.revents & POLLIN)
+        flags |= DBUS_WATCH_READABLE;
+    if (pollfd.revents & POLLOUT)
+        flags |= DBUS_WATCH_WRITABLE;
+    if (pollfd.revents & POLLHUP)
+        flags |= DBUS_WATCH_HANGUP;
+    if (pollfd.revents & POLLERR)
+        flags |= DBUS_WATCH_ERROR;
 
     DBusWatch *watch = (DBusWatch*) userData;
 
     dbus_connection_ref(mDbusConnection);
-    bool ok = dbus_watch_handle(watch, flags);
+    dbus_watch_handle(watch, flags);
     dbus_connection_unref(mDbusConnection);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::dbusFireCallback was called"));
+//    logInfo("DBusWrapper::dbusFireCallback was called");
 }
 
 void DBusWrapper::toggleTimeoutDelegate(DBusTimeout *timeout, void* userData)
 {
+    (void) userData;
     //get the pointer to the handle and remove the timer
     sh_timerHandle_t* handle = (sh_timerHandle_t*) dbus_timeout_get_data(timeout);
 
@@ -440,7 +464,7 @@ void DBusWrapper::toggleTimeoutDelegate(DBusTimeout *timeout, void* userData)
     else
     {
         mSocketHandler->stopTimer(*handle);
-    }DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::toggleTimeoutDelegate was called"));
+    }logInfo("DBusWrapper::toggleTimeoutDelegate was called");
 }
 
 void DBusWrapper::dbusTimerCallback(sh_timerHandle_t handle, void *userData)
@@ -454,6 +478,6 @@ void DBusWrapper::dbusTimerCallback(sh_timerHandle_t handle, void *userData)
         mSocketHandler->restartTimer(handle, ts);
     }
     dbus_timeout_handle((DBusTimeout*) userData);
-    DLT_LOG(AudioManager, DLT_LOG_INFO, DLT_STRING("DBusWrapper::dbusTimerCallback was called"));
+    logInfo("DBusWrapper::dbusTimerCallback was called");
 }