/*******************************************************************************
API: alarmClockStop

Description : Stops the clock (i.e the timer that drives the clock)

Arguments In: 
    None

Arguments Out:
    none
Return Value:
    none
*******************************************************************************/
void alarmClockStop( void )
{
    ClRcT rc = CL_OK;
    if (alarmClock.clockRunning == CL_TRUE)
    {
        rc = clTimerStop(timerHandle);
        if (rc != CL_OK)
        {
            alarmClockLogWrite(CL_LOG_SEV_ERROR,
                    "alarmClockStop(pid=%d): Timer stop failed: 0x%x\n", 
                    getpid(), rc);
        }
        else
        {
            alarmClock.clockRunning = CL_FALSE;

            alarmClockLogWrite(CL_LOG_DEBUG,
                "alarmClockStop(pid=%d): Clock Stop successful\n", 
                getpid());
        }
    }
    else
    {
        alarmClockLogWrite(CL_LOG_DEBUG,
            "alarmClockStop(pid=%d): Clock already stopped\n", 
            getpid());
    }
}
ClRcT
clTimerDelete (ClTimerHandleT*  pTimerHandle)
{
    TsTimer_t* pUserTimer = NULL;
    ClRcT returnCode = CL_ERR_INVALID_HANDLE;

    CL_FUNC_ENTER();
    if (NULL == pTimerHandle) {
        returnCode = CL_TIMER_RC(CL_ERR_NULL_POINTER);
        clDbgCodeError(returnCode, ("Bad timer handle storage"));
        CL_FUNC_EXIT();
        return (returnCode);
    }

    pUserTimer = (TsTimer_t*) *pTimerHandle;
    if (pUserTimer == NULL) {
        returnCode = CL_TIMER_RC(CL_ERR_INVALID_HANDLE);
        clDbgCodeError(returnCode, ("Bad timer handle"));
        CL_FUNC_EXIT();
        return (returnCode);
    }
    if (pUserTimer->state == TIMER_FREE) {
        returnCode = CL_TIMER_RC(CL_TIMER_ERR_INVALID_TIMER);
        clDbgCodeError(returnCode, ("Double delete of a timer"));
        CL_FUNC_EXIT();
        return (returnCode);
    }

    /* if timer is active, remove it from the active-timers queue */
    returnCode = clTimerStop (*pTimerHandle);

    if (returnCode != CL_OK) {
        CL_DEBUG_PRINT (CL_DEBUG_WARN, ("\nTimer delete failed"));
        CL_FUNC_EXIT();
        return (returnCode);
    }

    /* null out all the values */
    pUserTimer->fpTimeOutAction = NULL;
    pUserTimer->pActionArgument = NULL;
    pUserTimer->state = TIMER_FREE;
    pUserTimer->pNextActiveTimer = NULL; /* valid only for active timers */
    pUserTimer->pPreviousActiveTimer = NULL; /* valid only for active timers */

    /* return timer to the free pool */
    tsFreeTimerReturn (pUserTimer);

    *pTimerHandle = NULL;

    CL_FUNC_EXIT();
    return (CL_OK);
}
ClRcT
clTimerUpdate(ClTimerHandleT timerHandle,ClTimerTimeOutT newTimeout)
{
    ClRcT returnCode = CL_ERR_INVALID_HANDLE;
    ClRcT retCode = CL_ERR_INVALID_HANDLE;
    TsTimer_t* pUserTimer = NULL;
    TsTimerState_e currentState;
    pUserTimer = (TsTimer_t*) timerHandle;

    CL_FUNC_ENTER();

    if (pUserTimer == NULL) {
        /* debug message */
        returnCode = CL_TIMER_RC(CL_ERR_INVALID_HANDLE);
        CL_FUNC_EXIT();
        return (returnCode);
    }

    if (pUserTimer->state == TIMER_FREE) {
        /* debug message */
        returnCode = CL_TIMER_RC(CL_ERR_INVALID_HANDLE);
        CL_FUNC_EXIT();
        return (returnCode);
    }

    currentState = pUserTimer->state;

    if(TIMER_ACTIVE == currentState) {
        returnCode = clTimerStop(timerHandle);
        if(CL_OK != returnCode) {
            CL_FUNC_EXIT();
            return(returnCode);
        }
    }

    pUserTimer->timeOut.tsSec = newTimeout.tsSec + (newTimeout.tsMilliSec/1000);
    pUserTimer->timeOut.tsMilliSec = newTimeout.tsMilliSec % 1000;

    if(TIMER_ACTIVE == currentState) {
        timerHandle = pUserTimer;
        retCode = clTimerStart(timerHandle);
    }

    CL_FUNC_EXIT();
    return(CL_OK);
}
ClRcT
clTimerRestart (ClTimerHandleT  timerHandle)
{
    ClRcT returnCode = 0;

    CL_FUNC_ENTER();
    returnCode = clTimerStop (timerHandle);

    if (returnCode != CL_OK) {
        CL_FUNC_EXIT();
        return (returnCode);
    }

    returnCode = clTimerStart (timerHandle);

    if (returnCode != CL_OK) {
        CL_FUNC_EXIT();
        return (returnCode);
    }

    CL_FUNC_EXIT();
    return (CL_OK);
}