diff options
author | 2020-11-19 12:45:32 +0900 | |
---|---|---|
committer | 2020-11-19 12:45:32 +0900 | |
commit | 8e0e00d21146a84c18f9cf9409e187b4fb0248aa (patch) | |
tree | ef791689dad216ac61091a1d1bd3b928d563aba6 /nsframework/framework_unified/client/NS_SharedMemIf | |
parent | 18df6e21c6743a137e2760c52ca89d0789e90417 (diff) |
Init basesystem source codes.
Signed-off-by: Riku Nomoto <riku_nomoto@mail.toyota.co.jp>
Change-Id: I55aa2f1406ce7f751ae14140b613b53b68995528
Diffstat (limited to 'nsframework/framework_unified/client/NS_SharedMemIf')
7 files changed, 1903 insertions, 0 deletions
diff --git a/nsframework/framework_unified/client/NS_SharedMemIf/api/nsapi_sharedmem.cpp b/nsframework/framework_unified/client/NS_SharedMemIf/api/nsapi_sharedmem.cpp new file mode 100755 index 0000000..89c7421 --- /dev/null +++ b/nsframework/framework_unified/client/NS_SharedMemIf/api/nsapi_sharedmem.cpp @@ -0,0 +1,431 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +/************************************************************************* + * + * FILENAME : nsapi_sharedmem.cpp + * + * DESCRIPTION : TestApp - NS_SharedMem + * + ************************************************************************/ + +#include <native_service/ns_logger_if.h> +#include <native_service/ns_version_if.h> +#include <native_service/ns_sharedmem.h> +#include <native_service/ns_shared_mem_if.h> +#include <native_service/ns_transmit_log.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "ss_version.h" +#include "ns_unittest_internal.h" + +#define AVAILABLE_API +#if defined(AVAILABLE_API) +static CNSSharedMem *g_pTransmitLogSharedBuf = NULL; + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemTransmitLogOpen +/// Open the shared memory for transmit logging +//////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus NSSharedMemTransmitLogOpen() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL == g_pTransmitLogSharedBuf) { + // Create the instance + g_pTransmitLogSharedBuf = new(std::nothrow) CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + } + + if (NULL != g_pTransmitLogSharedBuf) { + if (!g_pTransmitLogSharedBuf->IsOpen()) { + // maps the shared memory buffer + l_eStatus = g_pTransmitLogSharedBuf->Open(); + } + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemTransmitLogClose +/// Close the transmit logging shared memory +//////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus NSSharedMemTransmitLogClose() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != g_pTransmitLogSharedBuf) { + // un-map the shared memory object + l_eStatus = g_pTransmitLogSharedBuf->Close(); + + delete g_pTransmitLogSharedBuf; + g_pTransmitLogSharedBuf = NULL; + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemReadTransmitLog +/// Reads transmit log from the shared memory buffer. +//////////////////////////////////////////////////////////////////////////////////////////// +SI_32 NSSharedMemReadTransmitLog(PSTR f_pBuffer, const UI_32 f_uiLength, const BOOL f_bBlock) { + // no. of bytes read + SI_32 l_iReadSize = NS_SHM_ERROR; + + if (NULL != g_pTransmitLogSharedBuf && NULL != f_pBuffer) { + // Writes log data into shared memory buffer + l_iReadSize = g_pTransmitLogSharedBuf->Read(f_pBuffer, f_uiLength, f_bBlock); + } + + return l_iReadSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemWriteTransmitLog +/// Write transmit log into the shared memory buffer. +//////////////////////////////////////////////////////////////////////////////////////////// +SI_32 NSSharedMemWriteTransmitLog(PCSTR f_pBuffer, const UI_32 f_uiLength) { + // no. of bytes read + SI_32 l_iWriteSize = NS_SHM_ERROR; + + if (NULL != g_pTransmitLogSharedBuf && NULL != f_pBuffer) { + // Writes log data into shared memory buffer + l_iWriteSize = g_pTransmitLogSharedBuf->Write(f_pBuffer, f_uiLength); + } + + return l_iWriteSize; +} +#endif + + +// For NsLog +CFrameworkunifiedVersion g_FrameworkunifiedVersion(MAJORNO, MINORNO, REVISION); +#define ZONE_INIT ZONEMASK(10) +#define ZONE_FUNC ZONEMASK(11) +#define ZONE_MEM ZONEMASK(12) +#define ZONE_INFO ZONEMASK(29) +#define ZONE_WARN ZONEMASK(30) +#define ZONE_ERR ZONEMASK(31) +#define FRAMEWORKUNIFIEDLOGAPPZONES ZONE_INFO, ZONE_WARN, ZONE_ERR +FRAMEWORKUNIFIEDLOGPARAM g_FrameworkunifiedLogParams = { + LPRINT, + { + "Init", "Function", "Memory", + "", "", "", + "", "", "", + "", "", "", + "", "", "", + "", "", "", + "", + "Info", "Warning", "Error" + }, + FRAMEWORKUNIFIEDLOGZONES +}; + +/** sleep */ +static int +sleep_msec(int msec) { + struct timespec ts, rem; + ts.tv_sec = msec / 1000; + ts.tv_nsec = (msec % 1000) * 1000000; + int ret; + for (;;) { + ret = nanosleep(&ts, &rem); + if (ret == 0 || (ret < 0 && errno != EINTR)) { + break; + } + ts = rem; + } + return ret; +} + +/** usage */ +void usage(void) { + printf("nsapi_sharedmem [command] [option]\n"); + printf(" command: preset\n"); +#if !defined(AVAILABLE_API) + printf(" preset service_write\n"); + printf(" preset service_write_read n\n"); +#endif + printf(" preset sharedmem_write\n"); + printf(" preset sharedmem_write_read n\n"); + printf(" preset write_6_A\n"); + printf(" preset write_6_B\n"); + printf(" preset write_7_A\n"); + printf(" preset write_7_B\n"); + printf(" command: manual\n"); + printf(" manual ***\n"); +} + +/** main */ +int main(int argc, char *argv[]) { + int status = 0; + + FRAMEWORKUNIFIED_SET_ZONES(); + + if (argc < 2) { + usage(); + return 0; + } else { + char *command = argv[1]; + int option = 0; + + /* Execute command */ + if (strcmp(command, "preset") == 0) { + if (argc > 2) { + char *preset = argv[2]; + if (argc > 3) { + option = atoi(argv[3]); + } + +#if defined(AVAILABLE_API) + if (strcmp(preset, "sharedmem_write") == 0) { + NSUT_ASSERT(NSSharedMemTransmitLogOpen() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogOpen() error."); + SI_32 size = NSSharedMemWriteTransmitLog(argv[2], static_cast<UI_32>(strlen(argv[2]) + 1)); + NSUT_ASSERT(size == (SI_32)(strlen(argv[2]) + 1), "NSSharedMemWriteTransmitLog() error."); + NSUT_ASSERT(NSSharedMemTransmitLogClose() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogClose() error."); + if (size != (SI_32)(strlen(argv[2]) + 1)) { + return -1; + } + } else if (strcmp(preset, "sharedmem_write_read") == 0) { + for (int i = 0; i < option; i++) { + NSUT_ASSERT(NSSharedMemTransmitLogOpen() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogOpen() error."); + SI_32 size = NSSharedMemWriteTransmitLog(argv[2], static_cast<UI_32>(strlen(argv[2]) + 1)); + NSUT_ASSERT(size == (SI_32)(strlen(argv[2]) + 1), "NSSharedMemWriteTransmitLog() error."); + char f_pBuffer[100] = {0}; + NSSharedMemReadTransmitLog(f_pBuffer, sizeof(f_pBuffer), FALSE); + NSUT_ASSERT(NSSharedMemTransmitLogClose() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogClose() error."); + } + } +#else + if (strcmp(preset, "service_write") == 0) { + TMemID id = SetDataToShared(argv[2], strlen(argv[2]) + 1); + NSUT_ASSERT(id != BAD_MEM_ID, "SetDataToShared() error."); + if (id == BAD_MEM_ID) { + return -1; + } + } else if (strcmp(preset, "service_write_read") == 0) { + for (int i = 0; i < option; i++) { + EFrameworkunifiedStatus status; + TMemID id = SetDataToShared(argv[2], strlen(argv[2]) + 1); + NSUT_ASSERT(id != BAD_MEM_ID, "SetDataToShared() error."); + UI_32 size = GetLengthOfDataFromShared(id); + NSUT_ASSERT(size == (strlen(argv[2]) + 1), "NSSharedMemWriteTransmitLog() error."); + void *data[100] = { 0 }; + status = GetDataFromShared(id, data, sizeof(data)); + NSUT_ASSERT(eFrameworkunifiedStatusOK == status, "GetDataFromShared()"); + status = DiscardDataFromShared(id); + NSUT_ASSERT(eFrameworkunifiedStatusOK == status, "DiscardDataFromShared()"); + } + } else if (strcmp(preset, "sharedmem_write") == 0) { + NSUT_ASSERT(NSSharedMemTransmitLogOpen() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogOpen() error."); + SI_32 size = NSSharedMemWriteTransmitLog(argv[2], strlen(argv[2]) + 1); + NSUT_ASSERT(size == (SI_32)(strlen(argv[2]) + 1), "NSSharedMemWriteTransmitLog() error."); + NSUT_ASSERT(NSSharedMemTransmitLogClose() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogClose() error."); + if (size != (SI_32)(strlen(argv[2]) + 1)) { + return -1; + } + } else if (strcmp(preset, "sharedmem_write_read") == 0) { + for (int i = 0; i < option; i++) { + NSUT_ASSERT(NSSharedMemTransmitLogOpen() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogOpen() error."); + SI_32 size = NSSharedMemWriteTransmitLog(argv[2], strlen(argv[2]) + 1); + NSUT_ASSERT(size == (SI_32)(strlen(argv[2]) + 1), "NSSharedMemWriteTransmitLog() error."); + char f_pBuffer[100] = {0}; + NSSharedMemReadTransmitLog(f_pBuffer, sizeof(f_pBuffer), FALSE); + NSUT_ASSERT(NSSharedMemTransmitLogClose() == eFrameworkunifiedStatusOK, "NSSharedMemTransmitLogClose() error."); + } + } +#endif + else if (strcmp(preset, "write_6_A") == 0) { // NOLINT (readability/nolint) + CNSSharedMem *testObj = new CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + if (testObj->Open() != eFrameworkunifiedStatusOK) { + status = -1; + } + sleep(2); + char f_pBuffer[100] = {0}; + if (testObj->Read(f_pBuffer, sizeof(f_pBuffer), FALSE) != 7) { + status = -1; + } + if (memcmp(f_pBuffer, "ABCDEFG", 7) != 0) { + printf("memcmp() error."); + return -1; + } + if (testObj->Close() != eFrameworkunifiedStatusOK) { + status = -1; + } + delete testObj; + } else if (strcmp(preset, "write_6_B") == 0) { + CNSSharedMem *testObj = new CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + if (testObj->Open() != eFrameworkunifiedStatusOK) { + status = -1; + } + if (testObj->Write("ABCDEFG", static_cast<UI_32>(strlen("ABCDEFG"))) != 7) { + status = -1; + } + if (testObj->Close() != eFrameworkunifiedStatusOK) { + status = -1; + } + delete testObj; + } else if (strcmp(preset, "write_7_A") == 0) { + CNSSharedMem *testObj = new CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + if (testObj->Open() != eFrameworkunifiedStatusOK) { + status = -1; + } + sleep(2); + char f_pBuffer[100] = {0}; + if (testObj->Read(f_pBuffer, sizeof(f_pBuffer), FALSE) == 7) { + status = -1; + } + if (testObj->Close() != eFrameworkunifiedStatusOK) { + status = -1; + } + delete testObj; + } else if (strcmp(preset, "write_7_B") == 0) { + CNSSharedMem *testObj = new CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + if (testObj->Open() == eFrameworkunifiedStatusOK) { + status = -1; + } + if (testObj->Write("ABCDEFG", static_cast<UI_32>(strlen("ABCDEFG"))) == 7) { + status = -1; + } + if (testObj->Close() == eFrameworkunifiedStatusOK) { + status = -1; + } + delete testObj; + } else if (strcmp(preset, "stress_4") == 0) { + int i; + char *writeBuffer = new char[0x400](); + for (i = 0; i < 0x400; i++) { + writeBuffer[i] = static_cast<char>(i); + } + + CNSSharedMem *testObj = new CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + if (testObj->Open() != eFrameworkunifiedStatusOK) { + status = -1; + } + int timeout = 0; + int test_count = 0; + while ((timeout < 10) && (test_count < option)) { + if (testObj->GetSize() > (SI_32)(TRANSMIT_LOG_SHAREDMEM_SIZE - 0x800)) { + sleep_msec(100); + timeout++; + // printf("timeout = %d\n", timeout); + continue; + } + if (testObj->ClearBuf() != eFrameworkunifiedStatusOK) { + status = -1; + } + if (testObj->SetReadPtrToWritePtr() != eFrameworkunifiedStatusOK) { + status = -1; + } + if (testObj->Write(writeBuffer, 0x400) != 0x400) { + status = -1; + } + timeout = 0; + test_count++; + } + if (testObj->Close() != eFrameworkunifiedStatusOK) { + status = -1; + } + delete testObj; + + delete[] writeBuffer; + } else if (strcmp(preset, "stress_5") == 0) { + int i; + char *writeBuffer = new char[0x100](); + for (i = 0; i < 0x100; i++) { + writeBuffer[i] = static_cast<char>(0xff - i); + } + + CNSSharedMem *testObj = new CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + if (testObj->Open() != eFrameworkunifiedStatusOK) { + status = -1; + } + int timeout = 0; + int count = 0; + while (timeout < 10) { + int size = (count % 0xff) + 1; + if (testObj->GetSize() > (SI_32)(TRANSMIT_LOG_SHAREDMEM_SIZE - 0x800)) { + sleep_msec(100); + timeout++; + continue; + } + if (testObj->Write(&writeBuffer[0xff - size], size) != size) { + status = -1; + } + timeout = 0; + count++; + } + if (testObj->Close() != eFrameworkunifiedStatusOK) { + status = -1; + } + delete testObj; + + delete[] writeBuffer; + } else { + usage(); + } + } else { + usage(); + } + } else if (strcmp(command, "manual") == 0) { + int testno; + char buf[32]; + INT8 active = 1; + + while (active) { + printf(" ----- imageapi ----------\n"); + printf(" 0: debug_test()\n"); + printf(" 1: gfx_ut_image_api()\n"); + printf(" 2: gfx_ut_image_csv()\n"); + printf(" q: quit\n"); + printf(" -------------------------\n"); + + fgets(buf, sizeof(buf), stdin); + if (buf[0] == '\n') { + continue; + } else if (buf[0] == 'q' || buf[0] == 'Q') { + active = 0; + continue; + } + testno = atoi(buf); + + switch (testno) { + case 0: + // debug_test(); + break; + case 1: + // gfx_ut_image_api(); + break; + case 2: + // gfx_ut_image_csv((char *)"it_image.csv"); + break; + default: + break; + } + } + } else { + usage(); + } + } + return status; +} diff --git a/nsframework/framework_unified/client/NS_SharedMemIf/cfg/depends.mk b/nsframework/framework_unified/client/NS_SharedMemIf/cfg/depends.mk new file mode 100755 index 0000000..93b9f97 --- /dev/null +++ b/nsframework/framework_unified/client/NS_SharedMemIf/cfg/depends.mk @@ -0,0 +1,25 @@ +# +# @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. +# +# 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. +# + +# +# Standard Dependency Makefile Version 2.0 +# +# Dependency file dictates not only what other modules the project is dependent on, but +# also where to get that dependent element which logically includes versioning or +# baselining information + +# Utilizing Base Domain Dependency File. Can override at any point +include $(PRJ_ROOT)/../NativeServices/cfg/depends.mk diff --git a/nsframework/framework_unified/client/NS_SharedMemIf/src/makefile_PosixBasedOS001 b/nsframework/framework_unified/client/NS_SharedMemIf/src/makefile_PosixBasedOS001 new file mode 100755 index 0000000..dc225c4 --- /dev/null +++ b/nsframework/framework_unified/client/NS_SharedMemIf/src/makefile_PosixBasedOS001 @@ -0,0 +1,190 @@ +# +# @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. +# +# 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. +# + +# +# Standard Module Makefile version 2.0 +# + +# Name of the componet (team/domain prefix '_' component name) +COMPONENT_NAME = NS_SharedMemIf + +ifndef PRJ_ROOT +export PRJ_ROOT = $(CURDIR)/../ +endif +include $(PRJ_ROOT)cfg/depends.mk + +# Name of the componet (team/domain prefix '_' component name) +# This must be the same as the RTC Component name and Eclipse Project Name +COMPONENT_NAME = NS_SharedMemIf + + +# Additive Compile Flags (Flags from initiating make process will still apply) +DEFS += -Wstrict-aliasing + +# Set local includes and then the reference includes (priority order determines search path) +# Default pattern are any configuration includes (which would be things like PosixBasedOS001), local (Team) component directories, +# dependencies includes (other teams) +# Local (current component references should be in the form of +# $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/directory +# Example your public include directory would be +# $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/inc +# Team references should only be to other's public includes such as +# $(CC_IFLAG)$(TEAM_ROOT)NS_MessageCenter/inc +# Global (non-team) references should be only to other's public includes such +# these are found in the depends include file and captured in the (DEPENDS_INCLUDES) variable +INCLUDES = \ + $(CFG_INCS) \ + $(CC_IFLAG)./ \ + $(DEPENDS_INCLUDES) \ + $(CC_IFLAG)$(TEAM_ROOT)NS_SharedMem/inc \ + $(CC_IFLAG)$(TEAM_ROOT)NS_UtilityCenter/inc \ + $(CC_IFLAG)$(TEAM_ROOT)$(COMPONENT_NAME)/inc + + +# Define binary outputs. These can be libraries or executables. +# Name a variable for each deliverable. Suffixes should be +# EXEC - For Executables -> output to the bin directory +#TIME_EXEC = $(BIN_PATH)time +# LIB - For Static Libraries -> output to lib directory with specific naming +#MATH_LIB = $(LIB_PATH)$(LIB_PREFIX)math.$(LIB_EXT) +# SLIB - For Shared Objects +#FRMWRK_SLIB = $(SLIB_PATH)frmwrk.$(SO_EXT) +# LIB - Define the static library for Message Queue +# +# +ifdef DYNAMIC + COMPONENT_LIB = $(SLIB_PATH)$(LIB_PREFIX)$(COMPONENT_NAME)$(DEBUG_EXT).$(SO_EXT) +else + COMPONENT_LIB = $(LIB_PATH)$(LIB_PREFIX)$(COMPONENT_NAME)$(DEBUG_EXT).$(LIB_EXT) +endif + +## Sources Section + +# Define Library & Executable Sources (on a per deliverable basis) +# This includes sources located in subdirectories. + +# Define generic line that pulls all c, cc, cpp files +# since your in the src folder is pull only files from there +COMPONENT_SRCS = \ + $(wildcard *.c) \ + $(wildcard *.cpp) + +# Define sources that my not be local to your component +# here, you can define indivial files or wildcard from +# a different folder. +NON_LOCAL_SRCS = \ + + +# List of all sources to be built. Can be assembled from the other defintitions. +# This only defines sources for the current directory, so if there are subdirectories +# those are not included. (Those are found in simple subdirectory makefiles that only +# direct the building of sources, but no linking into a binary) +SOURCES = \ + $(COMPONENT_SRCS) \ + $(NON_LOCAL_SRCS) \ + + + +# +# Convert the source files to object files with correct folder location. +# +# +C_LANG_OBJECTS = $(addprefix $(BLD_PATH),$(addsuffix .$(OBJ_EXT),$(basename $(filter %.c ,$(SOURCES) ) ) ) ) +CPP_LANG_OBJECTS = $(addprefix $(BLD_PATH),$(addsuffix .$(OBJ_EXT),$(basename $(filter %.cpp %.cc %.cxx,$(SOURCES) ) ) ) ) + + +# List of all sources to be generated. Can be assembled from the other defintitions. +OBJECTS = \ + $(C_LANG_OBJECTS) \ + $(CPP_LANG_OBJECTS) + + + +# All headers that are dependencies. Wildcard is easy to pickup local headers. +# This is only to automate the rebuilding, all builds on the servers are cleans +# So this is not a huge deal when building on a component level. +HEADERS = \ + $(wildcard *.h) \ + $(wildcard $(PRJ_ROOT)../$(COMPONENT_NAME)/inc/*.h) \ + + +LIBRARIES = \ + $(COMPONENT_LIB) \ + + +# Make targets +# Standard +all: banner module_dirs subdirs local library binary + +debug: + $(MAKE) TARGET=arm DEBUG=TRUE all + +base: banner module_dirs subdirs local + +# Standard Building of Source Files (Default builds for all objects defined above) +$(C_LANG_OBJECTS): $(SOURCES) $(HEADERS) + $(CC_CMD) + +$(CPP_LANG_OBJECTS): $(SOURCES) $(HEADERS) + $(CPP_CMD) + +local: $(OBJECTS) + +# Defines specific for each deliverable + +# For a static library +$(COMPONENT_LIB): $(OBJECTS) +ifdef DYNAMIC +# For a dynamic library + $(SLIB_CMD) + $(HIDE_ECHO_FLAG)$(OBJCPY) --only-keep-debug $(@) $(@).debug + $(HIDE_ECHO_FLAG)$(OBJCPY) --strip-all $(@) + $(HIDE_ECHO_FLAG)$(OBJCPY) --add-gnu-debuglink=$(@).debug $(@) +else +# For a static library + $(AR_CMD) +endif + +# Standard set of derived targets +library: base \ + $(LIBRARIES) + @echo "***** `date` Done building library: $(COMPONENT_NAME) ******" + +binary: base \ + $(BINARIES) + +# Subdirs should be to jump to subdirectories +# standard form is of +# $(MAKE) -C subdirectory_name $(MAKECMDGOALS) +subdirs: + +clean: + -rm -f $(BINARIES) + -rm -f $(LIBRARIES) + -rm -f $(OBJECTS) + -rm -f $(COMPONENT_LIB).map + -rm -f $(COMPONENT_LIB).debug + +-v: + @echo "objs: --> $(OBJECTS)" + @echo "sources: --> $(SOURCES)" + @echo "headers: --> $(HEADERS)" + @echo "includes: --> $(INCLUDES)" + @echo "lib: --> $(LIBRARIES)" + @echo "bin: --> $(BINARIES)" + + +module_dirs: build_dirs diff --git a/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_shared_mem_if.cpp b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_shared_mem_if.cpp new file mode 100755 index 0000000..e60a404 --- /dev/null +++ b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_shared_mem_if.cpp @@ -0,0 +1,254 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NSSharedMemory +/// \brief SharedMemIf implementation +/// +/// Implements interface functions to SharedMem service +////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_shared_mem_if.h> ///< Shared mem source interface file +#include <native_service/ns_logger_if.h> + +#include <sys/mman.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <time.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> + +#define NS_SHM_HEADER_SIZE (48) + +///////////////////////////////////////////////////////////////////////////// +/// Utility functions and classes + +static TMemID getclock(void) { + struct timespec tp; + if (clock_gettime(CLOCK_MONOTONIC_RAW, &tp) < 0) { // LCOV_EXCL_BR_LINE 5: clock_gettime's error case. + return 0; + } + return (TMemID)(((uint64_t)tp.tv_sec * 1000 * 1000 * 1000 + tp.tv_nsec) >> 8); +} + +static size_t uitoa(unsigned int value, char *buf) { + static const char c[] = "0123456789abcdef"; + int i; + + if (buf == NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "buf is NULL"); + return 0; + } + for (i = 0; i < 8; i++) { + buf[i] = c[(value >> ((7 - i) * 4)) & 0xf]; + } + buf[i] = '\0'; + + return i; +} + +static inline void create_shmname(char *buf, TMemID id) { + if (buf == NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "buf is NULL"); + return; + } + strcpy(buf, "nsshm_"); // NOLINT (readability/nolint) + buf += sizeof("nsshm_") - 1; + uitoa(id, buf); +} + +////////////////////////////////////////////////////////////////////////////////////////////////// +// Public interface function definitions + +TMemID SetDataToShared(const void *data, UI_32 dataBytes, const char *from, const char *to) { + TMemID id; + char shmname[16]; + int fd; + void *addr = MAP_FAILED; + + if (data == NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "data is NULL"); + return BAD_MEM_ID; + } + if (from == NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "from is NULL"); + return BAD_MEM_ID; + } + if (to == NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "to is NULL"); + return BAD_MEM_ID; + } + + if (dataBytes == 0) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "DataSize is invalied(%d)", dataBytes); + return BAD_MEM_ID; + } + + while (1) { + id = getclock(); + create_shmname(shmname, id); + + if ((fd = shm_open(shmname, O_CREAT | O_EXCL | O_RDWR, 0666)) < 0) { + if (errno == EEXIST) { + continue; + } + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "shm_open: %s", strerror(errno)); + return BAD_MEM_ID; + } + break; + } + + if (ftruncate(fd, dataBytes + NS_SHM_HEADER_SIZE) < 0) { // LCOV_EXCL_BR_LINE 5: ftruncate's error case. + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "ftruncate: %s", strerror(errno)); + id = BAD_MEM_ID; + goto exit; + } + + if ((addr = mmap(NULL, dataBytes + NS_SHM_HEADER_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "mmap: %s", strerror(errno)); + id = BAD_MEM_ID; + goto exit; + } + + strcpy(reinterpret_cast<char *>(addr), from); // NOLINT (readability/nolint) + strcpy(reinterpret_cast<char *>(addr) + (NS_SHM_HEADER_SIZE / 2), to); // NOLINT (readability/nolint) + /** + * @todo + * Specifying a large number (4097 or higher) for the Session Data Size for transmission results in a segmentation fault. + */ + memcpy(reinterpret_cast<char *>(addr) + NS_SHM_HEADER_SIZE, data, dataBytes); + +exit: + close(fd); + if (addr != MAP_FAILED) { + munmap(addr, dataBytes + NS_SHM_HEADER_SIZE); + } + if (id == BAD_MEM_ID) { + shm_unlink(shmname); + } + return id; +} + +EFrameworkunifiedStatus GetDataFromShared(TMemID id, void *data, UI_32 dataMaxBytes) { + EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail; + char shmname[16]; + int fd; + void *addr = MAP_FAILED; + struct stat sb; + + if (id == BAD_MEM_ID) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "ShMemID is invalied(%u)", id); + return eFrameworkunifiedStatusInvldParam; + } + if (data == NULL) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "data is NULL"); + return eFrameworkunifiedStatusInvldParam; + } + if (dataMaxBytes == 0) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "DataSize is invalied(%d)", dataMaxBytes); + return eFrameworkunifiedStatusInvldParam; + } + + create_shmname(shmname, id); + + if ((fd = shm_open(shmname, O_RDONLY, 0)) < 0) { + if (errno == ENOENT) { + eStatus = eFrameworkunifiedStatusInvldID; + } + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "shm_open: %s", strerror(errno)); + return eStatus; + } + + if (fstat(fd, &sb) < 0) { // LCOV_EXCL_BR_LINE 5: fstat's error case + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "fstat: %s", strerror(errno)); + goto exit; + } + if (sb.st_size - NS_SHM_HEADER_SIZE > static_cast<SI_64>(dataMaxBytes)) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "DataSize is invalied(%d-%d)", dataMaxBytes, (int)sb.st_size); + eStatus = eFrameworkunifiedStatusInvldParam; + goto exit; + } + + if ((addr = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) { // LCOV_EXCL_BR_LINE 5: mmap's error case. + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "mmap: %s", strerror(errno)); // LCOV_EXCL_LINE 5: mmap's error case. + goto exit; + } + + memcpy(data, reinterpret_cast<char *>(addr) + NS_SHM_HEADER_SIZE, sb.st_size - NS_SHM_HEADER_SIZE); + eStatus = eFrameworkunifiedStatusOK; + +exit: + close(fd); + if (addr != MAP_FAILED) { + munmap(addr, sb.st_size); + } + return eStatus; +} + +UI_32 GetLengthOfDataFromShared(TMemID id) { + UI_32 size = 0; + char shmname[16]; + int fd; + struct stat sb; + + if (id == BAD_MEM_ID) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "ShMemID is invalied(%u)", id); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h" + return 0; + } + + create_shmname(shmname, id); + + if ((fd = shm_open(shmname, O_RDWR, 0)) < 0) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "shm_open: %s", strerror(errno)); + return 0; + } + + if (fstat(fd, &sb) < 0) { // LCOV_EXCL_BR_LINE 5: fstat's error case. + AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "fstat: %s", strerror(errno)); // LCOV_EXCL_LINE 5: mmap's error case. + goto exit; + } + + size = static_cast<UI_32>(sb.st_size - NS_SHM_HEADER_SIZE); + +exit: + close(fd); + return size; +} + +EFrameworkunifiedStatus DiscardDataFromShared(TMemID id) { + EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK; + char shmname[16]; + + if (id == BAD_MEM_ID) { + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "ShMemID is invalied(%u)", id); + return eFrameworkunifiedStatusInvldParam; + } + + create_shmname(shmname, id); + + if (shm_unlink(shmname) < 0) { + if (errno == ENOENT) { + eStatus = eFrameworkunifiedStatusInvldID; + } else { + eStatus = eFrameworkunifiedStatusFail; + } + FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "shm_unlink: %s", strerror(errno)); + } + + return eStatus; +} diff --git a/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem.cpp b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem.cpp new file mode 100755 index 0000000..fe1e9bc --- /dev/null +++ b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem.cpp @@ -0,0 +1,697 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NSSharedMemory +/// \brief This file contains implementation of class CNSSharedMem. +/// This class provides API to open, read, write and close shared memory +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <unistd.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <limits.h> + +#include <native_service/ns_sharedmem.h> +#include <native_service/ns_transmit_log.h> + +#include <string> + +static CNSSharedMem *g_pTransmitLogSharedBuf = NULL; + +#define SHM_HEADER_EXT "Hdr" + +#ifdef __cplusplus +extern "C" { +#endif + + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemTransmitLogOpen +/// Open the shared memory for transmit logging +//////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus NSSharedMemTransmitLogOpen() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL == g_pTransmitLogSharedBuf) { + // Create the instance + g_pTransmitLogSharedBuf = new(std::nothrow) CNSSharedMem(TRANSMIT_LOG_SHAREDMEM_NAME, TRANSMIT_LOG_SHAREDMEM_SIZE); + } + + if (NULL != g_pTransmitLogSharedBuf) { + if (!g_pTransmitLogSharedBuf->IsOpen()) { + // maps the shared memory buffer + l_eStatus = g_pTransmitLogSharedBuf->Open(); + } + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemTransmitLogClose +/// Close the transmit logging shared memory +//////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus NSSharedMemTransmitLogClose() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != g_pTransmitLogSharedBuf) { + // un-map the shared memory object + l_eStatus = g_pTransmitLogSharedBuf->Close(); + + delete g_pTransmitLogSharedBuf; + g_pTransmitLogSharedBuf = NULL; + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemReadTransmitLog +/// Reads transmit log from the shared memory buffer. +//////////////////////////////////////////////////////////////////////////////////////////// +SI_32 NSSharedMemReadTransmitLog(PSTR f_pBuffer, const UI_32 f_uiLength, const BOOL f_bBlock) { + // no. of bytes read + SI_32 l_iReadSize = NS_SHM_ERROR; + + if (NULL != g_pTransmitLogSharedBuf && NULL != f_pBuffer) { + // Writes log data into shared memory buffer + l_iReadSize = g_pTransmitLogSharedBuf->Read(f_pBuffer, f_uiLength, f_bBlock); + } + + return l_iReadSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemWriteTransmitLog +/// Write transmit log into the shared memory buffer. +//////////////////////////////////////////////////////////////////////////////////////////// +SI_32 NSSharedMemWriteTransmitLog(PCSTR f_pBuffer, const UI_32 f_uiLength) { + // no. of bytes read + SI_32 l_iWriteSize = NS_SHM_ERROR; + + if (NULL != g_pTransmitLogSharedBuf && NULL != f_pBuffer) { + // Writes log data into shared memory buffer + l_iWriteSize = g_pTransmitLogSharedBuf->Write(f_pBuffer, f_uiLength); + } + + return l_iWriteSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemDumpTransmitLogToFile +/// Dump transmit log from the shared memory buffer into file. +//////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus NSSharedMemDumpTransmitLogToFile(PCSTR f_pPath, PUI_32 f_puiDumpSize) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail; + + if (NULL != g_pTransmitLogSharedBuf && NULL != f_pPath) { + // Writes log data into shared memory buffer + l_eStatus = g_pTransmitLogSharedBuf->DumpToFile(f_pPath, f_puiDumpSize); + } + + return l_eStatus; +} + +// Used only in nstest_sharedmem. +#if defined(SHM_UNITTEST_ENABLE) +//////////////////////////////////////////////////////////////////////////////////////////// +/// NSSharedMemTransmitLogIsOpen +//////////////////////////////////////////////////////////////////////////////////////////// +BOOL NSSharedMemTransmitLogIsOpen() { + if (g_pTransmitLogSharedBuf != NULL) { + return g_pTransmitLogSharedBuf->IsOpen(); + } else { + return FALSE; + } +} +#endif + +#ifdef __cplusplus +} +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CNSSharedMem +/// Parameterized Constructor of CNSSharedMem class +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMem::CNSSharedMem(const std::string &f_cSharedMemName, const UI_32 f_uiSize): + m_cShmName(f_cSharedMemName), m_uiShmBuffSize(f_uiSize), m_pShmHdr(NULL), m_pShmBuff(NULL) { + m_cShmHdrName = f_cSharedMemName; + m_cShmHdrName.append(SHM_HEADER_EXT); +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CNSSharedMem +/// Constructor of CNSSharedMem class +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMem::CNSSharedMem(): + m_cShmName(""), m_uiShmBuffSize(0), m_pShmHdr(NULL), m_pShmBuff(NULL) { + // do nothing +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// ~CNSSharedMem +/// Destructor of CNSSharedMem class +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMem::~CNSSharedMem() { + if (NULL != m_pShmHdr) { + // un-map the shared memory object + Close(); + + m_pShmHdr = NULL; + } + + // TODO(framework_unified): currently shared memory is not being unlinked, + // we need to find a method where we can unlink the sharedmem + // shm_unlink(m_cShmName.c_str()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Open +/// This function opens and maps the shared memory object. +/// It creates the shared memory if it does not exists. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::Open() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusErrOther; + +// Used only in nstest_sharedmem. +#if defined(SHM_UNITTEST_ENABLE) + if (getenv(NSTEST_FAIL_SHAREDMEM_OPEN) != NULL) { + return l_eStatus; + } +#endif + + if (NULL == m_pShmHdr) { + // Open header shared memory + if (eFrameworkunifiedStatusOK != (l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmHdr), + m_cShmHdrName, sizeof(NSSharedBufferHdr)))) { + if (ENOENT == errno) { // Shared memory is not created yet + errno = EOK; + + // Create shared memory + if (eFrameworkunifiedStatusOK == CreateSMHeader()) { + // Retry to open + l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmHdr), m_cShmHdrName, sizeof(NSSharedBufferHdr)); + } + } + } else { + pthread_mutex_lock(&m_pShmHdr->m_tBufMutex); + + // if shared memory buffer is created with size 0, then set new size in header if any change + if (0 == m_pShmHdr->m_uiShMemSize && 0 < m_uiShmBuffSize) { + m_pShmHdr->m_uiShMemSize = m_uiShmBuffSize; + } else if (0 < m_pShmHdr->m_uiShMemSize && 0 == m_uiShmBuffSize) { + m_uiShmBuffSize = m_pShmHdr->m_uiShMemSize; + } else { + // do nothing + } + + pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex); + } + } + + if (eFrameworkunifiedStatusOK == l_eStatus && 0 != m_uiShmBuffSize) { + if (NULL == m_pShmBuff) { + // Open shared memory data buffer, create if not exists + if (eFrameworkunifiedStatusOK != (l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmBuff), m_cShmName, m_uiShmBuffSize))) { + if (ENOENT == errno) { // Shared memory is not created yet + // Create shared memory + if (eFrameworkunifiedStatusOK == CreateSMDataBuffer()) { + // Retry to open + l_eStatus = MapSM(reinterpret_cast<PVOID *>(&m_pShmBuff), m_cShmName, m_uiShmBuffSize); + } + } + } + } else { + l_eStatus = eFrameworkunifiedStatusErrOther; + } + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// IsOpen +/// This function is used to check whether the shared memory buffer is opened or not. +//////////////////////////////////////////////////////////////////////////////////////////////// +BOOL CNSSharedMem::IsOpen() { + return NULL == m_pShmHdr ? FALSE : TRUE; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Close +/// This function closes the shared memory object. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::Close() { + EFrameworkunifiedStatus l_eStatus1 = UnMapSM(m_pShmHdr, sizeof(NSSharedBufferHdr)); + m_pShmHdr = NULL; + + EFrameworkunifiedStatus l_eStatus2 = eFrameworkunifiedStatusOK; + if (0 != m_uiShmBuffSize) { + l_eStatus2 = UnMapSM(m_pShmBuff, m_uiShmBuffSize); + m_pShmBuff = NULL; + } + /* + * todo + * Even if an error occurs due to eFrameworkunifiedStatusNullPointer when UnMapSM fails, + * the error type cannot be determined by the caller because it is rounded to eFrameworkunifiedStatusFail + * if the API final determination is not OK. + */ + return (eFrameworkunifiedStatusOK != l_eStatus1 || eFrameworkunifiedStatusOK != l_eStatus2) ? eFrameworkunifiedStatusFail : eFrameworkunifiedStatusOK; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Read +/// This function reads data from the shared memory. +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_32 CNSSharedMem::Read(PSTR f_pBuffer, const UI_32 f_uilength, const BOOL f_bBlock) { + SI_32 l_iReadSize = NS_SHM_ERROR; + + if ((NULL != f_pBuffer) && (NULL != m_pShmHdr) && (0 != f_uilength)) { + UI_32 l_uiDataSizeToRead = 0; + + // Remaining buffer size from read pointer to end of the buffer + UI_32 l_uiRemainSize = 0; + + if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) { + if ((TRUE == f_bBlock) && (0 == m_pShmHdr->m_uiUnReadSize)) { + pthread_cond_wait(&m_pShmHdr->m_tCondVar, &m_pShmHdr->m_tBufMutex); + } + + // if shared memory buffer size is changed by some other process, remap the updated buffer size in this process + // shared memory buffer size can only be changed if the initial size is 0. + if (m_uiShmBuffSize != m_pShmHdr->m_uiShMemSize) { + if (eFrameworkunifiedStatusOK == MapSM(reinterpret_cast<PVOID *>(&m_pShmBuff), m_cShmName, m_pShmHdr->m_uiShMemSize)) { + m_uiShmBuffSize = m_pShmHdr->m_uiShMemSize; + } + } + + if (NULL != m_pShmBuff) { + l_uiRemainSize = m_uiShmBuffSize - m_pShmHdr->m_uiReadPtr; + + // Round read data size depending on un-read data size in the buffer + l_uiDataSizeToRead = m_pShmHdr->m_uiUnReadSize < f_uilength ? m_pShmHdr->m_uiUnReadSize : f_uilength; + + if (l_uiRemainSize < l_uiDataSizeToRead) { + // Wrapping read + memcpy(f_pBuffer, m_pShmBuff + m_pShmHdr->m_uiReadPtr, l_uiRemainSize); + memcpy(f_pBuffer + l_uiRemainSize, m_pShmBuff, l_uiDataSizeToRead - l_uiRemainSize); + m_pShmHdr->m_uiReadPtr = l_uiDataSizeToRead - l_uiRemainSize; + } else { + memcpy(f_pBuffer, m_pShmBuff + m_pShmHdr->m_uiReadPtr, l_uiDataSizeToRead); + + m_pShmHdr->m_uiReadPtr += l_uiDataSizeToRead; + + // Read pointer is the end of the buffer + if (m_pShmHdr->m_uiReadPtr == m_uiShmBuffSize) { + m_pShmHdr->m_uiReadPtr = 0; + } + } + + m_pShmHdr->m_uiUnReadSize -= l_uiDataSizeToRead; // Update un-read data size + + l_iReadSize = l_uiDataSizeToRead; + } + + pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex); + } + } + + return l_iReadSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Write +/// This function writes the data into the shared memory. +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_32 CNSSharedMem::Write(PCSTR f_pBuffer, const UI_32 f_uilength) { + SI_32 l_iWriteSize = NS_SHM_ERROR; + + // size available in buffer + UI_32 l_uiRemainSize = 0; + + if (NULL != m_pShmHdr && NULL != m_pShmBuff && NULL != f_pBuffer && f_uilength <= m_uiShmBuffSize) { + if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) { + l_uiRemainSize = m_uiShmBuffSize - m_pShmHdr->m_uiWritePtr; + + // Write data to the buffer + if (l_uiRemainSize < f_uilength) { + // Wrapping write + memcpy(m_pShmBuff + m_pShmHdr->m_uiWritePtr, f_pBuffer, l_uiRemainSize); + memcpy(m_pShmBuff, f_pBuffer + l_uiRemainSize, f_uilength - l_uiRemainSize); + + // Update the write pointer + m_pShmHdr->m_uiWritePtr = f_uilength - l_uiRemainSize; + + // The buffer is full of valid data + m_pShmHdr->m_bIsFull = TRUE; + } else { + memcpy(m_pShmBuff + m_pShmHdr->m_uiWritePtr, f_pBuffer, f_uilength); + + // Update the write pointer + m_pShmHdr->m_uiWritePtr += f_uilength; + + // Write pointer is the end of the buffer + if (m_pShmHdr->m_uiWritePtr == m_uiShmBuffSize) { + m_pShmHdr->m_uiWritePtr = 0; + + // The buffer is full of valid data + m_pShmHdr->m_bIsFull = TRUE; + } + } + + // Update un-read data size + m_pShmHdr->m_uiUnReadSize += f_uilength; + + // Set read pointer to be same as write pointer if write pointer exceeds the read pointer + if (m_uiShmBuffSize < m_pShmHdr->m_uiUnReadSize) { + m_pShmHdr->m_uiReadPtr = m_pShmHdr->m_uiWritePtr; + m_pShmHdr->m_uiUnReadSize = m_uiShmBuffSize; + } + + pthread_cond_signal(&m_pShmHdr->m_tCondVar); + + pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex); + + l_iWriteSize = f_uilength; + } + } + + return l_iWriteSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// DumpToFile +/// This function writes all the data in the buffer into provided file f_pPath. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::DumpToFile(PCSTR f_pPath, PUI_32 f_uiDumpSize) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + ssize_t l_iSize = 0; + + SI_32 fd = -1; + + if (NULL == f_uiDumpSize) { + return eFrameworkunifiedStatusNullPointer; + } + *f_uiDumpSize = 0; + + if (NULL != f_pPath) { + if (NULL != m_pShmHdr) { + // Open file + if (-1 != (fd = open(f_pPath, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRWXU | S_IRWXG | S_IRWXO))) { + if (NULL != m_pShmBuff && 0 != m_uiShmBuffSize) { + if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) { + // Write buffer data to file + if (m_pShmHdr->m_bIsFull) { + // Buffer has full of data (read data from write pointer) + if (-1 != (l_iSize = write(fd, m_pShmBuff + m_pShmHdr->m_uiWritePtr, + m_uiShmBuffSize - m_pShmHdr->m_uiWritePtr))) { + *f_uiDumpSize += static_cast<UI_32>(l_iSize); + } else { + l_eStatus = eFrameworkunifiedStatusErrOther; + } + } + + if (-1 != (l_iSize = write(fd, m_pShmBuff, m_pShmHdr->m_uiWritePtr))) { + *f_uiDumpSize += static_cast<UI_32>(l_iSize); + } + + if (0 != pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex)) { + l_eStatus = eFrameworkunifiedStatusSemUnlockFail; + } + } else { + l_eStatus = eFrameworkunifiedStatusSemLockFail; + } + } else if (NULL == m_pShmBuff && 0 != m_uiShmBuffSize) { + l_eStatus = eFrameworkunifiedStatusFail; + } else { + // do nothing + } + + // Sync the file to force I/O operation completed + fsync(fd); + + close(fd); + } else { + l_eStatus = eFrameworkunifiedStatusFileLoadError; + } + } else { + l_eStatus = eFrameworkunifiedStatusFail; + } + } else { + l_eStatus = eFrameworkunifiedStatusInvldParam; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// GetSize +/// This function returns the number of unread bytes which can be read by Read(). +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_32 CNSSharedMem::GetSize() { + SI_32 l_uiReadSize = NS_SHM_ERROR; + + if (NULL != m_pShmHdr) { + l_uiReadSize = m_pShmHdr->m_uiUnReadSize; + } + + return l_uiReadSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// ClearBuf +/// This function clears the buffer. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::ClearBuf() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmHdr) { + if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) { + // Initialize the r/w pointers + m_pShmHdr->m_uiReadPtr = 0; + m_pShmHdr->m_uiWritePtr = 0; + m_pShmHdr->m_uiUnReadSize = 0; + m_pShmHdr->m_bIsFull = FALSE; + + if (0 != pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex)) { + l_eStatus = eFrameworkunifiedStatusSemUnlockFail; + } + } else { + l_eStatus = eFrameworkunifiedStatusSemLockFail; + } + } else { + l_eStatus = eFrameworkunifiedStatusFail; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// SetReadPtrToWritePtr +/// This function sets the position of read ptr to write ptr in buffer. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::SetReadPtrToWritePtr() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmHdr) { + if (0 == pthread_mutex_lock(&m_pShmHdr->m_tBufMutex)) { // LCOV_EXCL_BR_LINE 5: pthread_mutex_lock's error case + // Initialize the r/w pointers + m_pShmHdr->m_uiReadPtr = m_pShmHdr->m_uiWritePtr; + m_pShmHdr->m_uiUnReadSize = 0; + + if (0 != pthread_mutex_unlock(&m_pShmHdr->m_tBufMutex)) { // LCOV_EXCL_BR_LINE 5: pthread_mutex_unlock's error + l_eStatus = eFrameworkunifiedStatusSemUnlockFail; + } + } else { + l_eStatus = eFrameworkunifiedStatusSemLockFail; + } + } else { + l_eStatus = eFrameworkunifiedStatusFail; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CreateSMHeader +/// This function creates the shared memory object for header. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::CreateSMHeader() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail; + + // file descriptor of shared memory + SI_32 l_siId = -1; + + // shared memory buffer headers + NSSharedBufferHdr *l_pShmHdr = NULL; + + if ((!m_cShmHdrName.empty()) && (m_cShmHdrName.size() <= NAME_MAX)) { + // Try to create shared memory + l_siId = shm_open(m_cShmHdrName.c_str(), O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO); + + if (-1 != l_siId) { + // Set the size of shared memory + if (-1 != ftruncate(l_siId, sizeof(NSSharedBufferHdr))) { + // Map the shared memory + l_pShmHdr = reinterpret_cast<NSSharedBufferHdr *>(mmap(NULL, sizeof(NSSharedBufferHdr), + (PROT_READ | PROT_WRITE), MAP_SHARED, l_siId, 0)); + + if (MAP_FAILED != l_pShmHdr) { + // mutex attribute + pthread_mutexattr_t l_tMtxAttr = {}; + pthread_condattr_t l_tCondAttr = {}; + + // Initialize mutex + pthread_mutexattr_init(&l_tMtxAttr); + pthread_mutexattr_setpshared(&l_tMtxAttr, PTHREAD_PROCESS_SHARED); + pthread_mutex_init(&l_pShmHdr->m_tBufMutex, &l_tMtxAttr); + pthread_mutexattr_destroy(&l_tMtxAttr); + + pthread_condattr_init(&l_tCondAttr); + pthread_condattr_setpshared(&l_tCondAttr, PTHREAD_PROCESS_SHARED); + pthread_cond_init(&l_pShmHdr->m_tCondVar, &l_tCondAttr); + pthread_condattr_destroy(&l_tCondAttr); + + pthread_mutex_lock(&l_pShmHdr->m_tBufMutex); + + // Initialize the r/w pointers + l_pShmHdr->m_uiReadPtr = 0; + l_pShmHdr->m_uiWritePtr = 0; + l_pShmHdr->m_uiUnReadSize = 0; + l_pShmHdr->m_bIsFull = FALSE; + l_pShmHdr->m_uiShMemSize = m_uiShmBuffSize; + + pthread_mutex_unlock(&l_pShmHdr->m_tBufMutex); + + // Once initialized un-map the shared memory + munmap(l_pShmHdr, sizeof(NSSharedBufferHdr)); + + l_eStatus = eFrameworkunifiedStatusOK; + } + } + + close(l_siId); + } else if (EEXIST == errno) { + // Shared memory is already created + l_eStatus = eFrameworkunifiedStatusDuplicate; + } else { + // do nothing + } + } else { + l_eStatus = eFrameworkunifiedStatusInvldParam; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CreateSMDataBuffer +/// This function creates the shared memory object for data buffer. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::CreateSMDataBuffer() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail; + + // file descriptor of shared memory + SI_32 l_siId = -1; + + if ((!m_cShmName.empty()) && (m_cShmName.size() <= NAME_MAX)) { + // Try to create shared memory + l_siId = shm_open(m_cShmName.c_str(), O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO); + + if (-1 != l_siId) { + // Set the size of shared memory + if (-1 != ftruncate(l_siId, m_uiShmBuffSize)) { + l_eStatus = eFrameworkunifiedStatusOK; + } + + close(l_siId); + } else if (EEXIST == errno) { + // Shared memory is already created + l_eStatus = eFrameworkunifiedStatusDuplicate; + } else { + // do nothing + } + } else { + l_eStatus = eFrameworkunifiedStatusInvldParam; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// MapSMHeader +/// This function open and maps the shared memory header in process space. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::MapSM(PVOID *f_pShMem, const std::string &f_cShmName, const UI_32 f_uiShmSize) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail; + + // file descriptor of shared memory + SI_32 l_siId = -1; + + // shared memory buffer headers + PVOID l_pShmBuf = NULL; + + // Open shared memory + l_siId = shm_open(f_cShmName.c_str(), O_RDWR, 0); + + if (-1 != l_siId) { + // Map the shared memory into its memory space + l_pShmBuf = mmap(NULL, f_uiShmSize, (PROT_READ | PROT_WRITE), MAP_SHARED, l_siId, 0); + + if (MAP_FAILED != l_pShmBuf) { + *f_pShMem = l_pShmBuf; + l_eStatus = eFrameworkunifiedStatusOK; + } + + close(l_siId); + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// UnMapSM +/// This function unmaps the shared memory object. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMem::UnMapSM(PVOID f_pShMem, const UI_32 f_uiShmSize) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + // Un-map the shared memory + if (NULL != f_pShMem) { + if (0 != munmap(f_pShMem, f_uiShmSize)) { + l_eStatus = eFrameworkunifiedStatusFail; + } + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} diff --git a/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem_reader.cpp b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem_reader.cpp new file mode 100755 index 0000000..79f9f54 --- /dev/null +++ b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem_reader.cpp @@ -0,0 +1,169 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NSSharedMemory +/// \brief This file contains implementation of class CNSSharedMemReader. +/// This class provides API to open, close and perform read operation on shared memory. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_sharedmem.h> +#include <native_service/ns_sharedmem_reader.h> +#include <string> + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CNSSharedMemReader +/// Constructor of CNSSharedMemReader class +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMemReader::CNSSharedMemReader(): m_pShmReader(NULL), m_bBlock(TRUE) { +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CNSSharedMemReader +/// Parameterized Constructor of CNSSharedMemReader class +/// This creates the shared memory object. +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMemReader::CNSSharedMemReader(const std::string &f_cSharedMemName, const BOOL f_bBlock): m_bBlock(f_bBlock) { + m_pShmReader = new(std::nothrow) CNSSharedMem(f_cSharedMemName, 0); +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// ~CNSSharedMemReader +/// Destructor of CNSSharedMemReader class. +/// Closes the shared memory, if it is open. +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMemReader::~CNSSharedMemReader() { + if (NULL != m_pShmReader) { + if (m_pShmReader->IsOpen()) { + m_pShmReader->Close(); + } + + delete m_pShmReader; + m_pShmReader = NULL; + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Open +/// This function opens and maps the shared memory object. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMemReader::Open() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmReader) { + l_eStatus = m_pShmReader->Open(); + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// IsOpen +/// This function is used to check whether the shared memory buffer is opened or not. +//////////////////////////////////////////////////////////////////////////////////////////////// +BOOL CNSSharedMemReader::IsOpen() { + BOOL l_bOpen = FALSE; + + if (NULL != m_pShmReader) { + l_bOpen = m_pShmReader->IsOpen(); + } + + return l_bOpen; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Close +/// This function unmaps the shared memory object. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMemReader::Close() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmReader) { + l_eStatus = m_pShmReader->Close(); + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Read +/// This function reads data from the shared memory. +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_32 CNSSharedMemReader::Read(PSTR buffer, const UI_32 f_uilength) { + SI_32 l_iReadSize = NS_SHM_ERROR; + + if (NULL != m_pShmReader) { + l_iReadSize = m_pShmReader->Read(buffer, f_uilength, m_bBlock); + } + + return l_iReadSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// DumpToFile +/// This function writes all the data in the buffer into provided file f_pPath. +/// This function does not changes the unread buffer. +/// This function overwrites the file if it exists. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMemReader::DumpToFile(PCSTR f_pPath, PUI_32 f_uiDumpSize) { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmReader) { + l_eStatus = m_pShmReader->DumpToFile(f_pPath, f_uiDumpSize); + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// GetSize +/// This function returns the number of unread bytes which can be read by Read(). +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_32 CNSSharedMemReader::GetSize() { + UI_32 l_uiReadSize = NS_SHM_ERROR; + + if (NULL != m_pShmReader) { + l_uiReadSize = m_pShmReader->GetSize(); + } + + return l_uiReadSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// SetReadPtrToWritePtr +/// This function sets the position of read ptr to write ptr in buffer. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMemReader::SetReadPtrToWritePtr() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmReader) { + l_eStatus = m_pShmReader->SetReadPtrToWritePtr(); + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} diff --git a/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem_writer.cpp b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem_writer.cpp new file mode 100755 index 0000000..2561415 --- /dev/null +++ b/nsframework/framework_unified/client/NS_SharedMemIf/src/ns_sharedmem_writer.cpp @@ -0,0 +1,137 @@ +/* + * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. + * + * 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. + */ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// \ingroup tag_NSSharedMemory +/// \brief This file contains implementation of class CNSSharedMemReader. +/// This class provides API to open, close and perform read operation on shared memory. +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Include Files +//////////////////////////////////////////////////////////////////////////////////////////////////// +#include <native_service/ns_sharedmem.h> +#include <native_service/ns_sharedmem_writer.h> +#include <string> + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CNSSharedMemWriter +/// Constructor of CNSSharedMemWriter class +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMemWriter::CNSSharedMemWriter(): m_pShmWriter(NULL) { +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// CNSSharedMemWriter +/// Parameterized Constructor of CNSSharedMemWriter class +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMemWriter::CNSSharedMemWriter(const std::string &f_cSharedMemName, const UI_32 f_uiSize) { + m_pShmWriter = new(std::nothrow) CNSSharedMem(f_cSharedMemName, f_uiSize); +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// ~CNSSharedMemWriter +/// Destructor of CNSSharedMemWriter class. +/// Closes the shared memory, if it is open. +//////////////////////////////////////////////////////////////////////////////////////////////// +CNSSharedMemWriter::~CNSSharedMemWriter() { + if (NULL != m_pShmWriter) { + if (m_pShmWriter->IsOpen()) { + m_pShmWriter->Close(); + } + + delete m_pShmWriter; + m_pShmWriter = NULL; + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Open +/// This function opens and maps the shared memory object. +/// It creates the shared memory if it does not exists. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMemWriter::Open() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmWriter) { + l_eStatus = m_pShmWriter->Open(); + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// IsOpen +/// This function is used to check whether the shared memory buffer is opened or not. +//////////////////////////////////////////////////////////////////////////////////////////////// +BOOL CNSSharedMemWriter::IsOpen() { + BOOL l_bOpen = FALSE; + + if (NULL != m_pShmWriter) { + l_bOpen = m_pShmWriter->IsOpen(); + } + + return l_bOpen; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Close +/// This function unmaps the shared memory object. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMemWriter::Close() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmWriter) { + l_eStatus = m_pShmWriter->Close(); + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// Write +/// This function writes the data into the shared memory. +//////////////////////////////////////////////////////////////////////////////////////////////// +SI_32 CNSSharedMemWriter::Write(PCSTR buffer, const UI_32 f_uilength) { + SI_32 l_iWriteSize = NS_SHM_ERROR; + + if ((NULL != m_pShmWriter) && (NULL != buffer) && (0 != f_uilength)) { + l_iWriteSize = m_pShmWriter->Write(buffer, f_uilength); + } + + return l_iWriteSize; +} + +//////////////////////////////////////////////////////////////////////////////////////////////// +/// ClearBuf +/// This function clears the shared memory buffer. +//////////////////////////////////////////////////////////////////////////////////////////////// +EFrameworkunifiedStatus CNSSharedMemWriter::ClearBuf() { + EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK; + + if (NULL != m_pShmWriter) { + l_eStatus = m_pShmWriter->ClearBuf(); + } else { + l_eStatus = eFrameworkunifiedStatusNullPointer; + } + + return l_eStatus; +} |