Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / framework_unified / client / NS_FrameworkCore / src / frameworkunified_framework_session.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    Framework service protocol session APIs implementation
20 ///
21 ///
22 ///
23 ///////////////////////////////////////////////////////////////////////////////
24 #include <string.h>
25
26 #include <native_service/frameworkunified_framework_if.h>
27 #include <native_service/frameworkunified_framework_types.h>
28 #include <native_service/frameworkunified_service_protocol.h>
29 #include <native_service/ns_logger_if.h>
30 #include <other_service/strlcpy.h>
31 #include <utility>
32 #include "frameworkunified_framework_core.h"
33 #include "frameworkunified_framework_internal.h"
34
35 //////////////////////////////////////////////////////
36 /// FrameworkunifiedOpenService
37 //////////////////////////////////////////////////////
38 HANDLE FrameworkunifiedOpenService(HANDLE hApp, PCSTR pServiceName) {
39   HANDLE hService = NULL;
40
41   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pServiceName) {
42     if (NULL == (hService = FrameworkunifiedMcOpenSender(hApp, pServiceName))) {
43       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
44       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error::hApp:0x%p, %s failed to FrameworkunifiedMcOpenSender", hApp, pServiceName);
45       // LCOV_EXCL_BR_STOP
46     } else {
47       CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
48       frameworkunifiedAddConnectMonitor(hApp, pServiceName, pApp->cAppName);
49     }
50   } else {
51     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__,
52            "Invalid param. hApp:0x%p, name:0x%p",
53            hApp, pServiceName);
54   }
55
56   return static_cast<HANDLE>(hService);
57 }
58
59 //////////////////////////////////////////////////////
60 /// FrameworkunifiedCloseService
61 //////////////////////////////////////////////////////
62 EFrameworkunifiedStatus FrameworkunifiedCloseService(HANDLE hApp, HANDLE hService) {
63   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
64
65   if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hService)) {
66     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
67     MsgQInfo *pMsgQ = static_cast<MsgQInfo *>(hService);
68
69     if (eFrameworkunifiedStatusOK != frameworkunifiedDelConnectMonitor(hApp, pMsgQ->cMsgQName, pApp->cAppName)) {
70       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : frameworkunifiedDelConnectMonitor(%s, %s) fail",
71              pMsgQ->cMsgQName != 0 ? pMsgQ->cMsgQName : NULL, pApp->cAppName != 0 ? pApp->cAppName : NULL);
72     }
73
74     if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedMcClose(hService))) {
75       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : %s Failed to FrameworkunifiedMcClose",
76              pApp->cAppName != 0 ? pApp->cAppName : NULL);
77     }
78   } else {
79     eStatus = eFrameworkunifiedStatusInvldHandle;
80   }
81
82   return eStatus;
83 }
84
85 HANDLE FrameworkunifiedGenerateSessionHandle(HANDLE hApp, PCSTR pServiceName) {
86   HANDLE l_pSession = NULL;
87   if ((frameworkunifiedCheckValidAppHandle(hApp)) && (NULL != pServiceName)) {
88     l_pSession = FrameworkunifiedMcOpenSender(hApp, pServiceName);
89
90     if (NULL != l_pSession) {
91       MsgQInfo *pMsgQ    = reinterpret_cast<MsgQInfo *>(l_pSession);
92       pMsgQ->sessionId = FrameworkunifiedGenerateNewSessionId();
93     }
94   }
95
96   return l_pSession;
97 }
98
99 //////////////////////////////////////////////////////
100 /// FrameworkunifiedOpenSessionWithData
101 //////////////////////////////////////////////////////
102 EFrameworkunifiedStatus FrameworkunifiedOpenSessionWithData(HANDLE hService, PVOID pData, UI_32 length) {
103   return FrameworkunifiedSendMsg(hService, PROTOCOL_OPEN_SESSION_REQ, length, pData);
104 }
105
106
107 //////////////////////////////////////////////////////
108 /// FrameworkunifiedOpenSession
109 //////////////////////////////////////////////////////
110 EFrameworkunifiedStatus FrameworkunifiedOpenSession(HANDLE hService) {
111   UI_32 l_uiDummy = 0;
112   return FrameworkunifiedSendMsg(hService, PROTOCOL_OPEN_SESSION_REQ, 0, &l_uiDummy);
113 }
114
115 //////////////////////////////////////////////////////
116 /// FrameworkunifiedOpenSessionWithDataSync
117 //////////////////////////////////////////////////////
118 EFrameworkunifiedStatus FrameworkunifiedOpenSessionWithDataSync(HANDLE hService, PVOID pData, UI_32 length, OpenSessionAck *ack) {
119   UI_32 rcvLen;
120   return FrameworkunifiedInvokeSync(hService, PROTOCOL_OPEN_SESSION_REQ_SYNC, length, pData, sizeof(OpenSessionAck), ack, &rcvLen);
121 }
122
123
124 //////////////////////////////////////////////////////
125 /// FrameworkunifiedOpenSessionSync
126 //////////////////////////////////////////////////////
127 EFrameworkunifiedStatus FrameworkunifiedOpenSessionSync(HANDLE hService, OpenSessionAck *ack) {
128   UI_32 l_uiDummy = 0;
129   UI_32 rcvLen;
130   return FrameworkunifiedInvokeSync(hService, PROTOCOL_OPEN_SESSION_REQ_SYNC, 0, &l_uiDummy, sizeof(OpenSessionAck), ack, &rcvLen);
131 }
132
133 //////////////////////////////////////////////////////
134 /// FrameworkunifiedGetSessionHandle
135 //////////////////////////////////////////////////////
136 HANDLE FrameworkunifiedGetOpenSessionHandle(HANDLE hApp) {
137   HANDLE hSession = NULL;
138
139   if (frameworkunifiedCheckValidAppHandle(hApp)) {
140     OpenSessionAck tAck;
141
142     if (sizeof(OpenSessionAck) == FrameworkunifiedGetMsgLength(hApp)) {
143       if (eFrameworkunifiedStatusOK != FrameworkunifiedGetMsgDataOfSize(hApp, &tAck, sizeof(tAck))) {
144           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : FrameworkunifiedGetMsgDataOfSize");
145       } else {
146         if (eFrameworkunifiedStatusOK == tAck.eStatus) {
147           hSession = FrameworkunifiedMcOpenSender(hApp, tAck.cSessionName);
148           if (NULL != hSession) {
149             (reinterpret_cast<MsgQInfo *>(hSession))->sessionId = tAck.sessionId;
150           }
151         }
152       }
153     } else {
154         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : FrameworkunifiedGetOpenSessionHandle");
155     }
156   }
157
158   return hSession;
159 }
160
161 //////////////////////////////////////////////////////
162 /// FrameworkunifiedGetOpenSessionSyncHandle
163 //////////////////////////////////////////////////////
164 HANDLE FrameworkunifiedGetOpenSessionSyncHandle(HANDLE hApp, OpenSessionAck *tAck) {
165   HANDLE hSession = NULL;
166
167   if (frameworkunifiedCheckValidAppHandle(hApp) && (NULL != tAck)) {
168     if (eFrameworkunifiedStatusOK == tAck->eStatus) {
169       hSession = FrameworkunifiedMcOpenSender(hApp, tAck->cSessionName);
170       if (NULL != hSession) {
171         (reinterpret_cast<MsgQInfo *>(hSession))->sessionId = tAck->sessionId;
172       }
173     }
174   }
175
176   return hSession;
177 }
178
179 //////////////////////////////////////////////////////
180 /// frameworkunifiedCloseSessionInner
181 //////////////////////////////////////////////////////
182 static EFrameworkunifiedStatus frameworkunifiedCloseSessionInner(HANDLE hService, HANDLE hSession, CloseSessionAck *ack) {
183   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldHandle;
184   CloseSessionReq tClose = {};
185
186   if (frameworkunifiedCheckValidMsgQ(hService) && frameworkunifiedCheckValidMsgQ(hSession)) {
187     tClose.sessionId = (reinterpret_cast<MsgQInfo *>(hSession))->sessionId;
188     strlcpy(tClose.cSessionName, (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName, sizeof(tClose.cSessionName));
189
190     FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Info : sessionid %d", tClose.sessionId);
191
192     if (NULL != ack) {
193       UI_32 rcvLen;
194       eStatus = FrameworkunifiedInvokeSync(hService, PROTOCOL_CLOSE_SESSION_REQ_SYNC, sizeof(tClose), (PVOID)&tClose,
195                               sizeof(CloseSessionAck), ack, &rcvLen);
196       if (rcvLen != sizeof(CloseSessionAck)) {
197         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : Invalid receive length %d", rcvLen);
198       }
199     } else {
200       eStatus = FrameworkunifiedSendMsg(hService, PROTOCOL_CLOSE_SESSION_REQ, sizeof(tClose), (PVOID)&tClose);
201     }
202     if (eFrameworkunifiedStatusOK == eStatus) {
203       eStatus = FrameworkunifiedMcClose(hSession);
204       hSession = NULL;
205     }
206   }
207   return eStatus;
208 }
209
210 //////////////////////////////////////////////////////
211 /// FrameworkunifiedCloseSession
212 //////////////////////////////////////////////////////
213 EFrameworkunifiedStatus FrameworkunifiedCloseSession(HANDLE hService, HANDLE hSession) {
214   return frameworkunifiedCloseSessionInner(hService, hSession, NULL);
215 }
216
217 //////////////////////////////////////////////////////
218 /// FrameworkunifiedCloseSessionSync
219 //////////////////////////////////////////////////////
220 EFrameworkunifiedStatus FrameworkunifiedCloseSessionSync(HANDLE hService, HANDLE hSession, CloseSessionAck *ack) {
221   if (NULL == ack) {
222     return eFrameworkunifiedStatusInvldParam;
223   }
224   return frameworkunifiedCloseSessionInner(hService, hSession, ack);
225 }
226
227 //////////////////////////////////////////////////////
228 /// FrameworkunifiedGetSessionId -> On Client side
229 //////////////////////////////////////////////////////
230 UI_32 FrameworkunifiedGetSessionId(HANDLE hSession) {
231   if (frameworkunifiedCheckValidMsgQ(hSession)) {
232     return ((reinterpret_cast<MsgQInfo *>(hSession))->sessionId);
233   } else {
234     return MAX_SESSION_ID_VAL;
235   }
236 }
237
238 //////////////////////////////////////////////////////
239 /// FrameworkunifiedGetSessionId -> On Client side
240 //////////////////////////////////////////////////////
241 UI_32 FrameworkunifiedGetMsgSessionId(HANDLE hApp) {
242   if (frameworkunifiedCheckValidAppHandle(hApp)) {
243     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
244     return pApp->uiSessionId;
245   }
246   return MAX_SESSION_ID_VAL;
247 }
248
249
250 //////////////////////////////////////////////////////
251 /// GenerateNewSessionId -> On Server side
252 //////////////////////////////////////////////////////
253 UI_32 FrameworkunifiedGenerateNewSessionId() {
254   static UI_16 lastSessionId = 0;
255   lastSessionId++;
256   UI_32 sessionId = (lastSessionId % MAX_SESSION_ID_VAL);
257   return sessionId;
258 }
259
260
261
262 //////////////////////////////////////////////////////
263 /// FrameworkunifiedRegisterEvents
264 //////////////////////////////////////////////////////
265 EFrameworkunifiedStatus FrameworkunifiedRegisterEvents(HANDLE hSession, PVOID puiEventsArray, UI_32 uiListSize) {
266   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
267
268   if (hSession) {
269     eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_REGISTER_EVENTS,
270       static_cast<UI_32>(uiListSize * sizeof(uiListSize)), puiEventsArray);
271   } else {
272     eStatus = eFrameworkunifiedStatusInvldHandle;
273   }
274
275   return eStatus;
276 }
277
278 //////////////////////////////////////////////////////
279 /// FrameworkunifiedRegisterEvent
280 //////////////////////////////////////////////////////
281 EFrameworkunifiedStatus FrameworkunifiedRegisterEvent(HANDLE hSession, UI_32 uiEvent) {
282   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
283
284   if (hSession) {
285     eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_REGISTER_EVENTS, sizeof(uiEvent), (PVOID)&uiEvent);
286   } else {
287     eStatus = eFrameworkunifiedStatusInvldHandle;
288   }
289
290   return eStatus;
291 }
292
293 //////////////////////////////////////////////////////
294 /// FrameworkunifiedUnRegisterEvents
295 //////////////////////////////////////////////////////
296 EFrameworkunifiedStatus FrameworkunifiedUnRegisterEvents(HANDLE hSession, PVOID puiEventsArray, UI_32 uiListSize) {
297   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
298
299   if (hSession) {
300     eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_UNREGISTER_EVENTS,
301       static_cast<UI_32>(uiListSize * sizeof(uiListSize)), puiEventsArray);
302   } else {
303     eStatus = eFrameworkunifiedStatusInvldHandle;
304   }
305
306   return eStatus;
307 }
308
309 //////////////////////////////////////////////////////
310 /// FrameworkunifiedUnRegisterEvent
311 //////////////////////////////////////////////////////
312 EFrameworkunifiedStatus FrameworkunifiedUnRegisterEvent(HANDLE hSession, UI_32 uiEvent) {
313   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
314
315   if (hSession) {
316     eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_UNREGISTER_EVENTS, sizeof(uiEvent), (PVOID)&uiEvent);
317   } else {
318     eStatus = eFrameworkunifiedStatusInvldHandle;
319   }
320
321   return eStatus;
322 }
323
324 //////////////////////////////////////////////////////
325 /// FrameworkunifiedDefineStateEvents
326 //////////////////////////////////////////////////////
327 EFrameworkunifiedStatus FrameworkunifiedDefineStateEvents(HANDLE hApp, PVOID puiEvents, UI_32 uiListSize) {
328   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
329
330   if (frameworkunifiedCheckValidAppHandle(hApp) && puiEvents) {
331     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
332
333     EventData *l_pEventData = NULL;
334
335     UI_32 l_uiEventId;
336     PUI_32 l_pEventList = (PUI_32)puiEvents;
337
338     for (UI_32 uiCount = 0; uiCount < uiListSize; uiCount++) {
339       l_uiEventId = l_pEventList[uiCount];
340
341       // checks if event exists in state event list
342       if (pApp->publicstateeventtable.end() == pApp->publicstateeventtable.find(l_uiEventId)) {
343         // insert the event in state event list, data is passed as NULL as there is no data published
344         pApp->publicstateeventtable.insert(std::make_pair(l_uiEventId, l_pEventData));
345       } else {
346         FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Event %d already registered as state event ", l_uiEventId);
347       }
348     }
349   } else {
350     eStatus = eFrameworkunifiedStatusInvldHandle;
351   }
352
353   return eStatus;
354 }
355
356 //////////////////////////////////////////////////////
357 /// FrameworkunifiedBroadcastEvent
358 //////////////////////////////////////////////////////
359 EFrameworkunifiedStatus FrameworkunifiedBroadcastEvent(HANDLE hApp, UI_32 uiEventId, PCVOID pData, UI_32 uiLength) {
360   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
361
362   if (frameworkunifiedCheckValidAppHandle(hApp)) {
363     eStatus = FrameworkunifiedPublishEvent(hApp, uiEventId, NULL, pData, uiLength);
364   } else {
365     eStatus = eFrameworkunifiedStatusNullPointer;
366   }
367
368   return eStatus;
369 }
370
371 //////////////////////////////////////////////////////
372 /// FrameworkunifiedPublishEvent
373 //////////////////////////////////////////////////////
374 EFrameworkunifiedStatus FrameworkunifiedPublishEvent(HANDLE hApp, UI_32 uiEventId, PCSTR pClientName, PCVOID pData, UI_32 uiLength) {
375   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
376   EFrameworkunifiedStatus eRetStatus = eFrameworkunifiedStatusOK;
377
378   if (frameworkunifiedCheckValidAppHandle(hApp)) {
379     EventTable::iterator e_iterator;
380     ServiceSessionIdListTable::iterator ssidl_iterator;
381
382     PCSTR l_cServiceName = "";
383
384     // set the data, if event is a state events
385     UpdatePublicStateEventData(hApp, uiEventId, pData, uiLength);
386
387     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
388
389     // check if event is registered by client/s
390     e_iterator = pApp->eventtable.find(uiEventId);
391
392     if (pApp->eventtable.end() != e_iterator) {
393       // for loop for event table
394       for (ssidl_iterator = e_iterator->second.begin();
395            ssidl_iterator != e_iterator->second.end();
396            ssidl_iterator++) {
397         l_cServiceName = ssidl_iterator->first.c_str();
398
399         if (NULL != pClientName) {
400           // send message to only specified client
401           if (!std::strcmp(pClientName, l_cServiceName)) {
402             eStatus = SendEventMessage(hApp, ssidl_iterator->second, uiEventId, l_cServiceName, pData, uiLength);
403             break;
404           }
405         } else {
406           // send message to all registered client
407           eRetStatus = SendEventMessage(hApp, ssidl_iterator->second, uiEventId, l_cServiceName, pData, uiLength);
408           if (eFrameworkunifiedStatusOK != eRetStatus) {
409             eStatus = eRetStatus;
410           }
411         }
412       }
413     } else {
414       FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "No subscriber has subscribed for event %d.", uiEventId);
415     }
416   } else {
417     eStatus = eFrameworkunifiedStatusInvldHandle;
418   }
419
420   return eStatus;
421 }
422
423 //////////////////////////////////////////////////////
424 /// UpdatePublicStateEventData
425 //////////////////////////////////////////////////////
426 EFrameworkunifiedStatus UpdatePublicStateEventData(HANDLE hApp, UI_32 uiEventId, PCVOID pData, UI_32 uiLength) {
427   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
428
429   if (frameworkunifiedCheckValidAppHandle(hApp)) {
430     if ((NULL == pData) && (uiLength > 0)) {
431       eStatus = eFrameworkunifiedStatusInvldParam;
432     } else {
433       PublicStateEventTable::iterator se_iterator;
434       EventData *l_pEventData = NULL;
435
436       CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
437
438       // set the data, if event is a state events
439       se_iterator = pApp->publicstateeventtable.find(uiEventId);
440       if (pApp->publicstateeventtable.end() != se_iterator) {
441         l_pEventData = se_iterator->second;
442         if (NULL != l_pEventData) {
443           if (NULL != l_pEventData->pData) {
444             delete[](static_cast<PCHAR>(l_pEventData->pData));
445             (se_iterator->second)->pData = NULL;
446           }
447
448           delete l_pEventData;
449           se_iterator->second = NULL;
450         }
451
452         l_pEventData = new(std::nothrow) EventData();
453         /*
454          * @todo
455          * UpdatePublicStateEventData() in the pApp->publicstateeventtable
456          * The data area is being created, but there is no process to release.
457          */
458         if (NULL != l_pEventData) {
459           l_pEventData->uiLength = uiLength;
460           // LCOV_EXCL_BR_START 6:pData is checked in the top of this function, it should be not NULL here.
461           if (NULL == pData) {
462           // LCOV_EXCL_BR_STOP
463             // LCOV_EXCL_START 6:pData is checked in the top of this function, it should be not NULL here.
464             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
465             l_pEventData->pData = NULL;
466             // LCOV_EXCL_STOP
467           } else {
468             l_pEventData->pData = new(std::nothrow) CHAR[uiLength];
469             if (NULL != l_pEventData->pData) {
470               std::memset(l_pEventData->pData, 0, uiLength);
471               std::memcpy(l_pEventData->pData, pData, uiLength);
472             } else {
473               eStatus = eFrameworkunifiedStatusNullPointer;
474             }
475           }
476
477           se_iterator->second = l_pEventData;
478         } else {
479           eStatus = eFrameworkunifiedStatusNullPointer;
480         }
481       }
482     }
483   } else {
484     eStatus = eFrameworkunifiedStatusInvldParam;
485   }
486
487   return eStatus;
488 }
489
490 //////////////////////////////////////////////////////
491 /// SendEventMessage
492 //////////////////////////////////////////////////////
493 EFrameworkunifiedStatus SendEventMessage(HANDLE hApp,
494                             const SessionIdList &vSessionIdList,
495                             UI_32 uiEventId,
496                             PCSTR cServiceName,
497                             PCVOID pData,
498                             UI_32 uiLength) {
499   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
500   EFrameworkunifiedStatus eStatusSendMsg = eFrameworkunifiedStatusOK;
501
502   UI_32 l_uiSessionId = 0;
503   HANDLE l_hClientHandle = NULL;
504
505   if ((frameworkunifiedCheckValidAppHandle(hApp)) && (NULL != cServiceName)) {
506     for (UI_32 l_uiCount = 0;
507          l_uiCount < vSessionIdList.size();
508          l_uiCount++) {
509       l_uiSessionId = vSessionIdList[l_uiCount];
510       l_hClientHandle = GetMsgQueueHandle(hApp, cServiceName, l_uiSessionId);
511
512       // send message to all the registered sessions of the service
513       if (NULL != l_hClientHandle) {
514         if (eFrameworkunifiedStatusOK != (eStatusSendMsg = FrameworkunifiedSendMsg(l_hClientHandle, uiEventId, uiLength, pData))) {
515           eStatus = eFrameworkunifiedStatusErrOther;
516           FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "FrameworkunifiedSendMsg failed for EventID %d to service %s, status %d",
517                  uiEventId, cServiceName, eStatusSendMsg);
518         }
519       } else {
520         FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Unable to send event %d to service %s, session %d. NULL handle found.",
521                uiEventId, cServiceName, l_uiSessionId);
522         eStatus = eFrameworkunifiedStatusErrOther;
523       }
524     }
525   } else {
526     eStatus = eFrameworkunifiedStatusInvldParam;
527   }
528
529   return eStatus;
530 }
531
532 //////////////////////////////////////////////////////
533 /// RemoveEventEntryFromEventTable
534 //////////////////////////////////////////////////////
535 EFrameworkunifiedStatus RemoveEventEntryFromEventTable(HANDLE hApp, UI_32 uiEventId,
536                                           PCSTR cServiceName, const UI_32 uiSessionId) {
537   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
538
539   if ((frameworkunifiedCheckValidAppHandle(hApp)) && (NULL != cServiceName)) {
540     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
541
542     EventTable::iterator e_iterator;
543     ServiceSessionIdListTable::iterator ssidl_iterator;
544
545     // event table = event id, service name, vector of session ids
546     // check if event is registered by client/s
547     e_iterator = pApp->eventtable.find(uiEventId);
548
549     if (pApp->eventtable.end() != e_iterator) {
550       ssidl_iterator = e_iterator->second.find(cServiceName);
551
552       if (e_iterator->second.end() != ssidl_iterator) {
553         // search for the session id in list of registered session of service with the event uiEventId
554         for (UI_32 l_uiCount = 0;
555              l_uiCount < ssidl_iterator->second.size();
556              l_uiCount++) {
557           if (uiSessionId == ssidl_iterator->second[l_uiCount]) {
558             ssidl_iterator->second.erase(ssidl_iterator->second.begin() + l_uiCount);
559             break;
560           }
561         }
562
563         // no other session of service cServiceName is registered with the event uiEventId
564         if (ssidl_iterator->second.empty()) {
565           e_iterator->second.erase(ssidl_iterator);
566
567           // no other session is registered for the event uiEventId
568           if (e_iterator->second.empty()) {
569             pApp->eventtable.erase(e_iterator);
570           }
571         }
572       }
573     }
574   } else {
575     eStatus = eFrameworkunifiedStatusInvldParam;
576   }
577
578   return eStatus;
579 }
580
581 ////////////////////////////////////////////
582 // Function : CleanAllEventsOfSession
583 ////////////////////////////////////////////
584 EFrameworkunifiedStatus CleanAllEventsOfSession(HANDLE hApp, PCSTR serviceName, UI_32 sessionId) {
585   FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "+");
586   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
587
588   if ((frameworkunifiedCheckValidAppHandle(hApp)) && (NULL != serviceName)) {
589     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
590
591     ServiceSessionHandleTable::iterator ssh_iterator;
592     SessionToEventInfo::iterator sh_iterator;
593     HANDLE l_pClientHandle = NULL;
594     SessionEventInfo *l_ptSessionEventInfo = NULL;
595
596     // check whether client has registered for private event/s
597     ssh_iterator = pApp->servicesessionhandletable.find(serviceName);
598
599     if (pApp->servicesessionhandletable.end() != ssh_iterator) {
600       // get the table of session id and session handle of the received service over which private events are
601       // registered
602       sh_iterator =  ssh_iterator->second.find(sessionId);
603
604       // client has registered for private event/s over this session
605       if (ssh_iterator->second.end() != sh_iterator) {
606         l_ptSessionEventInfo = sh_iterator->second;
607
608         if (NULL != l_ptSessionEventInfo) {
609           l_pClientHandle = l_ptSessionEventInfo->m_hSession;
610           if (NULL != l_pClientHandle) {
611             // close the session handle
612             eStatus = FrameworkunifiedMcClose(l_pClientHandle);
613             l_ptSessionEventInfo->m_hSession = NULL;
614           }
615           // Remove all the event entries from Event registry table
616           for (UI_32 l_uiCount = 0;
617                l_uiCount < l_ptSessionEventInfo->m_vEvents.size();
618                l_uiCount++) {
619             (VOID)RemoveEventEntryFromEventTable(hApp,
620                                                  l_ptSessionEventInfo->m_vEvents[l_uiCount],
621                                                  serviceName,
622                                                  sessionId);
623           }
624           l_ptSessionEventInfo->m_vEvents.clear();  // clear the event list
625           delete l_ptSessionEventInfo;
626           sh_iterator->second = NULL;  // l_ptSessionEventInfo
627         }
628
629         FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Deleting session event info entry for service: %s, session id: %d.",
630                serviceName, sessionId);
631         // remove the entry from SessionHandle table
632         ssh_iterator->second.erase(sh_iterator);
633
634         // remove the entry from ServiceSessionHandle table if this service have not registered for
635         // private events over other session
636         if (ssh_iterator->second.empty()) {
637           pApp->servicesessionhandletable.erase(ssh_iterator);
638         }
639       }
640     }
641   } else {
642     eStatus = eFrameworkunifiedStatusInvldParam;
643     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid application handle or service name NULL");
644   }
645   FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "-");
646   return eStatus;
647 }
648
649 //////////////////////////////////////////////////////
650 /// RemoveEntryFromSessionEventInfoTable
651 //////////////////////////////////////////////////////
652 EFrameworkunifiedStatus RemoveEntryFromSessionEventInfoTable(HANDLE hApp, PCSTR serviceName, UI_32 sessionId, UI_32 eventId) {
653   FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "+");
654   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
655
656   if ((frameworkunifiedCheckValidAppHandle(hApp)) && (NULL != serviceName)) {
657     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
658
659     ServiceSessionHandleTable::iterator ssh_iterator;
660     SessionToEventInfo::iterator sh_iterator;
661     HANDLE l_pClientHandle = NULL;
662     SessionEventInfo *l_ptSessionEventInfo = NULL;
663
664     // check whether client has registered for private event/s
665     ssh_iterator = pApp->servicesessionhandletable.find(serviceName);
666
667     if (pApp->servicesessionhandletable.end() != ssh_iterator) {
668       // get the table of session id and session handle of the received service over which private events are
669       // registered
670       sh_iterator =  ssh_iterator->second.find(sessionId);
671
672       // client has registered for private event/s over this session
673       if (ssh_iterator->second.end() != sh_iterator) {
674         l_ptSessionEventInfo = sh_iterator->second;
675
676         if (NULL != l_ptSessionEventInfo) {
677           // Remove event entry from session event info table
678           for (UI_32 l_uiCount = 0;
679                l_uiCount < l_ptSessionEventInfo->m_vEvents.size();
680                l_uiCount++) {
681             if (eventId == l_ptSessionEventInfo->m_vEvents[l_uiCount]) {
682               FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Removing session event info entry for "
683                      "service:%s, session id:%d. event:%d",
684                      serviceName, sessionId, eventId);
685               l_ptSessionEventInfo->m_vEvents.erase(l_ptSessionEventInfo->m_vEvents.begin() + l_uiCount);
686               break;
687             }
688           }
689
690           if (l_ptSessionEventInfo->m_vEvents.empty()) {
691             l_pClientHandle = l_ptSessionEventInfo->m_hSession;
692             if (NULL != l_pClientHandle) {
693               // close the session handle
694               eStatus = FrameworkunifiedMcClose(l_pClientHandle);
695               l_ptSessionEventInfo->m_hSession = NULL;
696             }
697             delete l_ptSessionEventInfo;
698             sh_iterator->second = NULL;  // l_ptSessionEventInfo
699
700             // remove the entry from SessionHandle table
701             ssh_iterator->second.erase(sh_iterator);
702           }
703         }
704
705         // remove the entry from ServiceSessionHandle table if this service have not registered for
706         // private events over other session
707         if (ssh_iterator->second.empty()) {
708           pApp->servicesessionhandletable.erase(ssh_iterator);
709         }
710       }
711     }
712   } else {
713     eStatus = eFrameworkunifiedStatusInvldParam;
714     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid application handle status:: %d", eStatus);
715   }
716
717   FRAMEWORKUNIFIEDLOG_CUT(ZONE_NS_FUNC, __FUNCTION__, "-");
718   return eStatus;
719 }
720
721
722 //////////////////////////////////////////////////////
723 /// FrameworkunifiedDefinePublicStateEvents
724 //////////////////////////////////////////////////////
725 EFrameworkunifiedStatus FrameworkunifiedDefinePublicStateEvents(HANDLE hApp, PUI_32 puiEvents, UI_32 uiListSize) {
726   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
727
728   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != puiEvents) {
729     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
730
731     UI_32 l_uiEventId;
732
733     for (UI_32 uiCount = 0; uiCount < uiListSize; uiCount++) {
734       l_uiEventId = puiEvents[uiCount];
735
736       // checks if event exists in state event list
737       if (pApp->publicstateeventtable.end() == pApp->publicstateeventtable.find(l_uiEventId)) {
738         EventData *l_pEventData = NULL;
739         // insert the event in state event list, data is passed as NULL as there is no data published
740         pApp->publicstateeventtable.insert(std::make_pair(l_uiEventId, l_pEventData));
741       } else {
742         FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Event %d already registered as public state event ", l_uiEventId);
743       }
744     }
745   } else {
746     eStatus = eFrameworkunifiedStatusInvldHandle;
747   }
748
749   return eStatus;
750 }
751
752 //////////////////////////////////////////////////////
753 /// FrameworkunifiedDefinePrivateStateEvents
754 //////////////////////////////////////////////////////
755 EFrameworkunifiedStatus FrameworkunifiedDefinePrivateStateEvents(HANDLE hApp, PUI_32 puiEvents, UI_32 uiListSize) {
756   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
757
758   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != puiEvents) {
759     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
760
761     UI_32 l_uiEventId = 0;
762
763     for (UI_32 uiCount = 0; uiCount < uiListSize; uiCount++) {
764       l_uiEventId = puiEvents[uiCount];
765
766       // checks if event exists in state event list
767       if (pApp->privatestateeventtable.end() == pApp->privatestateeventtable.find(l_uiEventId)) {
768         ServiceSessionEventData servicesessioneventdata;
769         pApp->privatestateeventtable.insert(std::make_pair(l_uiEventId, servicesessioneventdata));
770       } else {
771         FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Event %d already registered as private state event", l_uiEventId);
772       }
773     }
774   } else {
775     eStatus = eFrameworkunifiedStatusInvldHandle;
776   }
777
778   return eStatus;
779 }
780
781 //////////////////////////////////////////////////////
782 /// FrameworkunifiedSubscribeToSessionEventWithCallback
783 //////////////////////////////////////////////////////
784 EFrameworkunifiedStatus FrameworkunifiedSubscribeToSessionEventWithCallback(HANDLE hApp, UI_32 uiCmd, CbFuncPtr fpOnCmd, HANDLE hSession) {
785   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
786
787   if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
788     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedAttachCallbackToDispatcher(hApp,
789                                                                  (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName,
790                                                                  uiCmd,
791                                                                  fpOnCmd,
792                                                                  hSession))) {
793       eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_REGISTER_EVENTS, sizeof(UI_32), (PVOID)&uiCmd);
794     } else {
795       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedAttachCallbackToDispatcher for "
796         "PROTOCOL_REGISTER_EVENTS Failed Status:: %d", eStatus);
797     }
798   } else {
799     eStatus = eFrameworkunifiedStatusInvldHandle;
800   }
801
802   return eStatus;
803 }
804
805 //////////////////////////////////////////////////////
806 /// FrameworkunifiedSubscribeToSessionEventsWithCallbacks
807 //////////////////////////////////////////////////////
808 EFrameworkunifiedStatus FrameworkunifiedSubscribeToSessionEventsWithCallbacks(HANDLE hApp, const FrameworkunifiedProtocolCallbackHandler *pMsgHandler,
809                                                     UI_32 uiHandlerCount, HANDLE hSession) {
810   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
811
812   if (NULL == pMsgHandler) {
813     eStatus = eFrameworkunifiedStatusInvldParam;
814   } else if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
815     /**
816      * @todo
817      *  If the CbFuncPtr in the pMsgHandler is set to NULL, the expected value is eFrameworkunifiedStatusInvldParam,
818      *  but eFrameworkunifiedStatusOK is returned in the implementation.
819      *  [Proposed measures]
820      *  Exit by referring to the return code of the FrameworkunifiedAttachCallbacksToDispatcher executed in the FrameworkunifiedSubscribeToSessionEventsWithCallbacks.
821      */
822     /**
823      * @todo
824      *  The behavior when NULL is set for a CbFuncPtr in a pMsgHandler in which more than one callback message is registered
825      *  is not specified in the specifications.
826      *  Ex: Prepare a pMsgHandler in which three callback information are registered,
827      *      and specify NULL as the callback function pointer of the second callback information, and execute it.
828      *  - Register the first callback function.
829      *  - The second callback function is not registered with an error.
830      *  - It is unspecified whether the third callback function is registered.
831      *  [Proposed measures]
832      *  Specifies that if there is callback information including NULL in the callback function pointer in pMsgHandler,
833      *  the callback information will not be registered from the element following that callback function pointer.
834      */
835     eStatus = FrameworkunifiedAttachCallbacksToDispatcher(hApp,
836                                              (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName,
837                                              pMsgHandler,
838                                              uiHandlerCount,
839                                              hSession);
840     UI_32 l_uiCmdList[uiHandlerCount];  // NOLINT  (readability/nolint)
841     for (UI_32 l_uiCnt = 0; l_uiCnt < uiHandlerCount; l_uiCnt++) {
842       l_uiCmdList[l_uiCnt] = pMsgHandler[l_uiCnt].iCmd;
843     }
844
845     eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_REGISTER_EVENTS,
846       static_cast<UI_32>(uiHandlerCount * sizeof(UI_32)), l_uiCmdList);
847   } else {
848     eStatus = eFrameworkunifiedStatusInvldHandle;
849   }
850
851   return eStatus;
852 }
853
854 //////////////////////////////////////////////////////
855 /// FrameworkunifiedUnSubscribeSessionEventWithCallback
856 //////////////////////////////////////////////////////
857 EFrameworkunifiedStatus FrameworkunifiedUnSubscribeSessionEventWithCallback(HANDLE hApp, UI_32 uiEvent, HANDLE hSession) {
858   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
859
860   if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
861     eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_UNREGISTER_EVENTS, sizeof(uiEvent), (PVOID)&uiEvent);
862     if (eFrameworkunifiedStatusOK == eStatus) {
863       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDetachCallbackFromDispatcher(hApp,
864                                                               (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName,
865                                                               uiEvent,
866                                                               hSession))) {
867         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : FrameworkunifiedDetachCallbackFromDispatcher failed status:: %d", eStatus);
868       }
869     } else {
870       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : Failed to send PROTOCOL_UNREGISTER_EVENTS request to service:: %d",
871         eStatus);
872     }
873   } else {
874     eStatus = eFrameworkunifiedStatusInvldHandle;
875   }
876
877   return eStatus;
878 }
879
880 //////////////////////////////////////////////////////
881 /// FrameworkunifiedUnSubscribeSessionEventsWithCallbacks
882 //////////////////////////////////////////////////////
883 EFrameworkunifiedStatus FrameworkunifiedUnSubscribeSessionEventsWithCallbacks(HANDLE hApp, PUI_32 puiEventsArray, UI_32 uiListSize,
884                                                     HANDLE hSession) {
885   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
886
887   if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
888     eStatus = FrameworkunifiedSendMsg(hSession, PROTOCOL_UNREGISTER_EVENTS, static_cast<UI_32>(uiListSize * sizeof(uiListSize)),
889                         puiEventsArray);
890     if (eFrameworkunifiedStatusOK == eStatus) {
891       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDetachCallbacksFromDispatcher(hApp,
892                                                               (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName,
893                                                               puiEventsArray,
894                                                               uiListSize,
895                                                               hSession))) {
896         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : FrameworkunifiedDetachCallbackFromDispatcher failed status:: %d", eStatus);
897       }
898     } else {
899       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error : Failed to send PROTOCOL_UNREGISTER_EVENTS request to service:: %d",
900                               eStatus);
901     }
902   } else {
903     eStatus = eFrameworkunifiedStatusInvldHandle;
904   }
905
906   return eStatus;
907 }
908
909 //////////////////////////////////////////////////////
910 /// FrameworkunifiedPublishPrivateEvent
911 //////////////////////////////////////////////////////
912 EFrameworkunifiedStatus FrameworkunifiedPublishPrivateEvent(HANDLE hApp, UI_32 uiEventId, PCVOID pData, UI_32 uiLength, HANDLE hSession) {
913   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
914
915   if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
916     EventTable::iterator e_iterator;
917     ServiceSessionIdListTable::iterator ssidl_iterator;
918
919     PCSTR l_cServiceName = "";
920     BOOL l_bIsPublished = FALSE;
921
922     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
923
924     // set the data, if event is a public state events
925     UpdatePrivateStateEventData(hApp,
926                                 uiEventId,
927                                 (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName,
928                                 (reinterpret_cast<MsgQInfo *>(hSession))->sessionId,
929                                 pData,
930                                 uiLength);
931
932     // check if event is registered by client/s
933     e_iterator = pApp->eventtable.find(uiEventId);
934
935     if (pApp->eventtable.end() != e_iterator) {
936       // for loop for event table
937       for (ssidl_iterator = e_iterator->second.begin();
938            ssidl_iterator != e_iterator->second.end();
939            ssidl_iterator++) {
940         l_cServiceName = ssidl_iterator->first.c_str();
941
942         // send message to only specified client
943         if (!std::strcmp(l_cServiceName, (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName)) {
944           for (UI_32 l_uiCount = 0;
945                l_uiCount < ssidl_iterator->second.size();
946                l_uiCount++) {
947             if ((reinterpret_cast<MsgQInfo *>(hSession))->sessionId == ssidl_iterator->second[l_uiCount]) {
948               if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedSendMsg(hSession, uiEventId, uiLength, pData))) {
949                 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedSendMsg Failed for Event %d", uiEventId);
950               }
951
952               l_bIsPublished = TRUE;
953               break;
954             }
955           }
956         }
957
958         if (l_bIsPublished) {
959           break;
960         }
961       }
962
963       if (!l_bIsPublished) {
964         FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Failed to publish event %d to client [%s], session: [%d]. "
965                "Client not subscribed to the event on specified session.",
966                uiEventId, (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName != 0 ? \
967                (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName : NULL,
968                (reinterpret_cast<MsgQInfo *>(hSession))->sessionId);
969         eStatus = eFrameworkunifiedStatusServNotFound;
970       }
971     } else {
972       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Client %s is not subscribed to Event %d, session: [%d]",
973              (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName, uiEventId,
974              (reinterpret_cast<MsgQInfo *>(hSession))->sessionId);
975       eStatus = eFrameworkunifiedStatusServNotFound;
976     }
977   } else {
978     eStatus = eFrameworkunifiedStatusInvldParam;
979   }
980
981   return eStatus;
982 }
983
984 //////////////////////////////////////////////////////
985 /// FrameworkunifiedPublishPublicEvent
986 //////////////////////////////////////////////////////
987 EFrameworkunifiedStatus FrameworkunifiedPublishPublicEvent(HANDLE hApp, UI_32 uiEventId, PCVOID pData, UI_32 uiLength) {
988   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
989   EFrameworkunifiedStatus eRetStatus = eFrameworkunifiedStatusOK;
990
991   if (frameworkunifiedCheckValidAppHandle(hApp)) {
992     EventTable::iterator e_iterator;
993     ServiceSessionIdListTable::iterator ssidl_iterator;
994
995     PCSTR l_cServiceName = "";
996
997     // set the data, if event is a state events
998     UpdatePublicStateEventData(hApp, uiEventId, pData, uiLength);
999
1000     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1001
1002     // check if event is registered by client/s
1003     e_iterator = pApp->eventtable.find(uiEventId);
1004
1005     if (pApp->eventtable.end() != e_iterator) {
1006       // for loop for event table
1007       for (ssidl_iterator = e_iterator->second.begin();
1008            ssidl_iterator != e_iterator->second.end();
1009            ssidl_iterator++) {
1010         l_cServiceName = ssidl_iterator->first.c_str();
1011
1012         eRetStatus = SendEventMessage(hApp, ssidl_iterator->second, uiEventId, l_cServiceName, pData, uiLength);
1013         if (eFrameworkunifiedStatusOK != eRetStatus) {
1014           eStatus = eRetStatus;
1015         }
1016       }
1017     } else {
1018       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "No client subscribed to EventID %d.", uiEventId);
1019     }
1020   } else {
1021     eStatus = eFrameworkunifiedStatusInvldHandle;
1022   }
1023
1024   return eStatus;
1025 }
1026
1027 //////////////////////////////////////////////////////
1028 /// UpdatePrivateStateEventData
1029 //////////////////////////////////////////////////////
1030 EFrameworkunifiedStatus UpdatePrivateStateEventData(HANDLE hApp, UI_32 uiEventId, PCSTR pClientName, UI_32 uiSessionId,
1031                                        PCVOID pData,
1032                                        UI_32 uiLength) {
1033   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1034
1035   if ((NULL == pData) && (uiLength > 0)) {
1036     eStatus = eFrameworkunifiedStatusInvldParam;
1037   }
1038
1039   if ((eFrameworkunifiedStatusOK == eStatus) && (frameworkunifiedCheckValidAppHandle(hApp)) && (NULL != pClientName)) {
1040     PrivateStateEventTable::iterator pse_iterator;
1041     ServiceSessionEventData::iterator ssed_iterator;
1042     SessionEventData::iterator sed_iterator;
1043
1044     EventData *l_pEventData = NULL;
1045
1046     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1047
1048     // set the data, if event is a state events
1049     pse_iterator = pApp->privatestateeventtable.find(uiEventId);
1050     if (pApp->privatestateeventtable.end() != pse_iterator) {
1051       ssed_iterator = (pse_iterator->second).find(pClientName);
1052
1053       if ((pse_iterator->second).end() != ssed_iterator) {
1054         sed_iterator = (ssed_iterator->second).find(uiSessionId);
1055         if ((ssed_iterator->second).end() != sed_iterator) {
1056           l_pEventData = sed_iterator->second;
1057
1058           if (NULL != l_pEventData) {
1059             if (NULL != l_pEventData->pData) {
1060               delete[](static_cast<PCHAR>(l_pEventData->pData));
1061               (sed_iterator->second)->pData = NULL;
1062             }
1063
1064             delete l_pEventData;
1065             sed_iterator->second = NULL;
1066           }
1067
1068           l_pEventData = new(std::nothrow) EventData();
1069
1070           if (NULL != l_pEventData) {
1071             l_pEventData->uiLength = uiLength;
1072
1073             if (NULL == pData) {
1074               l_pEventData->pData = NULL;
1075             } else {
1076               l_pEventData->pData = new(std::nothrow) CHAR[uiLength];
1077               if (NULL != l_pEventData->pData) {
1078                 std::memset(l_pEventData->pData, 0, uiLength);
1079                 std::memcpy(l_pEventData->pData, pData, uiLength);
1080               } else {
1081                 eStatus = eFrameworkunifiedStatusNullPointer;
1082                 l_pEventData->uiLength = 0;
1083               }
1084             }
1085
1086             sed_iterator->second = l_pEventData;
1087           } else {
1088             eStatus = eFrameworkunifiedStatusNullPointer;
1089           }
1090         } else {  // if client has not yet subscribed to this event over the session uiSessionId
1091           l_pEventData = new(std::nothrow) EventData();
1092
1093           if (NULL != l_pEventData) {
1094             l_pEventData->uiLength = uiLength;
1095
1096             if (NULL == pData) {
1097               l_pEventData->pData = NULL;
1098             } else {
1099               l_pEventData->pData = new(std::nothrow) CHAR[uiLength];
1100               if (NULL != l_pEventData->pData) {
1101                 std::memset(l_pEventData->pData, 0, uiLength);
1102                 std::memcpy(l_pEventData->pData, pData, uiLength);
1103               } else {
1104                 eStatus = eFrameworkunifiedStatusNullPointer;
1105               }
1106             }
1107
1108             (ssed_iterator->second).insert(std::make_pair(uiSessionId, l_pEventData));
1109           } else {
1110             eStatus = eFrameworkunifiedStatusNullPointer;
1111           }
1112         }
1113       } else {  // if client has not yet subscribed to this event
1114         SessionEventData sessionEventData;
1115
1116         l_pEventData = new(std::nothrow) EventData();
1117
1118         if (NULL != l_pEventData) {
1119           l_pEventData->uiLength = uiLength;
1120
1121           if (NULL == pData) {
1122             l_pEventData->pData = NULL;
1123           } else {
1124             l_pEventData->pData = new(std::nothrow) CHAR[uiLength];
1125             if (NULL != l_pEventData->pData) {
1126               std::memset(l_pEventData->pData, 0, uiLength);
1127               std::memcpy(l_pEventData->pData, pData, uiLength);
1128             } else {
1129               eStatus = eFrameworkunifiedStatusNullPointer;
1130               l_pEventData->uiLength = 0;
1131             }
1132           }
1133           sessionEventData.insert(std::make_pair(uiSessionId, l_pEventData));
1134
1135           (pse_iterator->second).insert(std::make_pair(pClientName, sessionEventData));
1136         } else {
1137           eStatus = eFrameworkunifiedStatusNullPointer;
1138         }
1139       }
1140     }
1141   } else {
1142     eStatus = eFrameworkunifiedStatusInvldParam;
1143   }
1144
1145   return eStatus;
1146 }
1147
1148 //////////////////////////////////////////////////////
1149 /// DeleteSessionEventData
1150 //////////////////////////////////////////////////////
1151 EFrameworkunifiedStatus DeleteSessionEventData(HANDLE hApp, PCSTR pClientName, UI_32 uiSessionId) {
1152   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1153
1154   // map: eventid -> servicename -> sessionid -> eventdata(data, length)
1155
1156   if (frameworkunifiedCheckValidAppHandle(hApp) && NULL != pClientName) {
1157     PrivateStateEventTable::iterator pse_iterator;
1158     ServiceSessionEventData::iterator ssed_iterator;
1159     SessionEventData::iterator sed_iterator;
1160
1161     EventData *l_pEventData = NULL;
1162
1163     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1164
1165     // delete the private event data associated with the service over this session
1166     for (pse_iterator  = pApp->privatestateeventtable.begin();
1167          pse_iterator != pApp->privatestateeventtable.end();
1168          ++pse_iterator) {
1169       ssed_iterator = (pse_iterator->second).find(pClientName);
1170
1171       if ((pse_iterator->second).end() != ssed_iterator) {
1172         sed_iterator = (ssed_iterator->second).find(uiSessionId);
1173         if ((ssed_iterator->second).end() != sed_iterator) {
1174           l_pEventData = sed_iterator->second;
1175
1176           if (NULL != l_pEventData) {
1177             if (NULL != l_pEventData->pData) {
1178               delete[](static_cast<PCHAR>(l_pEventData->pData));
1179               (sed_iterator->second)->pData = NULL;
1180             }
1181
1182             delete l_pEventData;
1183             sed_iterator->second = NULL;
1184           }
1185         }
1186       }
1187     }
1188   } else {
1189     eStatus = eFrameworkunifiedStatusNullPointer;
1190   }
1191
1192   return eStatus;
1193 }
1194
1195 //////////////////////////////////////////////////////
1196 /// Returns the number of session opened by the server for one client
1197 //////////////////////////////////////////////////////
1198 UI_32 FrameworkunifiedGetNumberOfSession(HANDLE hApp, PCSTR strServiceName) {
1199   UI_32 l_uiSessionCount = 0;
1200
1201   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1202     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1203     Services::iterator s_iterator = pApp->services.find(strServiceName);
1204     if (s_iterator != pApp->services.end()) {
1205       l_uiSessionCount = static_cast<UI_32>((s_iterator->second).size());
1206     }
1207   }
1208
1209   return l_uiSessionCount;
1210 }
1211
1212 //////////////////////////////////////////////////////
1213 /// Sets the handle in the Application Framework
1214 //////////////////////////////////////////////////////
1215 EFrameworkunifiedStatus FrameworkunifiedSetSessionHandle(HANDLE hApp, PCSTR strServiceName, HANDLE hSession) {
1216   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1217   UI_32 l_uiSessionId = 0;
1218   if (frameworkunifiedCheckValidMsgQ(hSession) && NULL != strServiceName && frameworkunifiedCheckValidAppHandle(hApp) &&
1219       0 != std::strlen(strServiceName)) {
1220     MsgQInfo *pMsgQ    = reinterpret_cast<MsgQInfo *>(hSession);
1221
1222     // Get session Id
1223     l_uiSessionId = pMsgQ->sessionId;
1224
1225     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1226     ServiceSessionTable::iterator session_iterator;
1227     SessionHandleTable::iterator session_handle_iterator;
1228
1229     // Find the service name
1230     session_iterator = pApp->sessiontable.find(strServiceName);
1231     if (session_iterator == pApp->sessiontable.end()) {
1232       pApp->sessiontable.insert(std::make_pair(strServiceName, SessionHandleTable()));
1233     }
1234
1235     // Find the session id
1236     session_handle_iterator = pApp->sessiontable[strServiceName].find(l_uiSessionId);
1237     if (session_handle_iterator != pApp->sessiontable[strServiceName].end()) {
1238       pApp->sessiontable[strServiceName].erase(l_uiSessionId);
1239     }
1240
1241     // Set the session handle
1242     pApp->sessiontable[strServiceName].insert(std::make_pair(l_uiSessionId, hSession));
1243
1244   } else {
1245     eStatus = eFrameworkunifiedStatusFail;
1246   }
1247   return eStatus;
1248 }
1249
1250 //////////////////////////////////////////////////////
1251 /// reads the handle from the Application Framework
1252 //////////////////////////////////////////////////////
1253 HANDLE FrameworkunifiedGetSessionHandle(HANDLE hApp, PCSTR strServiceName, UI_32 uiSessionId) {
1254   HANDLE hSession = NULL;
1255   if (frameworkunifiedCheckValidAppHandle(hApp) && strServiceName) {
1256     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1257     ServiceSessionTable::iterator session_iterator;
1258     SessionHandleTable::iterator session_handle_iterator;
1259
1260     // Find the service name
1261     session_iterator = pApp->sessiontable.find(strServiceName);
1262     if (session_iterator != pApp->sessiontable.end()) {
1263       // Find the session id
1264       session_handle_iterator = pApp->sessiontable[strServiceName].find(uiSessionId);
1265       if (session_handle_iterator != pApp->sessiontable[strServiceName].end()) {
1266         // Fetch the session handle
1267         hSession = session_handle_iterator->second;
1268       }
1269     }
1270   }
1271   return hSession;
1272 }
1273 //////////////////////////////////////////////////////
1274 /// Close the handle stored in the Application Framework
1275 //////////////////////////////////////////////////////
1276 EFrameworkunifiedStatus FrameworkunifiedRemoveSessionHandle(HANDLE hApp, PCSTR strServiceName, UI_32 uiSessionId) {
1277   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1278   if (frameworkunifiedCheckValidAppHandle(hApp) && strServiceName && 0 != std::strlen(strServiceName)) {
1279     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1280
1281     pApp->sessiontable[strServiceName].erase(uiSessionId);
1282     if (0 == pApp->sessiontable[strServiceName].size()) {
1283       // If no entry is available for current service then remove the servicename
1284       pApp->sessiontable.erase(strServiceName);
1285     }
1286   } else {
1287     eStatus = eFrameworkunifiedStatusFail;
1288   }
1289
1290   return eStatus;
1291 }
1292
1293 //////////////////////////////////////////////////////
1294 /// FrameworkunifiedGetSession|Name -> On Client side
1295 //////////////////////////////////////////////////////
1296 PCSTR FrameworkunifiedGetSessionName(HANDLE hSession) {
1297   if (frameworkunifiedCheckValidMsgQ(hSession)) {
1298     return ((reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName);
1299   } else {
1300     return NULL;
1301   }
1302 }
1303
1304
1305 HANDLE FrameworkunifiedGetCurrentSessionHandle(HANDLE hApp) {
1306   HANDLE hSession = NULL;
1307   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1308     UI_32 l_uiSessionId = FrameworkunifiedGetMsgSessionId(hApp);
1309     PCSTR pRequester   = FrameworkunifiedGetMsgSrc(hApp);
1310
1311     hSession = FrameworkunifiedGetSessionHandle(hApp, pRequester, l_uiSessionId);
1312   }
1313   return hSession;
1314 }
1315
1316 HANDLE FrameworkunifiedCreateSession(HANDLE hApp, PCSTR pSessionName) {
1317   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1318   HANDLE hSession = NULL;
1319   if (frameworkunifiedCheckValidAppHandle(hApp) && pSessionName && strlen(pSessionName)) {
1320     hSession = FrameworkunifiedGenerateSessionHandle(hApp, pSessionName);
1321     if (hSession) {
1322       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedSetSessionHandle(hApp, pSessionName, hSession))) {
1323         if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedMcClose(hSession))) {
1324           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__, "Close session failed");
1325         }
1326         hSession = NULL;
1327
1328         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__, "FrameworkunifiedCreateSession Failed Status:0x%x ", eStatus);
1329       }
1330     }
1331   } else {
1332     eStatus = eFrameworkunifiedStatusInvldParam;
1333   }
1334   return hSession;
1335 }
1336
1337 EFrameworkunifiedStatus FrameworkunifiedDestroySession(HANDLE hApp, HANDLE hSession) {
1338   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1339
1340   if (frameworkunifiedCheckValidAppHandle(hApp) && frameworkunifiedCheckValidMsgQ(hSession)) {
1341     UI_32 uiSessionId = (reinterpret_cast<MsgQInfo *>(hSession))->sessionId;
1342     CHAR pRequester[MAX_NAME_SIZE_APP] = {};
1343     strncpy(pRequester, (reinterpret_cast<MsgQInfo *>(hSession))->cMsgQName, sizeof(pRequester) - 1);
1344
1345     if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedMcClose(hSession))) {
1346       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__, "Close session failed. Status: %d", eStatus);
1347     }
1348
1349     if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedRemoveSessionHandle(hApp, pRequester, uiSessionId))) {
1350       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __PRETTY_FUNCTION__, "Remove session failed. Status: %d", eStatus);
1351     }
1352
1353     DeleteSessionEventData(hApp, pRequester, uiSessionId);
1354   }
1355
1356   return eStatus;
1357 }