~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to ntpd/refclock_irig.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
 * o The modulation index is less than MODMIN (0.5). This usually means
76
76
 *   overdriven IRIG signal or wrong IRIG format.
77
77
 *
78
 
 * o A frame synchronization error has occurred. This usually means wrong
79
 
 *   IRIG signal format or the IRIG signal source has lost
 
78
 * o A frame synchronization error has occurred. This usually means
 
79
 *   wrong IRIG signal format or the IRIG signal source has lost
80
80
 *   synchronization (signature control).
81
81
 *
82
82
 * o A data decoding error has occurred. This usually means wrong IRIG
97
97
 *
98
98
 * The timecode format used for debugging and data recording includes
99
99
 * data helpful in diagnosing problems with the IRIG signal and codec
100
 
 * connections. With debugging enabled (-d -d -d on the ntpd command
101
 
 * line), the driver produces one line for each timecode in the
102
 
 * following format:
 
100
 * connections. With debugging enabled (-d on the ntpd command line),
 
101
 * the driver produces one line for each timecode in the following
 
102
 * format:
103
103
 *
104
 
 * 00 1 98 23 19:26:52 721 143 0.694 47 20 0.083 66.5 3094572411.00027
 
104
 * 00 1 98 23 19:26:52 721 143 0.694 20 0.1 66.5 3094572411.00027
105
105
 *
106
106
 * The most recent line is also written to the clockstats file at 64-s
107
107
 * intervals.
111
111
 * indicator, year of century, day of year and time of day. The status
112
112
 * indicator and year are not produced by some IRIG devices. Following
113
113
 * these fields are the signal amplitude (0-8100), codec gain (0-255),
114
 
 * field phase (0-79), time constant (2-20), modulation index (0-1),
115
 
 * carrier phase error (0+-0.5) and carrier frequency error (PPM). The
116
 
 * last field is the on-time timestamp in NTP format.
 
114
 * modulation index (0-1), time constant (2-20), carrier phase error
 
115
 * (us) and carrier frequency error (PPM). The last field is the on-time
 
116
 * timestamp in NTP format.
117
117
 *
118
118
 * The fraction part of the on-time timestamp is a good indicator of how
119
 
 * well the driver is doing. With an UltrSPARC 30, this thing can keep
120
 
 * the clock within a few tens of microseconds relative to the IRIG-B
121
 
 * signal. Accuracy with IRIG-E is about ten times worse.
 
119
 * well the driver is doing. With an UltrSPARC 30 and Solaris 2.7, this
 
120
 * thing can keep the clock within a few tens of microseconds relative
 
121
 * to the IRIG-B signal. Accuracy with IRIG-E is about ten times worse.
 
122
 * Unfortunately, Sun broke the 2.7 audio driver in 2.8, which has a
 
123
 * 10-ms sawtooth modulation. The driver attempts to remove the
 
124
 * modulation by some clever estimation techniques which mostly work.
 
125
 * Your experience may vary.
122
126
 *
123
127
 * Unlike other drivers, which can have multiple instantiations, this
124
128
 * one supports only one. It does not seem likely that more than one
127
131
 *
128
132
 * Fudge factors
129
133
 *
130
 
 * Fudge flag2 selects the audio input port, where 0 is the mike port
 
134
 * Fudge flag4 causes the dubugging output described above to be
 
135
 * recorded in the clockstats file. When the audio driver is compiled,
 
136
 * fudge flag2 selects the audio input port, where 0 is the mike port
131
137
 * (default) and 1 is the line-in port. It does not seem useful to
132
138
 * select the compact disc player port. Fudge flag3 enables audio
133
 
 * monitoring of the input signal. For this purpose, the speaker volume
134
 
 * must be set before the driver is started. Fudge flag4 causes the
135
 
 * debugging output described above to be recorded in the clockstats
136
 
 * file. Any of these flags can be changed during operation with the
137
 
 * ntpdc program.
 
139
 * monitoring of the input signal. For this purpose, the monitor gain is
 
140
 * set to a default value. Fudgetime2 is used as a frequency vernier for
 
141
 * broken codec sample frequency.
138
142
 */
139
 
 
140
143
/*
141
144
 * Interface definitions
142
145
 */
