arm studio build

Dependencies:   libxDot-mbed5

Committer:
alan1974
Date:
Wed Jun 10 14:50:17 2020 +0000
Revision:
27:ee9c063a535b
Parent:
26:f51ff4ad7a93
Child:
29:b3e9a0477d96
started adding gps time request

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alan1974 27:ee9c063a535b 1 #include "mbed.h"
alan1974 1:0d25d9ddbe9f 2 #include "global.h"
alan1974 0:a91cd1b08360 3 #include "commI2C.h"
alan1974 0:a91cd1b08360 4 #include "dot_util.h"
alan1974 0:a91cd1b08360 5 #include "wbit_util.h"
alan1974 14:fc836a5a5d2f 6 #include "multicast.h"
alan1974 0:a91cd1b08360 7 #include "mDot.h"
alan1974 0:a91cd1b08360 8 #include "RadioEvent.h"
alan1974 15:ed77b752eaa9 9 #include "ChannelPlans.h"
alan1974 0:a91cd1b08360 10 #include "Lora.h"
alan1974 0:a91cd1b08360 11
alan1974 17:74d60177c6b6 12 bool verbose = true;
alan1974 17:74d60177c6b6 13 bool bRxDone = false; //true if callback function RxDone() triggered
alan1974 17:74d60177c6b6 14 bool bDownLinkCntrFail = false; //true if callback function RxDone() triggered and bad downlink frame count
alan1974 27:ee9c063a535b 15 bool bListen4Multicast = false;
alan1974 17:74d60177c6b6 16 uint16_t maxMulticastSessionTime = 0;
alan1974 1:0d25d9ddbe9f 17 //nvm storage params
alan1974 17:74d60177c6b6 18 nvm nvmData;
alan1974 17:74d60177c6b6 19 nvm *pNvmData = &nvmData;
alan1974 1:0d25d9ddbe9f 20
alan1974 15:ed77b752eaa9 21
alan1974 15:ed77b752eaa9 22
alan1974 0:a91cd1b08360 23 //=======================================================================================================
alan1974 10:061fab1195e7 24 // enable GPIO for scope trigger
alan1974 0:a91cd1b08360 25 //=======================================================================================================
alan1974 0:a91cd1b08360 26 //#define GPIO_ENABLE
alan1974 0:a91cd1b08360 27 //=======================================================================================================
alan1974 0:a91cd1b08360 28 // configure for either private (test only) or public network (standard)
alan1974 0:a91cd1b08360 29 //=======================================================================================================
alan1974 0:a91cd1b08360 30 //#define MT_PRIVATE_NETWORK //ENABLE THIS FOR PRIVATE NETWORK
alan1974 0:a91cd1b08360 31
alan1974 0:a91cd1b08360 32 //=======================================================================================================
alan1974 0:a91cd1b08360 33 //code api level and version
alan1974 0:a91cd1b08360 34 //api_level: proc code will not run if api level (last two bytess) greater than what it expects
alan1974 0:a91cd1b08360 35 //=======================================================================================================
alan1974 21:60e5b6becdb0 36 uint8_t api_level[4] = { 0x00, 0x00, 0x00, 0x04 }; //api-level, determines if xdot code works with proc code
alan1974 24:e5ff476cd04e 37 uint8_t ver_level[4] = { 0x00, 0x00, 0x00, 0x0B }; //updated for every code check-in
alan1974 0:a91cd1b08360 38
alan1974 0:a91cd1b08360 39 //=======================================================================================================
alan1974 0:a91cd1b08360 40 //configuring mbed pinsa; https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/io/DigitalInOut/
alan1974 0:a91cd1b08360 41 // * these options must match the settings on your gateway //
alan1974 0:a91cd1b08360 42 // * edit their values to match your configuration //
alan1974 0:a91cd1b08360 43 // * frequency sub band is only relevant for the 915 bands //
alan1974 0:a91cd1b08360 44 // * either the network name and passphrase can be used or //
alan1974 0:a91cd1b08360 45 // the network ID (8 bytes) and KEY (16 bytes) //
alan1974 0:a91cd1b08360 46 //=======================================================================================================
alan1974 1:0d25d9ddbe9f 47 static std::string network_name = "asdfqwer"; //not used
alan1974 1:0d25d9ddbe9f 48 static std::string network_passphrase = "zxcvasdf"; //not used
alan1974 12:7944e4dbe853 49
alan1974 0:a91cd1b08360 50
alan1974 0:a91cd1b08360 51 #ifdef MT_PRIVATE_NETWORK
alan1974 27:ee9c063a535b 52 static uint8_t frequency_sub_band = 4;
alan1974 0:a91cd1b08360 53 static bool public_network = false;
alan1974 0:a91cd1b08360 54 static uint8_t ack = 1; //0;
alan1974 0:a91cd1b08360 55 #else
alan1974 27:ee9c063a535b 56 uint8_t frequency_sub_band = 1;
alan1974 1:0d25d9ddbe9f 57 bool public_network = true; //false
alan1974 1:0d25d9ddbe9f 58 uint8_t ack = 1; //0;
alan1974 1:0d25d9ddbe9f 59 uint8_t link_check_treshold = 100; //5;
alan1974 0:a91cd1b08360 60 #endif
alan1974 0:a91cd1b08360 61
alan1974 1:0d25d9ddbe9f 62 bool adr = false; //set adaptive data rate
alan1974 10:061fab1195e7 63 //=======================================================================================================
alan1974 10:061fab1195e7 64 // -need to add a delay between exiting i2c read->proc and before putting xdot to sleep to allow for final
alan1974 10:061fab1195e7 65 // xfr of bytes in i2c buffer to be xfrd to proc. If log output enabled then it causes enough delay when
alan1974 10:061fab1195e7 66 // it prints status info to make the delay ok. For now add this delay when both log enabled and disabled.
alan1974 27:ee9c063a535b 67 int16_t wait_before_sleep_usec = 10000;
alan1974 0:a91cd1b08360 68 //=======================================================================================================
alan1974 0:a91cd1b08360 69 // deepsleep consumes slightly less current than sleep
alan1974 15:ed77b752eaa9 70 // sleep mode: IO state is maintained, RAM is retained, and application will resume after waking up
alan1974 15:ed77b752eaa9 71 // deepsleep mode: IOs float, RAM is lost, and application will start from beginning after waking up
alan1974 0:a91cd1b08360 72 // if deep_sleep == true, device will enter deepsleep mode
alan1974 8:a5316708e51d 73 // asb:
alan1974 8:a5316708e51d 74 // no longer use deepsleep since we want to retain state
alan1974 8:a5316708e51d 75 // sleep uses only 1uA more current than deepsleep
alan1974 0:a91cd1b08360 76 //=======================================================================================================
alan1974 0:a91cd1b08360 77 static bool deep_sleep = false; //false;
alan1974 0:a91cd1b08360 78
alan1974 0:a91cd1b08360 79 uint32_t packets_sent = 0;
alan1974 0:a91cd1b08360 80 uint32_t acks_rcvd = 0;
alan1974 0:a91cd1b08360 81 int8_t rssi =0; //rssi of last rcvd rx1/rx2
alan1974 0:a91cd1b08360 82 std::string eui = " ";
alan1974 0:a91cd1b08360 83 static bool led_enabled = false; //true; //false;
alan1974 0:a91cd1b08360 84 mDot* dot = NULL;
alan1974 0:a91cd1b08360 85 lora::ChannelPlan* plan = NULL;
alan1974 0:a91cd1b08360 86 uint8_t buf_xmt[BUFFER_SIZE_I2C]; //outgoing data
alan1974 0:a91cd1b08360 87 uint8_t buf_rcv[BUFFER_SIZE_I2C]; //incoming data
alan1974 27:ee9c063a535b 88 std::vector<uint8_t> upstream_packet;
alan1974 0:a91cd1b08360 89
alan1974 0:a91cd1b08360 90 #ifdef GPIO_ENABLE
alan1974 0:a91cd1b08360 91 DigitalInOut gpio1(PA_5); //scope debug PA-5 is connected to SW1 pads on Loren v04 and can be used for scope debug
alan1974 0:a91cd1b08360 92 #endif
alan1974 0:a91cd1b08360 93
alan1974 0:a91cd1b08360 94 Serial pc(USBTX, USBRX); //serial port output
alan1974 0:a91cd1b08360 95
alan1974 0:a91cd1b08360 96 //==================================================================================
alan1974 0:a91cd1b08360 97 //chksum
alan1974 0:a91cd1b08360 98 //compute checksum over i2c buffer except for last byte (chksum byte)
alan1974 0:a91cd1b08360 99 //==================================================================================
alan1974 27:ee9c063a535b 100 uint8_t chksum_proc(uint8_t *bfr_xdot)
alan1974 27:ee9c063a535b 101 {
alan1974 0:a91cd1b08360 102 uint8_t i;
alan1974 0:a91cd1b08360 103 uint8_t chksum = 0;
alan1974 27:ee9c063a535b 104 for (i=0; i < BUFFER_SIZE_I2C-1; i++)chksum += bfr_xdot[i]; //good code
alan1974 0:a91cd1b08360 105 return chksum;
alan1974 27:ee9c063a535b 106 }
alan1974 0:a91cd1b08360 107 //=================================================================================================
alan1974 0:a91cd1b08360 108 //cfg_network:
alan1974 0:a91cd1b08360 109 // configure the network public/private and sub-band
alan1974 0:a91cd1b08360 110 // bForceCfg : true => force a network cfg
alan1974 0:a91cd1b08360 111 // false => only change if bPublic or sub_band have changed
alan1974 0:a91cd1b08360 112 // bPublic : true if network is public, else private
alan1974 0:a91cd1b08360 113 // sub_band: sub band number (1..8)
alan1974 0:a91cd1b08360 114
alan1974 0:a91cd1b08360 115 // NOTE: THIS ONLY WORKS WHEN THE XDOT BOOTS UP. IT DOES NOT WORK WHEN TRYING TO CHANGE AFTER.
alan1974 0:a91cd1b08360 116 // NOT SURE WHY...WHEN THE XDOT REJOINS IT SHOULD USE NEW SESSION SETTINGS... asb
alan1974 0:a91cd1b08360 117 //
alan1974 27:ee9c063a535b 118 //asb:dec 2017: try this later and see if it works:mdot.h:
alan1974 0:a91cd1b08360 119 // int32_t setPublicNetwork(const bool& on);
alan1974 0:a91cd1b08360 120 // bool getPublicNetwork();
alan1974 0:a91cd1b08360 121 // int32_t setFrequencySubBand(const uint8_t& band);
alan1974 0:a91cd1b08360 122 //=================================================================================================
alan1974 27:ee9c063a535b 123 bool cfg_network(bool bForceCfg,bool bPublic,uint8_t sub_band)
alan1974 27:ee9c063a535b 124 {
alan1974 0:a91cd1b08360 125 // if bForceCfg false and network parameters haven't changed then just exit
alan1974 27:ee9c063a535b 126 if (!bForceCfg) {
alan1974 5:abfe25f0de33 127 if(verbose)pc.printf("no configuration change needed subband the same");
alan1974 27:ee9c063a535b 128 if ((public_network ==bPublic) && (frequency_sub_band ==sub_band))return true;
alan1974 27:ee9c063a535b 129 }
alan1974 5:abfe25f0de33 130 if(verbose)pc.printf("changing to subband: %d\r\n",sub_band);
alan1974 27:ee9c063a535b 131 // update network settings
alan1974 0:a91cd1b08360 132 public_network = bPublic;
alan1974 0:a91cd1b08360 133 frequency_sub_band =sub_band;
alan1974 0:a91cd1b08360 134 // start from a well-known state
alan1974 27:ee9c063a535b 135 logInfo("defaulting Dot configuration");
alan1974 0:a91cd1b08360 136 if (dot->getJoinMode() != mDot::OTA) { // update configuration if necessary
alan1974 0:a91cd1b08360 137 logInfo("changing network join mode to OTA");
alan1974 0:a91cd1b08360 138 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
alan1974 0:a91cd1b08360 139 logError("failed to set network join mode to OTA");
alan1974 0:a91cd1b08360 140 return false;
alan1974 0:a91cd1b08360 141 }
alan1974 27:ee9c063a535b 142 }
alan1974 0:a91cd1b08360 143 // 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
alan1974 0:a91cd1b08360 144 // only one method or the other should be used!
alan1974 27:ee9c063a535b 145 if (public_network) {
alan1974 12:7944e4dbe853 146 update_ota_config_id_key(nvmData.key_AppEUI,nvmData.key_AppKey, frequency_sub_band, public_network, ack);
alan1974 0:a91cd1b08360 147 logInfo("-------------- network configured for public access -----------------------------");
alan1974 27:ee9c063a535b 148 } else {
alan1974 0:a91cd1b08360 149 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
alan1974 0:a91cd1b08360 150 logInfo("-------------- network configured for private access -------------------------------");
alan1974 27:ee9c063a535b 151 }
alan1974 1:0d25d9ddbe9f 152 // configure network link check count
alan1974 1:0d25d9ddbe9f 153 // declares the Dot disconnected if no acks received within link_check_treshold transmits
alan1974 1:0d25d9ddbe9f 154 //update_network_link_check_config(3, 5);
alan1974 1:0d25d9ddbe9f 155 dot->setLinkCheckThreshold(link_check_treshold);
alan1974 11:77fe4f18a81b 156 return true;
alan1974 27:ee9c063a535b 157 }
alan1974 0:a91cd1b08360 158 //=================================================================================================
alan1974 0:a91cd1b08360 159 //main()
alan1974 0:a91cd1b08360 160 // main() runs in its own thread in the OS
alan1974 0:a91cd1b08360 161 // (note the calls to wait below for delays)
alan1974 0:a91cd1b08360 162 //=================================================================================================
alan1974 27:ee9c063a535b 163 int main()
alan1974 27:ee9c063a535b 164 {
alan1974 1:0d25d9ddbe9f 165 uint8_t i;
alan1974 27:ee9c063a535b 166 RadioEvent events; //class to return info on rx pkts
alan1974 0:a91cd1b08360 167 mDotEvent mdotevent; //used to get ping info????
alan1974 15:ed77b752eaa9 168 //plan = new lora::ChannelPlan_US915();
alan1974 15:ed77b752eaa9 169 static lora::ChannelPlan_US915 plan;
alan1974 27:ee9c063a535b 170 #ifdef GPIO_ENABLE
alan1974 27:ee9c063a535b 171 gpio1.output();
alan1974 27:ee9c063a535b 172 gpio1 =0;
alan1974 27:ee9c063a535b 173 #endif
alan1974 27:ee9c063a535b 174 pc.baud(115200);
alan1974 27:ee9c063a535b 175
alan1974 0:a91cd1b08360 176 pc.printf("\r\n**********************************************************\r\n");
alan1974 0:a91cd1b08360 177 pc.printf("\r\n XDOT BOOT\r\n");
alan1974 0:a91cd1b08360 178 pc.printf("COMM api_level = <HEX> %x.%x.%x.%x\r\n",api_level[0],api_level[1],api_level[2],api_level[3]);
alan1974 0:a91cd1b08360 179 pc.printf("COMM version = %x.%x.%x.%x\r\n",ver_level[0],ver_level[1],ver_level[2],ver_level[3]);
alan1974 27:ee9c063a535b 180 pc.printf("**********************************************************\r\n");
alan1974 15:ed77b752eaa9 181 //assert(plan);
alan1974 15:ed77b752eaa9 182 //dot = mDot::getInstance(plan);
alan1974 27:ee9c063a535b 183 dot = mDot::getInstance(&plan);
alan1974 27:ee9c063a535b 184 //assert(dot);
alan1974 15:ed77b752eaa9 185
alan1974 27:ee9c063a535b 186 dot->setEvents(&events);
alan1974 27:ee9c063a535b 187
alan1974 27:ee9c063a535b 188
alan1974 27:ee9c063a535b 189
alan1974 1:0d25d9ddbe9f 190 nvmRead(pNvmData);
alan1974 8:a5316708e51d 191
alan1974 27:ee9c063a535b 192 if (pNvmData->bLogOutputOn == 0) {
alan1974 5:abfe25f0de33 193 pc.printf("\r\nDISABLING TERMINAL OUTPUT\r\n");
alan1974 10:061fab1195e7 194 dot->setLogLevel(mts::MTSLog::NONE_LEVEL); //doesn't work
alan1974 8:a5316708e51d 195 verbose = false;
alan1974 27:ee9c063a535b 196 } else {
alan1974 27:ee9c063a535b 197 printNmvData(pNvmData);
alan1974 27:ee9c063a535b 198 //dot->setLogLevel((verbose) ? mts::MTSLog::TRACE_LEVEL : mts::MTSLog::TRACE_LEVEL); // TRACE_LEVEL , INFO_LEVEL
alan1974 1:0d25d9ddbe9f 199 }
alan1974 27:ee9c063a535b 200
alan1974 0:a91cd1b08360 201 // getStandbyFlag() should return the state of the standby flag directly from the processor
alan1974 0:a91cd1b08360 202 // Standby flag: This bit is set by hardware and cleared only by a POR/PDR (power on reset/power down reset) or by setting the CSBF bit in the PWR power control register (PWR_CR)
alan1974 0:a91cd1b08360 203 // 0: Device has not been in Standby mode
alan1974 0:a91cd1b08360 204 // 1: Device has been in Standby mode
alan1974 0:a91cd1b08360 205 // The xDot should enter standby mode when deep sleep in invoked. So you should see the standby flag set if it came out of deep sleep.
alan1974 1:0d25d9ddbe9f 206 if (!dot->getStandbyFlag()) { //if 0 => power-up/reset which should always be the case at this point
alan1974 27:ee9c063a535b 207 // logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); // MBED_LIBRARY_VERSION no longer declared?
alan1974 27:ee9c063a535b 208 frequency_sub_band = dot->getFrequencySubBand();
alan1974 27:ee9c063a535b 209 printRadioCfg();
alan1974 5:abfe25f0de33 210 cfg_network(true,public_network,frequency_sub_band); //force network cfg,
alan1974 0:a91cd1b08360 211 } else {
alan1974 0:a91cd1b08360 212 // restore the saved session (join OTAA info) if the dot woke from deepsleep mode
alan1974 0:a91cd1b08360 213 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
alan1974 0:a91cd1b08360 214 logInfo("restoring network session from NVM");
alan1974 0:a91cd1b08360 215 dot->restoreNetworkSession();
alan1974 0:a91cd1b08360 216 }
alan1974 0:a91cd1b08360 217 //--------------------------------------------------------------------------------------------------------------------------------------------
alan1974 1:0d25d9ddbe9f 218 // configure network link check count
alan1974 1:0d25d9ddbe9f 219 // declares the Dot disconnected if no acks received within link_check_treshold transmits
alan1974 1:0d25d9ddbe9f 220 //update_network_link_check_config(3, 5);
alan1974 1:0d25d9ddbe9f 221 dot->setLinkCheckThreshold(link_check_treshold);
alan1974 27:ee9c063a535b 222 //----------------------------------------------------------------------------------------------------------------------------------------------
alan1974 27:ee9c063a535b 223 eui = mts::Text::bin2hexString(dot->getDeviceId()).c_str();
alan1974 27:ee9c063a535b 224 if(verbose)pc.printf("\r\nEUI: %s\r\n",eui);
alan1974 27:ee9c063a535b 225 bool joined = false;
alan1974 27:ee9c063a535b 226
alan1974 27:ee9c063a535b 227 i2c_proc_init(); //init i2c comm
alan1974 27:ee9c063a535b 228
alan1974 27:ee9c063a535b 229 sleep_wake_interrupt_only(deep_sleep); //goto sleep;wake on next rising edge of lora_wake
alan1974 27:ee9c063a535b 230 if(verbose)pc.printf("\n\r lora wake detected -> monitoring i2c bus\n\r ");
alan1974 27:ee9c063a535b 231
alan1974 27:ee9c063a535b 232 //scope test
alan1974 27:ee9c063a535b 233 #ifdef GPIO_ENABLE
alan1974 27:ee9c063a535b 234 gpio1 =1;
alan1974 10:061fab1195e7 235 #endif
alan1974 0:a91cd1b08360 236 //==============================================================================
alan1974 0:a91cd1b08360 237 // -loop here forever
alan1974 0:a91cd1b08360 238 // -sleep until LORA_WAKE goes hi => proc ready to send i2c cmd
alan1974 0:a91cd1b08360 239 // -start polling incoming i2c bus for proc cmd
alan1974 0:a91cd1b08360 240 // -execute cmd
alan1974 0:a91cd1b08360 241 // -take control of LORA_WAKE and toggle it hi to signal proc that xdot
alan1974 0:a91cd1b08360 242 // ready to send i2c ack message
alan1974 0:a91cd1b08360 243 // -go back to sleep
alan1974 0:a91cd1b08360 244 //==============================================================================
alan1974 27:ee9c063a535b 245 //==============================================================================
alan1974 27:ee9c063a535b 246 // -loop here forever
alan1974 27:ee9c063a535b 247 // -sleep until LORA_WAKE goes hi => proc ready to send i2c cmd
alan1974 27:ee9c063a535b 248 // -start polling incoming i2c bus for proc cmd
alan1974 27:ee9c063a535b 249 // -execute cmd
alan1974 27:ee9c063a535b 250 // -take control of LORA_WAKE and toggle it hi to signal proc that xdot
alan1974 27:ee9c063a535b 251 // ready to send i2c ack message
alan1974 27:ee9c063a535b 252 // -go back to sleep
alan1974 27:ee9c063a535b 253 //==============================================================================
alan1974 0:a91cd1b08360 254 bool bPulseLoraWake = false;
alan1974 27:ee9c063a535b 255 while(1) {
alan1974 27:ee9c063a535b 256 if(verbose)pc.printf("\n\r***************************** ");
alan1974 27:ee9c063a535b 257
alan1974 27:ee9c063a535b 258 switch (i2c_proc_comm(buf_xmt,buf_rcv,BUFFER_SIZE_I2C)) {
alan1974 27:ee9c063a535b 259 case I2C_WRITE: //xdot ack ->proc
alan1974 27:ee9c063a535b 260 if(verbose)pc.printf("\n\r xdot ack -> proc done,going to sleep\n\r ");
alan1974 27:ee9c063a535b 261 bPulseLoraWake = false;
alan1974 27:ee9c063a535b 262 #ifdef GPIO_ENABLE
alan1974 27:ee9c063a535b 263 gpio1 =0;
alan1974 27:ee9c063a535b 264 #endif
alan1974 15:ed77b752eaa9 265 wait_us(wait_before_sleep_usec); //wait for i2c to complete data transfer before sleeping
alan1974 27:ee9c063a535b 266 if (bListen4Multicast) { //in ClassC and receiving multicst pkt, stay here until timeout or bListen4Multicast false
alan1974 27:ee9c063a535b 267 if(verbose)pc.printf("\n\r changed to class C\r\n");
alan1974 27:ee9c063a535b 268 while(bListen4Multicast) { //stay in mcast bListen4Mutlicast =0, xdot exits -> classA when terminating mcast frag# received else psoc resets xdot
alan1974 17:74d60177c6b6 269 wait(1);
alan1974 17:74d60177c6b6 270 }
alan1974 27:ee9c063a535b 271 }
alan1974 27:ee9c063a535b 272 sleep_wake_interrupt_only(deep_sleep); //sleep until rising edge of wake signal from proc
alan1974 27:ee9c063a535b 273 #ifdef GPIO_ENABLE
alan1974 27:ee9c063a535b 274 gpio1 =1;
alan1974 27:ee9c063a535b 275 #endif
alan1974 27:ee9c063a535b 276 if(verbose)pc.printf("\n\r lora wake detected -> monitoring i2c bus\n\r ");
alan1974 27:ee9c063a535b 277 break;
alan1974 27:ee9c063a535b 278 case I2C_READ: //psoc -> xdot i2c write
alan1974 27:ee9c063a535b 279 bPulseLoraWake = true;
alan1974 27:ee9c063a535b 280 switch (buf_rcv[0]) { //switch on command #
alan1974 27:ee9c063a535b 281 case XDOT_CMD_XMIT_PKT: //transmit data pkt
alan1974 27:ee9c063a535b 282 case XDOT_CMD_GET_GPS: //get gps time stamp //GPS TIME !!!!!!!!!!!!!
alan1974 27:ee9c063a535b 283 bool bGpsTimeRequest = false;
alan1974 22:034c134f17c7 284 bool bOk2XmitFullPayload = true; //if false we only transmit the mac data
alan1974 27:ee9c063a535b 285 pkt_upstrm *pUp= (pkt_upstrm*)&buf_rcv[0]; //psoc i2c pkt
alan1974 27:ee9c063a535b 286 for (i=0; i < sizeof(buf_xmt); i++)buf_xmt[i] = 0xff; //xdot i2c bfr fill
alan1974 27:ee9c063a535b 287 pkt_ack *pAck = (pkt_ack*)&buf_xmt[0]; //cast to xdot i2c bfr
alan1974 27:ee9c063a535b 288 pAck->ack = I2C_ACK_PROC; //write ack to i2c xdot bfr
alan1974 27:ee9c063a535b 289
alan1974 27:ee9c063a535b 290 uint8_t chksum = chksum_proc(buf_rcv); //i2c pkt chksum from psoc ok?
alan1974 27:ee9c063a535b 291 if(verbose)
alan1974 27:ee9c063a535b 292 pc.printf("\r\nI2C chksum rcvd/computed: %d/%d ",pUp->chksum,chksum);
alan1974 27:ee9c063a535b 293 pAck->bXmitAttempted = 1;
alan1974 0:a91cd1b08360 294 pAck->chksum_err = 0;
alan1974 27:ee9c063a535b 295 if(pUp->chksum != chksum)
alan1974 27:ee9c063a535b 296 {
alan1974 27:ee9c063a535b 297 if(verbose)pc.printf(" chksum err, aborting xmit");
alan1974 27:ee9c063a535b 298 pAck->bXmitAttempted = 0;
alan1974 27:ee9c063a535b 299 pAck->mdot_ret = XDOT_ERR_I2C_CHKSUM;
alan1974 27:ee9c063a535b 300 pAck->chksum_err = 1;
alan1974 0:a91cd1b08360 301 break;
alan1974 5:abfe25f0de33 302 }
alan1974 22:034c134f17c7 303
alan1974 27:ee9c063a535b 304 if (buf_rcv[0] == XDOT_CMD_GET_GPS) //GPS TIME !!!!!!!!!!!!!
alan1974 27:ee9c063a535b 305 {
alan1974 27:ee9c063a535b 306 bGpsTimeRequest = true;
alan1974 27:ee9c063a535b 307 pAck->cmd = XDOT_CMD_GET_GPS;
alan1974 27:ee9c063a535b 308 }
alan1974 27:ee9c063a535b 309 else
alan1974 27:ee9c063a535b 310 {
alan1974 27:ee9c063a535b 311 pAck->cmd = XDOT_CMD_XMIT_PKT; //sendint normal data pkt
alan1974 27:ee9c063a535b 312 pAck->dataLen = pUp->dataLen; //data len of xmitted pkt
alan1974 27:ee9c063a535b 313 if(verbose)pc.printf("\r\ndatalen of pkt to xmit: %d",pAck->dataLen);
alan1974 27:ee9c063a535b 314 //if next tx pkt includes mac data then xmit mac data and abort psoc payload
alan1974 27:ee9c063a535b 315 uint8_t maxPayLoad = dot->getMaxPacketLength();
alan1974 27:ee9c063a535b 316 uint8_t maxSizeNxtTxPkt = dot->getNextTxMaxSize(); //max payload available to transmit in next pkt
alan1974 27:ee9c063a535b 317 if(verbose)pc.printf("\r\nmax datalen of next pkt: %d",maxSizeNxtTxPkt);
alan1974 27:ee9c063a535b 318 if (maxSizeNxtTxPkt < maxPayLoad)
alan1974 27:ee9c063a535b 319 {
alan1974 27:ee9c063a535b 320 if(verbose)pc.printf("\r\n can't fit payload in next pkt,xmit only mac data\r\n");
alan1974 27:ee9c063a535b 321 bOk2XmitFullPayload = false;
alan1974 27:ee9c063a535b 322 }
alan1974 27:ee9c063a535b 323 else
alan1974 27:ee9c063a535b 324 {
alan1974 27:ee9c063a535b 325 if(verbose)pc.printf("\r\nok to xmit full payload");
alan1974 27:ee9c063a535b 326 }
alan1974 27:ee9c063a535b 327 if(verbose)pc.printf("\r\nsetting application port %d ",pUp->appPort);
alan1974 27:ee9c063a535b 328 dot->setAppPort(pUp->appPort);
alan1974 27:ee9c063a535b 329 uint8_t linkThresholdCnt = pUp->linkThreshCnt;
alan1974 27:ee9c063a535b 330 if(verbose)pc.printf("\r\nlinkThreshCnt %d\r\n",linkThresholdCnt);
alan1974 27:ee9c063a535b 331 dot->setLinkCheckThreshold(linkThresholdCnt);
alan1974 27:ee9c063a535b 332 if (pUp->dataLen == 0)
alan1974 27:ee9c063a535b 333 {
alan1974 27:ee9c063a535b 334 pAck->bXmitAttempted = 0;
alan1974 27:ee9c063a535b 335 break;
alan1974 27:ee9c063a535b 336 }
alan1974 27:ee9c063a535b 337 upstream_packet.clear(); //clear the xmit payload array
alan1974 27:ee9c063a535b 338 if(bOk2XmitFullPayload) //if no mac data to xmit, xfr data from incoming bfr to xmit bfr
alan1974 27:ee9c063a535b 339 for (i=0; i< pUp->dataLen; i++) upstream_packet.push_back(pUp->txData[i]);
alan1974 27:ee9c063a535b 340 if(verbose)
alan1974 27:ee9c063a535b 341 {
alan1974 27:ee9c063a535b 342 pc.printf("\r\n[TEST],Upstream Packet Received");
alan1974 27:ee9c063a535b 343 for(std::vector<uint8_t>::iterator it = upstream_packet.begin(); it != upstream_packet.end(); ++it)
alan1974 27:ee9c063a535b 344 pc.printf(",0x%x", *it);
alan1974 27:ee9c063a535b 345 pc.printf("\r\n"); // see i told you.
alan1974 27:ee9c063a535b 346 }
alan1974 27:ee9c063a535b 347 }
alan1974 0:a91cd1b08360 348 joined = dot->getNetworkJoinStatus(); //are we joined to Lorawan?
alan1974 27:ee9c063a535b 349 if(verbose)pc.printf("\r\njoin status on entry: %d\r\n",joined);
alan1974 27:ee9c063a535b 350 pAck->joinAttempts = 0; //no attempts made yet to join
alan1974 1:0d25d9ddbe9f 351 pAck->bAck = 0; //won't know if we receive a lorawan ack until after xmit
alan1974 1:0d25d9ddbe9f 352 pAck->bAckdata = 0; //won't know if we receive a lorawan ack downstream data until after xmit
alan1974 20:62a1a264fa1e 353 pAck->rssi = 0; //if not rx1/rx2 then no RSSI value -- 8 bit rssi, deprecated, keep for compatbility
alan1974 20:62a1a264fa1e 354 pAck->rssi2 = 0; //16 bit rssi
alan1974 27:ee9c063a535b 355 pAck->snr = 0; //if not rx1/rx2 then no SNR value
alan1974 26:f51ff4ad7a93 356 pAck->snr2 = 0; //16 bit snr
alan1974 26:f51ff4ad7a93 357 if(!joined) //if not previously joined, then need to join now
alan1974 27:ee9c063a535b 358 {
alan1974 27:ee9c063a535b 359 pAck->bJoined = 0;
alan1974 27:ee9c063a535b 360 if(verbose)pc.printf("\r\n----------- NETWORK NOT JOINED YET, WILL TRY TO JOIN %d TIMES\r\n",pUp->joinAttemps);
alan1974 14:fc836a5a5d2f 361 joined = join_network_wbit(pUp->joinAttemps); //try joinAttemps times to join network
alan1974 14:fc836a5a5d2f 362 pAck->joinAttempts = join_network_attempts_wbit(); //# of join attemps made
alan1974 27:ee9c063a535b 363 if(verbose)pc.printf("\r\njoin status after join attempt: %d\r\n",joined);
alan1974 27:ee9c063a535b 364 if (joined) //are we now joined?
alan1974 27:ee9c063a535b 365 {
alan1974 24:e5ff476cd04e 366 dot->saveNetworkSession(); //save OTAA keys and session info on new join success
alan1974 24:e5ff476cd04e 367 save_OTAA_session_keys(); //used for multicast
alan1974 27:ee9c063a535b 368 } else
alan1974 27:ee9c063a535b 369 if(verbose)pc.printf("\r\n----------- FAILED TO JOIN...GIVING UP\r\n"); // join network if not joined }
alan1974 27:ee9c063a535b 370 }
alan1974 27:ee9c063a535b 371 if (joined) //we are joined to the network
alan1974 27:ee9c063a535b 372 {
alan1974 27:ee9c063a535b 373 pAck->bJoined = 1;
alan1974 27:ee9c063a535b 374 packets_sent++;
alan1974 11:77fe4f18a81b 375 bRxDone = false; //true if callback function RxDone() triggered
alan1974 27:ee9c063a535b 376 bDownLinkCntrFail = false; //true if callback function RxDone() triggered and bad downlink frame count
alan1974 27:ee9c063a535b 377 plan.SetRx2DatarateIndex(8); //LORIOT FIX FOR RX2 & OTA
alan1974 22:034c134f17c7 378
alan1974 27:ee9c063a535b 379 if (bGpsTimeRequest) //GPS TIME !!!!!!!!!!!!!
alan1974 27:ee9c063a535b 380 {
alan1974 27:ee9c063a535b 381 uint64_t gpsTime = dot->getGPSTime();
alan1974 27:ee9c063a535b 382 if(verbose)
alan1974 27:ee9c063a535b 383 {
alan1974 27:ee9c063a535b 384 pc.printf("\r\n gps data: ");
alan1974 27:ee9c063a535b 385 for (i=7; i<= 0; i--)
alan1974 27:ee9c063a535b 386 {
alan1974 27:ee9c063a535b 387 pAck->rxData[i]= gpsTime >> i;
alan1974 27:ee9c063a535b 388 pc.printf(" %x",pAck->rxData[i]);
alan1974 27:ee9c063a535b 389 }
alan1974 27:ee9c063a535b 390 }
alan1974 27:ee9c063a535b 391 break;
alan1974 27:ee9c063a535b 392 }
alan1974 27:ee9c063a535b 393
alan1974 27:ee9c063a535b 394 if(bOk2XmitFullPayload) //send normal payload, no mac data to send
alan1974 27:ee9c063a535b 395 {
alan1974 27:ee9c063a535b 396 pAck->mdot_ret = dot->send(upstream_packet); //xmit the pkt
alan1974 27:ee9c063a535b 397 if (verbose)printf("\n\rdata->send() return code: %d\r\n",pAck->mdot_ret);
alan1974 27:ee9c063a535b 398 }
alan1974 27:ee9c063a535b 399 else //just xmit mac payload without psoc payload, return errcode for max payload exceeded
alan1974 27:ee9c063a535b 400 {
alan1974 27:ee9c063a535b 401 dot->send(upstream_packet); //xmit the pkt in blocking mode, return false if no ack
alan1974 27:ee9c063a535b 402 pAck->mdot_ret = XDOT_ERR_MAX_PAYLOAD;
alan1974 27:ee9c063a535b 403 pAck->bXmitAttempted = 0;
alan1974 27:ee9c063a535b 404 }
alan1974 27:ee9c063a535b 405 if (pAck->mdot_ret == mDot::MDOT_OK) //ack rcvd?
alan1974 27:ee9c063a535b 406 {
alan1974 0:a91cd1b08360 407 acks_rcvd++;
alan1974 27:ee9c063a535b 408 pAck->bAck = 1; //we got a Rx1 or Rx2 ack
alan1974 27:ee9c063a535b 409 mDot::rssi_stats rssiStats = dot->getRssiStats(); //rssi stat
alan1974 27:ee9c063a535b 410 pAck->rssi = (int8_t)rssiStats.last; //deprecated later!
alan1974 20:62a1a264fa1e 411 pAck->rssi2 = (int16_t)rssiStats.last;
alan1974 27:ee9c063a535b 412 mDot::snr_stats snrStats = dot->getSnrStats(); //snr stat
alan1974 27:ee9c063a535b 413 pAck->snr = (int8_t)snrStats.last; //deprecate later
alan1974 27:ee9c063a535b 414 pAck->snr2 = (int16_t)snrStats.last;
alan1974 27:ee9c063a535b 415 if (verbose)printf("\n\rdata->send()=true; ack:%d, rssi:%d snr:%d\r\n",pAck->bAck,pAck->rssi,pAck->snr);
alan1974 27:ee9c063a535b 416 if (events.is_packet_received()) //downstream data from the Rx1/Rx2 pkt?
alan1974 27:ee9c063a535b 417 {
alan1974 27:ee9c063a535b 418 if (verbose)printf("\n\revents.is_packet_received = true\r\n");
alan1974 27:ee9c063a535b 419 pAck->bAckdata = 1;
alan1974 27:ee9c063a535b 420 upstream_packet.clear();
alan1974 0:a91cd1b08360 421 upstream_packet = events.get_downstream_packet();
alan1974 27:ee9c063a535b 422 pAck->rxLen = upstream_packet.size();
alan1974 27:ee9c063a535b 423 pAck->appPort = events.port;
alan1974 27:ee9c063a535b 424 if (pAck->rxLen > I2C_MAX_ACK_DATA) {
alan1974 5:abfe25f0de33 425 if(verbose)pc.printf("\r\n got ack with pkt data too large.. rejected\r\n");
alan1974 27:ee9c063a535b 426 break;
alan1974 27:ee9c063a535b 427 }
alan1974 27:ee9c063a535b 428 if(verbose)
alan1974 27:ee9c063a535b 429 {
alan1974 27:ee9c063a535b 430 pc.printf("\r\n pkt data: ");
alan1974 27:ee9c063a535b 431 for (i=0; i< pAck->rxLen; i++)
alan1974 27:ee9c063a535b 432 {
alan1974 27:ee9c063a535b 433 pAck->rxData[i]= upstream_packet[i];
alan1974 27:ee9c063a535b 434 pc.printf(" %x",pAck->rxData[i]);
alan1974 27:ee9c063a535b 435 }
alan1974 0:a91cd1b08360 436 }
alan1974 27:ee9c063a535b 437 }
alan1974 27:ee9c063a535b 438 else
alan1974 27:ee9c063a535b 439 { //no downstream data
alan1974 27:ee9c063a535b 440 if (verbose)printf("\n\r ack rcvd without data\r\n");
alan1974 27:ee9c063a535b 441 }
alan1974 27:ee9c063a535b 442 }
alan1974 27:ee9c063a535b 443 else
alan1974 27:ee9c063a535b 444 {
alan1974 27:ee9c063a535b 445 if (verbose)printf("\n\r no ack rcvd \r\n"); //could be some other error
alan1974 27:ee9c063a535b 446 if ((pAck->mdot_ret != 0) && bRxDone) //chk if callback function triggered
alan1974 27:ee9c063a535b 447 {
alan1974 27:ee9c063a535b 448 if (bDownLinkCntrFail)pAck->mdot_ret = XDOT_ERR_FRAME_CNT; //bad downlink frame count return err code
alan1974 27:ee9c063a535b 449 }
alan1974 27:ee9c063a535b 450 }
alan1974 27:ee9c063a535b 451 }//if joined
alan1974 0:a91cd1b08360 452 break;
alan1974 0:a91cd1b08360 453 case XDOT_CMD_SET_RADIO:
alan1974 27:ee9c063a535b 454 if(verbose)pc.printf("\n\r proc cmd: CMD_SET_RADIO");
alan1974 27:ee9c063a535b 455 pkt_setradiodwn *pDwnRadio= (pkt_setradiodwn*)&buf_xmt[0];
alan1974 27:ee9c063a535b 456 pkt_setradioup *pUpRadio = (pkt_setradioup*)&buf_rcv[0];
alan1974 0:a91cd1b08360 457 pDwnRadio->ack = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 458 pDwnRadio->cmd = XDOT_CMD_SET_RADIO;
alan1974 0:a91cd1b08360 459
alan1974 27:ee9c063a535b 460 if (pUpRadio->bSetParams) {
alan1974 27:ee9c063a535b 461 if(verbose)pc.printf("\n\r setting radio params");
alan1974 27:ee9c063a535b 462 if(verbose)pc.printf("\n\r setting adr to %d ",pUpRadio->params.aDR);
alan1974 7:fba1e8fc7693 463 if (pUpRadio->params.aDR == 1) dot->setAdr(true); //test for adr problem
alan1974 27:ee9c063a535b 464 else dot->setAdr(false);
alan1974 7:fba1e8fc7693 465 dot->setAdr((uint8_t)pUpRadio->params.aDR); // enable or disable Adaptive Data Rate
alan1974 27:ee9c063a535b 466 if(verbose)pc.printf("\n\r setting subband to %d ",pUpRadio->params.sub_band);
alan1974 27:ee9c063a535b 467 cfg_network(false,true,(uint8_t)pUpRadio->params.sub_band);
alan1974 27:ee9c063a535b 468
alan1974 7:fba1e8fc7693 469 if(verbose)pc.printf("\n\r setting public/private network to %d ",pUpRadio->params.public_network);
alan1974 7:fba1e8fc7693 470 //public_network = (bool)pUpRadio->params.public_network;
alan1974 7:fba1e8fc7693 471 dot->setPublicNetwork((bool)pUpRadio->params.public_network);
alan1974 27:ee9c063a535b 472
alan1974 27:ee9c063a535b 473 if(verbose)pc.printf("\n\r setting antenna gain to %d ",pUpRadio->params.antennaGaindBi);
alan1974 2:0af50f386eb2 474 dot->setAntennaGain(pUpRadio->params.antennaGaindBi);
alan1974 27:ee9c063a535b 475 if(verbose)pc.printf("\n\r setting radio tx power to %d ",pUpRadio->params.txPower);
alan1974 10:061fab1195e7 476 dot->setTxPower(pUpRadio->params.txPower);
alan1974 27:ee9c063a535b 477 if(verbose)pc.printf("\n\r setting tx datarate to %d ",pUpRadio->params.dataRate);
alan1974 27:ee9c063a535b 478 dot->setTxDataRate(pUpRadio->params.dataRate);
alan1974 27:ee9c063a535b 479 //deprecated if(verbose)pc.printf("\n\r setting tx inverted to %d ",(bool)pUpRadio->params.txInverted);
alan1974 27:ee9c063a535b 480 //deprecated dot->setTxInverted((bool)pUpRadio->params.txInverted);
alan1974 27:ee9c063a535b 481 //deprecated if(verbose)pc.printf("\n\r setting rx inverted to %d ",(bool)pUpRadio->params.rxInverted);
alan1974 27:ee9c063a535b 482 //deprecated dot->setRxInverted((bool)pUpRadio->params.rxInverted);
alan1974 27:ee9c063a535b 483 if(verbose)pc.printf("\n\r setting rx delay to %d ",pUpRadio->params.rxDelay);
alan1974 27:ee9c063a535b 484 dot->setRxDelay(pUpRadio->params.rxDelay);
alan1974 27:ee9c063a535b 485 if(verbose)pc.printf("\n\r setting join delay to %d ",pUpRadio->params.join_delay);
alan1974 27:ee9c063a535b 486 dot->setJoinDelay(pUpRadio->params.join_delay);
alan1974 5:abfe25f0de33 487 if(verbose)pc.printf("\n\r saving configuration");
alan1974 27:ee9c063a535b 488 if (!dot->saveConfig())logError("failed to save configuration");
alan1974 27:ee9c063a535b 489
alan1974 27:ee9c063a535b 490 display_config();
alan1974 0:a91cd1b08360 491 }
alan1974 27:ee9c063a535b 492 if(verbose)pc.printf("\n\r reading radio params");
alan1974 2:0af50f386eb2 493 pDwnRadio->params.public_network = public_network;
alan1974 2:0af50f386eb2 494 pDwnRadio->params.sub_band = dot->getFrequencySubBand();
alan1974 5:abfe25f0de33 495 pDwnRadio->params.join_delay = dot->getJoinDelay();
alan1974 27:ee9c063a535b 496 //deprecated pDwnRadio->params.txInverted = dot->getTxInverted();
alan1974 27:ee9c063a535b 497 //deprecated pDwnRadio->params.rxInverted = dot->getRxInverted();
alan1974 22:034c134f17c7 498 pDwnRadio->params.txInverted = false; //not used anymorer
alan1974 22:034c134f17c7 499 pDwnRadio->params.rxInverted = false; //not used anymorer
alan1974 27:ee9c063a535b 500 pDwnRadio->params.rxDelay = dot->getRxDelay();
alan1974 27:ee9c063a535b 501 pDwnRadio->params.maxDataLen = dot->getMaxPacketLength();
alan1974 2:0af50f386eb2 502 pDwnRadio->params.maxTxPowerdBm = dot->getMaxTxPower();
alan1974 2:0af50f386eb2 503 pDwnRadio->params.minTxPowerdBm = dot->getMinTxPower();
alan1974 2:0af50f386eb2 504 pDwnRadio->params.aDR = dot->getAdr();
alan1974 2:0af50f386eb2 505 pDwnRadio->params.antennaGaindBi = dot->getAntennaGain();
alan1974 27:ee9c063a535b 506 pDwnRadio->params.txPower = dot->getTxPower(); //programmed tx power (non-adr)
alan1974 27:ee9c063a535b 507 pDwnRadio->params.dataRate = dot->getTxDataRate(); //programmed data rate (non-adr)
alan1974 27:ee9c063a535b 508 pDwnRadio->params.dataRateCurrent = dot->getSettings()->Session.TxDatarate; //presently used data rate (adr can change this);see Lora.h
alan1974 27:ee9c063a535b 509 pDwnRadio->params.txPowerCurrent = dot->getSettings()->Session.TxPower;//presently used tx power (adr can change this);see Lora.h
alan1974 27:ee9c063a535b 510 pDwnRadio->params.rx1DelayCurrent = dot->getSettings()->Session.RxDelay;//presently used rx1_delay;see Lora.h
alan1974 10:061fab1195e7 511
alan1974 27:ee9c063a535b 512
alan1974 27:ee9c063a535b 513 break;
alan1974 27:ee9c063a535b 514 case XDOT_CMD_GET_EUI: //0307: modified to include radio parameter settings
alan1974 27:ee9c063a535b 515 if(verbose)pc.printf("\n\r proc cmd: get EUI");
alan1974 27:ee9c063a535b 516 pkt_eui *peui = (pkt_eui*)&buf_xmt[0];
alan1974 0:a91cd1b08360 517 peui->ack = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 518 peui->cmd = XDOT_CMD_GET_EUI;
alan1974 27:ee9c063a535b 519 upstream_packet.clear();
alan1974 27:ee9c063a535b 520 upstream_packet = dot->getDeviceId();
alan1974 27:ee9c063a535b 521 peui->dataLen = upstream_packet.size();
alan1974 27:ee9c063a535b 522 for (i=0; i< peui->dataLen; i++) peui->euiData[i] = upstream_packet[i];
alan1974 27:ee9c063a535b 523 for (i=0; i< 4; i++) peui->apiLvlData[i] = api_level[i];
alan1974 27:ee9c063a535b 524 for (i=0; i< 4; i++) peui->verLvlData[i] = ver_level[i];
alan1974 27:ee9c063a535b 525 peui->dataLen = sizeof(pkt_eui)-3; //size of struc minus first 3 bytes
alan1974 27:ee9c063a535b 526 if(verbose)pc.printf("\n\r eui data length: %d",peui->dataLen);
alan1974 27:ee9c063a535b 527 break;
alan1974 9:cc23b2049639 528 case XDOT_CMD_NVM:
alan1974 27:ee9c063a535b 529 if(verbose)pc.printf("\n\r proc cmd: NVM OTAA");
alan1974 27:ee9c063a535b 530 pkt_setnvmup *pUpNvm = (pkt_setnvmup*)&buf_rcv[0];
alan1974 27:ee9c063a535b 531 pkt_setnvmdwn *pDwnNvm= (pkt_setnvmdwn*)&buf_xmt[0];
alan1974 9:cc23b2049639 532 pDwnNvm->ack = I2C_ACK_PROC;
alan1974 9:cc23b2049639 533 pDwnNvm->cmd = XDOT_CMD_NVM;
alan1974 9:cc23b2049639 534 pDwnNvm->dataLen = sizeof(nvm)-2;
alan1974 9:cc23b2049639 535 if (pUpNvm->nvm_option == XDOT_NVM_SET) nvmWrite(&pUpNvm->nvmData);
alan1974 9:cc23b2049639 536 if (pUpNvm->nvm_option == XDOT_NVM_RESTORE) nvmRestore(&pUpNvm->nvmData);
alan1974 27:ee9c063a535b 537
alan1974 27:ee9c063a535b 538 pDwnNvm->bChkSumOK = 0;
alan1974 9:cc23b2049639 539 if (nvmRead(&pDwnNvm->nvmData))
alan1974 27:ee9c063a535b 540 pDwnNvm->bChkSumOK = 1;
alan1974 27:ee9c063a535b 541 printNmvData(&pDwnNvm->nvmData);
alan1974 27:ee9c063a535b 542 break;
alan1974 24:e5ff476cd04e 543 case XDOT_CMD_RESTORE_SESSION: //restore OTAA session
alan1974 24:e5ff476cd04e 544 dot->restoreNetworkSession();
alan1974 24:e5ff476cd04e 545 dot->setDownLinkCounter(0); //reset frame counters
alan1974 24:e5ff476cd04e 546 dot->setUpLinkCounter(0);
alan1974 27:ee9c063a535b 547 pkt_ntwrk *pDwnRstSession = (pkt_ntwrk*)&buf_xmt[0];
alan1974 24:e5ff476cd04e 548 pDwnRstSession->ack = I2C_ACK_PROC;
alan1974 24:e5ff476cd04e 549 pDwnRstSession->cmd = XDOT_CMD_RESTORE_SESSION;
alan1974 24:e5ff476cd04e 550 pDwnRstSession->bSetNetwrk = 1;
alan1974 27:ee9c063a535b 551 pDwnRstSession->dataLen = sizeof(pkt_ntwrk)-2;
alan1974 27:ee9c063a535b 552 if(verbose)pc.printf("\n\r OTAA network session restored\r\n");
alan1974 27:ee9c063a535b 553 wait_ms(I2C_MIN_WAIT_DELAY);
alan1974 27:ee9c063a535b 554 break;
alan1974 27:ee9c063a535b 555 case XDOT_CMD_SET_RADIO_CLASS: //multicast_change_class A or C (change only after a join!)
alan1974 27:ee9c063a535b 556 if(verbose)pc.printf("\n\r proc cmd: change radio class\r\n");
alan1974 27:ee9c063a535b 557 pkt_setClassUp *pUpSetClass = (pkt_setClassUp*)&buf_rcv[0];
alan1974 27:ee9c063a535b 558 pkt_setClassDwn *pDwnSetClass = (pkt_setClassDwn*)&buf_xmt[0];
alan1974 14:fc836a5a5d2f 559 pDwnSetClass->ack = I2C_ACK_PROC;
alan1974 14:fc836a5a5d2f 560 pDwnSetClass->cmd = XDOT_CMD_SET_RADIO_CLASS;
alan1974 27:ee9c063a535b 561 pDwnSetClass->dataLen = sizeof(class_switch)-2;
alan1974 27:ee9c063a535b 562 bool bOk = multicast_change_class(&pUpSetClass->classData);
alan1974 27:ee9c063a535b 563 if (bOk && pUpSetClass->classData.bListen4Multicast) { //go into listen only mode for multicast pkts?
alan1974 27:ee9c063a535b 564 bListen4Multicast = true;;
alan1974 17:74d60177c6b6 565 maxMulticastSessionTime = pUpSetClass->classData.maxMulticastSessionTime;
alan1974 27:ee9c063a535b 566 }
alan1974 27:ee9c063a535b 567 pDwnSetClass->bSwitched = bOk;
alan1974 27:ee9c063a535b 568 break;
alan1974 27:ee9c063a535b 569
alan1974 27:ee9c063a535b 570
alan1974 17:74d60177c6b6 571 case XDOT_CMD_SET_NTWKSESS: //read or write network session to xdot flash
alan1974 1:0d25d9ddbe9f 572 bool bWriteSession = (bool)buf_rcv[1];
alan1974 27:ee9c063a535b 573 if (bWriteSession) {
alan1974 27:ee9c063a535b 574 if(verbose)pc.printf("\n\r proc cmd writing network sesion to flash");
alan1974 27:ee9c063a535b 575 dot->saveNetworkSession();
alan1974 27:ee9c063a535b 576 } else {
alan1974 27:ee9c063a535b 577 if(verbose)pc.printf("\n\r reading network session from flash");
alan1974 1:0d25d9ddbe9f 578 dot->restoreNetworkSession();
alan1974 27:ee9c063a535b 579 }
alan1974 27:ee9c063a535b 580 pkt_ntwrk *pktwrk = (pkt_ntwrk*)&buf_xmt[0];
alan1974 1:0d25d9ddbe9f 581 pktwrk->ack = I2C_ACK_PROC;
alan1974 1:0d25d9ddbe9f 582 pktwrk->cmd = XDOT_CMD_SET_NTWKSESS;
alan1974 27:ee9c063a535b 583 pktwrk->bSetNetwrk = (uint8_t)bWriteSession;
alan1974 27:ee9c063a535b 584 break;
alan1974 27:ee9c063a535b 585 /*
alan1974 27:ee9c063a535b 586 case XDOT_CMD_SET_KEY_X:
alan1974 27:ee9c063a535b 587 if(verbose)pc.printf("\n\r proc cmd: this command not used\r\n");
alan1974 27:ee9c063a535b 588 wait_ms(I2C_MIN_WAIT_DELAY);
alan1974 27:ee9c063a535b 589 buf_xmt[0] = I2C_ACK_PROC;
alan1974 27:ee9c063a535b 590 buf_xmt[1] = XDOT_CMD_SET_KEY_X;
alan1974 27:ee9c063a535b 591 break;
alan1974 27:ee9c063a535b 592 */
alan1974 0:a91cd1b08360 593 case XDOT_CMD_GATEWAY_PING:
alan1974 27:ee9c063a535b 594 if(verbose)pc.printf("\n\r proc cmd: xmit gateway ping\r\n");
alan1974 27:ee9c063a535b 595 pkt_ping *pPing = (pkt_ping*)&buf_xmt[0];
alan1974 0:a91cd1b08360 596 pPing->ack = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 597 pPing->cmd = XDOT_CMD_GATEWAY_PING;
alan1974 0:a91cd1b08360 598 pPing->dataLen = 3; //only 3 bytes returned
alan1974 27:ee9c063a535b 599 if(verbose)pc.printf("\r\n----------- SENDING GATEWAY PING \r\n");
alan1974 0:a91cd1b08360 600 mDot::ping_response ping_res;
alan1974 0:a91cd1b08360 601 ping_res = dot->ping();
alan1974 0:a91cd1b08360 602 pPing->status = (int8_t)ping_res.status;
alan1974 0:a91cd1b08360 603 pPing->rssi = (int8_t)ping_res.rssi;
alan1974 27:ee9c063a535b 604 pPing->snr = (int8_t)ping_res.snr;
alan1974 27:ee9c063a535b 605 if (ping_res.status == 0)
alan1974 27:ee9c063a535b 606 if(verbose)pc.printf("\r\n----------- GATEWAY PING SUCCEEDED \r\n");
alan1974 27:ee9c063a535b 607 else if(verbose)pc.printf("\r\n----------- GATEWAY PING FAIL \r\n");
alan1974 27:ee9c063a535b 608 break;
alan1974 27:ee9c063a535b 609
alan1974 0:a91cd1b08360 610 default:
alan1974 27:ee9c063a535b 611 if(verbose)pc.printf("\n\r proc cmd not recognized:%x",buf_rcv[0]);
alan1974 0:a91cd1b08360 612 wait_ms(I2C_MIN_WAIT_DELAY);
alan1974 0:a91cd1b08360 613 buf_xmt[0] = I2C_ACK_PROC;
alan1974 0:a91cd1b08360 614 buf_xmt[1] = XDOT_CMD_UNDEFINED;
alan1974 0:a91cd1b08360 615 } //switch buf_rcv[0]
alan1974 0:a91cd1b08360 616 //gpio1 =1; //test
alan1974 0:a91cd1b08360 617 if (bPulseLoraWake) i2c_pulse_wake(); //pulse wake-up lo->hi->lo to signal proc that xdot ready to send ack
alan1974 27:ee9c063a535b 618
alan1974 0:a91cd1b08360 619 } //switch i2c_proc_comm
alan1974 27:ee9c063a535b 620 } //while
alan1974 27:ee9c063a535b 621
alan1974 0:a91cd1b08360 622
alan1974 0:a91cd1b08360 623 } //main
alan1974 0:a91cd1b08360 624