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_SystemManager
19 /// \brief This file provides support for the application heartbeat system.
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>
31 #include "ss_sm_systemmanagerlog.h"
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);
42 static VOID DeletePObj(CHeartBeatThread * pObj) { // LCOV_EXCL_START 8: dead code
43 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
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()");
55 #define DetachParentCbThreadProtocols(hThread, handler) \
57 EFrameworkunifiedStatus l_eStatus = DetachParentCallbacksFromInterfaceunifiedDispatcher( \
60 static_cast<UI_32>(_countof(handler))); \
61 LOG_STATUS_IF_ERRORED(l_eStatus, "DetachParentCallbacksFromInterfaceunifiedDispatcher()") \
64 #define DetachCbProtocols(hThread, f_pStr, f_protocolHandlers, hSession) \
66 EFrameworkunifiedStatus l_eStatus = \
67 DetachCallbacksFromInterfaceunifiedDispatcher( \
71 static_cast<UI_32>(_countof(f_protocolHandlers)), \
73 LOG_STATUS_IF_ERRORED(l_eStatus, "DetachCallbacksFromInterfaceunifiedDispatcher()") \
76 /*****************************************************************************/
78 @ingroup: SS_SystemManager
79 @brief: HBThreadStart: function would be called at the time of starting of the thread
81 @param HANDLE - Handle to message queue of HeartBeat Service.
82 @return EFrameworkunifiedStatus
84 /*****************************************************************************/
85 EFrameworkunifiedStatus HBThreadStart(HANDLE hThread) {
86 EFrameworkunifiedStatus l_eStatus;
87 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
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)
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)
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))) {
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))))) {
121 // LCOV_EXCL_START 4: NSFW error case.
122 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
123 LOG_ERROR("FrameworkunifiedAttachParentCallbacksToDispatcher(HBThreadProtocolHandlers)");
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))))) {
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);
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);
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,
156 DetachParentCbThreadProtocols(hThread, HBThreadCommonHandlers);
157 DetachParentCbThreadProtocols(hThread, HBThreadProtocolHandlers);
162 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Successful");
165 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");
169 /*****************************************************************************/
171 @ingroup: SS_SystemManager
172 @brief: HBThreadStop: function would be called at the time of stopping of the thread
174 @param HANDLE - Handle to message queue of HeartBeat Service.
175 @return EFrameworkunifiedStatus
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;
182 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "+");
184 CHeartBeatThread * pObj = static_cast<CHeartBeatThread *>(FrameworkunifiedGetThreadSpecificData(hThread));
186 l_eStatus = eFrameworkunifiedStatusNullPointer;
187 LOG_ERROR("FrameworkunifiedGetThreadSpecificData(hThread)");
190 CALL_AND_LOG_STATUS_IF_ERRORED(pObj->HeartBeatTimersDelete());
191 CALL_AND_LOG_STATUS_IF_ERRORED(pObj->HBPublishAvailabilityStatus(hThread, FALSE));
195 FrameworkunifiedSetThreadSpecificData(hThread, NULL);
196 CALL_AND_LOG_STATUS_IF_ERRORED(FrameworkunifiedDetachServiceFromDispatcher(hThread, FrameworkunifiedGetAppName(hThread)));
198 FRAMEWORKUNIFIEDLOG0(ZONE_FUNC, __FUNCTION__, "-");