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 ///////////////////////////////////////////////////////////////////////////////
24 #include <sys/epoll.h>
26 #include <native_service/frameworkunified_framework_if.h>
27 #include <native_service/ns_message_center_if.h>
28 #include <native_service/ns_logger_if.h>
29 #include <native_service/ns_plogger_if.h>
30 #include <other_service/strlcpy.h>
35 #include "frameworkunified_framework_core.h"
36 ///////////////////////////////////////////////////
38 ///////////////////////////////////////////////////
40 //////////////////////////////////////////
41 // Function : FrameworkunifiedGetAppName
42 //////////////////////////////////////////
43 PCSTR FrameworkunifiedGetAppName(HANDLE hApp) {
44 if (frameworkunifiedCheckValidAppHandle(hApp)) {
45 const CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
46 return ((PCSTR)pApp->cAppName);
52 //////////////////////////////////////////
53 // Function : FrameworkunifiedSetThreadSpecificData
54 //////////////////////////////////////////
55 EFrameworkunifiedStatus FrameworkunifiedSetThreadSpecificData(HANDLE hApp, PVOID data) {
56 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
58 if (frameworkunifiedCheckValidAppHandle(hApp)) {
59 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
60 pApp->FrameworkData = data;
62 eStatus = eFrameworkunifiedStatusOK;
68 //////////////////////////////////////////
69 // Function : FrameworkunifiedGetThreadSpecificData
70 //////////////////////////////////////////
71 PVOID FrameworkunifiedGetThreadSpecificData(HANDLE hApp) {
72 if (frameworkunifiedCheckValidAppHandle(hApp)) {
73 const CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
74 return pApp->FrameworkData;
82 template <typename K, typename V, class C, class A>
83 std::ostream &operator<< (std::ostream &os, std::map<K, V, C, A> const &m) {
85 typename std::map<K, V, C, A>::const_iterator p;
86 for (p = m.begin(); p != m.end(); ++p) {
87 os << p->first << ":" << p->second << ", ";
98 ///////////////////////////////////////////////////
99 // Service Protocol attach/detach to dispatcher
100 ///////////////////////////////////////////////////
108 /////////////////////////////////////////////////////
109 // Function : FrameworkunifiedAttachCallbacksToDispatcher
110 /////////////////////////////////////////////////////
111 EFrameworkunifiedStatus FrameworkunifiedAttachCallbacksToDispatcher(HANDLE hApp, PCSTR pServiceName, const FrameworkunifiedProtocolCallbackHandler *handlers,
112 UI_32 handlerCount, HANDLE hSession) {
113 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
115 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName && NULL != handlers && 0 != handlerCount) {
117 for (UI_32 i = 0; i < handlerCount; ++i) {
118 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(hApp, pServiceName, handlers[ i ].iCmd,
119 handlers[ i ].callBack, hSession))) {
124 eStatus = eFrameworkunifiedStatusInvldParam;
131 /////////////////////////////////////////////
132 // Function : FrameworkunifiedAttachCallbackToDispatcher
133 /////////////////////////////////////////////
134 EFrameworkunifiedStatus FrameworkunifiedAttachCallbackToDispatcher(HANDLE hApp, PCSTR pServiceName, UI_32 iCmd, CbFuncPtr fpOnCmd,
136 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
138 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName && NULL != fpOnCmd) {
139 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
140 Services::iterator s_iterator;
141 SessionTable::iterator session_iterator;
143 UI_32 uiSessionId = 0;
145 uiSessionId = FrameworkunifiedGetSessionId(hSession);
148 // finding the service
149 s_iterator = pApp->services.find(pServiceName);
150 if (s_iterator == pApp->services.end()) {
151 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : (New service entry): service name [%s]", pApp->cAppName, pServiceName);
152 s_iterator = pApp->services.insert(std::make_pair(pServiceName, SessionTable())).first;
155 session_iterator = s_iterator->second.find(uiSessionId);
156 if (session_iterator == s_iterator->second.end()) {
157 session_iterator = s_iterator->second.insert(std::make_pair(uiSessionId, ServiceProtocolTable())).first;
160 session_iterator->second.insert(std::make_pair(iCmd, fpOnCmd));
162 eStatus = eFrameworkunifiedStatusInvldParam;
168 ///////////////////////////////////////////////////
169 // Function : FrameworkunifiedAttachCallbacksToDispatcherWithFd
170 ///////////////////////////////////////////////////
171 EFrameworkunifiedStatus FrameworkunifiedAttachCallbacksToDispatcherWithFd(HANDLE hApp, const FrameworkunifiedFdProtocolCallbackHandler *handlers,
172 UI_32 handlerCount) {
173 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
175 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != handlers && 0 != handlerCount) {
176 // setup callbacksWithFd
177 for (UI_32 i = 0; i < handlerCount; ++i) {
178 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedAttachCallbackToDispatcherWithFd(hApp, handlers[ i ].fd,
179 handlers[ i ].callBack))) {
184 eStatus = eFrameworkunifiedStatusInvldParam;
185 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : %d, Invalid param ", eStatus);
191 ///////////////////////////////////////////////////
192 // Function : FrameworkunifiedAttachCallbackToDispatcherWithFd
193 ///////////////////////////////////////////////////
194 EFrameworkunifiedStatus FrameworkunifiedAttachCallbackToDispatcherWithFd(HANDLE hApp, int fd, CbFuncPtr fpOnCmd) {
195 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
196 int efd; // FD for multi waiting
197 struct epoll_event ev; // Info struct to associate with multiwaiting FD
199 if (frameworkunifiedCheckValidAppHandle(hApp) && 0 < fd && NULL != fpOnCmd) {
200 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
202 // attach callbackFuncPtr and FD to FdTable.
203 pApp->fds.insert(std::make_pair(fd, fpOnCmd));
205 // Monitor FD by epoll.
210 if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev)) {
212 * Duplicate registering the same FD in the Dispatcher causes an internal epoll_ctl errno EEXIST(val17) to be returned, which results in an error.
214 eStatus = eFrameworkunifiedStatusFail;
215 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(ADD) Failed, status=%d, errno=%d", eStatus, errno);
218 eStatus = eFrameworkunifiedStatusFail;
219 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Multi waiting FD is Invalid , status=%d, efd=%d", eStatus, efd);
222 eStatus = eFrameworkunifiedStatusInvldParam;
223 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : %d, Invalid param ", eStatus);
229 /////////////////////////////////////////////////////
230 // Function : FrameworkunifiedAttachParentCallbacksToDispatcher
231 /////////////////////////////////////////////////////
232 EFrameworkunifiedStatus FrameworkunifiedAttachParentCallbacksToDispatcher(HANDLE hChildApp, const FrameworkunifiedProtocolCallbackHandler *handlers,
233 UI_32 handlerCount) {
234 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
236 if (frameworkunifiedCheckValidAppHandle(hChildApp) && NULL != handlers && 0 != handlerCount) {
237 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hChildApp);
239 eStatus = FrameworkunifiedAttachCallbacksToDispatcher(hChildApp, pApp->cParentAppName, handlers, handlerCount);
241 eStatus = eFrameworkunifiedStatusInvldParam;
248 /////////////////////////////////////////////////////
249 // Function : FrameworkunifiedDetachParentCallbacksFromDispatcher
250 /////////////////////////////////////////////////////
251 EFrameworkunifiedStatus FrameworkunifiedDetachParentCallbacksFromDispatcher(HANDLE hChildApp, const PUI_32 puiCmdArray, UI_32 uiCommandCount) {
252 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
254 if (frameworkunifiedCheckValidAppHandle(hChildApp) && NULL != puiCmdArray && 0 != uiCommandCount) {
255 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hChildApp);
257 eStatus = FrameworkunifiedDetachCallbacksFromDispatcher(hChildApp, pApp->cParentAppName, puiCmdArray, uiCommandCount);
259 eStatus = eFrameworkunifiedStatusInvldParam;
265 /////////////////////////////////////////////////////
266 // Function : FrameworkunifiedDetachCallbacksFromDispatcher
267 /////////////////////////////////////////////////////
268 EFrameworkunifiedStatus FrameworkunifiedDetachCallbacksFromDispatcher(HANDLE hApp, PCSTR pServiceName, const PUI_32 puiCmdArray,
269 UI_32 uiCommandCount, HANDLE hSession) {
270 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
272 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName && NULL != puiCmdArray && 0 != uiCommandCount) {
274 for (UI_32 i = 0; i < uiCommandCount; ++i) {
275 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(hApp, pServiceName,
276 puiCmdArray[ i ], hSession))) {
281 eStatus = eFrameworkunifiedStatusInvldParam;
289 ////////////////////////////////////////////
290 // Function : FrameworkunifiedDetachCallbackFromDispatcher
291 /////////////////////////////////////////////
292 EFrameworkunifiedStatus FrameworkunifiedDetachCallbackFromDispatcher(HANDLE hApp, PCSTR pServiceName, UI_32 iCmd, HANDLE hSession) {
293 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
295 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName) {
296 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
297 Services::iterator s_iterator;
299 UI_32 uiSessionId = 0;
301 uiSessionId = FrameworkunifiedGetSessionId(hSession);
304 // finding the service
305 s_iterator = pApp->services.find(pServiceName);
306 if (s_iterator != pApp->services.end()) {
307 SessionTable::iterator session_iterator;
308 session_iterator = (s_iterator->second).find(uiSessionId);
309 if (session_iterator != (s_iterator->second).end()) {
310 ServiceProtocolTable::iterator spt_iterator;
311 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : (found): service [%s]", pApp->cAppName, pServiceName);
313 spt_iterator = (session_iterator->second).find(iCmd);
314 if (spt_iterator != (session_iterator->second).end()) {
315 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : Detaching command [0x%X] service [%s]",
316 pApp->cAppName, iCmd, pServiceName);
317 (session_iterator->second).erase(spt_iterator);
319 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cmd NOT found [%d] service [%s]",
320 pApp->cAppName, iCmd, pServiceName);
325 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cannot find service [%s]", pApp->cAppName, pServiceName);
326 eStatus = eFrameworkunifiedStatusFail;
329 eStatus = eFrameworkunifiedStatusInvldParam;
336 /////////////////////////////////////////////////////
337 // Function : FrameworkunifiedDetachCallbacksFromDispatcherWithFd
338 /////////////////////////////////////////////////////
339 EFrameworkunifiedStatus FrameworkunifiedDetachCallbacksFromDispatcherWithFd(HANDLE hApp, const int *fdArray, UI_32 uiCommandCount) {
340 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
342 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != fdArray && 0 != uiCommandCount) {
344 for (UI_32 i = 0; i < uiCommandCount; ++i) {
345 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDetachCallbackFromDispatcherWithFd(hApp, fdArray[ i ]))) {
350 eStatus = eFrameworkunifiedStatusInvldParam;
351 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : %d, Invalid param ", eStatus);
357 ////////////////////////////////////////////////////
358 // Function : FrameworkunifiedDetachCallbackFromDispatcherWithFd
359 ////////////////////////////////////////////////////
360 EFrameworkunifiedStatus FrameworkunifiedDetachCallbackFromDispatcherWithFd(HANDLE hApp, int fd) {
361 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
363 if (frameworkunifiedCheckValidAppHandle(hApp) && 0 < fd) {
364 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
365 FdTable::iterator f_iterator;
366 int efd; // FD for multi waiting
367 struct epoll_event ev; // Info struct to associate with multiwaiting FD
369 // finding the FD from FD table
370 f_iterator = pApp->fds.find(fd);
371 if (f_iterator != pApp->fds.end()) {
372 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : Detaching fd [0x%x]", pApp->cAppName, fd);
373 pApp->fds.erase(f_iterator);
375 // Remove the monitoring of FD from multi waiting FD
380 if (-1 == epoll_ctl(efd, EPOLL_CTL_DEL, fd, &ev)) {
381 eStatus = eFrameworkunifiedStatusFail;
382 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(DEL) Failed, status=%d, errno=%d", eStatus, errno);
385 eStatus = eFrameworkunifiedStatusFail;
386 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Multi waiting FD is Invalid , status=%d, efd=%d", eStatus, efd);
389 eStatus = eFrameworkunifiedStatusFail;
390 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cannot find fd [0x%x]", pApp->cAppName, fd);
393 eStatus = eFrameworkunifiedStatusInvldParam;
394 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, Invalid param ", eStatus);
401 ///////////////////////////////////////////////////
402 // Function : FrameworkunifiedDetachServiceFromDispatcher
403 ///////////////////////////////////////////////////
404 EFrameworkunifiedStatus FrameworkunifiedDetachServiceFromDispatcher(HANDLE hApp, PCSTR pServiceName) {
405 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
407 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName) {
408 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
409 Services::iterator s_iterator;
410 EventServices::iterator es_iterator;
412 // finding the service
413 s_iterator = pApp->services.find(pServiceName);
414 if (s_iterator != pApp->services.end()) {
415 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : (found): service [%s]", pApp->cAppName, pServiceName);
416 pApp->services.erase(s_iterator);
418 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning: Cannot find service [%s]", pApp->cAppName, pServiceName);
419 eStatus = eFrameworkunifiedStatusFail;
422 // finding the service
423 es_iterator = pApp->eventservices.find(pServiceName);
424 if (es_iterator != pApp->eventservices.end()) {
425 eStatus = eFrameworkunifiedStatusOK;
426 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : (found): service [%s] in event map", pApp->cAppName, pServiceName);
427 pApp->eventservices.erase(es_iterator);
429 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cannot find service [%s] in event map",
430 pApp->cAppName, pServiceName);
433 eStatus = eFrameworkunifiedStatusInvldParam;
439 ///////////////////////////////////////////////////
440 // Notification functions
441 ///////////////////////////////////////////////////
444 ////////////////////////////////////////////////////////
445 // Function : FrameworkunifiedAttachNotificationCallbacksToDispatcher
446 ////////////////////////////////////////////////////////
447 EFrameworkunifiedStatus FrameworkunifiedSubscribeNotificationsWithCallback(HANDLE hApp, const FrameworkunifiedNotificationCallbackHandler *pNtfyHandler,
448 UI_32 uiHandlerCount) {
449 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
451 if (frameworkunifiedCheckValidAppHandle(hApp) && pNtfyHandler && (uiHandlerCount > 0)) {
452 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
454 /// Deal with handling batch processing of subscriptions.
455 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPSubscribeToNotifications(hApp, pNtfyHandler, uiHandlerCount))) {
456 for (UI_32 i = 0; i < uiHandlerCount; ++i) {
457 // Notification names are character arrays of the MAX_STRING_SIZE_NOTIFICATION byte
458 // If there are no NULL characters in the array of Notification names, they will overrun during make_pair,
459 // so use a strlcpy that substitutes a trailing NULL character.
460 char tmp_notification[MAX_STRING_SIZE_NOTIFICATION] = {0};
461 strlcpy(tmp_notification, pNtfyHandler[ i ].cNotification, MAX_STRING_SIZE_NOTIFICATION);
463 pApp->notifications.insert(std::make_pair(tmp_notification, pNtfyHandler[ i ].callBack));
465 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : attaching call-back for notification [%s]", pApp->cAppName,
466 pNtfyHandler[ i ].cNotification);
469 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to Subscribe to batch set of notifications",
470 pApp->cAppName, eStatus);
473 eStatus = eFrameworkunifiedStatusInvldParam;
479 ////////////////////////////////////////////////////////
480 // Function : FrameworkunifiedAttachNotificationCallbackToDispatcher
481 ////////////////////////////////////////////////////////
482 EFrameworkunifiedStatus FrameworkunifiedSubscribeNotificationWithCallback(HANDLE hApp, PCSTR pNotification, CbFuncPtr fpOnCmd) {
483 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
485 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNotification && NULL != fpOnCmd) {
486 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
487 NotificationTableRetStatus mRet;
489 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPSubscribeToNotification(hApp, pNotification))) {
490 // Notification names are character arrays of the MAX_STRING_SIZE_NOTIFICATION byte
491 // If there are no NULL characters in the array of Notification names, they will overrun during make_pair,
492 // so use a strlcpy that substitutes a trailing NULL character.
493 char tmp_notification[MAX_STRING_SIZE_NOTIFICATION] = {0};
494 strlcpy(tmp_notification, pNotification, MAX_STRING_SIZE_NOTIFICATION);
497 mRet = pApp->notifications.insert(std::make_pair(tmp_notification, fpOnCmd));
498 if (false == mRet.second) {
499 eStatus = eFrameworkunifiedStatusDuplicate;
500 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s:Error:[%d] Unable to Subscribe to notification [%s]",
501 pApp->cAppName, eStatus, pNotification);
503 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s:attaching call-back for notification [%s]",
504 pApp->cAppName, pNotification);
507 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s:Error:[%d] Unable to Subscribe to notification [%s]",
508 pApp->cAppName, eStatus, pNotification);
511 eStatus = eFrameworkunifiedStatusInvldParam;
519 ////////////////////////////////////////////////////////
520 // Function : FrameworkunifiedDetachNotificationCallbacksFromDispatcher
521 ////////////////////////////////////////////////////////
522 EFrameworkunifiedStatus FrameworkunifiedUnsubscribeNotificationsWithCallback(HANDLE hApp, const FrameworkunifiedNotificationCallbackHandler *pNtfyHandler,
523 UI_32 uiHandlerCount) {
524 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
525 NotificationTable::iterator n_iterator;
526 PCSTR pNotification = NULL;
528 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNtfyHandler && (uiHandlerCount > 0)) {
529 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
531 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPUnsubscribeFromNotifications(hApp, pNtfyHandler, uiHandlerCount))) {
532 for (UI_32 l_unCount = 0; l_unCount < uiHandlerCount; ++l_unCount) {
533 // When registering using FrameworkunifiedSubscribeNotificationWithCallback, etc., the Nortification names NULL the 64th byte.
534 // In response to this, the UnRegister uses a strlcpy that puts a NULL in the 64th byte.
535 char tmp_notification[MAX_STRING_SIZE_NOTIFICATION] = {0};
536 strlcpy(tmp_notification, pNtfyHandler[ l_unCount ].cNotification, MAX_STRING_SIZE_NOTIFICATION);
538 n_iterator = pApp->notifications.find(tmp_notification);
539 if (n_iterator != pApp->notifications.end()) {
540 pApp->notifications.erase(n_iterator);
541 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : removed notification [%s]", pApp->cAppName, pNotification);
543 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : Cannot find notification [%s]", pApp->cAppName,
544 pNotification != 0 ? pNotification : NULL);
545 eStatus = eFrameworkunifiedStatusFail;
549 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : detaching call-back for notification [%s]", pApp->cAppName,
550 pNotification != 0 ? pNotification : NULL);
552 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to UnSubscribe from notifications ", pApp->cAppName,
553 eFrameworkunifiedStatusOK);
556 eStatus = eFrameworkunifiedStatusInvldParam;
563 ////////////////////////////////////////////////////////
564 // Function : FrameworkunifiedDetachNotificationCallbackToDispatcher
565 ////////////////////////////////////////////////////////
566 EFrameworkunifiedStatus FrameworkunifiedUnsubscribeNotificationWithCallback(HANDLE hApp, PCSTR pNotification) {
567 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
569 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNotification) {
570 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
571 NotificationTable::iterator n_iterator;
573 if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPUnsubscribeFromNotification(hApp, pNotification))) {
574 // \todo : error handling on all map function calls
576 // When registering using FrameworkunifiedSubscribeNotificationWithCallback, etc., the Nortification names NULL the 64th byte.
577 // In response to this, the UnRegister uses a strlcpy that puts a NULL in the 64th byte.
578 char tmp_notification[MAX_STRING_SIZE_NOTIFICATION] = {0};
579 strlcpy(tmp_notification, pNotification, MAX_STRING_SIZE_NOTIFICATION);
581 n_iterator = pApp->notifications.find(tmp_notification);
582 if (n_iterator != pApp->notifications.end()) {
583 pApp->notifications.erase(n_iterator);
584 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s:removed notification [%s]", pApp->cAppName, pNotification);
586 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s:Error:Cannot find notification [%s]", pApp->cAppName, pNotification);
587 eStatus = eFrameworkunifiedStatusFail;
590 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s:Error:[%d] Unable to UnSubscribe from notification [%s]",
591 pApp->cAppName, eStatus, pNotification);
594 eStatus = eFrameworkunifiedStatusInvldParam;
600 ///////////////////////////////////////////////////
601 // Defer Message functions
602 ///////////////////////////////////////////////////
604 ////////////////////////////////////////////////////////
605 // Function : FrameworkunifiedGetDeferQueueCnt
606 ////////////////////////////////////////////////////////
607 UI_32 FrameworkunifiedGetDeferQueueCnt(HANDLE hApp) {
610 if (frameworkunifiedCheckValidAppHandle(hApp)) {
611 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
612 l_uiCnt = static_cast<UI_32>(pApp->deferedMsgQueue.size());
618 ////////////////////////////////////////////////////////
619 // Function : FrameworkunifiedIsDeferQueueEmpty
620 ////////////////////////////////////////////////////////
621 BOOL FrameworkunifiedIsDeferQueueEmpty(HANDLE hApp) {
622 BOOL l_bIsQEmpty = TRUE;
624 if (frameworkunifiedCheckValidAppHandle(hApp)) {
625 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
626 l_bIsQEmpty = static_cast<BOOL>(pApp->deferedMsgQueue.empty());
632 ////////////////////////////////////////////////////////
633 // Function : FrameworkunifiedDeferMessage
634 ////////////////////////////////////////////////////////
635 EFrameworkunifiedStatus FrameworkunifiedDeferMessage(HANDLE hApp) {
636 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
638 if (frameworkunifiedCheckValidAppHandle(hApp)) {
639 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
640 DeferedMsgInfo defer(pApp->uiProtocolCmd, pApp->cMsgSrcName, pApp->uiMsgRcvBuffer);
641 pApp->deferedMsgQueue.push(defer);
643 eStatus = eFrameworkunifiedStatusInvldParam;
649 ////////////////////////////////////////////////////////
650 // Function : FrameworkunifiedClearDeferMessages
651 ////////////////////////////////////////////////////////
652 EFrameworkunifiedStatus FrameworkunifiedClearDeferMessages(HANDLE hApp) {
653 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
655 if (frameworkunifiedCheckValidAppHandle(hApp)) {
656 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
658 // clear the pop flag!
659 pApp->fPopDeferedMsg = FALSE;
661 // remove all items from the queue!
662 while (!pApp->deferedMsgQueue.empty()) {
663 pApp->deferedMsgQueue.pop();
666 eStatus = eFrameworkunifiedStatusInvldParam;
672 ////////////////////////////////////////////////////////
673 // Function : FrameworkunifiedRetrieveDeferMessage
674 ////////////////////////////////////////////////////////
675 EFrameworkunifiedStatus FrameworkunifiedRetrieveDeferMessage(HANDLE hApp) {
676 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
678 if (frameworkunifiedCheckValidAppHandle(hApp)) {
679 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
680 pApp->fPopDeferedMsg = pApp->deferedMsgQueue.empty() ? FALSE : TRUE;
681 if (pApp->fPopDeferedMsg) {
682 if (-1 == (write(pApp->defer_evfd, &data, sizeof(uint64_t)))) {
683 eStatus = eFrameworkunifiedStatusFail;
684 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : write Failed, status=%d, errno=%d, defer_evfd=%d",
685 eStatus, errno, pApp->defer_evfd);
691 eStatus = eFrameworkunifiedStatusInvldParam;
698 ////////////////////////////////////////////////////////////////////////////////////////////
699 /// FrameworkunifiedIsStateMachineApp
700 /// Returns TRUE if it's a state machine application else FALSE.
701 ////////////////////////////////////////////////////////////////////////////////////////////
702 BOOL FrameworkunifiedIsStateMachineApp(HANDLE hApp) {
703 BOOL l_bIsStateMachine = FALSE;
705 if (frameworkunifiedCheckValidAppHandle(hApp)) {
706 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
707 if (NULL != pApp->m_pFrameworkunifiedStateMachine) {
708 l_bIsStateMachine = TRUE;
712 return l_bIsStateMachine;
715 ////////////////////////////////////////////////////////////////////////////////////////////
716 /// FrameworkunifiedGetXMLConfigHandle
717 /// Returns the handle to config file handle
718 ////////////////////////////////////////////////////////////////////////////////////////////
719 HANDLE FrameworkunifiedGetXMLConfigHandle(HANDLE hApp) {
720 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __PRETTY_FUNCTION__, "This function is not implemented!!!!");
724 EFrameworkunifiedStatus FrameworkunifiedSetMandatoryServiceInfo(HANDLE hApp, PCSTR pNotification, UI_32 uiEventId) {
725 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
727 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNotification) {
728 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
729 ServiceNotificationInfo objNotification = {};
730 if (strlen(pNotification) < MAX_STRING_SIZE_NOTIFICATION) {
731 strlcpy(objNotification.sNotificationName, pNotification, sizeof(objNotification.sNotificationName));
732 objNotification.uiEventId = uiEventId;
733 pApp->servicenotificationlist.push_back(objNotification);
735 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
736 " Error : Aborting ... Exceeds Max Notification name size MAX_STRING_SIZE_NOTIFICATION : %d ",
737 MAX_STRING_SIZE_NOTIFICATION);
738 eStatus = eFrameworkunifiedStatusFail;
741 eStatus = eFrameworkunifiedStatusInvldParam;
747 BOOL FrameworkunifiedIsServiceAvailable(HANDLE hApp) {
748 BOOL l_bIsServiceAvailable = FALSE;
750 if (frameworkunifiedCheckValidAppHandle(hApp)) {
751 // Publish notification
752 ServiceAvailability tServiceAvailability = {};
754 UI_32 l_uiLength = FrameworkunifiedGetMsgLength(hApp);
756 if (sizeof(tServiceAvailability) == l_uiLength) {
757 // Read the data from the message
758 if (eFrameworkunifiedStatusOK != FrameworkunifiedGetMsgDataOfSize(hApp, &tServiceAvailability, sizeof(tServiceAvailability))) {
759 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " FrameworkunifiedGetMsgDataOfSize Failed");
761 // Check the Service availability
762 if (eFrameworkunifiedServiceAvailable == tServiceAvailability.eServiceAvailability) {
763 l_bIsServiceAvailable = TRUE;
767 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error: Received message data size not matched :: %d", l_uiLength);
771 return l_bIsServiceAvailable;
774 EFrameworkunifiedStatus FrameworkunifiedPublishServiceAvailability(HANDLE hApp, BOOL bIsAvailable) {
775 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
777 if (frameworkunifiedCheckValidAppHandle(hApp)) {
778 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
779 if (strlen(pApp->sServiceAvailabilityNotification)) {
780 // Publish Service available this can also be published from FrameworkunifiedOnStart callback
781 pApp->bIsAvailable = bIsAvailable;
782 ServiceAvailability tServiceAvailability = {};
783 strlcpy(tServiceAvailability.cServiceName, FrameworkunifiedGetAppName(hApp), sizeof(tServiceAvailability.cServiceName));
786 tServiceAvailability.eServiceAvailability = eFrameworkunifiedServiceAvailable;
788 tServiceAvailability.eServiceAvailability = eFrameworkunifiedServiceNotAvailable;
791 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedNPPublishNotification(hApp, pApp->sServiceAvailabilityNotification,
792 &tServiceAvailability, sizeof(tServiceAvailability)))) {
793 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__,
794 "Failed to Publish notification: %s, status: %d", pApp->sServiceAvailabilityNotification, eStatus);
795 FRAMEWORKUNIFIEDLOG_PERFORMANCE("Service Availability Status: %s, ERRORED %d", bIsAvailable ? "TRUE" : "FALSE", eStatus);
797 FRAMEWORKUNIFIEDLOG_PERFORMANCE("Service Availability Status: %s", bIsAvailable ? "TRUE" : "FALSE");
798 FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__,
799 "Publish availability notfn:%s, status:%s", pApp->sServiceAvailabilityNotification,
800 bIsAvailable ? "TRUE" : "FALSE");
803 eStatus = eFrameworkunifiedStatusFail;
806 eStatus = eFrameworkunifiedStatusInvldParam;
812 BOOL FrameworkunifiedGetSelfAvailability(HANDLE hApp) {
813 if (frameworkunifiedCheckValidAppHandle(hApp)) {
814 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
815 return pApp->bIsAvailable;
821 EFrameworkunifiedStatus FrameworkunifiedRegisterServiceAvailabilityNotification(HANDLE hApp, PCSTR pNotification) {
822 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
824 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNotification) {
825 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
826 if (strlen(pNotification) < MAX_STRING_SIZE_NOTIFICATION) {
827 strlcpy(pApp->sServiceAvailabilityNotification, pNotification, sizeof(pApp->sServiceAvailabilityNotification));
829 // Register Notifications
830 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedNPRegisterNotification(hApp, pNotification,
831 sizeof(ServiceAvailability), eFrameworkunifiedStateVar))) {
832 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedNPRegisterNotifications %s Failed Status:0x%x ", pNotification, eStatus);
834 FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "FrameworkunifiedNPRegisterNotifications %s success Status:0x%x ",
835 pNotification, eStatus);
838 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
839 " Error : Aborting ... Exceeds Max Notification name size MAX_STRING_SIZE_NOTIFICATION : %d ",
840 MAX_STRING_SIZE_NOTIFICATION);
841 eStatus = eFrameworkunifiedStatusFail;
844 eStatus = eFrameworkunifiedStatusInvldParam;
850 EFrameworkunifiedStatus FrameworkunifiedUnRegisterServiceAvailabilityNotification(HANDLE hApp) {
851 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
853 if (frameworkunifiedCheckValidAppHandle(hApp)) {
854 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
856 // Register Notifications
857 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedNPUnRegisterNotification(hApp, pApp->sServiceAvailabilityNotification))) {
858 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedNPUnRegisterNotifications %s Failed Status:0x%x ",
859 pApp->sServiceAvailabilityNotification != 0 ? pApp->sServiceAvailabilityNotification : NULL, eStatus);
861 FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "FrameworkunifiedNPUnRegisterNotifications %s success Status:0x%x ",
862 pApp->sServiceAvailabilityNotification, eStatus);
864 memset(pApp->sServiceAvailabilityNotification, 0, MAX_SYS_INFO_SIZE);
865 pApp->bIsAvailable = FALSE;
867 eStatus = eFrameworkunifiedStatusInvldParam;
873 ////////////////////////////////////////////////////////////////////////////////////////////
874 /// FrameworkunifiedGetServiceNameOnServiceAvailabilityNotification
875 /// To be used when client receives service availability notification to get the available
877 ////////////////////////////////////////////////////////////////////////////////////////////
878 EFrameworkunifiedStatus FrameworkunifiedGetServiceNameOnServiceAvailabilityNotification(HANDLE hApp, PSTR pServiceName) {
879 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
880 // Publish notification
881 ServiceAvailability tServiceAvailability;
883 if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName) {
884 UI_32 l_uiLength = FrameworkunifiedGetMsgLength(hApp);
886 if (sizeof(tServiceAvailability) == l_uiLength) {
887 // Read the data from the message
888 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &tServiceAvailability, sizeof(tServiceAvailability)))) {
889 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " FrameworkunifiedGetMsgDataOfSize Failed");
890 eStatus = eFrameworkunifiedStatusFail;
892 if (NULL == std::strncpy(pServiceName, tServiceAvailability.cServiceName, MAX_NAME_SIZE_APP)) {
893 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " strcpy failed Failed");
894 eStatus = eFrameworkunifiedStatusFail;
898 eStatus = eFrameworkunifiedStatusFail;
899 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error: Received message data size not matched :: %d", l_uiLength);
902 eStatus = eFrameworkunifiedStatusInvldParam;
908 //////////////////////////////////////////
909 // Function : FrameworkunifiedGetCurrentUser
910 //////////////////////////////////////////
911 HANDLE FrameworkunifiedGetCurrentUser(HANDLE hApp) {
912 if (frameworkunifiedCheckValidAppHandle(hApp)) {
913 const CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
920 //////////////////////////////////////////
921 // Function : FrameworkunifiedSetUser
922 //////////////////////////////////////////
923 EFrameworkunifiedStatus FrameworkunifiedSetUser(HANDLE hApp, HANDLE hUser) {
924 if (frameworkunifiedCheckValidAppHandle(hApp)) {
925 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
927 return eFrameworkunifiedStatusOK;
929 return eFrameworkunifiedStatusFail;
933 //////////////////////////////////////////
934 // Function : FrameworkunifiedSetAppData
935 //////////////////////////////////////////
936 EFrameworkunifiedStatus FrameworkunifiedSetAppData(HANDLE hApp, PCSTR pKey, PVOID pData) {
937 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
938 if (frameworkunifiedCheckValidAppHandle(hApp)) {
939 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
940 // Insert the data element
941 std::pair<AppData::iterator, bool> l_tRet = pApp->appdata.insert(std::make_pair(pKey, pData));
942 if (false == l_tRet.second) {
943 eStatus = eFrameworkunifiedStatusFail;
945 eStatus = eFrameworkunifiedStatusOK;
951 //////////////////////////////////////////
952 // Function : FrameworkunifiedGetAppData
953 //////////////////////////////////////////
954 PVOID FrameworkunifiedGetAppData(HANDLE hApp, PCSTR pKey) {
955 if (frameworkunifiedCheckValidAppHandle(hApp)) {
956 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
957 AppData::iterator n_iterator = pApp->appdata.find(pKey);
958 if (n_iterator != pApp->appdata.end()) {
959 return n_iterator->second;
966 //////////////////////////////////////////
967 // Function : FrameworkunifiedRemoveAppData
968 //////////////////////////////////////////
969 EFrameworkunifiedStatus FrameworkunifiedRemoveAppData(HANDLE hApp, PCSTR pKey) {
970 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
971 if (frameworkunifiedCheckValidAppHandle(hApp)) {
972 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
973 AppData::iterator n_iterator = pApp->appdata.find(pKey);
974 if (n_iterator != pApp->appdata.end()) {
975 pApp->appdata.erase(n_iterator);
977 eStatus = eFrameworkunifiedStatusOK;
979 eStatus = eFrameworkunifiedStatusFail;