source: rtems/cpukit/libmisc/shell/main_rtc.c @ f16c059

4.115
Last change on this file since f16c059 was f16c059, checked in by Joel Sherrill <joel.sherrill@…>, on 01/17/11 at 22:12:48

2011-01-17 Alin Rus <alin.codejunkie@…>

  • posix/src/aio_cancel.c: Fixed ending of if braces.

2011-01-17 Alin Rus <alin.codejunkie@…>

  • posix/src/aio_misc.c: Add debug information. Fixed idle_threads/ active_threads issues. Fixed infinite loop in rtems_aio_handle().
  • 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#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <stdio.h>
25#include <inttypes.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29
30#include <rtems.h>
31#include <rtems/rtc.h>
32#include <rtems/error.h>
33#include <rtems/shell.h>
34
35#define RTEMS_RTC_SHELL_ERROR( fmt, ...) \
36  do { \
37    printf( "error: " fmt "\n", ##__VA_ARGS__); \
38    return -1; \
39  } while (0)
40
41#define RTEMS_RTC_SHELL_ERROR_SC( sc, fmt, ...) \
42  if ((sc) != RTEMS_SUCCESSFUL) { \
43    printf( "error: " fmt ": %s\n", ##__VA_ARGS__, rtems_status_text( sc)); \
44    return -1; \
45  }
46
47static const char rtems_rtc_shell_usage [] =
48  "real time clock read and set\n"
49  "\n"
50  "rtc\n"
51  "\tprints the current time of day\n"
52  "\n"
53  "rtc YYYY-MM-DD [HH:MM:SS [TICKS]]\n"
54  "\tsets the time of day and real time clock";
55
56static int rtems_rtc_shell_main( int argc, char **argv)
57{
58  rtems_status_code sc = RTEMS_SUCCESSFUL;
59  rtems_time_of_day tod = {
60    .year = 1988,
61    .month = 1,
62    .day = 1,
63    .hour = 0,
64    .minute = 0,
65    .second = 0,
66    .ticks = 0
67  };
68
69  if (argc == 1) {
70    sc = rtems_clock_get_tod( &tod);
71    RTEMS_RTC_SHELL_ERROR_SC( sc, "get time of day");
72
73    printf(
74      "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
75        " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
76        " %02" PRIu32 "\n",
77      tod.year,
78      tod.month,
79      tod.day,
80      tod.hour,
81      tod.minute,
82      tod.second,
83      tod.ticks
84    );
85  } else if (argc > 1 && argc < 5) {
86    int rv = 0;
87    int fd = 0;
88    ssize_t n = 0;
89    uint32_t v [3];
90
91    if (argc > 1) {
92      rv = sscanf(
93        argv [1],
94        "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32,
95        v,
96        v + 1,
97        v + 2
98      );
99
100      if (rv == 3) {
101        tod.year = v [0];
102        tod.month = v [1];
103        tod.day = v [2];
104      } else {
105        RTEMS_RTC_SHELL_ERROR( "unexpected YYYY-MM-DD input: %s", argv [1]);
106      }
107    }
108
109    if (argc > 2) {
110      rv = sscanf(
111        argv [2],
112        "%04" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
113        v,
114        v + 1,
115        v + 2
116      );
117
118      if (rv == 3) {
119        tod.hour = v [0];
120        tod.minute = v [1];
121        tod.second = v [2];
122      } else {
123        RTEMS_RTC_SHELL_ERROR( "unexpected HH:MM:SS input: %s", argv [2]);
124      }
125    }
126
127    if (argc > 3) {
128      rv = sscanf( argv [3], "%5" PRIu32, v);
129
130      if (rv == 1) {
131        tod.ticks = v [0];
132      } else {
133        RTEMS_RTC_SHELL_ERROR( "unexpected TICKS input: %s", argv [3]);
134      }
135    }
136
137    sc = rtems_clock_set( &tod);
138    RTEMS_RTC_SHELL_ERROR_SC( sc, "set time of day");
139
140    fd = open( RTC_DEVICE_NAME, O_WRONLY);
141    if (fd < 0) {
142      perror( "error: open " RTC_DEVICE_NAME);
143      return -1;
144    }
145
146    n = write( fd, &tod, sizeof( tod));
147    if (n != (ssize_t) sizeof( tod)) {
148      perror( "error: write to " RTC_DEVICE_NAME);
149      close( fd);
150      return -1;
151    }
152
153    rv = close( fd);
154    if (rv != 0) {
155      perror( "error: close " RTC_DEVICE_NAME);
156      return -1;
157    }
158  } else {
159    puts( rtems_rtc_shell_usage);
160    return -1;
161  }
162
163  return 0;
164}
165
166struct rtems_shell_cmd_tt rtems_shell_RTC_Command = {
167  .name = "rtc",
168  .usage = rtems_rtc_shell_usage,
169  .topic = "misc",
170  .command = rtems_rtc_shell_main,
171  .alias = NULL,
172  .next = NULL
173};
Note: See TracBrowser for help on using the repository browser.