/*
 * FreeModbus Libary: BARE Port
 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id$
 */

/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"
#include "port.h"

#if  MODBUS_USED_FREERTOS
 
#include <FreeRTOS.h>
// #include <task.h>
// #include <queue.h>

osEventFlagsId_t modbusEventHandle;
const osEventFlagsAttr_t modbusEvent_attributes = {
    .name = "modbusEvent"
};
 

BOOL xMBPortEventInit(void) {

    ENTER_CRITICAL_SECTION();
    modbusEventHandle = osEventFlagsNew(&modbusEvent_attributes);
    EXIT_CRITICAL_SECTION();

    if (modbusEventHandle != NULL) {
      // MODBUS_DEBUG("xMBPortEventInit Success!\r\n");
    } else {
      // MODBUS_DEBUG("xMBPortEventInit Faild !\r\n");
      return FALSE;
    }
    return TRUE;
}

void
vMBPortEventClose( void )
{
    if(modbusEventHandle != NULL )
    {
        osEventFlagsDelete( modbusEventHandle );
        modbusEventHandle = NULL;
    }
}

BOOL xMBPortEventPost(eMBEventType eEvent) {
  BaseType_t flag;
  //   osEventFlagsSet 兼容了是否在中断中的操作，否则需要判断是否在中断中
  if (modbusEventHandle != NULL) {
    osEventFlagsSet(modbusEventHandle,eEvent);
  }
  return TRUE;
}

BOOL xMBPortEventGet(eMBEventType *eEvent) {
    uint32_t recvedEvent;
    /* waiting forever OS event */
    recvedEvent = osEventFlagsWait (modbusEventHandle,
                                      EV_READY | EV_FRAME_RECEIVED | EV_EXECUTE |
                                          EV_FRAME_SENT, /* 接收任务感兴趣的事件                                                       */
                                      0,  
                                      portMAX_DELAY); /* 指定超时事件,无限等待 */
    switch (recvedEvent) {
    case EV_READY:
      *eEvent = EV_READY;
      break;
    case EV_FRAME_RECEIVED:
      *eEvent = EV_FRAME_RECEIVED;
      break;
    case EV_EXECUTE:
      *eEvent = EV_EXECUTE;
      break;
    case EV_FRAME_SENT:
      *eEvent = EV_FRAME_SENT;
      break;
    }
    return TRUE;
}

#else

 
// /* ----------------------- Variables ----------------------------------------*/
// static eMBEventType eQueuedEvent;
// static BOOL     xEventInQueue;

// /* ----------------------- Start implementation -----------------------------*/
// BOOL
// xMBPortEventInit( void )
// {
//     xEventInQueue = FALSE;
//     return TRUE;
// }

// BOOL
// xMBPortEventPost( eMBEventType eEvent )
// {
//     xEventInQueue = TRUE;
//     eQueuedEvent = eEvent;
//     return TRUE;
// }

// BOOL
// xMBPortEventGet( eMBEventType * eEvent )
// {
//     BOOL            xEventHappened = FALSE;

//     if( xEventInQueue )
//     {
//         *eEvent = eQueuedEvent;
//         xEventInQueue = FALSE;
//         xEventHappened = TRUE;
//     }
//     return xEventHappened;
// }

#endif