From 8727a3e5cbb75e54e0e6c134ea03f41c86cef720 Mon Sep 17 00:00:00 2001 From: jpmeijers Date: Wed, 14 Jun 2017 11:58:34 +0200 Subject: [PATCH 1/8] Add infinite loop protection to readLine function. Times out after 10 seconds now. --- src/TheThingsNetwork.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index 5434efee..b37bbda9 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -348,8 +348,12 @@ void TheThingsNetwork::clearReadBuffer() size_t TheThingsNetwork::readLine(char *buffer, size_t size) { size_t read = 0; - while (read == 0) + uint8_t loops = 10; + //Try a maximum of 10x1000ms to read data. + while (read == 0 && loops>0) { + loops--; + //readBytesUntil will timeout after 1000ms if nothing is read. read = modemStream->readBytesUntil('\n', buffer, size); } buffer[read - 1] = '\0'; // set \r to \0 From f9ca19d0a71cc58ef68aa6fd2db8d3a985f5558b Mon Sep 17 00:00:00 2001 From: jpmeijers Date: Wed, 14 Jun 2017 11:59:20 +0200 Subject: [PATCH 2/8] Print error message if communication with RN2483 is unsuccessful. --- src/TheThingsNetwork.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index b37bbda9..f8fa9c16 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -591,6 +591,11 @@ void TheThingsNetwork::showStatus() { readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_HWEUI, buffer, sizeof(buffer)); debugPrintIndex(SHOW_EUI, buffer); + if(strlen(buffer)<16) + { + debugPrintMessage(ERR_MESSAGE, ERR_UNEXPECTED_RESPONSE); + return; + } readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VDD, buffer, sizeof(buffer)); debugPrintIndex(SHOW_BATTERY, buffer); readResponse(MAC_TABLE, MAC_GET_SET_TABLE, MAC_APPEUI, buffer, sizeof(buffer)); From 87c7d8748457f59e22b7b00c1afb67fa9f510f23 Mon Sep 17 00:00:00 2001 From: JP Meijers Date: Wed, 14 Jun 2017 13:09:35 +0200 Subject: [PATCH 3/8] Update TheThingsNetwork.cpp Remove waitForOk to prevent a merge conflict --- src/TheThingsNetwork.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index f8fa9c16..efc8cfa5 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -438,7 +438,6 @@ void TheThingsNetwork::saveState() sendCommand(MAC_TABLE, MAC_SAVE, false); modemStream->write(SEND_MSG); debugPrintLn(); - waitForOk(); } void TheThingsNetwork::onMessage(void (*cb)(const uint8_t *payload, size_t size, port_t port)) From 1b496c7908bf90fc21676ddf12a8af629621a09d Mon Sep 17 00:00:00 2001 From: JP Meijers Date: Sat, 17 Jun 2017 10:38:14 +0200 Subject: [PATCH 4/8] Remove comments, add spaces --- src/TheThingsNetwork.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index efc8cfa5..715bba02 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -349,11 +349,9 @@ size_t TheThingsNetwork::readLine(char *buffer, size_t size) { size_t read = 0; uint8_t loops = 10; - //Try a maximum of 10x1000ms to read data. - while (read == 0 && loops>0) + while (read == 0 && loops > 0) { loops--; - //readBytesUntil will timeout after 1000ms if nothing is read. read = modemStream->readBytesUntil('\n', buffer, size); } buffer[read - 1] = '\0'; // set \r to \0 From 18f7fd4a7c88646d53c24b41f750501a5cafebaa Mon Sep 17 00:00:00 2001 From: JP Meijers Date: Tue, 20 Jun 2017 10:14:51 +0200 Subject: [PATCH 5/8] Update TheThingsNetwork.cpp --- src/TheThingsNetwork.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index 715bba02..bbe83539 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -588,7 +588,7 @@ void TheThingsNetwork::showStatus() { readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_HWEUI, buffer, sizeof(buffer)); debugPrintIndex(SHOW_EUI, buffer); - if(strlen(buffer)<16) + if(strlen(buffer) < 16) { debugPrintMessage(ERR_MESSAGE, ERR_UNEXPECTED_RESPONSE); return; From 5eef81f15cba33e24b2d426056d75e488961d582 Mon Sep 17 00:00:00 2001 From: jpmeijers Date: Tue, 20 Jun 2017 13:01:28 +0200 Subject: [PATCH 6/8] Make readLine timeout configurable. --- src/TheThingsNetwork.cpp | 7 +++---- src/TheThingsNetwork.h | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index bbe83539..1045ec24 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -345,13 +345,12 @@ void TheThingsNetwork::clearReadBuffer() } } -size_t TheThingsNetwork::readLine(char *buffer, size_t size) +size_t TheThingsNetwork::readLine(char *buffer, size_t size, uint8_t timeout) { size_t read = 0; - uint8_t loops = 10; - while (read == 0 && loops > 0) + while (read == 0 && timeout > 0) { - loops--; + timeout--; read = modemStream->readBytesUntil('\n', buffer, size); } buffer[read - 1] = '\0'; // set \r to \0 diff --git a/src/TheThingsNetwork.h b/src/TheThingsNetwork.h index 1f4bf29e..4565c4d0 100755 --- a/src/TheThingsNetwork.h +++ b/src/TheThingsNetwork.h @@ -53,7 +53,7 @@ class TheThingsNetwork void (*messageCallback)(const uint8_t *payload, size_t size, port_t port); void clearReadBuffer(); - size_t readLine(char *buffer, size_t size); + size_t readLine(char *buffer, size_t size, uint8_t timeout = 10); size_t readResponse(uint8_t prefixTable, uint8_t indexTable, uint8_t index, char *buffer, size_t size); size_t readResponse(uint8_t table, uint8_t index, char *buffer, size_t size); From 21e5e62a04fbcc9a8ef6cac8954865faaed2f65b Mon Sep 17 00:00:00 2001 From: Johan Stokking Date: Thu, 22 Jun 2017 14:40:20 +0200 Subject: [PATCH 7/8] Fixed spacing --- src/TheThingsNetwork.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index 1045ec24..63906bf1 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -587,7 +587,7 @@ void TheThingsNetwork::showStatus() { readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_HWEUI, buffer, sizeof(buffer)); debugPrintIndex(SHOW_EUI, buffer); - if(strlen(buffer) < 16) + if (strlen(buffer) < 16) { debugPrintMessage(ERR_MESSAGE, ERR_UNEXPECTED_RESPONSE); return; From 952e413edcad0bafbacae3ddefcfab663276848c Mon Sep 17 00:00:00 2001 From: Johan Stokking Date: Thu, 22 Jun 2017 14:51:33 +0200 Subject: [PATCH 8/8] Timeout is about time, not about attempts --- src/TheThingsNetwork.cpp | 6 +++--- src/TheThingsNetwork.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index 63906bf1..fda547ce 100755 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -345,12 +345,12 @@ void TheThingsNetwork::clearReadBuffer() } } -size_t TheThingsNetwork::readLine(char *buffer, size_t size, uint8_t timeout) +size_t TheThingsNetwork::readLine(char *buffer, size_t size, uint8_t attempts) { size_t read = 0; - while (read == 0 && timeout > 0) + while (read == 0 && attempts > 0) { - timeout--; + attempts--; read = modemStream->readBytesUntil('\n', buffer, size); } buffer[read - 1] = '\0'; // set \r to \0 diff --git a/src/TheThingsNetwork.h b/src/TheThingsNetwork.h index 4565c4d0..0bb3470f 100755 --- a/src/TheThingsNetwork.h +++ b/src/TheThingsNetwork.h @@ -53,7 +53,7 @@ class TheThingsNetwork void (*messageCallback)(const uint8_t *payload, size_t size, port_t port); void clearReadBuffer(); - size_t readLine(char *buffer, size_t size, uint8_t timeout = 10); + size_t readLine(char *buffer, size_t size, uint8_t attempts = 10); size_t readResponse(uint8_t prefixTable, uint8_t indexTable, uint8_t index, char *buffer, size_t size); size_t readResponse(uint8_t table, uint8_t index, char *buffer, size_t size);