Skip to content

Commit

Permalink
sys/shell: rewrite rtc command for periph/rtc
Browse files Browse the repository at this point in the history
  • Loading branch information
LudwigKnuepfer committed Dec 16, 2014
1 parent 2ec0a1c commit 4942882
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 52 deletions.
10 changes: 7 additions & 3 deletions sys/shell/commands/Makefile
Expand Up @@ -31,9 +31,6 @@ endif
ifneq (,$(filter rpl,$(USEMODULE)))
SRC += sc_rpl.c
endif
ifneq (,$(filter rtc,$(USEMODULE)))
SRC += sc_rtc.c
endif
ifneq (,$(filter sht11,$(USEMODULE)))
SRC += sc_sht11.c
endif
Expand All @@ -59,4 +56,11 @@ ifneq (,$(filter lsm303dlhc,$(USEMODULE)))
SRC += sc_lsm303dlhc.c
endif

# TODO
# Conditional building not possible at the moment due to
# https://github.com/RIOT-OS/RIOT/issues/2058
# The implementation is guarded in the source file instead, this
# should be changed once the issue has been resolved
SRC += sc_rtc.c

include $(RIOTBASE)/Makefile.base
189 changes: 147 additions & 42 deletions sys/shell/commands/sc_rtc.c
@@ -1,76 +1,181 @@
/**
* Shell commands for real time clock
*
* Copyright (C) 2013 INRIA.
/*
* Copyright 2013 INRIA.
* Copyright 2014 Ludwig Ortmann <ludwig.ortmann@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 shell_commands
* @{
* @file sc_rtc.c
* @brief provides shell commands to access the rtc
* @file
* @brief Shell command implementation for the peripheral RTC interface
*
* @author Oliver Hahm <oliver.hahm@inria.fr>
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
* @}
*/

#ifdef FEATURE_PERIPH_RTC

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#ifdef MODULE_RTC
#include "rtc.h"
#include "periph/rtc.h"

static void _gettime_handler(void)
void _alarm_handler(void *arg)
{
struct tm now;
rtc_get_localtime(&now);
(void) arg;

/* cppcheck: see man 3 asctime: obsoleted by POSIX.1-2008 */
/* cppcheck-suppress obsoleteFunctionsasctime */
printf("%s", asctime(&now));
puts("The alarm rang");
}

static void _settime_handler(char **argv)
static int dow(int year, int month, int day)
{
do {
short i1, i2, i3;
/* 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;
}

int res = sscanf(argv[1], "%6hd-%6hd-%6hd", &i1, &i2, &i3);
if (res != 3) {
break;
}

struct tm now;
now.tm_year = i1 - 1900;
now.tm_mon = i2 - 1;
now.tm_mday = i3;
static int _parse_time(char **argv, struct tm *time)
{
short i1, i2, i3;

if(sscanf(argv[0], "%6hd-%6hd-%6hd", &i1, &i2, &i3) != 3) {
puts("could not parse date");
return -1;
}

time->tm_year = i1 - 1900;
time->tm_mon = i2 - 1;
time->tm_mday = i3;
time->tm_wday = dow(i1, i2, i3);

if(sscanf(argv[1], "%6hd:%6hd:%6hd", &i1, &i2, &i3) != 3) {
puts("could not parse time");
return -1;
}

time->tm_hour = i1;
time->tm_min = i2;
time->tm_sec = i3;

time->tm_isdst = -1; /* undefined */

return 0;
}

static void _rtc_getalarm(void)
{
struct tm now;
if (rtc_get_alarm(&now) == 0) {
/* cppcheck: see man 3 asctime: obsoleted by POSIX.1-2008 */
/* cppcheck-suppress obsoleteFunctionsasctime */
printf("%s", asctime(&now));
}
else {
puts("rtc: error getting alarm");
}
}

static void _rtc_setalarm(char **argv)
{
struct tm now;

res = sscanf(argv[2], "%6hd:%6hd:%6hd", &i1, &i2, &i3);
if (res != 3) {
break;
if (_parse_time(argv, &now) == 0) {
if (rtc_set_alarm(&now, _alarm_handler, NULL) == -1) {
puts("rtc: error setting alarm");
}
now.tm_hour = i1;
now.tm_min = i2;
now.tm_sec = i3;
}
}

static void _rtc_gettime(void)
{
struct tm now;
if (rtc_get_time(&now) == 0) {
/* cppcheck: see man 3 asctime: obsoleted by POSIX.1-2008 */
/* cppcheck-suppress obsoleteFunctionsasctime */
printf("%s", asctime(&now));
}
else {
puts("rtc: error getting time");
}
}

static void _rtc_settime(char **argv)
{
struct tm now;

rtc_set_localtime(&now);
puts("OK");
return;
} while (0);
if (_parse_time(argv, &now) == 0) {
if (rtc_set_time(&now) == -1) {
puts("rtc: error setting time");
}
}
}

printf("Usage: %s YYYY-MM-DD hh:mm:ss\n", argv[0]);
static void _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");
}

