forked from cturvey/RandomNinjaChef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_ubx_file.c
150 lines (110 loc) · 3.44 KB
/
process_ubx_file.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
//****************************************************************************
// Quick UBX Packet File Processing Demo - [email protected]
//****************************************************************************
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//****************************************************************************
// Or from stdint.h in Linux
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
//****************************************************************************
#define UBX_BUFFER_SIZE 4096
//****************************************************************************
uint8_t ubx_buffer[UBX_BUFFER_SIZE];
int ubx_index = 0;
int ubx_fill = 0;
//****************************************************************************
int process_packet(uint8_t *packet, uint16_t length)
{
uint16_t i;
uint8_t a, b;
a = 0;
b = 0;
for(i=2; i<(length + 6); i++)
{
a += packet[i];
b += a;
}
if ((a != packet[i+0]) || // Checksum Fail
(b != packet[i+1]))
return(1);
// Print Whole Packets
for(i=0; i<(length + 8); i++)
fprintf(stdout, "%02X", packet[i]);
fprintf(stdout, "\n");
// Other user code to process and decode specific packets
return(length + 8); // Data size total
}
//****************************************************************************
void process_block(int length, uint8_t *data)
{
if ((ubx_fill + length) > UBX_BUFFER_SIZE)
{
fprintf(stderr, "UBX Buffer Overflowing");
exit(1);
}
memcpy(&ubx_buffer[ubx_fill], data, length);
ubx_fill += length;
// Find Sync
while(((ubx_fill - ubx_index) > 2) && (ubx_buffer[ubx_index+0] != 0xB5) && (ubx_buffer[ubx_index+1] != 0x62))
ubx_index++;
while((ubx_fill - ubx_index) >= 8) // Enough Data for UBX Packet Headers and Sums
{
length = (ubx_fill - ubx_index);
if ((ubx_buffer[ubx_index + 0] == 0xB5) &&
(ubx_buffer[ubx_index + 1] == 0x62))
{
uint16_t msg_length =
((uint16_t)ubx_buffer[ubx_index + 4] << 0) +
((uint16_t)ubx_buffer[ubx_index + 5] << 8);
if ((msg_length + 8) > UBX_BUFFER_SIZE) // Too big of a packet
{
ubx_index++;
}
else if ((msg_length + 8) > length) // Not Enough data yet
{
break;
}
else
ubx_index += process_packet(&ubx_buffer[ubx_index], msg_length);
}
else // No Sync Bytes, just consume
ubx_index++;
}
length = (ubx_fill - ubx_index);
if (length && ubx_index) // Move Down buffer
{
memcpy(ubx_buffer, &ubx_buffer[ubx_index], length);
ubx_index = 0;
ubx_fill = length;
}
}
//****************************************************************************
int main(int argc, char **argv)
{
FILE *f;
if (argc > 1)
{
f = fopen(argv[1], "rb");
if (f)
{
while(1)
{
uint8_t buffer[64];
int bytes_read;
bytes_read = fread(buffer, 1, sizeof(buffer), f);
if (bytes_read > 0) process_block(bytes_read, buffer);
else break; // Error or End-of-File
}
fclose(f);
}
else
fprintf(stderr, "Can't open file '%s'\n", argv[1]);
}
return(1);
}
//****************************************************************************