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
22 //////////////////////////////////////////////////////////////////////////////////////////////////
24 #include <native_service/frameworkunified_sm_framework_dispatch.h>
25 #include <native_service/frameworkunified_framework_if.h>
26 #include <native_service/frameworkunified_service_protocol.h>
27 #include <native_service/ns_message_center_if.h>
28 #include <native_service/ns_logger_if.h>
31 #include "frameworkunified_framework_core.h"
33 ////////////////////////////////////////////////////////////////////////////////////////////
34 /// Registers a single event with the dispatcher for a given service.
35 ////////////////////////////////////////////////////////////////////////////////////////////
36 EFrameworkunifiedStatus FrameworkunifiedAttachHSMEventToDispatcher(HANDLE hApp,
41 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
43 if (frameworkunifiedCheckValidAppHandle(hApp) && pServiceName) {
44 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
45 EventServices::iterator s_iterator;
46 EventSessionTable::iterator session_iterator;
48 UI_32 uiSessionId = 0;
50 uiSessionId = FrameworkunifiedGetSessionId(hSession);
53 // finding the service
54 s_iterator = pApp->eventservices.find(pServiceName);
55 if (s_iterator == pApp->eventservices.end()) {
56 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : (service NOT found): service name [%s]", pApp->cAppName, pServiceName);
57 pApp->eventservices.insert(std::make_pair(pServiceName, EventSessionTable()));
60 session_iterator = pApp->eventservices[pServiceName].find(uiSessionId);
61 if (session_iterator == pApp->eventservices[pServiceName].end()) {
62 pApp->eventservices[pServiceName].insert
63 (std::make_pair(uiSessionId, ServiceEventProtocolTable()));
66 (pApp->eventservices[pServiceName].find(uiSessionId)->second).insert(std::make_pair(iCmd, iEvent));
68 eStatus = eFrameworkunifiedStatusInvldHandle;
75 ////////////////////////////////////////////////////////////////////////////////////////////
76 /// FrameworkunifiedAttachHSMEventsToDispatcher
77 /// Registers a multiple event with the dispatcher for a given service.
78 ////////////////////////////////////////////////////////////////////////////////////////////
80 EFrameworkunifiedStatus FrameworkunifiedAttachHSMEventsToDispatcher(HANDLE hApp, PCSTR pServiceName,
81 const FrameworkunifiedProtocolEvent *pEventIds, UI_32 uiEventCount, HANDLE hSession) {
82 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
84 if (frameworkunifiedCheckValidAppHandle(hApp) && pServiceName && pEventIds) {
86 for (UI_32 i = 0; i < uiEventCount; ++i) {
87 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedAttachHSMEventToDispatcher(hApp,
90 pEventIds[ i ].iEventId,
96 eStatus = eFrameworkunifiedStatusInvldHandle;
102 /////////////////////////////////////////////////////
103 // Function : FrameworkunifiedAttachParentCallbacksToDispatcher
104 /////////////////////////////////////////////////////
105 EFrameworkunifiedStatus FrameworkunifiedAttachParentHSMEventsToDispatcher(HANDLE hChildApp, const FrameworkunifiedProtocolEvent *pEventIds,
106 UI_32 uiEventCount) {
107 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
109 if (frameworkunifiedCheckValidAppHandle(hChildApp)) {
110 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hChildApp);
112 eStatus = FrameworkunifiedAttachHSMEventsToDispatcher(hChildApp, pApp->cParentAppName, pEventIds, uiEventCount);
114 eStatus = eFrameworkunifiedStatusInvldHandle;
120 /////////////////////////////////////////////////////
121 // Function : FrameworkunifiedDetachParentCallbacksFromDispatcher
122 /////////////////////////////////////////////////////
123 EFrameworkunifiedStatus FrameworkunifiedDetachParentHSMEventsFromDispatcher(HANDLE hChildApp,
124 const PUI_32 puiEventArray, UI_32 uiEventCount) {
125 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
127 if (frameworkunifiedCheckValidAppHandle(hChildApp)) {
128 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hChildApp);
130 eStatus = FrameworkunifiedDetachHSMEventsFromDispatcher(hChildApp, pApp->cParentAppName, puiEventArray, uiEventCount);
132 eStatus = eFrameworkunifiedStatusInvldHandle;
139 /////////////////////////////////////////////////////
140 // Function : FrameworkunifiedDetachHSMEventsFromDispatcher
141 /////////////////////////////////////////////////////
142 EFrameworkunifiedStatus FrameworkunifiedDetachHSMEventsFromDispatcher(HANDLE hApp, PCSTR pServiceName, const PUI_32 puiEventArray,
143 UI_32 uiEventCount, HANDLE hSession) {
144 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
146 if (frameworkunifiedCheckValidAppHandle(hApp) && pServiceName) {
148 for (UI_32 i = 0; i < uiEventCount; ++i) {
151 * When an event-struct pointer is turned NULL, the array is accessed without NULL checking,
152 * and a segmentation fault occurs.
154 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDetachHSMEventFromDispatcher(hApp, pServiceName,
155 puiEventArray[ i ], hSession))) {
160 eStatus = eFrameworkunifiedStatusInvldHandle;
167 /////////////////////////////////////////////
168 // Function : FrameworkunifiedDetachHSMEventFromDispatcher
169 /////////////////////////////////////////////
170 EFrameworkunifiedStatus FrameworkunifiedDetachHSMEventFromDispatcher(HANDLE hApp, PCSTR pServiceName, UI_32 iEvent, HANDLE hSession) {
171 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
172 if (frameworkunifiedCheckValidAppHandle(hApp) && pServiceName) {
173 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
174 EventServices::iterator s_iterator;
176 UI_32 uiSessionId = 0;
178 uiSessionId = FrameworkunifiedGetSessionId(hSession);
181 // finding the service
182 s_iterator = pApp->eventservices.find(pServiceName);
183 if (s_iterator != pApp->eventservices.end()) {
184 EventSessionTable::iterator session_iterator;
185 session_iterator = (s_iterator->second).find(uiSessionId);
186 if (session_iterator != (s_iterator->second).end()) {
187 ServiceEventProtocolTable::iterator spt_iterator;
188 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : (found): service [%s]", pApp->cAppName, pServiceName);
190 spt_iterator = (session_iterator->second).find(iEvent);
191 if (spt_iterator != (session_iterator->second).end()) {
192 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : Cmd found [%d] service [%s]",
193 pApp->cAppName, iEvent, pServiceName);
194 (session_iterator->second).erase(spt_iterator);
196 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : Error : Cmd NOT found [%d] service [%s]",
197 pApp->cAppName, iEvent, pServiceName);
202 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : Cannot find service [%s]", pApp->cAppName, pServiceName);
203 eStatus = eFrameworkunifiedStatusFail;
207 eStatus = eFrameworkunifiedStatusInvldHandle;
213 ////////////////////////////////////////////////////////////////////////////////////////////
214 /// FrameworkunifiedAttachNotificationEventsToDispatcher
215 /// API to attach a event to the dispatcher on receiving a specific notification.
216 ////////////////////////////////////////////////////////////////////////////////////////////
217 EFrameworkunifiedStatus FrameworkunifiedSubscribeNotificationsWithHSMEvent(HANDLE hApp, const FrameworkunifiedNotificationEvent *pNtfyEvent,
218 UI_32 uiEventCount) {
219 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
221 if (frameworkunifiedCheckValidAppHandle(hApp) && pNtfyEvent) {
222 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
224 /// Deal with handling batch processing of subscriptions.
225 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPHSMSubscribeToNotificationsEvents(hApp, pNtfyEvent, uiEventCount))) {
226 for (UI_32 i = 0; i < uiEventCount; ++i) {
228 pApp->notificationevents.insert(std::make_pair(pNtfyEvent[ i ].cNotification, pNtfyEvent[ i ].iEventId));
230 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : attaching call-back for notification [%s]", pApp->cAppName,
231 pNtfyEvent[ i ].cNotification);
234 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to Subscribe "
235 "to batch set of notifications", pApp->cAppName,
239 eStatus = eFrameworkunifiedStatusInvldParam;
245 ////////////////////////////////////////////////////////////////////////////////////////////
246 /// FrameworkunifiedAttachNotificationEventToDispatcher
247 /// API to attach a event to the dispatcher on receiving a specific notification.
248 ////////////////////////////////////////////////////////////////////////////////////////////
249 EFrameworkunifiedStatus FrameworkunifiedSubscribeNotificationWithHSMEvent(HANDLE hApp, PCSTR pNotification, UI_32 iEventId) {
250 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
252 if (frameworkunifiedCheckValidAppHandle(hApp) && pNotification) {
253 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
255 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPHSMSubscribeToNotificationEvent(hApp, pNotification))) {
257 pApp->notificationevents.insert(std::make_pair(pNotification, iEventId));
259 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : attaching call-back for notification [%s]",
260 pApp->cAppName, pNotification);
262 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to Subscribe "
263 "to notification [%s]", pApp->cAppName, eFrameworkunifiedStatusOK, pNotification);
266 eStatus = eFrameworkunifiedStatusInvldHandle;
274 ////////////////////////////////////////////////////////////////////////////////////////////
275 /// FrameworkunifiedDetachNotificationEventsFromDispatcher
276 /// API to detach a notification event from the dispatcher.
277 ////////////////////////////////////////////////////////////////////////////////////////////
278 EFrameworkunifiedStatus FrameworkunifiedUnsubscribeNotificationsWithHSMEvent(HANDLE hApp, const FrameworkunifiedNotificationEvent *pNtfyEvent,
279 UI_32 uiEventCount) {
280 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
282 if (frameworkunifiedCheckValidAppHandle(hApp) && pNtfyEvent) {
283 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
285 NotificationEventTable::iterator n_iterator;
286 PCSTR pNotification = NULL;
288 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPHSMUnsubscribeFromNotificationEvents(hApp, pNtfyEvent, uiEventCount))) {
289 for (UI_32 l_unCount = 0; l_unCount < uiEventCount; ++l_unCount) {
290 pNotification = pNtfyEvent[ l_unCount ].cNotification;
291 n_iterator = pApp->notificationevents.find(pNotification);
292 if (n_iterator != pApp->notificationevents.end()) {
293 pApp->notificationevents.erase(n_iterator);
294 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : removed notification [%s]", pApp->cAppName, pNotification);
296 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : Cannot find notification [%s]",
297 pApp->cAppName, pNotification);
298 eStatus = eFrameworkunifiedStatusFail;
302 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : detaching call-back for notification [%s]",
303 pApp->cAppName, pNotification);
305 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to UnSubscribe from notifications ",
306 pApp->cAppName, eFrameworkunifiedStatusOK);
309 eStatus = eFrameworkunifiedStatusInvldHandle;
316 ////////////////////////////////////////////////////////////////////////////////////////////
317 /// FrameworkunifiedDetachNotificationEventsFromDispatcher
318 /// API to detach a notification event from the dispatcher.
319 ////////////////////////////////////////////////////////////////////////////////////////////
320 EFrameworkunifiedStatus FrameworkunifiedUnsubscribeNotificationWithHSMEvent(HANDLE hApp, PCSTR pNotification) {
321 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
323 if (frameworkunifiedCheckValidAppHandle(hApp) && pNotification) {
324 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
325 NotificationEventTable::iterator n_iterator;
327 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPHSMUnsubscribeFromNotificationEvent(hApp, pNotification))) {
328 // \todo : error handling on all map function calls
329 n_iterator = pApp->notificationevents.find(pNotification);
330 if (n_iterator != pApp->notificationevents.end()) {
331 pApp->notificationevents.erase(n_iterator);
332 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : removed notification [%s]", pApp->cAppName, pNotification);
334 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : Cannot find notification [%s]",
335 pApp->cAppName, pNotification);
336 eStatus = eFrameworkunifiedStatusFail;
339 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to UnSubscribe from notification [%s]",
340 pApp->cAppName, eFrameworkunifiedStatusOK, pNotification);
343 eStatus = eFrameworkunifiedStatusInvldHandle;
349 ////////////////////////////////////////////////////////////////////////////////////////////
350 /// FrameworkunifiedGetStateMachine
351 /// returns the pointer to the statemachine object
352 ////////////////////////////////////////////////////////////////////////////////////////////
353 CFrameworkunifiedHSMFramework *FrameworkunifiedGetStateMachine(HANDLE hApp) {
354 CFrameworkunifiedHSMFramework *l_pHSMFramework = NULL;
356 if (frameworkunifiedCheckValidAppHandle(hApp)) {
357 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
358 l_pHSMFramework = pApp->m_pFrameworkunifiedStateMachine;
361 return l_pHSMFramework;
364 ////////////////////////////////////////////////////////////////////////////////////////////
365 /// FrameworkunifiedSetStateMachine
366 /// sets the statemachine object
367 ////////////////////////////////////////////////////////////////////////////////////////////
368 EFrameworkunifiedStatus FrameworkunifiedSetStateMachine(HANDLE hApp,
369 CFrameworkunifiedHSM *f_pFrameworkunifiedHSM) {
370 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
372 if (frameworkunifiedCheckValidAppHandle(hApp)) {
373 if (NULL != f_pFrameworkunifiedHSM) {
374 (reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp))->m_pFrameworkunifiedStateMachine =
375 reinterpret_cast<CFrameworkunifiedHSMFramework *>(f_pFrameworkunifiedHSM);
377 eStatus = eFrameworkunifiedStatusNullPointer;
380 eStatus = eFrameworkunifiedStatusInvldHandle;
386 ////////////////////////////////////////////////////////////////////////////////////////////
387 /// FrameworkunifiedSubscribeToSessionEventWithHSMEvent
388 /// This API is used for subscribing to single event of a service.
389 /// This API also attaches the session event with HSM events.
390 ////////////////////////////////////////////////////////////////////////////////////////////
391 EFrameworkunifiedStatus FrameworkunifiedSubscribeToSessionEventWithHSMEvent(HANDLE hApp,
395 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
397 if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
398 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedAttachHSMEventToDispatcher(hApp,
399 (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName, uiEventId,
400 uiHSMEventId, hSession))) {
401 eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_REGISTER_EVENTS, sizeof(UI_32), (PVOID)&uiEventId);
403 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedAttachCallbackToDispatcher "
404 "for PROTOCOL_REGISTER_EVENTS Failed Status:: %d", eStatus);
407 eStatus = eFrameworkunifiedStatusInvldHandle;
413 ////////////////////////////////////////////////////////////////////////////////////////////
414 /// FrameworkunifiedSubscribeToSessionEventsWithHSMEvents
415 /// This API is used for subscribing to multiple events of a service.
416 /// This API also attaches the session events with hsm events.
417 ////////////////////////////////////////////////////////////////////////////////////////////
418 EFrameworkunifiedStatus FrameworkunifiedSubscribeToSessionEventsWithHSMEvents(HANDLE hApp, const FrameworkunifiedProtocolEvent *pEventIds,
421 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
423 if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession) && NULL != pEventIds) {
424 eStatus = FrameworkunifiedAttachHSMEventsToDispatcher(hApp,
425 (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName, pEventIds,
426 uiEventCount, hSession);
428 UI_32 l_uiCmdList[uiEventCount]; // NOLINT (readability/nolint)
429 for (UI_32 l_uiCnt = 0; l_uiCnt < uiEventCount; l_uiCnt++) {
430 l_uiCmdList[l_uiCnt] = pEventIds[l_uiCnt].iCmd;
433 eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_REGISTER_EVENTS, static_cast<UI_32>(uiEventCount * sizeof(UI_32)),
436 eStatus = eFrameworkunifiedStatusInvldHandle;
442 ////////////////////////////////////////////////////////////////////////////////////////////
443 /// FrameworkunifiedUnSubscribeSessionEventWithHSMEvent
444 /// API to unsubscribe from event of a service. Also detaches HSM event.
445 ////////////////////////////////////////////////////////////////////////////////////////////
446 EFrameworkunifiedStatus FrameworkunifiedUnSubscribeSessionEventWithHSMEvent(HANDLE hApp, UI_32 uiEventId, HANDLE hSession) {
447 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
449 if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
450 eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_UNREGISTER_EVENTS, sizeof(uiEventId), (PVOID)&uiEventId);
452 if (eFrameworkunifiedStatusOK != FrameworkunifiedDetachHSMEventFromDispatcher(hApp,
453 (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName, uiEventId, hSession)) {
454 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : FrameworkunifiedDetachCallbackFromDispatcher failed status:: %d", eStatus);
457 eStatus = eFrameworkunifiedStatusInvldHandle;
463 ////////////////////////////////////////////////////////////////////////////////////////////
464 /// FrameworkunifiedUnSubscribeSessionEventsWithHSMEvents
465 /// API to unsubscribe from multiple events of a service. Also detaches HSM events.
466 ////////////////////////////////////////////////////////////////////////////////////////////
467 EFrameworkunifiedStatus FrameworkunifiedUnSubscribeSessionEventsWithHSMEvents(HANDLE hApp, PUI_32 pEventsArray, UI_32 uiListSize,
469 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
471 if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession) && NULL != pEventsArray) {
472 eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_UNREGISTER_EVENTS,
473 static_cast<UI_32>(uiListSize * sizeof(uiListSize)), pEventsArray);
475 if (eFrameworkunifiedStatusOK != FrameworkunifiedDetachHSMEventsFromDispatcher(hApp,
476 (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName, pEventsArray,
478 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : FrameworkunifiedDetachCallbackFromDispatcher failed status:: %d", eStatus);
481 eStatus = eFrameworkunifiedStatusInvldHandle;