Skip to content

Commit cfa28a9

Browse files
committed
add new arduino_uip + tweak variants for Netduino2Plus ENC28J60/SPI networking
1 parent 4f03687 commit cfa28a9

62 files changed

Lines changed: 11319 additions & 5 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

STM32F4/cores/maple/Client.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Client.h - Base class that provides Client
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef client_h
21+
#define client_h
22+
#include "Print.h"
23+
#include "Stream.h"
24+
#include "IPAddress.h"
25+
26+
class Client : public Stream {
27+
28+
public:
29+
virtual int connect(IPAddress ip, uint16_t port) =0;
30+
virtual int connect(const char *host, uint16_t port) =0;
31+
virtual size_t write(uint8_t) =0;
32+
virtual size_t write(const uint8_t *buf, size_t size) =0;
33+
virtual int available() = 0;
34+
virtual int read() = 0;
35+
virtual int read(uint8_t *buf, size_t size) = 0;
36+
virtual int peek() = 0;
37+
virtual void flush() = 0;
38+
virtual void stop() = 0;
39+
virtual uint8_t connected() = 0;
40+
virtual operator bool() = 0;
41+
protected:
42+
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
43+
};
44+
45+
#endif

STM32F4/cores/maple/IPAddress.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
IPAddress.cpp - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <Arduino.h>
21+
#include <IPAddress.h>
22+
#include <Print.h>
23+
24+
IPAddress::IPAddress()
25+
{
26+
_address.dword = 0;
27+
}
28+
29+
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
30+
{
31+
_address.bytes[0] = first_octet;
32+
_address.bytes[1] = second_octet;
33+
_address.bytes[2] = third_octet;
34+
_address.bytes[3] = fourth_octet;
35+
}
36+
37+
IPAddress::IPAddress(uint32_t address)
38+
{
39+
_address.dword = address;
40+
}
41+
42+
IPAddress::IPAddress(const uint8_t *address)
43+
{
44+
memcpy(_address.bytes, address, sizeof(_address.bytes));
45+
}
46+
47+
IPAddress& IPAddress::operator=(const uint8_t *address)
48+
{
49+
memcpy(_address.bytes, address, sizeof(_address.bytes));
50+
return *this;
51+
}
52+
53+
IPAddress& IPAddress::operator=(uint32_t address)
54+
{
55+
_address.dword = address;
56+
return *this;
57+
}
58+
59+
bool IPAddress::operator==(const uint8_t* addr) const
60+
{
61+
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
62+
}
63+
64+
size_t IPAddress::printTo(Print& p) const
65+
{
66+
size_t n = 0;
67+
for (int i =0; i < 3; i++)
68+
{
69+
n += p.print(_address.bytes[i], DEC);
70+
n += p.print('.');
71+
}
72+
n += p.print(_address.bytes[3], DEC);
73+
return n;
74+
}
75+
76+
char *IPAddress::toCharArray()
77+
{
78+
static char szRet[20];
79+
String str = String(_address.bytes[0]);
80+
str += ".";
81+
str += String(_address.bytes[1]);
82+
str += ".";
83+
str += String(_address.bytes[2]);
84+
str += ".";
85+
str += String(_address.bytes[3]);
86+
str.toCharArray(szRet, 20);
87+
return szRet;
88+
}
89+

STM32F4/cores/maple/IPAddress.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
IPAddress.h - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef IPAddress_h
21+
#define IPAddress_h
22+
23+
#include <stdint.h>
24+
#include <Printable.h>
25+
26+
// A class to make it easier to handle and pass around IP addresses
27+
28+
class IPAddress : public Printable {
29+
private:
30+
union {
31+
uint8_t bytes[4]; // IPv4 address
32+
uint32_t dword;
33+
} _address;
34+
35+
// Access the raw byte array containing the address. Because this returns a pointer
36+
// to the internal structure rather than a copy of the address this function should only
37+
// be used when you know that the usage of the returned uint8_t* will be transient and not
38+
// stored.
39+
uint8_t* raw_address() { return _address.bytes; };
40+
41+
public:
42+
// Constructors
43+
IPAddress();
44+
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
45+
IPAddress(uint32_t address);
46+
IPAddress(const uint8_t *address);
47+
48+
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
49+
// to a four-byte uint8_t array is expected
50+
operator uint32_t() const { return _address.dword; };
51+
bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
52+
bool operator==(const uint8_t* addr) const;
53+
54+
// Overloaded index operator to allow getting and setting individual octets of the address
55+
uint8_t operator[](int index) const { return _address.bytes[index]; };
56+
uint8_t& operator[](int index) { return _address.bytes[index]; };
57+
58+
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
59+
IPAddress& operator=(const uint8_t *address);
60+
IPAddress& operator=(uint32_t address);
61+
62+
virtual size_t printTo(Print& p) const;
63+
char * toCharArray();
64+
65+
friend class EthernetClass;
66+
friend class UDP;
67+
friend class Client;
68+
friend class Server;
69+
friend class DhcpClass;
70+
friend class DNSClient;
71+
};
72+
73+
const IPAddress INADDR_NONE(0,0,0,0);
74+
75+
76+
#endif

