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

Last change on this file was bcef89f2, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/23 at 06:18:25

Update company name

The embedded brains GmbH & Co. KG is the legal successor of embedded
brains GmbH.

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