shell_cmd.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file shell_cmd.c
  15. * @brief Simple CLI shell common commands code.
  16. *
  17. * @addtogroup SHELL
  18. * @{
  19. */
  20. #include <string.h>
  21. #include "ch.h"
  22. #include "hal.h"
  23. #include "shell.h"
  24. #include "shell_cmd.h"
  25. #include "chprintf.h"
  26. #if (SHELL_CMD_TEST_ENABLED == TRUE) || defined(__DOXYGEN__)
  27. #include "rt_test_root.h"
  28. #include "oslib_test_root.h"
  29. #endif
  30. /*===========================================================================*/
  31. /* Module local definitions. */
  32. /*===========================================================================*/
  33. /*===========================================================================*/
  34. /* Module exported variables. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Module local types. */
  38. /*===========================================================================*/
  39. /*===========================================================================*/
  40. /* Module local variables. */
  41. /*===========================================================================*/
  42. /*===========================================================================*/
  43. /* Module local functions. */
  44. /*===========================================================================*/
  45. #if ((SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)) || \
  46. defined(__DOXYGEN__)
  47. static void cmd_exit(BaseSequentialStream *chp, int argc, char *argv[]) {
  48. (void)argv;
  49. if (argc > 0) {
  50. shellUsage(chp, "exit");
  51. return;
  52. }
  53. shellExit(MSG_OK);
  54. }
  55. #endif
  56. #if (SHELL_CMD_INFO_ENABLED == TRUE) || defined(__DOXYGEN__)
  57. static void cmd_info(BaseSequentialStream *chp, int argc, char *argv[]) {
  58. (void)argv;
  59. if (argc > 0) {
  60. shellUsage(chp, "info");
  61. return;
  62. }
  63. chprintf(chp, "Kernel: %s" SHELL_NEWLINE_STR, CH_KERNEL_VERSION);
  64. #ifdef PORT_COMPILER_NAME
  65. chprintf(chp, "Compiler: %s" SHELL_NEWLINE_STR, PORT_COMPILER_NAME);
  66. #endif
  67. chprintf(chp, "Architecture: %s" SHELL_NEWLINE_STR, PORT_ARCHITECTURE_NAME);
  68. #ifdef PORT_CORE_VARIANT_NAME
  69. chprintf(chp, "Core Variant: %s" SHELL_NEWLINE_STR, PORT_CORE_VARIANT_NAME);
  70. #endif
  71. #ifdef PORT_INFO
  72. chprintf(chp, "Port Info: %s" SHELL_NEWLINE_STR, PORT_INFO);
  73. #endif
  74. #ifdef PLATFORM_NAME
  75. chprintf(chp, "Platform: %s" SHELL_NEWLINE_STR, PLATFORM_NAME);
  76. #endif
  77. #ifdef BOARD_NAME
  78. chprintf(chp, "Board: %s" SHELL_NEWLINE_STR, BOARD_NAME);
  79. #endif
  80. #ifdef __DATE__
  81. #ifdef __TIME__
  82. chprintf(chp, "Build time: %s%s%s" SHELL_NEWLINE_STR, __DATE__, " - ", __TIME__);
  83. #endif
  84. #endif
  85. }
  86. #endif
  87. #if (SHELL_CMD_ECHO_ENABLED == TRUE) || defined(__DOXYGEN__)
  88. static void cmd_echo(BaseSequentialStream *chp, int argc, char *argv[]) {
  89. (void)argv;
  90. if (argc != 1) {
  91. shellUsage(chp, "echo \"message\"");
  92. return;
  93. }
  94. chprintf(chp, "%s" SHELL_NEWLINE_STR, argv[0]);
  95. }
  96. #endif
  97. #if (SHELL_CMD_SYSTIME_ENABLED == TRUE) || defined(__DOXYGEN__)
  98. static void cmd_systime(BaseSequentialStream *chp, int argc, char *argv[]) {
  99. (void)argv;
  100. if (argc > 0) {
  101. shellUsage(chp, "systime");
  102. return;
  103. }
  104. chprintf(chp, "%lu" SHELL_NEWLINE_STR, (unsigned long)chVTGetSystemTime());
  105. }
  106. #endif
  107. #if (SHELL_CMD_MEM_ENABLED == TRUE) || defined(__DOXYGEN__)
  108. static void cmd_mem(BaseSequentialStream *chp, int argc, char *argv[]) {
  109. size_t n, total, largest;
  110. (void)argv;
  111. if (argc > 0) {
  112. shellUsage(chp, "mem");
  113. return;
  114. }
  115. n = chHeapStatus(NULL, &total, &largest);
  116. chprintf(chp, "core free memory : %u bytes" SHELL_NEWLINE_STR, chCoreGetStatusX());
  117. chprintf(chp, "heap fragments : %u" SHELL_NEWLINE_STR, n);
  118. chprintf(chp, "heap free total : %u bytes" SHELL_NEWLINE_STR, total);
  119. chprintf(chp, "heap free largest: %u bytes" SHELL_NEWLINE_STR, largest);
  120. }
  121. #endif
  122. #if (SHELL_CMD_THREADS_ENABLED == TRUE) || defined(__DOXYGEN__)
  123. static void cmd_threads(BaseSequentialStream *chp, int argc, char *argv[]) {
  124. static const char *states[] = {CH_STATE_NAMES};
  125. thread_t *tp;
  126. (void)argv;
  127. if (argc > 0) {
  128. shellUsage(chp, "threads");
  129. return;
  130. }
  131. chprintf(chp, "stklimit stack addr refs prio state name\r\n" SHELL_NEWLINE_STR);
  132. tp = chRegFirstThread();
  133. do {
  134. #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE)
  135. uint32_t stklimit = (uint32_t)tp->wabase;
  136. #else
  137. uint32_t stklimit = 0U;
  138. #endif
  139. chprintf(chp, "%08lx %08lx %08lx %4lu %4lu %9s %12s" SHELL_NEWLINE_STR,
  140. stklimit, (uint32_t)tp->ctx.sp, (uint32_t)tp,
  141. (uint32_t)tp->refs - 1, (uint32_t)tp->prio, states[tp->state],
  142. tp->name == NULL ? "" : tp->name);
  143. tp = chRegNextThread(tp);
  144. } while (tp != NULL);
  145. }
  146. #endif
  147. #if (SHELL_CMD_TEST_ENABLED == TRUE) || defined(__DOXYGEN__)
  148. static THD_FUNCTION(test_rt, arg) {
  149. BaseSequentialStream *chp = (BaseSequentialStream *)arg;
  150. test_execute(chp, &rt_test_suite);
  151. }
  152. static THD_FUNCTION(test_oslib, arg) {
  153. BaseSequentialStream *chp = (BaseSequentialStream *)arg;
  154. test_execute(chp, &oslib_test_suite);
  155. }
  156. static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) {
  157. thread_t *tp;
  158. tfunc_t tfp;
  159. (void)argv;
  160. if (argc != 1) {
  161. shellUsage(chp, "test rt|oslib");
  162. return;
  163. }
  164. if (!strcmp(argv[0], "rt")) {
  165. tfp = test_rt;
  166. }
  167. else if (!strcmp(argv[0], "oslib")) {
  168. tfp = test_oslib;
  169. }
  170. else {
  171. shellUsage(chp, "test rt|oslib");
  172. return;
  173. }
  174. tp = chThdCreateFromHeap(NULL, SHELL_CMD_TEST_WA_SIZE,
  175. "test", chThdGetPriorityX(),
  176. tfp, chp);
  177. if (tp == NULL) {
  178. chprintf(chp, "out of memory" SHELL_NEWLINE_STR);
  179. return;
  180. }
  181. chThdWait(tp);
  182. }
  183. #endif
  184. /*===========================================================================*/
  185. /* Module exported functions. */
  186. /*===========================================================================*/
  187. /**
  188. * @brief Array of the default commands.
  189. */
  190. const ShellCommand shell_local_commands[] = {
  191. #if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
  192. {"exit", cmd_exit},
  193. #endif
  194. #if SHELL_CMD_INFO_ENABLED == TRUE
  195. {"info", cmd_info},
  196. #endif
  197. #if SHELL_CMD_ECHO_ENABLED == TRUE
  198. {"echo", cmd_echo},
  199. #endif
  200. #if SHELL_CMD_SYSTIME_ENABLED == TRUE
  201. {"systime", cmd_systime},
  202. #endif
  203. #if SHELL_CMD_MEM_ENABLED == TRUE
  204. {"mem", cmd_mem},
  205. #endif
  206. #if SHELL_CMD_THREADS_ENABLED == TRUE
  207. {"threads", cmd_threads},
  208. #endif
  209. #if SHELL_CMD_TEST_ENABLED == TRUE
  210. {"test", cmd_test},
  211. #endif
  212. {NULL, NULL}
  213. };
  214. /** @} */