summaryrefslogtreecommitdiffstats
path: root/usr.sbin/xntpd/parse/parse.c
blob: 16c7813acedcabc28a3ca2428598a2d36cf071a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
#if defined(REFCLOCK) && (defined(PARSE) || defined(PARSEPPS))
/*
 * /src/NTP/REPOSITORY/v3/parse/parse.c,v 3.27 1994/06/01 08:18:33 kardel Exp
 *  
 * parse.c,v 3.27 1994/06/01 08:18:33 kardel Exp
 *
 * Parser module for reference clock
 *
 * PARSEKERNEL define switches between two personalities of the module
 * if PARSEKERNEL is defined this module can be used with dcf77sync.c as
 * a PARSEKERNEL kernel module. In this case the time stamps will be
 * a struct timeval.
 * when PARSEKERNEL is not defined NTP time stamps will be used.
 *
 * Copyright (c) 1992,1993,1994
 * Frank Kardel Friedrich-Alexander Universitaet Erlangen-Nuernberg
 *                                    
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#if	!(defined(lint) || defined(__GNUC__))
static char rcsid[] = "parse.c,v 3.19 1994/01/25 19:05:20 kardel Exp";
#endif

#include "sys/types.h"
#include "sys/time.h"
#include "sys/errno.h"

#include "ntp_fp.h"
#include "ntp_unixtime.h"
#include "ntp_calendar.h"

#include "ntp_machine.h"

#if defined(PARSESTREAM) && (defined(SYS_SUNOS4) || defined(SYS_SOLARIS)) && defined(STREAM)
/*
 * Sorry, but in SunOS 4.x AND Solaris 2.x kernels there are no
 * mem* operations. I don't want them - bcopy, bzero
 * are fine in the kernel
 */
#ifndef NTP_NEED_BOPS
#define NTP_NEED_BOPS
#endif
#else
#ifndef NTP_NEED_BOPS
#ifndef bzero
#define bzero(_X_, _Y_) memset(_X_, 0, _Y_)
#define bcopy(_X_, _Y_, _Z_) memmove(_Y_, _X_, _Z_)
#endif
#endif
#endif

#include "parse.h"

#include "ntp_stdlib.h"

#ifdef PARSESTREAM
#include "sys/parsestreams.h"
#endif

extern clockformat_t *clockformats[];
extern unsigned short nformats;

static u_long timepacket();

/*
 * strings support usually not in kernel - duplicated, but what the heck
 */
static int
Strlen(s)
  register char *s;
{
  register int c;

  c = 0;
  if (s)
    {
      while (*s++)
	{
	  c++;
	}
    }
  return c;
}

static int
Strcmp(s, t)
  register char *s;
  register char *t;
{
  register int c = 0;

  if (!s || !t || (s == t))
    {
      return 0;
    }

  while (!(c = *s++ - *t++) && *s && *t)
    /* empty loop */;
  
  return c;
}

static int
timedout(parseio, ctime)
  register parse_t *parseio;
  register timestamp_t *ctime;
{
  struct timeval delta;

#ifdef PARSEKERNEL
  delta.tv_sec = ctime->tv.tv_sec - parseio->parse_lastchar.tv.tv_sec;
  delta.tv_usec = ctime->tv.tv_usec - parseio->parse_lastchar.tv.tv_usec;
  if (delta.tv_usec < 0)
    {
      delta.tv_sec  -= 1;
      delta.tv_usec += 1000000;
    }
#else
  extern long tstouslo[];
  extern long tstousmid[];
  extern long tstoushi[];

  l_fp delt;

  delt = ctime->fp;
  L_SUB(&delt, &parseio->parse_lastchar.fp);
  TSTOTV(&delt, &delta);
#endif

  if (timercmp(&delta, &parseio->parse_timeout, >))
    {
      parseprintf(DD_PARSE, ("parse: timedout: TRUE\n"));
      return 1;
    }
  else
    {
      parseprintf(DD_PARSE, ("parse: timedout: FALSE\n"));
      return 0;
    }
}

/*
 * setup_bitmaps
 * WARNING: NOT TO BE CALLED CONCURRENTLY WITH
 *          parse_ioread, parse_ioend, parse_ioinit
 */
