-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMachines.ino
426 lines (362 loc) · 11.3 KB
/
Machines.ino
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/// Expose Espressif SDK functionality - wrapped in ifdef so that it still
// compiles on other platforms
#ifdef ESP8266
extern "C" {
#include "user_interface.h"
}
#endif
#define VERSION 0x03
//uint8_t VERSION;
#define START_TTL 0x05
#define MSG_TYPE 0x00
#define TIMEOUT_HOST 60
#define TIMEOUT_REQUEST 20
#define KEEPALIVE_INTERVAL 60
#define CHANNEL 1
//weiße platine: RELAIS1=4 RELAIS2=13; neue lora-platine: RELAIS1=5 RELAIS2=4
#define RELAIS1 4
#define RELAIS2 13
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <SimpleTimer.h>
#include <map>
#include "defines.h"
WiFiClient client;
WiFiServer server(9900);
time_t timestamp = 0;
uint8_t* sendBuffer;
size_t sendBufferLength;
char packetBuffer[255]; //buffer to hold incoming packet
int localAddress = 0x00; // address of this device
bool LoRaEnabled = false;
SimpleTimer timer;
bool inProcess = false;
uint16_t ledState;
struct sniffer_buf2 *sniffer;
uint16_t seqnum = 0x000;
uint8_t rssi;
std::map<uint32_t,uint16_t> lastSeqNum;
std::map<uint32_t,uint32_t> lastRssi;
static inline uint32_t intDisable()
{
return xt_rsil(15);
}
static inline void intEnable(uint32_t state)
{
xt_wsr_ps(state);
}
uint16_t createPacket(uint8_t* result, uint8_t *buf, uint16_t len, uint32_t dst, uint8_t type)
{
memcpy(&result[0], &beacon_raw[0], sizeof(beacon_raw));
memcpy(&result[sizeof(beacon_raw)], &buf[0], len);
//dst
result[4 + 2] = (dst >> 24) & 0xFF;
result[4 + 3] = (dst >> 16) & 0xFF;
result[4 + 4] = (dst >> 8) & 0xFF;
result[4 + 5] = (dst) & 0xFF;
//src
result[10 + 2] = (ESP.getChipId() >> 24) & 0xFF;
result[10 + 3] = (ESP.getChipId() >> 16) & 0xFF;
result[10 + 4] = (ESP.getChipId() >> 8) & 0xFF;
result[10 + 5] = (ESP.getChipId()) & 0xFF;
//transmitc
result[16 + 2] = (ESP.getChipId() >> 24) & 0xFF;
result[16 + 3] = (ESP.getChipId() >> 16) & 0xFF;
result[16 + 4] = (ESP.getChipId() >> 8) & 0xFF;
result[16 + 5] = (ESP.getChipId()) & 0xFF;
result[22] = (seqnum >> 8) & 0xFF;
result[23] = (seqnum) & 0xFF;
uint16_t seqTmp = seqnum;
seqnum++;
if (seqnum > 0xfff)
seqnum = 1;
result[39] += len;
result[42] = VERSION;
result[43] = START_TTL;
result[44] = type;
return seqTmp;
}
void forwardPacket(uint8_t* result)
{
if(result[43] == 0) //double safty. if ttl is == 0, then make packet invalid
{
result[0] = 0;
result[1] = 0;
}
else
{
//decrease ttl
result[43]--;
}
//set transmitter
result[16 + 2] = (ESP.getChipId() >> 24) & 0xFF;
result[16 + 3] = (ESP.getChipId() >> 16) & 0xFF;
result[16 + 4] = (ESP.getChipId() >> 8) & 0xFF;
result[16 + 5] = (ESP.getChipId()) & 0xFF;
}
void processData(struct sniffer_buf2 *sniffer)
{
if(sniffer->buf[4] != 0xef || sniffer->buf[5] != 0x50) return;
msgData msg;
msg.dst = (sniffer->buf[6] << 24) | (sniffer->buf[7] << 16) | (sniffer->buf[8] << 8) | sniffer->buf[9];
msg.src = (sniffer->buf[12] << 24) | (sniffer->buf[13] << 16) | (sniffer->buf[14] << 8) | sniffer->buf[15];
msg.trs = (sniffer->buf[18] << 24) | (sniffer->buf[19] << 16) | (sniffer->buf[20] << 8) | sniffer->buf[21];
msg.seq = (sniffer->buf[22] << 8) | sniffer->buf[23];
msg.ver = sniffer->buf[42];
msg.ttl = sniffer->buf[43];
msg.type = sniffer->buf[44];
msg.dataLength = (sniffer->buf[39])-5;
memcpy(msg.data, &(sniffer->buf[45]), sniffer->buf[39]-5);
Serial.printf("Data (dst: %02x:%02x:%02x:%02x:%02x:%02x (0x%08x), src: %02x:%02x:%02x:%02x:%02x:%02x (0x%08x), rssi: %d, ttl: %d, type: %02x, seq: %d: ", sniffer->buf[4], sniffer->buf[5], sniffer->buf[6], sniffer->buf[7], sniffer->buf[8], sniffer->buf[9], msg.dst, sniffer->buf[10], sniffer->buf[11], sniffer->buf[12], sniffer->buf[13], sniffer->buf[14], sniffer->buf[15], msg.src, rssi, msg.ttl, msg.type, msg.seq);
for(uint16_t i = 0; i < msg.dataLength; i++)
Serial.printf("%02x ", msg.data[i]);
Serial.printf("\n");
uint32_t newRssi = millis() >> 8;
if(lastRssi.count(msg.src) > 0)
rssi = ((lastRssi[msg.src] >> 24) * 3 + sniffer->rx_ctrl.rssi) / 4;
newRssi += rssi << 24;
lastRssi[msg.src] = newRssi; //1byte rssi, 3byte timestamp
if(lastSeqNum[msg.src] == msg.seq)
{
Serial.printf("No new seq num :(\n");
return;
}
lastSeqNum[msg.src] = msg.seq;
if(msg.dst != ESP.getChipId() && msg.ttl > 0 && msg.ttl < START_TTL+1)
{
delayMicroseconds(1000+random(20000)); //1-30ms delay to avoid parallel-fwd of multiple nodes
//forward!
Serial.printf("Forward to dst(%d) from me(%d) with new ttl:%d!\n", msg.dst, ESP.getChipId(), (msg.ttl-1));
forwardPacket(sniffer->buf);
uint16_t res = wifi_send_pkt_freedom(sniffer->buf, sizeof(beacon_raw)+ msg.dataLength, 0);
}
else
{
Serial.printf("No Forward: TTL Death\n");
}
if(msg.type == MSG_Data && (msg.dst == ESP.getChipId() || msg.dst == 0xffffffff))
{
releaseRelais(msg.data[0], msg.data[1]);
//i am the reciever! yaaaaaay
Serial.printf("I am the dst (dst(%d) == chipid(%d))! Sending ack...\n", msg.dst, ESP.getChipId());
Serial.printf("My data is:\n");
for(uint16_t i = 0; i < msg.dataLength; i++)
Serial.printf("%02x ", msg.data[i]);
Serial.printf("\n\n");
if(msg.dst != 0xffffffff)
{
//send reply
uint8_t result[sizeof(beacon_raw) + 2];
uint8_t data[2] = {(msg.seq >> 8) & 0xFF, msg.seq & 0xFF}; //ack with msg.src & msg.seq
createPacket(result, data, 2, msg.src, MSG_Data_Ack);
uint16_t res = wifi_send_pkt_freedom(result, sizeof(result), 0);
/*for(uint16_t i = 0; i < sizeof(result); i++)
Serial.printf("%02x ", result[i]);
Serial.printf("\n"); */
}
}
}
void ICACHE_RAM_ATTR promisc_cb(uint8_t *buf, uint16_t len)
{
uint32_t old_ints = intDisable();
if (len == 128 && buf[12+4] == 0xef && buf[12] == 0x80){
Serial.printf("*");
if (!inProcess ){
inProcess = true;
sniffer = (struct sniffer_buf2*) buf;
rssi = buf[0];
if (sniffer->buf[0] == 0x80 /*beacon*/&& sniffer->buf[37] == 0x00 /*hidden ssid*/&& sniffer->buf[38] == 0xDD /*vendor info*/&& sniffer->buf[4] == 0xef /*magic word1*/&& sniffer->buf[5] == 0x50/*magic word2*/)
{
//dont process data here in interrupt!
// "inProcess" is set true and in the next loop "processData()" will be called to process the buffer (sniffer->buf).
}
else
{
inProcess = false;
}
}
}
intEnable(old_ints);
}
void sendKeepAlive()
{
if(inUpdateMode) return;
uint8_t result[sizeof(beacon_raw)];
uint16_t seq = createPacket(result, {}, 0, 0xffffffff, MSG_KeepAlive);
uint16_t res = wifi_send_pkt_freedom(result, sizeof(result), 0);
Serial.printf("sending KeepAlive (seqnum: %d, res: %d)\n", seq, res);
}
void setupFreedom()
{
Serial.println("Setting up Freedom Mode");
WiFi.mode(WIFI_STA);
wifi_set_channel(CHANNEL);
wifi_set_phy_mode(PHY_MODE_11B);
wifi_promiscuous_enable(0);
wifi_set_promiscuous_rx_cb(promisc_cb);
wifi_promiscuous_enable(1);
}
void setup() {
Serial.begin(115200);
delay(2000);
Serial.printf("\n\nSDK version: %s - chipId: 0x%08x - fw-version: %d\n", system_get_sdk_version(), ESP.getChipId(), VERSION);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
pinMode(RELAIS1, OUTPUT);
pinMode(RELAIS2, OUTPUT);
// Promiscuous works only with station mode
seqnum = ESP.getChipId() & 0xfff; //semi-rnd init
WiFi.mode(WIFI_STA);
setupFreedom();
sendKeepAlive();
//timer.setInterval(KEEPALIVE_INTERVAL * 1000 + (ESP.getChipId() & 0xfff), sendKeepAlive);
localAddress = ESP.getChipId();
}
void loop() {
//uint32_t currentMillis = millis();
if (millis() > 2592000000) ESP.restart(); //(every 30days)
//timer.run();
if(inProcess)
{
processData(sniffer);
inProcess = false;
}
delay(1);
}
void releaseRelais(byte lwscType, byte lwscPort)
{
uint32_t currentMillis = millis();
if(lwscType == 0xfa)
{
//1s
if(lwscPort == 0x01)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
Serial.println("1st Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS1, LOW);
}
if(lwscPort == 0x11)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS2, HIGH);
Serial.println("2nd Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS2, LOW);
}
if(lwscPort == 0x21)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
digitalWrite(RELAIS2, HIGH);
Serial.println("Both Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS1, LOW);
digitalWrite(RELAIS2, LOW);
}
//3s
if(lwscPort == 0x03)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
Serial.println("1st Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS1, LOW);
}
if(lwscPort == 0x13)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS2, HIGH);
Serial.println("2nd Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS2, LOW);
}
if(lwscPort == 0x23)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
digitalWrite(RELAIS2, HIGH);
Serial.println("Both Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS1, LOW);
digitalWrite(RELAIS2, LOW);
}
//0.4s
if(lwscPort == 0x04)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
Serial.println("1st Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS1, LOW);
}
if(lwscPort == 0x14)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS2, HIGH);
Serial.println("2nd Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS2, LOW);
}
if(lwscPort == 0x24)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
digitalWrite(RELAIS2, HIGH);
Serial.println("Both Release");
while (millis() < currentMillis + 1000) delay(1);
digitalWrite(RELAIS1, LOW);
digitalWrite(RELAIS2, LOW);
}
//dauer an
if(lwscPort == 0x0a)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
}
if(lwscPort == 0x1a)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS2, HIGH);
}
if(lwscPort == 0x2a)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, HIGH);
digitalWrite(RELAIS2, HIGH);
}
//dauer aus
if(lwscPort == 0x0b)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, LOW);
}
if(lwscPort == 0x1b)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS2, LOW);
}
if(lwscPort == 0x2b)
{
ledState != ledState;
digitalWrite(2, ledState);
digitalWrite(RELAIS1, LOW);
digitalWrite(RELAIS2, LOW);
}
}
}