-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.h
/
addresses.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef SENSOR_ETHER_SERVER_ADDRESSES_H
#define SENSOR_ETHER_SERVER_ADDRESSES_H
// Utilities for working with MAC and IP addresses.
// Author: James Synge
// Date: March, 2019
#include "Arduino.h"
#include <inttypes.h>
#include "Ethernet.h"
#include "eeprom_io.h"
void printMACAddress(byte mac[6]);
// Organizationally Unique Identifier: first 3 bytes of a MAC address that is
// NOT a globally unique address. The
struct OuiPrefix : Printable {
// Ctor will ensure that the bit marking this as an OUI is set, and that
// the multicast address bit is cleared.
OuiPrefix();
OuiPrefix(uint8_t a, uint8_t b, uint8_t c);
size_t printTo(Print&) const override;
byte bytes[3];
};
// Represents an Ethernet address.
struct MacAddress : Printable {
// Fills mac with a randomly generated, non-broadcast MAC address in the
// space of Organizationally Unique Identifiers. If an OuiPrefix is supplied,
// it will be used as the first 3 bytes of the MAC address.
// The Arduino random number library is used, so be sure to seed it according
// to the level or randomness you want in the generated address; if you don't
// set the seed, the same sequence of numbers is always produced.
void generateAddress(const OuiPrefix* oui_prefix=nullptr);
size_t printTo(Print&) const override;
// Saves to the specified address in the EEPROM; returns the address after
// the saved MAC address.
int save(int toAddress, eeprom_io::Crc32* crc) const;
// Reads from the specified address in the EEPROM; returns the address after
// the restored MAC address.
int read(int fromAddress, eeprom_io::Crc32* crc);
// Returns true if the first 3 bytes match the specified prefix.
bool hasOuiPrefix(const OuiPrefix& oui_prefix) const;
bool operator==(const MacAddress& other) const;
byte mac[6];
};
// Extends the IPAddress class provided by the Arduino core library to support
// saving an IPv4 address to EEPROM and later reading it back from EEPROM.
class SaveableIPAddress : public IPAddress {
public:
// Inherit the base class constructors.
using IPAddress::IPAddress;
// Saves to the specified address in the EEPROM; returns the address after
// the saved value.
int save(int toAddress, eeprom_io::Crc32* crc) const;
// Reads from the specified address in the EEPROM; returns the address after
// the restored value.
int read(int fromAddress, eeprom_io::Crc32* crc);
};
// A pair of addresses (MAC and IP); the two are needed togther when working
// with the Arduino Ethernet library. Supports saving to EEPROM and later
// reading it back from EEPROM. This is useful because it allows us to generate
// random addresses when we first boot up a sketch, and then use those same
// addresses each time the sketch boots up in the future; this may make it
// easier for the person using the sketch to find their device on the LAN.
struct Addresses : Printable {
// Load the saved addresses, which must have the oui_prefix if specified;
// if unable to load them (not stored or wrong prefix), generate addresses
// and store them in the EEPROM.
void loadOrGenAndSave(const OuiPrefix* oui_prefix);
// Save this struct's fields to EEPROM at address 0.
void save() const;
// Restore this struct's fields from EEPROM, starting at address 0.
// Returns true if successful (i.e. the named and CRC matched),
// false otherwse.
bool load(const OuiPrefix* oui_prefix);
// Randomly generate MAC and IPAddress. The MAC address has the specified OuiPrefix
// if supplied (else it is random), and the IPAddress is in the link local
// address range (169.254.1.0 to 169.254.254.255, according to RFC 3927).
// No support is provided for detecting conflicts with other users of the
// generated addresses.
// The Arduino random number library is used, so be sure to seed it according
// to the level or randomness you want in the generated address; if you don't
// set the seed, the same sequence of numbers is always produced.
void generateAddresses(const OuiPrefix* oui_prefix);
// Print the addresses, preceded by a prefix (if provided) and followed by a
// newline.
void println(const char* prefix=nullptr) const;
size_t printTo(Print&) const override;
bool operator==(const Addresses& other) const;
SaveableIPAddress ip;
MacAddress mac;
};
#endif // SENSOR_ETHER_SERVER_ADDRESSES_H