Repository: incubator-mynewt-core Updated Branches: refs/heads/master 33f4fbaed -> 5d37c6ece
sys/shell: add os and prompt subshells os subshell enables system utility commands. prompt subshell contains an example of a dynamic prompt. it can be used to provide additional information, like the address of a connected bluetooth device. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/98eebe05 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/98eebe05 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/98eebe05 Branch: refs/heads/master Commit: 98eebe0597d02a1a52857ee6d8b973eedb165982 Parents: 31d56fc Author: MichaÅ Narajowski <michal.narajow...@codecoup.pl> Authored: Wed May 3 11:08:11 2017 +0200 Committer: MichaÅ Narajowski <michal.narajow...@codecoup.pl> Committed: Wed May 3 11:37:52 2017 +0200 ---------------------------------------------------------------------- sys/shell/src/shell.c | 8 ++ sys/shell/src/shell_os.c | 226 ++++++++++++++++++++++++++++++++++++++ sys/shell/src/shell_priv.h | 36 ++++++ sys/shell/src/shell_prompt.c | 93 ++++++++++++++++ sys/shell/syscfg.yml | 7 ++ 5 files changed, 370 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98eebe05/sys/shell/src/shell.c ---------------------------------------------------------------------- diff --git a/sys/shell/src/shell.c b/sys/shell/src/shell.c index f33f4ba..fa5a8f5 100644 --- a/sys/shell/src/shell.c +++ b/sys/shell/src/shell.c @@ -26,6 +26,7 @@ #include "console/console.h" #include "sysinit/sysinit.h" #include "shell/shell.h" +#include "shell_priv.h" #define SHELL_PROMPT "shell> " @@ -490,4 +491,11 @@ shell_init(void) line_queue_init(); prompt = SHELL_PROMPT; console_set_queues(&avail_queue, os_eventq_dflt_get()); + +#if MYNEWT_VAL(SHELL_OS_MODULE) + shell_os_register(shell_register); +#endif +#if MYNEWT_VAL(SHELL_PROMPT_MODULE) + shell_prompt_register(shell_register); +#endif } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98eebe05/sys/shell/src/shell_os.c ---------------------------------------------------------------------- diff --git a/sys/shell/src/shell_os.c b/sys/shell/src/shell_os.c new file mode 100644 index 0000000..9abf712 --- /dev/null +++ b/sys/shell/src/shell_os.c @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <assert.h> +#include <string.h> + +#include "syscfg/syscfg.h" +#include "os/os.h" +#include "datetime/datetime.h" +#include "console/console.h" + +#include "shell/shell.h" +#include "shell_priv.h" + +#define SHELL_OS "os" + +int +shell_os_tasks_display_cmd(int argc, char **argv) +{ + struct os_task *prev_task; + struct os_task_info oti; + char *name; + int found; + + name = NULL; + found = 0; + + if (argc > 1 && strcmp(argv[1], "")) { + name = argv[1]; + } + + console_printf("Tasks: \n"); + prev_task = NULL; + console_printf("%8s %3s %3s %8s %8s %8s %8s %8s %8s %3s\n", + "task", "pri", "tid", "runtime", "csw", "stksz", "stkuse", + "lcheck", "ncheck", "flg"); + while (1) { + prev_task = os_task_info_get_next(prev_task, &oti); + if (prev_task == NULL) { + break; + } + + if (name) { + if (strcmp(name, oti.oti_name)) { + continue; + } else { + found = 1; + } + } + + console_printf("%8s %3u %3u %8lu %8lu %8u %8u %8lu %8lu %3x\n", + oti.oti_name, oti.oti_prio, oti.oti_taskid, + (unsigned long)oti.oti_runtime, (unsigned long)oti.oti_cswcnt, + oti.oti_stksize, oti.oti_stkusage, + (unsigned long)oti.oti_last_checkin, + (unsigned long)oti.oti_next_checkin, oti.oti_flags); + + } + + if (name && !found) { + console_printf("Couldn't find task with name %s\n", name); + } + + return 0; +} + +int +shell_os_mpool_display_cmd(int argc, char **argv) +{ + struct os_mempool *mp; + struct os_mempool_info omi; + char *name; + int found; + + name = NULL; + found = 0; + + if (argc > 1 && strcmp(argv[1], "")) { + name = argv[1]; + } + + console_printf("Mempools: \n"); + mp = NULL; + console_printf("%32s %5s %4s %4s %4s\n", "name", "blksz", "cnt", "free", + "min"); + while (1) { + mp = os_mempool_info_get_next(mp, &omi); + if (mp == NULL) { + break; + } + + if (name) { + if (strcmp(name, omi.omi_name)) { + continue; + } else { + found = 1; + } + } + + console_printf("%32s %5d %4d %4d %4d\n", omi.omi_name, + omi.omi_block_size, omi.omi_num_blocks, + omi.omi_num_free, omi.omi_min_free); + } + + if (name && !found) { + console_printf("Couldn't find a memory pool with name %s\n", + name); + } + + return 0; +} + +int +shell_os_date_cmd(int argc, char **argv) +{ + struct os_timeval tv; + struct os_timezone tz; + char buf[DATETIME_BUFSIZE]; + int rc; + + argc--; argv++; /* skip command name */ + + if (argc == 0) { + /* Display the current datetime */ + rc = os_gettimeofday(&tv, &tz); + assert(rc == 0); + rc = datetime_format(&tv, &tz, buf, sizeof(buf)); + assert(rc == 0); + console_printf("%s\n", buf); + } else if (argc == 1) { + /* Set the current datetime */ + rc = datetime_parse(*argv, &tv, &tz); + if (rc == 0) { + rc = os_settimeofday(&tv, &tz); + } else { + console_printf("Invalid datetime\n"); + } + } else { + rc = -1; + } + + return rc; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param tasks_params[] = { + {"", "task name"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help tasks_help = { + .summary = "show os tasks", + .usage = NULL, + .params = tasks_params, +}; + +static const struct shell_param mpool_params[] = { + {"", "mpool name"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help mpool_help = { + .summary = "show system mpool", + .usage = NULL, + .params = mpool_params, +}; + +static const struct shell_param date_params[] = { + {"", "datetime to set"}, + {NULL, NULL} +}; + +static const struct shell_cmd_help date_help = { + .summary = "show system date", + .usage = NULL, + .params = date_params, +}; +#endif + +static const struct shell_cmd os_commands[] = { + { + .sc_cmd = "tasks", + .sc_cmd_func = shell_os_tasks_display_cmd, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &tasks_help, +#endif + }, + { + .sc_cmd = "mpool", + .sc_cmd_func = shell_os_mpool_display_cmd, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &mpool_help, +#endif + }, + { + .sc_cmd = "date", + .sc_cmd_func = shell_os_date_cmd, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &date_help, +#endif + }, + { NULL, NULL, NULL }, +}; + +void +shell_os_register(shell_register_function_t register_func) +{ + register_func(SHELL_OS, os_commands); +} + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98eebe05/sys/shell/src/shell_priv.h ---------------------------------------------------------------------- diff --git a/sys/shell/src/shell_priv.h b/sys/shell/src/shell_priv.h new file mode 100644 index 0000000..fbb44c2 --- /dev/null +++ b/sys/shell/src/shell_priv.h @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef __SHELL_PRIV_H_ +#define __SHELL_PRIV_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "shell/shell.h" + +void shell_os_register(shell_register_function_t register_func); +void shell_prompt_register(shell_register_function_t register_func); + +#ifdef __cplusplus +} +#endif + +#endif /* __SHELL_PRIV_H_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98eebe05/sys/shell/src/shell_prompt.c ---------------------------------------------------------------------- diff --git a/sys/shell/src/shell_prompt.c b/sys/shell/src/shell_prompt.c new file mode 100644 index 0000000..4eaa6b1 --- /dev/null +++ b/sys/shell/src/shell_prompt.c @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <stdio.h> +#include <string.h> + +#include "syscfg/syscfg.h" +#include "os/os.h" +#include "console/console.h" + +#include "shell/shell.h" +#include "shell_priv.h" + +#define PROMPT_LEN 15 +#define SHELL_PROMPT "prompt" + +static const char * +ticks_prompt(void) +{ + static char str[PROMPT_LEN]; + snprintf(str, sizeof(str), "%lu", (unsigned long)os_time_get()); + strcat(str, "> "); + return str; +} + +/** + * Handles the 'ticks' command + */ +int +shell_ticks_cmd(int argc, char **argv) +{ + if (argc > 1) { + if (!strcmp(argv[1], "on")) { + shell_register_prompt_handler(ticks_prompt); + console_printf(" Console Ticks on\n"); + } + else if (!strcmp(argv[1],"off")) { + console_printf(" Console Ticks off\n"); + shell_register_prompt_handler(NULL); + } + return 0; + } + console_printf(" Usage: ticks [on|off]\n"); + return 0; +} + +#if MYNEWT_VAL(SHELL_CMD_HELP) +static const struct shell_param ticks_params[] = { + {"on", "turn on"}, + {"off", "turn on"}, + { NULL, NULL} +}; + +static const struct shell_cmd_help ticks_help = { + .summary = "shell ticks command", + .usage = "usage: ticks [on|off]", + .params = ticks_params, +}; +#endif + +static const struct shell_cmd prompt_commands[] = { + { + .sc_cmd = "ticks", + .sc_cmd_func = shell_ticks_cmd, +#if MYNEWT_VAL(SHELL_CMD_HELP) + .help = &ticks_help, +#endif + }, + { NULL, NULL, NULL }, +}; + + +void +shell_prompt_register(shell_register_function_t register_func) +{ + register_func(SHELL_PROMPT, prompt_commands); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98eebe05/sys/shell/syscfg.yml ---------------------------------------------------------------------- diff --git a/sys/shell/syscfg.yml b/sys/shell/syscfg.yml index 4b841fe..b3092f5 100644 --- a/sys/shell/syscfg.yml +++ b/sys/shell/syscfg.yml @@ -41,3 +41,10 @@ syscfg.defs: SHELL_MAX_COMPAT_COMMANDS: description: 'Max number of compatibility commands' value: 10 + + SHELL_OS_MODULE: + description: 'Include shell os module' + value: 1 + SHELL_PROMPT_MODULE: + description: 'Include shell prompt module' + value: 0