STM32F4/cores/maple/Printable.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Printable.h - Interface class that allows printing of complex types
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef Printable_h
21+
#define Printable_h
22+
23+
#include <stdlib.h>
24+
25+
class Print;
26+
27+
/** The Printable class provides a way for new classes to allow themselves to be printed.
28+
By deriving from Printable and implementing the printTo method, it will then be possible
29+
for users to print out instances of this class by passing them into the usual
30+
Print::print and Print::println methods.
31+
*/
32+
33+
class Printable
34+
{
35+
public:
36+
virtual size_t printTo(Print& p) const = 0;
37+
};
38+
39+
#endif
40+

STM32F4/cores/maple/Server.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Server.h - Base class that provides Server
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef server_h
21+
#define server_h
22+
23+
#include "Print.h"
24+
25+
class Server : public Print {
26+
public:
27+
virtual void begin() =0;
28+
};
29+
30+
#endif

STM32F4/cores/maple/Udp.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Udp.cpp: Library to send/receive UDP packets.
3+
*
4+
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
5+
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
6+
* might not happen often in practice, but in larger network topologies, a UDP
7+
* packet can be received out of sequence.
8+
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
9+
* aware of it. Again, this may not be a concern in practice on small local networks.
10+
* For more information, see http://www.cafeaulait.org/course/week12/35.html
11+
*
12+
* MIT License:
13+
* Copyright (c) 2008 Bjoern Hartmann
14+
* Permission is hereby granted, free of charge, to any person obtaining a copy
15+
* of this software and associated documentation files (the "Software"), to deal
16+
* in the Software without restriction, including without limitation the rights
17+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
* copies of the Software, and to permit persons to whom the Software is
19+
* furnished to do so, subject to the following conditions:
20+
*
21+
* The above copyright notice and this permission notice shall be included in
22+
* all copies or substantial portions of the Software.
23+
*
24+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30+
* THE SOFTWARE.
31+
*
32+
* bjoern@cs.stanford.edu 12/30/2008
33+
*/
34+
35+
#ifndef udp_h
36+
#define udp_h
37+
38+
#include <Stream.h>
39+
#include <IPAddress.h>
40+
41+
class UDP : public Stream {
42+
43+
public:
44+
virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
45+
virtual void stop() =0; // Finish with the UDP socket
46+
47+
// Sending UDP packets
48+
49+
// Start building up a packet to send to the remote host specific in ip and port
50+
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
51+
virtual int beginPacket(IPAddress ip, uint16_t port) =0;
52+
// Start building up a packet to send to the remote host specific in host and port
53+
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
54+
virtual int beginPacket(const char *host, uint16_t port) =0;
55+
// Finish off this packet and send it
56+
// Returns 1 if the packet was sent successfully, 0 if there was an error
57+
virtual int endPacket() =0;
58+
// Write a single byte into the packet
59+
virtual size_t write(uint8_t) =0;
60+
// Write size bytes from buffer into the packet
61+
virtual size_t write(const uint8_t *buffer, size_t size) =0;
62+
63+
// Start processing the next available incoming packet
64+
// Returns the size of the packet in bytes, or 0 if no packets are available
65+
virtual int parsePacket() =0;
66+
// Number of bytes remaining in the current packet
67+
virtual int available() =0;
68+
// Read a single byte from the current packet
69+
virtual int read() =0;
70+
// Read up to len bytes from the current packet and place them into buffer
71+
// Returns the number of bytes read, or 0 if none are available
72+
virtual int read(unsigned char* buffer, size_t len) =0;
73+
// Read up to len characters from the current packet and place them into buffer
74+
// Returns the number of characters read, or 0 if none are available
75+
virtual int read(char* buffer, size_t len) =0;
76+
// Return the next byte from the current packet without moving on to the next byte
77+
virtual int peek() =0;
78+
virtual void flush() =0; // Finish reading the current packet
79+
80+
// Return the IP address of the host who sent the current incoming packet
81+
virtual IPAddress remoteIP() =0;
82+
// Return the port of the host who sent the current incoming packet
83+
virtual uint16_t remotePort() =0;
84+
protected:
85+
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
86+
};
87+
88+
#endif

0 commit comments

Comments
 (0)