Go Down

Topic: WiFi101 UDP endPacket() Return Code (Read 984 times) previous topic - next topic

mlitke

Hmm, looks like commit c78dce64281aaf484b679a3afdb6c0f9d1aa63f2 may have broke checking the return code on a UDP socket endPacket() call.

From WiFiUdp.cpp:
Code: [Select]

...
#include "utility/WiFiSocket.h"
...
int WiFiUDP::endPacket()
{
   struct sockaddr_in addr;

   addr.sin_family = AF_INET;
   addr.sin_port = _htons(_sndPort);
   addr.sin_addr.s_addr = _sndIP;

   int result = WiFiSocket.sendto(_socket, (void *)_sndBuffer, _sndSize, 0, (struct sockaddr *)&addr, sizeof(addr));

   return (result <= 0) ? 0 : 1;
}
...


in utility/WiFiSocket.cpp:
Code: [Select]

...
sint16 WiFiSocketClass::sendto(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags, struct sockaddr *pstrDestAddr, uint8 u8AddrLen)
{
   m2m_wifi_handle_events(NULL);

   if (_info[sock].state != SOCKET_STATE_BOUND) {
      return 0;
   }

   return sendtoSocket(sock, pvSendBuffer, u16SendLength, flags, pstrDestAddr, u8AddrLen);
}
...


in socket/source/socket.c:
Code: [Select]

...
#ifdef ARDUINO
sint16 sendtoSocket(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags, struct sockaddr *pstrDestAddr, uint8 u8AddrLen)
#else
sint16 sendto(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags, struct sockaddr *pstrDestAddr, uint8 u8AddrLen)
#endif
{
#ifdef ARDUINO
   // Silence "unused" warning
   (void)flags;
   (void)u8AddrLen;
#endif
   sint16   s16Ret = SOCK_ERR_INVALID_ARG;

   if((sock >= 0) && (pvSendBuffer != NULL) && (u16SendLength <= SOCKET_BUFFER_MAX_LENGTH) && (gastrSockets[sock].bIsUsed == 1))
   {
      if(gastrSockets[sock].bIsUsed)
      {
         tstrSendCmd strSendTo;

         m2m_memset((uint8*)&strSendTo, 0, sizeof(tstrSendCmd));

         strSendTo.sock       = sock;
         strSendTo.u16DataSize   = NM_BSP_B_L_16(u16SendLength);
         strSendTo.u16SessionID  = gastrSockets[sock].u16SessionID;

         if(pstrDestAddr != NULL)
         {
            struct sockaddr_in   *pstrAddr;
            pstrAddr = (void*)pstrDestAddr;

            strSendTo.strAddr.u16Family   = pstrAddr->sin_family;
            strSendTo.strAddr.u16Port  = pstrAddr->sin_port;
            strSendTo.strAddr.u32IPAddr   = pstrAddr->sin_addr.s_addr;
         }
         s16Ret = SOCKET_REQUEST(SOCKET_CMD_SENDTO|M2M_REQ_DATA_PKT, (uint8*)&strSendTo,  sizeof(tstrSendCmd),
            pvSendBuffer, u16SendLength, UDP_TX_PACKET_OFFSET);

         if(s16Ret != SOCK_ERR_NO_ERROR)
         {
            s16Ret = SOCK_ERR_BUFFER_FULL;
         }
      }
   }
   return s16Ret;
}
...


So, where there is no error, it returns SOCK_ERR_NO_ERROR, which is defined as 0 in socket/include/socket.h

Why did the commit change the line in WiFiUdp.cpp from:
return (result < 0) ? 0 : 1;
to:
return (result <= 0) ? 0 : 1;

Seems like it was correct before the commit.

sandeepmistry

Hi @mlitke,

Could you please open an issue or pull request on Github to track this: https://github.com/arduino-libraries/WiFi101

Thanks.

mlitke

I believe I just made my first github pull request! :) Let me know if I messed anything up.

sandeepmistry

Nope, it was perfect. The change is part of the new WiFi101 v0.15.1 release. Thanks for the contribution :)

Go Up