sdk/modules/lte/altcom/api/socket/altcom_sendto.c
/****************************************************************************
 * Name: altcom_sendto
 *
 * Description:
 *   If altcom_sendto() is used on a connection-mode (ALTCOM_SOCK_STREAM)
 *   socket, the parameters to and 'tolen' are ignored (and the error EISCONN
 *   may be returned when they are not NULL and 0), and the error ENOTCONN is
 *   returned when the socket was not actually connected.
 *
 * Parameters:
 *   sockfd   Socket descriptor of socket
 *   buf      Data to send
 *   len      Length of data to send
 *   flags    Send flags
 *   to       Address of recipient
 *   tolen    The length of the address structure
 *
 * Returned Value:
 *   On success, returns the number of characters sent. On error,
 *   -1 is returned, and errno is set appropriately.
 *
 ****************************************************************************/
altcom_sendto
Description:
If altcom_sendto() is used on a connection-mode (ALTCOM_SOCK_STREAM)
socket, the parameters to and 'tolen' are ignored (and the error EISCONN
may be returned when they are not NULL and 0), and the error ENOTCONN is
returned when the socket was not actually connected.

sockfd   Socket descriptor of socket
buf      Data to send
len      Length of data to send
flags    Send flags
to       Address of recipient
tolen    The length of the address structure

