]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 3089] Serial Parser does not work anymore for hopfser like device
authorJuergen Perlinger <perlinger@ntp.org>
Tue, 8 Nov 2016 19:01:41 +0000 (20:01 +0100)
committerJuergen Perlinger <perlinger@ntp.org>
Tue, 8 Nov 2016 19:01:41 +0000 (20:01 +0100)
bk: 582221152oAONqDGsS0w446KtpctYA

ChangeLog
libparse/clk_hopf6021.c

index 0805467dc6b9b1ce7768a039f6a2d87af37546b9..e7210f7193ab9fa02987c3bc11dbb837704cfbfd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+---
+* [Bug 3089] Serial Parser does not work anymore for hopfser like device
+  - simplified / refactored hex-decoding in driver. <perlinger@ntp.org>
+
 ---
 (4.2.8p8) 2016/06/02 Released by Harlan Stenn <stenn@ntp.org>
 
index 235962890f8c786ad7f375206da9f8ea4c29df6e..c5980ef13f2b470884911481b115958e7e1fa488 100644 (file)
@@ -113,13 +113,10 @@ static struct format hopf6021_fmt =
 
 #define OFFS(x) format->field_offsets[(x)].offset
 #define STOI(x, y) Stoi(&buffer[OFFS(x)], y, format->field_offsets[(x)].length)
-#define hexval(x) (('0' <= (x) && (x) <= '9') ? (x) - '0' : \
-                  ('a' <= (x) && (x) <= 'f') ? (x) - 'a' + 10 : \
-                  ('A' <= (x) && (x) <= 'F') ? (x) - 'A' + 10 : \
-                  -1)
 
 static parse_cvt_fnc_t cvt_hopf6021;
 static parse_inp_fnc_t inp_hopf6021;
+static unsigned char   hexval(unsigned char);
 
 clockformat_t clock_hopf6021 =
 {
@@ -160,40 +157,40 @@ cvt_hopf6021(
                return CVT_FAIL|CVT_BADFMT;
        }
 
-       clock_time->usecond   = 0;
-       clock_time->utcoffset = 0;
+       clock_time->usecond = 0;
+       clock_time->flags   = 0;
 
-       status = (u_char) hexval(buffer[OFFS(O_FLAGS)]);
-       weekday= (u_char) hexval(buffer[OFFS(O_WDAY)]);
+       status  = hexval(buffer[OFFS(O_FLAGS)]);
+       weekday = hexval(buffer[OFFS(O_WDAY)]);
 
        if ((status == 0xFF) || (weekday == 0xFF))
        {
                return CVT_FAIL|CVT_BADFMT;
        }
 
-       clock_time->flags  = 0;
-
        if (weekday & HOPF_UTC)
        {
-               clock_time->flags |= PARSEB_UTC;
+               clock_time->flags     |= PARSEB_UTC;
+               clock_time->utcoffset  = 0;
+       }
+       else if (status & HOPF_DST)
+       {
+               clock_time->flags     |= PARSEB_DST;
+               clock_time->utcoffset  = -2*60*60; /* MET DST */
        }
        else
        {
-               if (status & HOPF_DST)
-               {
-                       clock_time->flags     |= PARSEB_DST;
-                       clock_time->utcoffset  = -2*60*60; /* MET DST */
-               }
-               else
-               {
-                       clock_time->utcoffset  = -1*60*60; /* MET */
-               }
+               clock_time->utcoffset  = -1*60*60; /* MET */
        }
 
-       clock_time->flags |= (status & HOPF_DSTWARN)  ? PARSEB_ANNOUNCE : 0;
-
+       if (status & HOPF_DSTWARN)
+       {
+               clock_time->flags |= PARSEB_ANNOUNCE;
+       }
+       
        switch (status & HOPF_MODE)
        {
+           default:    /* dummy: we cover all 4 cases. */
            case HOPF_INVALID:  /* Time/Date invalid */
                clock_time->flags |= PARSEB_POWERUP;
                break;
@@ -205,9 +202,6 @@ cvt_hopf6021(
            case HOPF_RADIO:    /* Radio clock */
            case HOPF_RADIOHP:  /* Radio clock high precision */
                break;
-
-           default:
-               return CVT_FAIL|CVT_BADFMT;
        }
 
        return CVT_OK;
@@ -244,6 +238,30 @@ inp_hopf6021(
        }
 }
 
+/*
+ * convert a hex-digit to numeric value
+ */
+static unsigned char
+hexval(
+       unsigned char ch
+       )
+{
+       unsigned int dv;
+       
+       if ((dv = ch - '0') >= 10u)
+       {
+               if ((dv -= 'A'-'0') < 6u || (dv -= 'a'-'A') < 6u)
+               {
+                       dv += 10;
+               }
+               else
+               {
+                       dv = 0xFF;
+               }
+       }
+       return (unsigned char)dv;
+}
+
 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_HOPF6021) */
 int clk_hopf6021_bs;
 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_HOPF6021) */