Skip to content

Commit 8a1ef61

Browse files
author
James Foster
committed
Start on Client/Server files needed to support Ethernet.
1 parent 6eee41a commit 8a1ef61

File tree

8 files changed

+3633
-10
lines changed

8 files changed

+3633
-10
lines changed

SampleProjects/TestSomething/test/ethernet.cpp

Lines changed: 3221 additions & 0 deletions
Large diffs are not rendered by default.

cpp/arduino/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

cpp/arduino/IPAddress.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
23+
IPAddress::IPAddress()
24+
{
25+
_address.dword = 0;
26+
}
27+
28+
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
29+
{
30+
_address.bytes[0] = first_octet;
31+
_address.bytes[1] = second_octet;
32+
_address.bytes[2] = third_octet;
33+
_address.bytes[3] = fourth_octet;
34+
}
35+
36+
IPAddress::IPAddress(uint32_t address)
37+
{
38+
_address.dword = address;
39+
}
40+
41+
IPAddress::IPAddress(const uint8_t *address)
42+
{
43+
memcpy(_address.bytes, address, sizeof(_address.bytes));
44+
}
45+
46+
bool IPAddress::fromString(const char *address)
47+
{
48+
uint16_t acc = 0; // Accumulator
49+
uint8_t dots = 0;
50+
51+
while (*address)
52+
{
53+
char c = *address++;
54+
if (c >= '0' && c <= '9')
55+
{
56+
acc = acc * 10 + (c - '0');
57+
if (acc > 255) {
58+
// Value out of [0..255] range
59+
return false;
60+
}
61+
}
62+
else if (c == '.')
63+
{
64+
if (dots == 3) {
65+
// Too much dots (there must be 3 dots)
66+
return false;
67+
}
68+
_address.bytes[dots++] = acc;
69+
acc = 0;
70+
}
71+
else
72+
{
73+
// Invalid char
74+
return false;
75+
}
76+
}
77+
78+
if (dots != 3) {
79+
// Too few dots (there must be 3 dots)
80+
return false;
81+
}
82+
_address.bytes[3] = acc;
83+
return true;
84+
}
85+
86+
IPAddress& IPAddress::operator=(const uint8_t *address)
87+
{
88+
memcpy(_address.bytes, address, sizeof(_address.bytes));
89+
return *this;
90+
}
91+
92+
IPAddress& IPAddress::operator=(uint32_t address)
93+
{
94+
_address.dword = address;
95+
return *this;
96+
}
97+
98+
bool IPAddress::operator==(const uint8_t* addr) const
99+
{
100+
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
101+
}
102+
103+
size_t IPAddress::printTo(Print& p) const
104+
{
105+
size_t n = 0;
106+
for (int i =0; i < 3; i++)
107+
{
108+
n += p.print(_address.bytes[i], DEC);
109+
n += p.print('.');
110+
}
111+
n += p.print(_address.bytes[3], DEC);
112+
return n;
113+
}

cpp/arduino/IPAddress.h

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

cpp/arduino/Print.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdio.h>
44
#include <avr/pgmspace.h>
55
#include "WString.h"
6+
#include "Printable.h"
67

78
#define DEC 10
89
#define HEX 16
@@ -14,20 +15,17 @@
1415

1516
class Print;
1617

17-
class Printable
18-
{
19-
public:
20-
virtual size_t printTo(Print& p) const = 0;
21-
};
22-
2318
class Print
2419
{
20+
private:
21+
int write_error;
22+
protected:
23+
void setWriteError(int err = 1) { write_error = err; }
2524
public:
26-
Print() {}
25+
Print() : write_error(0) {}
2726

28-
// Arduino's version of this is richer but until I see an actual error case I'm not sure how to mock
29-
int getWriteError() { return 0; }
30-
void clearWriteError() { }
27+
int getWriteError() { return write_error; }
28+
void clearWriteError() { setWriteError(0); }
3129
virtual int availableForWrite() { return 0; }
3230

3331
virtual size_t write(uint8_t) = 0;

cpp/arduino/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+

cpp/arduino/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

0 commit comments

Comments
 (0)