StdPJ/Core/Src/APP/BSP/ug96/ug96.c
/**
  * @brief  Retrieve Data from the C2C module over the UART interface.
  *         This function receives data from the  C2C module, the
  *         data is fetched from a ring buffer that is asynchronously and continuously
            filled with the received data.
  * @param  Obj: pointer to module handle
  * @param  pData: a buffer inside which the data will be read.
  * @param  Length: Size of the data to receive.
  * @param  ScanVals: when param Length = 0 : values to be retrieved in the coming data in order to exit.
  * @retval int32_t: if param Length = 0 : the actual RET_CODE found,
                     if param Length > 0 : the actual data size that has been received
                     if error (timeout): return -1 (UG96_RETURN_RETRIEVE_ERROR).
  */
@brief  Retrieve Data from the C2C module over the UART interface.
This function receives data from the  C2C module, the
data is fetched from a ring buffer that is asynchronously and continuously
filled with the received data.
@param  Obj: pointer to module handle
@param  pData: a buffer inside which the data will be read.
@param  Length: Size of the data to receive.
@param  ScanVals: when param Length = 0 : values to be retrieved in the coming data in order to exit.
@retval int32_t: if param Length = 0 : the actual RET_CODE found,
if param Length > 0 : the actual data size that has been received
if error (timeout): return -1 (UG96_RETURN_RETRIEVE_ERROR).
@brief Retrieve Data from the C2C module over the UART interface . This function receives data from the C2C module the data is fetched from a ring buffer that is asynchronously and continuously filled with the received data . @param Obj : pointer to module handle @param pData : a buffer inside which the data will be read . @param Length : Size of the data to receive . @param ScanVals : when param Length = 0 : values to be retrieved in the coming data in order to exit . @retval int32_t : if param Length = 0 : the actual RET_CODE found if param Length > 0 : the actual data size that has been received if error ( timeout ) : return - 1 ( UG96_RETURN_RETRIEVE_ERROR ) .
static int32_t AT_RetrieveData(Ug96Object_t *Obj, uint8_t* pData, uint16_t Length, uint32_t ScanVals, uint32_t Timeout)
{
  uint32_t tickstart = Obj->GetTickCb();
  int16_t ReadData = 0;
  uint16_t x;
  uint16_t index[NUM_RESPONSES];
  uint16_t lens[NUM_RESPONSES];
  uint16_t  pos;
  uint8_t c;
  int32_t  min_requested_time;
  min_requested_time = 2*(Length+15)*8*1000/UG96_DEFAULT_BAUDRATE;  
  if (Timeout < min_requested_time)       
  {
     Timeout = min_requested_time;
#ifdef UG96_DBG
     printf("                UART_C2C: Timeout forced to respect UART speed %d: %ld \n", UG96_DEFAULT_BAUDRATE, min_requested_time);
#endif
  }
  for (x = 0; x < NUM_RESPONSES; x++)
  {
    index[x] = 0;
    lens[x] = strlen(ReturnKeywords[x].retstr);
  }
  if ((Length == 0) && (ScanVals == RET_NONE))
  {
     return 0;  
  }
  memset(Obj->CmdResp, 0, UG96_CMD_SIZE);
  while (TimeLeftFromExpiration(tickstart, Obj->GetTickCb(), Timeout) > 0)
  {
    if(Obj->fops.IO_ReceiveOne(&c) == 0)   
    {
      pData[ReadData++] = c;
      if (Length == 0)
      {
        for(x = 0; x < NUM_RESPONSES; x++)
        {
          if (c != ReturnKeywords[x].retstr[index[x]])
          {
            index[x] = 0;
          }
          if (c == ReturnKeywords[x].retstr[index[x]])
          {
            pos = ++(index[x]);
            if( pos >= lens[x])
            {
              if (ScanVals & ReturnKeywords[x].retval)
              {
                return ReturnKeywords[x].retval;
              }
            }
          }
        }
      }
      else  
      {
        if (ReadData < Length)
        {
        }
        else 
        {
          return ReadData;
        }
      } 
    }
  }
  if ((Length > 0) && (ReadData > 0))
  {
#ifdef UG96_DBG
    printf("\n UART_C2C: Warning: timeout occurred before all data was read (%d/%u)\n ", ReadData, Length);
#endif
    return ReadData;
  }
#ifdef UG96_DEEP_DBG
    printf("\n UART_C2C: RET_CODE not found or non a single byte has been read at all \n ");
#endif
  return UG96_RETURN_RETRIEVE_ERROR;
}
static int32_t AT_RetrieveData ( Ug96Object_t * Obj , uint8_t * pData , uint16_t Length , uint32_t ScanVals , uint32_t Timeout ) { uint32_t tickstart = Obj -> GetTickCb ( ) ; int16_t ReadData = 0 ; uint16_t x ; uint16_t index [ NUM_RESPONSES ] ; uint16_t lens [ NUM_RESPONSES ] ; uint16_t pos ; uint8_t c ; int32_t min_requested_time ; min_requested_time = 2 * ( Length + 15 ) * 8 * 1000 / UG96_DEFAULT_BAUDRATE ; if ( Timeout < min_requested_time ) { Timeout = min_requested_time ; #ifdef UG96_DBG printf ( " \n " , UG96_DEFAULT_BAUDRATE , min_requested_time ) ; #endif } for ( x = 0 ; x < NUM_RESPONSES ; x ++ ) { index [ x ] = 0 ; lens [ x ] = strlen ( ReturnKeywords [ x ] . retstr ) ; } if ( ( Length == 0 ) && ( ScanVals == RET_NONE ) ) { return 0 ; } memset ( Obj -> CmdResp , 0 , UG96_CMD_SIZE ) ; while ( TimeLeftFromExpiration ( tickstart , Obj -> GetTickCb ( ) , Timeout ) > 0 ) { if ( Obj -> fops . IO_ReceiveOne ( & c ) == 0 ) { pData [ ReadData ++ ] = c ; if ( Length == 0 ) { for ( x = 0 ; x < NUM_RESPONSES ; x ++ ) { if ( c != ReturnKeywords [ x ] . retstr [ index [ x ] ] ) { index [ x ] = 0 ; } if ( c == ReturnKeywords [ x ] . retstr [ index [ x ] ] ) { pos = ++ ( index [ x ] ) ; if ( pos >= lens [ x ] ) { if ( ScanVals & ReturnKeywords [ x ] . retval ) { return ReturnKeywords [ x ] . retval ; } } } } } else { if ( ReadData < Length ) { } else { return ReadData ; } } } } if ( ( Length > 0 ) && ( ReadData > 0 ) ) { #ifdef UG96_DBG printf ( " \n \n " , ReadData , Length ) ; #endif return ReadData ; } #ifdef UG96_DEEP_DBG printf ( " \n \n " ) ; #endif return UG96_RETURN_RETRIEVE_ERROR ; }
@brief  Retrieve Data from the C2C module over the UART interface.
@brief Retrieve Data from the C2C module over the UART interface .
/* 15 is the max lengh of the return keyword */ /* UART speed 115200 bits per sec */ /* to avoid waiting a RET_VAL in case the parsed_lenth of payload is zero */ /* but no code needs to be retrieved */ /* Receive one sample from UART */ /* serial data available, so return data to user */ /* Check whether we hit an ESP return values */ /* end (Length > 0) */ /* nothing to do except keep reading in the while loop */ /* ReadData >= Length */ /* end (Length == 0) */