144
147
#define PRECISION       (-17)   /* precision assumed (about 10 us) */
145
148
#define REFID           "IRIG"  /* reference ID */
146
149
#define DESCRIPTION     "Generic IRIG Audio Driver" /* WRU */
147
 
 
 
150
#define AUDIO_BUFSIZ    320     /* audio buffer size (40 ms) */
148
151
#define SECOND          8000    /* nominal sample rate (Hz) */
149
152
#define BAUD            80      /* samples per baud interval */
150
153
#define OFFSET          128     /* companded sample offset */
155
158
#define MINTC           2       /* min PLL time constant */
156
159
#define MAXTC           20      /* max PLL time constant max */
157
160
#define MAXSIG          6000.   /* maximum signal level */
 
161
#define MAXCLP          100     /* max clips above reference per s */
158
162
#define DRPOUT          100.    /* dropout signal level */
159
163
#define MODMIN          0.5     /* minimum modulation index */
160
164
#define MAXFREQ         (250e-6 * SECOND) /* freq tolerance (.025%) */
161
165
#define PI              3.1415926535 /* the real thing */
 
166
#ifdef IRIG_SUCKS
 
167
#define WIGGLE          11      /* wiggle filter length */
 
168
#endif /* IRIG_SUCKS */
162
169
 
163
170
/*
164
 
 * Experimentally determined fudge factors
 
171
 * Experimentally determined filter delays
165
172
 */
166
 
#define IRIG_B          .0019           /* IRIG-B phase delay */
167
 
#define IRIG_E          .0019           /* IRIG-E phase delay */
 
173
#define IRIG_B          .0019   /* IRIG-B filter delay */
 
174
#define IRIG_E          .0019   /* IRIG-E filter delay */
168
175
 
169
176
/*
170
177
 * Data bit definitions
183
190
#define IRIG_ERR_DECODE 0x10    /* frame decoding error */
184
191
#define IRIG_ERR_CHECK  0x20    /* second numbering discrepancy */
185
192
#define IRIG_ERR_ERROR  0x40    /* codec error (overrun) */
 
193
#define IRIG_ERR_SIGERR 0x80    /* IRIG status error (Spectracom) */
186
194
 
187
195
/*
188
196
 * IRIG unit control structure
191
199
        u_char  timecode[21];   /* timecode string */
192
200
        l_fp    timestamp;      /* audio sample timestamp */
193
201
        l_fp    tick;           /* audio sample increment */
194
 
        double  comp[SIZE];     /* decompanding table */
195
202
        double  integ[BAUD];    /* baud integrator */
196
203
        double  phase, freq;    /* logical clock phase and frequency */
197
204
        double  zxing;          /* phase detector integrator */
198
 
        double  yxing;          /* phase detector display */
 
205
        double  yxing;          /* cycle phase */
 
206
        double  exing;          /* envelope phase */
199
207
        double  modndx;         /* modulation index */
200
208
        double  irig_b;         /* IRIG-B signal amplitude */
201
209
        double  irig_e;         /* IRIG-E signal amplitude */
202
210
        int     errflg;         /* error flags */
203
 
        int     bufcnt;         /* samples in buffer */
204
 
        int     bufptr;         /* buffer index pointer */
205
 
        int     pollcnt;        /* poll counter */
 
211
        /*
 
212
         * Audio codec variables
 
213
         */
 
214
        double  comp[SIZE];     /* decompanding table */
206
215
        int     port;           /* codec port */
207
216
        int     gain;           /* codec gain */
 
217
        int     mongain;        /* codec monitor gain */
208
218
        int     clipcnt;        /* sample clipped count */
209
219
        int     seccnt;         /* second interval counter */
210
 
        int     decim;          /* sample decimation factor */
211
220
 
212
221
        /*
213
222
         * RF variables
222
231
        double  lastenv[CYCLE]; /* last cycle amplitudes */
223
232
        double  lastint[CYCLE]; /* last integrated cycle amplitudes */
224
233
        double  lastsig;        /* last carrier sample */
225
 
        double  xxing;          /* phase detector interpolated output */
226
234
        double  fdelay;         /* filter delay */
 
235
        int     decim;          /* sample decimation factor */
227
236
        int     envphase;       /* envelope phase */
228
237
        int     envptr;         /* envelope phase pointer */
229
238
        int     carphase;       /* carrier phase */
236
245
        /*
237
246
         * Decoder variables
238
247
         */
