aboutsummaryrefslogtreecommitdiffstats
path: root/src/twa_t.c
blob: cfebb9633e4e00d37f735efd2e309bb99c54c072 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * twa_t.c:
 *
 * Copyright (c) 2017 James McKenzie <foss@madingley.org>,
 * All rights reserved.
 *
 */

#include "project.h"

void
tw_dt_to_tm (TW_DateTime * tw_dt, struct tm *tm)
{
  memset (tm, 0, sizeof (*tm));

  tm->tm_sec = tw_dt->tw_sec;
  tm->tm_min = tw_dt->tw_min;
  tm->tm_hour = tw_dt->tw_hour;
  tm->tm_mday = tw_dt->tw_day;
  tm->tm_mon = tw_dt->tw_month - 1;
  tm->tm_year = tw_dt->tw_year - 1900;
}



void
tm_to_tw_dt (struct tm *tm, TW_DateTime * tw_dt)
{
  memset (tw_dt, 0, sizeof (*tw_dt));

  tw_dt->tw_sec = tm->tm_sec;
  tw_dt->tw_min = tm->tm_min;
  tw_dt->tw_hour = tm->tm_hour;
  tw_dt->tw_day = tm->tm_mday;
  tw_dt->tw_month = tm->tm_mon + 1;
  tw_dt->tw_year = tm->tm_year + 1900;
}


static int
show_ctrl_time (int fd)
{
  TW_DateTime dt;
  ssize_t ret;
  struct tm tm;


  ret =
    twa_get_parameter (fd, TW_TIMEKEEP_TABLE, TW_TIMEKEEP_DATETIME, &dt,
                       sizeof (dt));

  if (ret != sizeof (dt))
    {
      fprintf (stderr, "Failed to get controller time\n");
      return -1;
    }

  tw_dt_to_tm (&dt, &tm);

  printf ("Controller time is: %s", asctime (&tm));

  return 0;
}



static int
set_ctrl_time (int fd)
{
  TW_DateTime dt;
  int ret;
  struct tm tm;
  time_t t;


  time (&t);

  localtime_r (&t, &tm);

  tm_to_tw_dt (&tm, &dt);

  ret =
    twa_set_parameter (fd, TW_TIMEKEEP_TABLE, TW_TIMEKEEP_DATETIME, &dt,
                       sizeof (dt));

  if (ret)
    {
      fprintf (stderr, "Failed to set controller time\n");
      return -1;
    }
  return 0;
}


static int
check_ctrl_time (int fd)
{
  TW_DateTime dt;
  ssize_t ret;
  struct tm tm;
  time_t ctrl_time, local_time;
  double d;
  int rc = 0;

  ret =
    twa_get_parameter (fd, TW_TIMEKEEP_TABLE, TW_TIMEKEEP_DATETIME, &dt,
                       sizeof (dt));

  if (ret != sizeof (dt))
    {
      fprintf (stderr, "Failed to get controller time\n");
      rc |= -1;
      return rc;
    }

  tw_dt_to_tm (&dt, &tm);

  printf ("Controller time is: %s", asctime (&tm));

  ctrl_time = mktime (&tm);

  time (&local_time);
  localtime_r (&local_time, &tm);
  local_time = mktime (&tm);

  d = difftime (local_time, ctrl_time);

  printf ("Controller time difference is %.1lf seconds\n", d);

  if ((d > 15.0) || (d < -15.0))
    {
      printf ("Setting controller time\n");
      rc |= set_ctrl_time (fd);
      rc |= show_ctrl_time (fd);
    }

  return rc;
}


int
main (int argc, char *argv[])
{
  char *dev = "/dev/twa0";
  int fd;
  int ret = 0;

  if (argc == 2)
    dev = argv[1];

  fd = open (dev, O_RDWR);

  if (fd < 0)
    {
      fprintf (stderr, "Failed to open %s: %m\n", dev);
      ret |= -1;
      return ret;
    }


  return check_ctrl_time (fd);
}