Returned Value:
On success, returns the number of characters sent. On error,
-1 is returned, and errno is set appropriately.
altcom_sendto Description : If altcom_sendto () is used on a connection - mode ( ALTCOM_SOCK_STREAM ) socket the parameters to and ' tolen ' are ignored ( and the error EISCONN may be returned when they are not NULL and 0 ) and the error ENOTCONN is returned when the socket was not actually connected . sockfd Socket descriptor of socket buf Data to send len Length of data to send flags Send flags to Address of recipient tolen The length of the address structure Returned Value : On success returns the number of characters sent . On error - 1 is returned and errno is set appropriately .
int altcom_sendto(int sockfd, const void *buf, size_t len, int flags,
                  const struct altcom_sockaddr *to, altcom_socklen_t tolen)
{
  int32_t                     ret;
  int32_t                     result;
  struct altcom_fd_set_s      writeset;
  FAR struct altcom_socket_s  *fsock;
  struct sendto_req_s         req;
  FAR struct altcom_timeval   *sendtimeo;
  if (!altcom_isinit())
    {
      DBGIF_LOG_ERROR("Not intialized\n");
      altcom_seterrno(ALTCOM_ENETDOWN);
      return -1;
    }
  fsock = altcom_sockfd_socket(sockfd);
  if (!fsock)
    {
      altcom_seterrno(ALTCOM_EINVAL);
      return -1;
    }
  if (len > APICMD_SENDTO_SENDDATA_LENGTH)
    {
      DBGIF_LOG2_WARNING("Truncate send length:%d -> %d.\n", len, APICMD_SENDTO_SENDDATA_LENGTH);
      len = APICMD_SENDTO_SENDDATA_LENGTH;
    }
  if (!buf)
    {
      DBGIF_LOG_ERROR("buf is NULL\n");
      altcom_seterrno(ALTCOM_EINVAL);
      return -1;
    }
  if (to && (!tolen))
    {
      DBGIF_LOG_ERROR("tolen is NULL\n");
      altcom_seterrno(ALTCOM_EINVAL);
      return -1;
    }
  req.sockfd = sockfd;
  req.buf    = buf;
  req.len    = len;
  req.flags  = flags;
  req.to     = to;
  req.tolen  = tolen;
  if (fsock->flags & ALTCOM_O_NONBLOCK)
    {
      ALTCOM_FD_ZERO(&writeset);
      ALTCOM_FD_SET(sockfd, &writeset);
      ret = altcom_select_nonblock((sockfd + 1), NULL, &writeset, NULL);
      if (ret > 0)
        {
          if (!ALTCOM_FD_ISSET(sockfd, &writeset))
            {
              altcom_seterrno(ALTCOM_EFAULT);
              DBGIF_LOG1_ERROR("select failed: %d\n", altcom_errno());
              return -1;
            }
          result = sendto_request(fsock, &req);
          if (result == SENDTO_REQ_FAILURE)
            {
              return -1;
            }
        }
      else
        {
          if (ret == 0)
            {
              altcom_seterrno(ALTCOM_EAGAIN);
            }
          else
            {
              DBGIF_LOG1_ERROR("select failed: %d\n", altcom_errno());
            }
          return -1;
        }
    }
  else
    {
      ALTCOM_FD_ZERO(&writeset);
      ALTCOM_FD_SET(sockfd, &writeset);
      sendtimeo = &fsock->sendtimeo;
      if ((fsock->sendtimeo.tv_sec == 0) && (fsock->sendtimeo.tv_usec == 0))
      {
        sendtimeo = NULL;
      }
      ret = altcom_select_block((sockfd + 1), NULL, &writeset, NULL, sendtimeo);
      if (ret <= 0)
        {
          if (ret == 0)
            {
              altcom_seterrno(ALTCOM_EFAULT);
            }
          if (altcom_errno() == ALTCOM_ETIMEDOUT)
            {
              altcom_seterrno(ALTCOM_EAGAIN);
            }
          DBGIF_LOG1_ERROR("select failed: %d\n", altcom_errno());
          return -1;
        }
      if (!ALTCOM_FD_ISSET(sockfd, &writeset))
        {
          altcom_seterrno(ALTCOM_EFAULT);
          DBGIF_LOG1_ERROR("select failed: %d\n", altcom_errno());
          return -1;
        }
      result = sendto_request(fsock, &req);
      if (result == SENDTO_REQ_FAILURE)
       {
         return -1;
       }
    }
  return result;
}
int altcom_sendto ( int sockfd , const void * buf , size_t len , int flags , const struct altcom_sockaddr * to , altcom_socklen_t tolen ) { int32_t ret ; int32_t result ; struct altcom_fd_set_s writeset ; FAR struct altcom_socket_s * fsock ; struct sendto_req_s req ; FAR struct altcom_timeval * sendtimeo ; if ( ! altcom_isinit ( ) ) { DBGIF_LOG_ERROR ( " \n " ) ; altcom_seterrno ( ALTCOM_ENETDOWN ) ; return -1 ; } fsock = altcom_sockfd_socket ( sockfd ) ; if ( ! fsock ) { altcom_seterrno ( ALTCOM_EINVAL ) ; return -1 ; } if ( len > APICMD_SENDTO_SENDDATA_LENGTH ) { DBGIF_LOG2_WARNING ( " \n " , len , APICMD_SENDTO_SENDDATA_LENGTH ) ; len = APICMD_SENDTO_SENDDATA_LENGTH ; } if ( ! buf ) { DBGIF_LOG_ERROR ( " \n " ) ; altcom_seterrno ( ALTCOM_EINVAL ) ; return -1 ; } if ( to && ( ! tolen ) ) { DBGIF_LOG_ERROR ( " \n " ) ; altcom_seterrno ( ALTCOM_EINVAL ) ; return -1 ; } req . sockfd = sockfd ; req . buf = buf ; req . len = len ; req . flags = flags ; req . to = to ; req . tolen = tolen ; if ( fsock -> flags & ALTCOM_O_NONBLOCK ) { ALTCOM_FD_ZERO ( & writeset ) ; ALTCOM_FD_SET ( sockfd , & writeset ) ; ret = altcom_select_nonblock ( ( sockfd + 1 ) , NULL , & writeset , NULL ) ; if ( ret > 0 ) { if ( ! ALTCOM_FD_ISSET ( sockfd , & writeset ) ) { altcom_seterrno ( ALTCOM_EFAULT ) ; DBGIF_LOG1_ERROR ( " \n " , altcom_errno ( ) ) ; return -1 ; } result = sendto_request ( fsock , & req ) ; if ( result == SENDTO_REQ_FAILURE ) { return -1 ; } } else { if ( ret == 0 ) { altcom_seterrno ( ALTCOM_EAGAIN ) ; } else { DBGIF_LOG1_ERROR ( " \n " , altcom_errno ( ) ) ; } return -1 ; } } else { ALTCOM_FD_ZERO ( & writeset ) ; ALTCOM_FD_SET ( sockfd , & writeset ) ; sendtimeo = & fsock -> sendtimeo ; if ( ( fsock -> sendtimeo . tv_sec == 0 ) && ( fsock -> sendtimeo . tv_usec == 0 ) ) { sendtimeo = NULL ; } ret = altcom_select_block ( ( sockfd + 1 ) , NULL , & writeset , NULL , sendtimeo ) ; if ( ret <= 0 ) { if ( ret == 0 ) { altcom_seterrno ( ALTCOM_EFAULT ) ; } if ( altcom_errno ( ) == ALTCOM_ETIMEDOUT ) { altcom_seterrno ( ALTCOM_EAGAIN ) ; } DBGIF_LOG1_ERROR ( " \n " , altcom_errno ( ) ) ; return -1 ; } if ( ! ALTCOM_FD_ISSET ( sockfd , & writeset ) ) { altcom_seterrno ( ALTCOM_EFAULT ) ; DBGIF_LOG1_ERROR ( " \n " , altcom_errno ( ) ) ; return -1 ; } result = sendto_request ( fsock , & req ) ; if ( result == SENDTO_REQ_FAILURE ) { return -1 ; } } return result ; }
Name: altcom_sendto
 Description:
   If altcom_sendto() is used on a connection-mode (ALTCOM_SOCK_STREAM)
   socket, the parameters to and 'tolen' are ignored (and the error EISCONN
   may be returned when they are not NULL and 0), and the error ENOTCONN is
   returned when the socket was not actually connected.
Name : altcom_sendto Description : If altcom_sendto () is used on a connection - mode ( ALTCOM_SOCK_STREAM ) socket the parameters to and ' tolen ' are ignored ( and the error EISCONN may be returned when they are not NULL and 0 ) and the error ENOTCONN is returned when the socket was not actually connected .
/* Check length of data to send */ /* Truncate the length to the maximum transfer size */ /* Check send buffer is available */ /* Send sendto request */ /* Wait until send buffer is available */