Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_FrameworkCore / src / frameworkunified_framework_dispatch.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 <sys/epoll.h>
25
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>
31
32 #include <map>
33 #include <utility>
34
35 #include "frameworkunified_framework_core.h"
36 ///////////////////////////////////////////////////
37 //        Utility
38 ///////////////////////////////////////////////////
39
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);
47   } else {
48     return NULL;
49   }
50 }
51
52 //////////////////////////////////////////
53 // Function : FrameworkunifiedSetThreadSpecificData
54 //////////////////////////////////////////
55 EFrameworkunifiedStatus FrameworkunifiedSetThreadSpecificData(HANDLE hApp, PVOID data) {
56   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
57
58   if (frameworkunifiedCheckValidAppHandle(hApp)) {
59     CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
60     pApp->FrameworkData = data;
61
62     eStatus = eFrameworkunifiedStatusOK;
63   }
64
65   return eStatus;
66 }
67
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;
75   } else {
76     return NULL;
77   }
78 }
79
80
81
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) {
84   os << "{ ";
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 << ", ";
88   }
89   return os << "}";
90 }
91
92
93
94
95
96
97
98 ///////////////////////////////////////////////////
99 //   Service Protocol attach/detach to dispatcher
100 ///////////////////////////////////////////////////
101
102
103
104
105
106
107
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;
114
115   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName && NULL != handlers && 0 != handlerCount) {
116     // setup callbacks
117     for (UI_32 i = 0; i < handlerCount; ++i) {
118       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedAttachCallbackToDispatcher(hApp, pServiceName, handlers[ i ].iCmd,
119                                                                    handlers[ i ].callBack, hSession))) {
120         break;
121       }
122     }
123   } else {
124     eStatus = eFrameworkunifiedStatusInvldParam;
125   }
126
127   return eStatus;
128 }
129
130
131 /////////////////////////////////////////////
132 // Function : FrameworkunifiedAttachCallbackToDispatcher
133 /////////////////////////////////////////////
134 EFrameworkunifiedStatus FrameworkunifiedAttachCallbackToDispatcher(HANDLE hApp, PCSTR pServiceName, UI_32 iCmd, CbFuncPtr fpOnCmd,
135                                          HANDLE hSession) {
136   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
137
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;
142
143     UI_32 uiSessionId = 0;
144     if (hSession) {
145       uiSessionId = FrameworkunifiedGetSessionId(hSession);
146     }
147
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;
153     }
154
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;
158     }
159
160     session_iterator->second.insert(std::make_pair(iCmd, fpOnCmd));
161   } else {
162     eStatus = eFrameworkunifiedStatusInvldParam;
163   }
164
165   return eStatus;
166 }
167
168 ///////////////////////////////////////////////////
169 // Function : FrameworkunifiedAttachCallbacksToDispatcherWithFd
170 ///////////////////////////////////////////////////
171 EFrameworkunifiedStatus FrameworkunifiedAttachCallbacksToDispatcherWithFd(HANDLE hApp, const FrameworkunifiedFdProtocolCallbackHandler *handlers,
172                                                 UI_32 handlerCount) {
173   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
174
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))) {
180         break;
181       }
182     }
183   } else {
184     eStatus = eFrameworkunifiedStatusInvldParam;
185     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : %d, Invalid param ", eStatus);
186   }
187
188   return eStatus;
189 }
190
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
198
199   if (frameworkunifiedCheckValidAppHandle(hApp) && 0 < fd && NULL != fpOnCmd) {
200     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
201
202     // attach callbackFuncPtr and FD to FdTable.
203     pApp->fds.insert(std::make_pair(fd, fpOnCmd));
204
205     // Monitor FD by epoll.
206     efd = pApp->efd;
207     if (0 < efd) {
208       ev.events = EPOLLIN;
209       ev.data.fd = fd;
210       if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ev)) {
211         /**
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.
213          */
214         eStatus = eFrameworkunifiedStatusFail;
215         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(ADD) Failed, status=%d, errno=%d", eStatus, errno);
216       }
217     } else {
218       eStatus = eFrameworkunifiedStatusFail;
219       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Multi waiting FD is Invalid , status=%d, efd=%d", eStatus, efd);
220     }
221   } else {
222     eStatus = eFrameworkunifiedStatusInvldParam;
223     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : %d, Invalid param ", eStatus);
224   }
225
226   return eStatus;
227 }
228
229 /////////////////////////////////////////////////////
230 // Function : FrameworkunifiedAttachParentCallbacksToDispatcher
231 /////////////////////////////////////////////////////
232 EFrameworkunifiedStatus FrameworkunifiedAttachParentCallbacksToDispatcher(HANDLE hChildApp, const FrameworkunifiedProtocolCallbackHandler *handlers,
233                                                 UI_32 handlerCount) {
234   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
235
236   if (frameworkunifiedCheckValidAppHandle(hChildApp) && NULL != handlers && 0 != handlerCount) {
237     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hChildApp);
238
239     eStatus = FrameworkunifiedAttachCallbacksToDispatcher(hChildApp, pApp->cParentAppName, handlers, handlerCount);
240   } else {
241     eStatus = eFrameworkunifiedStatusInvldParam;
242   }
243
244   return eStatus;
245 }
246
247
248 /////////////////////////////////////////////////////
249 // Function : FrameworkunifiedDetachParentCallbacksFromDispatcher
250 /////////////////////////////////////////////////////
251 EFrameworkunifiedStatus FrameworkunifiedDetachParentCallbacksFromDispatcher(HANDLE hChildApp, const PUI_32 puiCmdArray, UI_32 uiCommandCount) {
252   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
253
254   if (frameworkunifiedCheckValidAppHandle(hChildApp) && NULL != puiCmdArray && 0 != uiCommandCount) {
255     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hChildApp);
256
257     eStatus = FrameworkunifiedDetachCallbacksFromDispatcher(hChildApp, pApp->cParentAppName, puiCmdArray, uiCommandCount);
258   } else {
259     eStatus = eFrameworkunifiedStatusInvldParam;
260   }
261
262   return eStatus;
263 }
264
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;
271
272   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName && NULL != puiCmdArray && 0 != uiCommandCount) {
273     // setup callbacks
274     for (UI_32 i = 0; i < uiCommandCount; ++i) {
275       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(hApp, pServiceName,
276         puiCmdArray[ i ], hSession))) {
277         break;
278       }
279     }
280   } else {
281     eStatus = eFrameworkunifiedStatusInvldParam;
282   }
283
284   return eStatus;
285 }
286
287
288
289 ////////////////////////////////////////////
290 // Function : FrameworkunifiedDetachCallbackFromDispatcher
291 /////////////////////////////////////////////
292 EFrameworkunifiedStatus FrameworkunifiedDetachCallbackFromDispatcher(HANDLE hApp, PCSTR pServiceName, UI_32 iCmd, HANDLE hSession) {
293   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
294
295   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName) {
296     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
297     Services::iterator s_iterator;
298
299     UI_32 uiSessionId = 0;
300     if (hSession) {
301       uiSessionId = FrameworkunifiedGetSessionId(hSession);
302     }
303
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);
312
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);
318         } else {
319           FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cmd NOT found [%d] service [%s]",
320             pApp->cAppName, iCmd, pServiceName);
321         }
322       }
323
324     } else {
325       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cannot find service [%s]", pApp->cAppName, pServiceName);
326       eStatus = eFrameworkunifiedStatusFail;
327     }
328   } else {
329     eStatus = eFrameworkunifiedStatusInvldParam;
330   }
331
332
333   return eStatus;
334 }
335
336 /////////////////////////////////////////////////////
337 // Function : FrameworkunifiedDetachCallbacksFromDispatcherWithFd
338 /////////////////////////////////////////////////////
339 EFrameworkunifiedStatus FrameworkunifiedDetachCallbacksFromDispatcherWithFd(HANDLE hApp, const int *fdArray, UI_32 uiCommandCount) {
340   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
341
342   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != fdArray && 0 != uiCommandCount) {
343     // setup callbacks
344     for (UI_32 i = 0; i < uiCommandCount; ++i) {
345       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDetachCallbackFromDispatcherWithFd(hApp, fdArray[ i ]))) {
346         break;
347       }
348     }
349   } else {
350     eStatus = eFrameworkunifiedStatusInvldParam;
351     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : %d, Invalid param ", eStatus);
352   }
353
354   return eStatus;
355 }
356
357 ////////////////////////////////////////////////////
358 // Function : FrameworkunifiedDetachCallbackFromDispatcherWithFd
359 ////////////////////////////////////////////////////
360 EFrameworkunifiedStatus FrameworkunifiedDetachCallbackFromDispatcherWithFd(HANDLE hApp, int fd) {
361   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
362
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
368
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);
374
375       // Remove the monitoring of FD from multi waiting FD
376       efd = pApp->efd;
377       if (0 < efd) {
378         ev.events = EPOLLIN;
379         ev.data.fd = 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);
383         }
384       } else {
385         eStatus = eFrameworkunifiedStatusFail;
386         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Multi waiting FD is Invalid , status=%d, efd=%d", eStatus, efd);
387       }
388     } else {
389       eStatus = eFrameworkunifiedStatusFail;
390       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cannot find fd [0x%x]", pApp->cAppName, fd);
391     }
392   } else {
393     eStatus = eFrameworkunifiedStatusInvldParam;
394     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, Invalid param ", eStatus);
395   }
396
397
398   return eStatus;
399 }
400
401 ///////////////////////////////////////////////////
402 // Function : FrameworkunifiedDetachServiceFromDispatcher
403 ///////////////////////////////////////////////////
404 EFrameworkunifiedStatus FrameworkunifiedDetachServiceFromDispatcher(HANDLE hApp, PCSTR pServiceName) {
405   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
406
407   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName) {
408     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
409     Services::iterator s_iterator;
410     EventServices::iterator es_iterator;
411
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);
417     } else {
418       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning: Cannot find service [%s]", pApp->cAppName, pServiceName);
419       eStatus = eFrameworkunifiedStatusFail;
420     }
421
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);
428     } else {
429       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "%s : Warning : Cannot find service [%s] in event map",
430         pApp->cAppName, pServiceName);
431     }
432   } else {
433     eStatus = eFrameworkunifiedStatusInvldParam;
434   }
435
436   return eStatus;
437 }
438
439 ///////////////////////////////////////////////////
440 //        Notification functions
441 ///////////////////////////////////////////////////
442
443
444 ////////////////////////////////////////////////////////
445 // Function : FrameworkunifiedAttachNotificationCallbacksToDispatcher
446 ////////////////////////////////////////////////////////
447 EFrameworkunifiedStatus FrameworkunifiedSubscribeNotificationsWithCallback(HANDLE hApp,  const FrameworkunifiedNotificationCallbackHandler *pNtfyHandler,
448                                                  UI_32 uiHandlerCount) {
449   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
450
451   if (frameworkunifiedCheckValidAppHandle(hApp) && pNtfyHandler && (uiHandlerCount > 0)) {
452     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
453
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);
462         // service found
463         pApp->notifications.insert(std::make_pair(tmp_notification, pNtfyHandler[ i ].callBack));
464
465         FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : attaching call-back for notification [%s]", pApp->cAppName,
466                pNtfyHandler[ i ].cNotification);
467       }
468     } else {
469       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to Subscribe to batch set of notifications",
470         pApp->cAppName, eStatus);
471     }
472   } else {
473     eStatus = eFrameworkunifiedStatusInvldParam;
474   }
475
476   return eStatus;
477 }
478
479 ////////////////////////////////////////////////////////
480 // Function : FrameworkunifiedAttachNotificationCallbackToDispatcher
481 ////////////////////////////////////////////////////////
482 EFrameworkunifiedStatus FrameworkunifiedSubscribeNotificationWithCallback(HANDLE hApp, PCSTR pNotification, CbFuncPtr fpOnCmd) {
483   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
484
485   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNotification && NULL != fpOnCmd) {
486     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
487     NotificationTableRetStatus mRet;
488
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);
495
496       // service found
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);
502       } else {
503         FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s:attaching call-back for notification [%s]",
504           pApp->cAppName, pNotification);
505       }
506     } else {
507       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s:Error:[%d] Unable to Subscribe to notification [%s]",
508         pApp->cAppName, eStatus, pNotification);
509     }
510   } else {
511     eStatus = eFrameworkunifiedStatusInvldParam;
512   }
513
514   return eStatus;
515 }
516
517
518
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;
527
528   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNtfyHandler && (uiHandlerCount > 0)) {
529     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
530
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);
537
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);
542         } else {
543           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : Cannot find notification [%s]", pApp->cAppName,
544                  pNotification != 0 ? pNotification : NULL);
545           eStatus = eFrameworkunifiedStatusFail;
546         }
547       }
548
549       FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%s : detaching call-back for notification [%s]", pApp->cAppName,
550              pNotification != 0 ? pNotification : NULL);
551     } else {
552       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s : Error : [%d] Unable to UnSubscribe from notifications ", pApp->cAppName,
553              eFrameworkunifiedStatusOK);
554     }
555   } else {
556     eStatus = eFrameworkunifiedStatusInvldParam;
557   }
558
559   return eStatus;
560 }
561
562
563 ////////////////////////////////////////////////////////
564 // Function : FrameworkunifiedDetachNotificationCallbackToDispatcher
565 ////////////////////////////////////////////////////////
566 EFrameworkunifiedStatus FrameworkunifiedUnsubscribeNotificationWithCallback(HANDLE hApp, PCSTR pNotification) {
567   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
568
569   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pNotification) {
570     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
571     NotificationTable::iterator n_iterator;
572
573     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedNPUnsubscribeFromNotification(hApp, pNotification))) {
574       // \todo : error handling on all map function calls
575
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);
580
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);
585       } else {
586         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s:Error:Cannot find notification [%s]", pApp->cAppName, pNotification);
587         eStatus = eFrameworkunifiedStatusFail;
588       }
589     } else {
590       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "%s:Error:[%d] Unable to UnSubscribe from notification [%s]",
591         pApp->cAppName, eStatus, pNotification);
592     }
593   } else {
594     eStatus = eFrameworkunifiedStatusInvldParam;
595   }
596
597   return eStatus;
598 }
599
600 ///////////////////////////////////////////////////
601 //        Defer Message functions
602 ///////////////////////////////////////////////////
603
604 ////////////////////////////////////////////////////////
605 // Function : FrameworkunifiedGetDeferQueueCnt
606 ////////////////////////////////////////////////////////
607 UI_32 FrameworkunifiedGetDeferQueueCnt(HANDLE hApp) {
608   UI_32 l_uiCnt = 0;
609
610   if (frameworkunifiedCheckValidAppHandle(hApp)) {
611     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
612     l_uiCnt = static_cast<UI_32>(pApp->deferedMsgQueue.size());
613   }
614
615   return l_uiCnt;
616 }
617
618 ////////////////////////////////////////////////////////
619 // Function : FrameworkunifiedIsDeferQueueEmpty
620 ////////////////////////////////////////////////////////
621 BOOL FrameworkunifiedIsDeferQueueEmpty(HANDLE hApp) {
622   BOOL l_bIsQEmpty = TRUE;
623
624   if (frameworkunifiedCheckValidAppHandle(hApp)) {
625     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
626     l_bIsQEmpty = static_cast<BOOL>(pApp->deferedMsgQueue.empty());
627   }
628
629   return l_bIsQEmpty;
630 }
631
632 ////////////////////////////////////////////////////////
633 // Function : FrameworkunifiedDeferMessage
634 ////////////////////////////////////////////////////////
635 EFrameworkunifiedStatus FrameworkunifiedDeferMessage(HANDLE hApp) {
636   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
637
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);
642   } else {
643     eStatus = eFrameworkunifiedStatusInvldParam;
644   }
645
646   return eStatus;
647 }
648
649 ////////////////////////////////////////////////////////
650 // Function : FrameworkunifiedClearDeferMessages
651 ////////////////////////////////////////////////////////
652 EFrameworkunifiedStatus FrameworkunifiedClearDeferMessages(HANDLE hApp) {
653   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
654
655   if (frameworkunifiedCheckValidAppHandle(hApp)) {
656     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
657
658     // clear the pop flag!
659     pApp->fPopDeferedMsg = FALSE;
660
661     // remove all items from the queue!
662     while (!pApp->deferedMsgQueue.empty()) {
663       pApp->deferedMsgQueue.pop();
664     }
665   } else {
666     eStatus = eFrameworkunifiedStatusInvldParam;
667   }
668
669   return eStatus;
670 }
671
672 ////////////////////////////////////////////////////////
673 // Function : FrameworkunifiedRetrieveDeferMessage
674 ////////////////////////////////////////////////////////
675 EFrameworkunifiedStatus FrameworkunifiedRetrieveDeferMessage(HANDLE hApp) {
676   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
677   uint64_t data = 1;
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);
686       }
687     }
688
689
690   } else {
691     eStatus = eFrameworkunifiedStatusInvldParam;
692   }
693
694   return eStatus;
695 }
696
697
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;
704
705   if (frameworkunifiedCheckValidAppHandle(hApp)) {
706     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
707     if (NULL != pApp->m_pFrameworkunifiedStateMachine) {
708       l_bIsStateMachine = TRUE;
709     }
710   }
711
712   return l_bIsStateMachine;
713 }
714
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!!!!");
721   return NULL;
722 }
723
724 EFrameworkunifiedStatus FrameworkunifiedSetMandatoryServiceInfo(HANDLE hApp, PCSTR pNotification, UI_32 uiEventId) {
725   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
726
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);
734     } else {
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;
739     }
740   } else {
741     eStatus = eFrameworkunifiedStatusInvldParam;
742   }
743
744   return eStatus;
745 }
746
747 BOOL FrameworkunifiedIsServiceAvailable(HANDLE hApp) {
748   BOOL l_bIsServiceAvailable = FALSE;
749
750   if (frameworkunifiedCheckValidAppHandle(hApp)) {
751     // Publish notification
752     ServiceAvailability tServiceAvailability = {};
753
754     UI_32 l_uiLength = FrameworkunifiedGetMsgLength(hApp);
755
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");
760       } else {
761         // Check the Service availability
762         if (eFrameworkunifiedServiceAvailable == tServiceAvailability.eServiceAvailability) {
763           l_bIsServiceAvailable = TRUE;
764         }
765       }
766     } else {
767       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error: Received message data size not matched :: %d", l_uiLength);
768     }
769   }
770
771   return l_bIsServiceAvailable;
772 }
773
774 EFrameworkunifiedStatus FrameworkunifiedPublishServiceAvailability(HANDLE hApp, BOOL bIsAvailable) {
775   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
776
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));
784
785       if (bIsAvailable) {
786         tServiceAvailability.eServiceAvailability = eFrameworkunifiedServiceAvailable;
787       } else {
788         tServiceAvailability.eServiceAvailability = eFrameworkunifiedServiceNotAvailable;
789       }
790
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);
796       } else {
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");
801       }
802     } else {
803       eStatus = eFrameworkunifiedStatusFail;
804     }
805   } else {
806     eStatus = eFrameworkunifiedStatusInvldParam;
807   }
808
809   return eStatus;
810 }
811
812 BOOL FrameworkunifiedGetSelfAvailability(HANDLE hApp) {
813   if (frameworkunifiedCheckValidAppHandle(hApp)) {
814     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
815     return pApp->bIsAvailable;
816   }
817
818   return FALSE;
819 }
820
821 EFrameworkunifiedStatus FrameworkunifiedRegisterServiceAvailabilityNotification(HANDLE hApp, PCSTR pNotification) {
822   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
823
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));
828
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);
833       } else {
834         FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "FrameworkunifiedNPRegisterNotifications %s success Status:0x%x ",
835           pNotification, eStatus);
836       }
837     } else {
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;
842     }
843   } else {
844     eStatus = eFrameworkunifiedStatusInvldParam;
845   }
846
847   return eStatus;
848 }
849
850 EFrameworkunifiedStatus FrameworkunifiedUnRegisterServiceAvailabilityNotification(HANDLE hApp) {
851   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
852
853   if (frameworkunifiedCheckValidAppHandle(hApp)) {
854     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
855
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);
860     } else {
861       FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "FrameworkunifiedNPUnRegisterNotifications %s success Status:0x%x ",
862              pApp->sServiceAvailabilityNotification, eStatus);
863     }
864     memset(pApp->sServiceAvailabilityNotification, 0, MAX_SYS_INFO_SIZE);
865     pApp->bIsAvailable = FALSE;
866   } else {
867     eStatus = eFrameworkunifiedStatusInvldParam;
868   }
869
870   return eStatus;
871 }
872
873 ////////////////////////////////////////////////////////////////////////////////////////////
874 /// FrameworkunifiedGetServiceNameOnServiceAvailabilityNotification
875 /// To be used when client receives service availability notification to get the available
876 /// service name.
877 ////////////////////////////////////////////////////////////////////////////////////////////
878 EFrameworkunifiedStatus FrameworkunifiedGetServiceNameOnServiceAvailabilityNotification(HANDLE hApp, PSTR pServiceName) {
879   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
880   // Publish notification
881   ServiceAvailability tServiceAvailability;
882
883   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName) {
884     UI_32 l_uiLength = FrameworkunifiedGetMsgLength(hApp);
885
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;
891       } else {
892         if (NULL == std::strncpy(pServiceName, tServiceAvailability.cServiceName, MAX_NAME_SIZE_APP)) {
893           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " strcpy failed Failed");
894           eStatus = eFrameworkunifiedStatusFail;
895         }
896       }
897     } else {
898       eStatus = eFrameworkunifiedStatusFail;
899       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error: Received message data size not matched :: %d", l_uiLength);
900     }
901   } else {
902     eStatus = eFrameworkunifiedStatusInvldParam;
903   }
904
905   return eStatus;
906 }
907
908 //////////////////////////////////////////
909 // Function : FrameworkunifiedGetCurrentUser
910 //////////////////////////////////////////
911 HANDLE FrameworkunifiedGetCurrentUser(HANDLE hApp) {
912   if (frameworkunifiedCheckValidAppHandle(hApp)) {
913     const CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
914     return pApp->hUser;
915   } else {
916     return NULL;
917   }
918 }
919
920 //////////////////////////////////////////
921 // Function : FrameworkunifiedSetUser
922 //////////////////////////////////////////
923 EFrameworkunifiedStatus FrameworkunifiedSetUser(HANDLE hApp, HANDLE hUser) {
924   if (frameworkunifiedCheckValidAppHandle(hApp)) {
925     CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast<CFrameworkunifiedFrameworkApp *>(hApp);
926     pApp->hUser = hUser;
927     return eFrameworkunifiedStatusOK;
928   } else {
929     return eFrameworkunifiedStatusFail;
930   }
931 }
932
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;
944     } else {
945       eStatus = eFrameworkunifiedStatusOK;
946     }
947   }
948   return eStatus;
949 }
950
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;
960     }
961   }
962   return NULL;
963 }
964
965
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);
976
977       eStatus = eFrameworkunifiedStatusOK;
978     } else {
979       eStatus = eFrameworkunifiedStatusFail;
980     }
981   }
982   return eStatus;
983 }