239
 
        l_fp    montime;        /* reference timestamp for eyeball */
240
 
        int     timecnt;        /* timecode counter */
241
248
        int     pulse;          /* cycle counter */
242
249
        int     cycles;         /* carrier cycles */
243
250
        int     dcycles;        /* data cycles */
247
254
        int     fieldcnt;       /* subfield count in field */
248
255
        int     bits;           /* demodulated bits */
249
256
        int     bitcnt;         /* bit count in subfield */
 
257
#ifdef IRIG_SUCKS
 
258
        l_fp    wigwag;         /* wiggle accumulator */
 
259
        int     wp;             /* wiggle filter pointer */
 
260
        l_fp    wiggle[WIGGLE]; /* wiggle filter */
 
261
        l_fp    wigbot[WIGGLE]; /* wiggle bottom fisher*/
 
262
#endif /* IRIG_SUCKS */
 
263
        l_fp    wuggle;
250
264
};
251
265
 
252
266
/*
282
296
 * Global variables
283
297
 */
284
298
static char     hexchar[] = {   /* really quick decoding table */
285
 
        '0', '8', '4', 'c',             /* 0000 0001 0010 0011 */
286
 
        '2', 'a', '6', 'e',             /* 0100 0101 0110 0111 */
287
 
        '1', '9', '5', 'd',             /* 1000 1001 1010 1011 */
288
 
        '3', 'b', '7', 'f'              /* 1100 1101 1110 1111 */
 
299
        '0', '8', '4', 'c',     /* 0000 0001 0010 0011 */
 
300
        '2', 'a', '6', 'e',     /* 0100 0101 0110 0111 */
 
301
        '1', '9', '5', 'd',     /* 1000 1001 1010 1011 */
 
302
        '3', 'b', '7', 'f'      /* 1100 1101 1110 1111 */
289
303
};
290
304
 
291
305
 
294
308
 */
295
309
static int
296
310
irig_start(
297
 
        int     unit,           /* instance number (not used) */
 
311
        int     unit,           /* instance number (used for PCM) */
298
312
        struct peer *peer       /* peer structure pointer */
299
313
        )
