Deleted Added
full compact
refclock_jjy.c (182007) refclock_jjy.c (200576)
1/*
2 * refclock_jjy - clock driver for JJY receivers
3 */
4
5/**********************************************************************/
6/* */
7/* Copyright (C) 2001-2004, Takao Abe. All rights reserved. */
8/* */

--- 60 unchanged lines hidden (view full) ---

69/* */
70/* 2004/11/28 */
71/* [Add] Support the Echo Keisokuki LT-2000 receiver */
72/* */
73/* 2006/11/04 */
74/* [Fix] C-DEX JST2000 */
75/* Thanks to Hideo Kuramatsu for the patch */
76/* */
1/*
2 * refclock_jjy - clock driver for JJY receivers
3 */
4
5/**********************************************************************/
6/* */
7/* Copyright (C) 2001-2004, Takao Abe. All rights reserved. */
8/* */

--- 60 unchanged lines hidden (view full) ---

69/* */
70/* 2004/11/28 */
71/* [Add] Support the Echo Keisokuki LT-2000 receiver */
72/* */
73/* 2006/11/04 */
74/* [Fix] C-DEX JST2000 */
75/* Thanks to Hideo Kuramatsu for the patch */
76/* */
77/* 2009/04/05 */
78/* [Add] Support the CITIZEN T.I.C JJY-200 receiver */
79/* */
77/**********************************************************************/
78
79#ifdef HAVE_CONFIG_H
80#include <config.h>
81#endif
82
83#if defined(REFCLOCK) && defined(CLOCK_JJY)
84

--- 41 unchanged lines hidden (view full) ---

126/* ------------ ---------------------- --------------------- */
127/* # Mode 1 (Request&Send) */
128/* T YYMMDDWHHMMSS<BCC1><BCC2><CR> */
129/* C Mode 2 (Continuous) */
130/* YYMMDDWHHMMSS<ST1><ST2><ST3><ST4><CR> */
131/* <SUB> Second signal */
132/* */
133/**********************************************************************/
80/**********************************************************************/
81
82#ifdef HAVE_CONFIG_H
83#include <config.h>
84#endif
85
86#if defined(REFCLOCK) && defined(CLOCK_JJY)
87

--- 41 unchanged lines hidden (view full) ---

