/**************************************************************************************************
  Filename:       OSAL_ZNP.c
  Revised:        $Date: 2010-08-12 16:15:23 -0700 (Thu, 12 Aug 2010) $
  Revision:       $Revision: 23398 $

  Description:    This file is the Application-specific mandatory OSAL file.


  Copyright 2009-2010 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com. 
**************************************************************************************************/

/*********************************************************************
 * INCLUDES
 */

#include "ZComDef.h"
#include "hal_drivers.h"
#include "hal_mcu.h"
#include "OSAL.h"
#include "OSAL_Clock.h"
#include "OSAL_PwrMgr.h"
#include "OSAL_Tasks.h"

#include "MT.h"
#include "MT_TASK.h"

#include "nwk.h"
#include "APS.h"
#include "ZDApp.h"
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
#include "ZDNwkMgr.h"
#endif
#if defined ( ZIGBEE_FRAGMENTATION )
#include "aps_frag.h"
#endif
#if defined ( INTER_PAN )
#include "stub_aps.h"
#endif
#include "sapi.h"
#include "znp_app.h"
#if defined ( ZCL_KEY_ESTABLISH )
  #include "zcl_key_establish.h"
#endif

/*********************************************************************
 * GLOBAL VARIABLES
 */

// The order in this table must be identical to the task initialization calls below in osalInitTask.
const pTaskEventHandlerFn tasksArr[] = {
  znpEventLoop,
  macEventLoop,
  nwk_event_loop,
  APS_event_loop,
#if defined ( ZIGBEE_FRAGMENTATION )
  APSF_ProcessEvent,
#endif
  ZDApp_event_loop,
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  ZDNwkMgr_event_loop,
#endif
#if defined( INTER_PAN )
  StubAPS_ProcessEvent,
#endif
  SAPI_ProcessEvent,
#if defined ( TC_LINKKEY_JOIN )
  zclKeyEstablish_event_loop,
#endif
  Hal_ProcessEvent
};

const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
uint16 *tasksEvents;

/*********************************************************************
 * FUNCTIONS
 *********************************************************************/

void osal_start_znp(void);
static void osal_run_task(uint8 idx);

/*********************************************************************
 * @fn      osalInitTasks
 *
 * @brief   This function invokes the initialization function for each task.
 *
 * @param   void
 *
 * @return  none
 */
void osalInitTasks( void )
{
  uint8 taskID = 0;

  tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
  osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

  znpInit( taskID++ );
  macTaskInit( taskID++ );
  nwk_init( taskID++ );
  APS_Init( taskID++ );
#if defined ( ZIGBEE_FRAGMENTATION )
  APSF_Init( taskID++ );
#endif
  ZDApp_Init( taskID++ );
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  ZDNwkMgr_Init( taskID++ );
#endif
#if defined( INTER_PAN )
  StubAPS_Init( taskID++ );
#endif
  SAPI_Init( taskID++ );
#if defined ( TC_LINKKEY_JOIN )
  zclGeneral_KeyEstablish_Init( taskID++ );
#endif
  Hal_Init( taskID );
}

/*********************************************************************
 * @fn      osal_start_znp
 *
 * @brief
 *
 *   This function is the main loop function of the task system.  It
 *   will look through all task events and call the task_event_processor()
 *   function for the task with the event.  If there are no events (for
 *   all tasks), this function puts the processor into Sleep.
 *   This Function doesn't return.
 *
 * @param   void
 *
 * @return  none
 */
void osal_start_znp( void )
{
#if !defined ( ZBIT ) && !defined ( UBIT )
  for(;;)  // Forever Loop
#endif
  {
#if defined( POWER_SAVING )
    uint8 busy = FALSE;
#endif
    uint8 idx;

    osalTimeUpdate();
    Hal_ProcessPoll();

    for (idx = 1; idx < tasksCnt; idx++)
    {
      if (tasksEvents[idx])
      {
        osal_run_task(idx);
#if defined( POWER_SAVING )
        busy = TRUE;
#endif
        break;
      }
    }

    if (tasksEvents[0])  // Always run the ZNP task.
    {
      osal_run_task(0);
#if defined( POWER_SAVING )
      busy = TRUE;
#endif
    }

#if defined( POWER_SAVING )
    if (!busy)  // Complete pass through all task events with no activity?
    {
      osal_pwrmgr_powerconserve();  // Put the processor/system into sleep.
    }
#endif
  }
}

/*********************************************************************
 * @fn      osal_run_task
 *
 * @brief
 *
 *   This function is the main loop function of the task system.  It
 *   will look through all task events and call the task_event_processor()
 *   function for the task with the event.  If there are no events (for
 *   all tasks), this function puts the processor into Sleep.
 *   This Function doesn't return.
 *
 * @param   void
 *
 * @return  none
 */
static void osal_run_task(uint8 idx)
{
  uint16 events;
  halIntState_t intState;

  HAL_ENTER_CRITICAL_SECTION(intState);
  events = tasksEvents[idx];
  tasksEvents[idx] = 0;  // Clear the Events for this task.
  HAL_EXIT_CRITICAL_SECTION(intState);

  events = (tasksArr[idx])( idx, events );

  HAL_ENTER_CRITICAL_SECTION(intState);
  tasksEvents[idx] |= events;  // Add back unprocessed events to the current task.
  HAL_EXIT_CRITICAL_SECTION(intState);
}

/*********************************************************************
*********************************************************************/
