Index: /trunk/BNC/RTCM3/RTCM3coDecoder.cpp
===================================================================
--- /trunk/BNC/RTCM3/RTCM3coDecoder.cpp	(revision 1828)
+++ /trunk/BNC/RTCM3/RTCM3coDecoder.cpp	(revision 1829)
@@ -123,18 +123,25 @@
   errmsg.clear();
 
+//printf("Start with %d new bytes and %d old bytes\n", bufLen, _buffer.size());
+
   _buffer.append(QByteArray(buffer,bufLen));
 
   t_irc retCode = failure;
 
-  while (true) {
-   
+  while(_buffer.size())
+  {
     int bytesused = 0;
-    GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(), 
+    struct ClockOrbit cox;
+    memcpy(&cox, &_co, sizeof(cox)); /* save state */
+
+    GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
                                         _buffer.size(), &bytesused);
 
-    // Not enough Data
-    // ---------------
-    if      (irc == GCOBR_SHORTBUFFER         ||
-             irc == GCOBR_MESSAGEEXCEEDSBUFFER) {
+//printf("used %4d buffer %4d return %4d first byte %2x\n", bytesused, _buffer.size(), irc, (unsigned char)_buffer.data()[0]);
+    if(irc <= -30) /* not enough data found */
+    {
+      /* copy previous state back - necessary in case of MESSAGEFOLLOWS with
+         incomplete second block */
+      memcpy(&_co, &cox, sizeof(cox));
       if (retCode != success) {
         _GPSweeks = -1.0;
@@ -142,82 +149,80 @@
       return retCode;
     }
-
-    // Second part of the message follows
-    // ----------------------------------
-    else if (irc == GCOBR_MESSAGEFOLLOWS) {
+    else if(irc >= 0)
+    {
       _buffer = _buffer.mid(bytesused);
-    }
-    
-    // Message correctly decoded
-    // -------------------------
-    else if (bytesused > 0 && irc == GCOBR_OK) {
-
-      reopen();
-
-      int    GPSweek;
-      currentGPSWeeks(GPSweek, _GPSweeks);
-      if (_co.NumberOfGPSSat > 0) {
-        if      (_GPSweeks > _co.GPSEpochTime + 86400.0) {
-          GPSweek += 1;
-        }
-        else if (_GPSweeks < _co.GPSEpochTime - 86400.0) {
-          GPSweek -= 1;
-        }
-        _GPSweeks = _co.GPSEpochTime;
+
+      if(irc == GCOBR_OK) /* correctly and complete decoded */
+      {
+        reopen();
+
+//printf("TIME: gps %d glonass %d gps_tod %d\n", _co.GPSEpochTime, _co.GLONASSEpochTime, _co.GPSEpochTime%86400);
+
+        int    GPSweek;
+        currentGPSWeeks(GPSweek, _GPSweeks);
+        if (_co.NumberOfGPSSat > 0) {
+          if      (_GPSweeks > _co.GPSEpochTime + 86400.0) {
+            GPSweek += 1;
+          }
+          else if (_GPSweeks < _co.GPSEpochTime - 86400.0) {
+            GPSweek -= 1;
+          }
+          _GPSweeks = _co.GPSEpochTime;
+        }
+        else {
+          double GPSdaysec = fmod(_GPSweeks, 86400.0);
+          int    weekDay   = int((_GPSweeks - GPSdaysec) / 86400.0);
+          if      (GPSdaysec > _co.GLONASSEpochTime + 3600.0) {
+            weekDay += 1;
+            if (weekDay > 6) {
+              weekDay = 0;
+              GPSweek += 1;
+            }
+          }
+          else if (GPSdaysec < _co.GLONASSEpochTime - 3600.0) {
+            weekDay -= 1;
+            if (weekDay < 0) {
+              weekDay = 6;
+              GPSweek -= 1;
+            }
+          }
+          _GPSweeks = weekDay * 86400.0 + _co.GLONASSEpochTime;
+        }
+
+        for(int ii = 0; ii < _co.NumberOfGPSSat; ++ii) {
+          QString line;
+          line.sprintf("%d %.1f G%2.2d   %3d   %8.3f   %8.3f %8.3f %8.3f", 
+                  GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD, 
+                  _co.Sat[ii].Clock.DeltaA0,
+                  _co.Sat[ii].Orbit.DeltaRadial, 
+                  _co.Sat[ii].Orbit.DeltaAlongTrack,
+                  _co.Sat[ii].Orbit.DeltaCrossTrack);
+          long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
+          printLine(line, coTime);
+        }
+        for(int ii = CLOCKORBIT_NUMGPS; 
+            ii < CLOCKORBIT_NUMGPS + _co.NumberOfGLONASSSat; ++ii) {
+          QString line;
+          line.sprintf("%d %.1f R%2.2d   %3d   %8.3f   %8.3f %8.3f %8.3f", 
+                  GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD, 
+                  _co.Sat[ii].Clock.DeltaA0, 
+                  _co.Sat[ii].Orbit.DeltaRadial, 
+                  _co.Sat[ii].Orbit.DeltaAlongTrack,
+                  _co.Sat[ii].Orbit.DeltaCrossTrack);
+          long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
+          printLine(line, coTime);
+        }
+        retCode = success;
+        memset(&_co, 0, sizeof(_co));
       }
-      else {
-        double GPSdaysec = fmod(_GPSweeks, 86400.0);
-        int    weekDay   = int((_GPSweeks - GPSdaysec) / 86400.0);
-        if      (GPSdaysec > _co.GLONASSEpochTime + 3600.0) {
-          weekDay += 1;
-          if (weekDay > 6) {
-            weekDay = 0;
-            GPSweek += 1;
-          }
-        }
-        else if (GPSdaysec < _co.GLONASSEpochTime - 3600.0) {
-          weekDay -= 1;
-          if (weekDay < 0) {
-            weekDay = 6;
-            GPSweek -= 1;
-          }
-        }
-        _GPSweeks = weekDay * 86400.0 + _co.GLONASSEpochTime;
-      }
-
-      for(int ii = 0; ii < _co.NumberOfGPSSat; ++ii) {
-        QString line;
-        line.sprintf("%d %.1f G%2.2d   %3d   %8.3f   %8.3f %8.3f %8.3f", 
-               GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD, 
-               _co.Sat[ii].Clock.DeltaA0,
-               _co.Sat[ii].Orbit.DeltaRadial, 
-               _co.Sat[ii].Orbit.DeltaAlongTrack,
-               _co.Sat[ii].Orbit.DeltaCrossTrack);
-        long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
-        printLine(line, coTime);
-      }
-      for(int ii = CLOCKORBIT_NUMGPS; 
-          ii < CLOCKORBIT_NUMGPS + _co.NumberOfGLONASSSat; ++ii) {
-        QString line;
-        line.sprintf("%d %.1f R%2.2d   %3d   %8.3f   %8.3f %8.3f %8.3f", 
-               GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD, 
-               _co.Sat[ii].Clock.DeltaA0, 
-               _co.Sat[ii].Orbit.DeltaRadial, 
-               _co.Sat[ii].Orbit.DeltaAlongTrack,
-               _co.Sat[ii].Orbit.DeltaCrossTrack);
-        long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
-        printLine(line, coTime);
-      }
-      _buffer = _buffer.mid(bytesused);
-      retCode = success;
+    }
+    else /* error  - skip 1 byte and retry */
+    {
       memset(&_co, 0, sizeof(_co));
-    }
-
-    // All other Cases
-    // ---------------
-    else {
       _buffer = _buffer.mid(1);
     }
   }
+//printf("Return with %d (success = %d) bytes %d\n", retCode, success, _buffer.size());
+  return retCode;
 }
 
Index: /trunk/BNC/RTCM3/clock_orbit_rtcm.c
===================================================================
--- /trunk/BNC/RTCM3/clock_orbit_rtcm.c	(revision 1828)
+++ /trunk/BNC/RTCM3/clock_orbit_rtcm.c	(revision 1829)
@@ -3,5 +3,5 @@
         Name:           clock_orbit_rtcm.c
         Project:        RTCM3
-        Version:        $Id: clock_orbit_rtcm.c,v 1.6 2009/05/04 12:53:42 stoecker Exp $
+        Version:        $Id: clock_orbit_rtcm.c,v 1.12 2009/05/05 07:00:40 mervart Exp $
         Authors:        Dirk Stöcker
         Description:    state space approach for RTCM3
@@ -618,7 +618,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
+    if(co->OrbitDataSupplied&1)
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 1;
 #ifdef DEBUG
 fprintf(stderr, "epochtime %d ui %d mmi %d sats %d\n",co->GPSEpochTime,co->UpdateInterval,mmi,co->NumberOfGPSSat);
@@ -663,7 +663,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->ClockDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->ClockDataSupplied = 1;
+    if(co->ClockDataSupplied & 1)
+      return GCOBR_DATAMISMATCH;
+    co->ClockDataSupplied |= 1;
     for(i = 0; i < co->NumberOfGPSSat; ++i)
     {
@@ -683,8 +683,8 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->ClockDataSupplied || co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
-    co->ClockDataSupplied = 1;
+    if((co->ClockDataSupplied&1) || (co->OrbitDataSupplied&1))
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 1;
+    co->ClockDataSupplied |= 1;
     for(i = 0; i < co->NumberOfGPSSat; ++i)
     {
@@ -715,7 +715,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->URADataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->URADataSupplied = 1;
+    if(co->URADataSupplied&1)
+      return GCOBR_DATAMISMATCH;
+    co->URADataSupplied |= 1;
     for(i = 0; i < co->NumberOfGPSSat; ++i)
     {
@@ -733,7 +733,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->HRDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->HRDataSupplied = 1;
+    if(co->HRDataSupplied&1)
+      return GCOBR_DATAMISMATCH;
+    co->HRDataSupplied |= 1;
     for(i = 0; i < co->NumberOfGPSSat; ++i)
     {
@@ -749,7 +749,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
+    if(co->OrbitDataSupplied&2)
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
@@ -777,7 +777,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->ClockDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->ClockDataSupplied = 1;
+    if(co->ClockDataSupplied&2)
+      return GCOBR_DATAMISMATCH;
+    co->ClockDataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
@@ -796,8 +796,8 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->ClockDataSupplied || co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
-    co->ClockDataSupplied = 1;
+    if((co->ClockDataSupplied&2) || (co->OrbitDataSupplied&2))
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 2;
+    co->ClockDataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
@@ -827,7 +827,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->URADataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->URADataSupplied = 1;
+    if(co->URADataSupplied&2)
+      return GCOBR_DATAMISMATCH;
+    co->URADataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
@@ -844,7 +844,7 @@
     G_RESERVED5
     G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->HRDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->HRDataSupplied = 1;
+    if(co->HRDataSupplied&2)
+      return GCOBR_DATAMISMATCH;
+    co->HRDataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
@@ -900,7 +900,7 @@
     OLD_G_RESERVED6
     OLD_G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
+    if(co->OrbitDataSupplied&1)
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 1;
     for(i = 0; i < co->NumberOfGPSSat; ++i)
     {
@@ -928,7 +928,7 @@
     OLD_G_RESERVED6
     OLD_G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->ClockDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->ClockDataSupplied = 1;
+    if(co->ClockDataSupplied&1)
+      return GCOBR_DATAMISMATCH;
+    co->ClockDataSupplied |= 1;
     for(i = 0; i < co->NumberOfGPSSat; ++i)
     {
@@ -948,8 +948,8 @@
     OLD_G_RESERVED6
     OLD_G_NO_OF_SATELLITES(co->NumberOfGPSSat)
-    if(co->ClockDataSupplied || co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
-    co->ClockDataSupplied = 1;
+    if((co->ClockDataSupplied&1) || (co->OrbitDataSupplied&1))
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 1;
+    co->ClockDataSupplied |= 1;
     for(i = 0; i < co->NumberOfGPSSat; ++i)
     {
@@ -978,7 +978,7 @@
     OLD_G_RESERVED6
     OLD_G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
+    if(co->OrbitDataSupplied&2)
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
@@ -1005,7 +1005,7 @@
     OLD_G_RESERVED6
     OLD_G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->ClockDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->ClockDataSupplied = 1;
+    if(co->ClockDataSupplied&2)
+      return GCOBR_DATAMISMATCH;
+    co->ClockDataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
@@ -1024,8 +1024,8 @@
     OLD_G_RESERVED6
     OLD_G_NO_OF_SATELLITES(co->NumberOfGLONASSSat)
-    if(co->ClockDataSupplied || co->OrbitDataSupplied)
-      return GCOBR_DATAMISMATCH;
-    co->OrbitDataSupplied = 1;
-    co->ClockDataSupplied = 1;
+    if((co->ClockDataSupplied&2) || (co->OrbitDataSupplied&2))
+      return GCOBR_DATAMISMATCH;
+    co->OrbitDataSupplied |= 2;
+    co->ClockDataSupplied |= 2;
     for(i = CLOCKORBIT_NUMGPS;
     i < CLOCKORBIT_NUMGPS+co->NumberOfGLONASSSat; ++i)
