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

/* Error Code Mapping */
osal_err_t osal_get_errno(int native_err)
{
    rt_err_t err = (rt_err_t)native_err;
    
    switch (err)
    {
    case RT_EOK:
        return OSAL_OK;
    case -RT_ERROR:
        return OSAL_ERROR;
    case -RT_ETIMEOUT:
        return OSAL_ETIMEOUT;
    case -RT_EFULL:
        return OSAL_EFULL;
    case -RT_EEMPTY:
        return OSAL_EEMPTY;
    case -RT_ENOMEM:
        return OSAL_ENOMEM;
    case -RT_ENOSYS:
        return OSAL_ENOSYS;
    case -RT_EBUSY:
        return OSAL_EBUSY;
    case -RT_EIO:
        return OSAL_EIO;
    case -RT_EINTR:
        return OSAL_EINTR;
    case -RT_EINVAL:
        return OSAL_INVAL;
    default:
        return OSAL_ERROR;
    }
}

/* System Init */
osal_err_t osal_init(void)
{
    /* RT-Thread Nano initialization is done by system/startup */
    return OSAL_OK;
}

/* System Start */
osal_err_t osal_start(void)
{
    /* RT-Thread Nano startup is done by system/startup */
    return OSAL_OK;
}

/* Get System Tick */
osal_tick_t osal_tick_get(void)
{
    return rt_tick_get();
}

/* Delay (Alias for thread delay) */
osal_err_t osal_delay(osal_tick_t tick)
{
    return osal_thread_delay(tick);
}

/* Delay ms (Alias for thread mdelay) */
osal_err_t osal_mdelay(osal_int32_t ms)
{
    return osal_thread_mdelay(ms);
}
