Remove unused directories and files in video_in_hal
[staging/basesystem.git] / service / native / framework_unified / client / NS_FrameworkCore / src / frameworkunified_dispatcher.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NSFramework
19 /// \brief
20 ///
21 ///
22 ///
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/epoll.h>
28 #include <sys/eventfd.h>
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #include <fcntl.h>
32 #include <pthread.h>
33
34 #include <native_service/ns_logger_if.h>
35 #include <native_service/ns_version_if.h>
36 #include <native_service/ns_message_center_if.h>
37 #include <native_service/frameworkunified_framework_sync.h>
38 #include <native_service/frameworkunified_sm_hsmframework.h>
39 #include <native_service/frameworkunified_sm_framework_dispatch.h>
40 #include <native_service/frameworkunified_thread_priority.h>
41 #include <native_service/ns_plogger_if.h>
42 #include <native_service/ns_resource_controler.h>
43 #include <native_service/frameworkunified_service_protocol.h>
44 #include <native_service/frameworkunified_dispatcher.h>
45 #include <native_service/frameworkunified_framework_if.h>
46 #include <other_service/strlcpy.h>
47
48 #include <string>
49 #include <utility>
50 #include <map>
51
52 #include "frameworkunified_framework_core.h"
53 #include "frameworkunified_framework_internal.h"
54 #include "ns_message_center_internal.h"
55 #include "ns_mc_internal.h"
56 #include "frameworkunified_framework_error_internal.hpp"
57 #include "frameworkunified_framework_utility.h"
58 #include "frameworkunified_sm_framework_core.h"
59
60 #ifdef DISPATCHER_PROFILER
61 #include "frameworkunified_msgprofiler.h"
62 #include <boost/bind.hpp>
63 #endif
64
65 /*
66  * static variable
67  */
68 static FrameworkunifiedDefaultCallbackHandler g_defaultCallbackHandler;
69
70 //////////////////////////
71 // prototype declaration
72 //////////////////////////
73 static EFrameworkunifiedStatus GetDispatcherMqFD(HANDLE hApp, int *fd);
74 static EFrameworkunifiedStatus CreateMultiWaitingFD(HANDLE hApp);
75 static EFrameworkunifiedStatus ProcessMq(HANDLE hApp);
76 static EFrameworkunifiedStatus ProcessFd(HANDLE hApp , int fd);
77
78 //////////////////////////////////////////
79 // Function : ostream &operator<<
80 //////////////////////////////////////////
81 template <typename K, typename V, class C, class A>
82 std::ostream &operator<< (std::ostream &os, const std::map<K, V, C, A> &m) {
83   os << "{ ";
84   typename std::map<K, V, C, A>::const_iterator p;
85   for (p = m.begin(); p != m.end(); ++p) {
86     os << p->first << ":" << p->second << ", ";
87   }
88   return os << "}";
89 }
90
91 EFrameworkunifiedStatus CreateDispatcher(PCSTR cAppName, HANDLE &hApp, BOOL bIsChildThread) {  // NOLINT (readability/nolint)
92   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
93   CFrameworkunifiedFrameworkApp *pApp = new CFrameworkunifiedFrameworkApp;  // LCOV_EXCL_BR_LINE 11:except branch
94   if (pApp != NULL) {  // LCOV_EXCL_BR_LINE 5:fail safe for new
95     pApp->checkCode = APP_CHECK_CODE;
96     pApp->self = pthread_self();
97     // app name size should not exceed the max length
98     memset(pApp->cAppName, 0, MAX_NAME_SIZE_APP);
99     memset(pApp->sServiceAvailabilityNotification, 0, MAX_SYS_INFO_SIZE);
100     pApp->bIsAvailable = FALSE;
101     /*
102      * todo
103      * Setting NULL for arguments causes an Exception
104      */
105     if (strlen(cAppName) < MAX_NAME_SIZE_APP) {
106       memcpy(pApp->cAppName, cAppName, strlen(cAppName));
107     } else {
108       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error : Aborting ... Exceeds Max App name size MAX_NAME_SIZE_APP : %d ",
109              MAX_NAME_SIZE_APP);
110       /*
111        * Error is set here, but the error is overwritten by the following process
112        */
113       eStatus = eFrameworkunifiedStatusFail;
114     }
115
116     FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, " Application Created ");
117
118     // opening handle to receive message for the application
119     pApp->hAppRcvMsgQ = NULL;
120     if (NULL == (pApp->hAppRcvMsgQ = McOpenReceiver(pApp->cAppName))) {
121       FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, " Error : Failed to McOpenReceiver ");
122       eStatus = eFrameworkunifiedStatusNullPointer;
123     } else {
124       if (strcmp(cAppName, "NS_NPPService") != 0) {
125         McFlushReceiver(pApp->hAppRcvMsgQ);
126       }
127     }
128
129     // opening socket for abnormal monitoring
130     pApp->siMonitorSock = -1;
131     if ((eFrameworkunifiedStatusOK == eStatus) && (-1 == (pApp->siMonitorSock = mcOpenMonitor(pApp->cAppName)))) {
132       FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, " Error : Failed to mcOpenMonitor ");
133       eStatus = eFrameworkunifiedStatusFail;
134       pApp->siMonitorSock = -1;
135     }
136
137     // opening handle to send messages to itself
138     pApp->hAppSndMsgQ = NULL;
139     if ((eFrameworkunifiedStatusOK == eStatus) && (NULL == (pApp->hAppSndMsgQ = McOpenSender(pApp->cAppName)))) {
140       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
141       FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, " Error : Failed to McOpenSender ");
142       // LCOV_EXCL_BR_STOP
143       eStatus = eFrameworkunifiedStatusNullPointer;
144     }
145
146     // opening handles for Notification Persistence Service and Service Directory
147     pApp->hNPSndMsgQ = NULL;
148     if ((eFrameworkunifiedStatusOK == eStatus) &&
149         (NULL == (pApp->hNPSndMsgQ = FrameworkunifiedMcOpenSender(reinterpret_cast<HANDLE>(pApp), FRAMEWORKUNIFIED_NS_NPSERVICE)))) {
150       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
151       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error : Failed to FrameworkunifiedMcOpenSender < NS_NPPService >");
152       // LCOV_EXCL_BR_STOP
153       eStatus = eFrameworkunifiedStatusNullPointer;
154     }
155
156     // stores the message queue handle of the parent thread.
157     // It will set after the dispatcher of child thread is created. And will be NULL for main thread.
158     pApp->hParentSndMsgQ = NULL;
159
160     if ((eFrameworkunifiedStatusOK == eStatus) && (!bIsChildThread)) {
161       // Define any system callback's with the dispatcher.
162       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedAttachSystemCallbacksDispatcher(reinterpret_cast<HANDLE>(pApp)))) {
163         // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
164         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error :: (%d) Failed to FrameworkunifiedAttachSystemCallbacksDispatcher ", eStatus);
165         // LCOV_EXCL_BR_STOP
166       }
167
168       if (eFrameworkunifiedStatusOK == eStatus) {
169         // Define the logger callback's for setting up gets and sets with the dispatcher.
170         if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedAttachLoggerCallbacksDispatcher(reinterpret_cast<HANDLE>(pApp)))) {
171           // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
172           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error :: (%d) Failed to FrameworkunifiedAttachLoggerCallbacksDispatcher ", eStatus);
173           // LCOV_EXCL_BR_STOP
174         }
175       }
176     }
177
178     // initialize
179     pApp->m_pFrameworkunifiedStateMachine = NULL;
180     pApp->uiProtocolCmd = 0;
181
182     // Clear the Defer'ed message pop flag.
183     pApp->fPopDeferedMsg = FALSE;
184
185     // Clear the Defer'ed sync response flag.
186     pApp->fDeferredSyncResponse = FALSE;
187
188     // buffer initialize
189     memset(pApp->uiMsgRcvBuffer, 0, MAX_MSGQ_BUFFER);
190     memset(pApp->cMsgSrcName, '\0', MAX_NAME_SIZE_APP);
191     memset(pApp->cSystemInfo, '\0', MAX_SYS_INFO_SIZE);
192
193     pApp->defer_evfd = -1;
194     pApp->efd = -1;
195     if (eFrameworkunifiedStatusOK == eStatus) {
196       eStatus = CreateMultiWaitingFD((HANDLE)pApp);
197       if (eFrameworkunifiedStatusOK != eStatus) {
198         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : CreateMultiWaitingFD Failed, status=%d", eStatus);
199       }
200     }
201
202 #ifdef DISPATCHER_PROFILER
203     // Assign default value
204     pApp->m_pFrameworkunifiedMsgProfiler = NULL;
205 #endif
206
207     if (eFrameworkunifiedStatusOK != eStatus) {
208       if (NULL != pApp->hAppRcvMsgQ) {
209         McClose(pApp->hAppRcvMsgQ);
210         pApp->hAppRcvMsgQ = NULL;
211       }
212
213       if (0 < pApp->siMonitorSock) {
214         mcCloseMonitor(pApp->cAppName);
215         pApp->siMonitorSock = -1;
216       }
217
218       if (NULL != pApp->hAppSndMsgQ) {
219         McClose(pApp->hAppSndMsgQ);
220         pApp->hAppSndMsgQ = NULL;
221       }
222
223       if (NULL != pApp->hNPSndMsgQ) {
224         FrameworkunifiedMcClose(pApp->hNPSndMsgQ);
225         pApp->hNPSndMsgQ = NULL;
226       }
227
228       // LCOV_EXCL_BR_START 6:According to line 158, pApp->hParentSndMsgQ is NULL.
229       if (NULL != pApp->hParentSndMsgQ) {
230       // LCOV_EXCL_BR_STOP
231         // LCOV_EXCL_START 6:According to line 158, pApp->hParentSndMsgQ is NULL.
232         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
233         McClose(pApp->hParentSndMsgQ);
234         pApp->hParentSndMsgQ = NULL;
235         // LCOV_EXCL_STOP
236       }
237
238       if (0 < pApp->defer_evfd) {
239         close(pApp->defer_evfd);
240         pApp->defer_evfd = -1;
241       }
242
243       if (0 < pApp->efd) {
244         close(pApp->efd);
245         pApp->efd = -1;
246       }
247
248       delete pApp;  // LCOV_EXCL_BR_LINE 11:except branch
249       pApp = NULL;
250     }
251   }
252
253   hApp = pApp;
254
255   return eStatus;
256 }
257
258
259 //////////////////////////////////////////
260 // Function : FrameworkunifiedCreateDispatcher
261 //////////////////////////////////////////
262 EFrameworkunifiedStatus FrameworkunifiedCreateDispatcher(PCSTR cAppName, HANDLE &hApp, BOOL bIsChildThread) {  // NOLINT (readability/nolint)
263   return CreateDispatcher(cAppName, hApp, bIsChildThread);
264 }
265
266
267 //////////////////////////////////////////
268 // Function : FrameworkunifiedDispatchBlock
269 //////////////////////////////////////////
270 EFrameworkunifiedStatus FrameworkunifiedDispatchBlock(HANDLE hApp) {
271   EFrameworkunifiedStatus eStatus     = eFrameworkunifiedStatusOK;
272 //  FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "+");
273
274   if (frameworkunifiedCheckValidAppHandle(hApp)) {
275     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
276
277     if (eFrameworkunifiedStatusOK != (eStatus = McReceiveWithSession(pApp->hAppRcvMsgQ, pApp->cMsgSrcName,
278                                                         &pApp->uiProtocolCmd, &pApp->uiSessionId, MAX_MSGQ_BUFFER,
279                                                         (PVOID)(pApp->uiMsgRcvBuffer)))) {
280       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
281       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: (%d) Failed on McReceive", eStatus);
282       // LCOV_EXCL_BR_STOP
283     } else {
284 //      FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "Message received on dispatcher:: Source: %s, Cmd: 0x%X, SessionId: %d ",
285 //             pApp->cMsgSrcName, pApp->uiProtocolCmd, pApp->uiSessionId);
286     }
287   } else {
288     eStatus = eFrameworkunifiedStatusInvldHandle;
289   }
290
291 //  FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "-");
292   return eStatus;
293 }
294
295
296 EFrameworkunifiedStatus FrameworkunifiedProcessServiceProtocolCallback(HANDLE hApp, PCSTR pRequester) {
297   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
298
299   if (frameworkunifiedCheckValidAppHandle(hApp)) {
300     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
301     Services::iterator s_iterator;
302
303     s_iterator = pApp->services.find(pRequester);
304     if (s_iterator != pApp->services.end()) {
305       SessionTable::iterator session_iterator;
306       session_iterator = (s_iterator->second).find(pApp->uiSessionId);
307       if (session_iterator != (s_iterator->second).end()) {
308         //    FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, " %s found :: (%s)  ", pApp->cMsgSrcName, s_iterator->second);
309
310         // find the protocol req/ack handler
311         ServiceProtocolTable::iterator spt_iterator;
312         spt_iterator = (session_iterator->second).find(pApp->uiProtocolCmd);
313         if (spt_iterator != (session_iterator->second).end()) {
314           // calling the protocol req/ack handler
315           eStatus = (*spt_iterator).second(hApp);
316         } else if ((session_iterator->second).end() != (spt_iterator =
317           (session_iterator->second).find(FRAMEWORKUNIFIED_ANY_COMMAND))) {
318           // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
319           FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__,
320                  "Processing callback function for (FRAMEWORKUNIFIED_ANY_COMMAND). "
321                  "Dispatcher protocol command: (0x%x)", pApp->uiProtocolCmd);
322           // LCOV_EXCL_BR_STOP
323           eStatus = (*spt_iterator).second(hApp);
324         } else {
325           eStatus = eFrameworkunifiedStatusMsgNotProcessed;
326         }
327       } else {
328         eStatus = eFrameworkunifiedStatusMsgNotProcessed;
329       }
330     } else {
331       eStatus = eFrameworkunifiedStatusMsgNotProcessed;
332     }
333   } else {
334     eStatus = eFrameworkunifiedStatusInvldHandle;
335   }
336
337   return eStatus;
338 }
339
340 EFrameworkunifiedStatus FrameworkunifiedProcessServiceProtocol(HANDLE hApp) {
341   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
342
343   if (frameworkunifiedCheckValidAppHandle(hApp)) {
344     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
345
346     // Check if the callback is associated with the Protocol for message source
347     eStatus = FrameworkunifiedProcessServiceProtocolCallback(hApp, const_cast<PCSTR>(pApp->cMsgSrcName));
348
349     if (eFrameworkunifiedStatusMsgNotProcessed == eStatus) {
350       // Check if the callback is associated with the Protocol for FRAMEWORKUNIFIED_ANY_SOURCE
351       eStatus = FrameworkunifiedProcessServiceProtocolCallback(hApp, FRAMEWORKUNIFIED_ANY_SOURCE);
352       if (eFrameworkunifiedStatusMsgNotProcessed == eStatus) {
353         if (pApp->m_pFrameworkunifiedStateMachine) {
354           // Check if the HSM event is associated with the Protocol for message source
355           eStatus = FrameworkunifiedProcessServiceProtocolHSMEvent(hApp, const_cast<PCSTR>(pApp->cMsgSrcName));
356
357           if (eFrameworkunifiedStatusMsgNotProcessed == eStatus) {
358             // Check if the HSM event is associated with the Protocol for FRAMEWORKUNIFIED_ANY_SOURCE
359             eStatus = FrameworkunifiedProcessServiceProtocolHSMEvent(hApp, FRAMEWORKUNIFIED_ANY_SOURCE);
360           }
361         }
362       }
363     }
364   } else {
365     eStatus = eFrameworkunifiedStatusInvldHandle;
366   }
367
368   return eStatus;
369 }
370
371
372 EFrameworkunifiedStatus FrameworkunifiedSetDeferredSyncResponse(HANDLE hApp) {
373   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldBuf;
374
375   if (frameworkunifiedCheckValidAppHandle(hApp)) {
376     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
377     pApp->fDeferredSyncResponse = TRUE;
378     eStatus = eFrameworkunifiedStatusOK;
379   }
380
381   return eStatus;
382 }
383
384
385 static void frameworkunifiedProcessSyncResponse(HANDLE hApp, EFrameworkunifiedStatus retValue) {
386   EFrameworkunifiedStatus eStatus;
387   CHAR systemInfo[MAX_SYS_INFO_SIZE];
388   UI_32 seq_id;
389   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
390
391   if (frameworkunifiedGetIsTypeOfSync(hApp) == TRUE) {
392     if (pApp->fDeferredSyncResponse == TRUE) {
393       if (pApp->responsedata.size() != 0) {
394         FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Response data is ignored for "
395           ":: source: %s, Protocol Command: 0x%x, Session Id: %u.",
396                pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL,
397                pApp->uiProtocolCmd, pApp->uiSessionId);
398         pApp->responsedata.clear();
399       }
400     } else {
401       FrameworkunifiedGetSystemInfo(hApp, systemInfo);
402       seq_id = (UI_32)strtoul(systemInfo, NULL, 16);
403
404       eStatus = frameworkunifiedSendSyncResponse(hApp, PROTOCOL_FRAMEWORKUNIFIED_ANY_COMMAND, seq_id, retValue, pApp->responsedata.size(),
405                                     pApp->responsedata.data());
406       if (eStatus != eFrameworkunifiedStatusOK) {
407         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "retValu:%d errno:%d", eStatus, errno);
408       }
409       pApp->responsedata.clear();
410     }
411   } else {
412     if (pApp->responsedata.size() != 0) {
413       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Response data is ignored for :: "
414         "source: %s, Protocol Command: 0x%x, Session Id: %u.",
415              pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL,
416              pApp->uiProtocolCmd, pApp->uiSessionId);
417       pApp->responsedata.clear();
418     }
419   }
420   pApp->fDeferredSyncResponse = FALSE;
421 }
422
423
424 inline static time_t frameworkunifiedTimeval2usec(const struct timeval &time) {
425   return static_cast<time_t>(time.tv_sec * 1000000 + time.tv_usec);
426 }
427
428
429 static void frameworkunifiedPrintRusage(HANDLE hApp, const struct rusage &start, const struct rusage &end) {
430   UI_32 occupy_time;
431   UI_32 average;
432   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
433
434   occupy_time = static_cast<UI_32>(frameworkunifiedTimeval2usec(end.ru_utime) - frameworkunifiedTimeval2usec(start.ru_utime));
435   occupy_time += static_cast<UI_32>(frameworkunifiedTimeval2usec(end.ru_stime) - frameworkunifiedTimeval2usec(start.ru_stime));
436
437   pApp->total_occupy_time += occupy_time;
438   pApp->total_run_count++;
439   average = static_cast<UI_32>(pApp->total_occupy_time / pApp->total_run_count);
440
441   if (occupy_time > pApp->max_occupy_time) {
442     pApp->max_occupy_time = occupy_time;
443   }
444
445 //  FRAMEWORKUNIFIEDLOG(ZONE_NS_DEBUG_DUMP, __FUNCTION__, "cur:%d.%06d avg:%d.%06d max:%d.%06d",
446 //         occupy_time / 1000000, occupy_time % 1000000,
447 //         average / 1000000, average % 1000000,
448 //         pApp->max_occupy_time / 1000000, pApp->max_occupy_time % 1000000);
449 }
450
451
452 static inline void frameworkunifiedGetRusage(struct rusage *r) {
453   if (r == NULL) {
454     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "r is NULL");
455     return;
456   }
457   memset(r, 0, sizeof(struct rusage));
458   if (getrusage(RUSAGE_THREAD, r) == -1) {
459     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
460     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "getrusage errno:%d", errno);
461     // LCOV_EXCL_BR_STOP
462   }
463 }
464
465 static int frameworkunifiedPushLostSessionInfo(HANDLE hApp, PCSTR serviceName) {
466   int cntFound = 0;
467   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
468
469   ServiceSessionTable::iterator s_iterator = pApp->sessiontable.find(serviceName);
470   if (s_iterator != pApp->sessiontable.end()) {
471     // LCOV_EXCL_BR_START 11:except branch
472     SessionHandleTable::iterator sh_iterator = pApp->sessiontable[serviceName].begin();
473     // LCOV_EXCL_BR_STOP
474     while (sh_iterator != pApp->sessiontable[serviceName].end()) {
475       LostSessionInfo l_info;
476       l_info.sServiceName = serviceName;
477       l_info.iSessionId = sh_iterator->first;
478       pApp->lostSessionQueue.push(l_info);
479       cntFound++;
480       sh_iterator++;
481     }
482   }
483   return cntFound;
484 }
485
486 ///
487 /// Abnormal monitor(server): Client socket close handle
488 ///
489 EFrameworkunifiedStatus frameworkunifiedProcessClientAbnormalState(HANDLE hApp, int sockfd) {
490   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
491   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
492   AcceptMonitorTable::iterator it;
493
494   if ((it = pApp->acceptMonitorTable.find(sockfd)) == pApp->acceptMonitorTable.end()) {
495     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid fd", sockfd);
496   } else {
497     char clientName[16];
498     if (mcGetClientNameFromConnectKey((it->second).c_str(), clientName, sizeof(clientName)) < 0) {
499       eStatus = eFrameworkunifiedStatusFail;
500       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
501       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : Invlid connectKey:%s", (it->second).c_str());
502       // LCOV_EXCL_BR_STOP
503     } else {
504       if (pApp->lostSessionHandler != NULL) {
505         int cntFound = frameworkunifiedPushLostSessionInfo(hApp, clientName);
506         if (cntFound) {
507           // calling lost session handler
508           try {
509             eStatus = pApp->lostSessionHandler(hApp);
510           } catch (const std::exception &e) {
511             FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Recovering from: %s", e.what());
512             eStatus = eFrameworkunifiedStatusErrOther;
513           }
514         }
515       }
516     }
517     pApp->acceptMonitorTable.erase(sockfd);
518   }
519
520   return eStatus;  // LCOV_EXCL_BR_LINE 11:except branch
521 }
522
523 ///
524 /// Abnormal monitor(server): Add
525 ///
526 EFrameworkunifiedStatus frameworkunifiedAddAcceptMonitor(HANDLE hApp) {
527   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
528   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
529   int acceptSock;
530
531   if ((acceptSock = mcGetAcceptMonitor(pApp->cAppName, pApp->cMsgSrcName)) < 0) {
532     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
533     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : mcGetAcceptMonitor srv:%s clnt:%s",
534            pApp->cAppName != 0 ? pApp->cAppName : NULL, pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL);
535     // LCOV_EXCL_BR_STOP
536     eStatus = eFrameworkunifiedStatusFail;
537   } else {
538     char connectKey[32];
539     mcSetConnectKey(connectKey, sizeof(connectKey), pApp->cAppName, pApp->cMsgSrcName);
540     pApp->acceptMonitorTable.insert(std::make_pair(acceptSock, connectKey));
541   }
542
543   return eStatus;
544 }
545
546 ///
547 /// Abnormal monitor(server): Del
548 ///
549 EFrameworkunifiedStatus frameworkunifiedDelAcceptMonitor(HANDLE hApp) {
550   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
551   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
552   long acceptSock;  // NOLINT  (readability/nolint)
553
554   char connectKey[32];
555   mcSetConnectKey(connectKey, sizeof(connectKey), pApp->cAppName, pApp->cMsgSrcName);
556   if (frameworkunifiedGetResource(FRAMEWORKUNIFIED_RES_ABNMLMON, connectKey, &acceptSock) < 0) {
557     eStatus = eFrameworkunifiedStatusFail;
558   } else {
559     if (pApp->acceptMonitorTable.find(static_cast<Socket>(acceptSock)) != pApp->acceptMonitorTable.end()) {
560       pApp->acceptMonitorTable.erase(static_cast<Socket>(acceptSock));
561     }
562   }
563
564   return eStatus;
565 }
566
567 ///
568 /// Abnormal monitor(client): Server socket close handle
569 ///
570 EFrameworkunifiedStatus frameworkunifiedProcessServerAbnormalState(HANDLE hApp, int sockfd) {
571   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
572   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
573   ConnectMonitorTable::iterator it;
574
575   if ((it = pApp->connectMonitorTable.find(sockfd)) == pApp->connectMonitorTable.end()) {
576     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
577     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid fd", sockfd);
578     // LCOV_EXCL_BR_STOP
579   } else {
580     char serverName[16];
581     if (mcGetServerNameFromConnectKey((it->second).c_str(), serverName, sizeof(serverName)) < 0) {
582       eStatus = eFrameworkunifiedStatusFail;
583       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
584       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : Invlid connectKey:%s", (it->second).c_str());
585       // LCOV_EXCL_BR_STOP
586     } else {
587       if (pApp->lostSessionHandler != NULL) {
588         int cntFound = frameworkunifiedPushLostSessionInfo(hApp, serverName);  // LCOV_EXCL_BR_LINE 11:except branch
589         if (cntFound) {
590           // calling lost session handler
591           try {
592             eStatus = pApp->lostSessionHandler(hApp);
593           } catch (const std::exception &e) {
594             // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
595             FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Recovering from: %s", e.what());
596             // LCOV_EXCL_BR_STOP
597             eStatus = eFrameworkunifiedStatusErrOther;
598           }
599         }
600       }
601     }
602     pApp->connectMonitorTable.erase(sockfd);
603   }
604
605   return eStatus;  // LCOV_EXCL_BR_LINE 11:except branch
606 }
607
608 ///
609 /// Abnormal monitor(client): Add
610 ///
611 EFrameworkunifiedStatus frameworkunifiedAddConnectMonitor(HANDLE hApp, PCSTR serverName, PCSTR clientName) {
612   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
613   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
614
615   int connectSock = mcConnectMonitor(serverName, clientName);
616   if (connectSock < 0) {
617     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error::%s:%s mcConnectMonitor",
618            serverName != 0 ? serverName : NULL, pApp->cAppName != 0 ? pApp->cAppName : NULL);
619     eStatus = eFrameworkunifiedStatusFail;
620   } else {
621     if (pApp->connectMonitorTable.find(connectSock) == pApp->connectMonitorTable.end()) {
622       char connectKey[32];
623       mcSetConnectKey(connectKey, sizeof(connectKey), serverName, clientName);
624
625       struct epoll_event event;
626       event.events = EPOLLRDHUP;
627       event.data.fd = connectSock;
628
629       if (epoll_ctl(pApp->efd, EPOLL_CTL_ADD, connectSock, &event) < 0) {
630         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl:%s", strerror(errno));
631         eStatus = eFrameworkunifiedStatusFail;
632       }
633
634       if (eStatus == eFrameworkunifiedStatusOK) {
635         CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
636         pApp->connectMonitorTable.insert(std::make_pair(connectSock, connectKey));
637       }
638     }
639   }
640
641   return eStatus;
642 }
643
644 ///
645 /// Abnormal monitor(client): Del
646 ///
647 EFrameworkunifiedStatus frameworkunifiedDelConnectMonitor(HANDLE hApp, PCSTR serverName, PCSTR clientName) {
648   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
649   CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
650
651   char connectKey[32];
652   mcSetConnectKey(connectKey, sizeof(connectKey), serverName, clientName);
653
654   long connectSock;  // NOLINT  (readability/nolint)
655   if (frameworkunifiedGetResource(FRAMEWORKUNIFIED_RES_ABNMLMON, connectKey, &connectSock) < 0) {
656     eStatus = eFrameworkunifiedStatusFail;
657   } else {
658     if (pApp->connectMonitorTable.find(static_cast<Socket>(connectSock)) != pApp->connectMonitorTable.end()) {
659       pApp->connectMonitorTable.erase(static_cast<Socket>(connectSock));
660     }
661
662     if (epoll_ctl(pApp->efd, EPOLL_CTL_DEL, static_cast<int>(connectSock), NULL) < 0) {
663       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl:%s", strerror(errno));
664       eStatus = eFrameworkunifiedStatusFail;
665     }
666   }
667
668   return eStatus;
669 }
670
671 ///
672 /// Abnormal monitor(common): Attach callback
673 ///
674 EFrameworkunifiedStatus FrameworkunifiedAttachLostSessionCallbackToDispatcher(HANDLE hApp, CbFuncPtr fpLostSession) {
675   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
676
677   if (frameworkunifiedCheckValidAppHandle(hApp) == FALSE || fpLostSession == NULL) {
678     eStatus = eFrameworkunifiedStatusInvldParam;
679   } else {
680     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
681     pApp->lostSessionHandler = fpLostSession;
682   }
683
684   return eStatus;
685 }
686
687 ///
688 /// Abnormal monitor(common): Get lost session data
689 ///
690 EFrameworkunifiedStatus FrameworkunifiedGetLostSessionData(HANDLE hApp, PSTR pServiceName, PUI_32 puiSessionId) {
691   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
692
693   if (frameworkunifiedCheckValidAppHandle(hApp) == FALSE || pServiceName == NULL || puiSessionId == NULL) {
694     eStatus = eFrameworkunifiedStatusInvldParam;
695   } else {
696     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
697     if (pApp->lostSessionQueue.empty()) {
698       eStatus = eFrameworkunifiedStatusAccessError;
699     } else {
700       LostSessionInfo info = pApp->lostSessionQueue.front();
701       pApp->lostSessionQueue.pop();
702       strcpy(pServiceName, info.sServiceName.c_str());  // NOLINT  (readability/nolint)
703       *puiSessionId = info.iSessionId;
704     }
705   }
706
707   return eStatus;
708 }
709
710
711 //////////////////////////////////////////
712 // Function : FrameworkunifiedDispatchProcess
713 //////////////////////////////////////////
714 EFrameworkunifiedStatus FrameworkunifiedDispatchProcess(HANDLE hApp) {
715   EFrameworkunifiedStatus eStatus     = eFrameworkunifiedStatusOK;
716   // FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "+");
717
718   if (frameworkunifiedCheckValidAppHandle(hApp)) {
719     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
720     struct rusage start, end;
721     BOOL enable_rusage = NsLogIsZoneSet(ZONE_NS_DEBUG_DUMP);
722
723     if (enable_rusage == TRUE) {
724       frameworkunifiedGetRusage(&start);
725     }
726
727     if (0 != strcmp(FRAMEWORKUNIFIED_NS_NPSERVICE, pApp->cMsgSrcName)) {  // < if not from NPP service
728       switch (pApp->uiProtocolCmd) {
729         case PROTOCOL_REGISTER_EVENTS: {
730             FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "%s subscribing to session event on sessionid %d",
731                    pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL, pApp->uiSessionId);
732
733             eStatus = ProcessProtocolEvents(hApp);
734           }
735           break;
736
737         case PROTOCOL_UNREGISTER_EVENTS: {
738             FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "%s unsubscribing private event on sessionid %d",
739                    pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL, pApp->uiSessionId);
740
741             eStatus = UnregisterPrivateEvents(hApp);
742           }
743           break;
744
745         case PROTOCOL_CLOSE_SESSION_REQ:
746         case PROTOCOL_CLOSE_SESSION_REQ_SYNC: {
747             FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "Close session request received from %s",
748                    pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL);
749
750             // Handle the session events on the close session request
751             (VOID)HandleSessionEventOnCloseSession(hApp);
752
753             frameworkunifiedDelAcceptMonitor(hApp);
754
755             eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
756           }
757           break;
758
759         case PROTOCOL_OPEN_SESSION_REQ:
760         case PROTOCOL_OPEN_SESSION_REQ_SYNC: {
761             FRAMEWORKUNIFIEDLOG(ZONE_NS_IMP_INFO, __FUNCTION__, "Open session request received from %s",
762                    pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL);
763
764             eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
765             if (eStatus == eFrameworkunifiedStatusOK) {
766               frameworkunifiedAddAcceptMonitor(hApp);
767             }
768           }
769           break;
770
771         default: {
772             eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
773           }
774           break;
775       }
776     } else {
777       NotificationTable::iterator n_iterator;
778       FrameworkunifiedGetSystemInfo(pApp, pApp->cSystemInfo);
779       n_iterator = pApp->notifications.find(static_cast<PSTR>(pApp->cSystemInfo));
780       if (n_iterator != pApp->notifications.end()) {
781         FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, " Notification found in NS_NP :: (%s)."
782                " Processing callback function.", n_iterator->first.c_str());
783         eStatus = (*n_iterator).second(hApp);
784       } else {
785         NotificationEventTable::iterator n_iterator;
786         FrameworkunifiedGetSystemInfo(pApp, pApp->cSystemInfo);
787         n_iterator = pApp->notificationevents.find(static_cast<PSTR>(pApp->cSystemInfo));
788         if (n_iterator != pApp->notificationevents.end()) {
789           CFrameworkunifiedHSMFramework *l_pStateMachine = FrameworkunifiedGetStateMachine(hApp);
790           if (l_pStateMachine) {
791             FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, " Notification found in NS_NP :: (%s)."
792                    " Processing HSM event.", n_iterator->first.c_str());
793             eStatus = l_pStateMachine->FrameworkunifiedPostEvent(n_iterator->second);
794           }
795         } else {
796           /// Paul A. Dennis (FOUND BUG)
797           /// For Persistent Data NS_NP sends the data back as a normal command not via the
798           /// notification logic... The Ack's for NPReadPersistedData,NPRegisterFilePath and
799           /// NPLoadFileFromPersistentStorage were never getting back
800           /// to the requester because its's not looking in the correct location.
801           /// So, Check the Services to see if the user has defined the Ack commands via the
802           /// services map
803           eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
804         }
805       }
806     }
807
808     frameworkunifiedProcessSyncResponse(hApp, eStatus);
809
810     if ((eStatus != eFrameworkunifiedStatusOK) && (eStatus != eFrameworkunifiedStatusExit) && (eStatus != eFrameworkunifiedStatusMsgNotProcessed)) {
811       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error :: %d, (%s) Event Info :: (%s), (%d), (%s)",
812              eStatus,
813              pApp->cAppName != 0 ? static_cast<PSTR>(pApp->cAppName) : NULL,
814              pApp->cMsgSrcName != 0 ? static_cast<PSTR>(pApp->cMsgSrcName) : NULL,
815              pApp->uiProtocolCmd,
816              pApp->cSystemInfo != 0 ? static_cast<PSTR>(pApp->cSystemInfo) : NULL);
817     }
818
819     if (eStatus == eFrameworkunifiedStatusMsgNotProcessed) {
820       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "WARNING:: No handler found for :: "
821              "Source: %s, Protocol Command: 0x%x, Session Id: %u.",
822              pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL,
823              pApp->uiProtocolCmd, pApp->uiSessionId);
824       FrameworkunifiedClearMsgData(hApp);
825       eStatus = eFrameworkunifiedStatusOK;
826     } else {
827 //      FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "Message processed. Status (0x%X) ::"
828 //             " Source: %s Command: 0x%x  SessionId: %d.",
829 //             eStatus, pApp->cMsgSrcName != 0 ? pApp->cMsgSrcName : NULL,
830 //             pApp->uiProtocolCmd, pApp->uiSessionId);
831     }
832
833     if (enable_rusage == TRUE) {
834       frameworkunifiedGetRusage(&end);
835       frameworkunifiedPrintRusage(hApp, start, end);
836     }
837   } else {
838     eStatus = eFrameworkunifiedStatusInvldHandle;
839   }
840
841   // FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "-");
842   return eStatus;
843 }
844
845
846
847
848 //////////////////////////////////////////
849 // Function : FrameworkunifiedCloseDispatcher
850 //////////////////////////////////////////
851 EFrameworkunifiedStatus FrameworkunifiedCloseDispatcher(HANDLE hApp) {
852   EFrameworkunifiedStatus eStatus     = eFrameworkunifiedStatusOK;
853
854   if (frameworkunifiedCheckValidAppHandle(hApp)) {
855     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
856     pApp->checkCode = 0;
857
858     if (NULL != pApp->hAppRcvMsgQ) {
859       McClose(pApp->hAppRcvMsgQ);
860       pApp->hAppRcvMsgQ = NULL;
861     }
862
863     if (pApp->siMonitorSock) {
864       mcCloseMonitor(pApp->cAppName);
865       pApp->siMonitorSock = 0;
866     }
867
868     if (NULL != pApp->hAppSndMsgQ) {
869       McClose(pApp->hAppSndMsgQ);
870       pApp->hAppSndMsgQ = NULL;
871     }
872
873     if (NULL != pApp->hNPSndMsgQ) {
874       FrameworkunifiedMcClose(pApp->hNPSndMsgQ);
875       pApp->hNPSndMsgQ = NULL;
876     }
877
878     if (NULL != pApp->hParentSndMsgQ) {
879       McClose(pApp->hParentSndMsgQ);
880       pApp->hParentSndMsgQ = NULL;
881     }
882
883     if (0 < pApp->defer_evfd) {
884       close(pApp->defer_evfd);
885       pApp->defer_evfd = 0;
886     }
887
888     if (0 < pApp->efd) {
889       close(pApp->efd);
890       pApp->efd = 0;
891     }
892
893     delete(pApp);
894     pApp = NULL;  // mb20110110 Added per  comment 216
895     // NOTE: However, client still needs to set his own pointer to NULL
896   } else {
897     eStatus = eFrameworkunifiedStatusInvldHandle;
898   }
899
900   return eStatus;
901 }
902
903 //////////////////////////////////////////
904 // Function : FrameworkunifiedPopedDeferedMessage
905 //////////////////////////////////////////
906 BOOL FrameworkunifiedPopedDeferedMessage(HANDLE hApp) {
907   BOOL rtnFlag = FALSE;
908
909   // LCOV_EXCL_BR_START 6:FrameworkunifiedPopedDeferedMessage is internal function, hApp is checked in extenal API
910   if (frameworkunifiedCheckValidAppHandle(hApp)) {
911   // LCOV_EXCL_BR_STOP
912     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
913
914     // check if the pop flag is send and there is something to pop!
915     if (pApp->fPopDeferedMsg && !pApp->deferedMsgQueue.empty()) {
916       // get the message at the top\front of the queue
917       DeferedMsgInfo defered(pApp->deferedMsgQueue.front());
918
919       // remove this item from the queue using pop
920       pApp->deferedMsgQueue.pop();
921
922       // copy the data into the pApp
923       pApp->uiProtocolCmd = defered.uiProtocolCmd;
924       strlcpy(pApp->cMsgSrcName, defered.cMsgSrcName, sizeof(pApp->cMsgSrcName));
925       std::memcpy(pApp->uiMsgRcvBuffer, defered.uiMsgRcvBuffer, MAX_MSGQ_BUFFER);
926
927       // clear the pop flag!
928       pApp->fPopDeferedMsg = FALSE;
929
930       // set the return flag TRUE, we have pop'ed
931       rtnFlag = TRUE;
932     }
933   }
934
935   return rtnFlag;
936 }
937
938
939
940 //////////////////////////////////////////
941 // Function : FrameworkunifiedDispatcher
942 //////////////////////////////////////////
943 EFrameworkunifiedStatus FrameworkunifiedDispatcher(PCSTR cAppName, const FrameworkunifiedDefaultCallbackHandler *CbHandler) {
944   FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "In");
945   return FrameworkunifiedDispatcherWithArguments(cAppName, 0, NULL, CbHandler);
946 }
947
948 ///////////////////////////////////////////////////////////////////////////////////////////
949 // FrameworkunifiedDispatcherWithArguments
950 // Creates, initializes and runs the dispatcher
951 ///////////////////////////////////////////////////////////////////////////////////////////
952 EFrameworkunifiedStatus FrameworkunifiedDispatcherWithArguments(PCSTR cAppName, int argc, char *argv[],
953                                       const FrameworkunifiedDefaultCallbackHandler *CbHandler,
954                                       CustomCommandLineOptions *cmdLineOptions) {
955   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
956
957   if (cAppName == NULL) {
958     return eFrameworkunifiedStatusNullPointer;
959   }
960
961   NsLogSetProcessName(cAppName);
962   PLOG_TEXT("FrameworkunifiedDispatcher Start");
963   // set main thread name as provided in dispatcher
964   pthread_setname_np(pthread_self(), cAppName);
965
966   FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "In");
967
968   if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
969     return eStatus;
970   }
971
972   try {
973     HANDLE hFrameworkApp = NULL;
974
975     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateDispatcher(cAppName, hFrameworkApp, FALSE))) {
976       // LCOV_EXCL_BR_START 6:if FrameworkunifiedCreateDispatcher return eFrameworkunifiedStatusOK, hFrameworkApp is not NULL
977       if (NULL != hFrameworkApp) {
978       // LCOV_EXCL_BR_STOP
979         THApp hApp(hFrameworkApp);
980
981         /// Parse the Arguments via the FrameworkunifiedArgumentParser
982         /// passing an handle to the app and argument list
983         eStatus = FrameworkunifiedArgumentParser(hApp, argc, argv, cmdLineOptions);
984
985         if (eFrameworkunifiedStatusOK == eStatus) {
986           eStatus = FrameworkunifiedDispatcherMain(hApp);
987         }
988       } else {
989         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
990         eStatus = eFrameworkunifiedStatusNullPointer;
991       }
992     } else {
993       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateDispatcher error, status=%d", eStatus);
994     }
995   } catch (const std::exception &e) {
996     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher: %s", e.what());
997     eStatus = eFrameworkunifiedStatusNullPointer;
998   }
999
1000
1001   return eStatus;
1002 }
1003
1004 // required for parsing subargument of -r
1005 char *g_cPlogSubOpts[] = {
1006 #define PLOG_OUTPUT    0
1007   const_cast<PSTR>("output"),
1008   NULL
1009 };
1010
1011 EFrameworkunifiedStatus FrameworkunifiedArgumentParser(HANDLE hApp, int argc, char *argv[], CustomCommandLineOptions *cmdLineOptions) {
1012   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1013   TFrameworkunifiedZoneMask mask = {};
1014   PCHAR l_cMask = NULL;
1015   UI_32 l_iMaskCount = 0;
1016   CHAR l_logoutput[MAX_QUEUE_NAME_SIZE];
1017   CHAR l_tFilePath[MAX_PATH_LENGTH];
1018
1019   BOOL l_bHasConfigFilepath = FALSE;
1020
1021   SI_32 l_iOpt = 0;
1022   SI_32 l_iPriority = 10;
1023   PSTR l_cOptions;
1024   PSTR l_cValue;
1025
1026   FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "In");
1027
1028   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1029 #ifdef DISPATCHER_PROFILER
1030     CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
1031 #endif
1032
1033     if (argc > 0 && argv != NULL) {
1034       // l:m:t:c: options are reserved for NS Framework and should not be used by other FRAMEWORKUNIFIED Applications.
1035       CHAR l_cNSOptions[MAX_STRING_SIZE_TAG] = {};
1036
1037       strncpy(l_cNSOptions, "l:m:c:p:qr:s", (MAX_STRING_SIZE_TAG - 1));
1038       optind = 1;
1039
1040       if (NULL != cmdLineOptions) {
1041         // append custom options
1042         strncat(l_cNSOptions, cmdLineOptions->cShortOptions, (MAX_STRING_SIZE_TAG - strlen(l_cNSOptions) - 1));
1043       }
1044       while ((eFrameworkunifiedStatusOK == eStatus) && ((l_iOpt = getopt(argc, argv, l_cNSOptions)) != -1)) {
1045         switch (l_iOpt) {
1046           case 'm':  // mask value..
1047             FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-m (mask value): %s", optarg);
1048             /*
1049              * @todo
1050              * If the second of the command-line arguments is-m and the third is not, an exception occurs.
1051              */
1052             char *saveptr;
1053             l_cMask = strtok_r(optarg, ",", &saveptr);
1054
1055             while (NULL != l_cMask) {
1056               mask[l_iMaskCount++] = static_cast<TFrameworkunifiedZone>(strtoul(l_cMask, NULL, 16));
1057               l_cMask = strtok_r(NULL, ",",  &saveptr);
1058             }
1059             NsLogSetControlMask(mask);
1060             break;
1061           case 'l':  // log output
1062             strncpy(l_logoutput, optarg, (MAX_QUEUE_NAME_SIZE - 1));
1063             FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-l (log output option): %s", l_logoutput);
1064             NsLogSetLogMethod(NsLogDetermineLogMethod(l_logoutput));
1065             break;
1066           case 'c':  // config file option
1067             strncpy(l_tFilePath, optarg, (MAX_PATH_LENGTH - 1));
1068             FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-c (set config file): %s", l_tFilePath);
1069             eStatus = SetConfigHandle(hApp, l_tFilePath);
1070             l_bHasConfigFilepath = TRUE;
1071             break;
1072           case 'p':  // priority
1073             FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-p (set priority): %s", optarg);
1074             if (eFrameworkunifiedStatusOK != frameworkunified::framework::CFrameworkunifiedThreadPriorities::ParseThreadArguments(optarg)) {
1075               FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "WARNING!! Failed to parse sub arguments of -p");
1076             }
1077             l_iPriority = frameworkunified::framework::CFrameworkunifiedThreadPriorities::GetPriority(FrameworkunifiedGetAppName(hApp));
1078             // set only if valid prio, otherwise use the default one
1079             if (FRAMEWORKUNIFIED_PRIORITY_NOT_FOUND != l_iPriority) {
1080               pthread_setschedprio(pthread_self(),
1081               frameworkunified::framework::CFrameworkunifiedThreadPriorities::GetPriority(FrameworkunifiedGetAppName(hApp)));
1082             }
1083             break;
1084           case 'q':
1085 #ifdef DISPATCHER_PROFILER
1086             FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-q (set msg dispatcher profiler)");
1087             FrameworkunifiedMsgProfiler::m_bMsgProfilerEnabled = TRUE;
1088             pApp->m_pFrameworkunifiedMsgProfiler = new(nothrow) FrameworkunifiedMsgProfiler(FrameworkunifiedGetAppName(hApp));
1089 #endif
1090             break;
1091           case 'r':
1092             NSLogEnablePLog(TRUE);
1093             l_cOptions = optarg;
1094             while (*l_cOptions != '\0') {
1095               switch (getsubopt(&l_cOptions, g_cPlogSubOpts, &l_cValue)) {
1096                 case PLOG_OUTPUT: {
1097                     if (l_cValue != NULL) {
1098                       NSLogSetPlogOutputOptions(static_cast<UI_8>(strtoul(l_cValue, NULL, 16)));
1099                     }
1100                     break;
1101                   }
1102                 default: {
1103                     break;
1104                   }
1105               }
1106             }
1107             break;
1108           case 's':
1109             NSLogEnableSysEventLog(TRUE);
1110             break;
1111           default:
1112             if (NULL != cmdLineOptions) {
1113               // option other than l:m:t:c: is a custom option, therefore let application handle that option.
1114               if (eFrameworkunifiedStatusOK != cmdLineOptions->callback(l_iOpt, optarg)) {
1115                 // Do not terminate an application even if invalid arguments received
1116                 // eStatus = eFrameworkunifiedStatusFail;
1117                 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "WARNING!! Invalid cmd line argument received.");
1118               }
1119             } else {
1120               // Do not terminate an application even if invalid arguments received
1121               // eStatus = eFrameworkunifiedStatusFail;
1122               FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "WARNING!! Invalid cmd line argument received.");
1123             }
1124             break;
1125         }
1126       }
1127     }
1128
1129     // if no config option is selected then use the default config path
1130     if ((FALSE == l_bHasConfigFilepath) && (eFrameworkunifiedStatusOK == eStatus)) {
1131       eStatus = SetDefaultConfigFile(hApp);
1132     }
1133   } else {
1134     eStatus = eFrameworkunifiedStatusInvldHandle;
1135   }
1136
1137   return eStatus;
1138 }
1139
1140 //////////////////////////////////////////
1141 // Function : FrameworkunifiedSimpleDispatcher
1142 //////////////////////////////////////////
1143 EFrameworkunifiedStatus FrameworkunifiedSimpleDispatcher(PCSTR cAppName, const FrameworkunifiedDefaultCallbackHandler *CbHandler, CbFuncPtr CbShutdown,
1144                                BOOL isChildThread) {
1145   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1146
1147   // if main thread then set log process name
1148   if (!isChildThread) {
1149     NsLogSetProcessName(cAppName);
1150   }
1151
1152   if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
1153     return eStatus;
1154   }
1155
1156   FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "+");
1157
1158   if (NULL != CbShutdown) {
1159     try {
1160       HANDLE hFrameworkApp = NULL;
1161
1162       if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateDispatcher(cAppName, hFrameworkApp, isChildThread))) {
1163         // LCOV_EXCL_BR_START 6:if FrameworkunifiedCreateDispatcher return eFrameworkunifiedStatusOK, hFrameworkApp is not NULL
1164         if (NULL != hFrameworkApp) {
1165         // LCOV_EXCL_BR_STOP
1166           THApp hApp(hFrameworkApp);
1167
1168           // Call application defined start function
1169           eStatus = FrameworkunifiedOnInitializationInternal(hApp);
1170
1171           if (eFrameworkunifiedStatusOK != eStatus) {
1172             FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
1173             throw happ_error();
1174           } else {
1175             while (eFrameworkunifiedStatusExit != eStatus) {
1176               try {
1177                 // dispatch block: waits on receive message
1178                 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDispatchBlock(hApp))) {
1179                   FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: FrameworkunifiedDispatchBlock : 0x%x ",  eStatus);
1180                   continue;
1181                 }
1182                 // dispatch process: calls respective call back functions
1183                 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDispatchProcess(hApp))) {
1184                   FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: FrameworkunifiedDispatchProcess : 0x%x ",  eStatus);
1185                 }
1186               } catch (const frameworkunified::framework::error::CSystemError &err) {
1187                 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR :: %s", err.what());
1188                 eStatus = eFrameworkunifiedStatusErrOther;
1189                 FrameworkunifiedSendSystemErrMessage(hApp, err.m_eSystemError);
1190               } catch (const std::exception &err) {
1191                 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Recovering from: %s", err.what());
1192                 eStatus = eFrameworkunifiedStatusErrOther;
1193               }
1194             }
1195             FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exiting dispatcher ");
1196           }
1197           // Call application defined stop function
1198           eStatus = CbShutdown(hApp);
1199         } else {
1200           FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
1201           eStatus = eFrameworkunifiedStatusNullPointer;
1202         }
1203       } else {
1204         FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateDispatcher error, status=%d", eStatus);
1205       }
1206     } catch (const THApp::Exception &) {
1207       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher ");
1208       eStatus = eFrameworkunifiedStatusNullPointer;
1209     }
1210   } else {
1211     eStatus = eFrameworkunifiedStatusFail;
1212   }
1213
1214   FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-");
1215   return eStatus;
1216 }
1217
1218 EFrameworkunifiedStatus RunDispatcher(HANDLE hApp) {
1219   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1220   int efd;
1221
1222   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1223 #ifdef DISPATCHER_PROFILER
1224     // Get the application handle
1225     CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
1226     if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) {
1227       FrameworkunifiedAttachMsgProfilerCallbacksDispatcher(hApp);
1228     }
1229 #endif
1230     FrameworkunifiedGetDispatcherFD(hApp, &efd);
1231
1232     while (1) {
1233       frameworkunifiedFdHandler(hApp, efd);
1234     }
1235   } else {
1236     eStatus = eFrameworkunifiedStatusInvldHandle;
1237   }
1238
1239   return eStatus;
1240 }
1241
1242
1243 EFrameworkunifiedStatus FrameworkunifiedDispatcherMain(HANDLE hApp) {
1244   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1245
1246   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1247     try {
1248       FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "Success: FrameworkunifiedCreateDispatcher ");
1249
1250       // Initialize the global structures
1251       // register service with service dir
1252       // register notifications
1253       FRAMEWORKUNIFIEDLOG_PERFORMANCE_DEBUG("FrameworkunifiedOnInitialization +");
1254       eStatus = FrameworkunifiedOnInitializationInternal(hApp);
1255       FRAMEWORKUNIFIEDLOG_PERFORMANCE_DEBUG("FrameworkunifiedOnInitialization -");
1256
1257       if (eFrameworkunifiedStatusOK != eStatus) {
1258         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
1259         throw happ_error();
1260       } else {
1261         eStatus = RunDispatcher(hApp);
1262         CFrameworkunifiedSyncData::FrameworkunifiedReleaseSyncDataInstance();
1263       }
1264     } catch (const frameworkunified::framework::error::CSystemError &err) {
1265       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR :: %s", err.what());
1266       eStatus = eFrameworkunifiedStatusErrOther;
1267       FrameworkunifiedSendSystemErrMessage(hApp, err.m_eSystemError);
1268     } catch (const THApp::Exception &) {
1269       FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher ");
1270       eStatus = eFrameworkunifiedStatusNullPointer;
1271     }
1272   } else {
1273     eStatus = eFrameworkunifiedStatusInvldHandle;
1274   }
1275
1276   return eStatus;
1277 }
1278
1279 EFrameworkunifiedStatus FrameworkunifiedCreateDispatcherWithoutLoop(PCSTR cAppName, HANDLE &hApp, int argc, char *argv[],  // NOLINT  (readability/nolint)
1280                                           const FrameworkunifiedDefaultCallbackHandler *CbHandler,
1281                                           BOOL bIsConnectSM, CustomCommandLineOptions *cmdLineOptions) {
1282   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1283   THApp *l_hApp = NULL;
1284
1285   if (cAppName == NULL) {
1286     return eFrameworkunifiedStatusNullPointer;
1287   }
1288
1289   NsLogSetProcessName(cAppName);
1290
1291   PLOG_TEXT("FrameworkunifiedDispatcher Start");
1292   // set main thread name as provided in dispatcher
1293   pthread_setname_np(pthread_self(), cAppName);
1294
1295   if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
1296     return eStatus;
1297   }
1298
1299   FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "In");
1300
1301   try {
1302     HANDLE hFrameworkApp = NULL;
1303
1304     if (eFrameworkunifiedStatusOK == (eStatus = FrameworkunifiedCreateDispatcher(cAppName, hFrameworkApp,
1305       bIsConnectSM == TRUE ? FALSE : TRUE))) {
1306       // LCOV_EXCL_BR_START 6:if FrameworkunifiedCreateDispatcher return eFrameworkunifiedStatusOK, hFrameworkApp is not NULL
1307       if (NULL != hFrameworkApp) {
1308       // LCOV_EXCL_BR_STOP
1309         l_hApp = new THApp(hFrameworkApp);
1310         hApp = *l_hApp;
1311         CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1312         pApp->pRsrcMgr = reinterpret_cast<void *>(l_hApp);
1313
1314         eStatus = FrameworkunifiedArgumentParser(hApp, argc, argv, cmdLineOptions);
1315
1316         if (eFrameworkunifiedStatusOK == eStatus) {
1317           FRAMEWORKUNIFIEDLOG_PERFORMANCE_DEBUG("FrameworkunifiedOnInitialization +");
1318           eStatus = FrameworkunifiedOnInitializationInternal(hApp);
1319           FRAMEWORKUNIFIEDLOG_PERFORMANCE_DEBUG("FrameworkunifiedOnInitialization -");
1320
1321           if (eFrameworkunifiedStatusOK != eStatus) {
1322             FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
1323           }
1324         }
1325       }
1326     } else {
1327       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateDispatcher error, status=%d", eStatus);
1328     }
1329   } catch (const THApp::Exception &) {
1330     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher ");
1331     eStatus = eFrameworkunifiedStatusNullPointer;
1332   }
1333
1334   return eStatus;
1335 }
1336
1337 EFrameworkunifiedStatus FrameworkunifiedDispatchProcessWithoutLoop(HANDLE hApp) {
1338   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1339   int efd;  // FD for multi waiting
1340
1341   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1342     FrameworkunifiedGetDispatcherFD(hApp, &efd);
1343     eStatus = frameworkunifiedFdHandler(hApp, efd);
1344     if (eFrameworkunifiedStatusOK != eStatus) {
1345       eStatus = eFrameworkunifiedStatusFail;
1346     }
1347   } else {
1348     eStatus = eFrameworkunifiedStatusInvldHandle;
1349   }
1350
1351   return eStatus;
1352 }
1353
1354 EFrameworkunifiedStatus FrameworkunifiedDestroyDispatcherWithoutLoop(HANDLE hApp) {
1355   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1356
1357   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1358     try {
1359       CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1360       THApp *RsrcMgr = reinterpret_cast<THApp *>(pApp->pRsrcMgr);
1361       delete RsrcMgr;
1362     } catch (const std::exception &e) {
1363       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1364       eStatus = eFrameworkunifiedStatusNullPointer;
1365     }
1366   } else {
1367     eStatus = eFrameworkunifiedStatusInvldHandle;
1368   }
1369
1370   return eStatus;
1371 }
1372
1373 static EFrameworkunifiedStatus GetDispatcherMqFD(HANDLE hApp, int *fd) {
1374   if (fd == NULL) {
1375     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "fd is NULL");
1376     return eFrameworkunifiedStatusNullPointer;
1377   }
1378   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1379
1380   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1381     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1382     *fd = McGetQueueFD(pApp->hAppRcvMsgQ);
1383   } else {
1384     eStatus = eFrameworkunifiedStatusInvldHandle;
1385   }
1386
1387   return eStatus;
1388 }
1389
1390 static EFrameworkunifiedStatus CreateMultiWaitingFD(HANDLE hApp) {
1391   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1392   int efd;        // FD for multi waiting
1393   int q_fd;       // FD for Dispatcher's message queue
1394   int defer_evfd;     // FD for receiving Defer events to Dispatcher
1395   struct epoll_event ev;  // Info struct to associate with multiwaiting FD
1396
1397   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1398     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1399
1400     // Create efd
1401     efd = epoll_create1(EPOLL_CLOEXEC);
1402     if (-1 != efd) {
1403       pApp->efd = efd;
1404
1405       // get fd to Dispatcher's message queue
1406       eStatus = GetDispatcherMqFD(hApp, &q_fd);
1407       if (eFrameworkunifiedStatusOK == eStatus) {
1408         // efd to monitor the q_fd.
1409         ev.events = EPOLLIN;
1410         ev.data.fd = q_fd;
1411         if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, q_fd, &ev)) {
1412           eStatus = eFrameworkunifiedStatusFail;
1413           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(q_fd, ADD) Failed, "
1414             "status=%d, errno=%d", eStatus, errno);
1415         }
1416       } else {
1417         eStatus = eFrameworkunifiedStatusFail;
1418         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : GetDispatcherMqFD Failed, status=%d", eStatus);
1419       }
1420
1421       // create FD for receiving Defer events to Dispatcher
1422       defer_evfd = eventfd(0, EFD_CLOEXEC);
1423       if (-1 != defer_evfd) {
1424         pApp->defer_evfd = defer_evfd;
1425
1426         // efd to monitor the defer_evfd.
1427         ev.events = EPOLLIN;
1428         ev.data.fd = defer_evfd;
1429         if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, defer_evfd, &ev)) {
1430           eStatus = eFrameworkunifiedStatusFail;
1431           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl(defer_evfd, ADD) Failed,"
1432             " status=%d, errno=%d", eStatus, errno);
1433         }
1434       } else {
1435         eStatus = eFrameworkunifiedStatusFail;
1436         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : eventfd Failed, status=%d, errno=%d", eStatus, errno);
1437       }
1438
1439       // add abnormal monitor socket polling
1440       if (-1 != pApp->siMonitorSock) {
1441         ev.events = EPOLLIN;
1442         ev.data.fd = pApp->siMonitorSock;
1443         if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, pApp->siMonitorSock, &ev)) {
1444           eStatus = eFrameworkunifiedStatusFail;
1445           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl:%s", strerror(errno));
1446         }
1447       }
1448     } else {
1449       eStatus = eFrameworkunifiedStatusFail;
1450       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_create1 Failed, status=%d, errno=%d", eStatus, errno);
1451     }
1452   } else {
1453     eStatus = eFrameworkunifiedStatusInvldHandle;
1454     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1455   }
1456
1457   return eStatus;
1458 }
1459
1460 EFrameworkunifiedStatus FrameworkunifiedGetDispatcherFD(HANDLE hApp, int *efd) {
1461   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1462
1463   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1464     if (NULL != efd) {
1465       CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1466       *efd = pApp->efd;
1467     } else {
1468       eStatus = eFrameworkunifiedStatusNullPointer;
1469     }
1470   } else {
1471     eStatus = eFrameworkunifiedStatusInvldHandle;
1472     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1473   }
1474
1475   return eStatus;
1476 }
1477
1478 EFrameworkunifiedStatus FrameworkunifiedOnInitializationInternal(HANDLE hApp) {
1479   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1480
1481   if (g_defaultCallbackHandler.onInitilization != NULL) {
1482     try {
1483       eStatus = g_defaultCallbackHandler.onInitilization(hApp);
1484     } catch (const std::exception &e) {
1485       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1486     }
1487   }
1488   return eStatus;
1489 }
1490
1491 EFrameworkunifiedStatus FrameworkunifiedOnDestroyInternal(HANDLE hApp) {
1492   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1493
1494   if (g_defaultCallbackHandler.onDestroy != NULL) {
1495     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1496     if (pApp->hParentSndMsgQ == NULL) {
1497       // main thread
1498       try {
1499         eStatus = g_defaultCallbackHandler.onDestroy(hApp);
1500       } catch (const std::exception &e) {
1501         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1502       }
1503     } else {
1504       eStatus = eFrameworkunifiedStatusOK;
1505     }
1506   }
1507   return eStatus;
1508 }
1509
1510 EFrameworkunifiedStatus FrameworkunifiedOnStartInternal(HANDLE hApp) {
1511   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1512
1513   if (g_defaultCallbackHandler.onStart != NULL) {
1514     try {
1515       eStatus = g_defaultCallbackHandler.onStart(hApp);
1516     } catch (const std::exception &e) {
1517       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1518     }
1519   }
1520   return eStatus;
1521 }
1522
1523 EFrameworkunifiedStatus FrameworkunifiedOnStopInternal(HANDLE hApp) {
1524   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1525
1526   if (g_defaultCallbackHandler.onStop != NULL) {
1527     try {
1528       eStatus = g_defaultCallbackHandler.onStop(hApp);
1529     } catch (const std::exception &e) {
1530       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1531     }
1532   }
1533   return eStatus;
1534 }
1535
1536 EFrameworkunifiedStatus FrameworkunifiedOnPreStartInternal(HANDLE hApp) {
1537   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1538
1539   if (g_defaultCallbackHandler.onPreStart != NULL) {
1540     try {
1541       eStatus = g_defaultCallbackHandler.onPreStart(hApp);
1542     } catch (const std::exception &e) {
1543       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1544     }
1545   }
1546   return eStatus;  // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1547 }
1548
1549 EFrameworkunifiedStatus FrameworkunifiedOnPreStopInternal(HANDLE hApp) {
1550   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1551
1552   if (g_defaultCallbackHandler.onPreStop != NULL) {
1553     try {
1554       eStatus = g_defaultCallbackHandler.onPreStop(hApp);
1555     } catch (const std::exception &e) {
1556       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1557     }
1558   }
1559   return eStatus;  // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1560 }
1561
1562 EFrameworkunifiedStatus FrameworkunifiedOnBackgroundStartInternal(HANDLE hApp) {
1563   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1564
1565   if (g_defaultCallbackHandler.onBackgroundStart != NULL) {
1566     try {
1567       eStatus = g_defaultCallbackHandler.onBackgroundStart(hApp);
1568     } catch (const std::exception &e) {
1569       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1570     }
1571   }
1572   return eStatus;  // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1573 }
1574
1575 EFrameworkunifiedStatus FrameworkunifiedOnBackgroundStopInternal(HANDLE hApp) {
1576   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1577
1578   if (g_defaultCallbackHandler.onBackgroundStop != NULL) {
1579     try {
1580       eStatus = g_defaultCallbackHandler.onBackgroundStop(hApp);
1581     } catch (const std::exception &e) {
1582       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1583     }
1584   }
1585   return eStatus;  // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1586 }
1587
1588 EFrameworkunifiedStatus FrameworkunifiedOnDebugDumpInternal(HANDLE hApp) {
1589   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1590
1591   if (g_defaultCallbackHandler.onDebugDump != NULL) {
1592     try {
1593       eStatus = g_defaultCallbackHandler.onDebugDump(hApp);
1594     } catch (const std::exception &e) {
1595       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1596     }
1597   }
1598   return eStatus;
1599 }
1600
1601 EFrameworkunifiedStatus FrameworkunifiedCreateStateMachineInternal(HANDLE hApp) {
1602   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1603
1604   if (g_defaultCallbackHandler.createStateMachine != NULL) {
1605     try {
1606       eStatus = g_defaultCallbackHandler.createStateMachine(hApp);
1607     } catch (const std::exception &e) {
1608       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1609     }
1610   }
1611   return eStatus;
1612 }
1613
1614 EFrameworkunifiedStatus FrameworkunifiedSSFrameworkInterfaceInternal(HANDLE hApp) {
1615   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1616
1617   if (g_defaultCallbackHandler.ssFrameworkInterface != NULL) {
1618     try {
1619       eStatus = g_defaultCallbackHandler.ssFrameworkInterface(hApp);
1620     } catch (const std::exception &e) {
1621       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1622     }
1623   }
1624   return eStatus;
1625 }
1626
1627 EFrameworkunifiedStatus RegistDefaultCbHandler(const FrameworkunifiedDefaultCallbackHandler *CbHandler) {
1628   if (CbHandler == NULL) {
1629     return eFrameworkunifiedStatusNullPointer;
1630   } else if (CbHandler->onInitilization == NULL || CbHandler->onDestroy == NULL || CbHandler->onStart == NULL ||
1631              CbHandler->onStop == NULL
1632              || CbHandler->onPreStart == NULL || CbHandler->onPreStop == NULL
1633              || CbHandler->onBackgroundStart == NULL || CbHandler->onBackgroundStop == NULL
1634              || CbHandler->onDebugDump == NULL || CbHandler->createStateMachine == NULL
1635              || CbHandler->ssFrameworkInterface == NULL) {
1636     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " [ ABORT ] DefaultCallbackHandler is NULL");
1637     return eFrameworkunifiedStatusInvldParam;
1638   }
1639
1640   g_defaultCallbackHandler = *CbHandler;
1641   return eFrameworkunifiedStatusOK;
1642 }
1643
1644 EFrameworkunifiedStatus FrameworkunifiedGetDefaultCbHandler(FrameworkunifiedDefaultCallbackHandler *CbHandler) {
1645   if (CbHandler == NULL) {
1646     return eFrameworkunifiedStatusNullPointer;
1647   }
1648
1649   *CbHandler = g_defaultCallbackHandler;
1650   return eFrameworkunifiedStatusOK;
1651 }
1652
1653 EFrameworkunifiedStatus frameworkunifiedFdHandler(HANDLE hApp , int efd) {
1654   struct epoll_event events[MAX_FD_MULTIWAITING];
1655   int nfds;  // The number of events received
1656   int q_fd;  // The FD of events received
1657   int n;    // Loop counter
1658   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1659   const char *key;
1660
1661   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1662     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1663     eStatus = GetDispatcherMqFD(hApp, &q_fd);
1664     // LCOV_EXCL_BR_START 6:Because of hApp is valid, GetDispatcherMqFD return eFrameworkunifiedStatusOK.
1665     if (eFrameworkunifiedStatusOK == eStatus) {
1666     // LCOV_EXCL_BR_STOP
1667       nfds = epoll_wait(efd, events, MAX_FD_MULTIWAITING, -1);
1668       if (-1 != nfds) {
1669         for (n = 0; n < nfds; ++n) {
1670           if (pApp->siMonitorSock == events[n].data.fd) {
1671             // abnormal monitor connect request
1672             int socket;
1673
1674             if ((socket = mcAcceptMonitor(pApp->cAppName, pApp->siMonitorSock)) >= 0) {
1675               struct epoll_event event;
1676
1677               event.events = EPOLLRDHUP;
1678               event.data.fd = socket;
1679
1680               if (epoll_ctl(pApp->efd, EPOLL_CTL_ADD, socket, &event) < 0) {
1681                 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl:%s", strerror(errno));
1682               }
1683             }
1684           }
1685         }
1686
1687         for (n = 0; n < nfds; ++n) {
1688           // The branch processing by FD on which the message was received
1689           if (q_fd == events[n].data.fd) {
1690             // process the message to the Dispatcher's message queue
1691             eStatus = ProcessMq(hApp);
1692           } else if (pApp->defer_evfd == events[n].data.fd) {
1693             // process the Defer event to the Dispatcher
1694             uint64_t data;
1695             ssize_t len;
1696
1697             len = read(pApp->defer_evfd, &data, sizeof(uint64_t));
1698             if (len < 0) {
1699               // read error
1700               eStatus = eFrameworkunifiedStatusFail;
1701               FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : read Failed, status=%d, errno=%d, defer_evfd=%d",
1702                      eStatus, errno, pApp->defer_evfd);
1703             } else {
1704               while (TRUE == (FrameworkunifiedPopedDeferedMessage(hApp))) {
1705                 try {
1706                   // dispatch process: calls respective call back functions
1707                   if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDispatchProcess(hApp))) {
1708                     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : FrameworkunifiedDispatchProcess Status : 0x%x",  eStatus);
1709                   }
1710                 } catch (const frameworkunified::framework::error::CSystemError &err) {
1711                   FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %s", err.what());
1712                   eStatus = eFrameworkunifiedStatusErrOther;
1713                   FrameworkunifiedSendSystemErrMessage(hApp, err.m_eSystemError);
1714                 } catch (const std::exception &err) {
1715                   FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Recovering from : %s", err.what());
1716                   eStatus = eFrameworkunifiedStatusErrOther;
1717                 }
1718               }
1719             }
1720           } else if (pApp->siMonitorSock == events[n].data.fd) {
1721             // do nothing
1722           } else if (frameworkunifiedSearchResourseKey(FRAMEWORKUNIFIED_RES_ABNMLMON, events[n].data.fd, &key) == 0) {
1723             if (pApp->acceptMonitorTable.find(events[n].data.fd) != pApp->acceptMonitorTable.end()) {
1724               // server: accept socket close
1725               eStatus = frameworkunifiedProcessClientAbnormalState(hApp, events[n].data.fd);
1726             } else if (pApp->connectMonitorTable.find(events[n].data.fd) != pApp->connectMonitorTable.end()) {
1727               // client: connect socket close
1728               eStatus = frameworkunifiedProcessServerAbnormalState(hApp, events[n].data.fd);
1729             }
1730
1731             if (epoll_ctl(pApp->efd, EPOLL_CTL_DEL, events[n].data.fd, NULL) < 0) {
1732               FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl:%s", strerror(errno));
1733             }
1734             close(events[n].data.fd);
1735             frameworkunifiedUnregistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, key);
1736           } else {
1737             // process the message to the FD set by the user
1738             eStatus = ProcessFd(hApp, events[n].data.fd);
1739           }
1740         }
1741       } else {
1742         if (errno == EINTR) {
1743           // signal interrupt
1744         } else {
1745           eStatus = eFrameworkunifiedStatusFail;
1746           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_wait Failed, status=%d, errno=%d", eStatus, errno);
1747         }
1748       }
1749     } else {
1750       // LCOV_EXCL_START 6:Because of hApp is valid, GetDispatcherMqFD return eFrameworkunifiedStatusOK.
1751       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1752       eStatus = eFrameworkunifiedStatusFail;
1753       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : GetDispatcherMqFD Failed, status=%d", eStatus);
1754       // LCOV_EXCL_STOP
1755     }
1756   } else {
1757     eStatus = eFrameworkunifiedStatusInvldHandle;
1758     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1759   }
1760
1761   return eStatus;
1762 }
1763
1764 static EFrameworkunifiedStatus ProcessMq(HANDLE hApp) {
1765   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1766
1767   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1768 #ifdef DISPATCHER_PROFILER
1769     CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
1770 #endif
1771
1772     // handle defer'ed message
1773     if (FALSE == FrameworkunifiedPopedDeferedMessage(hApp)) {
1774       // dispatch block: waits on receive message
1775       if ((eStatus = FrameworkunifiedDispatchBlock(hApp)) != eFrameworkunifiedStatusOK) {
1776         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : FrameworkunifiedDispatchBlock Status : 0x%x",  eStatus);
1777       }
1778       #ifdef DISPATCHER_PROFILER
1779       else if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) {  // NOLINT (readability/nolint)
1780         pApp->m_pFrameworkunifiedMsgProfiler->MsgReceived();
1781       }
1782       #endif
1783     }
1784     try {
1785       // dispatch process: calls respective call back functions
1786       eStatus = FrameworkunifiedDispatchProcess(hApp);
1787     } catch (const frameworkunified::framework::error::CSystemError &err) {
1788       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %s", err.what());
1789       eStatus = eFrameworkunifiedStatusErrOther;
1790       FrameworkunifiedSendSystemErrMessage(hApp, err.m_eSystemError);
1791     } catch (const std::exception &err) {
1792       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Recovering from : %s", err.what());
1793       eStatus = eFrameworkunifiedStatusErrOther;
1794     }
1795 #ifdef DISPATCHER_PROFILER
1796     if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) {
1797       pApp->m_pFrameworkunifiedMsgProfiler->MsgProcessed();
1798     }
1799 #endif
1800   } else {
1801     eStatus = eFrameworkunifiedStatusInvldHandle;
1802     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1803   }
1804
1805   return eStatus;
1806 }
1807
1808 static EFrameworkunifiedStatus ProcessFd(HANDLE hApp , int fd) {
1809   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1810
1811   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1812     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1813     FdTable::iterator f_iterator;
1814     struct rusage start, end;
1815     BOOL enable_rusage = NsLogIsZoneSet(ZONE_NS_DEBUG_DUMP);
1816
1817     if (enable_rusage == TRUE) {
1818       frameworkunifiedGetRusage(&start);
1819     }
1820
1821     // finding callback func associated with FD
1822     f_iterator = pApp->fds.find(fd);
1823     if (f_iterator != pApp->fds.end()) {
1824       try {
1825         // calling func associated with FD
1826         eStatus = (*f_iterator).second(hApp);
1827       } catch (const frameworkunified::framework::error::CSystemError &err) {
1828         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR :: %s", err.what());
1829         eStatus = eFrameworkunifiedStatusErrOther;
1830         FrameworkunifiedSendSystemErrMessage(hApp, err.m_eSystemError);
1831       } catch (const std::exception &err) {
1832         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Recovering from: %s", err.what());
1833         eStatus = eFrameworkunifiedStatusErrOther;
1834       }
1835     } else {
1836       eStatus = eFrameworkunifiedStatusFail;
1837       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Callback associated with FD is not found. ", eStatus);
1838     }
1839
1840     if (enable_rusage == TRUE) {
1841       frameworkunifiedGetRusage(&end);
1842       frameworkunifiedPrintRusage(hApp, start, end);
1843     }
1844   } else {
1845     eStatus = eFrameworkunifiedStatusInvldHandle;
1846     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1847   }
1848
1849   return eStatus;
1850 }
1851
1852 /////////////////////////////////////////////////////////
1853 /// EFrameworkunifiedStatus SetConfigHandle(HANDLE hApp, PCHAR sFilePath )
1854 /// This Function opens the file from give path and sets the file handle in hApp
1855 ////////////////////////////////////////////////////////
1856 EFrameworkunifiedStatus SetConfigHandle(HANDLE hApp, PCHAR sFilePath) {
1857   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1858
1859   FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __PRETTY_FUNCTION__, "This function is not implemented");
1860
1861   return eStatus;
1862 }
1863
1864 /////////////////////////////////////////////////////////
1865 /// EFrameworkunifiedStatus SetDefaultFilePath(HANDLE hApp)
1866 /// This Function fetches the file path from Environment variable from PASA_APPCONFIG_FILES and
1867 /// pass it to the SetConfigHandle function
1868 ////////////////////////////////////////////////////////
1869 EFrameworkunifiedStatus SetDefaultConfigFile(HANDLE hApp) {
1870   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1871
1872   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1873     // Get the file path from environment variable
1874     PCHAR l_strEnvValue = getenv(PASA_APPCONFIG_FILES);
1875
1876     if (l_strEnvValue) {
1877       // FRAMEWORKUNIFIEDLOG( ZONE_NS_DIS, __FUNCTION__, "Configuration file path %s ",l_strEnvValue);
1878
1879       std::string l_strFilePath(l_strEnvValue);
1880
1881       if (!l_strFilePath.empty()) {
1882         FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "Configuration file path %s ", l_strFilePath.c_str());
1883         // Open the configuration file
1884         SetConfigHandle(hApp, (PCHAR)l_strFilePath.c_str());
1885       } else {
1886         eStatus = eFrameworkunifiedStatusFail;
1887       }
1888     }
1889   } else {
1890     eStatus = eFrameworkunifiedStatusInvldParam;
1891   }
1892
1893   return eStatus;
1894 }
1895
1896 EFrameworkunifiedStatus FrameworkunifiedProcessServiceProtocolHSMEvent(HANDLE hApp, PCSTR pRequester) {
1897   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1898
1899   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1900     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1901     EventServices::iterator s_iterator;
1902
1903     // finding the service
1904     s_iterator = pApp->eventservices.find(pRequester);
1905     if (s_iterator != pApp->eventservices.end()) {
1906       EventSessionTable::iterator session_iterator;
1907       session_iterator = (s_iterator->second).find(pApp->uiSessionId);
1908       if (session_iterator != (s_iterator->second).end()) {
1909         ServiceEventProtocolTable::iterator spt_iterator;
1910
1911         //      FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, " %s found :: (%s)  ", pApp->cMsgSrcName, s_iterator->second);
1912
1913         // find the protocol req/ack handler
1914         spt_iterator = (session_iterator->second).find(pApp->uiProtocolCmd);
1915         if (spt_iterator != (session_iterator->second).end()) {
1916           // FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, " Protocol Command :: (0x%x)  ", pApp->uiProtocolCmd);
1917           // Post corresponding event
1918           CFrameworkunifiedHSMFramework *l_pStateMachine = FrameworkunifiedGetStateMachine(hApp);
1919           if (l_pStateMachine) {
1920             eStatus = l_pStateMachine->FrameworkunifiedPostEvent(spt_iterator->second);
1921           }
1922         } else if ((session_iterator->second).end() != (spt_iterator =
1923           (session_iterator->second).find(FRAMEWORKUNIFIED_ANY_COMMAND))) {
1924           FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "Processing HSM event for (FRAMEWORKUNIFIED_ANY_COMMAND). "
1925             "Dispatcher protocol command: (0x%x)",
1926                  pApp->uiProtocolCmd);
1927           // Post corresponding event
1928           CFrameworkunifiedHSMFramework *l_pStateMachine = FrameworkunifiedGetStateMachine(hApp);
1929           if (l_pStateMachine) {
1930             eStatus = l_pStateMachine->FrameworkunifiedPostEvent(spt_iterator->second);
1931           }
1932         } else {
1933           eStatus = eFrameworkunifiedStatusMsgNotProcessed;
1934         }
1935
1936         // if FrameworkunifiedPostEvent returns NULL
1937         if (eFrameworkunifiedStatusNullPointer == eStatus) {
1938           eStatus = eFrameworkunifiedStatusExit;
1939         }
1940       } else {
1941         eStatus = eFrameworkunifiedStatusMsgNotProcessed;
1942       }
1943
1944     } else {
1945       eStatus = eFrameworkunifiedStatusMsgNotProcessed;
1946     }
1947   } else {
1948     eStatus = eFrameworkunifiedStatusInvldHandle;
1949   }
1950
1951   return eStatus;
1952 }
1953
1954 //////////////////////////////////////////
1955 // Function : ProcessProtocolEvents
1956 //////////////////////////////////////////
1957 EFrameworkunifiedStatus ProcessProtocolEvents(HANDLE hApp) {
1958   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1959
1960   if (frameworkunifiedCheckValidAppHandle(hApp)) {
1961     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1962
1963     // common event table iterator
1964     EventTable::iterator e_iterator;
1965     ServiceSessionIdListTable::iterator ssidl_iterator;
1966
1967     // public state event table iterator
1968     PublicStateEventTable::iterator se_iterator;
1969
1970     // private state event table iterator
1971     PrivateStateEventTable::iterator pse_iterator;
1972     ServiceSessionEventData::iterator ssed_iterator;
1973     SessionEventData::iterator sed_iterator;
1974
1975     HANDLE l_pClientHandle = NULL;
1976
1977     // get the name of the service from which message is received
1978     PCSTR l_cServiceName = pApp->cMsgSrcName;
1979     const UI_32 l_uiSessionId = pApp->uiSessionId;
1980
1981     // number of events to register
1982     UI_32 l_uiListSize = static_cast<UI_32>(FrameworkunifiedGetMsgLength(hApp) / sizeof(UI_32));
1983
1984     // check if the received service name and data is valid
1985     if (NULL != l_cServiceName && 0 < l_uiListSize) {
1986       PUI_32 l_pData = new UI_32[l_uiListSize];
1987       UI_32 l_uiEventId = 0;
1988
1989       // get the list of events
1990       eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, l_pData, static_cast<UI_32>(l_uiListSize * sizeof(UI_32)));
1991
1992       if (NULL != l_pData && eFrameworkunifiedStatusOK == eStatus) {
1993         for (UI_32 uiCount = 0; uiCount < l_uiListSize; uiCount++) {
1994           // received event id
1995           l_uiEventId = l_pData[uiCount];
1996
1997           // find the event in map of EventTable
1998           e_iterator = pApp->eventtable.find(l_uiEventId);
1999
2000           // if event is registered by any of the clients
2001           if (pApp->eventtable.end() != e_iterator) {
2002             // table of services with list of their session id's registering this event
2003             ServiceSessionIdListTable &l_mServiceSessionIdListTable = e_iterator->second;
2004
2005             // find the service name in map of ServiceSessionIdListTable
2006             ssidl_iterator = l_mServiceSessionIdListTable.find(l_cServiceName);
2007
2008             // if service have already registered this event with any of its session
2009             if (l_mServiceSessionIdListTable.end() != ssidl_iterator) {
2010               // list of sessions id's registering with this event
2011               SessionIdList &l_vSessionIdList = ssidl_iterator->second;
2012
2013               // event is registered with this session
2014               if (IsEventAlreadyRegistered(l_vSessionIdList, l_uiSessionId)) {
2015                 // get the message queue handle from map servicesessionhandletable
2016                 l_pClientHandle = GetMsgQueueHandle(hApp, l_cServiceName, l_uiSessionId);
2017               } else {  // event is not registered with this session
2018                 // create new message queue handle and insert it in map servicesessionhandletable
2019                 l_pClientHandle = GetNewHandleAndUpdateEventInfoTable(hApp,
2020                   l_cServiceName, l_uiSessionId, l_uiEventId);
2021
2022                 // insert the current session id
2023                 l_vSessionIdList.push_back(l_uiSessionId);
2024               }
2025             } else {  // if service have not registered this event with any of its session
2026               // create a list for storing session id's registering this event for this service
2027               SessionIdList l_vSessionIdList;
2028
2029               // insert the current session id
2030               l_vSessionIdList.push_back(l_uiSessionId);
2031
2032               // create new message queue handle and insert it in map servicesessionhandletable
2033               l_pClientHandle = GetNewHandleAndUpdateEventInfoTable(hApp, l_cServiceName, l_uiSessionId, l_uiEventId);
2034
2035               // make a entry for service name and session id's list in map of ServiceSessionIdListTable
2036               l_mServiceSessionIdListTable.insert(make_pair(l_cServiceName, l_vSessionIdList));
2037             }
2038           } else {  // if event is not registered by any of the clients
2039             // create a list for storing session id's registering this event for this service
2040             SessionIdList l_vSessionIdList;
2041
2042             // insert the current session id
2043             l_vSessionIdList.push_back(l_uiSessionId);
2044
2045             // create new message queue handle and insert it in map servicesessionhandletable
2046             l_pClientHandle = GetNewHandleAndUpdateEventInfoTable(hApp, l_cServiceName, l_uiSessionId, l_uiEventId);
2047
2048             // create a table for storing services and its session id list, registering this event
2049             ServiceSessionIdListTable l_mServiceSessionIdListTable;
2050
2051             // make a entry for service name and session id's list in map of ServiceSessionIdListTable
2052             l_mServiceSessionIdListTable.insert(make_pair(l_cServiceName, l_vSessionIdList));
2053
2054             // insert the table with event id in EventTable
2055             pApp->eventtable.insert(make_pair(l_uiEventId, l_mServiceSessionIdListTable));
2056           }
2057
2058           // check whether event is defined as public state event
2059           se_iterator = pApp->publicstateeventtable.find(l_uiEventId);
2060
2061           // if event is a state event, publish the event
2062           if (pApp->publicstateeventtable.end() != se_iterator) {
2063             FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "%d is a public state event", l_uiEventId);
2064
2065             if (NULL != (se_iterator->second)) {
2066               // send the event data for state event
2067               eStatus = FrameworkunifiedSendMsg(l_pClientHandle,
2068                                    l_uiEventId,
2069                                    (se_iterator->second)->uiLength,
2070                                    (se_iterator->second)->pData);
2071               FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Sending event:%d to service:%s, session:%d, status:%d",
2072                      l_uiEventId,
2073                      l_cServiceName,
2074                      l_uiSessionId,
2075                      eStatus);
2076             } else {
2077               FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Event %d yet not published ", l_uiEventId);
2078             }
2079           } else {  // check whether the event is defined as private state event
2080             pse_iterator = pApp->privatestateeventtable.find(l_uiEventId);
2081
2082             // if event found, publish the event
2083             if (pApp->privatestateeventtable.end() != pse_iterator) {
2084               FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "%d is a private state event", l_uiEventId);
2085
2086               ssed_iterator = (pse_iterator->second).find(l_cServiceName);
2087
2088               // check whether the event had been published for this service before subscription
2089               if ((pse_iterator->second).end() != ssed_iterator) {
2090                 sed_iterator = (ssed_iterator->second).find(l_uiSessionId);
2091
2092                 // check whether the event had been published for this service for the same session
2093                 if ((ssed_iterator->second).end() != sed_iterator) {
2094                   if (NULL != (sed_iterator->second)) {
2095                     FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "%d had been published for the "
2096                       "service:%s on sessionId:%d ", l_uiEventId,
2097                            l_cServiceName, l_uiSessionId);
2098
2099                     // send the event data for state event
2100                     eStatus = FrameworkunifiedSendMsg(l_pClientHandle,
2101                                          l_uiEventId,
2102                                          (sed_iterator->second)->uiLength,
2103                                          (sed_iterator->second)->pData);
2104                     FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Sending event:%d to service:%s, session:%d, status:%d",
2105                            l_uiEventId,
2106                            l_cServiceName,
2107                            l_uiSessionId,
2108                            eStatus);
2109                   } else {
2110                     FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Event %d yet not published ", l_uiEventId);
2111                   }
2112                 }
2113               }
2114             }
2115           }
2116         }  // end of for loop of events list
2117       } else {
2118         eStatus = eFrameworkunifiedStatusInvldBuf;
2119         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_REGISTER_EVENTS Invalid data ", eStatus);
2120       }
2121
2122       if (NULL != l_pData) {
2123         delete[] l_pData;
2124         l_pData = NULL;
2125       }
2126     } else {
2127       eStatus = eFrameworkunifiedStatusInvldParam;
2128       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_REGISTER_EVENTS Service Name invalid ", eStatus);
2129     }
2130   } else {
2131     eStatus = eFrameworkunifiedStatusInvldHandle;
2132     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, Invalid hApp ", eStatus);
2133   }
2134
2135   return eStatus;
2136 }
2137
2138 //////////////////////////////////////////
2139 // Function : IsEventAlreadyRegistered
2140 //////////////////////////////////////////
2141 BOOL IsEventAlreadyRegistered(const SessionIdList &vSessionIdList, const UI_32 uiSessionId) {
2142   for (UI_32 uiCount = 0; uiCount < vSessionIdList.size(); uiCount++) {
2143     if (uiSessionId == vSessionIdList[uiCount]) {
2144       return TRUE;
2145     }
2146   }
2147
2148   return FALSE;
2149 }
2150
2151 //////////////////////////////////////////
2152 // Function : GetMsgQueueHandle
2153 //////////////////////////////////////////
2154 HANDLE GetMsgQueueHandle(HANDLE hApp, PCSTR serviceName, const UI_32 uiSessionId) {
2155   HANDLE l_pClientHandle = NULL;
2156
2157   if (frameworkunifiedCheckValidAppHandle(hApp)) {
2158     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2159
2160     ServiceSessionHandleTable::iterator ssh_iterator;;
2161     SessionToEventInfo::iterator sh_iterator;
2162
2163     // find the service name
2164     ssh_iterator = pApp->servicesessionhandletable.find(serviceName);
2165
2166     if (pApp->servicesessionhandletable.end() != ssh_iterator) {
2167       // search for session id
2168       sh_iterator = ssh_iterator->second.find(uiSessionId);
2169
2170       if (ssh_iterator->second.end() != sh_iterator) {
2171         SessionEventInfo *l_ptSessionEventInfo = sh_iterator->second;
2172         if (NULL != l_ptSessionEventInfo) {
2173           l_pClientHandle = l_ptSessionEventInfo->m_hSession;
2174           FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Handle found for service: %s, session: %d, handle:%p.",
2175                  serviceName, uiSessionId, l_pClientHandle);
2176         }
2177       }
2178     }
2179   }
2180
2181   if (NULL == l_pClientHandle) {
2182     FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Handle NULL for service: %s, session: %d.",
2183            serviceName != 0 ? serviceName : NULL, uiSessionId);
2184   }
2185
2186   return l_pClientHandle;
2187 }
2188
2189 //////////////////////////////////////////
2190 // Function : GetNewHandleAndUpdateEventInfoTable
2191 //////////////////////////////////////////
2192 HANDLE GetNewHandleAndUpdateEventInfoTable(HANDLE hApp, PCSTR serviceName, const UI_32 uiSessionId,
2193                                            const UI_32 uiEventId) {
2194   HANDLE l_pClientHandle = NULL;
2195
2196   if (frameworkunifiedCheckValidAppHandle(hApp)) {
2197     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2198
2199     ServiceSessionHandleTable::iterator ssh_iterator;;
2200     SessionToEventInfo::iterator sh_iterator;
2201
2202     ssh_iterator = pApp->servicesessionhandletable.find(serviceName);
2203
2204     if (pApp->servicesessionhandletable.end() == ssh_iterator) {
2205       SessionToEventInfo l_mSessionToEventInfoTable;
2206       pApp->servicesessionhandletable.insert(make_pair(serviceName, l_mSessionToEventInfoTable));
2207     }
2208
2209     sh_iterator = pApp->servicesessionhandletable[serviceName].find(uiSessionId);
2210
2211     if (pApp->servicesessionhandletable[serviceName].end() == sh_iterator) {
2212       l_pClientHandle = FrameworkunifiedMcOpenSender(hApp, serviceName);
2213       if (NULL != l_pClientHandle) {
2214         (reinterpret_cast<MsgQInfo *>(l_pClientHandle))->sessionId = uiSessionId;
2215       }
2216       SessionEventInfo *l_ptSessionEventInfo = new(std::nothrow) SessionEventInfo;
2217       if (NULL != l_ptSessionEventInfo) {  // LCOV_EXCL_BR_LINE 5: new's error case.
2218         l_ptSessionEventInfo->m_hSession = l_pClientHandle;
2219         l_ptSessionEventInfo->m_vEvents.push_back(uiEventId);
2220         pApp->servicesessionhandletable[serviceName].insert(std::make_pair(uiSessionId, l_ptSessionEventInfo));
2221       } else {
2222         // LCOV_EXCL_START 5: malloc's error case.
2223         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2224         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error(NULL) createing new session event info for "
2225                "service:%s, session:%d, event:%d.",
2226                serviceName, uiSessionId, uiEventId);
2227         // LCOV_EXCL_STOP
2228       }
2229     } else {
2230       SessionEventInfo *l_ptSessionEventInfo = sh_iterator->second;
2231       if (NULL != l_ptSessionEventInfo) {
2232         if (NULL == l_ptSessionEventInfo->m_hSession) {
2233           l_ptSessionEventInfo->m_hSession = FrameworkunifiedMcOpenSender(hApp, serviceName);
2234           if (NULL != l_ptSessionEventInfo->m_hSession) {
2235             (reinterpret_cast<MsgQInfo *>(l_ptSessionEventInfo->m_hSession))->sessionId = uiSessionId;
2236           }
2237         }
2238         l_pClientHandle = l_ptSessionEventInfo->m_hSession;
2239         l_ptSessionEventInfo->m_vEvents.push_back(uiEventId);
2240       }
2241     }
2242   }
2243   return l_pClientHandle;
2244 }
2245
2246 //////////////////////////////////////////
2247 // Function : UnregisterPrivateEvents
2248 //////////////////////////////////////////
2249 EFrameworkunifiedStatus UnregisterPrivateEvents(HANDLE hApp) {
2250   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
2251
2252   if (frameworkunifiedCheckValidAppHandle(hApp)) {
2253     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2254
2255     // get the name of the service from which message is received
2256     PCSTR l_cServiceName = pApp->cMsgSrcName;
2257     const UI_32 l_uiSessionId = pApp->uiSessionId;
2258
2259     // number of events to unregister
2260     UI_32 l_uiListSize = static_cast<UI_32>(FrameworkunifiedGetMsgLength(hApp) / sizeof(UI_32));
2261
2262     // check if the received service name and data is valid
2263     if (NULL != l_cServiceName && 0 < l_uiListSize) {
2264       PUI_32 l_pData = new(std::nothrow) UI_32[l_uiListSize];
2265       UI_32 l_uiEventId = 0;
2266
2267       if (NULL != l_pData) {  // LCOV_EXCL_BR_LINE 5: new's error case.
2268         // get the list of events
2269         eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, l_pData, static_cast<UI_32>(l_uiListSize * sizeof(UI_32)));
2270
2271         if (eFrameworkunifiedStatusOK == eStatus) {
2272           for (UI_32 uiCount = 0; uiCount < l_uiListSize; uiCount++) {
2273             // received event id
2274             l_uiEventId = l_pData[uiCount];
2275
2276             // remove the entry from map of Events and Session id's of services
2277             eStatus = RemoveEventEntryFromEventTable(hApp, l_uiEventId, l_cServiceName, l_uiSessionId);
2278             (VOID)RemoveEntryFromSessionEventInfoTable(hApp, l_cServiceName, l_uiSessionId, l_uiEventId);
2279           }
2280         } else {
2281           eStatus = eFrameworkunifiedStatusInvldBuf;
2282           FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_REGISTER_EVENTS Invalid data ", eStatus);
2283         }
2284         delete[] l_pData;
2285         l_pData = NULL;
2286       } else {
2287         // LCOV_EXCL_START 5: new's error case.
2288         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
2289         eStatus = eFrameworkunifiedStatusNullPointer;
2290         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: PROTOCOL_UNREGISTER_EVENTS: "
2291           "Unable to allocate memory for l_pData");
2292         // LCOV_EXCL_STOP
2293       }
2294     } else {
2295       eStatus = eFrameworkunifiedStatusInvldParam;
2296       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_UNREGISTER_EVENTS Service Name invalid ", eStatus);
2297     }
2298   } else {
2299     eStatus = eFrameworkunifiedStatusInvldParam;
2300     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, Invalid hApp ", eStatus);
2301   }
2302
2303   return eStatus;
2304 }
2305
2306 //////////////////////////////////////////
2307 // Function : HandleSessionEventOnCloseSession
2308 //////////////////////////////////////////
2309 EFrameworkunifiedStatus HandleSessionEventOnCloseSession(HANDLE hApp) {
2310   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
2311
2312   if (frameworkunifiedCheckValidAppHandle(hApp)) {
2313     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2314     // close session request structure
2315     CloseSessionReq tClose = {};
2316     if (FrameworkunifiedGetMsgLength(hApp) == sizeof(tClose)) {
2317       if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, &tClose, sizeof(tClose)))) {
2318         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, FrameworkunifiedGetMsgDataOfSize Failed ", eStatus);
2319       } else {
2320         CleanAllEventsOfSession(hApp, pApp->cMsgSrcName, tClose.sessionId);
2321       }
2322     } else {
2323       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid data length received:: Expected: %ld, Received: %d",
2324              static_cast<long int>(sizeof(tClose)), FrameworkunifiedGetMsgLength(hApp));  // NOLINT  (readability/nolint)
2325       eStatus = eFrameworkunifiedStatusFail;
2326     }
2327   } else {
2328     eStatus = eFrameworkunifiedStatusInvldParam;
2329     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid application handle status.");
2330   }
2331
2332   return eStatus;
2333 }
2334
2335 //////////////////////////////////////////
2336 // Function : FrameworkunifiedOnDummy
2337 //////////////////////////////////////////
2338 EFrameworkunifiedStatus FrameworkunifiedOnDummy(HANDLE hApp) {
2339   return eFrameworkunifiedStatusOK;
2340 }
2341
2342 #ifdef DISPATCHER_PROFILER
2343 EFrameworkunifiedStatus FrameworkunifiedAttachMsgProfilerCallbacksDispatcher(HANDLE hApp) {
2344   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
2345
2346   if (frameworkunifiedCheckValidAppHandle(hApp)) {
2347     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2348     if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) {
2349       eStatus = FrameworkunifiedAttachCallbackToDispatcher(hApp,
2350                                               "NSProfileUtil",
2351                                               PROTOCOL_DIS_MSGPROFILER,
2352                                               boost::bind(&FrameworkunifiedMsgProfiler::PrintProfileInfo,
2353                                               pApp->m_pFrameworkunifiedMsgProfiler, hApp));
2354       if (eFrameworkunifiedStatusOK == eStatus) {
2355         FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, "", "FrameworkunifiedAttachCallbackToDispatcher success for PROTOCOL_DIS_MSGPROFILER.");
2356       }
2357     }
2358   }
2359   return eStatus;
2360 }
2361
2362 EFrameworkunifiedStatus FrameworkunifiedAttachChildMsgProfilerCallbacksDispatcher(HANDLE hChildApp) {
2363   EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
2364   FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, "", "+");
2365   if (frameworkunifiedCheckValidAppHandle(hChildApp)) {
2366     CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hChildApp);
2367     if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) {
2368       eStatus = FrameworkunifiedAttachCallbackToDispatcher(hChildApp,
2369                                               pApp->cParentAppName,
2370                                               PROTOCOL_DIS_MSGPROFILER,
2371                                               boost::bind(&FrameworkunifiedMsgProfiler::PrintProfileInfo,
2372                                               pApp->m_pFrameworkunifiedMsgProfiler, hChildApp));
2373       if (eFrameworkunifiedStatusOK == eStatus) {
2374         FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, "", "FrameworkunifiedAttachCallbackToDispatcher success for PROTOCOL_DIS_MSGPROFILER.");
2375       }
2376     }
2377   }
2378   FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, "", "-");
2379   return eStatus;
2380 }
2381 #endif
2382 // EOF