friendev EtherDune TCP/IP library
|
Abstract base class for any network service running in EtherDune. More...
#include <NetworkService.h>
Static Public Member Functions | |
static bool | begin (uint8_t cspin) |
Initializes EtherDune and the underlying hardware More... | |
static void | loop () |
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets. More... | |
static DNSClient & | DNS () |
Obtains access to the DNS service singleton instance. More... | |
![]() | |
static bool | isLinkUp () |
Determines whether the network link is ready More... | |
Static Public Attributes | |
static MACAddress | localMAC |
Ethernet MAC address. More... | |
static IPAddress | localIP = { 0, 0, 0, 0 } |
IP address of this application. More... | |
static IPAddress | gatewayIP = { 0, 0, 0, 0 } |
IP address of the gateway in this network. More... | |
static IPAddress | netmask = { 0, 0, 0, 0 } |
Subnet mask. More... | |
static IPAddress | dnsIP = { 0, 0, 0, 0 } |
IP address of the DNS server to use. More... | |
Protected Member Functions | |
NetworkService () | |
~NetworkService () | |
virtual bool | onPacketReceived ()=0 |
Called every time a packet is received. More... | |
virtual void | tick () |
This is a timer function that is called every NETWORK_TIMER_RESOLUTION milliseconds on every service. More... | |
virtual void | onDNSResolve (uint8_t status, uint16_t identification, const IPAddress &ip) |
Called on each network service every time a DNS response is received. More... | |
Static Protected Member Functions | |
static bool | sendIPPacket (uint16_t length) |
Puts the current in-memory packet in the network More... | |
static void | prepareIPPacket (const IPAddress &remoteIP) |
Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum More... | |
static bool | sameLAN (IPAddress &dst) |
Determines whether the given IP is in the same subnet as localIP More... | |
![]() | |
static void | writeByte (byte b) |
static void | writeByte (uint16_t dst, byte b) |
static void | writeBuf (uint16_t dst, uint16_t len, const byte *data) |
static void | writeBuf (uint16_t len, const byte *data) |
static uint16_t | hardwareChecksum (uint16_t src, uint16_t len) |
static uint16_t | hardwareChecksumRxOffset (uint16_t offset, uint16_t len) |
static void | moveMem (uint16_t dest, uint16_t src, uint16_t len) |
static void | readBuf (uint16_t src, uint16_t len, byte *data) |
static void | readBuf (uint16_t len, byte *data) |
static byte | readByte (uint16_t src) |
static void | packetSend (uint16_t len) |
static void | packetSend (uint16_t len, const byte *data) |
static void | loadSample () |
static void | loadAll () |
static void | release () |
static uint8_t | begin (uint8_t cspin) |
static void | loop () |
static void | enableBroadcast () |
Static Protected Attributes | |
static EthBuffer | packet |
in-memory packet buffer currently being processed. More... | |
static ARPService | ARP |
ARP singleton instance. More... | |
Friends | |
class | DNSClient |
class | ENC28J60 |
Abstract base class for any network service running in EtherDune.
The NetworkService class is the core of the EtherDune library. This class includes methods and virtual functions (events) that provide access to the network layer, providing an extensible framework for building reusable and maintenable components and maximizing code reuse.
To extend the functionality of EtherDune to new protocols, create a new class derived from NetworkService.
How does this work ?
All instances of NetworkService-derived classes get low-level Ethernet access in this way: Every time an Ethernet packet is received, NetworkService calls each instance's onPacketReceived() event handler. The receiving instance then has the chance to look at the packet and determine whether or not the packet belongs to it. If it decides it owns the packet, then it will return true
to claim it and process it. If does not own the packet, it will return false and NetworkService will query the next instance. If no instance claims the packet, the packet is discarded.
For example, an open UDP socket listening on a given port (implemented in EtherDune as the UDPSocket class) will check that the Ethernet Type field in the ethernet header is set to the IP protocol (packet.eth.etherType =
ETHTYPE_IP), then check whether the IP Header indicates UDP
as protocol type(packet.ip.protocol =
IP_PROTO_UDP) and then finally check if the port its listening on matches the destination port UDP local port (packet.udp.destinationPort
) for example:
EtherDune comes with the following protocols out of the box, implemented as NetworkService - derived classes :
Out of this list, ARP is instantiated by default to handle ARP traffic and respond to ARP queries from other hosts. Other basic services, like DHCP, DNS or ICMP Echo must be explicitly instantiated by the developer if needed in the application. If a service is not used, then it is not compiled in and therefore does not waste RAM or program space.
To create a new network service or protocol, create a new class derived from the most appropriate descendant of NetworkService or NetworkService itself. Most of the time this will be the TCPSocket and UDPSocket classes.
Definition at line 100 of file NetworkService.h.
|
protected |
Definition at line 74 of file NetworkService.cpp.
|
protected |
Definition at line 78 of file NetworkService.cpp.
|
static |
Initializes EtherDune and the underlying hardware
cspin | ENC28J60 chip select pin to use |
true
if successful, false
otherwise.Definition at line 67 of file NetworkService.cpp.
|
static |
Obtains access to the DNS service singleton instance.
Use of this function will immediately compile in all code needed for DNS to work.
Definition at line 215 of file NetworkService.cpp.
|
static |
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets.
Call this with the highest frequency possible, usually in your sketch's loop()
function.
Definition at line 114 of file NetworkService.cpp.
|
protectedvirtual |
Called on each network service every time a DNS response is received.
status | Status of the DNS reply. Corresponds to the DHS Header status field. Nonzero if an error was detected, zero otherwise |
identification | Identification token to match a query to a response. See DNSClient for more information |
ip | If status equals 0 , this contains the resolved IP address |
Definition at line 59 of file NetworkService.cpp.
|
protectedpure virtual |
Called every time a packet is received.
This is a pure virtual function – must be overriden in all derived classes.
true
to claim the packet or false
to ignore it.
|
staticprotected |
Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum
remoteIP | IP of the remote host |
Definition at line 194 of file NetworkService.cpp.
|
staticprotected |
Determines whether the given IP is in the same subnet as localIP
dst | IP address to test |
Definition at line 168 of file NetworkService.cpp.
|
staticprotected |
Puts the current in-memory packet in the network
Definition at line 133 of file NetworkService.cpp.
|
protectedvirtual |
This is a timer function that is called every NETWORK_TIMER_RESOLUTION milliseconds on every service.
It allows each network service to perform upkeep tasks, handle timeouts or send queued messages.
Reimplemented in UDPSocket.
Definition at line 51 of file NetworkService.cpp.
|
friend |
Definition at line 102 of file NetworkService.h.
|
friend |
Definition at line 103 of file NetworkService.h.
|
staticprotected |
ARP singleton instance.
Maintains ARP table and provides methods to query hosts in the network. See ARPService for more information.
Definition at line 115 of file NetworkService.h.
|
static |
IP address of the DNS server to use.
Definition at line 139 of file NetworkService.h.
|
static |
IP address of the gateway in this network.
Definition at line 137 of file NetworkService.h.
|
static |
IP address of this application.
Definition at line 136 of file NetworkService.h.
|
static |
Ethernet MAC address.
Set this to your desired MAC address before you call begin()
Definition at line 135 of file NetworkService.h.
|
static |
Subnet mask.
Definition at line 138 of file NetworkService.h.
|
staticprotected |
in-memory packet buffer currently being processed.
See EthBuffer for more information.
Definition at line 114 of file NetworkService.h.