StdPJ/Core/Src/APP/BSP/ug96/ug96.c
/**
  * @brief  Configure and Start a Client connection.
  * @param  Obj: pointer to module handle
  * @param  ContextID : range is 1-20 (max three are connected simultaneously)
  * @param  conn: pointer to the connection structure
  * @retval Operation Status.
  */
@brief  Configure and Start a Client connection.
@param  Obj: pointer to module handle
@param  ContextID : range is 1-20 (max three are connected simultaneously)
@param  conn: pointer to the connection structure
@retval Operation Status.
@brief Configure and Start a Client connection . @param Obj : pointer to module handle @param ContextID : range is 1 - 20 ( max three are connected simultaneously ) @param conn : pointer to the connection structure @retval Operation Status .
UG96_Return_t  UG96_OpenClientConnection(Ug96Object_t *Obj, uint8_t ContextID, UG96_Conn_t *conn)
{
  UG96_Return_t ret = UG96_RETURN_ERROR;
  char  type_string[12];
  int16_t recv;
  int32_t cmdret, retr_errcode;
  Obj->fops.IO_FlushBuffer();
  Obj->SocketInfo[conn->ConnectID].Type = conn->Type;
  Obj->SocketInfo[conn->ConnectID].AccessMode = conn->AccessMode;
  Obj->SocketInfo[conn->ConnectID].ComulatedQirdData = 0;
  Obj->SocketInfo[conn->ConnectID].HaveReadLength = 0;
  Obj->SocketInfo[conn->ConnectID].UnreadLength = 0;
  if (conn->Type == UG96_TCP_CONNECTION)
  {
    strcpy(type_string, "TCP");
  }
  else if (conn->Type == UG96_UDP_CONNECTION)
  {
    strcpy(type_string, "UDP");
  }
  else if (conn->Type == UG96_TCP_LISTENER_CONNECTION)
  {
    strcpy(type_string, "TCP_LISTENER");
  }
  else if (conn->Type == UG96_UDP_SERVER_CONNECTION)
  {
    strcpy(type_string, "UDP_SERVER");
  }
#ifdef UG96_DBG
        printf("                UG96_OpenClientConnection() \n");
#endif
  snprintf(CmdString, UG96_CMD_SIZE, "AT+QIOPEN=%d,%d,\"%s\",\"%s\",%u,%u,%d\r\n", ContextID, conn->ConnectID, type_string, conn->Url, conn->RemotePort, conn->LocalPort, conn->AccessMode);
  cmdret = AT_ExecuteCommand(Obj, UG96_TOUT_150000/2, (uint8_t *)CmdString, RET_OPEN | RET_ERROR);
  if (cmdret == RET_OPEN)
  {
    recv = AT_RetrieveData(Obj, Obj->CmdResp, 6, RET_NONE, UG96_TOUT_SHORT);
    if (recv > 0)
    {
      retr_errcode = ParseNumber((char *) Obj->CmdResp+3, NULL);
      if (  retr_errcode != 0) 
      {
         recv = AT_RetrieveData(Obj, Obj->CmdResp, 2, RET_NONE, UG96_TOUT_SHORT);
#ifdef UG96_DBG
         printf("                UG96_OpenClientConnection() retr_errcode: %ld \n", retr_errcode);
#endif
      }
      else
      {
#ifdef UG96_DBG
        snprintf(CmdString, UG96_CMD_SIZE, "AT+QISTATE=1,%d\r\n", conn->ConnectID);
        cmdret = AT_ExecuteCommand(Obj, UG96_TOUT_300, (uint8_t *)CmdString, RET_OK | RET_ERROR);
#endif
        ret = UG96_RETURN_OK;
      }
    }
    else
    {
#ifdef UG96_DBG
        printf("                UG96_OpenClientConnection() missing part of the response  \n");
#endif
    }
  }
  return ret;
}
UG96_Return_t UG96_OpenClientConnection ( Ug96Object_t * Obj , uint8_t ContextID , UG96_Conn_t * conn ) { UG96_Return_t ret = UG96_RETURN_ERROR ; char type_string [ 12 ] ; int16_t recv ; int32_t cmdret , retr_errcode ; Obj -> fops . IO_FlushBuffer ( ) ; Obj -> SocketInfo [ conn -> ConnectID ] . Type = conn -> Type ; Obj -> SocketInfo [ conn -> ConnectID ] . AccessMode = conn -> AccessMode ; Obj -> SocketInfo [ conn -> ConnectID ] . ComulatedQirdData = 0 ; Obj -> SocketInfo [ conn -> ConnectID ] . HaveReadLength = 0 ; Obj -> SocketInfo [ conn -> ConnectID ] . UnreadLength = 0 ; if ( conn -> Type == UG96_TCP_CONNECTION ) { strcpy ( type_string , " " ) ; } else if ( conn -> Type == UG96_UDP_CONNECTION ) { strcpy ( type_string , " " ) ; } else if ( conn -> Type == UG96_TCP_LISTENER_CONNECTION ) { strcpy ( type_string , " " ) ; } else if ( conn -> Type == UG96_UDP_SERVER_CONNECTION ) { strcpy ( type_string , " " ) ; } #ifdef UG96_DBG printf ( " \n " ) ; #endif snprintf ( CmdString , UG96_CMD_SIZE , " \" \" \" \" \r \n " , ContextID , conn -> ConnectID , type_string , conn -> Url , conn -> RemotePort , conn -> LocalPort , conn -> AccessMode ) ; cmdret = AT_ExecuteCommand ( Obj , UG96_TOUT_150000 / 2 , ( uint8_t * ) CmdString , RET_OPEN | RET_ERROR ) ; if ( cmdret == RET_OPEN ) { recv = AT_RetrieveData ( Obj , Obj -> CmdResp , 6 , RET_NONE , UG96_TOUT_SHORT ) ; if ( recv > 0 ) { retr_errcode = ParseNumber ( ( char * ) Obj -> CmdResp + 3 , NULL ) ; if ( retr_errcode != 0 ) { recv = AT_RetrieveData ( Obj , Obj -> CmdResp , 2 , RET_NONE , UG96_TOUT_SHORT ) ; #ifdef UG96_DBG printf ( " \n " , retr_errcode ) ; #endif } else { #ifdef UG96_DBG snprintf ( CmdString , UG96_CMD_SIZE , " \r \n " , conn -> ConnectID ) ; cmdret = AT_ExecuteCommand ( Obj , UG96_TOUT_300 , ( uint8_t * ) CmdString , RET_OK | RET_ERROR ) ; #endif ret = UG96_RETURN_OK ; } } else { #ifdef UG96_DBG printf ( " \n " ) ; #endif } } return ret ; }
@brief  Configure and Start a Client connection.
@brief Configure and Start a Client connection .
/* The maximum timeout of TCP connect is 75 seconds, hence UG96_TOUT_150000/2 */ /* read the remaining 2 characters if error nr 3 chiphers*/