Init basesystem source codes.
[staging/basesystem.git] / nsframework / framework_unified / client / NS_FrameworkCore / src / statemachine / frameworkunified_sm_dispatcher.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 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NSFramework
19 /// \brief
20 ///
21 ///
22 ///
23 ///////////////////////////////////////////////////////////////////////////////
24 #include <pthread.h>
25
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>
32
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"
37
38 static HSMConfigOptions s_tHSMConfigOptions = {eUserchangeIgnore, TRUE, FALSE};
39
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;
48
49   if (NULL != cAppName) {
50     if (eFrameworkunifiedStatusOK == (eStatus = CreateDispatcher(cAppName, hFrameworkApp, bIsThread))) {
51       if (NULL != hFrameworkApp) {
52         CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hFrameworkApp);
53
54         if (NULL != f_pFrameworkunifiedHSM) {
55           pApp->m_pFrameworkunifiedStateMachine = f_pFrameworkunifiedHSM;
56           pApp->m_pFrameworkunifiedStateMachine->m_pHApp = pApp;
57         } else {
58           if (bIsThread) {
59             pApp->m_pFrameworkunifiedStateMachine = new CFrameworkunifiedHSMChildFramework(pApp);
60           } else {
61             pApp->m_pFrameworkunifiedStateMachine = new CFrameworkunifiedHSMParentFramework(pApp);
62           }
63         }
64
65         if (NULL != pApp->m_pFrameworkunifiedStateMachine) {
66           pApp->m_pFrameworkunifiedStateMachine->FrameworkunifiedCreate(&s_tHSMConfigOptions);
67         }
68       } else {
69         FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
70         eStatus = eFrameworkunifiedStatusNullPointer;
71       }
72     } else {
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);
75       // LCOV_EXCL_BR_STOP
76     }
77   } else {
78     FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "AppName is NULL");
79     eStatus = eFrameworkunifiedStatusInvldParam;
80   }
81
82   return eStatus;
83 }
84
85 ////////////////////////////////////////////////////////////////////////////////////////////
86 /// FrameworkunifiedDispatcherWithArguments
87 /// Creates, initializes and runs the dispatcher
88 ///
89 /// \param [in] cAppname
90 ///         PCSTR - Application/ thread name
91 ///
92 /// \return Never does, main loop for your application.
93 ///
94 /// \see FrameworkunifiedCreateDispatcher, FrameworkunifiedDispatchBlock,
95 ///      FrameworkunifiedDispatchProcess, FrameworkunifiedCloseDispatcher,
96 ///
97 ////////////////////////////////////////////////////////////////////////////////////////////
98 EFrameworkunifiedStatus FrameworkunifiedHSMDispatcherWithArguments(PCSTR cAppName, int argc, char *argv[],
99                                          const FrameworkunifiedDefaultCallbackHandler *CbHandler, CFrameworkunifiedHSMFramework *f_pFrameworkunifiedHSM,
100                                          CustomCommandLineOptions *cmdLineOptions) {
101   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
102
103   if (cAppName == NULL) {
104     return eFrameworkunifiedStatusNullPointer;
105   }
106
107   NsLogSetProcessName(cAppName);
108   PLOG_TEXT("FrameworkunifiedHSMDispatcher Start");
109   // set main thread name as provided in dispatcher
110   pthread_setname_np(pthread_self(), cAppName);
111
112   if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
113     return eStatus;
114   }
115
116   FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "In");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
117   try {
118     HANDLE hFrameworkApp = NULL;
119
120     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateHSMDispatcher(cAppName, hFrameworkApp, FALSE, f_pFrameworkunifiedHSM))) {
121       if (NULL != hFrameworkApp) {
122         THApp hApp(hFrameworkApp);
123
124         /// Parse the Arguments via the FrameworkunifiedArgumentParser
125         /// passing an handle to the app and argument list
126         eStatus = FrameworkunifiedArgumentParser(hApp, argc, argv, cmdLineOptions);
127
128         if (eFrameworkunifiedStatusOK == eStatus) {
129           eStatus = FrameworkunifiedHSMDispatcherMain(hApp);
130         }
131       } else {
132         eStatus = eFrameworkunifiedStatusNullPointer;
133         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
134       }
135     } else {
136       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateHSMDispatcher error, status=%d", eStatus);
137     }
138   } catch (const THApp::Exception &) {
139     /**
140      * @todo
141      * When an exception error occurs in a FrameworkunifiedCreateHSMDispatcher,
142      * the caller cannot detect the exception because the exception becomes the eFrameworkunifiedStatusOK return code.
143      */
144     FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedHSMCreateDispatcher ");
145   }
146
147
148   return eStatus;
149 }
150
151 ////////////////////////////////////////////////////////////////////////////////////////////
152 /// Function : FrameworkunifiedHSMDispatcherMain
153 ////////////////////////////////////////////////////////////////////////////////////////////
154 EFrameworkunifiedStatus FrameworkunifiedHSMDispatcherMain(HANDLE hApp) {
155   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
156
157   try {
158     FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "Success: FrameworkunifiedCreateDispatcher ");
159
160     if (frameworkunifiedCheckValidAppHandle(hApp)) {
161       CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
162
163       CFrameworkunifiedHSMFramework *l_pStateMachine = pApp->m_pFrameworkunifiedStateMachine;
164
165       if (l_pStateMachine) {
166         eStatus = l_pStateMachine->FrameworkunifiedStart();
167
168         if (eFrameworkunifiedStatusOK != eStatus) {
169           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
170           l_pStateMachine->FrameworkunifiedClose();
171           throw happ_error();
172         } else {
173           eStatus = RunDispatcher(hApp);
174         }
175
176         l_pStateMachine->FrameworkunifiedClose();
177       } else {
178         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "l_pStateMachine is NULL");
179         eStatus = eFrameworkunifiedStatusNullPointer;
180       }
181     }
182   } catch (const frameworkunified::framework::error::CSystemError &err) {
183     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR :: %s", err.what());
184
185     FrameworkunifiedSendSystemErrMessage(hApp, err.m_eSystemError);
186   } catch (const THApp::Exception &) {
187     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher ");
188   }
189
190   return eStatus;
191 }
192
193 ////////////////////////////////////////////////////////////////////////////////////////////
194 /// Function : FrameworkunifiedSetHSMType
195 ////////////////////////////////////////////////////////////////////////////////////////////
196 void FrameworkunifiedSetHSMType(EUserChangeOptions f_eHSMType) {
197   s_tHSMConfigOptions.eUserChange = f_eHSMType;
198 }
199
200 ////////////////////////////////////////////////////////////////////////////////////////////
201 /// Function : FrameworkunifiedEnableAutoPublishServiceAvailable
202 ////////////////////////////////////////////////////////////////////////////////////////////
203 void FrameworkunifiedEnableAutoPublishServiceAvailable() {
204   s_tHSMConfigOptions.bAutoPublishServiceAvaialble = TRUE;
205 }
206
207 ////////////////////////////////////////////////////////////////////////////////////////////
208 /// Function : FrameworkunifiedDisableAutoPublishServiceAvailable
209 ////////////////////////////////////////////////////////////////////////////////////////////
210 void FrameworkunifiedDisableAutoPublishServiceAvailable() {
211   s_tHSMConfigOptions.bAutoPublishServiceAvaialble = FALSE;
212 }
213
214 ////////////////////////////////////////////////////////////////////////////////////////////
215 /// Function : FrameworkunifiedIsAutoPublishServiceAvailableEnabled
216 ////////////////////////////////////////////////////////////////////////////////////////////
217 BOOL FrameworkunifiedIsAutoPublishServiceAvailableEnabled() {
218   return s_tHSMConfigOptions.bAutoPublishServiceAvaialble;
219 }
220
221 ////////////////////////////////////////////////////////////////////////////////////////////
222 /// FrameworkunifiedHSMEnableWaitInStoppingState
223 ////////////////////////////////////////////////////////////////////////////////////////////
224 VOID FrameworkunifiedHSMEnableWaitInStoppingState() {
225   s_tHSMConfigOptions.bWaitInStoppingState = TRUE;
226 }
227
228 ////////////////////////////////////////////////////////////////////////////////////////////
229 /// FrameworkunifiedHSMDisableWaitInStoppingState
230 ////////////////////////////////////////////////////////////////////////////////////////////
231 VOID FrameworkunifiedHSMDisableWaitInStoppingState() {
232   s_tHSMConfigOptions.bWaitInStoppingState = FALSE;
233 }
234
235 ////////////////////////////////////////////////////////////////////////////////////////////
236 /// FrameworkunifiedIsWaitInStoppingStateEnabled
237 ////////////////////////////////////////////////////////////////////////////////////////////
238 BOOL FrameworkunifiedIsWaitInStoppingStateEnabled() {
239   return s_tHSMConfigOptions.bWaitInStoppingState;
240 }