129/* ------------ ---------------------- --------------------- */
130/* # Mode 1 (Request&Send) */
131/* T YYMMDDWHHMMSS<BCC1><BCC2><CR> */
132/* C Mode 2 (Continuous) */
133/* YYMMDDWHHMMSS<ST1><ST2><ST3><ST4><CR> */
134/* <SUB> Second signal */
135/* */
136/**********************************************************************/
137/* */
138/* The CITIZEN T.I.C CO., LTD. JJY receiver JJY200 */
139/* */
140/* Command Response Remarks */
141/* ------------ ---------------------- --------------------- */
142/* 'XX YY/MM/DD W HH:MM:SS<CR> */
143/* XX: OK|NG|ER */
144/* W: 0(Monday)-6(Sunday) */
145/* */
146/**********************************************************************/
134
135/*
136 * Interface definitions
137 */
138#define DEVICE "/dev/jjy%d" /* device name and unit */
139#define SPEED232 B9600 /* uart speed (9600 baud) */
147
148/*
149 * Interface definitions
150 */
151#define DEVICE "/dev/jjy%d" /* device name and unit */
152#define SPEED232 B9600 /* uart speed (9600 baud) */
153#define SPEED232_TRISTATE_JJY01 B9600 /* UART speed (9600 baud) */
154#define SPEED232_CDEX_JST2000 B9600 /* UART speed (9600 baud) */
155#define SPEED232_ECHOKEISOKUKI_LT2000 B9600 /* UART speed (9600 baud) */
156#define SPEED232_CITIZENTIC_JJY200 B4800 /* UART speed (4800 baud) */
140#define REFID "JJY" /* reference ID */
141#define DESCRIPTION "JJY Receiver"
142#define PRECISION (-3) /* precision assumed (about 100 ms) */
143
144/*
145 * JJY unit control structure
146 */
147struct jjyunit {
148 char unittype ; /* UNITTYPE_XXXXXXXXXX */
149 short operationmode ; /* Echo Keisokuki LT-2000 : 1 or 2 */
150 short version ;
151 short linediscipline ; /* LDISC_CLK or LDISC_RAW */
157#define REFID "JJY" /* reference ID */
158#define DESCRIPTION "JJY Receiver"
159#define PRECISION (-3) /* precision assumed (about 100 ms) */
160
161/*
162 * JJY unit control structure
163 */
164struct jjyunit {
165 char unittype ; /* UNITTYPE_XXXXXXXXXX */
166 short operationmode ; /* Echo Keisokuki LT-2000 : 1 or 2 */
167 short version ;
168 short linediscipline ; /* LDISC_CLK or LDISC_RAW */
169 char bPollFlag ; /* Set by jjy_pool and Reset by jjy_receive */
152 int linecount ;
153 int lineerror ;
154 int year, month, day, hour, minute, second, msecond ;
155/* LDISC_RAW only */
156#define MAX_LINECOUNT 8
157#define MAX_RAWBUF 64
158 int lineexpect ;
159 int charexpect [ MAX_LINECOUNT ] ;
160 int charcount ;
161 char rawbuf [ MAX_RAWBUF ] ;
162};
163
164#define UNITTYPE_TRISTATE_JJY01 1
165#define UNITTYPE_CDEX_JST2000 2
166#define UNITTYPE_ECHOKEISOKUKI_LT2000 3
170 int linecount ;
171 int lineerror ;
172 int year, month, day, hour, minute, second, msecond ;
173/* LDISC_RAW only */
174#define MAX_LINECOUNT 8
175#define MAX_RAWBUF 64
176 int lineexpect ;
177 int charexpect [ MAX_LINECOUNT ] ;
178 int charcount ;
179 char rawbuf [ MAX_RAWBUF ] ;
180};
181
182#define UNITTYPE_TRISTATE_JJY01 1
183#define UNITTYPE_CDEX_JST2000 2
184#define UNITTYPE_ECHOKEISOKUKI_LT2000 3
185#define UNITTYPE_CITIZENTIC_JJY200 4
167
168/*
169 * Function prototypes
170 */
171static int jjy_start P((int, struct peer *));
172static void jjy_shutdown P((int, struct peer *));
173static void jjy_poll P((int, struct peer *));
174static void jjy_poll_tristate_jjy01 P((int, struct peer *));
175static void jjy_poll_cdex_jst2000 P((int, struct peer *));
176static void jjy_poll_echokeisokuki_lt2000 P((int, struct peer *));
186
187/*
188 * Function prototypes
189 */
190static int jjy_start P((int, struct peer *));
191static void jjy_shutdown P((int, struct peer *));
192static void jjy_poll P((int, struct peer *));
193static void jjy_poll_tristate_jjy01 P((int, struct peer *));
194static void jjy_poll_cdex_jst2000 P((int, struct peer *));
195static void jjy_poll_echokeisokuki_lt2000 P((int, struct peer *));
196static void jjy_poll_citizentic_jjy200 P((int, struct peer *));
177static void jjy_receive P((struct recvbuf *));
178static int jjy_receive_tristate_jjy01 P((struct recvbuf *));
179static int jjy_receive_cdex_jst2000 P((struct recvbuf *));
180static int jjy_receive_echokeisokuki_lt2000 P((struct recvbuf *));
197static void jjy_receive P((struct recvbuf *));
198static int jjy_receive_tristate_jjy01 P((struct recvbuf *));
199static int jjy_receive_cdex_jst2000 P((struct recvbuf *));
200static int jjy_receive_echokeisokuki_lt2000 P((struct recvbuf *));
201static int jjy_receive_citizentic_jjy200 P((struct recvbuf *));
181
182/*
183 * Transfer vector
184 */
185struct refclock refclock_jjy = {
186 jjy_start, /* start up driver */
187 jjy_shutdown, /* shutdown driver */
188 jjy_poll, /* transmit poll message */

--- 23 unchanged lines hidden (view full) ---

212jjy_start ( int unit, struct peer *peer )
213{
214
215 struct jjyunit *up ;
216 struct refclockproc *pp ;
217 int fd ;
218 char *pDeviceName ;
219 short iDiscipline ;
202
203/*
204 * Transfer vector
205 */
206struct refclock refclock_jjy = {
207 jjy_start, /* start up driver */
208 jjy_shutdown, /* shutdown driver */
209 jjy_poll, /* transmit poll message */

--- 23 unchanged lines hidden (view full) ---

233jjy_start ( int unit, struct peer *peer )
234{
235
236 struct jjyunit *up ;
237 struct refclockproc *pp ;
238 int fd ;
239 char *pDeviceName ;
240 short iDiscipline ;
241 int iSpeed232 ;
220
221#ifdef DEBUG
222 if ( debug ) {
223 printf ( "jjy_start (refclock_jjy.c) : %s mode=%d ", ntoa(&peer->srcadr), peer->ttl ) ;
224 printf ( DEVICE, unit ) ;
225 printf ( "\n" ) ;
226 }
227#endif

--- 5 unchanged lines hidden (view full) ---

233 }
234 sprintf ( pDeviceName, DEVICE, unit ) ;
235
236 /*
237 * peer->ttl is a mode number specified by "127.127.40.X mode N" in the ntp.conf
238 */
239 switch ( peer->ttl ) {
240 case 0 :
242
243#ifdef DEBUG
244 if ( debug ) {
245 printf ( "jjy_start (refclock_jjy.c) : %s mode=%d ", ntoa(&peer->srcadr), peer->ttl ) ;
246 printf ( DEVICE, unit ) ;
247 printf ( "\n" ) ;
248 }
249#endif

--- 5 unchanged lines hidden (view full) ---

255 }
256 sprintf ( pDeviceName, DEVICE, unit ) ;
257
258 /*
259 * peer->ttl is a mode number specified by "127.127.40.X mode N" in the ntp.conf
260 */
261 switch ( peer->ttl ) {
262 case 0 :
241 case 1 : iDiscipline = LDISC_CLK ; break ;
242 case 2 : iDiscipline = LDISC_RAW ; break ;
243 case 3 : iDiscipline = LDISC_CLK ; break ;
263 case 1 :
264 iDiscipline = LDISC_CLK ;
265 iSpeed232 = SPEED232_TRISTATE_JJY01 ;
266 break ;
267 case 2 :
268 iDiscipline = LDISC_RAW ;
269 iSpeed232 = SPEED232_CDEX_JST2000 ;
270 break ;
271 case 3 :
272 iDiscipline = LDISC_CLK ;
273 iSpeed232 = SPEED232_ECHOKEISOKUKI_LT2000 ;
274 break ;
275 case 4 :
276 iDiscipline = LDISC_CLK ;
277 iSpeed232 = SPEED232_CITIZENTIC_JJY200 ;
278 break ;
244 default :
245 msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Unsupported mode",
246 ntoa(&peer->srcadr), peer->ttl ) ;
247 free ( (void*) pDeviceName ) ;
248 return RC_START_ERROR ;
249 }
250
279 default :
280 msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Unsupported mode",
281 ntoa(&peer->srcadr), peer->ttl ) ;
282 free ( (void*) pDeviceName ) ;
283 return RC_START_ERROR ;
284 }
285
251 if ( ! ( fd = refclock_open ( pDeviceName, SPEED232, iDiscipline ) ) ) {
286 if ( ! ( fd = refclock_open ( pDeviceName, iSpeed232, iDiscipline ) ) ) {
252 free ( (void*) pDeviceName ) ;
253 return RC_START_ERROR ;
254 }
255 free ( (void*) pDeviceName ) ;
256
257 /*
258 * Allocate and initialize unit structure
259 */

--- 34 unchanged lines hidden (view full) ---

294 case 1 :
295 up->charexpect[0] = 15 ; /* YYMMDDWHHMMSS<BCC1><BCC2><CR> */
296 break ;
297 case 2 :
298 up->charexpect[0] = 17 ; /* YYMMDDWHHMMSS<ST1><ST2><ST3><ST4><CR> */
299 break ;
300 }
301 break ;
287 free ( (void*) pDeviceName ) ;
288 return RC_START_ERROR ;
289 }
290 free ( (void*) pDeviceName ) ;
291
292 /*
293 * Allocate and initialize unit structure
294 */

