common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / system_manager / server / src / heartbeat / ss_hb_if.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_SystemManager
19 /// \brief    This file provides support for the application heartbeat system.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include <native_service/frameworkunified_application.h>
23 #include <native_service/frameworkunified_framework_if.h>
24 #include <native_service/frameworkunified_service_protocol.h>
25 #include <ss_hb_thread.h>
26 #include <system_service/ss_sm_client_if.h>
27 #include <system_service/ss_heartbeat_notifications.h>
28 #include <system_service/ss_heartbeat_service_protocol.h>
29 #include <system_service/ss_templates.h>
30 #include <new>
31 #include "ss_sm_systemmanagerlog.h"
32
33 template<typename C, eFrameworkunifiedStatus (C::*M)(HANDLE)> EFrameworkunifiedStatus HBThreadCallback(HANDLE hThread) {
34   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusNullPointer;
35   C * pObj = static_cast<C *>(FrameworkunifiedGetThreadSpecificData(hThread));
36   if (pObj) {  // LCOV_EXCL_BR_LINE 4:pObj must not be NULL
37     eStatus = (pObj->*M)(hThread);
38   }
39   return eStatus;
40 }
41
42 static VOID DeletePObj(CHeartBeatThread * pObj) {  // LCOV_EXCL_START 8: dead code
43   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
44   delete pObj;
45 }
46 // LCOV_EXCL_STOP
47
48 static VOID DeleteHBTimer(CHeartBeatThread * pObj) {  // LCOV_EXCL_START 8: dead code
49   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
50   EFrameworkunifiedStatus l_eStatus = pObj->HeartBeatTimersDelete();
51   LOG_STATUS_IF_ERRORED(l_eStatus, "pObj->HeartBeatTimersDelete()");
52 }
53 // LCOV_EXCL_STOP
54
55 #define DetachParentCbThreadProtocols(hThread, handler)                           \
56 {                                                                                 \
57   EFrameworkunifiedStatus l_eStatus = DetachParentCallbacksFromInterfaceunifiedDispatcher(                  \
58                             hThread,                                              \
59                             handler,                                              \
60                             static_cast<UI_32>(_countof(handler)));               \
61   LOG_STATUS_IF_ERRORED(l_eStatus, "DetachParentCallbacksFromInterfaceunifiedDispatcher()")    \
62 }
63
64 #define DetachCbProtocols(hThread, f_pStr, f_protocolHandlers, hSession)          \
65 {                                                                                 \
66   EFrameworkunifiedStatus l_eStatus =                                                          \
67                   DetachCallbacksFromInterfaceunifiedDispatcher(                               \
68                             hThread,                                              \
69                             f_pStr,                                               \
70                             f_protocolHandlers,                                   \
71                             static_cast<UI_32>(_countof(f_protocolHandlers)),     \
72                             hSession);                                            \
73   LOG_STATUS_IF_ERRORED(l_eStatus, "DetachCallbacksFromInterfaceunifiedDispatcher()")          \
74 }
75
76 /*****************************************************************************/
77 /**
78  @ingroup: SS_SystemManager
79  @brief: HBThreadStart: function would be called at the time of starting of the thread
80  @note: .
81  @param HANDLE  - Handle to message queue of HeartBeat Service.
82  @return EFrameworkunifiedStatus
83  */
84 /*****************************************************************************/
85 EFrameworkunifiedStatus HBThreadStart(HANDLE hThread) {
86   EFrameworkunifiedStatus l_eStatus;
87   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
88
89   FrameworkunifiedProtocolCallbackHandler HBThreadCommonHandlers[] = {
90     // Command ID,                       Call back functions
91   { SS_HEARTBEAT_RESPONSE,           HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnClientResponse> },
92   { SS_HEARTBEAT_PRINT_CONNECTIONS,  HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnPrintConnections> },
93   { SS_HEARTBEAT_PRINT_STACK,        HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnPrintStack> } };  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
94
95   FrameworkunifiedProtocolCallbackHandler HBThreadProtocolHandlers[] = {
96     //    Command ID,              Call back functions
97   { SS_HEARTBEAT_START,               HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnStartThread> },
98   { SS_HEARTBEAT_STOP,                HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnStopThread> },
99   { SS_HEARTBEAT_DELETE_MODULE_ENTRY, HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnRemoveModule> },
100   { SS_HEARTBEAT_APPEND_MODULE_ENTRY, HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnAppendModule> },
101   { SS_HEARTBEAT_PERIODIC_STATUS_REQ, HBThreadCallback<CHeartBeatThread,
102                                                                 &CHeartBeatThread::HBOnPeriodicStatusRequest> },
103   { SS_HEARTBEAT_AVAIL_CHECK_REQ,     HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnAvailCheckRequest> }
104   };  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
105
106   CHeartBeatThread * pObj = new (std::nothrow) CHeartBeatThread(hThread);  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
107   if (NULL == pObj) {  // LCOV_EXCL_BR_LINE 5: Standard lib
108     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
109     l_eStatus = eFrameworkunifiedStatusNullPointer;  // LCOV_EXCL_LINE 5: Standard lib
110     LOG_ERROR("new(std::nothrow) CHeartBeatThread(hThread)");  // LCOV_EXCL_LINE 5: Standard lib
111   // LCOV_EXCL_BR_START 6:always return eFrameworkunifiedStatusOK
112   } else if (eFrameworkunifiedStatusOK != (l_eStatus = pObj->HeartBeatTimerInit(hThread))) {
113   // LCOV_EXCL_BR_STOP
114     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
115     LOG_ERROR("pObj->HeartBeatTimerInit(hThread)");  // LCOV_EXCL_LINE 6:always return eFrameworkunifiedStatusOK
116     DeletePObj(pObj);  // LCOV_EXCL_LINE 6:always return eFrameworkunifiedStatusOK
117   // LCOV_EXCL_BR_START 4: NSFW error case.
118   } else if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedAttachParentCallbacksToDispatcher(hThread,
119               HBThreadProtocolHandlers, static_cast<UI_32>(_countof(HBThreadProtocolHandlers))))) {
120   // LCOV_EXCL_BR_STOP
121     // LCOV_EXCL_START 4: NSFW error case.
122     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
123     LOG_ERROR("FrameworkunifiedAttachParentCallbacksToDispatcher(HBThreadProtocolHandlers)");
124     DeleteHBTimer(pObj);
125     DeletePObj(pObj);
126     // LCOV_EXCL_STOP
127   // LCOV_EXCL_BR_START 4: NSFW error case.
128   } else if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedAttachParentCallbacksToDispatcher(hThread,
129                 HBThreadCommonHandlers, static_cast<UI_32>(_countof(HBThreadCommonHandlers))))) {
130   // LCOV_EXCL_BR_STOP
131     // LCOV_EXCL_START 4: NSFW error case.
132     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
133     LOG_ERROR("FrameworkunifiedAttachParentCallbacksToDispatcher(HBThreadCommonHandlers)");
134     DetachParentCbThreadProtocols(hThread, HBThreadProtocolHandlers);
135     DeleteHBTimer(pObj);
136     DeletePObj(pObj);
137     // LCOV_EXCL_STOP
138   } else if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedAttachCallbacksToDispatcher(hThread, FRAMEWORKUNIFIED_ANY_SOURCE, HBThreadCommonHandlers, static_cast<UI_32>(_countof(HBThreadCommonHandlers)), NULL))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT(whitespace/line_length)
139     // LCOV_EXCL_START 4: NSFW error case.
140     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
141     LOG_ERROR("FrameworkunifiedAttachCallbacksToDispatcher("
142             "FRAMEWORKUNIFIED_ANY_SOURCE, HBThreadCommonHandlers)");
143     DetachParentCbThreadProtocols(hThread, HBThreadCommonHandlers);
144     DetachParentCbThreadProtocols(hThread, HBThreadProtocolHandlers);
145     DeleteHBTimer(pObj);
146     DeletePObj(pObj);
147     // LCOV_EXCL_STOP
148   } else {
149     l_eStatus = FrameworkunifiedSetThreadSpecificData(hThread, pObj);  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
150     if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
151       // LCOV_EXCL_START 4: NSFW error case.
152       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
153       LOG_ERROR("FrameworkunifiedSetThreadSpecificData(hThread, pObj)");
154       DetachCbProtocols(hThread, FRAMEWORKUNIFIED_ANY_SOURCE, HBThreadCommonHandlers,
155               NULL);
156       DetachParentCbThreadProtocols(hThread, HBThreadCommonHandlers);
157       DetachParentCbThreadProtocols(hThread, HBThreadProtocolHandlers);
158       DeleteHBTimer(pObj);
159       DeletePObj(pObj);
160       // LCOV_EXCL_STOP
161     } else {
162       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Successful");
163     }
164   }
165   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
166   return l_eStatus;
167 }
168
169 /*****************************************************************************/
170 /**
171  @ingroup: SS_SystemManager
172  @brief: HBThreadStop: function would be called at the time of stopping of the thread
173  @note: .
174  @param HANDLE  - Handle to message queue of HeartBeat Service.
175  @return EFrameworkunifiedStatus
176  */
177 /*****************************************************************************/
178 EFrameworkunifiedStatus HBThreadStop(HANDLE hThread) {  // LCOV_EXCL_START 14: Resident process, not called by NSFW
179   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
180   EFrameworkunifiedStatus l_eStatus;
181
182   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
183
184   CHeartBeatThread * pObj = static_cast<CHeartBeatThread *>(FrameworkunifiedGetThreadSpecificData(hThread));
185   if (NULL == pObj) {
186     l_eStatus = eFrameworkunifiedStatusNullPointer;
187     LOG_ERROR("FrameworkunifiedGetThreadSpecificData(hThread)");
188   } else {
189     // delete timers
190     CALL_AND_LOG_STATUS_IF_ERRORED(pObj->HeartBeatTimersDelete());
191     CALL_AND_LOG_STATUS_IF_ERRORED(pObj->HBPublishAvailabilityStatus(hThread, FALSE));
192     DeletePObj(pObj);
193   }
194
195   FrameworkunifiedSetThreadSpecificData(hThread, NULL);
196   CALL_AND_LOG_STATUS_IF_ERRORED(FrameworkunifiedDetachServiceFromDispatcher(hThread, FrameworkunifiedGetAppName(hThread)));
197
198   FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
199
200   return l_eStatus;
201 }// LCOV_EXCL_STOP