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
23 ///////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_logger_if.h>
27 #include <native_service/ns_plogger_if.h>
28 #include <native_service/ns_message_center_if.h>
29 #include <native_service/frameworkunified_sm_hsmframework.h>
30 #include <native_service/frameworkunified_framework_if.h>
31 #include <native_service/frameworkunified_sm_dispatcher.h>
33 #include "frameworkunified_framework_core.h"
34 #include "frameworkunified_framework_error_internal.hpp"
35 #include "frameworkunified_sm_framework_core.h"
36 #include "frameworkunified_framework_utility.h"
38 static HSMConfigOptions s_tHSMConfigOptions = {eUserchangeIgnore, TRUE, FALSE};
40 EFrameworkunifiedStatus FrameworkunifiedHSMDispatcherMain(HANDLE hApp);
41 //////////////////////////////////////////
42 // Function : FrameworkunifiedCreateDispatcher
43 //////////////////////////////////////////
44 EFrameworkunifiedStatus FrameworkunifiedCreateHSMDispatcher(PCSTR cAppName,
45 HANDLE &hFrameworkApp, // NOLINT (readability/nolint)
46 BOOL bIsThread, CFrameworkunifiedHSMFramework *f_pFrameworkunifiedHSM) {
47 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
49 if (NULL != cAppName) {
50 if (eFrameworkunifiedStatusOK == (eStatus = CreateDispatcher(cAppName, hFrameworkApp, bIsThread))) {
51 if (NULL != hFrameworkApp) {
52 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hFrameworkApp);
54 if (NULL != f_pFrameworkunifiedHSM) {
55 pApp->m_pFrameworkunifiedStateMachine = f_pFrameworkunifiedHSM;
56 pApp->m_pFrameworkunifiedStateMachine->m_pHApp = pApp;
59 pApp->m_pFrameworkunifiedStateMachine = new CFrameworkunifiedHSMChildFramework(pApp);
61 pApp->m_pFrameworkunifiedStateMachine = new CFrameworkunifiedHSMParentFramework(pApp);
65 if (NULL != pApp->m_pFrameworkunifiedStateMachine) {
66 pApp->m_pFrameworkunifiedStateMachine->FrameworkunifiedCreate(&s_tHSMConfigOptions);
69 FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
70 eStatus = eFrameworkunifiedStatusNullPointer;
73 // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
74 FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "CreateDispatcher returned error, status=%d", eStatus);
78 FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "AppName is NULL");
79 eStatus = eFrameworkunifiedStatusInvldParam;
85 ////////////////////////////////////////////////////////////////////////////////////////////
86 /// FrameworkunifiedDispatcherWithArguments
87 /// Creates, initializes and runs the dispatcher
89 /// \param [in] cAppname
90 /// PCSTR - Application/ thread name
92 /// \return Never does, main loop for your application.
94 /// \see FrameworkunifiedCreateDispatcher, FrameworkunifiedDispatchBlock,
95 /// FrameworkunifiedDispatchProcess, FrameworkunifiedCloseDispatcher,
97 ////////////////////////////////////////////////////////////////////////////////////////////
98 EFrameworkunifiedStatus FrameworkunifiedHSMDispatcherWithArguments(PCSTR cAppName, int argc, char *argv[],
99 const FrameworkunifiedDefaultCallbackHandler *CbHandler, CFrameworkunifiedHSMFramework *f_pFrameworkunifiedHSM,
100 CustomCommandLineOptions *cmdLineOptions) {
101 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
103 if (cAppName == NULL) {
104 return eFrameworkunifiedStatusNullPointer;
107 NsLogSetProcessName(cAppName);
108 PLOG_TEXT("FrameworkunifiedHSMDispatcher Start");
109 // set main thread name as provided in dispatcher
110 pthread_setname_np(pthread_self(), cAppName);
112 if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
116 FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "In"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
118 HANDLE hFrameworkApp = NULL;
120 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateHSMDispatcher(cAppName, hFrameworkApp, FALSE, f_pFrameworkunifiedHSM))) {
121 if (NULL != hFrameworkApp) {
122 THApp hApp(hFrameworkApp);
124 /// Parse the Arguments via the FrameworkunifiedArgumentParser
125 /// passing an handle to the app and argument list
126 eStatus = FrameworkunifiedArgumentParser(hApp, argc, argv, cmdLineOptions);
128 if (eFrameworkunifiedStatusOK == eStatus) {
129 eStatus = FrameworkunifiedHSMDispatcherMain(hApp);
132 eStatus = eFrameworkunifiedStatusNullPointer;
133 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
136 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateHSMDispatcher error, status=%d", eStatus);
138 } catch (const THApp::Exception &) {
141 * When an exception error occurs in a FrameworkunifiedCreateHSMDispatcher,
142 * the caller cannot detect the exception because the exception becomes the eFrameworkunifiedStatusOK return code.
144 FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedHSMCreateDispatcher ");
151 ////////////////////////////////////////////////////////////////////////////////////////////
152 /// Function : FrameworkunifiedHSMDispatcherMain
153 ////////////////////////////////////////////////////////////////////////////////////////////
154 EFrameworkunifiedStatus FrameworkunifiedHSMDispatcherMain(HANDLE hApp) {
155 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
158 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "Success: FrameworkunifiedCreateDispatcher ");
160 if (frameworkunifiedCheckValidAppHandle(hApp)) {
161 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
163 CFrameworkunifiedHSMFramework *l_pStateMachine = pApp->m_pFrameworkunifiedStateMachine;
165 if (l_pStateMachine) {
166 eStatus = l_pStateMachine->FrameworkunifiedStart();
168 if (eFrameworkunifiedStatusOK != eStatus) {
169 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
170 l_pStateMachine->FrameworkunifiedClose();
173 eStatus = RunDispatcher(hApp);
176 l_pStateMachine->FrameworkunifiedClose();
178 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "l_pStateMachine is NULL");
179 eStatus = eFrameworkunifiedStatusNullPointer;
182 } catch (const frameworkunified::framework::error::CSystemError &err) {
183 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR :: %s", err.what());
185 FrameworkunifiedSendSystemErrMessage(hApp, err.m_eSystemError);
186 } catch (const THApp::Exception &) {
187 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher ");
193 ////////////////////////////////////////////////////////////////////////////////////////////
194 /// Function : FrameworkunifiedSetHSMType
195 ////////////////////////////////////////////////////////////////////////////////////////////
196 void FrameworkunifiedSetHSMType(EUserChangeOptions f_eHSMType) {
197 s_tHSMConfigOptions.eUserChange = f_eHSMType;
200 ////////////////////////////////////////////////////////////////////////////////////////////
201 /// Function : FrameworkunifiedEnableAutoPublishServiceAvailable
202 ////////////////////////////////////////////////////////////////////////////////////////////
203 void FrameworkunifiedEnableAutoPublishServiceAvailable() {
204 s_tHSMConfigOptions.bAutoPublishServiceAvaialble = TRUE;
207 ////////////////////////////////////////////////////////////////////////////////////////////
208 /// Function : FrameworkunifiedDisableAutoPublishServiceAvailable
209 ////////////////////////////////////////////////////////////////////////////////////////////
210 void FrameworkunifiedDisableAutoPublishServiceAvailable() {
211 s_tHSMConfigOptions.bAutoPublishServiceAvaialble = FALSE;
214 ////////////////////////////////////////////////////////////////////////////////////////////
215 /// Function : FrameworkunifiedIsAutoPublishServiceAvailableEnabled
216 ////////////////////////////////////////////////////////////////////////////////////////////
217 BOOL FrameworkunifiedIsAutoPublishServiceAvailableEnabled() {
218 return s_tHSMConfigOptions.bAutoPublishServiceAvaialble;
221 ////////////////////////////////////////////////////////////////////////////////////////////
222 /// FrameworkunifiedHSMEnableWaitInStoppingState
223 ////////////////////////////////////////////////////////////////////////////////////////////
224 VOID FrameworkunifiedHSMEnableWaitInStoppingState() {
225 s_tHSMConfigOptions.bWaitInStoppingState = TRUE;
228 ////////////////////////////////////////////////////////////////////////////////////////////
229 /// FrameworkunifiedHSMDisableWaitInStoppingState
230 ////////////////////////////////////////////////////////////////////////////////////////////
231 VOID FrameworkunifiedHSMDisableWaitInStoppingState() {
232 s_tHSMConfigOptions.bWaitInStoppingState = FALSE;
235 ////////////////////////////////////////////////////////////////////////////////////////////
236 /// FrameworkunifiedIsWaitInStoppingStateEnabled
237 ////////////////////////////////////////////////////////////////////////////////////////////
238 BOOL FrameworkunifiedIsWaitInStoppingStateEnabled() {
239 return s_tHSMConfigOptions.bWaitInStoppingState;