--- 34 unchanged lines hidden (view full) ---

329 case 1 :
330 up->charexpect[0] = 15 ; /* YYMMDDWHHMMSS<BCC1><BCC2><CR> */
331 break ;
332 case 2 :
333 up->charexpect[0] = 17 ; /* YYMMDDWHHMMSS<ST1><ST2><ST3><ST4><CR> */
334 break ;
335 }
336 break ;
337 case 4 :
338 up->unittype = UNITTYPE_CITIZENTIC_JJY200 ;
339 up->lineexpect = 1 ;
340 up->charexpect[0] = 23 ; /* 'XX YY/MM/DD W HH:MM:SS<CR> */
341 break ;
302 default :
303 msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Unsupported mode",
304 ntoa(&peer->srcadr), peer->ttl ) ;
305 close ( fd ) ;
306 free ( (void*) up ) ;
307 return RC_START_ERROR ;
308 }
309

--- 119 unchanged lines hidden (view full) ---

429 case UNITTYPE_CDEX_JST2000 :
430 rc = jjy_receive_cdex_jst2000 ( rbufp ) ;
431 break ;
432
433 case UNITTYPE_ECHOKEISOKUKI_LT2000 :
434 rc = jjy_receive_echokeisokuki_lt2000 ( rbufp ) ;
435 break ;
436
342 default :
343 msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Unsupported mode",
344 ntoa(&peer->srcadr), peer->ttl ) ;
345 close ( fd ) ;
346 free ( (void*) up ) ;
347 return RC_START_ERROR ;
348 }
349

