apps/nshlib: add uptime command support
run uptime command on sim: nsh> nsh> nsh> uptime 19:35:01 up 1:40, load average: 0.00, 0.00, 0.00 nsh> nsh> nsh> uptime -s 2022-09-16 17:54:26 nsh> nsh> nsh> uptime -p up 1 hour, 40 minutes nsh> nsh> nsh> uptime -h Usage: uptime [options] Options: -p, show uptime in pretty format -h, display this help and exit -s, system up since nsh> nsh> nsh> uptime -abc uptime: invalid option -- -abc Usage: uptime [options] Options: -p, show uptime in pretty format -h, display this help and exit -s, system up since nsh> nsh> nsh> date Fri, Sep 16 19:35:18 2022 nsh> nsh> Signed-off-by: Junbo Zheng <zhengjunbo1@xiaomi.com>
This commit is contained in:
parent
ac4b42fee7
commit
06f39d10f1
@ -518,6 +518,10 @@ config NSH_DISABLE_UNSET
|
|||||||
bool "Disable unset"
|
bool "Disable unset"
|
||||||
default DEFAULT_SMALL
|
default DEFAULT_SMALL
|
||||||
|
|
||||||
|
config NSH_DISABLE_UPTIME
|
||||||
|
bool "Disable uptime"
|
||||||
|
default DEFAULT_SMALL
|
||||||
|
|
||||||
config NSH_DISABLE_URLDECODE
|
config NSH_DISABLE_URLDECODE
|
||||||
bool "Disable urldecode"
|
bool "Disable urldecode"
|
||||||
default DEFAULT_SMALL
|
default DEFAULT_SMALL
|
||||||
|
@ -1204,6 +1204,10 @@ int cmd_pmconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
|||||||
int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CONFIG_NSH_DISABLE_UPTIME
|
||||||
|
int cmd_uptime(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_BASE64)
|
#if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_BASE64)
|
||||||
# ifndef CONFIG_NSH_DISABLE_BASE64DEC
|
# ifndef CONFIG_NSH_DISABLE_BASE64DEC
|
||||||
int cmd_base64decode(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
int cmd_base64decode(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
|
||||||
|
@ -564,6 +564,10 @@ static const struct cmdmap_s g_cmdmap[] =
|
|||||||
{ "unset", cmd_unset, 2, 2, "<name>" },
|
{ "unset", cmd_unset, 2, 2, "<name>" },
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CONFIG_NSH_DISABLE_UPTIME
|
||||||
|
{ "uptime", cmd_uptime, 1, 2, "[-sph]" },
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_URLCODE)
|
#if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_URLCODE)
|
||||||
# ifndef CONFIG_NSH_DISABLE_URLDECODE
|
# ifndef CONFIG_NSH_DISABLE_URLDECODE
|
||||||
{ "urldecode", cmd_urldecode, 2, 3, "[-f] <string or filepath>" },
|
{ "urldecode", cmd_urldecode, 2, 3, "[-f] <string or filepath>" },
|
||||||
|
@ -33,6 +33,8 @@
|
|||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <sys/sysinfo.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "nsh.h"
|
#include "nsh.h"
|
||||||
#include "nsh_console.h"
|
#include "nsh_console.h"
|
||||||
@ -45,6 +47,15 @@
|
|||||||
# define CONFIG_NSH_PROC_MOUNTPOINT "/proc"
|
# define CONFIG_NSH_PROC_MOUNTPOINT "/proc"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CONFIG_NSH_DISABLE_UPTIME
|
||||||
|
#ifndef FSHIFT
|
||||||
|
# define FSHIFT SI_LOAD_SHIFT
|
||||||
|
#endif
|
||||||
|
# define FIXED_1 (1 << FSHIFT) /* 1.0 as fixed-point */
|
||||||
|
# define LOAD_INT(x) ((x) >> FSHIFT)
|
||||||
|
# define LOAD_FRAC(x) (LOAD_INT(((x) & (FIXED_1 - 1)) * 100))
|
||||||
|
#endif
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Types
|
* Private Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -758,3 +769,114 @@ int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: cmd_uptime
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef CONFIG_NSH_DISABLE_UPTIME
|
||||||
|
int cmd_uptime(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
||||||
|
{
|
||||||
|
uint32_t updays;
|
||||||
|
uint32_t uphours;
|
||||||
|
uint32_t upminutes;
|
||||||
|
|
||||||
|
time_t current_time_seconds;
|
||||||
|
FAR struct tm *current_time;
|
||||||
|
|
||||||
|
struct sysinfo sys_info;
|
||||||
|
|
||||||
|
time_t uptime = 0;
|
||||||
|
|
||||||
|
bool pretty_format_opt = false;
|
||||||
|
bool system_load_opt = false;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
{
|
||||||
|
system_load_opt = true;
|
||||||
|
|
||||||
|
current_time_seconds = time(NULL);
|
||||||
|
current_time = localtime(¤t_time_seconds);
|
||||||
|
nsh_output(vtbl, "%02u:%02u:%02u ", current_time->tm_hour,
|
||||||
|
current_time->tm_min, current_time->tm_sec);
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[1], "-p") == 0)
|
||||||
|
{
|
||||||
|
pretty_format_opt = true;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[1], "-s") == 0)
|
||||||
|
{
|
||||||
|
sysinfo(&sys_info);
|
||||||
|
time(¤t_time_seconds);
|
||||||
|
current_time_seconds -= sys_info.uptime;
|
||||||
|
current_time = localtime(¤t_time_seconds);
|
||||||
|
nsh_output(vtbl, "%04u-%02u-%02u %02u:%02u:%02u\n",
|
||||||
|
current_time->tm_year + 1900, current_time->tm_mon + 1,
|
||||||
|
current_time->tm_mday, current_time->tm_hour,
|
||||||
|
current_time->tm_min, current_time->tm_sec);
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (strcmp(argv[1], "-h") != 0)
|
||||||
|
{
|
||||||
|
nsh_output(vtbl, "uptime: invalid option -- %s\n", argv[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
nsh_output(vtbl, "Usage:\n");
|
||||||
|
nsh_output(vtbl, "uptime [options]\n");
|
||||||
|
|
||||||
|
nsh_output(vtbl, "Options:\n");
|
||||||
|
nsh_output(vtbl, "-p, show uptime in pretty format\n");
|
||||||
|
nsh_output(vtbl, "-h, display this help and exit\n");
|
||||||
|
nsh_output(vtbl, "-s, system up since\n");
|
||||||
|
|
||||||
|
return ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
sysinfo(&sys_info);
|
||||||
|
uptime = sys_info.uptime;
|
||||||
|
|
||||||
|
updays = uptime / 86400;
|
||||||
|
uptime -= updays * 86400;
|
||||||
|
uphours = uptime / 3600;
|
||||||
|
uptime -= uphours * 3600;
|
||||||
|
upminutes = uptime / 60;
|
||||||
|
|
||||||
|
nsh_output(vtbl, "up ");
|
||||||
|
|
||||||
|
if (updays)
|
||||||
|
{
|
||||||
|
nsh_output(vtbl, "%" PRIu32 " day%s, ", updays,
|
||||||
|
(updays > 1) ? "s" : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pretty_format_opt)
|
||||||
|
{
|
||||||
|
if (uphours)
|
||||||
|
{
|
||||||
|
nsh_output(vtbl, "%" PRIu32 " hour%s, ", uphours,
|
||||||
|
(uphours > 1) ? "s" : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
nsh_output(vtbl, "%" PRIu32 " minute%s", upminutes,
|
||||||
|
(upminutes > 1) ? "s" : "");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nsh_output(vtbl, "%2" PRIu32 ":" "%02" PRIu32, uphours, upminutes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (system_load_opt)
|
||||||
|
{
|
||||||
|
nsh_output(vtbl, ", load average: %lu.%02lu, %lu.%02lu, %lu.%02lu",
|
||||||
|
LOAD_INT(sys_info.loads[0]), LOAD_FRAC(sys_info.loads[0]),
|
||||||
|
LOAD_INT(sys_info.loads[1]), LOAD_FRAC(sys_info.loads[1]),
|
||||||
|
LOAD_INT(sys_info.loads[2]), LOAD_FRAC(sys_info.loads[2]));
|
||||||
|
}
|
||||||
|
|
||||||
|
nsh_output(vtbl, "\n");
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user