/* * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /////////////////////////////////////////////////////////////////////////////// /// \ingroup tag_NSFramework /// \brief Application framework's core structures and data types. /// /// /// /////////////////////////////////////////////////////////////////////////////// #ifndef FRAMEWORK_UNIFIED_CLIENT_NS_FRAMEWORKCORE_INCLUDE_FRAMEWORKUNIFIED_FRAMEWORK_CORE_H_ #define FRAMEWORK_UNIFIED_CLIENT_NS_FRAMEWORKCORE_INCLUDE_FRAMEWORKUNIFIED_FRAMEWORK_CORE_H_ #include #include #include #include #include #include #include #include #include #include #include #include static const UI_32 THREAD_SESSION_ID = 0; typedef HANDLE SessionHandle; typedef int SessionId; // < service protocol command typedef int ServiceProtocolCommand; // < service protocol command typedef int Fd; // < FD associated with callback typedef std::string Notification; // < notification name typedef std::string ServiceName; // < service name typedef std::string ThreadName; typedef std::string DataKey; typedef std::string InvokerName; typedef int Socket; typedef std::string ConnectKey; typedef PVOID Data; typedef std::map NotificationTable; typedef std::map ServiceProtocolTable; typedef std::map SessionTable; typedef std::map Services; typedef std::map ServiceSendMsgHandle; typedef std::map FdTable; typedef int EventId; typedef std::map ServiceEventProtocolTable; typedef std::map EventSessionTable; typedef std::map EventServices; typedef std::map NotificationEventTable; typedef std::map SessionHandleTable; typedef std::map ServiceSessionTable; typedef std::map InvokeResponseQTable; typedef std::map AcceptMonitorTable; typedef std::map ConnectMonitorTable; typedef std::pair NotificationTableRetStatus; typedef std::map AppData; // private event data structure typedef struct _EventData { PVOID pData; /// (hService); /** * @todo * If an application handle is specified in place of a session handle, * the element is accessed illegally without type checking, * and a DeathTest occurs because the thread_self check is violated. */ if (pMsgQ->self != pthread_self() && detect_once == 0) { FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __func__, "Invalid thread context detect(hService/hSession):%#lx:%#lx", pMsgQ->self, pthread_self()); detect_once = 1; } if (pMsgQ->checkCode == MSGQ_CHECK_CODE) { return TRUE; } } return FALSE; } /* data structure for public state events and data associated with it */ // map of state events and data associated with it typedef std::map PublicStateEventTable; /* data structure for private state events and data associated with it */ // list of session id's of service and event data(private) associated with it typedef std::map SessionEventData; // map of service name and list of its session id's and associated event data typedef std::map ServiceSessionEventData; // map of private events and the data published on the all sessions of all services subscribed to it typedef std::map PrivateStateEventTable; // list of session id's for each service registering with event typedef std::vector SessionIdList; typedef std::vector EventIdList; // map of service name and and list of its session id typedef std::map ServiceSessionIdListTable; // map of events and its subscribers typedef std::map EventTable; // structure to hold the session handle and list of event ids for that session typedef struct _SessionEventInfo { HANDLE m_hSession; // Msg queue handle for the specific session EventIdList m_vEvents; // List of event ids corresponding to the session } SessionEventInfo, *PSessionEventInfo; // map to hold the session id and structure of session event info typedef std::map SessionToEventInfo; // map of service name with session id and session handle typedef std::map ServiceSessionHandleTable; // Defer'ed queue // When a pop is issued this data will be copied to the internal CFrameworkunifiedFrameworkApp struct // which will call the FrameworkunifiedDispatchProcess that will process the message as if // it was received off the message queue! class DeferedMsgInfo { void copy_me(const DeferedMsgInfo &defer) { if (this != &defer) { uiProtocolCmd = defer.uiProtocolCmd; strlcpy(cMsgSrcName, defer.cMsgSrcName, sizeof(cMsgSrcName)); std::memcpy(uiMsgRcvBuffer, defer.uiMsgRcvBuffer, MAX_MSGQ_BUFFER); } } public: UI_32 uiProtocolCmd; /// NotificationList; typedef std::queue DeferedMessageQueue; typedef struct _LostSessionInfo { std::string sServiceName; SessionId iSessionId; } LostSessionInfo; typedef std::queue LostSessionQueue; #ifdef DISPATCHER_PROFILER class FrameworkunifiedMsgProfiler; #endif class ResponseData { std::vector rawBuffer; public: void set(PVOID src, UI_32 size) { rawBuffer.resize(size); std::memcpy(&rawBuffer[0], src, size); } UI_8 *data(void) { return rawBuffer.empty() ? NULL : &rawBuffer[0]; } void clear(void) { std::vector().swap(rawBuffer); } UI_32 size(void) { return static_cast(rawBuffer.size()); } }; // framework container typedef struct _CFrameworkunifiedFrameworkApp { CHAR cAppName[MAX_NAME_SIZE_APP]; /// ServiceSendMsgHandle servicesHandle; /// NotificationTable notifications; /// FdTable fds; /// InvokeResponseQTable invokeResQTable; AcceptMonitorTable acceptMonitorTable; ConnectMonitorTable connectMonitorTable; LostSessionQueue lostSessionQueue; CallbackFunctionPtr lostSessionHandler; EventServices eventservices; NotificationEventTable notificationevents; AppData appdata; // Defer'ed Message Queue DeferedMessageQueue deferedMsgQueue; /// BOOL fPopDeferedMsg; HANDLE hNPSndMsgQ; /// (hApp); /** * @todo * eFrameworkunifiedStatusOK is returned even if a appHandle created in another thread is passed, * and the specifications and operations differ. * @todo * If an application handle is specified in place of a session handle, * the element is accessed illegally without type checking, * and a DeathTest occurs because the thread_self check is violated. * @todo * If you specify an app handle for a child thread, * the NULL's return value is not terminated abnormally at the intended point (frameworkunifiedCheckValidAppHandle). */ if (pApp->self != pthread_self() && detect_once == 0) { FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __func__, "Invalid thread context detect(hApp):%#lx:%#lx", pApp->self, pthread_self()); detect_once = 1; } if (pApp->checkCode == APP_CHECK_CODE) { return TRUE; } } return FALSE; } //////////////////////////////////////////////////////////////////////////////////////////// /// NPSubscribeToNotification /// API to send message to Notification Service to add to subscription list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pNotification /// PCSTR - Name of Notification /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPSubscribeToNotification(HANDLE hApp, PCSTR pNotification); //////////////////////////////////////////////////////////////////////////////////////////// /// FrameworkunifiedNPSubscribeToNotifications /// API to send message to Notification Service to add multiple subscriptions list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pList /// FrameworkunifiedNotificationCallbackHandler - Name of Notification /// \param [in] uiListLength /// UI_32 - number of notification that your subscribing too. /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPSubscribeToNotifications(HANDLE hApp, const FrameworkunifiedNotificationCallbackHandler *pList, UI_32 uiListLength); //////////////////////////////////////////////////////////////////////////////////////////// /// NPUnsubscribeFromNotification /// API to send message to Notification Service to remove from subscription list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pNotification /// PCSTR - Name of Notification /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPUnsubscribeFromNotification(HANDLE hApp, PCSTR pNotification); //////////////////////////////////////////////////////////////////////////////////////////// /// FrameworkunifiedNPUnsubscribeFromNotifications /// API to send message to Notification Service to remove multiple notification from subscription list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pList /// FrameworkunifiedNotificationCallbackHandler - notifications list /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPUnsubscribeFromNotifications(HANDLE hApp, const FrameworkunifiedNotificationCallbackHandler *pList, UI_32 uiListLength); //////////////////////////////////////////////////////////////////////////////////////////// /// FrameworkunifiedNPHSMSubscribeToNotificationsEvents /// API to send message to Notification Service to add multiple subscriptions list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pList /// FrameworkunifiedNotificationEvent - list of notification events /// \param [in] uiListLength /// UI_32 - number of notification that your subscribing too. /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPHSMSubscribeToNotificationsEvents(HANDLE hApp, const FrameworkunifiedNotificationEvent *pList, UI_32 uiListLength); //////////////////////////////////////////////////////////////////////////////////////////// /// FrameworkunifiedNPHSMSubscribeToNotificationEvent /// API to send message to Notification Service to add to subscription list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pNotification /// PCSTR - Name of Notification /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPHSMSubscribeToNotificationEvent(HANDLE hApp, PCSTR pNotification); //////////////////////////////////////////////////////////////////////////////////////////// /// FrameworkunifiedNPHSMUnsubscribeFromNotificationEvent /// API to send message to Notification Service to remove from subscription list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pNotification /// PCSTR - Name of Notification /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPHSMUnsubscribeFromNotificationEvent(HANDLE hApp, PCSTR pNotification); //////////////////////////////////////////////////////////////////////////////////////////// /// FrameworkunifiedNPHSMUnsubscribeFromNotificationEvents /// API to send message to Notification Service to remove multiple subscriptions list for /// that notification /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] pList /// FrameworkunifiedNotificationEvent - list of notification events /// \param [in] uiListLength /// UI_32 - number of notification that your subscribing too. /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedNPHSMUnsubscribeFromNotificationEvents(HANDLE hApp, const FrameworkunifiedNotificationEvent *pList, UI_32 uiListLength); //////////////////////////////////////////////////////////////////////////////////////////// /// CreateDispatcher /// /// \param [in] cAppName /// PCSTR - Application name /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus FrameworkunifiedProcessServiceProtocol(HANDLE hApp); EFrameworkunifiedStatus frameworkunifiedAddConnectMonitor(HANDLE hApp, PCSTR serverName, PCSTR clientName); EFrameworkunifiedStatus frameworkunifiedDelConnectMonitorPre(int efd, PCSTR serverName, PCSTR clientName); EFrameworkunifiedStatus frameworkunifiedDelConnectMonitor(HANDLE hApp, PCSTR serverName, PCSTR clientName); //////////////////////////////////////////////////////////////////////////////////////////// /// CreateDispatcher /// /// \param [in] cAppName /// PCSTR - Application name /// \param [out] hApp /// HANDLE& - Reference of application handle /// \param [in] bIsChildThread /// BOOL - child thread /// /// \return status /// EFrameworkunifiedStatus - eFrameworkunifiedStatusOK if success /// eFrameworkunifiedStatusFail if invalid app name /// eFrameworkunifiedStatusNullPointer if unable to open message Q handle /// or possible errors from call FrameworkunifiedAttachSystemCallbacksDispatcher/ /// FrameworkunifiedAttachLoggerCallbacksDispatcher/ /// FrameworkunifiedAttachVersionCallbacksDispatcher /// //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus CreateDispatcher(PCSTR cAppName, HANDLE &hApp, BOOL bIsChildThread = FALSE); // NOLINT (readability/nolint) //////////////////////////////////////////////////////////////////////////////////////////// /// RunDispatcher /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus RunDispatcher(HANDLE hApp); //////////////////////////////////////////////////////////////////////////////////////////// /// RunChildDispatcher /// Dispatcher for child thread. Only difference with \ref RunDispatcher is, child dispatcher /// exits when non eFrameworkunifiedStatusOK value by callback function is returned. /// Whereas, \ref RunDispatcher is not supposed to exit. /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus RunChildDispatcher(HANDLE hChildApp); //////////////////////////////////////////////////////////////////////////////////////////// /// IsValidWaitBarrier /// /// \param [in] wbret /// int - barrier /// /// \return bool /// valid or invalid //////////////////////////////////////////////////////////////////////////////////////////// bool IsValidWaitBarrier(int wbret); //////////////////////////////////////////////////////////////////////////////////////////// /// setChildThreadSched /// /// \param [in] policy /// EFrameworkunifiedSchedPolicy - scheduling policy /// \param [in] priority /// SI_32 - scheduling priority /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus setChildThreadSched(EFrameworkunifiedSchedPolicy policy, SI_32 priority); //////////////////////////////////////////////////////////////////////////////////////////// /// CreateChildThread /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// \param [in] childName /// PCSTR - Name of child thread to be created /// \param [in] CbInitialize /// CbFuncPtr - Initialize function. /// \param [in] CbShutdown /// CbFuncPtr - Shutdown function. /// \param [in] attr /// const FrameworkunifiedChildThreadAttr * - child thread attribute /// \param [in] CbCreateStateMachine /// CbFuncPtr - Function pointer to create state machine, in case of SM child thread, else NULL /// /// \return HANDLE /// Non-null value if thread is created else NULL //////////////////////////////////////////////////////////////////////////////////////////// HANDLE CreateChildThread(HANDLE hApp, PCSTR childName, CbFuncPtr CbInitialize, CbFuncPtr CbShutdown, const FrameworkunifiedChildThreadAttr *attr, CbFuncPtr CbCreateStateMachine); //////////////////////////////////////////////////////////////////////////////////////////// /// Child thread attribute initialize /// /// \param [out] attr /// FrameworkunifiedChildThreadAttr - Child thread attribute /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus CreateChildThreadAttrInit(FrameworkunifiedChildThreadAttr *attr); //////////////////////////////////////////////////////////////////////////////////////////// /// Child thread attribute set schedule policy & priority /// /// \param [out] attr /// FrameworkunifiedChildThreadAttr - Child thread attribute /// \param [int] policy /// EFrameworkunifiedSchedPolicy - Child thread scheduling policy /// \param [in] priority /// SI_32 - Child thread scheduling priority /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus CreateChildThreadAttrSetSched(FrameworkunifiedChildThreadAttr *attr, EFrameworkunifiedSchedPolicy policy, SI_32 priority); //////////////////////////////////////////////////////////////////////////////////////////// /// DestroyThread // Sends eFrameworkunifiedStatusExit status to exits the dispatcher loop to terminate the thread. /// /// \param [in] hApp /// HANDLE - Handle to the Framework /// /// \return status /// EFrameworkunifiedStatus - success or error //////////////////////////////////////////////////////////////////////////////////////////// EFrameworkunifiedStatus DestroyThread(HANDLE hApp); #endif // FRAMEWORK_UNIFIED_CLIENT_NS_FRAMEWORKCORE_INCLUDE_FRAMEWORKUNIFIED_FRAMEWORK_CORE_H_