Allow TABS in config
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.cpp
CommitLineData
58baeec1
MM
1/*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
cd011f58
AW
6*/
7
8
0325af12
AW
9#include "libs/Kernel.h"
10#include "SimpleShell.h"
11#include "libs/nuts_bolts.h"
12#include "libs/utils.h"
423df6df 13#include "libs/SerialMessage.h"
838b33b4 14#include "libs/StreamOutput.h"
3fceb8eb 15#include "modules/robot/Conveyor.h"
172d42d9 16#include "DirHandle.h"
0f0b1656 17#include "mri.h"
582559c6 18#include "version.h"
8293d443 19#include "PublicDataRequest.h"
618c9b0f 20#include "FileStream.h"
61134a65
JM
21#include "checksumm.h"
22#include "PublicData.h"
23#include "Gcode.h"
24
25#include "SimpleShell.h"
0325af12 26
47339e4a 27#include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
5647f709 28#include "modules/robot/RobotPublicAccess.h"
d4ee6ee2 29#include "NetworkPublicAccess.h"
a200fc31 30#include "platform_memory.h"
47339e4a 31
61134a65
JM
32#include "system_LPC17xx.h"
33#include "LPC17xx.h"
34
6187a020
JM
35extern unsigned int g_maximumHeapAddress;
36
ecc610a4
JM
37#include <malloc.h>
38#include <mri.h>
39#include <stdio.h>
40#include <stdint.h>
41
42extern "C" uint32_t __end__;
43extern "C" uint32_t __malloc_free_list;
44extern "C" uint32_t _sbrk(int size);
45
9e403697
JM
46#define get_temp_command_checksum CHECKSUM("temp")
47#define get_pos_command_checksum CHECKSUM("pos")
48
49// command lookup table
50SimpleShell::ptentry_t SimpleShell::commands_table[] = {
51 {CHECKSUM("ls"), &SimpleShell::ls_command},
52 {CHECKSUM("cd"), &SimpleShell::cd_command},
53 {CHECKSUM("pwd"), &SimpleShell::pwd_command},
54 {CHECKSUM("cat"), &SimpleShell::cat_command},
55 {CHECKSUM("rm"), &SimpleShell::rm_command},
56 {CHECKSUM("reset"), &SimpleShell::reset_command},
57 {CHECKSUM("dfu"), &SimpleShell::dfu_command},
58 {CHECKSUM("break"), &SimpleShell::break_command},
59 {CHECKSUM("help"), &SimpleShell::help_command},
d4ee6ee2 60 {CHECKSUM("?"), &SimpleShell::help_command},
9e403697
JM
61 {CHECKSUM("version"), &SimpleShell::version_command},
62 {CHECKSUM("mem"), &SimpleShell::mem_command},
63 {CHECKSUM("get"), &SimpleShell::get_command},
64 {CHECKSUM("set_temp"), &SimpleShell::set_temp_command},
d4ee6ee2 65 {CHECKSUM("net"), &SimpleShell::net_command},
618c9b0f
JM
66 {CHECKSUM("load"), &SimpleShell::load_command},
67 {CHECKSUM("save"), &SimpleShell::save_command},
9e403697
JM
68
69 // unknown command
70 {0, NULL}
71};
ecc610a4
JM
72
73// Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
0c683b26 74static uint32_t heapWalk(StreamOutput *stream, bool verbose)
ecc610a4
JM
75{
76 uint32_t chunkNumber = 1;
77 // The __end__ linker symbol points to the beginning of the heap.
78 uint32_t chunkCurr = (uint32_t)&__end__;
79 // __malloc_free_list is the head pointer to newlib-nano's link list of free chunks.
80 uint32_t freeCurr = __malloc_free_list;
81 // Calling _sbrk() with 0 reserves no more memory but it returns the current top of heap.
82 uint32_t heapEnd = _sbrk(0);
83 // accumulate totals
9e403697
JM
84 uint32_t freeSize = 0;
85 uint32_t usedSize = 0;
ecc610a4
JM
86
87 stream->printf("Used Heap Size: %lu\n", heapEnd - chunkCurr);
88
89 // Walk through the chunks until we hit the end of the heap.
9e403697 90 while (chunkCurr < heapEnd) {
ecc610a4
JM
91 // Assume the chunk is in use. Will update later.
92 int isChunkFree = 0;
93 // The first 32-bit word in a chunk is the size of the allocation. newlib-nano over allocates by 8 bytes.
94 // 4 bytes for this 32-bit chunk size and another 4 bytes to allow for 8 byte-alignment of returned pointer.
9e403697 95 uint32_t chunkSize = *(uint32_t *)chunkCurr;
ecc610a4
JM
96 // The start of the next chunk is right after the end of this one.
97 uint32_t chunkNext = chunkCurr + chunkSize;
98
99 // The free list is sorted by address.
100 // Check to see if we have found the next free chunk in the heap.
9e403697 101 if (chunkCurr == freeCurr) {
ecc610a4
JM
102 // Chunk is free so flag it as such.
103 isChunkFree = 1;
104 // The second 32-bit word in a free chunk is a pointer to the next free chunk (again sorted by address).
9e403697 105 freeCurr = *(uint32_t *)(freeCurr + 4);
ecc610a4
JM
106 }
107
108 // Skip past the 32-bit size field in the chunk header.
109 chunkCurr += 4;
110 // 8-byte align the data pointer.
111 chunkCurr = (chunkCurr + 7) & ~7;
112 // newlib-nano over allocates by 8 bytes, 4 bytes for the 32-bit chunk size and another 4 bytes to allow for 8
113 // byte-alignment of the returned pointer.
114 chunkSize -= 8;
9e403697 115 if (verbose)
ecc610a4
JM
116 stream->printf(" Chunk: %lu Address: 0x%08lX Size: %lu %s\n", chunkNumber, chunkCurr, chunkSize, isChunkFree ? "CHUNK FREE" : "");
117
9e403697 118 if (isChunkFree) freeSize += chunkSize;
ecc610a4
JM
119 else usedSize += chunkSize;
120
121 chunkCurr = chunkNext;
122 chunkNumber++;
123 }
124 stream->printf("Allocated: %lu, Free: %lu\r\n", usedSize, freeSize);
0c683b26 125 return freeSize;
ecc610a4
JM
126}
127
128
9e403697
JM
129void SimpleShell::on_module_loaded()
130{
0325af12 131 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
d4ee6ee2
JM
132 this->register_for_event(ON_GCODE_RECEIVED);
133 this->register_for_event(ON_SECOND_TICK);
c4e56997 134
d4ee6ee2 135 this->reset_delay_secs = 0;
ead17727
JM
136}
137
9e403697
JM
138void SimpleShell::on_second_tick(void *)
139{
ead17727 140 // we are timing out for the reset
5895ead3 141 if (this->reset_delay_secs > 0) {
9e403697 142 if (--this->reset_delay_secs == 0) {
ead17727
JM
143 system_reset(false);
144 }
145 }
0325af12
AW
146}
147
9e403697
JM
148void SimpleShell::on_gcode_received(void *argument)
149{
150 Gcode *gcode = static_cast<Gcode *>(argument);
3a238fdc 151 string args= get_arguments(gcode->command);
c4e56997
JM
152
153 if (gcode->has_m) {
154 if (gcode->m == 20) { // list sd card
155 gcode->mark_as_taken();
156 gcode->stream->printf("Begin file list\r\n");
157 ls_command("/sd", gcode->stream);
158 gcode->stream->printf("End file list\r\n");
d4ee6ee2
JM
159
160 } else if (gcode->m == 30) { // remove file
3a238fdc
CG
161 gcode->mark_as_taken();
162 rm_command("/sd/" + args, gcode->stream);
618c9b0f
JM
163
164 }else if(gcode->m == 501) { // load config override
165 gcode->mark_as_taken();
166 if(args.empty()) {
167 load_command("/sd/config-override", gcode->stream);
168 }else{
169 load_command("/sd/config-override." + args, gcode->stream);
170 }
171
172 }else if(gcode->m == 504) { // save to specific config override file
173 gcode->mark_as_taken();
174 if(args.empty()) {
175 save_command("/sd/config-override", gcode->stream);
176 }else{
177 save_command("/sd/config-override." + args, gcode->stream);
178 }
3a238fdc 179 }
c4e56997
JM
180 }
181}
182
9e403697
JM
183bool SimpleShell::parse_command(unsigned short cs, string args, StreamOutput *stream)
184{
185 for (ptentry_t *p = commands_table; p->pfunc != NULL; ++p) {
186 if (cs == p->command_cs) {
187 PFUNC fnc= p->pfunc;
188 (this->*fnc)(args, stream);
189 return true;
190 }
191 }
192
193 return false;
194}
195
0325af12 196// When a new line is received, check if it is a command, and if it is, act upon it
9e403697
JM
197void SimpleShell::on_console_line_received( void *argument )
198{
199 SerialMessage new_message = *static_cast<SerialMessage *>(argument);
7f613782
JM
200
201 // ignore comments
9e403697 202 if (new_message.message[0] == ';') return;
7f613782 203
b6c86164 204 string possible_command = new_message.message;
0325af12 205
3add9a23
AW
206 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
207
9e403697 208 unsigned short check_sum = get_checksum( possible_command.substr(0, possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
6187a020 209
9e403697
JM
210 // find command and execute it
211 parse_command(check_sum, get_arguments(possible_command), new_message.stream);
0325af12
AW
212}
213
0325af12
AW
214// Act upon an ls command
215// Convert the first parameter into an absolute path, then list the files in that path
9e403697
JM
216void SimpleShell::ls_command( string parameters, StreamOutput *stream )
217{
75f4581c 218 string folder = absolute_from_relative( parameters );
9e403697
JM
219 DIR *d;
220 struct dirent *p;
0325af12 221 d = opendir(folder.c_str());
9e403697
JM
222 if (d != NULL) {
223 while ((p = readdir(d)) != NULL) {
224 stream->printf("%s\r\n", lc(string(p->d_name)).c_str());
225 }
ed7c5844 226 closedir(d);
0325af12 227 } else {
b6c86164 228 stream->printf("Could not open directory %s \r\n", folder.c_str());
0325af12
AW
229 }
230}
231
9e403697
JM
232// Delete a file
233void SimpleShell::rm_command( string parameters, StreamOutput *stream )
234{
75f4581c 235 const char *fn= absolute_from_relative(shift_parameter( parameters )).c_str();
9e403697
JM
236 int s = remove(fn);
237 if (s != 0) stream->printf("Could not delete %s \r\n", fn);
238}
239
0325af12 240// Change current absolute path to provided path
9e403697
JM
241void SimpleShell::cd_command( string parameters, StreamOutput *stream )
242{
75f4581c 243 string folder = absolute_from_relative( parameters );
6bcd4886 244
0325af12 245 DIR *d;
0325af12 246 d = opendir(folder.c_str());
9e403697 247 if (d == NULL) {
58baeec1 248 stream->printf("Could not open directory %s \r\n", folder.c_str() );
9e403697 249 } else {
75f4581c 250 THEKERNEL->current_path = folder;
ed7c5844 251 closedir(d);
0325af12
AW
252 }
253}
254
b7250484 255// Responds with the present working directory
9e403697
JM
256void SimpleShell::pwd_command( string parameters, StreamOutput *stream )
257{
75f4581c 258 stream->printf("%s\r\n", THEKERNEL->current_path.c_str());
b7250484
L
259}
260
0325af12 261// Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
9e403697
JM
262void SimpleShell::cat_command( string parameters, StreamOutput *stream )
263{
58baeec1 264 // Get parameters ( filename and line limit )
75f4581c 265 string filename = absolute_from_relative(shift_parameter( parameters ));
0325af12
AW
266 string limit_paramater = shift_parameter( parameters );
267 int limit = -1;
9e403697
JM
268 if ( limit_paramater != "" ) {
269 char *e = NULL;
f7e6f459
MM
270 limit = strtol(limit_paramater.c_str(), &e, 10);
271 if (e <= limit_paramater.c_str())
272 limit = -1;
273 }
58baeec1
MM
274
275 // Open file
0325af12 276 FILE *lp = fopen(filename.c_str(), "r");
9e403697 277 if (lp == NULL) {
58baeec1
MM
278 stream->printf("File not found: %s\r\n", filename.c_str());
279 return;
9ed670c5 280 }
0325af12
AW
281 string buffer;
282 int c;
58baeec1 283 int newlines = 0;
dfb15d68 284 int linecnt= 0;
0325af12 285 // Print each line of the file
9e403697 286 while ((c = fgetc (lp)) != EOF) {
58baeec1 287 buffer.append((char *)&c, 1);
dfb15d68 288 if ( char(c) == '\n' || ++linecnt > 80) {
58baeec1 289 newlines++;
d728799b 290 stream->puts(buffer.c_str());
58baeec1 291 buffer.clear();
dfb15d68 292 if(linecnt > 80) linecnt= 0;
68b7afb4 293 }
9e403697
JM
294 if ( newlines == limit ) {
295 break;
296 }
58baeec1 297 };
0325af12
AW
298 fclose(lp);
299
300}
301
618c9b0f
JM
302// loads the specified config-override file
303void SimpleShell::load_command( string parameters, StreamOutput *stream )
304{
305 // Get parameters ( filename )
75f4581c 306 string filename = absolute_from_relative(parameters);
618c9b0f
JM
307 if(filename == "/") {
308 filename = THEKERNEL->config_override_filename();
309 }
310
311 FILE *fp= fopen(filename.c_str(), "r");
312 if(fp != NULL) {
313 char buf[132];
314 stream->printf("Loading config override file: %s...\n", filename.c_str());
315 while(fgets(buf, sizeof buf, fp) != NULL) {
316 stream->printf(" %s", buf);
317 if(buf[0] == ';') continue; // skip the comments
318 struct SerialMessage message= {&(StreamOutput::NullStream), buf};
319 THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
320 }
321 stream->printf("config override file executed\n");
322 fclose(fp);
323
324 }else{
325 stream->printf("File not found: %s\n", filename.c_str());
326 }
327}
328
329// saves the specified config-override file
330void SimpleShell::save_command( string parameters, StreamOutput *stream )
331{
332 // Get parameters ( filename )
75f4581c 333 string filename = absolute_from_relative(parameters);
618c9b0f
JM
334 if(filename == "/") {
335 filename = THEKERNEL->config_override_filename();
336 }
337
338 // replace stream with one that writes to config-override file
339 FileStream *gs = new FileStream(filename.c_str());
340 if(!gs->is_open()) {
341 stream->printf("Unable to open File %s for write\n", filename.c_str());
342 return;
343 }
344
345 // issue a M500 which will store values in the file stream
346 Gcode *gcode = new Gcode("M500", gs);
347 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
348 delete gs;
349 delete gcode;
350
351 stream->printf("Settings Stored to %s\r\n", filename.c_str());
352}
353
6187a020 354// show free memory
9e403697
JM
355void SimpleShell::mem_command( string parameters, StreamOutput *stream)
356{
357 bool verbose = shift_parameter( parameters ).find_first_of("Vv") != string::npos ;
358 unsigned long heap = (unsigned long)_sbrk(0);
359 unsigned long m = g_maximumHeapAddress - heap;
ecc610a4
JM
360 stream->printf("Unused Heap: %lu bytes\r\n", m);
361
0c683b26
JM
362 uint32_t f= heapWalk(stream, verbose);
363 stream->printf("Total Free RAM: %lu bytes\r\n", m + f);
a200fc31 364
0c683b26 365 stream->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0.free(), AHB1.free());
1803076a
MM
366 if (verbose)
367 {
368 AHB0.debug(stream);
369 AHB1.debug(stream);
370 }
6187a020
JM
371}
372
9e403697
JM
373static uint32_t getDeviceType()
374{
375#define IAP_LOCATION 0x1FFF1FF1
01f35bcc
JM
376 uint32_t command[1];
377 uint32_t result[5];
9e403697 378 typedef void (*IAP)(uint32_t *, uint32_t *);
01f35bcc
JM
379 IAP iap = (IAP) IAP_LOCATION;
380
381 __disable_irq();
382
383 command[0] = 54;
384 iap(command, result);
385
386 __enable_irq();
387
388 return result[1];
389}
390
d4ee6ee2
JM
391// get network config
392void SimpleShell::net_command( string parameters, StreamOutput *stream)
393{
394 void *returned_data;
395 bool ok= THEKERNEL->public_data->get_value( network_checksum, get_ipconfig_checksum, &returned_data );
396 if(ok) {
397 char *str= (char *)returned_data;
398 stream->printf("%s\r\n", str);
399 free(str);
400
401 }else{
402 stream->printf("No network detected\n");
403 }
404}
405
582559c6 406// print out build version
9e403697
JM
407void SimpleShell::version_command( string parameters, StreamOutput *stream)
408{
582559c6 409 Version vers;
9e403697
JM
410 uint32_t dev = getDeviceType();
411 const char *mcu = (dev & 0x00100000) ? "LPC1769" : "LPC1768";
01f35bcc 412 stream->printf("Build version: %s, Build date: %s, MCU: %s, System Clock: %ldMHz\r\n", vers.get_build(), vers.get_build_date(), mcu, SystemCoreClock / 1000000);
582559c6
JM
413}
414
77983aa1 415// Reset the system
9e403697
JM
416void SimpleShell::reset_command( string parameters, StreamOutput *stream)
417{
ead17727 418 stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
9e403697 419 this->reset_delay_secs = 5; // reboot in 5 seconds
2742fca9
JM
420}
421
422// go into dfu boot mode
9e403697
JM
423void SimpleShell::dfu_command( string parameters, StreamOutput *stream)
424{
ed7c5844
JM
425 stream->printf("Entering boot mode...\r\n");
426 system_reset(true);
77983aa1
L
427}
428
0f0b1656 429// Break out into the MRI debugging system
9e403697
JM
430void SimpleShell::break_command( string parameters, StreamOutput *stream)
431{
0f0b1656
L
432 stream->printf("Entering MRI debug mode...\r\n");
433 __debugbreak();
434}
435
8293d443 436// used to test out the get public data events
9e403697
JM
437void SimpleShell::get_command( string parameters, StreamOutput *stream)
438{
439 int what = get_checksum(shift_parameter( parameters ));
b55cfff1 440 void *returned_data;
c4e56997 441
9e403697
JM
442 if (what == get_temp_command_checksum) {
443 string type = shift_parameter( parameters );
314ab8f7 444 bool ok = THEKERNEL->public_data->get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data );
b55cfff1 445
9e403697
JM
446 if (ok) {
447 struct pad_temperature temp = *static_cast<struct pad_temperature *>(returned_data);
b55cfff1 448 stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
9e403697 449 } else {
b55cfff1
JM
450 stream->printf("%s is not a known temperature device\r\n", type.c_str());
451 }
c4e56997 452
9e403697 453 } else if (what == get_pos_command_checksum) {
314ab8f7 454 bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
b55cfff1 455
9e403697 456 if (ok) {
1ad23cd3 457 float *pos = static_cast<float *>(returned_data);
b55cfff1 458 stream->printf("Position X: %f, Y: %f, Z: %f\r\n", pos[0], pos[1], pos[2]);
c4e56997 459
9e403697 460 } else {
b55cfff1
JM
461 stream->printf("get pos command failed\r\n");
462 }
463 }
8293d443
JM
464}
465
77047e76 466// used to test out the get public data events
9e403697
JM
467void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
468{
469 string type = shift_parameter( parameters );
470 string temp = shift_parameter( parameters );
04211969 471 float t = temp.empty() ? 0.0 : strtof(temp.c_str(), NULL);
314ab8f7 472 bool ok = THEKERNEL->public_data->set_value( temperature_control_checksum, get_checksum(type), &t );
991d98cc 473
9e403697 474 if (ok) {
991d98cc 475 stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
9e403697 476 } else {
991d98cc
JM
477 stream->printf("%s is not a known temperature device\r\n", type.c_str());
478 }
77047e76
JM
479}
480
9e403697
JM
481void SimpleShell::help_command( string parameters, StreamOutput *stream )
482{
ed7c5844 483 stream->printf("Commands:\r\n");
582559c6 484 stream->printf("version\r\n");
ecc610a4 485 stream->printf("mem [-v]\r\n");
ed7c5844
JM
486 stream->printf("ls [folder]\r\n");
487 stream->printf("cd folder\r\n");
c4e56997 488 stream->printf("pwd\r\n");
ed7c5844 489 stream->printf("cat file [limit]\r\n");
9e403697 490 stream->printf("rm file\r\n");
4eb0e279 491 stream->printf("play file [-v]\r\n");
ed7c5844
JM
492 stream->printf("progress - shows progress of current play\r\n");
493 stream->printf("abort - abort currently playing file\r\n");
c4e56997
JM
494 stream->printf("reset - reset smoothie\r\n");
495 stream->printf("dfu - enter dfu boot loader\r\n");
496 stream->printf("break - break into debugger\r\n");
ed7c5844
JM
497 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
498 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
499 stream->printf("config-load [<file_name>]\r\n");
5647f709 500 stream->printf("get temp [bed|hotend]\r\n");
991d98cc 501 stream->printf("set_temp bed|hotend 185\r\n");
b55cfff1 502 stream->printf("get pos\r\n");
d4ee6ee2 503 stream->printf("net\r\n");
618c9b0f
JM
504 stream->printf("load [file] - loads a configuration override file from soecified name or config-override\r\n");
505 stream->printf("save [file] - saves a configuration override file as specified filename or as config-override\r\n");
235a7435
JM
506}
507