2010-08-02 20:59:44 +02:00
|
|
|
/*
|
|
|
|
* Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
|
|
|
|
* This version only offers minimal wrapping of socket.c/socket.h
|
|
|
|
* Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/
|
|
|
|
*
|
|
|
|
* MIT License:
|
|
|
|
* Copyright (c) 2008 Bjoern Hartmann
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* bjoern@cs.stanford.edu 12/30/2008
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "w5100.h"
|
|
|
|
#include "socket.h"
|
2010-08-17 22:37:17 +02:00
|
|
|
#include "Ethernet.h"
|
|
|
|
#include "Udp.h"
|
2011-02-04 22:15:42 +01:00
|
|
|
#include "Dns.h"
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-01-01 22:42:23 +01:00
|
|
|
/* Constructor */
|
2011-08-30 22:27:31 +02:00
|
|
|
EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {}
|
2011-01-01 22:42:23 +01:00
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
/* Start EthernetUDP socket, listening at local port PORT */
|
|
|
|
uint8_t EthernetUDP::begin(uint16_t port) {
|
2010-12-28 16:16:42 +01:00
|
|
|
if (_sock != MAX_SOCK_NUM)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
|
|
|
uint8_t s = W5100.readSnSR(i);
|
|
|
|
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
|
|
|
|
_sock = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_sock == MAX_SOCK_NUM)
|
|
|
|
return 0;
|
|
|
|
|
2010-08-02 20:59:44 +02:00
|
|
|
_port = port;
|
2012-02-15 01:41:22 +01:00
|
|
|
_remaining = 0;
|
2010-08-02 20:59:44 +02:00
|
|
|
socket(_sock, SnMR::UDP, _port, 0);
|
2010-12-28 16:16:42 +01:00
|
|
|
|
|
|
|
return 1;
|
2010-08-02 20:59:44 +02:00
|
|
|
}
|
|
|
|
|
2012-02-15 01:41:22 +01:00
|
|
|
/* return number of bytes available in the current packet,
|
|
|
|
will return zero if parsePacket hasn't been called yet */
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::available() {
|
2012-02-15 01:41:22 +01:00
|
|
|
return _remaining;
|
2010-08-02 20:59:44 +02:00
|
|
|
}
|
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
/* Release any resources being used by this EthernetUDP instance */
|
|
|
|
void EthernetUDP::stop()
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
|
|
|
if (_sock == MAX_SOCK_NUM)
|
|
|
|
return;
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-03-24 04:28:33 +01:00
|
|
|
close(_sock);
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-03-24 04:28:33 +01:00
|
|
|
EthernetClass::_server_port[_sock] = 0;
|
|
|
|
_sock = MAX_SOCK_NUM;
|
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::beginPacket(const char *host, uint16_t port)
|
2011-02-04 22:15:42 +01:00
|
|
|
{
|
|
|
|
// Look up the host first
|
|
|
|
int ret = 0;
|
|
|
|
DNSClient dns;
|
|
|
|
IPAddress remote_addr;
|
|
|
|
|
|
|
|
dns.begin(Ethernet.dnsServerIP());
|
|
|
|
ret = dns.getHostByName(host, remote_addr);
|
|
|
|
if (ret == 1) {
|
|
|
|
return beginPacket(remote_addr, port);
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::beginPacket(IPAddress ip, uint16_t port)
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
|
|
|
_offset = 0;
|
2011-08-30 22:27:31 +02:00
|
|
|
return startUDP(_sock, rawIPAddress(ip), port);
|
2011-03-24 04:28:33 +01:00
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::endPacket()
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
|
|
|
return sendUDP(_sock);
|
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
size_t EthernetUDP::write(uint8_t byte)
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
2011-08-24 01:12:03 +02:00
|
|
|
return write(&byte, 1);
|
2011-03-24 04:28:33 +01:00
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
|
|
|
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
|
|
|
|
_offset += bytes_written;
|
2011-08-24 01:12:03 +02:00
|
|
|
return bytes_written;
|
2010-08-02 20:59:44 +02:00
|
|
|
}
|
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::parsePacket()
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
2012-02-15 01:41:22 +01:00
|
|
|
// discard any remaining bytes in the last packet
|
|
|
|
flush();
|
|
|
|
|
|
|
|
if (W5100.getRXReceivedSize(_sock) > 0)
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
2011-02-04 22:44:51 +01:00
|
|
|
//HACK - hand-parse the UDP packet using TCP recv method
|
|
|
|
uint8_t tmpBuf[8];
|
2012-02-15 01:41:22 +01:00
|
|
|
int ret =0;
|
2011-02-04 22:44:51 +01:00
|
|
|
//read 8 header bytes and get IP and port from it
|
|
|
|
ret = recv(_sock,tmpBuf,8);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
_remoteIP = tmpBuf;
|
|
|
|
_remotePort = tmpBuf[4];
|
|
|
|
_remotePort = (_remotePort << 8) + tmpBuf[5];
|
2012-02-15 01:41:22 +01:00
|
|
|
_remaining = tmpBuf[6];
|
|
|
|
_remaining = (_remaining << 8) + tmpBuf[7];
|
|
|
|
|
2011-02-04 22:44:51 +01:00
|
|
|
// When we get here, any remaining bytes are the data
|
2012-02-15 01:41:22 +01:00
|
|
|
ret = _remaining;
|
2011-02-04 22:44:51 +01:00
|
|
|
}
|
|
|
|
return ret;
|
2011-03-24 04:28:33 +01:00
|
|
|
}
|
2011-02-04 22:44:51 +01:00
|
|
|
// There aren't any packets available
|
|
|
|
return 0;
|
2010-09-17 16:17:01 +02:00
|
|
|
}
|
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::read()
|
2010-12-28 16:16:42 +01:00
|
|
|
{
|
2011-03-24 04:28:33 +01:00
|
|
|
uint8_t byte;
|
2012-02-15 01:41:22 +01:00
|
|
|
|
|
|
|
if ((_remaining > 0) && (recv(_sock, &byte, 1) > 0))
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
|
|
|
// We read things without any problems
|
2012-02-15 01:41:22 +01:00
|
|
|
_remaining--;
|
2011-03-24 04:28:33 +01:00
|
|
|
return byte;
|
|
|
|
}
|
2012-02-15 01:41:22 +01:00
|
|
|
|
2011-03-24 04:28:33 +01:00
|
|
|
// If we get here, there's no data available
|
|
|
|
return -1;
|
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::read(unsigned char* buffer, size_t len)
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
2012-02-15 01:41:22 +01:00
|
|
|
|
|
|
|
if (_remaining > 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
int got;
|
|
|
|
|
|
|
|
if (_remaining <= len)
|
|
|
|
{
|
|
|
|
// data should fit in the buffer
|
|
|
|
got = recv(_sock, buffer, _remaining);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// too much data for the buffer,
|
|
|
|
// grab as much as will fit
|
|
|
|
got = recv(_sock, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (got > 0)
|
|
|
|
{
|
|
|
|
_remaining -= got;
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
2011-03-24 04:28:33 +01:00
|
|
|
}
|
2012-02-15 01:41:22 +01:00
|
|
|
|
|
|
|
// If we get here, there's no data available or recv failed
|
|
|
|
return -1;
|
|
|
|
|
2011-03-24 04:28:33 +01:00
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
int EthernetUDP::peek()
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
|
|
|
uint8_t b;
|
2012-02-15 01:41:22 +01:00
|
|
|
// Unlike recv, peek doesn't check to see if there's any data available, so we must.
|
|
|
|
// If the user hasn't called parsePacket yet then return nothing otherwise they
|
|
|
|
// may get the UDP header
|
|
|
|
if (!_remaining)
|
2011-03-24 04:28:33 +01:00
|
|
|
return -1;
|
|
|
|
::peek(_sock, &b);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2011-08-30 22:27:31 +02:00
|
|
|
void EthernetUDP::flush()
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
2012-02-15 01:41:22 +01:00
|
|
|
// could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
|
|
|
|
// should only occur if recv fails after telling us the data is there, lets
|
|
|
|
// hope the w5100 always behaves :)
|
|
|
|
|
|
|
|
while (_remaining)
|
2011-03-24 04:28:33 +01:00
|
|
|
{
|
|
|
|
read();
|
|
|
|
}
|
2010-12-28 16:16:42 +01:00
|
|
|
}
|
2010-08-02 20:59:44 +02:00
|
|
|
|