AllWize Library
AllWize_LoRaWAN.h
Go to the documentation of this file.
1 /*
2 
3 AllWize LoRaWAN Library
4 
5 This code is based on Adafruit's TinyLora Library and thus
6 
7 Copyright (C) 2015, 2016 Ideetron B.V.
8 Modified by Brent Rubell for Adafruit Industries.
9 Copyright (C) 2018-2019 by AllWize <github@allwize.io>
10 
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Lesser General Public License for more details.
20 
21 You should have received a copy of the GNU Lesser General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 
24 */
25 
31 #pragma once
32 
33 #include <Arduino.h>
34 #include "AllWize.h"
35 
36 class AllWize_LoRaWAN: public AllWize {
37 
38  public:
39 
40  AllWize_LoRaWAN(HardwareSerial * serial, uint8_t reset_gpio = GPIO_NONE): AllWize(serial, reset_gpio) {}
41  #if not defined(ARDUINO_ARCH_SAMD) && not defined(ARDUINO_ARCH_ESP32)
42  AllWize_LoRaWAN(SoftwareSerial * serial, uint8_t reset_gpio = GPIO_NONE): AllWize(serial, reset_gpio) {}
43  #endif
44  AllWize_LoRaWAN(uint8_t rx, uint8_t tx, uint8_t reset_gpio = GPIO_NONE): AllWize(rx, tx, reset_gpio) {}
45 
46 
47  bool joinABP(uint8_t *DevAddr, uint8_t *AppSKey, uint8_t * NwkSKey);
48  bool send(uint8_t *Data, uint8_t Data_Length, uint8_t Frame_Port = 0x01);
49  uint16_t getFrameCounter();
50  void setFrameCounter(uint16_t value);
51 
52  private:
53 
54  uint8_t _devaddr[4];
55  uint8_t _appskey[16];
56  uint8_t _nwkskey[16];
57  uint16_t _frame_counter = 0;
58  static const uint8_t S_Table[16][16];
59 
60  void Encrypt_Payload(uint8_t *Data, uint8_t Data_Length, uint16_t Frame_Counter, uint8_t Direction);
61  void Calculate_MIC(uint8_t *Data, uint8_t *Final_MIC, uint8_t Data_Length, uint16_t Frame_Counter, uint8_t Direction);
62  void Generate_Keys(uint8_t *K1, uint8_t *K2);
63  void Shift_Left(uint8_t *Data);
64  void XOR(uint8_t *New_Data, uint8_t *Old_Data);
65 
66  void AES_Encrypt(uint8_t *Data, const uint8_t *Key);
67  void AES_Add_Round_Key(uint8_t *Round_Key, uint8_t(*State)[4]);
68  uint8_t AES_Sub_Byte(uint8_t Byte);
69  void AES_Shift_Rows(uint8_t(*State)[4]);
70  void AES_Mix_Collums(uint8_t(*State)[4]);
71  void AES_Calculate_Round_Key(uint8_t Round, uint8_t *Round_Key);
72 
73 };
AllWize_LoRaWAN(HardwareSerial *serial, uint8_t reset_gpio=GPIO_NONE)
AllWize_LoRaWAN(uint8_t rx, uint8_t tx, uint8_t reset_gpio=GPIO_NONE)
bool send(uint8_t *Data, uint8_t Data_Length, uint8_t Frame_Port=0x01)
Function to assemble and send a LoRaWAN package.
bool joinABP(uint8_t *DevAddr, uint8_t *AppSKey, uint8_t *NwkSKey)
Stores the application and network keys for ABP activation.
#define GPIO_NONE
Definition: AllWize.h:44
uint16_t getFrameCounter()
Returns current frame counter.
void setFrameCounter(uint16_t value)
Sets new frame counter.