aboutsummaryrefslogtreecommitdiff
path: root/tools/ootx_decode/ootx_decoder.c
blob: c244b805d5bc720969c6964a17362c2afc3acb77 (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
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
// (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 <zlib.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

void (*ootx_packet_clbk)(ootx_packet* packet) = NULL;
void ootx_pump_bit(ootx_decoder_context *ctx, uint8_t dbit);

void ootx_init_decoder_context(ootx_decoder_context *ctx) {
	ctx->buf_offset = 0;
	ctx->bits_written = 0;

	ctx->preamble = 0XFFFFFFFF;
	ctx->bits_processed = 0;
	ctx->found_preamble = 0;

	ctx->buffer = (uint8_t*)malloc(MAX_BUFF_SIZE);
	ctx->payload_size = (uint16_t*)ctx->buffer;
	*(ctx->payload_size) = 0;
}
/*
void ootx_init_buffer() {
	buffer = (uint8_t*)malloc(MAX_BUFF_SIZE);
	payload_size = (uint16_t*)buffer;
	*payload_size = 0;
}
*/

/*
	how to decode pulses
	ticks>2000 && delta>100000== master lighthouse
	ticks>2000 && delta>10000 == slave lighthouse
*/

int8_t ootx_decode_lighthouse_number(uint8_t last_num, uint32_t ticks, int32_t delta) {
	if (ticks<2000) return -1; //sweep
	if (delta>100000) return 0; //master
	if (delta>10000) return 1; //a slave
	return -1;
}

uint8_t decode_internal(uint32_t length) {
		uint16_t temp = length - 2880;
//		printf

#if BETTER_SAFE_THAN_FAST
	if (temp < 0 || length > 6525) {
		return -1;
	}
#endif

	if ((temp % 500) < 150) {
		return temp / 500;
	}

	return -1;

}

uint8_t ootx_decode_bit(uint32_t length) {
	length = ((length/500)*500)+500;

	length-=3000;
	if (length>=2000) { length-=2000; }
	if (length>=1000)  { return 0xFF; }

	return 0x00;
}
/*
uint8_t ootx_decode_bit(uint32_t ticks) {
	int8_t bits = decode_internal(ticks);
	return bits&0x02;
}
*/
/*
void ootx_accumulate_bit(ootx_decoder_context *ctx, uint32_t ticks) {
	uint8_t dbit = ootx_decode_bit(ticks);
//	printf("%d\n\n", dbit);
	ctx->bit_count[(dbit&0x01)]++;
//	printf("%d %d %d\n", dbit, ctx->bit_count[0], ctx->bit_count[1]);
}
*/
void ootx_accumulate_bit(ootx_decoder_context *ctx, uint8_t bit) {
	ctx->bit_count[bit&0x01]++;
}

uint8_t ootx_pump_greatest_bit(ootx_decoder_context *ctx) {
	//pump the bit
	uint8_t bit = 0x00;
	if (ctx->bit_count[0] < ctx->bit_count[1]) bit = 0xFF;

//	printf("pump %d\n", bit);
	ootx_pump_bit( ctx, bit );

	ctx->bit_count[0] = 0;
	ctx->bit_count[1] = 0;

	return bit;
}

uint8_t ootx_detect_preamble(ootx_decoder_context *ctx, uint8_t dbit) {
	ctx->preamble <<= 1;
	ctx->preamble |= (0x01 & dbit);
	if ((ctx->preamble & 0x0003ffff) == 0x00000001) return 1;
	return 0;
}

void ootx_reset_buffer(ootx_decoder_context *ctx) {
	ctx->buf_offset = 0;
	ctx->buffer[ctx->buf_offset] = 0;
	ctx->bits_written = 0;
	*(ctx->payload_size) = 0;
}

void ootx_inc_buffer_offset(ootx_decoder_context *ctx) {
	++(ctx->buf_offset);

//	assert(ctx->buf_offset<MAX_BUFF_SIZE);

	/* the buffer is going to overflow, wrap the buffer and don't write more data until the preamble is found again */
	if(ctx->buf_offset>=MAX_BUFF_SIZE) {
		ctx->buf_offset = 0;
		ctx->found_preamble = 0;
	}

	ctx->buffer[ctx->buf_offset] = 0;
}

void ootx_write_to_buffer(ootx_decoder_context *ctx, uint8_t dbit) {
	uint8_t *current_byte = ctx->buffer + ctx->buf_offset;
//	printf("%d\n", dbit);
//	*current_byte >>= 1;
//	*current_byte |= (0x80 & dbit);
	*current_byte <<= 1;
	*current_byte |= (0x01 & dbit);
	++(ctx->bits_written);
	if (ctx->bits_written>7) {
		ctx->bits_written=0;
//		printf("%d\n", *current_byte);
		ootx_inc_buffer_offset(ctx);
	}
}

void ootx_process_bit(ootx_decoder_context *ctx, uint32_t length) {
	int8_t dbit = ootx_decode_bit(length);

	ootx_pump_bit( ctx, dbit );
}

void print_crc32(uint32_t crc) {
//	uint8_t* p = (uint32_t*)&crc;
//	uint8_t i = 0;

	printf("%X\n", crc);
}

void write_to_file(uint8_t *d, uint16_t length){
	FILE *fp = fopen("binary.data","w");
	fwrite(d, length, 1, fp);
	fclose(fp);
}

void ootx_pump_bit(ootx_decoder_context *ctx, uint8_t dbit) {
//	uint8_t dbit = ootx_decode_bit(length);
	++(ctx->bits_processed);

//	printf("z %d %d\n", bits_processed,dbit);
//	printf("d %d\n", bits_processed,dbit);

	if ( ootx_detect_preamble(ctx, dbit) ) {
		/*	data stream can start over at any time so we must
			always look for preamble bits */
		printf("Preamble found\n");
		ootx_reset_buffer(ctx);
		ctx->bits_processed = 0;
		ctx->found_preamble = 1;
	}
	else if(ctx->bits_processed>16) {
		//every 17th bit needs to be dropped (sync bit)
//		printf("drop %d\n", dbit);
		ctx->bits_processed = 0;
	}
	else if (ctx->found_preamble > 0)
	{
		/*	only write to buffer if the preamble is found.
			if the buffer overflows, found_preamble will be cleared
			and writing will stop. data would be corrupted, so there is no point in continuing
		*/

		ootx_write_to_buffer(ctx, dbit);

		uint16_t padded_length = *(ctx->payload_size);
		padded_length += (padded_length&0x01); //extra null byte if odd

		if (ctx->buf_offset >= (padded_length+6)) {
			/*	once we have a complete ootx packet, send it out in the callback */
			ootx_packet op;

			op.length = *(ctx->payload_size);
			op.data = ctx->buffer+2;
			op.crc32 = *(uint32_t*)(op.data+padded_length);

			uint32_t crc = crc32( 0L, Z_NULL, 0 );
			crc = crc32( crc, op.data,op.length);
//			uint32_t crc = crc32(0xffffffff,op.data,op.length);


			if (crc != op.crc32) {
				printf("CRC mismatch\n");
/*
				printf("r:");
				print_crc32(op.crc32);

				printf("c:");
				print_crc32(crc);
//				write_to_file(op.data,op.length);
*/
			}

			if ((crc == op.crc32) && ootx_packet_clbk) ootx_packet_clbk(&op);

			ootx_reset_buffer(ctx);
		}
	}
}

uint8_t* get_ptr(uint8_t* data, uint8_t bytes, uint16_t* idx) {
	uint8_t* x = data + *idx;
	*idx += bytes;
	return x;
}

float _to_float(uint8_t* data) {
	uint16_t h = *(uint16_t*)(data);
	uint16_t h_exp, h_sig;
	uint32_t f_sgn, f_exp, f_sig;

	h_exp = (h&0x7c00u);
	f_sgn = ((uint32_t)h&0x8000u) << 16;
	if (h_exp == 0x0000u) { /* 0 or subnormal */
		h_sig = (h&0x03ffu);
		/* Signed zero */
		if (h_sig == 0) {
			return f_sgn;
		}
		/* Subnormal */
		h_sig <<= 1;
		while ((h_sig&0x0400u) == 0) {
			h_sig <<= 1;
			h_exp++;
		}
		f_exp = ((uint32_t)(127 - 15 - h_exp)) << 23;
		f_sig = ((uint32_t)(h_sig&0x03ffu)) << 13;
			return f_sgn + f_exp + f_sig;
	}
	else if (h_exp == 0x7c00u) { /* inf or NaN */
		/* All-ones exponent and a copy of the significand */
		return f_sgn + 0x7f800000u + (((uint32_t)(h&0x03ffu)) << 13);
	}
	else { /* normalized */
		/* Just need to adjust the exponent and shift */
		return f_sgn + (((uint32_t)(h&0x7fffu) + 0x1c000u) << 13);
	}
}

void init_lighthouse_info_v6(lighthouse_info_v6* lhi, uint8_t* data) {
	uint16_t idx = 0;
	/*
	uint16_t fw_version;//Firmware version (bit 15..6), protocol version (bit 5..0)
	uint32_t id; //Unique identifier of the base station
	float fcal_0_phase; //"phase" for rotor 0
	float fcal_1_phase; //"phase" for rotor 1
	float fcal_0_tilt; //"tilt" for rotor 0
	float fcal_1_tilt; //"tilt" for rotor 1
	uint8_t sys_unlock_count; //Lowest 8 bits of the rotor desynchronization counter
	uint8_t hw_version; //Hardware version
	float fcal_0_curve; //"curve" for rotor 0
	float fcal_1_curve; //"curve" for rotor 1
	int8_t accel_dir_x; //"orientation vector"
	int8_t accel_dir_y; //"orientation vector"
	int8_t accel_dir_z; //"orientation vector"
	float fcal_0_gibphase; //"gibbous phase" for rotor 0 (normalized angle)
	float fcal_1_gibphase; //"gibbous phase" for rotor 1 (normalized angle)
	float fcal_0_gibmag; //"gibbous magnitude" for rotor 0
	float fcal_1_gibmag; //"gibbous magnitude" for rotor 1
	uint8_t mode_current; //Currently selected mode (default: 0=A, 1=B, 2=C)
	uint8_t sys_faults; //"fault detect flags" (should be 0)
	*/

	lhi->fw_version = *(uint16_t*)get_ptr(data,2,&idx);
	lhi->id = *(uint32_t*)get_ptr(data,4,&idx);
	lhi->fcal_0_phase = _to_float( get_ptr(data,2,&idx) );
	lhi->fcal_1_phase = _to_float( get_ptr(data,2,&idx) );
	lhi->fcal_0_tilt = _to_float( get_ptr(data,2,&idx) );
	lhi->fcal_1_tilt = _to_float( get_ptr(data,2,&idx) );
	lhi->sys_unlock_count = *get_ptr(data,1,&idx);
	lhi->hw_version = *get_ptr(data,1,&idx);
	lhi->fcal_0_curve = _to_float( get_ptr(data,2,&idx) );
	lhi->fcal_1_curve = _to_float( get_ptr(data,2,&idx) );
	lhi->accel_dir_x = *(int8_t*)get_ptr(data,1,&idx);
	lhi->accel_dir_y = *(int8_t*)get_ptr(data,1,&idx);
	lhi->accel_dir_z = *(int8_t*)get_ptr(data,1,&idx);
	lhi->fcal_0_gibphase = _to_float( get_ptr(data,2,&idx) );
	lhi->fcal_1_gibphase = _to_float( get_ptr(data,2,&idx) );
	lhi->fcal_0_gibmag = _to_float( get_ptr(data,2,&idx) );
	lhi->fcal_1_gibmag = _to_float( get_ptr(data,2,&idx) );
	lhi->mode_current = *get_ptr(data,1,&idx);
	lhi->sys_faults = *get_ptr(data,1,&idx);

}

void print_lighthouse_info_v6(lighthouse_info_v6* lhi) {

	printf("\t%X\n\t%X\n\t%f\n\t%f\n\t%f\n\t%f\n\t%d\n\t%d\n\t%f\n\t%f\n\t%d\n\t%d\n\t%d\n\t%f\n\t%f\n\t%f\n\t%f\n\t%d\n\t%d\n",
		lhi->fw_version,
		lhi->id,
		lhi->fcal_0_phase,
		lhi->fcal_1_phase,
		lhi->fcal_0_tilt,
		lhi->fcal_1_tilt,
		lhi->sys_unlock_count,
		lhi->hw_version,
		lhi->fcal_0_curve,
		lhi->fcal_1_curve,
		lhi->accel_dir_x,
		lhi->accel_dir_y,
		lhi->accel_dir_z,
		lhi->fcal_0_gibphase,
		lhi->fcal_1_gibphase,
		lhi->fcal_0_gibmag,
		lhi->fcal_1_gibmag,
		lhi->mode_current,
		lhi->sys_faults);
}