Skip to content

Commit

Permalink
Refactoring & modification for LoRaWAN v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed Oct 3, 2024
1 parent 4c8da65 commit 3e062f0
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 132 deletions.
263 changes: 132 additions & 131 deletions src/LoadSecrets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// History:
//
// 20240815 Copied from BresserWeatherSensorLW
// 20241002 Refactoring & modification for LoRaWAN v1.0.4 (requires no nwkKey)
//
// ToDo:
// -
Expand All @@ -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(
Expand All @@ -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<uint32_t>(_joinEUI >> 32), static_cast<uint32_t>(_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<uint32_t>(_devEUI >> 32), static_cast<uint32_t>(_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<uint32_t>(_joinEUI >> 32), static_cast<uint32_t>(_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<uint32_t>(_devEUI >> 32), static_cast<uint32_t>(_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<uint8_t>(tmp);
check |= _nwkKey[i];
unsigned int tmp;
sscanf(buf, "%x", &tmp);
_appKey[i] = static_cast<uint8_t>(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<uint8_t>(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<uint8_t>(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;
}
4 changes: 3 additions & 1 deletion src/LoadSecrets.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// History:
//
// 20240815 Copied from BresserWeatherSensorLW
// 20241002 Refactoring & modification for LoRaWAN v1.0.4 (requires no nwkKey)
//
// ToDo:
// -
Expand All @@ -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);
void loadSecrets(bool requireNwkKey, uint64_t &joinEUI, uint64_t &devEUI, uint8_t *nwkKey, uint8_t *appKey);

0 comments on commit 3e062f0

Please sign in to comment.