Update processCommand()

This commit is contained in:
Felix Rusu 2020-05-18 19:46:36 -04:00
parent 21a981a44f
commit 3940d55b46
2 changed files with 98 additions and 23 deletions

View File

@ -31,7 +31,7 @@
#define ENABLE_WIRELESS_PROGRAMMING //comment out this line to disable Wireless Programming of this gateway node #define ENABLE_WIRELESS_PROGRAMMING //comment out this line to disable Wireless Programming of this gateway node
//#define ENABLE_LCD //comment this out if you don't have or don't want to use the LCD //#define ENABLE_LCD //comment this out if you don't have or don't want to use the LCD
//***************************************************************************************************************************** //*****************************************************************************************************************************
#define SERIAL_BAUD 19200 #define SERIAL_BAUD 115200 //change to 19200 if ENABLE_LCD is left uncommented
#define DEBUG_EN //comment out if you don't want any serial verbose output (keep out in real use) #define DEBUG_EN //comment out if you don't want any serial verbose output (keep out in real use)
#define BTN_LED_RED 9 #define BTN_LED_RED 9
@ -90,7 +90,7 @@
//******************************************** BEGIN ADVANCED variables ******************************************************************************** //******************************************** BEGIN ADVANCED variables ********************************************************************************
#define RAMSIZE 2048 #define RAMSIZE 2048
#define MAX_BUFFER_LENGTH 25 //limit parameter update requests to 20 chars. ex: Parameter:LongRequest #define MAX_BUFFER_LENGTH 40 //limit parameter update requests to 40 chars. ex: Parameter:LongRequest
#define MAX_ACK_REQUEST_LENGTH 30 //60 is max for ACK (with ATC enabled), but need to allow appending :OK and :INV to confirmations from node #define MAX_ACK_REQUEST_LENGTH 30 //60 is max for ACK (with ATC enabled), but need to allow appending :OK and :INV to confirmations from node
typedef struct req { typedef struct req {
@ -656,18 +656,17 @@ void setup() {
#ifdef IS_RFM69HW_HCW #ifdef IS_RFM69HW_HCW
radio.setHighPower(); radio.setHighPower();
#endif #endif
Pbuff=""; Pbuff="SYSFREQ:";
Pbuff << "Listening @ " << radio.getFrequency() << "Hz"; Pbuff << radio.getFrequency();
DEBUGln(buff); DEBUGln(buff);
if (flash.initialize()) DEBUGln("SPI_Flash_Init_OK"); if (!flash.initialize()) DEBUGln(F("DEBUG:SPI_Flash_Init_FAIL"));
else DEBUGln(F("SPI_Flash_Init_FAIL"));
#ifdef FREQUENCY_EXACT #ifdef FREQUENCY_EXACT
radio.setFrequency(FREQUENCY_EXACT); //set frequency to some custom frequency radio.setFrequency(FREQUENCY_EXACT); //set frequency to some custom frequency
#endif #endif
readBattery(); readBattery();
DEBUG(F("DEBUG:freeRAM():"));DEBUGln(freeRAM()); DEBUG(F("FREERAM:"));DEBUGln(freeRAM());
#ifdef ENABLE_LCD #ifdef ENABLE_LCD
pinMode(PIN_LCD_LIGHT, OUTPUT); //LCD backlight, LOW = backlight ON pinMode(PIN_LCD_LIGHT, OUTPUT); //LCD backlight, LOW = backlight ON
@ -770,9 +769,11 @@ boolean insert(uint16_t new_id, char new_data[]) {
//processCommand - parse the command and send it to target //processCommand - parse the command and send it to target
//if target is non-responsive it(sleeppy node?) then queue command to send when target wakes and asks for an ACK //if target is non-responsive it(sleeppy node?) then queue command to send when target wakes and asks for an ACK
//SPECIAL COMMANDS FROM HOST: //SPECIAL COMMANDS FROM HOST:
// - REQUESTQUEUE:123:MESSAGE - send or (upon fail) queue message
// - 123:VOID - removes all queued commands for node 123 // - 123:VOID - removes all queued commands for node 123
// - 123:VOID:command - removes 'command' from queue (if found) // - 123:VOID:command - removes 'command' from queue (if found)
// - REQUESTQUEUE - prints the queued list of nodes on serial port, to host (Pi?) // - REQUESTQUEUE - prints the queued list of nodes on serial port, to host (Pi?)
// - REQUESTQUEUE:VOID - flush entire queue
// - FREERAM - returns # of unallocated bytes at end of heap // - FREERAM - returns # of unallocated bytes at end of heap
// - SYSFREQ - returns operating frequency in Hz // - SYSFREQ - returns operating frequency in Hz
// - UPTIME - returns millis() // - UPTIME - returns millis()
@ -781,16 +782,23 @@ void processCommand(char data[], boolean allowDuplicate=false) {
char dataPart[MAX_BUFFER_LENGTH]; char dataPart[MAX_BUFFER_LENGTH];
uint16_t targetId; uint16_t targetId;
byte sendLen = 0; byte sendLen = 0;
byte isQueueRequest = false;
ptr = strtok(data, ":"); ptr = strtok(data, ":");
if (strcmp(data, "FREERAM")==0) if (strcmp(data, "FREERAM")==0)
Serial << F("FREERAM:") << freeRAM() << ':' << RAMSIZE << endl; Serial << F("FREERAM:") << freeRAM() << ':' << RAMSIZE << endl;
if (strcmp(data, "REQUESTQUEUE")==0) if (strcmp(data, "REQUESTQUEUE")==0)
printQueue(queue); {
ptr = strtok(NULL, ":"); //move to next :
if (ptr == NULL) printQueue(queue);
else isQueueRequest = true;
}
if (strcmp(data, "SYSFREQ")==0) if (strcmp(data, "SYSFREQ")==0)
Serial << F("SYSFREQ:") << radio.getFrequency() << endl; Serial << F("SYSFREQ:") << radio.getFrequency() << endl;
if (strcmp(data, "UPTIME")==0) if (strcmp(data, "UPTIME")==0)
Serial << F("UPTIME:") << millis() << endl; Serial << F("UPTIME:") << millis() << endl;
if (strcmp(data, "NETWORKID")==0)
Serial << F("NETWORKID:") << NETWORKID << endl;
if (strcmp(data, "BEEP")==0) Beep(5, false); if (strcmp(data, "BEEP")==0) Beep(5, false);
if (strcmp(data, "BEEP2")==0) Beep(10, false); if (strcmp(data, "BEEP2")==0) Beep(10, false);
if (strcmp(data, "ENCRYPTKEY")==0) if (strcmp(data, "ENCRYPTKEY")==0)
@ -802,8 +810,36 @@ void processCommand(char data[], boolean allowDuplicate=false) {
if(ptr != NULL) { // delimiter found, valid command if(ptr != NULL) { // delimiter found, valid command
sprintf(dataPart, "%s", ptr); sprintf(dataPart, "%s", ptr);
targetId = atoi(dataPart); // get nodeID part
ptr = strtok(NULL, ""); // get command part //if "REQUESTQUEUE:VOID" then flush entire requst queue
if (isQueueRequest && strcmp(dataPart, "VOID")==0) {
REQUEST* aux = queue;
byte removed=0;
while(aux != NULL) {
if (aux == queue) {
if (aux->next == NULL) {
free(queue);
queue=NULL;
removed++;
break;
}
else {
queue = queue->next;
free(aux);
removed++;
aux = queue;
continue;
}
}
}
DEBUG("DEBUG:VOIDED_commands:");DEBUGln(removed);
size_of_queue = size_of_queue - removed;
return;
}
targetId = atoi(dataPart); // attempt to extract nodeID part
ptr = strtok(NULL, ""); // get command part to the end of the string
sprintf(dataPart, "%s", ptr); sprintf(dataPart, "%s", ptr);
//check for empty command //check for empty command
@ -826,8 +862,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
//iterate over queue //iterate over queue
aux = queue; aux = queue;
while(aux != NULL) while(aux != NULL) {
{
if (aux->nodeId==targetId) if (aux->nodeId==targetId)
{ {
if (removeAll || (!removeAll && strcmp(aux->data, dataPart+5)==0)) if (removeAll || (!removeAll && strcmp(aux->data, dataPart+5)==0))
@ -879,6 +914,8 @@ void processCommand(char data[], boolean allowDuplicate=false) {
} }
LED_LOW; LED_LOW;
if (!isQueueRequest) return; //just return at this time if not queued request
//check for duplicate //check for duplicate
if (!allowDuplicate) { if (!allowDuplicate) {
//walk queue and check for duplicates //walk queue and check for duplicates
@ -890,7 +927,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
{ {
if (strcmp(aux->data, dataPart)==0) if (strcmp(aux->data, dataPart)==0)
{ {
//DEBUGln(F("processCommand() skip (duplicate)")); DEBUGln(F("DEBUG:processCommand_skip_duplicate"));
return; return;
} }
} }
@ -918,7 +955,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
void printQueue(REQUEST* p) { void printQueue(REQUEST* p) {
if (!size_of_queue) { if (!size_of_queue) {
Serial << F("REQUESTQUEUE:VOID") << endl; Serial << F("REQUESTQUEUE:EMPTY") << endl;
return; return;
} }

View File

@ -29,7 +29,7 @@
//#define ENABLE_WIRELESS_PROGRAMMING //#define ENABLE_WIRELESS_PROGRAMMING
//***************************************************************************************************************************** //*****************************************************************************************************************************
#define DEBUG_EN //comment out if you don't want any serial verbose output #define DEBUG_EN //comment out if you don't want any serial verbose output
#define SERIAL_BAUD 19200 // Serial baud rate must match your Pi/host computer serial port baud rate! #define SERIAL_BAUD 115200 // Serial baud rate must match your Pi/host computer serial port baud rate!
//***************************************************************************************************************************** //*****************************************************************************************************************************
#ifdef DEBUG_EN #ifdef DEBUG_EN
#define DEBUG(input) Serial.print(input) #define DEBUG(input) Serial.print(input)
@ -180,9 +180,11 @@ boolean insert(uint16_t new_id, char new_data[]) {
//processCommand - parse the command and send it to target //processCommand - parse the command and send it to target
//if target is non-responsive it(sleeppy node?) then queue command to send when target wakes and asks for an ACK //if target is non-responsive it(sleeppy node?) then queue command to send when target wakes and asks for an ACK
//SPECIAL COMMANDS FROM HOST: //SPECIAL COMMANDS FROM HOST:
// - REQUESTQUEUE:123:MESSAGE - send or (upon fail) queue message
// - 123:VOID - removes all queued commands for node 123 // - 123:VOID - removes all queued commands for node 123
// - 123:VOID:command - removes 'command' from queue (if found) // - 123:VOID:command - removes 'command' from queue (if found)
// - REQUESTQUEUE - prints the queued list of nodes on serial port, to host (Pi?) // - REQUESTQUEUE - prints the queued list of nodes on serial port, to host (Pi?)
// - REQUESTQUEUE:VOID - flush entire queue
// - FREERAM - returns # of unallocated bytes at end of heap // - FREERAM - returns # of unallocated bytes at end of heap
// - SYSFREQ - returns operating frequency in Hz // - SYSFREQ - returns operating frequency in Hz
// - UPTIME - returns millis() // - UPTIME - returns millis()
@ -191,18 +193,25 @@ void processCommand(char data[], boolean allowDuplicate=false) {
char dataPart[MAX_BUFFER_LENGTH]; char dataPart[MAX_BUFFER_LENGTH];
uint16_t targetId; uint16_t targetId;
byte sendLen = 0; byte sendLen = 0;
byte isQueueRequest = false;
ptr = strtok(data, ":"); ptr = strtok(data, ":");
if (strcmp(data, "FREERAM")==0) if (strcmp(data, "FREERAM")==0)
Serial << F("FREERAM:") << freeRAM() << ':' << RAMSIZE << endl; Serial << F("FREERAM:") << freeRAM() << ':' << RAMSIZE << endl;
if (strcmp(data, "REQUESTQUEUE")==0) if (strcmp(data, "REQUESTQUEUE")==0)
printQueue(queue); {
ptr = strtok(NULL, ":"); //move to next :
if (ptr == NULL) printQueue(queue);
else isQueueRequest = true;
}
if (strcmp(data, "SYSFREQ")==0) if (strcmp(data, "SYSFREQ")==0)
Serial << F("SYSFREQ:") << radio.getFrequency() << endl; Serial << F("SYSFREQ:") << radio.getFrequency() << endl;
if (strcmp(data, "UPTIME")==0) if (strcmp(data, "UPTIME")==0)
Serial << F("UPTIME:") << millis() << endl; Serial << F("UPTIME:") << millis() << endl;
if (strcmp(data, "NETWORKID")==0) if (strcmp(data, "NETWORKID")==0)
Serial << F("NETWORKID:") << NETWORKID << endl; Serial << F("NETWORKID:") << NETWORKID << endl;
if (strcmp(data, "BEEP")==0) Beep(5, false);
if (strcmp(data, "BEEP2")==0) Beep(10, false);
if (strcmp(data, "ENCRYPTKEY")==0) if (strcmp(data, "ENCRYPTKEY")==0)
#ifdef ENCRYPTKEY #ifdef ENCRYPTKEY
Serial << F("ENCRYPTKEY:") << ENCRYPTKEY << endl; Serial << F("ENCRYPTKEY:") << ENCRYPTKEY << endl;
@ -212,8 +221,36 @@ void processCommand(char data[], boolean allowDuplicate=false) {
if(ptr != NULL) { // delimiter found, valid command if(ptr != NULL) { // delimiter found, valid command
sprintf(dataPart, "%s", ptr); sprintf(dataPart, "%s", ptr);
targetId = atoi(dataPart); // get nodeID part
ptr = strtok(NULL, ""); // get command part //if "REQUESTQUEUE:VOID" then flush entire requst queue
if (isQueueRequest && strcmp(dataPart, "VOID")==0) {
REQUEST* aux = queue;
byte removed=0;
while(aux != NULL) {
if (aux == queue) {
if (aux->next == NULL) {
free(queue);
queue=NULL;
removed++;
break;
}
else {
queue = queue->next;
free(aux);
removed++;
aux = queue;
continue;
}
}
}
DEBUG("DEBUG:VOIDED_commands:");DEBUGln(removed);
size_of_queue = size_of_queue - removed;
return;
}
targetId = atoi(dataPart); // attempt to extract nodeID part
ptr = strtok(NULL, ""); // get command part to the end of the string
sprintf(dataPart, "%s", ptr); sprintf(dataPart, "%s", ptr);
//check for empty command //check for empty command
@ -236,8 +273,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
//iterate over queue //iterate over queue
aux = queue; aux = queue;
while(aux != NULL) while(aux != NULL) {
{
if (aux->nodeId==targetId) if (aux->nodeId==targetId)
{ {
if (removeAll || (!removeAll && strcmp(aux->data, dataPart+5)==0)) if (removeAll || (!removeAll && strcmp(aux->data, dataPart+5)==0))
@ -275,7 +311,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
} }
else aux=aux->next; else aux=aux->next;
} }
DEBUG("DEBUG:VOIDED commands: ");DEBUGln(removed); DEBUG("DEBUG:VOIDED_commands:");DEBUGln(removed);
size_of_queue = size_of_queue - removed; size_of_queue = size_of_queue - removed;
return; return;
} }
@ -289,6 +325,8 @@ void processCommand(char data[], boolean allowDuplicate=false) {
} }
LED_LOW; LED_LOW;
if (!isQueueRequest) return; //just return at this time if not queued request
//check for duplicate //check for duplicate
if (!allowDuplicate) { if (!allowDuplicate) {
//walk queue and check for duplicates //walk queue and check for duplicates
@ -300,7 +338,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
{ {
if (strcmp(aux->data, dataPart)==0) if (strcmp(aux->data, dataPart)==0)
{ {
DEBUGln(F("DEBUG:processCommand() skip (duplicate)")); DEBUGln(F("DEBUG:processCommand_skip_duplicate"));
return; return;
} }
} }
@ -319,7 +357,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
} }
else else
{ {
DEBUGln(F("DEBUG:INSERT FAIL - MEM FULL!")); DEBUGln(F("DEBUG:INSERT_FAIL:MEM_FULL"));
Serial << F("[") << targetId << F("] ") << dataPart << F(":MEMFULL") << endl; Serial << F("[") << targetId << F("] ") << dataPart << F(":MEMFULL") << endl;
} }
} }