summaryrefslogtreecommitdiffstats
path: root/task_manager/server/src/tskm_port_subsys.cpp
blob: cfdec4901cc2338a5937d7f87d4ade8c6c95e060 (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
/*
 * @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.
 */

#include "tskm_port_subsys.h"
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <native_service/cl_monitor.h>
#include <system_service/ss_sm_client_if.h>
#include <system_service/resm.h>
//#include <stub/ss_diag.h>
#include <string>

#include "agl_thread.h"

#include "tskm_main.h"
#include "tskm_debug.h"
#include "tskm_util.h"
#include "tskm_port_pf.h"


#ifndef oops_name2pid
  #define oops_name2pid(processname) syscall(OOPSTRACE_SYSCALL, 17, processname)
#endif


typedef struct {
  CL_MonitorSearch_t clMonSearch;
  uint32_t resmId;        // Session ID of RESM
} SUB_SYS_CTX_t;

static SUB_SYS_CTX_t g_sub;

/******************************************************************
 *        Subsystem initialization
 ******************************************************************/
int tskm_sub_init(void) {
  int clRet;
  SUB_SYS_CTX_t* p_ctx = &g_sub;

  clRet = CL_MonitorSearchInit(&p_ctx->clMonSearch);
  if (clRet != 0) {  // LCOV_EXCL_BR_LINE 6: For processing intializing process
    // LCOV_EXCL_START 6:For processing initializing process
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
    // LCOV_EXCL_STOP 6: For processing initializing process
  }

  return 0;
  // LCOV_EXCL_START 6:For processing initializing process
  ERROR:
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  return 1;
  // LCOV_EXCL_STOP
}

/******************************************************************
 *        Subsystem termination
 ******************************************************************/
int tskm_sub_term(void) {
  int ret;
  SUB_SYS_CTX_t* p_ctx = &g_sub;

  ret = CL_MonitorSearchDestroy(&p_ctx->clMonSearch);
  if (ret != 0) {
    TSKM_ASSERT_ERRNO(0);
  }

  return 0;
}
/*********************************************************
 *        REBOOT
 *********************************************************/
void tskm_sub_reboot(const TSKM_ERROR_REBOOT_t* p_info) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  TSKM_ASSERT(0);

  l_eStatus = SendCpuResetRequestToSystemManager(
      e_SS_SM_CPU_RESET_REASON_GENERIC_ERR, p_info->log.messageStr);
  if (eFrameworkunifiedStatusOK != l_eStatus) {
    TSKM_ASSERT(0);
  }
}

/*********************************************************
 *        REBOOT NORMAL
 *********************************************************/
void tskm_sub_reboot_normal(void) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  l_eStatus = SendCpuResetRequestToSystemManager(
      e_SS_SM_CPU_RESET_REASON_NORMAL);
  if (l_eStatus != eFrameworkunifiedStatusOK) {
    TSKM_ASSERT(0);
  }
}

/*********************************************************
 *        LOGGING
 *********************************************************/
void tskm_sub_logging(const TSKM_LOGGING_INFO_t* p_info) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  eSMUserLogType l_eType;

  switch (p_info->type) {
    case TSKM_LOGGING_TYPE_GRP_RELAUNCH:
      l_eType = e_SS_SM_CAPTURE_GROUP_RELAUNCH;
      break;
    case TSKM_LOGGING_TYPE_MODULE_LOGS:
    default:
      l_eType = e_SS_SM_CAPTURE_MODULE_LOGS;
      break;
  }

  l_eStatus = SendUserInvokedLoggingRequestToSystemManager(l_eType,
                                                           p_info->messageStr);
  if (eFrameworkunifiedStatusOK != l_eStatus) {
    TSKM_ASSERT(0);
  }
}

/*********************************************************
 *        DEBUG DUMP
 *********************************************************/
void tskm_sub_debugDumpRes(BOOL bIsNeedSvcName, const char *p_dumpMsg) {
  if (bIsNeedSvcName) {  // LCOV_EXCL_BR_LINE 8: As the bIsNeedSvcName is set to FALSE only
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    SSDEBUGDUMP("%s", p_dumpMsg);  // LCOV_EXCL_LINE 8: As the bIsNeedSvcName is set to FALSE only
  } else {
    SSDEBUGDUMP_RAW("%s", p_dumpMsg);
  }
}

/*********************************************************
 *        EXT BOOT INFO
 *********************************************************/
int tskm_sub_getExtBootInfo(T_SS_SM_START_ExtDataStructType *p_info) {
  EFrameworkunifiedStatus l_eStatus;
  T_SS_SM_START_ExtDataStructType extStartupInfo;

  l_eStatus = GetInterfaceunifiedOnStartExtInfo(extStartupInfo);
  if (l_eStatus != eFrameworkunifiedStatusOK) {
    TSKM_ASSERT(0);
    goto ERROR;
  }

  memcpy(p_info, &extStartupInfo, sizeof(extStartupInfo));

  return 0;
  ERROR: return -1;
}

