arm studio build

Dependencies:   libxDot-mbed5

Committer:
alan1974
Date:
Fri Jan 18 18:15:29 2019 +0000
Revision:
14:fc836a5a5d2f
Child:
17:74d60177c6b6
upload of ABP credentials done

Who changed what in which revision?

UserRevisionLine numberNew 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 14:fc836a5a5d2f 7 #include "multicast.h"
alan1974 14:fc836a5a5d2f 8
alan1974 14:fc836a5a5d2f 9
alan1974 14:fc836a5a5d2f 10 extern uint8_t nodeState;
alan1974 14:fc836a5a5d2f 11 //OTAA keys
alan1974 14:fc836a5a5d2f 12 extern uint8_t app_eui[];
alan1974 14:fc836a5a5d2f 13 extern uint8_t app_key[];
alan1974 14:fc836a5a5d2f 14
alan1974 14:fc836a5a5d2f 15 //arrays to hold OTAA keys
alan1974 14:fc836a5a5d2f 16 std::vector<uint8_t> netwrk_address_OTAA;
alan1974 14:fc836a5a5d2f 17 std::vector<uint8_t> netwrk_session_key_OTAA;
alan1974 14:fc836a5a5d2f 18 std::vector<uint8_t> data_session_key_OTAA;
alan1974 14:fc836a5a5d2f 19 uint32_t downcounter_OTAA;
alan1974 14:fc836a5a5d2f 20 uint32_t upcounter_OTAA;
alan1974 14:fc836a5a5d2f 21
alan1974 14:fc836a5a5d2f 22 //local
alan1974 14:fc836a5a5d2f 23 uint8_t frags_old_nmb = 0;
alan1974 14:fc836a5a5d2f 24 uint8_t frags_missing_cnts = 0;
alan1974 14:fc836a5a5d2f 25 uint8_t frags_nmb_sending = 0; //# of frags to be send
alan1974 14:fc836a5a5d2f 26 uint8_t frags_size = 0; //size of each frag to be sent
alan1974 14:fc836a5a5d2f 27 uint32_t frag_missd = 0;
alan1974 14:fc836a5a5d2f 28 //========================================================================================================
alan1974 14:fc836a5a5d2f 29 //save_OTAA_session
alan1974 14:fc836a5a5d2f 30 // save OTAA session parameters
alan1974 14:fc836a5a5d2f 31 //========================================================================================================
alan1974 14:fc836a5a5d2f 32 void save_OTAA_session_keys()
alan1974 14:fc836a5a5d2f 33 {
alan1974 14:fc836a5a5d2f 34 netwrk_address_OTAA = dot->getNetworkAddress();
alan1974 14:fc836a5a5d2f 35 logInfo(" OTAA devAddr %s", mts::Text::bin2hexString(netwrk_address_OTAA).c_str());
alan1974 14:fc836a5a5d2f 36 netwrk_session_key_OTAA = dot->getNetworkSessionKey();
alan1974 14:fc836a5a5d2f 37 logInfo(" OTAA network session key %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str());
alan1974 14:fc836a5a5d2f 38 data_session_key_OTAA = dot->getDataSessionKey();
alan1974 14:fc836a5a5d2f 39 logInfo(" OTAA app session key %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str());
alan1974 14:fc836a5a5d2f 40 }
alan1974 14:fc836a5a5d2f 41 //========================================================================================================
alan1974 14:fc836a5a5d2f 42 //change radio class to class C or class A
alan1974 14:fc836a5a5d2f 43 // bClass_C:true => change class C credentials
alan1974 14:fc836a5a5d2f 44 // :false=> change class A credentials
alan1974 14:fc836a5a5d2f 45 // sClass contains class C credentials
alan1974 14:fc836a5a5d2f 46 // returns true if class change OK
alan1974 14:fc836a5a5d2f 47 //========================================================================================================
alan1974 14:fc836a5a5d2f 48 bool multicast_change_class(class_switch *sClass)
alan1974 14:fc836a5a5d2f 49 {
alan1974 14:fc836a5a5d2f 50 if (sClass->bClassC)
alan1974 14:fc836a5a5d2f 51 {
alan1974 14:fc836a5a5d2f 52 uint8_t joined = dot->getNetworkJoinStatus(); //are we joined to Lorawan?
alan1974 14:fc836a5a5d2f 53 if (!joined){
alan1974 14:fc836a5a5d2f 54 printf("\r\n cannot change radio class unless already joined");
alan1974 14:fc836a5a5d2f 55 return false; //don't switch if not joined
alan1974 14:fc836a5a5d2f 56 }
alan1974 14:fc836a5a5d2f 57 downcounter_OTAA = dot->getDownLinkCounter(); //save frame counters before switching
alan1974 14:fc836a5a5d2f 58 upcounter_OTAA = dot->getUpLinkCounter();
alan1974 14:fc836a5a5d2f 59
alan1974 14:fc836a5a5d2f 60 dot->setDownLinkCounter(0);//do all nodes need to reset, else reject packts with high DownlinkCounter values?
alan1974 14:fc836a5a5d2f 61
alan1974 14:fc836a5a5d2f 62 //load ABP keys into arrays
alan1974 14:fc836a5a5d2f 63 std::vector<uint8_t> dev_adr;
alan1974 14:fc836a5a5d2f 64 std::vector<uint8_t> nwky;
alan1974 14:fc836a5a5d2f 65 std::vector<uint8_t> dataky;
alan1974 14:fc836a5a5d2f 66 bool bKeyOk = true;
alan1974 14:fc836a5a5d2f 67 uint8_t indx;
alan1974 14:fc836a5a5d2f 68
alan1974 14:fc836a5a5d2f 69 //set ABP keys
alan1974 14:fc836a5a5d2f 70 for (indx =0; indx < sizeof(sClass->devAdr);indx++) dev_adr.push_back(sClass->devAdr[indx]);
alan1974 14:fc836a5a5d2f 71 for (indx =0; indx < sizeof(sClass->key_nsk);indx++) nwky.push_back(sClass->key_nsk[indx]);
alan1974 14:fc836a5a5d2f 72 for (indx =0; indx < sizeof(sClass->key_aps);indx++) dataky.push_back(sClass->key_aps[indx]);
alan1974 14:fc836a5a5d2f 73 //set ABP keys
alan1974 14:fc836a5a5d2f 74 printf("\r\nsetting ABP network address %s\r\n", mts::Text::bin2hexString(dev_adr).c_str());
alan1974 14:fc836a5a5d2f 75 if (dot->setNetworkAddress(dev_adr) != mDot::MDOT_OK){
alan1974 14:fc836a5a5d2f 76 printf(" failed to set key\r\n");
alan1974 14:fc836a5a5d2f 77 bKeyOk = false;
alan1974 14:fc836a5a5d2f 78 }
alan1974 14:fc836a5a5d2f 79 printf("\r\nsetting ABP network session key %sr\n", mts::Text::bin2hexString(nwky).c_str());
alan1974 14:fc836a5a5d2f 80 if (dot->setNetworkSessionKey(nwky) != mDot::MDOT_OK){
alan1974 14:fc836a5a5d2f 81 printf(" failed to set key\r\n");
alan1974 14:fc836a5a5d2f 82 bKeyOk = false;
alan1974 14:fc836a5a5d2f 83 }
alan1974 14:fc836a5a5d2f 84 printf("\r\nsettomg ABP data session key %sr\n", mts::Text::bin2hexString(dataky).c_str());
alan1974 14:fc836a5a5d2f 85 if (dot->setDataSessionKey(dataky) != mDot::MDOT_OK){
alan1974 14:fc836a5a5d2f 86 printf(" failed to set key\r\n");
alan1974 14:fc836a5a5d2f 87 bKeyOk = false;
alan1974 14:fc836a5a5d2f 88 }
alan1974 14:fc836a5a5d2f 89 if (!bKeyOk) return false;
alan1974 14:fc836a5a5d2f 90
alan1974 14:fc836a5a5d2f 91 if (dot->setClass("C") == mDot::MDOT_OK){
alan1974 14:fc836a5a5d2f 92 printf("\r\n set network mode to class C\r\n");
alan1974 14:fc836a5a5d2f 93 return true;
alan1974 14:fc836a5a5d2f 94 }
alan1974 14:fc836a5a5d2f 95 else{
alan1974 14:fc836a5a5d2f 96 printf("\r\n FAILED to set network mode to class C\r\n");
alan1974 14:fc836a5a5d2f 97 return false;
alan1974 14:fc836a5a5d2f 98 }
alan1974 14:fc836a5a5d2f 99 } //if Class C
alan1974 14:fc836a5a5d2f 100 else{ //change back to OTAA
alan1974 14:fc836a5a5d2f 101 printf("\r\nchanging network address to OTAA device address %s\r\n", mts::Text::bin2hexString(netwrk_address_OTAA).c_str());
alan1974 14:fc836a5a5d2f 102 if (dot->setNetworkAddress(netwrk_address_OTAA) != mDot::MDOT_OK) {
alan1974 14:fc836a5a5d2f 103 printf("\r\nfailed to set network device address to %s", mts::Text::bin2hexString(netwrk_address_OTAA).c_str());
alan1974 14:fc836a5a5d2f 104 }
alan1974 14:fc836a5a5d2f 105 printf("\r\nchanging network session key to OTAA device key %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str());
alan1974 14:fc836a5a5d2f 106 if (dot->setNetworkSessionKey(netwrk_session_key_OTAA) != mDot::MDOT_OK) {
alan1974 14:fc836a5a5d2f 107 printf("\r\nfailed to set network session key to %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str());
alan1974 14:fc836a5a5d2f 108 }
alan1974 14:fc836a5a5d2f 109 printf("\r\nchanging data session key to OTAA device data key %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str());
alan1974 14:fc836a5a5d2f 110 if (dot->setDataSessionKey(data_session_key_OTAA) != mDot::MDOT_OK) {
alan1974 14:fc836a5a5d2f 111 printf("\r\nfailed to set data session key to %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str());
alan1974 14:fc836a5a5d2f 112 }
alan1974 14:fc836a5a5d2f 113 dot->setDownLinkCounter(downcounter_OTAA); //restore frame counters before switching
alan1974 14:fc836a5a5d2f 114 dot->setUpLinkCounter(upcounter_OTAA);
alan1974 14:fc836a5a5d2f 115
alan1974 14:fc836a5a5d2f 116 if (dot->setClass("A") == mDot::MDOT_OK){
alan1974 14:fc836a5a5d2f 117 printf("\r\nset network mode to class A");
alan1974 14:fc836a5a5d2f 118 return true;
alan1974 14:fc836a5a5d2f 119 }
alan1974 14:fc836a5a5d2f 120 else{
alan1974 14:fc836a5a5d2f 121 printf("\r\n FAILED set network mode to class A");
alan1974 14:fc836a5a5d2f 122 return false;
alan1974 14:fc836a5a5d2f 123 }
alan1974 14:fc836a5a5d2f 124
alan1974 14:fc836a5a5d2f 125
alan1974 14:fc836a5a5d2f 126 }
alan1974 14:fc836a5a5d2f 127
alan1974 14:fc836a5a5d2f 128 }
alan1974 14:fc836a5a5d2f 129 /*
alan1974 14:fc836a5a5d2f 130 //========================================================================================================
alan1974 14:fc836a5a5d2f 131 //multicast_cmd_process()
alan1974 14:fc836a5a5d2f 132 //process a rx incoming multicast packet
alan1974 14:fc836a5a5d2f 133 //========================================================================================================
alan1974 14:fc836a5a5d2f 134 void multicast_cmd_process(std::vector<uint8_t> rx_packet){
alan1974 14:fc836a5a5d2f 135 uint8_t i;
alan1974 14:fc836a5a5d2f 136 uint8_t rxLen = rx_packet.size();
alan1974 14:fc836a5a5d2f 137 printf("\r\n**********************************************");
alan1974 14:fc836a5a5d2f 138 printf("\r\nmulticast cmd len, %d, data: ",rxLen);
alan1974 14:fc836a5a5d2f 139 for (i=0; i< rxLen;i++)printf(" %x",rx_packet[i]);
alan1974 14:fc836a5a5d2f 140 printf("\r\n");
alan1974 14:fc836a5a5d2f 141
alan1974 14:fc836a5a5d2f 142 uint8_t mCmd = rx_packet[0];
alan1974 14:fc836a5a5d2f 143 switch (mCmd){
alan1974 14:fc836a5a5d2f 144 case MULTICAST_CMD_SPOOF:
alan1974 14:fc836a5a5d2f 145 printf("\r\nmulticast to change to spoof mode: cmd#: %d",mCmd);
alan1974 14:fc836a5a5d2f 146 multicast_change_creds(true); //change credentials to NodeX
alan1974 14:fc836a5a5d2f 147 nodeState = STATE_SPOOF;
alan1974 14:fc836a5a5d2f 148 break;
alan1974 14:fc836a5a5d2f 149 case MULTICAST_CMD_GOTOCLASSC:
alan1974 14:fc836a5a5d2f 150 nodeState = STATE_MULTICAST_ACK_RCVD;
alan1974 14:fc836a5a5d2f 151 frags_missing_cnts = 0;
alan1974 14:fc836a5a5d2f 152 printf("\r\nmulticast to change to class c: cmd#: %d",mCmd);
alan1974 14:fc836a5a5d2f 153 frags_nmb_sending = rx_packet[1]; //# of frags to be sent
alan1974 14:fc836a5a5d2f 154 frags_size = rx_packet[2]; //size of each frag to be sent
alan1974 14:fc836a5a5d2f 155 frag_missd = 0xffffffff; //preload to all frags missed
alan1974 14:fc836a5a5d2f 156 printf(" frags2BeRcvd: %d frag_size(bytes): %d",frags_nmb_sending,frags_size);
alan1974 14:fc836a5a5d2f 157 break;
alan1974 14:fc836a5a5d2f 158 case MULTICAST_CMD_GOTOCLASSA:
alan1974 14:fc836a5a5d2f 159 printf("\r\nmulticast to change back to class A: cmd#: %d",mCmd);
alan1974 14:fc836a5a5d2f 160 multicast_change_creds(false); //change credentials to OTAA
alan1974 14:fc836a5a5d2f 161 nodeState = STATE_CLASSA;
alan1974 14:fc836a5a5d2f 162 break;
alan1974 14:fc836a5a5d2f 163 case MULTICAST_CMD_SENDFRAGDATA: //send the frag multicast info status
alan1974 14:fc836a5a5d2f 164 printf("\r\nmulticast send frag data: cmd#: %d",mCmd);
alan1974 14:fc836a5a5d2f 165 nodeState = STATE_MULTICAST_FRAG_INFO;
alan1974 14:fc836a5a5d2f 166 break;
alan1974 14:fc836a5a5d2f 167
alan1974 14:fc836a5a5d2f 168 } //switch
alan1974 14:fc836a5a5d2f 169 printf("\r\n **********************************************\r\n");
alan1974 14:fc836a5a5d2f 170 }
alan1974 14:fc836a5a5d2f 171 //========================================================================================================
alan1974 14:fc836a5a5d2f 172 //multicast_cmd_process()
alan1974 14:fc836a5a5d2f 173 //process a rx incoming multicast packet
alan1974 14:fc836a5a5d2f 174 //missing frags are a bit location in uint32_t frag_missd; a bit location = 1 => missing frag
alan1974 14:fc836a5a5d2f 175 //========================================================================================================
alan1974 14:fc836a5a5d2f 176 void multicast_data_process(std::vector<uint8_t> rx_packet){
alan1974 14:fc836a5a5d2f 177 uint8_t i;
alan1974 14:fc836a5a5d2f 178 uint8_t rxLen = rx_packet.size();
alan1974 14:fc836a5a5d2f 179 uint8_t frag_nmb = rx_packet[0];
alan1974 14:fc836a5a5d2f 180 printf("\r\n**********************************************");
alan1974 14:fc836a5a5d2f 181 printf("\r\n frag nmb: %d",frag_nmb);
alan1974 14:fc836a5a5d2f 182 frag_missd = frag_missd &~(1UL << (frag_nmb-1));
alan1974 14:fc836a5a5d2f 183 printf(" missdd frag bit pattern: %x,",frag_missd);
alan1974 14:fc836a5a5d2f 184 if (frag_nmb > frags_old_nmb+1) frags_missing_cnts++;
alan1974 14:fc836a5a5d2f 185
alan1974 14:fc836a5a5d2f 186 printf("\n\rmulticast data rcvd, len: %d,missing frag cnt: %d ",rxLen,frags_missing_cnts);
alan1974 14:fc836a5a5d2f 187 printf("\r\ndata: ");
alan1974 14:fc836a5a5d2f 188 for (i=1; i< rxLen;i++)printf(" %x",rx_packet[i]);
alan1974 14:fc836a5a5d2f 189 printf("\r\n");
alan1974 14:fc836a5a5d2f 190 printf("\r\n**********************************************\r\n");
alan1974 14:fc836a5a5d2f 191 frags_old_nmb = frag_nmb;
alan1974 14:fc836a5a5d2f 192
alan1974 14:fc836a5a5d2f 193 if (frag_nmb == frags_nmb_sending){
alan1974 14:fc836a5a5d2f 194 printf("last frag rcvd, changing to classA");
alan1974 14:fc836a5a5d2f 195 multicast_change_creds(false); //change credentials to OTAA
alan1974 14:fc836a5a5d2f 196 nodeState = STATE_CLASSA;
alan1974 14:fc836a5a5d2f 197 }
alan1974 14:fc836a5a5d2f 198 }
alan1974 14:fc836a5a5d2f 199 */