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

osal_event_t osal_event_create(const char* name)
{
    OSAL_ASSERT(name != NULL);

    rt_event_t event;
    
    event = rt_event_create(name, RT_IPC_FLAG_FIFO);
    
    return (osal_event_t)event;
}

osal_err_t osal_event_delete(osal_event_t event)
{
    if (event == NULL)
    {
        return OSAL_INVAL;
    }
    
    rt_err_t ret = rt_event_delete((rt_event_t)event);
    
    return osal_get_errno(ret);
}

osal_err_t osal_event_send(osal_event_t event, osal_uint32_t set)
{
    if (event == NULL)
    {
        return OSAL_INVAL;
    }
    
    rt_err_t ret = rt_event_send((rt_event_t)event, set);
    
    return osal_get_errno(ret);
}

osal_err_t osal_event_recv(osal_event_t event,
                          osal_uint32_t set,
                          osal_uint8_t opt,
                          osal_int32_t timeout,
                          osal_uint32_t* recved)
{
    if (event == NULL)
    {
        return OSAL_INVAL;
    }
    
    rt_uint32_t recv_set;
    rt_err_t ret;
    
    ret = rt_event_recv((rt_event_t)event, 
                       set, 
                       opt, 
                       timeout, 
                       &recv_set);
    
    if (recved != NULL)
    {
        *recved = recv_set;
    }
    
    return osal_get_errno(ret);
}
