Skip to content

Commit

Permalink
Adding attempts to readLine and state marker if radio module is unres…
Browse files Browse the repository at this point in the history
…ponsive
  • Loading branch information
savnik committed Aug 17, 2018
1 parent 23c933f commit 4629038
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/TheThingsNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,21 @@ void TheThingsNetwork::clearReadBuffer()
}
}

size_t TheThingsNetwork::readLine(char *buffer, size_t size)
size_t TheThingsNetwork::readLine(char *buffer, size_t size, uint8_t attempts = 3) // Default timeout value 10s and is set in class initiator
{
size_t read = 0;
while (read == 0)
while (read == 0 && attempts>0)
{
read = modemStream->readBytesUntil('\n', buffer, size);
}
buffer[read - 1] = '\0'; // set \r to \0
return read;
if(attempts<=0){ // If attempts is activated return 0 and set RN state marker
this->radioModuleInvalidState = true; // Inform the application about the radio module is not responsive.
return 0;
}
else{
buffer[read - 1] = '\0'; // set \r to \0
return read;
}
}

size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t index, char *buffer, size_t size)
Expand Down Expand Up @@ -417,7 +423,7 @@ void TheThingsNetwork::reset(bool adr)
size_t length = readResponse(SYS_TABLE, SYS_RESET, buffer, sizeof(buffer));

autoBaud();
length = readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer));
length = readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof(buffer));

// buffer contains "RN2xx3[xx] x.x.x ...", splitting model from version
char *model = strtok(buffer, " ");
Expand All @@ -436,6 +442,7 @@ void TheThingsNetwork::reset(bool adr)
sendMacSet(MAC_ADR, "off");
}
this->adr = adr;
this->radioModuleInvalidState = false;
}

void TheThingsNetwork::saveState()
Expand Down Expand Up @@ -776,8 +783,8 @@ void TheThingsNetwork::configureKR920_923()
void TheThingsNetwork::configureIN865_867()
{
sendMacSet(MAC_ADR, "off"); // TODO: remove when ADR is implemented for this plan
sendMacSet(MAC_RX2, "2 866550000"); // SF10
sendMacSet(MAC_RX2, "2 866550000"); // SF10

// Disable the three default LoRaWAN channels
sendChSet(MAC_CHANNEL_STATUS, 0, "off");
sendChSet(MAC_CHANNEL_STATUS, 1, "off");
Expand Down Expand Up @@ -1035,7 +1042,7 @@ void TheThingsNetwork::sleep(uint32_t mseconds)
}

void TheThingsNetwork::wake()
{
{
autoBaud();
}

Expand All @@ -1051,7 +1058,7 @@ void TheThingsNetwork::linkCheck(uint16_t seconds)
modemStream->write(buffer);
modemStream->write(SEND_MSG);
debugPrintLn(buffer);
waitForOk();
waitForOk();
}

uint8_t TheThingsNetwork::getLinkCheckGateways()
Expand Down
1 change: 1 addition & 0 deletions src/TheThingsNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TheThingsNetwork
bool adr;
char buffer[512];
bool baudDetermined = false;
bool radioModuleInvalidState = false;
void (*messageCallback)(const uint8_t *payload, size_t size, port_t port);

void clearReadBuffer();
Expand Down

0 comments on commit 4629038

Please sign in to comment.