Skip to content
Open
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
111 changes: 80 additions & 31 deletions aREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,11 @@ void send_http_headers(){
void reset_status() {

#if defined(ESP8266)
Serial.print("Memory loss before reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
if (DEBUG_MODE) {
Serial.print("Memory loss before reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
}
#endif

answer = "";
Expand All @@ -284,11 +286,13 @@ void reset_status() {
//memset(&buffer[0], 0, sizeof(buffer));

#if defined(ESP8266)
Serial.print("Memory loss after reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
Serial.print("Memory free:");
Serial.println(freeMemory, DEC);
if (DEBUG_MODE) {
Serial.print("Memory loss after reset:");
Serial.println(freeMemory - ESP.getFreeHeap(),DEC);
freeMemory = ESP.getFreeHeap();
Serial.print("Memory free:");
Serial.println(freeMemory, DEC);
}
#endif

}
Expand All @@ -298,12 +302,15 @@ void reset_status() {
void handle(Adafruit_CC3000_ClientRef& client) {

if (client.available()) {
client_print_object = &client;
clientChunkSize = 32;
client_wait_time = 20;

// Handle request
handle_proto(client,true,0);

// Answer
sendBuffer(client,32,20);
sendBuffer();
client.stop();

// Reset variables for the next command
Expand All @@ -324,12 +331,15 @@ void publish(Adafruit_CC3000_ClientRef& client, String eventName, T value) {
void handle(YunClient& client) {

if (client.available()) {
client_print_object = &client;
clientChunkSize = 25;
client_wait_time = 10;

// Handle request
handle_proto(client,false,0);

// Answer
sendBuffer(client,25,10);
sendBuffer();
client.stop();

// Reset variables for the next command
Expand All @@ -350,12 +360,15 @@ void publish(YunClient& client, String eventName, T value) {
void handle(Adafruit_BLE_UART& serial) {

if (serial.available()) {
client_print_object = &serial;
clientChunkSize = 100;
client_wait_time = 1;

// Handle request
handle_proto(serial,false,0);

// Answer
sendBuffer(serial,100,1);
sendBuffer();

// Reset variables for the next command
reset_status();
Expand All @@ -375,12 +388,15 @@ void publish(Adafruit_BLE_UART& serial, String eventName, T value) {
void handle(EthernetClient& client){

if (client.available()) {
client_print_object = &client;
clientChunkSize = 50;
client_wait_time = 0;

// Handle request
handle_proto(client,true,0);

// Answer
sendBuffer(client,50,0);
sendBuffer();
client.stop();

// Reset variables for the next command
Expand All @@ -407,6 +423,9 @@ void handle(WiFiClient& client){
}

if (client.available()) {
client_print_object = &client;
clientChunkSize = 0;
client_wait_time = 0;

if (DEBUG_MODE) {
Serial.print("Memory loss before handling:");
Expand All @@ -424,7 +443,7 @@ void handle(WiFiClient& client){
}

// Answer
sendBuffer(client,0,0);
sendBuffer();
client.stop();

// Reset variables for the next command
Expand Down Expand Up @@ -465,14 +484,17 @@ void handle(WiFiClient& client){
void handle(WiFiClient& client){

if (client.available()) {
client_print_object = &client;
clientChunkSize = 50;
client_wait_time = 1;

if (DEBUG_MODE) {Serial.println("Request received");}

// Handle request
handle_proto(client,true,0);

// Answer
sendBuffer(client,50,1);
sendBuffer();
client.stop();

// Reset variables for the next command
Expand All @@ -493,12 +515,15 @@ void publish(WiFiClient& client, String eventName, T value) {
void handle(usb_serial_class& serial){

if (serial.available()) {
client_print_object = &serial;
clientChunkSize = 25;
client_wait_time = 1;

// Handle request
handle_proto(serial,false,1);

// Answer
sendBuffer(serial,25,1);
sendBuffer();

// Reset variables for the next command
reset_status();
Expand All @@ -518,12 +543,15 @@ void publish(usb_serial_class& client, String eventName, T value) {
void handle(Serial_& serial){

if (serial.available()) {
client_print_object = &serial;
clientChunkSize = 25;
client_wait_time = 1;

// Handle request
handle_proto(serial,false,1);

// Answer
sendBuffer(serial,25,1);
sendBuffer();

// Reset variables for the next command
reset_status();
Expand All @@ -543,12 +571,15 @@ void publish(Serial_& client, String eventName, T value) {
void handle(HardwareSerial& serial){

if (serial.available()) {
client_print_object = &serial;
clientChunkSize = 25;
client_wait_time = 1;

// Handle request
handle_proto(serial,false,1);

// Answer
sendBuffer(serial,25,1);
sendBuffer();

// Reset variables for the next command
reset_status();
Expand Down Expand Up @@ -1397,8 +1428,19 @@ void removeLastBufferChar() {

}

void sendBufferIfNeeded(size_t len_needed)
{
if (index + len_needed >= OUTPUT_BUFFER_SIZE-1)
{
sendBuffer();
}
}

// Add to output buffer
void addToBuffer(char * toAdd){
size_t toAddLen = strlen(toAdd);

sendBufferIfNeeded(toAddLen);

if (DEBUG_MODE) {
#if defined(ESP8266)
Expand All @@ -1410,15 +1452,16 @@ void addToBuffer(char * toAdd){
Serial.println(toAdd);
}

for (int i = 0; i < strlen(toAdd); i++){
for (int i = 0; i < toAddLen; i++){
buffer[index+i] = toAdd[i];
}
index = index + strlen(toAdd);
index = index + toAddLen;
}

// Add to output buffer
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(ESP8266) || defined(CORE_WILDFIRE) || !defined(ADAFRUIT_CC3000_H)
void addToBuffer(String toAdd){
sendBufferIfNeeded(toAdd.length());

if (DEBUG_MODE) {
#if defined(ESP8266)
Expand Down Expand Up @@ -1484,6 +1527,7 @@ void addToBuffer(const __FlashStringHelper *toAdd){
PGM_P p = reinterpret_cast<PGM_P>(toAdd);

while (1) {
sendBufferIfNeeded(idx+1);
unsigned char c = pgm_read_byte(p++);
if (c == 0) break;
buffer[index + idx] = c;
Expand All @@ -1492,8 +1536,7 @@ void addToBuffer(const __FlashStringHelper *toAdd){
index = index + idx;
}

template <typename T>
void sendBuffer(T& client, uint8_t chunkSize, uint8_t wait_time) {
void sendBuffer() {

if (DEBUG_MODE) {
#if defined(ESP8266)
Expand All @@ -1506,31 +1549,31 @@ void sendBuffer(T& client, uint8_t chunkSize, uint8_t wait_time) {
}

// Send all of it
if (chunkSize == 0) {
client.print(buffer);
if (clientChunkSize == 0) {
client_print_object->print(buffer);
}

// Send chunk by chunk
else {

// Max iteration
uint8_t max_iteration = (int)(index/chunkSize) + 1;
uint8_t max_iteration = (int)(index/clientChunkSize) + 1;

// Send data
for (uint8_t i = 0; i < max_iteration; i++) {
char intermediate_buffer[chunkSize+1];
memcpy(intermediate_buffer, buffer + i*chunkSize, chunkSize);
intermediate_buffer[chunkSize] = '\0';
char intermediate_buffer[clientChunkSize+1];
memcpy(intermediate_buffer, buffer + i*clientChunkSize, clientChunkSize);
intermediate_buffer[clientChunkSize] = '\0';

// Send intermediate buffer
#ifdef ADAFRUIT_CC3000_H
client.fastrprint(intermediate_buffer);
((Adafruit_CC3000_ClientRef*)client_print_object)->fastrprint(intermediate_buffer);
#else
client.print(intermediate_buffer);
client_print_object->print(intermediate_buffer);
#endif

// Wait for client to get data
delay(wait_time);
delay(client_wait_time);

if (DEBUG_MODE) {
Serial.print(F("Sent buffer: "));
Expand Down Expand Up @@ -1560,6 +1603,7 @@ char * getBuffer() {
void resetBuffer(){

memset(&buffer[0], 0, sizeof(buffer));
index = 0;
// free(buffer);

}
Expand Down Expand Up @@ -1602,7 +1646,12 @@ void setMQTTServer(char* new_mqtt_server){
char id[ID_SIZE+1];
String arguments;

// Output uffer
// Pointer to just the Print object of the client
Print *client_print_object;
uint8_t clientChunkSize = 0;
uint8_t client_wait_time = 0;

// Output buffer
char buffer[OUTPUT_BUFFER_SIZE];
uint16_t index;

Expand Down