summaryrefslogtreecommitdiff
path: root/AudioManagerUtilities
diff options
context:
space:
mode:
authorChristian Linke <christian.linke@bmw.de>2015-03-12 13:43:48 +0100
committerChristian Linke <christian.linke@bmw.de>2015-03-12 13:43:48 +0100
commit6ffbf25bacf589879198e49ac227cb56e261b7c9 (patch)
treee6b3ca48d22d06d43da638b1998ab1220ec7ab08 /AudioManagerUtilities
parentc628fa5304171cd827f56aa475668d99d26ad28a (diff)
downloadaudiomanager-6ffbf25bacf589879198e49ac227cb56e261b7c9.tar.gz
* fully rework. Plugins have now a new own git repository
* building out of source of plugins without AM workss * build system cmake contructions is different now Signed-off-by: Christian Linke <christian.linke@bmw.de>
Diffstat (limited to 'AudioManagerUtilities')
-rw-r--r--AudioManagerUtilities/include/CAmCommonAPIWrapper.h143
-rw-r--r--AudioManagerUtilities/include/CAmDbusWrapper.h92
-rw-r--r--AudioManagerUtilities/include/CAmDltWrapper.h915
-rw-r--r--AudioManagerUtilities/include/CAmSerializer.h1508
-rw-r--r--AudioManagerUtilities/include/CAmSocketHandler.h576
-rw-r--r--AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp275
-rw-r--r--AudioManagerUtilities/src/CAmDbusWrapper.cpp472
-rw-r--r--AudioManagerUtilities/src/CAmDltWrapper.cpp350
-rw-r--r--AudioManagerUtilities/src/CAmSocketHandler.cpp546
9 files changed, 4877 insertions, 0 deletions
diff --git a/AudioManagerUtilities/include/CAmCommonAPIWrapper.h b/AudioManagerUtilities/include/CAmCommonAPIWrapper.h
new file mode 100644
index 0000000..9c293f7
--- /dev/null
+++ b/AudioManagerUtilities/include/CAmCommonAPIWrapper.h
@@ -0,0 +1,143 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ * \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
+ *
+ * \copyright
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * \file CAmCommonAPIWrapper.h
+ * For further information see http://www.genivi.org/.
+ */
+
+
+#ifndef COMMONAPIWRAPPER_H_
+#define COMMONAPIWRAPPER_H_
+
+#include <string>
+#include <list>
+#include <map>
+#include <queue>
+#include <memory>
+#include <CommonAPI/CommonAPI.h>
+#include "config.h"
+#include "CAmSocketHandler.h"
+
+
+/**
+ * A Common-API wrapper class, that loads the common-api runtime and instantiates all necessary other objects. Works with the CAmSocketHandler.
+ * It is implemented as singleton and usually once instantiated at the beginning with CAmSocketHandler.
+ * Example: CAmCommonAPIWrapper *pCAPIWrapper = CAmCommonAPIWrapper::instantiateOnce( aSocketHandlerPointer );
+ */
+
+namespace am
+{
+using namespace CommonAPI;
+
+class CAmSocketHandler;
+
+class CAmCommonAPIWrapper
+{
+public:
+
+ virtual ~CAmCommonAPIWrapper();
+ /**
+ * \brief Returns an already instantiated object.
+ *
+ * This method should be called after the instantiateOnce(...) has been called with non null socket handler parameter.
+ *
+ * @return The common-api wrapper object.
+ */
+ static CAmCommonAPIWrapper* getInstance();
+ /**
+ * \brief Creates a singleton instance attached to the provided socket handler object.
+ *
+ * This method should be called only once because it instantiates a single object.
+ * Otherwise it will throw an exception.
+ * The first call of this method with non null parameter loads the common-api and attaches it to the main loop.
+ *
+ * @param socketHandler: A pointer to socket handler or NULL
+ *
+ * @return The common-api wrapper object.
+ */
+ static CAmCommonAPIWrapper* instantiateOnce(CAmSocketHandler* socketHandler);
+
+ void registerDispatchSource(DispatchSource* dispatchSource, const DispatchPriority dispatchPriority);
+ void deregisterDispatchSource(DispatchSource* dispatchSource);
+ void registerWatch(Watch* watch, const DispatchPriority dispatchPriority);
+ void deregisterWatch(Watch* watch);
+ void registerTimeout(Timeout* timeout, const DispatchPriority dispatchPriority);
+ void deregisterTimeout(Timeout* timeout);
+ void wakeup();
+
+ std::shared_ptr<CommonAPI::Factory> factory() const;
+ std::shared_ptr<CommonAPI::Runtime> runtime() const;
+ //Wraps the invitation to the service publisher
+ template <class TStubImp> bool registerStub(const std::shared_ptr<TStubImp> & shStub, const std::string & aCommonAPIAddress)
+ {
+ return runtime()->getServicePublisher()->registerService(shStub, aCommonAPIAddress, factory());
+ }
+ bool unregisterStub(const std::string & aCommonAPIAddress)
+ {
+ (void)aCommonAPIAddress;
+ /** Not implemented yet
+ todo: Check whether the appropriate method is available and uncomment...
+
+ return runtime()->getServicePublisher()->unregisterService(aCommonAPIAddress);
+ */
+ return true;
+ }
+
+
+protected:
+ CAmCommonAPIWrapper(CAmSocketHandler* socketHandler) ;
+private:
+ void commonPrepareCallback(const sh_pollHandle_t handle, void* userData);
+ TAmShPollPrepare<CAmCommonAPIWrapper> pCommonPrepareCallback;
+
+ bool commonDispatchCallback(const sh_pollHandle_t handle, void* userData);
+ TAmShPollDispatch<CAmCommonAPIWrapper> pCommonDispatchCallback;
+
+ void commonFireCallback(const pollfd pollfd, const sh_pollHandle_t, void*);
+ TAmShPollFired<CAmCommonAPIWrapper> pCommonFireCallback;
+
+ bool commonCheckCallback(const sh_pollHandle_t handle, void*);
+ TAmShPollCheck<CAmCommonAPIWrapper> pCommonCheckCallback;
+
+ void commonTimerCallback(sh_timerHandle_t handle, void* userData);
+ TAmShTimerCallBack<CAmCommonAPIWrapper> pCommonTimerCallback;
+
+ struct timerHandles
+ {
+ sh_timerHandle_t handle;
+ Timeout* timeout;
+ };
+ //!< reference to the dbus instance
+ CAmSocketHandler *mpSocketHandler; //!< pointer to the sockethandler
+
+ std::shared_ptr<CommonAPI::Factory> mFactory;
+ std::shared_ptr<CommonAPI::MainLoopContext> mContext;
+
+ DispatchSourceListenerSubscription mDispatchSourceListenerSubscription;
+ WatchListenerSubscription mWatchListenerSubscription;
+ TimeoutSourceListenerSubscription mTimeoutSourceListenerSubscription;
+ WakeupListenerSubscription mWakeupListenerSubscription;
+ std::multimap<DispatchPriority, DispatchSource*> mRegisteredDispatchSources;
+ std::map<int,Watch*> mMapWatches;
+ Watch* mWatchToCheck;
+ std::list<DispatchSource*> mSourcesToDispatch;
+ std::vector<timerHandles> mpListTimerhandles;
+};
+
+#define Am_CAPI CAmCommonAPIWrapper::getInstance()
+
+}
+
+#endif /* COMMONAPIWRAPPER_H_ */
diff --git a/AudioManagerUtilities/include/CAmDbusWrapper.h b/AudioManagerUtilities/include/CAmDbusWrapper.h
new file mode 100644
index 0000000..0f8a0bd
--- /dev/null
+++ b/AudioManagerUtilities/include/CAmDbusWrapper.h
@@ -0,0 +1,92 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ *
+ * \copyright
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * \file CAmDbusWrapper.h
+ * For further information see http://www.genivi.org/.
+ */
+
+/**
+ * todo add removeCallback ...
+ */
+
+#ifndef DBUSWRAPPER_H_
+#define DBUSWRAPPER_H_
+
+#include <dbus/dbus.h>
+#include <string>
+#include <list>
+#include <map>
+#include "config.h"
+#include "CAmSocketHandler.h"
+
+namespace am
+{
+
+/**
+ * This wraps dbus and provides everything needed to anyone who wants to use dbus (including plugins). Works on the basis of CAmSocketHandler
+ */
+class CAmDbusWrapper
+{
+public:
+ CAmDbusWrapper(CAmSocketHandler* socketHandler,DBusBusType type=DBUS_BUS_SESSION);
+ virtual ~CAmDbusWrapper();
+
+ void registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata);
+ void getDBusConnection(DBusConnection*& connection) const;
+
+ static dbus_bool_t addWatch(DBusWatch *watch, void *userData);
+ static void removeWatch(DBusWatch *watch, void *userData);
+ static void toogleWatch(DBusWatch *watch, void *userData);
+
+ static dbus_bool_t addTimeout(DBusTimeout *timeout, void* userData);
+ static void removeTimeout(DBusTimeout *timeout, void* userData);
+ static void toggleTimeout(DBusTimeout *timeout, void* userData);
+
+ void dbusPrepareCallback(const sh_pollHandle_t handle, void* userData);
+ TAmShPollPrepare<CAmDbusWrapper> pDbusPrepareCallback;
+
+ bool dbusDispatchCallback(const sh_pollHandle_t handle, void* userData);
+ TAmShPollDispatch<CAmDbusWrapper> pDbusDispatchCallback;
+
+ void dbusFireCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
+ TAmShPollFired<CAmDbusWrapper> pDbusFireCallback;
+
+ bool dbusCheckCallback(const sh_pollHandle_t handle, void* userData);
+ TAmShPollCheck<CAmDbusWrapper> pDbusCheckCallback;
+
+ void dbusTimerCallback(sh_timerHandle_t handle, void* userData);
+ TAmShTimerCallBack<CAmDbusWrapper> pDbusTimerCallback;
+
+private:
+ static CAmDbusWrapper* mpReference; //!< reference to the dbus instance
+ static DBusHandlerResult cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference);
+ dbus_bool_t addWatchDelegate(DBusWatch * watch, void* userData);
+ void removeWatchDelegate(DBusWatch *watch, void *userData);
+ void toogleWatchDelegate(DBusWatch *watch, void *userData);
+ dbus_bool_t addTimeoutDelegate(DBusTimeout *timeout, void* userData);
+ void removeTimeoutDelegate(DBusTimeout *timeout, void* userData);
+ void toggleTimeoutDelegate(DBusTimeout *timeout, void* userData);
+ DBusObjectPathVTable mObjectPathVTable; //!< the vpathtable
+ DBusConnection* mpDbusConnection; //!< pointer to the dbus connection used
+ DBusError mDBusError; //!< dbuserror
+ std::vector<std::string> mListNodes; //!< holds a list of all nodes of the dbus
+ std::vector<sh_timerHandle_t*> mpListTimerhandles; //!< pointer to the timer handles
+ CAmSocketHandler *mpSocketHandler; //!< pointer to the sockethandler
+ std::map<DBusWatch*, sh_pollHandle_t> mMapHandleWatch; //!< map to the handle watches
+ DBusBusType mDbusType;
+};
+
+}
+
+#endif /* DBUSWRAPPER_H_ */
diff --git a/AudioManagerUtilities/include/CAmDltWrapper.h b/AudioManagerUtilities/include/CAmDltWrapper.h
new file mode 100644
index 0000000..8492567
--- /dev/null
+++ b/AudioManagerUtilities/include/CAmDltWrapper.h
@@ -0,0 +1,915 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ * Jens Lorenz, jlorenz@de.adit-jv.com ADIT 2014
+ *
+ * \copyright
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * \file CAmDltWrapper.h
+ * For further information see http://www.genivi.org/.
+ */
+
+#ifndef DLTWRAPPER_H_
+#define DLTWRAPPER_H_
+
+#include <config.h>
+#include <string>
+#include <pthread.h>
+#include <config.h>
+#include "audiomanagertypes.h"
+
+#ifdef WITH_DLT
+#include <dlt.h>
+namespace am
+{
+#else
+#include <stdint.h>
+#include <sstream>
+
+#define DLT_USER_BUF_MAX_SIZE 2048
+
+/**
+ * This structure is used for every context used in an application.
+ */
+typedef struct
+{
+ char contextID[4]; /**< context id */
+ int32_t log_level_pos; /**< offset in user-application context field */
+} DltContext;
+
+/**
+ * Definitions of DLT log level
+ */
+typedef enum
+{
+ DLT_LOG_DEFAULT = -1, /**< Default log level */
+ DLT_LOG_OFF = 0x00, /**< Log level off */
+ DLT_LOG_FATAL = 0x01, /**< fatal system error */
+ DLT_LOG_ERROR = 0x02, /**< error with impact to correct functionality */
+ DLT_LOG_WARN = 0x03, /**< warning, correct behaviour could not be ensured */
+ DLT_LOG_INFO = 0x04, /**< informational */
+ DLT_LOG_DEBUG = 0x05, /**< debug */
+ DLT_LOG_VERBOSE = 0x06 /**< highest grade of information */
+} DltLogLevelType;
+
+/**
+ * This structure is used for context data used in an application.
+ */
+typedef struct
+{
+ DltContext *handle; /**< pointer to DltContext */
+ std::stringstream buffer; /**< buffer for building log message*/
+ int32_t log_level; /**< log level */
+ int32_t trace_status; /**< trace status */
+ int32_t args_num; /**< number of arguments for extended header*/
+ uint8_t mcnt; /**< message counter */
+ char* context_description; /**< description of context */
+} DltContextData;
+
+#define DLT_DECLARE_CONTEXT(CONTEXT) \
+DltContext CONTEXT;
+
+#define DLT_IMPORT_CONTEXT(CONTEXT) \
+extern DltContext CONTEXT;
+namespace am
+{
+#endif // WITH_DLT
+
+/**
+ * Wraps around the dlt. This class is instantiated as a singleton and offers a default
+ * context (maincontext) that is registered to log to.
+ * Logging under the default context can simply be done with the logInfo/logError templates with up to 10 values at a time.
+ * For logging with a different context, you can use the log template. First register a context with registerContext.
+ */
+class CAmDltWrapper
+{
+public:
+ static CAmDltWrapper* instance(const bool enableNoDLTDebug = false);
+ void registerApp(const char *appid, const char * description);
+ void registerContext(DltContext& handle, const char *contextid, const char * description);
+ void unregisterContext(DltContext& handle);
+
+ bool init(DltLogLevelType loglevel, DltContext* context = NULL);
+ void deinit();
+ void send();
+ void append(const int8_t value);
+ void append(const uint8_t value);
+ void append(const int16_t value);
+ void append(const uint16_t value);
+ void append(const int32_t value);
+ void append(const uint32_t value);
+ void append(const uint64_t value);
+ void append(const int64_t value);
+ void append(const char*& value);
+ void append(const std::string& value);
+ void append(const bool value);
+ void append(const am_Error_e value);
+#ifndef WITH_DLT
+ void enableNoDLTDebug(const bool enableNoDLTDebug = true);
+#endif
+ ~CAmDltWrapper();
+private:
+ CAmDltWrapper(const bool enableNoDLTDebug); //is private because of singleton pattern
+#ifndef WITH_DLT
+ template<class T> void appendNoDLT(T value);
+ bool mEnableNoDLTDebug;
+#endif
+ DltContext mDltContext; //!< the default context
+ DltContextData mDltContextData; //!< contextdata
+ static CAmDltWrapper* mpDLTWrapper; //!< pointer to the wrapper instance
+ static pthread_mutex_t mMutex;
+
+};
+
+/**
+ * returns the instance of the CAmDltWrapper
+ * @return
+ */
+inline CAmDltWrapper* getWrapper()
+{
+ return (CAmDltWrapper::instance());
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ */
+template<typename T> void logInfo(T value)
+{
+ CAmDltWrapper* inst(getWrapper());
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->send();
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1mDltContext
+ */
+template<typename T, typename T1> void logInfo(T value, T1 value1)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ */
+template<typename T, typename T1, typename T2> void logInfo(T value, T1 value1, T2 value2)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ */
+template<typename T, typename T1, typename T2, typename T3> void logInfo(T value, T1 value1, T2 value2, T3 value3)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4> void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5> void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ * @param value8
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->append(value8);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ * @param value8
+ * @param value9
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9> void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->append(value8);
+ inst->append(value9);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ * @param value8
+ * @param value9
+ * @param value10
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10> void logInfo(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9, T10 value10)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_INFO))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->append(value8);
+ inst->append(value9);
+ inst->append(value10);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ */
+template<typename T> void logError(T value)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ */
+template<typename T, typename T1> void logError(T value, T1 value1)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ */
+template<typename T, typename T1, typename T2> void logError(T value, T1 value1, T2 value2)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ */
+template<typename T, typename T1, typename T2, typename T3> void logError(T value, T1 value1, T2 value2, T3 value3)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4> void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5> void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ * @param value8
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->append(value8);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ * @param value8
+ * @param value9
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9> void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->append(value8);
+ inst->append(value9);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with errorlevel with the default context
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ * @param value8
+ * @param value9
+ * @param value10
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10> void logError(T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9, T10 value10)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(DLT_LOG_ERROR))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->append(value8);
+ inst->append(value9);
+ inst->append(value10);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ */
+template<typename T> void log(DltContext* const context, DltLogLevelType loglevel, T value)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ */
+template<typename T, typename T1> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ * @param value2
+ */
+template<typename T, typename T1, typename T2> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ */
+template<typename T, typename T1, typename T2, typename T3> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->send();
+
+}
+
+/**
+ * logs a given value with a given context (register first!) and given loglevel
+ * @param context
+ * @param loglevel
+ * @param value
+ * @param value1
+ * @param value2
+ * @param value3
+ * @param value4
+ * @param value5
+ * @param value6
+ * @param value7
+ * @param value8
+ */
+template<typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> void log(DltContext* const context, DltLogLevelType loglevel, T value, T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
+{
+ CAmDltWrapper* inst(getWrapper());
+
+ if (!inst->init(loglevel, context))
+ return;
+ inst->append(value);
+ inst->append(value1);
+ inst->append(value2);
+ inst->append(value3);
+ inst->append(value4);
+ inst->append(value5);
+ inst->append(value6);
+ inst->append(value7);
+ inst->append(value8);
+ inst->send();
+
+}
+}
+
+#endif /* DLTWRAPPER_H_ */
diff --git a/AudioManagerUtilities/include/CAmSerializer.h b/AudioManagerUtilities/include/CAmSerializer.h
new file mode 100644
index 0000000..1f13a66
--- /dev/null
+++ b/AudioManagerUtilities/include/CAmSerializer.h
@@ -0,0 +1,1508 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ *
+ * \copyright
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * \file CAmSerializer.h
+ * For further information see http://www.genivi.org/.
+ */
+
+#ifndef CAMSERIALIZER_H_
+#define CAMSERIALIZER_H_
+
+#include <pthread.h>
+#include <deque>
+#include <cassert>
+#include <memory>
+#include <stdexcept>
+#include <unistd.h>
+#include "CAmDltWrapper.h"
+#include "CAmSocketHandler.h"
+
+/**
+ * todo: performance improvement we could implement a memory pool that is more efficient here and avoids
+ * allocation and deallocation times.
+ */
+
+namespace am
+{
+/**
+ * magic class that does the serialization of functions calls
+ * The constructor must be called within the main threadcontext, after that using the
+ * overloaded template function call will serialize all calls and call them within the
+ * main thread context.\n
+ * More details can be found here: \ref util
+ * \warning asynchronous calls may be used in the mainthread context, but if you want to use synchronous calls make sure that you use one
+ * instance of this class per thread otherwise you could be lost in never returning calls.\n
+ * Examples of the usage can be found in IAmCommandReceiverShadow of the ControlPlugin or IAmRoutingReceiverShadow of the
+ * PluginRoutingInterfaceAsync.
+ *
+ */
+class CAmSerializer
+{
+private:
+
+ /**
+ * Prototype for a delegate
+ */
+ class CAmDelegate
+ {
+ public:
+ virtual ~CAmDelegate()
+ {};
+ virtual bool call(int* pipe)=0;
+
+ };
+
+ typedef CAmDelegate* CAmDelegagePtr; //!< pointer to a delegate
+
+ /**
+ * delegate template for no argument
+ */
+ template<class TClass> class CAmNoArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)();
+
+ public:
+ CAmNoArgDelegate(TClass* instance, void (TClass::*function)()) :
+ mInstance(instance), //
+ mFunction(function)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)();
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for one argument
+ */
+ template<class TClass, typename Targ> class CAmOneArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ);
+ Targ mArgument;
+
+ public:
+ CAmOneArgDelegate(TClass* instance, void (TClass::*function)(Targ), Targ argument) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for one argument
+ */
+ template<class TClass, typename Targ> class CAmOneArgDelegateRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ&);
+ Targ mArgument;
+
+ public:
+ CAmOneArgDelegateRef(TClass* instance, void (TClass::*function)(Targ&), Targ& argument) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for two arguments
+ */
+ template<class TClass, typename Targ, typename Targ1> class CAmTwoArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ argument, Targ1 argument1);
+ Targ mArgument;
+ Targ1 mArgument1;
+
+ public:
+ CAmTwoArgDelegate(TClass* instance, void (TClass::*function)(Targ argument, Targ1 argument1), Targ argument, Targ1 argument1) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1)
+ { };
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for two arguments
+ */
+ template<class TClass, typename Targ, typename Targ1> class CAmTwoArgDelegateFirstRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ& argument, Targ1 argument1);
+ Targ mArgument;
+ Targ1 mArgument1;
+
+ public:
+ CAmTwoArgDelegateFirstRef(TClass* instance, void (TClass::*function)(Targ& argument, Targ1 argument1), Targ& argument, Targ1 argument1) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1)
+ { };
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1);
+ return (true);
+ };
+ };
+
+
+ template<class TClass, typename Targ, typename Targ1> class CAmTwoArgDelegateSecondRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ argument, Targ1& argument1);
+ Targ mArgument;
+ Targ1 mArgument1;
+
+ public:
+ CAmTwoArgDelegateSecondRef(TClass* instance, void (TClass::*function)(Targ argument, Targ1& argument1), Targ argument, Targ1& argument1) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for two arguments
+ */
+ template<class TClass, typename Targ, typename Targ1> class CAmTwoArgDelegateAllRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ& argument, Targ1& argument1);
+ Targ mArgument;
+ Targ1 mArgument1;
+
+ public:
+ CAmTwoArgDelegateAllRef(TClass* instance, void (TClass::*function)(Targ& argument, Targ1& argument1), Targ& argument, Targ1& argument1) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1)
+ { };
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1);
+ return (true);
+ };
+ };
+
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ argument, Targ1 argument1, Targ2 argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegate(TClass* instance, void (TClass::*function)(Targ argument, Targ1 argument1, Targ2 argument2), Targ argument, Targ1 argument1, Targ2 argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ }
+ ;
+ };
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegateFirstRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ& argument, Targ1 argument1, Targ2 argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegateFirstRef(TClass* instance, void (TClass::*function)(Targ& argument, Targ1 argument1, Targ2 argument2), Targ& argument, Targ1 argument1, Targ2 argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegateSecondRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ argument, Targ1& argument1, Targ2 argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegateSecondRef(TClass* instance, void (TClass::*function)(Targ argument, Targ1& argument1, Targ2 argument2), Targ argument, Targ1& argument1, Targ2 argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegateThirdRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ argument, Targ1 argument1, Targ2& argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegateThirdRef(TClass* instance, void (TClass::*function)(Targ argument, Targ1 argument1, Targ2& argument2), Targ argument, Targ1 argument1, Targ2& argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegateFirstSecondRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ& argument, Targ1& argument1, Targ2 argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegateFirstSecondRef(TClass* instance, void (TClass::*function)(Targ& argument, Targ1& argument1, Targ2 argument2), Targ& argument, Targ1& argument1, Targ2 argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegateFirstThirdRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ& argument, Targ1 argument1, Targ2& argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegateFirstThirdRef(TClass* instance, void (TClass::*function)(Targ& argument, Targ1 argument1, Targ2& argument2), Targ& argument, Targ1 argument1, Targ2& argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegateSecondThirdRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ argument, Targ1& argument1, Targ2& argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegateSecondThirdRef(TClass* instance, void (TClass::*function)(Targ argument, Targ1& argument1, Targ2& argument2), Targ argument, Targ1& argument1, Targ2& argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for three arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2> class CAmThreeArgDelegateAllRef: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ& argument, Targ1& argument1, Targ2& argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+
+ public:
+ CAmThreeArgDelegateAllRef(TClass* instance, void (TClass::*function)(Targ& argument, Targ1& argument1, Targ2& argument2), Targ& argument, Targ1& argument1, Targ2& argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2)
+ {};
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ return (true);
+ };
+ };
+
+ /**
+ * delegate template for four arguments
+ */
+ template<class TClass, typename Targ, typename Targ1, typename Targ2, typename Targ3> class CAmFourArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ Targ3 mArgument3;
+
+ public:
+ CAmFourArgDelegate(TClass* instance, void (TClass::*function)(Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2), //
+ mArgument3(argument3)
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ (void) pipe;
+ (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2, mArgument3);
+ return (true);
+ }
+ ;
+ };
+
+ /**
+ * Template for synchronous calls with no argument
+ */
+ template<class TClass, typename TretVal> class CAmSyncNoArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)();
+ TretVal mRetval;
+
+ public:
+ friend class CAmSerializer;
+ CAmSyncNoArgDelegate(TClass* instance, TretVal (TClass::*function)()) :
+ mInstance(instance), //
+ mFunction(function), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)();
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults()
+ {
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous calls with one argument
+ */
+ template<class TClass, typename TretVal, typename TargCall, typename Targ> class CAmSyncOneArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCall argument);
+ Targ mArgument;
+ TretVal mRetval;
+
+ public:
+ friend class CAmSerializer;
+ CAmSyncOneArgDelegate(TClass* instance, TretVal (TClass::*function)(TargCall argument), Targ argument) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument)
+ {
+ argument = mArgument;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous calls with one argument on a const function
+ */
+ template<class TClass, typename TretVal, typename TargCall, typename Targ> class CAmSyncOneArgConstDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCall argument) const;
+ Targ mArgument;
+ TretVal mRetval;
+
+ public:
+ friend class CAmSerializer;
+ CAmSyncOneArgConstDelegate(TClass* instance, TretVal (TClass::*function)(TargCall argument) const, Targ argument) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument)
+ {
+ argument = mArgument;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous calls with two arguments
+ */
+ template<class TClass, typename TretVal, typename TargCall, typename TargCall1, typename Targ, typename Targ1> class CAmSyncTwoArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCall, TargCall1);
+ Targ mArgument;
+ Targ1 mArgument1;
+ TretVal mRetval;
+
+ public:
+ friend class CAmSerializer;
+ CAmSyncTwoArgDelegate(TClass* instance, TretVal (TClass::*function)(TargCall, TargCall1), Targ& argument, Targ1& argument1) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument, mArgument1);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument, Targ1& argument1)
+ {
+ argument = mArgument;
+ argument1 = mArgument1;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous calls with two arguments on a const function
+ */
+ template<class TClass, typename TretVal, typename TargCall, typename TargCall1, typename Targ, typename Targ1> class CAmSyncTwoArgConstDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCall, TargCall1) const;
+ Targ mArgument;
+ Targ1 mArgument1;
+ TretVal mRetval;
+
+ public:
+ friend class CAmSerializer;
+ CAmSyncTwoArgConstDelegate(TClass* instance, TretVal (TClass::*function)(TargCall, TargCall1) const, Targ& argument, Targ1& argument1) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument, mArgument1);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument, Targ1& argument1)
+ {
+ argument = mArgument;
+ argument1 = mArgument1;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous calls with three arguments
+ */
+ template<class TClass, typename TretVal, typename TargCall, typename TargCall1, typename TargCall2, typename Targ, typename Targ1, typename Targ2> class CAmSyncThreeArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCall argument, TargCall1 argument1, TargCall2 argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ TretVal mRetval;
+
+ public:
+ CAmSyncThreeArgDelegate(TClass* instance, TretVal (TClass::*function)(TargCall argument, TargCall1 argument1, TargCall2 argument2), Targ argument, Targ1 argument1, Targ2 argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument, Targ1& argument1, Targ2& argument2)
+ {
+ argument = mArgument;
+ argument1 = mArgument1;
+ argument2 = mArgument2;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous const calls with three arguments
+ */
+ template<class TClass, typename TretVal, typename TargCall, typename TargCall1, typename TargCall2, typename Targ, typename Targ1, typename Targ2> class CAmSyncThreeArgConstDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCall argument, TargCall1 argument1, TargCall2 argument2) const;
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ TretVal mRetval;
+
+ public:
+ CAmSyncThreeArgConstDelegate(TClass* instance, TretVal (TClass::*function)(TargCall argument, TargCall1 argument1, TargCall2 argument2) const, Targ argument, Targ1 argument1, Targ2 argument2) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument, Targ1& argument1, Targ2& argument2)
+ {
+ argument = mArgument;
+ argument1 = mArgument1;
+ argument2 = mArgument2;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous calls with four arguments
+ */
+ template<class TClass, typename TretVal, typename TargCAll, typename TargCall1, typename TargCall2, typename TargCall3, typename Targ, typename Targ1, typename Targ2, typename Targ3> class CAmSyncFourArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCAll argument, TargCall1 argument1, TargCall2 argument2, TargCall3 argument3);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ Targ3 mArgument3;
+ TretVal mRetval;
+ public:
+ CAmSyncFourArgDelegate(TClass* instance, TretVal (TClass::*function)(TargCAll argument, TargCall1 argument1, TargCall2 argument2, TargCall3 argument3), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2), //
+ mArgument3(argument3), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2, mArgument3);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3)
+ {
+ argument = mArgument;
+ argument1 = mArgument1;
+ argument2 = mArgument2;
+ argument3 = mArgument3;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * delegate template for five arguments
+ */
+ template<class TClass, typename TretVal, typename TargCAll, typename TargCall1, typename TargCall2, typename TargCall3, typename TargCall4, typename Targ, typename Targ1, typename Targ2, typename Targ3, typename Targ4> class CAmSyncFiveArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCAll argument, TargCall1 argument1, TargCall2 argument2, TargCall3 argument3, TargCall4);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ Targ3 mArgument3;
+ Targ4 mArgument4;
+ TretVal mRetval;
+ public:
+
+ CAmSyncFiveArgDelegate(TClass* instance, TretVal (TClass::*function)(TargCAll argument, TargCall1 argument1, TargCall2 argument2, TargCall3 argument3, TargCall4 argument4), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3, Targ4 argument4) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2), //
+ mArgument3(argument3), //
+ mArgument4(argument4), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2, mArgument3, mArgument4);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3, Targ4& argument4)
+ {
+ argument = mArgument;
+ argument1 = mArgument1;
+ argument2 = mArgument2;
+ argument3 = mArgument3;
+ argument4 = mArgument4;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * template for synchronous calls with six arguments
+ */
+ template<class TClass, typename TretVal, typename TargCAll, typename TargCall1, typename TargCall2, typename TargCall3, typename TargCall4, typename TargCall5, typename Targ, typename Targ1, typename Targ2, typename Targ3, typename Targ4, typename Targ5> class CAmSyncSixArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(TargCAll argument, TargCall1 argument1, TargCall2 argument2, TargCall3 argument3, TargCall4, TargCall5);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ Targ3 mArgument3;
+ Targ4 mArgument4;
+ Targ5 mArgument5;
+ TretVal mRetval;
+
+ CAmSyncSixArgDelegate(TClass* instance, TretVal (TClass::*function)(TargCAll, TargCall1, TargCall2, TargCall3, TargCall4, TargCall5), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3, Targ4 argument4, Targ5 argument5) :
+ mInstance(instance), //
+ mFunction(function), //
+ mArgument(argument), //
+ mArgument1(argument1), //
+ mArgument2(argument2), //
+ mArgument3(argument3), //
+ mArgument4(argument4), //
+ mArgument5(argument5), //
+ mRetval()
+ {
+ }
+ ;
+
+ bool call(int* pipe)
+ {
+ mRetval = (*mInstance.*mFunction)(mArgument, mArgument1, mArgument2, mArgument3, mArgument4, mArgument5);
+ write(pipe[1], this, sizeof(this));
+ return (false);
+ }
+ ;
+
+ TretVal returnResults(Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3, Targ4& argument4, Targ5& argument5)
+ {
+ argument = mArgument;
+ argument1 = mArgument1;
+ argument2 = mArgument2;
+ argument3 = mArgument3;
+ argument4 = mArgument4;
+ argument5 = mArgument5;
+ return (mRetval);
+ }
+ };
+
+ /**
+ * rings the line of the pipe and adds the delegate pointer to the queue
+ * @param p delegate pointer
+ */
+ inline void send(CAmDelegagePtr p)
+ {
+ if (write(mPipe[1], &p, sizeof(p)) == -1)
+ {
+ throw std::runtime_error("could not write to pipe !");
+ }
+ }
+
+ int mPipe[2]; //!< the pipe
+ int mReturnPipe[2]; //!< pipe handling returns
+ std::deque<CAmDelegagePtr> mListDelegatePoiters; //!< intermediate queue to store the pipe results
+
+public:
+
+ /**
+ * calls a function with no arguments threadsafe
+ * @param instance the instance of the class that shall be called
+ * @param function the function that shall be called as memberfunction pointer.
+ * @tparam TClass1 the type of the Class to be called
+ * \section ex Example:
+ * @code
+ * class myClass
+ * {
+ * public:
+ * void myfunction();
+ * }
+ * CAmSerializer serial(&Sockethandler);
+ * myClass instanceMyClass;
+ * serial<CommandSender>(&instanceMyClass,&myClass::myfunction);
+ * @endcode
+ */
+ template<class TClass>
+ void asyncCall(TClass* instance, void (TClass::*function)())
+ {
+ CAmDelegagePtr p(new CAmNoArgDelegate<TClass>(instance, function));
+ send(p);
+ }
+
+ /**
+ * calls a function with one arguments asynchronously threadsafe
+ * @param instance the instance of the class that shall be called
+ * @param function the function that shall be called as memberfunction pointer.
+ * @param argument the argument
+ * @tparam TClass1 the type of the Class to be called
+ * @tparam Targ the type of the argument to be called
+ * \section ex Example:
+ * @code
+ * class myClass
+ * {
+ * public:
+ * void myfunction(int k);
+ * }
+ * CAmSerializer serial(&Sockethandler);
+ * myClass instanceMyClass;
+ * serial<CommandSender,int>(&instanceMyClass,&myClass::myfunction,k);
+ * @endcode
+ *
+ */
+ template<class TClass1, class Targ>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ), Targ argument)
+ {
+ CAmDelegagePtr p(new CAmOneArgDelegate<TClass1, Targ>(instance, function, argument));
+ send(p);
+ }
+
+ /**
+ * calls a function with one argument called by reference asynchronously threadsafe
+ * @param instance the instance of the class that shall be called
+ * @param function the function that shall be called as memberfunction pointer.
+ * @param argument the argument
+ * @tparam TClass1 the type of the Class to be called
+ * @tparam Targ the type of the argument to be called
+ * \section ex Example:
+ * @code
+ * class myClass
+ * {
+ * public:
+ * void myfunction(int k);
+ * }
+ * CAmSerializer serial(&Sockethandler);
+ * myClass instanceMyClass;
+ * serial<CommandSender,int>(&instanceMyClass,&myClass::myfunction,k);
+ * @endcode
+ *
+ */
+ template<class TClass1, class Targ>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ&), Targ& argument)
+ {
+ CAmDelegagePtr p(new CAmOneArgDelegateRef<TClass1, Targ>(instance, function, argument));
+ send(p);
+ }
+
+ /**
+ * calls a function with two arguments asynchronously threadsafe. for more see asyncCall with one argument
+ * @param instance pointer to the instance of the class
+ * @param function memberfunction poitner
+ * @param argument the first argument
+ * @param argument1 the second argument
+ * @tparam TClass1 the type of the Class to be called
+ * @tparam Targ the type of the argument to be called
+ * @tparam Targ1 the type of the first argument to be called
+ */
+ template<class TClass1, class Targ, class Targ1>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1), Targ argument, Targ1 argument1)
+ {
+ CAmDelegagePtr p(new CAmTwoArgDelegate<TClass1, Targ, Targ1>(instance, function, argument, argument1));
+ send(p);
+ }
+
+ /**
+ * calls a function with two arguments asynchronously threadsafe, first argument is a reference. for more see asyncCall with one argument
+ * @param instance pointer to the instance of the class
+ * @param function memberfunction poitner
+ * @param argument the first argument
+ * @param argument1 the second argument
+ * @tparam TClass1 the type of the Class to be called
+ * @tparam Targ the type of the argument to be called
+ * @tparam Targ1 the type of the first argument to be called
+ */
+ template<class TClass1, class Targ, class Targ1>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1 argument1), Targ& argument, Targ1 argument1)
+ {
+ CAmDelegagePtr p(new CAmTwoArgDelegateFirstRef<TClass1, Targ, Targ1>(instance, function, argument, argument1));
+ send(p);
+ }
+
+ /**
+ * calls a function with two arguments asynchronously threadsafe, second argument is a reference. for more see asyncCall with one argument
+ * @param instance pointer to the instance of the class
+ * @param function memberfunction poitner
+ * @param argument the first argument
+ * @param argument1 the second argument
+ * @tparam TClass1 the type of the Class to be called
+ * @tparam Targ the type of the argument to be called
+ * @tparam Targ1 the type of the first argument to be called
+ */
+ template<class TClass1, class Targ, class Targ1>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1& argument1), Targ argument, Targ1& argument1)
+ {
+ CAmDelegagePtr p(new CAmTwoArgDelegateSecondRef<TClass1, Targ, Targ1>(instance, function, argument, argument1));
+ send(p);
+ }
+
+ /**
+ * calls a function with two arguments asynchronously threadsafe, both arguments are references. for more see asyncCall with one argument
+ * @param instance pointer to the instance of the class
+ * @param function memberfunction poitner
+ * @param argument the first argument
+ * @param argument1 the second argument
+ * @tparam TClass1 the type of the Class to be called
+ * @tparam Targ the type of the argument to be called
+ * @tparam Targ1 the type of the first argument to be called
+ */
+ template<class TClass1, class Targ, class Targ1>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1& argument1), Targ& argument, Targ1& argument1)
+ {
+ CAmDelegagePtr p(new CAmTwoArgDelegateAllRef<TClass1, Targ, Targ1>(instance, function, argument, argument1));
+ send(p);
+ }
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1, Targ2 argument2), Targ argument, Targ1 argument1, Targ2 argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegate<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1 argument1, Targ2 argument2), Targ& argument, Targ1 argument1, Targ2 argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegateFirstRef<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1& argument1, Targ2 argument2), Targ argument, Targ1& argument1, Targ2 argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegateSecondRef<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1, Targ2& argument2), Targ argument, Targ1 argument1, Targ2& argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegateThirdRef<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1& argument1, Targ2& argument2), Targ argument, Targ1& argument1, Targ2& argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegateSecondThirdRef<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1& argument1, Targ2& argument2), Targ& argument, Targ1& argument1, Targ2& argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegateAllRef<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1& argument1, Targ2 argument2), Targ& argument, Targ1& argument1, Targ2 argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegateFirstSecondRef<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+ /**
+ * calls a function with three arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ& argument, Targ1 argument1, Targ2& argument2), Targ& argument, Targ1 argument1, Targ2& argument2)
+ {
+ CAmDelegagePtr p(new CAmThreeArgDelegateFirstThirdRef<TClass1, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(p);
+ }
+
+ /**
+ * calls a function with four arguments asynchronously threadsafe. for more see other asycCall
+ */
+ template<class TClass1, class Targ, class Targ1, class Targ2, class Targ3>
+ void asyncCall(TClass1* instance, void (TClass1::*function)(Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3), Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3)
+ {
+ CAmDelegagePtr p(new CAmFourArgDelegate<TClass1, Targ, Targ1, Targ2, Targ3>(instance, function, argument, argument1, argument2, argument3));
+ send(p);
+ }
+
+ /**
+ * calls a synchronous function with no arguments threadsafe
+ * @param instance the instance of the class that shall be called
+ * @param function the function that shall be called as memberfunction pointer.
+ * @param retVal the return parameter, no const allowed !
+ * @tparam TClass1 the type of the class to be called
+ * @tparam TretVal the type of the return parameter
+ * \section ex Example:
+ * @code
+ * class myClass
+ * {
+ * public:
+ * am_Error_e myfunction();
+ * }
+ * CAmSerializer serial(&Sockethandler);
+ * myClass instanceMyClass;
+ * am_Error_e error;
+ * serial<CommandSender,am_Error_e>(&instanceMyClass,&myClass::myfunction, error);
+ * @endcode
+ * All arguments given to synchronous functions must be non-const since the results of the operations will be written back to the arguments.
+ *
+ */
+ template<class TClass1, class TretVal>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(), TretVal& retVal)
+ {
+ CAmSyncNoArgDelegate<TClass1, TretVal>* p(new CAmSyncNoArgDelegate<TClass1, TretVal>(instance, function));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it works...
+ retVal = p->returnResults();
+ delete p;
+ }
+
+ /**
+ * calls a function with one argument synchronous threadsafe
+ * @param instance the instance of the class that shall be called
+ * @param function the function that shall be called as memberfunction pointer.
+ * @param retVal the return parameter, no const allowed !
+ * @param argument the argument, no const allowed !
+ * @tparam TClass1 the type of the class to be called
+ * @tparam TretVal the type of the return parameter
+ * @tparam TargCall the type of the argument like in the function to be called. here all references and const must be
+ * respected!
+ * @tparam Targ the type of the argument, here no const and no references allowed !
+ * \section ex Example:
+ * @code
+ * class myClass
+ * {
+ * public:
+ * am_Error_e myfunction(int k);
+ * }
+ * CAmSerializer serial(&Sockethandler);
+ * myClass instanceMyClass;
+ * am_Error_e error;
+ * int l;
+ * serial<CommandSender,am_Error_e,int>(&instanceMyClass,&myClass::myfunction,error,l);
+ * @endcode
+ * All arguments given to synchronous functions must be non-const since the results of the operations will be written back to the arguments.
+ */
+ template<class TClass1, class TretVal, class TargCall, class Targ>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall), TretVal& retVal, Targ& argument)
+ {
+ CAmSyncOneArgDelegate<TClass1, TretVal, TargCall, Targ>* p(new CAmSyncOneArgDelegate<TClass1, TretVal, TargCall, Targ>(instance, function, argument));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it works...
+ retVal = p->returnResults(argument);
+ delete p;
+ }
+
+ /**
+ * calls a function with one argument synchronous threadsafe for const functions. For more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class Targ>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall) const, TretVal& retVal, Targ& argument)
+ {
+ CAmSyncOneArgConstDelegate<TClass1, TretVal, TargCall, Targ>* p(new CAmSyncOneArgConstDelegate<TClass1, TretVal, TargCall, Targ>(instance, function, argument));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it works...
+ retVal = p->returnResults(argument);
+ delete p;
+ }
+
+ /**
+ * calls a function with two arguments synchronously threadsafe. For more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class Targ1Call, class Targ, class Targ1>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, Targ1Call), TretVal& retVal, Targ& argument, Targ1& argument1)
+ {
+ CAmSyncTwoArgDelegate<TClass1, TretVal, TargCall, Targ1Call, Targ, Targ1>* p(new CAmSyncTwoArgDelegate<TClass1, TretVal, TargCall, Targ1Call, Targ, Targ1>(instance, function, argument, argument1));
+ send(dynamic_cast<CAmDelegagePtr>(p));
+
+ CAmDelegagePtr ptr;
+ if (read(mReturnPipe[0], &ptr, sizeof(ptr)) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ retVal = p->returnResults(argument, argument1);
+ delete p;
+ }
+ /**
+ * calls a function with two arguments synchronously threadsafe const. For more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class Targ1Call, class Targ, class Targ1>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, Targ1Call) const, TretVal& retVal, Targ& argument, Targ1& argument1)
+ {
+ CAmSyncTwoArgConstDelegate<TClass1, TretVal, TargCall, Targ1Call, Targ, Targ1>* p(new CAmSyncTwoArgConstDelegate<TClass1, TretVal, TargCall, Targ1Call, Targ, Targ1>(instance, function, argument, argument1));
+ send(dynamic_cast<CAmDelegagePtr>(p));
+
+ CAmDelegagePtr ptr;
+ if (read(mReturnPipe[0], &ptr, sizeof(ptr)) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ retVal = p->returnResults(argument, argument1);
+ delete p;
+ }
+
+ /**
+ * calls a function with three arguments synchronously threadsafe. for more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class Targ, class Targ1, class Targ2>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2)
+ {
+ CAmSyncThreeArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, Targ, Targ1, Targ2>* p(new CAmSyncThreeArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it worCAmTwoArgDelegateks...
+ retVal = p->returnResults(argument, argument1, argument2);
+ delete p;
+ }
+
+ /**
+ * calls a const function with three arguments synchronously threadsafe. for more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class Targ, class Targ1, class Targ2>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2) const, TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2)
+ {
+ CAmSyncThreeArgConstDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, Targ, Targ1, Targ2>* p(new CAmSyncThreeArgConstDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, Targ, Targ1, Targ2>(instance, function, argument, argument1, argument2));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it worCAmTwoArgDelegateks...
+ retVal = p->returnResults(argument, argument1, argument2);
+ delete p;
+ }
+
+ /**
+ * calls a function with four arguments synchronously threadsafe. for more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class TargCall3, class Targ, class Targ1, class Targ2, class Targ3>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3)
+ {
+ CAmSyncFourArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, TargCall3, Targ, Targ1, Targ2, Targ3>* p(new CAmSyncFourArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, TargCall3, Targ, Targ1, Targ2, Targ3>(instance, function, argument, argument1, argument2, argument3));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it works...
+ retVal = p->returnResults(argument, argument1, argument2, argument3);
+ delete p;
+ }
+
+ /**
+ * calls a function with five arguments synchronously threadsafe. for more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class TargCall3, class TargCall4, class Targ, class Targ1, class Targ2, class Targ3, class Targ4>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3, TargCall4), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3, Targ4& argument4)
+ {
+ CAmSyncFiveArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, TargCall3, TargCall4, Targ, Targ1, Targ2, Targ3, Targ4>* p(new CAmSyncFiveArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, TargCall3, TargCall4, Targ, Targ1, Targ2, Targ3, Targ4>(instance, function, argument, argument1, argument2, argument3, argument4));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it works...
+ retVal = p->returnResults(argument, argument1, argument2, argument3, argument4);
+ delete p;
+ }
+
+ /**
+ * calls a function with six arguments synchronously threadsafe. for more see syncCall with one argument
+ */
+ template<class TClass1, class TretVal, class TargCall, class TargCall1, class TargCall2, class TargCall3, class TargCall4, class TargCall5, class Targ, class Targ1, class Targ2, class Targ3, class Targ4, class Targ5>
+ void syncCall(TClass1* instance, TretVal (TClass1::*function)(TargCall, TargCall1, TargCall2, TargCall3, TargCall4, TargCall5), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3, Targ4& argument4, Targ5& argument5)
+ {
+ CAmSyncSixArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, TargCall3, TargCall4, TargCall5, Targ, Targ1, Targ2, Targ3, Targ4, Targ5>* p(new CAmSyncSixArgDelegate<TClass1, TretVal, TargCall, TargCall1, TargCall2, TargCall3, TargCall4, TargCall5, Targ, Targ1, Targ2, Targ3, Targ4, Targ5>(instance, function, argument, argument1, argument2, argument3, argument4, argument5));
+ send(static_cast<CAmDelegagePtr>(p));
+ int numReads;
+ CAmDelegagePtr ptr;
+ if ((numReads = read(mReturnPipe[0], &ptr, sizeof(ptr))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ //working with friend class here is not the finest of all programming stiles but it works...
+ retVal = p->returnResults(argument, argument1, argument2, argument3, argument4, argument5);
+ delete p;
+ }
+
+ /**
+ * receiver callback for sockethandling, for more, see CAmSocketHandler
+ */
+ void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
+ {
+ (void) handle;
+ (void) userData;
+ int numReads;
+ CAmDelegagePtr listPointers[3];
+ if ((numReads = read(pollfd.fd, &listPointers, sizeof(listPointers))) == -1)
+ {
+ logError("CAmSerializer::receiverCallback could not read pipe!");
+ throw std::runtime_error("CAmSerializer Could not read pipe!");
+ }
+ mListDelegatePoiters.assign(listPointers, listPointers + (numReads / sizeof(CAmDelegagePtr)));
+ }
+
+ /**
+ * checker callback for sockethandling, for more, see CAmSocketHandler
+ */
+ bool checkerCallback(const sh_pollHandle_t handle, void* userData)
+ {
+ (void) handle;
+ (void) userData;
+ if (mListDelegatePoiters.empty())
+ return (false);
+ return (true);
+ }
+
+ /**
+ * dispatcher callback for sockethandling, for more, see CAmSocketHandler
+ */
+ bool dispatcherCallback(const sh_pollHandle_t handle, void* userData)
+ {
+ (void) handle;
+ (void) userData;
+ CAmDelegagePtr delegatePoiter = mListDelegatePoiters.front();
+ mListDelegatePoiters.pop_front();
+ if (delegatePoiter->call(mReturnPipe))
+ delete delegatePoiter;
+ if (mListDelegatePoiters.empty())
+ return (false);
+ return (true);
+ }
+
+ TAmShPollFired<CAmSerializer> receiverCallbackT;
+ TAmShPollDispatch<CAmSerializer> dispatcherCallbackT;
+ TAmShPollCheck<CAmSerializer> checkerCallbackT;
+
+ /**
+ * The constructor must be called in the mainthread context !
+ * @param iSocketHandler pointer to the CAmSocketHandler
+ */
+ CAmSerializer(CAmSocketHandler *iSocketHandler) :
+ mPipe(), //
+ mListDelegatePoiters(), //
+ receiverCallbackT(this, &CAmSerializer::receiverCallback), //
+ dispatcherCallbackT(this, &CAmSerializer::dispatcherCallback), //
+ checkerCallbackT(this, &CAmSerializer::checkerCallback)
+ {
+ if (pipe(mPipe) == -1)
+ {
+ logError("CAmSerializer could not create pipe!");
+ throw std::runtime_error("CAmSerializer Could not open pipe!");
+ }
+
+ if (pipe(mReturnPipe) == -1)
+ {
+ logError("CAmSerializer could not create mReturnPipe!");
+ throw std::runtime_error("CAmSerializer Could not open mReturnPipe!");
+ }
+
+ short event = 0;
+ sh_pollHandle_t handle;
+ event |= POLLIN;
+ iSocketHandler->addFDPoll(mPipe[0], event, NULL, &receiverCallbackT, &checkerCallbackT, &dispatcherCallbackT, NULL, handle);
+ }
+
+ ~CAmSerializer()
+ {
+ }
+};
+} /* namespace am */
+#endif /* CAMSERIALIZER_H_ */
diff --git a/AudioManagerUtilities/include/CAmSocketHandler.h b/AudioManagerUtilities/include/CAmSocketHandler.h
new file mode 100644
index 0000000..7759346
--- /dev/null
+++ b/AudioManagerUtilities/include/CAmSocketHandler.h
@@ -0,0 +1,576 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ *
+ * \copyright
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * \file CAmSocketHandler.h
+ * For further information see http://www.genivi.org/.
+ */
+
+#ifndef SOCKETHANDLER_H_
+#define SOCKETHANDLER_H_
+
+#include "audiomanagertypes.h"
+#include <sys/socket.h>
+#include <stdint.h>
+#include <sys/poll.h>
+#include <list>
+#include <map>
+#include <signal.h>
+
+#include <iostream> //todo: remove me
+namespace am
+{
+
+#define MAX_NS 1000000000L
+
+typedef uint16_t sh_timerHandle_t; //!<this is a handle for a timer to be used with the SocketHandler
+typedef uint16_t sh_pollHandle_t; //!<this is a handle for a filedescriptor to be used with the SocketHandler
+
+/**
+ * prototype for poll prepared callback
+ */
+class IAmShPollPrepare
+{
+public:
+ virtual void Call(const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~IAmShPollPrepare()
+ {
+ }
+ ;
+};
+
+/**
+ * prototype for poll fired callback
+ */
+class IAmShPollFired
+{
+public:
+ virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~ IAmShPollFired()
+ {
+ }
+ ;
+};
+
+/**
+ * prototype for poll check callback
+ */
+class IAmShPollCheck
+{
+public:
+ virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~ IAmShPollCheck()
+ {
+ }
+ ;
+};
+
+/**
+ * prototype for dispatch callback
+ */
+class IAmShPollDispatch
+{
+public:
+ virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~ IAmShPollDispatch()
+ {
+ }
+ ;
+};
+
+/**
+ * prototype for the timer callback
+ */
+class IAmShTimerCallBack
+{
+public:
+ virtual void Call(const sh_timerHandle_t handle, void* userData)=0;
+ virtual ~IAmShTimerCallBack()
+ {
+ }
+ ;
+};
+
+/**
+ * The am::CAmSocketHandler implements a mainloop for the AudioManager. Plugins and different parts of the AudioManager add their filedescriptors to the handler
+ * to get called on communication of the filedescriptors.\n
+ * More information can be found here : \ref mainl
+ */
+class CAmSocketHandler
+{
+public:
+ template<class TClass> class TAmShPollFired: public IAmShPollFired
+ {
+ private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
+
+ public:
+ TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
+ mInstance(instance), //
+ mFunction(function) {};
+
+ virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
+ {
+ (*mInstance.*mFunction)(pollfd, handle, userData);
+ };
+ };
+
+ template<class TClass> class TAmShPollCheck: public IAmShPollCheck
+ {
+ private:
+ TClass* mInstance;
+ bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
+
+ public:
+ TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
+ mInstance(instance), //
+ mFunction(function) {};
+
+ virtual bool Call(const sh_pollHandle_t handle, void* userData)
+ {
+ return ((*mInstance.*mFunction)(handle, userData));
+ };
+ };
+
+ CAmSocketHandler();
+ ~CAmSocketHandler();
+
+ am_Error_e addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void* userData, sh_pollHandle_t& handle);
+ am_Error_e removeFDPoll(const sh_pollHandle_t handle);
+ am_Error_e updateEventFlags(const sh_pollHandle_t handle, const short events);
+ am_Error_e addTimer(const timespec timeouts, IAmShTimerCallBack* callback, sh_timerHandle_t& handle, void* userData);
+ am_Error_e removeTimer(const sh_timerHandle_t handle);
+ am_Error_e restartTimer(const sh_timerHandle_t handle);
+ am_Error_e updateTimer(const sh_timerHandle_t handle, const timespec timeouts);
+ am_Error_e stopTimer(const sh_timerHandle_t handle);
+ void start_listenting();
+ void stop_listening();
+ void exit_mainloop();
+ void receiverCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
+ {
+ (void) pollfd;
+ (void) handle;
+ (void) userData;
+ };
+ bool checkerCallback(const sh_pollHandle_t handle, void* userData)
+ {
+ (void) handle;
+ (void) userData;
+ return (false);
+ };
+
+ TAmShPollFired<CAmSocketHandler> receiverCallbackT;
+ TAmShPollCheck<CAmSocketHandler> checkerCallbackT;
+
+private:
+
+ static CAmSocketHandler* mInstance;
+ int mPipe[2];
+ int mDispatchDone; //this starts / stops the mainloop
+ struct sh_timer_s //!<struct that holds information of timers
+ {
+ sh_timerHandle_t handle; //!<the handle of the timer
+ timespec countdown; //!<the countdown, this value is decreased every time the timer is up
+ IAmShTimerCallBack* callback; //!<the callbackfunction
+ void * userData; //!<saves a void pointer together with the rest.
+ };
+
+ typedef std::reverse_iterator<sh_timer_s> rListTimerIter; //!<typedef for reverseiterator on timer lists
+
+ struct sh_poll_s //!<struct that holds information about polls
+ {
+ sh_pollHandle_t handle; //!<handle to uniquely adress a filedesriptor
+ IAmShPollPrepare *prepareCB; //!<pointer to preperation callback
+ IAmShPollFired *firedCB; //!<pointer to fired callback
+ IAmShPollCheck *checkCB; //!< pointer to check callback
+ IAmShPollDispatch *dispatchCB; //!<pointer to dispatch callback
+ pollfd pollfdValue; //!<the array for polling the filedescriptors
+ void *userData; //!<userdata saved together with the callback.
+ };
+
+ typedef std::vector<pollfd> mListPollfd_t; //!<vector of filedescriptors
+ typedef std::vector<sh_poll_s> mListPoll_t; //!<list for the callbacks
+
+ bool fdIsValid(const int fd) const;
+ void timerUp();
+ void timerCorrection();
+ timespec* insertTime(timespec& buffertime);
+
+ /**
+ * compares countdown values
+ * @param a
+ * @param b
+ * @return true if b greater a
+ */
+ inline static bool compareCountdown(const sh_timer_s& a, const sh_timer_s& b)
+ {
+ return ((a.countdown.tv_sec == b.countdown.tv_sec) ? (a.countdown.tv_nsec < b.countdown.tv_nsec) : (a.countdown.tv_sec < b.countdown.tv_sec));
+ }
+
+ /**
+ * Subtracts b from a
+ * @param a
+ * @param b
+ * @return subtracted value
+ */
+ inline static timespec timespecSub(const timespec& a, const timespec& b)
+ {
+ timespec result;
+
+ if ((a.tv_sec < b.tv_sec) || ((a.tv_sec == b.tv_sec) && (a.tv_nsec <= b.tv_nsec)))
+ {
+ result.tv_sec = result.tv_nsec = 0;
+ }
+ else
+ {
+ result.tv_sec = a.tv_sec - b.tv_sec;
+ if (a.tv_nsec < b.tv_nsec)
+ {
+ result.tv_nsec = a.tv_nsec + MAX_NS - b.tv_nsec;
+ result.tv_sec--; /* Borrow a second. */
+ }
+ else
+ {
+ result.tv_nsec = a.tv_nsec - b.tv_nsec;
+ }
+ }
+ return (result);
+ }
+
+ /**
+ * adds timespec values
+ * @param a
+ * @param b
+ * @return the added values
+ */
+ inline timespec timespecAdd(const timespec& a, const timespec& b)
+ {
+ timespec result;
+ result.tv_sec = a.tv_sec + b.tv_sec;
+ result.tv_nsec = a.tv_nsec + b.tv_nsec;
+ if (result.tv_nsec >= MAX_NS)
+ {
+ result.tv_sec++;
+ result.tv_nsec = result.tv_nsec - MAX_NS;
+ }
+ return (result);
+ }
+
+ /**
+ * comapares timespec values
+ * @param a
+ * @param b
+ * @return
+ */
+ inline int timespecCompare(const timespec& a, const timespec& b)
+ {
+ //less
+ if (a.tv_sec < b.tv_sec)
+ return (-1);
+ //greater
+ else if (a.tv_sec > b.tv_sec)
+ return (1);
+ //less
+ else if (a.tv_nsec < b.tv_nsec)
+ return (-1);
+ //greater
+ else if (a.tv_nsec > b.tv_nsec)
+ return (1);
+ //equal
+ else
+ return (0);
+ }
+
+ /**
+ * functor to return all fired events
+ * @param a
+ * @return
+ */
+ inline static bool eventFired(const pollfd& a)
+ {
+ return (a.revents == 0 ? false : true);
+ }
+
+ /**
+ * functor to help find the items that do not need dispatching
+ * @param a
+ * @return
+ */
+ inline static bool noDispatching(const sh_poll_s& a)
+ {
+ //remove from list of there is no checkCB
+ if (!a.checkCB)
+ return (true);
+ return (!a.checkCB->Call(a.handle, a.userData));
+ }
+
+ /**
+ * checks if dispatching is already finished
+ * @param a
+ * @return
+ */
+ inline static bool dispatchingFinished(const sh_poll_s& a)
+ {
+ //remove from list of there is no dispatchCB
+ if (!a.dispatchCB)
+ return (true);
+ return (!a.dispatchCB->Call(a.handle, a.userData));
+ }
+
+ class CAmShCopyPollfd //!< functor to copy filedescriptors into the poll array
+ {
+ private:
+ mListPollfd_t& mArray;
+ public:
+ CAmShCopyPollfd(mListPollfd_t& dest) :
+ mArray(dest)
+ {
+ }
+ void operator()(const sh_poll_s& row)
+ {
+ pollfd temp = row.pollfdValue;
+ temp.revents = 0;
+ mArray.push_back(temp);
+ }
+ };
+
+ class CAmShCallFire //!< functor to call the firecallbacks
+ {
+ public:
+ CAmShCallFire()
+ {
+ }
+ ;
+ void operator()(sh_poll_s& row)
+ {
+ row.firedCB->Call(row.pollfdValue, row.handle, row.userData);
+ }
+ };
+
+ class CAmShCallPrep //!< functor to call the preparation callbacks
+ {
+ public:
+ CAmShCallPrep()
+ {
+ }
+ ;
+ void operator()(sh_poll_s& row)
+ {
+ if (row.prepareCB)
+ row.prepareCB->Call(row.handle, row.userData);
+ }
+ };
+
+ class CAmShCallTimer //!<functor to call a timer
+ {
+ public:
+ CAmShCallTimer()
+ {
+ }
+ ;
+ void operator()(sh_timer_s& row)
+ {
+ row.callback->Call(row.handle, row.userData);
+ }
+ };
+
+ class CAmShCountdownUp //!<functor that checks if a timer is up
+ {
+ private:
+ timespec mDiffTime;
+ public:
+ CAmShCountdownUp(const timespec& differenceTime) :
+ mDiffTime(differenceTime)
+ {
+ }
+ ;
+ bool operator()(const sh_timer_s& row)
+ {
+ timespec sub = timespecSub(row.countdown, mDiffTime);
+ if (sub.tv_nsec == 0 && sub.tv_sec == 0)
+ return (true);
+ return (false);
+ }
+ };
+
+ class CAmShCountdownZero //!<functor that checks if a timer is zero
+ {
+ public:
+ CAmShCountdownZero()
+ {
+ }
+ ;
+ bool operator()(const sh_timer_s& row)
+ {
+ if (row.countdown.tv_nsec == 0 && row.countdown.tv_sec == 0)
+ return (true);
+ return (false);
+ }
+ };
+
+ class CAmShSubstractTime //!<functor to easy substract from each countdown value
+ {
+ private:
+ timespec param;
+ public:
+ CAmShSubstractTime(timespec param) :
+ param(param)
+ {
+ }
+ inline void operator()(sh_timer_s& t)
+ {
+ t.countdown = timespecSub(t.countdown, param);
+ }
+ };
+
+ mListPollfd_t mfdPollingArray; //!<the polling array for ppoll
+ mListPoll_t mListPoll; //!<list that holds all information for the ppoll
+ std::list<sh_timer_s> mListTimer; //!<list of all timers
+ std::list<sh_timer_s> mListActiveTimer; //!<list of all currently active timers
+ sh_timerHandle_t mLastInsertedHandle; //!<keeps track of the last inserted timer handle
+ sh_pollHandle_t mLastInsertedPollHandle; //!<keeps track of the last inserted poll handle
+ bool mRecreatePollfds; //!<when this is true, the poll list needs to be recreated
+ timespec mStartTime; //!<here the actual time is saved for timecorrection
+
+// void debugPrintTimerList ()
+// {
+// std::list<sh_timer_s>::iterator it(mListActiveTimer.begin());
+// for(;it!=mListActiveTimer.end();++it)
+// {
+// std::cout<< "Handle " << it->handle << "sec " << it->countdown.tv_sec << "nsec " << it->countdown.tv_nsec<<std::endl;
+// }
+// }
+}
+;
+
+/**
+ * template to create the functor for a class
+ */
+template<class TClass> class TAmShTimerCallBack: public IAmShTimerCallBack
+{
+private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(sh_timerHandle_t handle, void* userData);
+
+public:
+ TAmShTimerCallBack(TClass* instance, void (TClass::*function)(sh_timerHandle_t handle, void* userData)) :
+ mInstance(instance), //
+ mFunction(function)
+ {
+ }
+ ;
+
+ virtual void Call(sh_timerHandle_t handle, void* userData)
+ {
+ (*mInstance.*mFunction)(handle, userData);
+ }
+};
+
+/**
+ * template for a callback
+ */
+template<class TClass> class TAmShPollPrepare: public IAmShPollPrepare
+{
+private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(const sh_timerHandle_t handle, void* userData);
+
+public:
+ TAmShPollPrepare(TClass* instance, void (TClass::*function)(const sh_timerHandle_t handle, void* userData)) :
+ mInstance(instance), //
+ mFunction(function)
+ {
+ }
+ ;
+
+ virtual void Call(const sh_timerHandle_t handle, void* userData)
+ {
+ (*mInstance.*mFunction)(handle, userData);
+ }
+ ;
+};
+
+/**make private, not public
+ * template for a callback
+ */
+template<class TClass> class TAmShPollFired: public IAmShPollFired
+{
+private:
+ TClass* mInstance;
+ void (TClass::*mFunction)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
+
+public:
+ TAmShPollFired(TClass* instance, void (TClass::*function)(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)) :
+ mInstance(instance), //
+ mFunction(function)
+ {
+ }
+ ;
+
+ virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)
+ {
+ (*mInstance.*mFunction)(pollfd, handle, userData);
+ }
+ ;
+};
+
+/**
+ * template for a callback
+ */
+template<class TClass> class TAmShPollCheck: public IAmShPollCheck
+{
+private:
+ TClass* mInstance;
+ bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
+
+public:
+ TAmShPollCheck(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
+ mInstance(instance), //
+ mFunction(function)
+ {
+ }
+ ;
+
+ virtual bool Call(const sh_pollHandle_t handle, void* userData)
+ {
+ return ((*mInstance.*mFunction)(handle, userData));
+ }
+ ;
+};
+
+/**
+ * template for a callback
+ */
+template<class TClass> class TAmShPollDispatch: public IAmShPollDispatch
+{
+private:
+ TClass* mInstance;
+ bool (TClass::*mFunction)(const sh_pollHandle_t handle, void* userData);
+
+public:
+ TAmShPollDispatch(TClass* instance, bool (TClass::*function)(const sh_pollHandle_t handle, void* userData)) :
+ mInstance(instance), //
+ mFunction(function)
+ {
+ }
+ ;
+
+ virtual bool Call(const sh_pollHandle_t handle, void* userData)
+ {
+ return ((*mInstance.*mFunction)(handle, userData));
+ }
+ ;
+};
+} /* namespace am */
+#endif /* SOCKETHANDLER_H_ */
diff --git a/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp b/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp
new file mode 100644
index 0000000..46aa004
--- /dev/null
+++ b/AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp
@@ -0,0 +1,275 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ * \author Aleksandar Donchev, aleksander.donchev@partner.bmw.de BMW 2013
+ *
+ * \copyright
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * \file CAmCommonAPIWrapper.cpp
+ * For further information see http://www.genivi.org/.
+ */
+
+
+#include <config.h>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <algorithm>
+#include <cassert>
+#include <cstdlib>
+#include <stdexcept>
+#include <poll.h>
+#include <tuple>
+#include "audiomanagertypes.h"
+#include "CAmSocketHandler.h"
+#include "CAmDltWrapper.h"
+#include "CAmCommonAPIWrapper.h"
+
+
+namespace am
+{
+static CAmCommonAPIWrapper* pSingleCommonAPIInstance = NULL;
+
+
+using namespace CommonAPI;
+
+CAmCommonAPIWrapper::CAmCommonAPIWrapper(CAmSocketHandler* socketHandler):
+ pCommonPrepareCallback(this,&CAmCommonAPIWrapper::commonPrepareCallback), //
+ pCommonDispatchCallback(this, &CAmCommonAPIWrapper::commonDispatchCallback), //
+ pCommonFireCallback(this, &CAmCommonAPIWrapper::commonFireCallback), //
+ pCommonCheckCallback(this, &CAmCommonAPIWrapper::commonCheckCallback), //
+ pCommonTimerCallback(this, &CAmCommonAPIWrapper::commonTimerCallback), //
+ mpSocketHandler(socketHandler), //
+ mWatchToCheck(NULL)
+{
+ assert(NULL!=socketHandler);
+//1. Load the runtime
+ std::shared_ptr<CommonAPI::Runtime> runtime = CommonAPI::Runtime::load();
+//2. Get the context and store a pointer to it
+ mContext = runtime->getNewMainLoopContext();
+//3. Make subscriptions
+ mDispatchSourceListenerSubscription = mContext->subscribeForDispatchSources(
+ std::bind(&CAmCommonAPIWrapper::registerDispatchSource, this, std::placeholders::_1, std::placeholders::_2),
+ std::bind(&CAmCommonAPIWrapper::deregisterDispatchSource, this, std::placeholders::_1));
+ mWatchListenerSubscription = mContext->subscribeForWatches(
+ std::bind(&CAmCommonAPIWrapper::registerWatch, this, std::placeholders::_1, std::placeholders::_2),
+ std::bind(&CAmCommonAPIWrapper::deregisterWatch, this, std::placeholders::_1));
+ mTimeoutSourceListenerSubscription = mContext->subscribeForTimeouts(
+ std::bind(&CAmCommonAPIWrapper::registerTimeout, this, std::placeholders::_1, std::placeholders::_2),
+ std::bind(&CAmCommonAPIWrapper::deregisterTimeout, this, std::placeholders::_1));
+//4. Create the factory
+ std::shared_ptr<CommonAPI::Factory> factory = runtime->createFactory(mContext);
+ assert(factory);
+ logInfo(__PRETTY_FUNCTION__,"CommonAPI -> Factory created");
+ mFactory = factory;
+//5. Get the publisher V.2.1
+// std::shared_ptr<CommonAPI::ServicePublisher> servicePublisher = runtime->getServicePublisher();
+// assert(servicePublisher);
+// logInfo(__PRETTY_FUNCTION__,"CommonAPI -> Publisher available");
+//6. Instantiate your concrete stub implementations
+// std::shared_ptr<StubImpl> theStub = std::make_shared<StubImpl>(1);
+//7. Register the services
+// std::string capiAddress("local:org.genivi.audiomanager.sourcestate:de.bmw.infotainment.broadcast.ta");
+// registerStub(theStub, capiAddress);
+}
+
+CAmCommonAPIWrapper::~CAmCommonAPIWrapper()
+{
+ mContext->unsubscribeForDispatchSources(mDispatchSourceListenerSubscription);
+ mContext->unsubscribeForWatches(mWatchListenerSubscription);
+ mContext->unsubscribeForTimeouts(mTimeoutSourceListenerSubscription);
+//The following objects must be released in the given order.
+ mFactory.reset();
+ mContext.reset();
+
+ mpSocketHandler = NULL;
+ mWatchToCheck = NULL;
+}
+
+CAmCommonAPIWrapper* CAmCommonAPIWrapper::instantiateOnce(CAmSocketHandler* socketHandler)
+{
+ if(NULL==pSingleCommonAPIInstance)
+ {
+ if(NULL==socketHandler)
+ throw std::runtime_error(std::string("Expected a valid socket handler. The socket handler pointer must not be NULL."));
+ else
+ pSingleCommonAPIInstance = new CAmCommonAPIWrapper(socketHandler);
+ }
+ else
+ throw std::logic_error(std::string("The singleton instance has been already instantiated. This method should be called only once."));
+ return pSingleCommonAPIInstance;
+}
+
+CAmCommonAPIWrapper* CAmCommonAPIWrapper::getInstance()
+{
+ assert(NULL!=pSingleCommonAPIInstance);
+ return pSingleCommonAPIInstance;
+}
+
+std::shared_ptr<CommonAPI::Factory> CAmCommonAPIWrapper::factory() const
+{
+ return mFactory;
+}
+
+
+std::shared_ptr<CommonAPI::Runtime> CAmCommonAPIWrapper::runtime() const
+{
+ return mFactory->getRuntime();
+}
+
+bool CAmCommonAPIWrapper::commonDispatchCallback(const sh_pollHandle_t handle, void *userData)
+{
+ (void) handle;
+ (void) userData;
+
+ std::list<DispatchSource*>::iterator iterator(mSourcesToDispatch.begin());
+ for(;iterator!=mSourcesToDispatch.end();)
+ {
+ DispatchSource* source = *iterator;
+ if (!source->dispatch()) {
+ iterator=mSourcesToDispatch.erase(iterator);
+ }
+ else
+ iterator++;
+ }
+ if (!mSourcesToDispatch.empty())
+ return (true);
+
+ return false;
+}
+
+bool CAmCommonAPIWrapper::commonCheckCallback(const sh_pollHandle_t, void *)
+{
+ std::vector<DispatchSource*> vecDispatch=mWatchToCheck->getDependentDispatchSources();
+ mSourcesToDispatch.insert(mSourcesToDispatch.end(), vecDispatch.begin(), vecDispatch.end());
+
+ return (mWatchToCheck || !mSourcesToDispatch.empty());
+}
+
+void CAmCommonAPIWrapper::commonFireCallback(const pollfd pollfd, const sh_pollHandle_t, void *)
+{
+ mWatchToCheck=NULL;
+ try
+ {
+ mWatchToCheck=mMapWatches.at(pollfd.fd);
+ }
+ catch (const std::out_of_range& error) {
+ logInfo(__PRETTY_FUNCTION__,error.what());
+ return;
+ }
+
+ mWatchToCheck->dispatch(pollfd.events);
+}
+
+void CAmCommonAPIWrapper::commonPrepareCallback(const sh_pollHandle_t, void*)
+{
+ for (auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
+ dispatchSourceIterator != mRegisteredDispatchSources.end();
+ dispatchSourceIterator++)
+ {
+ int64_t dispatchTimeout(TIMEOUT_INFINITE);
+ if(dispatchSourceIterator->second->prepare(dispatchTimeout))
+ {
+ while (dispatchSourceIterator->second->dispatch());
+ }
+ }
+}
+
+void CAmCommonAPIWrapper::registerDispatchSource(DispatchSource* dispatchSource, const DispatchPriority dispatchPriority)
+{
+ mRegisteredDispatchSources.insert({dispatchPriority, dispatchSource});
+}
+
+void CAmCommonAPIWrapper::deregisterDispatchSource(DispatchSource* dispatchSource)
+{
+ for(auto dispatchSourceIterator = mRegisteredDispatchSources.begin();
+ dispatchSourceIterator != mRegisteredDispatchSources.end();
+ dispatchSourceIterator++) {
+
+ if(dispatchSourceIterator->second == dispatchSource) {
+ mRegisteredDispatchSources.erase(dispatchSourceIterator);
+ break;
+ }
+ }
+}
+
+void CAmCommonAPIWrapper::deregisterWatch(Watch* watch)
+{
+ logInfo(__PRETTY_FUNCTION__);
+ for(std::map<int,Watch*>::iterator iter(mMapWatches.begin());iter!=mMapWatches.end();iter++)
+ {
+ if (iter->second == watch)
+ {
+ mMapWatches.erase(iter);
+ break;
+ }
+ }
+}
+
+void CAmCommonAPIWrapper::registerTimeout(Timeout* timeout, const DispatchPriority)
+{
+ logInfo(__PRETTY_FUNCTION__);
+ timespec pollTimeout;
+ int64_t localTimeout = timeout->getTimeoutInterval();
+
+ pollTimeout.tv_sec = localTimeout / 1000;
+ pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
+
+ //prepare handle and callback. new is eval, but there is no other choice because we need the pointer!
+ sh_timerHandle_t handle;
+ timerHandles myHandle({handle,timeout});
+ mpListTimerhandles.push_back(myHandle);
+
+ //add the timer to the pollLoop
+ mpSocketHandler->addTimer(pollTimeout, &pCommonTimerCallback, handle, timeout);
+
+ return;
+}
+
+void CAmCommonAPIWrapper::deregisterTimeout(Timeout* timeout)
+{
+ logInfo(__PRETTY_FUNCTION__);
+ for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
+ {
+ if(iter->timeout==timeout)
+ {
+ mpSocketHandler->removeTimer(iter->handle);
+ }
+ }
+}
+
+void CAmCommonAPIWrapper::registerWatch(Watch* watch, const DispatchPriority)
+{
+ logInfo(__PRETTY_FUNCTION__);
+ pollfd pollfd_ (watch->getAssociatedFileDescriptor());
+ sh_pollHandle_t handle (0);
+
+ am_Error_e error = mpSocketHandler->addFDPoll(pollfd_.fd, pollfd_.events, &pCommonPrepareCallback, &pCommonFireCallback, &pCommonCheckCallback, &pCommonDispatchCallback, watch, handle);
+
+ //if everything is alright, add the watch and the handle to our map so we know this relationship
+ if (error == !am_Error_e::E_OK || handle == 0)
+ logError(__PRETTY_FUNCTION__,"entering watch failed");
+
+ mMapWatches.insert(std::make_pair(pollfd_.fd,watch));
+}
+
+void CAmCommonAPIWrapper::commonTimerCallback(sh_timerHandle_t handle, void *)
+{
+ for( std::vector<timerHandles>::iterator iter(mpListTimerhandles.begin());iter!=mpListTimerhandles.end();iter++)
+ {
+ if(iter->handle==handle)
+ {
+ iter->timeout->dispatch();
+ }
+ }
+}
+
+}
diff --git a/AudioManagerUtilities/src/CAmDbusWrapper.cpp b/AudioManagerUtilities/src/CAmDbusWrapper.cpp
new file mode 100644
index 0000000..c5230ec
--- /dev/null
+++ b/AudioManagerUtilities/src/CAmDbusWrapper.cpp
@@ -0,0 +1,472 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * This file is part of GENIVI Project AudioManager.
+ *
+ * Contributions are licensed to the GENIVI Alliance under one or more
+ * Contribution License Agreements.
+ *
+ * \copyright
+ * This Source Code Form is subject to the terms of the
+ * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
+ * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ *
+ * \file CAmDbusWrapper.cpp
+ * For further information see http://www.genivi.org/.
+ *
+ */
+
+#include "CAmDbusWrapper.h"
+#include <config.h>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <cassert>
+#include <cstdlib>
+#include <stdexcept>
+#include "CAmDltWrapper.h"
+#include "CAmSocketHandler.h"
+
+namespace am
+{
+
+/**
+ * introspectio header
+ */
+#define ROOT_INTROSPECT_XML \
+DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
+"<node>" \
+"<interface name='org.AudioManager.freedesktop.DBus.Introspectable'>" \
+"<method name='Introspect'>" \
+" <arg name='xml_data' type='s' direction='out'/>" \
+"</method>" \
+"</interface>" \
+
+CAmDbusWrapper* CAmDbusWrapper::mpReference = NULL;
+
+CAmDbusWrapper::CAmDbusWrapper(CAmSocketHandler* socketHandler, DBusBusType type) :
+ pDbusPrepareCallback(this,&CAmDbusWrapper::dbusPrepareCallback), //
+ pDbusDispatchCallback(this, &CAmDbusWrapper::dbusDispatchCallback), //
+ pDbusFireCallback(this, &CAmDbusWrapper::dbusFireCallback), //
+ pDbusCheckCallback(this, &CAmDbusWrapper::dbusCheckCallback), //
+ pDbusTimerCallback(this, &CAmDbusWrapper::dbusTimerCallback), //
+ mpDbusConnection(0), //
+ mDBusError(), //
+ mListNodes(), //
+ mpListTimerhandles(), //
+ mpSocketHandler(socketHandler), //
+ mDbusType(type)
+{
+ assert(mpSocketHandler!=0);
+
+ dbus_error_init(&mDBusError);
+
+ if (!dbus_threads_init_default())
+ logError("CAmDbusWrapper::CAmDbusWrapper threads init call failed");
+ logInfo("DBusWrapper::DBusWrapper Opening DBus connection");
+ mpDbusConnection = dbus_bus_get(mDbusType, &mDBusError);
+ if (dbus_error_is_set(&mDBusError))
+ {
+ logError("DBusWrapper::DBusWrapper Error while getting the DBus");
+ dbus_error_free(&mDBusError);
+ }
+ if (NULL == mpDbusConnection)
+ {
+ logError("DBusWrapper::DBusWrapper DBus Connection is null");
+ }
+
+ //then we need to adopt the dbus to our mainloop:
+ //first, we are old enought to live longer then the connection:
+ dbus_connection_set_exit_on_disconnect(mpDbusConnection, FALSE);
+
+ //we do not need the manual dispatching, since it is not allowed to call from a different thread. So leave it uncommented:
+ //dbus_connection_set_dispatch_status_function
+
+ //add watch functions:
+ dbus_bool_t watch = dbus_connection_set_watch_functions(mpDbusConnection, addWatch, removeWatch, toogleWatch, this, NULL);
+ if (!watch)
+ {
+ logError("DBusWrapper::DBusWrapper Registering of watch functions failed");
+ }
+
+ //add timer functions:
+ dbus_bool_t timer = dbus_connection_set_timeout_functions(mpDbusConnection, addTimeout, removeTimeout, toggleTimeout, this, NULL);
+ if (!timer)
+ {
+ logError("DBusWrapper::DBusWrapper Registering of timer functions failed");
+ }
+
+ //register callback for Introspectio
+ mObjectPathVTable.message_function = CAmDbusWrapper::cbRootIntrospection;
+ logInfo("dbusconnection ",mpDbusConnection);
+ dbus_connection_register_object_path(mpDbusConnection, DBUS_SERVICE_OBJECT_PATH, &mObjectPathVTable, this);
+ int ret = dbus_bus_request_name(mpDbusConnection, DBUS_SERVICE_PREFIX, DBUS_NAME_FLAG_DO_NOT_QUEUE, &mDBusError);
+ if (dbus_error_is_set(&mDBusError))
+ {
+ logError("DBusWrapper::DBusWrapper Name Error", mDBusError.message);
+ dbus_error_free(&mDBusError);
+ }
+ if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
+ {
+ logError("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner ! Another instance already running?");
+ throw std::runtime_error("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner ! Another instance already running?");
+ }
+}
+
+CAmDbusWrapper::~CAmDbusWrapper()
+{
+ //close the connection again
+ logInfo("DBusWrapper::~DBusWrapper Closing DBus connection");
+ dbus_connection_unref(mpDbusConnection);
+
+ //clean up all timerhandles we created but did not delete before
+ std::vector<sh_timerHandle_t*>::iterator it = mpListTimerhandles.begin();
+ for (; it != mpListTimerhandles.end(); ++it)
+ {
+ delete *it;
+ }
+}
+
+/**
+ * registers a callback that is entered as path below the main path.
+ * The configuration of the mainpath is done via DBusConfiguration.h
+ * @param vtable the vtable that holds a pointer to the callback that is called when the path is called from the dbus
+ * @param path the name of the path
+ * @param userdata pointer to the class that will handle the callback
+ */
+void CAmDbusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata)
+{
+ logInfo("DBusWrapper::registerCallback register callback:", path);
+
+ std::string completePath = std::string(DBUS_SERVICE_OBJECT_PATH) + "/" + path;
+ dbus_error_init(&mDBusError);
+ mpDbusConnection = dbus_bus_get(mDbusType, &mDBusError);
+ dbus_connection_register_object_path(mpDbusConnection, completePath.c_str(), vtable, userdata);
+ if (dbus_error_is_set(&mDBusError))
+ {
+ logError("DBusWrapper::registerCallack error: ", mDBusError.message);
+ dbus_error_free(&mDBusError);
+ }
+ mListNodes.push_back(path);
+}
+
+/**
+ * internal callback for the root introspection
+ * @param conn
+ * @param msg
+ * @param reference
+ * @return
+ */
+DBusHandlerResult CAmDbusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference)
+{
+ //logInfo("DBusWrapper::~cbRootIntrospection called:");
+
+ mpReference = (CAmDbusWrapper*) reference;
+ std::vector<std::string> nodesList = mpReference->mListNodes;
+ DBusMessage * reply;
+ DBusMessageIter args;
+ dbus_uint32_t serial = 0;
+ if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
+ {
+ std::vector<std::string>::iterator nodeIter = nodesList.begin();
+ const char *xml = ROOT_INTROSPECT_XML;
+ std::stringstream introspect;
+ introspect << std::string(xml);
+ for (; nodeIter != nodesList.end(); ++nodeIter)
+ {
+ introspect << "<node name='" << nodeIter->c_str() << "'/>";
+ }
+ introspect << "</node>";
+
+ reply = dbus_message_new_method_return(msg);
+ std::string s = introspect.str();
+ const char* string = s.c_str();
+
+ // add the arguments to the reply
+ dbus_message_iter_init_append(reply, &args);
+ if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))
+ {
+ logError("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!");
+ }
+
+ // send the reply && flush the connection
+ if (!dbus_connection_send(conn, reply, &serial))
+ {
+ logError("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!");
+ }
+ dbus_connection_flush(conn);
+ // free the reply
+ dbus_message_unref(reply);
+
+ return (DBUS_HANDLER_RESULT_HANDLED);
+ }
+ else
+ {
+ return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+ }
+}
+
+/**
+ * returns the dbus connection
+ * @param connection pointer to the connection
+ */
+void CAmDbusWrapper::getDBusConnection(DBusConnection *& connection) const
+{
+ connection = mpDbusConnection;
+}
+
+dbus_bool_t CAmDbusWrapper::addWatch(DBusWatch *watch, void *userData)
+{
+ mpReference = (CAmDbusWrapper*) userData;
+ assert(mpReference!=0);
+ return (mpReference->addWatchDelegate(watch, userData));
+}
+
+dbus_bool_t CAmDbusWrapper::addWatchDelegate(DBusWatch * watch, void* userData)
+{
+ (void) userData;
+ int16_t event = 0;
+ sh_pollHandle_t handle = 0;
+ 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;
+ }
+
+ logInfo("DBusWrapper::addWatchDelegate entered new watch, fd=", dbus_watch_get_unix_fd(watch), "event flag=", event);
+ am_Error_e error = mpSocketHandler->addFDPoll(dbus_watch_get_unix_fd(watch), event, &pDbusPrepareCallback, &pDbusFireCallback, &pDbusCheckCallback, &pDbusDispatchCallback, watch, handle);
+
+ //if everything is alright, add the watch and the handle to our map so we know this relationship
+ if (error == E_OK && handle != 0)
+ {
+ mMapHandleWatch.insert(std::make_pair(watch, handle));
+ return (true);
+ }
+ logError("DBusWrapper::addWatchDelegate entering watch failed");
+ return (true);
+}
+
+void CAmDbusWrapper::removeWatch(DBusWatch *watch, void *userData)
+{
+ mpReference = (CAmDbusWrapper*) userData;
+ assert(mpReference!=0);
+ mpReference->removeWatchDelegate(watch, userData);
+}
+
+void CAmDbusWrapper::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())
+ {
+ mpSocketHandler->removeFDPoll(iterator->second);
+ logInfo("DBusWrapper::removeWatch removed watch with handle", iterator->second);
+ mMapHandleWatch.erase(iterator);
+ }
+ else
+ {
+ logError("DBusWrapper::removeWatch could not find handle !");
+ }
+}
+
+void CAmDbusWrapper::toogleWatch(DBusWatch *watch, void *userData)
+{
+ mpReference = (CAmDbusWrapper*) userData;
+ assert(mpReference!=0);
+ mpReference->toogleWatchDelegate(watch, userData);
+}
+
+void CAmDbusWrapper::toogleWatchDelegate(DBusWatch *watch, void *userData)
+{
+ (void) userData;
+ int16_t event = 0;
+ 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;
+ }
+ std::map<DBusWatch*, sh_pollHandle_t>::iterator iterator = mMapHandleWatch.begin();
+ iterator = mMapHandleWatch.find(watch);
+ if (iterator != mMapHandleWatch.end())
+ mpSocketHandler->updateEventFlags(iterator->second, event);
+}
+
+dbus_bool_t CAmDbusWrapper::addTimeout(DBusTimeout *timeout, void* userData)
+{
+ mpReference = (CAmDbusWrapper*) userData;
+ assert(mpReference!=0);
+ return (mpReference->addTimeoutDelegate(timeout, userData));
+}
+
+dbus_bool_t CAmDbusWrapper::addTimeoutDelegate(DBusTimeout *timeout, void* userData)
+{
+ (void)userData;
+
+ if (!dbus_timeout_get_enabled(timeout))
+ return (false);
+
+ //calculate the timeout in timeval
+ timespec pollTimeout;
+ int localTimeout = dbus_timeout_get_interval(timeout);
+ pollTimeout.tv_sec = localTimeout / 1000;
+ pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
+
+ //prepare handle and callback. new is eval, but there is no other choice because we need the pointer!
+ sh_timerHandle_t* handle = new sh_timerHandle_t;
+ mpListTimerhandles.push_back(handle);
+
+ //add the timer to the pollLoop
+ mpSocketHandler->addTimer(pollTimeout, &pDbusTimerCallback, *handle, timeout);
+
+ //save the handle with dbus context
+ dbus_timeout_set_data(timeout, handle, NULL);
+
+ //save timeout in Socket context
+ userData = timeout;
+ return (true);
+}
+
+void CAmDbusWrapper::removeTimeout(DBusTimeout *timeout, void* userData)
+{
+ mpReference = (CAmDbusWrapper*) userData;
+ assert(mpReference!=0);
+ mpReference->removeTimeoutDelegate(timeout, userData);
+}
+
+void CAmDbusWrapper::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);
+ mpSocketHandler->removeTimer(*handle);
+
+ //now go throught the timerlist and remove the pointer, free memory
+ std::vector<sh_timerHandle_t*>::iterator it = mpListTimerhandles.begin();
+ for (; it != mpListTimerhandles.end(); ++it)
+ {
+ if (*it == handle)
+ {
+ mpListTimerhandles.erase(it);
+ break;
+ }
+ }
+ delete handle;
+ }
+
+void CAmDbusWrapper::toggleTimeout(DBusTimeout *timeout, void* userData)
+{
+ mpReference = (CAmDbusWrapper*) userData;
+ assert(mpReference!=0);
+ mpReference->toggleTimeoutDelegate(timeout, userData);
+}
+
+bool am::CAmDbusWrapper::dbusDispatchCallback(const sh_pollHandle_t handle, void *userData)
+{
+ (void) handle;
+ (void) userData;
+ bool returnVal = true;
+ dbus_connection_ref(mpDbusConnection);
+ if (dbus_connection_dispatch(mpDbusConnection) == DBUS_DISPATCH_COMPLETE)
+ returnVal = false;
+ dbus_connection_unref(mpDbusConnection);
+ //logInfo("DBusWrapper::dbusDispatchCallback was called");
+ return (returnVal);
+}
+
+bool am::CAmDbusWrapper::dbusCheckCallback(const sh_pollHandle_t handle, void *userData)
+{
+ (void) handle;
+ (void) userData;
+ bool returnVal = false;
+ dbus_connection_ref(mpDbusConnection);
+ if (dbus_connection_get_dispatch_status(mpDbusConnection) == DBUS_DISPATCH_DATA_REMAINS)
+ returnVal = true;
+ dbus_connection_unref(mpDbusConnection);
+ //logInfo("DBusWrapper::dbusCheckCallback was called");
+ return (returnVal);
+}
+
+void am::CAmDbusWrapper::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;
+
+ DBusWatch *watch = (DBusWatch*) userData;
+
+ dbus_connection_ref(mpDbusConnection);
+ dbus_watch_handle(watch, flags);
+ dbus_connection_unref(mpDbusConnection);
+ //logInfo("DBusWrapper::dbusFireCallback was called");
+}
+
+void CAmDbusWrapper::dbusPrepareCallback(const sh_pollHandle_t handle, void* userData)
+{
+ (void) handle;
+ (void) userData;
+ dbus_connection_ref(mpDbusConnection);
+ while (dbus_connection_get_dispatch_status(mpDbusConnection) == DBUS_DISPATCH_DATA_REMAINS)
+ {
+ dbus_connection_dispatch(mpDbusConnection);
+ //logInfo("prepare was neccessary!");
+ }
+ dbus_connection_unref(mpDbusConnection);
+}
+
+void CAmDbusWrapper::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);
+
+ //stop or restart?
+ if (dbus_timeout_get_enabled(timeout))
+ {
+ //calculate the timeout in timeval
+ timespec pollTimeout;
+ int localTimeout = dbus_timeout_get_interval(timeout);
+ pollTimeout.tv_sec = localTimeout / 1000;
+ pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
+ mpSocketHandler->updateTimer(*handle, pollTimeout);
+ }
+ else
+ {
+ mpSocketHandler->stopTimer(*handle);
+ }
+}
+
+void CAmDbusWrapper::dbusTimerCallback(sh_timerHandle_t handle, void *userData)
+{
+ assert(userData!=NULL);
+ if (dbus_timeout_get_enabled((DBusTimeout*) userData))
+ {
+ mpSocketHandler->restartTimer(handle);
+ }
+ dbus_timeout_handle((DBusTimeout*) userData);
+}
+}
+
diff --git a/AudioManagerUtilities/src/CAmDltWrapper.cpp b/AudioManagerUtilities/src/CAmDltWrapper.cpp
new file mode 100644
index 0000000..b841eb8
--- /dev/null
+++ b/AudioManagerUtilities/src/CAmDltWrapper.cpp
@@ -0,0 +1,350 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * This file is part of GENIVI Project AudioManager.
+ *
+ * Contributions are licensed to the GENIVI Alliance under one or more
+ * Contribution License Agreements.
+ *
+ * \copyright
+ * This Source Code Form is subject to the terms of the
+ * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
+ * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ * Jens Lorenz, jlorenz@de.adit-jv.com ADIT 2014
+ *
+ * \file CAmDltWrapper.cpp
+ * For further information see http://www.genivi.org/.
+ *
+ */
+
+
+#include "CAmDltWrapper.h"
+#include <string>
+#include <sstream>
+#include <iostream>
+#include <string.h>
+
+namespace am
+{
+
+CAmDltWrapper* CAmDltWrapper::mpDLTWrapper = NULL;
+pthread_mutex_t CAmDltWrapper::mMutex = PTHREAD_MUTEX_INITIALIZER;
+
+CAmDltWrapper *CAmDltWrapper::instance(const bool enableNoDLTDebug)
+{
+ if (!mpDLTWrapper)
+ mpDLTWrapper = new CAmDltWrapper(enableNoDLTDebug);
+#ifndef WITH_DLT
+ if(enableNoDLTDebug)
+ mpDLTWrapper->enableNoDLTDebug(true);
+#endif
+ return (mpDLTWrapper);
+}
+
+void CAmDltWrapper::unregisterContext(DltContext & handle)
+{
+#ifdef WITH_DLT
+ dlt_unregister_context(&handle);
+#else
+ (void) handle;
+#endif
+}
+
+void CAmDltWrapper::deinit()
+{
+#ifdef WITH_DLT
+ unregisterContext(mDltContext);
+#endif
+}
+
+CAmDltWrapper::CAmDltWrapper(const bool enableNoDLTDebug) :
+#ifndef WITH_DLT
+ mEnableNoDLTDebug(enableNoDLTDebug),
+#endif
+ mDltContext(), //
+ mDltContextData()
+{
+ (void) enableNoDLTDebug;
+#ifndef WITH_DLT
+ std::cout << "\e[0;34m[DLT]\e[0;30m\tRunning without DLT-support" << std::endl;
+#endif
+}
+
+void CAmDltWrapper::registerApp(const char *appid, const char *description)
+{
+#ifdef WITH_DLT
+ dlt_register_app(appid, description);
+ //register a default context
+ dlt_register_context(&mDltContext, "def", "default Context registered by DLTWrapper CLass");
+#else
+ (void) appid;
+ (void) description;
+#endif
+}
+
+void CAmDltWrapper::registerContext(DltContext& handle, const char *contextid, const char *description)
+{
+#ifdef WITH_DLT
+ dlt_register_context(&handle, contextid, description);
+#else
+ strncpy(handle.contextID,contextid,4);
+
+ // store only the first contextID
+ if(0 == strlen(mDltContext.contextID))
+ {
+ memcpy(&mDltContext.contextID,contextid,4);
+ const size_t str_len = strlen(description);
+ if(str_len < 2000)
+ {
+ mDltContextData.context_description = new char[str_len + 1];
+ (void) strcpy(mDltContextData.context_description,description);
+ }
+ }
+
+ std::cout << "\e[0;34m[DLT]\e[0;30m\tRegistering Context " << contextid << " , " << description << std::endl;
+
+#endif
+}
+
+bool CAmDltWrapper::init(DltLogLevelType loglevel, DltContext* context)
+{
+ (void) loglevel;
+ pthread_mutex_lock(&mMutex);
+ if (!context)
+ context = &mDltContext;
+#ifdef WITH_DLT
+ if (dlt_user_log_write_start(context, &mDltContextData, loglevel) <= 0)
+ {
+ pthread_mutex_unlock(&mMutex);
+ return false;
+ }
+#else
+ if(mEnableNoDLTDebug)
+ std::cout << "\e[0;34m[" << context->contextID << "]\e[0;30m\t";
+#endif
+ return true;
+}
+
+void CAmDltWrapper::send()
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_finish(&mDltContextData);
+#else
+ if(mEnableNoDLTDebug)
+ std::cout << mDltContextData.buffer.str().c_str() << std::endl;
+
+ mDltContextData.buffer.str("");
+ mDltContextData.buffer.clear();
+#endif
+ pthread_mutex_unlock(&mMutex);
+}
+
+void CAmDltWrapper::append(const int8_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_int8(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const uint8_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_uint8(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const int16_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_int16(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const uint16_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_uint16(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const int32_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_int32(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const uint32_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_uint32(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const char*& value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, value);
+#else
+ mDltContextData.buffer << value;
+#endif
+}
+
+void CAmDltWrapper::append(const std::string& value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, value.c_str());
+#else
+ mDltContextData.buffer << value;
+#endif
+}
+
+void CAmDltWrapper::append(const bool value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_bool(&mDltContextData, static_cast<uint8_t>(value));
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const int64_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_int64(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const uint64_t value)
+{
+#ifdef WITH_DLT
+ dlt_user_log_write_uint64(&mDltContextData, value);
+#else
+ appendNoDLT(value);
+#endif
+}
+
+void CAmDltWrapper::append(const am_Error_e value)
+{
+ switch (value)
+ {
+ case am_Error_e::E_OK:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_OK");
+ #else
+ mDltContextData.buffer << "E_OK";
+ #endif
+ break;
+ case am_Error_e::E_ABORTED:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_ABORTED");
+ #else
+ mDltContextData.buffer << "E_ABORTED";
+ #endif
+ break;
+ case am_Error_e::E_ALREADY_EXISTS:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_ALREADY_EXISTS");
+ #else
+ mDltContextData.buffer << "E_ALREADY_EXISTS";
+ #endif
+ break;
+ case am_Error_e::E_DATABASE_ERROR:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_DATABASE_ERROR");
+ #else
+ mDltContextData.buffer << "E_DATABASE_ERROR";
+ #endif
+ break;
+ case am_Error_e::E_NON_EXISTENT:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_NON_EXISTENT");
+ #else
+ mDltContextData.buffer << "E_NON_EXISTENT";
+ #endif
+ break;
+ case am_Error_e::E_NOT_POSSIBLE:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_NOT_POSSIBLE");
+ #else
+ mDltContextData.buffer << "E_NOT_POSSIBLE";
+ #endif
+ break;
+ case am_Error_e::E_NOT_USED:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_NOT_USED");
+ #else
+ mDltContextData.buffer << "E_NOT_USED";
+ #endif
+ break;
+ case am_Error_e::E_NO_CHANGE:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_NO_CHANGE");
+ #else
+ mDltContextData.buffer << "E_NO_CHANGE";
+ #endif
+ break;
+ case am_Error_e::E_OUT_OF_RANGE:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_OUT_OF_RANGE");
+ #else
+ mDltContextData.buffer << "E_OUT_OF_RANGE";
+ #endif
+ break;
+ case am_Error_e::E_UNKNOWN:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_UNKNOWN");
+ #else
+ mDltContextData.buffer << "E_UNKNOWN";
+ #endif
+ break;
+ default:
+ #ifdef WITH_DLT
+ dlt_user_log_write_string(&mDltContextData, "E_UNKNOWN");
+ #else
+ mDltContextData.buffer << "E_UNKNOWN";
+ #endif
+
+ }
+}
+
+#ifndef WITH_DLT
+template<class T> void CAmDltWrapper::appendNoDLT(T value)
+{
+ mDltContextData.buffer << value;
+}
+
+void CAmDltWrapper::enableNoDLTDebug(const bool enableNoDLTDebug)
+{
+ mEnableNoDLTDebug = enableNoDLTDebug;
+}
+#endif
+
+CAmDltWrapper::~CAmDltWrapper()
+{
+ if (mpDLTWrapper)
+ {
+ mpDLTWrapper->unregisterContext(mDltContext);
+ delete mpDLTWrapper;
+ }
+}
+}
+
+
diff --git a/AudioManagerUtilities/src/CAmSocketHandler.cpp b/AudioManagerUtilities/src/CAmSocketHandler.cpp
new file mode 100644
index 0000000..2e8806a
--- /dev/null
+++ b/AudioManagerUtilities/src/CAmSocketHandler.cpp
@@ -0,0 +1,546 @@
+/**
+ * Copyright (C) 2012, BMW AG
+ *
+ * This file is part of GENIVI Project AudioManager.
+ *
+ * Contributions are licensed to the GENIVI Alliance under one or more
+ * Contribution License Agreements.
+ *
+ * \copyright
+ * This Source Code Form is subject to the terms of the
+ * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
+ * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ *
+ * \author Christian Linke, christian.linke@bmw.de BMW 2011,2012
+ *
+ * \file CAmSocketHandler.cpp
+ * For further information see http://www.genivi.org/.
+ *
+ */
+
+#include "CAmSocketHandler.h"
+//#include <config.h>
+#include <cassert>
+#include <sys/fcntl.h>
+#include <sys/errno.h>
+#include <sys/poll.h>
+#include <time.h>
+#include <algorithm>
+#include <features.h>
+#include <csignal>
+#include <unistd.h>
+#include "CAmDltWrapper.h"
+
+namespace am
+{
+
+CAmSocketHandler::CAmSocketHandler() :
+ receiverCallbackT(this, &CAmSocketHandler::receiverCallback),//
+ checkerCallbackT(this, &CAmSocketHandler::checkerCallback),//
+ mPipe(), //
+ mDispatchDone(1),//
+ mListPoll(), //
+ mListTimer(), //
+ mListActiveTimer(), //
+ mLastInsertedHandle(0), //
+ mLastInsertedPollHandle(0), //
+ mRecreatePollfds(true), //
+ mStartTime() //
+{
+ if (pipe(mPipe) == -1)
+ {
+ logError("CAmSerializer could not create pipe!");
+ }
+
+ //add the pipe to the poll - nothing needs to be proccessed here we just need the pipe to trigger the ppoll
+ short event = 0;
+ sh_pollHandle_t handle;
+ event |= POLLIN;
+ addFDPoll(mPipe[0], event, NULL, &receiverCallbackT, &checkerCallbackT, NULL, NULL, handle);
+}
+
+CAmSocketHandler::~CAmSocketHandler()
+{
+}
+
+//todo: maybe have some: give me more time returned?
+/**
+ * start the block listening for filedescriptors. This is the mainloop.
+ */
+void CAmSocketHandler::start_listenting()
+{
+ mDispatchDone = 0;
+ int16_t pollStatus;
+
+ //prepare the signalmask
+ sigset_t sigmask;
+ sigemptyset(&sigmask);
+ sigaddset(&sigmask, SIGINT);
+ sigaddset(&sigmask, SIGQUIT);
+ sigaddset(&sigmask, SIGTERM);
+ sigaddset(&sigmask, SIGHUP);
+ sigaddset(&sigmask, SIGQUIT);
+
+ clock_gettime(CLOCK_MONOTONIC, &mStartTime);
+ while (!mDispatchDone)
+ {
+ //first we go through the registered filedescriptors and check if someone needs preparation:
+ std::for_each(mListPoll.begin(), mListPoll.end(), CAmShCallPrep());
+
+ if (mRecreatePollfds)
+ {
+ mfdPollingArray.clear();
+ //there was a change in the setup, so we need to recreate the fdarray from the list
+ std::for_each(mListPoll.begin(), mListPoll.end(), CAmShCopyPollfd(mfdPollingArray));
+ mRecreatePollfds = false;
+ }
+
+ timerCorrection();
+
+ //block until something is on a filedescriptor
+
+ timespec buffertime;
+ if ((pollStatus = ppoll(&mfdPollingArray[0], mfdPollingArray.size(), insertTime(buffertime), &sigmask)) < 0)
+ {
+ if (errno == EINTR)
+ {
+ //a signal was received, that means it's time to go...
+ pollStatus = 0;
+ }
+ else
+ {
+ logError("SocketHandler::start_listenting ppoll returned with error", errno);
+ exit(0);
+ }
+ }
+
+ if (pollStatus != 0) //only check filedescriptors if there was a change
+ {
+ //todo: here could be a timer that makes sure naughty plugins return!
+
+ //freeze mListPoll by copying it - otherwise we get problems when we want to manipulate it during the next lines
+ std::list<sh_poll_s> listPoll;
+ mListPoll_t::iterator listmPollIt;
+
+ //remove all filedescriptors who did not fire
+ std::vector<pollfd>::iterator it = mfdPollingArray.begin();
+ do
+ {
+ it = std::find_if(it, mfdPollingArray.end(), eventFired);
+ if (it != mfdPollingArray.end())
+ {
+ listmPollIt = mListPoll.begin();
+ std::advance(listmPollIt, std::distance(mfdPollingArray.begin(), it));
+ listPoll.push_back(*listmPollIt);
+ listPoll.back().pollfdValue = *it;
+ it++;
+ }
+ } while (it != mfdPollingArray.end());
+
+ //stage 1, call firedCB
+ std::for_each(listPoll.begin(), listPoll.end(), CAmShCallFire());
+
+ //stage 2, lets ask around if some dispatching is necessary, the ones who need stay on the list
+ listPoll.remove_if(noDispatching);
+
+ //stage 3, the ones left need to dispatch, we do this as long as there is something to dispatch..
+ do
+ {
+ listPoll.remove_if(dispatchingFinished);
+ } while (!listPoll.empty());
+
+ }
+ else //Timerevent
+ {
+ //this was a timer event, we need to take care about the timers
+ timerUp();
+ }
+ }
+}
+
+/**
+ * exits the loop
+ */
+void CAmSocketHandler::stop_listening()
+{
+ mDispatchDone = 1;
+
+ //this is for all running timers only - we need to handle the additional offset here
+ if (!mListActiveTimer.empty())
+ {
+ timespec currentTime, correctionTime;
+ clock_gettime(CLOCK_MONOTONIC, &currentTime);
+ correctionTime = timespecSub(currentTime, mStartTime);
+ std::for_each(mListActiveTimer.begin(), mListActiveTimer.end(), CAmShSubstractTime(correctionTime));
+ }
+
+}
+
+/**
+ * Adds a filedescriptor to the polling loop
+ * @param fd the filedescriptor
+ * @param event the event flags
+ * @param prepare a callback that is called before the loop is entered
+ * @param fired a callback that is called when the filedescriptor needs to be read
+ * @param check a callback that is called to check if further actions are neccessary
+ * @param dispatch a callback that is called to dispatch the received data
+ * @param userData a pointer to userdata that is always passed around
+ * @param handle the handle of this poll
+ * @return E_OK if the descriptor was added, E_NON_EXISTENT if the fd is not valid
+ */
+am_Error_e CAmSocketHandler::addFDPoll(const int fd, const short event, IAmShPollPrepare *prepare, IAmShPollFired *fired, IAmShPollCheck *check, IAmShPollDispatch *dispatch, void *userData, sh_pollHandle_t & handle)
+{
+ if (!fdIsValid(fd))
+ return (E_NON_EXISTENT);
+
+ sh_poll_s pollData;
+ pollData.pollfdValue.fd = fd;
+ pollData.handle = ++mLastInsertedPollHandle;
+ pollData.pollfdValue.events = event;
+ pollData.pollfdValue.revents = 0;
+ pollData.userData = userData;
+ pollData.prepareCB = prepare;
+ pollData.firedCB = fired;
+ pollData.checkCB = check;
+ pollData.dispatchCB = dispatch;
+
+ //add new data to the list
+ mListPoll.push_back(pollData);
+
+ mRecreatePollfds = true;
+
+ handle = pollData.handle;
+ return (E_OK);
+}
+
+/**
+ * removes a filedescriptor from the poll loop
+ * @param handle
+ * @return
+ */
+am_Error_e CAmSocketHandler::removeFDPoll(const sh_pollHandle_t handle)
+{
+ mListPoll_t::iterator iterator = mListPoll.begin();
+
+ for (; iterator != mListPoll.end(); ++iterator)
+ {
+ if (iterator->handle == handle)
+ {
+ iterator = mListPoll.erase(iterator);
+ mRecreatePollfds = true;
+ return (E_OK);
+ }
+ }
+ return (E_UNKNOWN);
+}
+
+/**
+ * adds a timer to the list of timers. The callback will be fired when the timer is up.
+ * This is not a high precise timer, it is very coarse. It is meant to be used for timeouts when waiting
+ * for an answer via a filedescriptor.
+ * One time timer. If you need again a timer, you need to add a new timer in the callback of the old one.
+ * @param timeouts timeouts time until the callback is fired
+ * @param callback callback the callback
+ * @param handle handle the handle that is created for the timer is returned. Can be used to remove the timer
+ * @param userData pointer always passed with the call
+ * @return E_OK in case of success
+ */
+am_Error_e CAmSocketHandler::addTimer(const timespec timeouts, IAmShTimerCallBack* callback, sh_timerHandle_t& handle, void * userData)
+{
+ assert(!((timeouts.tv_sec==0) && (timeouts.tv_nsec==0)));
+ assert(callback!=NULL);
+
+ sh_timer_s timerItem;
+
+ //create a new handle for the timer
+ handle = ++mLastInsertedHandle; //todo: overflow ruling !o
+ timerItem.handle = handle;
+ timerItem.countdown = timeouts;
+ timerItem.callback = callback;
+ timerItem.userData = userData;
+
+ mListTimer.push_back(timerItem);
+
+ //we add here the time difference between startTime and currenttime, because this time will be substracted later on in timecorrection
+ timespec currentTime;
+ clock_gettime(CLOCK_MONOTONIC, &currentTime);
+ if (!mDispatchDone) //the mainloop is started
+ timerItem.countdown = timespecAdd(timeouts, timespecSub(currentTime, mStartTime));
+
+ mListActiveTimer.push_back(timerItem);
+ mListActiveTimer.sort(compareCountdown);
+ return (E_OK);
+}
+
+/**
+ * removes a timer from the list of timers
+ * @param handle the handle to the timer
+ * @return E_OK in case of success, E_UNKNOWN if timer was not found.
+ */
+am_Error_e CAmSocketHandler::removeTimer(const sh_timerHandle_t handle)
+{
+ assert(handle!=0);
+
+ //stop the current timer
+ stopTimer(handle);
+
+ std::list<sh_timer_s>::iterator it(mListTimer.begin());
+ for (; it != mListTimer.end(); ++it)
+ {
+ if (it->handle == handle)
+ {
+ it = mListTimer.erase(it);
+ return (E_OK);
+ }
+ }
+ return (E_UNKNOWN);
+}
+
+/**
+ * restarts a timer and updates with a new interva
+ * @param handle handle to the timer
+ * @param timeouts new timout time
+ * @return E_OK on success, E_NON_EXISTENT if the handle was not found
+ */
+am_Error_e CAmSocketHandler::updateTimer(const sh_timerHandle_t handle, const timespec timeouts)
+{
+ //update the mList ....
+ sh_timer_s timerItem;
+ std::list<sh_timer_s>::iterator it(mListTimer.begin()), activeIt(mListActiveTimer.begin());
+ bool found(false);
+ for (; it != mListTimer.end(); ++it)
+ {
+ if (it->handle == handle)
+ {
+ it->countdown = timeouts;
+ timerItem = *it;
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ return (E_NON_EXISTENT);
+
+ found = false;
+
+ //we add here the time difference between startTime and currenttime, because this time will be substracted later on in timecorrection
+ timespec currentTime, timeoutsCorrected;
+ currentTime.tv_nsec=timeoutsCorrected.tv_nsec=0;
+ currentTime.tv_sec=timeoutsCorrected.tv_sec=0;
+ clock_gettime(CLOCK_MONOTONIC, &currentTime);
+ if (!mDispatchDone) //the mainloop is started
+ timeoutsCorrected = timespecAdd(timeouts, timespecSub(currentTime, mStartTime));
+
+ for (; activeIt != mListActiveTimer.end(); ++activeIt)
+ {
+ if (activeIt->handle == handle)
+ {
+ activeIt->countdown = timeoutsCorrected;
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ timerItem.countdown = timeoutsCorrected;
+ mListActiveTimer.push_back(timerItem);
+
+ mListActiveTimer.sort(compareCountdown);
+ return (E_OK);
+}
+
+/**
+ * restarts a timer with the original value
+ * @param handle
+ * @return E_OK on success, E_NON_EXISTENT if the handle was not found
+ */
+am_Error_e CAmSocketHandler::restartTimer(const sh_timerHandle_t handle)
+{
+ sh_timer_s timerItem; //!<the original timer value
+ //find the original value
+ std::list<sh_timer_s>::iterator it(mListTimer.begin()), activeIt(mListActiveTimer.begin());
+ bool found(false);
+ for (; it != mListTimer.end(); ++it)
+ {
+ if (it->handle == handle)
+ {
+ timerItem = *it;
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ return (E_NON_EXISTENT);
+
+ found = false;
+
+ //we add here the time difference between startTime and currenttime, because this time will be substracted later on in timecorrection
+ timespec currentTime, timeoutsCorrected;
+ clock_gettime(CLOCK_MONOTONIC, &currentTime);
+ if (!mDispatchDone) //the mainloop is started
+ {
+ timeoutsCorrected = timespecAdd(timerItem.countdown, timespecSub(currentTime, mStartTime));
+ timerItem.countdown = timeoutsCorrected;
+ }
+
+ for (; activeIt != mListActiveTimer.end(); ++activeIt)
+ {
+ if (activeIt->handle == handle)
+ {
+ activeIt->countdown = timerItem.countdown;
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ mListActiveTimer.push_back(timerItem);
+
+ mListActiveTimer.sort(compareCountdown);
+
+ return (E_OK);
+}
+
+/**
+ * stops a timer
+ * @param handle
+ * @return E_OK on success, E_NON_EXISTENT if the handle was not found
+ */
+am_Error_e CAmSocketHandler::stopTimer(const sh_timerHandle_t handle)
+{
+ //go through the list and remove the timer with the handle
+ std::list<sh_timer_s>::iterator it(mListActiveTimer.begin());
+ for (; it != mListActiveTimer.end(); ++it)
+ {
+ if (it->handle == handle)
+ {
+ it = mListActiveTimer.erase(it);
+ return (E_OK);
+ }
+ }
+ return (E_NON_EXISTENT);
+}
+
+/**
+ * updates the eventFlags of a poll
+ * @param handle
+ * @param events
+ * @return @return E_OK on succsess, E_NON_EXISTENT if fd was not found
+ */
+am_Error_e CAmSocketHandler::updateEventFlags(const sh_pollHandle_t handle, const short events)
+{
+ mListPoll_t::iterator iterator = mListPoll.begin();
+
+ for (; iterator != mListPoll.end(); ++iterator)
+ {
+ if (iterator->handle == handle)
+ {
+ iterator->pollfdValue.events = events;
+ mRecreatePollfds = true;
+ return (E_OK);
+ }
+ }
+ return (E_UNKNOWN);
+}
+
+/**
+ * checks if a filedescriptor is validCAmShSubstractTime
+ * @param fd the filedescriptor
+ * @return true if the fd is valid
+ */
+bool CAmSocketHandler::fdIsValid(const int fd) const
+{
+ return (fcntl(fd, F_GETFL) != -1 || errno != EBADF);
+}
+
+/**
+ * timer is up.
+ */
+void CAmSocketHandler::timerUp()
+{
+ //find out the timedifference to starttime
+ timespec currentTime, diffTime;
+ clock_gettime(CLOCK_MONOTONIC, &currentTime);
+ diffTime = timespecSub(currentTime, mStartTime);
+
+ //now we need to substract the diffTime from all timers and see if they are up
+ std::list<sh_timer_s>::reverse_iterator overflowIter = std::find_if(mListActiveTimer.rbegin(), mListActiveTimer.rend(), CAmShCountdownUp(diffTime));
+
+ //copy all fired timers into a list
+ std::vector<sh_timer_s> tempList(overflowIter, mListActiveTimer.rend());
+
+ //erase all fired timers
+ std::list<sh_timer_s>::iterator it(overflowIter.base());
+ mListActiveTimer.erase(mListActiveTimer.begin(), it);
+
+ //call the callbacks for the timers
+ std::for_each(tempList.begin(), tempList.end(), CAmShCallTimer());
+}
+
+/**
+ * correct timers and fire the ones who are up
+ */
+void CAmSocketHandler::timerCorrection()
+{
+ //get the current time and calculate the correction value
+ timespec currentTime, correctionTime;
+ clock_gettime(CLOCK_MONOTONIC, &currentTime);
+ correctionTime = timespecSub(currentTime, mStartTime);
+ mStartTime = currentTime;
+
+ if (!mListActiveTimer.empty())
+ {
+
+ //subtract the correction value from all items in the list
+ std::for_each(mListActiveTimer.begin(), mListActiveTimer.end(), CAmShSubstractTime(correctionTime));
+
+ //find the last occurrence of zero -> timer overflowed
+ std::list<sh_timer_s>::reverse_iterator overflowIter = std::find_if(mListActiveTimer.rbegin(), mListActiveTimer.rend(), CAmShCountdownZero());
+
+ //only if a timer overflowed
+ if (overflowIter != mListActiveTimer.rend())
+ {
+ //copy all timers that need to be called to a new list
+ std::vector<sh_timer_s> tempList(overflowIter, mListActiveTimer.rend());
+
+ //erase all fired timers
+ std::list<sh_timer_s>::iterator it(overflowIter.base());
+ mListActiveTimer.erase(mListActiveTimer.begin(), it);
+
+ //call the callbacks for the timers
+ std::for_each(tempList.begin(), tempList.end(), CAmShCallTimer());
+ }
+ }
+}
+
+void CAmSocketHandler::exit_mainloop()
+{
+ //end the while loop
+ stop_listening();
+
+ //fire the ending filedescriptor
+ int p(1);
+ write(mPipe[1], &p, sizeof(p));
+}
+
+/**
+ * is used to set the pointer for the ppoll command
+ * @param buffertime
+ * @return
+ */
+inline timespec* CAmSocketHandler::insertTime(timespec& buffertime)
+{
+ if (!mListActiveTimer.empty())
+ {
+ buffertime = mListActiveTimer.front().countdown;
+ return (&buffertime);
+ }
+ else
+ {
+ return (NULL);
+ }
+}
+
+}
+