1 /*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 * Copyright (c) 2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "kernel_shell.h"
9
10 #include <zephyr/kernel.h>
11
12 #define MINUTES_FACTOR (MSEC_PER_SEC * SEC_PER_MIN)
13 #define HOURS_FACTOR (MINUTES_FACTOR * MIN_PER_HOUR)
14 #define DAYS_FACTOR (HOURS_FACTOR * HOUR_PER_DAY)
15
cmd_kernel_uptime(const struct shell * sh,size_t argc,char ** argv)16 static int cmd_kernel_uptime(const struct shell *sh, size_t argc, char **argv)
17 {
18 int64_t milliseconds = k_uptime_get();
19 int64_t days;
20 int64_t hours;
21 int64_t minutes;
22 int64_t seconds;
23
24 if (argc == 1) {
25 shell_print(sh, "Uptime: %llu ms", milliseconds);
26 return 0;
27 }
28
29 /* No need to enable the getopt and getopt_long for just one option. */
30 if (strcmp("-p", argv[1]) && strcmp("--pretty", argv[1]) != 0) {
31 shell_error(sh, "Unsupported option: %s", argv[1]);
32 return -EIO;
33 }
34
35 days = milliseconds / DAYS_FACTOR;
36 milliseconds %= DAYS_FACTOR;
37 hours = milliseconds / HOURS_FACTOR;
38 milliseconds %= HOURS_FACTOR;
39 minutes = milliseconds / MINUTES_FACTOR;
40 milliseconds %= MINUTES_FACTOR;
41 seconds = milliseconds / MSEC_PER_SEC;
42 milliseconds = milliseconds % MSEC_PER_SEC;
43
44 shell_print(sh,
45 "uptime: %llu days, %llu hours, %llu minutes, %llu seconds, %llu milliseconds",
46 days, hours, minutes, seconds, milliseconds);
47
48 return 0;
49 }
50
51 KERNEL_CMD_ARG_ADD(uptime, NULL, "Kernel uptime. Can be called with the -p or --pretty options",
52 cmd_kernel_uptime, 1, 1);
53