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_SS_LoggerService
19 /// \brief This file supports reading from a shared mem area.
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "readerWriter/reader/mem_reader.h"
25 namespace ReaderWriter {
27 CMemReader::CMemReader()
32 CMemReader::~CMemReader() {
36 EFrameworkunifiedStatus CMemReader::Initialize(CLoggerCfg* f_pLoggerCfg, std::string f_name,
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();
45 l_eStatus = eFrameworkunifiedStatusNullPointer;
47 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
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();
58 // Already opened no action required
59 l_eStatus = eFrameworkunifiedStatusOK;
62 l_eStatus = eFrameworkunifiedStatusNullPointer;
67 BOOL CMemReader::IsOpen(void) {
68 return this->m_pSharedBuf->IsOpen();
71 EFrameworkunifiedStatus CMemReader::Read(UI_8* f_data, UI_32 f_length, SI_32& f_bytesRead) {
72 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
74 if (this->m_pSharedBuf != NULL) {
75 if (TRUE != this->m_pSharedBuf->IsOpen()) {
76 l_eStatus = m_pSharedBuf->Open();
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;
83 l_eStatus = eFrameworkunifiedStatusFail;
88 l_eStatus = eFrameworkunifiedStatusNullPointer;
93 EFrameworkunifiedStatus CMemReader::ReadToFile(std::string f_fileName, UI_32& f_Written) {
94 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
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);
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();
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
122 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
125 } // namespace ReaderWriter