text: /**
  ********************************************************************************
  \file   eventk.c
  
  \brief  Source file for kernel event module
  
  This file contains the source code of the kernel event module. It provides the
  interface for posting events to other kernel modules.
  
  \ingroup module_eventk
  *******************************************************************************/
  
  /*------------------------------------------------------------------------------
  Copyright (c) 2012, SYSTEC electronic GmbH
  Copyright (c) 2014, Bernecker+Rainer Industrie-Elektronik Ges.m.b.H. (B&R)
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
      * Neither the name of the copyright holders nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  ------------------------------------------------------------------------------*/
  
  //------------------------------------------------------------------------------
  // includes
  //------------------------------------------------------------------------------
  #include <stddef.h>
  
  #include <kernel/eventk.h>
  #include <kernel/eventkcal.h>
  #include <kernel/nmtk.h>
  #include <kernel/dllk.h>
  #include <kernel/dllkcal.h>
  #include <kernel/errhndk.h>
  #include <oplk/benchmark.h>
  
  #if defined(CONFIG_INCLUDE_PDO)
  #include <kernel/pdok.h>
  #include <kernel/pdokcal.h>
  #endif
  
  #include "common/event/event.h"
  
  //============================================================================//
  //            G L O B A L   D E F I N I T I O N S                             //
  //============================================================================//
  
  //------------------------------------------------------------------------------
  // const defines
  //------------------------------------------------------------------------------
  
  //------------------------------------------------------------------------------
  // module global vars
  //------------------------------------------------------------------------------
  
  //------------------------------------------------------------------------------
  // global function prototypes
  //------------------------------------------------------------------------------
  
  //============================================================================//
  //            P R I V A T E   D E F I N I T I O N S                           //
  //============================================================================//
  
  //------------------------------------------------------------------------------
  // const defines
  //------------------------------------------------------------------------------
  
  //------------------------------------------------------------------------------
  // local types
  //------------------------------------------------------------------------------
  
  //------------------------------------------------------------------------------
  // local function prototypes
  //------------------------------------------------------------------------------
  static tOplkError handleNmtEventinDll(tEvent* pEvent_p);
  
  //------------------------------------------------------------------------------
  // local vars
  //------------------------------------------------------------------------------
  
  /**
  \brief  Event dispatch table
  
  The following table defines the event handlers to be used for the specific
  event sinks.
  */
  static tEventDispatchEntry eventDispatchTbl_l[] =
  {
      { kEventSinkNmtk,        kEventSourceNmtk,        nmtk_process },
      { kEventSinkNmtk,        kEventSourceDllk,        handleNmtEventinDll },
      { kEventSinkDllk,        kEventSourceDllk,        dllk_process },
      { kEventSinkDllkCal,     kEventSourceDllk,        dllkcal_process },
      { kEventSinkErrk,        kEventSourceErrk,        errhndk_process },
  #if defined(CONFIG_INCLUDE_PDO)
      { kEventSinkPdokCal,     kEventSourcePdok,        pdokcal_process },
  #endif
      { kEventSinkInvalid,     kEventSourceInvalid,     NULL }
  };
  
  //============================================================================//
  //            P U B L I C   F U N C T I O N S                                 //
  //============================================================================//
  
  //------------------------------------------------------------------------------
  /**
  \brief    Initialize kernel event module
  
  The function initializes the kernel event module. It is also responsible to call
  the init function of its CAL module.
  
  \return The function returns a tOplkError error code.
  \retval kErrorOk                Function executes correctly
  \retval other error codes       An error occurred
  
  \ingroup module_eventk
  */
  //------------------------------------------------------------------------------
  tOplkError eventk_init (void)
  {
      tOplkError  ret = kErrorOk;
  
      ret = eventkcal_init();
  
      return ret;
  }
  
  //------------------------------------------------------------------------------
  /**
  \brief    Clean up kernel event module
  
  This function cleans up the kernel event module.
  
  \return The function returns a tOplkError error code.
  \retval kErrorOk                Function executes correctly
  \retval other error codes       An error occurred
  
  \ingroup module_eventk
  */
  //------------------------------------------------------------------------------
  tOplkError eventk_exit(void)
  {
      tOplkError      ret = kErrorOk;
  
      ret = eventkcal_exit();
  
      return ret;
  }
  
  //------------------------------------------------------------------------------
  /**
  \brief    Kernel event handler
  
  This function processes events posted to the kernel layer. It examines the
  sink and forwards the events by calling the event process function of the
  specific module.
  
  \param  pEvent_p                Received event.
  
  \return The function returns a tOplkError error code.
  \retval kErrorOk                Function executes correctly
  \retval other error codes       An error occurred
  
  \ingroup module_eventk
  */
  //------------------------------------------------------------------------------
  tOplkError eventk_process(tEvent* pEvent_p)
  {
      tOplkError              ret = kErrorOk;
      tEventSource            eventSource;
      tProcessEventCb         pfnEventHandler;
      BOOL                    fStop = FALSE;
      BOOL                    fAlreadyHandled = FALSE;
      tEventDispatchEntry*    pDispatchEntry;
  
      pDispatchEntry = &eventDispatchTbl_l[0];
      while (!fStop)
      {
          ret = event_getHandlerForSink(&pDispatchEntry, pEvent_p->eventSink,
                                        &pfnEventHandler, &eventSource);
          if (ret == kErrorEventUnknownSink)
          {
                  if (!fAlreadyHandled)
                  {
                      // Unknown sink, provide error event to API layer
                      eventk_postError(kEventSourceEventk, ret,
                                       sizeof(pEvent_p->eventSink),
                                       &pEvent_p->eventSink);
                  }
                  else
                  {
                      ret = kErrorOk;
                  }
                  fStop = TRUE;
          }
          else
          {
              if (pfnEventHandler != NULL)
              {
                  ret = pfnEventHandler(pEvent_p);
                  if ((ret != kErrorOk) && (ret != kErrorShutdown))
                  {
                      // forward error event to API layer
                      eventk_postError(kEventSourceEventk, ret,
                                       sizeof(eventSource),
                                       &eventSource);
                  }
              }
              fAlreadyHandled = TRUE;
          }
      }
  
      return ret;
  }
  
  //------------------------------------------------------------------------------
  /**
  \brief    Post kernel event
  
  This function posts an event to a queue. It calls the post function of the
  CAL module which distributes the event to the suitable event queue.
  
  \param  pEvent_p                Event to be posted.
  
  \return The function returns a tOplkError error code.
  \retval kErrorOk                Function executes correctly
  \retval other error codes       An error occurred
  
  \ingroup module_eventk
  */
  //------------------------------------------------------------------------------
  tOplkError eventk_postEvent(tEvent* pEvent_p)
  {
      tOplkError ret = kErrorOk;
  
      switch (pEvent_p->eventSink)
      {
          case kEventSinkNmtMnu:
          case kEventSinkNmtu:
          case kEventSinkSdoAsySeq:
          case kEventSinkApi:
          case kEventSinkDlluCal:
          case kEventSinkErru:
          case kEventSinkLedu:
              ret = eventkcal_postUserEvent(pEvent_p);
              break;
  
          case kEventSinkSync:
          case kEventSinkNmtk:
          case kEventSinkDllk:
          case kEventSinkDllkCal:
          case kEventSinkPdok:
          case kEventSinkPdokCal:
          case kEventSinkErrk:
              ret = eventkcal_postKernelEvent(pEvent_p);
              break;
  
          default:
              ret = kErrorEventUnknownSink;
              break;
      }
  
      return ret;
  }
  
  //------------------------------------------------------------------------------
  /**
  \brief    Post an error event
  
  This function posts an error event to the API module.
  
  \param  eventSource_p           Source that caused the error
  \param  oplkError_p             Error code
  \param  argSize_p               Size of error argument
  \param  pArg_p                  Error argument
  
  \return The function returns a tOplkError error code.
  \retval kErrorOk                Function executes correctly
  \retval other error codes       An error occurred
  
  \ingroup module_eventk
  */
  //------------------------------------------------------------------------------
  tOplkError eventk_postError(tEventSource eventSource_p, tOplkError oplkError_p,
                              UINT argSize_p, void* pArg_p)
  {
      tOplkError          ret;
      tEventError         eventError;
      tEvent              oplkEvent;
  
      ret = kErrorOk;
  
      // create argument
      eventError.eventSource = eventSource_p;
      eventError.oplkError = oplkError_p;
      argSize_p = (UINT)min((size_t)argSize_p, sizeof(eventError.errorArg));
      OPLK_MEMCPY(&eventError.errorArg, pArg_p, argSize_p);
  
      // create event
      oplkEvent.eventType = kEventTypeError;
      oplkEvent.eventSink = kEventSinkApi;
      OPLK_MEMSET(&oplkEvent.netTime, 0x00, sizeof(oplkEvent.netTime));
      oplkEvent.eventArgSize = offsetof(tEventError, errorArg) + argSize_p;
      oplkEvent.pEventArg = &eventError;
  
      ret = eventk_postEvent(&oplkEvent);
  
      return ret;
  }
  
  //============================================================================//
  //            P R I V A T E   F U N C T I O N S                               //
  //============================================================================//
  /// \name Private Functions
  /// \{
  
  //------------------------------------------------------------------------------
  /**
  \brief  Handle NMT event in DLL
  
  This function dispatches NMT events which need to be handled by DLL to the DLLk
  module.
  
  \param  pEvent_p                Event to process.
  
  \return The function returns a tOplkError error code.
  \retval kErrorOk                Function executes correctly
  \retval other error codes       An error occurred
  */
  //------------------------------------------------------------------------------
  static tOplkError handleNmtEventinDll(tEvent* pEvent_p)
  {
      tOplkError          ret = kErrorOk;
  
      BENCHMARK_MOD_27_RESET(0);
  
      if ((pEvent_p->eventType == kEventTypeNmtEvent) &&
          (*((tNmtEvent*)pEvent_p->pEventArg) == kNmtEventDllCeSoa))
      {
          BENCHMARK_MOD_27_SET(0);
          // forward SoA event to DLLk module for cycle preprocessing
          ret = dllk_process(pEvent_p);
  
          BENCHMARK_MOD_27_RESET(0);
      }
  
      return ret;
  }
  
  /// \}
  
  