blob: 2bd818f0702600da7d3361a7c9b2b238c7c97e08 (
plain)
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
|
// (C) 2016 Joshua Allen, MIT/x11 License.
//
//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses.
/* ootx data decoder */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "ootx_decoder.h"
#include "crc32.h"
//char* fmt_str = "L Y HMD %d 5 1 206230 %d\n";
#define MAX_BUFF_SIZE 1024
uint8_t* buffer = NULL;
uint16_t buf_offset = 0;
uint8_t bits_written = 0;
uint16_t* payload_size = NULL;
void (*ootx_packet_clbk)(ootx_packet* packet) = NULL;
void ootx_init_buffer() {
buffer = (uint8_t*)malloc(MAX_BUFF_SIZE);
payload_size = (uint16_t*)buffer;
*payload_size = 0;
}
uint8_t ootx_decode_bit(uint32_t length) {
length = ((length/500)*500)+500;
length-=3000;
if (length>=2000) { length-=2000; }
if (length>=1000) { return 0x01; }
return 0x00;
}
uint8_t ootx_detect_preamble(uint8_t dbit) {
static uint32_t preamble = 0x00;
preamble <<= 1;
preamble |= dbit;
if ((preamble & 0x0001ffff) == 0x01) return 1;
return 0;
}
void ootx_reset_buffer() {
buf_offset = 0;
buffer[buf_offset] = 0;
bits_written = 0;
*payload_size = 0;
}
void ootx_inc_buffer_offset() {
++buf_offset;
// if (buf_offset>=MAX_BUFF_SIZE) buf_offset = 0;
assert(buf_offset<MAX_BUFF_SIZE);
buffer[buf_offset] = 0;
}
void ootx_write_to_buffer(uint8_t dbit) {
uint8_t *current_byte = buffer+buf_offset;
// printf("%d\n", dbit);
*current_byte >>= 1;
*current_byte |= (0x80 * dbit);
++bits_written;
if (bits_written>7) {
bits_written=0;
// printf("%d\n", *current_byte);
ootx_inc_buffer_offset();
}
}
void ootx_process_bit(uint32_t length) {
static uint8_t bits_processed = 0;
uint8_t dbit = ootx_decode_bit(length);
++bits_processed;
// printf("z %d %d\n", bits_processed,dbit);
// printf("d %d\n", bits_processed,dbit);
if ( ootx_detect_preamble(dbit) ) {
/* data stream can start over at any time so we must
always look for preamble bits */
// printf("Preamble found\n");
ootx_reset_buffer();
bits_processed = 0;
}
else if(bits_processed>16) {
//every 17th bit needs to be dropped
// printf("drop %d\n", dbit);
bits_processed = 0;
}
else
{
ootx_write_to_buffer(dbit);
if (buf_offset >= (*payload_size+6)) {
/* once we have a complete ootx packet, send it out in the callback */
ootx_packet op;
op.length = *(uint16_t*)buffer;
op.data = buffer+2;
op.crc32 = *(uint32_t*)(buffer+2+op.length);
uint32_t crc = crc32(0xffffffff,op.data,op.length);
if (crc != op.crc32) {
printf("CRC mismatch\n");
}
if ((crc == op.crc32) && ootx_packet_clbk) ootx_packet_clbk(&op);
ootx_reset_buffer();
}
}
}
|