#include "osal_timer.h"
#include <rtthread.h>

osal_timer_t osal_timer_create(const char* name,
                              osal_timer_callback timeout,
                              void* parameter,
                              osal_tick_t time,
                              osal_uint8_t flag)
{
    OSAL_ASSERT(name != NULL);
    OSAL_ASSERT(timeout != NULL);

    rt_timer_t timer;
    
    timer = rt_timer_create(name, 
                           (void (*)(void*))timeout, 
                           parameter, 
                           time, 
                           flag);
    
    return (osal_timer_t)timer;
}

osal_err_t osal_timer_delete(osal_timer_t timer)
{
    if (timer == NULL)
    {
        return OSAL_INVAL;
    }
    
    rt_err_t ret = rt_timer_delete((rt_timer_t)timer);
    
    if (ret == RT_EOK)
    {
        return OSAL_OK;
    }
    else
    {
        return OSAL_ERROR;
    }
}

osal_err_t osal_timer_start(osal_timer_t timer)
{
    if (timer == NULL)
    {
        return OSAL_INVAL;
    }
    
    rt_err_t ret = rt_timer_start((rt_timer_t)timer);
    
    if (ret == RT_EOK)
    {
        return OSAL_OK;
    }
    else
    {
        return OSAL_ERROR;
    }
}

osal_err_t osal_timer_stop(osal_timer_t timer)
{
    if (timer == NULL)
    {
        return OSAL_INVAL;
    }
    
    rt_err_t ret = rt_timer_stop((rt_timer_t)timer);
    
    if (ret == RT_EOK)
    {
        return OSAL_OK;
    }
    else
    {
        return OSAL_ERROR;
    }
}
