From b6bf9d71477b3695b1a3dfe7253137c5936c7ebc Mon Sep 17 00:00:00 2001
From: "Robin.Mueller" <robin.mueller.m@gmail.com>
Date: Mon, 23 Mar 2020 19:09:42 +0100
Subject: [PATCH] to avoid dynamic casting, introuced CookieIF

---
 devicehandlers/AcceptsDeviceResponsesIF.h |  5 ++--
 devicehandlers/ChildHandlerBase.cpp       |  6 ++---
 devicehandlers/ChildHandlerBase.h         |  5 ++--
 devicehandlers/Cookie.cpp                 |  8 ++++---
 devicehandlers/Cookie.h                   | 28 +++++++++++++----------
 devicehandlers/CookieIF.h                 | 25 ++++++++++++++++++++
 devicehandlers/DeviceCommunicationIF.h    |  8 +++----
 devicehandlers/DeviceHandlerBase.cpp      | 26 ++++++++++-----------
 devicehandlers/DeviceHandlerBase.h        | 27 +++++-----------------
 rmap/RMAPCookie.cpp                       |  8 +++----
 rmap/RMAPCookie.h                         |  4 ++--
 11 files changed, 82 insertions(+), 68 deletions(-)
 create mode 100644 devicehandlers/CookieIF.h

diff --git a/devicehandlers/AcceptsDeviceResponsesIF.h b/devicehandlers/AcceptsDeviceResponsesIF.h
index 0eedae88..c811158f 100644
--- a/devicehandlers/AcceptsDeviceResponsesIF.h
+++ b/devicehandlers/AcceptsDeviceResponsesIF.h
@@ -15,9 +15,8 @@ public:
 	/**
 	 * Default empty virtual destructor.
 	 */
-	virtual ~AcceptsDeviceResponsesIF() {
-}
-virtual MessageQueueId_t getDeviceQueue() = 0;
+	virtual ~AcceptsDeviceResponsesIF() {}
+	virtual MessageQueueId_t getDeviceQueue() = 0;
 };
 
 #endif /* ACCEPTSDEVICERESPONSESIF_H_ */
diff --git a/devicehandlers/ChildHandlerBase.cpp b/devicehandlers/ChildHandlerBase.cpp
index a0630fc6..ac0b2438 100644
--- a/devicehandlers/ChildHandlerBase.cpp
+++ b/devicehandlers/ChildHandlerBase.cpp
@@ -2,12 +2,12 @@
 #include <framework/devicehandlers/ChildHandlerBase.h>
 #include <framework/subsystem/SubsystemBase.h>
 
