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

Last change on this file since 2515 was 1445, checked in by mervart, 16 years ago

* empty log message *

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}
55
56// Destructor
57////////////////////////////////////////////////////////////////////////////
58gpssDecoder::~gpssDecoder() {
59}
60
61//
62////////////////////////////////////////////////////////////////////////////
63t_irc gpssDecoder::Decode(char* data, int dataLen, vector<string>& errmsg) {
64
65 errmsg.clear();
66
67 _buffer += QByteArray(data, dataLen);
68
69 bool obsFound = false;
70 bool ephFound = false;
71 int iBeg;
72 while ( (iBeg = _buffer.indexOf(0x02)) != -1) {
73 _buffer = _buffer.mid(iBeg);
74
75 int recordSize;
76 int crc;
77
78 // Observations
79 // ------------
80 if (_buffer.length() > 0 && char(_buffer[1]) == 0x00) {
81
82 int reqLength = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER);
83
84 if (_buffer.length() >= reqLength) {
85 EPOCHHEADER epochHdr;
86 memcpy(&epochHdr, _buffer.data() + 2 + sizeof(recordSize),
87 sizeof(epochHdr));
88
89 reqLength += epochHdr.n_svs * sizeof(t_obsInternal) + sizeof(crc) + 1;
90
91 if (_buffer.length() >= reqLength) {
92
93 int checkLen = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER) +
94 epochHdr.n_svs * sizeof(t_obsInternal);
95 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
96 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
97
98 if (crc == crcCal) {
99 for (int is = 0; is < epochHdr.n_svs; is++) {
100 obsFound = true;
101 t_obs* obs = new t_obs();
102 memcpy(&(obs->_o), _buffer.data() + 2 + sizeof(recordSize) +
103 sizeof(epochHdr) + is * sizeof(t_obsInternal),
104 sizeof(t_obsInternal));
105 _obsList.push_back(obs);
106 }
107 }
108 }
109 }
110 _buffer = _buffer.mid(reqLength);
111 }
112
113 // Ephemeris
114 // ---------
115 else if (_buffer.length() > 0 && char(_buffer[1]) == 0x01) {
116 int reqLength = 2 + sizeof(recordSize) + sizeof(gpsephemeris) +
117 sizeof(crc) + 1;
118
119 if (_buffer.length() >= reqLength) {
120
121 int checkLen = 2 + sizeof(recordSize) + sizeof(gpsephemeris);
122 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
123 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
124
125 if (crc == crcCal) {
126 ephFound = true;
127 gpsephemeris* gpsEph = new gpsephemeris;
128 memcpy(gpsEph, _buffer.data() + 2 + sizeof(recordSize),
129 sizeof(gpsephemeris));
130 emit newGPSEph(gpsEph);
131 }
132 }
133 _buffer = _buffer.mid(reqLength);
134 }
135
136 else {
137 _buffer == _buffer.mid(1);
138 }
139 }
140
141 if (obsFound || ephFound) {
142 return success;
143 }
144 else {
145 return failure;
146 }
147}
Note: See TracBrowser for help on using the repository browser.