--- 119 unchanged lines hidden (view full) ---

469 case UNITTYPE_CDEX_JST2000 :
470 rc = jjy_receive_cdex_jst2000 ( rbufp ) ;
471 break ;
472
473 case UNITTYPE_ECHOKEISOKUKI_LT2000 :
474 rc = jjy_receive_echokeisokuki_lt2000 ( rbufp ) ;
475 break ;
476
477 case UNITTYPE_CITIZENTIC_JJY200 :
478 rc = jjy_receive_citizentic_jjy200 ( rbufp ) ;
479 break ;
480
437 default :
438 rc = 0 ;
439 break ;
440
441 }
442
443 if ( up->linediscipline == LDISC_RAW ) {
444 if ( up->linecount <= up->lineexpect && up->charcount > up->charexpect[up->linecount-1] ) {
445 for ( i = 0 ; i < up->charcount - up->charexpect[up->linecount-1] ; i ++ ) {
446 up->rawbuf[i] = up->rawbuf[i+up->charexpect[up->linecount-1]] ;
447 }
448 up->charcount -= up->charexpect[up->linecount-1] ;
449 } else {
450 up->charcount = 0 ;
451 }
452 }
453
454 if ( rc == 0 ) return ;
455
481 default :
482 rc = 0 ;
483 break ;
484
485 }
486
487 if ( up->linediscipline == LDISC_RAW ) {
488 if ( up->linecount <= up->lineexpect && up->charcount > up->charexpect[up->linecount-1] ) {
489 for ( i = 0 ; i < up->charcount - up->charexpect[up->linecount-1] ; i ++ ) {
490 up->rawbuf[i] = up->rawbuf[i+up->charexpect[up->linecount-1]] ;
491 }
492 up->charcount -= up->charexpect[up->linecount-1] ;
493 } else {
494 up->charcount = 0 ;
495 }
496 }
497
498 if ( rc == 0 ) return ;
499
500 up->bPollFlag = 0 ;
501
456 if ( up->lineerror != 0 ) {
457 refclock_report ( peer, CEVNT_BADREPLY ) ;
458 strcpy ( sLogText, "BAD REPLY [" ) ;
459 if ( up->linediscipline == LDISC_RAW ) {
460 strncat ( sLogText, up->rawbuf, MAX_LOGTEXT - strlen ( sLogText ) - 1 ) ;
461 } else {
462 strncat ( sLogText, pp->a_lastcode, MAX_LOGTEXT - strlen ( sLogText ) - 1 ) ;
463 }

--- 397 unchanged lines hidden (view full) ---

861
862 }
863
864 return 1 ;
865
866}
867
868/**************************************************************************************************/
502 if ( up->lineerror != 0 ) {
503 refclock_report ( peer, CEVNT_BADREPLY ) ;
504 strcpy ( sLogText, "BAD REPLY [" ) ;
505 if ( up->linediscipline == LDISC_RAW ) {
506 strncat ( sLogText, up->rawbuf, MAX_LOGTEXT - strlen ( sLogText ) - 1 ) ;
507 } else {
508 strncat ( sLogText, pp->a_lastcode, MAX_LOGTEXT - strlen ( sLogText ) - 1 ) ;
509 }

--- 397 unchanged lines hidden (view full) ---

907
908 }
909
910 return 1 ;
911
912}
913
914/**************************************************************************************************/
915
916static int
917jjy_receive_citizentic_jjy200 ( struct recvbuf *rbufp )
918{
919
920 static char *sFunctionName = "jjy_receive_citizentic_jjy200" ;
921
922 struct jjyunit *up ;
923 struct refclockproc *pp ;
924 struct peer *peer;
925
926 char *pBuf ;
927 int iLen ;
928 int rc ;
929 char cApostrophe, sStatus[3] ;
930 int iWeekday ;
931
932 /*
933 * Initialize pointers and read the timecode and timestamp
934 */
935 peer = (struct peer *) rbufp->recv_srcclock ;
936 pp = peer->procptr ;
937 up = (struct jjyunit *) pp->unitptr ;
938
939 if ( up->linediscipline == LDISC_RAW ) {
940 pBuf = up->rawbuf ;
941 iLen = up->charcount ;
942 } else {
943 pBuf = pp->a_lastcode ;
944 iLen = pp->lencode ;
945 }
946
947 /*
948 * JJY-200 sends a timestamp every second.
949 * So, a timestamp is ignored unless it is right after polled.
950 */
951 if ( ! up->bPollFlag ) return 0 ;
952
953 switch ( up->linecount ) {
954
955 case 1 : /* 'XX YY/MM/DD W HH:MM:SS<CR> */
956
957 if ( iLen != 23 ) {
958#ifdef DEBUG
959 if ( debug >= 2 ) {
960 printf ( "%s (refclock_jjy.c) : Reply length error ( iLen=%d )\n", sFunctionName, iLen ) ;
961 }
962#endif
963 up->lineerror = 1 ;
964 break ;
965 }
966
967 rc = sscanf ( pBuf, "%c%2s %2d/%2d/%2d %1d %2d:%2d:%2d",
968 &cApostrophe, sStatus,
969 &up->year, &up->month, &up->day, &iWeekday, &up->hour, &up->minute, &up->second ) ;
970 sStatus[2] = 0 ;
971 if ( rc != 9 || cApostrophe != '\'' || strcmp( sStatus, "OK" ) != 0
972 || up->month < 1 || up->month > 12 || up->day < 1 || up->day > 31
973 || iWeekday > 6
974 || up->hour > 23 || up->minute > 59 || up->second > 60 ) {
975#ifdef DEBUG
976 if ( debug >= 2 ) {
977 printf ( "%s (refclock_jjy.c) : Time error (rc=%d) [ %c %2s %02d %02d %02d %d %02d %02d %02d ]\n", sFunctionName,
978 rc, cApostrophe, sStatus, up->year, up->month, up->day, iWeekday, up->hour, up->minute, up->second ) ;
979 }
980#endif
981 up->lineerror = 1 ;
982 break ;
983 }
984
985 up->year += 2000 ;
986 up->msecond = 0 ;
987
988 break ;
989
990 default : /* Unexpected reply */
991
992 up->lineerror = 1 ;
993 break ;
994
995 }
996
997 return 1 ;
998
999}
1000
1001/**************************************************************************************************/
869/* jjy_poll - called by the transmit procedure */
870/**************************************************************************************************/
871static void
872jjy_poll ( int unit, struct peer *peer )
873{
874
875 struct jjyunit *up;
876 struct refclockproc *pp;

--- 11 unchanged lines hidden (view full) ---

888#ifdef DEBUG
889 if ( debug ) {
890 printf ( "jjy_poll (refclock_jjy.c) : %ld\n", pp->polls ) ;
891 }
892#endif
893
894 pp->polls ++ ;
895
1002/* jjy_poll - called by the transmit procedure */
1003/**************************************************************************************************/
1004static void
1005jjy_poll ( int unit, struct peer *peer )
1006{
1007
1008 struct jjyunit *up;
1009 struct refclockproc *pp;

--- 11 unchanged lines hidden (view full) ---

1021#ifdef DEBUG
1022 if ( debug ) {
1023 printf ( "jjy_poll (refclock_jjy.c) : %ld\n", pp->polls ) ;
1024 }
1025#endif
1026
1027 pp->polls ++ ;
1028
1029 up->bPollFlag = 1 ;
896 up->linecount = 0 ;
897 up->lineerror = 0 ;
898 up->charcount = 0 ;
899
900 switch ( up->unittype ) {
901
902 case UNITTYPE_TRISTATE_JJY01 :
903 jjy_poll_tristate_jjy01 ( unit, peer ) ;
904 break ;
905
906 case UNITTYPE_CDEX_JST2000 :
907 jjy_poll_cdex_jst2000 ( unit, peer ) ;
908 break ;
909
910 case UNITTYPE_ECHOKEISOKUKI_LT2000 :
911 jjy_poll_echokeisokuki_lt2000 ( unit, peer ) ;
912 break ;
913
1030 up->linecount = 0 ;
1031 up->lineerror = 0 ;
1032 up->charcount = 0 ;
1033
1034 switch ( up->unittype ) {
1035
1036 case UNITTYPE_TRISTATE_JJY01 :
1037 jjy_poll_tristate_jjy01 ( unit, peer ) ;
1038 break ;
1039
1040 case UNITTYPE_CDEX_JST2000 :
1041 jjy_poll_cdex_jst2000 ( unit, peer ) ;
1042 break ;
1043
1044 case UNITTYPE_ECHOKEISOKUKI_LT2000 :
1045 jjy_poll_echokeisokuki_lt2000 ( unit, peer ) ;
1046 break ;
1047
1048 case UNITTYPE_CITIZENTIC_JJY200 :
1049 jjy_poll_citizentic_jjy200 ( unit, peer ) ;
1050 break ;
1051
914 default :
915 break ;
916
917 }
918
919}
920
921/**************************************************************************************************/

--- 79 unchanged lines hidden (view full) ---

1001#endif
1002
1003 if ( write ( pp->io.fd, sCmd, 1 ) != 1 ) {
1004 refclock_report ( peer, CEVNT_FAULT ) ;
1005 }
1006
1007}
1008
1052 default :
1053 break ;
1054
1055 }
1056
1057}
1058
1059/**************************************************************************************************/

--- 79 unchanged lines hidden (view full) ---

1139#endif
1140
1141 if ( write ( pp->io.fd, sCmd, 1 ) != 1 ) {
1142 refclock_report ( peer, CEVNT_FAULT ) ;
1143 }
1144
1145}
1146
1147/**************************************************************************************************/
1148
1149static void
1150jjy_poll_citizentic_jjy200 ( int unit, struct peer *peer )
1151{
1152
1153 /* Do nothing ( up->bPollFlag is set by the jjy_poll ) */
1154
1155}
1156
1009#else
1010int refclock_jjy_bs ;
1011#endif /* REFCLOCK */
1157#else
1158int refclock_jjy_bs ;
1159#endif /* REFCLOCK */