StdPJ/Core/Src/APP/BSP/ug96/ug96.c
/**
  * @brief  Initialize UG96 module.
  * @param  Obj: pointer to module handle
  * @retval Operation Status.
  */
@brief  Initialize UG96 module.
@param  Obj: pointer to module handle
@retval Operation Status.
@brief Initialize UG96 module . @param Obj : pointer to module handle @retval Operation Status .
UG96_InitRet_t  UG96_Init(Ug96Object_t *Obj)
{
  UG96_InitRet_t fret = UG96_INIT_OTHER_ERR;
  int32_t ret = RET_ERROR;
  int8_t i;
  char *align_ptr, *token;
  uint8_t parse_count;
  uint32_t tickstart;
  Obj->APsActive = 0;
  for (i = 0; i < UG96_MAX_SOCKETS; i++)
  {
    Obj->SocketInfo[i].Type = UG96_TCP_CONNECTION;
    Obj->SocketInfo[i].AccessMode = UG96_BUFFER_MODE;
    Obj->SocketInfo[i].ComulatedQirdData = 0;
    Obj->SocketInfo[i].HaveReadLength = 0;
    Obj->SocketInfo[i].UnreadLength = 0;
  }
  Obj->fops.IO_FlushBuffer();  
  if (Obj->fops.IO_Init() == 0) 
  {
    ret = AT_Synchro(Obj);
    if(ret != RET_OK)
    {
      printf("Fail to AT SYNC, after several attempts\n");
      fret = UG96_INIT_RET_AT_ERR; 
    }
    else
    {
      ret = UG96_ResetToFactoryDefault(Obj);
      ret = ret | UG96_GetUARTConfig(Obj, &Obj->UART_Config);
      ret = ret | AT_ExecuteCommand(Obj, UG96_TOUT_SHORT, (uint8_t *)"ATV1\r\n", RET_OK | RET_ERROR);
      ret = ret | AT_ExecuteCommand(Obj, UG96_TOUT_SHORT, (uint8_t *)"ATE0\r\n", RET_OK | RET_ERROR);
      ret = ret | AT_ExecuteCommand(Obj, UG96_TOUT_300, (uint8_t *)"AT+CMEE=1\r\n", RET_OK | RET_ERROR);
#ifndef USE_BG96
      ret = ret | AT_ExecuteCommand(Obj, UG96_TOUT_300, (uint8_t *)"AT+QCFG=\"URC/PORT\",1,\"uart1\"\r\n", RET_OK | RET_ERROR);
#endif
    }
    if(ret == RET_OK)
    {
        ret = AT_ExecuteCommand(Obj, UG96_TOUT_SHORT, (uint8_t *)"AT+GMI\r\n", RET_OK | RET_ERROR);
        if (ret == RET_OK)
        {
          align_ptr = strstr((char *) Obj->CmdResp,"\r\n") + 2;
          token = strtok(align_ptr,"\r\n\r\n");
          strncpy((char *)Obj->Manufacturer, token, UG96_MFC_SIZE);
        }
        ret = AT_ExecuteCommand(Obj, UG96_TOUT_SHORT, (uint8_t *)"AT+GMM\r\n", RET_OK | RET_ERROR);
        if (ret == RET_OK)
        {
          align_ptr = strstr((char *) Obj->CmdResp,"\r\n") + 2;
          token = strtok(align_ptr,"\r\n\r\n");
          strncpy((char *)Obj->ProductID, token, UG96_PROD_ID_SIZE);
        }
        ret = AT_ExecuteCommand(Obj, UG96_TOUT_SHORT, (uint8_t *)"AT+GMR\r\n", RET_OK | RET_ERROR);
        if (ret == RET_OK)
        {
          align_ptr = strstr((char *) Obj->CmdResp,"\r\n") + 2;
          token = strtok(align_ptr,"\r\n\r\n");
          strncpy((char *)Obj->FW_Rev, token, UG96_FW_REV_SIZE);
        }
        ret = AT_ExecuteCommand(Obj, UG96_TOUT_300, (uint8_t *)"AT+GSN\r\n", RET_OK | RET_ERROR);
        if (ret == RET_OK)
        {
          align_ptr = strstr((char *) Obj->CmdResp,"\r\n") + 2;
          token = strtok(align_ptr,"\r\n\r\n");
          strncpy((char *)Obj->Imei, token, UG96_FW_REV_SIZE);
        }
    }
    if(ret == RET_OK)
    {
      tickstart = Obj->GetTickCb();
      while((Obj->GetTickCb() - tickstart) < 2000)
      {
      }
      ret = AT_ExecuteCommand(Obj, UG96_TOUT_5000, (uint8_t *)"AT+CPIN?\r\n", RET_OK | RET_ERROR | RET_CME_ERROR);
      if (RET_OK == ret)
      {
        align_ptr = strstr((char *) Obj->CmdResp,"+CPIN: READY");
        if ( NULL != align_ptr )
        {
          Obj->SimInfo.SimStatus = UG96_SIM_READY;
          if (RET_OK == AT_ExecuteCommand(Obj, UG96_TOUT_300, (uint8_t *)"AT+CIMI\r\n", RET_OK | RET_ERROR | RET_CME_ERROR))
          {
            align_ptr = strstr((char *) Obj->CmdResp,"\r\n") + 2;
            token = strtok(align_ptr,"\r\n\r\n");
            strncpy((char *)Obj->SimInfo.IMSI, token, UG96_IMSI_SIZE);
          }
          if (RET_OK == AT_ExecuteCommand(Obj, UG96_TOUT_300, (uint8_t *)"AT+QCCID\r\n", RET_OK | RET_ERROR))
          {
            align_ptr = strstr((char *) Obj->CmdResp,"+QCCID:") + sizeof("+QCCID:");
            token = strtok(align_ptr,"\r\n\r\n");
            strncpy((char *)Obj->SimInfo.ICCID, token, UG96_ICCID_SIZE);
          }
          fret = UG96_INIT_RET_OK;
        }
      }
      else
      {
        if (RET_CME_ERROR == ret)
        {
          AT_RetrieveData(Obj, Obj->CmdResp, 0, RET_CRLF, UG96_TOUT_SHORT);
          Obj->SimInfo.SimStatus = (UG96_SIMState_t) ParseNumber((char *) Obj->CmdResp+1, &parse_count);
          fret = UG96_INIT_RET_SIM_ERR;
        }
      }
    }
#ifdef USE_BG96
    ret = AT_ExecuteCommand(Obj, UG96_TOUT_15000, (uint8_t *)"AT+CFUN=1\r\n", RET_OK | RET_ERROR | RET_CME_ERROR);
    if (RET_OK != ret)
    {
      fret = UG96_INIT_OTHER_ERR;
    }
#endif
  }
  else
  {
     fret = UG96_INIT_RET_IO_ERR;
  }
  return fret;
}
UG96_InitRet_t UG96_Init ( Ug96Object_t * Obj ) { UG96_InitRet_t fret = UG96_INIT_OTHER_ERR ; int32_t ret = RET_ERROR ; int8_t i ; char * align_ptr , * token ; uint8_t parse_count ; uint32_t tickstart ; Obj -> APsActive = 0 ; for ( i = 0 ; i < UG96_MAX_SOCKETS ; i ++ ) { Obj -> SocketInfo [ i ] . Type = UG96_TCP_CONNECTION ; Obj -> SocketInfo [ i ] . AccessMode = UG96_BUFFER_MODE ; Obj -> SocketInfo [ i ] . ComulatedQirdData = 0 ; Obj -> SocketInfo [ i ] . HaveReadLength = 0 ; Obj -> SocketInfo [ i ] . UnreadLength = 0 ; } Obj -> fops . IO_FlushBuffer ( ) ; if ( Obj -> fops . IO_Init ( ) == 0 ) { ret = AT_Synchro ( Obj ) ; if ( ret != RET_OK ) { printf ( " \n " ) ; fret = UG96_INIT_RET_AT_ERR ; } else { ret = UG96_ResetToFactoryDefault ( Obj ) ; ret = ret | UG96_GetUARTConfig ( Obj , & Obj -> UART_Config ) ; ret = ret | AT_ExecuteCommand ( Obj , UG96_TOUT_SHORT , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ; ret = ret | AT_ExecuteCommand ( Obj , UG96_TOUT_SHORT , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ; ret = ret | AT_ExecuteCommand ( Obj , UG96_TOUT_300 , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ; #ifndef USE_BG96 ret = ret | AT_ExecuteCommand ( Obj , UG96_TOUT_300 , ( uint8_t * ) " \" \" \" \" \r \n " , RET_OK | RET_ERROR ) ; #endif } if ( ret == RET_OK ) { ret = AT_ExecuteCommand ( Obj , UG96_TOUT_SHORT , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ; if ( ret == RET_OK ) { align_ptr = strstr ( ( char * ) Obj -> CmdResp , " \r \n " ) + 2 ; token = strtok ( align_ptr , " \r \n \r \n " ) ; strncpy ( ( char * ) Obj -> Manufacturer , token , UG96_MFC_SIZE ) ; } ret = AT_ExecuteCommand ( Obj , UG96_TOUT_SHORT , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ; if ( ret == RET_OK ) { align_ptr = strstr ( ( char * ) Obj -> CmdResp , " \r \n " ) + 2 ; token = strtok ( align_ptr , " \r \n \r \n " ) ; strncpy ( ( char * ) Obj -> ProductID , token , UG96_PROD_ID_SIZE ) ; } ret = AT_ExecuteCommand ( Obj , UG96_TOUT_SHORT , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ; if ( ret == RET_OK ) { align_ptr = strstr ( ( char * ) Obj -> CmdResp , " \r \n " ) + 2 ; token = strtok ( align_ptr , " \r \n \r \n " ) ; strncpy ( ( char * ) Obj -> FW_Rev , token , UG96_FW_REV_SIZE ) ; } ret = AT_ExecuteCommand ( Obj , UG96_TOUT_300 , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ; if ( ret == RET_OK ) { align_ptr = strstr ( ( char * ) Obj -> CmdResp , " \r \n " ) + 2 ; token = strtok ( align_ptr , " \r \n \r \n " ) ; strncpy ( ( char * ) Obj -> Imei , token , UG96_FW_REV_SIZE ) ; } } if ( ret == RET_OK ) { tickstart = Obj -> GetTickCb ( ) ; while ( ( Obj -> GetTickCb ( ) - tickstart ) < 2000 ) { } ret = AT_ExecuteCommand ( Obj , UG96_TOUT_5000 , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR | RET_CME_ERROR ) ; if ( RET_OK == ret ) { align_ptr = strstr ( ( char * ) Obj -> CmdResp , " " ) ; if ( NULL != align_ptr ) { Obj -> SimInfo . SimStatus = UG96_SIM_READY ; if ( RET_OK == AT_ExecuteCommand ( Obj , UG96_TOUT_300 , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR | RET_CME_ERROR ) ) { align_ptr = strstr ( ( char * ) Obj -> CmdResp , " \r \n " ) + 2 ; token = strtok ( align_ptr , " \r \n \r \n " ) ; strncpy ( ( char * ) Obj -> SimInfo . IMSI , token , UG96_IMSI_SIZE ) ; } if ( RET_OK == AT_ExecuteCommand ( Obj , UG96_TOUT_300 , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR ) ) { align_ptr = strstr ( ( char * ) Obj -> CmdResp , " " ) + sizeof ( " " ) ; token = strtok ( align_ptr , " \r \n \r \n " ) ; strncpy ( ( char * ) Obj -> SimInfo . ICCID , token , UG96_ICCID_SIZE ) ; } fret = UG96_INIT_RET_OK ; } } else { if ( RET_CME_ERROR == ret ) { AT_RetrieveData ( Obj , Obj -> CmdResp , 0 , RET_CRLF , UG96_TOUT_SHORT ) ; Obj -> SimInfo . SimStatus = ( UG96_SIMState_t ) ParseNumber ( ( char * ) Obj -> CmdResp + 1 , & parse_count ) ; fret = UG96_INIT_RET_SIM_ERR ; } } } #ifdef USE_BG96 ret = AT_ExecuteCommand ( Obj , UG96_TOUT_15000 , ( uint8_t * ) " \r \n " , RET_OK | RET_ERROR | RET_CME_ERROR ) ; if ( RET_OK != ret ) { fret = UG96_INIT_OTHER_ERR ; } #endif } else { fret = UG96_INIT_RET_IO_ERR ; } return fret ; }
@brief  Initialize UG96 module.
@brief Initialize UG96 module .
/* Flush Uart intermediate buffer */ /* configure and initialize UART */ /* if does not respond to AT command set specific return status */ /* Retrieve Quectel Factory Default values */ /* Retrieve Quectel UART baud rate and flow control*/ /* If not aligned to the UART of MCU (_io.h), already previous AT command will fail */ /* Use ATV1 to set the response format */ /* Use ATE1 to enable or  ATE0 to disable echo mode */ /* Use AT+CMEE=1 to enable result code and use "integer" values */ /* Use AT+QCFG to enable/disable URC on port UART1 */ /* retrieve module info */ /* Use AT+GSN to query the IMEI (International Mobile Equipment Identity) of module */ /* retrieve SIM info */ /* A tempo is required to get the SIM ready. Set to 2000 ms */ /* Set the radio ON with the full functionality in the modem */