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

Last change on this file since 2545 was 2545, checked in by mervart, 14 years ago
File size: 3.9 KB
RevLine 
[1429]1\
[1310]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"
[1314]19#include "bncapp.h"
[1310]20
21using namespace std;
22
[1311]23typedef struct epochHeader {
24 double t_epoch;
25 int n_svs;
26} EPOCHHEADER;
27
[1434]28// Cyclic Redundancy Check
29////////////////////////////////////////////////////////////////////////////
[1435]30unsigned short cal_crc(unsigned char *buf, int num) {
[1434]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
[1310]49// Constructor
50////////////////////////////////////////////////////////////////////////////
51gpssDecoder::gpssDecoder() : GPSDecoder() {
[1314]52 connect(this, SIGNAL(newGPSEph(gpsephemeris*)),
[2545]53 (bncApp*) qApp, SLOT(slotNewGPSEph(gpsephemeris*)),
54 Qt::DirectConnection);
[1310]55}
56
57// Destructor
58////////////////////////////////////////////////////////////////////////////
59gpssDecoder::~gpssDecoder() {
60}
61
62//
63////////////////////////////////////////////////////////////////////////////
[1311]64t_irc gpssDecoder::Decode(char* data, int dataLen, vector<string>& errmsg) {
65
[1310]66 errmsg.clear();
67
[1429]68 _buffer += QByteArray(data, dataLen);
[1311]69
[1438]70 bool obsFound = false;
71 bool ephFound = false;
[1429]72 int iBeg;
73 while ( (iBeg = _buffer.indexOf(0x02)) != -1) {
74 _buffer = _buffer.mid(iBeg);
[1311]75
[1429]76 int recordSize;
[1430]77 int crc;
[1429]78
79 // Observations
80 // ------------
[1438]81 if (_buffer.length() > 0 && char(_buffer[1]) == 0x00) {
[1431]82
83 int reqLength = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER);
84
85 if (_buffer.length() >= reqLength) {
86 EPOCHHEADER epochHdr;
[1429]87 memcpy(&epochHdr, _buffer.data() + 2 + sizeof(recordSize),
88 sizeof(epochHdr));
[1431]89
90 reqLength += epochHdr.n_svs * sizeof(t_obsInternal) + sizeof(crc) + 1;
[1311]91
[1430]92 if (_buffer.length() >= reqLength) {
[1434]93
94 int checkLen = 2 + sizeof(recordSize) + sizeof(EPOCHHEADER) +
95 epochHdr.n_svs * sizeof(t_obsInternal);
96 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
[1436]97 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
[1434]98
[1436]99 if (crc == crcCal) {
100 for (int is = 0; is < epochHdr.n_svs; is++) {
[1438]101 obsFound = true;
[1436]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 }
[1429]108 }
109 }
[1425]110 }
[1433]111 _buffer = _buffer.mid(reqLength);
[1425]112 }
[1311]113
[1429]114 // Ephemeris
115 // ---------
[1438]116 else if (_buffer.length() > 0 && char(_buffer[1]) == 0x01) {
[1430]117 int reqLength = 2 + sizeof(recordSize) + sizeof(gpsephemeris) +
[1438]118 sizeof(crc) + 1;
[1431]119
[1434]120 if (_buffer.length() >= reqLength) {
[1431]121
[1434]122 int checkLen = 2 + sizeof(recordSize) + sizeof(gpsephemeris);
123 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
[1436]124 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
[1434]125
[1436]126 if (crc == crcCal) {
[1438]127 ephFound = true;
[1436]128 gpsephemeris* gpsEph = new gpsephemeris;
129 memcpy(gpsEph, _buffer.data() + 2 + sizeof(recordSize),
130 sizeof(gpsephemeris));
131 emit newGPSEph(gpsEph);
132 }
[1426]133 }
[1433]134 _buffer = _buffer.mid(reqLength);
[1425]135 }
[1311]136
[1425]137 else {
[1429]138 _buffer == _buffer.mid(1);
[1311]139 }
[1312]140 }
[1311]141
[1438]142 if (obsFound || ephFound) {
143 return success;
144 }
145 else {
146 return failure;
147 }
[1310]148}
Note: See TracBrowser for help on using the repository browser.