300
314
{
311
325
        /*
312
326
         * Open audio device
313
327
         */
314
 
        fd = audio_init(DEVICE_AUDIO);
 
328
        fd = audio_init(DEVICE_AUDIO, AUDIO_BUFSIZ, unit);
315
329
        if (fd < 0)
316
330
                return (0);
317
331
#ifdef DEBUG
350
364
        up->decim = 1;
351
365
        up->fdelay = IRIG_B;
352
366
        up->gain = 127;
353
 
        up->pollcnt = 2;
354
367
 
355
368
        /*
356
369
         * The companded samples are encoded sign-magnitude. The table
364
377
                up->comp[i] = up->comp[i - 1] + step;
365
378
                up->comp[OFFSET + i] = -up->comp[i];
366
379
                if (i % 16 == 0)
367
 
                    step *= 2.;
 
380
                        step *= 2.;
368
381
        }
369
382
        DTOLFP(1. / SECOND, &up->tick);
370
383
        return (1);
410
423
         */
411
424
        double  sample;         /* codec sample */
412
425
        u_char  *dpt;           /* buffer pointer */
 
426
        int     bufcnt;         /* buffer counter */
413
427
        l_fp    ltemp;          /* l_fp temp */
414
428
 
415
429
        peer = (struct peer *)rbufp->recv_srcclock;
420
434
         * Main loop - read until there ain't no more. Note codec
421
435
         * samples are bit-inverted.
422
436
         */
 
437
        DTOLFP((double)rbufp->recv_length / SECOND, &ltemp);
 
438
        L_SUB(&rbufp->recv_time, &ltemp);
423
439
        up->timestamp = rbufp->recv_time;
424
 
        up->bufcnt = rbufp->recv_length;
425
 
        DTOLFP((double)up->bufcnt / SECOND, &ltemp);
426
 
        L_SUB(&up->timestamp, &ltemp);
427
440
        dpt = rbufp->recv_buffer;
428
 
        for (up->bufptr = 0; up->bufptr < up->bufcnt; up->bufptr++) {
 
441
        for (bufcnt = 0; bufcnt < rbufp->recv_length; bufcnt++) {
429
442
                sample = up->comp[~*dpt++ & 0xff];
430
443
 
431
444
                /*
432
445
                 * Clip noise spikes greater than MAXSIG. If no clips,
433
446
                 * increase the gain a tad; if the clips are too high, 
434
 
                 * decrease a tad. Choose either IRIG-B or IRIG-E
435
 
                 * according to the energy at the respective filter
436
 
                 * output.
 
447
                 * decrease a tad.
437
448
                 */
438
449
                if (sample > MAXSIG) {
439
450
                        sample = MAXSIG;
444
455
                }
445
456
 
446
457
                /*
447
 
                 * Variable frequency oscillator. A phase change of one
448
 
                 * unit produces a change of 360 degrees; a frequency
449
 
                 * change of one unit produces a change of 1 Hz.
 
458
                 * Variable frequency oscillator. The codec oscillator
 
459
                 * runs at the nominal rate of 8000 samples per second,
 
460
                 * or 125 us per sample. A frequency change of one unit
 
461
                 * results in either duplicating or deleting one sample
 
462
                 * per second, which results in a frequency change of
 
463
                 * 125 PPM.
450
464
                 */
451
465
                up->phase += up->freq / SECOND;
 
466
                up->phase += pp->fudgetime2 / 1e6;
452
467
                if (up->phase >= .5) {
453
468
                        up->phase -= 1.;
454
469
                } else if (up->phase < -.5) {
461
476
                L_ADD(&up->timestamp, &up->tick);
462
477
 
463
478
                /*
464
 
                 * Once each second, determine the IRIG format, codec
465
 
                 * port and gain.
 
479
                 * Once each second, determine the IRIG format and gain.
466
480
                 */
467
481
                up->seccnt = (up->seccnt + 1) % SECOND;
468
482
                if (up->seccnt == 0) {
473
487
                                up->decim = 10;
474
488
                                up->fdelay = IRIG_E;
475
489
                        }
476
 
                        if (pp->sloppyclockflag & CLK_FLAG2)
477
 
                            up->port = 2;
478
 
                        else
479
 
                            up->port = 1;
480
490
                        irig_gain(peer);
481
491
                        up->irig_b = up->irig_e = 0;
482
492
                }
483
493
        }
484
494
 
485
495
        /*
486
 
         * Squawk to the monitor speaker if enabled.
 
496
         * Set the input port and monitor gain for the next buffer.
487
497
         */
 
498
        if (pp->sloppyclockflag & CLK_FLAG2)
 
499
                up->port = 2;
 
500
        else
 
501
                up->port = 1;
488
502
        if (pp->sloppyclockflag & CLK_FLAG3)
489
 
            if (write(pp->io.fd, (u_char *)&rbufp->recv_space,
490
 
                      (u_int)up->bufcnt) < 0)
491
 
                perror("irig:");
 
503
                up->mongain = MONGAIN;
 
504
        else
 
505
                up->mongain = 0;
492
506
}
493
507
 
494
508
/*
521
535
 
522
536
        /*
523
537
         * IRIG-B filter. 4th-order elliptic, 800-Hz highpass, 0.3 dB
524
 
         * passband ripple, -50 dB stopband ripple, phase delay -.0022
 
538
         * passband ripple, -50 dB stopband ripple, phase delay .0022
525
539
         * s)
526
540
         */
527
541
        irig_b = (up->hpf[4] = up->hpf[3]) * 2.322484e-01;
558
572
        up->badcnt = (up->badcnt + 1) % up->decim;
559
573
        if (up->badcnt == 0) {
560
574
                if (up->decim == 1)
561
 
                    irig_base(peer, irig_b);
 
575
                        irig_base(peer, irig_b);
562
576
                else
563
 
                    irig_base(peer, irig_e);
 
577
                        irig_base(peer, irig_e);
564
578
        }
565
579
}
566
580
 
583
597
        /*
584
598
         * Local variables
585
599
         */
 
600
        double  xxing;          /* phase detector interpolated output */
586
601
        double  lope;           /* integrator output */
587
602
        double  env;            /* envelope detector output */
588
603
        double  dtemp;          /* double temp */
589
 
        int     i;              /* index temp */
590
604
 
591
605
        pp = peer->procptr;
592
606
        up = (struct irigunit *)pp->unitptr;
