arm studio build

Dependencies:   libxDot-mbed5

Committer:
alan1974
Date:
Sat Feb 22 18:14:25 2020 +0000
Revision:
26:f51ff4ad7a93
Parent:
22:034c134f17c7
Child:
31:4539c212ecaa
fixed log printout; removed wake.mode(OpenDrain); no change in rev lvl

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 22:034c134f17c7 39 //deprecated API: pc.printf("\n\r tx inverted %d ",dot->getTxInverted());
alan1974 22:034c134f17c7 40 //deprecated API: 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 17:74d60177c6b6 74 //printNvmABPData --- OLD CODE
alan1974 12:7944e4dbe853 75 //==============================================================================
alan1974 17:74d60177c6b6 76 /*
alan1974 12:7944e4dbe853 77 void printNvmABPData(nvmABP *pNvmABP){
alan1974 12:7944e4dbe853 78 uint8_t i;
alan1974 12:7944e4dbe853 79 if(verbose){
alan1974 12:7944e4dbe853 80 pc.printf("\r\n************************************************************");
alan1974 12:7944e4dbe853 81 pc.printf("\r\nnon-volatile memory (nvm) for ABP credendtials:");
alan1974 12:7944e4dbe853 82 pc.printf("\r\nnetworkSessionKey:");
alan1974 12:7944e4dbe853 83 for (i = 0; i < 16;i++)
alan1974 12:7944e4dbe853 84 pc.printf(" %x",pNvmABP->key_nsk[i]);
alan1974 13:1f3a8d0be511 85 pc.printf("\r\nappicationSessonKey");
alan1974 12:7944e4dbe853 86 for (i = 0; i < 16; i++)
alan1974 12:7944e4dbe853 87 pc.printf(" %x",pNvmABP->key_aps[i]);
alan1974 13:1f3a8d0be511 88 pc.printf("\r\nDevAddr");
alan1974 12:7944e4dbe853 89 for (i = 0 ; i < 8; i++)
alan1974 12:7944e4dbe853 90 pc.printf(" %x",pNvmABP->devAdr[i]);
alan1974 12:7944e4dbe853 91 }
alan1974 12:7944e4dbe853 92 if(verbose)pc.printf("\r\n************************************************************\r\n");
alan1974 17:74d60177c6b6 93 }
alan1974 17:74d60177c6b6 94 */
alan1974 13:1f3a8d0be511 95 //==============================================================================
alan1974 13:1f3a8d0be511 96 //getChkSum
alan1974 13:1f3a8d0be511 97 //compute chksum
alan1974 13:1f3a8d0be511 98 // *pData : pointer to byte data array
alan1974 13:1f3a8d0be511 99 // len : len of byte data array
alan1974 13:1f3a8d0be511 100 //==============================================================================
alan1974 13:1f3a8d0be511 101 uint8_t getChkSum(uint8_t *pData,uint8_t len ){
alan1974 13:1f3a8d0be511 102 uint8_t i;
alan1974 13:1f3a8d0be511 103 uint8_t chksum= 0;
alan1974 13:1f3a8d0be511 104 for (i = 0 ; i < len;i++)chksum += pData[i];
alan1974 13:1f3a8d0be511 105 return chksum;
alan1974 13:1f3a8d0be511 106 }
alan1974 13:1f3a8d0be511 107 /*
alan1974 2:0af50f386eb2 108 //==============================================================================
alan1974 2:0af50f386eb2 109 //getNvmChkSum
alan1974 1:0d25d9ddbe9f 110 //compute chksum for nvm data; don't include chksum byte in nvm struc
alan1974 1:0d25d9ddbe9f 111 //==============================================================================
alan1974 2:0af50f386eb2 112 uint8_t getNvmChkSum(nvm *pNvm){
alan1974 2:0af50f386eb2 113 uint8_t i;
alan1974 1:0d25d9ddbe9f 114 uint8_t chksum= 0;
alan1974 1:0d25d9ddbe9f 115 uint8_t *pData = (uint8_t *)pNvm;
alan1974 1:0d25d9ddbe9f 116
alan1974 1:0d25d9ddbe9f 117 for (i = 0 ; i < sizeof(nvm)-1;i++)chksum += pData[i];
alan1974 1:0d25d9ddbe9f 118 return chksum;
alan1974 13:1f3a8d0be511 119 }
alan1974 13:1f3a8d0be511 120 */
alan1974 1:0d25d9ddbe9f 121 //==============================================================================
alan1974 12:7944e4dbe853 122 //getNvmADPChkSum
alan1974 12:7944e4dbe853 123 //compute chksum for nvm data; don't include chksum byte in nvm struc
alan1974 12:7944e4dbe853 124 // TODO: COMBINE TWO CHKSUM PROCEDURES
alan1974 12:7944e4dbe853 125 //==============================================================================
alan1974 13:1f3a8d0be511 126 /*
alan1974 13:1f3a8d0be511 127 uint8_t getNvmABPChkSum(nvmABP *pNvmABP){
alan1974 12:7944e4dbe853 128 uint8_t i;
alan1974 12:7944e4dbe853 129 uint8_t chksum= 0;
alan1974 12:7944e4dbe853 130 uint8_t *pData = (uint8_t *)pNvmABP;
alan1974 12:7944e4dbe853 131
alan1974 12:7944e4dbe853 132 for (i = 0 ; i < sizeof(nvmABP)-1;i++)chksum += pData[i];
alan1974 12:7944e4dbe853 133 return chksum;
alan1974 12:7944e4dbe853 134 }
alan1974 13:1f3a8d0be511 135 */
alan1974 12:7944e4dbe853 136 //==============================================================================
alan1974 1:0d25d9ddbe9f 137 //nvmWrite
alan1974 1:0d25d9ddbe9f 138 //write nvmData struc to nvm memory
alan1974 1:0d25d9ddbe9f 139 //==============================================================================
alan1974 1:0d25d9ddbe9f 140 bool nvmWrite(nvm *pNvm){
alan1974 13:1f3a8d0be511 141 //pNvm->chksum = getNvmChkSum(pNvm);
alan1974 13:1f3a8d0be511 142 uint8_t len = sizeof(pNvm)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 143 pNvm->chksum = getChkSum((uint8_t *)pNvm,len);
alan1974 4:d87f8dcf40ef 144 printNmvData(pNvm);
alan1974 12:7944e4dbe853 145 return dot->nvmWrite(NVM_ADDR_OTAA,pNvm,sizeof(nvm));
alan1974 12:7944e4dbe853 146 }
alan1974 17:74d60177c6b6 147 /*
alan1974 12:7944e4dbe853 148 //==============================================================================
alan1974 12:7944e4dbe853 149 //nvmWriteABP
alan1974 12:7944e4dbe853 150 //write nvmABPData struc to nvm memory TODO: WRITE PROCEDUREES
alan1974 12:7944e4dbe853 151 //==============================================================================
alan1974 12:7944e4dbe853 152 bool nvmWriteABP(nvmABP *pNvmABP){
alan1974 13:1f3a8d0be511 153 // pNvmABP->chksum = getNvmABPChkSum(pNvmABP);
alan1974 13:1f3a8d0be511 154 uint8_t len = sizeof(pNvmABP)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 155 pNvmABP->chksum = getChkSum((uint8_t *)pNvmABP,len);
alan1974 13:1f3a8d0be511 156 if(verbose)pc.printf("\r\n writing ABP creds to nvm:");
alan1974 12:7944e4dbe853 157 printNvmABPData(pNvmABP);
alan1974 13:1f3a8d0be511 158 return dot->nvmWrite(NVM_ADDR_ABP,pNvmABP,sizeof(nvmABP));
alan1974 1:0d25d9ddbe9f 159 }
alan1974 17:74d60177c6b6 160 */
alan1974 1:0d25d9ddbe9f 161 //==============================================================================
alan1974 8:a5316708e51d 162 //nvmRestorekeys
alan1974 13:1f3a8d0be511 163 //restore OTAA hard coded keys to nvm and also logdisplay enable/disable
alan1974 8:a5316708e51d 164 //==============================================================================
alan1974 9:cc23b2049639 165 bool nvmRestore(nvm *pNvm){
alan1974 8:a5316708e51d 166 uint8_t i;
alan1974 12:7944e4dbe853 167 for (i = 0; i < sizeof(key_AppEUI);i++){
alan1974 12:7944e4dbe853 168 pNvm->key_AppEUI[i] = key_AppEUI[i];
alan1974 8:a5316708e51d 169 }
alan1974 12:7944e4dbe853 170 for (i = 0; i < sizeof(key_AppKey);i++){
alan1974 12:7944e4dbe853 171 pNvm->key_AppKey[i] = key_AppKey[i];
alan1974 8:a5316708e51d 172 }
alan1974 13:1f3a8d0be511 173 uint8_t len = sizeof(pNvm)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 174 pNvm->chksum = getChkSum((uint8_t *)pNvm,len);
alan1974 13:1f3a8d0be511 175 //pNvm->chksum = getNvmChkSum(pNvm);
alan1974 13:1f3a8d0be511 176 if(verbose)pc.printf("\r\n restoring OTAA backup keys:");
alan1974 8:a5316708e51d 177 printNmvData(pNvm);
alan1974 12:7944e4dbe853 178 return dot->nvmWrite(NVM_ADDR_OTAA,pNvm,sizeof(nvm));
alan1974 8:a5316708e51d 179 }
alan1974 8:a5316708e51d 180 //==============================================================================
alan1974 1:0d25d9ddbe9f 181 //nvmRead
alan1974 1:0d25d9ddbe9f 182 //- read nvmData struc from nvm memory
alan1974 1:0d25d9ddbe9f 183 //- if bad chksum then default to hard code network keys
alan1974 1:0d25d9ddbe9f 184 //==============================================================================
alan1974 1:0d25d9ddbe9f 185 bool nvmRead(nvm *pNvm){
alan1974 12:7944e4dbe853 186 dot->nvmRead(NVM_ADDR_OTAA,pNvm,sizeof(nvm));
alan1974 13:1f3a8d0be511 187 uint8_t len = sizeof(pNvm)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 188 uint8_t chksum = getChkSum((uint8_t *)pNvm,len);
alan1974 13:1f3a8d0be511 189 //uint8_t chksum = getNvmChkSum(pNvm);
alan1974 7:fba1e8fc7693 190
alan1974 1:0d25d9ddbe9f 191 if (chksum == pNvm->chksum){
alan1974 13:1f3a8d0be511 192 logInfo("nvmRead: nvm OTAA chksum ok");
alan1974 1:0d25d9ddbe9f 193 return true;
alan1974 1:0d25d9ddbe9f 194 }
alan1974 8:a5316708e51d 195 return false;
alan1974 1:0d25d9ddbe9f 196 }
alan1974 12:7944e4dbe853 197
alan1974 12:7944e4dbe853 198 //==============================================================================
alan1974 17:74d60177c6b6 199 //nvmReadABP ------------ OLD CODE
alan1974 12:7944e4dbe853 200 //- read nvmDataABP struc from nvm memory
alan1974 12:7944e4dbe853 201 //- if bad chksum then default to hard code network keys
alan1974 12:7944e4dbe853 202 //==============================================================================
alan1974 17:74d60177c6b6 203 /*
alan1974 12:7944e4dbe853 204 bool nvmReadABP(nvmABP *pNvmABP){
alan1974 13:1f3a8d0be511 205 dot->nvmRead(NVM_ADDR_ABP,pNvmABP,sizeof(nvmABP));
alan1974 13:1f3a8d0be511 206 //uint8_t chksum = getNvmABPChkSum(pNvmABP);
alan1974 13:1f3a8d0be511 207 uint8_t len = sizeof(pNvmABP)-1; //doesn't include chksum byte
alan1974 13:1f3a8d0be511 208 uint8_t chksum = getChkSum((uint8_t *)pNvmABP,len);
alan1974 12:7944e4dbe853 209
alan1974 12:7944e4dbe853 210 if (chksum == pNvmABP->chksum){
alan1974 13:1f3a8d0be511 211 logInfo("nvmRead: ABP chksum ok");
alan1974 12:7944e4dbe853 212 return true;
alan1974 12:7944e4dbe853 213 }
alan1974 12:7944e4dbe853 214 return false;
alan1974 12:7944e4dbe853 215 }
alan1974 17:74d60177c6b6 216 */
alan1974 1:0d25d9ddbe9f 217 //============================================================================
alan1974 1:0d25d9ddbe9f 218
alan1974 0:a91cd1b08360 219 uint8_t join_network_attempts_wbit() {
alan1974 0:a91cd1b08360 220 return j_attempts;
alan1974 0:a91cd1b08360 221 }
alan1974 0:a91cd1b08360 222
alan1974 0:a91cd1b08360 223 bool join_network_wbit(uint8_t nmbAttempts) {
alan1974 0:a91cd1b08360 224 j_attempts = 0;
alan1974 0:a91cd1b08360 225 int32_t ret = mDot::MDOT_ERROR;
alan1974 0:a91cd1b08360 226
alan1974 0:a91cd1b08360 227 // attempt to join the network
alan1974 14:fc836a5a5d2f 228 while (ret != mDot::MDOT_OK)
alan1974 14:fc836a5a5d2f 229 {
alan1974 0:a91cd1b08360 230 j_attempts++;
alan1974 0:a91cd1b08360 231 logInfo("attempt %d to join network",j_attempts);
alan1974 0:a91cd1b08360 232 ret = dot->joinNetwork();
alan1974 0:a91cd1b08360 233 if (ret == mDot::MDOT_OK) return true;
alan1974 0:a91cd1b08360 234
alan1974 0:a91cd1b08360 235 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
alan1974 0:a91cd1b08360 236 if (j_attempts >= nmbAttempts)
alan1974 0:a91cd1b08360 237 {
alan1974 26:f51ff4ad7a93 238 logInfo("attempts %d to join network exceeds specified attempts %d ",j_attempts,nmbAttempts);
alan1974 0:a91cd1b08360 239 return false;
alan1974 0:a91cd1b08360 240 }
alan1974 0:a91cd1b08360 241 // in some frequency bands we need to wait until another channel is available before transmitting again
alan1974 0:a91cd1b08360 242 uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1;
alan1974 0:a91cd1b08360 243 if (delay_s < 2) {
alan1974 0:a91cd1b08360 244 logInfo("waiting %lu s until next free channel", delay_s);
alan1974 0:a91cd1b08360 245 wait(delay_s);
alan1974 0:a91cd1b08360 246 } else {
alan1974 0:a91cd1b08360 247 logInfo("sleeping %lu s until next free channel", delay_s);
alan1974 0:a91cd1b08360 248 dot->sleep(delay_s, mDot::RTC_ALARM, false);
alan1974 0:a91cd1b08360 249 }
alan1974 0:a91cd1b08360 250 }//while
alan1974 0:a91cd1b08360 251 return false;
alan1974 0:a91cd1b08360 252 }