alan broad
/
carbon_v5_arm_studio
arm studio build
src/multicast.cpp@31:4539c212ecaa, 2020-07-29 (annotated)
- Committer:
- alan1974
- Date:
- Wed Jul 29 05:38:19 2020 +0000
- Revision:
- 31:4539c212ecaa
- Parent:
- 18:d95e1a2c4303
updated to libxDot 3.3.5 and mbed_os 5.15.1; build but not working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alan1974 | 14:fc836a5a5d2f | 1 | #include "mbed.h" |
alan1974 | 14:fc836a5a5d2f | 2 | #include "dot_util.h" |
alan1974 | 14:fc836a5a5d2f | 3 | #include "mDot.h" |
alan1974 | 14:fc836a5a5d2f | 4 | #include "RadioEvent.h" |
alan1974 | 14:fc836a5a5d2f | 5 | #include "ChannelPlans.h" |
alan1974 | 14:fc836a5a5d2f | 6 | #include "Lora.h" |
alan1974 | 18:d95e1a2c4303 | 7 | #include "commI2C.h" |
alan1974 | 14:fc836a5a5d2f | 8 | #include "multicast.h" |
alan1974 | 14:fc836a5a5d2f | 9 | |
alan1974 | 18:d95e1a2c4303 | 10 | extern bool bListen4Multicast; |
alan1974 | 18:d95e1a2c4303 | 11 | extern uint8_t nodeState; |
alan1974 | 18:d95e1a2c4303 | 12 | // extern uint8_t buf_xmt[BUFFER_SIZE_I2C]; //outgoing data |
alan1974 | 14:fc836a5a5d2f | 13 | |
alan1974 | 14:fc836a5a5d2f | 14 | //OTAA keys |
alan1974 | 14:fc836a5a5d2f | 15 | extern uint8_t app_eui[]; |
alan1974 | 14:fc836a5a5d2f | 16 | extern uint8_t app_key[]; |
alan1974 | 14:fc836a5a5d2f | 17 | |
alan1974 | 14:fc836a5a5d2f | 18 | //arrays to hold OTAA keys |
alan1974 | 14:fc836a5a5d2f | 19 | std::vector<uint8_t> netwrk_address_OTAA; |
alan1974 | 14:fc836a5a5d2f | 20 | std::vector<uint8_t> netwrk_session_key_OTAA; |
alan1974 | 14:fc836a5a5d2f | 21 | std::vector<uint8_t> data_session_key_OTAA; |
alan1974 | 14:fc836a5a5d2f | 22 | uint32_t downcounter_OTAA; |
alan1974 | 14:fc836a5a5d2f | 23 | uint32_t upcounter_OTAA; |
alan1974 | 14:fc836a5a5d2f | 24 | |
alan1974 | 14:fc836a5a5d2f | 25 | //local |
alan1974 | 18:d95e1a2c4303 | 26 | //uint8_t frags_old_nmb = 0; |
alan1974 | 18:d95e1a2c4303 | 27 | //uint8_t frags_missing_cnts = 0; |
alan1974 | 18:d95e1a2c4303 | 28 | //uint8_t frags_nmb_sending = 0; //# of frags to be send |
alan1974 | 18:d95e1a2c4303 | 29 | //uint8_t frags_size = 0; //size of each frag to be sent |
alan1974 | 18:d95e1a2c4303 | 30 | //uint32_t frag_missd = 0; |
alan1974 | 14:fc836a5a5d2f | 31 | //======================================================================================================== |
alan1974 | 17:74d60177c6b6 | 32 | // save_OTAA_session session parameters |
alan1974 | 14:fc836a5a5d2f | 33 | //======================================================================================================== |
alan1974 | 14:fc836a5a5d2f | 34 | void save_OTAA_session_keys() |
alan1974 | 14:fc836a5a5d2f | 35 | { |
alan1974 | 17:74d60177c6b6 | 36 | printf("\r\n saving OTAA session keys --------------------------------- \r\n"); |
alan1974 | 14:fc836a5a5d2f | 37 | netwrk_address_OTAA = dot->getNetworkAddress(); |
alan1974 | 14:fc836a5a5d2f | 38 | logInfo(" OTAA devAddr %s", mts::Text::bin2hexString(netwrk_address_OTAA).c_str()); |
alan1974 | 14:fc836a5a5d2f | 39 | netwrk_session_key_OTAA = dot->getNetworkSessionKey(); |
alan1974 | 14:fc836a5a5d2f | 40 | logInfo(" OTAA network session key %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str()); |
alan1974 | 14:fc836a5a5d2f | 41 | data_session_key_OTAA = dot->getDataSessionKey(); |
alan1974 | 14:fc836a5a5d2f | 42 | logInfo(" OTAA app session key %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str()); |
alan1974 | 14:fc836a5a5d2f | 43 | } |
alan1974 | 14:fc836a5a5d2f | 44 | //======================================================================================================== |
alan1974 | 18:d95e1a2c4303 | 45 | //change radio back to class to class A, //change back to OTAA |
alan1974 | 18:d95e1a2c4303 | 46 | // returns true if class change OK |
alan1974 | 18:d95e1a2c4303 | 47 | //======================================================================================================== |
alan1974 | 18:d95e1a2c4303 | 48 | bool multicast_change_classA() |
alan1974 | 18:d95e1a2c4303 | 49 | { |
alan1974 | 18:d95e1a2c4303 | 50 | printf("\r\n switching to class A \r\n"); |
alan1974 | 18:d95e1a2c4303 | 51 | printf("\r\nchanging network address to OTAA device address %s\r\n", mts::Text::bin2hexString(netwrk_address_OTAA).c_str()); |
alan1974 | 18:d95e1a2c4303 | 52 | if (dot->setNetworkAddress(netwrk_address_OTAA) != mDot::MDOT_OK) { |
alan1974 | 18:d95e1a2c4303 | 53 | printf("\r\nfailed to set network device address to %s", mts::Text::bin2hexString(netwrk_address_OTAA).c_str()); |
alan1974 | 18:d95e1a2c4303 | 54 | } |
alan1974 | 18:d95e1a2c4303 | 55 | printf("\r\nchanging network session key to OTAA device key %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str()); |
alan1974 | 18:d95e1a2c4303 | 56 | if (dot->setNetworkSessionKey(netwrk_session_key_OTAA) != mDot::MDOT_OK) { |
alan1974 | 18:d95e1a2c4303 | 57 | printf("\r\nfailed to set network session key to %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str()); |
alan1974 | 18:d95e1a2c4303 | 58 | } |
alan1974 | 18:d95e1a2c4303 | 59 | printf("\r\nchanging data session key to OTAA device data key %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str()); |
alan1974 | 18:d95e1a2c4303 | 60 | if (dot->setDataSessionKey(data_session_key_OTAA) != mDot::MDOT_OK) { |
alan1974 | 18:d95e1a2c4303 | 61 | printf("\r\nfailed to set data session key to %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str()); |
alan1974 | 18:d95e1a2c4303 | 62 | } |
alan1974 | 18:d95e1a2c4303 | 63 | dot->setDownLinkCounter(downcounter_OTAA); //restore frame counters before switching |
alan1974 | 18:d95e1a2c4303 | 64 | dot->setUpLinkCounter(upcounter_OTAA); |
alan1974 | 18:d95e1a2c4303 | 65 | |
alan1974 | 18:d95e1a2c4303 | 66 | if (dot->setClass("A") == mDot::MDOT_OK){ |
alan1974 | 18:d95e1a2c4303 | 67 | printf("\r\nset network mode to class A"); |
alan1974 | 18:d95e1a2c4303 | 68 | bListen4Multicast = false; //exit radio rcv only mode |
alan1974 | 18:d95e1a2c4303 | 69 | return true; |
alan1974 | 18:d95e1a2c4303 | 70 | } |
alan1974 | 18:d95e1a2c4303 | 71 | else{ |
alan1974 | 18:d95e1a2c4303 | 72 | printf("\r\n FAILED set network mode to class A"); |
alan1974 | 18:d95e1a2c4303 | 73 | return false; |
alan1974 | 18:d95e1a2c4303 | 74 | } |
alan1974 | 18:d95e1a2c4303 | 75 | } |
alan1974 | 18:d95e1a2c4303 | 76 | //======================================================================================================== |
alan1974 | 14:fc836a5a5d2f | 77 | //change radio class to class C or class A |
alan1974 | 14:fc836a5a5d2f | 78 | // bClass_C:true => change class C credentials |
alan1974 | 14:fc836a5a5d2f | 79 | // :false=> change class A credentials |
alan1974 | 14:fc836a5a5d2f | 80 | // sClass contains class C credentials |
alan1974 | 14:fc836a5a5d2f | 81 | // returns true if class change OK |
alan1974 | 14:fc836a5a5d2f | 82 | //======================================================================================================== |
alan1974 | 14:fc836a5a5d2f | 83 | bool multicast_change_class(class_switch *sClass) |
alan1974 | 14:fc836a5a5d2f | 84 | { |
alan1974 | 14:fc836a5a5d2f | 85 | if (sClass->bClassC) |
alan1974 | 14:fc836a5a5d2f | 86 | { |
alan1974 | 14:fc836a5a5d2f | 87 | uint8_t joined = dot->getNetworkJoinStatus(); //are we joined to Lorawan? |
alan1974 | 14:fc836a5a5d2f | 88 | if (!joined){ |
alan1974 | 14:fc836a5a5d2f | 89 | printf("\r\n cannot change radio class unless already joined"); |
alan1974 | 14:fc836a5a5d2f | 90 | return false; //don't switch if not joined |
alan1974 | 14:fc836a5a5d2f | 91 | } |
alan1974 | 17:74d60177c6b6 | 92 | printf("\r\n switching to class C \r\n"); |
alan1974 | 14:fc836a5a5d2f | 93 | downcounter_OTAA = dot->getDownLinkCounter(); //save frame counters before switching |
alan1974 | 14:fc836a5a5d2f | 94 | upcounter_OTAA = dot->getUpLinkCounter(); |
alan1974 | 14:fc836a5a5d2f | 95 | |
alan1974 | 17:74d60177c6b6 | 96 | dot->setUpLinkCounter(0); |
alan1974 | 17:74d60177c6b6 | 97 | dot->setDownLinkCounter(0); |
alan1974 | 14:fc836a5a5d2f | 98 | |
alan1974 | 14:fc836a5a5d2f | 99 | //load ABP keys into arrays |
alan1974 | 14:fc836a5a5d2f | 100 | std::vector<uint8_t> dev_adr; |
alan1974 | 14:fc836a5a5d2f | 101 | std::vector<uint8_t> nwky; |
alan1974 | 14:fc836a5a5d2f | 102 | std::vector<uint8_t> dataky; |
alan1974 | 14:fc836a5a5d2f | 103 | bool bKeyOk = true; |
alan1974 | 14:fc836a5a5d2f | 104 | uint8_t indx; |
alan1974 | 14:fc836a5a5d2f | 105 | |
alan1974 | 14:fc836a5a5d2f | 106 | //set ABP keys |
alan1974 | 14:fc836a5a5d2f | 107 | for (indx =0; indx < sizeof(sClass->devAdr);indx++) dev_adr.push_back(sClass->devAdr[indx]); |
alan1974 | 14:fc836a5a5d2f | 108 | for (indx =0; indx < sizeof(sClass->key_nsk);indx++) nwky.push_back(sClass->key_nsk[indx]); |
alan1974 | 14:fc836a5a5d2f | 109 | for (indx =0; indx < sizeof(sClass->key_aps);indx++) dataky.push_back(sClass->key_aps[indx]); |
alan1974 | 14:fc836a5a5d2f | 110 | //set ABP keys |
alan1974 | 14:fc836a5a5d2f | 111 | printf("\r\nsetting ABP network address %s\r\n", mts::Text::bin2hexString(dev_adr).c_str()); |
alan1974 | 14:fc836a5a5d2f | 112 | if (dot->setNetworkAddress(dev_adr) != mDot::MDOT_OK){ |
alan1974 | 14:fc836a5a5d2f | 113 | printf(" failed to set key\r\n"); |
alan1974 | 14:fc836a5a5d2f | 114 | bKeyOk = false; |
alan1974 | 14:fc836a5a5d2f | 115 | } |
alan1974 | 14:fc836a5a5d2f | 116 | printf("\r\nsetting ABP network session key %sr\n", mts::Text::bin2hexString(nwky).c_str()); |
alan1974 | 14:fc836a5a5d2f | 117 | if (dot->setNetworkSessionKey(nwky) != mDot::MDOT_OK){ |
alan1974 | 14:fc836a5a5d2f | 118 | printf(" failed to set key\r\n"); |
alan1974 | 14:fc836a5a5d2f | 119 | bKeyOk = false; |
alan1974 | 14:fc836a5a5d2f | 120 | } |
alan1974 | 14:fc836a5a5d2f | 121 | printf("\r\nsettomg ABP data session key %sr\n", mts::Text::bin2hexString(dataky).c_str()); |
alan1974 | 14:fc836a5a5d2f | 122 | if (dot->setDataSessionKey(dataky) != mDot::MDOT_OK){ |
alan1974 | 14:fc836a5a5d2f | 123 | printf(" failed to set key\r\n"); |
alan1974 | 14:fc836a5a5d2f | 124 | bKeyOk = false; |
alan1974 | 14:fc836a5a5d2f | 125 | } |
alan1974 | 14:fc836a5a5d2f | 126 | if (!bKeyOk) return false; |
alan1974 | 14:fc836a5a5d2f | 127 | |
alan1974 | 14:fc836a5a5d2f | 128 | if (dot->setClass("C") == mDot::MDOT_OK){ |
alan1974 | 14:fc836a5a5d2f | 129 | printf("\r\n set network mode to class C\r\n"); |
alan1974 | 14:fc836a5a5d2f | 130 | return true; |
alan1974 | 14:fc836a5a5d2f | 131 | } |
alan1974 | 14:fc836a5a5d2f | 132 | else{ |
alan1974 | 14:fc836a5a5d2f | 133 | printf("\r\n FAILED to set network mode to class C\r\n"); |
alan1974 | 14:fc836a5a5d2f | 134 | return false; |
alan1974 | 14:fc836a5a5d2f | 135 | } |
alan1974 | 14:fc836a5a5d2f | 136 | } //if Class C |
alan1974 | 18:d95e1a2c4303 | 137 | else{ |
alan1974 | 18:d95e1a2c4303 | 138 | multicast_change_classA(); //change back to OTAA |
alan1974 | 18:d95e1a2c4303 | 139 | } |
alan1974 | 31:4539c212ecaa | 140 | return true; |
alan1974 | 14:fc836a5a5d2f | 141 | } |
alan1974 | 14:fc836a5a5d2f | 142 | //======================================================================================================== |
alan1974 | 18:d95e1a2c4303 | 143 | //multicast_data_process() |
alan1974 | 18:d95e1a2c4303 | 144 | //transfer incoming rx mcast code frag pkt to proc via I2C |
alan1974 | 18:d95e1a2c4303 | 145 | //- I2C xfr pkt: always xfr size = BUFFER_SIZE_MCAST to proc, must match same size as used in proc, else proc will fault on i2c comm |
alan1974 | 18:d95e1a2c4303 | 146 | //- end of multicast session: |
alan1974 | 18:d95e1a2c4303 | 147 | // - first 2 bytes in mcast pkt = flash rowNmb |
alan1974 | 18:d95e1a2c4303 | 148 | // - if rowNmb = 0xffff => end of mcast session, return to normal ClassA mode |
alan1974 | 14:fc836a5a5d2f | 149 | //======================================================================================================== |
alan1974 | 14:fc836a5a5d2f | 150 | void multicast_data_process(std::vector<uint8_t> rx_packet){ |
alan1974 | 18:d95e1a2c4303 | 151 | uint8_t i; |
alan1974 | 18:d95e1a2c4303 | 152 | uint8_t buf_xmt[BUFFER_SIZE_MCAST] = {0}; |
alan1974 | 18:d95e1a2c4303 | 153 | uint8_t rxLen = rx_packet.size(); |
alan1974 | 18:d95e1a2c4303 | 154 | |
alan1974 | 18:d95e1a2c4303 | 155 | printf("\n\rmulticast frag rcvd, len: %d",rxLen); |
alan1974 | 18:d95e1a2c4303 | 156 | printf("\r\nmcast data: "); |
alan1974 | 18:d95e1a2c4303 | 157 | for (i=1; i< rxLen;i++)printf(" %x",rx_packet[i]); |
alan1974 | 18:d95e1a2c4303 | 158 | printf("\r\n"); |
alan1974 | 18:d95e1a2c4303 | 159 | //transfer to output buffer... is this needed? |
alan1974 | 18:d95e1a2c4303 | 160 | for (i=0; i< rxLen;i++) buf_xmt[i] = rx_packet[i]; |
alan1974 | 18:d95e1a2c4303 | 161 | //xfr to proc |
alan1974 | 18:d95e1a2c4303 | 162 | i2c_pulse_wake(); |
alan1974 | 18:d95e1a2c4303 | 163 | i2c_proc_comm(buf_xmt,buf_xmt,BUFFER_SIZE_MCAST); |
alan1974 | 18:d95e1a2c4303 | 164 | //check for end of mcast session |
alan1974 | 18:d95e1a2c4303 | 165 | if ((rx_packet[0] == 0xff) && (rx_packet[1] == 0xff)){ |
alan1974 | 18:d95e1a2c4303 | 166 | multicast_change_classA(); |
alan1974 | 18:d95e1a2c4303 | 167 | } |
alan1974 | 14:fc836a5a5d2f | 168 | } |