Skip to content

Commit 1fd9c34

Browse files
committed
Merge branch 'new-extension' of github.com:arduino/Arduino into new-extension
2 parents 9ca5d60 + ff8190a commit 1fd9c34

File tree

15 files changed

+356
-6
lines changed

15 files changed

+356
-6
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/HardwareSerial.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
8888
#else
8989
void serialEvent() __attribute__((weak));
9090
void serialEvent() {}
91+
volatile static unsigned char serialEvent_flag = 0;
92+
#define serialEvent_implemented
9193
#if defined(USART_RX_vect)
9294
SIGNAL(USART_RX_vect)
9395
#elif defined(SIG_USART0_RECV)
@@ -108,18 +110,20 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
108110
#error UDR not defined
109111
#endif
110112
store_char(c, &rx_buffer);
111-
serialEvent();
113+
serialEvent_flag = 1;
112114
}
113115
#endif
114116

115117
#if defined(USART1_RX_vect)
116118
void serialEvent1() __attribute__((weak));
117119
void serialEvent1() {}
120+
volatile static unsigned char serialEvent1_flag = 0;
121+
#define serialEvent1_implemented
118122
SIGNAL(USART1_RX_vect)
119123
{
120124
unsigned char c = UDR1;
121125
store_char(c, &rx_buffer1);
122-
serialEvent1();
126+
serialEvent1_flag = 1;
123127
}
124128
#elif defined(SIG_USART1_RECV)
125129
#error SIG_USART1_RECV
@@ -128,11 +132,13 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
128132
#if defined(USART2_RX_vect) && defined(UDR2)
129133
void serialEvent2() __attribute__((weak));
130134
void serialEvent2() {}
135+
volatile static unsigned char serialEvent2_flag = 0;
136+
#define serialEvent2_implemented
131137
SIGNAL(USART2_RX_vect)
132138
{
133139
unsigned char c = UDR2;
134140
store_char(c, &rx_buffer2);
135-
serialEvent2();
141+
serialEvent2_flag = 1;
136142
}
137143
#elif defined(SIG_USART2_RECV)
138144
#error SIG_USART2_RECV
@@ -141,16 +147,55 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
141147
#if defined(USART3_RX_vect) && defined(UDR3)
142148
void serialEvent3() __attribute__((weak));
143149
void serialEvent3() {}
150+
volatile static unsigned char serialEvent3_flag = 0;
151+
#define serialEvent3_implemented
144152
SIGNAL(USART3_RX_vect)
145153
{
146154
unsigned char c = UDR3;
147155
store_char(c, &rx_buffer3);
148-
serialEvent3();
156+
serialEvent3_flag = 1;
149157
}
150158
#elif defined(SIG_USART3_RECV)
151159
#error SIG_USART3_RECV
152160
#endif
153161

162+
void serialEventRun(void)
163+
{
164+
unsigned char flag, oldSREG;
165+
#ifdef serialEvent_implemented
166+
oldSREG = SREG;
167+
noInterrupts();
168+
flag = serialEvent_flag;
169+
serialEvent_flag = 0;
170+
SREG = oldSREG;
171+
if (flag) serialEvent();
172+
#endif
173+
#ifdef serialEvent1_implemented
174+
oldSREG = SREG;
175+
noInterrupts();
176+
flag = serialEvent1_flag;
177+
serialEvent1_flag = 0;
178+
SREG = oldSREG;
179+
if (flag) serialEvent1();
180+
#endif
181+
#ifdef serialEvent2_implemented
182+
oldSREG = SREG;
183+
noInterrupts();
184+
flag = serialEvent2_flag;
185+
serialEvent2_flag = 0;
186+
SREG = oldSREG;
187+
if (flag) serialEvent2();
188+
#endif
189+
#ifdef serialEvent3_implemented
190+
oldSREG = SREG;
191+
noInterrupts();
192+
flag = serialEvent3_flag;
193+
serialEvent3_flag = 0;
194+
SREG = oldSREG;
195+
if (flag) serialEvent3();
196+
#endif
197+
}
198+
154199

