/*
 * voipTimerManager.h
 *
 * Copyright (C) 2005, Ranch Networks, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * Author : Srivatsa Chivukula <srivatsa@ranchnetworks.com>
 *
 */

#ifndef _VOIP_TIMER_MANAGER_H_
#define _VOIP_TIMER_MANAGER_H_

#ifdef VXWORKS
#include <vxWorks.h>
#else
#endif

#include "voipSem.h"
#include "voipTask.h"
#include "voipTimer.h"
#include "voipTimerP.h"

typedef void *(*ALLOCATOR)(size_t size);
typedef void (*DEALLOCATOR)(void *p);
typedef int (*PRE_HANDLER)( TIMER_HANDLER handler_p,
                            void *context,
                            size_t context_size );

class VoipTimerManager
{
    private :
       voipMutexSemaphore m_lock;
       ALLOCATOR    m_alloc;        // Allocation routine pointer.
       DEALLOCATOR  m_free;         // Allocation routine pointer.
       PRE_HANDLER  m_pre_handler;  // called before executing timer handler

       unsigned short     m_timer_on;
       unsigned long      m_tic_counter; // Number if ticks since init
       unsigned long      m_tic;         // Value of tic in milliseconds;
       int a;
       timerNode_t  *m_eTimer_p;    // Current timer in progress.
       timerQueue_t m_pTimerQ;  // Pending List
       timerQueue_t m_eTimerQ;  // Execution List

       int insertTimer( timerNode_t *node_p );
       int flushTimerQueue( timerQueue_t *p, unsigned long force);
       int flushTimerQueue( timerQueue_t *p, unsigned long owner, unsigned long froce);

    public:
       VoipTimerManager(void);
       virtual ~VoipTimerManager(void);

       VoipTimerManager( ALLOCATOR alloc_p, 
                     DEALLOCATOR free_p,
                     PRE_HANDLER prehandler );

       virtual unsigned long preHandler( TIMER_HANDLER handler_p,
                                   void *context, 
                                   size_t context_size );

       void  tic(void);    // Trigger to handle ticks.
       unsigned long init(unsigned long tic);
       unsigned long unitTic(void);

       // If proper deallocator function is passed then the context pointer
       // supplied by the user will be freed.

       int  flush(void);

       int  shutdown(void);
       int  pause(void);
       int  resume(void);

       // Sets interval timer whose interval is specified in
       // 100s of millisecs.

       timer_id_t setTimer(sysTimer_t *t);

       int getTimer(timer_id_t handle, sysTimer_t *t );
       int cancelTimer(timer_id_t handle );
       
       // If unit is set to 0 then time is in milli secs
       // If unit is set to 1 then time is in secs

       unsigned long getElapsedTime(timer_id_t handle, int unit);
       unsigned long getRemainingTime(timer_id_t handle, int unit);
       int setTimerContext(timer_id_t handle, void *cp, size_t cs );
       int setTimerHandler( unsigned long handle, TIMER_HANDLER handler_p);
};

#endif  // _VOIP_TIMER_MANAGER_H_
