-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblinkercough.ino
211 lines (173 loc) · 5.48 KB
/
blinkercough.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
/*
* BLINKERCOUGH: NSA Playset implant for IR bridging of airgap
*
* Copyright (C) 2015 Hacker, J.R. <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "irframe.h"
#include "codec.h"
#include "mac.h"
#include "i2c.h"
#include "util.h"
#include "address.h"
#include <EventManager.h>
#include <SoftwareSerial.h>
#include <stdint.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <Wire.h>
#define READ_CMD "READ "
#define WRITE_CMD "WRITE "
//SERIAL PROTOCOL
// gives register interface like i2cregs
//
// PC says: READ_REGISTER <register address>
// BC replies: <register value>
//
// PC says: WRITE_REGISTER <register address> <register value>
// BC replies <nothing>
//
// BC may give debug information with prefix
// DBG:
EventManager eventManager;
char commandbuf[21];
uint8_t pos = 0;
typedef void (*CB)(int eventCode, int eventParam);
#define IREVENT EventManager::kEventUser0
static void process_command(char i)
{
if ((i != '\n' && i != '\r') && pos < sizeof(commandbuf)-1) {
commandbuf[pos] = i;
pos++;
return;
}
// a whole command is in buffer
//clear out any remaining input
while(Serial.available()) {
(void)Serial.read();
}
//debug("cmd buf \"%s\"", commandbuf);
if (strncmp_P(commandbuf, PSTR(READ_CMD), sizeof(READ_CMD)-1) == 0) {
int address;
sscanf_P(commandbuf, PSTR(READ_CMD "%x"), &address);
auto data = i2cRegs.read(address);
printf("%02x\n", data);
} else if (strncmp_P(commandbuf, PSTR(WRITE_CMD), sizeof(WRITE_CMD)-1) == 0) {
int address;
int data;
sscanf_P(commandbuf, PSTR(WRITE_CMD "%x %x"), &address, &data);
i2cRegs.write(address, data);
} else {
commandbuf[sizeof(commandbuf)-1] = 0;
debug("invalid command: '%s'", commandbuf);
}
pos = 0;
memset((void*)commandbuf, 0, sizeof(commandbuf));
return;
}
static void serialCallback(__attribute__((unused)) int event, char c)
{
process_command(c);
}
GenericCallable<void(int,int)> serialCB((CB)serialCallback);
static void irSerialCallback(__attribute__((unused)) int event, char c)
{
mac.recv(c);
}
GenericCallable<void(int,int)> irSerialCB((CB)irSerialCallback);
static void FrameCallback(int event, IRFrame *frame)
{
if (event == BlinkerMac::ValidFrameRecievedEvent) {
debugstart("packet from: 0x%04x to: ", frame->source);
if (frame->destination == mac.get_address()) {
debugcont("ME");
} else {
debugcont("0x4x", frame->destination);
}
debugend();
//we don't call free here b/c
//i2cRegisters::recvHook does
//this is hack b/c event manager
//can't free resources
// see https://github.com/igormiktor/arduino-EventManager/issues/7
} else if (event == BlinkerMac::InvalidFrameRecievedEvent) {
debug("Invalid packet recieved!");
IRFrame::hexdump(frame);
FrameFactory.free(frame);
}
}
GenericCallable<void(int,int)> frameCB((CB)FrameCallback);
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(115200);
init_debug();
AddressStorage.begin();
if (!AddressStorage.present()) {
auto randaddr = AddressStorage.generate();
debug("storing 0x%04x into prom", randaddr);
AddressStorage.store(randaddr);
}
mac.set_address(AddressStorage.load());
debug("This station's address is 0x%04x", mac.get_address());
mac.set_baud_invert(IR_BAUD, false);
eventManager.addListener(EventManager::kEventSerial,
&serialCB);
eventManager.addListener(IREVENT,
&irSerialCB);
eventManager.addListener(BlinkerMac::ValidFrameRecievedEvent,
&frameCB);
eventManager.addListener(BlinkerMac::InvalidFrameRecievedEvent,
&frameCB);
FrameFactory.print_high_water_mark();
i2cRegs.begin();
mac.begin();
debug("Starting up...\n");
}
void loop()
{
static auto last = millis();
auto now = millis();
if (now - last > 30000) {
last = now;
FrameFactory.print_high_water_mark();
}
if (Serial.available()) {
char c = Serial.read();
eventManager.queueEvent(EventManager::kEventSerial, c);
}
//FIXME: encapsulate this in RX
// and abstract with method
if (mac.serial.available()) {
const uint8_t c = mac.serial.read();
eventManager.queueEvent(IREVENT, c);
}
eventManager.processEvent();
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* mode: c++
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/