share common init and make OTAA work correctly with a non single-ch gway

This commit is contained in:
geeksville 2020-01-17 20:49:26 -08:00
parent 6a0382cba5
commit 33483b3d53
2 changed files with 59 additions and 18 deletions

View File

@ -101,9 +101,45 @@ void sleep() {
#endif
}
void printHex2(unsigned v) {
v &= 0xff;
if (v < 16)
Serial.print('0');
Serial.print(v, HEX);
}
void callback(uint8_t message) {
if (EV_JOINING == message) screen_print("Joining TTN...\n");
if (EV_JOINED == message) screen_print("TTN joined!\n");
if (EV_JOINED == message) {
screen_print("TTN joined!\n");
Serial.println(F("EV_JOINED"));
u4_t netid = 0;
devaddr_t devaddr = 0;
u1_t nwkKey[16];
u1_t artKey[16];
LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
Serial.print("netid: ");
Serial.println(netid, DEC);
Serial.print("devaddr: ");
Serial.println(devaddr, HEX);
Serial.print("AppSKey: ");
for (size_t i=0; i<sizeof(artKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(artKey[i]);
}
Serial.println("");
Serial.print("NwkSKey: ");
for (size_t i=0; i<sizeof(nwkKey); ++i) {
if (i != 0)
Serial.print("-");
printHex2(nwkKey[i]);
}
Serial.println();
}
if (EV_JOIN_FAILED == message) screen_print("TTN join failed\n");
if (EV_REJOIN_FAILED == message) screen_print("TTN rejoin failed\n");
if (EV_RESET == message) screen_print("Reset TTN connection\n");

View File

@ -92,6 +92,11 @@ void onEvent(ev_t event) {
#ifdef SINGLE_CHANNEL_GATEWAY
forceTxSingleChannelDr();
#endif
// Disable link check validation (automatically enabled
// during join, but because slow data rates change max TX
// size, we don't use it in this example.
LMIC_setLinkCheckMode(0);
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (inc. RX win. wait)"));
@ -148,16 +153,6 @@ void ttn_join() {
LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR / 100);
#endif
#if defined(USE_ABP)
// Set static session parameters. Instead of dynamically establishing a session
// by joining the network, precomputed session parameters are be provided.
uint8_t appskey[sizeof(APPSKEY)];
uint8_t nwkskey[sizeof(NWKSKEY)];
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession(0x1, DEVADDR, nwkskey, appskey);
#if defined(CFG_eu868)
// Set up the channels used by the Things Network, which corresponds
@ -196,9 +191,6 @@ void ttn_join() {
// Disable link check validation
LMIC_setLinkCheckMode(0);
// TTN uses SF9 for its RX2 window.
LMIC.dn2Dr = DR_SF9;
#ifdef SINGLE_CHANNEL_GATEWAY
forceTxSingleChannelDr();
#else
@ -206,21 +198,34 @@ void ttn_join() {
ttn_sf(LORAWAN_SF);
#endif
#if defined(USE_ABP)
// Set static session parameters. Instead of dynamically establishing a session
// by joining the network, precomputed session parameters are be provided.
uint8_t appskey[sizeof(APPSKEY)];
uint8_t nwkskey[sizeof(NWKSKEY)];
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession(0x1, DEVADDR, nwkskey, appskey);
// TTN uses SF9 for its RX2 window.
LMIC.dn2Dr = DR_SF9;
// Trigger a false joined
_ttn_callback(EV_JOINED);
#elif defined(USE_OTAA)
#ifdef SINGLE_CHANNEL_GATEWAY
// Make LMiC initialize the default channels, choose a channel, and
// schedule the OTAA join
LMIC_startJoining();
// LMiC will already have decided to send on one of the 3 default
// channels; ensure it uses the one we want
LMIC.txChnl = SINGLE_CHANNEL_GATEWAY;
#endif
// Make LMiC initialize the default channels, choose a channel, and
// schedule the OTAA join
LMIC_startJoining();
#endif
}