2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NSFramework
23 ///////////////////////////////////////////////////////////////////////////////
27 #include <sys/epoll.h>
28 #include <sys/eventfd.h>
30 #include <sys/resource.h>
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>
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"
60 #ifdef DISPATCHER_PROFILER
61 #include "frameworkunified_msgprofiler.h"
62 #include <boost/bind.hpp>
68 static FrameworkunifiedDefaultCallbackHandler g_defaultCallbackHandler;
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);
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) {
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 << ", ";
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;
103 * Setting NULL for arguments causes an Exception
105 if (strlen(cAppName) < MAX_NAME_SIZE_APP) {
106 memcpy(pApp->cAppName, cAppName, strlen(cAppName));
108 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error : Aborting ... Exceeds Max App name size MAX_NAME_SIZE_APP : %d ",
111 * Error is set here, but the error is overwritten by the following process
113 eStatus = eFrameworkunifiedStatusFail;
116 FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, " Application Created ");
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;
124 if (strcmp(cAppName, "NS_NPPService") != 0) {
125 McFlushReceiver(pApp->hAppRcvMsgQ);
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;
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 ");
143 eStatus = eFrameworkunifiedStatusNullPointer;
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 >");
153 eStatus = eFrameworkunifiedStatusNullPointer;
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;
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);
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);
179 pApp->m_pFrameworkunifiedStateMachine = NULL;
180 pApp->uiProtocolCmd = 0;
182 // Clear the Defer'ed message pop flag.
183 pApp->fPopDeferedMsg = FALSE;
185 // Clear the Defer'ed sync response flag.
186 pApp->fDeferredSyncResponse = FALSE;
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);
193 pApp->defer_evfd = -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);
202 #ifdef DISPATCHER_PROFILER
203 // Assign default value
204 pApp->m_pFrameworkunifiedMsgProfiler = NULL;
207 if (eFrameworkunifiedStatusOK != eStatus) {
208 if (NULL != pApp->hAppRcvMsgQ) {
209 McClose(pApp->hAppRcvMsgQ);
210 pApp->hAppRcvMsgQ = NULL;
213 if (0 < pApp->siMonitorSock) {
214 mcCloseMonitor(pApp->cAppName);
215 pApp->siMonitorSock = -1;
218 if (NULL != pApp->hAppSndMsgQ) {
219 McClose(pApp->hAppSndMsgQ);
220 pApp->hAppSndMsgQ = NULL;
223 if (NULL != pApp->hNPSndMsgQ) {
224 FrameworkunifiedMcClose(pApp->hNPSndMsgQ);
225 pApp->hNPSndMsgQ = NULL;
228 // LCOV_EXCL_BR_START 6:According to line 158, pApp->hParentSndMsgQ is NULL.
229 if (NULL != pApp->hParentSndMsgQ) {
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;
238 if (0 < pApp->defer_evfd) {
239 close(pApp->defer_evfd);
240 pApp->defer_evfd = -1;
248 delete pApp; // LCOV_EXCL_BR_LINE 11:except branch
259 //////////////////////////////////////////
260 // Function : FrameworkunifiedCreateDispatcher
261 //////////////////////////////////////////
262 EFrameworkunifiedStatus FrameworkunifiedCreateDispatcher(PCSTR cAppName, HANDLE &hApp, BOOL bIsChildThread) { // NOLINT (readability/nolint)
263 return CreateDispatcher(cAppName, hApp, bIsChildThread);
267 //////////////////////////////////////////
268 // Function : FrameworkunifiedDispatchBlock
269 //////////////////////////////////////////
270 EFrameworkunifiedStatus FrameworkunifiedDispatchBlock(HANDLE hApp) {
271 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
272 // FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "+");
274 if (frameworkunifiedCheckValidAppHandle(hApp)) {
275 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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);
284 // FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "Message received on dispatcher:: Source: %s, Cmd: 0x%X, SessionId: %d ",
285 // pApp->cMsgSrcName, pApp->uiProtocolCmd, pApp->uiSessionId);
288 eStatus = eFrameworkunifiedStatusInvldHandle;
291 // FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "-");
296 EFrameworkunifiedStatus FrameworkunifiedProcessServiceProtocolCallback(HANDLE hApp, PCSTR pRequester) {
297 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
299 if (frameworkunifiedCheckValidAppHandle(hApp)) {
300 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
301 Services::iterator s_iterator;
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);
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);
323 eStatus = (*spt_iterator).second(hApp);
325 eStatus = eFrameworkunifiedStatusMsgNotProcessed;
328 eStatus = eFrameworkunifiedStatusMsgNotProcessed;
331 eStatus = eFrameworkunifiedStatusMsgNotProcessed;
334 eStatus = eFrameworkunifiedStatusInvldHandle;
340 EFrameworkunifiedStatus FrameworkunifiedProcessServiceProtocol(HANDLE hApp) {
341 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
343 if (frameworkunifiedCheckValidAppHandle(hApp)) {
344 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
346 // Check if the callback is associated with the Protocol for message source
347 eStatus = FrameworkunifiedProcessServiceProtocolCallback(hApp, const_cast<PCSTR>(pApp->cMsgSrcName));
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));
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);
365 eStatus = eFrameworkunifiedStatusInvldHandle;
372 EFrameworkunifiedStatus FrameworkunifiedSetDeferredSyncResponse(HANDLE hApp) {
373 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusInvldBuf;
375 if (frameworkunifiedCheckValidAppHandle(hApp)) {
376 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
377 pApp->fDeferredSyncResponse = TRUE;
378 eStatus = eFrameworkunifiedStatusOK;
385 static void frameworkunifiedProcessSyncResponse(HANDLE hApp, EFrameworkunifiedStatus retValue) {
386 EFrameworkunifiedStatus eStatus;
387 CHAR systemInfo[MAX_SYS_INFO_SIZE];
389 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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();
401 FrameworkunifiedGetSystemInfo(hApp, systemInfo);
402 seq_id = (UI_32)strtoul(systemInfo, NULL, 16);
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);
409 pApp->responsedata.clear();
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();
420 pApp->fDeferredSyncResponse = FALSE;
424 inline static time_t frameworkunifiedTimeval2usec(const struct timeval &time) {
425 return static_cast<time_t>(time.tv_sec * 1000000 + time.tv_usec);
429 static void frameworkunifiedPrintRusage(HANDLE hApp, const struct rusage &start, const struct rusage &end) {
432 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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));
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);
441 if (occupy_time > pApp->max_occupy_time) {
442 pApp->max_occupy_time = occupy_time;
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);
452 static inline void frameworkunifiedGetRusage(struct rusage *r) {
454 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "r is NULL");
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);
465 static int frameworkunifiedPushLostSessionInfo(HANDLE hApp, PCSTR serviceName) {
467 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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();
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);
487 /// Abnormal monitor(server): Client socket close handle
489 EFrameworkunifiedStatus frameworkunifiedProcessClientAbnormalState(HANDLE hApp, int sockfd) {
490 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
491 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
492 AcceptMonitorTable::iterator it;
494 if ((it = pApp->acceptMonitorTable.find(sockfd)) == pApp->acceptMonitorTable.end()) {
495 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid fd", sockfd);
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());
504 if (pApp->lostSessionHandler != NULL) {
505 int cntFound = frameworkunifiedPushLostSessionInfo(hApp, clientName);
507 // calling lost session handler
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;
517 pApp->acceptMonitorTable.erase(sockfd);
520 return eStatus; // LCOV_EXCL_BR_LINE 11:except branch
524 /// Abnormal monitor(server): Add
526 EFrameworkunifiedStatus frameworkunifiedAddAcceptMonitor(HANDLE hApp) {
527 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
528 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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);
536 eStatus = eFrameworkunifiedStatusFail;
539 mcSetConnectKey(connectKey, sizeof(connectKey), pApp->cAppName, pApp->cMsgSrcName);
540 pApp->acceptMonitorTable.insert(std::make_pair(acceptSock, connectKey));
547 /// Abnormal monitor(server): Del
549 EFrameworkunifiedStatus frameworkunifiedDelAcceptMonitor(HANDLE hApp) {
550 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
551 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
552 long acceptSock; // NOLINT (readability/nolint)
555 mcSetConnectKey(connectKey, sizeof(connectKey), pApp->cAppName, pApp->cMsgSrcName);
556 if (frameworkunifiedGetResource(FRAMEWORKUNIFIED_RES_ABNMLMON, connectKey, &acceptSock) < 0) {
557 eStatus = eFrameworkunifiedStatusFail;
559 if (pApp->acceptMonitorTable.find(static_cast<Socket>(acceptSock)) != pApp->acceptMonitorTable.end()) {
560 pApp->acceptMonitorTable.erase(static_cast<Socket>(acceptSock));
568 /// Abnormal monitor(client): Server socket close handle
570 EFrameworkunifiedStatus frameworkunifiedProcessServerAbnormalState(HANDLE hApp, int sockfd) {
571 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
572 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
573 ConnectMonitorTable::iterator it;
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);
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());
587 if (pApp->lostSessionHandler != NULL) {
588 int cntFound = frameworkunifiedPushLostSessionInfo(hApp, serverName); // LCOV_EXCL_BR_LINE 11:except branch
590 // calling lost session handler
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());
597 eStatus = eFrameworkunifiedStatusErrOther;
602 pApp->connectMonitorTable.erase(sockfd);
605 return eStatus; // LCOV_EXCL_BR_LINE 11:except branch
609 /// Abnormal monitor(client): Add
611 EFrameworkunifiedStatus frameworkunifiedAddConnectMonitor(HANDLE hApp, PCSTR serverName, PCSTR clientName) {
612 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
613 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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;
621 if (pApp->connectMonitorTable.find(connectSock) == pApp->connectMonitorTable.end()) {
623 mcSetConnectKey(connectKey, sizeof(connectKey), serverName, clientName);
625 struct epoll_event event;
626 event.events = EPOLLRDHUP;
627 event.data.fd = connectSock;
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;
634 if (eStatus == eFrameworkunifiedStatusOK) {
635 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
636 pApp->connectMonitorTable.insert(std::make_pair(connectSock, connectKey));
645 /// Abnormal monitor(client): Del
647 EFrameworkunifiedStatus frameworkunifiedDelConnectMonitor(HANDLE hApp, PCSTR serverName, PCSTR clientName) {
648 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
649 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
652 mcSetConnectKey(connectKey, sizeof(connectKey), serverName, clientName);
654 long connectSock; // NOLINT (readability/nolint)
655 if (frameworkunifiedGetResource(FRAMEWORKUNIFIED_RES_ABNMLMON, connectKey, &connectSock) < 0) {
656 eStatus = eFrameworkunifiedStatusFail;
658 if (pApp->connectMonitorTable.find(static_cast<Socket>(connectSock)) != pApp->connectMonitorTable.end()) {
659 pApp->connectMonitorTable.erase(static_cast<Socket>(connectSock));
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;
672 /// Abnormal monitor(common): Attach callback
674 EFrameworkunifiedStatus FrameworkunifiedAttachLostSessionCallbackToDispatcher(HANDLE hApp, CbFuncPtr fpLostSession) {
675 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
677 if (frameworkunifiedCheckValidAppHandle(hApp) == FALSE || fpLostSession == NULL) {
678 eStatus = eFrameworkunifiedStatusInvldParam;
680 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
681 pApp->lostSessionHandler = fpLostSession;
688 /// Abnormal monitor(common): Get lost session data
690 EFrameworkunifiedStatus FrameworkunifiedGetLostSessionData(HANDLE hApp, PSTR pServiceName, PUI_32 puiSessionId) {
691 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
693 if (frameworkunifiedCheckValidAppHandle(hApp) == FALSE || pServiceName == NULL || puiSessionId == NULL) {
694 eStatus = eFrameworkunifiedStatusInvldParam;
696 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
697 if (pApp->lostSessionQueue.empty()) {
698 eStatus = eFrameworkunifiedStatusAccessError;
700 LostSessionInfo info = pApp->lostSessionQueue.front();
701 pApp->lostSessionQueue.pop();
702 strcpy(pServiceName, info.sServiceName.c_str()); // NOLINT (readability/nolint)
703 *puiSessionId = info.iSessionId;
711 //////////////////////////////////////////
712 // Function : FrameworkunifiedDispatchProcess
713 //////////////////////////////////////////
714 EFrameworkunifiedStatus FrameworkunifiedDispatchProcess(HANDLE hApp) {
715 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
716 // FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "+");
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);
723 if (enable_rusage == TRUE) {
724 frameworkunifiedGetRusage(&start);
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);
733 eStatus = ProcessProtocolEvents(hApp);
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);
741 eStatus = UnregisterPrivateEvents(hApp);
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);
750 // Handle the session events on the close session request
751 (VOID)HandleSessionEventOnCloseSession(hApp);
753 frameworkunifiedDelAcceptMonitor(hApp);
755 eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
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);
764 eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
765 if (eStatus == eFrameworkunifiedStatusOK) {
766 frameworkunifiedAddAcceptMonitor(hApp);
772 eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
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);
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);
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
803 eStatus = FrameworkunifiedProcessServiceProtocol(hApp);
808 frameworkunifiedProcessSyncResponse(hApp, eStatus);
810 if ((eStatus != eFrameworkunifiedStatusOK) && (eStatus != eFrameworkunifiedStatusExit) && (eStatus != eFrameworkunifiedStatusMsgNotProcessed)) {
811 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, " Error :: %d, (%s) Event Info :: (%s), (%d), (%s)",
813 pApp->cAppName != 0 ? static_cast<PSTR>(pApp->cAppName) : NULL,
814 pApp->cMsgSrcName != 0 ? static_cast<PSTR>(pApp->cMsgSrcName) : NULL,
816 pApp->cSystemInfo != 0 ? static_cast<PSTR>(pApp->cSystemInfo) : NULL);
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;
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);
833 if (enable_rusage == TRUE) {
834 frameworkunifiedGetRusage(&end);
835 frameworkunifiedPrintRusage(hApp, start, end);
838 eStatus = eFrameworkunifiedStatusInvldHandle;
841 // FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "-");
848 //////////////////////////////////////////
849 // Function : FrameworkunifiedCloseDispatcher
850 //////////////////////////////////////////
851 EFrameworkunifiedStatus FrameworkunifiedCloseDispatcher(HANDLE hApp) {
852 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
854 if (frameworkunifiedCheckValidAppHandle(hApp)) {
855 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
858 if (NULL != pApp->hAppRcvMsgQ) {
859 McClose(pApp->hAppRcvMsgQ);
860 pApp->hAppRcvMsgQ = NULL;
863 if (pApp->siMonitorSock) {
864 mcCloseMonitor(pApp->cAppName);
865 pApp->siMonitorSock = 0;
868 if (NULL != pApp->hAppSndMsgQ) {
869 McClose(pApp->hAppSndMsgQ);
870 pApp->hAppSndMsgQ = NULL;
873 if (NULL != pApp->hNPSndMsgQ) {
874 FrameworkunifiedMcClose(pApp->hNPSndMsgQ);
875 pApp->hNPSndMsgQ = NULL;
878 if (NULL != pApp->hParentSndMsgQ) {
879 McClose(pApp->hParentSndMsgQ);
880 pApp->hParentSndMsgQ = NULL;
883 if (0 < pApp->defer_evfd) {
884 close(pApp->defer_evfd);
885 pApp->defer_evfd = 0;
894 pApp = NULL; // mb20110110 Added per comment 216
895 // NOTE: However, client still needs to set his own pointer to NULL
897 eStatus = eFrameworkunifiedStatusInvldHandle;
903 //////////////////////////////////////////
904 // Function : FrameworkunifiedPopedDeferedMessage
905 //////////////////////////////////////////
906 BOOL FrameworkunifiedPopedDeferedMessage(HANDLE hApp) {
907 BOOL rtnFlag = FALSE;
909 // LCOV_EXCL_BR_START 6:FrameworkunifiedPopedDeferedMessage is internal function, hApp is checked in extenal API
910 if (frameworkunifiedCheckValidAppHandle(hApp)) {
912 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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());
919 // remove this item from the queue using pop
920 pApp->deferedMsgQueue.pop();
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);
927 // clear the pop flag!
928 pApp->fPopDeferedMsg = FALSE;
930 // set the return flag TRUE, we have pop'ed
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);
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;
957 if (cAppName == NULL) {
958 return eFrameworkunifiedStatusNullPointer;
961 NsLogSetProcessName(cAppName);
962 PLOG_TEXT("FrameworkunifiedDispatcher Start");
963 // set main thread name as provided in dispatcher
964 pthread_setname_np(pthread_self(), cAppName);
966 FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "In");
968 if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
973 HANDLE hFrameworkApp = NULL;
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) {
979 THApp hApp(hFrameworkApp);
981 /// Parse the Arguments via the FrameworkunifiedArgumentParser
982 /// passing an handle to the app and argument list
983 eStatus = FrameworkunifiedArgumentParser(hApp, argc, argv, cmdLineOptions);
985 if (eFrameworkunifiedStatusOK == eStatus) {
986 eStatus = FrameworkunifiedDispatcherMain(hApp);
989 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
990 eStatus = eFrameworkunifiedStatusNullPointer;
993 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateDispatcher error, status=%d", eStatus);
995 } catch (const std::exception &e) {
996 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher: %s", e.what());
997 eStatus = eFrameworkunifiedStatusNullPointer;
1004 // required for parsing subargument of -r
1005 char *g_cPlogSubOpts[] = {
1006 #define PLOG_OUTPUT 0
1007 const_cast<PSTR>("output"),
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];
1019 BOOL l_bHasConfigFilepath = FALSE;
1022 SI_32 l_iPriority = 10;
1026 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "In");
1028 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1029 #ifdef DISPATCHER_PROFILER
1030 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
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] = {};
1037 strncpy(l_cNSOptions, "l:m:c:p:qr:s", (MAX_STRING_SIZE_TAG - 1));
1040 if (NULL != cmdLineOptions) {
1041 // append custom options
1042 strncat(l_cNSOptions, cmdLineOptions->cShortOptions, (MAX_STRING_SIZE_TAG - strlen(l_cNSOptions) - 1));
1044 while ((eFrameworkunifiedStatusOK == eStatus) && ((l_iOpt = getopt(argc, argv, l_cNSOptions)) != -1)) {
1046 case 'm': // mask value..
1047 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-m (mask value): %s", optarg);
1050 * If the second of the command-line arguments is-m and the third is not, an exception occurs.
1053 l_cMask = strtok_r(optarg, ",", &saveptr);
1055 while (NULL != l_cMask) {
1056 mask[l_iMaskCount++] = static_cast<TFrameworkunifiedZone>(strtoul(l_cMask, NULL, 16));
1057 l_cMask = strtok_r(NULL, ",", &saveptr);
1059 NsLogSetControlMask(mask);
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));
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;
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");
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)));
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));
1092 NSLogEnablePLog(TRUE);
1093 l_cOptions = optarg;
1094 while (*l_cOptions != '\0') {
1095 switch (getsubopt(&l_cOptions, g_cPlogSubOpts, &l_cValue)) {
1097 if (l_cValue != NULL) {
1098 NSLogSetPlogOutputOptions(static_cast<UI_8>(strtoul(l_cValue, NULL, 16)));
1109 NSLogEnableSysEventLog(TRUE);
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.");
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.");
1129 // if no config option is selected then use the default config path
1130 if ((FALSE == l_bHasConfigFilepath) && (eFrameworkunifiedStatusOK == eStatus)) {
1131 eStatus = SetDefaultConfigFile(hApp);
1134 eStatus = eFrameworkunifiedStatusInvldHandle;
1140 //////////////////////////////////////////
1141 // Function : FrameworkunifiedSimpleDispatcher
1142 //////////////////////////////////////////
1143 EFrameworkunifiedStatus FrameworkunifiedSimpleDispatcher(PCSTR cAppName, const FrameworkunifiedDefaultCallbackHandler *CbHandler, CbFuncPtr CbShutdown,
1144 BOOL isChildThread) {
1145 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1147 // if main thread then set log process name
1148 if (!isChildThread) {
1149 NsLogSetProcessName(cAppName);
1152 if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
1156 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "+");
1158 if (NULL != CbShutdown) {
1160 HANDLE hFrameworkApp = NULL;
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);
1168 // Call application defined start function
1169 eStatus = FrameworkunifiedOnInitializationInternal(hApp);
1171 if (eFrameworkunifiedStatusOK != eStatus) {
1172 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
1175 while (eFrameworkunifiedStatusExit != eStatus) {
1177 // dispatch block: waits on receive message
1178 if (eFrameworkunifiedStatusOK != (eStatus = FrameworkunifiedDispatchBlock(hApp))) {
1179 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: FrameworkunifiedDispatchBlock : 0x%x ", eStatus);
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);
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;
1195 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Exiting dispatcher ");
1197 // Call application defined stop function
1198 eStatus = CbShutdown(hApp);
1200 FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "hFrameworkApp is NULL");
1201 eStatus = eFrameworkunifiedStatusNullPointer;
1204 FRAMEWORKUNIFIEDLOG0(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateDispatcher error, status=%d", eStatus);
1206 } catch (const THApp::Exception &) {
1207 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher ");
1208 eStatus = eFrameworkunifiedStatusNullPointer;
1211 eStatus = eFrameworkunifiedStatusFail;
1214 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, "-");
1218 EFrameworkunifiedStatus RunDispatcher(HANDLE hApp) {
1219 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
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);
1230 FrameworkunifiedGetDispatcherFD(hApp, &efd);
1233 frameworkunifiedFdHandler(hApp, efd);
1236 eStatus = eFrameworkunifiedStatusInvldHandle;
1243 EFrameworkunifiedStatus FrameworkunifiedDispatcherMain(HANDLE hApp) {
1244 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1246 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1248 FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "Success: FrameworkunifiedCreateDispatcher ");
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 -");
1257 if (eFrameworkunifiedStatusOK != eStatus) {
1258 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
1261 eStatus = RunDispatcher(hApp);
1262 CFrameworkunifiedSyncData::FrameworkunifiedReleaseSyncDataInstance();
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;
1273 eStatus = eFrameworkunifiedStatusInvldHandle;
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;
1285 if (cAppName == NULL) {
1286 return eFrameworkunifiedStatusNullPointer;
1289 NsLogSetProcessName(cAppName);
1291 PLOG_TEXT("FrameworkunifiedDispatcher Start");
1292 // set main thread name as provided in dispatcher
1293 pthread_setname_np(pthread_self(), cAppName);
1295 if ((eStatus = RegistDefaultCbHandler(CbHandler)) != eFrameworkunifiedStatusOK) {
1299 FRAMEWORKUNIFIEDLOG0(ZONE_NS_DIS, __FUNCTION__, "In");
1302 HANDLE hFrameworkApp = NULL;
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);
1311 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1312 pApp->pRsrcMgr = reinterpret_cast<void *>(l_hApp);
1314 eStatus = FrameworkunifiedArgumentParser(hApp, argc, argv, cmdLineOptions);
1316 if (eFrameworkunifiedStatusOK == eStatus) {
1317 FRAMEWORKUNIFIEDLOG_PERFORMANCE_DEBUG("FrameworkunifiedOnInitialization +");
1318 eStatus = FrameworkunifiedOnInitializationInternal(hApp);
1319 FRAMEWORKUNIFIEDLOG_PERFORMANCE_DEBUG("FrameworkunifiedOnInitialization -");
1321 if (eFrameworkunifiedStatusOK != eStatus) {
1322 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedOnInitialization failed ");
1327 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "FrameworkunifiedCreateDispatcher error, status=%d", eStatus);
1329 } catch (const THApp::Exception &) {
1330 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: Failed to FrameworkunifiedCreateDispatcher ");
1331 eStatus = eFrameworkunifiedStatusNullPointer;
1337 EFrameworkunifiedStatus FrameworkunifiedDispatchProcessWithoutLoop(HANDLE hApp) {
1338 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1339 int efd; // FD for multi waiting
1341 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1342 FrameworkunifiedGetDispatcherFD(hApp, &efd);
1343 eStatus = frameworkunifiedFdHandler(hApp, efd);
1344 if (eFrameworkunifiedStatusOK != eStatus) {
1345 eStatus = eFrameworkunifiedStatusFail;
1348 eStatus = eFrameworkunifiedStatusInvldHandle;
1354 EFrameworkunifiedStatus FrameworkunifiedDestroyDispatcherWithoutLoop(HANDLE hApp) {
1355 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1357 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1359 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1360 THApp *RsrcMgr = reinterpret_cast<THApp *>(pApp->pRsrcMgr);
1362 } catch (const std::exception &e) {
1363 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1364 eStatus = eFrameworkunifiedStatusNullPointer;
1367 eStatus = eFrameworkunifiedStatusInvldHandle;
1373 static EFrameworkunifiedStatus GetDispatcherMqFD(HANDLE hApp, int *fd) {
1375 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "fd is NULL");
1376 return eFrameworkunifiedStatusNullPointer;
1378 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1380 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1381 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1382 *fd = McGetQueueFD(pApp->hAppRcvMsgQ);
1384 eStatus = eFrameworkunifiedStatusInvldHandle;
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
1397 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1398 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1401 efd = epoll_create1(EPOLL_CLOEXEC);
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;
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);
1417 eStatus = eFrameworkunifiedStatusFail;
1418 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : GetDispatcherMqFD Failed, status=%d", eStatus);
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;
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);
1435 eStatus = eFrameworkunifiedStatusFail;
1436 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : eventfd Failed, status=%d, errno=%d", eStatus, errno);
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));
1449 eStatus = eFrameworkunifiedStatusFail;
1450 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_create1 Failed, status=%d, errno=%d", eStatus, errno);
1453 eStatus = eFrameworkunifiedStatusInvldHandle;
1454 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1460 EFrameworkunifiedStatus FrameworkunifiedGetDispatcherFD(HANDLE hApp, int *efd) {
1461 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1463 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1465 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1468 eStatus = eFrameworkunifiedStatusNullPointer;
1471 eStatus = eFrameworkunifiedStatusInvldHandle;
1472 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1478 EFrameworkunifiedStatus FrameworkunifiedOnInitializationInternal(HANDLE hApp) {
1479 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1481 if (g_defaultCallbackHandler.onInitilization != NULL) {
1483 eStatus = g_defaultCallbackHandler.onInitilization(hApp);
1484 } catch (const std::exception &e) {
1485 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1491 EFrameworkunifiedStatus FrameworkunifiedOnDestroyInternal(HANDLE hApp) {
1492 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1494 if (g_defaultCallbackHandler.onDestroy != NULL) {
1495 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1496 if (pApp->hParentSndMsgQ == NULL) {
1499 eStatus = g_defaultCallbackHandler.onDestroy(hApp);
1500 } catch (const std::exception &e) {
1501 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1504 eStatus = eFrameworkunifiedStatusOK;
1510 EFrameworkunifiedStatus FrameworkunifiedOnStartInternal(HANDLE hApp) {
1511 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1513 if (g_defaultCallbackHandler.onStart != NULL) {
1515 eStatus = g_defaultCallbackHandler.onStart(hApp);
1516 } catch (const std::exception &e) {
1517 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1523 EFrameworkunifiedStatus FrameworkunifiedOnStopInternal(HANDLE hApp) {
1524 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1526 if (g_defaultCallbackHandler.onStop != NULL) {
1528 eStatus = g_defaultCallbackHandler.onStop(hApp);
1529 } catch (const std::exception &e) {
1530 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1536 EFrameworkunifiedStatus FrameworkunifiedOnPreStartInternal(HANDLE hApp) {
1537 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1539 if (g_defaultCallbackHandler.onPreStart != NULL) {
1541 eStatus = g_defaultCallbackHandler.onPreStart(hApp);
1542 } catch (const std::exception &e) {
1543 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1546 return eStatus; // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1549 EFrameworkunifiedStatus FrameworkunifiedOnPreStopInternal(HANDLE hApp) {
1550 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1552 if (g_defaultCallbackHandler.onPreStop != NULL) {
1554 eStatus = g_defaultCallbackHandler.onPreStop(hApp);
1555 } catch (const std::exception &e) {
1556 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1559 return eStatus; // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1562 EFrameworkunifiedStatus FrameworkunifiedOnBackgroundStartInternal(HANDLE hApp) {
1563 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1565 if (g_defaultCallbackHandler.onBackgroundStart != NULL) {
1567 eStatus = g_defaultCallbackHandler.onBackgroundStart(hApp);
1568 } catch (const std::exception &e) {
1569 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1572 return eStatus; // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1575 EFrameworkunifiedStatus FrameworkunifiedOnBackgroundStopInternal(HANDLE hApp) {
1576 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1578 if (g_defaultCallbackHandler.onBackgroundStop != NULL) {
1580 eStatus = g_defaultCallbackHandler.onBackgroundStop(hApp);
1581 } catch (const std::exception &e) {
1582 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1585 return eStatus; // LCOV_EXCL_BR_LINE 11:Excluded due to gcov constraints (others)
1588 EFrameworkunifiedStatus FrameworkunifiedOnDebugDumpInternal(HANDLE hApp) {
1589 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1591 if (g_defaultCallbackHandler.onDebugDump != NULL) {
1593 eStatus = g_defaultCallbackHandler.onDebugDump(hApp);
1594 } catch (const std::exception &e) {
1595 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1601 EFrameworkunifiedStatus FrameworkunifiedCreateStateMachineInternal(HANDLE hApp) {
1602 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1604 if (g_defaultCallbackHandler.createStateMachine != NULL) {
1606 eStatus = g_defaultCallbackHandler.createStateMachine(hApp);
1607 } catch (const std::exception &e) {
1608 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
1614 EFrameworkunifiedStatus FrameworkunifiedSSFrameworkInterfaceInternal(HANDLE hApp) {
1615 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusFail;
1617 if (g_defaultCallbackHandler.ssFrameworkInterface != NULL) {
1619 eStatus = g_defaultCallbackHandler.ssFrameworkInterface(hApp);
1620 } catch (const std::exception &e) {
1621 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error: %s", e.what());
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;
1640 g_defaultCallbackHandler = *CbHandler;
1641 return eFrameworkunifiedStatusOK;
1644 EFrameworkunifiedStatus FrameworkunifiedGetDefaultCbHandler(FrameworkunifiedDefaultCallbackHandler *CbHandler) {
1645 if (CbHandler == NULL) {
1646 return eFrameworkunifiedStatusNullPointer;
1649 *CbHandler = g_defaultCallbackHandler;
1650 return eFrameworkunifiedStatusOK;
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;
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);
1669 for (n = 0; n < nfds; ++n) {
1670 if (pApp->siMonitorSock == events[n].data.fd) {
1671 // abnormal monitor connect request
1674 if ((socket = mcAcceptMonitor(pApp->cAppName, pApp->siMonitorSock)) >= 0) {
1675 struct epoll_event event;
1677 event.events = EPOLLRDHUP;
1678 event.data.fd = socket;
1680 if (epoll_ctl(pApp->efd, EPOLL_CTL_ADD, socket, &event) < 0) {
1681 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_ctl:%s", strerror(errno));
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
1697 len = read(pApp->defer_evfd, &data, sizeof(uint64_t));
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);
1704 while (TRUE == (FrameworkunifiedPopedDeferedMessage(hApp))) {
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);
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;
1720 } else if (pApp->siMonitorSock == events[n].data.fd) {
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);
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));
1734 close(events[n].data.fd);
1735 frameworkunifiedUnregistResouce(FRAMEWORKUNIFIED_RES_ABNMLMON, key);
1737 // process the message to the FD set by the user
1738 eStatus = ProcessFd(hApp, events[n].data.fd);
1742 if (errno == EINTR) {
1745 eStatus = eFrameworkunifiedStatusFail;
1746 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : epoll_wait Failed, status=%d, errno=%d", eStatus, errno);
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);
1757 eStatus = eFrameworkunifiedStatusInvldHandle;
1758 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1764 static EFrameworkunifiedStatus ProcessMq(HANDLE hApp) {
1765 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1767 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1768 #ifdef DISPATCHER_PROFILER
1769 CFrameworkunifiedFrameworkApp *pApp = reinterpret_cast< CFrameworkunifiedFrameworkApp * >(hApp);
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);
1778 #ifdef DISPATCHER_PROFILER
1779 else if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) { // NOLINT (readability/nolint)
1780 pApp->m_pFrameworkunifiedMsgProfiler->MsgReceived();
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;
1795 #ifdef DISPATCHER_PROFILER
1796 if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) {
1797 pApp->m_pFrameworkunifiedMsgProfiler->MsgProcessed();
1801 eStatus = eFrameworkunifiedStatusInvldHandle;
1802 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
1808 static EFrameworkunifiedStatus ProcessFd(HANDLE hApp , int fd) {
1809 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
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);
1817 if (enable_rusage == TRUE) {
1818 frameworkunifiedGetRusage(&start);
1821 // finding callback func associated with FD
1822 f_iterator = pApp->fds.find(fd);
1823 if (f_iterator != pApp->fds.end()) {
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;
1836 eStatus = eFrameworkunifiedStatusFail;
1837 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Callback associated with FD is not found. ", eStatus);
1840 if (enable_rusage == TRUE) {
1841 frameworkunifiedGetRusage(&end);
1842 frameworkunifiedPrintRusage(hApp, start, end);
1845 eStatus = eFrameworkunifiedStatusInvldHandle;
1846 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "ERROR : %d, Invalid hApp ", eStatus);
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;
1859 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __PRETTY_FUNCTION__, "This function is not implemented");
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;
1872 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1873 // Get the file path from environment variable
1874 PCHAR l_strEnvValue = getenv(PASA_APPCONFIG_FILES);
1876 if (l_strEnvValue) {
1877 // FRAMEWORKUNIFIEDLOG( ZONE_NS_DIS, __FUNCTION__, "Configuration file path %s ",l_strEnvValue);
1879 std::string l_strFilePath(l_strEnvValue);
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());
1886 eStatus = eFrameworkunifiedStatusFail;
1890 eStatus = eFrameworkunifiedStatusInvldParam;
1896 EFrameworkunifiedStatus FrameworkunifiedProcessServiceProtocolHSMEvent(HANDLE hApp, PCSTR pRequester) {
1897 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1899 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1900 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1901 EventServices::iterator s_iterator;
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;
1911 // FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, __FUNCTION__, " %s found :: (%s) ", pApp->cMsgSrcName, s_iterator->second);
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);
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);
1933 eStatus = eFrameworkunifiedStatusMsgNotProcessed;
1936 // if FrameworkunifiedPostEvent returns NULL
1937 if (eFrameworkunifiedStatusNullPointer == eStatus) {
1938 eStatus = eFrameworkunifiedStatusExit;
1941 eStatus = eFrameworkunifiedStatusMsgNotProcessed;
1945 eStatus = eFrameworkunifiedStatusMsgNotProcessed;
1948 eStatus = eFrameworkunifiedStatusInvldHandle;
1954 //////////////////////////////////////////
1955 // Function : ProcessProtocolEvents
1956 //////////////////////////////////////////
1957 EFrameworkunifiedStatus ProcessProtocolEvents(HANDLE hApp) {
1958 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
1960 if (frameworkunifiedCheckValidAppHandle(hApp)) {
1961 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
1963 // common event table iterator
1964 EventTable::iterator e_iterator;
1965 ServiceSessionIdListTable::iterator ssidl_iterator;
1967 // public state event table iterator
1968 PublicStateEventTable::iterator se_iterator;
1970 // private state event table iterator
1971 PrivateStateEventTable::iterator pse_iterator;
1972 ServiceSessionEventData::iterator ssed_iterator;
1973 SessionEventData::iterator sed_iterator;
1975 HANDLE l_pClientHandle = NULL;
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;
1981 // number of events to register
1982 UI_32 l_uiListSize = static_cast<UI_32>(FrameworkunifiedGetMsgLength(hApp) / sizeof(UI_32));
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;
1989 // get the list of events
1990 eStatus = FrameworkunifiedGetMsgDataOfSize(hApp, l_pData, static_cast<UI_32>(l_uiListSize * sizeof(UI_32)));
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];
1997 // find the event in map of EventTable
1998 e_iterator = pApp->eventtable.find(l_uiEventId);
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;
2005 // find the service name in map of ServiceSessionIdListTable
2006 ssidl_iterator = l_mServiceSessionIdListTable.find(l_cServiceName);
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;
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);
2022 // insert the current session id
2023 l_vSessionIdList.push_back(l_uiSessionId);
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;
2029 // insert the current session id
2030 l_vSessionIdList.push_back(l_uiSessionId);
2032 // create new message queue handle and insert it in map servicesessionhandletable
2033 l_pClientHandle = GetNewHandleAndUpdateEventInfoTable(hApp, l_cServiceName, l_uiSessionId, l_uiEventId);
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));
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;
2042 // insert the current session id
2043 l_vSessionIdList.push_back(l_uiSessionId);
2045 // create new message queue handle and insert it in map servicesessionhandletable
2046 l_pClientHandle = GetNewHandleAndUpdateEventInfoTable(hApp, l_cServiceName, l_uiSessionId, l_uiEventId);
2048 // create a table for storing services and its session id list, registering this event
2049 ServiceSessionIdListTable l_mServiceSessionIdListTable;
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));
2054 // insert the table with event id in EventTable
2055 pApp->eventtable.insert(make_pair(l_uiEventId, l_mServiceSessionIdListTable));
2058 // check whether event is defined as public state event
2059 se_iterator = pApp->publicstateeventtable.find(l_uiEventId);
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);
2065 if (NULL != (se_iterator->second)) {
2066 // send the event data for state event
2067 eStatus = FrameworkunifiedSendMsg(l_pClientHandle,
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",
2077 FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Event %d yet not published ", l_uiEventId);
2079 } else { // check whether the event is defined as private state event
2080 pse_iterator = pApp->privatestateeventtable.find(l_uiEventId);
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);
2086 ssed_iterator = (pse_iterator->second).find(l_cServiceName);
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);
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);
2099 // send the event data for state event
2100 eStatus = FrameworkunifiedSendMsg(l_pClientHandle,
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",
2110 FRAMEWORKUNIFIEDLOG(ZONE_NS_INFO, __FUNCTION__, "Event %d yet not published ", l_uiEventId);
2116 } // end of for loop of events list
2118 eStatus = eFrameworkunifiedStatusInvldBuf;
2119 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_REGISTER_EVENTS Invalid data ", eStatus);
2122 if (NULL != l_pData) {
2127 eStatus = eFrameworkunifiedStatusInvldParam;
2128 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_REGISTER_EVENTS Service Name invalid ", eStatus);
2131 eStatus = eFrameworkunifiedStatusInvldHandle;
2132 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, Invalid hApp ", eStatus);
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]) {
2151 //////////////////////////////////////////
2152 // Function : GetMsgQueueHandle
2153 //////////////////////////////////////////
2154 HANDLE GetMsgQueueHandle(HANDLE hApp, PCSTR serviceName, const UI_32 uiSessionId) {
2155 HANDLE l_pClientHandle = NULL;
2157 if (frameworkunifiedCheckValidAppHandle(hApp)) {
2158 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2160 ServiceSessionHandleTable::iterator ssh_iterator;;
2161 SessionToEventInfo::iterator sh_iterator;
2163 // find the service name
2164 ssh_iterator = pApp->servicesessionhandletable.find(serviceName);
2166 if (pApp->servicesessionhandletable.end() != ssh_iterator) {
2167 // search for session id
2168 sh_iterator = ssh_iterator->second.find(uiSessionId);
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);
2181 if (NULL == l_pClientHandle) {
2182 FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __FUNCTION__, "Handle NULL for service: %s, session: %d.",
2183 serviceName != 0 ? serviceName : NULL, uiSessionId);
2186 return l_pClientHandle;
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;
2196 if (frameworkunifiedCheckValidAppHandle(hApp)) {
2197 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2199 ServiceSessionHandleTable::iterator ssh_iterator;;
2200 SessionToEventInfo::iterator sh_iterator;
2202 ssh_iterator = pApp->servicesessionhandletable.find(serviceName);
2204 if (pApp->servicesessionhandletable.end() == ssh_iterator) {
2205 SessionToEventInfo l_mSessionToEventInfoTable;
2206 pApp->servicesessionhandletable.insert(make_pair(serviceName, l_mSessionToEventInfoTable));
2209 sh_iterator = pApp->servicesessionhandletable[serviceName].find(uiSessionId);
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;
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));
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);
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;
2238 l_pClientHandle = l_ptSessionEventInfo->m_hSession;
2239 l_ptSessionEventInfo->m_vEvents.push_back(uiEventId);
2243 return l_pClientHandle;
2246 //////////////////////////////////////////
2247 // Function : UnregisterPrivateEvents
2248 //////////////////////////////////////////
2249 EFrameworkunifiedStatus UnregisterPrivateEvents(HANDLE hApp) {
2250 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
2252 if (frameworkunifiedCheckValidAppHandle(hApp)) {
2253 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
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;
2259 // number of events to unregister
2260 UI_32 l_uiListSize = static_cast<UI_32>(FrameworkunifiedGetMsgLength(hApp) / sizeof(UI_32));
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;
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)));
2271 if (eFrameworkunifiedStatusOK == eStatus) {
2272 for (UI_32 uiCount = 0; uiCount < l_uiListSize; uiCount++) {
2273 // received event id
2274 l_uiEventId = l_pData[uiCount];
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);
2281 eStatus = eFrameworkunifiedStatusInvldBuf;
2282 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_REGISTER_EVENTS Invalid data ", eStatus);
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");
2295 eStatus = eFrameworkunifiedStatusInvldParam;
2296 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, PROTOCOL_UNREGISTER_EVENTS Service Name invalid ", eStatus);
2299 eStatus = eFrameworkunifiedStatusInvldParam;
2300 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error :: %d, Invalid hApp ", eStatus);
2306 //////////////////////////////////////////
2307 // Function : HandleSessionEventOnCloseSession
2308 //////////////////////////////////////////
2309 EFrameworkunifiedStatus HandleSessionEventOnCloseSession(HANDLE hApp) {
2310 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
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);
2320 CleanAllEventsOfSession(hApp, pApp->cMsgSrcName, tClose.sessionId);
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;
2328 eStatus = eFrameworkunifiedStatusInvldParam;
2329 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid application handle status.");
2335 //////////////////////////////////////////
2336 // Function : FrameworkunifiedOnDummy
2337 //////////////////////////////////////////
2338 EFrameworkunifiedStatus FrameworkunifiedOnDummy(HANDLE hApp) {
2339 return eFrameworkunifiedStatusOK;
2342 #ifdef DISPATCHER_PROFILER
2343 EFrameworkunifiedStatus FrameworkunifiedAttachMsgProfilerCallbacksDispatcher(HANDLE hApp) {
2344 EFrameworkunifiedStatus eStatus = eFrameworkunifiedStatusOK;
2346 if (frameworkunifiedCheckValidAppHandle(hApp)) {
2347 CFrameworkunifiedFrameworkApp *pApp = static_cast<CFrameworkunifiedFrameworkApp *>(hApp);
2348 if (NULL != pApp->m_pFrameworkunifiedMsgProfiler) {
2349 eStatus = FrameworkunifiedAttachCallbackToDispatcher(hApp,
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.");
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.");
2378 FRAMEWORKUNIFIEDLOG(ZONE_NS_DIS, "", "-");