common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / interface_unified / library / src / ss_heartbeat_client.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_SystemManagerIf
19 /// \brief    This file provides support for the System Manager client
20 ///           heartbeat service interface.
21 ///
22 ///////////////////////////////////////////////////////////////////////////////
23 #include <string.h>
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>
28 #include <new>
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"
36
37 CHeartBeatServiceIf * pHeartBeatServiceIf = NULL;
38
39 template <typename C, eFrameworkunifiedStatus(C::*M)(HANDLE)>
40
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);
45   if (pObj) {
46     l_eStatus = (pObj->*M)(hApp);
47   }
48   return l_eStatus;
49 }
50 // LCOV_EXCL_STOP
51
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
71
72     /// Attach the valid callback for this service
73     if ( eFrameworkunifiedStatusOK !=  // LCOV_EXCL_BR_LINE 6:Because process initialization
74         (l_eStatus = FrameworkunifiedAttachCallbacksToDispatcher(
75                                     hApp,
76                                     SS_SMHeartbeat,
77                                     g_aryHeartBeat_Protocol_Cbs,
78                                     _countof(g_aryHeartBeat_Protocol_Cbs)))) {
79       LOG_ERROR("FrameworkunifiedAttachCallbacksToDispatcher()");
80     } else {
81       // LCOV_EXCL_BR_LINE 5:Because constructor
82       pHeartBeatServiceIf =
83           new(std::nothrow) CHeartBeatServiceIf(FALSE, NULL, FrameworkunifiedGetAppName(hApp));
84     }  // LCOV_EXCL_BR_LINE 11:Unexpected branch
85
86   } else {
87     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Heartbeat service name duplicated.");
88   }
89
90   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
91
92   return l_eStatus;  // LCOV_EXCL_BR_LINE 11:Unexpected branch
93 }
94
95 /*****************************************************************************
96     @ingroup: SS_SystemManager
97     @brief:  CHeartBeatServiceIf constructor
98     @param  -
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;
104 }
105
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;
115   }
116 }
117 // LCOV_EXCL_STOP
118
119 // LCOV_EXCL_START 6:Because process initialization
120 /*****************************************************************************
121     @ingroup: SS_SystemManager
122     @brief:  OnHeartBeatRequest
123     @note: .
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;
129   BOOL l_availability;
130
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",
136         SS_SMHeartbeat);
137       return l_eStatus;
138     }
139   }
140
141   if ( (0 == strcmp(SS_SMHeartbeat, FrameworkunifiedGetMsgSrc(hApp))) ) {
142     if (NULL != g_tHeartBeatSession.hService) {
143       l_availability = FrameworkunifiedGetSelfAvailability(hApp);
144
145       if (eFrameworkunifiedStatusOK != (l_eStatus = FrameworkunifiedSendMsg(g_tHeartBeatSession.hService, SS_HEARTBEAT_RESPONSE,
146                                                   sizeof(l_availability), &l_availability))) {
147         LOG_ERROR("FrameworkunifiedSendMsg");
148       }
149     } else {
150       l_eStatus = eFrameworkunifiedStatusInvldHandle;
151       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Invalid handle[_g_hHeartBeatSessionHandle = :NULL]");
152     }
153   }
154   return l_eStatus;
155 }
156 // LCOV_EXCL_STOP