summaryrefslogtreecommitdiffstats
path: root/msf.c
blob: 38e089653a7622af794092c666c48ca4860cd682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "project.h"


static int check_min_ident (uint8_t *i)
{
  if (i[0]) return 1;

  if (!i[1]) return 1;

  if (!i[2]) return 1;

  if (!i[3]) return 1;

  if (!i[4]) return 1;

  if (!i[5]) return 1;

  if (!i[6]) return 1;

  if (i[7]) return 1;

  return 0;
}




static void msf_set_ident (uint8_t *i)
{
  i[0] = 0;
  i[1] = 1;
  i[2] = 1;
  i[3] = 1;
  i[4] = 1;
  i[5] = 1;
  i[6] = 1;
  i[7] = 0;
}





static void msf_make_bits (UTC u, uint8_t *a, uint8_t *b)
{

  bzero (a, 60);
  bzero (b, 60);


  a[0] = 1;
  b[0] = 1;


  bcd_set (a, 17, 24, u.year % 100);
  bcd_set (a, 25, 29, u.month);
  bcd_set (a, 30, 35, u.mday);
  bcd_set (a, 36, 38, u.wday - 1);
  bcd_set (a, 39, 44, u.hour);
  bcd_set (a, 45, 51, u.minute);

  msf_set_ident (&a[52]);


  b[53] = 0; /* about to be BST */

  b[54] = set_parity (a, 17, 24);
  b[55] = set_parity (a, 25, 35);
  b[56] = set_parity (a, 36, 38);
  b[57] = set_parity (a, 39, 51);

  b[58] = 0; /*BST vs GMT */

}

static void process_bits (uint8_t *bitsa, uint8_t *bitsb)
{
  UTC u;


  if (check_min_ident (&bitsa[52])) return;

  if (check_parity (bitsa, 17, 24, bitsb[54])) return;

  if (check_parity (bitsa, 25, 35, bitsb[55])) return;

  if (check_parity (bitsa, 36, 38, bitsb[56])) return;

  if (check_parity (bitsa, 39, 51, bitsb[57])) return;

  u.jday = 0;
  u.year = bcd (bitsa, 17, 24);
  u.month = bcd (bitsa, 25, 29);
  u.mday = bcd (bitsa, 30, 35);
  u.hour = bcd (bitsa, 39, 44);
  u.minute = bcd (bitsa, 45, 51);
  u.second = 0;
  u.nanosecond = 0;



  printf ("MSF: decoded %02d-%02d-%02d %02d:%02d\r\n", u.year, u.month, u.mday, u.hour, u.minute);
}



void msf_make_stream (uint8_t *v, UTC u)
{
  unsigned s;
  uint8_t a[60], b[60];
  uint8_t *pa, *pb;

  msf_make_bits (u, a, b);
  process_bits (a, b);

  bzero (v, 600);

  for (s = 0, pa = a, pb = b; s < 60; ++s, v += 10, ++pa, ++pb) {

    if (!s)
      memset (v, 1, 5);

    v[0] = 1;
    v[1] = *pa;
    v[2] = *pb;
  }
}