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

Last change on this file since 4056 was 2711, checked in by mervart, 14 years ago
File size: 3.8 KB
RevLine 
[2709]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
[2710]23struct t_epochHeader {
[1311]24 double t_epoch;
25 int n_svs;
[2710]26};
[1311]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*)),
[2585]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
[2710]82 int reqLength = 2 + sizeof(recordSize) + sizeof(t_epochHeader);
[1431]83
84 if (_buffer.length() >= reqLength) {
[2710]85 t_epochHeader epochHdr;
[1429]86 memcpy(&epochHdr, _buffer.data() + 2 + sizeof(recordSize),
87 sizeof(epochHdr));
[1431]88
[2709]89 reqLength += epochHdr.n_svs * sizeof(t_obs) + sizeof(crc) + 1;
[1311]90
[1430]91 if (_buffer.length() >= reqLength) {
[1434]92
[2710]93 int checkLen = 2 + sizeof(recordSize) + sizeof(t_epochHeader) +
[2709]94 epochHdr.n_svs * sizeof(t_obs);
[1434]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;
[2711]101 t_obs obs;
102 memcpy(&obs, _buffer.data() + 2 + sizeof(recordSize) +
[2709]103 sizeof(epochHdr) + is * sizeof(t_obs), sizeof(t_obs));
[1436]104 _obsList.push_back(obs);
105 }
[1429]106 }
107 }
[1425]108 }
[1433]109 _buffer = _buffer.mid(reqLength);
[1425]110 }
[1311]111
[1429]112 // Ephemeris
113 // ---------
[1438]114 else if (_buffer.length() > 0 && char(_buffer[1]) == 0x01) {
[1430]115 int reqLength = 2 + sizeof(recordSize) + sizeof(gpsephemeris) +
[1438]116 sizeof(crc) + 1;
[1431]117
[1434]118 if (_buffer.length() >= reqLength) {
[1431]119
[1434]120 int checkLen = 2 + sizeof(recordSize) + sizeof(gpsephemeris);
121 memcpy(&crc, _buffer.data() + checkLen, sizeof(crc));
[1436]122 int crcCal = cal_crc((unsigned char*) _buffer.data(), checkLen);
[1434]123
[1436]124 if (crc == crcCal) {
[1438]125 ephFound = true;
[1436]126 gpsephemeris* gpsEph = new gpsephemeris;
127 memcpy(gpsEph, _buffer.data() + 2 + sizeof(recordSize),
128 sizeof(gpsephemeris));
129 emit newGPSEph(gpsEph);
130 }
[1426]131 }
[1433]132 _buffer = _buffer.mid(reqLength);
[1425]133 }
[1311]134
[1425]135 else {
[1429]136 _buffer == _buffer.mid(1);
[1311]137 }
[1312]138 }
[1311]139
[1438]140 if (obsFound || ephFound) {
141 return success;
142 }
143 else {
144 return failure;
145 }
[1310]146}
Note: See TracBrowser for help on using the repository browser.