Skip to content

Commit da5ccf4

Browse files
committed
Merge remote-tracking branch 'amcewen/wifly_integration' into new-extension
2 parents eff3754 + a6093a8 commit da5ccf4

File tree

36 files changed

+333
-140
lines changed

36 files changed

+333
-140
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef client_h
2+
#define client_h
3+
#include "Print.h"
4+
#include "Stream.h"
5+
#include "IPAddress.h"
6+
7+
class Client : public Stream {
8+
9+
public:
10+
virtual int connect(IPAddress ip, uint16_t port) =0;
11+
virtual int connect(const char *host, uint16_t port) =0;
12+
virtual size_t write(uint8_t) =0;
13+
virtual size_t write(const char *str) =0;
14+
virtual size_t write(const uint8_t *buf, size_t size) =0;
15+
virtual int available() = 0;
16+
virtual int read() = 0;
17+
virtual int read(uint8_t *buf, size_t size) = 0;
18+
virtual int peek() = 0;
19+
virtual void flush() = 0;
20+
virtual void stop() = 0;
21+
virtual uint8_t connected() = 0;
22+
virtual operator bool() = 0;
23+
protected:
24+
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
25+
};
26+
27+
#endif

hardware/arduino/cores/arduino/Printable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
#ifndef Printable_h
2121
#define Printable_h
2222

23+
#include <new.h>
24+
2325
class Print;
2426

2527
/** The Printable class provides a way for new classes to allow themselves to be printed.
2628
By deriving from Printable and implementing the printTo method, it will then be possible
2729
for users to print out instances of this class by passing them into the usual
2830
Print::print and Print::println methods.
2931
*/
32+
3033
class Printable
3134
{
3235
public:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef server_h
2+
#define server_h
3+
4+
class Server {
5+
public:
6+
virtual void begin() =0;
7+
};
8+
9+
#endif

hardware/arduino/cores/arduino/Udp.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 a string of characters into the packet
61+
virtual size_t write(const char *str) =0;
62+
// Write size bytes from buffer into the packet
63+
virtual size_t write(const uint8_t *buffer, size_t size) =0;
64+
65+
// Start processing the next available incoming packet
66+
// Returns the size of the packet in bytes, or 0 if no packets are available
67+
virtual int parsePacket() =0;
68+
// Number of bytes remaining in the current packet
69+
virtual int available() =0;
70+
// Read a single byte from the current packet
71+
virtual int read() =0;
72+
// Read up to len bytes from the current packet and place them into buffer
73+
// Returns the number of bytes read, or 0 if none are available
74+
virtual int read(unsigned char* buffer, size_t len) =0;
75+
// Read up to len characters from the current packet and place them into buffer
76+
// Returns the number of characters read, or 0 if none are available
77+
virtual int read(char* buffer, size_t len) =0;
78+
// Return the next byte from the current packet without moving on to the next byte
79+
virtual int peek() =0;
80+
virtual void flush() =0; // Finish reading the current packet
81+
82+
// Return the IP address of the host who sent the current incoming packet
83+
virtual IPAddress remoteIP() =0;
84+
// Return the port of the host who sent the current incoming packet
85+
virtual uint16_t remotePort() =0;
86+
protected:
87+
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
88+
};
89+
90+
#endif
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <new.h>
2+
3+
void * operator new(size_t size)
4+
{
5+
return malloc(size);
6+
}
7+
8+
void operator delete(void * ptr)
9+
{
10+
free(ptr);
11+
}
12+
13+
int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
14+
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
15+
void __cxa_guard_abort (__guard *) {};
16+
17+
void __cxa_pure_virtual(void) {};
18+

hardware/arduino/cores/arduino/new.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Header to define new/delete operators as they aren't provided by avr-gcc by default
2+
Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453
3+
*/
4+
5+
#ifndef NEW_H
6+
#define NEW_H
7+
8+
#include <stdlib.h>
9+
10+
void * operator new(size_t size);
11+
void operator delete(void * ptr);
12+
13+
__extension__ typedef int __guard __attribute__((mode (__DI__)));
14+
15+
extern "C" int __cxa_guard_acquire(__guard *);
16+
extern "C" void __cxa_guard_release (__guard *);
17+
extern "C" void __cxa_guard_abort (__guard *);
18+
19+
extern "C" void __cxa_pure_virtual(void);
20+
21+
#endif
22+