612
626
         * change of 360 degrees produces an output change of one unit.
613
627
         */ 
614
628
        if (up->lastsig > 0 && lope <= 0) {
615
 
                up->xxing = lope / (up->lastsig - lope);
616
 
                up->zxing += (up->carphase - 4 + up->xxing) / 8.;
 
629
                xxing = lope / (up->lastsig - lope);
 
630
                up->zxing += (up->carphase - 4 + xxing) / CYCLE;
617
631
        }
618
632
        up->lastsig = lope;
619
633
 
629
643
                up->maxsignal = up->intmax;
630
644
                up->noise = up->intmin;
631
645
                if (up->maxsignal < DRPOUT)
632
 
                    up->errflg |= IRIG_ERR_AMP;
633
 
                if (up->intmax > 0)
634
 
                    up->modndx = (up->intmax - up->intmin) / up->intmax;
 
646
                        up->errflg |= IRIG_ERR_AMP;
 
647
                if (up->maxsignal > 0)
 
648
                        up->modndx = (up->intmax - up->intmin) /
 
649
                            up->intmax;
635
650
                else
636
 
                    up->modndx = 0;
 
651
                        up->modndx = 0;
637
652
                if (up->modndx < MODMIN)
638
 
                    up->errflg |= IRIG_ERR_MOD;
 
653
                        up->errflg |= IRIG_ERR_MOD;
639
654
                up->intmin = 1e6; up->intmax = 0;
640
655
                if (up->errflg & (IRIG_ERR_AMP | IRIG_ERR_FREQ |
641
 
                                  IRIG_ERR_MOD | IRIG_ERR_SYNCH)) {
 
656
                   IRIG_ERR_MOD | IRIG_ERR_SYNCH)) {
642
657
                        up->tc = MINTC;
643
658
                        up->tcount = 0;
644
659
                }
674
689
         * the slice level and left-shifted in the decoding register.
675
690
         */
676
691
        if (up->carphase != 7)
677
 
            return;
 
692
                return;
678
693
        env = (up->lastenv[2] - up->lastenv[6]) / 2.;
679
694
        lope = (up->lastint[2] - up->lastint[6]) / 2.;
680
695
        if (lope > up->intmax)
681
 
            up->intmax = lope;
 
696
                up->intmax = lope;
682
697
        if (lope < up->intmin)
683
 
            up->intmin = lope;
 
698
                up->intmin = lope;
684
699
 
685
700
        /*
686
701
         * Pulse code demodulator and reference timestamp. The decoder
690
705
         */
691
706
        up->pulse = (up->pulse + 1) % 10;
692
707
        if (up->pulse == 1)
693
 
            up->envmax = env;
 
708
                up->envmax = env;
694
709
        else if (up->pulse == 9)
695
 
            up->envmin = env;
 
710
                up->envmin = env;
696
711
        up->dcycles <<= 1;
697
712
        if (env >= (up->envmax + up->envmin) / 2.)
698
 
            up->dcycles |= 1;
 
713
                up->dcycles |= 1;
699
714
        up->cycles <<= 1;
700
715
        if (lope >= (up->maxsignal + up->noise) / 2.)
701
 
            up->cycles |= 1;
 
716
                up->cycles |= 1;
