@@ -66,8 +66,10 @@ const char join_failed[] PROGMEM = "Send join command failed";
66
66
const char join_not_accepted[] PROGMEM = " Join not accepted: " ;
67
67
const char personalize_not_accepted[] PROGMEM = " Personalize not accepted" ;
68
68
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." ;
69
71
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 };
71
73
72
74
#define ERR_INVALID_SF 0
73
75
#define ERR_INVALID_FP 1
@@ -77,6 +79,8 @@ const char* const error_msg[] PROGMEM = {invalid_sf,invalid_fp,unexpected_respon
77
79
#define ERR_JOIN_NOT_ACCEPTED 5
78
80
#define ERR_PERSONALIZE_NOT_ACCEPTED 6
79
81
#define ERR_RESPONSE_IS_NOT_OK 7
82
+ #define ERR_KEY_LENGTH 8
83
+ #define ERR_CHECK_CONFIGURATION 9
80
84
81
85
const char personalize_accepted[] PROGMEM = " Personalize accepted. Status: " ;
82
86
const char join_accepted[] PROGMEM = " Join accepted. Status: " ;
@@ -320,10 +324,14 @@ void TheThingsNetwork::onMessage(void (*cb)(const byte* payload, size_t length,
320
324
321
325
bool TheThingsNetwork::personalize (const char *devAddr, const char *nwkSKey, const char *appSKey) {
322
326
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 ;
327
335
}
328
336
329
337
bool TheThingsNetwork::personalize () {
@@ -332,6 +340,7 @@ bool TheThingsNetwork::personalize() {
332
340
const char *response = readLine ();
333
341
if (!compareStrings (response, CMP_ACCEPTED)) {
334
342
stateMessage (ERR_MESSAGE, ERR_PERSONALIZE_NOT_ACCEPTED, response);
343
+ stateMessage (ERR_MESSAGE, ERR_CHECK_CONFIGURATION);
335
344
return false ;
336
345
}
337
346
@@ -341,29 +350,40 @@ bool TheThingsNetwork::personalize() {
341
350
}
342
351
343
352
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 ;
352
365
}
353
366
354
367
bool TheThingsNetwork::join (int8_t retries, uint32_t retryDelay) {
355
368
configureChannels (this ->sf , this ->fsb );
369
+ int8_t nbr_retries = retries;
356
370
const char *devEui = readValue (SYS_TABLE, SYS_TABLE, SYS_GET_HWEUI);
357
371
sendMacSet (MAC_SET_DEVEUI, devEui);
358
372
while (--retries) {
359
373
if (!sendJoinSet (MAC_JOIN_MODE_OTAA)) {
360
374
stateMessage (ERR_MESSAGE, ERR_JOIN_FAILED);
375
+ if ((nbr_retries - retries) >= 3 ) {
376
+ stateMessage (ERR_MESSAGE, ERR_CHECK_CONFIGURATION);
377
+ }
361
378
delay (retryDelay);
362
379
continue ;
363
380
}
364
381
const char *response = readLine ();
365
382
if (!compareStrings (response, CMP_ACCEPTED)) {
366
383
stateMessage (ERR_MESSAGE, ERR_JOIN_NOT_ACCEPTED, response);
384
+ if ((nbr_retries - retries) >= 3 ) {
385
+ stateMessage (ERR_MESSAGE, ERR_CHECK_CONFIGURATION);
386
+ }
367
387
delay (retryDelay);
368
388
continue ;
369
389
}
@@ -377,7 +397,9 @@ bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay) {
377
397
378
398
bool TheThingsNetwork::join (const char *appEui, const char *appKey, int8_t retries, uint32_t retryDelay) {
379
399
reset ();
380
- provision (appEui, appKey);
400
+ if (!provision (appEui, appKey)) {
401
+ return false ;
402
+ }
381
403
return join (retries, retryDelay);
382
404
}
383
405
0 commit comments