static int
setup_bitmaps(parseio, low, high)
  register parse_t *parseio;
  register unsigned short low;
  register unsigned short high;
{
  register unsigned short i;
  register int f = 0;
  register clockformat_t *fmt;
  register unsigned short index, mask, plen;

  if ((low >= high) ||
      (high > nformats))
    {
      parseprintf(DD_PARSE, ("setup_bitmaps: failed: bounds error (low=%d, high=%d, nformats=%d)\n", low, high, nformats));
      return 0;
    }
  
  bzero(parseio->parse_startsym, sizeof (parseio->parse_startsym));
  bzero(parseio->parse_endsym, sizeof (parseio->parse_endsym));
  bzero(parseio->parse_syncsym, sizeof (parseio->parse_syncsym));

  plen = 0;

  parseio->parse_syncflags = 0;
  parseio->parse_timeout.tv_sec = 0;
  parseio->parse_timeout.tv_usec = 0;
  
  /*
   * gather bitmaps of possible start and end values
   */
  for (i=low; i < high; i++)
    {
      fmt = clockformats[i];

      if (!(parseio->parse_flags & PARSE_FIXED_FMT) &&
	   (fmt->flags & CVT_FIXEDONLY)) {
        if (parseio->parse_dsize < fmt->length)
	  parseio->parse_dsize = fmt->length;
	continue;
      }
      
      if (fmt->flags & F_START)
	{
	  index = fmt->startsym >> 3;
	  mask  = 1 << (fmt->startsym & 0x7);

	  if (parseio->parse_endsym[index] & mask)
	    {
#ifdef PARSEKERNEL
	      printf("parse: setup_bitmaps: failed: START symbol collides with END symbol (format %d)\n", i);
#else
	      syslog(LOG_ERR, "parse: setup_bitmaps: failed: START symbol collides with END symbol (format %d)\n", i);
#endif
	      return 0;
	    }
	  else
	    {
	      parseio->parse_startsym[index] |= mask;
	      f = 1;
	    }
	}

      if (fmt->flags & F_END)
	{
	  index = fmt->endsym >> 3;
	  mask  = 1 << (fmt->endsym & 0x7);

	  if (parseio->parse_startsym[index] & mask)
	    {
#ifdef PARSEKERNEL
	      printf("parse: setup_bitmaps: failed: END symbol collides with START symbol (format %d)\n", i);
#else
	      syslog(LOG_ERR, "parse: setup_bitmaps: failed: END symbol collides with START symbol (format %d)\n", i);
#endif
	      return 0;
	    }
	  else
	    {
	      parseio->parse_endsym[index] |= mask;
	      f = 1;
	    }
	}

      if (fmt->flags & SYNC_CHAR)
	{
	  parseio->parse_syncsym[fmt->syncsym >> 3] |= (1 << (fmt->syncsym & 0x7));
	}

      parseio->parse_syncflags |= fmt->flags & (SYNC_START|SYNC_END|SYNC_CHAR|SYNC_ONE|SYNC_ZERO|SYNC_TIMEOUT|SYNC_SYNTHESIZE);

      if (((fmt->flags & (SYNC_TIMEOUT|CVT_FIXEDONLY)) == (SYNC_TIMEOUT|CVT_FIXEDONLY)) &&
	  ((parseio->parse_timeout.tv_sec || parseio->parse_timeout.tv_usec) ? timercmp(&parseio->parse_timeout, &fmt->timeout, >) : 1))
	{
	  parseio->parse_timeout = fmt->timeout;
	}
      
      if (parseio->parse_dsize < fmt->length)
	parseio->parse_dsize = fmt->length;
    }

  if (parseio->parse_pdata)
    {
      FREE(parseio->parse_pdata, parseio->parse_plen);
      parseio->parse_plen = 0;
      parseio->parse_pdata = (void *)0;
    }

  if (!f && ((int)(high - low) > 1))
    {
      /*
       * need at least one start or end symbol
       */
#ifdef PARSEKERNEL
      printf("parse: setup_bitmaps: failed: neither START nor END symbol defined\n");
#else
      syslog(LOG_ERR, "parse: setup_bitmaps: failed: neither START nor END symbol defined\n");
#endif
      return 0;
    }

  if ((high - low == 1) && (clockformats[low]->flags & CVT_FIXEDONLY) &&
      (clockformats[low]->plen))
    {
      parseio->parse_plen  = clockformats[low]->plen;
      parseio->parse_pdata = (void *)MALLOC(parseio->parse_plen);

      if (!parseio->parse_pdata)
	{
	  /*
	   * no memory
	   */
#ifdef PARSEKERNEL
	  printf("parse: setup_bitmaps: failed: no memory for private data\n");
#else
	  syslog(LOG_ERR, "parse: setup_bitmaps: failed: no memory for private data\n");
#endif
	  return 0;
	}
      bzero((char *)parseio->parse_pdata, parseio->parse_plen);
    }

  return 1;
}

/*ARGSUSED*/
int
parse_ioinit(parseio)
  register parse_t *parseio;
{
  parseprintf(DD_PARSE, ("parse_iostart\n"));
  
  parseio->parse_plen = 0;
  parseio->parse_pdata = (void *)0;
  
  if (!setup_bitmaps(parseio, 0, nformats))
    return 0;

  parseio->parse_data = MALLOC(parseio->parse_dsize * 2 + 2);
  if (!parseio->parse_data)
    {
      parseprintf(DD_PARSE, ("init failed: malloc for data area failed\n"));
      return 0;
    }

  /*
   * leave room for '\0'
   */
  parseio->parse_ldata     = parseio->parse_data + parseio->parse_dsize + 1;
  parseio->parse_lformat   = 0;
  parseio->parse_badformat = 0;
  parseio->parse_ioflags   = PARSE_IO_CS7;	/* usual unix default */
  parseio->parse_flags     = 0;		/* true samples */
  parseio->parse_index     = 0;
  parseio->parse_ldsize    = 0;
  
  return 1;
}

