/**
  ******************************************************************************
  * @file    WiFi/EMW3080_IAP_Client/Src/EMW3080_io.c
  * @author  MCD Application Team
  * @brief   This file implments the IO operations to deal with the EMW3080 wifi
  *          module. It mainly Inits and Deinits the UART interface. Send and
  *          receive data over it.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics International N.V. 
  * All rights reserved.</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without 
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice, 
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other 
  *    contributors to this software may be used to endorse or promote products 
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this 
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under 
  *    this license is void and will automatically terminate your rights under 
  *    this license. 
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "emw3080_io.h"

/* Private define ------------------------------------------------------------*/

//
///* Private typedef -----------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
RingBuffer_t WiFiRxBuffer;
esp_sock_ctx_t esp_socks[5];
extern UART_HandleTypeDef huart1;
uint8_t UartTxDone;
/* Private function prototypes -----------------------------------------------*/
static void ESP_Socks_Init(void);
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  EMW3080 IO Initalization.
  *         This function inits the UART interface to deal with the EMW3080,
  *         then starts asynchronous listening on the RX port.
  * @param None
  * @retval 0 on success, -1 otherwise.
  */
int8_t EMW3080_IO_Init(void)
{
  ESP_Socks_Init();
  
//  /* Set the WiFi USART configuration parameters */
//  WiFiUartHandle.Instance        = USART1;
//  WiFiUartHandle.Init.BaudRate   = 115200;
//  WiFiUartHandle.Init.WordLength = UART_WORDLENGTH_8B;
//  WiFiUartHandle.Init.StopBits   = UART_STOPBITS_1;
//  WiFiUartHandle.Init.Parity     = UART_PARITY_NONE;
//  WiFiUartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
//  WiFiUartHandle.Init.Mode       = UART_MODE_TX_RX;
//  WiFiUartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
//
//  /* Configure the USART IP */
//  if(HAL_UART_Init(&WiFiUartHandle) != HAL_OK)
//  {
//    return -1;
//  }
 
  /* Once the WiFi UART is intialized, start an asynchrounous recursive 
   listening. the HAL_UART_Receive_IT() call below will wait until one char is
   received to trigger the HAL_UART_RxCpltCallback(). The latter will recursively
   call the former to read another char.  */
  WiFiRxBuffer.head = 0;
  WiFiRxBuffer.tail = 0;
 
  HAL_UART_Receive_IT(&huart1, (uint8_t *)&WiFiRxBuffer.data[WiFiRxBuffer.tail], 1);

  return 0;
}

/**
  * @brief  EMW3080 IO Deinitialization.
  *         This function Deinits the UART interface of the EMW3080. When called
  *         the EMW3080 commands can't be executed anymore.
  * @param  None.
  * @retval None.
  */
void EMW3080_IO_DeInit(void)
{
  /* Reset USART configuration to default */
  HAL_UART_DeInit(&huart1);
}

/**
  * @brief  Send Data to the EMW3080 module over the UART interface.
  *         This function allows sending data to the  EMW3080 WiFi Module, the
  *          data can be either an AT command or raw data to send over 
             a pre-established WiFi connection.
  * @param pData: data to send.
  * @param Length: the data length.
  * @retval 0 on success, -1 otherwise.
  */
int8_t EMW3080_IO_Send(uint8_t* pData, uint32_t Length)
{
  uint32_t tickstart;

  UartTxDone = 0;
  if (HAL_UART_Transmit_IT(&huart1, (uint8_t*)pData, Length) != HAL_OK)
  {
     return -1;
  }
  tickstart = HAL_GetTick();
  while(UartTxDone!=1)
  {
    if((HAL_GetTick()-tickstart) > DEFAULT_TIME_OUT)
      return -1;
  }
  
  return 0;
}

/**
  * @brief  Receive Data from the EMW3080 module over the UART interface.
  *         This function receives data from the  EMW3080 WiFi module, the
  *         data is fetched from a ring buffer that is asynchonously and continuously
            filled with the received data.
  * @param  Buffer: a buffer inside which the data will be read.
  * @param  Length: the Maximum size of the data to receive.
  * @retval int32_t: the actual data size that has been received.
  */
int32_t EMW3080_IO_Receive(uint8_t* Buffer, uint32_t Length, uint32_t Timeout)
{
  uint32_t ReadData = 0;

 
  /* Loop until data received */
  while (Length--)
  {
    uint32_t tickStart = HAL_GetTick();
    do
    {
      if(WiFiRxBuffer.head != WiFiRxBuffer.tail)
      {
        /* serial data available, so return data to user */
        *Buffer++ = WiFiRxBuffer.data[WiFiRxBuffer.head++];
        ReadData++;
		
        /* check for ring buffer wrap */
        if (WiFiRxBuffer.head >= RING_BUFFER_SIZE)
        {
          /* Ring buffer wrap, so reset head pointer to start of buffer */
          WiFiRxBuffer.head = 0;
        }
        break;
      }
    }while((HAL_GetTick() - tickStart ) < Timeout);
  }

  return ReadData;
}

/**
  * @brief  Rx Callback when new data is received on the UART.
  * @param  UartHandle: Uart handle receiving the data.
  * @retval None.
  */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
  static uint32_t count_test1 =0;
  count_test1++;
  /* If ring buffer end is reached reset tail pointer to start of buffer */
  if(++WiFiRxBuffer.tail >= RING_BUFFER_SIZE)
  {
    WiFiRxBuffer.tail = 0;   
  }
  
  HAL_UART_Receive_IT(&huart1, (uint8_t *)&WiFiRxBuffer.data[WiFiRxBuffer.tail], 1);
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
  /* Set transmission flag: transfer complete */
  UartTxDone = 1;  
}
/**
  * @brief  Function called when error happens on the UART.
  * @param  UartHandle: Uart handle receiving the data.
  * @retval None.
  */
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
  /* Add user code here*/
}

/**
  * @brief  Handler to deinialize the EMW3080 UART interface in case of errors.
  * @param  None
  * @retval None.
  */
//static void WIFI_Handler(void)
//{
//  HAL_UART_DeInit(&WiFiUartHandle);
//  
//  while(1)
//  {
//  }
//}

static void ESP_Socks_Init(void)
{
  uint8_t i=0;
  
  for(i=0;i<5;i++)
  {
    esp_socks[i].socket = i;
#if defined __GNUC__
    esp_socks[i].pdata = (uint8_t *)NULL;
#else
    esp_socks[i].pdata = NULL;
#endif
    esp_socks[i].offset = 0;
    esp_socks[i].left = 0;
  }
}

esp_sock_ctx_t* Get_Esp_Sock(uint8_t socket)
{
   if(socket >=4)
#if defined __GNUC__
     return (esp_sock_ctx_t *)0;
#else
   	 return NULL;
#endif
 
  return &(esp_socks[socket]);
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
