Update to new build system.
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.cpp
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/>.
6 */
7
8
9 #include "libs/Kernel.h"
10 #include "SimpleShell.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/utils.h"
13 #include "libs/SerialMessage.h"
14 #include "libs/StreamOutput.h"
15 #include "modules/robot/Conveyor.h"
16 #include "DirHandle.h"
17 #include "mri.h"
18
19
20 void SimpleShell::on_module_loaded(){
21 this->current_path = "/";
22 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
23 }
24
25 // When a new line is received, check if it is a command, and if it is, act upon it
26 void SimpleShell::on_console_line_received( void* argument ){
27 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
28 string possible_command = new_message.message;
29
30 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
31
32 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
33 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
34
35 // Act depending on command
36 if (check_sum == ls_command_checksum)
37 this->ls_command( get_arguments(possible_command), new_message.stream );
38 else if (check_sum == cd_command_checksum)
39 this->cd_command( get_arguments(possible_command), new_message.stream );
40 else if (check_sum == pwd_command_checksum)
41 this->pwd_command( get_arguments(possible_command), new_message.stream );
42 else if (check_sum == cat_command_checksum)
43 this->cat_command( get_arguments(possible_command), new_message.stream );
44 else if (check_sum == break_command_checksum)
45 this->break_command(get_arguments(possible_command),new_message.stream );
46 else if (check_sum == reset_command_checksum)
47 this->reset_command(get_arguments(possible_command),new_message.stream );
48 else if (check_sum == dfu_command_checksum)
49 this->reset_command(get_arguments(possible_command),new_message.stream );
50 else if (check_sum == help_command_checksum)
51 this->help_command(get_arguments(possible_command),new_message.stream );
52 }
53
54 // Convert a path indication ( absolute or relative ) into a path ( absolute )
55 string SimpleShell::absolute_from_relative( string path ){
56 if( path[0] == '/' ){ return path; }
57 if( path[0] == '.' ){ return this->current_path; }
58 return this->current_path + path;
59 }
60
61 // Act upon an ls command
62 // Convert the first parameter into an absolute path, then list the files in that path
63 void SimpleShell::ls_command( string parameters, StreamOutput* stream ){
64 string folder = this->absolute_from_relative( parameters );
65 DIR* d;
66 struct dirent* p;
67 d = opendir(folder.c_str());
68 if(d != NULL) {
69 while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
70 closedir(d);
71 } else {
72 stream->printf("Could not open directory %s \r\n", folder.c_str());
73 }
74 }
75
76 // Change current absolute path to provided path
77 void SimpleShell::cd_command( string parameters, StreamOutput* stream ){
78 string folder = this->absolute_from_relative( parameters );
79 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
80 DIR *d;
81 d = opendir(folder.c_str());
82 if(d == NULL) {
83 stream->printf("Could not open directory %s \r\n", folder.c_str() );
84 }else{
85 this->current_path = folder;
86 closedir(d);
87 }
88 }
89
90 // Responds with the present working directory
91 void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){
92 stream->printf("%s\r\n", this->current_path.c_str());
93 }
94
95 // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
96 void SimpleShell::cat_command( string parameters, StreamOutput* stream ){
97
98 // Get parameters ( filename and line limit )
99 string filename = this->absolute_from_relative(shift_parameter( parameters ));
100 string limit_paramater = shift_parameter( parameters );
101 int limit = -1;
102 if( limit_paramater != "" )
103 {
104 char* e = NULL;
105 limit = strtol(limit_paramater.c_str(), &e, 10);
106 if (e <= limit_paramater.c_str())
107 limit = -1;
108 }
109
110 // Open file
111 FILE *lp = fopen(filename.c_str(), "r");
112 if(lp == NULL) {
113 stream->printf("File not found: %s\r\n", filename.c_str());
114 return;
115 }
116 string buffer;
117 int c;
118 int newlines = 0;
119
120 // Print each line of the file
121 while ((c = fgetc (lp)) != EOF){
122 buffer.append((char *)&c, 1);
123 if( char(c) == '\n' ){
124 newlines++;
125 stream->puts(buffer.c_str());
126 buffer.clear();
127 }
128 if( newlines == limit ){ break; }
129 };
130 fclose(lp);
131
132 }
133
134 // Reset the system
135 void SimpleShell::reset_command( string parameters, StreamOutput* stream){
136 stream->printf("Smoothie out. Peace.\r\n");
137 system_reset();
138 }
139
140 // Break out into the MRI debugging system
141 void SimpleShell::break_command( string parameters, StreamOutput* stream){
142 stream->printf("Entering MRI debug mode...\r\n");
143 __debugbreak();
144 }
145
146 void SimpleShell::help_command( string parameters, StreamOutput* stream ){
147 stream->printf("Commands:\r\n");
148 stream->printf("ls [folder]\r\n");
149 stream->printf("cd folder\r\n");
150 stream->printf("pwd\r\n");
151 stream->printf("cat file [limit]\r\n");
152 stream->printf("play file [-q]\r\n");
153 stream->printf("progress\r\n");
154 stream->printf("abort\r\n");
155 stream->printf("reset\r\n");
156 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
157 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
158 stream->printf("config-load [<file_name>]\r\n");
159 }
160