/*ARGSUSED*/
void
parse_ioend(parseio)
  register parse_t *parseio;
{
  parseprintf(DD_PARSE, ("parse_ioend\n"));

  if (parseio->parse_pdata)
    FREE(parseio->parse_pdata, parseio->parse_plen);

  if (parseio->parse_data)
    FREE(parseio->parse_data, parseio->parse_dsize * 2 + 2);
}

/*ARGSUSED*/
int
parse_ioread(parseio, ch, ctime)
  register parse_t *parseio;
  register unsigned char ch;
  register timestamp_t *ctime;
{
  register unsigned updated = CVT_NONE;
  register unsigned short low, high;
  register unsigned index, mask;

  /*
   * within STREAMS CSx (x < 8) chars still have the upper bits set
   * so we normalize the characters by masking unecessary bits off.
   */
  switch (parseio->parse_ioflags & PARSE_IO_CSIZE)
    {
    case PARSE_IO_CS5:
      ch &= 0x1F;
      break;

    case PARSE_IO_CS6:
      ch &= 0x3F;
      break;

    case PARSE_IO_CS7:
      ch &= 0x7F;
      break;
      
    case PARSE_IO_CS8:
      break;
    }

  parseprintf(DD_PARSE, ("parse_ioread(0x%x, char=0x%x, ..., ...)\n", (unsigned int)parseio, ch & 0xFF));

  if (parseio->parse_flags & PARSE_FIXED_FMT)
    {
      if (!clockformats[parseio->parse_lformat]->convert)
	{
	  parseprintf(DD_PARSE, ("parse_ioread: input dropped.\n"));
	  return CVT_NONE;
	}

      if (clockformats[parseio->parse_lformat]->input)
	{
	  if (clockformats[parseio->parse_lformat]->input(parseio, ch, ctime))
	    updated = timepacket(parseio); /* got a datagram - process */
	  
	  low = high = 0;	/* all done - just post processing */
	}
      else
	{
	  low = parseio->parse_lformat;
	  high = low + 1;	/* scan just one format */
	}
    }
  else
    {
      low = 0;
      high = nformats;		/* scan all non fixed formats */
    }

  if (low != high)
    {
      index = ch >> 3;
      mask  =  1 << (ch & 0x7);

      if ((parseio->parse_syncflags & SYNC_CHAR) &&
	  (parseio->parse_syncsym[index] & mask))
	{
	  register clockformat_t *fmt;
	  register unsigned short i;
	  /*
	   * got a sync event - call sync routine
	   */

	  for (i = low; i < high; i++)
	    {
	      fmt = clockformats[i];

	      if ((fmt->flags & SYNC_CHAR) &&
		  (fmt->syncsym == ch))
		{
		  parseprintf(DD_PARSE, ("parse_ioread: SYNC_CHAR event\n"));
		  if (fmt->syncevt)
		    fmt->syncevt(parseio, ctime, fmt->data, SYNC_CHAR);
		}
	    }
	}
		
      if ((((parseio->parse_syncflags & SYNC_START) &&
	    (parseio->parse_startsym[index] & mask)) ||
	   (parseio->parse_index == 0)) ||
	  ((parseio->parse_syncflags & SYNC_TIMEOUT) &&
	   timedout(parseio, ctime)))
	{
	  register unsigned short i;
	  /*
	   * packet start - re-fill buffer
	   */
	  if (parseio->parse_index)
	    {
	      /*
	       * filled buffer - thus not end character found
	       * do processing now
	       */
	      parseio->parse_data[parseio->parse_index] = '\0';

	      updated = timepacket(parseio);
	      bcopy(parseio->parse_data, parseio->parse_ldata, parseio->parse_index+1);
	      parseio->parse_ldsize = parseio->parse_index+1;
	      if (parseio->parse_syncflags & SYNC_TIMEOUT)
		parseio->parse_dtime.parse_stime = *ctime;
	    }

	  /*
	   * could be a sync event - call sync routine if needed
	   */
	  if (parseio->parse_syncflags & SYNC_START)
	    for (i = low; i < high; i++)
	      {
		register clockformat_t *fmt = clockformats[i];

		if ((parseio->parse_index == 0) ||
		    ((fmt->flags & SYNC_START) && (fmt->startsym == ch)))
		  {
		    parseprintf(DD_PARSE, ("parse_ioread: SYNC_START event\n"));
		    if (fmt->syncevt)
		      fmt->syncevt(parseio, ctime, fmt->data, SYNC_START);
		  }
	      }
	  parseio->parse_index = 1;
	  parseio->parse_data[0] = ch;
	  parseprintf(DD_PARSE, ("parse: parse_ioread: buffer start\n"));
	}
      else
	{
	  register unsigned short i;

	  if (parseio->parse_index < parseio->parse_dsize)
	    {
	      /*
	       * collect into buffer
	       */
	      parseprintf(DD_PARSE, ("parse: parse_ioread: buffer[%d] = 0x%x\n", parseio->parse_index, ch));
	      parseio->parse_data[parseio->parse_index++] = ch;
	    }

	  if ((parseio->parse_endsym[index] & mask) ||
	      (parseio->parse_index >= parseio->parse_dsize))
	    {
	      /*
	       * packet end - process buffer
	       */
	      if (parseio->parse_syncflags & SYNC_END)
		for (i = low; i < high; i++)
		  {
		    register clockformat_t *fmt = clockformats[i];

		    if ((fmt->flags & SYNC_END) && (fmt->endsym == ch))
		      {
			parseprintf(DD_PARSE, ("parse_ioread: SYNC_END event\n"));
			if (fmt->syncevt)
			  fmt->syncevt(parseio, ctime, fmt->data, SYNC_END);
		      }
		  }
	      parseio->parse_data[parseio->parse_index] = '\0';
	      updated = timepacket(parseio);
	      bcopy(parseio->parse_data, parseio->parse_ldata, parseio->parse_index+1);
	      parseio->parse_ldsize = parseio->parse_index+1;
	      parseio->parse_index = 0;
	      parseprintf(DD_PARSE, ("parse: parse_ioread: buffer end\n"));
	    }
	}
    }

  if ((updated == CVT_NONE) &&
      (parseio->parse_flags & PARSE_FIXED_FMT) &&
      (parseio->parse_syncflags & SYNC_SYNTHESIZE) &&
      ((parseio->parse_dtime.parse_status & CVT_MASK) == CVT_OK) &&
      clockformats[parseio->parse_lformat]->synth)
    {
      updated = clockformats[parseio->parse_lformat]->synth(parseio, ctime);
    }
  
  /*
   * remember last character time
   */
  parseio->parse_lastchar = *ctime;

#ifdef DEBUG
  if ((updated & CVT_MASK) != CVT_NONE)
    {
      parseprintf(DD_PARSE, ("parse_ioread: time sample accumulated (status=0x%x)\n", updated));
    }
#endif

  parseio->parse_dtime.parse_status = updated;

  return (updated & CVT_MASK) != CVT_NONE;
}

