Handle internal messages sent from controller to gateway

Reduce memory footprint form W5100 gateway
This commit is contained in:
Henrik Ekblad
2015-11-18 19:53:33 +01:00
parent a6a86ba1dd
commit 2cdcb95499
2 changed files with 75 additions and 35 deletions
@@ -20,11 +20,13 @@
#include "MyGatewayTransport.h" #include "MyGatewayTransport.h"
extern boolean transportSendRoute(MyMessage &message); extern boolean transportSendRoute(MyMessage &message);
extern MyMessage _msg;
inline void gatewayTransportProcess() { inline void gatewayTransportProcess() {
if (gatewayTransportAvailable()) { if (gatewayTransportAvailable()) {
MyMessage &_msg = gatewayTransportReceive(); _msg = gatewayTransportReceive();
if (_msg.destination == GATEWAY_ADDRESS) { if (_msg.destination == GATEWAY_ADDRESS) {
// Check if sender requests an ack back. // Check if sender requests an ack back.
if (mGetRequestAck(_msg)) { if (mGetRequestAck(_msg)) {
// Copy message // Copy message
@@ -60,8 +60,13 @@ typedef struct
#endif #endif
static EthernetClient clients[MY_GATEWAY_MAX_CLIENTS]; #if defined(MY_GATEWAY_ESP8266)
static inputBuffer inputString[MY_GATEWAY_MAX_CLIENTS]; static EthernetClient clients[MY_GATEWAY_MAX_CLIENTS];
static inputBuffer inputString[MY_GATEWAY_MAX_CLIENTS];
#else
static EthernetClient client = EthernetClient();
static inputBuffer inputString;
#endif
#ifndef MY_IP_ADDRESS #ifndef MY_IP_ADDRESS
@@ -179,34 +184,67 @@ bool gatewayTransportSend(MyMessage &message)
} }
bool _readFromClient(uint8_t i) {
while (clients[i].connected() && clients[i].available()) {
char inChar = clients[i].read();
if (inputString[i].idx < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) {
// if newline then command is complete
if (inChar == '\n' || inChar == '\r') {
// Add string terminator and prepare for the next message
inputString[i].string[inputString[i].idx] = 0;
debug(PSTR("Client %d: %s\n"), i, inputString[i].string);
inputString[i].idx = 0;
if (protocolParse(_ethernetMsg, inputString[i].string)) {
return true;
}
#if defined(MY_GATEWAY_ESP8266)
bool _readFromClient(uint8_t i) {
while (clients[i].connected() && clients[i].available()) {
char inChar = clients[i].read();
if (inputString[i].idx < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) {
// if newline then command is complete
if (inChar == '\n' || inChar == '\r') {
// Add string terminator and prepare for the next message
inputString[i].string[inputString[i].idx] = 0;
debug(PSTR("Client %d: %s\n"), i, inputString[i].string);
inputString[i].idx = 0;
if (protocolParse(_ethernetMsg, inputString[i].string)) {
return true;
}
} else {
// add it to the inputString:
inputString[i].string[inputString[i].idx++] = inChar;
}
} else { } else {
// add it to the inputString: // Incoming message too long. Throw away
inputString[i].string[inputString[i].idx++] = inChar; debug(PSTR("Client %d: Message too long\n"), i);
inputString[i].idx = 0;
// Finished with this client's message. Next loop() we'll see if there's more to read.
break;
} }
} else {
// Incoming message too long. Throw away
debug(PSTR("Client %d: Message too long\n"), i);
inputString[i].idx = 0;
// Finished with this client's message. Next loop() we'll see if there's more to read.
break;
} }
return false;
} }
return false; #else
} bool _readFromClient() {
while (client.connected() && client.available()) {
char inChar = client.read();
if (inputString.idx < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) {
// if newline then command is complete
if (inChar == '\n' || inChar == '\r') {
// Add string terminator and prepare for the next message
inputString.string[inputString.idx] = 0;
debug(PSTR("Eth: %s\n"), inputString.string);
inputString.idx = 0;
if (protocolParse(_ethernetMsg, inputString.string)) {
return true;
}
} else {
// add it to the inputString:
inputString.string[inputString.idx++] = inChar;
}
} else {
// Incoming message too long. Throw away
debug(PSTR("Eth: Message too long\n"));
inputString.idx = 0;
// Finished with this client's message. Next loop() we'll see if there's more to read.
break;
}
}
return false;
}
#endif
bool gatewayTransportAvailable() bool gatewayTransportAvailable()
{ {
@@ -270,20 +308,20 @@ bool gatewayTransportAvailable()
EthernetClient newclient = _ethernetServer.available(); EthernetClient newclient = _ethernetServer.available();
// if a new client connects make sure to dispose any previous existing sockets // if a new client connects make sure to dispose any previous existing sockets
if (newclient) { if (newclient) {
if (clients[0] != newclient) { if (client != newclient) {
clients[0].stop(); client.stop();
clients[0] = newclient; client = newclient;
debug(PSTR("Client connected\n")); debug(PSTR("Eth: connect\n"));
_w5100_spi_en(false); _w5100_spi_en(false);
gatewayTransportSend(buildGw(_msg, I_GATEWAY_READY).set("Gateway startup complete.")); gatewayTransportSend(buildGw(_msg, I_GATEWAY_READY).set("Gateway startup complete."));
} }
} }
if (clients[0]) { if (client) {
if (!clients[0].connected()) { if (!client.connected()) {
debug(PSTR("Client disconnected\n")); debug(PSTR("Eth: disconnect\n"));
clients[0].stop(); client.stop();
} else { } else {
if (_readFromClient(0)) { if (_readFromClient()) {
_w5100_spi_en(false); _w5100_spi_en(false);
return true; return true;
} }