generate DevEUI based on macaddr if not defined

This commit is contained in:
geeksville 2020-01-18 14:11:00 -08:00
parent c6e1053ed1
commit 4018141a45
2 changed files with 52 additions and 10 deletions

View File

@ -7,8 +7,8 @@ Credentials file
#pragma once #pragma once
// Only one of these settings must be defined // Only one of these settings must be defined
#define USE_ABP //#define USE_ABP
//#define USE_OTAA #define USE_OTAA
#ifdef USE_ABP #ifdef USE_ABP
@ -31,7 +31,8 @@ Credentials file
static const u1_t PROGMEM APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const u1_t PROGMEM APPEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// This should also be in little endian format, see above. // This should also be in little endian format, see above.
static const u1_t PROGMEM DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Note: You do not need to set this field, if unset it will be generated automatically based on the device macaddr
static u1_t DEVEUI[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// This key should be in big endian format (or, since it is not really a // This key should be in big endian format (or, since it is not really a
// number but a block of memory, endianness does not really apply). In // number but a block of memory, endianness does not really apply). In

View File

@ -58,7 +58,7 @@ void os_getDevKey (u1_t* buf) { }
#ifdef USE_OTAA #ifdef USE_OTAA
void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); } void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8); }
void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8); } void os_getDevEui (u1_t* buf) { memcpy(buf, DEVEUI, 8); }
void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); } void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16); }
#endif #endif
@ -91,6 +91,24 @@ void forceTxSingleChannelDr() {
} }
// DevEUI generator using devices's MAC address - from https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/lorawan.cpp
void gen_lora_deveui(uint8_t *pdeveui) {
uint8_t *p = pdeveui, dmac[6];
int i = 0;
esp_efuse_mac_get_default(dmac);
// deveui is LSB, we reverse it so TTN DEVEUI display
// will remain the same as MAC address
// MAC is 6 bytes, devEUI 8, set first 2 ones
// with an arbitrary value
*p++ = 0xFF;
*p++ = 0xFE;
// Then next 6 bytes are mac address reversed
for (i = 0; i < 6; i++) {
*p++ = dmac[5 - i];
}
}
static void printHex2(unsigned v) { static void printHex2(unsigned v) {
v &= 0xff; v &= 0xff;
if (v < 16) if (v < 16)
@ -98,6 +116,24 @@ static void printHex2(unsigned v) {
Serial.print(v, HEX); Serial.print(v, HEX);
} }
// generate DevEUI from macaddr if needed
void initDevEUI() {
bool needInit = true;
for(int i = 0; i < sizeof(DEVEUI); i++)
if(DEVEUI[i]) needInit = false;
if(needInit)
gen_lora_deveui(DEVEUI);
Serial.print("DevEUI: ");
for(int i = 0; i < sizeof(DEVEUI); i++) {
if (i != 0)
Serial.print("-");
printHex2(DEVEUI[i]);
}
Serial.println();
}
// LMIC library will call this method when an event is fired // LMIC library will call this method when an event is fired
void onEvent(ev_t event) { void onEvent(ev_t event) {
switch(event) { switch(event) {
@ -126,16 +162,16 @@ void onEvent(ev_t event) {
Serial.println(devaddr, HEX); Serial.println(devaddr, HEX);
Serial.print("AppSKey: "); Serial.print("AppSKey: ");
for (size_t i=0; i<sizeof(artKey); ++i) { for (size_t i=0; i<sizeof(artKey); ++i) {
if (i != 0) if (i != 0)
Serial.print("-"); Serial.print("-");
printHex2(artKey[i]); printHex2(artKey[i]);
} }
Serial.println(""); Serial.println("");
Serial.print("NwkSKey: "); Serial.print("NwkSKey: ");
for (size_t i=0; i<sizeof(nwkKey); ++i) { for (size_t i=0; i<sizeof(nwkKey); ++i) {
if (i != 0) if (i != 0)
Serial.print("-"); Serial.print("-");
printHex2(nwkKey[i]); printHex2(nwkKey[i]);
} }
Serial.println(); Serial.println();
@ -199,6 +235,7 @@ static void initCount() {
bool ttn_setup() { bool ttn_setup() {
initCount(); initCount();
initDevEUI();
// SPI interface // SPI interface
SPI.begin(SCK_GPIO, MISO_GPIO, MOSI_GPIO, NSS_GPIO); SPI.begin(SCK_GPIO, MISO_GPIO, MOSI_GPIO, NSS_GPIO);
@ -241,6 +278,10 @@ void ttn_join() {
// but only one group of 8 should (a subband) should be active // but only one group of 8 should (a subband) should be active
// TTN recommends the second sub band, 1 in a zero based count. // TTN recommends the second sub band, 1 in a zero based count.
// https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
// in the US, with TTN, it saves join time if we start on subband 1
// (channels 8-15). This will get overridden after the join by
// parameters from the network. If working with other networks or in
// other regions, this will need to be changed.
LMIC_selectSubBand(1); LMIC_selectSubBand(1);
#endif #endif