702
717
        if ((up->cycles & 0x303c0f03) == 0x300c0300) {
703
718
                l_fp ltemp;
704
719
                int bitz;
711
726
                 * persist for lots of samples.
712
727
                 */
713
728
                if (up->pulse != 9)
714
 
                    up->errflg |= IRIG_ERR_SYNCH;
 
729
                        up->errflg |= IRIG_ERR_SYNCH;
715
730
                up->pulse = 9;
716
 
                dtemp = BAUD - up->zxing;
717
 
                i = up->envxing - up->envphase;
718
 
                if (i < 0)
719
 
                    i -= i;
720
 
                if (i <= 1) {
 
731
                up->exing = -up->yxing;
 
732
                if (fabs(up->envxing - up->envphase) <= 1) {
721
733
                        up->tcount++;
722
734
                        if (up->tcount > 50 * up->tc) {
723
735
                                up->tc++;
724
736
                                if (up->tc > MAXTC)
725
 
                                    up->tc = MAXTC;
 
737
                                        up->tc = MAXTC;
726
738
                                up->tcount = 0;
727
739
                                up->envxing = up->envphase;
728
740
                        } else {
729
 
                                dtemp -= up->envxing - up->envphase;
 
741
                                up->exing -= up->envxing - up->envphase;
730
742
                        }
731
743
                } else {
732
744
                        up->tcount = 0;
740
752
                 * this plus the delay since the last carrier positive
741
753
                 * zero crossing.
742
754
                 */
743
 
                DTOLFP(up->decim * (dtemp / SECOND + 1.) + up->fdelay,
744
 
                       &ltemp);
 
755
                dtemp = up->decim * ((up->exing + BAUD) / SECOND + 1.) +
 
756
                    up->fdelay;
 
757
                DTOLFP(dtemp, &ltemp);
745
758
                pp->lastrec = up->timestamp;
746
759
                L_SUB(&pp->lastrec, &ltemp);
747
760
 
756
769
                bitz = up->dcycles & 0xfc;
757
770
                switch(bitz) {
758
771
 
759
 
                    case 0x00:
760
 
                    case 0x80:
 
772
                case 0x00:
 
773
                case 0x80:
761
774
                        irig_decode(peer, BIT0);
762
775
                        break;
763
776
 
764
 
                    case 0xc0:
765
 
                    case 0xe0:
766
 
                    case 0xf0:
 
777
                case 0xc0:
 
778
                case 0xe0:
 
779
                case 0xf0:
767
780
                        irig_decode(peer, BIT1);
768
781
                        break;
769
782
 
770
 
                    case 0xf8:
771
 
                    case 0xfc:
 
783
                case 0xf8:
 
784
                case 0xfc:
772
785
                        irig_decode(peer, BITP);
773
786
                        break;
774
787
 
775
 
                    default:
 
788
                default:
776
789
                        irig_decode(peer, 0);
777
790
                        up->errflg |= IRIG_ERR_DECODE;
778
791
                }
797
810
{
798
811
        struct refclockproc *pp;
799
812
        struct irigunit *up;
 
813
#ifdef IRIG_SUCKS
 
814
        int     i;
 
815
#endif /* IRIG_SUCKS */
800
816
 
801
817
        /*
802
818
         * Local variables
803
819
         */
804
 
        char    syncchar;       /* sync character (Spectracom only) */
 
820
        char    syncchar;       /* sync character (Spectracom) */
805
821
        char    sbs[6];         /* binary seconds since 0h */
806
822
        char    spare[2];       /* mulligan digits */
807
823
 
824
840
                up->bitcnt = 1;
825
841
                up->fieldcnt = 0;
826
842
                up->lastbit = 0;
827
 
                up->montime = pp->lastrec;
828
843
                if (up->errflg == 0) {
829
 
                        up->timecnt++;
830
 
                        refclock_process(pp);
831
 
                }
832
 
                if (up->timecnt >= MAXSTAGE) {
833
 
                        refclock_receive(peer);
834
 
                        up->timecnt = 0;
835
 
                        up->pollcnt = 2;
 
844
#ifdef IRIG_SUCKS
 
845
                        l_fp    ltemp;
 
846
 
 
847
                        /*
 
848
                         * You really don't wanna know what comes down
 
849
                         * here. Leave it to say Solaris 2.8 broke the
 
850
                         * nice clean audio stream, apparently affected
 
851
                         * by a 5-ms sawtooth jitter. Sundown on
 
852
                         * Solaris. This leaves a little twilight.
 
853
                         *
 
854
                         * The scheme involves differentiation, forward
 
855
                         * learning and integration. The sawtooth has a
 
856
                         * period of 11 seconds. The timestamp
 
857
                         * differences are integrated and subtracted
 
858
                         * from the signal.
 
859
                         */
 
860
                        ltemp = pp->lastrec;
 
861
                        L_SUB(&ltemp, &pp->lastref);
 
862
                        if (ltemp.l_f < 0)
 
863
                                ltemp.l_i = -1;
 
864
                        else
 
865
                                ltemp.l_i = 0;
 
866
                        pp->lastref = pp->lastrec;
 
867
                        if (!L_ISNEG(&ltemp))
 
868
                                L_CLR(&up->wigwag);
 
869
                        else
 
870
                                L_ADD(&up->wigwag, &ltemp);
 
871
                        L_SUB(&pp->lastrec, &up->wigwag);
 
872
                        up->wiggle[up->wp] = ltemp;
 
873
 
 
874
                        /*
 
875
                         * Bottom fisher. To understand this, you have
 
876
                         * to know about velocity microphones and AM
 
877
                         * transmitters. No further explanation is
 
878
                         * offered, as this is truly a black art.
 
879
                         */
 
880
                        up->wigbot[up->wp] = pp->lastrec;
 
881
                        for (i = 0; i < WIGGLE; i++) {
 
882
                                if (i != up->wp)
 
883
                                        up->wigbot[i].l_ui++;
 
884
                                L_SUB(&pp->lastrec, &up->wigbot[i]);
 
885
                                if (L_ISNEG(&pp->lastrec))
 
886
                                        L_ADD(&pp->lastrec,
 
887
                                            &up->wigbot[i]);
 
888
                                else
 
889
                                        pp->lastrec = up->wigbot[i];
 
890
                        }
 
891
                        up->wp++;
 
892
                        up->wp %= WIGGLE;
 
893
                        up->wuggle = pp->lastrec;
 
894
                        refclock_process(pp);
 
895
#else /* IRIG_SUCKS */
 
896
                        pp->lastref = pp->lastrec;
 
897
                        up->wuggle = pp->lastrec;
 
898
                        refclock_process(pp);
 
899
#endif /* IRIG_SUCKS */
836
900
                }
837
901
                up->errflg = 0;
838
902
        }
848
912
                if (up->xptr < 2)
849
913
                    up->xptr = 2 * FIELD;
850
914
                up->timecode[--up->xptr] = hexchar[(up->bits >> 5) &
851
 
                                                  0xf];
 
915
                    0xf];
852
916
                up->timecode[--up->xptr] = hexchar[up->bits & 0xf];
853
917
                up->fieldcnt = (up->fieldcnt + 1) % FIELD;
854
918
                if (up->fieldcnt == 0) {
855
919
 
856
920
                        /*
857
 
                         * End of field. Decode the timecode, adjust the
858
 
                         * gain and set the input port. Set the port
859
 
                         * here on the assumption somebody might even
860
 
                         * change it on-wing.
 
921
                         * End of field. Decode the timecode and wind
 
922
                         * the clock. Not all IRIG generators have the
 
923
                         * year; if so, it is nonzero after year 2000.
 
924
                         * Not all have the hardware status bit; if so,
 
925
                         * it is lit when the source is okay and dim
 
926
                         * when bad. We watch this only if the year is
 
927
                         * nonzero. Not all are configured for signature
 
928
                         * control. If so, all BCD digits are set to
 
929
                         * zero if the source is bad. In this case the
 
930
                         * refclock_process() will reject the timecode
 
931
                         * as invalid.
861
932
                         */
862
933
                        up->xptr = 2 * FIELD;
863
934
                        if (sscanf((char *)up->timecode,
864
 
                                   "%6s%2d%c%2s%3d%2d%2d%2d",
865
 
                                   sbs, &pp->year, &syncchar, spare, &pp->day,
866
 
                                   &pp->hour, &pp->minute, &pp->second) != 8)
867
 
                            pp->leap = LEAP_NOTINSYNC;
 
935
                           "%6s%2d%c%2s%3d%2d%2d%2d", sbs, &pp->year,
 
936
                            &syncchar, spare, &pp->day, &pp->hour,
 
937
                            &pp->minute, &pp->second) != 8)
 
938
                                pp->leap = LEAP_NOTINSYNC;
868
939
                        else
869
 
                            pp->leap = LEAP_NOWARNING;
 
940
                                pp->leap = LEAP_NOWARNING;
870
941
                        up->second = (up->second + up->decim) % 60;
 
942
                        if (pp->year > 0) {
 
943
                                pp->year += 2000;
 
944
                                if (syncchar == '0')
 
945
                                        up->errflg |= IRIG_ERR_CHECK;
 
946
                        }
871
947
                        if (pp->second != up->second)
872
 
                            up->errflg |= IRIG_ERR_CHECK;
 
948
                                up->errflg |= IRIG_ERR_CHECK;
873
949
                        up->second = pp->second;
874
950
                        sprintf(pp->a_lastcode,
875
 
                                "%02x %c %2d %3d %02d:%02d:%02d %4.0f %3d %6.3f %2d %2d %6.3f %6.1f %s",
876
 
                                up->errflg, syncchar, pp->year, pp->day,
877
 
                                pp->hour, pp->minute, pp->second,
878
 
                                up->maxsignal, up->gain, up->modndx,
879
 
                                up->envxing, up->tc, up->yxing, up->freq *
880
 
                                1e6 / SECOND, ulfptoa(&up->montime, 6));
 
951
                            "%02x %c %2d %3d %02d:%02d:%02d %4.0f %3d %6.3f %2d %6.1f %6.1f %s",
 
952
                            up->errflg, syncchar, pp->year, pp->day,
 
953
                            pp->hour, pp->minute, pp->second,
 
954
                            up->maxsignal, up->gain, up->modndx,
 
955
                            up->tc, up->exing * 1e6 / SECOND, up->freq *
 
956
                            1e6 / SECOND, ulfptoa(&up->wuggle, 6));