/*********************************************************
 *        Service Error Monitoring Check
 *********************************************************/
TSKM_HUNG_INFO_t*
tskm_sub_searchHungSvcs() {
  int ret;
  TSKM_HUNG_INFO_t *p_hungList = NULL;
  SUB_SYS_CTX_t* p_ctx = &g_sub;

  ret = CL_MonitorSearchTimeout(&p_ctx->clMonSearch);
  if (ret == -1) {
    TSKM_ASSERT_ERRNO(0);
    goto ERROR;
  }

  if (0 < p_ctx->clMonSearch.entry_num) {
    int ii;

    TSKM_PRINTF(TSKM_LOG_DEBUG, "TimeoutSvcNum = %d",
                p_ctx->clMonSearch.entry_num);

    if (NULL == p_ctx->clMonSearch.entry_list) {
      TSKM_ASSERT(0);
      goto ERROR;
    }

    p_hungList = (TSKM_HUNG_INFO_t *) malloc(  // NOLINT (readability/casting)
        sizeof(TSKM_HUNG_INFO_t) * (p_ctx->clMonSearch.entry_num + 1));
    if (p_hungList == NULL) {  // LCOV_EXCL_BR_LINE 5: malloc's error case.
      // LCOV_EXCL_START 5: Checked in Death testing, but it is not reflected in the coverage and excluded
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      TSKM_ASSERT_ERRNO(0);
      goto ERROR;
      // LCOV_EXCL_STOP
    }

    for (ii = 0; ii < p_ctx->clMonSearch.entry_num; ii++) {
      p_hungList[ii].pid = p_ctx->clMonSearch.entry_list[ii].pid;
      p_hungList[ii].type = p_ctx->clMonSearch.entry_list[ii].type;
    }
    // set "-1" to pid at the end of list.
    p_hungList[p_ctx->clMonSearch.entry_num].pid = -1;
  }

  ERROR: return p_hungList;
}

/*********************************************************
 *        RESM Initialization
 *********************************************************/
int tskm_sub_resmInit(void) {  // LCOV_EXCL_START 6: Because the condition cannot be set
  AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
  int redmFd;
  SUB_SYS_CTX_t* p_ctx = &g_sub;
  RESM_ERR_t resmRet = RESM_E_OK;
  RESM_REQ_EVENT_t ev;

  int pid;

  resmRet = RESM_Open(NULL, &(p_ctx->resmId));
  if (RESM_E_OK != resmRet) {
    TSKM_ASSERT(0);

#warning RESM backtrace disabled, because 'print_backtrace_pid' not support.
#if 0
    // Backtrace outoutput of the RESM
    pid = oops_name2pid(MN_SS_RESOURCEMGR);
    if(pid > 0){
      TSKM_ASSERT(-1 != print_backtrace_pid(pid));
    }else{
      TSKM_PRINTF(TSKM_LOG_ERROR, "ERR : oops_name2pid %d , err:%d, msg:%s",
                  pid, errno, strerror(errno));
    }
#endif

    goto ERROR;
  }

  resmRet = RESM_GetEventFd(p_ctx->resmId, &redmFd);
  if (RESM_E_OK != resmRet) {
    TSKM_ASSERT(0);
    goto ERROR;
  }

  ev.reqEvent = RESM_EV_MEM;
  ev.prm.restMemThresh = 512;  // Threshold(BYTE)

  resmRet = RESM_ReqEvent(p_ctx->resmId, &ev);
  if (RESM_E_OK != resmRet) {
    TSKM_ASSERT(0);
    goto ERROR;
  }

  return redmFd;
  ERROR: return -1;
}
// LCOV_EXCL_STOP
/*********************************************************
 *        Get EV of RESM
 *********************************************************/
int tskm_sub_resmRcv(TSKM_EVENT_INFO_t* p_ev) {
  SUB_SYS_CTX_t* p_ctx = &g_sub;
  RESM_ERR_t resmRet = RESM_E_OK;
  RESM_EV_t evFlag;

  resmRet = RESM_GetEvent(p_ctx->resmId, &evFlag);
  if (RESM_E_OK != resmRet) {
    TSKM_ASSERT(0);
    goto ERROR;
  }

  if (RESM_EV_MEM != evFlag) {
    TSKM_ASSERT(0);
    goto ERROR;
  }

  TSKM_PRINTF(TSKM_LOG_ERROR, "Detect LowMem");

  p_ev->event = TSKM_EV_LCL_REP_LOWMEM;

  return 0;
  ERROR: return -1;
}

int tskm_sub_setWakeupOrder(const char* p_order) {
  // Under consideration
  return 0;
}  // LCOV_EXCL_BR_LINE 10: Gcov constraints (last row)