Skip to content

Commit

Permalink
refactor TCPclient, make stream transmit entire line, not just one ch…
Browse files Browse the repository at this point in the history
…aracter, related to #48
  • Loading branch information
paidforby committed Apr 1, 2020
1 parent e227e86 commit a0db340
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 82 deletions.
3 changes: 2 additions & 1 deletion firmware/esp32/DisasterServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
#include "DisasterClient.h"

#define DATAGRAM_HEADER 7
#define DATAGRAM_MESSAGE 233

struct Datagram {
uint8_t destination[6];
uint8_t type; // 1 byte (typically a char) corresponding to intended client/service
uint8_t message[233]; // content of message, possibly binary data, not necessarily null-terminated
uint8_t message[DATAGRAM_MESSAGE]; // content of message, possibly binary data, not necessarily null-terminated
};

class DisasterClient;
Expand Down
58 changes: 46 additions & 12 deletions firmware/esp32/client/StreamClient.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,66 @@

#include "StreamClient.h"
#include <LoRaLayer2.h>

#define STREAM_ECHO

void StreamClient::setup()
{
stream->setTimeout(0);
stream->setTimeout(0);
inputLength = 0;
}

void StreamClient::loop()
{
if (stream->available() > 0)
if (stream->available() > 0)
{
String message = stream->readString();
size_t len = message.length();
uint8_t data[len];
memcpy(&data, message.c_str(), len);
struct Datagram datagram = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
memset(datagram.message, 0, 233);
String message = stream->readString();
size_t len = message.length();
uint8_t data[len];
memcpy(&data, message.c_str(), len);

char p = (char)data[0];

stream->write(p);

if(p == '\b') // backspace
{
if(inputLength > 0){
input[inputLength] = NULL;
inputLength--;
stream->write(" \b");
}
}
else if(p == 0x7f ) // delete
{
if(inputLength > 0){
input[inputLength] = NULL;
inputLength--;
stream->write("\b \b");
}
}
else if(p == '\r') // enter key
{
stream->write("\n");

struct Datagram datagram;
memcpy(datagram.destination, LL2.broadcastAddr(), ADDR_LENGTH);
datagram.type = 'c';
memcpy(datagram.message, data, len);
len = len + DATAGRAM_HEADER;
memcpy(datagram.message, input, inputLength);
server->transmit(this, datagram, inputLength + DATAGRAM_HEADER);

server->transmit(this, datagram, len);
memset(input, 0, DATAGRAM_MESSAGE);
inputLength = 0;
}
else
{
input[inputLength] = p;
inputLength++;
}
}
};

void StreamClient::receive(struct Datagram datagram, size_t len)
{
stream->write(datagram.message, len - DATAGRAM_HEADER);
stream->write(datagram.message, len - DATAGRAM_HEADER);
};
5 changes: 5 additions & 0 deletions firmware/esp32/client/StreamClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class StreamClient : public DisasterClient
void loop();

void receive(struct Datagram datagram, size_t len);

private:
char input[DATAGRAM_MESSAGE];
int inputLength;

};

#endif
68 changes: 39 additions & 29 deletions firmware/esp32/client/TCPClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "TCPClient.h"
#include <LoRaLayer2.h>

