Changeset 709 in ntrip for trunk/BNC/RTCM/RTCM2.cpp


Ignore:
Timestamp:
Mar 7, 2008, 5:56:07 PM (16 years ago)
Author:
weber
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/RTCM/RTCM2.cpp

    r708 r709  
    4545//   2008/03/04  AHA  Fixed problems with PRN 32
    4646//   2008/03/05  AHA  Implemeted fix for Trimble 4000SSI receivers
     47//   2008/03/07  AHA  Major revision of input buffer handling
     48//   2008/03/07  AHA  Removed unnecessary failure flag
    4749//
    4850// (c) DLR/GSOC
     
    6365// undersized packets in get(Unsigned)Bits
    6466
    65 #define DEBUG 0   
     67#define DEBUG 0
    6668
    6769// Activate (1) or deactivate (0) rounding of measurement epochs to 100ms
     
    136138void ThirtyBitWord::clear() {
    137139  W = 0;
    138 };
    139 
    140 // Failure indicator for input operations
    141 
    142 bool ThirtyBitWord::fail() const {
    143   return failure;
    144140};
    145141
     
    259255
    260256
    261 
    262257// Append a byte with six data bits
    263258
     
    275270  // Bits 7 and 6 (of 0..7) must be "01" for valid data bytes
    276271  if ( (b & 0x40) != 0x40 ) {
    277     failure = true;
     272    // We simply skip the invalid input byte and leave the word unchanged
    278273    return;
    279274  };
     
    290285// Get next 30bit word from string
    291286
    292 void ThirtyBitWord::get(string& buf) {
     287void ThirtyBitWord::get(const string& buf) {
    293288
    294289  // Check if string is long enough
    295290   
    296291  if (buf.size()<5) {
    297     failure = true;
     292    // Ignore; users should avoid this case prior to calling get()
    298293    return;
    299294  };
     
    302297 
    303298  for (int i=0; i<5; i++) append(buf[i]);
    304   buf.erase(0,5);
    305299
    306300#if (DEBUG>0)
    307301  if (!validParity()) {
    308     cerr << "Parity error "
     302    cerr << "Parity error in get()"
    309303         << bitset<32>(all()) << endl;
    310304  };
    311305#endif
    312   failure = false;
    313306
    314307};
     
    328321#if (DEBUG>0)
    329322  if (!validParity()) {
    330     cerr << "Parity error "
     323    cerr << "Parity error in get()"
    331324         << bitset<32>(all()) << endl;
    332325  };
    333326#endif
    334   failure = false;
    335327
    336328};
     
    340332void ThirtyBitWord::getHeader(string& buf) {
    341333
    342   unsigned int W_old = W;
     334  const int wordLen = 5; // Number of bytes representing a 30-bit word
     335  const int spare   = 1; // Number of spare words for resync of parity
     336                         // (same value as inRTCM2packet::getPacket())
    343337  unsigned int i;
    344338 
    345339  i=0;
    346   while (!isHeader() || i<5 ) {
    347     // Check if string is long enough; if not restore old word and exit
    348     if (buf.size()<i+1) {
    349       W = W_old;
    350       failure = true;
    351       return;
    352     };
     340  while (!isHeader() && i<buf.size() ) {
    353341    // Process byte
    354     append(buf[i]); i++;
    355   };
    356 
    357   // Remove processed bytes from buffer
    358  
    359   buf.erase(0,i);
     342    append(buf[i]);
     343    // Increment count
     344    i++;
     345  };
     346
     347  // Remove processed bytes from buffer. Retain also the previous word to
     348  // allow a resync if getHeader() is called repeatedly on the same buffer.
     349  if (i>=(1+spare)*wordLen) buf.erase(0,i-(1+spare)*wordLen);
    360350
    361351#if (DEBUG>0)
    362352  if (!validParity()) {
    363     cerr << "Parity error "
     353    cerr << "Parity error in getHeader()"
    364354         << bitset<32>(all()) << endl;
    365355  };
    366356#endif
    367   failure = false;
    368 
     357 
    369358};
    370359
     
    385374#if (DEBUG>0)
    386375  if (!validParity()) {
    387     cerr << "Parity error "
     376    cerr << "Parity error in getHeader()"
    388377         << bitset<32>(all()) << endl;
    389378  };
    390379#endif
    391   failure = false;
    392380
    393381};
     
    443431void RTCM2packet::getPacket(std::string& buf) {
    444432
    445   int           n;
    446   ThirtyBitWord W_old = W;
    447   string        buf_old = buf;
    448  
    449   // Try to read a full packet. If the input buffer is too short
    450   // clear all data and restore the latest 30-bit word prior to
    451   // the getPacket call. The empty header word will indicate
    452   // an invalid message, which signals an unsuccessful getPacket()
    453   // call.
    454    
    455   W.getHeader(buf);
    456   H1 = W.value();
    457   if (W.fail()) { clear(); W=W_old; buf=buf_old; return; };
    458   if (!W.validParity()) { clear(); return; };
    459  
    460   W.get(buf);       
    461   H2 = W.value();
    462   if (W.fail()) { clear(); W=W_old; buf=buf_old; return; };
    463   if (!W.validParity()) { clear(); return; };
     433  const int wordLen = 5; // Number of bytes representing a 30-bit word
     434  const int spare   = 1; // Number of spare words for resync of parity
     435                         // (same value as used in ThirtyBitWord::getHeader)
     436  unsigned int n;
     437 
     438  // Try to read a full packet. Processed bytes are removed from the input
     439  // buffer except for the latest spare*wordLen bytes to restore the parity
     440  // bytes upon subseqeunt calls of getPAcket().
     441 
     442  // Locate and read the first header word
     443  W.getHeader(buf);
     444  if (!W.isHeader()) {
     445    // No header found; try again next time. buf retains only the spare
     446    // words. The packet contents is cleared to indicate an unsuccessful
     447    // termination of getPacket().
     448    clear();
     449    return;
     450  };
     451  H1 = W.value();
     452 
     453  // Do we have enough bytes to read the next word? If not, the packet
     454  // contents is cleared to indicate an unsuccessful termination. The
     455  // previously read spare and header bytes are retained in the buffer
     456  // for use in the next call of getPacket().
     457  if (buf.size()<(spare+2)*wordLen) { clear(); return; };
     458 
     459  // Read the second header word
     460  W.get(buf.substr((spare+1)*wordLen,buf.size()-1-(spare+1)*wordLen)); 
     461  H2 = W.value();
     462  if (!W.validParity()) {
     463    // Invalid H2 word; delete first buffer byte and try to resynch next time.
     464    // The packet contents is cleared to indicate an unsuccessful termination.
     465    clear();
     466    buf.erase(0,1);
     467    return;
     468  };
    464469
    465470  n = nDataWords();
     471 
     472  // Do we have enough bytes to read the next word? If not, the packet
     473  // contents is cleared to indicate an unsuccessful termination. The
     474  // previously read spare and header bytes are retained in the buffer
     475  // for use in the next call of getPacket().
     476  if (buf.size()<(spare+2+n)*wordLen) { clear(); return; };
     477 
    466478  DW.resize(n);
    467   for (int i=0; i<n; i++) {
    468     W.get(buf);
    469     DW[i] = W.value();
    470     if (W.fail()) { clear(); W=W_old; buf=buf_old; return; };
    471     if (!W.validParity()) { clear(); return; };
    472   };
    473 
     479  for (unsigned int i=0; i<n; i++) {
     480    W.get(buf.substr((spare+2+i)*wordLen,buf.size()-1-(spare+2+i)*wordLen));
     481    DW[i] = W.value();
     482    if (!W.validParity()) {
     483      // Invalid data word; delete first byte and try to resynch next time.
     484      // The packet contents is cleared to indicate an unsuccessful termination.
     485      clear();
     486      buf.erase(0,1);
     487      return;
     488    };
     489  };
     490
     491  // Successful packet extraction; delete total number of message bytes
     492  // from buffer.
     493  // Note: a total of "spare" words remain in the buffer to enable a
     494  // parity resynchronization when searching the next header.
     495 
     496  buf.erase(0,(n+2)*wordLen);
     497 
    474498  return;
    475499 
     
    487511  W.getHeader(inp);
    488512  H1 = W.value();
    489   if (W.fail() || !W.validParity()) { clear(); return; }
     513  if (inp.fail() || !W.isHeader()) { clear(); return; }
    490514 
    491515  W.get(inp);       
    492516  H2 = W.value();
    493   if (W.fail() || !W.validParity()) { clear(); return; }
     517  if (inp.fail() || !W.validParity()) { clear(); return; }
    494518
    495519  n = nDataWords();
     
    498522    W.get(inp);
    499523    DW[i] = W.value();
    500     if (W.fail() || !W.validParity()) { clear(); return; }
     524    if (inp.fail() || !W.validParity()) { clear(); return; }
    501525  };
    502526
Note: See TracChangeset for help on using the changeset viewer.