Basic xdot code to check how many memory is available for user APP after initializing libxdot lorawan stack

Committer:
jreiss
Date:
Wed Apr 24 12:50:08 2019 +0000
Revision:
33:15ea8f985c54
Child:
42:20f6b29a9903
Add Class B example; Update mbed-os to 5.11.1; Update mbed verison macros

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jreiss 33:15ea8f985c54 1 #include "dot_util.h"
jreiss 33:15ea8f985c54 2 #include "RadioEvent.h"
jreiss 33:15ea8f985c54 3
jreiss 33:15ea8f985c54 4 #if ACTIVE_EXAMPLE == CLASS_B_EXAMPLE
jreiss 33:15ea8f985c54 5
jreiss 33:15ea8f985c54 6 /////////////////////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 7 // -------------------- DOT LIBRARY REQUIRED ------------------------------//
jreiss 33:15ea8f985c54 8 // * Because these example programs can be used for both mDot and xDot //
jreiss 33:15ea8f985c54 9 // devices, the LoRa stack is not included. The libmDot library should //
jreiss 33:15ea8f985c54 10 // be imported if building for mDot devices. The libxDot library //
jreiss 33:15ea8f985c54 11 // should be imported if building for xDot devices. //
jreiss 33:15ea8f985c54 12 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ //
jreiss 33:15ea8f985c54 13 // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ //
jreiss 33:15ea8f985c54 14 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ //
jreiss 33:15ea8f985c54 15 // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ //
jreiss 33:15ea8f985c54 16 /////////////////////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 17
jreiss 33:15ea8f985c54 18 /////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 19 // * these options must match the settings on your gateway //
jreiss 33:15ea8f985c54 20 // * edit their values to match your configuration //
jreiss 33:15ea8f985c54 21 // * frequency sub band is only relevant for the 915 bands //
jreiss 33:15ea8f985c54 22 // * either the network name and passphrase can be used or //
jreiss 33:15ea8f985c54 23 // the network ID (8 bytes) and KEY (16 bytes) //
jreiss 33:15ea8f985c54 24 /////////////////////////////////////////////////////////////
jreiss 33:15ea8f985c54 25 static std::string network_name = "MultiTech";
jreiss 33:15ea8f985c54 26 static std::string network_passphrase = "MultiTech";
jreiss 33:15ea8f985c54 27 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
jreiss 33:15ea8f985c54 28 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
jreiss 33:15ea8f985c54 29 static uint8_t frequency_sub_band = 1;
jreiss 33:15ea8f985c54 30 static lora::NetworkType public_network = lora::PUBLIC_LORAWAN;
jreiss 33:15ea8f985c54 31 static uint8_t join_delay = 5;
jreiss 33:15ea8f985c54 32 static uint8_t ack = 3;
jreiss 33:15ea8f985c54 33 static bool adr = true;
jreiss 33:15ea8f985c54 34
jreiss 33:15ea8f985c54 35 // Number of ping slots to open per beacon interval - see mDot.h
jreiss 33:15ea8f985c54 36 static uint8_t ping_periodicity = 4;
jreiss 33:15ea8f985c54 37
jreiss 33:15ea8f985c54 38 mDot* dot = NULL;
jreiss 33:15ea8f985c54 39 lora::ChannelPlan* plan = NULL;
jreiss 33:15ea8f985c54 40
jreiss 33:15ea8f985c54 41 Serial pc(USBTX, USBRX);
jreiss 33:15ea8f985c54 42
jreiss 33:15ea8f985c54 43 #if defined(TARGET_XDOT_L151CC)
jreiss 33:15ea8f985c54 44 I2C i2c(I2C_SDA, I2C_SCL);
jreiss 33:15ea8f985c54 45 ISL29011 lux(i2c);
jreiss 33:15ea8f985c54 46 #else
jreiss 33:15ea8f985c54 47 AnalogIn lux(XBEE_AD0);
jreiss 33:15ea8f985c54 48 #endif
jreiss 33:15ea8f985c54 49
jreiss 33:15ea8f985c54 50 int main() {
jreiss 33:15ea8f985c54 51 // Custom event handler for automatically displaying RX data
jreiss 33:15ea8f985c54 52 RadioEvent events;
jreiss 33:15ea8f985c54 53
jreiss 33:15ea8f985c54 54 pc.baud(115200);
jreiss 33:15ea8f985c54 55
jreiss 33:15ea8f985c54 56 #if defined(TARGET_XDOT_L151CC)
jreiss 33:15ea8f985c54 57 i2c.frequency(400000);
jreiss 33:15ea8f985c54 58 #endif
jreiss 33:15ea8f985c54 59
jreiss 33:15ea8f985c54 60 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
jreiss 33:15ea8f985c54 61
jreiss 33:15ea8f985c54 62 #if CHANNEL_PLAN == CP_US915
jreiss 33:15ea8f985c54 63 plan = new lora::ChannelPlan_US915();
jreiss 33:15ea8f985c54 64 #elif CHANNEL_PLAN == CP_AU915
jreiss 33:15ea8f985c54 65 plan = new lora::ChannelPlan_AU915();
jreiss 33:15ea8f985c54 66 #elif CHANNEL_PLAN == CP_EU868
jreiss 33:15ea8f985c54 67 plan = new lora::ChannelPlan_EU868();
jreiss 33:15ea8f985c54 68 #elif CHANNEL_PLAN == CP_KR920
jreiss 33:15ea8f985c54 69 plan = new lora::ChannelPlan_KR920();
jreiss 33:15ea8f985c54 70 #elif CHANNEL_PLAN == CP_AS923
jreiss 33:15ea8f985c54 71 plan = new lora::ChannelPlan_AS923();
jreiss 33:15ea8f985c54 72 #elif CHANNEL_PLAN == CP_AS923_JAPAN
jreiss 33:15ea8f985c54 73 plan = new lora::ChannelPlan_AS923_Japan();
jreiss 33:15ea8f985c54 74 #elif CHANNEL_PLAN == CP_IN865
jreiss 33:15ea8f985c54 75 plan = new lora::ChannelPlan_IN865();
jreiss 33:15ea8f985c54 76 #endif
jreiss 33:15ea8f985c54 77 assert(plan);
jreiss 33:15ea8f985c54 78
jreiss 33:15ea8f985c54 79 dot = mDot::getInstance(plan);
jreiss 33:15ea8f985c54 80 assert(dot);
jreiss 33:15ea8f985c54 81
jreiss 33:15ea8f985c54 82 logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
jreiss 33:15ea8f985c54 83
jreiss 33:15ea8f985c54 84 // start from a well-known state
jreiss 33:15ea8f985c54 85 logInfo("defaulting Dot configuration");
jreiss 33:15ea8f985c54 86 dot->resetConfig();
jreiss 33:15ea8f985c54 87 dot->resetNetworkSession();
jreiss 33:15ea8f985c54 88
jreiss 33:15ea8f985c54 89 // make sure library logging is turned on
jreiss 33:15ea8f985c54 90 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
jreiss 33:15ea8f985c54 91
jreiss 33:15ea8f985c54 92 // attach the custom events handler
jreiss 33:15ea8f985c54 93 dot->setEvents(&events);
jreiss 33:15ea8f985c54 94
jreiss 33:15ea8f985c54 95 // update configuration if necessary
jreiss 33:15ea8f985c54 96 if (dot->getJoinMode() != mDot::OTA) {
jreiss 33:15ea8f985c54 97 logInfo("changing network join mode to OTA");
jreiss 33:15ea8f985c54 98 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 99 logError("failed to set network join mode to OTA");
jreiss 33:15ea8f985c54 100 }
jreiss 33:15ea8f985c54 101 }
jreiss 33:15ea8f985c54 102 // 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
jreiss 33:15ea8f985c54 103 // only one method or the other should be used!
jreiss 33:15ea8f985c54 104 // network ID = crc64(network name)
jreiss 33:15ea8f985c54 105 // network KEY = cmac(network passphrase)
jreiss 33:15ea8f985c54 106 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
jreiss 33:15ea8f985c54 107 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
jreiss 33:15ea8f985c54 108
jreiss 33:15ea8f985c54 109 // enable or disable Adaptive Data Rate
jreiss 33:15ea8f985c54 110 dot->setAdr(adr);
jreiss 33:15ea8f985c54 111
jreiss 33:15ea8f985c54 112 // Configure the join delay
jreiss 33:15ea8f985c54 113 dot->setJoinDelay(join_delay);
jreiss 33:15ea8f985c54 114
jreiss 33:15ea8f985c54 115 // Configure the class B ping periodicity
jreiss 33:15ea8f985c54 116 dot->setPingPeriodicity(ping_periodicity);
jreiss 33:15ea8f985c54 117
jreiss 33:15ea8f985c54 118 // save changes to configuration
jreiss 33:15ea8f985c54 119 logInfo("saving configuration");
jreiss 33:15ea8f985c54 120 if (!dot->saveConfig()) {
jreiss 33:15ea8f985c54 121 logError("failed to save configuration");
jreiss 33:15ea8f985c54 122 }
jreiss 33:15ea8f985c54 123
jreiss 33:15ea8f985c54 124 // display configuration
jreiss 33:15ea8f985c54 125 display_config();
jreiss 33:15ea8f985c54 126
jreiss 33:15ea8f985c54 127 // join the network - must do this before attempting to switch to class B
jreiss 33:15ea8f985c54 128 join_network();
jreiss 33:15ea8f985c54 129
jreiss 33:15ea8f985c54 130 // configure the Dot for class B operation
jreiss 33:15ea8f985c54 131 // the Dot must also be configured on the gateway for class B
jreiss 33:15ea8f985c54 132 // use the lora-query application to do this on a Conduit: http://www.multitech.net/developer/software/lora/lora-network-server/
jreiss 33:15ea8f985c54 133 // to provision your Dot for class B operation with a 3rd party gateway, see the gateway or network provider documentation
jreiss 33:15ea8f985c54 134 // Note: we won't actually switch to class B until we receive a beacon (mDotEvent::BeaconRx fires)
jreiss 33:15ea8f985c54 135 logInfo("changing network mode to class B");
jreiss 33:15ea8f985c54 136
jreiss 33:15ea8f985c54 137 if (dot->setClass("B") != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 138 logError("Failed to set network mode to class B");
jreiss 33:15ea8f985c54 139 logInfo("Reset the MCU to try again");
jreiss 33:15ea8f985c54 140 return 0;
jreiss 33:15ea8f985c54 141 }
jreiss 33:15ea8f985c54 142
jreiss 33:15ea8f985c54 143 // Start a timer to check the beacon was acquired
jreiss 33:15ea8f985c54 144 LowPowerTimer bcn_timer;
jreiss 33:15ea8f985c54 145 bcn_timer.start();
jreiss 33:15ea8f985c54 146
jreiss 33:15ea8f985c54 147 while (true) {
jreiss 33:15ea8f985c54 148 std::vector<uint8_t> tx_data;
jreiss 33:15ea8f985c54 149 static bool send_uplink = true;
jreiss 33:15ea8f985c54 150
jreiss 33:15ea8f985c54 151 // Check if we locked the beacon yet and send an uplink to notify the network server
jreiss 33:15ea8f985c54 152 // To receive data from the gateway in class B ping slots, we must have received a beacon
jreiss 33:15ea8f985c54 153 // already, and sent one uplink to signal to the network server that we are in class B mode
jreiss 33:15ea8f985c54 154 if (events.BeaconLocked && send_uplink) {
jreiss 33:15ea8f985c54 155 logInfo("Acquired a beacon lock");
jreiss 33:15ea8f985c54 156
jreiss 33:15ea8f985c54 157 // Add a random delay before trying the uplink to avoid collisions w/ other motes
jreiss 33:15ea8f985c54 158 srand(dot->getRadioRandom());
jreiss 33:15ea8f985c54 159 uint32_t rand_delay = rand() % 5000;
jreiss 33:15ea8f985c54 160 logInfo("Applying a random delay of %d ms before class notification uplink", rand_delay);
jreiss 33:15ea8f985c54 161 osDelay(rand_delay);
jreiss 33:15ea8f985c54 162
jreiss 33:15ea8f985c54 163 // Ensure the link is idle before trying to transmit
jreiss 33:15ea8f985c54 164 while (!dot->getIsIdle()) {
jreiss 33:15ea8f985c54 165 osDelay(10);
jreiss 33:15ea8f985c54 166 }
jreiss 33:15ea8f985c54 167
jreiss 33:15ea8f985c54 168 if (send_data(tx_data) != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 169 logError("Failed to inform the network server we are in class B");
jreiss 33:15ea8f985c54 170 logInfo("Reset the MCU to try again");
jreiss 33:15ea8f985c54 171 return 0;
jreiss 33:15ea8f985c54 172 }
jreiss 33:15ea8f985c54 173
jreiss 33:15ea8f985c54 174 logInfo("Enqueued packets may now be scheduled on class B ping slots");
jreiss 33:15ea8f985c54 175 send_uplink = false;
jreiss 33:15ea8f985c54 176 bcn_timer.stop();
jreiss 33:15ea8f985c54 177 } else if (!events.BeaconLocked) {
jreiss 33:15ea8f985c54 178 logInfo("Waiting to receive a beacon..");
jreiss 33:15ea8f985c54 179
jreiss 33:15ea8f985c54 180 if (bcn_timer.read() > lora::DEFAULT_BEACON_PERIOD) {
jreiss 33:15ea8f985c54 181 if (dot->setClass("B") != mDot::MDOT_OK) {
jreiss 33:15ea8f985c54 182 logError("Failed to set network mode to class B");
jreiss 33:15ea8f985c54 183 logInfo("Reset the MCU to try again");
jreiss 33:15ea8f985c54 184 return 0;
jreiss 33:15ea8f985c54 185 }
jreiss 33:15ea8f985c54 186
jreiss 33:15ea8f985c54 187 bcn_timer.reset();
jreiss 33:15ea8f985c54 188 }
jreiss 33:15ea8f985c54 189 }
jreiss 33:15ea8f985c54 190
jreiss 33:15ea8f985c54 191 Thread::wait(10000);
jreiss 33:15ea8f985c54 192 }
jreiss 33:15ea8f985c54 193
jreiss 33:15ea8f985c54 194 return 0;
jreiss 33:15ea8f985c54 195 }
jreiss 33:15ea8f985c54 196
jreiss 33:15ea8f985c54 197 #endif