881
957
                        pp->lencode = strlen(pp->a_lastcode);
882
 
                        if (up->timecnt == 0 || pp->sloppyclockflag &
883
 
                            CLK_FLAG4)
884
 
                            record_clock_stats(&peer->srcadr,
885
 
                                               pp->a_lastcode);
 
958
                        if (pp->sloppyclockflag & CLK_FLAG4) {
 
959
                                record_clock_stats(&peer->srcadr,
 
960
                                    pp->a_lastcode);
886
961
#ifdef DEBUG
887
 
                        if (debug > 2)
888
 
                            printf("irig: %s\n", pp->a_lastcode);
 
962
                                if (debug)
 
963
                                        printf("irig: %s\n",
 
964
                                            pp->a_lastcode);
889
965
#endif /* DEBUG */
 
966
                        }
890
967
                }
891
968
        }
892
969
        up->lastbit = bit;
896
973
/*
897
974
 * irig_poll - called by the transmit procedure
898
975
 *
899
 
 * This routine keeps track of status. If nothing is heard for two
900
 
 * successive poll intervals, a timeout event is declared and any
901
 
 * orphaned timecode updates are sent to foster care. 
 
976
 * This routine sweeps up the timecode updates since the last poll. For
 
977
 * IRIG-B there should be at least 60 updates; for IRIG-E there should
 
978
 * be at least 6. If nothing is heard, a timeout event is declared and
 
979
 * any orphaned timecode updates are sent to foster care. 
902
980
 */
