Commit Iniziale, progetto funzionante caricato su box ETcontroller in
cantina
This commit is contained in:
54
WIZnetInterface/Socket/Endpoint.cpp
Normal file
54
WIZnetInterface/Socket/Endpoint.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#include "Socket.h"
|
||||
#include "Endpoint.h"
|
||||
|
||||
Endpoint::Endpoint() {
|
||||
reset_address();
|
||||
}
|
||||
Endpoint::~Endpoint() {}
|
||||
|
||||
void Endpoint::reset_address(void) {
|
||||
_ipAddress[0] = '\0';
|
||||
_port = 0;
|
||||
}
|
||||
|
||||
int Endpoint::set_address(const char* host, const int port) {
|
||||
//Resolve DNS address or populate hard-coded IP address
|
||||
WIZnet_Chip* eth = WIZnet_Chip::getInstance();
|
||||
if (eth == NULL) {
|
||||
error("Endpoint constructor error: no WIZnet chip instance available!\r\n");
|
||||
return -1;
|
||||
}
|
||||
uint32_t addr;
|
||||
if (!eth->gethostbyname(host, &addr)) {
|
||||
return -1;
|
||||
}
|
||||
snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
|
||||
_port = port;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* Endpoint::get_address() {
|
||||
return _ipAddress;
|
||||
}
|
||||
|
||||
int Endpoint::get_port() {
|
||||
return _port;
|
||||
}
|
||||
|
||||
65
WIZnetInterface/Socket/Endpoint.h
Normal file
65
WIZnetInterface/Socket/Endpoint.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef ENDPOINT_H
|
||||
#define ENDPOINT_H
|
||||
|
||||
#include "wiznet.h"
|
||||
|
||||
class UDPSocket;
|
||||
|
||||
/**
|
||||
IP Endpoint (address, port)
|
||||
*/
|
||||
class Endpoint {
|
||||
friend class UDPSocket;
|
||||
|
||||
public:
|
||||
/** IP Endpoint (address, port)
|
||||
*/
|
||||
Endpoint(void);
|
||||
|
||||
~Endpoint(void);
|
||||
|
||||
/** Reset the address of this endpoint
|
||||
*/
|
||||
void reset_address(void);
|
||||
|
||||
/** Set the address of this endpoint
|
||||
\param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS).
|
||||
\param port The endpoint port
|
||||
\return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
|
||||
*/
|
||||
int set_address(const char* host, const int port);
|
||||
|
||||
/** Get the IP address of this endpoint
|
||||
\return The IP address of this endpoint.
|
||||
*/
|
||||
char* get_address(void);
|
||||
|
||||
/** Get the port of this endpoint
|
||||
\return The port of this endpoint
|
||||
*/
|
||||
int get_port(void);
|
||||
|
||||
protected:
|
||||
char _ipAddress[16];
|
||||
int _port;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
49
WIZnetInterface/Socket/Socket.cpp
Normal file
49
WIZnetInterface/Socket/Socket.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "Socket.h"
|
||||
|
||||
Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500)
|
||||
{
|
||||
eth = WIZnet_Chip::getInstance();
|
||||
if (eth == NULL) {
|
||||
error("Socket constructor error: no W5500 instance available!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::set_blocking(bool blocking, unsigned int timeout)
|
||||
{
|
||||
_blocking = blocking;
|
||||
_timeout = timeout;
|
||||
}
|
||||
|
||||
int Socket::close()
|
||||
{
|
||||
// add this code refer from EthernetInterface.
|
||||
// update by Patrick Pollet
|
||||
int res;
|
||||
res = eth->close(_sock_fd);
|
||||
_sock_fd = -1;
|
||||
return (res)? 0: -1;
|
||||
}
|
||||
|
||||
Socket::~Socket()
|
||||
{
|
||||
close(); //Don't want to leak
|
||||
}
|
||||
|
||||
59
WIZnetInterface/Socket/Socket.h
Normal file
59
WIZnetInterface/Socket/Socket.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef SOCKET_H_
|
||||
#define SOCKET_H_
|
||||
|
||||
#include "wiznet.h"
|
||||
|
||||
#define htons(x) __REV16(x)
|
||||
#define ntohs(x) __REV16(x)
|
||||
#define htonl(x) __REV(x)
|
||||
#define ntohl(x) __REV(x)
|
||||
|
||||
/** Socket file descriptor and select wrapper
|
||||
*/
|
||||
class Socket {
|
||||
public:
|
||||
/** Socket
|
||||
*/
|
||||
Socket();
|
||||
|
||||
/** Set blocking or non-blocking mode of the socket and a timeout on
|
||||
blocking socket operations
|
||||
\param blocking true for blocking mode, false for non-blocking mode.
|
||||
\param timeout timeout in ms [Default: (1500)ms].
|
||||
*/
|
||||
void set_blocking(bool blocking, unsigned int timeout=1500);
|
||||
|
||||
/** Close the socket file descriptor
|
||||
*/
|
||||
int close();
|
||||
|
||||
~Socket();
|
||||
|
||||
protected:
|
||||
int _sock_fd;
|
||||
bool _blocking;
|
||||
int _timeout;
|
||||
|
||||
WIZnet_Chip* eth;
|
||||
};
|
||||
|
||||
|
||||
#endif /* SOCKET_H_ */
|
||||
|
||||
139
WIZnetInterface/Socket/TCPSocketConnection.cpp
Normal file
139
WIZnetInterface/Socket/TCPSocketConnection.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#include "TCPSocketConnection.h"
|
||||
#include <cstring>
|
||||
|
||||
using std::memset;
|
||||
using std::memcpy;
|
||||
|
||||
// not a big code.
|
||||
// refer from EthernetInterface by mbed official driver
|
||||
TCPSocketConnection::TCPSocketConnection() :
|
||||
_is_connected(false)
|
||||
{
|
||||
}
|
||||
|
||||
int TCPSocketConnection::connect(const char* host, const int port)
|
||||
{
|
||||
if (_sock_fd < 0) {
|
||||
_sock_fd = eth->new_socket();
|
||||
if (_sock_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (set_address(host, port) != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (!eth->connect(_sock_fd, get_address(), port)) {
|
||||
return -1;
|
||||
}
|
||||
set_blocking(false);
|
||||
// add code refer from EthernetInterface.
|
||||
_is_connected = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool TCPSocketConnection::is_connected(void)
|
||||
{
|
||||
// force update recent state.
|
||||
_is_connected = eth->is_connected(_sock_fd);
|
||||
return _is_connected;
|
||||
}
|
||||
|
||||
|
||||
bool TCPSocketConnection::is_fin_received(void)
|
||||
{
|
||||
return eth->is_fin_received(_sock_fd);
|
||||
}
|
||||
|
||||
int TCPSocketConnection::send(char* data, int length)
|
||||
{
|
||||
// add to cover exception.
|
||||
if ((_sock_fd < 0) || !_is_connected)
|
||||
return -1;
|
||||
|
||||
int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
|
||||
if (size < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (size > length) {
|
||||
size = length;
|
||||
}
|
||||
return eth->send(_sock_fd, data, size);
|
||||
}
|
||||
|
||||
// -1 if unsuccessful, else number of bytes written
|
||||
int TCPSocketConnection::send_all(char* data, int length)
|
||||
{
|
||||
|
||||
int writtenLen = 0;
|
||||
while (writtenLen < length) {
|
||||
int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
|
||||
if (size < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (size > (length-writtenLen)) {
|
||||
size = (length-writtenLen);
|
||||
}
|
||||
int ret = eth->send(_sock_fd, data + writtenLen, size);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
writtenLen += ret;
|
||||
}
|
||||
return writtenLen;
|
||||
}
|
||||
|
||||
// -1 if unsuccessful, else number of bytes received
|
||||
int TCPSocketConnection::receive(char* data, int length)
|
||||
{
|
||||
// add to cover exception.
|
||||
if ((_sock_fd < 0) || !_is_connected)
|
||||
return -1;
|
||||
|
||||
int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
|
||||
if (size < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (size > length) {
|
||||
size = length;
|
||||
}
|
||||
return eth->recv(_sock_fd, data, size);
|
||||
}
|
||||
|
||||
// -1 if unsuccessful, else number of bytes received
|
||||
int TCPSocketConnection::receive_all(char* data, int length)
|
||||
{
|
||||
int readLen = 0;
|
||||
while (readLen < length) {
|
||||
int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
|
||||
if (size <= 0) {
|
||||
break;
|
||||
}
|
||||
if (size > (length - readLen)) {
|
||||
size = length - readLen;
|
||||
}
|
||||
int ret = eth->recv(_sock_fd, data + readLen, size);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
readLen += ret;
|
||||
}
|
||||
return readLen;
|
||||
}
|
||||
|
||||
85
WIZnetInterface/Socket/TCPSocketConnection.h
Normal file
85
WIZnetInterface/Socket/TCPSocketConnection.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef TCPSOCKET_H
|
||||
#define TCPSOCKET_H
|
||||
|
||||
#include "Socket.h"
|
||||
#include "Endpoint.h"
|
||||
|
||||
/**
|
||||
TCP socket connection
|
||||
*/
|
||||
class TCPSocketConnection: public Socket, public Endpoint {
|
||||
friend class TCPSocketServer;
|
||||
|
||||
public:
|
||||
/** TCP socket connection
|
||||
*/
|
||||
TCPSocketConnection();
|
||||
|
||||
/** Connects this TCP socket to the server
|
||||
\param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
|
||||
\param port The host's port to connect to.
|
||||
\return 0 on success, -1 on failure.
|
||||
*/
|
||||
int connect(const char* host, const int port);
|
||||
|
||||
/** Check if the socket is connected
|
||||
\return true if connected, false otherwise.
|
||||
*/
|
||||
bool is_connected(void);
|
||||
|
||||
/** Check if FIN signal received.
|
||||
\return true if received, false otherwise.
|
||||
*/
|
||||
bool is_fin_received(void);
|
||||
/** Send data to the remote host.
|
||||
\param data The buffer to send to the host.
|
||||
\param length The length of the buffer to send.
|
||||
\return the number of written bytes on success (>=0) or -1 on failure
|
||||
*/
|
||||
int send(char* data, int length);
|
||||
|
||||
/** Send all the data to the remote host.
|
||||
\param data The buffer to send to the host.
|
||||
\param length The length of the buffer to send.
|
||||
\return the number of written bytes on success (>=0) or -1 on failure
|
||||
*/
|
||||
int send_all(char* data, int length);
|
||||
|
||||
/** Receive data from the remote host.
|
||||
\param data The buffer in which to store the data received from the host.
|
||||
\param length The maximum length of the buffer.
|
||||
\return the number of received bytes on success (>=0) or -1 on failure
|
||||
*/
|
||||
int receive(char* data, int length);
|
||||
|
||||
/** Receive all the data from the remote host.
|
||||
\param data The buffer in which to store the data received from the host.
|
||||
\param length The maximum length of the buffer.
|
||||
\return the number of received bytes on success (>=0) or -1 on failure
|
||||
*/
|
||||
int receive_all(char* data, int length);
|
||||
|
||||
private:
|
||||
bool _is_connected;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
99
WIZnetInterface/Socket/TCPSocketServer.cpp
Normal file
99
WIZnetInterface/Socket/TCPSocketServer.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "TCPSocketServer.h"
|
||||
|
||||
TCPSocketServer::TCPSocketServer() {}
|
||||
|
||||
// Server initialization
|
||||
int TCPSocketServer::bind(int port)
|
||||
{
|
||||
|
||||
if (_sock_fd < 0) {
|
||||
_sock_fd = eth->new_socket();
|
||||
if (_sock_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// set the listen_port for next connection.
|
||||
listen_port = port;
|
||||
// set TCP protocol
|
||||
eth->setProtocol(_sock_fd, TCP);
|
||||
// set local port
|
||||
eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
|
||||
// connect the network
|
||||
eth->scmd(_sock_fd, OPEN);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TCPSocketServer::listen(int backlog)
|
||||
{
|
||||
if (_sock_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (backlog != 1) {
|
||||
return -1;
|
||||
}
|
||||
eth->scmd(_sock_fd, LISTEN);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int TCPSocketServer::accept(TCPSocketConnection& connection)
|
||||
{
|
||||
if (_sock_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
Timer t;
|
||||
t.reset();
|
||||
t.start();
|
||||
while(1) {
|
||||
if (t.read_ms() > _timeout && _blocking == false) {
|
||||
return -1;
|
||||
}
|
||||
if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
|
||||
char host[16];
|
||||
snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
|
||||
uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
|
||||
// change this server socket to connection socket.
|
||||
connection._sock_fd = _sock_fd;
|
||||
connection._is_connected = true;
|
||||
connection.set_address(host, port);
|
||||
|
||||
// and then, for the next connection, server socket should be assigned new one.
|
||||
_sock_fd = -1; // want to assign new available _sock_fd.
|
||||
if(bind(listen_port) < 0) {
|
||||
// modified by Patrick Pollet
|
||||
error("No more socket for listening, bind error");
|
||||
return -1;
|
||||
} else {
|
||||
//return -1;
|
||||
if(listen(1) < 0) {
|
||||
// modified by Patrick Pollet
|
||||
error("No more socket for listening, listen error");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
57
WIZnetInterface/Socket/TCPSocketServer.h
Normal file
57
WIZnetInterface/Socket/TCPSocketServer.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef TCPSOCKETSERVER_H
|
||||
#define TCPSOCKETSERVER_H
|
||||
|
||||
#include "Socket.h"
|
||||
#include "TCPSocketConnection.h"
|
||||
|
||||
/** TCP Server.
|
||||
*/
|
||||
class TCPSocketServer : public Socket
|
||||
{
|
||||
public:
|
||||
/** Instantiate a TCP Server.
|
||||
*/
|
||||
TCPSocketServer();
|
||||
|
||||
/** Bind a socket to a specific port.
|
||||
\param port The port to listen for incoming connections on.
|
||||
\return 0 on success, -1 on failure.
|
||||
*/
|
||||
int bind(int port);
|
||||
|
||||
/** Start listening for incoming connections.
|
||||
\param backlog number of pending connections that can be queued up at any
|
||||
one time [Default: 1].
|
||||
\return 0 on success, -1 on failure.
|
||||
*/
|
||||
int listen(int backlog=1);
|
||||
|
||||
/** Accept a new connection.
|
||||
\param connection A TCPSocketConnection instance that will handle the incoming connection.
|
||||
\return 0 on success, -1 on failure.
|
||||
*/
|
||||
int accept(TCPSocketConnection& connection);
|
||||
|
||||
private :
|
||||
int listen_port;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
112
WIZnetInterface/Socket/UDPSocket.cpp
Normal file
112
WIZnetInterface/Socket/UDPSocket.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "UDPSocket.h"
|
||||
|
||||
static int udp_local_port;
|
||||
|
||||
UDPSocket::UDPSocket()
|
||||
{
|
||||
}
|
||||
|
||||
// After init function, bind() should be called.
|
||||
int UDPSocket::init(void)
|
||||
{
|
||||
if (_sock_fd < 0) {
|
||||
_sock_fd = eth->new_socket();
|
||||
}
|
||||
if (eth->setProtocol(_sock_fd, UDP) == false) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Server initialization
|
||||
int UDPSocket::bind(int port)
|
||||
{
|
||||
if (_sock_fd < 0) {
|
||||
_sock_fd = eth->new_socket();
|
||||
if (_sock_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// set local port
|
||||
if (port != 0) {
|
||||
eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
|
||||
} else {
|
||||
udp_local_port++;
|
||||
eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port);
|
||||
}
|
||||
// set udp protocol
|
||||
eth->setProtocol(_sock_fd, UDP);
|
||||
eth->scmd(_sock_fd, OPEN);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// -1 if unsuccessful, else number of bytes written
|
||||
int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
|
||||
{
|
||||
int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
|
||||
if (size < 0) {
|
||||
return -1;
|
||||
}
|
||||
confEndpoint(remote);
|
||||
int ret = eth->send(_sock_fd, packet, length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// -1 if unsuccessful, else number of bytes received
|
||||
int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
|
||||
{
|
||||
uint8_t info[8];
|
||||
int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
|
||||
if (size < 0) {
|
||||
return -1;
|
||||
}
|
||||
eth->recv(_sock_fd, (char*)info, sizeof(info));
|
||||
readEndpoint(remote, info);
|
||||
int udp_size = info[6]<<8|info[7];
|
||||
//TEST_ASSERT(udp_size <= (size-sizeof(info)));
|
||||
if (udp_size > (size-sizeof(info))) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Perform Length check here to prevent buffer overrun */
|
||||
/* fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/) */
|
||||
if (udp_size > length) {
|
||||
//printf("udp_size: %d\n",udp_size);
|
||||
return -1;
|
||||
}
|
||||
return eth->recv(_sock_fd, buffer, udp_size);
|
||||
}
|
||||
|
||||
void UDPSocket::confEndpoint(Endpoint & ep)
|
||||
{
|
||||
char * host = ep.get_address();
|
||||
// set remote host
|
||||
eth->sreg_ip(_sock_fd, Sn_DIPR, host);
|
||||
// set remote port
|
||||
eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
|
||||
}
|
||||
|
||||
void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
|
||||
{
|
||||
char addr[17];
|
||||
snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
|
||||
uint16_t port = info[4]<<8|info[5];
|
||||
ep.set_address(addr, port);
|
||||
}
|
||||
|
||||
69
WIZnetInterface/Socket/UDPSocket.h
Normal file
69
WIZnetInterface/Socket/UDPSocket.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/* Copyright (C) 2012 mbed.org, MIT License
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef UDPSOCKET_H
|
||||
#define UDPSOCKET_H
|
||||
|
||||
#include "Endpoint.h"
|
||||
#include "Socket.h"
|
||||
|
||||
/**
|
||||
UDP Socket
|
||||
*/
|
||||
class UDPSocket: public Socket {
|
||||
|
||||
public:
|
||||
/** Instantiate an UDP Socket.
|
||||
*/
|
||||
UDPSocket();
|
||||
|
||||
/** Init the UDP Client Socket without binding it to any specific port
|
||||
\return 0 on success, -1 on failure.
|
||||
*/
|
||||
int init(void);
|
||||
|
||||
/** Bind a UDP Server Socket to a specific port
|
||||
\param port The port to listen for incoming connections on
|
||||
\return 0 on success, -1 on failure.
|
||||
*/
|
||||
int bind(int port = -1);
|
||||
|
||||
/** Send a packet to a remote endpoint
|
||||
\param remote The remote endpoint
|
||||
\param packet The packet to be sent
|
||||
\param length The length of the packet to be sent
|
||||
\return the number of written bytes on success (>=0) or -1 on failure
|
||||
*/
|
||||
int sendTo(Endpoint &remote, char *packet, int length);
|
||||
|
||||
/** Receive a packet from a remote endpoint
|
||||
\param remote The remote endpoint
|
||||
\param buffer The buffer for storing the incoming packet data. If a packet
|
||||
is too long to fit in the supplied buffer, excess bytes are discarded
|
||||
\param length The length of the buffer
|
||||
\return the number of received bytes on success (>=0) or -1 on failure
|
||||
*/
|
||||
int receiveFrom(Endpoint &remote, char *buffer, int length);
|
||||
|
||||
private:
|
||||
void confEndpoint(Endpoint & ep);
|
||||
void readEndpoint(Endpoint & ep, uint8_t info[]);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user