Skip to content

Commit d76b60e

Browse files
committed
init
0 parents  commit d76b60e

20 files changed

+1956
-0
lines changed

LICENSE

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
# Arduino Networking API Helper classes library
3+
4+
A library to support Arduino Networking libraries development. It accompanies the [Arduino Networking API usage guidelines](https://github.com/JAndrassy/Arduino-Networking-API/blob/main/ArduinoNetAPIDev.md).
5+
6+
The purpose of this library is to help networking library developers to ensure that their library can be used as a replacement of existing Arduino networking libraries and to simplify development of the Arduino Networking API in their library.
7+
8+
_This library is still in development_
9+
10+
## MACAddress
11+
12+
The class MACAddress works similar as the well known IPAddress class from Arduino Core.
13+
14+
It is compatible with an uint8_t array, implements Printable, can be parsed from string and formated to string.
15+
16+
Some existing libraries return the byte array with values in reversed order. Method reverse() reverses the order of bytes in an MACAddress object.
17+
18+
## Tests
19+
20+
### Tests for the WiFi object
21+
22+
Every WiFi library should be able to run the WiFiTest and WiFiScanNetworks sketches with no or minimal modifications.
23+
24+
25+
### Tests for the Ethernet object
26+
27+
The LegacyEthernetTest has what would be good to have in a classic Ethernet library. But even the Ethernet library dosn't run it without modifications.
28+
29+
A future ModernEtherneTest is will test Ethernet libraries with API aligned with WiFi libraries, especially method `config` for static IP configuration.
30+
31+
### Tests for server implementation
32+
33+
* AdvancedChatServer - example and test for server.accept()
34+
* PagerServer - example and test for server.available() and print-to-all-clients
35+
36+
## Server Helpers
37+
38+
### ServerNew
39+
40+
ServerNew is an abstract class template with all pure virtual functions. It helps to code a modern Server class with accept(), begin(port), end() and operator bool. It is not useful at runtime and takes same memory so eventually remove it from the published version of your networking library.
41+
42+
ServerNew is what users ask and old Arduino Server implementations don't have.
43+
44+
Usage:
45+
```
46+
#include <ServerNew.h>
47+
#include "EthernetClient.h"
48+
49+
class EthernetServer
50+
: public ServerNew<EthernetClient>
51+
{
52+
53+
public:
54+
...
55+
```
56+
57+
58+
### ServerTemplate
59+
60+
ServerTemplate implements method available() and print-to-all-clients using accept() method of a class from which it inherits. It is a template with Server type and Client type as template parameters.
61+
62+
Implement a ServerNew realization, don't inherit it from Server or Print. Name it EthernetServerNew or WiFiServerNew. Then define EthernetServer/WiFiServer in your library as
63+
```
64+
typedef ServerTemplate<EthernetServerNew, EthernetClient> EthernetServer;
65+
```
66+
or
67+
```
68+
typedef ServerTemplate<WiFiServerNew, WiFiClient> WiFiServer;
69+
```
70+
71+
This way your library will have a legacy Arduino Server.
72+
73+
ServerTemplate is configurable with some #defines. See the header file for the details.
74+
75+
Note: ServerTemplate requires a proper implementation of the Client class.
76+
77+
### ArduinoWiFiSever/ArduinoEthernetServer
78+
79+
The ArduinoWiFiSever and ArduinoEthernetServer use ServerTemplate to implement server class with proper available() and print-to-all-clients for some existing libraries where the WiFiServer/EthernetServer doesn't have it.
80+
81+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Advanced Chat Server - server.accept() demo/test
3+
4+
A more advanced server that distributes any incoming messages
5+
to all connected clients but the client the message comes from.
6+
To use, telnet to your device's IP address and type.
7+
You can see the client's input in the serial monitor as well.
8+
*/
9+
10+
#include <Ethernet.h>
11+
12+
// Enter a MAC address and IP address for your controller below.
13+
// The IP address will be dependent on your local network.
14+
// gateway and subnet are optional:
15+
byte mac[] = {
16+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
17+
};
18+
19+
// telnet defaults to port 23
20+
EthernetServer server(23);
21+
22+
EthernetClient clients[8];
23+
24+
void setup() {
25+
26+
// initialize the Ethernet device
27+
Ethernet.init(10);
28+
Ethernet.begin(mac);
29+
30+
// Open serial communications and wait for port to open:
31+
Serial.begin(115200);
32+
while (!Serial) {
33+
; // wait for serial port to connect. Needed for native USB port only
34+
}
35+
36+
// Check for Ethernet hardware present
37+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
38+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
39+
while (true) {
40+
delay(1); // do nothing, no point running without Ethernet hardware
41+
}
42+
}
43+
if (Ethernet.linkStatus() == LinkOFF) {
44+
Serial.println("Ethernet cable is not connected.");
45+
}
46+
47+
// start listening for clients
48+
server.begin();
49+
50+
Serial.print("Chat server address:");
51+
Serial.println(Ethernet.localIP());
52+
}
53+
54+
void loop() {
55+
// check for any new client connecting, and say hello (before any incoming data)
56+
EthernetClient newClient = server.accept();
57+
if (newClient) {
58+
for (byte i=0; i < 8; i++) {
59+
if (!clients[i]) {
60+
Serial.print("We have a new client #");
61+
Serial.println(i);
62+
newClient.print("Hello, client number: ");
63+
newClient.println(i);
64+
// Once we "accept", the client is no longer tracked by EthernetServer
65+
// so we must store it into our list of clients
66+
clients[i] = newClient;
67+
break;
68+
}
69+
}
70+
}
71+
72+
// check for incoming data from all clients
73+
for (byte i=0; i < 8; i++) {
74+
if (clients[i] && clients[i].available() > 0) {
75+
// read bytes from a client
76+
byte buffer[80];
77+
int count = clients[i].read(buffer, 80);
78+
// write the bytes to all other connected clients
79+
for (byte j=0; j < 8; j++) {
80+
if (j != i && clients[j].connected()) {
81+
clients[j].write(buffer, count);
82+
}
83+
}
84+
}
85+
}
86+
87+
// stop any clients which disconnect
88+
for (byte i=0; i < 8; i++) {
89+
if (clients[i] && !clients[i].connected()) {
90+
Serial.print("disconnect client #");
91+
Serial.println(i);
92+
clients[i].stop();
93+
}
94+
}
95+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Ethernet Pager Server - - server.available
3+
and print-to-all-clients demo/test
4+
5+
The example is a simple server that echoes any incoming
6+
messages to all connected clients. Connect two or more
7+
telnet sessions to see how server.available() and
8+
server.print() work.
9+
*/
10+
11+
#include <Ethernet.h>
12+
13+
#if (defined(ARDUINO_ARCH_MBED) && defined(MBEDSOCKETCLASS_H))
14+
// the EthernetServer in these libraries doesn't have print-to-all-clients and proper available()
15+
// so we use ArduinoEthernetServer, which configures the ServerTemplate for these platforms
16+
#include <NetApiHelpers.h>
17+
#include <ArduinoEthernetServer.h>
18+
19+
ArduinoEthernetServer server(2323);
20+
21+
#else
22+
23+
EthernetServer server(2323);
24+
#endif
25+
26+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
27+
28+
void setup() {
29+
30+
Serial.begin(115200);
31+
while (!Serial);
32+
33+
Ethernet.begin(mac);
34+
35+
server.begin();
36+
37+
IPAddress ip = Ethernet.localIP();
38+
Serial.println();
39+
Serial.print("To access the server, connect with Telnet client to ");
40+
Serial.print(ip);
41+
Serial.println(" 2323");
42+
}
43+
44+
void loop() {
45+
46+
EthernetClient client = server.available(); // returns first client which has data to read or a 'false' client
47+
if (client) { // client is true only if it is connected and has data to read
48+
String s = client.readStringUntil('\n'); // read the message incoming from one of the clients
49+
s.trim(); // trim eventual \r
50+
Serial.println(s); // print the message to Serial Monitor
51+
client.print("echo: "); // this is only for the sending client
52+
server.println(s); // send the message to all connected clients
53+
server.flush(); // flush the buffers
54+
}
55+
}

0 commit comments

Comments
 (0)