common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / logger_service / server / src / mem_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_SS_LoggerService
19 /// \brief    This file supports reading from a shared mem area.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "readerWriter/reader/mem_reader.h"
23 #include <string>
24
25 namespace ReaderWriter {
26
27 CMemReader::CMemReader()
28     : m_pSharedBuf(NULL),
29       m_sharedBufSize(0) {
30 }
31
32 CMemReader::~CMemReader() {
33   this->Close();
34 }
35
36 EFrameworkunifiedStatus CMemReader::Initialize(CLoggerCfg* f_pLoggerCfg, std::string f_name,
37                                   UI_32 f_maxSize) {
38   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
39   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldParam;
40   m_sharedBufSize = f_maxSize;
41   m_pSharedBuf = new (std::nothrow) CNSSharedMemReader(f_name, TRUE);  // LCOV_EXCL_BR_LINE 5:Cannot pass because it cannot be new
42   if (NULL != m_pSharedBuf) {  // LCOV_EXCL_BR_LINE 5:Cannot pass because it cannot be new
43     l_eStatus = this->Open();
44   } else {
45     l_eStatus = eFrameworkunifiedStatusNullPointer;
46   }
47   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
48   return (l_eStatus);
49 }
50
51 EFrameworkunifiedStatus CMemReader::Open(void) {
52   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldParam;
53   if (NULL != m_pSharedBuf) {  // LCOV_EXCL_BR_LINE 5: new will aways sucess, so m_pSharedBuf will noe be null
54     if (!m_pSharedBuf->IsOpen()) {
55       // maps the shared memory buffer
56       l_eStatus = m_pSharedBuf->Open();
57     } else {
58       // Already opened no action required
59       l_eStatus = eFrameworkunifiedStatusOK;
60     }
61   } else {
62     l_eStatus = eFrameworkunifiedStatusNullPointer;
63   }
64   return l_eStatus;
65 }
66
67 BOOL CMemReader::IsOpen(void) {
68   return this->m_pSharedBuf->IsOpen();
69 }
70
71 EFrameworkunifiedStatus CMemReader::Read(UI_8* f_data, UI_32 f_length, SI_32& f_bytesRead) {
72   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
73
74   if (this->m_pSharedBuf != NULL) {
75     if (TRUE != this->m_pSharedBuf->IsOpen()) {
76       l_eStatus = m_pSharedBuf->Open();
77     }
78     if (l_eStatus == eFrameworkunifiedStatusOK) {
79       f_bytesRead = m_pSharedBuf->Read(reinterpret_cast<char *>(f_data), f_length);
80       if (NS_SHM_ERROR != f_bytesRead) {
81         l_eStatus = eFrameworkunifiedStatusOK;
82       } else {
83         l_eStatus = eFrameworkunifiedStatusFail;
84       }
85     }
86
87   } else {
88     l_eStatus = eFrameworkunifiedStatusNullPointer;
89   }
90   return (l_eStatus);
91 }
92
93 EFrameworkunifiedStatus CMemReader::ReadToFile(std::string f_fileName, UI_32& f_Written) {
94   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
95
96   if ((NULL != m_pSharedBuf) &&  // LCOV_EXCL_BR_LINE 5: new will aways sucess, so m_pSharedBuf will noe be null
97       (f_fileName.length() != 0)) {
98     l_eStatus = m_pSharedBuf->DumpToFile(f_fileName.c_str(), &f_Written);
99   }
100
101   return l_eStatus;
102 }
103
104 void CMemReader::Close(void) {
105   if (NULL != m_pSharedBuf) {  // LCOV_EXCL_BR_LINE 6: m_pSharedBuf can not be null
106     // un-map the shared memory object
107     (void) m_pSharedBuf->Close();
108
109     delete m_pSharedBuf;
110     m_pSharedBuf = NULL;
111   }
112 }
113
114 EFrameworkunifiedStatus CMemReader::ResetPosition(void) {
115   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
116   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusNullPointer;
117   if (NULL != m_pSharedBuf) {  // LCOV_EXCL_BR_LINE 6:Because m_pSharedBuf is always NULL
118     // un-map the shared memory object
119     l_eStatus = m_pSharedBuf->SetReadPtrToWritePtr();
120     LOG_STATUS_IF_ERRORED(l_eStatus, "m_pSharedBuf->SetReadPtrToWritePtr();");  // LCOV_EXCL_BR_LINE 15:Macro
121   }
122   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
123   return (l_eStatus);
124 }
125 }  // namespace ReaderWriter