Skip to content

Commit 83ac64b

Browse files
committedDec 10, 2015
add support for AVR
this need some more testing but basics are done
1 parent 098c488 commit 83ac64b

9 files changed

+273
-33
lines changed
 

‎README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ WebSocket Server and Client for Arduino
33

44
a WebSocket Server and Client for Arduino based on RFC6455.
55

6+
67
##### Supported features of RFC6455 #####
78
- text frame
89
- binary frame
@@ -16,14 +17,18 @@ a WebSocket Server and Client for Arduino based on RFC6455.
1617
##### Limitations #####
1718
- max input length is limited to the ram size and the ```WEBSOCKETS_MAX_DATA_SIZE``` define
1819
- max output length has no limit (the hardware is the limit)
19-
- Client send masked frames always with mask 0x00000000 (open todo)
20+
- Client send masked frames always with mask 0x00000000
2021

2122
##### Supported Hardware #####
2223
- ESP8266 [Arduino for ESP8266](https://github.com/Links2004/Arduino)
23-
- ATmega328 with Ethernet Shield (planed)
24-
- ATmega328 with enc28j60 (planed)
25-
- ATmega2560 with Ethernet Shield (planed)
26-
- ATmega2560 with enc28j60 (planed)
24+
- ATmega328 with Ethernet Shield (alpha)
25+
- ATmega328 with enc28j60 (alpha)
26+
- ATmega2560 with Ethernet Shield (alpha)
27+
- ATmega2560 with enc28j60 (alpha)
28+
29+
### wss / SSL ###
30+
supported for:
31+
- wss client on the ESP8266
2732

2833
### Issues ###
2934
Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues

‎library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=WebSockets
2-
version=1.2
2+
version=1.3
33
author=Markus Sattler
44
maintainer=Markus Sattler
55
sentence=WebSockets for Arduino (Server + Client)

‎src/WebSockets.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ extern "C" {
3838

3939
#ifdef ESP8266
4040
#include <Hash.h>
41+
#else
42+
43+
extern "C" {
44+
#include "libsha1/libsha1.h"
45+
}
46+
4147
#endif
4248

4349
/**
@@ -343,8 +349,13 @@ String WebSockets::acceptKey(String clientKey) {
343349
#ifdef ESP8266
344350
sha1(clientKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", &sha1HashBin[0]);
345351
#else
346-
#error todo implement sha1 for AVR
352+
clientKey =+ "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
353+
SHA1_CTX ctx;
354+
SHA1Init(&ctx);
355+
SHA1Update(&ctx, (const unsigned char*)clientKey.c_str(), clientKey.length());
356+
SHA1Final(&sha1HashBin[0], &ctx);
347357
#endif
358+
348359
String key = base64_encode(sha1HashBin, 20);
349360
key.trim();
350361

‎src/WebSockets.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include <Arduino.h>
2929

30-
#define DEBUG_WEBSOCKETS(...) Serial1.printf( __VA_ARGS__ )
30+
//#define DEBUG_WEBSOCKETS(...) Serial1.printf( __VA_ARGS__ )
3131

3232
#ifndef DEBUG_WEBSOCKETS
3333
#define DEBUG_WEBSOCKETS(...)
@@ -63,17 +63,20 @@
6363

6464
#include <ESP8266WiFi.h>
6565
#define WEBSOCKETS_NETWORK_CLASS WiFiClient
66+
#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer
6667

6768
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_W5100)
6869

6970
#include <Ethernet.h>
7071
#include <SPI.h>
7172
#define WEBSOCKETS_NETWORK_CLASS EthernetClient
73+
#define WEBSOCKETS_NETWORK_SERVER_CLASS EthernetServer
7274

7375
#elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ENC28J60)
7476

7577
#include <UIPEthernet.h>
7678
#define WEBSOCKETS_NETWORK_CLASS UIPClient
79+
#define WEBSOCKETS_NETWORK_SERVER_CLASS UIPServer
7780

7881
#else
7982
#error "no network type selected!"

‎src/WebSocketsClient.h

-11
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@
2626
#define WEBSOCKETSCLIENT_H_
2727

2828
#include <Arduino.h>
29-
30-
#ifdef ESP8266
31-
#include <ESP8266WiFi.h>
32-
#else
33-
#include <UIPEthernet.h>
34-
#ifndef UIPETHERNET_H
35-
#include <Ethernet.h>
36-
#include <SPI.h>
37-
#endif
38-
#endif
39-
4029
#include "WebSockets.h"
4130

4231
class WebSocketsClient: private WebSockets {

‎src/WebSocketsServer.cpp

+20-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
WebSocketsServer::WebSocketsServer(uint16_t port) {
2929
_port = port;
30-
_server = new WiFiServer(port);
30+
_server = new WEBSOCKETS_NETWORK_SERVER_CLASS(port);
3131

3232
_cbEvent = NULL;
3333

@@ -240,6 +240,7 @@ void WebSocketsServer::disconnect(uint8_t num) {
240240
}
241241
}
242242

243+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
243244
/**
244245
* get an IP for a client
245246
* @param num uint8_t client id
@@ -255,6 +256,7 @@ IPAddress WebSocketsServer::remoteIP(uint8_t num) {
255256

256257
return IPAddress();
257258
}
259+
#endif
258260

259261
//#################################################################################
260262
//#################################################################################
@@ -363,18 +365,22 @@ bool WebSocketsServer::clientIsConnected(WSclient_t * client) {
363365
*/
364366
void WebSocketsServer::handleNewClients(void) {
365367
WSclient_t * client;
368+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
366369
while(_server->hasClient()) {
370+
#endif
367371
bool ok = false;
368372
// search free list entry for client
369373
for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
370374
client = &_clients[i];
371375

372376
// state is not connected or tcp connection is lost
373377
if(!clientIsConnected(client)) {
374-
378+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
375379
// store new connection
376380
client->tcp = new WEBSOCKETS_NETWORK_CLASS(_server->available());
377-
381+
#else
382+
client->tcp = new WEBSOCKETS_NETWORK_CLASS(_server->available());
383+
#endif
378384
if(!client->tcp) {
379385
DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!");
380386
return;
@@ -387,9 +393,12 @@ void WebSocketsServer::handleNewClients(void) {
387393
// set Timeout for readBytesUntil and readStringUntil
388394
client->tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
389395
client->status = WSC_HEADER;
390-
396+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
391397
IPAddress ip = client->tcp->remoteIP();
392398
DEBUG_WEBSOCKETS("[WS-Server][%d] new client from %d.%d.%d.%d\n", client->num, ip[0], ip[1], ip[2], ip[3]);
399+
#else
400+
DEBUG_WEBSOCKETS("[WS-Server][%d] new client\n", client->num);
401+
#endif
393402
ok = true;
394403
break;
395404
}
@@ -398,15 +407,21 @@ void WebSocketsServer::handleNewClients(void) {
398407
if(!ok) {
399408
// no free space to handle client
400409
WEBSOCKETS_NETWORK_CLASS tcpClient = _server->available();
401-
IPAddress ip = tcpClient.remoteIP();
410+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
411+
IPAddress ip = client->tcp->remoteIP();
402412
DEBUG_WEBSOCKETS("[WS-Server] no free space new client from %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
413+
#else
414+
DEBUG_WEBSOCKETS("[WS-Server] no free space new client\n");
415+
#endif
403416
tcpClient.stop();
404417
}
405418

406419
#ifdef ESP8266
407420
delay(0);
408421
#endif
422+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
409423
}
424+
#endif
410425
}
411426

412427
/**

‎src/WebSocketsServer.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,14 @@ class WebSocketsServer: private WebSockets {
6868
void disconnect(void);
6969
void disconnect(uint8_t num);
7070

71+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
7172
IPAddress remoteIP(uint8_t num);
73+
#endif
7274

7375
protected:
7476
uint16_t _port;
7577

76-
#ifdef ESP8266
77-
WiFiServer * _server;
78-
#else
79-
#ifdef UIPETHERNET_H
80-
UIPServer * _server;
81-
#else
82-
EthernetServer * _server;
83-
#endif
84-
#endif
78+
WEBSOCKETS_NETWORK_SERVER_CLASS * _server;
8579

8680
WSclient_t _clients[WEBSOCKETS_SERVER_CLIENT_MAX];
8781

0 commit comments

Comments
 (0)
Please sign in to comment.