/*
 * parse_iopps
 *
 * take status line indication and derive synchronisation information
 * from it.
 * It can also be used to decode a serial serial data format (such as the
 * ONE, ZERO, MINUTE sync data stream from DCF77)
 */
/*ARGSUSED*/
int
parse_iopps(parseio, status, ptime)
  register parse_t *parseio;
  register int status;
  register timestamp_t *ptime;
{
  register unsigned updated = CVT_NONE;

  /*
   * PPS pulse information will only be delivered to ONE clock format
   * this is either the last successful conversion module with a ppssync
   * routine, or a fixed format with a ppssync routine
   */
  parseprintf(DD_PARSE, ("parse_iopps: STATUS %s\n", (status == SYNC_ONE) ? "ONE" : "ZERO"));

  if (((parseio->parse_flags & PARSE_FIXED_FMT) ||
       ((parseio->parse_dtime.parse_status & CVT_MASK) == CVT_OK)) &&
      clockformats[parseio->parse_lformat]->syncpps &&
      (status & clockformats[parseio->parse_lformat]->flags))
    {
      updated = clockformats[parseio->parse_lformat]->syncpps(parseio, status == SYNC_ONE, ptime);
      parseprintf(DD_PARSE, ("parse_iopps: updated = 0x%x\n", updated));
    }
  else
    {
      parseprintf(DD_PARSE, ("parse_iopps: STATUS dropped\n"));
    }

  return (updated & CVT_MASK) != CVT_NONE;
}

/*
 * parse_iodone
 *
 * clean up internal status for new round
 */
/*ARGSUSED*/
void
parse_iodone(parseio)
  register parse_t *parseio;
{
  /*
   * we need to clean up certain flags for the next round
   */
  parseprintf(DD_PARSE, ("parse_iodone: DONE\n"));
  parseio->parse_dtime.parse_state = 0; /* no problems with ISRs */
}

/*---------- conversion implementation --------------------*/

/*
 * convert a struct clock to UTC since Jan, 1st 1970 0:00 (the UNIX EPOCH)
 */
#define dysize(x)	((x) % 4 ? 365 : ((x % 400) ? 365 :366))

