From 49f5e0fe54d48bfb188014ce00c8985151a1fa4b Mon Sep 17 00:00:00 2001
From: Terry Moore <tmm@mcci.com>
Date: Mon, 5 Jul 2021 10:59:33 -0400
Subject: [PATCH] Fix #171: don't use ABP info for OTAA unless saved state is
 valid

---
 src/lib/arduino_lorawan_begin.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/lib/arduino_lorawan_begin.cpp b/src/lib/arduino_lorawan_begin.cpp
index 30c0ff3..b24fb33 100644
--- a/src/lib/arduino_lorawan_begin.cpp
+++ b/src/lib/arduino_lorawan_begin.cpp
@@ -49,7 +49,8 @@ bool Arduino_LoRaWAN::begin(
     LMIC_reset();
 
     // if we can get saved state, go on.
-    if (! this->RestoreSessionState())
+    auto const fHaveSavedState = this->RestoreSessionState();
+    if (! fHaveSavedState)
         {
         // Otherwise set data rate and transmit power, based on regional considerations.
         this->NetBeginRegionInit();
@@ -69,7 +70,8 @@ bool Arduino_LoRaWAN::begin(
     //
     AbpProvisioningInfo abpInfo;
 
-    if (this->GetAbpProvisioningInfo(&abpInfo))
+    if ((fHaveSavedState || this->GetProvisioningStyle() == ProvisioningStyle::kABP) &&
+         this->GetAbpProvisioningInfo(&abpInfo))
         {
         LMIC_setSession(
                 abpInfo.NetID,