summaryrefslogtreecommitdiff
path: root/include/shared
diff options
context:
space:
mode:
authorchristian mueller <christian.ei.mueller@bmw.de>2012-03-01 19:12:55 +0100
committerchristian mueller <christian.ei.mueller@bmw.de>2012-03-01 19:12:55 +0100
commitdfcafbec34e0e454d9995551998af5d40a27c7ad (patch)
treed93d70fea7feb7004f6a2ce9bfce9b83d3469e42 /include/shared
parentffccf1e2003cea4230413ccfdfd4cd6515484e4f (diff)
downloadaudiomanager-dfcafbec34e0e454d9995551998af5d40a27c7ad.tar.gz
* fix [GAM-28] problems in routingsender.cpp
* [GAM-27] major refactoring: bringing everything to standardized names * Enhancement of CAmSerializer.h * improvement of documentation * adoption & regeneration of inerfaces
Diffstat (limited to 'include/shared')
-rw-r--r--include/shared/CAmDbusWrapper.h92
-rw-r--r--include/shared/CAmDltWrapper.h820
-rw-r--r--include/shared/CAmSerializer.h708
-rw-r--r--include/shared/CAmSocketHandler.h258
4 files changed, 1878 insertions, 0 deletions
diff --git a/include/shared/CAmDbusWrapper.h b/include/shared/CAmDbusWrapper.h
new file mode 100644
index 0000000..26131c2
--- /dev/null
+++ b/include/shared/CAmDbusWrapper.h
@@ -0,0 +1,92 @@
+/** Copyright (c) 2012 GENIVI Alliance
+ * Copyright (c) 2012 BMW
+ *
+ * \author Christian Mueller, BMW
+ *
+ * \section license
+ * 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.
+ *
+ */
+
+#ifndef DBUSWRAPPER_H_
+#define DBUSWRAPPER_H_
+
+#include <config.h>
+#include <dbus/dbus.h>
+#include <string>
+#include <list>
+#include "shared/CAmSocketHandler.h"
+
+namespace am
+{
+/**
+ * This wraps dbus and provides everything needed to anyone who wants to use dbus (including plugins)
+ */
+class CAmDbusWrapper
+{
+public:
+ CAmDbusWrapper(CAmSocketHandler* socketHandler);
+ virtual ~CAmDbusWrapper();
+
+ /**
+ * 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 registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata);
+
+ /**
+ * returns the dbus connection
+ * @param connection pointer to the connection
+ */
+ 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);
+
+ 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;
+ 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;
+ DBusConnection* mpDbusConnection;
+ DBusError mDBusError;
+ std::list<std::string> mListNodes;
+ std::vector<sh_timerHandle_t*> mpListTimerhandles;
+ CAmSocketHandler *mpSocketHandler;
+ std::map<DBusWatch*, sh_pollHandle_t> mMapHandleWatch;
+};
+
+}
+
+#endif /* DBUSWRAPPER_H_ */
diff --git a/include/shared/CAmDltWrapper.h b/include/shared/CAmDltWrapper.h
new file mode 100644
index 0000000..ea6f246
--- /dev/null
+++ b/include/shared/CAmDltWrapper.h
@@ -0,0 +1,820 @@
+/** Copyright (c) 2012 GENIVI Alliance
+ * Copyright (c) 2012 BMW
+ *
+ * \author Christian Mueller, BMW
+ *
+ * \section license
+ * 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.
+ *
+ */
+
+#ifndef DLTWRAPPER_H_
+#define DLTWRAPPER_H_
+
+#include "config.h"
+
+#ifdef WITH_DLT
+#include <dlt/dlt.h>
+#else
+
+#include <stdint.h>
+
+namespace am {
+
+#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 */
+ char buffer[DLT_USER_BUF_MAX_SIZE]; /**< buffer for building log message*/
+ int32_t size; /**< payload size */
+ 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;
+
+}
+
+#endif
+
+#include <string>
+
+namespace am
+{
+
+/**
+ * Wraps around the dlt wrapper. 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);
+
+ void init(DltLogLevelType loglevel, DltContext* context = NULL);
+ 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 char*& value);
+ void append(const std::string& value);
+ void append(const bool 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
+
+};
+
+/**
+ * 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());
+ inst->init(DLT_LOG_INFO);
+ inst->append(value);
+ inst->send();
+}
+
+/**
+ * logs a given value with infolevel with the default context
+ * @param value
+ * @param value1
+ */
+template<typename T, typename T1> void logInfo(T value, T1 value1)
+{
+ CAmDltWrapper* inst(getWrapper());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_INFO);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(DLT_LOG_ERROR);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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());
+ inst->init(loglevel, context);
+ 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/include/shared/CAmSerializer.h b/include/shared/CAmSerializer.h
new file mode 100644
index 0000000..a27edaf
--- /dev/null
+++ b/include/shared/CAmSerializer.h
@@ -0,0 +1,708 @@
+/** Copyright (c) 2012 GENIVI Alliance
+ * Copyright (c) 2012 BMW
+ *
+ * \author Christian Mueller, BMW
+ *
+ * \section license
+ * 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.
+ *
+ */
+
+#ifndef CAMSERIALIZER_H_
+#define CAMSERIALIZER_H_
+
+#include <pthread.h>
+#include <deque>
+#include <cassert>
+#include <memory>
+#include <stdexcept>
+#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
+ * 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.
+ */
+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 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 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 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 Targ> class CAmSyncOneArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(Targ argument);
+ Targ mArgument;
+ TretVal mRetval;
+
+ public:
+ friend class CAmSerializer;
+ CAmSyncOneArgDelegate(TClass* instance, TretVal(TClass::*function)(Targ 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 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 three arguments
+ */
+ template<class TClass, typename TretVal, typename Targ, typename Targ1, typename Targ2> class CAmSyncThreeArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(Targ argument, Targ1 argument1, Targ2 argument2);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ TretVal mRetval;
+
+ public:
+ CAmSyncThreeArgDelegate(TClass* instance, TretVal(TClass::*function)(Targ argument, Targ1 argument1, Targ2 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 calls with four arguments
+ */
+ template<class TClass, typename TretVal, typename Targ, typename Targ1, typename Targ2, typename Targ3> class CAmSyncFourArgDelegate: public CAmDelegate
+ {
+ private:
+ TClass* mInstance;
+ TretVal (TClass::*mFunction)(Targ argument, Targ1 argument1, Targ2 argument2, Targ3 argument3);
+ Targ mArgument;
+ Targ1 mArgument1;
+ Targ2 mArgument2;
+ Targ3 mArgument3;
+ TretVal mRetval;
+
+ CAmSyncFourArgDelegate(TClass* instance, TretVal(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), //
+ 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;
+ }
+ };
+
+ /**
+ * 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.
+ * Here is an 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.
+ * Here is an 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 two arguments asynchronously threadsafe. for more see asyncCall with one argument
+ * @param instance
+ * @param function
+ * @param argument
+ * @param argument1
+ */
+ 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 three arguments asynchronously threadsafe. for more see asyncCall with one argument
+ * @param instance
+ * @param function
+ * @param argument
+ * @param argument1 template<class TClass1, class TretVal, class Targ, class Targ1>
+ void syncCall(TClass1* instance, TretVal(TClass1::*function)(Targ,Targ1), TretVal& retVal, Targ& argument, Targ1& argument1)
+ {
+ CAmSyncTwoArgDelegate<TClass1, TretVal, Targ, Targ1>* p(new CAmSyncTwoArgDelegate<TClass1, TretVal, Targ, Targ1>(instance, function, argument, argument1));
+ 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);
+ }
+ * @param argument2
+ */
+ 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 four arguments asynchronously threadsafe. for more see asyncCall with one argument
+ * @param instance
+ * @param function
+ * @param argument
+ * @param argument1
+ * @param argument2
+ * @param argument3
+ */
+ 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.
+ * Here is an 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.
+ * Here is an 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 Targ>
+ void syncCall(TClass1* instance, TretVal(TClass1::*function)(Targ), TretVal& retVal,Targ& argument)
+ {
+ CAmSyncOneArgDelegate<TClass1, TretVal, Targ>* p(new CAmSyncOneArgDelegate<TClass1, TretVal, 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
+ * @param instance
+ * @param function
+ * @param retVal
+ * @param argument
+ * @param argument1
+ */
+ 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 three arguments synchronously threadsafe. for more see syncCall with one argument
+ * @param instance
+ * @param function
+ * @param retVal
+ * @param argument
+ * @param argument1
+ * @param argument2
+ */
+ template<class TClass1, class TretVal, class Targ, class Targ1, class Targ2>
+ void syncCall(TClass1* instance, TretVal(TClass1::*function)(Targ,Targ1,Targ2), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2)
+ {
+ CAmSyncThreeArgDelegate<TClass1, TretVal, Targ, Targ1, Targ2>* p(new CAmSyncThreeArgDelegate<TClass1, TretVal, 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 works...
+ retVal=p->returnResults(argument, argument1, argument2);
+ delete p;
+ }
+
+ /**
+ * calls a function with four arguments synchronously threadsafe. for more see syncCall with one argument
+ * @param instance
+ * @param function
+ * @param retVal
+ * @param argument
+ * @param argument1
+ * @param argument2
+ * @param argument3
+ */
+ template<class TClass1, class TretVal, class Targ, class Targ1, class Targ2, class Targ3>
+ void syncCall(TClass1* instance, TretVal(TClass1::*function)(Targ,Targ1,Targ2,Targ3), TretVal& retVal, Targ& argument, Targ1& argument1, Targ2& argument2, Targ3& argument3)
+ {
+ CAmSyncFourArgDelegate<TClass1, TretVal, Targ, Targ1, Targ2, Targ3>* p(new CAmSyncFourArgDelegate<TClass1, TretVal, 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;
+ }
+
+
+ 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)));
+ }
+
+ bool checkerCallback(const sh_pollHandle_t handle, void* userData)
+ {
+ (void) handle;
+ (void) userData;
+ if (mListDelegatePoiters.empty())
+ return false;
+ return true;
+ }
+
+ 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 sockethandler
+ */
+ 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, NULL, &dispatcherCallbackT, NULL, handle);
+ }
+
+ ~CAmSerializer(){}
+};
+} /* namespace am */
+#endif /* CAMSERIALIZER_H_ */
diff --git a/include/shared/CAmSocketHandler.h b/include/shared/CAmSocketHandler.h
new file mode 100644
index 0000000..e0ea797
--- /dev/null
+++ b/include/shared/CAmSocketHandler.h
@@ -0,0 +1,258 @@
+/** Copyright (c) 2012 GENIVI Alliance
+ * Copyright (c) 2012 BMW
+ *
+ * \author Christian Mueller, BMW
+ *
+ * \section license
+ * 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.
+ *
+ */
+
+#ifndef SOCKETHANDLER_H_
+#define SOCKETHANDLER_H_
+
+#include "audiomanagertypes.h"
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <stdint.h>
+#include <sys/poll.h>
+#include <list>
+#include <map>
+#include <signal.h>
+
+namespace am
+{
+
+static volatile sig_atomic_t gDispatchDone = 0; //this global is used to stop the mainloop
+
+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
+
+class CAmShPollPrepare;
+class CAmShPollCheck;
+class CAmShPollFired;
+class CAmShPollDispatch;
+class CAmShTimerCallBack;
+
+class CAmSocketHandler
+{
+public:
+ CAmSocketHandler();
+ virtual ~CAmSocketHandler();
+
+ am_Error_e addFDPoll(const int fd, const short event, CAmShPollPrepare *prepare, CAmShPollFired *fired, CAmShPollCheck *check, CAmShPollDispatch *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, CAmShTimerCallBack*& 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, const timespec timeouts);
+ am_Error_e stopTimer(const sh_timerHandle_t handle);
+ void start_listenting();
+ void stop_listening();
+private:
+ 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
+ timespec timeout; //!<the original timer value
+ CAmShTimerCallBack* callback; //!<the callbackfunction
+ void * userData; //!<saves a void pointer together with the rest.
+ };
+
+ class CAmShSubstractTime //!<functor to easy substract from each countdown value
+ {
+ private:
+ timespec param;
+ public:
+ CAmShSubstractTime(timespec param) :
+ param(param){}
+ void operator()(sh_timer_s& t) const;
+ };
+
+ struct sh_poll_s //!<struct that holds information about polls
+ {
+ sh_pollHandle_t handle; //!<handle to uniquely adress a filedesriptor
+ CAmShPollPrepare *prepareCB;
+ CAmShPollFired *firedCB;
+ CAmShPollCheck *checkCB;
+ CAmShPollDispatch *dispatchCB;
+ 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
+
+ class CAmShCopyPollfd
+ {
+ private:
+ mListPollfd_t& mArray;
+ public:
+ CAmShCopyPollfd(mListPollfd_t& dest) :
+ mArray(dest){}
+ void operator()(const sh_poll_s& row);
+ };
+
+ bool fdIsValid(const int fd) const;
+ void initTimer();
+ void timerUp();
+ int timespec2ms(const timespec& time);
+ timespec* insertTime(timespec& buffertime);
+ 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);
+ }
+
+ static bool onlyFiredEvents(const pollfd& a)
+ {
+ return a.revents == 0 ? false : true;
+ }
+
+ //todo: maybe we could simplify mListActiveTimer to hold only the handle and the countdown ....
+ mListPollfd_t mfdPollingArray;
+ mListPoll_t mListPoll;
+ 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 mNextTimer;
+ sh_timerHandle_t mLastInsertedHandle;
+ sh_pollHandle_t mLastInsertedPollHandle;
+ bool mRecreatePollfds;
+ timespec mTimeout;
+};
+
+/**
+ * classic functor for the BasicTimerCallback
+ */
+class CAmShTimerCallBack
+{
+public:
+ virtual void Call(const sh_timerHandle_t handle, void* userData)=0;
+ virtual ~CAmShTimerCallBack(){};
+};
+
+class CAmShPollPrepare
+{
+public:
+ virtual void Call(const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~CAmShPollPrepare(){};
+};
+
+class CAmShPollFired
+{
+public:
+ virtual void Call(const pollfd pollfd, const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~ CAmShPollFired(){};
+};
+
+class CAmShPollCheck
+{
+public:
+ virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~ CAmShPollCheck(){};
+};
+
+class CAmShPollDispatch
+{
+public:
+ virtual bool Call(const sh_pollHandle_t handle, void* userData)=0;
+ virtual ~ CAmShPollDispatch() {};
+};
+
+/**
+ * template to create the functor for a class
+ */
+template<class TClass> class TAmShTimerCallBack: public CAmShTimerCallBack
+{
+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 to create the functor for a class
+ */
+template<class TClass> class TAmShPollPrepare: public CAmShPollPrepare
+{
+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);
+ };
+};
+
+template<class TClass> class TAmShPollFired: public CAmShPollFired
+{
+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 CAmShPollCheck
+{
+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<class TClass> class TAmShPollDispatch: public CAmShPollDispatch
+{
+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_ */