Add control socket message handling classes 95/174795/2
authorIgor Kotrasinski <i.kotrasinsk@partner.samsung.com>
Thu, 7 Dec 2017 13:04:57 +0000 (14:04 +0100)
committerIgor Kotrasinski <i.kotrasinsk@partner.samsung.com>
Fri, 6 Apr 2018 10:03:34 +0000 (12:03 +0200)
Change-Id: Ia97a02e550b93f250be1ebc5ba14159dd6b3baa9
Signed-off-by: Igor Kotrasinski <i.kotrasinsk@partner.samsung.com>
simulatordaemon/CMakeLists.txt
simulatordaemon/daemonctl/inc/ControlCommand.h [new file with mode: 0644]
simulatordaemon/inc/ControlConnectionHandler.h [new file with mode: 0644]
simulatordaemon/src/ControlConnectionHandler.cpp [new file with mode: 0644]

index 5efd4a0..725c12b 100644 (file)
@@ -17,6 +17,8 @@
 # @brief   CMakeLists for tef-simulator daemon unit
 #
 
+SET(DAEMONCTL_PATH ${DAEMON_PATH}/daemonctl)
+
 PKG_CHECK_MODULES(DAEMON_DEPS REQUIRED
                   cynara-client
                   cynara-session
@@ -84,6 +86,7 @@ INCLUDE_DIRECTORIES(
     ${DAEMON_PATH}/inc/ResponseCommands
     # TODO move TABinaryManager headers to inc directory
     ${DAEMON_PATH}/src/TABinaryManager/
+    ${DAEMONCTL_PATH}/inc
     ${TEF_SIMULATOR_INCLUDE_PATH}/include
     ${LOG_PATH}
     ${OSAL_PATH}
diff --git a/simulatordaemon/daemonctl/inc/ControlCommand.h b/simulatordaemon/daemonctl/inc/ControlCommand.h
new file mode 100644 (file)
index 0000000..5b5d32c
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+/**
+ * @file
+ * @author Igor Kotrasinski (i.kotrasinsk@partner.samsung.com)
+ * @brief  Command types for the control socket
+ */
+
+#ifndef _CONTROLCOMMAND_H
+#define _CONTROLCOMMAND_H
+
+#include <cstdint>
+#include "tee_client_api.h"
+
+enum ControlCommand : uint32_t {
+       CTL_SET_PORT,
+};
+
+enum ControlCommandReply : uint32_t {
+       CTL_SET_PORT_REPLY,
+       CTL_INVALID_CMD_REPLY,
+};
+
+enum ControlReplyStatus : uint32_t {
+       CTL_REPLY_SUCCESS,
+       CTL_REPLY_TA_NOT_FOUND,
+};
+
+struct SetPortControlCommand {
+       TEEC_UUID uuid;
+       uint32_t port;
+};
+
+struct SetPortControlCommandReply {
+       enum ControlReplyStatus status;
+};
+
+#endif /* _CONTROLCOMMAND_H */
diff --git a/simulatordaemon/inc/ControlConnectionHandler.h b/simulatordaemon/inc/ControlConnectionHandler.h
new file mode 100644 (file)
index 0000000..474104a
--- /dev/null
@@ -0,0 +1,49 @@
+/**
+ * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+/**
+ * @file
+ * @author Igor Kotrasinski (i.kotrasinsk@partner@samsung.com)
+ * @brief  Handler for data coming over TEEC socket
+ */
+
+#ifndef _CONTROLCONNECTIONHANDLER_H
+#define _CONTROLCONNECTIONHANDLER_H
+
+#include <cstdint>
+#include <boost/system/error_code.hpp>
+#include "IConnectionHandler.h"
+#include "ControlCommand.h"
+#include "TABinaryManager.h"
+
+
+class ControlConnectionHandler: public IConnectionHandler<enum ControlCommand>
+{
+public:
+       void setWriter(IConnectionWriter<enum ControlCommand> &writer) override;
+       void handleConnect(int sock) override;
+       int32_t getDataSize(enum ControlCommand cmd) override;
+       void handleRead(enum ControlCommand header, std::vector<char> &data) override;
+       void handleReadError(boost::system::error_code e) override;
+private:
+       TABinaryManager *getBinaryManager();
+       IConnectionWriter<enum ControlCommand> *m_writer;
+       void handleSetPortCommand(std::vector<char> &data);
+       void handleInvalidCommand();
+       void handleConnectionClosed();
+};
+
+#endif /* _CONTROLCONNECTIONHANDLER_H */
diff --git a/simulatordaemon/src/ControlConnectionHandler.cpp b/simulatordaemon/src/ControlConnectionHandler.cpp
new file mode 100644 (file)
index 0000000..7daa776
--- /dev/null
@@ -0,0 +1,105 @@
+/**
+ * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+/**
+ * @file
+ * @author Igor Kotrasinski (i.kotrasinsk@partner@samsung.com)
+ * @brief  ControlConnectionHandler class
+ */
+
+#include <cstring>
+#include <boost/asio.hpp>
+#include "log.h"
+#include "ControlConnectionHandler.h"
+#include "IConnectionWriter.h"
+
+void ControlConnectionHandler::setWriter(IConnectionWriter<enum ControlCommand> &writer)
+{
+       this->m_writer = &writer;
+}
+
+void ControlConnectionHandler::handleConnect(int sock)
+{
+       return;
+}
+
+int32_t ControlConnectionHandler::getDataSize(enum ControlCommand cmd)
+{
+       LOGD(SIM_DAEMON, "Control command received: %d", (uint32_t)cmd);
+       switch(cmdData) {
+       case CTL_SET_PORT:
+               return sizeof(SetPortControlCommand);
+       default:
+               LOGE(SIM_DAEMON, "Invalid command received!");
+               return -1;
+       }
+}
+
+void ControlConnectionHandler::handleRead(enum ControlCommand header,
+                                         std::vector<char> &data)
+{
+       switch(header) {
+       case CTL_SET_PORT:
+               handleSetPortCommand(data);
+               break;
+       default:
+               handleInvalidCommand();
+               break;
+       }
+}
+
+void ControlConnectionHandler::handleReadError(boost::system::error_code e)
+{
+       LOGE(SIM_DAEMON, "Error in reading from the control socket: Response returned with error code %d, message %s",
+            e.value(), e.category().name());
+}
+
+void ControlConnectionHandler::handleConnectionClosed() {}
+
+TABinaryManager *ControlConnectionHandler::getBinaryManager()
+{
+       return TABinaryManager::getInstance();
+}
+
+void ControlConnectionHandler::handleSetPortCommand(std::vector<char> &data)
+{
+       struct SetPortControlCommand cmd;
+       struct SetPortControlCommandReply reply;
+       TABinaryManager *binaryManager;
+       int ret;
+
+       std::memcpy(&cmd, data.data(), sizeof(cmd));
+       std::string portString = std::to_string(cmd.port);
+       binaryManager = getBinaryManager();
+       std::string uuidString = binaryManager.getUUIDAsString(data.uuid);
+
+       ret = binaryManager.setPort(uuidString, portString);
+       if (!ret) {
+               reply.status = CTL_REPLY_SUCCESS;
+               LOGI(SIM_DAEMON, "Set debug port of UUID %s to %s",
+                               uuidString.c_str(), portString.c_str());
+       } else {
+               reply.status = CTL_REPLY_TA_NOT_FOUND;
+               LOGE(SIM_DAEMON, "Failed to set debug port of UUID %s to %s - TA not found",
+                               uuidString.c_str(), portString.c_str());
+       }
+       m_writer->write(CTL_SET_PORT_REPLY, (char *) reply, sizeof(reply));
+}
+
+void ControlConnectionHandler::handleInvalidCommand()
+{
+       m_writer->write(CTL_INVALID_CMD_REPLY, nullptr, 0);
+}