friendev EtherDune TCP/IP library
Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Static Protected Attributes | Friends | List of all members
NetworkService Class Referenceabstract

Abstract base class for any network service running in EtherDune. More...

#include <NetworkService.h>

Inheritance diagram for NetworkService:
ListItem ENC28J60 ARPService ICMP Socket ICMPPingAutoReply ICMPPinger TCPSocket UDPSocket HTTPClient HTTPServer TCPListener< SOCKET, MAX_CLIENTS > DHCP DNSClient

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 DNSClientDNS ()
 Obtains access to the DNS service singleton instance. More...
 
- Static Public Member Functions inherited from ENC28J60
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 Protected Member Functions inherited from ENC28J60
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
 

Detailed Description

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:

bool UDPSocket::onPacketReceived()
{
if (
localPort == packet.udp.destinationPort)
{
// This packet is targeted at us. Process packet
//
// [packet processing code]
//
return true; // this instance claimed this packet and processed it
}
else
{
return false; // this packet is not mine, ask someone else.
}
}

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.

Constructor & Destructor Documentation

NetworkService::NetworkService ( )
protected

Definition at line 74 of file NetworkService.cpp.

NetworkService::~NetworkService ( )
protected

Definition at line 78 of file NetworkService.cpp.

Member Function Documentation

bool NetworkService::begin ( uint8_t  cspin)
static

Initializes EtherDune and the underlying hardware

Parameters
cspinENC28J60 chip select pin to use
Returns
true if successful, false otherwise.

Definition at line 67 of file NetworkService.cpp.

DNSClient & NetworkService::DNS ( )
static

Obtains access to the DNS service singleton instance.

Use of this function will immediately compile in all code needed for DNS to work.

Returns
The DNSClient singleton instance

Definition at line 215 of file NetworkService.cpp.

void NetworkService::loop ( )
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.

void NetworkService::onDNSResolve ( uint8_t  status,
uint16_t  identification,
const IPAddress ip 
)
protectedvirtual

Called on each network service every time a DNS response is received.

Parameters
statusStatus of the DNS reply. Corresponds to the DHS Header status field. Nonzero if an error was detected, zero otherwise
identificationIdentification token to match a query to a response. See DNSClient for more information
ipIf status equals 0, this contains the resolved IP address

Definition at line 59 of file NetworkService.cpp.

virtual bool NetworkService::onPacketReceived ( )
protectedpure virtual

Called every time a packet is received.

This is a pure virtual function – must be overriden in all derived classes.

Returns
The derived instance must return true to claim the packet or false to ignore it.
void NetworkService::prepareIPPacket ( const IPAddress remoteIP)
staticprotected

Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum

Parameters
remoteIPIP of the remote host

Definition at line 194 of file NetworkService.cpp.

bool NetworkService::sameLAN ( IPAddress dst)
staticprotected

Determines whether the given IP is in the same subnet as localIP

Parameters
dstIP address to test
Returns

Definition at line 168 of file NetworkService.cpp.

bool NetworkService::sendIPPacket ( uint16_t  length)
staticprotected

Puts the current in-memory packet in the network

Parameters
lengthNumber of bytes of packet to put in ENC28J60's buffer
Returns

Definition at line 133 of file NetworkService.cpp.

void NetworkService::tick ( )
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.

Friends And Related Function Documentation

friend class DNSClient
friend

Definition at line 102 of file NetworkService.h.

friend class ENC28J60
friend

Definition at line 103 of file NetworkService.h.

Member Data Documentation

ARPService NetworkService::ARP
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.

IPAddress NetworkService::dnsIP = { 0, 0, 0, 0 }
static

IP address of the DNS server to use.

Definition at line 139 of file NetworkService.h.

IPAddress NetworkService::gatewayIP = { 0, 0, 0, 0 }
static

IP address of the gateway in this network.

Definition at line 137 of file NetworkService.h.

IPAddress NetworkService::localIP = { 0, 0, 0, 0 }
static

IP address of this application.

Definition at line 136 of file NetworkService.h.

MACAddress NetworkService::localMAC
static

Ethernet MAC address.

Set this to your desired MAC address before you call begin()

Definition at line 135 of file NetworkService.h.

IPAddress NetworkService::netmask = { 0, 0, 0, 0 }
static

Subnet mask.

Definition at line 138 of file NetworkService.h.

EthBuffer NetworkService::packet
staticprotected

in-memory packet buffer currently being processed.

See EthBuffer for more information.

Definition at line 114 of file NetworkService.h.


The documentation for this class was generated from the following files: