Init basesystem source codes.
[staging/basesystem.git] / nsframework / framework_unified / client / NS_SharedMemIf / src / ns_sharedmem_writer.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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.
21 ///
22 ////////////////////////////////////////////////////////////////////////////////////////////////////
23
24 ////////////////////////////////////////////////////////////////////////////////////////////////////
25 // Include Files
26 ////////////////////////////////////////////////////////////////////////////////////////////////////
27 #include <native_service/ns_sharedmem.h>
28 #include <native_service/ns_sharedmem_writer.h>
29 #include <string>
30
31 ////////////////////////////////////////////////////////////////////////////////////////////////
32 /// CNSSharedMemWriter
33 /// Constructor of CNSSharedMemWriter class
34 ////////////////////////////////////////////////////////////////////////////////////////////////
35 CNSSharedMemWriter::CNSSharedMemWriter(): m_pShmWriter(NULL) {
36 }
37
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);
44 }
45
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();
55     }
56
57     delete m_pShmWriter;
58     m_pShmWriter = NULL;
59   }
60 }
61
62 ////////////////////////////////////////////////////////////////////////////////////////////////
63 /// Open
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;
69
70   if (NULL != m_pShmWriter) {
71     l_eStatus = m_pShmWriter->Open();
72   } else {
73     l_eStatus = eFrameworkunifiedStatusNullPointer;
74   }
75
76   return l_eStatus;
77 }
78
79 ////////////////////////////////////////////////////////////////////////////////////////////////
80 /// IsOpen
81 /// This function is used to check whether the shared memory buffer is opened or not.
82 ////////////////////////////////////////////////////////////////////////////////////////////////
83 BOOL CNSSharedMemWriter::IsOpen() {
84   BOOL l_bOpen = FALSE;
85
86   if (NULL != m_pShmWriter) {
87     l_bOpen = m_pShmWriter->IsOpen();
88   }
89
90   return l_bOpen;
91 }
92
93 ////////////////////////////////////////////////////////////////////////////////////////////////
94 /// Close
95 /// This function unmaps the shared memory object.
96 ////////////////////////////////////////////////////////////////////////////////////////////////
97 EFrameworkunifiedStatus CNSSharedMemWriter::Close() {
98   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
99
100   if (NULL != m_pShmWriter) {
101     l_eStatus = m_pShmWriter->Close();
102   } else {
103     l_eStatus = eFrameworkunifiedStatusNullPointer;
104   }
105
106   return l_eStatus;
107 }
108
109 ////////////////////////////////////////////////////////////////////////////////////////////////
110 /// Write
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;
115
116   if ((NULL != m_pShmWriter) && (NULL != buffer) && (0 != f_uilength)) {
117     l_iWriteSize = m_pShmWriter->Write(buffer, f_uilength);
118   }
119
120   return l_iWriteSize;
121 }
122
123 ////////////////////////////////////////////////////////////////////////////////////////////////
124 /// ClearBuf
125 /// This function clears the shared memory buffer.
126 ////////////////////////////////////////////////////////////////////////////////////////////////
127 EFrameworkunifiedStatus CNSSharedMemWriter::ClearBuf() {
128   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
129
130   if (NULL != m_pShmWriter) {
131     l_eStatus = m_pShmWriter->ClearBuf();
132   } else {
133     l_eStatus = eFrameworkunifiedStatusNullPointer;
134   }
135
136   return l_eStatus;
137 }