arm studio build

Dependencies:   libxDot-mbed5

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alan1974 1:0d25d9ddbe9f 1 #include "global.h"
alan1974 0:a91cd1b08360 2 #include "wbit_util.h"
alan1974 0:a91cd1b08360 3 #include "dot_util.h"
alan1974 1:0d25d9ddbe9f 4 #include "commI2C.h"
alan1974 0:a91cd1b08360 5 #include "mbed.h"
alan1974 0:a91cd1b08360 6 #include "mDot.h"
alan1974 2:0af50f386eb2 7 extern Serial pc;
alan1974 5:abfe25f0de33 8 extern bool verbose;
alan1974 1:0d25d9ddbe9f 9 uint8_t j_attempts = 0; //return number of attempts it took to join the network
alan1974 1:0d25d9ddbe9f 10
alan1974 1:0d25d9ddbe9f 11
alan1974 1:0d25d9ddbe9f 12 //network keys
alan1974 1:0d25d9ddbe9f 13 //these are used as backup keys in case nvm memory is corrupted and we can't read the keys correctly
alan1974 12:7944e4dbe853 14 uint8_t key_AppEUI[] = { 0x90, 0xF1, 0x47, 0x90, 0x6C, 0x48, 0x1D, 0x29 }; //AppEui
alan1974 12:7944e4dbe853 15 uint8_t key_AppKey[] = { 0x0F, 0xF9, 0xA2, 0x90, 0x2E, 0xAA, 0x6B, 0x8C, 0x6A, 0x4E, 0xFD, 0x67, 0xF9, 0xA6, 0xF3, 0xD3 }; ////OTAA appkey => loriot "Appkey" -> common to all nodes
alan1974 12:7944e4dbe853 16
alan1974 13:1f3a8d0be511 17 //ADP keys: for xdot spoofer eui ---> need to be uploaded from proc, using EUI: #spoofer (EUI 00-80-00-00-04-00-FF-FF)
alan1974 14:fc836a5a5d2f 18 //static uint8_t network_address[] = {0x01,0x59,0xE2,0xC7};
alan1974 14:fc836a5a5d2f 19 //static uint8_t network_session_key[] = {0xBA, 0xEF, 0x4D, 0xA3, 0x66, 0xD1, 0x85, 0xDE, 0xB1, 0xCD, 0x79, 0x15, 0xC7, 0xEB, 0x72, 0x35};
alan1974 14:fc836a5a5d2f 20 //static uint8_t data_session_key[] = {0xF9,0x13,0x94,0x0D,0x12,0x95,0x96,0x5A,0xF4,0x34,0xBB,0xA9,0x8E,0x2A,0x83,0x10};
alan1974 12:7944e4dbe853 21
alan1974 5:abfe25f0de33 22
alan1974 5:abfe25f0de33 23 //============================================================================
alan1974 5:abfe25f0de33 24 // printRadioCfg
alan1974 5:abfe25f0de33 25
alan1974 5:abfe25f0de33 26 //============================================================================
alan1974 5:abfe25f0de33 27 void printRadioCfg(){
alan1974 5:abfe25f0de33 28 if(verbose){
alan1974 5:abfe25f0de33 29 pc.printf("\r\n************************************************************");
alan1974 5:abfe25f0de33 30 pc.printf("\r\n saved radio configuration: ");
alan1974 5:abfe25f0de33 31 pc.printf("\r\n public network: %d ",dot->getPublicNetwork());
alan1974 5:abfe25f0de33 32 pc.printf("\r\n subband: %d ",dot->getFrequencySubBand());
alan1974 5:abfe25f0de33 33 pc.printf("\n\r join delay %d ",dot->getJoinDelay());
alan1974 5:abfe25f0de33 34 pc.printf("\r\n ADR: %d ",dot->getAdr());
alan1974 5:abfe25f0de33 35 pc.printf("\r\n antenna gain: %d ",dot->getAntennaGain());
alan1974 5:abfe25f0de33 36 pc.printf("\r\n transmit max pwr: %d ",dot->getMaxTxPower()); //not saved in cfg
alan1974 5:abfe25f0de33 37 pc.printf("\r\n transmit min pwr: %d ",dot->getMinTxPower()); //not saved in cfg
alan1974 5:abfe25f0de33 38 pc.printf("\r\n transmit pwr: %d ",dot->getTxPower());
alan1974 5:abfe25f0de33 39 pc.printf("\n\r tx inverted %d ",dot->getTxInverted());
alan1974 5:abfe25f0de33 40 pc.printf("\n\r rx inverted %d ",dot->getRxInverted());
alan1974 5:abfe25f0de33 41 pc.printf("\n\r rx delay %d ",dot->getRxDelay());
alan1974 5:abfe25f0de33 42 pc.printf("\r\n TxDataRate: %d ",dot->getTxDataRate());
alan1974 5:abfe25f0de33 43 pc.printf("\r\n maxPktLen: %d ",dot->getMaxPacketLength()); //not saved in cfg
alan1974 5:abfe25f0de33 44 pc.printf("\r\n************************************************************\r\n");
alan1974 5:abfe25f0de33 45 }
alan1974 5:abfe25f0de33 46 }
alan1974 2:0af50f386eb2 47 //==============================================================================
alan1974 2:0af50f386eb2 48 //printNmvData
alan1974 1:0d25d9ddbe9f 49 //==============================================================================
alan1974 2:0af50f386eb2 50 void printNmvData(nvm *pNvm){
alan1974 4:d87f8dcf40ef 51 uint8_t i;
alan1974 7:fba1e8fc7693 52 if(verbose){
alan1974 7:fba1e8fc7693 53 pc.printf("\r\n************************************************************");
alan1974 7:fba1e8fc7693 54 pc.printf("\r\nnon-volatile memory (nvm):");
alan1974 12:7944e4dbe853 55 pc.printf("\r\nkey_AppEUI:");
alan1974 12:7944e4dbe853 56 for (i = 0; i < sizeof(key_AppEUI);i++)
alan1974 12:7944e4dbe853 57 pc.printf(" %x",pNvm->key_AppEUI[i]);
alan1974 12:7944e4dbe853 58 pc.printf("\r\nkey_AppKey:");
alan1974 12:7944e4dbe853 59 for (i = 0 ; i < sizeof(key_AppKey);i++)
alan1974 12:7944e4dbe853 60 pc.printf(" %x",pNvm->key_AppKey[i]);
alan1974 7:fba1e8fc7693 61 pc.printf("\r\nbLogOutputOn: %d ",pNvm->bLogOutputOn);
alan1974 7:fba1e8fc7693 62
alan1974 13:1f3a8d0be511 63 pc.printf("\r\nstatic backup keys:");
alan1974 12:7944e4dbe853 64 pc.printf("\r\nkey_AppEUI:");
alan1974 12:7944e4dbe853 65 for (i = 0; i < sizeof(key_AppEUI);i++)
alan1974 12:7944e4dbe853 66 pc.printf(" %x",key_AppEUI[i]);
alan1974 12:7944e4dbe853 67 pc.printf("\r\nkey_AppKey:");
alan1974 12:7944e4dbe853 68 for (i = 0 ; i < sizeof(key_AppKey);i++)
alan1974 12:7944e4dbe853 69 pc.printf(" %x",key_AppKey[i]);
alan1974 7:fba1e8fc7693 70 }
alan1974 5:abfe25f0de33 71 if(verbose)pc.printf("\r\n************************************************************\r\n");
alan1974 12:7944e4dbe853 72 }
alan1974 12:7944e4dbe853 73 //==============================================================================
alan1974 12:7944e4dbe853 74 //printNvmABPData
alan1974 12:7944e4dbe853 75 //==============================================================================
alan1974 12:7944e4dbe853 76 void printNvmABPData(nvmABP *pNvmABP){
alan1974 12:7944e4dbe853 77 uint8_t i;
alan1974 12:7944e4dbe853 78 if(verbose){
alan1974 12:7944e4dbe853 79 pc.printf("\r\n************************************************************");
alan1974 12:7944e4dbe853 80 pc.printf("\r\nnon-volatile memory (nvm) for ABP credendtials:");
alan1974 12:7944e4dbe853 81 pc.printf("\r\nnetworkSessionKey:");
alan1974 12:7944e4dbe853 82 for (i = 0; i < 16;i++)
alan1974 12:7944e4dbe853 83 pc.printf(" %x",pNvmABP->key_nsk[i]);
alan1974 13:1f3a8d0be511 84 pc.printf("\r\nappicationSessonKey");
alan1974 12:7944e4dbe853 85 for (i = 0; i < 16; i++)
alan1974 12:7944e4dbe853 86 pc.printf(" %x",pNvmABP->key_aps[i]);
alan1974 13:1f3a8d0be511 87 pc.printf("\r\nDevAddr");
alan1974 12:7944e4dbe853 88 for (i = 0 ; i < 8; i++)
alan1974 12:7944e4dbe853 89 pc.printf(" %x",pNvmABP->devAdr[i]);
alan1974 12:7944e4dbe853 90 }
alan1974 12:7944e4dbe853 91 if(verbose)pc.printf("\r\n************************************************************\r\n");
alan1974 13:1f3a8d0be511 92 }
alan1974 13:1f3a8d0be511 93 //==============================================================================
alan1974 13:1f3a8d0be511 94 //getChkSum
alan1974 13:1f3a8d0be511 95 //compute chksum
alan1974 13:1f3a8d0be511 96 // *pData : pointer to byte data array
alan1974 13:1f3a8d0be511 97 // len : len of byte data array
alan1974 13:1f3a8d0be511 98 //==============================================================================
alan1974 13:1f3a8d0be511 99 uint8_t getChkSum(uint8_t *pData,uint8_t len ){
alan1974 13:1f3a8d0be511 100 uint8_t i;
alan1974 13:1f3a8d0be511 101 uint8_t chksum= 0;
alan1974 13:1f3a8d0be511 102 for (i = 0 ; i < len;i++)chksum += pData[i];
alan1974 13:1f3a8d0be511 103 return chksum;
alan1974 13:1f3a8d0be511 104 }
alan1974 13:1f3a8d0be511 105 /*
alan1974 2:0af50f386eb2 106 //==============================================================================
alan1974 2:0af50f386eb2 107 //getNvmChkSum
alan1974 1:0d25d9ddbe9f 108 //compute chksum for nvm data; don't include chksum byte in nvm struc
alan1974 1:0d25d9ddbe9f 109 //==============================================================================
alan1974 2:0af50f386eb2 110 uint8_t getNvmChkSum(nvm *pNvm){
alan1974 2:0af50f386eb2 111 uint8_t i;
alan1974 1:0d25d9ddbe9f 112 uint8_t chksum= 0;
alan1974 1:0d25d9ddbe9f 113 uint8_t *pData = (uint8_t *)pNvm;
alan1974 1:0d25d9ddbe9f 114
alan1974 1:0d25d9ddbe9f 115 for (i = 0 ; i < sizeof(nvm)-1;i++)chksum += pData[i];
alan1974 1:0d25d9ddbe9f 116 return chksum;
alan1974 13:1f3a8d0be511 117 }
alan1974 13:1f3a8d0be511 118 */
alan1974 1:0d25d9ddbe9f 119 //==============================================================================
alan1974 12:7944e4dbe853 120 //getNvmADPChkSum
alan1974 12:7944e4dbe853 121 //compute chksum for nvm data; don't include chksum byte in nvm struc
alan1974 12:7944e4dbe853 122 // TODO: COMBINE TWO CHKSUM PROCEDURES
alan1974 12:7944e4dbe853 123 //==============================================================================
alan1974 13:1f3a8d0be511 124 /*
alan1974 13:1f3a8d0be511 125 uint8_t getNvmABPChkSum(nvmABP *pNvmABP){
alan1974 12:7944e4dbe853 126 uint8_t i;
alan1974 12:7944e4dbe853 127 uint8_t chksum= 0;
alan1974 12:7944e4dbe853 128 uint8_t *pData = (uint8_t *)pNvmABP;
alan1974 12:7944e4dbe853 129
alan1974 12:7944e4dbe853 130 for (i = 0 ; i < sizeof(nvmABP)-1;i++)chksum += pData[i];
alan1974 12:7944e4dbe853 131 return chksum;
alan1974 12:7944e4dbe853 132 }
alan1974 13:1f3a8d0be511 133 */
alan1974 12:7944e4dbe853 134 //==============================================================================
alan1974 1:0d25d9ddbe9f 135 //nvmWrite
alan1974 1:0d25d9ddbe9f 136 //write nvmData struc to nvm memory
alan1974 1:0d25d9ddbe9f 137 //==============================================================================
alan1974 1:0d25d9ddbe9f 138 bool nvmWrite(nvm *pNvm){
alan1974 13:1f3a8d0be511 139 //pNvm->chksum = getNvmChkSum(pNvm);
alan1974 13:1f3a8d0be511 140 uint8_t len = sizeof(pNvm)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 141 pNvm->chksum = getChkSum((uint8_t *)pNvm,len);
alan1974 4:d87f8dcf40ef 142 printNmvData(pNvm);
alan1974 12:7944e4dbe853 143 return dot->nvmWrite(NVM_ADDR_OTAA,pNvm,sizeof(nvm));
alan1974 12:7944e4dbe853 144 }
alan1974 12:7944e4dbe853 145 //==============================================================================
alan1974 12:7944e4dbe853 146 //nvmWriteABP
alan1974 12:7944e4dbe853 147 //write nvmABPData struc to nvm memory TODO: WRITE PROCEDUREES
alan1974 12:7944e4dbe853 148 //==============================================================================
alan1974 12:7944e4dbe853 149 bool nvmWriteABP(nvmABP *pNvmABP){
alan1974 13:1f3a8d0be511 150 // pNvmABP->chksum = getNvmABPChkSum(pNvmABP);
alan1974 13:1f3a8d0be511 151 uint8_t len = sizeof(pNvmABP)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 152 pNvmABP->chksum = getChkSum((uint8_t *)pNvmABP,len);
alan1974 13:1f3a8d0be511 153 if(verbose)pc.printf("\r\n writing ABP creds to nvm:");
alan1974 12:7944e4dbe853 154 printNvmABPData(pNvmABP);
alan1974 13:1f3a8d0be511 155 return dot->nvmWrite(NVM_ADDR_ABP,pNvmABP,sizeof(nvmABP));
alan1974 1:0d25d9ddbe9f 156 }
alan1974 1:0d25d9ddbe9f 157 //==============================================================================
alan1974 8:a5316708e51d 158 //nvmRestorekeys
alan1974 13:1f3a8d0be511 159 //restore OTAA hard coded keys to nvm and also logdisplay enable/disable
alan1974 8:a5316708e51d 160 //==============================================================================
alan1974 9:cc23b2049639 161 bool nvmRestore(nvm *pNvm){
alan1974 8:a5316708e51d 162 uint8_t i;
alan1974 12:7944e4dbe853 163 for (i = 0; i < sizeof(key_AppEUI);i++){
alan1974 12:7944e4dbe853 164 pNvm->key_AppEUI[i] = key_AppEUI[i];
alan1974 8:a5316708e51d 165 }
alan1974 12:7944e4dbe853 166 for (i = 0; i < sizeof(key_AppKey);i++){
alan1974 12:7944e4dbe853 167 pNvm->key_AppKey[i] = key_AppKey[i];
alan1974 8:a5316708e51d 168 }
alan1974 13:1f3a8d0be511 169 uint8_t len = sizeof(pNvm)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 170 pNvm->chksum = getChkSum((uint8_t *)pNvm,len);
alan1974 13:1f3a8d0be511 171 //pNvm->chksum = getNvmChkSum(pNvm);
alan1974 13:1f3a8d0be511 172 if(verbose)pc.printf("\r\n restoring OTAA backup keys:");
alan1974 8:a5316708e51d 173 printNmvData(pNvm);
alan1974 12:7944e4dbe853 174 return dot->nvmWrite(NVM_ADDR_OTAA,pNvm,sizeof(nvm));
alan1974 8:a5316708e51d 175 }
alan1974 8:a5316708e51d 176 //==============================================================================
alan1974 1:0d25d9ddbe9f 177 //nvmRead
alan1974 1:0d25d9ddbe9f 178 //- read nvmData struc from nvm memory
alan1974 1:0d25d9ddbe9f 179 //- if bad chksum then default to hard code network keys
alan1974 1:0d25d9ddbe9f 180 //==============================================================================
alan1974 1:0d25d9ddbe9f 181 bool nvmRead(nvm *pNvm){
alan1974 12:7944e4dbe853 182 dot->nvmRead(NVM_ADDR_OTAA,pNvm,sizeof(nvm));
alan1974 13:1f3a8d0be511 183 uint8_t len = sizeof(pNvm)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 184 uint8_t chksum = getChkSum((uint8_t *)pNvm,len);
alan1974 13:1f3a8d0be511 185 //uint8_t chksum = getNvmChkSum(pNvm);
alan1974 7:fba1e8fc7693 186
alan1974 1:0d25d9ddbe9f 187 if (chksum == pNvm->chksum){
alan1974 13:1f3a8d0be511 188 logInfo("nvmRead: nvm OTAA chksum ok");
alan1974 1:0d25d9ddbe9f 189 return true;
alan1974 1:0d25d9ddbe9f 190 }
alan1974 8:a5316708e51d 191 return false;
alan1974 1:0d25d9ddbe9f 192 }
alan1974 12:7944e4dbe853 193
alan1974 12:7944e4dbe853 194 //==============================================================================
alan1974 12:7944e4dbe853 195 //nvmReadABP
alan1974 12:7944e4dbe853 196 //- read nvmDataABP struc from nvm memory
alan1974 12:7944e4dbe853 197 //- if bad chksum then default to hard code network keys
alan1974 12:7944e4dbe853 198 //==============================================================================
alan1974 12:7944e4dbe853 199 bool nvmReadABP(nvmABP *pNvmABP){
alan1974 13:1f3a8d0be511 200 dot->nvmRead(NVM_ADDR_ABP,pNvmABP,sizeof(nvmABP));
alan1974 13:1f3a8d0be511 201 //uint8_t chksum = getNvmABPChkSum(pNvmABP);
alan1974 13:1f3a8d0be511 202 uint8_t len = sizeof(pNvmABP)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 203 uint8_t chksum = getChkSum((uint8_t *)pNvmABP,len);
alan1974 12:7944e4dbe853 204
alan1974 12:7944e4dbe853 205 if (chksum == pNvmABP->chksum){
alan1974 13:1f3a8d0be511 206 logInfo("nvmRead: ABP chksum ok");
alan1974 12:7944e4dbe853 207 return true;
alan1974 12:7944e4dbe853 208 }
alan1974 12:7944e4dbe853 209 return false;
alan1974 12:7944e4dbe853 210 }
alan1974 1:0d25d9ddbe9f 211 //============================================================================
alan1974 1:0d25d9ddbe9f 212
alan1974 0:a91cd1b08360 213 uint8_t join_network_attempts_wbit() {
alan1974 0:a91cd1b08360 214 return j_attempts;
alan1974 0:a91cd1b08360 215 }
alan1974 0:a91cd1b08360 216
alan1974 0:a91cd1b08360 217 bool join_network_wbit(uint8_t nmbAttempts) {
alan1974 0:a91cd1b08360 218 j_attempts = 0;
alan1974 0:a91cd1b08360 219 int32_t ret = mDot::MDOT_ERROR;
alan1974 0:a91cd1b08360 220
alan1974 0:a91cd1b08360 221 // attempt to join the network
alan1974 14:fc836a5a5d2f 222 while (ret != mDot::MDOT_OK)
alan1974 14:fc836a5a5d2f 223 {
alan1974 0:a91cd1b08360 224 j_attempts++;
alan1974 0:a91cd1b08360 225 logInfo("attempt %d to join network",j_attempts);
alan1974 0:a91cd1b08360 226 ret = dot->joinNetwork();
alan1974 0:a91cd1b08360 227 if (ret == mDot::MDOT_OK) return true;
alan1974 0:a91cd1b08360 228
alan1974 0:a91cd1b08360 229 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
alan1974 0:a91cd1b08360 230 if (j_attempts >= nmbAttempts)
alan1974 0:a91cd1b08360 231 {
alan1974 0:a91cd1b08360 232 logInfo("attempts %d to join network exceeds specified attempts $d ",j_attempts,nmbAttempts);
alan1974 0:a91cd1b08360 233 return false;
alan1974 0:a91cd1b08360 234 }
alan1974 0:a91cd1b08360 235 // in some frequency bands we need to wait until another channel is available before transmitting again
alan1974 0:a91cd1b08360 236 uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1;
alan1974 0:a91cd1b08360 237 if (delay_s < 2) {
alan1974 0:a91cd1b08360 238 logInfo("waiting %lu s until next free channel", delay_s);
alan1974 0:a91cd1b08360 239 wait(delay_s);
alan1974 0:a91cd1b08360 240 } else {
alan1974 0:a91cd1b08360 241 logInfo("sleeping %lu s until next free channel", delay_s);
alan1974 0:a91cd1b08360 242 dot->sleep(delay_s, mDot::RTC_ALARM, false);
alan1974 0:a91cd1b08360 243 }
alan1974 0:a91cd1b08360 244 }//while
alan1974 0:a91cd1b08360 245 return false;
alan1974 0:a91cd1b08360 246 }