-
Notifications
You must be signed in to change notification settings - Fork 10
/
acc_id_sniff.c
134 lines (107 loc) · 2.44 KB
/
acc_id_sniff.c
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
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL // 16 MHz
#include <util/delay.h>
#define USART_BUFFER_SIZE 2048
unsigned char usart_buffer[USART_BUFFER_SIZE];
unsigned char sniffed_byte = 0;
unsigned int sniffed_bit_index = 0;
unsigned int usart_pointer = 0;
unsigned int usart_end = 0;
unsigned char pind_value = 0x08;
volatile unsigned char sending_usart = 0;
unsigned char hex_symbols[16] = { '0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'
};
void init_usart()
{
UCSR0C = 0x06; // 8 bit, no parity, 1 stop bit
UBRR0H = 0;
UBRR0L = 103;
UCSR0B = 0x08; // enable transmission
}
inline void send_byte_to_usart(unsigned char byte)
{
usart_buffer[usart_end] = byte;
usart_end = (usart_end + 1) % USART_BUFFER_SIZE;
}
inline void send_byte_in_hex_format(unsigned char byte)
{
send_byte_to_usart(hex_symbols[(byte>>4)&0x0F]);
send_byte_to_usart(hex_symbols[byte & 0x0F]);
send_byte_to_usart('\n');
}
ISR(USART0_UDRE_vect) // transmit buffer is empty, send next byte
{
if( usart_pointer != usart_end )
{
UDR0 = usart_buffer[usart_pointer];
usart_pointer = (usart_pointer + 1) % USART_BUFFER_SIZE;
}
else
{
// nothing to send, disable interrupts
UCSR0B &= 0xCF;
sending_usart = 0;
}
}
int main()
{
cli();
init_usart();
DDRD &= 0xF7; // set PD3 as input
PORTD &= 0xF7; // remove pull-up resistor to not alterate the bus
TCNT0 = 0;
TIMSK0 = 0;
TCCR0B = 0x00;
sei();
unsigned char new_pind_value = 0x08;
TCCR1B = 0x02;
while(1)
{
TCNT1 = 0;
while(1)
{
//detect falling edge
new_pind_value = (PIND & 0x08);
if( pind_value != 0x00 && new_pind_value == 0x00 )
{
// wait 2 us
_delay_us(2);
if( TCNT1 > 24 )
{
sniffed_bit_index = 0;
sniffed_byte = 0;
}
if( PIND & 0x08 ) // line is high
{
sniffed_byte |= (1 << sniffed_bit_index);
}
else
{
sniffed_byte &= ~(1 << sniffed_bit_index);
}
sniffed_bit_index++;
if( sniffed_bit_index >= 8 )
{
send_byte_in_hex_format(sniffed_byte);
sniffed_byte = 0;
sniffed_bit_index = 0;
}
TCNT1 = 0;
}
if( TCNT1 > 65000 )
break; // send bytes to uart when communication is over
pind_value = new_pind_value;
}
if( usart_pointer != usart_end )
{
sending_usart = 1;
UCSR0B |= 0x20; // enable interrupts
while(sending_usart) ;
}
}
return 0;
}