File indexing completed on 2025-05-11 08:24:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifdef HAVE_CONFIG_H
0016 #include "config.h"
0017 #endif
0018
0019 #include <stdio.h>
0020 #include <inttypes.h>
0021 #include <sys/types.h>
0022 #include <sys/stat.h>
0023 #include <fcntl.h>
0024
0025 #include <rtems.h>
0026 #include <rtems/rtc.h>
0027 #include <rtems/error.h>
0028 #include <rtems/shell.h>
0029
0030 #define RTEMS_RTC_SHELL_ERROR( fmt, ...) \
0031 do { \
0032 printf( "error: " fmt "\n", ##__VA_ARGS__); \
0033 return -1; \
0034 } while (0)
0035
0036 #define RTEMS_RTC_SHELL_ERROR_SC( sc, fmt, ...) \
0037 if ((sc) != RTEMS_SUCCESSFUL) { \
0038 printf( "error: " fmt ": %s\n", ##__VA_ARGS__, rtems_status_text( sc)); \
0039 return -1; \
0040 }
0041
0042 static const char rtems_rtc_shell_usage [] =
0043 "real time clock read and set\n"
0044 "\n"
0045 "rtc\n"
0046 "\tprints the current time of day\n"
0047 "\n"
0048 "rtc YYYY-MM-DD [HH:MM:SS [TICKS]]\n"
0049 "\tsets the time of day and real time clock";
0050
0051 static int rtems_rtc_shell_main( int argc, char **argv)
0052 {
0053 rtems_status_code sc = RTEMS_SUCCESSFUL;
0054 rtems_time_of_day tod = {
0055 .year = 1988,
0056 .month = 1,
0057 .day = 1,
0058 .hour = 0,
0059 .minute = 0,
0060 .second = 0,
0061 .ticks = 0
0062 };
0063
0064 if (argc == 1) {
0065 sc = rtems_clock_get_tod( &tod);
0066 RTEMS_RTC_SHELL_ERROR_SC( sc, "get time of day");
0067
0068 printf(
0069 "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32
0070 " %02" PRIu32 ":%02" PRIu32 ":%02" PRIu32
0071 " %02" PRIu32 "\n",
0072 tod.year,
0073 tod.month,
0074 tod.day,
0075 tod.hour,
0076 tod.minute,
0077 tod.second,
0078 tod.ticks
0079 );
0080 } else if (argc > 1 && argc < 5) {
0081 int rv = 0;
0082 int fd = 0;
0083 ssize_t n = 0;
0084 uint32_t v [3];
0085
0086 if (argc > 1) {
0087 rv = sscanf(
0088 argv [1],
0089 "%04" PRIu32 "-%02" PRIu32 "-%02" PRIu32,
0090 v,
0091 v + 1,
0092 v + 2
0093 );
0094
0095 if (rv == 3) {
0096 tod.year = v [0];
0097 tod.month = v [1];
0098 tod.day = v [2];
0099 } else {
0100 RTEMS_RTC_SHELL_ERROR( "unexpected YYYY-MM-DD input: %s", argv [1]);
0101 }
0102 }
0103
0104 if (argc > 2) {
0105 rv = sscanf(
0106 argv [2],
0107 "%04" PRIu32 ":%02" PRIu32 ":%02" PRIu32,
0108 v,
0109 v + 1,
0110 v + 2
0111 );
0112
0113 if (rv == 3) {
0114 tod.hour = v [0];
0115 tod.minute = v [1];
0116 tod.second = v [2];
0117 } else {
0118 RTEMS_RTC_SHELL_ERROR( "unexpected HH:MM:SS input: %s", argv [2]);
0119 }
0120 }
0121
0122 if (argc > 3) {
0123 rv = sscanf( argv [3], "%5" PRIu32, v);
0124
0125 if (rv == 1) {
0126 tod.ticks = v [0];
0127 } else {
0128 RTEMS_RTC_SHELL_ERROR( "unexpected TICKS input: %s", argv [3]);
0129 }
0130 }
0131
0132 sc = rtems_clock_set( &tod);
0133 RTEMS_RTC_SHELL_ERROR_SC( sc, "set time of day");
0134
0135 fd = open( RTC_DEVICE_NAME, O_WRONLY);
0136 if (fd < 0) {
0137 perror( "error: open " RTC_DEVICE_NAME);
0138 return -1;
0139 }
0140
0141 n = write( fd, &tod, sizeof( tod));
0142 if (n != (ssize_t) sizeof( tod)) {
0143 perror( "error: write to " RTC_DEVICE_NAME);
0144 close( fd);
0145 return -1;
0146 }
0147
0148 rv = close( fd);
0149 if (rv != 0) {
0150 perror( "error: close " RTC_DEVICE_NAME);
0151 return -1;
0152 }
0153 } else {
0154 puts( rtems_rtc_shell_usage);
0155 return -1;
0156 }
0157
0158 return 0;
0159 }
0160
0161 struct rtems_shell_cmd_tt rtems_shell_RTC_Command = {
0162 .name = "rtc",
0163 .usage = rtems_rtc_shell_usage,
0164 .topic = "misc",
0165 .command = rtems_rtc_shell_main,
0166 .alias = NULL,
0167 .next = NULL
0168 };