Basic xdot code to check how many memory is available for user APP after initializing libxdot lorawan stack
src/main.cpp@43:97fd5b4de956, 2021-07-07 (annotated)
- Committer:
- jose_23991
- Date:
- Wed Jul 07 11:34:06 2021 +0000
- Revision:
- 43:97fd5b4de956
Basic xDot Bootlaoder+ABP+FUOTA code to check available memory for user app
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jose_23991 | 43:97fd5b4de956 | 1 | |
jose_23991 | 43:97fd5b4de956 | 2 | /* Libraries */ |
jose_23991 | 43:97fd5b4de956 | 3 | |
jose_23991 | 43:97fd5b4de956 | 4 | #include "mbed.h" |
jose_23991 | 43:97fd5b4de956 | 5 | #include "mDot.h" |
jose_23991 | 43:97fd5b4de956 | 6 | #include "ChannelPlans.h" |
jose_23991 | 43:97fd5b4de956 | 7 | #include "RadioEvent.h" |
jose_23991 | 43:97fd5b4de956 | 8 | #include "dot_util.h" |
jose_23991 | 43:97fd5b4de956 | 9 | |
jose_23991 | 43:97fd5b4de956 | 10 | #if defined(FOTA) |
jose_23991 | 43:97fd5b4de956 | 11 | #include "Fota.h" |
jose_23991 | 43:97fd5b4de956 | 12 | #include "SPIFBlockDevice.h" |
jose_23991 | 43:97fd5b4de956 | 13 | #include "DataFlashBlockDevice.h" |
jose_23991 | 43:97fd5b4de956 | 14 | #endif |
jose_23991 | 43:97fd5b4de956 | 15 | |
jose_23991 | 43:97fd5b4de956 | 16 | /*****************************************************************************/ |
jose_23991 | 43:97fd5b4de956 | 17 | |
jose_23991 | 43:97fd5b4de956 | 18 | /* Connection Parameters */ |
jose_23991 | 43:97fd5b4de956 | 19 | |
jose_23991 | 43:97fd5b4de956 | 20 | // Channel Plan |
jose_23991 | 43:97fd5b4de956 | 21 | // The active channel plan is the one that will be compiled |
jose_23991 | 43:97fd5b4de956 | 22 | // CP_US915, CP_AU915, CP_EU868, CP_KR920, CP_AS923, CP_AS923_2, CP_AS923_3, |
jose_23991 | 43:97fd5b4de956 | 23 | // CP_AS923_JAPAN, CP_AS923_JAPAN1, CP_AS923_JAPAN2, CP_IN865, CP_RU864 |
jose_23991 | 43:97fd5b4de956 | 24 | #if !defined(CHANNEL_PLAN) |
jose_23991 | 43:97fd5b4de956 | 25 | #define CHANNEL_PLAN CP_EU868 |
jose_23991 | 43:97fd5b4de956 | 26 | #endif |
jose_23991 | 43:97fd5b4de956 | 27 | |
jose_23991 | 43:97fd5b4de956 | 28 | static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 }; |
jose_23991 | 43:97fd5b4de956 | 29 | static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
jose_23991 | 43:97fd5b4de956 | 30 | static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
jose_23991 | 43:97fd5b4de956 | 31 | static uint8_t frequency_sub_band = 6; |
jose_23991 | 43:97fd5b4de956 | 32 | static lora::NetworkType network_type = lora::PUBLIC_LORAWAN; |
jose_23991 | 43:97fd5b4de956 | 33 | static uint8_t join_delay = 5; |
jose_23991 | 43:97fd5b4de956 | 34 | static uint8_t ack = 1; |
jose_23991 | 43:97fd5b4de956 | 35 | static bool adr = true; |
jose_23991 | 43:97fd5b4de956 | 36 | |
jose_23991 | 43:97fd5b4de956 | 37 | /*****************************************************************************/ |
jose_23991 | 43:97fd5b4de956 | 38 | |
jose_23991 | 43:97fd5b4de956 | 39 | /* Global Elements */ |
jose_23991 | 43:97fd5b4de956 | 40 | |
jose_23991 | 43:97fd5b4de956 | 41 | mDot* dot = NULL; |
jose_23991 | 43:97fd5b4de956 | 42 | lora::ChannelPlan* plan = NULL; |
jose_23991 | 43:97fd5b4de956 | 43 | |
jose_23991 | 43:97fd5b4de956 | 44 | /*****************************************************************************/ |
jose_23991 | 43:97fd5b4de956 | 45 | |
jose_23991 | 43:97fd5b4de956 | 46 | /* Main Function */ |
jose_23991 | 43:97fd5b4de956 | 47 | |
jose_23991 | 43:97fd5b4de956 | 48 | int main() |
jose_23991 | 43:97fd5b4de956 | 49 | { |
jose_23991 | 43:97fd5b4de956 | 50 | RadioEvent events; |
jose_23991 | 43:97fd5b4de956 | 51 | |
jose_23991 | 43:97fd5b4de956 | 52 | // Create channel plan |
jose_23991 | 43:97fd5b4de956 | 53 | plan = create_channel_plan(); |
jose_23991 | 43:97fd5b4de956 | 54 | assert(plan); |
jose_23991 | 43:97fd5b4de956 | 55 | |
jose_23991 | 43:97fd5b4de956 | 56 | #if defined(FOTA) |
jose_23991 | 43:97fd5b4de956 | 57 | mbed::BlockDevice* ext_bd = NULL; |
jose_23991 | 43:97fd5b4de956 | 58 | |
jose_23991 | 43:97fd5b4de956 | 59 | ext_bd = new SPIFBlockDevice(); |
jose_23991 | 43:97fd5b4de956 | 60 | int ret = ext_bd->init(); |
jose_23991 | 43:97fd5b4de956 | 61 | if (ext_bd->init() < 0) |
jose_23991 | 43:97fd5b4de956 | 62 | { |
jose_23991 | 43:97fd5b4de956 | 63 | delete ext_bd; |
jose_23991 | 43:97fd5b4de956 | 64 | ext_bd = new DataFlashBlockDevice(); |
jose_23991 | 43:97fd5b4de956 | 65 | ret = ext_bd->init(); |
jose_23991 | 43:97fd5b4de956 | 66 | // Check for zero size because DataFlashBlockDevice doesn't |
jose_23991 | 43:97fd5b4de956 | 67 | // return an error if the chip is not present |
jose_23991 | 43:97fd5b4de956 | 68 | if ((ret < 0) || (ext_bd->size() == 0)) |
jose_23991 | 43:97fd5b4de956 | 69 | { |
jose_23991 | 43:97fd5b4de956 | 70 | delete ext_bd; |
jose_23991 | 43:97fd5b4de956 | 71 | ext_bd = NULL; |
jose_23991 | 43:97fd5b4de956 | 72 | } |
jose_23991 | 43:97fd5b4de956 | 73 | } |
jose_23991 | 43:97fd5b4de956 | 74 | |
jose_23991 | 43:97fd5b4de956 | 75 | dot = mDot::getInstance(plan, ext_bd); |
jose_23991 | 43:97fd5b4de956 | 76 | |
jose_23991 | 43:97fd5b4de956 | 77 | if (ext_bd != NULL) |
jose_23991 | 43:97fd5b4de956 | 78 | { |
jose_23991 | 43:97fd5b4de956 | 79 | logInfo("External flash device detected, type: %s, size: 0x%08x", |
jose_23991 | 43:97fd5b4de956 | 80 | ext_bd->get_type(), (uint32_t)ext_bd->size()); |
jose_23991 | 43:97fd5b4de956 | 81 | } |
jose_23991 | 43:97fd5b4de956 | 82 | |
jose_23991 | 43:97fd5b4de956 | 83 | Fota::getInstance(dot); |
jose_23991 | 43:97fd5b4de956 | 84 | #else |
jose_23991 | 43:97fd5b4de956 | 85 | dot = mDot::getInstance(plan); |
jose_23991 | 43:97fd5b4de956 | 86 | #endif |
jose_23991 | 43:97fd5b4de956 | 87 | assert(dot); |
jose_23991 | 43:97fd5b4de956 | 88 | |
jose_23991 | 43:97fd5b4de956 | 89 | // attach the custom events handler |
jose_23991 | 43:97fd5b4de956 | 90 | dot->setEvents(&events); |
jose_23991 | 43:97fd5b4de956 | 91 | |
jose_23991 | 43:97fd5b4de956 | 92 | if (!dot->getStandbyFlag()) { |
jose_23991 | 43:97fd5b4de956 | 93 | logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
jose_23991 | 43:97fd5b4de956 | 94 | |
jose_23991 | 43:97fd5b4de956 | 95 | // start from a well-known state |
jose_23991 | 43:97fd5b4de956 | 96 | logInfo("defaulting Dot configuration"); |
jose_23991 | 43:97fd5b4de956 | 97 | dot->resetConfig(); |
jose_23991 | 43:97fd5b4de956 | 98 | dot->resetNetworkSession(); |
jose_23991 | 43:97fd5b4de956 | 99 | |
jose_23991 | 43:97fd5b4de956 | 100 | // make sure library logging is turned on |
jose_23991 | 43:97fd5b4de956 | 101 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
jose_23991 | 43:97fd5b4de956 | 102 | |
jose_23991 | 43:97fd5b4de956 | 103 | // update configuration if necessary |
jose_23991 | 43:97fd5b4de956 | 104 | if (dot->getJoinMode() != mDot::MANUAL) { |
jose_23991 | 43:97fd5b4de956 | 105 | logInfo("changing network join mode to MANUAL"); |
jose_23991 | 43:97fd5b4de956 | 106 | if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) { |
jose_23991 | 43:97fd5b4de956 | 107 | logError("failed to set network join mode to MANUAL"); |
jose_23991 | 43:97fd5b4de956 | 108 | } |
jose_23991 | 43:97fd5b4de956 | 109 | } |
jose_23991 | 43:97fd5b4de956 | 110 | // in MANUAL join mode there is no join request/response transaction |
jose_23991 | 43:97fd5b4de956 | 111 | // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate |
jose_23991 | 43:97fd5b4de956 | 112 | // network address - 4 bytes (00000001 - FFFFFFFE) |
jose_23991 | 43:97fd5b4de956 | 113 | // network session key - 16 bytes |
jose_23991 | 43:97fd5b4de956 | 114 | // data session key - 16 bytes |
jose_23991 | 43:97fd5b4de956 | 115 | // to provision your Dot with a Conduit gateway, follow the following steps |
jose_23991 | 43:97fd5b4de956 | 116 | // * ssh into the Conduit |
jose_23991 | 43:97fd5b4de956 | 117 | // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/ |
jose_23991 | 43:97fd5b4de956 | 118 | // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304 |
jose_23991 | 43:97fd5b4de956 | 119 | // * if you change the network address, network session key, or data session key, make sure you update them on the gateway |
jose_23991 | 43:97fd5b4de956 | 120 | // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation |
jose_23991 | 43:97fd5b4de956 | 121 | update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, network_type, ack); |
jose_23991 | 43:97fd5b4de956 | 122 | |
jose_23991 | 43:97fd5b4de956 | 123 | // enable or disable Adaptive Data Rate |
jose_23991 | 43:97fd5b4de956 | 124 | dot->setAdr(adr); |
jose_23991 | 43:97fd5b4de956 | 125 | |
jose_23991 | 43:97fd5b4de956 | 126 | // Configure the join delay |
jose_23991 | 43:97fd5b4de956 | 127 | dot->setJoinDelay(join_delay); |
jose_23991 | 43:97fd5b4de956 | 128 | |
jose_23991 | 43:97fd5b4de956 | 129 | // save changes to configuration |
jose_23991 | 43:97fd5b4de956 | 130 | logInfo("saving configuration"); |
jose_23991 | 43:97fd5b4de956 | 131 | if (!dot->saveConfig()) { |
jose_23991 | 43:97fd5b4de956 | 132 | logError("failed to save configuration"); |
jose_23991 | 43:97fd5b4de956 | 133 | } |
jose_23991 | 43:97fd5b4de956 | 134 | |
jose_23991 | 43:97fd5b4de956 | 135 | // display configuration |
jose_23991 | 43:97fd5b4de956 | 136 | display_config(); |
jose_23991 | 43:97fd5b4de956 | 137 | } else { |
jose_23991 | 43:97fd5b4de956 | 138 | // restore the saved session if the dot woke from deepsleep mode |
jose_23991 | 43:97fd5b4de956 | 139 | // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep |
jose_23991 | 43:97fd5b4de956 | 140 | logInfo("restoring network session from NVM"); |
jose_23991 | 43:97fd5b4de956 | 141 | dot->restoreNetworkSession(); |
jose_23991 | 43:97fd5b4de956 | 142 | } |
jose_23991 | 43:97fd5b4de956 | 143 | |
jose_23991 | 43:97fd5b4de956 | 144 | while(1) |
jose_23991 | 43:97fd5b4de956 | 145 | { |
jose_23991 | 43:97fd5b4de956 | 146 | |
jose_23991 | 43:97fd5b4de956 | 147 | } |
jose_23991 | 43:97fd5b4de956 | 148 | |
jose_23991 | 43:97fd5b4de956 | 149 | return 0; |
jose_23991 | 43:97fd5b4de956 | 150 | } |
jose_23991 | 43:97fd5b4de956 | 151 | |
jose_23991 | 43:97fd5b4de956 | 152 | /*****************************************************************************/ |