-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmac.h
129 lines (110 loc) · 3.13 KB
/
mac.h
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
/*
* 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
*/
#ifndef _MAC_H
#define _MAC_H
#include "irframe.h"
#include "codec.h"
#include <SoftwareSerial.h>
#include <stdint.h>
#include <EventManager.h>
extern EventManager eventManager;
#define IR_RX_PIN A3
#define IR_TX_PIN A2 /* this is actually not really used */
#define IR_BAUD 4800
/* notes about encoding:
*
* byte stuffing doesn't appear to really work
*
* 0x80 transmits fine
* 0x01 does not
*/
class BlinkerMac : public ByteStuffCodec, public Writer
{
public:
BlinkerMac();
void operator() (const uint8_t input)
{
serial.write(&input, 1);
}
void begin(); //to be called in setup()
//YOU MUST HANDLE FRAME RECIEVED EVENTS
//IF YOU DO NOT, MEMORY LEAKS
enum Events {
//for frame recv'd events, param is the frame pointer
//the handler is responsible for freeing with FrameFactory.free()
ValidFrameRecievedEvent = 400,
InvalidFrameRecievedEvent,
PacketNeedsRelayEvent,
};
SoftwareSerial serial;
private:
bool rx_blink;
uint16_t buf_pos;
bool packet_in_progress;
uint8_t prefix_state;
unsigned long last_rx_time;
IRFrame* buf;
uint16_t address;
uint16_t baud;
bool invert;
const uint8_t packet_prefix[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
const uint8_t preamble_byte = 0xA;
const uint8_t preamble_count = 8;
const uint8_t modulation_khz = 38;
void write_prefix();
bool prefix_hit(uint8_t c);
bool blink();
bool stalled();
public:
inline void set_address(uint16_t _address)
{
address = _address;
}
inline uint16_t get_address() const
{
return address;
}
inline void set_baud_invert(uint16_t _baud, bool _invert)
{
baud = _baud;
invert = invert;
}
void reset();
void send_frame(IRFrame &frame);
// fires recv'd events
void recv(const uint8_t input);
void relayCallback(int event, int param);
};
extern BlinkerMac mac;
extern MemberFunctionCallable<BlinkerMac> RXRelayCB;
#endif /* _RX_H */
/*
* 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:
*/