summaryrefslogtreecommitdiffstats
path: root/systemservice/system_manager/server/src/heartbeat/ss_hb_thread.cpp
blob: 19d124ae67183107d2e1cac79fbe5c0e185f1770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

///////////////////////////////////////////////////////////////////////////////
/// \ingroup  tag_SystemManager
/// \brief    This file provides support for the application heartbeat system.
///
///////////////////////////////////////////////////////////////////////////////
#include <native_service/frameworkunified_application.h>
#include <native_service/frameworkunified_framework_if.h>
#include <native_service/frameworkunified_service_protocol.h>
#include <system_service/ss_heartbeat_notifications.h>
#include <system_service/ss_heartbeat_service_protocol.h>
#include <system_service/ss_power_service_notifications.h>
#include <system_service/ss_services.h>
#include <system_service/ss_templates.h>

#include "ss_hb_thread.h"
#include "ss_sm_systemmanagerlog.h"
#include "ss_system_manager.h"

/// Total Timers used by Heart Beat Thread
/// This value is used for creating timers
#define HBMaxTimers       1
#define HBTimerInterval   1

template<typename C, eFrameworkunifiedStatus (C::*M)(HANDLE)> EFrameworkunifiedStatus HBThreadCallback(HANDLE hThread) {
  EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
  C * pObj = static_cast<C *>(FrameworkunifiedGetThreadSpecificData(hThread));
  if (pObj) {  // LCOV_EXCL_BR_LINE 4:pObj must not be NULL
    eStatus = (pObj->*M)(hThread);
  }
  return eStatus;
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: CHeartBeatThread
 @note: Constructor
*****************************************************************************/
CHeartBeatThread::CHeartBeatThread(HANDLE f_hThread) :
      m_oTimerCtrl(HBMaxTimers)
    , m_HBTimerID(0)
    , m_NextChkIndex(0)
    , m_hThread(f_hThread) {  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
  std::memset(&m_HBConfigParams, 0, sizeof(m_HBConfigParams));
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: ~CHeartBeatThread
 @note: Destructor
*****************************************************************************/
CHeartBeatThread::~CHeartBeatThread() {  // LCOV_EXCL_START 14: Resident process, not called by NSFW
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
}
// LCOV_EXCL_STOP

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HBPublishAvailabilityStatus
 @note: .
 @param HANDLE  - Handle to message queue of HeartBeat Service.
 @param BOOL    - The Availability Status to be published
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HBPublishAvailabilityStatus(HANDLE hThread, BOOL f_bAvailabiltyStatus) {// LCOV_EXCL_START 14: Resident process, not called by NSFW
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  // Send availability notification to users
  l_eStatus = FrameworkunifiedPublishServiceAvailability(hThread, f_bAvailabiltyStatus);
  char l_cBuf[100] = { 0 };
  snprintf(l_cBuf,
           sizeof(l_cBuf),
           "FrameworkunifiedPublishServiceAvailability(%s)",
           GetStr(f_bAvailabiltyStatus).c_str());
  LOG_STATUS(l_eStatus, l_cBuf);  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h  // NOLINT(whitespace/line_length)

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}// LCOV_EXCL_STOP

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HBOnStartThread
 @note: .
 @param HANDLE  - Handle to message queue of HeartBeat Service.
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HBOnStartThread(HANDLE hThread) {
  EFrameworkunifiedStatus l_eStatus;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  UI_32 l_DataLen = FrameworkunifiedGetMsgLength(hThread);  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)

  if ((sizeof(UI_32) + (SS_MAX_NUM_MODULES * SS_SM_HB_MAX_PROC_NAME_SIZE) + sizeof(m_HBConfigParams)) < l_DataLen) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           " Error: message buffer sizes invalid:received %d", l_DataLen);
    return eFrameworkunifiedStatusInvldParam;
  }

  CHAR l_Data[l_DataLen];  // NOLINT
  // LCOV_EXCL_BR_START 4: NSFW error case.
  if (eFrameworkunifiedStatusOK !=
      (l_eStatus = FrameworkunifiedGetMsgDataOfSize(hThread, (PVOID) & l_Data, static_cast<UI_32>(sizeof(l_Data)), eSMRRelease))) {
  // LCOV_EXCL_BR_STOP
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
            " Error: FrameworkunifiedGetMsgDataOfSize() errored: 0x%x", l_eStatus);
    return l_eStatus;
  }

  CHAR* p_Data = &l_Data[0];
  UI_32 list_num;
  memcpy(&m_HBConfigParams, p_Data, sizeof(m_HBConfigParams));
  p_Data = p_Data + sizeof(m_HBConfigParams);
  memcpy(&list_num, p_Data, sizeof(list_num));
  p_Data = p_Data + sizeof((list_num));

  for (UI_32 i = 0; i < list_num; i++) {
    SubscriberName l_Subscriber = p_Data;  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
    // LCOV_EXCL_BR_START 4: NSFW error case.
    if (eFrameworkunifiedStatusOK != m_oSessionHandler.HBEntrySubscriber(l_Subscriber)) {
    // LCOV_EXCL_BR_STOP
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: HBEntrySubscriber");
    }
    p_Data = p_Data + SS_SM_HB_MAX_PROC_NAME_SIZE;
  }

  if (0 == m_HBConfigParams.ApplicationHeartBeatIntervalInitial) {
    FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__,
         " Warning: Heart beat disabled via configuration file interval "
         "parameter set to 0.");
  } else if (0 != m_HBTimerID) {
    FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__,
        " Warning: Heart Beat Timer ID '%d' already created, ignoring "
        "repeated Heart Beat Start command.", m_HBTimerID);
  } else {
    // LCOV_EXCL_BR_START 4: NSFW error case.
    if (eFrameworkunifiedStatusOK !=
        (l_eStatus = FrameworkunifiedRegisterServiceAvailabilityNotification(hThread, NTFY_HeartBeatAvailability))) {
    // LCOV_EXCL_BR_STOP
      // LCOV_EXCL_START 4: NSFW error case.
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      LOG_ERROR("FrameworkunifiedRegisterServiceAvailabilityNotification(" NTFY_HeartBeatAvailability ")");
      // LCOV_EXCL_STOP
    }
    else {
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
          "Service availability %s Registered SOURCE : %s",
          NTFY_HeartBeatAvailability,
          FrameworkunifiedGetAppName(hThread));

      // Publish Heart Beat Availability
      // LCOV_EXCL_BR_START 4: NSFW error case.
      if (eFrameworkunifiedStatusOK != (l_eStatus = HBPublishAvailabilityStatus(hThread, TRUE))) {
      // LCOV_EXCL_BR_STOP
        // LCOV_EXCL_START 4: NSFW error case.
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        LOG_ERROR("HBPublishAvailabilityStatus(TRUE)");
        // LCOV_EXCL_STOP
      }
    }
    // Create Heart Beat app monitor
    m_HBTimerID = m_oTimerCtrl.CreateTimer(HBThreadCallback<CHeartBeatThread, &CHeartBeatThread::HBOnTimerExpiry>);  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
    if (0 == m_HBTimerID) {  // LCOV_EXCL_BR_LINE 8: m_HBTimerID can't be 0
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: Heart Beat Timer ID creation failed.");
      // return eFrameworkunifiedStatusFail;
    } else {
      // Start Heart Beat app monitor
      m_oTimerCtrl.StartTimer(m_HBTimerID,
                              m_HBConfigParams.ApplicationHeartBeatIntervalInitial,
                              0,
                              HBTimerInterval,
                              0);  // LCOV_EXCL_BR_LINE 11:unexpected branch  // NOLINT(whitespace/line_length)
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Initial Heart Beat Timer created for %d seconds interval",
                                      m_HBConfigParams.ApplicationHeartBeatIntervalInitial);
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Repetitive Heart Beat Timer created for %d seconds interval",
              m_HBConfigParams.ApplicationHeartBeatIntervalRepeat);
    }
  }

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HBOnStopThread
 @note: .
 @param HANDLE  - Handle to message queue of HeartBeat Service.
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HBOnStopThread(HANDLE hThread) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  if (0 != m_HBTimerID) {
    // Stop HeartBeat timer
    m_oTimerCtrl.StopTimer(m_HBTimerID);
    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Timer '%d' stopped", m_HBTimerID);
  }
  else {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: 'm_HBTimerID' is '0'; unable to stop timer");
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HeartBeatTimerInit
 @note:
 @param HANDLE  - Handle to message queue of HeartBeat Service.
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HeartBeatTimerInit(HANDLE hThread) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  m_oTimerCtrl.Initialize(hThread);

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HeartBeatTimersDelete
 @note:
 @param void
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HeartBeatTimersDelete() {// LCOV_EXCL_START 14: Resident process, not called by NSFW
  EFrameworkunifiedStatus l_eStatus;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  if (0 == m_HBTimerID) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error: 'm_HBTimerID' is '0'; unable to delete timer");
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  } else {
    // Delete Heart Beat timer
    UI_32 l_TimerID = m_oTimerCtrl.DeleteTimer(m_HBTimerID);
    if (0 == l_TimerID) {
      l_eStatus = eFrameworkunifiedStatusDbRecNotFound;
      LOG_ERROR("m_oTimerCtrl.DeleteTimer(m_HBTimerID)");  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h  // NOLINT(whitespace/line_length)
    } else {
      l_eStatus = eFrameworkunifiedStatusOK;
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " Successful");
    }
    m_HBTimerID = 0;
  }
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}// LCOV_EXCL_STOP

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HBCheckHeartBeatResponses
 @note:  This function checks heartbeat responses and generates a summary
         report for System Manager if one or more modules have failed to
         reply within the configured maximum number of heartbeat retries.
 @param  HANDLE  - Handle to message queue of HeartBeat Service.
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HBCheckHeartBeatResponses(HANDLE hThread) {
  EFrameworkunifiedStatus l_eStatus;
  THbReportData f_tReportData;
  FRAMEWORKUNIFIEDLOG(ZONE_PERIODIC_FUNC, __FUNCTION__, "+");

  l_eStatus = m_oSessionHandler.HBCheckResponses(f_tReportData,
                                                 m_HBConfigParams.MaxHeartBeatRetryCount,
                                                 m_HBConfigParams.ApplicationHeartBeatIntervalRepeat,
                                                 m_NextChkIndex);
  if (l_eStatus == eFrameworkunifiedStatusFail) {  // Send a heartbeat report ONLY if a heartbeat failure is detected.
    // send report to system manager
    l_eStatus = FrameworkunifiedSendParent(hThread,
                              SS_HEARTBEAT_ERROR_DETECTED,
                              sizeof(f_tReportData),
                              &f_tReportData);
    LOG_STATUS_IF_ERRORED(l_eStatus, "FrameworkunifiedSendParent(SS_HEARTBEAT_ERROR_DETECTED)");  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h  // NOLINT(whitespace/line_length)
  }
  FRAMEWORKUNIFIEDLOG0(ZONE_PERIODIC_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HBSendRequest
 @note: .
 @param  HANDLE  - Handle to message queue of HeartBeat Service.
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HBSendRequest(HANDLE hThread) {
  EFrameworkunifiedStatus l_eStatus;

  CALL_AND_LOG_STATUS_IF_ERRORED(  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h  // NOLINT(whitespace/line_length)
    m_oSessionHandler.HBSendRequest(hThread, m_HBConfigParams.ApplicationHeartBeatIntervalRepeat, m_NextChkIndex));  // LCOV_EXCL_BR_LINE 15: marco defined in ss_templates.h  // NOLINT(whitespace/line_length)

  m_NextChkIndex++;
  if (m_HBConfigParams.ApplicationHeartBeatIntervalRepeat <= m_NextChkIndex) {
    m_NextChkIndex = 0;
  }

  return l_eStatus;
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HBOnPrintConnections
 @note: .Prints all active sessions connected to HeartBeat
 @param HANDLE hThread
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HBOnPrintConnections(HANDLE hThread) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  m_oSessionHandler.HBPrintConnection();

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}

/*****************************************************************************
 @ingroup: SS_SystemManager
 @brief: HBOnPrintStack
 @note: . Prints all sessions connected to HeartBeat
 @param HANDLE  - Handle to message queue of HeartBeat Service.
 @return EFrameworkunifiedStatus
*****************************************************************************/
EFrameworkunifiedStatus CHeartBeatThread::HBOnPrintStack(HANDLE hThread) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  m_oSessionHandler.HBPrintStack(m_HBConfigParams.MaxHeartBeatRetryCount);

  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
  return l_eStatus;
}