123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
-
- #if GATEWAYNODE==1
- #include "LoRaCode.h"
- unsigned char DevAddr[4] = _DEVADDR ;
- #if _GPS==1
- static void smartDelay(unsigned long ms)
- {
- unsigned long start = millis();
- do
- {
- while (Serial1.available())
- gps.encode(Serial1.read());
- } while (millis() - start < ms);
- }
- #endif
- static int LoRaSensors(uint8_t *buf) {
-
- uint8_t tchars = 1;
- buf[0] = 0x86;
- #if _DUSB>=1
- if (debug>=0)
- Serial.print(F("LoRaSensors:: "));
- #endif
- #if _BATTERY==1
- #if _DUSB>=1
- if (debug>=0)
- Serial.print(F("Battery "));
- #endif
- #if defined(ARDUINO_ARCH_ESP8266) || defined(ESP32)
-
-
- pinMode(35, INPUT);
- #if defined(ESP32)
- int devider=4095;
- #else
- int devider=1023;
- #endif
- float volts=3.3 * analogRead(35) / 4095 * 2;
- #else
-
- float volts=0;
- #endif
- tchars += lcode.eBattery(volts, buf + tchars);
- #endif
- #if _GPS==1
- #if _DUSB>=1
- if (debug>=0)
- Serial.print(F("M GPS "));
- if (( debug>=1 ) && ( pdebug & P_MAIN )) {
- Serial.print("\tLatitude : ");
- Serial.println(gps.location.lat(), 5);
- Serial.print("\tLongitude : ");
- Serial.println(gps.location.lng(), 4);
- Serial.print("\tSatellites: ");
- Serial.println(gps.satellites.value());
- Serial.print("\tAltitude : ");
- Serial.print(gps.altitude.feet() / 3.2808);
- Serial.println("M");
- Serial.print("\tTime : ");
- Serial.print(gps.time.hour());
- Serial.print(":");
- Serial.print(gps.time.minute());
- Serial.print(":");
- Serial.println(gps.time.second());
- }
- #endif
- smartDelay(1000);
-
- if (millis() > 5000 && gps.charsProcessed() < 10) {
- #if _DUSB>=1
- Serial.println(F("No GPS data received: check wiring"));
- #endif
- return(0);
- }
-
-
-
-
- tchars += lcode.eGpsL(gps.location.lat(), gps.location.lng(), gps.altitude.value(),
- gps.satellites.value(), buf + tchars);
- #endif
- #if _DUSB>=1
- if (debug>=0)
- Serial.println();
- #endif
-
- lcode.eMsg(buf, tchars);
-
- return(tchars);
- }
- static void mXor(uint8_t *buf, uint8_t *key) {
- for (uint8_t i = 0; i < 16; ++i) buf[i] ^= key[i];
- }
- static void shift_left(uint8_t * buf, uint8_t len) {
- while (len--) {
- uint8_t next = len ? buf[1] : 0;
- uint8_t val = (*buf << 1);
- if (next & 0x80) val |= 0x01;
- *buf++ = val;
- }
- }
- static void generate_subkey(uint8_t *key, uint8_t *k1, uint8_t *k2) {
- memset(k1, 0, 16);
-
-
- AES_Encrypt(k1,key);
-
-
- if (k1[0] & 0x80) {
- shift_left(k1,16);
- k1[15] ^= 0x87;
- }
- else {
- shift_left(k1,16);
- }
-
-
- for (uint8_t i=0; i<16; i++) k2[i]=k1[i];
- if (k1[0] & 0x80) {
- shift_left(k2,16);
- k2[15] ^= 0x87;
- }
- else {
- shift_left(k2,16);
- }
-
-
- return;
- }
- uint8_t micPacket(uint8_t *data, uint8_t len, uint16_t FrameCount, uint8_t * NwkSKey, uint8_t dir) {
-
- uint8_t Block_B[16];
- uint8_t X[16];
- uint8_t Y[16];
-
-
-
- Block_B[0]= 0x49;
-
- Block_B[1]= 0x00;
- Block_B[2]= 0x00;
- Block_B[3]= 0x00;
- Block_B[4]= 0x00;
-
- Block_B[5]= dir;
-
- Block_B[6]= DevAddr[3];
- Block_B[7]= DevAddr[2];
- Block_B[8]= DevAddr[1];
- Block_B[9]= DevAddr[0];
-
- Block_B[10]= (FrameCount & 0x00FF);
- Block_B[11]= ((FrameCount >> 8) & 0x00FF);
- Block_B[12]= 0x00;
- Block_B[13]= 0x00;
-
- Block_B[14]= 0x00;
-
- Block_B[15]= len;
-
-
-
-
- uint8_t k1[16];
- uint8_t k2[16];
- generate_subkey(NwkSKey, k1, k2);
-
-
-
-
- uint8_t micBuf[len+16];
- for (uint8_t i=0; i<16; i++) micBuf[i]=Block_B[i];
- for (uint8_t i=0; i<len; i++) micBuf[i+16]=data[i];
-
-
-
-
- uint8_t numBlocks = len/16 + 1;
- if ((len % 16)!=0) numBlocks++;
-
-
-
-
- uint8_t restBits = len%16;
-
-
-
-
-
- memset(X, 0, 16);
-
-
-
-
- for(uint8_t i= 0x0; i < (numBlocks - 1); i++) {
- for (uint8_t j=0; j<16; j++) Y[j] = micBuf[(i*16)+j];
- mXor(Y, X);
- AES_Encrypt(Y, NwkSKey);
- for (uint8_t j=0; j<16; j++) X[j] = Y[j];
- }
-
-
-
-
-
-
- if (restBits) {
- for (uint8_t i=0; i<16; i++) {
- if (i< restBits) Y[i] = micBuf[((numBlocks-1)*16)+i];
- if (i==restBits) Y[i] = 0x80;
- if (i> restBits) Y[i] = 0x00;
- }
- mXor(Y, k2);
- }
- else {
- for (uint8_t i=0; i<16; i++) {
- Y[i] = micBuf[((numBlocks-1)*16)+i];
- }
- mXor(Y, k1);
- }
- mXor(Y, X);
- AES_Encrypt(Y,NwkSKey);
-
-
-
-
-
-
- data[len+0]=Y[0];
- data[len+1]=Y[1];
- data[len+2]=Y[2];
- data[len+3]=Y[3];
- return 4;
- }
- #if _CHECK_MIC==1
- static void checkMic(uint8_t *buf, uint8_t len, uint8_t *key) {
- uint8_t cBuf[len+1];
- uint8_t NwkSKey[16] = _NWKSKEY;
-
- if (debug>=2) {
- Serial.print(F("old="));
- for (uint8_t i=0; i<len; i++) {
- printHexDigit(buf[i]);
- Serial.print(' ');
- }
- Serial.println();
- }
- for (uint8_t i=0; i<len-4; i++) cBuf[i] = buf[i];
- len -=4;
-
- uint16_t FrameCount = ( cBuf[7] * 256 ) + cBuf[6];
- len += micPacket(cBuf, len, FrameCount, NwkSKey, 0);
-
- if (debug>=2) {
- Serial.print(F("new="));
- for (uint8_t i=0; i<len; i++) {
- printHexDigit(cBuf[i]);
- Serial.print(' ');
- }
- Serial.println();
- }
-
- }
- #endif
- int sensorPacket() {
- uint8_t buff_up[512];
- uint8_t message[64]={ 0 };
- uint8_t mlength = 0;
- uint32_t tmst = micros();
- struct LoraUp LUP;
- uint8_t NwkSKey[16] = _NWKSKEY;
- uint8_t AppSKey[16] = _APPSKEY;
- uint8_t DevAddr[4] = _DEVADDR;
-
-
- LUP.sf = 8;
- LUP.prssi = -50;
- LUP.rssicorr = 139;
- LUP.snr = 0;
-
-
-
-
-
-
-
-
- LUP.payLoad[0] = 0x40;
-
-
-
-
-
- LUP.payLoad[1] = DevAddr[3];
- LUP.payLoad[2] = DevAddr[2];
- LUP.payLoad[3] = DevAddr[1];
- LUP.payLoad[4] = DevAddr[0];
-
- LUP.payLoad[5] = 0x00;
- LUP.payLoad[6] = frameCount % 0x100;
- LUP.payLoad[7] = frameCount / 0x100;
-
-
-
- LUP.payLoad[8] = 0x01;
- LUP.payLength = 9;
-
-
-
-
-
-
-
- uint8_t PayLength = LoRaSensors((uint8_t *)(LUP.payLoad + LUP.payLength));
- #if _DUSB>=1
- if ((debug>=2) && (pdebug & P_RADIO )) {
- Serial.print(F("old: "));
- for (int i=0; i<PayLength; i++) {
- Serial.print(LUP.payLoad[i],HEX);
- Serial.print(' ');
- }
- Serial.println();
- }
- #endif
-
-
- uint8_t CodeLength = encodePacket((uint8_t *)(LUP.payLoad + LUP.payLength), PayLength, (uint16_t)frameCount, DevAddr, AppSKey, 0);
- #if _DUSB>=1
- if ((debug>=2) && (pdebug & P_RADIO )) {
- Serial.print(F("new: "));
- for (int i=0; i<CodeLength; i++) {
- Serial.print(LUP.payLoad[i],HEX);
- Serial.print(' ');
- }
- Serial.println();
- }
- #endif
- LUP.payLength += CodeLength;
-
-
-
-
-
-
-
- LUP.payLength += micPacket((uint8_t *)(LUP.payLoad), LUP.payLength, (uint16_t)frameCount, NwkSKey, 0);
- #if _DUSB>=1
- if ((debug>=2) && (pdebug & P_RADIO )) {
- Serial.print(F("mic: "));
- for (int i=0; i<LUP.payLength; i++) {
- Serial.print(LUP.payLoad[i],HEX);
- Serial.print(' ');
- }
- Serial.println();
- }
- #endif
-
-
-
-
- int buff_index = buildPacket(tmst, buff_up, LUP, true);
-
- frameCount++;
- statc.msg_ttl++;
- switch(ifreq) {
- case 0: statc.msg_ttl_0++; break;
- case 1: statc.msg_ttl_1++; break;
- case 2: statc.msg_ttl_2++; break;
- }
-
-
-
-
-
-
- if (( frameCount % 10)==0) writeGwayCfg(CONFIGFILE);
-
- if (buff_index > 512) {
- if (debug>0) Serial.println(F("sensorPacket:: ERROR buffer size too large"));
- return(-1);
- }
- #ifdef _TTNSERVER
- if (!sendUdp(ttnServer, _TTNPORT, buff_up, buff_index)) {
- return(-1);
- }
- #endif
- #ifdef _THINGSERVER
- if (!sendUdp(thingServer, _THINGPORT, buff_up, buff_index)) {
- return(-1);
- }
- #endif
- #if _DUSB>=1
-
-
- if ((debug>=2) && (pdebug & P_RADIO )) {
- CodeLength = encodePacket((uint8_t *)(LUP.payLoad + 9), PayLength, (uint16_t)frameCount-1, DevAddr, AppSKey, 0);
- Serial.print(F("rev: "));
- for (int i=0; i<CodeLength; i++) {
- Serial.print(LUP.payLoad[i],HEX);
- Serial.print(' ');
- }
- Serial.print(F(", addr="));
- for (int i=0; i<4; i++) {
- Serial.print(DevAddr[i],HEX);
- Serial.print(' ');
- }
- Serial.println();
- }
- #endif
- if (_cad) {
-
- _state = S_SCAN;
- sf = SF7;
- cadScanner();
- }
- else {
-
- _state = S_RX;
- rxLoraModem();
- }
-
- return(buff_index);
- }
- #endif
- #if (GATEWAYNODE==1) || (_LOCALSERVER==1)
- uint8_t encodePacket(uint8_t *Data, uint8_t DataLength, uint16_t FrameCount, uint8_t *DevAddr, uint8_t *AppSKey, uint8_t Direction) {
- #if _DUSB>=1
- if (( debug>=2 ) && ( pdebug & P_GUI )) {
- Serial.print(F("G encodePacket:: DevAddr="));
- for (int i=0; i<4; i++ ) { Serial.print(DevAddr[i],HEX); Serial.print(' '); }
- Serial.print(F("G encodePacket:: AppSKey="));
- for (int i=0; i<16; i++ ) { Serial.print(AppSKey[i],HEX); Serial.print(' '); }
- Serial.println();
- }
- #endif
-
- uint8_t i, j;
- uint8_t Block_A[16];
- uint8_t bLen=16;
-
- uint8_t restLength = DataLength % 16;
- uint8_t numBlocks = DataLength / 16;
- if (restLength>0) numBlocks++;
- for(i = 1; i <= numBlocks; i++) {
- Block_A[0] = 0x01;
-
- Block_A[1] = 0x00;
- Block_A[2] = 0x00;
- Block_A[3] = 0x00;
- Block_A[4] = 0x00;
- Block_A[5] = Direction;
- Block_A[6] = DevAddr[3];
- Block_A[7] = DevAddr[2];
- Block_A[8] = DevAddr[1];
- Block_A[9] = DevAddr[0];
- Block_A[10] = (FrameCount & 0x00FF);
- Block_A[11] = ((FrameCount >> 8) & 0x00FF);
- Block_A[12] = 0x00;
- Block_A[13] = 0x00;
- Block_A[14] = 0x00;
- Block_A[15] = i;
-
- AES_Encrypt(Block_A, AppSKey);
-
-
- if ((i == numBlocks) && (restLength>0)) bLen = restLength;
-
- for(j = 0; j < bLen; j++) {
- *Data = *Data ^ Block_A[j];
- Data++;
- }
- }
-
- return(DataLength);
- }
- #endif
|