time_t
parse_to_unixtime(clock, cvtrtc)
  register clocktime_t   *clock;
  register u_long *cvtrtc;
{
#define SETRTC(_X_)	{ if (cvtrtc) *cvtrtc = (_X_); }
  static int days_of_month[] = 
    {
      0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };
  register int i;
  time_t t;
  
  if (clock->utctime)
    return clock->utctime;	/* if the conversion routine gets it right away - why not */

  if (clock->year < 100)
    clock->year += 1900;

  if (clock->year < 1970)
    clock->year += 100;		/* XXX this will do it till <2070 */

  if (clock->year < 0)
    {
      SETRTC(CVT_FAIL|CVT_BADDATE);
      return -1;
    }
  
  /*
   * sorry, slow section here - but it's not time critical anyway
   */
  t =  (clock->year - 1970) * 365;
  t += (clock->year >> 2) - (1970 >> 2);
  t -= clock->year / 400 - 1970 / 400;

  				/* month */
  if (clock->month <= 0 || clock->month > 12)
    {
      SETRTC(CVT_FAIL|CVT_BADDATE);
      return -1;		/* bad month */
    }
				/* adjust leap year */
  if (clock->month >= 3 && dysize(clock->year) == 366)
      t++;

  for (i = 1; i < clock->month; i++)
    {
      t += days_of_month[i];
    }
				/* day */
  if (clock->day < 1 || ((clock->month == 2 && dysize(clock->year) == 366) ?
		  clock->day > 29 : clock->day > days_of_month[clock->month]))
    {
      SETRTC(CVT_FAIL|CVT_BADDATE);
      return -1;		/* bad day */
    }

  t += clock->day - 1;
				/* hour */
  if (clock->hour < 0 || clock->hour >= 24)
    {
      SETRTC(CVT_FAIL|CVT_BADTIME);
      return -1;		/* bad hour */
    }

  t = TIMES24(t) + clock->hour;

  				/* min */
  if (clock->minute < 0 || clock->minute > 59)
    {
      SETRTC(CVT_FAIL|CVT_BADTIME);
      return -1;		/* bad min */
    }

  t = TIMES60(t) + clock->minute;
				/* sec */
  
  if (clock->second < 0 || clock->second > 60)	/* allow for LEAPs */
    {
      SETRTC(CVT_FAIL|CVT_BADTIME);
      return -1;		/* bad sec */
    }

  t  = TIMES60(t) + clock->second;

  t += clock->utcoffset;	/* warp to UTC */

				/* done */

  clock->utctime = t;		/* documentray only */

  return t;
}

/*--------------- format conversion -----------------------------------*/

int
Stoi(s, zp, cnt)
  char *s;
  long *zp;
  int cnt;
{
  char *b = s;
  int f,z,v;
  char c;

  f=z=v=0;

  while(*s == ' ')
    s++;
  
  if (*s == '-')
    {
      s++;
      v = 1;
    }
  else
    if (*s == '+')
      s++;
  
  for(;;)
    {
      c = *s++;
      if (c == '\0' || c < '0' || c > '9' || (cnt && ((s-b) > cnt)))
	{
	  if (f == 0)
	    {
	      return(-1);
	    }
	  if (v)
	    z = -z;
	  *zp = z;
	  return(0);
	}
      z = (z << 3) + (z << 1) + ( c - '0' );
      f=1;
    }
}


int
Strok(s, m)
  char *s;
  char *m;
{
  if (!s || !m)
    return 0;

  while(*s && *m)
    {
      if ((*m == ' ') ? 1 : (*s == *m))
	{
	  s++;
	  m++;
	}
      else
	{
	  return 0;
	}
    }
  return !*m;
}

