common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / task_manager / client / libprimary / src / pri_api.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 #include "system_service/tskm_local_type.h"
18 #include "system_service/INI_API.hpp"
19 #include "tskm_debug.h"
20 #include "pri_main.h"
21
22 BOOL __thread isMain = FALSE;  // Check for accessibility from the main thread context
23
24 TSKM_STATIC BOOL isInitPrmValid(const T_PRIM_PRM* p_prm) {
25   if (p_prm == NULL) {
26     TSKM_ASSERT(0);
27     return FALSE;
28   } else if (p_prm->shmTbl == NULL) {
29     TSKM_ASSERT(0);
30     return FALSE;
31   } else if (p_prm->wakeupExFuncTbl == NULL) {
32     TSKM_ASSERT(0);
33     return FALSE;
34   } else if (p_prm->downExFuncTbl == NULL) {
35     TSKM_ASSERT(0);
36     return FALSE;
37   } else if (p_prm->onInit == NULL) {
38     TSKM_ASSERT(0);
39     return FALSE;
40   } else if (p_prm->onDestory == NULL) {
41     TSKM_ASSERT(0);
42     return FALSE;
43   } else if (p_prm->onDebugDump == NULL) {
44     TSKM_ASSERT(0);
45     return FALSE;
46   } else if (p_prm->onTouch == NULL) {
47     TSKM_ASSERT(0);
48     return FALSE;
49   }
50
51   return TRUE;
52 }
53
54 /******************************************************************************
55  *      APIs for implementing the main thread on the service side
56  *******************************************************************************/
57 /*********************************************************
58  *  Primary library initialization
59  *********************************************************/
60 int32_t INI_Init(T_PRIM_PRM* p_prm, int argc, char* argv[], int *fdNum,
61                  int fdlist[INI_FD_MAX]) {
62   isMain = TRUE;
63   if (isInitPrmValid(p_prm) == FALSE) {
64     TSKM_ASSERT(0);
65     return INI_FALSE;
66   }
67   pri_init(p_prm, argc, argv, fdNum, fdlist);
68   return INI_SUCCESS;
69 }
70
71 /***********************************************************
72  *  Primary library / Event handler
73  *  ret:TRUE  Running
74  *  ret:FALSE Finished (Terminated a process with INI_Term())
75  ************************************************************/
76 BOOL INI_Handler(fd_set* p_fds) {
77   if (!isMain || NULL == p_fds) {
78     TSKM_ASSERT(0);
79     return FALSE;
80   }
81
82   return pri_handler(p_fds);
83 }
84
85 /***********************************************************
86  *  Primary (Processing finished)
87  ************************************************************/
88 void INI_Term(void) {
89   pri_term();
90 }
91
92 /***********************************************************
93  *  Primary (State setting for watching Services)
94  ************************************************************/
95 int32_t INI_SetMonitorState(T_PRIM_MONITOR_PRM *p_prm) {
96   if (!p_prm) {
97     TSKM_ASSERT(0);
98     goto ERROR;
99   }
100
101   return pri_setMonitorState(p_prm->bIsRun, p_prm->timeout);
102   ERROR: return INI_FALSE;
103 }
104
105 /******************************************************************************
106  *      Hiding the Main Thread in the Primary Library
107  *******************************************************************************/
108 /************************************
109  *  Startup FW MainLoop
110  ************************************/
111 int INI_Main(T_PRIM_PRM* p_prm, int argc, char* argv[]) {
112   if (isInitPrmValid(p_prm) == FALSE) {  // LCOV_EXCL_BR_LINE 6: Checked by Init()
113     TSKM_ASSERT(0);
114     return INI_FALSE;
115   }
116
117   isMain = TRUE;
118
119   return pri_main(p_prm, argc, argv);
120 }
121
122 /******************************************************************************
123  *      Common API
124  *******************************************************************************/
125 /************************************
126  *  Process termination
127  ************************************/
128 void INI_ExitStart(void * rsv) {
129   static int isCalled = 0;
130   if (isCalled) {
131     return;
132   }
133   isCalled = 1;
134   pri_exitStart(rsv);
135 }
136
137 void INI_ExitDone(int status) {
138   static int isCalled = 0;
139   if (isCalled) {
140     return;
141   }
142   isCalled = 1;
143   pri_exitDone(status);
144 }
145
146 /************************************
147  *  Private Info Acquisition
148  ************************************/
149 void* INI_GetPrivate() {
150   return pri_getPrivate();
151 }
152
153 /************************************
154  *  Applicastion Handle Aquisition
155  ************************************/
156 HANDLE INI_GetHandle() {
157   return pri_getHandle();
158 }
159
160 /************************************
161  *  Timeout setting for service monitoring status setting
162  ************************************/
163 int32_t INI_SetMonitorTimeout(uint32_t timeout) {
164   return pri_setMonitorTimeout(timeout);
165 }
166
167 /************************************
168  *  Event completion notification at startup
169  ************************************/
170 int32_t INI_StepForkComp(uint64_t compId) {
171   return pri_stepForkComp(compId);
172 }
173
174 /************************************
175  *  Event completion notification at termination
176  ************************************/
177 int32_t INI_AccOffComp(uint64_t compId) {
178   return pri_accOffComp(compId);
179 }
180
181 /************************************
182  *  BOOT Info Acquisition
183  ************************************/
184 int32_t INI_GetBootInfo(T_SS_SM_START_DataStructType *info) {
185   if (info == NULL) {
186     TSKM_ASSERT(0);
187     return INI_FALSE;
188   }
189   return pri_getBootInfo(info);
190 }
191
192 /************************************
193  *  Extended BOOT Info Acquisition
194  ************************************/
195 int32_t INI_GetExtBootInfo(T_SS_SM_START_ExtDataStructType *info) {
196   if (info == NULL) {
197     TSKM_ASSERT(0);
198     return INI_FALSE;
199   }
200   return pri_getExtBootInfo(info);
201 }
202
203 /************************************
204  *  DebugDump Responding
205  ************************************/
206 void _INI_DEBUGDUMP(BOOL bIsNeedSvcName, PCSTR cFormat, ...) {  // LCOV_EXCL_START 7: for debugging
207   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
208   va_list argList;
209   char buf[TSKM_EV_DEBUGDUMP_SIZE] = { 0 };
210
211   if (bIsNeedSvcName) {
212     snprintf(buf, TSKM_EV_DEBUGDUMP_SIZE, "%s/",
213              FrameworkunifiedGetAppName(pri_getHandle()));
214   }
215
216   va_start(argList, cFormat);
217   vsnprintf(&buf[strlen(buf)], TSKM_EV_DEBUGDUMP_SIZE - strlen(buf), cFormat,
218             argList);
219   va_end(argList);
220
221   return pri_sendDebugDumpRes(buf);
222 }
223 // LCOV_EXCL_STOP 7
224