2 * refclock_jjy - clock driver for JJY receivers
5 /**********************************************************************/
7 /* Copyright (C) 2001-2020, Takao Abe. All rights reserved. */
9 /* Permission to use, copy, modify, and distribute this software */
10 /* and its documentation for any purpose is hereby granted */
11 /* without fee, provided that the following conditions are met: */
13 /* One retains the entire copyright notice properly, and both the */
14 /* copyright notice and this license. in the documentation and/or */
15 /* other materials provided with the distribution. */
17 /* This software and the name of the author must not be used to */
18 /* endorse or promote products derived from this software without */
19 /* prior written permission. */
21 /* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESSED OR IMPLIED */
22 /* WARRANTIES OF ANY KIND, INCLUDING, BUT NOT LIMITED TO, THE */
23 /* IMPLIED WARRANTIES OF MERCHANTABLILITY AND FITNESS FOR A */
24 /* PARTICULAR PURPOSE. */
25 /* IN NO EVENT SHALL THE AUTHOR TAKAO ABE BE LIABLE FOR ANY DIRECT, */
26 /* INDIRECT, GENERAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
27 /* ( INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
28 /* GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS */
29 /* INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, */
30 /* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ( INCLUDING */
31 /* NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF */
32 /* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34 /* This driver is developed in my private time, and is opened as */
35 /* voluntary contributions for the NTP. */
36 /* The manufacturer of the JJY receiver has not participated in */
37 /* a development of this driver. */
38 /* The manufacturer does not warrant anything about this driver, */
39 /* and is not liable for anything about this driver. */
41 /**********************************************************************/
43 /* Author Takao Abe */
44 /* Email takao_abe@xurb.jp */
45 /* Homepage http://www.bea.hi-ho.ne.jp/abetakao/ */
47 /* The email address abetakao@bea.hi-ho.ne.jp is never read */
48 /* from 2010, because a few filtering rule are provided by the */
49 /* "hi-ho.ne.jp", and lots of spam mail are reached. */
50 /* New email address for supporting the refclock_jjy is */
51 /* takao_abe@xurb.jp */
53 /**********************************************************************/
58 /* [New] Support the Tristate Ltd. JJY receiver */
61 /* [Change] Log to clockstats even if bad reply */
62 /* [Fix] PRECISION = (-3) (about 100 ms) */
63 /* [Add] Support the C-DEX Co.Ltd. JJY receiver */
66 /* [Fix] C-DEX JST2000 ( fukusima@goto.info.waseda.ac.jp ) */
69 /* [Fix] Portability for FreeBSD ( patched by the user ) */
72 /* [Change] Command send timing for the Tristate Ltd. JJY receiver */
73 /* JJY-01 ( Firmware version 2.01 ) */
74 /* Thanks to Andy Taki for testing under FreeBSD */
77 /* [Add] Support the Echo Keisokuki LT-2000 receiver */
80 /* [Fix] C-DEX JST2000 */
81 /* Thanks to Hideo Kuramatsu for the patch */
84 /* [Add] Support the CITIZEN T.I.C JJY-200 receiver */
87 /* [Change] Bug 1618 ( Harmless ) */
88 /* Code clean up ( Remove unreachable codes ) in */
90 /* [Change] Change clockstats format of the Tristate JJY01/02 */
91 /* Issues more command to get the status of the receiver */
92 /* when "fudge 127.127.40.X flag1 1" is specified */
93 /* ( DATE,STIM -> DCST,STUS,DATE,STIM ) */
96 /* [Add] Support the Tristate Ltd. TS-GPSclock-01 */
99 /* [Add] Support the Telephone JJY */
100 /* [Change] Split the start up routine into each JJY receivers. */
101 /* Change raw data internal bufferring process */
102 /* Change over midnight handling of TS-JJY01 and TS-GPS01 */
103 /* to put DATE command between before and after TIME's. */
104 /* Unify the writing clockstats of all JJY receivers. */
107 /* [Add] Support the SEIKO TIME SYSTEMS TDC-300 */
110 /* [Fix] C-DEX JST2000 */
111 /* Thanks to Mr. Kuramatsu for the report and the patch. */
114 /* [Change] Avoid a wrong report of the coverity static analysis */
115 /* tool. ( The code is harmless and has no bug. ) */
116 /* teljjy_conn_send() */
119 /* [Change] Handling TS-JJY01/02 status of the the STUS reply. */
120 /* Time synchronization can be skipped by the settings of */
121 /* the flag2 when the status of the reply is UNADJUSTED. */
122 /* [Change] Quiet compilation for the GCC 9.2.0. */
123 /* [Fix] Correct typos in comment lines */
125 /**********************************************************************/
131 #if defined(REFCLOCK) && defined(CLOCK_JJY)
136 #include <sys/time.h>
142 #include "ntp_refclock.h"
143 #include "ntp_calendar.h"
144 #include "ntp_stdlib.h"
146 /**********************************************************************/
149 * Interface definitions
151 #define DEVICE "/dev/jjy%d" /* device name and unit */
152 #define SPEED232_TRISTATE_JJY01 B9600 /* UART speed (9600 baud) */
153 #define SPEED232_CDEX_JST2000 B9600 /* UART speed (9600 baud) */
154 #define SPEED232_ECHOKEISOKUKI_LT2000 B9600 /* UART speed (9600 baud) */
155 #define SPEED232_CITIZENTIC_JJY200 B4800 /* UART speed (4800 baud) */
156 #define SPEED232_TRISTATE_GPSCLOCK01 B38400 /* USB speed (38400 baud) */
157 #define SPEED232_SEIKO_TIMESYS_TDC_300 B2400 /* UART speed (2400 baud) */
158 #define SPEED232_TELEPHONE B2400 /* UART speed (4800 baud) */
159 #define REFID "JJY" /* reference ID */
160 #define DESCRIPTION "JJY Receiver"
161 #define PRECISION (-3) /* precision assumed (about 100 ms) */
164 * JJY unit control structure
167 struct jjyRawDataBreak {
168 const char * pString ;
172 #define MAX_TIMESTAMP 6
173 #define MAX_RAWBUF 100
174 #define MAX_LOOPBACK 5
177 /* Set up by the function "jjy_start_xxxxxxxx" */
178 char unittype ; /* UNITTYPE_XXXXXXXXXX */
179 short operationmode ; /* Echo Keisokuki LT-2000 */
180 int linespeed ; /* SPEED232_XXXXXXXXXX */
181 short linediscipline ; /* LDISC_CLK or LDISC_RAW */
183 char bInitError ; /* Set by jjy_start if any error during initialization */
184 short iProcessState ; /* JJY_PROCESS_STATE_XXXXXX */
185 char bReceiveFlag ; /* Set and reset by jjy_receive */
186 char bLineError ; /* Reset by jjy_poll / Set by jjy_receive_xxxxxxxx*/
187 short iCommandSeq ; /* 0:Idle Non-Zero:Issued */
190 int year, month, day, hour, minute, second, msecond ;
192 int iTimestampCount ; /* TS-JJY01, TS-GPS01, Telephone-JJY */
193 int iTimestamp [ MAX_TIMESTAMP ] ; /* Serial second ( 0 - 86399 ) */
195 char sRawBuf [ MAX_RAWBUF ] ;
197 struct jjyRawDataBreak *pRawBreak ;
198 char bWaitBreakString ;
199 char sLineBuf [ MAX_RAWBUF ] ;
201 char sTextBuf [ MAX_RAWBUF ] ;
203 char bSkipCntrlCharOnly ;
204 /* TS-JJY01, TS-JJY02 */
205 time_t tLastAdjustedTimestamp ;
206 char bStusReplyAdjusted ;
207 char bStusReplyAdjustedAtLeastOnce ;
208 /* Telephone JJY auto measurement of the loopback delay */
210 short iLoopbackCount ;
211 struct timeval sendTime[MAX_LOOPBACK], delayTime[MAX_LOOPBACK] ;
212 char bLoopbackTimeout[MAX_LOOPBACK] ;
213 short iLoopbackValidCount ;
214 /* Telephone JJY timer */
215 short iTeljjySilentTimer ;
216 short iTeljjyStateTimer ;
217 /* Telephone JJY control finite state machine */
220 short iClockCommandSeq ;
222 short iModemSilentCount ;
223 short iModemSilentTimer ;
224 short iModemStateTimer ;
225 /* Modem control finite state machine */
228 short iModemCommandSeq ;
231 #define UNITTYPE_TRISTATE_JJY01 1
232 #define UNITTYPE_CDEX_JST2000 2
233 #define UNITTYPE_ECHOKEISOKUKI_LT2000 3
234 #define UNITTYPE_CITIZENTIC_JJY200 4
235 #define UNITTYPE_TRISTATE_GPSCLOCK01 5
236 #define UNITTYPE_SEIKO_TIMESYS_TDC_300 6
237 #define UNITTYPE_TELEPHONE 100
239 #define JJY_PROCESS_STATE_IDLE 0
240 #define JJY_PROCESS_STATE_POLL 1
241 #define JJY_PROCESS_STATE_RECEIVE 2
242 #define JJY_PROCESS_STATE_DONE 3
243 #define JJY_PROCESS_STATE_ERROR 4
245 /**********************************************************************/
248 * Function calling structure
251 * |-- jjy_start_tristate_jjy01
252 * |-- jjy_start_cdex_jst2000
253 * |-- jjy_start_echokeisokuki_lt2000
254 * |-- jjy_start_citizentic_jjy200
255 * |-- jjy_start_tristate_gpsclock01
256 * |-- jjy_start_seiko_tsys_tdc_300
257 * |-- jjy_start_telephone
262 * |-- jjy_poll_tristate_jjy01
263 * |-- jjy_poll_cdex_jst2000
264 * |-- jjy_poll_echokeisokuki_lt2000
265 * |-- jjy_poll_citizentic_jjy200
266 * |-- jjy_poll_tristate_gpsclock01
267 * |-- jjy_poll_seiko_tsys_tdc_300
268 * |-- jjy_poll_telephone
270 * |-- teljjy_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
273 * |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
277 * |-- jjy_receive_tristate_jjy01
279 * |-- jjy_receive_cdex_jst2000
281 * |-- jjy_receive_echokeisokuki_lt2000
283 * |-- jjy_receive_citizentic_jjy200
285 * |-- jjy_receive_tristate_gpsclock01
287 * |-- jjy_receive_seiko_tsys_tdc_300
289 * |-- jjy_receive_telephone
291 * | |-- modem_control
292 * | |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
294 * |-- teljjy_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
296 * |-- modem_disconnect
298 * |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
301 * |-- jjy_timer_telephone
303 * | |-- modem_control
304 * | |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
306 * |-- teljjy_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
307 * |-- modem_disconnect
309 * |-- modem_XXXX_YYYY ( XXXX_YYYY is an event handler name. )
311 * Function prototypes
314 static int jjy_start (int, struct peer *);
315 static int jjy_start_tristate_jjy01 (int, struct peer *, struct jjyunit *);
316 static int jjy_start_cdex_jst2000 (int, struct peer *, struct jjyunit *);
317 static int jjy_start_echokeisokuki_lt2000 (int, struct peer *, struct jjyunit *);
318 static int jjy_start_citizentic_jjy200 (int, struct peer *, struct jjyunit *);
319 static int jjy_start_tristate_gpsclock01 (int, struct peer *, struct jjyunit *);
320 static int jjy_start_seiko_tsys_tdc_300 (int, struct peer *, struct jjyunit *);
321 static int jjy_start_telephone (int, struct peer *, struct jjyunit *);
323 static void jjy_shutdown (int, struct peer *);
325 static void jjy_poll (int, struct peer *);
326 static void jjy_poll_tristate_jjy01 (int, struct peer *);
327 static void jjy_poll_cdex_jst2000 (int, struct peer *);
328 static void jjy_poll_echokeisokuki_lt2000 (int, struct peer *);
329 static void jjy_poll_citizentic_jjy200 (int, struct peer *);
330 static void jjy_poll_tristate_gpsclock01 (int, struct peer *);
331 static void jjy_poll_seiko_tsys_tdc_300 (int, struct peer *);
332 static void jjy_poll_telephone (int, struct peer *);
334 static void jjy_receive (struct recvbuf *);
335 static int jjy_receive_tristate_jjy01 (struct recvbuf *);
336 static int jjy_receive_cdex_jst2000 (struct recvbuf *);
337 static int jjy_receive_echokeisokuki_lt2000 (struct recvbuf *);
338 static int jjy_receive_citizentic_jjy200 (struct recvbuf *);
339 static int jjy_receive_tristate_gpsclock01 (struct recvbuf *);
340 static int jjy_receive_seiko_tsys_tdc_300 (struct recvbuf *);
341 static int jjy_receive_telephone (struct recvbuf *);
343 static void jjy_timer (int, struct peer *);
344 static void jjy_timer_telephone (int, struct peer *);
346 static void jjy_synctime ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
347 static void jjy_write_clockstats ( struct peer *, int, const char* ) ;
349 static int getRawDataBreakPosition ( struct jjyunit *, int ) ;
351 static short getModemState ( struct jjyunit * ) ;
352 static int isModemStateConnect ( short ) ;
353 static int isModemStateDisconnect ( short ) ;
354 static int isModemStateTimerOn ( struct jjyunit * ) ;
355 static void modem_connect ( int, struct peer * ) ;
356 static void modem_disconnect ( int, struct peer * ) ;
357 static int modem_receive ( struct recvbuf * ) ;
358 static void modem_timer ( int, struct peer * );
360 static void printableString ( char*, int, const char*, int ) ;
365 struct refclock refclock_jjy = {
366 jjy_start, /* start up driver */
367 jjy_shutdown, /* shutdown driver */
368 jjy_poll, /* transmit poll message */
369 noentry, /* not used */
370 noentry, /* not used */
371 noentry, /* not used */
372 jjy_timer /* 1 second interval timer */
376 * Start up driver return code
378 #define RC_START_SUCCESS 1
379 #define RC_START_ERROR 0
382 * Local constants definition
385 #define MAX_LOGTEXT 200
391 #define FALSE (!TRUE)
394 /* Local constants definition for the return code of the jjy_receive_xxxxxxxx */
396 #define JJY_RECEIVE_DONE 0
397 #define JJY_RECEIVE_SKIP 1
398 #define JJY_RECEIVE_UNPROCESS 2
399 #define JJY_RECEIVE_WAIT 3
400 #define JJY_RECEIVE_ERROR 4
402 /* Local constants definition for the 2nd parameter of the jjy_write_clockstats */
404 #define JJY_CLOCKSTATS_MARK_NONE 0
405 #define JJY_CLOCKSTATS_MARK_JJY 1
406 #define JJY_CLOCKSTATS_MARK_SEND 2
407 #define JJY_CLOCKSTATS_MARK_RECEIVE 3
408 #define JJY_CLOCKSTATS_MARK_INFORMATION 4
409 #define JJY_CLOCKSTATS_MARK_ATTENTION 5
410 #define JJY_CLOCKSTATS_MARK_WARNING 6
411 #define JJY_CLOCKSTATS_MARK_ERROR 7
412 #define JJY_CLOCKSTATS_MARK_BUG 8
414 /* Local constants definition for the clockstats messages */
416 #define JJY_CLOCKSTATS_MESSAGE_ECHOBACK "* Echoback"
417 #define JJY_CLOCKSTATS_MESSAGE_IGNORE_REPLY "* Ignore replay : [%s]"
418 #define JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_2 "* Over midnight : timestamp=%d, %d"
419 #define JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_3 "* Over midnight : timestamp=%d, %d, %d"
420 #define JJY_CLOCKSTATS_MESSAGE_TIMESTAMP_UNSURE "* Unsure timestamp : %s"
421 #define JJY_CLOCKSTATS_MESSAGE_LOOPBACK_DELAY "* Loopback delay : %d.%03d mSec."
422 #define JJY_CLOCKSTATS_MESSAGE_DELAY_ADJUST "* Delay adjustment : %d mSec. ( valid=%hd/%d )"
423 #define JJY_CLOCKSTATS_MESSAGE_DELAY_UNADJUST "* Delay adjustment : None ( valid=%hd/%d )"
424 #define JJY_CLOCKSTATS_MESSAGE_STUS_UNADJUSTED "* Skip time synchronization : STUS is 'UNADJUSTED' for %.0lf %s"
426 #define JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY "# Unexpected reply : [%s]"
427 #define JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH "# Invalid length : length=%d"
428 #define JJY_CLOCKSTATS_MESSAGE_TOO_MANY_REPLY "# Too many reply : count=%d"
429 #define JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY "# Invalid reply : [%s]"
430 #define JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_2 "# Slow reply : timestamp=%d, %d"
431 #define JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_3 "# Slow reply : timestamp=%d, %d, %d"
432 #define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE "# Invalid date : rc=%d year=%d month=%d day=%d"
433 #define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME "# Invalid time : rc=%d hour=%d minute=%d second=%d"
434 #define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME "# Invalid time : rc=%d year=%d month=%d day=%d hour=%d minute=%d second=%d"
435 #define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_LEAP "# Invalid leap : leapsecond=[%s]"
436 #define JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_STATUS "# Invalid status : status=[%s]"
438 /* Debug print macro */
441 #define DEBUG_PRINTF_JJY_RECEIVE(sFunc,iLen) { if ( debug ) { printf ( "refclock_jjy.c : %s : iProcessState=%d bLineError=%d iCommandSeq=%d iLineCount=%d iTimestampCount=%d iLen=%d\n", sFunc, up->iProcessState, up->bLineError, up->iCommandSeq, up->iLineCount, up->iTimestampCount, iLen ) ; } }
443 #define DEBUG_PRINTF_JJY_RECEIVE(sFunc,iLen)
446 /**************************************************************************************************/
447 /* jjy_start - open the devices and initialize data for processing */
448 /**************************************************************************************************/
450 jjy_start ( int unit, struct peer *peer )
453 struct refclockproc *pp ;
457 char sDeviceName [ sizeof(DEVICE) + 10 ], sLog [ 60 ] ;
461 printf( "refclock_jjy.c : jjy_start : %s mode=%d dev=%s unit=%d\n",
462 ntoa(&peer->srcadr), peer->ttl, DEVICE, unit ) ;
466 /* Allocate memory for the unit structure */
467 up = emalloc( sizeof(*up) ) ;
469 msyslog ( LOG_ERR, "refclock_jjy.c : jjy_start : emalloc" ) ;
470 return RC_START_ERROR ;
472 memset ( up, 0, sizeof(*up) ) ;
474 up->bInitError = FALSE ;
475 up->iProcessState = JJY_PROCESS_STATE_IDLE ;
476 up->bReceiveFlag = FALSE ;
477 up->iCommandSeq = 0 ;
479 up->iTimestampCount = 0 ;
480 up->bWaitBreakString = FALSE ;
481 up->iRawBufLen = up->iLineBufLen = up->iTextBufLen = 0 ;
482 up->bSkipCntrlCharOnly = TRUE ;
484 /* Set up the device name */
485 snprintf( sDeviceName, sizeof(sDeviceName), DEVICE, unit ) ;
487 snprintf( sLog, sizeof(sLog), "mode=%d dev=%s", peer->ttl, sDeviceName ) ;
488 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
491 * peer->ttl is a mode number specified by "127.127.40.X mode N" in the ntp.conf
493 switch ( peer->ttl ) {
496 rc = jjy_start_tristate_jjy01 ( unit, peer, up ) ;
499 rc = jjy_start_cdex_jst2000 ( unit, peer, up ) ;
502 rc = jjy_start_echokeisokuki_lt2000 ( unit, peer, up ) ;
505 rc = jjy_start_citizentic_jjy200 ( unit, peer, up ) ;
508 rc = jjy_start_tristate_gpsclock01 ( unit, peer, up ) ;
511 rc = jjy_start_seiko_tsys_tdc_300 ( unit, peer, up ) ;
514 rc = jjy_start_telephone ( unit, peer, up ) ;
517 if ( 101 <= peer->ttl && peer->ttl <= 180 ) {
518 rc = jjy_start_telephone ( unit, peer, up ) ;
520 msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Unsupported mode",
521 ntoa(&peer->srcadr), peer->ttl ) ;
522 free ( (void*) up ) ;
523 return RC_START_ERROR ;
528 msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Initialize error",
529 ntoa(&peer->srcadr), peer->ttl ) ;
530 free ( (void*) up ) ;
531 return RC_START_ERROR ;
534 /* Open the device */
535 fd = refclock_open ( sDeviceName, up->linespeed, up->linediscipline ) ;
537 free ( (void*) up ) ;
538 return RC_START_ERROR ;
542 * Initialize variables
546 pp->clockdesc = DESCRIPTION ;
548 pp->io.clock_recv = jjy_receive ;
549 pp->io.srcclock = peer ;
552 if ( ! io_addclock(&pp->io) ) {
557 return RC_START_ERROR ;
559 memcpy( (char*)&pp->refid, REFID, strlen(REFID) ) ;
561 peer->precision = PRECISION ;
563 snprintf( sLog, sizeof(sLog), "minpoll=%d maxpoll=%d", peer->minpoll, peer->maxpoll ) ;
564 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
566 return RC_START_SUCCESS ;
570 /**************************************************************************************************/
571 /* jjy_shutdown - shutdown the clock */
572 /**************************************************************************************************/
574 jjy_shutdown ( int unit, struct peer *peer )
578 struct refclockproc *pp;
584 if ( -1 != pp->io.fd ) {
585 io_closeclock ( &pp->io ) ;
591 snprintf( sLog, sizeof(sLog), "JJY stopped. unit=%d mode=%d", unit, peer->ttl ) ;
592 record_clock_stats( &peer->srcadr, sLog ) ;
596 /**************************************************************************************************/
597 /* jjy_receive - receive data from the serial interface */
598 /**************************************************************************************************/
600 jjy_receive ( struct recvbuf *rbufp )
603 static const char *sFunctionName = "jjy_receive" ;
607 struct refclockproc *pp ;
610 l_fp tRecvTimestamp; /* arrival timestamp */
612 char *pBuf, sLogText [ MAX_LOGTEXT ] ;
613 size_t iLen, iCopyLen ;
614 int i, j, iReadRawBuf, iBreakPosition ;
617 * Initialize pointers and read the timecode and timestamp
619 peer = rbufp->recv_peer ;
624 * Get next input line
626 if ( up->linediscipline == LDISC_RAW ) {
628 pp->lencode = refclock_gtraw ( rbufp, pp->a_lastcode, BMAX-1, &tRecvTimestamp ) ;
629 /* 3rd argument can be BMAX, but the coverity scan tool claim "Memory - corruptions (OVERRUN)" */
630 /* "a_lastcode" is defined as "char a_lastcode[BMAX]" in the ntp_refclock.h */
631 /* To avoid its claim, pass the value BMAX-1. */
634 * Append received characters to temporary buffer
637 i < pp->lencode && up->iRawBufLen < MAX_RAWBUF - 2 ;
638 i ++ , up->iRawBufLen ++ ) {
639 up->sRawBuf[up->iRawBufLen] = pp->a_lastcode[i] ;
641 up->sRawBuf[up->iRawBufLen] = 0 ;
646 pp->lencode = refclock_gtlin ( rbufp, pp->a_lastcode, BMAX, &tRecvTimestamp ) ;
650 printf( "\nrefclock_jjy.c : %s : Len=%d ", sFunctionName, pp->lencode ) ;
651 for ( i = 0 ; i < pp->lencode ; i ++ ) {
652 if ( iscntrl( (u_char)(pp->a_lastcode[i] & 0x7F) ) ) {
653 printf( "<x%02X>", pp->a_lastcode[i] & 0xFF ) ;
655 printf( "%c", pp->a_lastcode[i] ) ;
662 * The reply with <CR><LF> gives a blank line
665 if ( pp->lencode == 0 ) return ;
668 * Receiving data is not expected
671 if ( up->iProcessState == JJY_PROCESS_STATE_IDLE
672 || up->iProcessState == JJY_PROCESS_STATE_DONE
673 || up->iProcessState == JJY_PROCESS_STATE_ERROR ) {
674 /* Discard received data */
678 printf( "refclock_jjy.c : %s : Discard received data\n", sFunctionName ) ;
685 * We get down to business
688 pp->lastrec = tRecvTimestamp ;
692 up->iProcessState = JJY_PROCESS_STATE_RECEIVE ;
693 up->bReceiveFlag = TRUE ;
696 iBreakPosition = up->iRawBufLen - 1 ;
697 for ( ; up->iProcessState == JJY_PROCESS_STATE_RECEIVE ; ) {
699 if ( up->linediscipline == LDISC_RAW ) {
701 if ( up->bWaitBreakString ) {
702 iBreakPosition = getRawDataBreakPosition( up, iReadRawBuf ) ;
703 if ( iBreakPosition == -1 ) {
704 /* Break string have not come yet */
705 if ( up->iRawBufLen < MAX_RAWBUF - 2
706 || iReadRawBuf > 0 ) {
707 /* Temporary buffer is not full */
710 /* Temporary buffer is full */
711 iBreakPosition = up->iRawBufLen - 1 ;
715 iBreakPosition = up->iRawBufLen - 1 ;
718 /* Copy characters from temporary buffer to process buffer */
719 up->iLineBufLen = up->iTextBufLen = 0 ;
720 for ( i = iReadRawBuf ; i <= iBreakPosition ; i ++ ) {
722 /* Copy all characters */
723 up->sLineBuf[up->iLineBufLen] = up->sRawBuf[i] ;
726 /* Copy printable characters */
727 if ( ! iscntrl( (u_char)up->sRawBuf[i] ) ) {
728 up->sTextBuf[up->iTextBufLen] = up->sRawBuf[i] ;
733 up->sLineBuf[up->iLineBufLen] = 0 ;
734 up->sTextBuf[up->iTextBufLen] = 0 ;
736 printf( "refclock_jjy.c : %s : up->iLineBufLen=%d up->iTextBufLen=%d\n",
737 sFunctionName, up->iLineBufLen, up->iTextBufLen ) ;
740 if ( up->bSkipCntrlCharOnly && up->iTextBufLen == 0 ) {
742 printf( "refclock_jjy.c : %s : Skip cntrl char only : up->iRawBufLen=%d iReadRawBuf=%d iBreakPosition=%d\n",
743 sFunctionName, up->iRawBufLen, iReadRawBuf, iBreakPosition ) ;
745 if ( iBreakPosition + 1 < up->iRawBufLen ) {
746 iReadRawBuf = iBreakPosition + 1 ;
756 if ( up->linediscipline == LDISC_RAW ) {
757 pBuf = up->sLineBuf ;
758 iLen = up->iLineBufLen ;
760 pBuf = pp->a_lastcode ;
764 iCopyLen = ( iLen <= sizeof(sLogText)-1 ? iLen : sizeof(sLogText)-1 ) ;
765 memcpy( sLogText, pBuf, iCopyLen ) ;
766 sLogText[iCopyLen] = '\0' ;
767 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_RECEIVE, sLogText ) ;
769 switch ( up->unittype ) {
771 case UNITTYPE_TRISTATE_JJY01 :
772 rc = jjy_receive_tristate_jjy01 ( rbufp ) ;
775 case UNITTYPE_CDEX_JST2000 :
776 rc = jjy_receive_cdex_jst2000 ( rbufp ) ;
779 case UNITTYPE_ECHOKEISOKUKI_LT2000 :
780 rc = jjy_receive_echokeisokuki_lt2000 ( rbufp ) ;
783 case UNITTYPE_CITIZENTIC_JJY200 :
784 rc = jjy_receive_citizentic_jjy200 ( rbufp ) ;
787 case UNITTYPE_TRISTATE_GPSCLOCK01 :
788 rc = jjy_receive_tristate_gpsclock01 ( rbufp ) ;
791 case UNITTYPE_SEIKO_TIMESYS_TDC_300 :
792 rc = jjy_receive_seiko_tsys_tdc_300 ( rbufp ) ;
795 case UNITTYPE_TELEPHONE :
796 rc = jjy_receive_telephone ( rbufp ) ;
800 rc = JJY_RECEIVE_ERROR ;
806 case JJY_RECEIVE_DONE :
807 case JJY_RECEIVE_SKIP :
808 up->iProcessState = JJY_PROCESS_STATE_DONE ;
810 case JJY_RECEIVE_ERROR :
811 up->iProcessState = JJY_PROCESS_STATE_ERROR ;
817 if ( up->linediscipline == LDISC_RAW ) {
818 if ( rc == JJY_RECEIVE_UNPROCESS ) {
821 iReadRawBuf = iBreakPosition + 1 ;
822 if ( iReadRawBuf >= up->iRawBufLen ) {
823 /* Processed all received data */
828 if ( up->linediscipline == LDISC_CLK ) {
834 if ( up->linediscipline == LDISC_RAW && iReadRawBuf > 0 ) {
835 for ( i = 0, j = iReadRawBuf ; j < up->iRawBufLen ; i ++, j++ ) {
836 up->sRawBuf[i] = up->sRawBuf[j] ;
838 up->iRawBufLen -= iReadRawBuf ;
839 if ( up->iRawBufLen < 0 ) {
844 up->bReceiveFlag = FALSE ;
848 /**************************************************************************************************/
851 getRawDataBreakPosition ( struct jjyunit *up, int iStart )
856 if ( iStart >= up->iRawBufLen ) {
858 printf( "refclock_jjy.c : getRawDataBreakPosition : iStart=%d return=-1\n", iStart ) ;
863 for ( i = iStart ; i < up->iRawBufLen ; i ++ ) {
865 for ( j = 0 ; up->pRawBreak[j].pString != NULL ; j ++ ) {
867 if ( i + up->pRawBreak[j].iLength <= up->iRawBufLen ) {
869 if ( strncmp( up->sRawBuf + i,
870 up->pRawBreak[j].pString,
871 up->pRawBreak[j].iLength ) == 0 ) {
874 printf( "refclock_jjy.c : getRawDataBreakPosition : iStart=%d return=%d\n",
875 iStart, i + up->pRawBreak[j].iLength - 1 ) ;
877 return i + up->pRawBreak[j].iLength - 1 ;
885 printf( "refclock_jjy.c : getRawDataBreakPosition : iStart=%d return=-1\n", iStart ) ;
891 /**************************************************************************************************/
892 /* jjy_poll - called by the transmit procedure */
893 /**************************************************************************************************/
895 jjy_poll ( int unit, struct peer *peer )
898 char sLog [ 40 ], sReach [ 9 ] ;
901 struct refclockproc *pp;
906 if ( up->bInitError ) {
907 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, "Ignore polling because of error during initializing" ) ;
911 if ( pp->polls > 0 && up->iLineCount == 0 ) {
913 * No reply for last command
915 refclock_report ( peer, CEVNT_TIMEOUT ) ;
920 sReach[0] = peer->reach & 0x80 ? '1' : '0' ;
921 sReach[1] = peer->reach & 0x40 ? '1' : '0' ;
922 sReach[2] = peer->reach & 0x20 ? '1' : '0' ;
923 sReach[3] = peer->reach & 0x10 ? '1' : '0' ;
924 sReach[4] = peer->reach & 0x08 ? '1' : '0' ;
925 sReach[5] = peer->reach & 0x04 ? '1' : '0' ;
926 sReach[6] = peer->reach & 0x02 ? '1' : '0' ;
927 sReach[7] = 0 ; /* This poll */
930 snprintf( sLog, sizeof(sLog), "polls=%ld reach=%s", pp->polls, sReach ) ;
931 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ATTENTION, sLog ) ;
933 up->iProcessState = JJY_PROCESS_STATE_POLL ;
934 up->iCommandSeq = 0 ;
935 up->iReceiveSeq = 0 ;
937 up->bLineError = FALSE ;
940 switch ( up->unittype ) {
942 case UNITTYPE_TRISTATE_JJY01 :
943 jjy_poll_tristate_jjy01 ( unit, peer ) ;
946 case UNITTYPE_CDEX_JST2000 :
947 jjy_poll_cdex_jst2000 ( unit, peer ) ;
950 case UNITTYPE_ECHOKEISOKUKI_LT2000 :
951 jjy_poll_echokeisokuki_lt2000 ( unit, peer ) ;
954 case UNITTYPE_CITIZENTIC_JJY200 :
955 jjy_poll_citizentic_jjy200 ( unit, peer ) ;
958 case UNITTYPE_TRISTATE_GPSCLOCK01 :
959 jjy_poll_tristate_gpsclock01 ( unit, peer ) ;
962 case UNITTYPE_SEIKO_TIMESYS_TDC_300 :
963 jjy_poll_seiko_tsys_tdc_300 ( unit, peer ) ;
966 case UNITTYPE_TELEPHONE :
967 jjy_poll_telephone ( unit, peer ) ;
977 /**************************************************************************************************/
978 /* jjy_timer - called at one-second intervals */
979 /**************************************************************************************************/
981 jjy_timer ( int unit, struct peer *peer )
984 struct refclockproc *pp ;
989 printf ( "refclock_jjy.c : jjy_timer\n" ) ;
996 if ( up->bReceiveFlag ) {
999 printf ( "refclock_jjy.c : jjy_timer : up->bReceiveFlag= TRUE : Timer skipped.\n" ) ;
1005 switch ( up->unittype ) {
1007 case UNITTYPE_TELEPHONE :
1008 jjy_timer_telephone ( unit, peer ) ;
1018 /**************************************************************************************************/
1020 /**************************************************************************************************/
1022 jjy_synctime ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
1025 char sLog [ 80 ], cStatus ;
1026 const char *pStatus ;
1028 pp->year = up->year ;
1029 pp->day = ymd2yd( up->year, up->month, up->day ) ;
1030 pp->hour = up->hour ;
1031 pp->minute = up->minute ;
1032 pp->second = up->second ;
1033 pp->nsec = up->msecond * 1000000 ;
1039 if ( pp->hour < 0 ) {
1042 if ( pp->day < 1 ) {
1044 pp->day = ymd2yd( pp->year, 12, 31 ) ;
1049 * Process the new sample in the median filter and determine the
1050 * timecode timestamp.
1053 if ( ! refclock_process( pp ) ) {
1054 refclock_report( peer, CEVNT_BADTIME ) ;
1058 pp->lastref = pp->lastrec ;
1060 refclock_receive( peer ) ;
1063 * Write into the clockstats file
1065 snprintf ( sLog, sizeof(sLog),
1066 "%04d/%02d/%02d %02d:%02d:%02d.%03d JST ( %04d/%03d %02d:%02d:%02d.%03d UTC )",
1067 up->year, up->month, up->day,
1068 up->hour, up->minute, up->second, up->msecond,
1069 pp->year, pp->day, pp->hour, pp->minute, pp->second,
1070 (int)(pp->nsec/1000000) ) ;
1071 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ATTENTION, sLog ) ;
1076 switch ( peer->status ) {
1077 case 0 : cStatus = ' ' ; pStatus = "Reject" ; break ;
1078 case 1 : cStatus = 'x' ; pStatus = "FalseTick" ; break ;
1079 case 2 : cStatus = '.' ; pStatus = "Excess" ; break ;
1080 case 3 : cStatus = '-' ; pStatus = "Outlier" ; break ;
1081 case 4 : cStatus = '+' ; pStatus = "Candidate" ; break ;
1082 case 5 : cStatus = '#' ; pStatus = "Selected" ; break ;
1083 case 6 : cStatus = '*' ; pStatus = "Sys.Peer" ; break ;
1084 case 7 : cStatus = 'o' ; pStatus = "PPS.Peer" ; break ;
1088 snprintf ( sLog, sizeof(sLog),
1089 "status %d [%c] %s : offset %3.3f mSec. : jitter %3.3f mSec.",
1090 peer->status, cStatus, pStatus, peer->offset * 1000, peer->jitter * 1000 ) ;
1091 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
1095 /*################################################################################################*/
1096 /*################################################################################################*/
1098 /*## The Tristate Ltd. JJY receiver TS-JJY01, TS-JJY02 ##*/
1100 /*## server 127.127.40.X mode 1 ##*/
1102 /*################################################################################################*/
1103 /*################################################################################################*/
1105 /* Command Response Remarks */
1106 /* -------------------- ---------------------------------------- ---------------------------- */
1107 /* dcst<CR><LF> VALID<CR><LF> or INVALID<CR><LF> */
1108 /* stus<CR><LF> ADJUSTED<CR><LF> or UNADJUSTED<CR><LF> */
1109 /* date<CR><LF> YYYY/MM/DD XXX<CR><LF> XXX is the day of the week */
1110 /* time<CR><LF> HH:MM:SS<CR><LF> Not used by this driver */
1111 /* stim<CR><LF> HH:MM:SS<CR><LF> Reply at just second */
1113 /*################################################################################################*/
1115 #define TS_JJY01_COMMAND_NUMBER_DATE 1
1116 #define TS_JJY01_COMMAND_NUMBER_TIME 2
1117 #define TS_JJY01_COMMAND_NUMBER_STIM 3
1118 #define TS_JJY01_COMMAND_NUMBER_STUS 4
1119 #define TS_JJY01_COMMAND_NUMBER_DCST 5
1121 #define TS_JJY01_REPLY_DATE "yyyy/mm/dd www"
1122 #define TS_JJY01_REPLY_STIM "hh:mm:ss"
1123 #define TS_JJY01_REPLY_STUS_ADJUSTED "adjusted"
1124 #define TS_JJY01_REPLY_STUS_UNADJUSTED "unadjusted"
1125 #define TS_JJY01_REPLY_DCST_VALID "valid"
1126 #define TS_JJY01_REPLY_DCST_INVALID "invalid"
1128 #define TS_JJY01_REPLY_LENGTH_DATE 14 /* Length without <CR><LF> */
1129 #define TS_JJY01_REPLY_LENGTH_TIME 8 /* Length without <CR><LF> */
1130 #define TS_JJY01_REPLY_LENGTH_STIM 8 /* Length without <CR><LF> */
1131 #define TS_JJY01_REPLY_LENGTH_STUS_ADJUSTED 8 /* Length without <CR><LF> */
1132 #define TS_JJY01_REPLY_LENGTH_STUS_UNADJUSTED 10 /* Length without <CR><LF> */
1133 #define TS_JJY01_REPLY_LENGTH_DCST_VALID 5 /* Length without <CR><LF> */
1134 #define TS_JJY01_REPLY_LENGTH_DCST_INVALID 7 /* Length without <CR><LF> */
1138 const char commandNumber ;
1139 const char *command ;
1141 int iExpectedReplyLength [ 2 ] ;
1142 } tristate_jjy01_command_sequence[] =
1144 { 0, NULL, 0, { 0, 0 } }, /* Idle */
1145 { TS_JJY01_COMMAND_NUMBER_DCST, "dcst\r\n", 6, { TS_JJY01_REPLY_LENGTH_DCST_VALID , TS_JJY01_REPLY_LENGTH_DCST_INVALID } },
1146 { TS_JJY01_COMMAND_NUMBER_STUS, "stus\r\n", 6, { TS_JJY01_REPLY_LENGTH_STUS_ADJUSTED, TS_JJY01_REPLY_LENGTH_STUS_UNADJUSTED } },
1147 { TS_JJY01_COMMAND_NUMBER_TIME, "time\r\n", 6, { TS_JJY01_REPLY_LENGTH_TIME , TS_JJY01_REPLY_LENGTH_TIME } },
1148 { TS_JJY01_COMMAND_NUMBER_DATE, "date\r\n", 6, { TS_JJY01_REPLY_LENGTH_DATE , TS_JJY01_REPLY_LENGTH_DATE } },
1149 { TS_JJY01_COMMAND_NUMBER_STIM, "stim\r\n", 6, { TS_JJY01_REPLY_LENGTH_STIM , TS_JJY01_REPLY_LENGTH_STIM } },
1150 /* End of command */
1151 { 0, NULL, 0, { 0, 0 } }
1154 /**************************************************************************************************/
1157 jjy_start_tristate_jjy01 ( int unit, struct peer *peer, struct jjyunit *up )
1160 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Tristate Ltd. TS-JJY01, TS-JJY02" ) ;
1162 up->unittype = UNITTYPE_TRISTATE_JJY01 ;
1163 up->linespeed = SPEED232_TRISTATE_JJY01 ;
1164 up->linediscipline = LDISC_CLK ;
1166 time( &(up->tLastAdjustedTimestamp) ) ;
1167 up->bStusReplyAdjustedAtLeastOnce = FALSE ;
1173 /**************************************************************************************************/
1176 jjy_receive_tristate_jjy01 ( struct recvbuf *rbufp )
1178 struct jjyunit *up ;
1179 struct refclockproc *pp ;
1183 char sLog [ MAX_LOGTEXT ] ;
1192 /* Initialize pointers */
1194 peer = rbufp->recv_peer ;
1195 pp = peer->procptr ;
1198 if ( up->linediscipline == LDISC_RAW ) {
1199 pBuf = up->sTextBuf ;
1200 iLen = up->iTextBufLen ;
1202 pBuf = pp->a_lastcode ;
1203 iLen = pp->lencode ;
1206 DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_tristate_jjy01", iLen ) ;
1208 /* Check expected reply */
1210 if ( tristate_jjy01_command_sequence[up->iCommandSeq].command == NULL ) {
1211 /* Command sequence has not been started, or has been completed */
1212 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
1214 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1215 up->bLineError = TRUE ;
1216 return JJY_RECEIVE_ERROR ;
1219 /* Check reply length */
1221 if ( iLen != tristate_jjy01_command_sequence[up->iCommandSeq].iExpectedReplyLength[0]
1222 && iLen != tristate_jjy01_command_sequence[up->iCommandSeq].iExpectedReplyLength[1] ) {
1223 /* Unexpected reply length */
1224 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
1226 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1227 up->bLineError = TRUE ;
1228 return JJY_RECEIVE_ERROR ;
1233 switch ( tristate_jjy01_command_sequence[up->iCommandSeq].commandNumber ) {
1235 case TS_JJY01_COMMAND_NUMBER_DATE : /* YYYY/MM/DD WWW */
1237 rc = sscanf ( pBuf, "%4d/%2d/%2d",
1238 &up->year, &up->month, &up->day ) ;
1240 if ( rc != 3 || up->year < 2000 || 2099 <= up->year
1241 || up->month < 1 || 12 < up->month
1242 || up->day < 1 || 31 < up->day ) {
1244 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE,
1245 rc, up->year, up->month, up->day ) ;
1246 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1247 up->bLineError = TRUE ;
1248 return JJY_RECEIVE_ERROR ;
1253 case TS_JJY01_COMMAND_NUMBER_TIME : /* HH:MM:SS */
1254 case TS_JJY01_COMMAND_NUMBER_STIM : /* HH:MM:SS */
1256 if ( up->iTimestampCount >= 2 ) {
1257 /* Too many time reply */
1258 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_TOO_MANY_REPLY,
1259 up->iTimestampCount ) ;
1260 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1261 up->bLineError = TRUE ;
1262 return JJY_RECEIVE_ERROR ;
1265 rc = sscanf ( pBuf, "%2d:%2d:%2d",
1266 &up->hour, &up->minute, &up->second ) ;
1268 if ( rc != 3 || up->hour > 23 || up->minute > 59 ||
1271 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
1272 rc, up->hour, up->minute, up->second ) ;
1273 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1274 up->bLineError = TRUE ;
1275 return JJY_RECEIVE_ERROR ;
1278 up->iTimestamp[up->iTimestampCount] = ( up->hour * 60 + up->minute ) * 60 + up->second ;
1280 up->iTimestampCount++ ;
1286 case TS_JJY01_COMMAND_NUMBER_STUS :
1288 if ( strncmp( pBuf, TS_JJY01_REPLY_STUS_ADJUSTED,
1289 TS_JJY01_REPLY_LENGTH_STUS_ADJUSTED ) == 0 ) {
1290 /* STUS reply : adjusted */
1291 up->bStusReplyAdjusted = TRUE ;
1292 up->bStusReplyAdjustedAtLeastOnce = TRUE ;
1293 time( &(up->tLastAdjustedTimestamp) ) ;
1294 } else if ( strncmp( pBuf, TS_JJY01_REPLY_STUS_UNADJUSTED,
1295 TS_JJY01_REPLY_LENGTH_STUS_UNADJUSTED ) == 0 ) {
1296 /* STUS reply : unadjusted */
1297 up->bStusReplyAdjusted = FALSE ;
1300 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
1302 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1303 up->bLineError = TRUE ;
1304 return JJY_RECEIVE_ERROR ;
1309 case TS_JJY01_COMMAND_NUMBER_DCST :
1311 if ( strncmp( pBuf, TS_JJY01_REPLY_DCST_VALID,
1312 TS_JJY01_REPLY_LENGTH_DCST_VALID ) == 0
1313 || strncmp( pBuf, TS_JJY01_REPLY_DCST_INVALID,
1314 TS_JJY01_REPLY_LENGTH_DCST_INVALID ) == 0 ) {
1318 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
1320 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1321 up->bLineError = TRUE ;
1322 return JJY_RECEIVE_ERROR ;
1327 default : /* Unexpected reply */
1329 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
1331 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1332 up->bLineError = TRUE ;
1333 return JJY_RECEIVE_ERROR ;
1337 if ( up->iTimestampCount == 2 ) {
1338 /* Process date and time */
1341 fSeconds = difftime( now, up->tLastAdjustedTimestamp ) ;
1343 if ( ( pp->sloppyclockflag & CLK_FLAG2 ) != 0
1344 && ( ! up->bStusReplyAdjusted )
1345 && ( fSeconds >= ( pp->fudgetime2 * 3600 ) || ( ! up->bStusReplyAdjustedAtLeastOnce ) ) ) {
1346 /* STUS is not ADJUSTED */
1347 if ( fSeconds < 60 ) {
1348 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_STUS_UNADJUSTED, fSeconds, "seconds" ) ;
1349 } else if ( fSeconds < 3600 ) {
1350 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_STUS_UNADJUSTED, fSeconds / 60, "minutes" ) ;
1351 } else if ( fSeconds < 86400 ) {
1352 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_STUS_UNADJUSTED, fSeconds / 3600, "hours" ) ;
1354 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_STUS_UNADJUSTED, fSeconds / 86400, "days" ) ;
1356 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
1357 return JJY_RECEIVE_SKIP ;
1358 } else if ( up->iTimestamp[1] - 2 <= up->iTimestamp[0]
1359 && up->iTimestamp[0] <= up->iTimestamp[1] ) {
1360 /* 3 commands (time,date,stim) was executed in two seconds */
1361 jjy_synctime( peer, pp, up ) ;
1362 return JJY_RECEIVE_DONE ;
1363 } else if ( up->iTimestamp[0] > up->iTimestamp[1] ) {
1364 /* Over midnight, and date is unsure */
1365 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_2,
1366 up->iTimestamp[0], up->iTimestamp[1] ) ;
1367 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
1368 return JJY_RECEIVE_SKIP ;
1371 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_2,
1372 up->iTimestamp[0], up->iTimestamp[1] ) ;
1373 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1374 up->bLineError = TRUE ;
1375 return JJY_RECEIVE_ERROR ;
1380 /* Issue next command */
1382 if ( tristate_jjy01_command_sequence[up->iCommandSeq].command != NULL ) {
1383 up->iCommandSeq ++ ;
1386 if ( tristate_jjy01_command_sequence[up->iCommandSeq].command == NULL ) {
1387 /* Command sequence completed */
1388 return JJY_RECEIVE_DONE ;
1391 pCmd = tristate_jjy01_command_sequence[up->iCommandSeq].command ;
1392 iCmdLen = tristate_jjy01_command_sequence[up->iCommandSeq].commandLength ;
1393 if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
1394 refclock_report ( peer, CEVNT_FAULT ) ;
1397 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
1399 return JJY_RECEIVE_WAIT ;
1403 /**************************************************************************************************/
1406 jjy_poll_tristate_jjy01 ( int unit, struct peer *peer )
1409 static const char *sFunctionName = "jjy_poll_tristate_jjy01" ;
1412 struct refclockproc *pp ;
1413 struct jjyunit *up ;
1421 up->bLineError = FALSE ;
1422 up->iTimestampCount = 0 ;
1424 if ( ( pp->sloppyclockflag & CLK_FLAG1 ) == 0 ) {
1425 /* Skip "dcst" and "stus" commands */
1426 up->iCommandSeq = 2 ;
1427 up->iLineCount = 2 ;
1430 up->bStusReplyAdjusted = FALSE ;
1434 printf ( "%s (refclock_jjy.c) : flag1=%X CLK_FLAG1=%X up->iLineCount=%d\n",
1435 sFunctionName, pp->sloppyclockflag, CLK_FLAG1,
1441 * Send a first command
1444 up->iCommandSeq ++ ;
1446 pCmd = tristate_jjy01_command_sequence[up->iCommandSeq].command ;
1447 iCmdLen = tristate_jjy01_command_sequence[up->iCommandSeq].commandLength ;
1448 if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
1449 refclock_report ( peer, CEVNT_FAULT ) ;
1452 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
1456 /*################################################################################################*/
1457 /*################################################################################################*/
1459 /*## The C-DEX Co. Ltd. JJY receiver JST2000 ##*/
1461 /*## server 127.127.40.X mode 2 ##*/
1463 /*################################################################################################*/
1464 /*################################################################################################*/
1466 /* Command Response Remarks */
1467 /* -------------------- ---------------------------------------- ---------------------------- */
1468 /* <ENQ>1J<ETX> <STX>JYYMMDDWHHMMSSS<ETX> J is a fixed character */
1470 /*################################################################################################*/
1472 static struct jjyRawDataBreak cdex_jst2000_raw_break [ ] =
1474 { "\x03", 1 }, { NULL, 0 }
1477 /**************************************************************************************************/
1480 jjy_start_cdex_jst2000 ( int unit, struct peer *peer, struct jjyunit *up )
1483 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: C-DEX Co. Ltd. JST2000" ) ;
1485 up->unittype = UNITTYPE_CDEX_JST2000 ;
1486 up->linespeed = SPEED232_CDEX_JST2000 ;
1487 up->linediscipline = LDISC_RAW ;
1489 up->pRawBreak = cdex_jst2000_raw_break ;
1490 up->bWaitBreakString = TRUE ;
1492 up->bSkipCntrlCharOnly = FALSE ;
1498 /**************************************************************************************************/
1501 jjy_receive_cdex_jst2000 ( struct recvbuf *rbufp )
1504 struct jjyunit *up ;
1505 struct refclockproc *pp ;
1508 char *pBuf, sLog [ MAX_LOGTEXT ] ;
1512 /* Initialize pointers */
1514 peer = rbufp->recv_peer ;
1515 pp = peer->procptr ;
1518 if ( up->linediscipline == LDISC_RAW ) {
1519 pBuf = up->sTextBuf ;
1520 iLen = up->iTextBufLen ;
1522 pBuf = pp->a_lastcode ;
1523 iLen = pp->lencode ;
1526 DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_cdex_jst2000", iLen ) ;
1528 /* Check expected reply */
1530 if ( up->iCommandSeq != 1 ) {
1531 /* Command sequence has not been started, or has been completed */
1532 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
1534 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1535 up->bLineError = TRUE ;
1536 return JJY_RECEIVE_ERROR ;
1539 /* Wait until ETX comes */
1541 if ( up->iLineBufLen < 17 || up->sLineBuf[up->iLineBufLen-1] != 0x03 ) {
1542 return JJY_RECEIVE_UNPROCESS ;
1545 /* Check reply length */
1548 /* Unexpected reply length */
1549 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
1551 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1552 up->bLineError = TRUE ;
1553 return JJY_RECEIVE_ERROR ;
1556 /* JYYMMDDWHHMMSSS */
1558 rc = sscanf ( pBuf, "J%2d%2d%2d%*1d%2d%2d%2d%1d",
1559 &up->year, &up->month, &up->day,
1560 &up->hour, &up->minute, &up->second,
1563 if ( rc != 7 || up->month < 1 || up->month > 12 ||
1564 up->day < 1 || up->day > 31 || up->hour > 23 ||
1565 up->minute > 59 || up->second > 60 ) {
1566 /* Invalid date and time */
1567 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
1568 rc, up->year, up->month, up->day,
1569 up->hour, up->minute, up->second ) ;
1570 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1571 up->bLineError = TRUE ;
1572 return JJY_RECEIVE_ERROR ;
1576 up->msecond *= 100 ;
1578 jjy_synctime( peer, pp, up ) ;
1580 return JJY_RECEIVE_DONE ;
1584 /**************************************************************************************************/
1587 jjy_poll_cdex_jst2000 ( int unit, struct peer *peer )
1590 struct refclockproc *pp ;
1591 struct jjyunit *up ;
1593 pp = peer->procptr ;
1596 up->bLineError = FALSE ;
1597 up->iRawBufLen = 0 ;
1598 up->iLineBufLen = 0 ;
1599 up->iTextBufLen = 0 ;
1602 * Send "<ENQ>1J<ETX>" command
1605 up->iCommandSeq ++ ;
1607 if ( write ( pp->io.fd, "\0051J\003", 4 ) != 4 ) {
1608 refclock_report ( peer, CEVNT_FAULT ) ;
1611 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, "\0051J\003" ) ;
1615 /*################################################################################################*/
1616 /*################################################################################################*/
1618 /*## The Echo Keisokuki Co. Ltd. JJY receiver LT2000 ##*/
1620 /*## server 127.127.40.X mode 3 ##*/
1622 /*################################################################################################*/
1623 /*################################################################################################*/
1625 /* Command Response Remarks */
1626 /* -------------------- ---------------------------------------- ---------------------------- */
1627 /* # Mode 1 ( Request & Send ) */
1628 /* T YYMMDDWHHMMSS<BCC1><BCC2><CR> */
1629 /* C Mode 2 ( Continuous ) */
1630 /* YYMMDDWHHMMSS<ST1><ST2><ST3><ST4><CR> 0.5 sec before time stamp */
1631 /* <SUB> Second signal */
1633 /*################################################################################################*/
1635 #define ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND 1
1636 #define ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS 2
1637 #define ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS 3
1639 #define ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND "#"
1640 #define ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_TIME "T"
1641 #define ECHOKEISOKUKI_LT2000_COMMAND_CONTINUOUS "C"
1643 /**************************************************************************************************/
1646 jjy_start_echokeisokuki_lt2000 ( int unit, struct peer *peer, struct jjyunit *up )
1649 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Echo Keisokuki Co. Ltd. LT2000" ) ;
1651 up->unittype = UNITTYPE_ECHOKEISOKUKI_LT2000 ;
1652 up->linespeed = SPEED232_ECHOKEISOKUKI_LT2000 ;
1653 up->linediscipline = LDISC_CLK ;
1655 up->operationmode = ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS ;
1661 /**************************************************************************************************/
1664 jjy_receive_echokeisokuki_lt2000 ( struct recvbuf *rbufp )
1667 struct jjyunit *up ;
1668 struct refclockproc *pp ;
1671 char *pBuf, sLog [ 100 ], sErr [ 60 ] ;
1674 int i, ibcc, ibcc1, ibcc2 ;
1676 /* Initialize pointers */
1678 peer = rbufp->recv_peer ;
1679 pp = peer->procptr ;
1682 if ( up->linediscipline == LDISC_RAW ) {
1683 pBuf = up->sTextBuf ;
1684 iLen = up->iTextBufLen ;
1686 pBuf = pp->a_lastcode ;
1687 iLen = pp->lencode ;
1690 DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_echokeisokuki_lt2000", iLen ) ;
1692 /* Check reply length */
1694 if ( ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND
1696 || ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS
1698 || ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS
1700 /* Unexpected reply length */
1701 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
1703 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1704 up->bLineError = TRUE ;
1705 return JJY_RECEIVE_ERROR ;
1708 if ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND && iLen == 15 ) {
1709 /* YYMMDDWHHMMSS<BCC1><BCC2> */
1711 for ( i = ibcc = 0 ; i < 13 ; i ++ ) {
1715 ibcc1 = 0x30 | ( ( ibcc >> 4 ) & 0xF ) ;
1716 ibcc2 = 0x30 | ( ( ibcc ) & 0xF ) ;
1717 if ( pBuf[13] != ibcc1 || pBuf[14] != ibcc2 ) {
1718 snprintf( sErr, sizeof(sErr)-1, " BCC error : Recv=%02X,%02X / Calc=%02X,%02X ",
1719 pBuf[13] & 0xFF, pBuf[14] & 0xFF,
1721 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
1723 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1724 up->bLineError = TRUE ;
1725 return JJY_RECEIVE_ERROR ;
1730 if ( ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND
1732 || ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS
1734 || ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS
1736 /* YYMMDDWHHMMSS<BCC1><BCC2> or YYMMDDWHHMMSS<ST1><ST2><ST3><ST4> */
1738 rc = sscanf ( pBuf, "%2d%2d%2d%*1d%2d%2d%2d",
1739 &up->year, &up->month, &up->day,
1740 &up->hour, &up->minute, &up->second ) ;
1742 if ( rc != 6 || up->month < 1 || up->month > 12
1743 || up->day < 1 || up->day > 31
1744 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
1745 /* Invalid date and time */
1746 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
1747 rc, up->year, up->month, up->day,
1748 up->hour, up->minute, up->second ) ;
1749 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1750 up->bLineError = TRUE ;
1751 return JJY_RECEIVE_ERROR ;
1756 if ( up->operationmode == ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS
1757 || up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS ) {
1758 /* A time stamp comes on every 0.5 second in the mode 2 of the LT-2000. */
1762 if ( up->second < 0 ) {
1765 if ( up->minute < 0 ) {
1768 if ( up->hour < 0 ) {
1771 if ( up->day < 1 ) {
1773 if ( up->month < 1 ) {
1784 jjy_synctime( peer, pp, up ) ;
1789 if (up->operationmode == ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS ) {
1790 /* Switch from mode 2 to mode 1 in order to restraint of useless time stamp. */
1792 iLen = strlen( ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND ) ;
1793 if ( write ( pp->io.fd, ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND, iLen ) != iLen ) {
1794 refclock_report ( peer, CEVNT_FAULT ) ;
1797 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, ECHOKEISOKUKI_LT2000_COMMAND_REQUEST_SEND ) ;
1801 return JJY_RECEIVE_DONE ;
1805 /**************************************************************************************************/
1808 jjy_poll_echokeisokuki_lt2000 ( int unit, struct peer *peer )
1811 struct refclockproc *pp ;
1812 struct jjyunit *up ;
1816 pp = peer->procptr ;
1819 up->bLineError = FALSE ;
1822 * Send "T" or "C" command
1825 switch ( up->operationmode ) {
1826 case ECHOKEISOKUKI_LT2000_MODE_REQUEST_SEND :
1829 case ECHOKEISOKUKI_LT2000_MODE_CONTINUOUS :
1830 case ECHOKEISOKUKI_LT2000_MODE_SWITCHING_CONTINUOUS :
1836 if ( write ( pp->io.fd, sCmd, 1 ) != 1 ) {
1837 refclock_report ( peer, CEVNT_FAULT ) ;
1840 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, sCmd ) ;
1844 /*################################################################################################*/
1845 /*################################################################################################*/
1847 /*## The CITIZEN T.I.C CO., LTD. JJY receiver JJY200 ##*/
1849 /*## server 127.127.40.X mode 4 ##*/
1851 /*################################################################################################*/
1852 /*################################################################################################*/
1854 /* Command Response Remarks */
1855 /* -------------------- ---------------------------------------- ---------------------------- */
1856 /* 'XX YY/MM/DD W HH:MM:SS<CR> XX:OK|NG|ER W:0(Mon)-6(Sun) */
1858 /*################################################################################################*/
1861 jjy_start_citizentic_jjy200 ( int unit, struct peer *peer, struct jjyunit *up )
1864 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: CITIZEN T.I.C CO. LTD. JJY200" ) ;
1866 up->unittype = UNITTYPE_CITIZENTIC_JJY200 ;
1867 up->linespeed = SPEED232_CITIZENTIC_JJY200 ;
1868 up->linediscipline = LDISC_CLK ;
1874 /**************************************************************************************************/
1877 jjy_receive_citizentic_jjy200 ( struct recvbuf *rbufp )
1880 struct jjyunit *up ;
1881 struct refclockproc *pp ;
1884 char *pBuf, sLog [ 100 ], sMsg [ 16 ] ;
1887 char cApostrophe, sStatus[3] ;
1890 /* Initialize pointers */
1892 peer = rbufp->recv_peer ;
1893 pp = peer->procptr ;
1896 if ( up->linediscipline == LDISC_RAW ) {
1897 pBuf = up->sTextBuf ;
1898 iLen = up->iTextBufLen ;
1900 pBuf = pp->a_lastcode ;
1901 iLen = pp->lencode ;
1904 DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_citizentic_jjy200", iLen ) ;
1907 * JJY-200 sends a timestamp every second.
1908 * So, a timestamp is ignored unless it is right after polled.
1911 if ( up->iProcessState != JJY_PROCESS_STATE_RECEIVE ) {
1912 return JJY_RECEIVE_SKIP ;
1915 /* Check reply length */
1918 /* Unexpected reply length */
1919 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
1921 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1922 up->bLineError = TRUE ;
1923 return JJY_RECEIVE_ERROR ;
1926 /* 'XX YY/MM/DD W HH:MM:SS<CR> */
1928 rc = sscanf ( pBuf, "%c%2s %2d/%2d/%2d %1d %2d:%2d:%2d",
1929 &cApostrophe, sStatus,
1930 &up->year, &up->month, &up->day, &iWeekday,
1931 &up->hour, &up->minute, &up->second ) ;
1934 if ( rc != 9 || cApostrophe != '\''
1935 || ( strcmp( sStatus, "OK" ) != 0
1936 && strcmp( sStatus, "NG" ) != 0
1937 && strcmp( sStatus, "ER" ) != 0 )
1938 || up->month < 1 || up->month > 12 || up->day < 1 || up->day > 31
1940 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
1941 /* Invalid date and time */
1942 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
1943 rc, up->year, up->month, up->day,
1944 up->hour, up->minute, up->second ) ;
1945 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
1946 up->bLineError = TRUE ;
1947 return JJY_RECEIVE_ERROR ;
1948 } else if ( strcmp( sStatus, "NG" ) == 0
1949 || strcmp( sStatus, "ER" ) == 0 ) {
1950 /* Timestamp is unsure */
1951 snprintf( sMsg, sizeof(sMsg)-1, "status=%s", sStatus ) ;
1952 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_TIMESTAMP_UNSURE,
1954 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_WARNING, sLog ) ;
1955 return JJY_RECEIVE_SKIP ;
1961 jjy_synctime( peer, pp, up ) ;
1963 return JJY_RECEIVE_DONE ;
1967 /**************************************************************************************************/
1970 jjy_poll_citizentic_jjy200 ( int unit, struct peer *peer )
1973 struct refclockproc *pp ;
1974 struct jjyunit *up ;
1976 pp = peer->procptr ;
1979 up->bLineError = FALSE ;
1983 /*################################################################################################*/
1984 /*################################################################################################*/
1986 /*## The Tristate Ltd. GPS clock TS-GPS01 ##*/
1988 /*## server 127.127.40.X mode 5 ##*/
1990 /*################################################################################################*/
1991 /*################################################################################################*/
1993 /* This clock has NMEA mode and command/response mode. */
1994 /* When this jjy driver are used, set to command/response mode of this clock */
1995 /* by the onboard switch SW4, and make sure the LED-Y is tured on. */
1996 /* Other than this JJY driver, the refclock driver type 20, generic NMEA driver, */
1997 /* works with the NMEA mode of this clock. */
1999 /* Command Response Remarks */
2000 /* -------------------- ---------------------------------------- ---------------------------- */
2001 /* stus<CR><LF> *R|*G|*U|+U<CR><LF> */
2002 /* date<CR><LF> YY/MM/DD<CR><LF> */
2003 /* time<CR><LF> HH:MM:SS<CR><LF> */
2005 /*################################################################################################*/
2007 #define TS_GPS01_COMMAND_NUMBER_DATE 1
2008 #define TS_GPS01_COMMAND_NUMBER_TIME 2
2009 #define TS_GPS01_COMMAND_NUMBER_STUS 4
2011 #define TS_GPS01_REPLY_DATE "yyyy/mm/dd"
2012 #define TS_GPS01_REPLY_TIME "hh:mm:ss"
2013 #define TS_GPS01_REPLY_STUS_RTC "*R"
2014 #define TS_GPS01_REPLY_STUS_GPS "*G"
2015 #define TS_GPS01_REPLY_STUS_UTC "*U"
2016 #define TS_GPS01_REPLY_STUS_PPS "+U"
2018 #define TS_GPS01_REPLY_LENGTH_DATE 10 /* Length without <CR><LF> */
2019 #define TS_GPS01_REPLY_LENGTH_TIME 8 /* Length without <CR><LF> */
2020 #define TS_GPS01_REPLY_LENGTH_STUS 2 /* Length without <CR><LF> */
2024 char commandNumber ;
2025 const char *command ;
2027 int iExpectedReplyLength ;
2028 } tristate_gps01_command_sequence[] =
2030 { 0, NULL, 0, 0 }, /* Idle */
2031 { TS_GPS01_COMMAND_NUMBER_STUS, "stus\r\n", 6, TS_GPS01_REPLY_LENGTH_STUS },
2032 { TS_GPS01_COMMAND_NUMBER_TIME, "time\r\n", 6, TS_GPS01_REPLY_LENGTH_TIME },
2033 { TS_GPS01_COMMAND_NUMBER_DATE, "date\r\n", 6, TS_GPS01_REPLY_LENGTH_DATE },
2034 { TS_GPS01_COMMAND_NUMBER_TIME, "time\r\n", 6, TS_GPS01_REPLY_LENGTH_TIME },
2035 /* End of command */
2039 /**************************************************************************************************/
2042 jjy_start_tristate_gpsclock01 ( int unit, struct peer *peer, struct jjyunit *up )
2045 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Tristate Ltd. TS-GPS01" ) ;
2047 up->unittype = UNITTYPE_TRISTATE_GPSCLOCK01 ;
2048 up->linespeed = SPEED232_TRISTATE_GPSCLOCK01 ;
2049 up->linediscipline = LDISC_CLK ;
2055 /**************************************************************************************************/
2058 jjy_receive_tristate_gpsclock01 ( struct recvbuf *rbufp )
2061 static const char *sFunctionName = "jjy_receive_tristate_gpsclock01" ;
2064 struct jjyunit *up ;
2065 struct refclockproc *pp ;
2069 char sLog [ MAX_LOGTEXT ] ;
2076 /* Initialize pointers */
2078 peer = rbufp->recv_peer ;
2079 pp = peer->procptr ;
2082 if ( up->linediscipline == LDISC_RAW ) {
2083 pBuf = up->sTextBuf ;
2084 iLen = up->iTextBufLen ;
2086 pBuf = pp->a_lastcode ;
2087 iLen = pp->lencode ;
2090 DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_tristate_gpsclock01", iLen ) ;
2092 /* Ignore NMEA data stream */
2095 && ( strncmp( pBuf, "$GP", 3 ) == 0 || strncmp( pBuf, "$PFEC", 5 ) == 0 ) ) {
2098 printf ( "%s (refclock_jjy.c) : Skip NMEA stream [%s]\n",
2099 sFunctionName, pBuf ) ;
2102 return JJY_RECEIVE_WAIT ;
2106 * Skip command prompt '$Cmd>' from the TS-GPSclock-01
2108 if ( iLen == 5 && strncmp( pBuf, "$Cmd>", 5 ) == 0 ) {
2109 return JJY_RECEIVE_WAIT ;
2110 } else if ( iLen > 5 && strncmp( pBuf, "$Cmd>", 5 ) == 0 ) {
2116 * Ignore NMEA data stream after command prompt
2119 && ( strncmp( pBuf, "$GP", 3 ) == 0 || strncmp( pBuf, "$PFEC", 5 ) == 0 ) ) {
2122 printf ( "%s (refclock_jjy.c) : Skip NMEA stream [%s]\n",
2123 sFunctionName, pBuf ) ;
2126 return JJY_RECEIVE_WAIT ;
2129 /* Check expected reply */
2131 if ( tristate_gps01_command_sequence[up->iCommandSeq].command == NULL ) {
2132 /* Command sequence has not been started, or has been completed */
2133 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
2135 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2136 up->bLineError = TRUE ;
2137 return JJY_RECEIVE_ERROR ;
2140 /* Check reply length */
2142 if ( iLen != tristate_gps01_command_sequence[up->iCommandSeq].iExpectedReplyLength ) {
2143 /* Unexpected reply length */
2144 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
2146 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2147 up->bLineError = TRUE ;
2148 return JJY_RECEIVE_ERROR ;
2153 switch ( tristate_gps01_command_sequence[up->iCommandSeq].commandNumber ) {
2155 case TS_GPS01_COMMAND_NUMBER_DATE : /* YYYY/MM/DD */
2157 rc = sscanf ( pBuf, "%4d/%2d/%2d", &up->year, &up->month, &up->day ) ;
2159 if ( rc != 3 || up->year < 2000 || 2099 <= up->year
2160 || up->month < 1 || 12 < up->month
2161 || up->day < 1 || 31 < up->day ) {
2163 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE,
2164 rc, up->year, up->month, up->day ) ;
2165 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2166 up->bLineError = TRUE ;
2167 return JJY_RECEIVE_ERROR ;
2172 case TS_GPS01_COMMAND_NUMBER_TIME : /* HH:MM:SS */
2174 if ( up->iTimestampCount >= 2 ) {
2175 /* Too many time reply */
2176 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_TOO_MANY_REPLY,
2177 up->iTimestampCount ) ;
2178 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2179 up->bLineError = TRUE ;
2180 return JJY_RECEIVE_ERROR ;
2183 rc = sscanf ( pBuf, "%2d:%2d:%2d",
2184 &up->hour, &up->minute, &up->second ) ;
2187 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
2189 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
2190 rc, up->hour, up->minute, up->second ) ;
2191 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2192 up->bLineError = TRUE ;
2193 return JJY_RECEIVE_ERROR ;
2196 up->iTimestamp[up->iTimestampCount] = ( up->hour * 60 + up->minute ) * 60 + up->second ;
2198 up->iTimestampCount++ ;
2204 case TS_GPS01_COMMAND_NUMBER_STUS :
2206 if ( strncmp( pBuf, TS_GPS01_REPLY_STUS_RTC, TS_GPS01_REPLY_LENGTH_STUS ) == 0
2207 || strncmp( pBuf, TS_GPS01_REPLY_STUS_GPS, TS_GPS01_REPLY_LENGTH_STUS ) == 0
2208 || strncmp( pBuf, TS_GPS01_REPLY_STUS_UTC, TS_GPS01_REPLY_LENGTH_STUS ) == 0
2209 || strncmp( pBuf, TS_GPS01_REPLY_STUS_PPS, TS_GPS01_REPLY_LENGTH_STUS ) == 0 ) {
2212 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
2214 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2215 up->bLineError = TRUE ;
2216 return JJY_RECEIVE_ERROR ;
2221 default : /* Unexpected reply */
2223 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
2225 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2226 up->bLineError = TRUE ;
2227 return JJY_RECEIVE_ERROR ;
2231 if ( up->iTimestampCount == 2 ) {
2232 /* Process date and time */
2234 if ( up->iTimestamp[1] - 2 <= up->iTimestamp[0]
2235 && up->iTimestamp[0] <= up->iTimestamp[1] ) {
2236 /* 3 commands (time,date,stim) was executed in two seconds */
2237 jjy_synctime( peer, pp, up ) ;
2238 return JJY_RECEIVE_DONE ;
2239 } else if ( up->iTimestamp[0] > up->iTimestamp[1] ) {
2240 /* Over midnight, and date is unsure */
2241 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_OVER_MIDNIGHT_2,
2242 up->iTimestamp[0], up->iTimestamp[1] ) ;
2243 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
2244 return JJY_RECEIVE_SKIP ;
2247 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SLOW_REPLY_2,
2248 up->iTimestamp[0], up->iTimestamp[1] ) ;
2249 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2250 up->bLineError = TRUE ;
2251 return JJY_RECEIVE_ERROR ;
2256 if ( tristate_gps01_command_sequence[up->iCommandSeq].command == NULL ) {
2257 /* Command sequence completed */
2258 jjy_synctime( peer, pp, up ) ;
2259 return JJY_RECEIVE_DONE ;
2262 /* Issue next command */
2264 if ( tristate_gps01_command_sequence[up->iCommandSeq].command != NULL ) {
2265 up->iCommandSeq ++ ;
2268 if ( tristate_gps01_command_sequence[up->iCommandSeq].command == NULL ) {
2269 /* Command sequence completed */
2270 up->iProcessState = JJY_PROCESS_STATE_DONE ;
2271 return JJY_RECEIVE_DONE ;
2274 pCmd = tristate_gps01_command_sequence[up->iCommandSeq].command ;
2275 iCmdLen = tristate_gps01_command_sequence[up->iCommandSeq].commandLength ;
2276 if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
2277 refclock_report ( peer, CEVNT_FAULT ) ;
2280 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
2282 return JJY_RECEIVE_WAIT ;
2286 /**************************************************************************************************/
2289 jjy_poll_tristate_gpsclock01 ( int unit, struct peer *peer )
2292 static const char *sFunctionName = "jjy_poll_tristate_gpsclock01" ;
2295 struct refclockproc *pp ;
2296 struct jjyunit *up ;
2301 pp = peer->procptr ;
2304 up->iTimestampCount = 0 ;
2306 if ( ( pp->sloppyclockflag & CLK_FLAG1 ) == 0 ) {
2307 /* Skip "stus" command */
2308 up->iCommandSeq = 1 ;
2309 up->iLineCount = 1 ;
2314 printf ( "%s (refclock_jjy.c) : flag1=%X CLK_FLAG1=%X up->iLineCount=%d\n",
2315 sFunctionName, pp->sloppyclockflag, CLK_FLAG1,
2321 * Send a first command
2324 up->iCommandSeq ++ ;
2326 pCmd = tristate_gps01_command_sequence[up->iCommandSeq].command ;
2327 iCmdLen = tristate_gps01_command_sequence[up->iCommandSeq].commandLength ;
2328 if ( write ( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
2329 refclock_report ( peer, CEVNT_FAULT ) ;
2332 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
2336 /*################################################################################################*/
2337 /*################################################################################################*/
2339 /*## The SEIKO TIME SYSTEMS TDC-300 ##*/
2341 /*## server 127.127.40.X mode 6 ##*/
2343 /*################################################################################################*/
2344 /*################################################################################################*/
2346 /* Type Response Remarks */
2347 /* -------------------- ---------------------------------------- ---------------------------- */
2348 /* Type 1 <STX>HH:MM:SS<ETX> */
2349 /* Type 2 <STX>YYMMDDHHMMSSWLSCU<ETX> W:0(Sun)-6(Sat) */
2350 /* Type 3 <STX>YYMMDDWHHMMSS<ETX> W:0(Sun)-6(Sat) */
2351 /* <STX><xE5><ETX> 5 to 10 mSec. before second */
2353 /*################################################################################################*/
2355 static struct jjyRawDataBreak seiko_tsys_tdc_300_raw_break [ ] =
2357 { "\x03", 1 }, { NULL, 0 }
2360 /**************************************************************************************************/
2363 jjy_start_seiko_tsys_tdc_300 ( int unit, struct peer *peer, struct jjyunit *up )
2366 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: SEIKO TIME SYSTEMS TDC-300" ) ;
2368 up->unittype = UNITTYPE_SEIKO_TIMESYS_TDC_300 ;
2369 up->linespeed = SPEED232_SEIKO_TIMESYS_TDC_300 ;
2370 up->linediscipline = LDISC_RAW ;
2372 up->pRawBreak = seiko_tsys_tdc_300_raw_break ;
2373 up->bWaitBreakString = TRUE ;
2375 up->bSkipCntrlCharOnly = FALSE ;
2381 /**************************************************************************************************/
2384 jjy_receive_seiko_tsys_tdc_300 ( struct recvbuf *rbufp )
2388 struct refclockproc *pp ;
2389 struct jjyunit *up ;
2391 char *pBuf, sLog [ MAX_LOGTEXT ] ;
2397 /* Initialize pointers */
2399 peer = rbufp->recv_peer ;
2400 pp = peer->procptr ;
2403 if ( up->linediscipline == LDISC_RAW ) {
2404 pBuf = up->sTextBuf ;
2405 iLen = up->iTextBufLen ;
2407 pBuf = pp->a_lastcode ;
2408 iLen = pp->lencode ;
2411 DEBUG_PRINTF_JJY_RECEIVE( "jjy_receive_seiko_tsys_tdc_300", iLen ) ;
2414 * TDC-300 sends a timestamp every second.
2415 * So, a timestamp is ignored unless it is right after polled.
2418 if ( up->iProcessState != JJY_PROCESS_STATE_RECEIVE ) {
2419 return JJY_RECEIVE_SKIP ;
2422 /* Process timestamp */
2424 up->iReceiveSeq ++ ;
2428 case 8 : /* Type 1 : <STX>HH:MM:SS<ETX> */
2430 for ( i = 0 ; i < iLen ; i ++ ) {
2434 rc = sscanf ( pBuf+1, "%2d:%2d:%2d",
2435 &up->hour, &up->minute, &up->second ) ;
2438 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
2440 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
2441 rc, up->hour, up->minute, up->second ) ;
2442 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2443 up->bLineError = TRUE ;
2444 return JJY_RECEIVE_ERROR ;
2445 } else if ( up->hour == 23 && up->minute == 59 && up->second >= 55 ) {
2446 /* Uncertainty date guard */
2447 return JJY_RECEIVE_WAIT ;
2451 pTime = localtime( &now ) ;
2452 up->year = pTime->tm_year ;
2453 up->month = pTime->tm_mon + 1 ;
2454 up->day = pTime->tm_mday ;
2458 case 17 : /* Type 2 : <STX>YYMMDDHHMMSSWLSCU<ETX> */
2460 for ( i = 0 ; i < iLen ; i ++ ) {
2464 rc = sscanf ( pBuf+1, "%2d%2d%2d%2d%2d%2d%1d",
2465 &up->year, &up->month, &up->day,
2466 &up->hour, &up->minute, &up->second, &iWeekday ) ;
2469 || up->month < 1 || up->month > 12 || up->day < 1 || up->day > 31
2471 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
2472 /* Invalid date and time */
2473 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
2474 rc, up->year, up->month, up->day,
2475 up->hour, up->minute, up->second ) ;
2476 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2477 up->bLineError = TRUE ;
2478 return JJY_RECEIVE_ERROR ;
2483 case 13 : /* Type 3 : <STX>YYMMDDWHHMMSS<ETX> */
2485 rc = sscanf ( pBuf, "%2d%2d%2d%1d%2d%2d%2d",
2486 &up->year, &up->month, &up->day, &iWeekday,
2487 &up->hour, &up->minute, &up->second ) ;
2490 || up->month < 1 || up->month > 12 || up->day < 1 || up->day > 31
2492 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
2493 /* Invalid date and time */
2494 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATETIME,
2495 rc, up->year, up->month, up->day,
2496 up->hour, up->minute, up->second ) ;
2497 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2498 up->bLineError = TRUE ;
2499 return JJY_RECEIVE_ERROR ;
2502 return JJY_RECEIVE_WAIT ;
2504 case 1 : /* Type 3 : <STX><xE5><ETX> */
2506 if ( ( *pBuf & 0xFF ) != 0xE5 ) {
2507 /* Invalid second signal */
2508 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_REPLY,
2510 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2511 up->bLineError = TRUE ;
2512 return JJY_RECEIVE_ERROR ;
2513 } else if ( up->iReceiveSeq == 1 ) {
2514 /* Wait for next timestamp */
2515 up->iReceiveSeq -- ;
2516 return JJY_RECEIVE_WAIT ;
2517 } else if ( up->iReceiveSeq >= 3 ) {
2518 /* Unexpected second signal */
2519 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
2521 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2522 up->bLineError = TRUE ;
2523 return JJY_RECEIVE_ERROR ;
2528 default : /* Unexpected reply length */
2530 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_INVALID_LENGTH,
2532 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
2533 up->bLineError = TRUE ;
2534 return JJY_RECEIVE_ERROR ;
2541 jjy_synctime( peer, pp, up ) ;
2543 return JJY_RECEIVE_DONE ;
2547 /**************************************************************************************************/
2550 jjy_poll_seiko_tsys_tdc_300 ( int unit, struct peer *peer )
2553 struct refclockproc *pp ;
2554 struct jjyunit *up ;
2556 pp = peer->procptr ;
2559 up->bLineError = FALSE ;
2563 /*################################################################################################*/
2564 /*################################################################################################*/
2566 /*## Telephone JJY ##*/
2568 /*## server 127.127.40.X mode 100 to 180 ##*/
2570 /*################################################################################################*/
2571 /*################################################################################################*/
2573 /* Prompt Command Response Remarks */
2574 /* -------------------- -------------------- -------------------- -------------------------- */
2575 /* Name<SP>?<SP> TJJY<CR> Welcome messages TJJY is a guest user ID */
2576 /* > 4DATE<CR> YYYYMMDD<CR> */
2577 /* > LEAPSEC<CR> XX<CR> One of <SP>0, +1, -1 */
2578 /* > TIME<CR> HHMMSS<CR> 3 times on second */
2579 /* > BYE<CR> Sayounara messages */
2581 /*################################################################################################*/
2583 static struct jjyRawDataBreak teljjy_raw_break [ ] =
2594 #define TELJJY_STATE_IDLE 0
2595 #define TELJJY_STATE_DAILOUT 1
2596 #define TELJJY_STATE_LOGIN 2
2597 #define TELJJY_STATE_CONNECT 3
2598 #define TELJJY_STATE_BYE 4
2600 #define TELJJY_EVENT_NULL 0
2601 #define TELJJY_EVENT_START 1
2602 #define TELJJY_EVENT_CONNECT 2
2603 #define TELJJY_EVENT_DISCONNECT 3
2604 #define TELJJY_EVENT_COMMAND 4
2605 #define TELJJY_EVENT_LOGIN 5 /* Posted by the jjy_receive_telephone */
2606 #define TELJJY_EVENT_PROMPT 6 /* Posted by the jjy_receive_telephone */
2607 #define TELJJY_EVENT_DATA 7 /* Posted by the jjy_receive_telephone */
2608 #define TELJJY_EVENT_ERROR 8 /* Posted by the jjy_receive_telephone */
2609 #define TELJJY_EVENT_SILENT 9 /* Posted by the jjy_timer_telephone */
2610 #define TELJJY_EVENT_TIMEOUT 10 /* Posted by the jjy_timer_telephone */
2612 static void teljjy_control ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2614 static int teljjy_idle_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2615 static int teljjy_idle_dialout ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2616 static int teljjy_dial_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2617 static int teljjy_dial_login ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2618 static int teljjy_dial_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2619 static int teljjy_login_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2620 static int teljjy_login_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2621 static int teljjy_login_conn ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2622 static int teljjy_login_login ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2623 static int teljjy_login_silent ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2624 static int teljjy_login_error ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2625 static int teljjy_conn_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2626 static int teljjy_conn_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2627 static int teljjy_conn_send ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2628 static int teljjy_conn_data ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2629 static int teljjy_conn_silent ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2630 static int teljjy_conn_error ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2631 static int teljjy_bye_ignore ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2632 static int teljjy_bye_disc ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2633 static int teljjy_bye_modem ( struct peer *peer, struct refclockproc *, struct jjyunit * ) ;
2635 static int ( *pTeljjyHandler [ ] [ 5 ] ) ( struct peer *, struct refclockproc *, struct jjyunit *) =
2636 { /*STATE_IDLE STATE_DAILOUT STATE_LOGIN STATE_CONNECT STATE_BYE */
2637 /* NULL */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_ignore },
2638 /* START */ { teljjy_idle_dialout, teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_ignore },
2639 /* CONNECT */ { teljjy_idle_ignore , teljjy_dial_login , teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_ignore },
2640 /* DISCONNECT */ { teljjy_idle_ignore , teljjy_dial_disc , teljjy_login_disc , teljjy_conn_disc , teljjy_bye_disc },
2641 /* COMMAND */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_ignore, teljjy_bye_modem },
2642 /* LOGIN */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_login , teljjy_conn_error , teljjy_bye_ignore },
2643 /* PROMPT */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_conn , teljjy_conn_send , teljjy_bye_ignore },
2644 /* DATA */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_ignore, teljjy_conn_data , teljjy_bye_ignore },
2645 /* ERROR */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_error , teljjy_conn_error , teljjy_bye_ignore },
2646 /* SILENT */ { teljjy_idle_ignore , teljjy_dial_ignore, teljjy_login_silent, teljjy_conn_silent, teljjy_bye_ignore },
2647 /* TIMEOUT */ { teljjy_idle_ignore , teljjy_dial_disc , teljjy_login_error , teljjy_conn_error , teljjy_bye_modem }
2650 static short iTeljjyNextState [ ] [ 5 ] =
2651 { /*STATE_IDLE STATE_DAILOUT STATE_LOGIN STATE_CONNECT STATE_BYE */
2652 /* NULL */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
2653 /* START */ { TELJJY_STATE_DAILOUT, TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
2654 /* CONNECT */ { TELJJY_STATE_IDLE , TELJJY_STATE_LOGIN , TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
2655 /* DISCONNECT */ { TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_IDLE },
2656 /* COMMAND */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
2657 /* LOGIN */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_BYE , TELJJY_STATE_BYE },
2658 /* PROMPT */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_CONNECT, TELJJY_STATE_BYE , TELJJY_STATE_BYE },
2659 /* DATA */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
2660 /* ERROR */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_BYE , TELJJY_STATE_BYE , TELJJY_STATE_BYE },
2661 /* SILENT */ { TELJJY_STATE_IDLE , TELJJY_STATE_DAILOUT, TELJJY_STATE_LOGIN , TELJJY_STATE_CONNECT, TELJJY_STATE_BYE },
2662 /* TIMEOUT */ { TELJJY_STATE_IDLE , TELJJY_STATE_IDLE , TELJJY_STATE_BYE , TELJJY_STATE_BYE , TELJJY_STATE_BYE }
2665 static short iTeljjyPostEvent [ ] [ 5 ] =
2666 { /*STATE_IDLE STATE_DAILOUT STATE_LOGIN STATE_CONNECT STATE_BYE */
2667 /* NULL */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
2668 /* START */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
2669 /* CONNECT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
2670 /* DISCONNECT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
2671 /* COMMAND */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
2672 /* LOGIN */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL },
2673 /* PROMPT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_PROMPT , TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL },
2674 /* DATA */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
2675 /* ERROR */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_COMMAND, TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL },
2676 /* SILENT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_NULL , TELJJY_EVENT_NULL , TELJJY_EVENT_NULL },
2677 /* TIMEOUT */ { TELJJY_EVENT_NULL, TELJJY_EVENT_NULL, TELJJY_EVENT_COMMAND, TELJJY_EVENT_COMMAND, TELJJY_EVENT_NULL }
2680 static short iTeljjySilentTimeout [ 5 ] = { 0, 0, 10, 5, 0 } ;
2681 static short iTeljjyStateTimeout [ 5 ] = { 0, 120, 60, 60, 40 } ;
2683 #define TELJJY_STAY_CLOCK_STATE 0
2684 #define TELJJY_CHANGE_CLOCK_STATE 1
2686 /* Command and replay */
2688 #define TELJJY_REPLY_NONE 0
2689 #define TELJJY_REPLY_4DATE 1
2690 #define TELJJY_REPLY_TIME 2
2691 #define TELJJY_REPLY_LEAPSEC 3
2692 #define TELJJY_REPLY_LOOP 4
2693 #define TELJJY_REPLY_PROMPT 5
2694 #define TELJJY_REPLY_LOOPBACK 6
2695 #define TELJJY_REPLY_COM 7
2697 #define TELJJY_COMMAND_START_SKIP_LOOPBACK 7
2701 const char *command ;
2703 int iEchobackReplyLength ;
2704 int iExpectedReplyType ;
2705 int iExpectedReplyLength ;
2706 } teljjy_command_sequence[] =
2708 { NULL, 0, 0, 0, 0 }, /* Idle */
2709 { "LOOP\r" , 5, 4, TELJJY_REPLY_LOOP , 0 }, /* Getting into loopback mode */
2710 { ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
2711 { ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
2712 { ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
2713 { ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
2714 { ">" , 1, 1, TELJJY_REPLY_LOOPBACK, 0 }, /* Loopback measuring of delay time */
2715 { "COM\r" , 4, 3, TELJJY_REPLY_COM , 0 }, /* Exit from loopback mode */
2716 /* TELJJY_COMMAND_START_SKIP_LOOPBACK */
2717 { "TIME\r" , 5, 4, TELJJY_REPLY_TIME , 6 },
2718 { "4DATE\r" , 6, 5, TELJJY_REPLY_4DATE , 8 },
2719 { "LEAPSEC\r", 8, 7, TELJJY_REPLY_LEAPSEC , 2 },
2720 { "TIME\r" , 5, 4, TELJJY_REPLY_TIME , 6 },
2721 { "BYE\r" , 4, 3, TELJJY_REPLY_NONE , 0 },
2722 /* End of command */
2723 { NULL, 0, 0, 0, 0 }
2726 #define TELJJY_LOOPBACK_DELAY_THRESHOLD 700 /* Milli second */
2729 #define DEBUG_TELJJY_PRINTF(sFunc) { if ( debug ) { printf ( "refclock_jjy.c : %s : iClockState=%d iClockEvent=%d iTeljjySilentTimer=%d iTeljjyStateTimer=%d iClockCommandSeq=%d\n", sFunc, up->iClockState, up->iClockEvent, up->iTeljjySilentTimer, up->iTeljjyStateTimer, up->iClockCommandSeq ) ; } }
2731 #define DEBUG_TELJJY_PRINTF(sFunc)
2734 /**************************************************************************************************/
2737 jjy_start_telephone ( int unit, struct peer *peer, struct jjyunit *up )
2740 char sLog [ 80 ], sFirstThreeDigits [ 4 ] ;
2741 int iNumberOfDigitsOfPhoneNumber, iCommaCount, iCommaPosition ;
2743 size_t iFirstThreeDigitsCount ;
2745 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, "Refclock: Telephone JJY" ) ;
2747 up->unittype = UNITTYPE_TELEPHONE ;
2748 up->linespeed = SPEED232_TELEPHONE ;
2749 up->linediscipline = LDISC_RAW ;
2751 up->pRawBreak = teljjy_raw_break ;
2752 up->bWaitBreakString = TRUE ;
2754 up->bSkipCntrlCharOnly = TRUE ;
2756 up->iClockState = TELJJY_STATE_IDLE ;
2757 up->iClockEvent = TELJJY_EVENT_NULL ;
2759 /* Check the telephone number */
2761 if ( sys_phone[0] == NULL ) {
2762 msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf must be specified." ) ;
2763 up->bInitError = TRUE ;
2767 if ( sys_phone[1] != NULL ) {
2768 msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf should be only one." ) ;
2769 up->bInitError = TRUE ;
2773 iNumberOfDigitsOfPhoneNumber = iCommaCount = iCommaPosition = iFirstThreeDigitsCount = 0 ;
2774 for ( i = 0 ; i < strlen( sys_phone[0] ) ; i ++ ) {
2775 if ( isdigit( (u_char)sys_phone[0][i] ) ) {
2776 if ( iFirstThreeDigitsCount < sizeof(sFirstThreeDigits)-1 ) {
2777 sFirstThreeDigits[iFirstThreeDigitsCount++] = sys_phone[0][i] ;
2779 iNumberOfDigitsOfPhoneNumber ++ ;
2780 } else if ( sys_phone[0][i] == ',' ) {
2782 if ( iCommaCount > 1 ) {
2783 msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf should be zero or one comma." ) ;
2784 up->bInitError = TRUE ;
2787 iFirstThreeDigitsCount = 0 ;
2788 iCommaPosition = i ;
2789 } else if ( sys_phone[0][i] != '-' ) {
2790 msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone in the ntpd.conf should be a number or a hyphen." ) ;
2791 up->bInitError = TRUE ;
2795 sFirstThreeDigits[iFirstThreeDigitsCount] = 0 ;
2797 if ( iCommaCount == 1 ) {
2798 if ( iCommaPosition != 1 || *sys_phone[0] != '0' ) {
2799 msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : Getting an outside line should be '0,'." ) ;
2800 up->bInitError = TRUE ;
2805 if ( iNumberOfDigitsOfPhoneNumber - iCommaPosition < 6 || 10 < iNumberOfDigitsOfPhoneNumber - iCommaPosition ) {
2806 /* Too short or too long */
2807 msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone=%s : Number of digits should be 6 to 10.", sys_phone[0] ) ;
2808 up->bInitError = TRUE ;
2812 if ( strncmp( sFirstThreeDigits + iCommaPosition, "00" , 2 ) == 0
2813 || strncmp( sFirstThreeDigits + iCommaPosition, "10" , 2 ) == 0
2814 || strncmp( sFirstThreeDigits + iCommaPosition, "11" , 2 ) == 0
2815 || strncmp( sFirstThreeDigits + iCommaPosition, "12" , 2 ) == 0
2816 || strncmp( sFirstThreeDigits + iCommaPosition, "171", 3 ) == 0
2817 || strncmp( sFirstThreeDigits + iCommaPosition, "177", 3 ) == 0
2818 || ( sFirstThreeDigits[0] == '0' && sFirstThreeDigits[2] == '0' ) ) {
2819 /* Not allowed because of emergency numbers or special service numbers */
2820 msyslog( LOG_ERR, "refclock_jjy.c : jjy_start_telephone : phone=%s : First 2 or 3 digits are not allowed.", sys_phone[0] ) ;
2821 up->bInitError = TRUE ;
2825 snprintf( sLog, sizeof(sLog), "phone=%s", sys_phone[0] ) ;
2826 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
2828 if ( peer->minpoll < 8 ) {
2829 /* minpoll must be greater or equal to 8 ( 256 seconds = about 4 minutes ) */
2830 int oldminpoll = peer->minpoll ;
2832 if ( peer->ppoll < peer->minpoll ) {
2833 peer->ppoll = peer->minpoll ;
2835 if ( peer->maxpoll < peer->minpoll ) {
2836 peer->maxpoll = peer->minpoll ;
2838 snprintf( sLog, sizeof(sLog), "minpoll=%d -> %d", oldminpoll, peer->minpoll ) ;
2839 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_JJY, sLog ) ;
2846 /**************************************************************************************************/
2849 jjy_receive_telephone ( struct recvbuf *rbufp )
2852 static const char *sFunctionName = "jjy_receive_telephone" ;
2856 struct refclockproc *pp ;
2857 struct jjyunit *up ;
2860 short iPreviousModemState ;
2862 peer = rbufp->recv_peer ;
2863 pp = peer->procptr ;
2866 DEBUG_TELJJY_PRINTF( sFunctionName ) ;
2868 if ( up->iClockState == TELJJY_STATE_IDLE
2869 || up->iClockState == TELJJY_STATE_DAILOUT
2870 || up->iClockState == TELJJY_STATE_BYE ) {
2872 iPreviousModemState = getModemState( up ) ;
2874 modem_receive ( rbufp ) ;
2876 if ( iPreviousModemState != up->iModemState ) {
2877 /* Modem state is changed just now. */
2878 if ( isModemStateDisconnect( up->iModemState ) ) {
2879 up->iClockEvent = TELJJY_EVENT_DISCONNECT ;
2880 teljjy_control ( peer, pp, up ) ;
2881 } else if ( isModemStateConnect( up->iModemState ) ) {
2882 up->iClockEvent = TELJJY_EVENT_CONNECT ;
2883 teljjy_control ( peer, pp, up ) ;
2887 return JJY_RECEIVE_WAIT ;
2891 if ( up->linediscipline == LDISC_RAW ) {
2892 pBuf = up->sTextBuf ;
2893 iLen = up->iTextBufLen ;
2895 pBuf = pp->a_lastcode ;
2896 iLen = pp->lencode ;
2899 up->iTeljjySilentTimer = 0 ;
2900 if ( iLen == 7 && strncmp( pBuf, "Name ? ", 7 ) == 0 ) { up->iClockEvent = TELJJY_EVENT_LOGIN ; }
2901 else if ( iLen == 1 && strncmp( pBuf, ">" , 1 ) == 0 ) { up->iClockEvent = TELJJY_EVENT_PROMPT ; }
2902 else if ( iLen >= 1 && strncmp( pBuf, "?" , 1 ) == 0 ) { up->iClockEvent = TELJJY_EVENT_ERROR ; }
2903 else { up->iClockEvent = TELJJY_EVENT_DATA ; }
2905 teljjy_control ( peer, pp, up ) ;
2907 return JJY_RECEIVE_WAIT ;
2911 /**************************************************************************************************/
2914 jjy_poll_telephone ( int unit, struct peer *peer )
2917 static const char *sFunctionName = "jjy_poll_telephone" ;
2920 struct refclockproc *pp ;
2921 struct jjyunit *up ;
2923 pp = peer->procptr ;
2926 DEBUG_TELJJY_PRINTF( sFunctionName ) ;
2928 if ( up->iClockState == TELJJY_STATE_IDLE ) {
2929 up->iRawBufLen = 0 ;
2930 up->iLineBufLen = 0 ;
2931 up->iTextBufLen = 0 ;
2934 up->iClockEvent = TELJJY_EVENT_START ;
2935 teljjy_control ( peer, pp, up ) ;
2939 /**************************************************************************************************/
2942 jjy_timer_telephone ( int unit, struct peer *peer )
2945 static const char *sFunctionName = "jjy_timer_telephone" ;
2948 struct refclockproc *pp ;
2949 struct jjyunit *up ;
2950 short iPreviousModemState ;
2952 pp = peer->procptr ;
2955 DEBUG_TELJJY_PRINTF( sFunctionName ) ;
2957 if ( iTeljjySilentTimeout[up->iClockState] != 0 ) {
2958 up->iTeljjySilentTimer++ ;
2959 if ( iTeljjySilentTimeout[up->iClockState] <= up->iTeljjySilentTimer ) {
2960 up->iClockEvent = TELJJY_EVENT_SILENT ;
2961 teljjy_control ( peer, pp, up ) ;
2965 if ( iTeljjyStateTimeout[up->iClockState] != 0 ) {
2966 up->iTeljjyStateTimer++ ;
2967 if ( iTeljjyStateTimeout[up->iClockState] <= up->iTeljjyStateTimer ) {
2968 up->iClockEvent = TELJJY_EVENT_TIMEOUT ;
2969 teljjy_control ( peer, pp, up ) ;
2973 if ( isModemStateTimerOn( up ) ) {
2975 iPreviousModemState = getModemState( up ) ;
2977 modem_timer ( unit, peer ) ;
2979 if ( iPreviousModemState != up->iModemState ) {
2980 /* Modem state is changed just now. */
2981 if ( isModemStateDisconnect( up->iModemState ) ) {
2982 up->iClockEvent = TELJJY_EVENT_DISCONNECT ;
2983 teljjy_control ( peer, pp, up ) ;
2984 } else if ( isModemStateConnect( up->iModemState ) ) {
2985 up->iClockEvent = TELJJY_EVENT_CONNECT ;
2986 teljjy_control ( peer, pp, up ) ;
2994 /**************************************************************************************************/
2997 teljjy_control ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3001 short iPostEvent = TELJJY_EVENT_NULL ;
3003 DEBUG_TELJJY_PRINTF( "teljjy_control" ) ;
3005 rc = (*pTeljjyHandler[up->iClockEvent][up->iClockState])( peer, pp, up ) ;
3007 if ( rc == TELJJY_CHANGE_CLOCK_STATE ) {
3008 iPostEvent = iTeljjyPostEvent[up->iClockEvent][up->iClockState] ;
3011 printf( "refclock_jjy.c : teljjy_control : iClockState=%hd -> %hd iPostEvent=%hd\n",
3012 up->iClockState, iTeljjyNextState[up->iClockEvent][up->iClockState], iPostEvent ) ;
3015 up->iTeljjySilentTimer = 0 ;
3016 if ( up->iClockState != iTeljjyNextState[up->iClockEvent][up->iClockState] ) {
3017 /* Telephone JJY state is changing now */
3018 up->iTeljjyStateTimer = 0 ;
3019 up->bLineError = FALSE ;
3020 up->iClockCommandSeq = 0 ;
3021 up->iTimestampCount = 0 ;
3022 up->iLoopbackCount = 0 ;
3023 for ( i = 0 ; i < MAX_LOOPBACK ; i ++ ) {
3024 up->bLoopbackTimeout[i] = FALSE ;
3026 if (iTeljjyNextState[up->iClockEvent][up->iClockState] == TELJJY_STATE_IDLE ) {
3027 /* Telephone JJY state is changing to IDLE just now */
3028 up->iProcessState = JJY_PROCESS_STATE_DONE ;
3031 up->iClockState = iTeljjyNextState[up->iClockEvent][up->iClockState] ;
3035 if ( iPostEvent != TELJJY_EVENT_NULL ) {
3036 up->iClockEvent = iPostEvent ;
3037 teljjy_control ( peer, pp, up ) ;
3040 up->iClockEvent = TELJJY_EVENT_NULL ;
3044 /**************************************************************************************************/
3047 teljjy_setDelay ( struct peer *peer, struct jjyunit *up )
3051 int milliSecond, microSecond ;
3053 gettimeofday( &(up->delayTime[up->iLoopbackCount]), NULL ) ;
3055 up->delayTime[up->iLoopbackCount].tv_sec -= up->sendTime[up->iLoopbackCount].tv_sec ;
3056 up->delayTime[up->iLoopbackCount].tv_usec -= up->sendTime[up->iLoopbackCount].tv_usec ;
3057 if ( up->delayTime[up->iLoopbackCount].tv_usec < 0 ) {
3058 up->delayTime[up->iLoopbackCount].tv_sec -- ;
3059 up->delayTime[up->iLoopbackCount].tv_usec += 1000000 ;
3062 milliSecond = up->delayTime[up->iLoopbackCount].tv_usec / 1000 ;
3063 microSecond = up->delayTime[up->iLoopbackCount].tv_usec - milliSecond * 1000 ;
3064 milliSecond += up->delayTime[up->iLoopbackCount].tv_sec * 1000 ;
3066 snprintf( sLog, sizeof(sLog), JJY_CLOCKSTATS_MESSAGE_LOOPBACK_DELAY,
3067 milliSecond, microSecond ) ;
3069 if ( milliSecond > TELJJY_LOOPBACK_DELAY_THRESHOLD ) {
3070 /* Delay > 700 mS */
3071 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_WARNING, sLog ) ;
3073 /* Delay <= 700 mS */
3074 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
3079 /**************************************************************************************************/
3082 teljjy_getDelay ( struct peer *peer, struct jjyunit *up )
3085 struct timeval maxTime, minTime, averTime ;
3087 int minIndex = 0, maxIndex = 0, iAverCount = 0 ;
3088 int iThresholdSecond, iThresholdMicroSecond ;
3091 minTime.tv_sec = minTime.tv_usec = 0 ;
3092 maxTime.tv_sec = maxTime.tv_usec = 0 ;
3094 iThresholdSecond = TELJJY_LOOPBACK_DELAY_THRESHOLD / 1000 ;
3095 iThresholdMicroSecond = ( TELJJY_LOOPBACK_DELAY_THRESHOLD - ( TELJJY_LOOPBACK_DELAY_THRESHOLD / 1000 ) * 1000 ) * 1000 ;
3097 up->iLoopbackValidCount = 0 ;
3099 for ( i = 0 ; i < MAX_LOOPBACK && i < up->iLoopbackCount ; i ++ ) {
3100 if ( up->bLoopbackTimeout[i]
3101 || up->delayTime[i].tv_sec > iThresholdSecond
3102 || ( up->delayTime[i].tv_sec == iThresholdSecond
3103 && up->delayTime[i].tv_usec > iThresholdMicroSecond ) ) {
3106 if ( up->iLoopbackValidCount == 0 ) {
3107 minTime.tv_sec = up->delayTime[i].tv_sec ;
3108 minTime.tv_usec = up->delayTime[i].tv_usec ;
3109 maxTime.tv_sec = up->delayTime[i].tv_sec ;
3110 maxTime.tv_usec = up->delayTime[i].tv_usec ;
3111 minIndex = maxIndex = i ;
3112 } else if ( minTime.tv_sec > up->delayTime[i].tv_sec
3113 || ( minTime.tv_sec == up->delayTime[i].tv_sec
3114 && minTime.tv_usec > up->delayTime[i].tv_usec ) ) {
3115 minTime.tv_sec = up->delayTime[i].tv_sec ;
3116 minTime.tv_usec = up->delayTime[i].tv_usec ;
3118 } else if ( maxTime.tv_sec < up->delayTime[i].tv_sec
3119 || ( maxTime.tv_sec == up->delayTime[i].tv_sec
3120 && maxTime.tv_usec < up->delayTime[i].tv_usec ) ) {
3121 maxTime.tv_sec = up->delayTime[i].tv_sec ;
3122 maxTime.tv_usec = up->delayTime[i].tv_usec ;
3125 up->iLoopbackValidCount ++ ;
3128 if ( up->iLoopbackValidCount < 2 ) {
3132 averTime.tv_usec = 0;
3134 for ( i = 0 ; i < MAX_LOOPBACK && i < up->iLoopbackCount ; i ++ ) {
3135 if ( up->bLoopbackTimeout[i]
3136 || up->delayTime[i].tv_sec > iThresholdSecond
3137 || ( up->delayTime[i].tv_sec == iThresholdSecond
3138 && up->delayTime[i].tv_usec > iThresholdMicroSecond ) ) {
3141 if ( up->iLoopbackValidCount >= 3 && i == maxIndex ) {
3144 if ( up->iLoopbackValidCount >= 4 && i == minIndex ) {
3147 averTime.tv_usec += up->delayTime[i].tv_usec ;
3151 if ( iAverCount == 0 ) {
3152 /* This is never happened. */
3153 /* Previous for-if-for blocks assure iAverCount > 0. */
3154 /* This code avoids a claim by the coverity scan tool. */
3158 /* mode 101 = 1%, mode 150 = 50%, mode 180 = 80% */
3160 iPercent = ( peer->ttl - 100 ) ;
3162 /* Average delay time in milli second */
3164 return ( ( averTime.tv_usec / iAverCount ) * iPercent ) / 100000 ;
3168 /******************************/
3170 teljjy_idle_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3173 DEBUG_TELJJY_PRINTF( "teljjy_idle_ignore" ) ;
3175 return TELJJY_STAY_CLOCK_STATE ;
3179 /******************************/
3181 teljjy_idle_dialout ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3184 DEBUG_TELJJY_PRINTF( "teljjy_idle_dialout" ) ;
3186 modem_connect ( peer->refclkunit, peer ) ;
3188 return TELJJY_CHANGE_CLOCK_STATE ;
3192 /******************************/
3194 teljjy_dial_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3197 DEBUG_TELJJY_PRINTF( "teljjy_dial_ignore" ) ;
3199 return TELJJY_STAY_CLOCK_STATE ;
3203 /******************************/
3205 teljjy_dial_login ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3208 DEBUG_TELJJY_PRINTF( "teljjy_dial_login" ) ;
3210 return TELJJY_CHANGE_CLOCK_STATE ;
3214 /******************************/
3216 teljjy_dial_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3219 DEBUG_TELJJY_PRINTF( "teljjy_dial_disc" ) ;
3221 return TELJJY_CHANGE_CLOCK_STATE ;
3225 /******************************/
3227 teljjy_login_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3230 DEBUG_TELJJY_PRINTF( "teljjy_login_ignore" ) ;
3232 return TELJJY_STAY_CLOCK_STATE ;
3236 /******************************/
3238 teljjy_login_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3241 DEBUG_TELJJY_PRINTF( "teljjy_login_disc" ) ;
3243 return TELJJY_CHANGE_CLOCK_STATE ;
3247 /******************************/
3249 teljjy_login_conn ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3254 DEBUG_TELJJY_PRINTF( "teljjy_login_conn" ) ;
3256 up->bLineError = FALSE ;
3257 up->iClockCommandSeq = 0 ;
3258 up->iTimestampCount = 0 ;
3259 up->iLoopbackCount = 0 ;
3260 for ( i = 0 ; i < MAX_LOOPBACK ; i ++ ) {
3261 up->bLoopbackTimeout[i] = FALSE ;
3264 return TELJJY_CHANGE_CLOCK_STATE ;
3268 /******************************/
3270 teljjy_login_login ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3276 DEBUG_TELJJY_PRINTF( "teljjy_login_login" ) ;
3278 /* Send a guest user ID */
3282 iCmdLen = strlen( pCmd ) ;
3283 if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
3284 refclock_report( peer, CEVNT_FAULT ) ;
3287 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
3289 return TELJJY_STAY_CLOCK_STATE ;
3293 /******************************/
3295 teljjy_login_silent ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3298 DEBUG_TELJJY_PRINTF( "teljjy_login_silent" ) ;
3300 if ( write( pp->io.fd, "\r", 1 ) != 1 ) {
3301 refclock_report( peer, CEVNT_FAULT ) ;
3304 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, "\r" ) ;
3306 up->iTeljjySilentTimer = 0 ;
3308 return TELJJY_CHANGE_CLOCK_STATE ;
3312 /******************************/
3314 teljjy_login_error ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3317 DEBUG_TELJJY_PRINTF( "teljjy_login_error" ) ;
3319 return TELJJY_CHANGE_CLOCK_STATE ;
3323 /******************************/
3325 teljjy_conn_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3328 DEBUG_TELJJY_PRINTF( "teljjy_conn_ignore" ) ;
3330 return TELJJY_STAY_CLOCK_STATE ;
3334 /******************************/
3336 teljjy_conn_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3339 DEBUG_TELJJY_PRINTF( "teljjy_conn_disc" ) ;
3341 return TELJJY_CHANGE_CLOCK_STATE ;
3345 /******************************/
3347 teljjy_conn_send ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3351 int i, iLen, iNextClockState ;
3354 DEBUG_TELJJY_PRINTF( "teljjy_conn_send" ) ;
3356 if ( up->iClockCommandSeq > 0
3357 && teljjy_command_sequence[up->iClockCommandSeq].command == NULL ) {
3358 /* Command sequence has been completed */
3359 return TELJJY_CHANGE_CLOCK_STATE ;
3362 if ( up->iClockCommandSeq == 0 && peer->ttl == 100 ) {
3365 up->iClockCommandSeq = TELJJY_COMMAND_START_SKIP_LOOPBACK ;
3367 } else if ( up->iClockCommandSeq == 0 && peer->ttl != 100 ) {
3368 /* Loopback start */
3370 up->iLoopbackCount = 0 ;
3371 for ( i = 0 ; i < MAX_LOOPBACK ; i ++ ) {
3372 up->bLoopbackTimeout[i] = FALSE ;
3375 } else if ( up->iClockCommandSeq > 0 && peer->ttl != 100
3376 && teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK
3377 && up->iLoopbackCount < MAX_LOOPBACK ) {
3378 /* Loopback character comes */
3381 printf( "refclock_jjy.c : teljjy_conn_send : iClockCommandSeq=%d iLoopbackCount=%d\n",
3382 up->iClockCommandSeq, up->iLoopbackCount ) ;
3386 teljjy_setDelay( peer, up ) ;
3388 up->iLoopbackCount ++ ;
3392 up->iClockCommandSeq++ ;
3394 pCmd = teljjy_command_sequence[up->iClockCommandSeq].command ;
3395 iLen = teljjy_command_sequence[up->iClockCommandSeq].commandLength ;
3397 if ( pCmd != NULL ) {
3399 if ( write( pp->io.fd, pCmd, iLen ) != iLen ) {
3400 refclock_report( peer, CEVNT_FAULT ) ;
3403 if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK ) {
3404 /* Loopback character and timestamp */
3405 if ( up->iLoopbackCount < MAX_LOOPBACK ) {
3406 gettimeofday( &(up->sendTime[up->iLoopbackCount]), NULL ) ;
3407 up->bLoopbackMode = TRUE ;
3409 /* This else-block is never come. */
3410 /* This code avoid wrong report of the coverity static analysis scan tool. */
3411 snprintf( sLog, sizeof(sLog)-1, "refclock_jjy.c ; teljjy_conn_send ; iClockCommandSeq=%d iLoopbackCount=%d MAX_LOOPBACK=%d",
3412 up->iClockCommandSeq, up->iLoopbackCount, MAX_LOOPBACK ) ;
3413 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_BUG, sLog ) ;
3414 msyslog ( LOG_ERR, "%s", sLog ) ;
3415 up->bLoopbackMode = FALSE ;
3418 /* Regular command */
3419 up->bLoopbackMode = FALSE ;
3422 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
3424 if ( teljjy_command_sequence[up->iClockCommandSeq+1].command == NULL ) {
3425 /* Last command of the command sequence */
3426 iNextClockState = TELJJY_CHANGE_CLOCK_STATE ;
3428 /* More commands to be issued */
3429 iNextClockState = TELJJY_STAY_CLOCK_STATE ;
3434 iNextClockState = TELJJY_CHANGE_CLOCK_STATE ;
3438 return iNextClockState ;
3442 /******************************/
3444 teljjy_conn_data ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3449 char sLog [ MAX_LOGTEXT ] ;
3453 DEBUG_TELJJY_PRINTF( "teljjy_conn_data" ) ;
3455 if ( up->linediscipline == LDISC_RAW ) {
3456 pBuf = up->sTextBuf ;
3457 iLen = up->iTextBufLen ;
3459 pBuf = pp->a_lastcode ;
3460 iLen = pp->lencode ;
3463 if ( teljjy_command_sequence[up->iClockCommandSeq].iEchobackReplyLength == iLen
3464 && teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK
3465 && up->sTextBuf[0] == *(teljjy_command_sequence[up->iClockCommandSeq].command)
3466 && up->iLoopbackCount < MAX_LOOPBACK ) {
3469 teljjy_setDelay( peer, up ) ;
3471 up->iLoopbackCount ++ ;
3473 } else if ( teljjy_command_sequence[up->iClockCommandSeq].iEchobackReplyLength == iLen
3474 && strncmp( pBuf, teljjy_command_sequence[up->iClockCommandSeq].command, iLen ) == 0 ) {
3475 /* Maybe echoback */
3477 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, JJY_CLOCKSTATS_MESSAGE_ECHOBACK ) ;
3479 } else if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyLength == iLen
3480 && teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_4DATE ) {
3481 /* 4DATE<CR> -> YYYYMMDD<CR> */
3483 rc = sscanf ( pBuf, "%4d%2d%2d", &up->year, &up->month, &up->day ) ;
3485 if ( rc != 3 || up->year < 2000 || 2099 <= up->year
3486 || up->month < 1 || 12 < up->month || up->day < 1 || 31 < up->day ) {
3488 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_DATE,
3489 rc, up->year, up->month, up->day ) ;
3490 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
3491 up->bLineError = TRUE ;
3494 } else if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyLength == iLen
3495 && teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LEAPSEC
3496 && ( strncmp( pBuf, " 0", 2 ) == 0 || strncmp( pBuf, "+1", 2 ) == 0 || strncmp( pBuf, "-1", 2 ) == 0 ) ) {
3497 /* LEAPSEC<CR> -> XX<CR> ( One of <SP>0, +1, -1 ) */
3499 rc = sscanf ( pBuf, "%2d", &up->leapsecond ) ;
3501 if ( rc != 1 || up->leapsecond < -1 || 1 < up->leapsecond ) {
3502 /* Invalid leap second */
3503 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_LEAP,
3505 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
3506 up->bLineError = TRUE ;
3509 } else if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyLength == iLen
3510 && teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_TIME ) {
3511 /* TIME<CR> -> HHMMSS<CR> ( 3 times on second ) */
3513 rc = sscanf ( pBuf, "%2d%2d%2d", &up->hour, &up->minute, &up->second ) ;
3515 if ( rc != 3 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
3517 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_SSCANF_INVALID_TIME,
3518 rc, up->hour, up->minute, up->second ) ;
3519 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
3520 up->bLineError = TRUE ;
3522 up->iTimestamp[up->iTimestampCount] = ( up->hour * 60 + up->minute ) * 60 + up->second ;
3524 up->iTimestampCount++ ;
3526 if ( up->iTimestampCount == 6 && ! up->bLineError ) {
3528 printf( "refclock_jjy.c : teljjy_conn_data : bLineError=%d iTimestamp=%d, %d, %d\n",
3530 up->iTimestamp[3], up->iTimestamp[4], up->iTimestamp[5] ) ;
3532 bAdjustment = TRUE ;
3534 if ( peer->ttl == 100 ) {
3538 /* mode=101 to 110 */
3539 up->msecond = teljjy_getDelay( peer, up ) ;
3540 if (up->msecond < 0 ) {
3542 bAdjustment = FALSE ;
3546 if ( ( up->iTimestamp[3] - 15 ) <= up->iTimestamp[2]
3547 && up->iTimestamp[2] <= up->iTimestamp[3]
3548 && ( up->iTimestamp[3] + 1 ) == up->iTimestamp[4]
3549 && ( up->iTimestamp[4] + 1 ) == up->iTimestamp[5] ) {
3550 /* Non over midnight */
3552 jjy_synctime( peer, pp, up ) ;
3554 if ( peer->ttl != 100 ) {
3555 if ( bAdjustment ) {
3556 snprintf( sLog, sizeof(sLog),
3557 JJY_CLOCKSTATS_MESSAGE_DELAY_ADJUST,
3558 up->msecond, up->iLoopbackValidCount, MAX_LOOPBACK ) ;
3559 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_INFORMATION, sLog ) ;
3561 snprintf( sLog, sizeof(sLog),
3562 JJY_CLOCKSTATS_MESSAGE_DELAY_UNADJUST,
3563 up->iLoopbackValidCount, MAX_LOOPBACK ) ;
3564 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
3571 } else if ( teljjy_command_sequence[up->iClockCommandSeq].iEchobackReplyLength != iLen
3572 && teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK ) {
3573 /* Loopback noise ( Unexpected replay ) */
3575 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_IGNORE_REPLY,
3577 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_WARNING, sLog ) ;
3581 up->bLineError = TRUE ;
3583 snprintf( sLog, sizeof(sLog)-1, JJY_CLOCKSTATS_MESSAGE_UNEXPECTED_REPLY,
3585 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_ERROR, sLog ) ;
3589 return TELJJY_STAY_CLOCK_STATE ;
3593 /******************************/
3595 teljjy_conn_silent ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3600 DEBUG_TELJJY_PRINTF( "teljjy_conn_silent" ) ;
3602 if ( up->iClockCommandSeq >= 1
3603 && up->iClockCommandSeq < TELJJY_COMMAND_START_SKIP_LOOPBACK ) {
3607 printf( "refclock_jjy.c : teljjy_conn_silent : call teljjy_conn_send\n" ) ;
3610 if ( teljjy_command_sequence[up->iClockCommandSeq].iExpectedReplyType == TELJJY_REPLY_LOOPBACK ) {
3611 up->bLoopbackTimeout[up->iLoopbackCount] = TRUE ;
3613 up->iTeljjySilentTimer = 0 ;
3614 return teljjy_conn_send( peer, pp, up ) ;
3619 if ( write( pp->io.fd, pCmd, 1 ) != 1 ) {
3620 refclock_report( peer, CEVNT_FAULT ) ;
3623 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
3625 up->iTeljjySilentTimer = 0 ;
3627 return TELJJY_STAY_CLOCK_STATE ;
3631 /******************************/
3633 teljjy_conn_error ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3636 DEBUG_TELJJY_PRINTF( "teljjy_conn_error" ) ;
3638 return TELJJY_CHANGE_CLOCK_STATE ;
3642 /******************************/
3644 teljjy_bye_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3647 DEBUG_TELJJY_PRINTF( "teljjy_bye_ignore" ) ;
3649 return TELJJY_STAY_CLOCK_STATE ;
3653 /******************************/
3655 teljjy_bye_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3658 DEBUG_TELJJY_PRINTF( "teljjy_bye_disc" ) ;
3660 return TELJJY_CHANGE_CLOCK_STATE ;
3664 /******************************/
3666 teljjy_bye_modem ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3669 DEBUG_TELJJY_PRINTF( "teljjy_bye_modem" ) ;
3671 modem_disconnect ( peer->refclkunit, peer ) ;
3673 return TELJJY_STAY_CLOCK_STATE ;
3677 /*################################################################################################*/
3678 /*################################################################################################*/
3680 /*## Modem control finite state machine ##*/
3682 /*################################################################################################*/
3683 /*################################################################################################*/
3685 /* struct jjyunit.iModemState */
3687 #define MODEM_STATE_DISCONNECT 0
3688 #define MODEM_STATE_INITIALIZE 1
3689 #define MODEM_STATE_DAILING 2
3690 #define MODEM_STATE_CONNECT 3
3691 #define MODEM_STATE_ESCAPE 4
3693 /* struct jjyunit.iModemEvent */
3695 #define MODEM_EVENT_NULL 0
3696 #define MODEM_EVENT_INITIALIZE 1
3697 #define MODEM_EVENT_DIALOUT 2
3698 #define MODEM_EVENT_DISCONNECT 3
3699 #define MODEM_EVENT_RESP_OK 4
3700 #define MODEM_EVENT_RESP_CONNECT 5
3701 #define MODEM_EVENT_RESP_RING 6
3702 #define MODEM_EVENT_RESP_NO_CARRIER 7
3703 #define MODEM_EVENT_RESP_ERROR 8
3704 #define MODEM_EVENT_RESP_CONNECT_X 9
3705 #define MODEM_EVENT_RESP_NO_DAILTONE 10
3706 #define MODEM_EVENT_RESP_BUSY 11
3707 #define MODEM_EVENT_RESP_NO_ANSWER 12
3708 #define MODEM_EVENT_RESP_UNKNOWN 13
3709 #define MODEM_EVENT_SILENT 14
3710 #define MODEM_EVENT_TIMEOUT 15
3712 /* Function prototypes */
3714 static void modem_control ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3716 static int modem_disc_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3717 static int modem_disc_init ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3718 static int modem_init_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3719 static int modem_init_start ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3720 static int modem_init_disc ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3721 static int modem_init_resp00 ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3722 static int modem_init_resp04 ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3723 static int modem_dial_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3724 static int modem_dial_dialout ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3725 static int modem_dial_escape ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3726 static int modem_dial_connect ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3727 static int modem_dial_disc ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3728 static int modem_conn_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3729 static int modem_conn_escape ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3730 static int modem_esc_ignore ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3731 static int modem_esc_escape ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3732 static int modem_esc_data ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3733 static int modem_esc_silent ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3734 static int modem_esc_disc ( struct peer *, struct refclockproc *, struct jjyunit * ) ;
3736 static int ( *pModemHandler [ ] [ 5 ] ) ( struct peer *, struct refclockproc *, struct jjyunit * ) =
3737 { /*STATE_DISCONNECT STATE_INITIALIZE STATE_DAILING STATE_CONNECT STATE_ESCAPE */
3738 /* NULL */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_ignore },
3739 /* INITIALIZE */ { modem_disc_init , modem_init_start , modem_dial_ignore , modem_conn_ignore, modem_esc_ignore },
3740 /* DIALOUT */ { modem_disc_ignore, modem_init_ignore, modem_dial_dialout, modem_conn_ignore, modem_esc_ignore },
3741 /* DISCONNECT */ { modem_disc_ignore, modem_init_disc , modem_dial_escape , modem_conn_escape, modem_esc_escape },
3742 /* RESP: 0: OK */ { modem_disc_ignore, modem_init_resp00, modem_dial_ignore , modem_conn_ignore, modem_esc_data },
3743 /* RESP: 1: CONNECT */ { modem_disc_ignore, modem_init_ignore, modem_dial_connect, modem_conn_ignore, modem_esc_data },
3744 /* RESP: 2: RING */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_data },
3745 /* RESP: 3: NO CARRIER */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
3746 /* RESP: 4: ERROR */ { modem_disc_ignore, modem_init_resp04, modem_dial_disc , modem_conn_ignore, modem_esc_data },
3747 /* RESP: 5: CONNECT */ { modem_disc_ignore, modem_init_ignore, modem_dial_connect, modem_conn_ignore, modem_esc_data },
3748 /* RESP: 6: NO DAILTONE */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
3749 /* RESP: 7: BUSY */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
3750 /* RESP: 8: NO ANSWER */ { modem_disc_ignore, modem_init_ignore, modem_dial_disc , modem_conn_ignore, modem_esc_data },
3751 /* RESP: 9: UNKNOWN */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_data },
3752 /* SILENT */ { modem_disc_ignore, modem_init_ignore, modem_dial_ignore , modem_conn_ignore, modem_esc_silent },
3753 /* TIMEOUT */ { modem_disc_ignore, modem_init_disc , modem_dial_escape , modem_conn_escape, modem_esc_disc }
3756 static short iModemNextState [ ] [ 5 ] =
3757 { /*STATE_DISCONNECT STATE_INITIALIZE STATE_DAILING STATE_CONNECT STATE_ESCAPE */
3758 /* NULL */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3759 /* INITIALIZE */ { MODEM_STATE_INITIALIZE, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3760 /* DIALOUT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3761 /* DISCONNECT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DISCONNECT, MODEM_STATE_ESCAPE , MODEM_STATE_ESCAPE , MODEM_STATE_ESCAPE },
3762 /* RESP: 0: OK */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DAILING , MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3763 /* RESP: 1: CONNECT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_CONNECT , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3764 /* RESP: 2: RING */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3765 /* RESP: 3: NO CARRIER */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3766 /* RESP: 4: ERROR */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DAILING , MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3767 /* RESP: 5: CONNECT X */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_CONNECT , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3768 /* RESP: 6: NO DAILTONE */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3769 /* RESP: 7: BUSY */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3770 /* RESP: 8: NO ANSWER */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DISCONNECT, MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3771 /* RESP: 9: UNKNOWN */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_ESCAPE },
3772 /* SILENT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_INITIALIZE, MODEM_STATE_DAILING , MODEM_STATE_CONNECT, MODEM_STATE_DISCONNECT },
3773 /* TIMEOUT */ { MODEM_STATE_DISCONNECT, MODEM_STATE_DISCONNECT, MODEM_STATE_ESCAPE , MODEM_STATE_ESCAPE , MODEM_STATE_DISCONNECT }
3776 static short iModemPostEvent [ ] [ 5 ] =
3777 { /*STATE_DISCONNECT STATE_INITIALIZE STATE_DAILING STATE_CONNECT STATE_ESCAPE */
3778 /* NULL */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3779 /* INITIALIZE */ { MODEM_EVENT_INITIALIZE, MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3780 /* DIALOUT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3781 /* DISCONNECT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_DISCONNECT, MODEM_EVENT_DISCONNECT, MODEM_EVENT_NULL },
3782 /* RESP: 0: OK */ { MODEM_EVENT_NULL , MODEM_EVENT_DIALOUT, MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3783 /* RESP: 1: CONNECT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3784 /* RESP: 2: RING */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3785 /* RESP: 3: NO CARRIER */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3786 /* RESP: 4: ERROR */ { MODEM_EVENT_NULL , MODEM_EVENT_DIALOUT, MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3787 /* RESP: 5: CONNECT X */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3788 /* RESP: 6: NO DAILTONE */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3789 /* RESP: 7: BUSY */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3790 /* RESP: 8: NO ANSWER */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3791 /* RESP: 9: UNKNOWN */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3792 /* SILENT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_NULL },
3793 /* TIMEOUT */ { MODEM_EVENT_NULL , MODEM_EVENT_NULL , MODEM_EVENT_DISCONNECT, MODEM_EVENT_DISCONNECT, MODEM_EVENT_NULL }
3796 static short iModemSilentTimeout [ 5 ] = { 0, 0, 0, 0, 5 } ;
3797 static short iModemStateTimeout [ 5 ] = { 0, 20, 90, 0, 20 } ;
3799 #define STAY_MODEM_STATE 0
3800 #define CHANGE_MODEM_STATE 1
3803 #define DEBUG_MODEM_PRINTF(sFunc) { if ( debug ) { printf ( "refclock_jjy.c : %s : iModemState=%d iModemEvent=%d iModemSilentTimer=%d iModemStateTimer=%d\n", sFunc, up->iModemState, up->iModemEvent, up->iModemSilentTimer, up->iModemStateTimer ) ; } }
3805 #define DEBUG_MODEM_PRINTF(sFunc)
3808 /**************************************************************************************************/
3811 getModemState ( struct jjyunit *up )
3813 return up->iModemState ;
3816 /**************************************************************************************************/
3819 isModemStateConnect ( short iCheckState )
3821 return ( iCheckState == MODEM_STATE_CONNECT ) ;
3824 /**************************************************************************************************/
3827 isModemStateDisconnect ( short iCheckState )
3829 return ( iCheckState == MODEM_STATE_DISCONNECT ) ;
3832 /**************************************************************************************************/
3835 isModemStateTimerOn ( struct jjyunit *up )
3837 return ( iModemSilentTimeout[up->iModemState] != 0 || iModemStateTimeout[up->iModemState] != 0 ) ;
3840 /**************************************************************************************************/
3843 modem_connect ( int unit, struct peer *peer )
3845 struct refclockproc *pp;
3848 pp = peer->procptr ;
3851 DEBUG_MODEM_PRINTF( "modem_connect" ) ;
3853 up->iModemEvent = MODEM_EVENT_INITIALIZE ;
3855 modem_control ( peer, pp, up ) ;
3859 /**************************************************************************************************/
3862 modem_disconnect ( int unit, struct peer *peer )
3864 struct refclockproc *pp;
3867 pp = peer->procptr ;
3870 DEBUG_MODEM_PRINTF( "modem_disconnect" ) ;
3872 up->iModemEvent = MODEM_EVENT_DISCONNECT ;
3874 modem_control ( peer, pp, up ) ;
3878 /**************************************************************************************************/
3881 modem_receive ( struct recvbuf *rbufp )
3886 struct refclockproc *pp;
3891 static const char *sFunctionName = "modem_receive" ;
3894 peer = rbufp->recv_peer ;
3895 pp = peer->procptr ;
3898 DEBUG_MODEM_PRINTF( sFunctionName ) ;
3900 if ( up->linediscipline == LDISC_RAW ) {
3901 pBuf = up->sTextBuf ;
3902 iLen = up->iTextBufLen ;
3904 pBuf = pp->a_lastcode ;
3905 iLen = pp->lencode ;
3908 if ( iLen == 2 && strncmp( pBuf, "OK" , 2 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_OK ; }
3909 else if ( iLen == 7 && strncmp( pBuf, "CONNECT" , 7 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_CONNECT ; }
3910 else if ( iLen == 4 && strncmp( pBuf, "RING" , 4 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_RING ; }
3911 else if ( iLen == 10 && strncmp( pBuf, "NO CARRIER" , 10 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_NO_CARRIER ; }
3912 else if ( iLen == 5 && strncmp( pBuf, "ERROR" , 5 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_ERROR ; }
3913 else if ( iLen >= 8 && strncmp( pBuf, "CONNECT " , 8 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_CONNECT_X ; }
3914 else if ( iLen == 11 && strncmp( pBuf, "NO DAILTONE", 11 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_NO_DAILTONE ; }
3915 else if ( iLen == 4 && strncmp( pBuf, "BUSY" , 4 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_BUSY ; }
3916 else if ( iLen == 9 && strncmp( pBuf, "NO ANSWER" , 9 ) == 0 ) { up->iModemEvent = MODEM_EVENT_RESP_NO_ANSWER ; }
3917 else { up->iModemEvent = MODEM_EVENT_RESP_UNKNOWN ; }
3923 iCopyLen = ( iLen <= sizeof(sResp)-1 ? iLen : sizeof(sResp)-1 ) ;
3924 strncpy( sResp, pBuf, iLen <= sizeof(sResp)-1 ? iLen : sizeof(sResp)-1 ) ;
3925 sResp[iCopyLen] = 0 ;
3926 printf ( "refclock_jjy.c : modem_receive : iLen=%zu pBuf=[%s] iModemEvent=%d\n", iCopyLen, sResp, up->iModemEvent ) ;
3929 modem_control ( peer, pp, up ) ;
3935 /**************************************************************************************************/
3938 modem_timer ( int unit, struct peer *peer )
3941 struct refclockproc *pp ;
3942 struct jjyunit *up ;
3944 pp = peer->procptr ;
3947 DEBUG_MODEM_PRINTF( "modem_timer" ) ;
3949 if ( iModemSilentTimeout[up->iModemState] != 0 ) {
3950 up->iModemSilentTimer++ ;
3951 if ( iModemSilentTimeout[up->iModemState] <= up->iModemSilentTimer ) {
3952 up->iModemEvent = MODEM_EVENT_SILENT ;
3953 modem_control ( peer, pp, up ) ;
3957 if ( iModemStateTimeout[up->iModemState] != 0 ) {
3958 up->iModemStateTimer++ ;
3959 if ( iModemStateTimeout[up->iModemState] <= up->iModemStateTimer ) {
3960 up->iModemEvent = MODEM_EVENT_TIMEOUT ;
3961 modem_control ( peer, pp, up ) ;
3967 /**************************************************************************************************/
3970 modem_control ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
3974 short iPostEvent = MODEM_EVENT_NULL ;
3976 DEBUG_MODEM_PRINTF( "modem_control" ) ;
3978 rc = (*pModemHandler[up->iModemEvent][up->iModemState])( peer, pp, up ) ;
3980 if ( rc == CHANGE_MODEM_STATE ) {
3981 iPostEvent = iModemPostEvent[up->iModemEvent][up->iModemState] ;
3984 printf( "refclock_jjy.c : modem_control : iModemState=%d -> %d iPostEvent=%d\n",
3985 up->iModemState, iModemNextState[up->iModemEvent][up->iModemState], iPostEvent ) ;
3989 if ( up->iModemState != iModemNextState[up->iModemEvent][up->iModemState] ) {
3990 up->iModemSilentCount = 0 ;
3991 up->iModemStateTimer = 0 ;
3992 up->iModemCommandSeq = 0 ;
3995 up->iModemState = iModemNextState[up->iModemEvent][up->iModemState] ;
3998 if ( iPostEvent != MODEM_EVENT_NULL ) {
3999 up->iModemEvent = iPostEvent ;
4000 modem_control ( peer, pp, up ) ;
4003 up->iModemEvent = MODEM_EVENT_NULL ;
4007 /******************************/
4009 modem_disc_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4012 DEBUG_MODEM_PRINTF( "modem_disc_ignore" ) ;
4014 return STAY_MODEM_STATE ;
4018 /******************************/
4020 modem_disc_init ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4023 DEBUG_MODEM_PRINTF( "modem_disc_init" ) ;
4025 return CHANGE_MODEM_STATE ;
4029 /******************************/
4031 modem_init_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4034 DEBUG_MODEM_PRINTF( "modem_init_ignore" ) ;
4036 return STAY_MODEM_STATE ;
4040 /******************************/
4042 modem_init_start ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4045 DEBUG_MODEM_PRINTF( "modem_init_start" ) ;
4047 up->iModemCommandSeq = 0 ;
4051 printf( "refclock_jjy.c : modem_init_start : call modem_init_resp00\n" ) ;
4055 return modem_init_resp00( peer, pp, up ) ;
4059 /******************************/
4061 modem_init_resp00 ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4067 int iErrorCorrection, iSpeakerSwitch, iSpeakerVolume ;
4068 int iNextModemState = STAY_MODEM_STATE ;
4070 DEBUG_MODEM_PRINTF( "modem_init_resp00" ) ;
4072 up->iModemCommandSeq++ ;
4074 switch ( up->iModemCommandSeq ) {
4077 /* En = Echoback 0:Off 1:On */
4078 /* Qn = Result codes 0:On 1:Off */
4079 /* Vn = Result codes 0:Numeric 1:Text */
4080 pCmd = "ATE0Q0V1\r\n" ;
4084 /* Mn = Speaker switch 0:Off 1:On until remote carrier detected 2:On */
4085 if ( ( pp->sloppyclockflag & CLK_FLAG3 ) == 0 ) {
4086 /* fudge 127.127.40.n flag3 0 */
4087 iSpeakerSwitch = 0 ;
4089 /* fudge 127.127.40.n flag3 1 */
4090 iSpeakerSwitch = 2 ;
4093 /* Ln = Speaker volume 0:Very low 1:Low 2:Middle 3:High */
4094 if ( ( pp->sloppyclockflag & CLK_FLAG4 ) == 0 ) {
4095 /* fudge 127.127.40.n flag4 0 */
4096 iSpeakerVolume = 1 ;
4098 /* fudge 127.127.40.n flag4 1 */
4099 iSpeakerVolume = 2 ;
4103 snprintf( cBuf, sizeof(cBuf), "ATM%dL%d\r\n", iSpeakerSwitch, iSpeakerVolume ) ;
4107 /* &Kn = Flow control 4:XON/XOFF */
4108 pCmd = "AT&K4\r\n" ;
4112 /* +MS = Protocol V22B:1200,2400bps
\81iV.22bis) */
4113 pCmd = "AT+MS=V22B\r\n" ;
4117 /* %Cn = Data compression 0:No data compression */
4118 pCmd = "AT%C0\r\n" ;
4122 /* \Nn = Error correction 0:Normal mode 1:Direct mode 2:V42,MNP 3:V42,MNP,Normal */
4123 if ( ( pp->sloppyclockflag & CLK_FLAG2 ) == 0 ) {
4124 /* fudge 127.127.40.n flag2 0 */
4125 iErrorCorrection = 0 ;
4127 /* fudge 127.127.40.n flag2 1 */
4128 iErrorCorrection = 3 ;
4132 snprintf( cBuf, sizeof(cBuf), "AT\\N%d\r\n", iErrorCorrection ) ;
4136 /* Hn = Hook 0:Hook-On ( Disconnect ) 1:Hook-Off ( Connect ) */
4141 /* Initialize completion */
4143 iNextModemState = CHANGE_MODEM_STATE ;
4152 if ( pCmd != NULL ) {
4154 iCmdLen = strlen( pCmd ) ;
4155 if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
4156 refclock_report( peer, CEVNT_FAULT ) ;
4159 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
4163 return iNextModemState ;
4167 /******************************/
4169 modem_init_resp04 ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4172 DEBUG_MODEM_PRINTF( "modem_init_resp04" ) ;
4174 return modem_init_resp00( peer, pp, up ) ;
4178 /******************************/
4180 modem_init_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4183 DEBUG_MODEM_PRINTF( "modem_init_disc" ) ;
4186 printf( "refclock_jjy.c : modem_init_disc : call modem_esc_disc\n" ) ;
4190 return CHANGE_MODEM_STATE ;
4194 /******************************/
4196 modem_dial_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4199 DEBUG_MODEM_PRINTF( "modem_dial_ignore" ) ;
4201 return STAY_MODEM_STATE ;
4205 /******************************/
4207 modem_dial_dialout ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4214 DEBUG_MODEM_PRINTF( "modem_dial_dialout" ) ;
4217 if ( ( pp->sloppyclockflag & CLK_FLAG1 ) == 0 ) {
4218 /* fudge 127.127.40.n flag1 0 */
4219 cToneOrPulse = 'T' ;
4221 /* fudge 127.127.40.n flag1 1 */
4222 cToneOrPulse = 'P' ;
4225 /* Connect ( Dial number ) */
4226 snprintf( sCmd, sizeof(sCmd), "ATDW%c%s\r\n", cToneOrPulse, *sys_phone ) ;
4229 iCmdLen = strlen( sCmd ) ;
4230 if ( write( pp->io.fd, sCmd, iCmdLen ) != iCmdLen ) {
4231 refclock_report( peer, CEVNT_FAULT ) ;
4234 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, sCmd ) ;
4236 return STAY_MODEM_STATE ;
4240 /******************************/
4242 modem_dial_escape ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4245 DEBUG_MODEM_PRINTF( "modem_dial_escape" ) ;
4248 printf( "refclock_jjy.c : modem_dial_escape : call modem_conn_escape\n" ) ;
4252 return modem_conn_escape( peer, pp, up ) ;
4256 /******************************/
4258 modem_dial_connect ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4261 DEBUG_MODEM_PRINTF( "modem_dial_connect" ) ;
4263 return CHANGE_MODEM_STATE ;
4267 /******************************/
4269 modem_dial_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4272 DEBUG_MODEM_PRINTF( "modem_dial_disc" ) ;
4275 printf( "refclock_jjy.c : modem_dial_disc : call modem_esc_disc\n" ) ;
4279 modem_esc_disc( peer, pp, up ) ;
4281 return CHANGE_MODEM_STATE ;
4285 /******************************/
4287 modem_conn_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4290 DEBUG_MODEM_PRINTF( "modem_conn_ignore" ) ;
4292 return STAY_MODEM_STATE ;
4296 /******************************/
4298 modem_conn_escape ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4301 DEBUG_MODEM_PRINTF( "modem_conn_escape" ) ;
4303 return CHANGE_MODEM_STATE ;
4307 /******************************/
4309 modem_esc_ignore ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4312 DEBUG_MODEM_PRINTF( "modem_esc_ignore" ) ;
4314 return STAY_MODEM_STATE ;
4318 /******************************/
4320 modem_esc_escape ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4326 DEBUG_MODEM_PRINTF( "modem_esc_escape" ) ;
4328 /* Escape command ( Go to command mode ) */
4332 iCmdLen = strlen( pCmd ) ;
4333 if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
4334 refclock_report( peer, CEVNT_FAULT ) ;
4337 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
4339 return STAY_MODEM_STATE ;
4343 /******************************/
4345 modem_esc_data ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4348 DEBUG_MODEM_PRINTF( "modem_esc_data" ) ;
4350 up->iModemSilentTimer = 0 ;
4352 return STAY_MODEM_STATE ;
4356 /******************************/
4358 modem_esc_silent ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4361 DEBUG_MODEM_PRINTF( "modem_esc_silent" ) ;
4363 up->iModemSilentCount ++ ;
4365 if ( up->iModemSilentCount < iModemStateTimeout[up->iModemState] / iModemSilentTimeout[up->iModemState] ) {
4368 printf( "refclock_jjy.c : modem_esc_silent : call modem_esc_escape\n" ) ;
4371 modem_esc_escape( peer, pp, up ) ;
4372 up->iModemSilentTimer = 0 ;
4373 return STAY_MODEM_STATE ;
4378 printf( "refclock_jjy.c : modem_esc_silent : call modem_esc_disc\n" ) ;
4381 return modem_esc_disc( peer, pp, up ) ;
4384 /******************************/
4386 modem_esc_disc ( struct peer *peer, struct refclockproc *pp, struct jjyunit *up )
4392 DEBUG_MODEM_PRINTF( "modem_esc_disc" ) ;
4398 iCmdLen = strlen( pCmd ) ;
4399 if ( write( pp->io.fd, pCmd, iCmdLen ) != iCmdLen ) {
4400 refclock_report( peer, CEVNT_FAULT ) ;
4403 jjy_write_clockstats( peer, JJY_CLOCKSTATS_MARK_SEND, pCmd ) ;
4405 return CHANGE_MODEM_STATE ;
4409 /*################################################################################################*/
4410 /*################################################################################################*/
4412 /*## jjy_write_clockstats ##*/
4414 /*################################################################################################*/
4415 /*################################################################################################*/
4418 jjy_write_clockstats ( struct peer *peer, int iMark, const char *pData )
4422 const char * pMark ;
4423 int iMarkLen, iDataLen ;
4426 case JJY_CLOCKSTATS_MARK_JJY :
4429 case JJY_CLOCKSTATS_MARK_SEND :
4432 case JJY_CLOCKSTATS_MARK_RECEIVE :
4435 case JJY_CLOCKSTATS_MARK_INFORMATION :
4438 case JJY_CLOCKSTATS_MARK_ATTENTION :
4441 case JJY_CLOCKSTATS_MARK_WARNING :
4444 case JJY_CLOCKSTATS_MARK_ERROR :
4447 case JJY_CLOCKSTATS_MARK_BUG :
4455 iDataLen = strlen( pData ) ;
4456 iMarkLen = strlen( pMark ) ;
4457 strcpy( sLog, pMark ) ; /* Harmless because of enough length */
4458 printableString( sLog+iMarkLen, sizeof(sLog)-iMarkLen, pData, iDataLen ) ;
4462 printf( "refclock_jjy.c : clockstats : %s\n", sLog ) ;
4465 record_clock_stats( &peer->srcadr, sLog ) ;
4469 /*################################################################################################*/
4470 /*################################################################################################*/
4472 /*## printableString ##*/
4474 /*################################################################################################*/
4475 /*################################################################################################*/
4478 printableString ( char *sOutput, int iOutputLen, const char *sInput, int iInputLen )
4480 const char *printableControlChar[] = {
4481 "<NUL>", "<SOH>", "<STX>", "<ETX>",
4482 "<EOT>", "<ENQ>", "<ACK>", "<BEL>",
4483 "<BS>" , "<HT>" , "<LF>" , "<VT>" ,
4484 "<FF>" , "<CR>" , "<SO>" , "<SI>" ,
4485 "<DLE>", "<DC1>", "<DC2>", "<DC3>",
4486 "<DC4>", "<NAK>", "<SYN>", "<ETB>",
4487 "<CAN>", "<EM>" , "<SUB>", "<ESC>",
4488 "<FS>" , "<GS>" , "<RS>" , "<US>" ,
4495 InputLen = (size_t)iInputLen;
4496 OutputLen = (size_t)iOutputLen;
4497 for ( i = j = 0 ; i < InputLen && j < OutputLen ; i ++ ) {
4498 if ( isprint( (unsigned char)sInput[i] ) ) {
4500 if ( j + 1 >= OutputLen )
4502 sOutput[j] = sInput[i] ;
4503 } else if ( ( sInput[i] & 0xFF ) <
4504 COUNTOF(printableControlChar) ) {
4505 n = strlen( printableControlChar[sInput[i] & 0xFF] ) ;
4506 if ( j + n + 1 >= OutputLen )
4508 strlcpy( sOutput + j,
4509 printableControlChar[sInput[i] & 0xFF],
4513 if ( j + n + 1 >= OutputLen )
4515 snprintf( sOutput + j, OutputLen - j, "<x%X>",
4516 sInput[i] & 0xFF ) ;
4521 sOutput[min(j, (size_t)iOutputLen - 1)] = '\0' ;
4525 /**************************************************************************************************/
4528 int refclock_jjy_bs ;
4529 #endif /* REFCLOCK */