libraries/Ethernet/Dhcp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,19 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
271271
case routersOnSubnet :
272272
opt_len = _dhcpUdpSocket.read();
273273
_dhcpUdpSocket.read(_dhcpGatewayIp, 4);
274+
for (int i = 0; i < opt_len-4; i++)
275+
{
276+
_dhcpUdpSocket.read();
277+
}
274278
break;
275279

276280
case dns :
277281
opt_len = _dhcpUdpSocket.read();
278282
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
283+
for (int i = 0; i < opt_len-4; i++)
284+
{
285+
_dhcpUdpSocket.read();
286+
}
279287
break;
280288

281289
case dhcpServerIdentifier :

libraries/Ethernet/Dhcp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#ifndef Dhcp_h
55
#define Dhcp_h
66

7-
#include "Udp.h"
7+
#include "EthernetUdp.h"
88

99
/* DHCP state machine. */
1010
#define STATE_DHCP_START 0
@@ -139,7 +139,7 @@ class DhcpClass {
139139
uint8_t _dhcpGatewayIp[4];
140140
uint8_t _dhcpDhcpServerIp[4];
141141
uint8_t _dhcpDnsServerIp[4];
142-
UDP _dhcpUdpSocket;
142+
EthernetUDP _dhcpUdpSocket;
143143

144144
void presend_DHCP();
145145
void send_DHCP_MESSAGE(uint8_t, uint16_t);

libraries/Ethernet/Dns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Released under Apache License, version 2.0
44

55
#include "w5100.h"
6-
#include "Udp.h"
6+
#include "EthernetUdp.h"
77
#include "util.h"
88

99
#include "Dns.h"

libraries/Ethernet/Dns.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef DNSClient_h
66
#define DNSClient_h
77

8-
#include <Udp.h>
8+
#include <EthernetUdp.h>
99

1010
class DNSClient
1111
{
@@ -35,7 +35,7 @@ class DNSClient
3535

3636
IPAddress iDNSServer;
3737
uint16_t iRequestId;
38-
UDP iUdp;
38+
EthernetUDP iUdp;
3939
};
4040

4141
#endif

libraries/Ethernet/Ethernet.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,37 @@ int EthernetClass::begin(uint8_t *mac_address)
3333
}
3434

3535
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
36+
{
37+
// Assume the DNS server will be the machine on the same network as the local IP
38+
// but with last octet being '1'
39+
IPAddress dns_server = local_ip;
40+
dns_server[3] = 1;
41+
begin(mac_address, local_ip, dns_server);
42+
}
43+
44+
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
3645
{
3746
// Assume the gateway will be the machine on the same network as the local IP
3847
// but with last octet being '1'
3948
IPAddress gateway = local_ip;
4049
gateway[3] = 1;
41-
begin(mac_address, local_ip, gateway);
50+
begin(mac_address, local_ip, dns_server, gateway);
4251
}
4352

44-
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway)
53+
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
4554
{
4655
IPAddress subnet(255, 255, 255, 0);
47-
begin(mac_address, local_ip, gateway, subnet);
56+
begin(mac_address, local_ip, dns_server, gateway, subnet);
4857
}
4958

50-
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress gateway, IPAddress subnet)
59+
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
5160
{
5261
W5100.init();
5362
W5100.setMACAddress(mac);
5463
W5100.setIPAddress(local_ip._address);
5564
W5100.setGatewayIp(gateway._address);
5665
W5100.setSubnetMask(subnet._address);
66+
_dnsServerAddress = dns_server;
5767
}
5868

5969
IPAddress EthernetClass::localIP()

libraries/Ethernet/Ethernet.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <inttypes.h>
55
//#include "w5100.h"
66
#include "IPAddress.h"
7-
#include "Client.h"
8-
#include "Server.h"
7+
#include "EthernetClient.h"
8+
#include "EthernetServer.h"
99

1010
#define MAX_SOCK_NUM 4
1111

@@ -20,16 +20,17 @@ class EthernetClass {
2020
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
2121
int begin(uint8_t *mac_address);
2222
void begin(uint8_t *mac_address, IPAddress local_ip);
23-
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway);
24-
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress gateway, IPAddress subnet);
23+
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
24+
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
25+
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
2526

2627
IPAddress localIP();
2728
IPAddress subnetMask();
2829
IPAddress gatewayIP();
2930
IPAddress dnsServerIP();
3031

31-
friend class Client;
32-
friend class Server;
32+
friend class EthernetClient;
33+
friend class EthernetServer;
3334
};
3435

3536
extern EthernetClass Ethernet;

0 commit comments

Comments
 (0)