155200
#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
156201
#error Don't know what the Data Register Empty vector is called for the first UART

hardware/arduino/cores/arduino/HardwareSerial.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ class HardwareSerial : public Stream
7474
extern HardwareSerial Serial3;
7575
#endif
7676

77+
extern void serialEventRun(void);
78+
7779
#endif
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
#include <Arduino.h>
3+
#include <IPAddress.h>
4+
5+
IPAddress::IPAddress()
6+
{
7+
memset(_address, 0, sizeof(_address));
8+
}
9+
10+
IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
11+
{
12+
_address[0] = first_octet;
13+
_address[1] = second_octet;
14+
_address[2] = third_octet;
15+
_address[3] = fourth_octet;
16+
}
17+
18+
IPAddress::IPAddress(uint32_t address)
19+
{
20+
memcpy(_address, &address, sizeof(_address));
21+
}
22+
23+
IPAddress::IPAddress(const uint8_t *address)
24+
{
25+
memcpy(_address, address, sizeof(_address));
26+
}
27+
28+
IPAddress& IPAddress::operator=(const uint8_t *address)
29+
{
30+
memcpy(_address, address, sizeof(_address));
31+
return *this;
32+
}
33+
34+
IPAddress& IPAddress::operator=(uint32_t address)
35+
{
36+
memcpy(_address, (const uint8_t *)&address, sizeof(_address));
37+
return *this;
38+
}
39+
40+
bool IPAddress::operator==(const uint8_t* addr)
41+
{
42+
return memcmp(addr, _address, sizeof(_address)) == 0;
43+
}
44+
45+
size_t IPAddress::printTo(Print& p) const
46+
{
47+
size_t n = 0;
48+
for (int i =0; i < 3; i++)
49+
{
50+
n += p.print(_address[i], DEC);
51+
n += p.print('.');
52+
}
53+
n += p.print(_address[3], DEC);
54+
return n;
55+
}
56+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
*
3+
* MIT License:
4+
* Copyright (c) 2011 Adrian McEwen
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*
23+
* [email protected] 1/1/2011
24+
*/
25+
26+
#ifndef IPAddress_h
27+
#define IPAddress_h
28+
29+
#include <Printable.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+
uint8_t _address[4]; // IPv4 address
36+
// Access the raw byte array containing the address. Because this returns a pointer
37+
// to the internal structure rather than a copy of the address this function should only
38+
// be used when you know that the usage of the returned uint8_t* will be transient and not
39+
// stored.
40+
uint8_t* raw_address() { return _address; };
41+
42+
public:
43+
// Constructors
44+
IPAddress();
45+
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
46+
IPAddress(uint32_t address);
47+
IPAddress(const uint8_t *address);
48+
49+
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
50+
// to a four-byte uint8_t array is expected
51+
operator uint32_t() { return *((uint32_t*)_address); };
52+
bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
53+
bool operator==(const uint8_t* addr);
54+
55+
// Overloaded index operator to allow getting and setting individual octets of the address
56+
uint8_t operator[](int index) const { return _address[index]; };
57+
uint8_t& operator[](int index) { return _address[index]; };
58+
59+
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
60+
IPAddress& operator=(const uint8_t *address);
61+
IPAddress& operator=(uint32_t address);
62+
63+
virtual size_t printTo(Print& p) const;
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

hardware/arduino/cores/arduino/Print.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Print
4242
public:
4343
Print() : write_error(0) {}
4444

45-
int writeError() { return write_error; }
45+
int getWriteError() { return write_error; }
4646
void clearWriteError() { setWriteError(0); }
4747

4848
virtual size_t write(uint8_t) = 0;

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+
* [email protected] 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

0 commit comments

Comments
 (0)