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

Last change on this file since 2561 was 2545, checked in by mervart, 14 years ago
File size: 3.9 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
21using namespace std;
22
23typedef struct epochHeader {
24 double t_epoch;
25 int n_svs;
26} EPOCHHEADER;
27
28// Cyclic Redundancy Check
29////////////////////////////////////////////////////////////////////////////
30unsigned short cal_crc(unsigned char *buf, int num) {
31 unsigned short polynomial = 0x8408;
32 unsigned short crc = 0;
33 int i;
34 while ( num-- ) {
35 crc = ( crc & 0xFF00 ) | ( *buf++^( crc & 0x00FF ) );
36 for( i=0; i<8; i++ ){
37 if( crc & 0x0001 ){
38 crc >>= 1;
39 crc ^= polynomial;
40 }
41 else{
42 crc >>= 1;
43 }
44 }
45 }
46 return (crc);
47}
48
49// Constructor
50////////////////////////////////////////////////////////////////////////////
51gpssDecoder::gpssDecoder() : GPSDecoder() {
52 connect(this, SIGNAL(newGPSEph(gpsephemeris*)),
53 (bncApp*) qApp, SLOT(slotNewGPSEph(gpsephemeris*)),
54 Qt::DirectConnection);
55}
56
57// Destructor
58////////////////////////////////////////////////////////////////////////////
59gpssDecoder::~gpssDecoder() {
60}
61
62//
63////////////////////////////////////////////////////////////////////////////
64t_irc gpssDecoder::Decode(char* data, int dataLen, vector<string>& errmsg) {
65
66 errmsg.clear();
67
68 _buffer += QByteArray(data, dataLen);
69
70 bool obsFound = false;
71 bool ephFound = false;
72 int iBeg;
73 while ( (iBeg = _buffer.indexOf(0x02)) != -1) {
74 _buffer = _buffer.mid(iBeg);
75
76 int recordSize;
77 int crc;
78
79 // Observations
80 // ------------
81 if (_buffer.length() > 0 && char(_buffer[1]) == 0x00) {
82
83 int reqLength = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER);
84
85 if (_buffer.length() >= reqLength) {
86 EPOCHHEADER epochHdr;
87 memcpy(&epochHdr, _buffer.data() + 2 + sizeof(recordSize),
88 sizeof(epochHdr));
89
90 reqLength += epochHdr.n_svs * sizeof(t_obsInternal) + sizeof(crc) + 1;
91
92 if (_buffer.length() >= reqLength) {
93
94 int checkLen = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER) +
95 epochHdr.n_svs * sizeof(t_obsInternal);
96 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
97 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
98
99 if (crc == crcCal) {
100 for (int is = 0; is < epochHdr.n_svs; is++) {
101 obsFound = true;
102 t_obs* obs = new t_obs();
103 memcpy(&(obs->_o), _buffer.data() + 2 + sizeof(recordSize) +
104 sizeof(epochHdr) + is * sizeof(t_obsInternal),
105 sizeof(t_obsInternal));
106 _obsList.push_back(obs);
107 }
108 }
109 }
110 }
111 _buffer = _buffer.mid(reqLength);
112 }
113
114 // Ephemeris
115 // ---------
116 else if (_buffer.length() > 0 && char(_buffer[1]) == 0x01) {
117 int reqLength = 2 + sizeof(recordSize) + sizeof(gpsephemeris) +
118 sizeof(crc) + 1;
119
120 if (_buffer.length() >= reqLength) {
121
122 int checkLen = 2 + sizeof(recordSize) + sizeof(gpsephemeris);
123 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
124 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
125
126 if (crc == crcCal) {
127 ephFound = true;
128 gpsephemeris* gpsEph = new gpsephemeris;
129 memcpy(gpsEph, _buffer.data() + 2 + sizeof(recordSize),
130 sizeof(gpsephemeris));
131 emit newGPSEph(gpsEph);
132 }
133 }
134 _buffer = _buffer.mid(reqLength);
135 }
136
137 else {
138 _buffer == _buffer.mid(1);
139 }
140 }
141
142 if (obsFound || ephFound) {
143 return success;
144 }
145 else {
146 return failure;
147 }
148}
Note: See TracBrowser for help on using the repository browser.