source: rtems/cpukit/libmisc/shell/main_rtc.c @ 22ed4172

4.104.115
Last change on this file since 22ed4172 was 22ed4172, checked in by Joel Sherrill <joel.sherrill@…>, on May 27, 2009 at 2:23:23 PM

2009-05-27 Sebastian Huber <sebastian.huber@…>

  • libcsupport/include/rtc.h: New RTC driver interface.
  • libmisc/Makefile.am, libmisc/shell/shellconfig.h: Added RTC command.
  • libmisc/shell/main_rtc.c: New file.
  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**
2 * @file
3 *
4 * Real time clock shell command.
5 */
6
7/*
8 * Copyright (c) 2009
9 * embedded brains GmbH
10 * Obere Lagerstr. 30
11 * D-82178 Puchheim
12 * Germany
13 * <rtems@embedded-brains.de>
14 *
15 * The license and distribution terms for this file may be
16 * found in the file LICENSE in this distribution or at
17 * http://www.rtems.com/license/LICENSE.
18 */
19
20#include <stdio.h>
21#include <inttypes.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25
26#include <rtems.h>
27#include <rtems/rtc.h>
28#include <rtems/error.h>
29#include <rtems/shell.h>
30
31#define RTEMS_RTC_SHELL_ERROR( fmt, ...) \
32  do { \
33    printf( "error: " fmt "\n", ##__VA_ARGS__); \
34    return -1; \
35  } while (0)
36
37#define RTEMS_RTC_SHELL_ERROR_SC( sc, fmt, ...) \
38  if ((sc) != RTEMS_SUCCESSFUL) { \
39    printf( "error: " fmt ": %s\n", ##__VA_ARGS__, rtems_status_text( sc)); \
40    return -1; \
41  }
42
43static const char rtems_rtc_shell_usage [] =
44  "real time clock read and set\n"
45  "\n"
46  "rtc\n"
47  "\tprints the current time of day\n"
48  "\n"
49  "rtc YYYY-MM-DD [HH:MM:SS [TICKS]]\n"
50  "\tsets the time of day and real time clock";
51
52static int rtems_rtc_shell_main( int argc, char **argv)
53{
54  rtems_status_code sc = RTEMS_SUCCESSFUL;
55  rtems_time_of_day tod = {
56    .year = 1988,
57    .month = 1,
58    .day = 1,
59    .hour = 0,
60    .minute = 0,
61    .second = 0,
62    .ticks = 0
63  };
64
65  if (argc == 1) {
66    sc = rtems_clock_get_tod( &tod);
67    RTEMS_RTC_SHELL_ERROR_SC( sc, "get time of day");
68
69    printf(
70      "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
71        " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
72        " %02" PRIu32 "\n",
73      tod.year,
74      tod.month,
75      tod.day,
76      tod.hour,
77      tod.minute,
78      tod.second,
79      tod.ticks
80    );
81  } else if (argc > 1 && argc < 5) {
82    int rv = 0;
83    int fd = 0;
84    ssize_t n = 0;
85    uint32_t v [3];
86
87    if (argc > 1) {
88      rv = sscanf(
89        argv [1],
90        "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32,
91        v,
92        v + 1,
93        v + 2
94      );
95
96      if (rv == 3) {
97        tod.year = v [0];
98        tod.month = v [1];
99        tod.day = v [2];
100      } else {
101        RTEMS_RTC_SHELL_ERROR( "unexpected YYYY-MM-DD input: %s", argv [1]);
102      }
103    }
104
105    if (argc > 2) {
106      rv = sscanf(
107        argv [2],
108        "%04" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
109        v,
110        v + 1,
111        v + 2
112      );
113
114      if (rv == 3) {
115        tod.hour = v [0];
116        tod.minute = v [1];
117        tod.second = v [2];
118      } else {
119        RTEMS_RTC_SHELL_ERROR( "unexpected HH:MM:SS input: %s", argv [2]);
120      }
121    }
122
123    if (argc > 3) {
124      rv = sscanf( argv [3], "%" PRIu32, v);
125
126      if (rv == 1) {
127        tod.ticks = v [0];
128      } else {
129        RTEMS_RTC_SHELL_ERROR( "unexpected TICKS input: %s", argv [3]);
130      }
131    }
132
133    sc = rtems_clock_set( &tod);
134    RTEMS_RTC_SHELL_ERROR_SC( sc, "set time of day");
135
136    fd = open( RTC_DEVICE_NAME, O_WRONLY);
137    if (fd < 0) {
138      perror( "error: open " RTC_DEVICE_NAME);
139      return -1;
140    }
141
142    n = write( fd, &tod, sizeof( tod));
143    if (n != (ssize_t) sizeof( tod)) {
144      perror( "error: write to " RTC_DEVICE_NAME);
145      close( fd);
146      return -1;
147    }
148
149    rv = close( fd);
150    if (rv != 0) {
151      perror( "error: close " RTC_DEVICE_NAME);
152      return -1;
153    }
154  } else {
155    puts( rtems_rtc_shell_usage);
156    return -1;
157  }
158
159  return 0;
160}
161
162struct rtems_shell_cmd_tt rtems_shell_RTC_Command = {
163  .name = "rtc",
164  .usage = rtems_rtc_shell_usage,
165  .topic = "misc",
166  .command = rtems_rtc_shell_main,
167  .alias = NULL,
168  .next = NULL
169};
Note: See TracBrowser for help on using the repository browser.