-ChildHandlerBase::ChildHandlerBase(object_id_t setObjectId, address_t logicalAddress,
-		object_id_t deviceCommunication, Cookie * cookie,
+ChildHandlerBase::ChildHandlerBase(object_id_t setObjectId,
+		object_id_t deviceCommunication, CookieIF * cookie,
 		uint32_t maxDeviceReplyLen, uint8_t setDeviceSwitch,
 		uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId,
 		uint32_t parent, FailureIsolationBase* customFdir, size_t cmdQueueSize) :
-		DeviceHandlerBase(setObjectId, logicalAddress, deviceCommunication, cookie,
+		DeviceHandlerBase(setObjectId, deviceCommunication, cookie,
 			maxDeviceReplyLen, setDeviceSwitch,  thermalStatePoolId,
 			thermalRequestPoolId, (customFdir == NULL? &childHandlerFdir : customFdir),
 			cmdQueueSize),
diff --git a/devicehandlers/ChildHandlerBase.h b/devicehandlers/ChildHandlerBase.h
index 4e9eee49..493b22ee 100644
--- a/devicehandlers/ChildHandlerBase.h
+++ b/devicehandlers/ChildHandlerBase.h
@@ -6,9 +6,8 @@
 
 class ChildHandlerBase: public DeviceHandlerBase {
 public:
-	ChildHandlerBase(object_id_t setObjectId, uint32_t logicalAddress,
-			object_id_t deviceCommunication, Cookie * cookie,
-			uint32_t maxDeviceReplyLen, uint8_t setDeviceSwitch,
+	ChildHandlerBase(object_id_t setObjectId, object_id_t deviceCommunication,
+			CookieIF * cookie, uint32_t maxDeviceReplyLen, uint8_t setDeviceSwitch,
 			uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId,
 			uint32_t parent, FailureIsolationBase* customFdir = nullptr,
 			size_t cmdQueueSize = 20);
diff --git a/devicehandlers/Cookie.cpp b/devicehandlers/Cookie.cpp
index f0748b5d..05b9425c 100644
--- a/devicehandlers/Cookie.cpp
+++ b/devicehandlers/Cookie.cpp
@@ -5,12 +5,12 @@
  */
 #include <framework/devicehandlers/Cookie.h>
 
-//Cookie::Cookie(address_t logicalAddress_, size_t maxReplyLen_):
-//	logicalAddress(logicalAddress_), maxReplyLen(maxReplyLen_){}
-
 Cookie::Cookie(address_t logicalAddress_): logicalAddress(logicalAddress_) {
 }
 
+void Cookie::setAddress(address_t logicalAddress_) {
+	logicalAddress = logicalAddress_;
+}
 void Cookie::setMaxReplyLen(size_t maxReplyLen_) {
 	maxReplyLen = maxReplyLen_;
 }
@@ -22,3 +22,5 @@ address_t Cookie::getAddress() const {
 size_t Cookie::getMaxReplyLen() const {
 	return maxReplyLen;
 }
+
+
diff --git a/devicehandlers/Cookie.h b/devicehandlers/Cookie.h
index 60f20ff1..d76facc0 100644
--- a/devicehandlers/Cookie.h
+++ b/devicehandlers/Cookie.h
@@ -1,31 +1,35 @@
 #ifndef COOKIE_H_
 #define COOKIE_H_
-#include <framework/devicehandlers/DeviceHandlerIF.h>
+#include <framework/devicehandlers/CookieIF.h>
 
 /**
  * @brief This datatype is used to identify different connection over a single interface
  *        (like RMAP or I2C)
  * @details
- * To use this class, implement a communication specific child cookie. This cookie
- * can be used in the device communication interface by performing
- * a C++ dynamic cast and passing it to a child device handler, which stores
- * it and passes the Cookie to the communication interface where it can be used
- * by again performing a dynamic cast.
+ * To use this class, implement a communication specific child cookie which
+ * inherits Cookie. Cookie instances are created in config/ Factory.cpp by calling
+ * CookieIF* childCookie = new ChildCookie(...).ยด
+ *
+ * This cookie is then passed to the child device handlers, which stores the
+ * pointer and passes it to the communication interface functions.
+ *
  * The cookie can be used to store all kinds of information
  * about the communication, like slave addresses, communication status,
  * communication parameters etc.
+ *
  * @ingroup comm
  */
-class Cookie{
+class Cookie: public CookieIF {
 public:
-	Cookie() = default;
+	Cookie();
 	Cookie(address_t logicalAddress_);
-	virtual ~Cookie(){}
+	virtual ~Cookie() {};
 
-	void setMaxReplyLen(size_t maxReplyLen_);
+	virtual void setAddress(address_t logicalAddres_);
+	virtual void setMaxReplyLen(size_t maxReplyLen_);
 
-	address_t getAddress() const;
-	size_t getMaxReplyLen() const;
+	virtual address_t getAddress() const;
+	virtual size_t getMaxReplyLen() const;
 private:
 	address_t logicalAddress = 0;
 	size_t maxReplyLen = 0;
diff --git a/devicehandlers/CookieIF.h b/devicehandlers/CookieIF.h
new file mode 100644
index 00000000..3e08709b
--- /dev/null
+++ b/devicehandlers/CookieIF.h
@@ -0,0 +1,25 @@
+/**
+ * @file CookieIF.h
+ *
+ * @date 23 Mar 2020
+ */
+
+#ifndef FRAMEWORK_DEVICEHANDLERS_COOKIEIF_H_
+#define FRAMEWORK_DEVICEHANDLERS_COOKIEIF_H_
+#include <framework/devicehandlers/DeviceHandlerIF.h>
+
+class CookieIF {
+public:
+	/**
+	 * Default empty virtual destructor.
+	 */
+	virtual ~CookieIF() {};
+
+	virtual void setAddress(address_t logicalAddress_) = 0;
+	virtual address_t getAddress() const = 0;
+
+	virtual void setMaxReplyLen(size_t maxReplyLen_) = 0;
+	virtual size_t getMaxReplyLen() const = 0;
+};
+
+#endif /* FRAMEWORK_DEVICEHANDLERS_COOKIEIF_H_ */
diff --git a/devicehandlers/DeviceCommunicationIF.h b/devicehandlers/DeviceCommunicationIF.h
index 43aa7965..6f271504 100644
--- a/devicehandlers/DeviceCommunicationIF.h
+++ b/devicehandlers/DeviceCommunicationIF.h
@@ -57,7 +57,7 @@ public:
 	 *         - Everything else triggers sending failed event with
 	 *           returnvalue as parameter 1
 	 */
-	virtual ReturnValue_t sendMessage(Cookie *cookie, const uint8_t * sendData,
+	virtual ReturnValue_t sendMessage(CookieIF *cookie, const uint8_t * sendData,
 			size_t sendLen) = 0;
 
 	/**
@@ -68,7 +68,7 @@ public:
 	 * 		   - Everything else triggers sending failed event with
 	 *           returnvalue as parameter 1
 	 */
-	virtual ReturnValue_t getSendSuccess(Cookie *cookie) = 0;
+	virtual ReturnValue_t getSendSuccess(CookieIF *cookie) = 0;
 
 	/**
 	 * Called by DHB in the SEND_WRITE doSendRead().
@@ -76,7 +76,7 @@ public:
 	 * @param cookie
 	 * @return
 	 */
-	virtual ReturnValue_t requestReceiveMessage(Cookie *cookie, size_t requestLen) = 0;
+	virtual ReturnValue_t requestReceiveMessage(CookieIF *cookie, size_t requestLen) = 0;
 
 	/**
 	 * Called by DHB in the GET_WRITE doGetRead().
@@ -89,7 +89,7 @@ public:
 	 *         - Everything else triggers receiving failed with
 	 *           returnvalue as parameter 1
 	 */
-	virtual ReturnValue_t readReceivedMessage(Cookie *cookie, uint8_t **buffer,
+	virtual ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer,
 			size_t *size) = 0;
 };
 
diff --git a/devicehandlers/DeviceHandlerBase.cpp b/devicehandlers/DeviceHandlerBase.cpp
index 4b88355a..33e44146 100644
--- a/devicehandlers/DeviceHandlerBase.cpp
+++ b/devicehandlers/DeviceHandlerBase.cpp
@@ -16,23 +16,22 @@ object_id_t DeviceHandlerBase::powerSwitcherId = 0;
 object_id_t DeviceHandlerBase::rawDataReceiverId = 0;
 object_id_t DeviceHandlerBase::defaultFDIRParentId = 0;
 
-DeviceHandlerBase::DeviceHandlerBase(object_id_t setObjectId, address_t logicalAddress_,
-		object_id_t deviceCommunication, Cookie * cookie_, size_t maxReplyLen,
-		uint8_t setDeviceSwitch, uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId,
+DeviceHandlerBase::DeviceHandlerBase(object_id_t setObjectId, object_id_t deviceCommunication,
+		CookieIF * comCookie_, size_t maxReplyLen, uint8_t setDeviceSwitch,
+		uint32_t thermalStatePoolId, uint32_t thermalRequestPoolId,
 		FailureIsolationBase* fdirInstance, size_t cmdQueueSize) :
 		SystemObject(setObjectId), mode(MODE_OFF), submode(SUBMODE_NONE),
 		wiretappingMode(OFF), storedRawData(StorageManagerIF::INVALID_ADDRESS),
-	    deviceCommunicationId(deviceCommunication), cookie(cookie_),
+	    deviceCommunicationId(deviceCommunication), comCookie(comCookie_),
 		deviceThermalStatePoolId(thermalStatePoolId), deviceThermalRequestPoolId(thermalRequestPoolId),
 		healthHelper(this, setObjectId), modeHelper(this), parameterHelper(this),
 		fdirInstance(fdirInstance), hkSwitcher(this),
 		defaultFDIRUsed(fdirInstance == nullptr), switchOffWasReported(false),
 		executingTask(nullptr), actionHelper(this, nullptr), cookieInfo(),
-		logicalAddress(logicalAddress_), childTransitionDelay(5000),
-		transitionSourceMode(_MODE_POWER_DOWN), transitionSourceSubMode(SUBMODE_NONE),
-		deviceSwitch(setDeviceSwitch)
+		childTransitionDelay(5000), transitionSourceMode(_MODE_POWER_DOWN),
+		transitionSourceSubMode(SUBMODE_NONE), deviceSwitch(setDeviceSwitch)
 {
-	this->cookie->setMaxReplyLen(maxReplyLen);
+	this->comCookie->setMaxReplyLen(maxReplyLen);
 	commandQueue = QueueFactory::instance()->
 			createMessageQueue(cmdQueueSize, CommandMessage::MAX_MESSAGE_SIZE);
 	cookieInfo.state = COOKIE_UNUSED;
@@ -44,6 +43,7 @@ DeviceHandlerBase::DeviceHandlerBase(object_id_t setObjectId, address_t logicalA
 }
 
 DeviceHandlerBase::~DeviceHandlerBase() {
+	delete comCookie;
 	if (defaultFDIRUsed) {
 		delete fdirInstance;
 	}
@@ -506,7 +506,7 @@ void DeviceHandlerBase::replyToReply(DeviceReplyMap::iterator iter,
 void DeviceHandlerBase::doSendWrite() {
 	if (cookieInfo.state == COOKIE_WRITE_READY) {
 
-		ReturnValue_t result = communicationInterface->sendMessage(cookie,
+		ReturnValue_t result = communicationInterface->sendMessage(comCookie,
 				rawPacket, rawPacketLen);
 
 		if (result == RETURN_OK) {
@@ -527,7 +527,7 @@ void DeviceHandlerBase::doGetWrite() {
 		return;
 	}
 	cookieInfo.state = COOKIE_UNUSED;
-	ReturnValue_t result = communicationInterface->getSendSuccess(cookie);
+	ReturnValue_t result = communicationInterface->getSendSuccess(comCookie);
 	if (result == RETURN_OK) {
 		if (wiretappingMode == RAW) {
 			replyRawData(rawPacket, rawPacketLen, requestedRawTraffic, true);
@@ -548,7 +548,7 @@ void DeviceHandlerBase::doGetWrite() {
 
 void DeviceHandlerBase::doSendRead() {
 	ReturnValue_t result;
-	result = communicationInterface->requestReceiveMessage(cookie, requestLen);
+	result = communicationInterface->requestReceiveMessage(comCookie, requestLen);
 	if (result == RETURN_OK) {
 		cookieInfo.state = COOKIE_READ_SENT;
 	} else {
@@ -575,7 +575,7 @@ void DeviceHandlerBase::doGetRead() {
 
 	cookieInfo.state = COOKIE_UNUSED;
 
-	result = communicationInterface->readReceivedMessage(cookie, &receivedData,
+	result = communicationInterface->readReceivedMessage(comCookie, &receivedData,
 			&receivedDataLen);
 
 	if (result != RETURN_OK) {
@@ -1277,7 +1277,7 @@ void DeviceHandlerBase::debugInterface(uint8_t positionTracker, object_id_t obje
 }
 
 uint32_t DeviceHandlerBase::getLogicalAddress() {
-	return logicalAddress;
+	return this->comCookie->getAddress();
 }
 
 void DeviceHandlerBase::performOperationHook() {
diff --git a/devicehandlers/DeviceHandlerBase.h b/devicehandlers/DeviceHandlerBase.h
index 3e588115..05be2d45 100644
--- a/devicehandlers/DeviceHandlerBase.h
+++ b/devicehandlers/DeviceHandlerBase.h
@@ -17,6 +17,7 @@
 #include <framework/tasks/ExecutableObjectIF.h>
 #include <framework/devicehandlers/DeviceHandlerFailureIsolation.h>
 #include <framework/datapool/HkSwitchHelper.h>
+#include <framework/devicehandlers/CookieIF.h>
 #include <framework/serialize/SerialFixedArrayListAdapter.h>
 #include <framework/ipc/MessageQueueIF.h>
 #include <framework/tasks/PeriodicTaskIF.h>
@@ -93,9 +94,9 @@ public:
 	 * @param fdirInstance
 	 * @param cmdQueueSize
 	 */
-	DeviceHandlerBase(object_id_t setObjectId, address_t logicalAddress_,
-			object_id_t deviceCommunication, Cookie* cookie_, size_t maxReplyLen,
-			uint8_t setDeviceSwitch, uint32_t thermalStatePoolId = PoolVariableIF::NO_PARAMETER,
+	DeviceHandlerBase(object_id_t setObjectId, object_id_t deviceCommunication,
+			CookieIF * comCookie_, size_t maxReplyLen, uint8_t setDeviceSwitch,
+			uint32_t thermalStatePoolId = PoolVariableIF::NO_PARAMETER,
 			uint32_t thermalRequestPoolId = PoolVariableIF::NO_PARAMETER,
 			FailureIsolationBase* fdirInstance = nullptr, size_t cmdQueueSize = 20);
 
@@ -422,22 +423,6 @@ protected:
 	 */
 	size_t requestLen = 0;
 
-//	/**
-//	 * This union (or std::variant) type can be used to set comParameters which
-//	 * are passed in the open() and reOpen() calls to the communication
-//	 * interface
-//	 * TODO: I don't know if we should use C++17 features but if we do we propably
-//	 *		 should also write a function to get either a storeId handle
-//	 *		 or an array handle so these values can be set conveniently.
-//	 * The good think is that if there are many parameters, they can
-//	 * be stored in the IPC pool. But maybe two uint32_t parameters are enough.
-//	 * Simple = Good. It is downwards compatible and one can still store
-//	 * 4 bytes of parameters AND a store ID.
-//	 */
-//	comParameters_t comParameters;
-	// uint32_t comParameter1 = 0;
-	// uint32_t comParameter2 = 0;
-
 	/**
 	 * The mode the device handler is currently in.
 	 *
@@ -515,7 +500,7 @@ protected:
 	 * Cookie used for communication. This is passed to the communication
 	 * interface.
 	 */
-	Cookie *cookie;
+	CookieIF *comCookie;
 
 	/**
 	 * The MessageQueue used to receive device handler commands and to send replies.
@@ -959,7 +944,7 @@ private:
 	/**
 	* cached from ctor for initialize()
 	*/
-	const uint32_t logicalAddress;
+	//const uint32_t logicalAddress = 0;
 
 	/**
 	 * Used for timing out mode transitions.
diff --git a/rmap/RMAPCookie.cpp b/rmap/RMAPCookie.cpp
index 5bf2ba9f..84e407ba 100644
--- a/rmap/RMAPCookie.cpp
+++ b/rmap/RMAPCookie.cpp
@@ -93,10 +93,10 @@ RMAPCookie::~RMAPCookie() {
 
 }
 
-uint32_t RMAPCookie::getMaxReplyLen() const {
-	return maxReplyLen;
-}
-
+//uint32_t RMAPCookie::getMaxReplyLen() const {
+//	return maxReplyLen;
+//}
+//
 void RMAPCookie::setMaxReplyLen(uint32_t maxReplyLen) {
 	this->maxReplyLen = maxReplyLen;
 }
diff --git a/rmap/RMAPCookie.h b/rmap/RMAPCookie.h
index c091ba18..cf033bae 100644
--- a/rmap/RMAPCookie.h
+++ b/rmap/RMAPCookie.h
@@ -6,7 +6,7 @@
 
 class RMAPChannelIF;
 
-class RMAPCookie : public Cookie{
+class RMAPCookie : public Cookie {
 public:
 	//To Uli: Sorry, I need an empty ctor to initialize an array of cookies.
 	RMAPCookie();
@@ -28,7 +28,7 @@ public:
 	void setCommandMask(uint8_t commandMask);
 	uint8_t getCommandMask();
 
-	uint32_t getMaxReplyLen() const;
+	//size_t getMaxReplyLen() const;
 	void setMaxReplyLen(uint32_t maxReplyLen);
 
 	uint16_t getTransactionIdentifier() const;