IsolatedEthernet
|
Replacement for UDP class to use Ethernet. More...
#include <IsolatedEthernet.h>
Public Member Functions | |
UDP () | |
Construct a new UDP object. This is safe as a globally constructed object. | |
virtual | ~UDP () |
Destroy the UDP object. This releases the socket (it allocated) and any buffers. | |
bool | setBuffer (size_t buffer_size, uint8_t *buffer=NULL) |
Allocates a buffer for parsePacket(). Default is 512 if not specified. More... | |
void | releaseBuffer () |
Releases the current buffer, discarding any previously allocated memory. More... | |
virtual uint8_t | begin (uint16_t port, network_interface_t nif=0) |
Initializes a UDP socket. More... | |
virtual void | stop () |
Disconnects this UDP socket. | |
virtual int | sendPacket (const uint8_t *buffer, size_t buffer_size, IPAddress destination, uint16_t port) |
Sends an packet directly. You should use this method instead of beginPacket(), write(), and endPacket(). More... | |
virtual int | sendPacket (const char *buffer, size_t buffer_size, IPAddress destination, uint16_t port) |
Sends an packet directly. You should use this method instead of beginPacket(), write(), and endPacket(). More... | |
virtual int | receivePacket (uint8_t *buffer, size_t buf_size, system_tick_t timeout=0) |
Retrieves a packet directly. You should use this method instead of parsePacket(). More... | |
virtual int | receivePacket (char *buffer, size_t buf_size, system_tick_t timeout=0) |
Retrieves a packet directly. You should use this method instead of parsePacket(). More... | |
virtual int | beginPacket (IPAddress ip, uint16_t port) |
Begin writing a packet to the given destination. It is often better to use sendPacket() instead. More... | |
virtual int | beginPacket (const char *host, uint16_t port) |
Begin writing a packet to the given destination. It is often better to use sendPacket() instead. More... | |
virtual size_t | write (uint8_t) |
Writes to the currently open packet after a call to beginPacket(). More... | |
virtual size_t | write (const uint8_t *buffer, size_t size) |
Writes to the currently open packet after a call to beginPacket(). More... | |
virtual int | endPacket () |
Sends the current buffered packet over the network and clears the buffer. More... | |
virtual int | parsePacket (system_tick_t timeout=0) |
Reads a UDP packet into the packet buffer. You should use receivePacket instead. More... | |
virtual int | available () |
Get the number of bytes that can be read after parsePacket. More... | |
virtual int | read () |
Read a single byte from the read buffer. Available after parsePacket(). More... | |
virtual int | read (unsigned char *buffer, size_t len) |
Reads multiple bytes. Available after parsePacket(). More... | |
virtual int | read (char *buffer, size_t len) |
Reads multiple bytes. Available after parsePacket(). More... | |
virtual int | peek () |
Returns the next character that read() will return without consuming it. More... | |
virtual void | flush () |
Blocks until all data has been sent out. More... | |
void | flush_buffer () |
Discards the currently read packet from parsePacket(). | |
const uint8_t * | buffer () const |
Returns the buffer used by parsePacket() and beginPacket() More... | |
virtual IPAddress | remoteIP () |
Return the IP address of the other side of the connection. More... | |
virtual uint16_t | remotePort () |
Return the port number of the other side of the connection. More... | |
virtual size_t | printTo (Print &p) const |
int | joinMulticast (const IPAddress &ip) |
Join a multicast network. More... | |
int | leaveMulticast (const IPAddress &ip) |
Leave a multicast network. More... | |
bool | isOpen (sock_handle_t sock) |
Returns true if the socket is currently open. Used internally. More... | |
sock_handle_t | socket () |
Returns the internal socket handle for this connection. More... | |
virtual size_t | write (uint8_t c)=0 |
Write a single byte to the stream or file. More... | |
size_t | write (const char *str) |
Write a null-terminated c-string the stream or file. More... | |
virtual size_t | write (const uint8_t *buffer, size_t size) |
Write a bytes specified by a buffer and length to the stream or file. More... | |
void | setTimeout (system_tick_t timeout) |
Sets the read timeout (default: 1000 milliseconds) More... | |
bool | find (char *target) |
Reads data from the stream until the target string is found. More... | |
bool | find (char *target, size_t length) |
Reads data from the stream until the target string is found. More... | |
bool | findUntil (char *target, char *terminator) |
Reads data from the stream until the target string is found or the terminator string is found. More... | |
bool | findUntil (char *target, size_t targetLen, char *terminate, size_t termLen) |
Reads data from the stream until the target string is found or the terminator string is found. More... | |
long | parseInt () |
returns the first valid (long) integer value from the current position More... | |
float | parseFloat () |
returns the first valid float value from the current position More... | |
size_t | readBytes (char *buffer, size_t length) |
Read chars from stream into buffer. More... | |
size_t | readBytesUntil (char terminator, char *buffer, size_t length) |
Read chars from stream into buffer until the character terminator is found. More... | |
String | readString () |
Reads the remainder of the file into a string. | |
String | readStringUntil (char terminator) |
Reads the remainder of the file into a string or until terminator is found. More... | |
int | getWriteError () |
Return the last error code. 0 means no error. | |
void | clearWriteError () |
Clear the last error code to 0. | |
size_t | print (const char[]) |
Print a null-terminated array of char variables (a c-string) to the stream or file. | |
size_t | print (char) |
Print a single character to the stream or file. | |
size_t | print (unsigned char value, int base=DEC) |
Print an unsigned char (byte value, 8 bits) in the specified base to the stream or file. More... | |
size_t | print (int value, int base=DEC) |
Print an int (32 bit integer) the specified base to the stream or file. More... | |
size_t | print (unsigned int value, int base=DEC) |
Print an unsigned int (32 bit unsigned integer) the specified base to the stream or file. More... | |
size_t | print (long value, int base=DEC) |
Print a long (32 bit integer) the specified base to the stream or file. More... | |
size_t | print (unsigned long value, int base=DEC) |
Print a unsigned long (32 bit unsigned integer) the specified base to the stream or file. More... | |
size_t | print (double value, int dec=2) |
Print a double floating point value to the stream or file. More... | |
size_t | print (const Printable &) |
Print an object derived from Printable to the stream or file. | |
size_t | println (const char[]) |
Print a null-terminated array of char variables (a c-string) plus a CRLF end-of-line terminator to the stream or file. | |
size_t | println (char value) |
Print a single character plus a CRLF end-of-line terminator to the stream or file. | |
size_t | println (unsigned char value, int base=DEC) |
Print an unsigned char (byte value. 8 bits) in the specified base plus a CRLF end-of-line terminator to the stream or file. More... | |
size_t | println (int value, int base=DEC) |
Print an int (32 bit integer) the specified base to plus a CRLF end-of-line terminator the stream or file. More... | |
size_t | println (unsigned int value, int base=DEC) |
Print an unsigned int (32 bit unsigned integer) the specified base plus a CRLF end-of-line terminator to the stream or file. More... | |
size_t | println (long value, int base=DEC) |
Print a long (32 bit signed integer) the specified base plus a CRLF end-of-line terminator to the stream or file. More... | |
size_t | println (unsigned long value, int base=DEC) |
Print a unsigned long (32 bit unsigned integer) the specified base plus a CRLF end-of-line terminator to the stream or file. More... | |
size_t | println (double value, int dec=2) |
Print a double floating point value plus a CRLF end-of-line terminator to the stream or file. More... | |
size_t | println (const Printable &) |
Print an object derived from Printable plus a CRLF end-of-line terminator to the stream or file. | |
size_t | println (void) |
Print a CRLF end-of-line terminator to the stream or file. | |
template<typename... Args> | |
size_t | printf (const char *format, Args... args) |
Print using printf-style formatting to the stream or file. More... | |
template<typename... Args> | |
size_t | printlnf (const char *format, Args... args) |
Print using printf-style formatting plus a CRLF end-of-line terminator to the stream or file. More... | |
Replacement for UDP class to use Ethernet.
Replace UDP
with IsolatedEthernet::UDP
to use Ethernet instead of the Particle device's native networking (cellular or Wi-Fi).
|
virtual |
|
virtual |
Initializes a UDP socket.
port | The local port to connect to. |
nif | The network interface to connect to (not used, pass 0) |
|
virtual |
Begin writing a packet to the given destination. It is often better to use sendPacket() instead.
host | The DNS hostname to send to. |
port | The destination port of the peer |
DNS will be done for every packet when using a hostname, the result is not cached. If you will be sending numerous packets you should resolve the hostname once and save the IPAddress instead.
The beginPacket() and parsePacket() functions share a buffer so you should never use both at the same time.
|
virtual |
Begin writing a packet to the given destination. It is often better to use sendPacket() instead.
ip | The IP address of the destination peer. |
port | The destination port of the peer |
The beginPacket() and parsePacket() functions share a buffer so you should never use both at the same time.
|
inline |
Returns the buffer used by parsePacket() and beginPacket()
|
virtual |
Sends the current buffered packet over the network and clears the buffer.
|
inherited |
Reads data from the stream until the target string is found.
target | The string to search for (null-terminated c-string) |
|
inherited |
Reads data from the stream until the target string is found.
target | The string to search for. The string is specified by length so it does not need to be null-terminated. |
length | The number of bytes in target to search for. |
|
inherited |
Reads data from the stream until the target string is found or the terminator string is found.
target | The string to search for (null-terminated c-string) |
terminator | The terminator string to search for (null-terminated c-string) |
|
inherited |
Reads data from the stream until the target string is found or the terminator string is found.
target | The string to search for. The string is specified by length so it does not need to be null-terminated. |
targetLen | The number of bytes in target to search for. |
terminate | The terminator string to search for. The string is specified by length so it does not need to be null-terminated. |
termLen | The number of bytes in terminator to search for. |
|
virtual |
Blocks until all data has been sent out.
Implements Stream.
bool IsolatedEthernet::UDP::isOpen | ( | sock_handle_t | sock | ) |
Returns true if the socket is currently open. Used internally.
sock |
If you want to see if a connection is open, use connected() or status().
int IsolatedEthernet::UDP::joinMulticast | ( | const IPAddress & | ip | ) |
Join a multicast network.
ip | The multicast IP address. This must be a multicast IP address, not a LAN address. |
You must call begin() before joinMulticast() because the port is specified as a parameter to begin().
You can only bind to a single multicast address per UDP object.
int IsolatedEthernet::UDP::leaveMulticast | ( | const IPAddress & | ip | ) |
Leave a multicast network.
ip | The multicast IP address to leave. This parameter is not used |
When you leave a multicast network it turns the socket back into a normal unicast socket, as when you call begin(). If you want to free all resources, you must also call stop().
|
inherited |
returns the first valid float value from the current position
Initial characters that are not digits (or the minus sign or .) are skipped float is terminated by the first character that is not a float character. Decimal part is separate by '.'.
|
inherited |
returns the first valid (long) integer value from the current position
Initial characters that are not digits (or the minus sign) are skipped integer is terminated by the first character that is not a digit.
|
virtual |
Reads a UDP packet into the packet buffer. You should use receivePacket instead.
timeout | Timeout in milliseconds, or 0 to not wait |
Aside from parsePacket() / read() being much less efficient, another problem is that parsePacket() does not differentiate between no data available and an error getting a packet. When listening for packets, if the underlying network goes away, all listeners are closed and must be reopened using begin(). The parsePacket() method does not differentiate between the case where no packet has arrived yet and packets will never arrive again (because you need to call begin() again). This is very annoying and does not occur with receivePacket() which returns all three states separately.
The beginPacket() and parsePacket() functions share a buffer so you should never use both at the same time.
|
virtual |
|
inherited |
Print a double floating point value to the stream or file.
value | The value to print. |
dec | The number of decimal places to include for the fractional part. Default: 2 |
|
inherited |
Print an int (32 bit integer) the specified base to the stream or file.
value | The value to print. |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print a long (32 bit integer) the specified base to the stream or file.
value | The value to print. |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print an unsigned char (byte value, 8 bits) in the specified base to the stream or file.
value | The value to print. |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print an unsigned int (32 bit unsigned integer) the specified base to the stream or file.
value | The value to print. |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print a unsigned long (32 bit unsigned integer) the specified base to the stream or file.
value | The value to print. |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inlineinherited |
Print using printf-style formatting to the stream or file.
format | printf-style formatting string |
args | variable arguments |
|
inherited |
Print a double floating point value plus a CRLF end-of-line terminator to the stream or file.
value | The value to print. |
dec | The number of decimal places to include for the fractional part. Default: 2 |
|
inherited |
Print an int (32 bit integer) the specified base to plus a CRLF end-of-line terminator the stream or file.
value | The value to print |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print a long (32 bit signed integer) the specified base plus a CRLF end-of-line terminator to the stream or file.
value | The value to print |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print an unsigned char (byte value. 8 bits) in the specified base plus a CRLF end-of-line terminator to the stream or file.
value | The value to print. |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print an unsigned int (32 bit unsigned integer) the specified base plus a CRLF end-of-line terminator to the stream or file.
value | The value to print |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inherited |
Print a unsigned long (32 bit unsigned integer) the specified base plus a CRLF end-of-line terminator to the stream or file.
value | The value to print. |
base | The base to print. Default is DEC (decimal). Other values are HEX (hexadecimal), OCT (octal), and BIN (binary). |
|
inlineinherited |
Print using printf-style formatting plus a CRLF end-of-line terminator to the stream or file.
format | printf-style formatting string |
args | variable arguments |
|
virtual |
Prints the current read parsed packet to the given output.
p |
Implements Printable.
|
virtual |
Read a single byte from the read buffer. Available after parsePacket().
You should use receivePacket() instead of parsePacket() / read().
Implements Stream.
|
inlinevirtual |
Reads multiple bytes. Available after parsePacket().
This is not the same as receivePacket() even though it has the same prototype. This method reads the bytes left in the packet buffer filled in by parsePacket(). The various read() methods consume the data in the buffer until empty.
Even though this method takes a char * buffer, it does not create a c-string. The data could be binary, may contain embedded nulls, and is not null-terminated.
You should use receivePacket() instead of parsePacket() / read().
|
virtual |
Reads multiple bytes. Available after parsePacket().
This is not the same as receivePacket() even though it has the same prototype. This method reads the bytes left in the packet buffer filled in by parsePacket(). The various read() methods consume the data in the buffer until empty.
You should use receivePacket() instead of parsePacket() / read().
|
inherited |
Read chars from stream into buffer.
buffer | The buffer to read into |
length | The number of bytes to read |
|
inherited |
Read chars from stream into buffer until the character terminator is found.
terminator | The character to stop reading |
buffer | The buffer to read into |
length | The number of bytes to read |
The terminator could be some thing like \n (newline), \t (tab), etc. depending on the data you are parsing.
|
inherited |
Reads the remainder of the file into a string or until terminator is found.
terminator | The character to stop reading at. |
|
inlinevirtual |
Retrieves a packet directly. You should use this method instead of parsePacket().
buffer | The buffer to read data to (char *). It may contain binary data. |
buf_size | The buffer size |
timeout | In milliseconds to wait for a packet, or 0 = do not wait and return 0 if there is no packet available. |
Even though the buffer is a char *, there is no guarantee the packet will contain a c-string. It could contain embedded nulls, and may not be null-terminated.
This does not require the UDP instance to have an allocated buffer.
If the buffer is not large enough for the packet, the remainder that doesn't fit is discarded.
|
virtual |
Retrieves a packet directly. You should use this method instead of parsePacket().
buffer | The buffer to read data to (uint8_t *). It may contain binary data. |
buf_size | The buffer size |
timeout | In milliseconds to wait for a packet, or 0 = do not wait and return 0 if there is no packet available. |
This does not require the UDP instance to have an allocated buffer.
If the buffer is not large enough for the packet, the remainder that doesn't fit is discarded.
This method is preferable to parsePacket() for several reasons:
void IsolatedEthernet::UDP::releaseBuffer | ( | ) |
Releases the current buffer, discarding any previously allocated memory.
After this call only sendPacket() and receivePacket() may be used, until a new buffer is set via setBuffer().
|
inlinevirtual |
Return the IP address of the other side of the connection.
|
inlinevirtual |
Return the port number of the other side of the connection.
|
inlinevirtual |
Sends an packet directly. You should use this method instead of beginPacket(), write(), and endPacket().
buffer | Pointer to a buffer to send (const char *), can be binary data. |
buffer_size | Size of buffer in bytes, must be > 0. |
destination | The IP address to send to. |
port | The port to send to. |
Even though the parameter is a const char *, buffer is not treated as a c-string; pass stren(buffer) as the buffer_size if desired.
This does not require the UDP instance to have an allocated buffer.
|
virtual |
Sends an packet directly. You should use this method instead of beginPacket(), write(), and endPacket().
buffer | Pointer to a buffer to send (const uint8_t *), can be binary data. |
buffer_size | Size of buffer in bytes, must be > 0. |
destination | The IP address to send to. |
port | The port to send to. |
This does not require the UDP instance to have an allocated buffer.
bool IsolatedEthernet::UDP::setBuffer | ( | size_t | buffer_size, |
uint8_t * | buffer = NULL |
||
) |
Allocates a buffer for parsePacket(). Default is 512 if not specified.
buffer_size | The size of the read/write buffer. Can be 0 if only readPacket() and sendPacket() are used, as these methods use client-provided buffers. |
buffer | A pre-allocated buffer. This is optional, and if not specified the UDP class will allocate the buffer on the heap. |
|
inherited |
Sets the read timeout (default: 1000 milliseconds)
This makes more sense for network and serial streams.
|
inline |
Returns the internal socket handle for this connection.
|
inline |
Write a null-terminated c-string the stream or file.
str | point to a null-terminated c-string. |
|
virtual |
Writes to the currently open packet after a call to beginPacket().
Reimplemented from Print.
|
virtual |
Write a bytes specified by a buffer and length to the stream or file.
buffer | pointer to the buffer. The data does not need to be null-terminated. |
size | size in bytes |
Reimplemented from Print.
|
virtual |
Write a single byte to the stream or file.
c | The byte to write. All values 0 - 255 are allowed. |
Implements Print.
|
virtual |
Writes to the currently open packet after a call to beginPacket().
Implements Print.