Skip to content

Commit 1a72a14

Browse files
committed
[core] Added builtin command to manage RTC.
1 parent 206b17c commit 1a72a14

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

src/commands.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* DreamShell ##version## *
33
* commands.c *
44
* DreamShell CMD *
5-
* (c)2004-2023 SWAT *
5+
* (c)2004-2024 SWAT *
66
* http://www.dc-swat.ru *
77
****************************/
88

@@ -2007,6 +2007,57 @@ static int builtin_callfunc(int argc, char *argv[]) {
20072007
}
20082008

20092009

2010+
static int builtin_rtc(int argc, char *argv[]) {
2011+
2012+
if(argc == 1) {
2013+
ds_printf("Usage: %s option args...\n"
2014+
"Options: \n"
2015+
" -s, --set -Set value\n"
2016+
" -g, --get -Get value\n", argv[0]);
2017+
ds_printf("Arguments: \n"
2018+
" -u, --unix -UNIX time to set\n");
2019+
return CMD_NO_ARG;
2020+
}
2021+
2022+
int set_rtc_value = 0, get_rtc_value = 0;
2023+
int input_value = 0;
2024+
time_t unix_time;
2025+
struct tm *time;
2026+
2027+
struct cfg_option options[] = {
2028+
{"set", 's', NULL, CFG_BOOL, (void *) &set_rtc_value, 0},
2029+
{"get", 'g', NULL, CFG_BOOL, (void *) &get_rtc_value, 0},
2030+
{"unix", 'u', NULL, CFG_INT, (void *) &input_value, 0},
2031+
CFG_END_OF_LIST
2032+
};
2033+
2034+
CMD_DEFAULT_ARGS_PARSER(options);
2035+
2036+
if(set_rtc_value) {
2037+
if(input_value > 0) {
2038+
unix_time = (time_t)input_value;
2039+
rtc_set_unix_secs(unix_time);
2040+
}
2041+
return CMD_OK;
2042+
}
2043+
2044+
if(get_rtc_value) {
2045+
unix_time = rtc_unix_secs();
2046+
time = gmtime(&unix_time);
2047+
2048+
if(unix_time < 0 || time == NULL) {
2049+
ds_printf("DS_ERROR: RTC value is invalid");
2050+
return CMD_ERROR;
2051+
}
2052+
ds_printf("DS_INFO: %s", ctime(&unix_time));
2053+
return CMD_OK;
2054+
}
2055+
2056+
ds_printf("DS_ERROR: There is no option.\n");
2057+
return CMD_NO_ARG;
2058+
}
2059+
2060+
20102061
/* Setup all our builtins */
20112062
int InitCmd() {
20122063

@@ -2049,6 +2100,7 @@ int InitCmd() {
20492100
AddCmd("console", "Console manager", (CmdHandler *) builtin_console);
20502101
AddCmd("speedtest", "Testing r/w speed of device", (CmdHandler *) builtin_speedtest);
20512102
AddCmd("callfunc", "Call any exported function", (CmdHandler *) builtin_callfunc);
2103+
AddCmd("rtc", "Real Time Clock manager", (CmdHandler *) builtin_rtc);
20522104
//ds_printf("Command list initialised.\n");
20532105
return 1;
20542106
}

0 commit comments

Comments
 (0)