-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlm_reader.c
297 lines (235 loc) · 8.66 KB
/
tlm_reader.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
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
/*
TLM reader library
Copyright (C) 2015 Hannes Diethelm
This file is part of tlm_reader
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include "tlm_reader.h"
#define DEBUG
void _error_handler(ERROR_TYPE err, int line, const char *file, char* format, ...){
va_list argList;
if(err == E_ERROR){
fprintf(ERROR_FP, "Error: %s:%i: ", file, line);
}else if(err == E_WARNING){
fprintf(ERROR_FP, "Warning: %s:%i: ", file, line);
}else{
fprintf(ERROR_FP, "Unknown error code: %s:%i: ", file, line);
}
va_start(argList, format);
vfprintf(ERROR_FP, format, argList);
va_end(argList);
if(err == E_ERROR){
exit(EXIT_FAILURE);
}
}
GENERIC_BLOCK *tlm_reader_read(void * log_data, size_t log_size){
GENERIC_BLOCK *first_block, *work_block;
DATA_BLOCK *data;
void *data_ptr;
bool main_header;
first_block = malloc(sizeof(GENERIC_BLOCK));
first_block->prev=NULL;
first_block->next=NULL;
data_ptr = log_data;
work_block = first_block;
main_header = true;
while(data_ptr - log_data < log_size){
data = data_ptr;
if(data->common.timestamp == TIMESTAMP_HEADER){
memcpy(&work_block->data, data, SIZE_HEADER);
work_block->data_size = SIZE_HEADER;
if(main_header){
work_block->type = HEADER_MAIN;
main_header = false;
}else{
work_block->type = HEADER_AUX;
if(work_block->data.common.type[0] != work_block->data.common.type[1]){
error_handler(E_WARNING, "Expecting type[0] == type[1]!\n");
}
/*Seams that this is the identification of the last aux header block */
if(work_block->data.common.type[0] == ENA_LAST){
main_header = true;
}
}
tlm_reader_decode_header(work_block);
}else{
memcpy(&work_block->data, data, SIZE_DATA);
work_block->data_size=SIZE_DATA;
work_block->type=work_block->data.common.type[0];
tlm_reader_decode_data(work_block);
}
data_ptr=data_ptr + work_block->data_size;
work_block->next = malloc(sizeof(GENERIC_BLOCK));
work_block->next->prev = work_block;
work_block->next = work_block;
}
return first_block;
}
void tlm_reader_decode_header(GENERIC_BLOCK * block){
#ifdef DEBUG
/*tlm_print_raw(block);*/
#endif
switch(block->type) {
case HEADER_MAIN:
#ifdef DEBUG
printf("Header Main: model_number=0x%02X model_type=0x%02X model_name=%s\n",
block->data.header_main.model_number, block->data.header_main.model_type, block->data.header_main.model_name);
#endif
break;
case HEADER_AUX:
#ifdef DEBUG
printf("Header Aux: ena_info=0x%02X 0x%02X\n",
block->data.header_aux.ena_info[0], block->data.header_aux.ena_info[1]);
#endif
break;
default:
error_handler(E_WARNING, "block->type = 0x%02X unknown!\n", block->type);
break;
}
}
void tlm_reader_decode_data(GENERIC_BLOCK * block){
#ifdef DEBUG
/*tlm_print_raw(block);*/
printf("Data: timestamp=%06i type=0x%02X:", block->data.common.timestamp, block->type);
#endif
switch(block->type) {
case CURRENT:
#ifdef DEBUG
printf("CURRENT");
#endif
block->decoded.current.current = (float)swap_int16(block->data.current.current) * DECODE_CURRENT_FACTOR;
#ifdef DEBUG
printf(" current=0x%04X=%.1fA", 0xFFFF & swap_int16(block->data.current.current), block->decoded.current.current);
#endif
break;
case POWERBOX:
#ifdef DEBUG
printf("POWERBOX");
#endif
break;
case AIRSPEED:
#ifdef DEBUG
printf("AIRSPEED");
#endif
break;
case ATLITUDE:
#ifdef DEBUG
printf("ATLITUDE");
#endif
break;
case ACCELLERATION:
#ifdef DEBUG
printf("ACCELLERATION");
#endif
break;
case JET_CAT:
#ifdef DEBUG
printf("JET_CAT");
#endif
break;
case GPS1:
#ifdef DEBUG
printf("GPS1");
#endif
break;
case GPS2:
#ifdef DEBUG
printf("GPS2");
#endif
break;
case VARIO:
#ifdef DEBUG
printf("VARIO");
#endif
break;
case RPM_TEMP_VOLT_TM1000:
case RPM_TEMP_VOLT_TM1100:
#ifdef DEBUG
printf("RPM_TEMP_VOLT");
#endif
block->decoded.rpm_volt_temp.rpm = (float)swap_uint16(block->data.rpm_volt_temp.rpm);
block->decoded.rpm_volt_temp.volt = (float)swap_uint16(block->data.rpm_volt_temp.volt) / 100.0;
block->decoded.rpm_volt_temp.temp = (float)swap_uint16(block->data.rpm_volt_temp.temp);
/* Farenheit to Celsius */
block->decoded.rpm_volt_temp.temp = ( block->decoded.rpm_volt_temp.temp - 32.0 ) *(5.0/9.0);
#ifdef DEBUG
printf(" rpm=0x%04X=%i", 0xFFFF & swap_uint16(block->data.rpm_volt_temp.rpm), block->decoded.rpm_volt_temp.rpm);
printf(" volt=0x%04X=%.1fV", 0xFFFF & swap_uint16(block->data.rpm_volt_temp.volt), block->decoded.rpm_volt_temp.volt);
printf(" temp=0x%04X=%.1fC", 0xFFFF & swap_uint16(block->data.rpm_volt_temp.temp), block->decoded.rpm_volt_temp.temp);
#endif
break;
case RX_STAT_TM1000:
case RX_STAT_TM1100:
#ifdef DEBUG
printf("RX_STAT");
#endif
block->decoded.rx_stat.a_fade = swap_uint16(block->data.rx_stat.a_fade);
block->decoded.rx_stat.b_fade = swap_uint16(block->data.rx_stat.b_fade);
block->decoded.rx_stat.l_fade = swap_uint16(block->data.rx_stat.l_fade);
block->decoded.rx_stat.r_fade = swap_uint16(block->data.rx_stat.r_fade);
block->decoded.rx_stat.frame_loss = swap_uint16(block->data.rx_stat.frame_loss);
block->decoded.rx_stat.hold = swap_uint16(block->data.rx_stat.hold);
block->decoded.rx_stat.rx_volt = (float)swap_uint16(block->data.rx_stat.rx_volt) / 100.0;
#ifdef DEBUG
printf(" a_fade=0x%04X=%i", 0xFFFF & swap_uint16(block->data.rx_stat.a_fade), block->decoded.rx_stat.a_fade);
printf(" b_fade=0x%04X=%i", 0xFFFF & swap_uint16(block->data.rx_stat.b_fade), block->decoded.rx_stat.b_fade);
printf(" l_fade=0x%04X=%i", 0xFFFF & swap_uint16(block->data.rx_stat.l_fade), block->decoded.rx_stat.l_fade);
printf(" r_fade=0x%04X=%i", 0xFFFF & swap_uint16(block->data.rx_stat.r_fade), block->decoded.rx_stat.r_fade);
printf(" frame_loss=0x%04X=%i", 0xFFFF & swap_uint16(block->data.rx_stat.frame_loss), block->decoded.rx_stat.frame_loss);
printf(" hold=0x%04X=%i", 0xFFFF & swap_uint16(block->data.rx_stat.hold), block->decoded.rx_stat.hold);
printf(" rx_volt=0x%04X=%.1fV", 0xFFFF & swap_uint16(block->data.rx_stat.rx_volt), block->decoded.rx_stat.rx_volt);
#endif
break;
default:
error_handler(E_WARNING, "block->type = 0x%02X unknown!\n", block->type);
break;
}
#ifdef DEBUG
printf("\n");
#endif
}
void tlm_print_raw(GENERIC_BLOCK * block){
uint8_t *ptr;
size_t i;
ptr=(uint8_t *)&block->data;
for(i=0 ; i < block->data_size ; i++){
printf("0x%02X ", ptr[i]);
}
printf("\n");
}
/* Byte swap unsigned short */
uint16_t swap_uint16( uint16_t val )
{
return (val << 8) | (val >> 8 );
}
/* Byte swap short */
int16_t swap_int16( int16_t val )
{
return (val << 8) | ((val >> 8) & 0xFF);
}
/* Byte swap unsigned int */
uint32_t swap_uint32( uint32_t val )
{
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
/* Byte swap int */
int32_t swap_int32( int32_t val )
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
return (val << 16) | ((val >> 16) & 0xFFFF);
}