903
981
static void
904
982
irig_poll(
912
990
        pp = peer->procptr;
913
991
        up = (struct irigunit *)pp->unitptr;
914
992
 
915
 
        /*
916
 
         * Keep book for tattletales
917
 
         */
918
 
        if (up->pollcnt == 0) {
 
993
        if (pp->coderecv == pp->codeproc) {
919
994
                refclock_report(peer, CEVNT_TIMEOUT);
920
 
                up->timecnt = 0;
921
995
                return;
 
996
        } else {
 
997
                refclock_receive(peer);
 
998
                record_clock_stats(&peer->srcadr, pp->a_lastcode);
 
999
#ifdef DEBUG
 
1000
                if (debug)
 
1001
                        printf("irig: %s\n", pp->a_lastcode);
 
1002
#endif /* DEBUG */
922
1003
        }
923
 
        up->pollcnt--;
924
1004
        pp->polls++;
925
1005
        
926
1006
}
953
1033
         */
954
1034
        if (up->clipcnt == 0) {
955
1035
                up->gain += 4;
956
 
                if (up->gain > 255)
957
 
                        up->gain = 255;
958
 
        } else if (up->clipcnt > SECOND / 100) {
 
1036
                if (up->gain > MAXGAIN)
 
1037
                        up->gain = MAXGAIN;
 
1038
        } else if (up->clipcnt > MAXCLP) {
959
1039
                up->gain -= 4;
960
1040
                if (up->gain < 0)
961
1041
                        up->gain = 0;
962
1042
        }
963
 
        audio_gain(up->gain, up->port);
 
1043
        audio_gain(up->gain, up->mongain, up->port);
964
1044
        up->clipcnt = 0;
965
1045
}
966
1046
 
967
 
 
968
1047
#else
969
1048
int refclock_irig_bs;
970
1049
#endif /* REFCLOCK */