forked from vogelchr/radioclkd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_wwvb.c
143 lines (111 loc) · 3.95 KB
/
decode_wwvb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2002 Jon Atkins http://www.jonatkins.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "systime.h"
#include "clock.h"
#include "decode_dcf77.h"
#include "logger.h"
#include "settings.h"
#include "utctime.h"
#define CENTURY (2000) //added to 2 digit years
//all these macros assume the following:
//1. clock is a variable for the current clock
//2. the index used is the second index
#define DATA_OK(bit) ((clock->numdata-60+(bit)) >= 0 && (clock->numdata-60+(bit)) < clock->numdata)
#define GET_DATA(bit) (DATA_OK(bit) ? clock->data[clock->numdata-60+(bit)] : 0)
#define GET(bit) (GET_DATA(bit)==5)
int
wwvbGetBCD ( clkInfoT* clock, int valstart, int valcount )
{
static const int BCD[12] = { 200, 100, 0, 80, 40, 20, 10, 0, 8, 4, 2, 1 };
int val,i;
if ( valcount > 12 )
return -1;
val = 0;
for ( i=0; i<valcount; i++ )
{
if ( GET(valstart+i) )
val += BCD [ 12 - valcount + i ];
}
return val;
}
void
wwvbDump ( clkInfoT* clock )
{
int i;
loggerf ( LOGGER_TRACE, "WWVB : " );
for ( i=0; i<60; i+=5 )
loggerf ( LOGGER_TRACE, "|%-4d", i );
loggerf ( LOGGER_TRACE, "\n" );
loggerf ( LOGGER_TRACE, "WWVB : " );
for ( i=0; i<60; i++ )
loggerf ( LOGGER_TRACE, "%c", DATA_OK(i)?(GET(i)?'1':'.'):' ' );
loggerf ( LOGGER_TRACE, "\n" );
}
int
wwvbDecode ( clkInfoT* clock, time_f minstart )
{
struct tm dectime;
time_t dectimet;
wwvbDump ( clock );
if ( !DATA_OK(1) )
return -1;
memset ( &dectime, 0, sizeof(dectime) );
dectime.tm_year = wwvbGetBCD ( clock, 44, 10 ) + CENTURY - 1900;
dectime.tm_mon = 0;
dectime.tm_mday = wwvbGetBCD ( clock, 22, 12 );
dectime.tm_hour = wwvbGetBCD ( clock, 12, 7 );
dectime.tm_min = wwvbGetBCD ( clock, 1, 8 ) + 1;
dectime.tm_sec = 0;
dectime.tm_isdst = 0; //time is always UTC... (although there are bits for summer time)
//NOTE: decoding this depends on a mktime() which will normalize day numbers correctly
if ( (dectime.tm_mday < 1) || (dectime.tm_mday > 366) )
return -1;
if ( dectime.tm_hour > 23 )
return -1;
if ( dectime.tm_min > 60 )
return -1;
/*
WWVB Sends the on-time marker, which is back-to-back 0.8s pulses
and THEN the time at that marker. So, we need to always move the
time forward one minute to be correct.
*/
if ( dectime.tm_min == 60 ) {
dectime.tm_min = 0;
dectime.tm_hour++;
if ( dectime.tm_hour > 23 ) {
dectime.tm_hour = 0;
}
}
loggerf ( LOGGER_DEBUG, "WWVB time: %04d-%03d %02d:%02d %s%s\n", dectime.tm_year+1900, dectime.tm_mday, dectime.tm_hour, dectime.tm_min, GET(55)?" leap year":"", GET(56)?" leap second soon":"" );
dectimet = UTCtime ( &dectime );
if ( dectimet == (time_t)(-1) )
return -1;
clock->pctime = minstart;
clock->radiotime = dectimet + clock->fudgeoffset;
clock->radioleap = GET(56) ? LEAP_ADDSECOND : LEAP_NOWARNING;
clock->secondssincetime = 0;
return 0;
}