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
src/CommandParser/cmd.cpp@14:cc916fa8dd11, 2016-09-07 (annotated)
- Committer:
- jmarkel44
- Date:
- Wed Sep 07 14:51:00 2016 +0000
- Revision:
- 14:cc916fa8dd11
- Parent:
- 13:c80c283f9db2
- Child:
- 17:dfa2c53b3f38
- Child:
- 18:9cf694a764c0
added "ls" and "rm" commands;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:65cfa4873284 | 1 | /* |
jmarkel44 | 0:65cfa4873284 | 2 | * =============================================================== |
jmarkel44 | 0:65cfa4873284 | 3 | * Natural Tiny Shell (NT-Shell) Application example. |
jmarkel44 | 0:65cfa4873284 | 4 | * Version 0.0.6 |
jmarkel44 | 0:65cfa4873284 | 5 | * =============================================================== |
jmarkel44 | 0:65cfa4873284 | 6 | * Copyright (c) 2010-2011 Shinichiro Nakamura |
jmarkel44 | 0:65cfa4873284 | 7 | * |
jmarkel44 | 0:65cfa4873284 | 8 | * Permission is hereby granted, free of charge, to any person |
jmarkel44 | 0:65cfa4873284 | 9 | * obtaining a copy of this software and associated documentation |
jmarkel44 | 0:65cfa4873284 | 10 | * files (the "Software"), to deal in the Software without |
jmarkel44 | 0:65cfa4873284 | 11 | * restriction, including without limitation the rights to use, |
jmarkel44 | 0:65cfa4873284 | 12 | * copy, modify, merge, publish, distribute, sublicense, and/or |
jmarkel44 | 0:65cfa4873284 | 13 | * sell copies of the Software, and to permit persons to whom the |
jmarkel44 | 0:65cfa4873284 | 14 | * Software is furnished to do so, subject to the following |
jmarkel44 | 0:65cfa4873284 | 15 | * conditions: |
jmarkel44 | 0:65cfa4873284 | 16 | * |
jmarkel44 | 0:65cfa4873284 | 17 | * The above copyright notice and this permission notice shall be |
jmarkel44 | 0:65cfa4873284 | 18 | * included in all copies or substantial portions of the Software. |
jmarkel44 | 0:65cfa4873284 | 19 | * |
jmarkel44 | 0:65cfa4873284 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
jmarkel44 | 0:65cfa4873284 | 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
jmarkel44 | 0:65cfa4873284 | 22 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jmarkel44 | 0:65cfa4873284 | 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
jmarkel44 | 0:65cfa4873284 | 24 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
jmarkel44 | 0:65cfa4873284 | 25 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
jmarkel44 | 0:65cfa4873284 | 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
jmarkel44 | 0:65cfa4873284 | 27 | * OTHER DEALINGS IN THE SOFTWARE. |
jmarkel44 | 0:65cfa4873284 | 28 | * =============================================================== |
jmarkel44 | 0:65cfa4873284 | 29 | */ |
jmarkel44 | 0:65cfa4873284 | 30 | |
jmarkel44 | 0:65cfa4873284 | 31 | #include "cmd.h" |
jmarkel44 | 0:65cfa4873284 | 32 | #include <mbed.h> |
jmarkel44 | 0:65cfa4873284 | 33 | #include "ntshell.h" |
jmarkel44 | 0:65cfa4873284 | 34 | #include "ntopt.h" |
jmarkel44 | 0:65cfa4873284 | 35 | #include "global.h" |
jmarkel44 | 0:65cfa4873284 | 36 | #include "ConfigurationHandler.h" |
jmarkel44 | 3:8ea4db957749 | 37 | #include "mDot.h" |
jmarkel44 | 0:65cfa4873284 | 38 | |
jmarkel44 | 0:65cfa4873284 | 39 | Serial serial(USBTX, USBRX); |
jmarkel44 | 0:65cfa4873284 | 40 | ntshell_t ntshell; |
jmarkel44 | 0:65cfa4873284 | 41 | |
jmarkel44 | 14:cc916fa8dd11 | 42 | extern mDot *GLOBAL_mdot; |
jmarkel44 | 14:cc916fa8dd11 | 43 | |
jmarkel44 | 0:65cfa4873284 | 44 | typedef struct { |
jmarkel44 | 0:65cfa4873284 | 45 | char *command; // command (from shell) |
jmarkel44 | 0:65cfa4873284 | 46 | char *description; // descrption |
jmarkel44 | 0:65cfa4873284 | 47 | void (*func)(int argc, char **argv); // callback function |
jmarkel44 | 0:65cfa4873284 | 48 | } command_table_t; |
jmarkel44 | 0:65cfa4873284 | 49 | |
jmarkel44 | 0:65cfa4873284 | 50 | // see cmd.h |
jmarkel44 | 0:65cfa4873284 | 51 | const command_table_t cmdlist[] = { |
jmarkel44 | 0:65cfa4873284 | 52 | {"?", "help command", cmd_help }, |
jmarkel44 | 0:65cfa4873284 | 53 | {"create", "create a control", cmd_create }, |
jmarkel44 | 0:65cfa4873284 | 54 | {"destroy", "destroy a control", cmd_destroy }, |
jmarkel44 | 0:65cfa4873284 | 55 | {"heap", "show heap statistics", cmd_heap }, |
jmarkel44 | 0:65cfa4873284 | 56 | {"help", "help command", cmd_help }, |
jmarkel44 | 14:cc916fa8dd11 | 57 | {"ls", "list user files", cmd_ls }, |
jmarkel44 | 0:65cfa4873284 | 58 | {"modify", "modify a control", cmd_modify }, |
jmarkel44 | 0:65cfa4873284 | 59 | {"reset", "reset the controller", cmd_reset }, |
jmarkel44 | 14:cc916fa8dd11 | 60 | {"rm", "remove a user file", cmd_rm }, |
jmarkel44 | 0:65cfa4873284 | 61 | {"showControls", "display active controls", cmd_ShowControls }, |
jmarkel44 | 0:65cfa4873284 | 62 | {NULL, NULL, NULL} |
jmarkel44 | 0:65cfa4873284 | 63 | }; |
jmarkel44 | 0:65cfa4873284 | 64 | |
jmarkel44 | 0:65cfa4873284 | 65 | int func_read(char *buf, int cnt); |
jmarkel44 | 0:65cfa4873284 | 66 | int func_write(const char *buf, int cnt); |
jmarkel44 | 0:65cfa4873284 | 67 | int func_cb_ntshell(const char *text); |
jmarkel44 | 0:65cfa4873284 | 68 | void func_cb_ntopt(int argc, char **argv); |
jmarkel44 | 0:65cfa4873284 | 69 | |
jmarkel44 | 0:65cfa4873284 | 70 | /** |
jmarkel44 | 0:65cfa4873284 | 71 | * Serial read function. |
jmarkel44 | 0:65cfa4873284 | 72 | */ |
jmarkel44 | 0:65cfa4873284 | 73 | int func_read(char *buf, int cnt) |
jmarkel44 | 0:65cfa4873284 | 74 | { |
jmarkel44 | 0:65cfa4873284 | 75 | for (int i = 0; i < cnt; i++) { |
jmarkel44 | 0:65cfa4873284 | 76 | buf[i] = serial.getc(); |
jmarkel44 | 0:65cfa4873284 | 77 | } |
jmarkel44 | 0:65cfa4873284 | 78 | return 0; |
jmarkel44 | 0:65cfa4873284 | 79 | } |
jmarkel44 | 0:65cfa4873284 | 80 | |
jmarkel44 | 0:65cfa4873284 | 81 | /** |
jmarkel44 | 0:65cfa4873284 | 82 | * Serial write function. |
jmarkel44 | 0:65cfa4873284 | 83 | */ |
jmarkel44 | 0:65cfa4873284 | 84 | int func_write(const char *buf, int cnt) |
jmarkel44 | 0:65cfa4873284 | 85 | { |
jmarkel44 | 0:65cfa4873284 | 86 | for (int i = 0; i < cnt; i++) { |
jmarkel44 | 0:65cfa4873284 | 87 | serial.putc(buf[i]); |
jmarkel44 | 0:65cfa4873284 | 88 | } |
jmarkel44 | 0:65cfa4873284 | 89 | return 0; |
jmarkel44 | 0:65cfa4873284 | 90 | } |
jmarkel44 | 0:65cfa4873284 | 91 | |
jmarkel44 | 0:65cfa4873284 | 92 | /** |
jmarkel44 | 0:65cfa4873284 | 93 | * Callback function for ntshell module. |
jmarkel44 | 0:65cfa4873284 | 94 | */ |
jmarkel44 | 0:65cfa4873284 | 95 | int func_cb_ntshell(const char *text) |
jmarkel44 | 0:65cfa4873284 | 96 | { |
jmarkel44 | 0:65cfa4873284 | 97 | return ntopt_parse(text, func_cb_ntopt); |
jmarkel44 | 0:65cfa4873284 | 98 | } |
jmarkel44 | 0:65cfa4873284 | 99 | |
jmarkel44 | 0:65cfa4873284 | 100 | /** |
jmarkel44 | 0:65cfa4873284 | 101 | * Callback function for ntopt module. |
jmarkel44 | 0:65cfa4873284 | 102 | */ |
jmarkel44 | 0:65cfa4873284 | 103 | void func_cb_ntopt(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 104 | { |
jmarkel44 | 0:65cfa4873284 | 105 | if (argc == 0) { |
jmarkel44 | 0:65cfa4873284 | 106 | return; |
jmarkel44 | 0:65cfa4873284 | 107 | } |
jmarkel44 | 0:65cfa4873284 | 108 | int execnt = 0; |
jmarkel44 | 0:65cfa4873284 | 109 | const command_table_t *p = &cmdlist[0]; |
jmarkel44 | 0:65cfa4873284 | 110 | while (p->command != NULL) { |
jmarkel44 | 0:65cfa4873284 | 111 | if (strcmp(argv[0], p->command) == 0) { |
jmarkel44 | 0:65cfa4873284 | 112 | p->func(argc, argv); |
jmarkel44 | 0:65cfa4873284 | 113 | execnt++; |
jmarkel44 | 0:65cfa4873284 | 114 | } |
jmarkel44 | 0:65cfa4873284 | 115 | p++; |
jmarkel44 | 0:65cfa4873284 | 116 | } |
jmarkel44 | 0:65cfa4873284 | 117 | if (execnt == 0) { |
jmarkel44 | 0:65cfa4873284 | 118 | printf("Command not found.\r\n"); |
jmarkel44 | 0:65cfa4873284 | 119 | } |
jmarkel44 | 0:65cfa4873284 | 120 | wait_ms(250); |
jmarkel44 | 0:65cfa4873284 | 121 | } |
jmarkel44 | 0:65cfa4873284 | 122 | |
jmarkel44 | 0:65cfa4873284 | 123 | /************************* callback functions *******************************/ |
jmarkel44 | 0:65cfa4873284 | 124 | void cmd_help(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 125 | { |
jmarkel44 | 0:65cfa4873284 | 126 | UNUSED(argc); |
jmarkel44 | 0:65cfa4873284 | 127 | UNUSED(argv); |
jmarkel44 | 0:65cfa4873284 | 128 | |
jmarkel44 | 0:65cfa4873284 | 129 | const command_table_t *tblPtr = cmdlist; |
jmarkel44 | 0:65cfa4873284 | 130 | |
jmarkel44 | 0:65cfa4873284 | 131 | while (tblPtr->command) { |
jmarkel44 | 0:65cfa4873284 | 132 | printf("\r%-32s:\t%s\n", tblPtr->command, tblPtr->description); |
jmarkel44 | 0:65cfa4873284 | 133 | tblPtr++; |
jmarkel44 | 0:65cfa4873284 | 134 | } |
jmarkel44 | 0:65cfa4873284 | 135 | printf("\r\n"); |
jmarkel44 | 0:65cfa4873284 | 136 | } |
jmarkel44 | 0:65cfa4873284 | 137 | |
jmarkel44 | 14:cc916fa8dd11 | 138 | void cmd_ls(int argc, char **argv) |
jmarkel44 | 14:cc916fa8dd11 | 139 | { |
jmarkel44 | 14:cc916fa8dd11 | 140 | vector<mDot::mdot_file> userFiles; |
jmarkel44 | 14:cc916fa8dd11 | 141 | userFiles = GLOBAL_mdot->listUserFiles(); |
jmarkel44 | 14:cc916fa8dd11 | 142 | vector<mDot::mdot_file>::iterator pos; |
jmarkel44 | 14:cc916fa8dd11 | 143 | |
jmarkel44 | 14:cc916fa8dd11 | 144 | for ( pos = userFiles.begin(); pos != userFiles.end(); ++pos ) { |
jmarkel44 | 14:cc916fa8dd11 | 145 | printf("\r %-33s %d\n", pos->name, pos->size); |
jmarkel44 | 14:cc916fa8dd11 | 146 | } |
jmarkel44 | 14:cc916fa8dd11 | 147 | printf("\r\n"); |
jmarkel44 | 14:cc916fa8dd11 | 148 | } |
jmarkel44 | 14:cc916fa8dd11 | 149 | |
jmarkel44 | 0:65cfa4873284 | 150 | |
jmarkel44 | 0:65cfa4873284 | 151 | void cmd_ShowControls(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 152 | { |
jmarkel44 | 0:65cfa4873284 | 153 | UNUSED(argc); |
jmarkel44 | 0:65cfa4873284 | 154 | UNUSED(argv); |
jmarkel44 | 12:ea87887ca7ad | 155 | ConfigurationHandler_showControls(); |
jmarkel44 | 0:65cfa4873284 | 156 | } |
jmarkel44 | 0:65cfa4873284 | 157 | |
jmarkel44 | 0:65cfa4873284 | 158 | void cmd_reset(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 159 | { |
jmarkel44 | 0:65cfa4873284 | 160 | UNUSED(argc); |
jmarkel44 | 0:65cfa4873284 | 161 | UNUSED(argv); |
jmarkel44 | 13:c80c283f9db2 | 162 | GLOBAL_mdot->resetCpu(); |
jmarkel44 | 0:65cfa4873284 | 163 | } |
jmarkel44 | 0:65cfa4873284 | 164 | |
jmarkel44 | 14:cc916fa8dd11 | 165 | void cmd_rm(int argc, char **argv) |
jmarkel44 | 14:cc916fa8dd11 | 166 | { |
jmarkel44 | 14:cc916fa8dd11 | 167 | UNUSED(argc); |
jmarkel44 | 14:cc916fa8dd11 | 168 | UNUSED(argv); |
jmarkel44 | 14:cc916fa8dd11 | 169 | |
jmarkel44 | 14:cc916fa8dd11 | 170 | if ( argc != 2 ) { |
jmarkel44 | 14:cc916fa8dd11 | 171 | printf("\rusage: rm <filename>\n"); |
jmarkel44 | 14:cc916fa8dd11 | 172 | return; |
jmarkel44 | 14:cc916fa8dd11 | 173 | } |
jmarkel44 | 14:cc916fa8dd11 | 174 | // attempt to remove a user file |
jmarkel44 | 14:cc916fa8dd11 | 175 | GLOBAL_mdot->deleteUserFile(argv[1]); |
jmarkel44 | 14:cc916fa8dd11 | 176 | } |
jmarkel44 | 14:cc916fa8dd11 | 177 | |
jmarkel44 | 0:65cfa4873284 | 178 | void cmd_create(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 179 | { |
jmarkel44 | 0:65cfa4873284 | 180 | if ( argc != 3 ) { |
jmarkel44 | 0:65cfa4873284 | 181 | printf("\r\nusage: create [controlName] [controlType]\n"); |
jmarkel44 | 0:65cfa4873284 | 182 | printf("\rcontrolType-> 0=timer, 1=PID, 2=setpoint, 3=composite, 4=manual\n"); |
jmarkel44 | 0:65cfa4873284 | 183 | return; |
jmarkel44 | 0:65cfa4873284 | 184 | } |
jmarkel44 | 0:65cfa4873284 | 185 | // send a message to the configuration handler to create the control |
jmarkel44 | 0:65cfa4873284 | 186 | Message_t *msg = MailBox.alloc(); |
jmarkel44 | 0:65cfa4873284 | 187 | memset(msg, 0, sizeof(Message_t)); |
jmarkel44 | 0:65cfa4873284 | 188 | msg->action = ACTION_CREATE; |
jmarkel44 | 0:65cfa4873284 | 189 | msg->control = (Control_t) atoi(argv[2]); |
jmarkel44 | 0:65cfa4873284 | 190 | strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1); |
jmarkel44 | 0:65cfa4873284 | 191 | |
jmarkel44 | 0:65cfa4873284 | 192 | printf("%s: Sending a create request for control %s type = %u\r\n", |
jmarkel44 | 0:65cfa4873284 | 193 | __func__, msg->controlFile, msg->control); |
jmarkel44 | 0:65cfa4873284 | 194 | |
jmarkel44 | 0:65cfa4873284 | 195 | MailBox.put(msg); |
jmarkel44 | 0:65cfa4873284 | 196 | return; |
jmarkel44 | 0:65cfa4873284 | 197 | } |
jmarkel44 | 0:65cfa4873284 | 198 | |
jmarkel44 | 0:65cfa4873284 | 199 | void cmd_destroy(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 200 | { |
jmarkel44 | 12:ea87887ca7ad | 201 | if ( argc != 3 ) { |
jmarkel44 | 12:ea87887ca7ad | 202 | printf("\r\nusage: destroy [controlName] [controlType]\n"); |
jmarkel44 | 12:ea87887ca7ad | 203 | printf("\rcontrolType-> 0=timer, 1=PID, 2=setpoint, 3=composite, 4=manual\n"); |
jmarkel44 | 0:65cfa4873284 | 204 | return; |
jmarkel44 | 0:65cfa4873284 | 205 | } |
jmarkel44 | 0:65cfa4873284 | 206 | |
jmarkel44 | 0:65cfa4873284 | 207 | // send a message to the configuration handler to destroy the control |
jmarkel44 | 0:65cfa4873284 | 208 | Message_t *msg = MailBox.alloc(); |
jmarkel44 | 0:65cfa4873284 | 209 | memset(msg, 0, sizeof(Message_t)); |
jmarkel44 | 0:65cfa4873284 | 210 | msg->action = ACTION_DESTROY; |
jmarkel44 | 12:ea87887ca7ad | 211 | msg->control = (Control_t) atoi(argv[2]); |
jmarkel44 | 0:65cfa4873284 | 212 | strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1); |
jmarkel44 | 0:65cfa4873284 | 213 | |
jmarkel44 | 0:65cfa4873284 | 214 | printf("%s: Sending a destroy request for control %s\r\n", |
jmarkel44 | 0:65cfa4873284 | 215 | __func__, msg->controlFile); |
jmarkel44 | 0:65cfa4873284 | 216 | |
jmarkel44 | 0:65cfa4873284 | 217 | MailBox.put(msg); |
jmarkel44 | 0:65cfa4873284 | 218 | return; |
jmarkel44 | 0:65cfa4873284 | 219 | } |
jmarkel44 | 0:65cfa4873284 | 220 | |
jmarkel44 | 0:65cfa4873284 | 221 | void cmd_heap(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 222 | { |
jmarkel44 | 0:65cfa4873284 | 223 | UNUSED(argc), UNUSED(argv); |
jmarkel44 | 0:65cfa4873284 | 224 | __heapstats((__heapprt)fprintf,stderr); // print initial free heap size |
jmarkel44 | 0:65cfa4873284 | 225 | } |
jmarkel44 | 0:65cfa4873284 | 226 | |
jmarkel44 | 0:65cfa4873284 | 227 | void cmd_modify(int argc, char **argv) |
jmarkel44 | 0:65cfa4873284 | 228 | { |
jmarkel44 | 0:65cfa4873284 | 229 | // stubbed |
jmarkel44 | 3:8ea4db957749 | 230 | printf("\rNot yet implemented.\n"); |
jmarkel44 | 0:65cfa4873284 | 231 | return; |
jmarkel44 | 0:65cfa4873284 | 232 | } |
jmarkel44 | 0:65cfa4873284 | 233 | |
jmarkel44 | 0:65cfa4873284 | 234 | |
jmarkel44 | 0:65cfa4873284 | 235 | |
jmarkel44 | 0:65cfa4873284 | 236 |