Blame view

RIOT/sys/shell/commands/sc_rtc.c 4.23 KB
a752c7ab   elopes   add first test an...
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  /*
   * Copyright 2013 INRIA.
   * Copyright 2014 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys_shell_commands
   * @{
   *
   * @file
   * @brief       Shell command implementation for the peripheral RTC interface
   *
   * @author  Oliver Hahm <oliver.hahm@inria.fr>
   * @author  Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>#
   *
   * @}
   */
  
  #include <stdio.h>
  #include <stdint.h>
  #include <string.h>
  #include <stdlib.h>
  
  #include "periph/rtc.h"
  
  static void _alarm_handler(void *arg)
  {
      (void) arg;
  
      puts("The alarm rang");
  }
  
  static int dow(int year, int month, int day)
  {
      /* calculate the day of week using Tøndering's algorithm */
      static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
      year -= month < 3;
      return (year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
  }
  
  
  static int _parse_time(char **argv, struct tm *time)
  {
      short i;
      char *end;
  
      i = strtol(argv[0], &end, 10);
      time->tm_year = i - 1900;
  
      i = strtol(end + 1, &end, 10);
      time->tm_mon = i - 1;
  
      i = strtol(end + 1, &end, 10);
      time->tm_mday = i;
  
      i = strtol(argv[1], &end, 10);
      time->tm_hour = i;
  
      i = strtol(end + 1, &end, 10);
      time->tm_min = i;
  
      i = strtol(end + 1, &end, 10);
      time->tm_sec = i;
  
      time->tm_wday = dow(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday);
      time->tm_isdst = -1; /* undefined */
  
      return 0;
  }
  
  static int _print_time(struct tm *time)
  {
      printf("%04i-%02i-%02i %02i:%02i:%02i\n",
              time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
              time->tm_hour, time->tm_min, time->tm_sec
            );
      return 0;
  }
  
  static int _rtc_getalarm(void)
  {
      struct tm t;
      if (rtc_get_alarm(&t) == 0) {
          _print_time(&t);
          return 0;
      }
      else {
          puts("rtc: error getting alarm");
          return 1;
      }
  }
  
  static int _rtc_setalarm(char **argv)
  {
      struct tm now;
  
      if (_parse_time(argv, &now) == 0) {
          if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) {
              puts("rtc: error setting alarm");
              return 1;
          }
          return 0;
      }
      return 1;
  }
  
  static int _rtc_gettime(void)
  {
      struct tm t;
      if (rtc_get_time(&t) == 0) {
          _print_time(&t);
          return 0;
      }
      else {
          puts("rtc: error getting time");
          return 1;
      }
  }
  
  static int _rtc_settime(char **argv)
  {
      struct tm now;
  
      if (_parse_time(argv, &now) == 0) {
          if (rtc_set_time(&now) == -1) {
              puts("rtc: error setting time");
              return 1;
          }
          return 0;
      }
      return 1;
  }
  
  static int _rtc_usage(void)
  {
      puts("usage: rtc <command> [arguments]");
      puts("commands:");
      puts("\tinit\t\tinitialize the interface");
      puts("\tpoweron\t\tpower the interface on");
      puts("\tpoweroff\tpower the interface off");
      puts("\tclearalarm\tdeactivate the current alarm");
      puts("\tgetalarm\tprint the currently alarm time");
      puts("\tsetalarm YYYY-MM-DD HH:MM:SS\n\t\t\tset an alarm for the specified time");
      puts("\tgettime\t\tprint the current time");
      puts("\tsettime YYYY-MM-DD HH:MM:SS\n\t\t\tset the current time");
      return 0;
  }
  
  int _rtc_handler(int argc, char **argv)
  {
      if (argc < 2) {
          _rtc_usage();
          return 1;
      }
      else if (strncmp(argv[1], "init", 4) == 0) {
          rtc_init();
      }
      else if (strncmp(argv[1], "poweron", 7) == 0) {
          rtc_poweron();
      }
      else if (strncmp(argv[1], "poweroff", 8) == 0) {
          rtc_poweroff();
      }
      else if (strncmp(argv[1], "clearalarm", 8) == 0) {
          rtc_clear_alarm();
      }
      else if (strncmp(argv[1], "getalarm", 8) == 0) {
          _rtc_getalarm();
      }
      else if ((strncmp(argv[1], "setalarm", 8) == 0) && (argc == 4)) {
          _rtc_setalarm(argv + 2);
      }
      else if (strncmp(argv[1], "gettime", 7) == 0) {
          _rtc_gettime();
      }
      else if ((strncmp(argv[1], "settime", 7) == 0)  && (argc == 4)) {
          _rtc_settime(argv + 2);
      }
      else {
          printf("unknown command or missing parameters: %s\n\n", argv[1]);
          _rtc_usage();
          return 1;
      }
      return 0;
  }