source: ntrip/trunk/BNC/GPSS/gpssDecoder.cpp@ 1438

Last change on this file since 1438 was 1438, checked in by mervart, 15 years ago

* empty log message *

File size: 4.2 KB
Line 
1\
2/* -------------------------------------------------------------------------
3 * BKG NTRIP Client
4 * -------------------------------------------------------------------------
5 *
6 * Class: gpssDecoder
7 *
8 * Purpose: Decode Data in GPSS Format
9 *
10 * Author: L. Mervart
11 *
12 * Created: 20-Dec-2008
13 *
14 * Changes:
15 *
16 * -----------------------------------------------------------------------*/
17
18#include "gpssDecoder.h"
19#include "bncapp.h"
20
21#define MODE_SEARCH 0
22#define MODE_TYPE 1
23#define MODE_EPOCH 2
24#define MODE_EPOCH_BODY 3
25#define MODE_EPOCH_CRC 4
26#define MODE_EPOCH_ETX 5
27#define MODE_EPH 6
28#define MODE_EPH_BODY 7
29#define MODE_EPH_CRC 8
30#define MODE_EPH_ETX 9
31
32using namespace std;
33
34typedef struct epochHeader {
35 double t_epoch;
36 int n_svs;
37} EPOCHHEADER;
38
39// Cyclic Redundancy Check
40////////////////////////////////////////////////////////////////////////////
41unsigned short cal_crc(unsigned char *buf, int num) {
42 unsigned short polynomial = 0x8408;
43 unsigned short crc = 0;
44 int i;
45 while ( num-- ) {
46 crc = ( crc & 0xFF00 ) | ( *buf++^( crc & 0x00FF ) );
47 for( i=0; i<8; i++ ){
48 if( crc & 0x0001 ){
49 crc >>= 1;
50 crc ^= polynomial;
51 }
52 else{
53 crc >>= 1;
54 }
55 }
56 }
57 return (crc);
58}
59
60// Constructor
61////////////////////////////////////////////////////////////////////////////
62gpssDecoder::gpssDecoder() : GPSDecoder() {
63 _mode = MODE_SEARCH;
64
65 connect(this, SIGNAL(newGPSEph(gpsephemeris*)),
66 (bncApp*) qApp, SLOT(slotNewGPSEph(gpsephemeris*)));
67}
68
69// Destructor
70////////////////////////////////////////////////////////////////////////////
71gpssDecoder::~gpssDecoder() {
72}
73
74//
75////////////////////////////////////////////////////////////////////////////
76t_irc gpssDecoder::Decode(char* data, int dataLen, vector<string>& errmsg) {
77
78 errmsg.clear();
79
80 _buffer += QByteArray(data, dataLen);
81
82 bool obsFound = false;
83 bool ephFound = false;
84 int iBeg;
85 while ( (iBeg = _buffer.indexOf(0x02)) != -1) {
86 _buffer = _buffer.mid(iBeg);
87
88 int recordSize;
89 int crc;
90
91 // Observations
92 // ------------
93 if (_buffer.length() > 0 && char(_buffer[1]) == 0x00) {
94
95 int reqLength = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER);
96
97 if (_buffer.length() >= reqLength) {
98 EPOCHHEADER epochHdr;
99 memcpy(&epochHdr, _buffer.data() + 2 + sizeof(recordSize),
100 sizeof(epochHdr));
101
102 reqLength += epochHdr.n_svs * sizeof(t_obsInternal) + sizeof(crc) + 1;
103
104 if (_buffer.length() >= reqLength) {
105
106 int checkLen = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER) +
107 epochHdr.n_svs * sizeof(t_obsInternal);
108 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
109 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
110
111 if (crc == crcCal) {
112 for (int is = 0; is < epochHdr.n_svs; is++) {
113 obsFound = true;
114 t_obs* obs = new t_obs();
115 memcpy(&(obs->_o), _buffer.data() + 2 + sizeof(recordSize) +
116 sizeof(epochHdr) + is * sizeof(t_obsInternal),
117 sizeof(t_obsInternal));
118 _obsList.push_back(obs);
119 }
120 }
121 }
122 }
123 _buffer = _buffer.mid(reqLength);
124 }
125
126 // Ephemeris
127 // ---------
128 else if (_buffer.length() > 0 && char(_buffer[1]) == 0x01) {
129 int reqLength = 2 + sizeof(recordSize) + sizeof(gpsephemeris) +
130 sizeof(crc) + 1;
131
132 if (_buffer.length() >= reqLength) {
133
134 int checkLen = 2 + sizeof(recordSize) + sizeof(gpsephemeris);
135 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
136 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
137
138 if (crc == crcCal) {
139 ephFound = true;
140 gpsephemeris* gpsEph = new gpsephemeris;
141 memcpy(gpsEph, _buffer.data() + 2 + sizeof(recordSize),
142 sizeof(gpsephemeris));
143 emit newGPSEph(gpsEph);
144 }
145 }
146 _buffer = _buffer.mid(reqLength);
147 }
148
149 else {
150 _buffer == _buffer.mid(1);
151 }
152 }
153
154 if (obsFound || ephFound) {
155 return success;
156 }
157 else {
158 return failure;
159 }
160}
Note: See TracBrowser for help on using the repository browser.