Multitech xDot Utils

Revision:
6:febbdd0d0e55
Parent:
4:db99b2a7d062
--- a/MultitechDot.cpp	Sun Jul 22 12:42:09 2018 +0300
+++ b/MultitechDot.cpp	Mon Sep 17 12:47:16 2018 +0300
@@ -6,6 +6,9 @@
     using namespace lora;
 
     ChannelPlan *plan = new ChannelPlan_EU868();
+    plan->SetNumberOfChannels(1);
+    plan->SetTxChannel(0);
+
     MultitechDot *dot = (MultitechDot *) mDot::getInstance(plan);
 
     dot->config(config);
@@ -17,9 +20,20 @@
     _config = config;
 
     this->setLogLevel(config->log_level);
-    this->setDisableDutyCycle(config->disable_duty_cycle);
 
-    logInfo("Start configuring the device");
+    if(config->join_mode == mDot::MANUAL) {
+        _manual_config();
+    } else if (config->join_mode == mDot::OTA) {
+        _ota_config();
+    }
+}
+
+struct dot_config *MultitechDot::get_config() {
+    return _config;
+}
+
+void MultitechDot::_manual_config() {
+    logInfo("MANUAL config");
     if (!this->getStandbyFlag()) {
         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
 
@@ -28,6 +42,9 @@
         this->resetConfig();
         this->resetNetworkSession();
 
+        this->setDisableDutyCycle(_config->disable_duty_cycle);
+        this->setTxDataRate(_config->data_rate);
+
         // update configuration if necessary
         if (this->getJoinMode() != this->MANUAL) {
             logInfo("changing network join mode to MANUAL");
@@ -46,10 +63,20 @@
         //      lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
         //   * if you change the network address, network session key, or data session key, make sure you update them on the gateway
         // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
-        this->update_manual_config(config);
+        BaseDot::update_manual_config(
+                _config->network_address,
+                _config->network_session_key,
+                _config->data_session_key,
+                _config->frequency_sub_band,
+                _config->public_network,
+                _config->ack
+        );
 
         // enable or disable Adaptive Data Rate
-        this->setAdr(config->adr);
+        this->setAdr(_config->adr);
+
+        // Configure the join delay
+        this->setJoinDelay(_config->join_delay);
 
         // save changes to configuration
         logInfo("saving configuration");
@@ -67,20 +94,70 @@
     }
 }
 
-struct dot_config *MultitechDot::get_config() {
-    return _config;
-}
+
+void MultitechDot::_ota_config() {
+    logInfo("OTA config");
+    if (!this->getStandbyFlag()) {
+        logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
 
+        // start from a well-known state
+        logInfo("defaulting Dot configuration");
+        this->resetConfig();
+        this->resetNetworkSession();
+
+        this->setDisableDutyCycle(_config->disable_duty_cycle);
+        this->setTxDataRate(_config->data_rate);
+
+        // make sure library logging is turned on
+        this->setLogLevel(mts::MTSLog::INFO_LEVEL);
 
-void MultitechDot::update_manual_config(struct dot_config *config) {
-    BaseDot::update_manual_config(
-            config->network_address,
-            config->network_session_key,
-            config->data_session_key,
-            config->frequency_sub_band,
-            config->public_network,
-            config->ack
-    );
+        // update configuration if necessary
+        if (this->getJoinMode() != mDot::OTA) {
+            logInfo("changing network join mode to OTA");
+            if (this->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
+                logError("failed to set network join mode to OTA");
+            }
+        }
+        // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
+        // only one method or the other should be used!
+        // network ID = crc64(network name)
+        // network KEY = cmac(network passphrase)
+        BaseDot::update_ota_config_id_key(
+                _config->network_id,
+                _config->network_key,
+                _config->frequency_sub_band,
+                _config->public_network,
+                _config->ack
+        );
+        //update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
+
+        // configure network link checks
+        // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
+        // check the link every count packets
+        // declare the Dot disconnected after threshold failed link checks
+        // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
+        update_network_link_check_config(3, 5);
+
+        // enable or disable Adaptive Data Rate
+        this->setAdr(_config->adr);
+
+        // Configure the join delay
+        this->setJoinDelay(_config->join_delay);
+
+        // save changes to configuration
+        logInfo("saving configuration");
+        if (!this->saveConfig()) {
+            logError("failed to save configuration");
+        }
+
+        // display configuration
+        display_config();
+    } else {
+        // restore the saved session if the dot woke from deepsleep mode
+        // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
+        logInfo("restoring network session from NVM");
+        this->restoreNetworkSession();
+    }
 }
 
 void MultitechDot::sleep_wake_rtc_or_interrupt(uint32_t delay_s, bool deepsleep) {