u_long
updatetimeinfo(parseio, t, usec, flags)
  register parse_t         *parseio;
  register time_t         t;
  register u_long  usec;
  register u_long  flags;
{
  register long usecoff;
  register long mean;
  long delta[PARSE_DELTA];

#ifdef PARSEKERNEL
    usecoff = (t - parseio->parse_dtime.parse_stime.tv.tv_sec) * 1000000
      - parseio->parse_dtime.parse_stime.tv.tv_usec + usec;
#else
    extern long tstouslo[];
    extern long tstousmid[];
    extern long tstoushi[];

    TSFTOTVU(parseio->parse_dtime.parse_stime.fp.l_uf, usecoff);
    usecoff  = -usecoff;
    usecoff += (t - parseio->parse_dtime.parse_stime.fp.l_ui + JAN_1970) * 1000000
              + usec;
#endif
  
  /*
   * filtering (median) if requested
   */
  if (parseio->parse_flags & PARSE_STAT_FILTER)
    {
      register int n, i, s, k;
      
      parseio->parse_delta[parseio->parse_dindex] = usecoff;

      parseio->parse_dindex = (parseio->parse_dindex + 1) % PARSE_DELTA;

      /*
       * sort always - thus every sample gets its data
       */
      bcopy((caddr_t)parseio->parse_delta, (caddr_t)delta, sizeof(delta));
  
      for (s = 0; s < PARSE_DELTA; s++)
	for (k = s+1; k < PARSE_DELTA; k++)
	  {			/* Yes - it's slow sort */
	    if (delta[s] > delta[k]) 
	      {
		register long tmp;
		
		tmp      = delta[k];
		delta[k] = delta[s];
		delta[s] = tmp;
	      }
	  }

      i = 0;
      n = PARSE_DELTA;

      /*
       * you know this median loop if you have read the other code
       */
      while ((n - i) > 8)
	{
	  register long top = delta[n-1];
	  register long mid = delta[(n+i)>>1];
	  register long low = delta[i];
	  
	  if ((top - mid) > (mid - low))
	    {
	      /*
	       * cut off high end
	       */
	      n--;
	    }
	  else
	    {
	      /*
	       * cut off low end
	       */
	      i++;
	    }
	}
      
      parseio->parse_dtime.parse_usecdisp  = delta[n-1] - delta[i];

      if (parseio->parse_flags & PARSE_STAT_AVG)
	{
	  /*
	   * take the average of the median samples as this clock
	   * is a little bumpy
	   */
	  mean = 0;

	  while (i < n)
	    {
	      mean += delta[i++];
	    }

	  mean >>= 3;
	}
      else
	{
	  mean = delta[(n+i)>>1];
	}
      
      parseio->parse_dtime.parse_usecerror = mean;
    }
  else
    {
      parseio->parse_dtime.parse_usecerror = usecoff;
      parseio->parse_dtime.parse_usecdisp  = 0;
    }
  
  parseprintf(DD_PARSE,("parse: updatetimeinfo: T=%x+%d usec, useccoff=%d, usecerror=%d, usecdisp=%d\n",
			(int)t, (int)usec, (int)usecoff, (int)parseio->parse_dtime.parse_usecerror,
			(int)parseio->parse_dtime.parse_usecdisp));
  

#ifdef PARSEKERNEL
  {
    int s = splhigh();
#endif
  
    parseio->parse_lstate          = parseio->parse_dtime.parse_state | flags | PARSEB_TIMECODE;
    
    parseio->parse_dtime.parse_state = parseio->parse_lstate;

#ifdef PARSEKERNEL
    (void)splx(s);
  }
#endif
  
  return CVT_OK;		/* everything fine and dandy... */
}


/*
 * syn_simple
 *
 * handle a sync time stamp
 */
/*ARGSUSED*/
void
syn_simple(parseio, ts, format, why)
  register parse_t *parseio;
  register timestamp_t *ts;
  register struct format *format;
  register u_long why;
{
  parseio->parse_dtime.parse_stime = *ts;
}

/*
 * pps_simple
 *
 * handle a pps time stamp
 */
/*ARGSUSED*/
u_long
pps_simple(parseio, status, ptime)
  register parse_t *parseio;
  register int status;
  register timestamp_t *ptime;
{
  parseio->parse_dtime.parse_ptime  = *ptime;
  parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
  
  return CVT_NONE;
}

/*
 * timepacket
 *
 * process a data packet
 */
