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_NSFramework
19 /// \brief This file has the function implementation for creating state machine child thread
22 //////////////////////////////////////////////////////////////////////////////////////////////////
23 #include <sys/prctl.h>
25 #include <native_service/frameworkunified_sm_hsmframework.h>
26 #include <native_service/frameworkunified_sm_framework_dispatch.h>
27 #include <native_service/frameworkunified_framework_if.h>
28 #include <native_service/ns_utility.hpp>
29 #include <native_service/ns_system_mode.h>
30 #include <native_service/frameworkunified_sm_multithreading.h>
31 #include <native_service/frameworkunified_multithreading.h>
36 #include "frameworkunified_framework_utility.h"
37 #include "frameworkunified_framework_core.h"
38 #include "frameworkunified_framework_internal.h"
40 ////////////////////////////////////////////////////////////////////////////////////////////
41 /// FrameworkunifiedCreateHSMDispatcherChild
42 ////////////////////////////////////////////////////////////////////////////////////////////
43 EFrameworkunifiedStatus FrameworkunifiedCreateHSMDispatcherChild(PCSTR childName,
45 HANDLE &hChildApp, // NOLINT (readability/nolint)
46 CbFuncPtr pOnThreadStart,
47 CbFuncPtr pOnThreadStop) {
48 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
50 if ((NULL != childName) && (NULL != parentName) && (strlen(childName) <= MAX_NAME_SIZE_APP) &&
51 (strlen(parentName) <= MAX_NAME_SIZE_APP)) {
52 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateHSMDispatcher(childName, hChildApp, TRUE))) {
53 if (frameworkunifiedCheckValidAppHandle(hChildApp)) {
54 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hChildApp);
56 (reinterpret_cast<CFrameworkunifiedHSMChildFramework *>(pApp->m_pFrameworkunifiedStateMachine))->m_fpStartThread
59 (reinterpret_cast<CFrameworkunifiedHSMChildFramework *>(pApp->m_pFrameworkunifiedStateMachine))->m_fpStopThread
62 pApp->hParentSndMsgQ = McOpenSender(parentName);
63 if (NULL == pApp->hParentSndMsgQ) {
64 eStatus = eFrameworkunifiedStatusNullPointer;
65 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "McOpenSender failed");
68 pApp->uiSessionId = THREAD_SESSION_ID;
70 memset(pApp->cParentAppName, 0, sizeof(pApp->cParentAppName));
71 memcpy(pApp->cParentAppName, parentName, strlen(parentName));
73 eStatus = eFrameworkunifiedStatusNullPointer;
74 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hChildApp is NULL");
77 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
78 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateHSMDispatcher error, status=%d", eStatus);
82 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid Param received"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
83 eStatus = eFrameworkunifiedStatusInvldParam;
89 void *child_hsm_thread_proc(void *args) {
91 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "args is NULL");
94 PCData pcdata = *reinterpret_cast< PCData * >(args); // Create a local copy of data
97 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
99 HANDLE hFrameworkApp = NULL;
101 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateHSMDispatcherChild(pcdata.childName.c_str(),
102 pcdata.parentName.c_str(),
106 if (NULL != hFrameworkApp) {
107 THApp hChildApp(hFrameworkApp);
109 if (pcdata.CbCreateStateMachine) {
110 pcdata.CbCreateStateMachine(hChildApp);
111 FRAMEWORKUNIFIED_PRINT_HSM(hChildApp)
113 const FrameworkunifiedProtocolEvent pcbhs[] = { { SYSTEM_ON_INITIALIZATION, FRAMEWORKUNIFIED_EVENT(evFrameworkunifiedStart) },
114 { SYSTEM_ON_SHUTDOWN, FRAMEWORKUNIFIED_EVENT(evFrameworkunifiedStop) },
115 { SYSTEM_ON_DESTROY, FRAMEWORKUNIFIED_EVENT(evFrameworkunifiedDestroy) }
118 if (eFrameworkunifiedStatusOK != FrameworkunifiedAttachHSMEventsToDispatcher(hChildApp,
119 pcdata.parentName.c_str(),
121 static_cast<UI_32>(_countof(pcbhs)),
123 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to attach hsm events to child thread %s",
124 pcdata.childName.c_str());
127 // set child thread name
128 char thread_name[16];
129 strncpy(thread_name, pcdata.childName.c_str(), sizeof(thread_name));
130 prctl(PR_SET_NAME, thread_name);
131 thread_name[15] = '\0';
133 setChildThreadSched(pcdata.schedPolicy, pcdata.schedPriority);
135 (FrameworkunifiedGetStateMachine(hChildApp))->FrameworkunifiedStart();
137 *pcdata.childStatus = eFrameworkunifiedStatusOK;
138 if (IsValidWaitBarrier(pthread_barrier_wait(pcdata.barrier))) {
139 RunChildDispatcher(hChildApp);
142 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
145 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
146 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateHSMDispatcherChild error, status=%d", eStatus);
149 *pcdata.childStatus = eFrameworkunifiedStatusFail;
150 pthread_barrier_wait(pcdata.barrier);
152 } catch (const THApp::Exception &) {
153 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to create child %s", pcdata.childName.c_str());
161 ////////////////////////////////////////////////////////////////////////////////////////////
162 /// FrameworkunifiedCreateHSMChildThread
163 ////////////////////////////////////////////////////////////////////////////////////////////
164 HANDLE FrameworkunifiedCreateHSMChildThread(HANDLE hApp, PCSTR childName, CbFuncPtr CbInitialize,
165 CbFuncPtr CbShutdown, CbFuncPtr CbCreateStateMachine) {
166 HANDLE hChildQ = NULL;
167 FrameworkunifiedChildThreadAttr attr;
169 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != childName && NULL != CbInitialize && NULL != CbShutdown &&
170 NULL != CbCreateStateMachine) {
171 CreateChildThreadAttrInit(&attr);
172 hChildQ = CreateChildThread(hApp, childName, CbInitialize, CbShutdown, &attr, CbCreateStateMachine);
178 ////////////////////////////////////////////////////////////////////////////////////////////
179 /// FrameworkunifiedCreateHSMChildThreadWithPriority
180 ////////////////////////////////////////////////////////////////////////////////////////////
181 HANDLE FrameworkunifiedCreateHSMChildThreadWithPriority(HANDLE hApp, PCSTR childName, CbFuncPtr CbInitialize,
182 CbFuncPtr CbShutdown, CbFuncPtr CbCreateStateMachine, SI_32 schedPrio) {
183 HANDLE hChildQ = NULL;
184 FrameworkunifiedChildThreadAttr attr;
186 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != childName && NULL != CbInitialize && NULL != CbShutdown &&
187 NULL != CbCreateStateMachine) {
188 CreateChildThreadAttrInit(&attr);
189 CreateChildThreadAttrSetSched(&attr, eFrameworkunifiedSchedPolicyRR, schedPrio);
190 hChildQ = CreateChildThread(hApp, childName, CbInitialize, CbShutdown, &attr, CbCreateStateMachine);
195 ////////////////////////////////////////////////////////////////////////////////////////////
196 /// FrameworkunifiedCreateHSMChildThreadWithAttribute
197 ////////////////////////////////////////////////////////////////////////////////////////////
198 HANDLE FrameworkunifiedCreateHSMChildThreadWithAttribute(HANDLE hApp,
200 CbFuncPtr CbInitialize,
201 CbFuncPtr CbShutdown,
202 CbFuncPtr CbCreateStateMachine,
203 const FrameworkunifiedChildThreadAttr *attr) {
204 HANDLE hChildQ = NULL;
206 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != childName && NULL != CbInitialize && NULL != CbShutdown &&
207 NULL != CbCreateStateMachine) {
208 hChildQ = CreateChildThread(hApp, childName, CbInitialize, CbShutdown, attr, CbCreateStateMachine);