diff --git a/src/LoadSecrets.cpp b/src/LoadSecrets.cpp index 9818266..35f9a1e 100644 --- a/src/LoadSecrets.cpp +++ b/src/LoadSecrets.cpp @@ -32,6 +32,7 @@ // History: // // 20240815 Copied from BresserWeatherSensorLW +// 20241002 Refactoring & modification for LoRaWAN v1.0.4 (requires no nwkKey) // // ToDo: // - @@ -41,7 +42,7 @@ #include "LoadSecrets.h" // Load LoRaWAN secrets from file 'secrets.json' on LittleFS, if available -void loadSecrets(uint64_t &joinEUI, uint64_t &devEUI, uint8_t *nwkKey, uint8_t *appKey) +void loadSecrets(bool requireNwkKey, uint64_t &joinEUI, uint64_t &devEUI, uint8_t *nwkKey, uint8_t *appKey) { if (!LittleFS.begin( @@ -52,147 +53,147 @@ void loadSecrets(uint64_t &joinEUI, uint64_t &devEUI, uint8_t *nwkKey, uint8_t * )) { log_d("Could not initialize LittleFS."); + return; } - else + + File file = LittleFS.open("/secrets.json", "r"); + + if (!file) + { + log_i("File 'secrets.json' not found."); + return; + } + + log_d("Reading 'secrets.json'"); + JsonDocument doc; + + // Deserialize the JSON document + DeserializationError error = deserializeJson(doc, file); + file.close(); + + if (error) + { + log_d("Failed to read JSON file, using defaults."); + return; + } + + const char *joinEUIStr = doc["joinEUI"]; + if (joinEUIStr == nullptr) + { + log_e("Missing joinEUI."); + return; + } + + uint64_t _joinEUI = 0; + for (int i = 2; i < 18; i += 2) + { + char tmpStr[3] = ""; + unsigned int tmpByte; + strncpy(tmpStr, &joinEUIStr[i], 2); + sscanf(tmpStr, "%x", &tmpByte); + _joinEUI = (_joinEUI << 8) | tmpByte; + } + // printf() cannot print 64-bit hex numbers (sic!), so we split it in two 32-bit numbers... + log_d("joinEUI: 0x%08X%08X", static_cast(_joinEUI >> 32), static_cast(_joinEUI & 0xFFFFFFFF)); + + const char *devEUIStr = doc["devEUI"]; + if (devEUIStr == nullptr) { - File file = LittleFS.open("/secrets.json", "r"); + log_e("Missing devEUI."); + return; + } + + uint64_t _devEUI = 0; + for (int i = 2; i < 18; i += 2) + { + char tmpStr[3] = ""; + unsigned int tmpByte; + strncpy(tmpStr, &devEUIStr[i], 2); + sscanf(tmpStr, "%x", &tmpByte); + _devEUI = (_devEUI << 8) | tmpByte; + } + if (_devEUI == 0) + { + log_e("devEUI is zero."); + return; + } + // printf() cannot print 64-bit hex numbers (sic!), so we split it in two 32-bit numbers... + log_d("devEUI: 0x%08X%08X", static_cast(_devEUI >> 32), static_cast(_devEUI & 0xFFFFFFFF)); - if (!file) + uint8_t check = 0; + bool fail = false; + + log_i("appKey:"); + uint8_t _appKey[16]; + for (size_t i = 0; i < 16; i++) + { + const char *buf = doc["appKey"][i]; + if (buf == nullptr) { - log_i("File 'secrets.json' not found."); + fail = true; + break; } - else - { - log_d("Reading 'secrets.json'"); - JsonDocument doc; - - // Deserialize the JSON document - DeserializationError error = deserializeJson(doc, file); - if (error) - { - log_d("Failed to read JSON file, using defaults."); - } - else - { - const char *joinEUIStr = doc["joinEUI"]; - if (joinEUIStr == nullptr) - { - log_e("Missing joinEUI."); - file.close(); - return; - } - uint64_t _joinEUI = 0; - for (int i = 2; i < 18; i += 2) - { - char tmpStr[3] = ""; - unsigned int tmpByte; - strncpy(tmpStr, &joinEUIStr[i], 2); - sscanf(tmpStr, "%x", &tmpByte); - _joinEUI = (_joinEUI << 8) | tmpByte; - } - // printf() cannot print 64-bit hex numbers (sic!), so we split it in two 32-bit numbers... - log_d("joinEUI: 0x%08X%08X", static_cast(_joinEUI >> 32), static_cast(_joinEUI & 0xFFFFFFFF)); - - const char *devEUIStr = doc["devEUI"]; - if (devEUIStr == nullptr) - { - log_e("Missing devEUI."); - file.close(); - return; - } - uint64_t _devEUI = 0; - for (int i = 2; i < 18; i += 2) - { - char tmpStr[3] = ""; - unsigned int tmpByte; - strncpy(tmpStr, &devEUIStr[i], 2); - sscanf(tmpStr, "%x", &tmpByte); - _devEUI = (_devEUI << 8) | tmpByte; - } - if (_devEUI == 0) - { - log_e("devEUI is zero."); - file.close(); - return; - } - // printf() cannot print 64-bit hex numbers (sic!), so we split it in two 32-bit numbers... - log_d("devEUI: 0x%08X%08X", static_cast(_devEUI >> 32), static_cast(_devEUI & 0xFFFFFFFF)); - - uint8_t check = 0; - bool fail = false; - - log_d("nwkKey:"); - uint8_t _nwkKey[16]; - for (size_t i = 0; i < 16; i++) - { - const char *buf = doc["nwkKey"][i]; - if (buf == nullptr) - { - fail = true; - break; - } - unsigned int tmp; - sscanf(buf, "%x", &tmp); - _nwkKey[i] = static_cast(tmp); - check |= _nwkKey[i]; + unsigned int tmp; + sscanf(buf, "%x", &tmp); + _appKey[i] = static_cast(tmp); + check |= _appKey[i]; #if CORE_DEBUG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO - printf("0x%02X", _nwkKey[i]); - if (i < 15) - { - printf(", "); - } + printf("0x%02X", _appKey[i]); + if (i < 15) + { + printf(", "); + } #endif - } // for all nwk_key bytes + } // for all app_key bytes #if CORE_DEBUG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO - printf("\n"); + printf("\n"); #endif - if (fail || (check == 0)) - { - log_e("nwkKey parse error"); - file.close(); - return; - } - - check = 0; - log_i("appKey:"); - uint8_t _appKey[16]; - for (size_t i = 0; i < 16; i++) - { - const char *buf = doc["appKey"][i]; - if (buf == nullptr) - { - fail = true; - break; - } - unsigned int tmp; - sscanf(buf, "%x", &tmp); - _appKey[i] = static_cast(tmp); - check |= _appKey[i]; + if (fail || (check == 0)) + { + log_e("appKey parse error"); + return; + } + + check = 0; + if (requireNwkKey) + { + log_d("nwkKey:"); + uint8_t _nwkKey[16]; + for (size_t i = 0; i < 16; i++) + { + const char *buf = doc["nwkKey"][i]; + if (buf == nullptr) + { + fail = true; + break; + } + unsigned int tmp; + sscanf(buf, "%x", &tmp); + _nwkKey[i] = static_cast(tmp); + check |= _nwkKey[i]; #if CORE_DEBUG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO - printf("0x%02X", _appKey[i]); - if (i < 15) - { - printf(", "); - } + printf("0x%02X", _nwkKey[i]); + if (i < 15) + { + printf(", "); + } #endif - } // for all app_key bytes + } // for all nwk_key bytes #if CORE_DEBUG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO - printf("\n"); + printf("\n"); #endif - if (fail || (check == 0)) - { - log_e("appKey parse error"); - file.close(); - return; - } - - // Every check passed, copy intermediate values as result - joinEUI = _joinEUI; - devEUI = _devEUI; - memcpy(nwkKey, _nwkKey, 16); - memcpy(appKey, _appKey, 16); - } // deserializeJson o.k. - } // file read o.k. - file.close(); - } // LittleFS o.k. + if (fail || (check == 0)) + { + log_e("nwkKey parse error"); + return; + } + memcpy(nwkKey, _nwkKey, 16); + } // if (requireNwkKey) + + // Every check passed, copy intermediate values as result + joinEUI = _joinEUI; + devEUI = _devEUI; + memcpy(appKey, _appKey, 16); + + return; } diff --git a/src/LoadSecrets.h b/src/LoadSecrets.h index 9cef6a0..e17d6a9 100644 --- a/src/LoadSecrets.h +++ b/src/LoadSecrets.h @@ -32,6 +32,7 @@ // History: // // 20240815 Copied from BresserWeatherSensorLW +// 20241002 Refactoring & modification for LoRaWAN v1.0.4 (requires no nwkKey) // // ToDo: // - @@ -51,9 +52,10 @@ * Use https://github.com/earlephilhower/arduino-littlefs-upload for uploading * the file to Flash. * + * \param requireNwkKey * \param joinEUI * \param devEUI * \param nwkKey * \param appKey */ -void loadSecrets(uint64_t &joinEUI, uint64_t &devEUI, uint8_t *nwkKey, uint8_t *appKey); \ No newline at end of file +void loadSecrets(bool requireNwkKey, uint64_t &joinEUI, uint64_t &devEUI, uint8_t *nwkKey, uint8_t *appKey); \ No newline at end of file