Multitech xDot Utils

Revision:
2:8d1c64670642
Parent:
1:73cea350269e
Child:
3:7fa5603c10dc
--- a/MultitechDot.cpp	Thu Jul 20 16:43:35 2017 +0300
+++ b/MultitechDot.cpp	Mon Feb 19 16:11:52 2018 +0200
@@ -2,14 +2,23 @@
 #include "plans/ChannelPlan_EU868.h"
 #include "MultitechDot.h"
 
-MultitechDot *MultitechDot::getInstance() {
+MultitechDot *MultitechDot::get_instance(struct dot_config *config) {
     using namespace lora;
+
     ChannelPlan *plan = new ChannelPlan_EU868();
-    return (MultitechDot *) mDot::getInstance(plan);
+    MultitechDot *dot = (MultitechDot *) mDot::getInstance(plan);
+
+    dot->config(config);
+
+    return dot;
 }
 
-void MultitechDot::config(uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key,
-                          uint8_t frequency_sub_band, bool public_network, uint8_t ack) {
+void MultitechDot::config(struct dot_config *config) {
+    _config = config;
+
+    this->setLogLevel(config->log_level);
+
+    logInfo("Start configuring the device");
     if (!this->getStandbyFlag()) {
         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
 
@@ -18,9 +27,6 @@
         this->resetConfig();
         this->resetNetworkSession();
 
-        // make sure library logging is turned on
-        this->setLogLevel(mts::MTSLog::INFO_LEVEL);
-
         // update configuration if necessary
         if (this->getJoinMode() != this->MANUAL) {
             logInfo("changing network join mode to MANUAL");
@@ -39,8 +45,7 @@
         //      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(network_address, network_session_key, data_session_key, frequency_sub_band,
-                                   public_network, ack);
+        this->update_manual_config(config);
 
         // save changes to configuration
         logInfo("saving configuration");
@@ -58,6 +63,10 @@
     }
 }
 
+struct dot_config *MultitechDot::get_config() {
+    return _config;
+}
+
 void MultitechDot::display_config() {
     // display configuration and library version information
     logInfo("=====================");
@@ -192,10 +201,7 @@
     }
 }
 
-void MultitechDot::update_manual_config(
-        uint8_t *network_address, uint8_t *network_session_key, uint8_t *data_session_key,
-        uint8_t frequency_sub_band, bool public_network, uint8_t ack
-) {
+void MultitechDot::update_manual_config(struct dot_config *config) {
     std::vector<uint8_t> current_network_address = this->getNetworkAddress();
     std::vector<uint8_t> current_network_session_key = this->getNetworkSessionKey();
     std::vector<uint8_t> current_data_session_key = this->getDataSessionKey();
@@ -203,9 +209,9 @@
     bool current_public_network = this->getPublicNetwork();
     uint8_t current_ack = this->getAck();
 
-    std::vector<uint8_t> network_address_vector(network_address, network_address + 4);
-    std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16);
-    std::vector<uint8_t> data_session_key_vector(data_session_key, data_session_key + 16);
+    std::vector<uint8_t> network_address_vector(config->network_address, config->network_address + 4);
+    std::vector<uint8_t> network_session_key_vector(config->network_session_key, config->network_session_key + 16);
+    std::vector<uint8_t> data_session_key_vector(config->data_session_key, config->data_session_key + 16);
 
     if (current_network_address != network_address_vector) {
         logInfo("changing network address from \"%s\" to \"%s\"",
@@ -237,25 +243,25 @@
         }
     }
 
-    if (current_frequency_sub_band != frequency_sub_band) {
-        logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band);
-        if (this->setFrequencySubBand(frequency_sub_band) != this->MDOT_OK) {
-            logError("failed to set frequency sub band to %u", frequency_sub_band);
+    if (current_frequency_sub_band != config->frequency_sub_band) {
+        logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, config->frequency_sub_band);
+        if (this->setFrequencySubBand(config->frequency_sub_band) != this->MDOT_OK) {
+            logError("failed to set frequency sub band to %u", config->frequency_sub_band);
         }
     }
 
-    if (current_public_network != public_network) {
+    if (current_public_network != config->public_network) {
         logInfo("changing public network from %s to %s", current_public_network ? "on" : "off",
-                public_network ? "on" : "off");
-        if (this->setPublicNetwork(public_network) != this->MDOT_OK) {
-            logError("failed to set public network to %s", public_network ? "on" : "off");
+                config->public_network ? "on" : "off");
+        if (this->setPublicNetwork(config->public_network) != this->MDOT_OK) {
+            logError("failed to set public network to %s", config->public_network ? "on" : "off");
         }
     }
 
-    if (current_ack != ack) {
-        logInfo("changing acks from %u to %u", current_ack, ack);
-        if (this->setAck(ack) != this->MDOT_OK) {
-            logError("failed to set acks to %u", ack);
+    if (current_ack != config->ack) {
+        logInfo("changing acks from %u to %u", current_ack, config->ack);
+        if (this->setAck(config->ack) != this->MDOT_OK) {
+            logError("failed to set acks to %u", config->ack);
         }
     }
 }
@@ -268,7 +274,7 @@
     std::vector<uint8_t> current_data_session_key = this->getDataSessionKey();
     uint32_t current_tx_frequency = this->getTxFrequency();
     uint8_t current_tx_datarate = this->getTxDataRate();
-    uint8_t current_tx_power = this->getTxPower();
+    uint32_t current_tx_power = this->getTxPower();
 
     std::vector<uint8_t> network_address_vector(network_address, network_address + 4);
     std::vector<uint8_t> network_session_key_vector(network_session_key, network_session_key + 16);
@@ -395,7 +401,7 @@
         delay_s = 10;
     }
 
-    logInfo("%ssleeping %lus", deepsleep ? "deep" : "", delay_s);
+    logInfo("%s sleeping %lus", deepsleep ? "deep" : "", delay_s);
     logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume");
 
     // lowest current consumption in sleep mode can only be achieved by configuring IOs as analog inputs with no pull resistors
@@ -599,7 +605,7 @@
 }
 
 void MultitechDot::send_data(std::vector<uint8_t> data) {
-    uint32_t ret;
+    int32_t ret;
 
     ret = this->send(data);
     if (ret != this->MDOT_OK) {