2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NSSharedMemory
19 /// \brief This file contains implementation of class CNSSharedMemReader.
20 /// This class provides API to open, close and perform read operation on shared memory.
22 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 ////////////////////////////////////////////////////////////////////////////////////////////////////
27 #include <native_service/ns_sharedmem.h>
28 #include <native_service/ns_sharedmem_writer.h>
31 ////////////////////////////////////////////////////////////////////////////////////////////////
32 /// CNSSharedMemWriter
33 /// Constructor of CNSSharedMemWriter class
34 ////////////////////////////////////////////////////////////////////////////////////////////////
35 CNSSharedMemWriter::CNSSharedMemWriter(): m_pShmWriter(NULL) {
38 ////////////////////////////////////////////////////////////////////////////////////////////////
39 /// CNSSharedMemWriter
40 /// Parameterized Constructor of CNSSharedMemWriter class
41 ////////////////////////////////////////////////////////////////////////////////////////////////
42 CNSSharedMemWriter::CNSSharedMemWriter(const std::string &f_cSharedMemName, const UI_32 f_uiSize) {
43 m_pShmWriter = new(std::nothrow) CNSSharedMem(f_cSharedMemName, f_uiSize);
46 ////////////////////////////////////////////////////////////////////////////////////////////////
47 /// ~CNSSharedMemWriter
48 /// Destructor of CNSSharedMemWriter class.
49 /// Closes the shared memory, if it is open.
50 ////////////////////////////////////////////////////////////////////////////////////////////////
51 CNSSharedMemWriter::~CNSSharedMemWriter() {
52 if (NULL != m_pShmWriter) {
53 if (m_pShmWriter->IsOpen()) {
54 m_pShmWriter->Close();
62 ////////////////////////////////////////////////////////////////////////////////////////////////
64 /// This function opens and maps the shared memory object.
65 /// It creates the shared memory if it does not exists.
66 ////////////////////////////////////////////////////////////////////////////////////////////////
67 EFrameworkunifiedStatus CNSSharedMemWriter::Open() {
68 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
70 if (NULL != m_pShmWriter) {
71 l_eStatus = m_pShmWriter->Open();
73 l_eStatus = eFrameworkunifiedStatusNullPointer;
79 ////////////////////////////////////////////////////////////////////////////////////////////////
81 /// This function is used to check whether the shared memory buffer is opened or not.
82 ////////////////////////////////////////////////////////////////////////////////////////////////
83 BOOL CNSSharedMemWriter::IsOpen() {
86 if (NULL != m_pShmWriter) {
87 l_bOpen = m_pShmWriter->IsOpen();
93 ////////////////////////////////////////////////////////////////////////////////////////////////
95 /// This function unmaps the shared memory object.
96 ////////////////////////////////////////////////////////////////////////////////////////////////
97 EFrameworkunifiedStatus CNSSharedMemWriter::Close() {
98 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
100 if (NULL != m_pShmWriter) {
101 l_eStatus = m_pShmWriter->Close();
103 l_eStatus = eFrameworkunifiedStatusNullPointer;
109 ////////////////////////////////////////////////////////////////////////////////////////////////
111 /// This function writes the data into the shared memory.
112 ////////////////////////////////////////////////////////////////////////////////////////////////
113 SI_32 CNSSharedMemWriter::Write(PCSTR buffer, const UI_32 f_uilength) {
114 SI_32 l_iWriteSize = NS_SHM_ERROR;
116 if ((NULL != m_pShmWriter) && (NULL != buffer) && (0 != f_uilength)) {
117 l_iWriteSize = m_pShmWriter->Write(buffer, f_uilength);
123 ////////////////////////////////////////////////////////////////////////////////////////////////
125 /// This function clears the shared memory buffer.
126 ////////////////////////////////////////////////////////////////////////////////////////////////
127 EFrameworkunifiedStatus CNSSharedMemWriter::ClearBuf() {
128 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
130 if (NULL != m_pShmWriter) {
131 l_eStatus = m_pShmWriter->ClearBuf();
133 l_eStatus = eFrameworkunifiedStatusNullPointer;