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

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

* empty log message *

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