From f60a02e52693eab39c180ec9bf2a08b3847dca2f Mon Sep 17 00:00:00 2001 From: Zofia Abramowska Date: Mon, 10 Oct 2016 16:09:07 +0200 Subject: [PATCH] SM : Add MessagePipe Change-Id: I720056bc27c837914ed56ee36bca1512093ac4a9 --- src/common/CMakeLists.txt | 1 + src/common/message_pipe.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++ src/common/message_pipe.h | 59 +++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 src/common/message_pipe.cpp create mode 100644 src/common/message_pipe.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 4e1055f..4fd8953 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -28,6 +28,7 @@ SET(COMMON_TARGET_TEST_SOURCES ${PROJECT_SOURCE_DIR}/src/common/fs_label_manager.cpp ${PROJECT_SOURCE_DIR}/src/common/passwd_access.cpp ${PROJECT_SOURCE_DIR}/src/common/uds.cpp + ${PROJECT_SOURCE_DIR}/src/common/message_pipe.cpp ${PROJECT_SOURCE_DIR}/src/common/synchronization_pipe.cpp ${PROJECT_SOURCE_DIR}/src/common/timeout.cpp ${PROJECT_SOURCE_DIR}/src/common/temp_test_user.cpp diff --git a/src/common/message_pipe.cpp b/src/common/message_pipe.cpp new file mode 100644 index 0000000..a29ea8d --- /dev/null +++ b/src/common/message_pipe.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 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 message_pipe.cpp + * @author Zofia Abramowska + * @version 1.0 + * @brief Simple wrapper for message pipe + */ + +#include "message_pipe.h" + +static void closeFd(int *fd) { + if (*fd > -1) { + close(*fd); + *fd = -1; + } +} + +MessagePipe::MessagePipe() { + auto ret = pipe(m_pipeCP); + RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed"); + + ret = pipe(m_pipePC); + RUNNER_ASSERT_ERRNO_MSG(ret == 0, "pipe failed"); +} + +MessagePipe::~MessagePipe() { + closeFd(m_pipeCP + 0); + closeFd(m_pipeCP + 1); + closeFd(m_pipePC + 0); + closeFd(m_pipePC + 1); +} + +void MessagePipe::claimParentEp() { + if (m_epClaimed) + return; + + m_readEp = m_pipeCP[0]; + closeFd(m_pipeCP + 1); + + m_writeEp = m_pipePC[1]; + closeFd(m_pipePC + 0); + + m_epClaimed = true; +} + +void MessagePipe::claimChildEp() { + if (m_epClaimed) + return; + + m_readEp = m_pipePC[0]; + closeFd(m_pipePC + 1); + + m_writeEp = m_pipeCP[1]; + closeFd(m_pipeCP + 0); + + m_epClaimed = true; +} + diff --git a/src/common/message_pipe.h b/src/common/message_pipe.h new file mode 100644 index 0000000..51780e9 --- /dev/null +++ b/src/common/message_pipe.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016 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 message_pipe.h + * @author Zofia Abramowska + * @version 1.0 + * @brief Simple message pipe wrapper + */ + +#pragma once + +#include + +#include + +class MessagePipe { +public: + MessagePipe(); + ~MessagePipe(); + + void claimParentEp(); + void claimChildEp(); + + template + void write(const T& data) { + RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed"); + auto ret = TEMP_FAILURE_RETRY(::write(m_writeEp, &data, sizeof(data))); + RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Write failed ret = " << ret); + RUNNER_ASSERT_MSG(ret == sizeof(data), "ERROR : lazy programmer didn't implement write properly"); + + } + template + void read(T& data) { + RUNNER_ASSERT_MSG(m_epClaimed == true, "Endpoint not claimed"); + auto ret = TEMP_FAILURE_RETRY(::read(m_readEp, &data, sizeof(data))); + RUNNER_ASSERT_ERRNO_MSG(ret > 0, "Read failed ret = " << ret); + RUNNER_ASSERT_MSG(ret == sizeof(data), "ERROR : lazy programmer didn't implement read properly"); + } + +private: + int m_pipeCP[2]; // Child -> Parent + int m_pipePC[2]; // Parent -> Child + int m_readEp = -1; + int m_writeEp = -1; + bool m_epClaimed = false; +}; -- 2.7.4