common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / logger_service / server / src / queue_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    TODO
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "readerWriter/reader/queue_reader.h"
23 #include <string>
24
25 namespace ReaderWriter {
26 CQueueReader::CQueueReader()
27     : m_handle(-1),
28       m_queName("") {  // LCOV_EXCL_BR_LINE 11:except,C++ STL
29 }
30
31 CQueueReader::~CQueueReader() {
32   if (this->IsOpen()) {
33     this->Close();
34   }
35 }
36
37 EFrameworkunifiedStatus CQueueReader::Initialize(CLoggerCfg* f_pLoggerCfg,
38                                     std::string f_name, UI_32 f_maxSize) {
39   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldParam;
40   if (f_pLoggerCfg != NULL) {  // LCOV_EXCL_BR_LINE 6:Because the applicable variable cannot be changed from the external API
41     this->m_pLoggerCfg = f_pLoggerCfg;
42     // Check for invalid name
43     if (f_name.length() != 0) {  // LCOV_EXCL_BR_LINE 6:it can not be 0
44       m_queName = f_name;
45       l_eStatus = this->Open();
46     }
47   }
48   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
49   return (l_eStatus);
50 }
51
52 EFrameworkunifiedStatus CQueueReader::Open(void) {
53   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusInvldParam;
54
55   if (-1 != m_handle) {  // LCOV_EXCL_BR_LINE 6:Because m_handle is always -1
56     // Queue already opened, no action required.
57     l_eStatus = eFrameworkunifiedStatusOK;
58   } else if (m_queName.length() != 0) {  // LCOV_EXCL_BR_LINE 6:Because m_queName is not always an empty string
59     const UI_32 MAX_MESSAGES_STORED_IN_QUEUE = 512;
60     struct mq_attr mqattr;
61     mqattr.mq_flags = 0;
62     mqattr.mq_maxmsg = MAX_MESSAGES_STORED_IN_QUEUE;
63     mqattr.mq_msgsize = MAX_QUEUE_MSG_SIZE;
64
65     this->m_handle = mq_open(m_queName.c_str(), O_RDONLY | O_CREAT, 0666,
66                              &mqattr);
67     if (-1 != m_handle) {  // LCOV_EXCL_BR_LINE 5:mq_open cannot be passed because it cannot be turned mock
68       l_eStatus = eFrameworkunifiedStatusOK;
69     }
70   }
71   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
72   return (l_eStatus);
73 }
74
75 BOOL CQueueReader::IsOpen(void) {
76   return (-1 != m_handle) ? TRUE : FALSE;  // LCOV_EXCL_BR_LINE 6:Because the condition cannot be set
77 }
78
79 EFrameworkunifiedStatus CQueueReader::Read(UI_8* f_data, UI_32 f_length,
80                               SI_32& f_bytesRead) {
81   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
82
83   if (this->m_handle != -1) {  // LCOV_EXCL_BR_LINE 6:Because the condition cannot be set
84     if (f_length <= MAX_QUEUE_MSG_SIZE) {  // LCOV_EXCL_BR_LINE 6:Because the applicable variable cannot be changed from the external API
85       f_bytesRead = static_cast<SI_32>(mq_receive(this->m_handle, reinterpret_cast<char *>(f_data),
86                                (size_t) MAX_QUEUE_MSG_SIZE, NULL));
87
88       if ((0 <= f_bytesRead) && ((UI_32) f_bytesRead <= f_length)) {  // LCOV_EXCL_BR_LINE 6:Because the condition cannot be set
89         l_eStatus = eFrameworkunifiedStatusOK;
90       }
91     }
92   }
93   return (l_eStatus);
94 }
95
96 void CQueueReader::Close(void) {
97   if (this->m_handle != -1) {  // LCOV_EXCL_BR_LINE 200: m_handle is aways not -1
98     (void) mq_close(this->m_handle);
99   }
100 }
101 }  // namespace ReaderWriter