From: Igor Kotrasinski Date: Thu, 7 Dec 2017 13:04:57 +0000 (+0100) Subject: Add control socket message handling classes X-Git-Tag: submit/tizen/20180412.092951~16 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9db99b58d0a9af39a6f8cffa3d5ad2775f3d7cf0;p=platform%2Fcore%2Fsecurity%2Ftef-simulator.git Add control socket message handling classes Change-Id: Ia97a02e550b93f250be1ebc5ba14159dd6b3baa9 Signed-off-by: Igor Kotrasinski --- diff --git a/simulatordaemon/CMakeLists.txt b/simulatordaemon/CMakeLists.txt index 5efd4a0..725c12b 100644 --- a/simulatordaemon/CMakeLists.txt +++ b/simulatordaemon/CMakeLists.txt @@ -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 index 0000000..5b5d32c --- /dev/null +++ b/simulatordaemon/daemonctl/inc/ControlCommand.h @@ -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 +#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 index 0000000..474104a --- /dev/null +++ b/simulatordaemon/inc/ControlConnectionHandler.h @@ -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 +#include +#include "IConnectionHandler.h" +#include "ControlCommand.h" +#include "TABinaryManager.h" + + +class ControlConnectionHandler: public IConnectionHandler +{ +public: + void setWriter(IConnectionWriter &writer) override; + void handleConnect(int sock) override; + int32_t getDataSize(enum ControlCommand cmd) override; + void handleRead(enum ControlCommand header, std::vector &data) override; + void handleReadError(boost::system::error_code e) override; +private: + TABinaryManager *getBinaryManager(); + IConnectionWriter *m_writer; + void handleSetPortCommand(std::vector &data); + void handleInvalidCommand(); + void handleConnectionClosed(); +}; + +#endif /* _CONTROLCONNECTIONHANDLER_H */ diff --git a/simulatordaemon/src/ControlConnectionHandler.cpp b/simulatordaemon/src/ControlConnectionHandler.cpp new file mode 100644 index 0000000..7daa776 --- /dev/null +++ b/simulatordaemon/src/ControlConnectionHandler.cpp @@ -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 +#include +#include "log.h" +#include "ControlConnectionHandler.h" +#include "IConnectionWriter.h" + +void ControlConnectionHandler::setWriter(IConnectionWriter &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 &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 &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); +}