Skip to content

Commit afc0cd4

Browse files
NicolasdejeanFokkeZB
authored andcommitted
add return messages for join and personalize + rebased (#143)
1 parent cd12be2 commit afc0cd4

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

src/TheThingsNetwork.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ const char join_failed[] PROGMEM = "Send join command failed";
6666
const char join_not_accepted[] PROGMEM = "Join not accepted: ";
6767
const char personalize_not_accepted[] PROGMEM = "Personalize not accepted";
6868
const char response_is_not_ok[] PROGMEM = "Response is not OK: ";
69+
const char error_key_length[] PROGMEM = "One or more keys are of invalid length.";
70+
const char check_configuration[] PROGMEM = "Check your coverage, keys and backend status.";
6971

70-
const char* const error_msg[] PROGMEM = {invalid_sf,invalid_fp,unexpected_response,send_command_failed,join_failed,join_not_accepted,personalize_not_accepted,response_is_not_ok};
72+
const char* const error_msg[] PROGMEM = {invalid_sf,invalid_fp,unexpected_response,send_command_failed,join_failed,join_not_accepted,personalize_not_accepted,response_is_not_ok,error_key_length,check_configuration};
7173

7274
#define ERR_INVALID_SF 0
7375
#define ERR_INVALID_FP 1
@@ -77,6 +79,8 @@ const char* const error_msg[] PROGMEM = {invalid_sf,invalid_fp,unexpected_respon
7779
#define ERR_JOIN_NOT_ACCEPTED 5
7880
#define ERR_PERSONALIZE_NOT_ACCEPTED 6
7981
#define ERR_RESPONSE_IS_NOT_OK 7
82+
#define ERR_KEY_LENGTH 8
83+
#define ERR_CHECK_CONFIGURATION 9
8084

8185
const char personalize_accepted[] PROGMEM = "Personalize accepted. Status: ";
8286
const char join_accepted[] PROGMEM = "Join accepted. Status: ";
@@ -320,10 +324,14 @@ void TheThingsNetwork::onMessage(void (*cb)(const byte* payload, size_t length,
320324

321325
bool TheThingsNetwork::personalize(const char *devAddr, const char *nwkSKey, const char *appSKey) {
322326
reset();
323-
sendMacSet(MAC_SET_DEVICEADDRESS, devAddr);
324-
sendMacSet(MAC_SET_NWKSKEY, nwkSKey);
325-
sendMacSet(MAC_SET_APPSKEY, appSKey);
326-
return personalize();
327+
if (bufLength(devAddr) == 8 && bufLength(appSKey) == 32 && bufLength(nwkSKey) == 32) {
328+
sendMacSet(MAC_SET_DEVICEADDRESS, devAddr);
329+
sendMacSet(MAC_SET_NWKSKEY, nwkSKey);
330+
sendMacSet(MAC_SET_APPSKEY, appSKey);
331+
return personalize();
332+
}
333+
stateMessage(ERR_MESSAGE, ERR_KEY_LENGTH);
334+
return false;
327335
}
328336

329337
bool TheThingsNetwork::personalize() {
@@ -332,6 +340,7 @@ bool TheThingsNetwork::personalize() {
332340
const char *response = readLine();
333341
if (!compareStrings(response, CMP_ACCEPTED)) {
334342
stateMessage(ERR_MESSAGE, ERR_PERSONALIZE_NOT_ACCEPTED, response);
343+
stateMessage(ERR_MESSAGE, ERR_CHECK_CONFIGURATION);
335344
return false;
336345
}
337346

@@ -341,29 +350,40 @@ bool TheThingsNetwork::personalize() {
341350
}
342351

343352
bool TheThingsNetwork::provision(const char *appEui, const char *appKey) {
344-
sendMacSet(MAC_SET_APPEUI, appEui);
345-
sendMacSet(MAC_SET_APPKEY, appKey);
346-
debugPrint(SENDING);
347-
sendCommand(MAC_TABLE, MAC_PREFIX, true);
348-
sendCommand(MAC_TABLE, MAC_SAVE, false);
349-
modemStream->write(SEND_MSG);
350-
debugPrintLn();
351-
return true;
353+
if (bufLength(appEui) == 16 && bufLength(appKey) == 32) {
354+
sendMacSet(MAC_SET_APPEUI, appEui);
355+
sendMacSet(MAC_SET_APPKEY, appKey);
356+
debugPrint(SENDING);
357+
sendCommand(MAC_TABLE, MAC_PREFIX, true);
358+
sendCommand(MAC_TABLE, MAC_SAVE, false);
359+
modemStream->write(SEND_MSG);
360+
debugPrintLn();
361+
return true;
362+
}
363+
stateMessage(ERR_MESSAGE, ERR_KEY_LENGTH);
364+
return false;
352365
}
353366

354367
bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay) {
355368
configureChannels(this->sf, this->fsb);
369+
int8_t nbr_retries = retries;
356370
const char *devEui = readValue(SYS_TABLE, SYS_TABLE, SYS_GET_HWEUI);
357371
sendMacSet(MAC_SET_DEVEUI, devEui);
358372
while (--retries) {
359373
if (!sendJoinSet(MAC_JOIN_MODE_OTAA)) {
360374
stateMessage(ERR_MESSAGE, ERR_JOIN_FAILED);
375+
if ((nbr_retries - retries) >= 3) {
376+
stateMessage(ERR_MESSAGE, ERR_CHECK_CONFIGURATION);
377+
}
361378
delay(retryDelay);
362379
continue;
363380
}
364381
const char *response = readLine();
365382
if (!compareStrings(response, CMP_ACCEPTED)) {
366383
stateMessage(ERR_MESSAGE, ERR_JOIN_NOT_ACCEPTED, response);
384+
if ((nbr_retries - retries) >= 3) {
385+
stateMessage(ERR_MESSAGE, ERR_CHECK_CONFIGURATION);
386+
}
367387
delay(retryDelay);
368388
continue;
369389
}
@@ -377,7 +397,9 @@ bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay) {
377397

378398
bool TheThingsNetwork::join(const char *appEui, const char *appKey, int8_t retries, uint32_t retryDelay) {
379399
reset();
380-
provision(appEui, appKey);
400+
if (!provision(appEui, appKey)) {
401+
return false;
402+
}
381403
return join(retries, retryDelay);
382404
}
383405

0 commit comments

Comments
 (0)