spg_log_parser.c 3.62 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	spg log parser
	
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2009

	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 <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include "block_setpoint_generator.h"

#define BUFSIZE	4096

typedef union {
	uint32_t u;
	float f;
} float32_t;

int main (int argc, char **argv)
{
	unsigned char buffer[BUFSIZE];
	int r;
	size_t len = 0;
	bool verbose = 0;
	bool verbcmd = 0;
	bool summary = 0;
	uint32_t start_t = 0;
	uint32_t end_t = 0;
	uint32_t start_v = 0;
	uint32_t end_v = -1;
	int commands = 0;
	
	if (argc < 2) {
		printf("Usage:\n");
		printf("\n");
		printf("%s v\teverything\n", argv[0]);
		printf("%s s\tsummary\n", argv[0]);
		printf("%s t start end\teverything from start till end\n", argv[0]);
		printf("\n");
		return 1;
	}
	
	if (argc == 2 && !strcmp(argv[1], "v"))
		verbcmd = 1;
	if (argc == 2 && !strcmp(argv[1], "s"))
		summary = 1;
	if (argc == 4 && !strcmp(argv[1], "t")) {
		start_v = atoi(argv[2]);
		end_v = atoi(argv[3]);
		verbcmd = 1;
	}
	
	do {
		r = read(0, buffer + len, BUFSIZE - len);
		if (r > 0) {
			int i;
			len += r;
			for (i = 0; i < len; i++) {
				if (buffer[i] == 0xff && i > 16) {
					if (i >= 16) {
						uint32_t __attribute__ ((aligned (1))) *n;
						uint32_t t;
						uint32_t cmd;
						float32_t pos;
						float32_t spd;
						uint32_t st;
						int len;
						
						commands++;
						n = (uint32_t*)buffer;
						t = ntohl(n[0]);
						cmd = ntohl(n[1]);
						pos.u = ntohl(n[2]);
						spd.u = ntohl(n[2]);
						st = ntohl(n[3]);
						if (t >= start_v && t <= end_v)
							verbose = verbcmd;
						else
							verbose = 0;
						if (verbose)
							printf("t: %" PRIu32 "\t", t);
						end_t = t;
						if (start_t == 0)
							start_t = t;
						switch (cmd) {
							case BLOCK_SPG_SETPOINT:
								if (verbose) printf("SETPOINT %f\t",
								    pos.f);
								len = 8;
								break;
							case BLOCK_SPG_SPEED:
								if (verbose) printf("SPEED %f\t",
								    spd.f);
								len = 8;
								break;
							case BLOCK_SPG_SPEED_DIRECT:
								if (verbose) printf("SPEED %f\t",
								    spd.f);
								len = 8;
								break;
							case BLOCK_SPG_SETPOINT_TIME:
								if (verbose) {
									 printf("SETPOINT_TIME %f\t",
									    pos.f);
									printf("%" PRIu32 "\t",
									    st);
								}
								len = 12;
								break;
							default:
								if (verbose) printf("UNKNOWN %d\t",
								    cmd);
								len = 4;
						}
						if (buffer[i -1] == 0) {
							buffer[i] = 0;
							if (verbose) printf("%s\t%s",
							    buffer + len + 4,
							    buffer + len + 4 + strlen((char*)buffer+len+4) + 1);
						}
						if (verbose)
							printf("\n");
					}
					memmove(buffer, buffer + i + 1, len - i -1);
					len -= i + 1;
					i = -1;
				}
			}
		}
	} while (r > 0);

	if (summary) {
		printf("Start: %"PRIu32"\n", start_t);
		printf("End: %"PRIu32"\n", end_t);
		printf("Nr. commands: %d\n", commands);
	}

	return 0;
}