Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_SharedMemIf / src / ns_sharedmem_reader.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_reader.h>
29 #include <string>
30
31 ////////////////////////////////////////////////////////////////////////////////////////////////
32 /// CNSSharedMemReader
33 /// Constructor of CNSSharedMemReader class
34 ////////////////////////////////////////////////////////////////////////////////////////////////
35 CNSSharedMemReader::CNSSharedMemReader(): m_pShmReader(NULL), m_bBlock(TRUE) {
36 }
37
38 ////////////////////////////////////////////////////////////////////////////////////////////////
39 /// CNSSharedMemReader
40 /// Parameterized Constructor of CNSSharedMemReader class
41 /// This creates the shared memory object.
42 ////////////////////////////////////////////////////////////////////////////////////////////////
43 CNSSharedMemReader::CNSSharedMemReader(const std::string &f_cSharedMemName, const BOOL f_bBlock): m_bBlock(f_bBlock) {
44   m_pShmReader = new(std::nothrow) CNSSharedMem(f_cSharedMemName, 0);
45 }
46
47 ////////////////////////////////////////////////////////////////////////////////////////////////
48 /// ~CNSSharedMemReader
49 /// Destructor of CNSSharedMemReader class.
50 /// Closes the shared memory, if it is open.
51 ////////////////////////////////////////////////////////////////////////////////////////////////
52 CNSSharedMemReader::~CNSSharedMemReader() {
53   if (NULL != m_pShmReader) {
54     if (m_pShmReader->IsOpen()) {
55       m_pShmReader->Close();
56     }
57
58     delete m_pShmReader;
59     m_pShmReader = NULL;
60   }
61 }
62
63 ////////////////////////////////////////////////////////////////////////////////////////////////
64 /// Open
65 /// This function opens and maps the shared memory object.
66 ////////////////////////////////////////////////////////////////////////////////////////////////
67 EFrameworkunifiedStatus CNSSharedMemReader::Open() {
68   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
69
70   if (NULL != m_pShmReader) {
71     l_eStatus = m_pShmReader->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 CNSSharedMemReader::IsOpen() {
84   BOOL l_bOpen = FALSE;
85
86   if (NULL != m_pShmReader) {
87     l_bOpen = m_pShmReader->IsOpen();
88   }
89
90   return l_bOpen;
91 }
92
93 ////////////////////////////////////////////////////////////////////////////////////////////////
94 /// Close
95 /// This function unmaps the shared memory object.
96 ////////////////////////////////////////////////////////////////////////////////////////////////
97 EFrameworkunifiedStatus CNSSharedMemReader::Close() {
98   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
99
100   if (NULL != m_pShmReader) {
101     l_eStatus = m_pShmReader->Close();
102   } else {
103     l_eStatus = eFrameworkunifiedStatusNullPointer;
104   }
105
106   return l_eStatus;
107 }
108
109 ////////////////////////////////////////////////////////////////////////////////////////////////
110 /// Read
111 /// This function reads data from the shared memory.
112 ////////////////////////////////////////////////////////////////////////////////////////////////
113 SI_32 CNSSharedMemReader::Read(PSTR buffer, const UI_32 f_uilength) {
114   SI_32 l_iReadSize = NS_SHM_ERROR;
115
116   if (NULL != m_pShmReader) {
117     l_iReadSize = m_pShmReader->Read(buffer, f_uilength, m_bBlock);
118   }
119
120   return l_iReadSize;
121 }
122
123 ////////////////////////////////////////////////////////////////////////////////////////////////
124 /// DumpToFile
125 /// This function writes all the data in the buffer into provided file f_pPath.
126 /// This function does not changes the unread buffer.
127 /// This function overwrites the file if it exists.
128 ////////////////////////////////////////////////////////////////////////////////////////////////
129 EFrameworkunifiedStatus CNSSharedMemReader::DumpToFile(PCSTR f_pPath, PUI_32 f_uiDumpSize) {
130   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
131
132   if (NULL != m_pShmReader) {
133     l_eStatus = m_pShmReader->DumpToFile(f_pPath, f_uiDumpSize);
134   } else {
135     l_eStatus = eFrameworkunifiedStatusNullPointer;
136   }
137
138   return l_eStatus;
139 }
140
141 ////////////////////////////////////////////////////////////////////////////////////////////////
142 /// GetSize
143 /// This function returns the number of unread bytes which can be read by Read().
144 ////////////////////////////////////////////////////////////////////////////////////////////////
145 SI_32 CNSSharedMemReader::GetSize() {
146   UI_32 l_uiReadSize = NS_SHM_ERROR;
147
148   if (NULL != m_pShmReader) {
149     l_uiReadSize = m_pShmReader->GetSize();
150   }
151
152   return l_uiReadSize;
153 }
154
155 ////////////////////////////////////////////////////////////////////////////////////////////////
156 /// SetReadPtrToWritePtr
157 /// This function sets the position of read ptr to write ptr in buffer.
158 ////////////////////////////////////////////////////////////////////////////////////////////////
159 EFrameworkunifiedStatus CNSSharedMemReader::SetReadPtrToWritePtr() {
160   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
161
162   if (NULL != m_pShmReader) {
163     l_eStatus = m_pShmReader->SetReadPtrToWritePtr();
164   } else {
165     l_eStatus = eFrameworkunifiedStatusNullPointer;
166   }
167
168   return l_eStatus;
169 }