Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void setStatePinToNewValue(const char* path, bool state);
void afterVoltageIsUpdated(const char* code, int voltage);

void setup() {
Serial.begin(9600);
.begin(9600);
startWiFi();
// This initializes the SDK's configurations and returns reference to your project.
project = grandeur.init(apiKey, token);
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Grandeur
version=1.0.2
version=1.0.3
author=Grandeur Technologies
maintainer=Grandeur Technologies <hi@grandeur.tech>
sentence=Let your arduinos and ESPs communicate with Grandeur in realtime.
Expand Down
4 changes: 2 additions & 2 deletions src/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ void Grandeur::Project::Device::Data::set(const char* path, Var data) {
oPayload["path"] = path;
oPayload["data"] = data;

// Sending the packet and scheduling callback.
_duplex->send("/device/data/set", oPayload, NULL);
// Sending the packet without response.
_duplex->send("/device/data/set", oPayload);
}

Grandeur::Project::Device::Event Grandeur::Project::Device::Data::on(const char* path, Callback cb) {
Expand Down
37 changes: 34 additions & 3 deletions src/DuplexHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void DuplexHandler::loop(bool valve) {
timeSinceLastMessage = millis();

DEBUG_GRANDEUR("Pinging Grandeur.");
send("ping", NULL);
send("ping");
}
// Running duplex loop
_client.loop();
Expand Down Expand Up @@ -111,6 +111,22 @@ Message DuplexHandler::send(const char* task, Callback cb) {
return {message.id, message.str};
}

Message DuplexHandler::send(const char* task) {
// Preparing a new message.
Message message = prepareMessage(task);

// If channel isn't connected yet, buffer the message and return.
if(_status != CONNECTED) {
_buffer.push(message.id, message.str);
return {message.id, message.str};
}

// Sending message.
sendMessage(message.str.c_str());

return {message.id, message.str};
}

Message DuplexHandler::send(const char* task, Var payload, Callback cb) {
// Preparing a new message.
Message message = prepareMessage(task, payload);
Expand All @@ -130,6 +146,22 @@ Message DuplexHandler::send(const char* task, Var payload, Callback cb) {
return {message.id, message.str};
}

Message DuplexHandler::send(const char* task, Var payload) {
// Preparing a new message.
Message message = prepareMessage(task, payload);

// If channel isn't connected yet, buffer the message and return.
if(_status != CONNECTED) {
_buffer.push(message.id, message.str);
return {message.id, message.str};
}

// Sending message.
sendMessage(message.str.c_str());

return {message.id, message.str};
}

void DuplexHandler::receive(Var header, Var payload) {
// Extracting task and id from header and code.
const char* task = header["task"];
Expand Down Expand Up @@ -254,8 +286,7 @@ void DuplexHandler::duplexEventHandler(WStype_t eventType, uint8_t* message, siz
// We do not need to handle the unpair event in Device SDKs.
if(strcmp(task, "unpair") == 0);
// Ping has no data so we simply emit.
else if(strcmp(task, "ping") == 0)
_tasks.emit((gId) header["id"], "", undefined);
else if(strcmp(task, "ping") == 0);
// If it is an update event rather than a task (response message).
else if(strcmp(task, "update") == 0)
publish(payload["event"], payload["path"], payload["update"]);
Expand Down
8 changes: 7 additions & 1 deletion src/DuplexHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ class DuplexHandler {
// Constructor
DuplexHandler();
void init(Config config);
// Sends a message to duplex channel.
// Sends a message to duplex channel:
// without payload.
Message send(const char* task, Callback cb);
// without payload, without response.
Message send(const char* task);
// with payload.
Message send(const char* task, Var payload, Callback cb);
// with payload, without response.
Message send(const char* task, Var payload);

// Subscribes to a topic.
gId subscribe(const char* topic, Var payload, Callback updateHandler);
Expand Down
57 changes: 54 additions & 3 deletions src/arduinoWebSockets/SocketIOclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,59 @@ SocketIOclient::~SocketIOclient() {
void SocketIOclient::begin(const char * host, uint16_t port, const char * url, const char * protocol) {
WebSocketsClient::beginSocketIO(host, port, url, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
}

void SocketIOclient::begin(String host, uint16_t port, String url, String protocol) {
WebSocketsClient::beginSocketIO(host, port, url, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
}
#if defined(HAS_SSL)
void SocketIOclient::beginSSL(const char * host, uint16_t port, const char * url, const char * protocol) {
WebSocketsClient::beginSocketIOSSL(host, port, url, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
}

void SocketIOclient::beginSSL(String host, uint16_t port, String url, String protocol) {
WebSocketsClient::beginSocketIOSSL(host, port, url, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
}
#if defined(SSL_BARESSL)
void SocketIOclient::beginSSLWithCA(const char * host, uint16_t port, const char * url, const char * CA_cert, const char * protocol) {
WebSocketsClient::beginSocketIOSSLWithCA(host, port, url, CA_cert, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
}

void SocketIOclient::beginSSLWithCA(const char * host, uint16_t port, const char * url, BearSSL::X509List * CA_cert, const char * protocol) {
WebSocketsClient::beginSocketIOSSLWithCA(host, port, url, CA_cert, protocol);
WebSocketsClient::enableHeartbeat(60 * 1000, 90 * 1000, 5);
initClient();
}

void SocketIOclient::setSSLClientCertKey(const char * clientCert, const char * clientPrivateKey) {
WebSocketsClient::setSSLClientCertKey(clientCert, clientPrivateKey);
}

void SocketIOclient::setSSLClientCertKey(BearSSL::X509List * clientCert, BearSSL::PrivateKey * clientPrivateKey) {
WebSocketsClient::setSSLClientCertKey(clientCert, clientPrivateKey);
}

#endif
#endif

void SocketIOclient::configureEIOping(bool disableHeartbeat) {
_disableHeartbeat = disableHeartbeat;
}

void SocketIOclient::initClient(void) {
if(_client.cUrl.indexOf("EIO=4") != -1) {
DEBUG_WEBSOCKETS("[wsIOc] found EIO=4 disable EIO ping on client\n");
configureEIOping(true);
}
}

/**
Expand Down Expand Up @@ -51,7 +99,7 @@ bool SocketIOclient::send(socketIOmessageType_t type, uint8_t * payload, size_t
if(length == 0) {
length = strlen((const char *)payload);
}
if(clientIsConnected(&_client)) {
if(clientIsConnected(&_client) && _client.status == WSC_CONNECTED) {
if(!headerToPayload) {
// webSocket Header
ret = WebSocketsClient::sendFrameHeader(&_client, WSop_text, length + 2, true);
Expand Down Expand Up @@ -118,8 +166,8 @@ bool SocketIOclient::sendEVENT(String & payload) {
void SocketIOclient::loop(void) {
WebSocketsClient::loop();
unsigned long t = millis();
if((t - _lastConnectionFail) > EIO_HEARTBEAT_INTERVAL) {
_lastConnectionFail = t;
if(!_disableHeartbeat && (t - _lastHeartbeat) > EIO_HEARTBEAT_INTERVAL) {
_lastHeartbeat = t;
DEBUG_WEBSOCKETS("[wsIOc] send ping\n");
WebSocketsClient::sendTXT(eIOtype_PING);
}
Expand All @@ -135,6 +183,7 @@ void SocketIOclient::handleCbEvent(WStype_t type, uint8_t * payload, size_t leng
DEBUG_WEBSOCKETS("[wsIOc] Connected to url: %s\n", payload);
// send message to server when Connected
// Engine.io upgrade confirmation message (required)
WebSocketsClient::sendTXT("2probe");
WebSocketsClient::sendTXT(eIOtype_UPGRADE);
runIOCbEvent(sIOtype_CONNECT, payload, length);
} break;
Expand Down Expand Up @@ -165,6 +214,8 @@ void SocketIOclient::handleCbEvent(WStype_t type, uint8_t * payload, size_t leng
DEBUG_WEBSOCKETS("[wsIOc] get event (%d): %s\n", lData, data);
break;
case sIOtype_CONNECT:
DEBUG_WEBSOCKETS("[wsIOc] connected (%d): %s\n", lData, data);
return;
case sIOtype_DISCONNECT:
case sIOtype_ACK:
case sIOtype_ERROR:
Expand Down
15 changes: 15 additions & 0 deletions src/arduinoWebSockets/SocketIOclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class SocketIOclient : protected WebSocketsClient {
void begin(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void begin(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");

#ifdef HAS_SSL
void beginSSL(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * protocol = "arduino");
void beginSSL(String host, uint16_t port, String url = "/socket.io/?EIO=3", String protocol = "arduino");
#ifndef SSL_AXTLS
void beginSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", const char * CA_cert = NULL, const char * protocol = "arduino");
void beginSSLWithCA(const char * host, uint16_t port, const char * url = "/socket.io/?EIO=3", BearSSL::X509List * CA_cert = NULL, const char * protocol = "arduino");
void setSSLClientCertKey(const char * clientCert = NULL, const char * clientPrivateKey = NULL);
void setSSLClientCertKey(BearSSL::X509List * clientCert = NULL, BearSSL::PrivateKey * clientPrivateKey = NULL);
#endif
#endif
bool isConnected(void);

void onEvent(SocketIOclientEvent cbEvent);
Expand All @@ -67,7 +77,10 @@ class SocketIOclient : protected WebSocketsClient {

void loop(void);

void configureEIOping(bool disableHeartbeat = false);

protected:
bool _disableHeartbeat = false;
uint64_t _lastHeartbeat = 0;
SocketIOclientEvent _cbEvent;
virtual void runIOCbEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
Expand All @@ -76,6 +89,8 @@ class SocketIOclient : protected WebSocketsClient {
}
}

void initClient(void);

// Handeling events from websocket layer
virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
handleCbEvent(type, payload, length);
Expand Down
22 changes: 17 additions & 5 deletions src/arduinoWebSockets/WebSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ extern "C" {
#ifdef ESP8266
#include <Hash.h>
#elif defined(ESP32)
#include <esp_system.h>

#if ESP_IDF_VERSION_MAJOR >= 4
#include <esp32/sha.h>
#else
#include <hwcrypto/sha.h>
#endif

#else

extern "C" {
Expand Down Expand Up @@ -494,7 +501,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
reasonCode = payload[0] << 8 | payload[1];
}
#endif
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d", client->num, reasonCode);
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] get ask for close. Code: %d\n", client->num, reasonCode);
if(header->payloadLen > 2) {
DEBUG_WEBSOCKETS(" (%s)\n", (payload + 2));
} else {
Expand All @@ -503,6 +510,7 @@ void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t
clientDisconnect(client, 1000);
} break;
default:
DEBUG_WEBSOCKETS("[WS][%d][handleWebsocket] got unknown opcode: %d\n", client->num, header->opCode);
clientDisconnect(client, 1002);
break;
}
Expand Down Expand Up @@ -623,7 +631,7 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
}

if(!client->tcp->available()) {
WEBSOCKETS_YIELD();
WEBSOCKETS_YIELD_MORE();
continue;
}

Expand All @@ -636,7 +644,9 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
} else {
//DEBUG_WEBSOCKETS("Receive %d left %d!\n", len, n);
}
WEBSOCKETS_YIELD();
if(n > 0) {
WEBSOCKETS_YIELD();
}
}
if(cb) {
cb(client, true);
Expand Down Expand Up @@ -686,9 +696,11 @@ size_t WebSockets::write(WSclient_t * client, uint8_t * out, size_t n) {
total += len;
//DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
} else {
//DEBUG_WEBSOCKETS("write %d failed left %d!\n", len, n);
DEBUG_WEBSOCKETS("WS write %d failed left %d!\n", len, n);
}
if(n > 0) {
WEBSOCKETS_YIELD();
}
WEBSOCKETS_YIELD();
}
WEBSOCKETS_YIELD();
return total;
Expand Down
Loading