1 /*
2  * Copyright (c) 2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/shell/shell.h>
8 #include <zephyr/stats/stats.h>
9 
stats_cb(struct stats_hdr * hdr,void * arg,const char * name,uint16_t off)10 static int stats_cb(struct stats_hdr *hdr, void *arg, const char *name, uint16_t off)
11 {
12 	struct shell *sh = arg;
13 	void *addr = (uint8_t *)hdr + off;
14 	uint64_t val = 0;
15 
16 	switch (hdr->s_size) {
17 	case sizeof(uint16_t):
18 		val = *(uint16_t *)(addr);
19 		break;
20 
21 	case sizeof(uint32_t):
22 		val = *(uint32_t *)(addr);
23 		break;
24 	case sizeof(uint64_t):
25 		val = *(uint64_t *)(addr);
26 		break;
27 	}
28 	shell_print(sh, "\t%s (offset: %u, addr: %p): %" PRIu64, name, off, addr, val);
29 	return 0;
30 }
31 
stats_group_cb(struct stats_hdr * hdr,void * arg)32 static int stats_group_cb(struct stats_hdr *hdr, void *arg)
33 {
34 	struct shell *sh = arg;
35 
36 	shell_print(sh, "Stats Group %s (hdr addr: %p)", hdr->s_name, (void *)hdr);
37 	return stats_walk(hdr, stats_cb, arg);
38 }
39 
cmd_stats_list(const struct shell * sh,size_t argc,char ** argv)40 static int cmd_stats_list(const struct shell *sh, size_t argc,
41 			  char **argv)
42 {
43 	return stats_group_walk(stats_group_cb, (struct shell *)sh);
44 }
45 
46 SHELL_STATIC_SUBCMD_SET_CREATE(sub_stats,
47 			       SHELL_CMD(list, NULL, "List stats", cmd_stats_list),
48 			       SHELL_SUBCMD_SET_END /* Array terminated. */
49 			       );
50 
51 SHELL_CMD_REGISTER(stats, &sub_stats, "Stats commands", NULL);
52