void _date_handler(int argc, char **argv)
void _rtc_handler(int argc, char **argv)
{
if (argc != 3) {
_gettime_handler();
if (argc < 2) {
_rtc_usage();
}
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) {
_rtc_setalarm(argv + 2);
}
else if (strncmp(argv[1], "gettime", 7) == 0) {
_rtc_gettime();
}
else if (strncmp(argv[1], "settime", 7) == 0) {
_rtc_settime(argv + 2);
}
else {
_settime_handler(argv);
printf("unknown command: %s\n", argv[1]);
}
}

#endif
#else

#include <stdio.h>

void _rtc_handler(int argc, char **argv)
{
(void) argc;
(void) argv;

puts("not implemented");
}

#endif /* FEATURE_RTC */
14 changes: 7 additions & 7 deletions sys/shell/commands/shell_commands.c
Expand Up @@ -38,10 +38,6 @@ extern void _heap_handler(int argc, char **argv);
extern void _ps_handler(int argc, char **argv);
#endif

#ifdef MODULE_RTC
extern void _date_handler(int argc, char **argv);
#endif

#ifdef MODULE_SHT11
extern void _get_temperature_handler(int argc, char **argv);
extern void _get_humidity_handler(int argc, char **argv);
Expand Down Expand Up @@ -74,6 +70,10 @@ extern void _get_current_handler(int argc, char **argv);
extern void _reset_current_handler(int argc, char **argv);
#endif

#if FEATURE_PERIPH_RTC
extern void _rtc_handler(int argc, char **argv);
#endif

#ifdef CPU_X86
extern void _x86_lspci(int argc, char **argv);
#endif
Expand Down Expand Up @@ -162,9 +162,6 @@ const shell_command_t _shell_command_list[] = {
#ifdef MODULE_PS
{"ps", "Prints information about running threads.", _ps_handler},
#endif
#ifdef MODULE_RTC
{"date", "Gets or sets current date and time.", _date_handler},
#endif
#ifdef MODULE_SHT11
{"temp", "Prints measured temperature.", _get_temperature_handler},
{"hum", "Prints measured humidity.", _get_humidity_handler},
Expand Down Expand Up @@ -241,6 +238,9 @@ const shell_command_t _shell_command_list[] = {
{ "mersenne_init", "initializes the PRNG", _mersenne_init },
{ "mersenne_get", "returns 32 bit of pseudo randomness", _mersenne_get },
#endif
#if FEATURE_PERIPH_RTC
{"rtc", "control RTC peripheral interface", _rtc_handler},
#endif
#ifdef CPU_X86
{"lspci", "Lists PCI devices", _x86_lspci},
#endif
Expand Down

0 comments on commit 4942882

Please sign in to comment.