static u_long
timepacket(parseio)
  register parse_t *parseio;
{
  register int k;
  register unsigned short format;
  register time_t t;
  register u_long cvtsum = 0;/* accumulated CVT_FAIL errors */
  u_long cvtrtc;		/* current conversion result */
  clocktime_t clock;
  
  bzero(&clock, sizeof clock);
  format = parseio->parse_lformat;

  k = 0;

  if (parseio->parse_flags & PARSE_FIXED_FMT)
    {
      switch ((cvtrtc = clockformats[format]->convert ? clockformats[format]->convert(parseio->parse_data, parseio->parse_index, clockformats[format]->data, &clock, parseio->parse_pdata) : CVT_NONE) & CVT_MASK)
	{
	case CVT_FAIL:
	  parseio->parse_badformat++;
	  cvtsum = cvtrtc & ~CVT_MASK;
	      
	  /*
	   * may be too often ... but is nice to know when it happens
	   */
#ifdef PARSEKERNEL
	  printf("parse: \"%s\" failed to convert\n", clockformats[format]->name);
#else
	  syslog(LOG_WARNING, "parse: \"%s\" failed to convert\n", clockformats[format]->name);
#endif
	  break;
	  
	case CVT_NONE:
	  /*
	   * too bad - pretend bad format
	   */
	  parseio->parse_badformat++;
	  cvtsum = CVT_BADFMT;
	  break;
	  
	case CVT_OK:
	  k = 1;
	  break;

	case CVT_SKIP:
	  k = 2;
	  break;

	default:
	  /* shouldn't happen */
#ifdef PARSEKERNEL
	  printf("parse: INTERNAL error: bad return code of convert routine \"%s\"\n", clockformats[format]->name);
#else
	  syslog(LOG_WARNING, "parse: INTERNAL error: bad return code of convert routine \"%s\"\n", clockformats[format]->name);
#endif	  
	  return CVT_FAIL|cvtrtc;
	}
    }
  else
    {
      /*
       * find correct conversion routine
       * and convert time packet
       * RR search starting at last successful conversion routine
       */
  
      if (nformats)		/* very careful ... */
	{
	  do
	    {
	      switch ((cvtrtc = (clockformats[format]->convert && !(clockformats[format]->flags & CVT_FIXEDONLY)) ?
		       clockformats[format]->convert(parseio->parse_data, parseio->parse_index, clockformats[format]->data, &clock, parseio->parse_pdata) :
		       CVT_NONE) & CVT_MASK)
		{
		case CVT_FAIL:
		  parseio->parse_badformat++;
		  cvtsum |= cvtrtc & ~CVT_MASK;
	      
		  /*
		   * may be too often ... but is nice to know when it happens
		   */
#ifdef PARSEKERNEL
		  printf("parse: \"%s\" failed to convert\n", clockformats[format]->name);
#else		  
		  syslog(LOG_WARNING, "parse: \"%s\" failed to convert\n", clockformats[format]->name);
#endif
		  /*FALLTHROUGH*/
		case CVT_NONE:
		  format++;
		  break;
	  
		case CVT_OK:
		  k = 1;
		  break;

		default:
		  /* shouldn't happen */
#ifdef PARSEKERNEL
		  printf("parse: INTERNAL error: bad return code of convert routine \"%s\"\n", clockformats[format]->name);
#else
		  syslog(LOG_WARNING, "parse: INTERNAL error: bad return code of convert routine \"%s\"\n", clockformats[format]->name);
#endif
		  return CVT_BADFMT;
		}
	      if (format >= nformats)
		format = 0;
	    }
	  while (!k && (format != parseio->parse_lformat));
	}
    }

  if (!k)
    {
#ifdef PARSEKERNEL
      printf("parse: time format \"%s\" not convertable\n", parseio->parse_data);
#else
      syslog(LOG_WARNING, "parse: time format \"%s\" not convertable\n", parseio->parse_data);
#endif
      return CVT_FAIL|cvtsum;
    }
  
  if (k == 2) return CVT_OK;

  if ((t = parse_to_unixtime(&clock, &cvtrtc)) == -1)
    {
#ifdef PARSEKERNEL
      printf("parse: bad time format \"%s\"\n", parseio->parse_data);
#else
      syslog(LOG_WARNING,"parse: bad time format \"%s\"\n", parseio->parse_data);
#endif
      return CVT_FAIL|cvtrtc;
    }
  
  parseio->parse_lformat = format;

  /*
   * time stamp
   */
#ifdef PARSEKERNEL
  parseio->parse_dtime.parse_time.tv.tv_sec  = t;
  parseio->parse_dtime.parse_time.tv.tv_usec = clock.usecond;
#else
  parseio->parse_dtime.parse_time.fp.l_ui = t + JAN_1970;
  TVUTOTSF(clock.usecond, parseio->parse_dtime.parse_time.fp.l_uf);
#endif

  parseio->parse_dtime.parse_format       = format;

  return updatetimeinfo(parseio, t, clock.usecond, clock.flags);
}


/*
 * control operations
 */
/*ARGSUSED*/
int
parse_getstat(dct, parse)
  parsectl_t *dct;
  parse_t    *parse;
{
  dct->parsestatus.flags = parse->parse_flags & PARSE_STAT_FLAGS;
  return 1;
}

		  
/*ARGSUSED*/
int
parse_setstat(dct, parse)
  parsectl_t *dct;
  parse_t    *parse;
{
  parse->parse_flags = (parse->parse_flags & ~PARSE_STAT_FLAGS) | dct->parsestatus.flags;
  return 1;
}

		  
/*ARGSUSED*/
int
parse_timecode(dct, parse)
  parsectl_t *dct;
  parse_t    *parse;
{
  dct->parsegettc.parse_state  = parse->parse_lstate;
  dct->parsegettc.parse_format = parse->parse_lformat;
  /*
   * move out current bad packet count
   * user program is expected to sum these up
   * this is not a problem, as "parse" module are
   * exclusive open only
   */
  dct->parsegettc.parse_badformat = parse->parse_badformat;
  parse->parse_badformat = 0;
		  
  if (parse->parse_ldsize <= PARSE_TCMAX)
    {
      dct->parsegettc.parse_count = parse->parse_ldsize;
      bcopy(parse->parse_ldata, dct->parsegettc.parse_buffer, dct->parsegettc.parse_count);
      return 1;
    }
  else
    {
      return 0;
    }
}

		  
/*ARGSUSED*/
int
parse_setfmt(dct, parse)
  parsectl_t *dct;
  parse_t    *parse;
{
  if (dct->parseformat.parse_count <= PARSE_TCMAX)
    {
      if (dct->parseformat.parse_count)
	{
	  register unsigned short i;

	  for (i = 0; i < nformats; i++)
	    {
	      if (!Strcmp(dct->parseformat.parse_buffer, clockformats[i]->name))
		{
		  parse->parse_lformat  = i;
		  parse->parse_flags   |= PARSE_FIXED_FMT; /* set fixed format indication */
		  return setup_bitmaps(parse, i, i+1);
		}
	    }

	  return 0;
	}
      else
	{
	  parse->parse_flags &= ~PARSE_FIXED_FMT; /* clear fixed format indication */
	  return setup_bitmaps(parse, 0, nformats);
	}
    }
  else
    {
      return 0;
    }
}

