-
Notifications
You must be signed in to change notification settings - Fork 7
/
LoraNode.cpp
233 lines (199 loc) · 6.14 KB
/
LoraNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "Arduino.h"
#include "LoraNode.h"
#include "QList.h"
//Todo: Jeder Node kann immer nur einem Gateway zugeordnet sein.
//Bisher ist diese Zuordnung per #define fest im Code verankert.
//Spaeter koennte diese Zuordnung per Eeprom-Konfiguration festgelegt werden.
#define DEVICE_LORA_GATEWAY (DEVICE_CLASS_INFRASTRUCTURE | DEVICE_TYPE_GATEWAY | 0x0001) //LoRa Gateway
#define LORA_RESP_WAITTIME 40 //Zeit in ms zwischen Empfang eines Lora Command Frames und dem Versenden des ACK Frames
typedef struct _tagCallbackElem
{
uint32_t radioId;
void * cbFunction;
uint8_t cmdId;
}CALLBACK_ELEM;
LoraNode::LoraNode(int spiSelect, int reset, int busy, int interrupt) : SX126x(spiSelect,reset,busy,interrupt)
{
timer = LORA_TIMER_INACTIVE;
frameLen = 0;
onLORA_ACTOR_REQ = NULL;
onLORA_ACTOR_REQ = NULL;
onLORA_SENSOR_REQ = NULL;
onLORA_EEPWRITE_REQ = NULL;
onLORA_EEPREAD_REQ = NULL;
onLORA_PROPERTY_REQ = NULL;
#ifndef LORANODE_NO_ENCRYPT
encryptDecryptState = 0;
#endif
return;
}
LoraNode::~LoraNode()
{
}
void LoraNode::begin(uint8_t taskIntervalInMs, uint8_t spreadingFactor, uint32_t frequencyInHz, int8_t txPowerInDbm)
{
taskInterval = taskIntervalInMs;
currentTransactionContext = 0;
SX126x::begin(spreadingFactor, frequencyInHz, txPowerInDbm);
}
void LoraNode::LoRaConfig(uint8_t spreadingFactor, uint8_t bandwidth, uint8_t codingRate, uint16_t preambleLength, uint8_t payloadLen, bool crcOn, bool invertIrq)
{
SX126x::LoRaConfig(spreadingFactor, bandwidth, codingRate, preambleLength, payloadLen, crcOn, invertIrq);
}
void LoraNode::RegisterCallback(uint32_t radioId, LORACMDID cmdId, void* cb)
{
CALLBACK_ELEM cbElem;
cbElem.radioId = radioId;
cbElem.cmdId = cmdId;
cbElem.cbFunction = cb;
cbList.push_back(cbElem);
}
void LoraNode::Send_LORA_ACTOR_RESP(uint8_t status)
{
ACTOR_RESP data;
data.status = status;
QueueResponse(LORA_ACTOR_RESP, (void*)&data, sizeof(data));
}
void LoraNode::Send_LORA_SENSOR_RESP(SENSOR_RESP *pResp)
{
uint8_t len = sizeof(pResp->nSensors);
len += pResp->nSensors * sizeof(SENSOR_ELEM);
QueueResponse(LORA_SENSOR_RESP, pResp, len);
}
void LoraNode::Send_LORA_EEPWRITE_RESP(void)
{
QueueResponse(LORA_EEPREAD_RESP, NULL, 0);
}
void LoraNode::Send_LORA_EEPREAD_RESP(void)
{
QueueResponse(LORA_EEPREAD_RESP, NULL, 0);
}
void LoraNode::Send_LORA_PROPERTY_RESP(void)
{
QueueResponse(LORA_EEPREAD_RESP, NULL, 0);
}
void LoraNode::QueueResponse(LORACMDID cmdId, void* pData, uint16_t len)
{
if ( !pData || (0 == len) )
return;
frameLen = sizeof(LORAHEAD) + len;
//Todo: Jeder Node kann immer nur einem Gateway zugeordnet sein.
//Bisher ist diese Zuordnung per #define fest im Code verankert.
//Spaeter koennte diese Zuordnung per Eeprom-Konfiguration festgelegt werden.
dataFrame.head.radioId = DEVICE_LORA_GATEWAY;
dataFrame.head.cmdId = cmdId;
dataFrame.head.transactionContext = currentTransactionContext;
memcpy(&dataFrame.data, pData, len);
timer = LORA_MILLISECONDS(LORA_RESP_WAITTIME);
}
void LoraNode::ReceiveReq(void)
{
CALLBACK_ELEM cbElem;
uint8_t rxLen = 0;
bool found = false;
if ( true == ReceiveMode() )
{
rxLen = SX126x::Receive((uint8_t*)&dataFrame, sizeof(LORAFRAME));
if ( (rxLen >= 4) && (rxLen <= sizeof(LORAFRAME)) )
{
#ifndef LORANODE_NO_ENCRYPT
EncryptDecrypt((uint8_t*)&dataFrame, (uint8_t*)&dataFrame, rxLen);
#endif
for(int i=0;i<cbList.size();i++)
{
cbElem = cbList.at(i);
if ( cbElem.radioId == dataFrame.head.radioId )
if ( cbElem.cmdId == dataFrame.head.cmdId )
{
found = true;
break;
}
}
if ( found )
{
currentTransactionContext = dataFrame.head.transactionContext;
switch ( dataFrame.head.cmdId )
{
case LORA_ACTOR_REQ:
((cbLORA_ACTOR_REQ)cbElem.cbFunction)(dataFrame.actorReq.id, dataFrame.actorReq.action);
break;
case LORA_SENSOR_REQ:
((cbLORA_SENSOR_REQ)cbElem.cbFunction)(dataFrame.sensorReq.startId, dataFrame.sensorReq.nSensors);
break;
case LORA_EEPWRITE_REQ:
((cbLORA_EEPWRITE_REQ)cbElem.cbFunction)();
break;
case LORA_EEPREAD_REQ:
((cbLORA_EEPREAD_REQ)cbElem.cbFunction)();
break;
case LORA_PROPERTY_REQ:
((cbLORA_PROPERTY_REQ)cbElem.cbFunction)();
break;
default:
break;
}
}
}
}
}
void LoraNode::Send(void)
{
#ifndef LORANODE_NO_ENCRYPT
LORAFRAME d;
#endif
if ( timer == 0 )
{
#ifndef LORANODE_NO_ENCRYPT
EncryptDecrypt((uint8_t*)&dataFrame, (uint8_t*)&d, frameLen);
SX126x::Send((uint8_t*)&d, frameLen, SX126x_TXMODE_ASYNC);
#else
SX126x::Send((uint8_t*)&dataFrame, frameLen, SX126x_TXMODE_ASYNC);
#endif
timer = LORA_TIMER_INACTIVE;
}
}
void LoraNode::Timer(void)
{
if ( (timer != LORA_TIMER_INACTIVE) && (timer > 0) )
timer--;
}
#ifndef LORANODE_NO_ENCRYPT
void LoraNode::EncryptDecrypt(uint8_t* input, uint8_t* output, uint16_t len)
{
if ( encryptDecryptState == 0 )
{
memcpy(output, input, len);
return;
}
if ( encryptDecryptState == 1 )
{
//wenn wir hier sind wuerde das bedeuten, dass in einem Taskzyklus 2x gesendet
//oder 2x empfangen oder sowohl empfangen als auch gesendet wurde.
//Das kann eigentlich nicht sein, denn die Statemachines im Gateway und den Endgeraeten
//lassen das nicht zu.
spritz_setup(&encryptDecryptCtx, (uint8_t*)&encryptDecryptKey, sizeof(encryptDecryptKey));
encryptDecryptState = 2;
}
encryptDecryptState = 1;
spritz_crypt(&encryptDecryptCtx, input, len, output);
}
void LoraNode::EncryptDecryptKey(CIPHERKEY * key)
{
encryptDecryptKey = *key;
spritz_setup(&encryptDecryptCtx, (uint8_t*)&encryptDecryptKey, sizeof(encryptDecryptKey));
encryptDecryptState = 2;
}
#endif
void LoraNode::Service(void)
{
#ifndef LORANODE_NO_ENCRYPT
if ( encryptDecryptState == 1 )
{
spritz_setup(&encryptDecryptCtx, (uint8_t*)&encryptDecryptKey, sizeof(encryptDecryptKey));
encryptDecryptState = 2;
}
#endif
Timer();
ReceiveReq();
Send();
}