void TCPClient::receive(struct Datagram datagram, size_t len)
{
Expand All @@ -13,52 +14,61 @@ void TCPClient::receive(struct Datagram datagram, size_t len)

void TCPClient::handleData(void *data, size_t len)
{
struct Datagram datagram = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
memset(datagram.message, 0, 233);
datagram.type = 'c';
memcpy(datagram.message, data, len);
len = len+DATAGRAM_HEADER;
struct Datagram datagram;
memcpy(datagram.destination, LL2.broadcastAddr(), ADDR_LENGTH);
datagram.type = 'c';
memset(datagram.message, 0, DATAGRAM_MESSAGE);
memcpy(datagram.message, data, len);

server->transmit(this, datagram, len);
Serial.printf("telnet data: ");
for( int i = 0 ; i < len ; i++)
{
Serial.printf("%c", datagram.message[i]);
}
Serial.printf("\r\n");

len = len+DATAGRAM_HEADER;

server->transmit(this, datagram, len);
}

void TCPClient::handleDisconnect()
{
server->disconnect(this);
server->disconnect(this);
}

void TCPClient::handleError(int8_t error)
{
server->disconnect(this);
server->disconnect(this);
}

void TCPClient::handleTimeout(uint32_t time)
{
server->disconnect(this);
server->disconnect(this);
}

void TCPClient::startServer(AsyncServer *server, void (*callback)(TCPClient *))
{
server->onClient([callback](void *arg, AsyncClient *client) {
// add to clients list
TCPClient *tcp_client = new TCPClient(client);
server->onClient([callback](void *arg, AsyncClient *client) {
// add to clients list
TCPClient *tcp_client = new TCPClient(client);

// register events
client->onData([tcp_client](void *arg, AsyncClient *client, void *data, size_t len) {
tcp_client->handleData(data, len);
});
client->onDisconnect([tcp_client](void *arg, AsyncClient *client) {
tcp_client->handleDisconnect();
});
client->onError([tcp_client](void *arg, AsyncClient *client, int8_t error) {
tcp_client->handleError(error);
});
client->onTimeout([tcp_client](void *arg, AsyncClient *client, uint32_t time) {
tcp_client->handleTimeout(time);
});
// register events
client->onData([tcp_client](void *arg, AsyncClient *client, void *data, size_t len) {
tcp_client->handleData(data, len);
});
client->onDisconnect([tcp_client](void *arg, AsyncClient *client) {
tcp_client->handleDisconnect();
});
client->onError([tcp_client](void *arg, AsyncClient *client, int8_t error) {
tcp_client->handleError(error);
});
client->onTimeout([tcp_client](void *arg, AsyncClient *client, uint32_t time) {
tcp_client->handleTimeout(time);
});

callback(tcp_client);
},
NULL);
server->begin();
callback(tcp_client);
},
NULL);
server->begin();
}
4 changes: 1 addition & 3 deletions firmware/esp32/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ void setupSerial()
->connect(new StreamClient(&Serial));
}

/*
void setupTelnet()
{
Serial.println("* Initializing telnet server...");
Expand All @@ -293,7 +292,6 @@ void setupTelnet()
->connect(tcp_client);
});
}
*/

void setupWebSocket()
{
Expand Down Expand Up @@ -464,7 +462,7 @@ void setup()
setupSerial();
if (!useBLE)
{
//setupTelnet();
setupTelnet();
setupWebSocket();
}
else
Expand Down
40 changes: 7 additions & 33 deletions firmware/esp32/middleware/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ void Console::setup()
{
history->replay(this);
}
inputLength = 0;
sessionConnected = 0;
}

void Console::processLine(char *message, size_t len)
{
struct Datagram response;
memset(response.message, 0, 233);
memset(response.message, 0, DATAGRAM_MESSAGE);
int msgLen;

#ifdef DEBUG_OUT
Expand Down Expand Up @@ -176,8 +175,8 @@ void Console::printPrompt()
{
if (username.length() > 0)
{
char message[MAX_INPUT_LENGTH];
sprintf(message, "<%s> ", username);
char message[DATAGRAM_MESSAGE];
sprintf(message, "<%s> ", username.c_str());
print(message);
}
else
Expand All @@ -197,41 +196,16 @@ void Console::transmit(DisasterClient *client, struct Datagram datagram, size_t
Serial.println("\n" + String((char *)datagram.message));
#endif

// TODO: set sessionConnected back to zero on disconnection?
if(sessionConnected == 0){
printBanner();
printPrompt();
sessionConnected = 1;
}
// TODO: set sessionConnected back to zero on disconnection?
else if(sessionConnected == 1){
// Console should always receive datagram message of one char
// Split in case it is multi line
// \todo handle CR-LF combination
Serial.printf("%c", datagram.message[0]);

char p = (char)datagram.message[0];
if(p == '\b'){ // backspace
input[inputLength] = NULL;
inputLength--;
Serial.printf(" \b");
}
else if(p == 0x7f ){ // delete
if(inputLength > 0){
input[inputLength] = NULL;
inputLength--;
Serial.printf("\b \b");
}
}
else if(p == '\r'){ // enter key
print("\n");
processLine(input, inputLength);
memset(input, 0, MAX_INPUT_LENGTH);
inputLength = 0;
printPrompt();
}else{
input[inputLength] = p;
inputLength++;
}
// Console receives one line at a time
processLine((char *) datagram.message, len - DATAGRAM_HEADER);
printPrompt();
}
}

Expand Down
4 changes: 0 additions & 4 deletions firmware/esp32/middleware/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "../DisasterClient.h"
#include "../DisasterHistory.h"

#define MAX_INPUT_LENGTH 128

class Console : public DisasterMiddleware
{
String username = "";
Expand All @@ -29,8 +27,6 @@ class Console : public DisasterMiddleware
void printBanner();
void printPrompt();

char input[MAX_INPUT_LENGTH];
int inputLength;
int sessionConnected;
};

Expand Down

0 comments on commit a0db340

Please sign in to comment.