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_SystemManagerIf
19 /// \brief This file provides support for the System Manager client
20 /// heartbeat service interface.
22 ///////////////////////////////////////////////////////////////////////////////
24 #include <native_service/frameworkunified_application.h>
25 #include <native_service/frameworkunified_framework_if.h>
26 #include <native_service/frameworkunified_types.h>
27 #include <native_service/frameworkunified_framework_types.h>
29 #include "system_service/ss_heartbeat_service_protocol.h"
30 #include "system_service/ss_heartbeat_notifications.h"
31 #include "system_service/ss_heartbeat_if.h"
32 #include "system_service/ss_services.h"
33 #include "system_service/ss_sm_thread_names.h"
34 #include "system_service/ss_templates.h"
35 #include "ss_system_if_interfaceunifiedlog.h"
37 CHeartBeatServiceIf * pHeartBeatServiceIf = NULL;
39 template <typename C, eFrameworkunifiedStatus(C::*M)(HANDLE)>
41 // LCOV_EXCL_START 6:Because process initialization
42 EFrameworkunifiedStatus HeartBeatIfCallback(HANDLE hApp) {
43 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusNullPointer;
44 C * pObj = static_cast<C *>(pHeartBeatServiceIf);
46 l_eStatus = (pObj->*M)(hApp);
52 /*****************************************************************************
53 @ingroup: SS_SystemManager
54 @brief: InterfaceunifiedSystemConnectToHeartBeatService
55 @note: . Called from framework for every app to start connection to HeartBeat
56 @param HANDLE - Handle to message queue of HeartBeat Service.
57 @return EFrameworkunifiedStatus OK or Fail
58 *****************************************************************************/
59 EFrameworkunifiedStatus InterfaceunifiedSystemConnectToHeartBeatService(HANDLE hApp) {
60 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
61 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
62 /// Avoid opening a session to the HeartBeat
63 /// service Since it uses the framework as well
64 /// Avoid opening a session to System Manager For testing
65 // TODO(my_username): Remove system manager check
66 if ((0 != strcmp(SERVICE_SYSMANAGER, FrameworkunifiedGetAppName(hApp)))) {
67 FrameworkunifiedProtocolCallbackHandler g_aryHeartBeat_Protocol_Cbs[] = {
68 // Command ID, Call back functions
69 { SS_HEARTBEAT_REQUEST, HeartBeatIfCallback<CHeartBeatServiceIf, &CHeartBeatServiceIf::OnHeartBeatRequest>}
70 }; // LCOV_EXCL_BR_LINE 11:Unexpected branch
72 /// Attach the valid callback for this service
73 if ( eFrameworkunifiedStatusOK != // LCOV_EXCL_BR_LINE 6:Because process initialization
74 (l_eStatus = FrameworkunifiedAttachCallbacksToDispatcher(
77 g_aryHeartBeat_Protocol_Cbs,
78 _countof(g_aryHeartBeat_Protocol_Cbs)))) {
79 LOG_ERROR("FrameworkunifiedAttachCallbacksToDispatcher()");
81 // LCOV_EXCL_BR_LINE 5:Because constructor
83 new(std::nothrow) CHeartBeatServiceIf(FALSE, NULL, FrameworkunifiedGetAppName(hApp));
84 } // LCOV_EXCL_BR_LINE 11:Unexpected branch
87 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Heartbeat service name duplicated.");
90 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
92 return l_eStatus; // LCOV_EXCL_BR_LINE 11:Unexpected branch
95 /*****************************************************************************
96 @ingroup: SS_SystemManager
97 @brief: CHeartBeatServiceIf constructor
99 *****************************************************************************/
100 CHeartBeatServiceIf::CHeartBeatServiceIf(BOOL avail, HANDLE service, PCSTR name) {
101 g_tHeartBeatSession.szServiceName = name; // LCOV_EXCL_BR_LINE 11:Unexpected branch
102 g_tHeartBeatSession.fAvailable = avail;
103 g_tHeartBeatSession.hService = service;
106 // LCOV_EXCL_START 10:Because destructor
107 /*****************************************************************************
108 @ingroup: SS_SystemManager
109 @brief: CHeartBeatServiceIf destructor
110 *****************************************************************************/
111 CHeartBeatServiceIf::~CHeartBeatServiceIf() {
112 if (pHeartBeatServiceIf != NULL) {
113 delete pHeartBeatServiceIf;
114 pHeartBeatServiceIf = NULL;
119 // LCOV_EXCL_START 6:Because process initialization
120 /*****************************************************************************
121 @ingroup: SS_SystemManager
122 @brief: OnHeartBeatRequest
124 @param HANDLE - Handle to message queue of HeartBeat Service.
125 @return EFrameworkunifiedStatus OK or Fail
126 *****************************************************************************/
127 EFrameworkunifiedStatus CHeartBeatServiceIf::OnHeartBeatRequest(HANDLE hApp) {
128 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
131 if (g_tHeartBeatSession.hService == NULL) {
132 if ( NULL == (g_tHeartBeatSession.hService = FrameworkunifiedMcOpenSender(hApp, SS_SMHeartbeat)) ) {
133 l_eStatus = eFrameworkunifiedStatusNullPointer;
134 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
135 " Error: FrameworkunifiedMcOpenSender(%s) returned NULL",
141 if ( (0 == strcmp(SS_SMHeartbeat, FrameworkunifiedGetMsgSrc(hApp))) ) {
142 if (NULL != g_tHeartBeatSession.hService) {
143 l_availability = FrameworkunifiedGetSelfAvailability(hApp);
145 if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedSendMsg(g_tHeartBeatSession.hService, SS_HEARTBEAT_RESPONSE,
146 sizeof(l_availability), &l_availability))) {
147 LOG_ERROR("FrameworkunifiedSendMsg");
150 l_eStatus = eFrameworkunifiedStatusInvldHandle;
151 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Invalid handle[_g_hHeartBeatSessionHandle = :NULL]");