WiFi UDP
#include <WiFiUDP.h>
begin available stop beginPacket
endPacket write parsePacket read
peek flush
/*H*******************************************************
  WiFiUdp.cpp - Library for Arduino WiFi shield.
  Copyright (c) 2011-2014 Arduino LLC.  All right reserved.
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
********************************************************/

extern "C" {
  #include "utility/debug.h"
  #include "utility/wifi_spi.h"
}
#include 
#include "utility/server_drv.h"
#include "utility/wifi_drv.h"

#include "WiFi.h"
#include "WiFiUdp.h"
#include "WiFiClient.h"
#include "WiFiServer.h"


/* Constructor */
WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {}



/*F******************************************************************** * START WiFiUDP SOCKET, LISTENING AT LOCAL PORT PORT **********************************************************************/ uint8_t WiFiUDP:: begin( uint16_t port ) { uint8_t sock = WiFiClass::getSocket(); if( sock != NO_SOCKET_AVAIL ) { ServerDrv::startServer( port, sock, UDP_MODE ); WiFiClass::_server_port[sock] = port; _sock = sock; _port = port; return 1; } return 0; }
/*F******************************************************************** * RETURN NUMBER OF BYTES AVAILABLE IN THE CURRENT PACKET, WILL RETURN ZERO IF PARSEpACKET HASN'T BEEN CALLED YET **********************************************************************/ int WiFiUDP:: available() { if( _sock != NO_SOCKET_AVAIL ) { return ServerDrv::availData( _sock ); } return 0; }
/*F******************************************************************** * RELEASE ANY RESOURCES BEING USED BY THIS WiFiUDP INSTANCE **********************************************************************/ void WiFiUDP:: stop() { if( _sock == NO_SOCKET_AVAIL) return; ServerDrv::stopClient(_sock); _sock = NO_SOCKET_AVAIL; }
/*F******************************************************************** * LOOK UP THE HOST FIRST **********************************************************************/ int WiFiUDP:: beginPacket( const char *host, uint16_t port ) { int ret = 0; IPAddress remote_addr; if( WiFi.hostByName(host, remote_addr)) { return beginPacket(remote_addr, port); } return ret; }
/*F******************************************************************** * **********************************************************************/ int WiFiUDP:: beginPacket( IPAddress ip, uint16_t port ) { if( _sock == NO_SOCKET_AVAIL) _sock = WiFiClass::getSocket(); if( _sock != NO_SOCKET_AVAIL) { ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE); WiFiClass::_state[_sock] = _sock; return 1; } return 0; }
/*F******************************************************************** * **********************************************************************/ int WiFiUDP:: endPacket() { return ServerDrv::sendUdpData(_sock); }
/*F******************************************************************** * **********************************************************************/ size_t WiFiUDP:: write( uint8_t byte ) { return write(&byte, 1); } size_t WiFiUDP::write(const uint8_t *buffer, size_t size) { ServerDrv::insertDataBuf(_sock, buffer, size); return size; }
/*F******************************************************************** * **********************************************************************/ int WiFiUDP:: parsePacket() { return available(); }
/*F******************************************************************** * **********************************************************************/ int WiFiUDP:: read() { uint8_t b; if( available()) { ServerDrv::getData( _sock, &b ); return b; }else{ return -1; } }
/*F******************************************************************** * **********************************************************************/ int WiFiUDP:: read( unsigned char *buffer, size_t len ) { if( available()) { uint16_t size = 0; if( !ServerDrv::getDataBuf(_sock, buffer, &size)) return -1; // TODO check if the buffer is too small in respect to buffer size return size; }else{ return -1; } }
/*F******************************************************************** * **********************************************************************/ int WiFiUDP:: peek() { uint8_t b; if( !available()) return -1; ServerDrv::getData(_sock, &b, 1); return b; }
/*F******************************************************************** * **********************************************************************/ void WiFiUDP:: flush() { // TODO: a real check to ensure transmission has been completed }
/*F******************************************************************** * **********************************************************************/ IPAddress WiFiUDP:: remoteIP() { uint8_t _remoteIp[4] = { 0 }; uint8_t _remotePort[2] = { 0 }; WiFiDrv::getRemoteData( _sock, _remoteIp, _remotePort ); IPAddress ip( _remoteIp ); return ip; }
/*F******************************************************************** * **********************************************************************/ uint16_t WiFiUDP:: remotePort() { uint8_t _remoteIp[4] = { 0 }; uint8_t _remotePort[2] = { 0 }; WiFiDrv::getRemoteData( _sock, _remoteIp, _remotePort ); uint16_t port = ( _remotePort[0] << 8 ) + _remotePort[1]; return port; }