/*ARGSUSED*/
int
parse_getfmt(dct, parse)
  parsectl_t *dct;
  parse_t    *parse;
{
  if (dct->parseformat.parse_format < nformats &&
      Strlen(clockformats[dct->parseformat.parse_format]->name) <= PARSE_TCMAX)
    {
      dct->parseformat.parse_count = Strlen(clockformats[dct->parseformat.parse_format]->name)+1;
      bcopy(clockformats[dct->parseformat.parse_format]->name, dct->parseformat.parse_buffer, dct->parseformat.parse_count);
      return 1;
    }
  else
    {
      return 0;
    }
}

/*ARGSUSED*/
int
parse_setcs(dct, parse)
  parsectl_t *dct;
  parse_t    *parse;
{
  parse->parse_ioflags &= ~PARSE_IO_CSIZE;
  parse->parse_ioflags |= dct->parsesetcs.parse_cs & PARSE_IO_CSIZE;
  return 1;
}

#endif /* defined(REFCLOCK) && defined(PARSE) */

/*
 * History:
 *
 * parse.c,v
 * Revision 3.27  1994/06/01  08:18:33  kardel
 * more debug info
 *
 * Revision 3.26  1994/05/30  10:20:07  kardel
 * LONG cleanup
 *
 * Revision 3.25  1994/05/12  12:49:12  kardel
 * printf fmt/arg cleanup
 *
 * Revision 3.24  1994/03/27  15:01:36  kardel
 * reorder include file to cope with PTX
 *
 * Revision 3.23  1994/03/25  13:09:02  kardel
 * considering FIXEDONLY entries only in FIXEDONLY mode
 *
 * Revision 3.22  1994/02/25  12:34:49  kardel
 * allow for converter generated utc times
 *
 * Revision 3.21  1994/02/02  17:45:30  kardel
 * rcs ids fixed
 *
 * Revision 3.19  1994/01/25  19:05:20  kardel
 * 94/01/23 reconcilation
 *
 * Revision 3.18  1994/01/23  17:21:59  kardel
 * 1994 reconcilation
 *
 * Revision 3.17  1993/11/11  11:20:29  kardel
 * declaration fixes
 *
 * Revision 3.16  1993/11/06  22:26:07  duwe
 * Linux cleanup after config change
 *
 * Revision 3.15  1993/11/04  11:14:18  kardel
 * ansi/K&R traps
 *
 * Revision 3.14  1993/11/04  10:03:28  kardel
 * disarmed ansiism
 *
 * Revision 3.13  1993/11/01  20:14:13  kardel
 * useless comparision removed
 *
 * Revision 3.12  1993/11/01  20:00:22  kardel
 * parse Solaris support (initial version)
 *
 * Revision 3.11  1993/10/30  09:41:25  kardel
 * minor optimizations
 *
 * Revision 3.10  1993/10/22  14:27:51  kardel
 * Oct. 22nd 1993 reconcilation
 *
 * Revision 3.9  1993/10/05  23:15:09  kardel
 * more STREAM protection
 *
 * Revision 3.8  1993/09/27  21:08:00  kardel
 * utcoffset now in seconds
 *
 * Revision 3.7  1993/09/26  23:40:16  kardel
 * new parse driver logic
 *
 * Revision 3.6  1993/09/07  10:12:46  kardel
 * September 7th reconcilation - 3.2 (alpha)
 *
 * Revision 3.5  1993/09/01  21:44:48  kardel
 * conditional cleanup
 *
 * Revision 3.4  1993/08/27  00:29:39  kardel
 * compilation cleanup
 *
 * Revision 3.3  1993/08/24  22:27:13  kardel
 * cleaned up AUTOCONF DCF77 mess 8-) - wasn't too bad
 *
 * Revision 3.2  1993/07/09  11:37:11  kardel
 * Initial restructured version + GPS support
 *
 * Revision 3.1  1993/07/06  10:00:08  kardel
 * DCF77 driver goes generic...
 *
 */
OpenPOWER on IntegriCloud