Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
Diff: src/CommandParser/cmd.cpp
- Revision:
- 0:65cfa4873284
- Child:
- 2:da28f21b72a1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/CommandParser/cmd.cpp Thu Sep 01 18:57:04 2016 +0000 @@ -0,0 +1,275 @@ +/* + * =============================================================== + * Natural Tiny Shell (NT-Shell) Application example. + * Version 0.0.6 + * =============================================================== + * Copyright (c) 2010-2011 Shinichiro Nakamura + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * =============================================================== + */ + +#include "cmd.h" +#include <mbed.h> +#include "ntshell.h" +#include "ntopt.h" +#include "global.h" +#include "ConfigurationHandler.h" + +Serial serial(USBTX, USBRX); +ntshell_t ntshell; + +typedef struct { + char *command; // command (from shell) + char *description; // descrption + void (*func)(int argc, char **argv); // callback function +} command_table_t; + +// see cmd.h +const command_table_t cmdlist[] = { + {"?", "help command", cmd_help }, + {"create", "create a control", cmd_create }, + {"destroy", "destroy a control", cmd_destroy }, + {"heap", "show heap statistics", cmd_heap }, + {"help", "help command", cmd_help }, + {"modify", "modify a control", cmd_modify }, + {"reset", "reset the controller", cmd_reset }, + {"showControls", "display active controls", cmd_ShowControls }, + {"stress", "test utility to stress control creation/deletion", cmd_stress }, + {NULL, NULL, NULL} +}; + +int func_read(char *buf, int cnt); +int func_write(const char *buf, int cnt); +int func_cb_ntshell(const char *text); +void func_cb_ntopt(int argc, char **argv); + +// stress test private functions +static void create ( const char *controlFile, Control_t controlType ); +static void destroy ( const char *controlFile ); + +/** + * Serial read function. + */ +int func_read(char *buf, int cnt) +{ + for (int i = 0; i < cnt; i++) { + buf[i] = serial.getc(); + } + return 0; +} + +/** + * Serial write function. + */ +int func_write(const char *buf, int cnt) +{ + for (int i = 0; i < cnt; i++) { + serial.putc(buf[i]); + } + return 0; +} + +/** + * Callback function for ntshell module. + */ +int func_cb_ntshell(const char *text) +{ + return ntopt_parse(text, func_cb_ntopt); +} + +/** + * Callback function for ntopt module. + */ +void func_cb_ntopt(int argc, char **argv) +{ + if (argc == 0) { + return; + } + int execnt = 0; + const command_table_t *p = &cmdlist[0]; + while (p->command != NULL) { + if (strcmp(argv[0], p->command) == 0) { + p->func(argc, argv); + execnt++; + } + p++; + } + if (execnt == 0) { + printf("Command not found.\r\n"); + } + wait_ms(250); +} + +/************************* callback functions *******************************/ +void cmd_help(int argc, char **argv) +{ + UNUSED(argc); + UNUSED(argv); + + const command_table_t *tblPtr = cmdlist; + + while (tblPtr->command) { + printf("\r%-32s:\t%s\n", tblPtr->command, tblPtr->description); + tblPtr++; + } + printf("\r\n"); +} + + +void cmd_ShowControls(int argc, char **argv) +{ + UNUSED(argc); + UNUSED(argv); + ConfigurationHandler_DisplayThreads(); +} + +void cmd_reset(int argc, char **argv) +{ + UNUSED(argc); + UNUSED(argv); + NVIC_SystemReset(); +} + +void cmd_create(int argc, char **argv) +{ + if ( argc != 3 ) { + printf("\r\nusage: create [controlName] [controlType]\n"); + printf("\rcontrolType-> 0=timer, 1=PID, 2=setpoint, 3=composite, 4=manual\n"); + return; + } + // send a message to the configuration handler to create the control + Message_t *msg = MailBox.alloc(); + memset(msg, 0, sizeof(Message_t)); + msg->action = ACTION_CREATE; + msg->control = (Control_t) atoi(argv[2]); + strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1); + + printf("%s: Sending a create request for control %s type = %u\r\n", + __func__, msg->controlFile, msg->control); + + MailBox.put(msg); + return; +} + +void cmd_destroy(int argc, char **argv) +{ + if ( argc != 2 ) { + printf("\r\nusage: destroy [threadName]\r\n"); + return; + } + + // send a message to the configuration handler to destroy the control + Message_t *msg = MailBox.alloc(); + memset(msg, 0, sizeof(Message_t)); + msg->action = ACTION_DESTROY; + strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1); + + printf("%s: Sending a destroy request for control %s\r\n", + __func__, msg->controlFile); + + MailBox.put(msg); + return; +} + +void cmd_heap(int argc, char **argv) +{ + UNUSED(argc), UNUSED(argv); + __heapstats((__heapprt)fprintf,stderr); // print initial free heap size +} + +void cmd_modify(int argc, char **argv) +{ + // stubbed + printf("\nNo-op\r\n"); + return; +} + + +void cmd_stress(int argc, char **argv) +{ + UNUSED(argc), UNUSED(argv); + static int counter = 0; + const unsigned int delay = 1.0; + + // let's just run forever to see what happens + while ( true ) { + wait(delay); + create("TimerControl00.json", CONTROL_TIMER); + wait(delay); + create("PIDControl00.json", CONTROL_PID); + wait(delay); + create("SetpointControl00.json", CONTROL_SETPOINT); + wait(delay); + create("CompositeControl00.json", CONTROL_COMPOSITE); + wait(delay); + create("ManualControl00.json", CONTROL_MANUAL); + wait(delay); + + cmd_heap(0, 0); + + wait(2.5); + + // now delete everything + destroy("TimerControl00.json"); + wait(delay); + destroy("PIDControl00.json"); + wait(delay); + destroy("SetpointControl00.json"); + wait(delay); + destroy("CompositeControl00.json"); + wait(delay); + destroy("ManualControl00.json"); + wait(delay); + printf("\rInterval => %u\n", ++counter); + cmd_heap(0, 0); + wait(2.5); + } +} + +static void create(const char *controlFile, Control_t controlType) +{ + Message_t *msg = MailBox.alloc(); + memset(msg, 0, sizeof(Message_t)); + msg->action = ACTION_CREATE; + msg->control = controlType; + strncpy(msg->controlFile, controlFile, sizeof(msg->controlFile)-1); + + printf("\r%s: Sending a create request for control %s\r\n", + __func__, msg->controlFile); + + MailBox.put(msg); +} + +static void destroy(const char *controlFile) +{ + Message_t *msg = MailBox.alloc(); + memset(msg, 0, sizeof(Message_t)); + msg->action = ACTION_DESTROY; + strncpy(msg->controlFile, controlFile, sizeof(msg->controlFile)-1); + + printf("\r%s: Sending a destroy request for control %s\r\n", + __func__, msg->controlFile); + + MailBox.put(msg); +} +