Merge branch 'edge' into debugbreak
[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/Player.h"
16 #include "mri.h"
17
18
19 void SimpleShell::on_module_loaded(){
20 this->current_path = "/";
21 this->playing_file = false;
22 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
23 this->register_for_event(ON_MAIN_LOOP);
24 }
25
26 // When a new line is received, check if it is a command, and if it is, act upon it
27 void SimpleShell::on_console_line_received( void* argument ){
28 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
29 string possible_command = new_message.message;
30
31 //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
32
33 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
34 unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
35
36 // Act depending on command
37 if (check_sum == ls_command_checksum)
38 this->ls_command( get_arguments(possible_command), new_message.stream );
39 else if (check_sum == cd_command_checksum)
40 this->cd_command( get_arguments(possible_command), new_message.stream );
41 else if (check_sum == pwd_command_checksum)
42 this->pwd_command( get_arguments(possible_command), new_message.stream );
43 else if (check_sum == cat_command_checksum)
44 this->cat_command( get_arguments(possible_command), new_message.stream );
45 else if (check_sum == play_command_checksum)
46 this->play_command(get_arguments(possible_command), new_message.stream );
47 else if (check_sum == break_command_checksum)
48 this->break_command(get_arguments(possible_command),new_message.stream );
49 else if (check_sum == reset_command_checksum)
50 this->reset_command(get_arguments(possible_command),new_message.stream );
51 else if (check_sum == dfu_command_checksum)
52 this->reset_command(get_arguments(possible_command),new_message.stream );
53 else if (check_sum == help_command_checksum)
54 this->help_command(get_arguments(possible_command),new_message.stream );
55 else if (check_sum == progress_command_checksum)
56 this->progress_command(get_arguments(possible_command),new_message.stream );
57 else if (check_sum == abort_command_checksum)
58 this->abort_command(get_arguments(possible_command),new_message.stream );
59 }
60
61 // Convert a path indication ( absolute or relative ) into a path ( absolute )
62 string SimpleShell::absolute_from_relative( string path ){
63 if( path[0] == '/' ){ return path; }
64 if( path[0] == '.' ){ return this->current_path; }
65 return this->current_path + path;
66 }
67
68 // Act upon an ls command
69 // Convert the first parameter into an absolute path, then list the files in that path
70 void SimpleShell::ls_command( string parameters, StreamOutput* stream ){
71 string folder = this->absolute_from_relative( parameters );
72 DIR* d;
73 struct dirent* p;
74 d = opendir(folder.c_str());
75 if(d != NULL) {
76 while((p = readdir(d)) != NULL) { stream->printf("%s\r\n", lc(string(p->d_name)).c_str()); }
77 closedir(d);
78 } else {
79 stream->printf("Could not open directory %s \r\n", folder.c_str());
80 }
81 }
82
83 // Change current absolute path to provided path
84 void SimpleShell::cd_command( string parameters, StreamOutput* stream ){
85 string folder = this->absolute_from_relative( parameters );
86 if( folder[folder.length()-1] != '/' ){ folder += "/"; }
87 DIR *d;
88 d = opendir(folder.c_str());
89 if(d == NULL) {
90 stream->printf("Could not open directory %s \r\n", folder.c_str() );
91 }else{
92 this->current_path = folder;
93 closedir(d);
94 }
95 }
96
97 // Responds with the present working directory
98 void SimpleShell::pwd_command( string parameters, StreamOutput* stream ){
99 stream->printf("%s\r\n", this->current_path.c_str());
100 }
101
102 // Output the contents of a file, first parameter is the filename, second is the limit ( in number of lines to output )
103 void SimpleShell::cat_command( string parameters, StreamOutput* stream ){
104
105 // Get parameters ( filename and line limit )
106 string filename = this->absolute_from_relative(shift_parameter( parameters ));
107 string limit_paramater = shift_parameter( parameters );
108 int limit = -1;
109 if( limit_paramater != "" )
110 {
111 char* e = NULL;
112 limit = strtol(limit_paramater.c_str(), &e, 10);
113 if (e <= limit_paramater.c_str())
114 limit = -1;
115 }
116
117 // Open file
118 FILE *lp = fopen(filename.c_str(), "r");
119 if(lp == NULL) {
120 stream->printf("File not found: %s\r\n", filename.c_str());
121 return;
122 }
123 string buffer;
124 int c;
125 int newlines = 0;
126
127 // Print each line of the file
128 while ((c = fgetc (lp)) != EOF){
129 buffer.append((char *)&c, 1);
130 if( char(c) == '\n' ){
131 newlines++;
132 stream->puts(buffer.c_str());
133 buffer.clear();
134 }
135 if( newlines == limit ){ break; }
136 };
137 fclose(lp);
138
139 }
140
141 // Play a gcode file by considering each line as if it was received on the serial console
142 void SimpleShell::play_command( string parameters, StreamOutput* stream ){
143
144 // Get filename
145 string filename = this->absolute_from_relative(shift_parameter( parameters ));
146 stream->printf("Playing %s\r\n", filename.c_str());
147 string options = shift_parameter( parameters );
148
149 this->current_file_handler = fopen( filename.c_str(), "r");
150 if(this->current_file_handler == NULL)
151 {
152 stream->printf("File not found: %s\r\n", filename.c_str());
153 return;
154 }
155 this->playing_file = true;
156 if( options.find_first_of("Qq") == string::npos ){
157 this->current_stream = stream;
158 }else{
159 this->current_stream = kernel->streams;
160 }
161
162 // get size of file
163 int result = fseek(this->current_file_handler, 0, SEEK_END);
164 if (0 != result){
165 stream->printf("WARNING - Could not get file size\r\n");
166 file_size= -1;
167 }else{
168 file_size= ftell(this->current_file_handler);
169 fseek(this->current_file_handler, 0, SEEK_SET);
170 stream->printf(" File size %ld\r\n", file_size);
171 }
172 played_cnt= 0;
173 }
174
175 void SimpleShell::progress_command( string parameters, StreamOutput* stream ){
176 if(!playing_file) {
177 stream->printf("Not currently playing\r\n");
178 return;
179 }
180
181 if(file_size > 0) {
182 int pcnt= (file_size - (file_size - played_cnt)) * 100 / file_size;
183 stream->printf("%d %% complete\r\n", pcnt);
184 }else{
185 stream->printf("File size is unknown\r\n");
186 }
187 }
188
189 void SimpleShell::abort_command( string parameters, StreamOutput* stream ){
190 if(!playing_file) {
191 stream->printf("Not currently playing\r\n");
192 return;
193 }
194 playing_file = false;
195 played_cnt= 0;
196 file_size= 0;
197 fclose(current_file_handler);
198 stream->printf("Aborted playing file\r\n");
199 }
200
201 // Reset the system
202 void SimpleShell::reset_command( string parameters, StreamOutput* stream){
203 stream->printf("Smoothie out. Peace.\r\n");
204 system_reset();
205 }
206
207 // Break out into the MRI debugging system
208 void SimpleShell::break_command( string parameters, StreamOutput* stream){
209 stream->printf("Entering MRI debug mode...\r\n");
210 __debugbreak();
211 }
212
213 void SimpleShell::help_command( string parameters, StreamOutput* stream ){
214 stream->printf("Commands:\r\n");
215 stream->printf("ls [folder]\r\n");
216 stream->printf("cd folder\r\n");
217 stream->printf("pwd\r\n");
218 stream->printf("cat file [limit]\r\n");
219 stream->printf("play file [-q]\r\n");
220 stream->printf("progress\r\n");
221 stream->printf("abort\r\n");
222 stream->printf("reset\r\n");
223 stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
224 stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
225 stream->printf("config-load [<file_name>]\r\n");
226 }
227
228 void SimpleShell::on_main_loop(void* argument){
229
230 if( this->playing_file ){
231 string buffer;
232 int c;
233 buffer.reserve(20);
234 // Print each line of the file
235 while ((c = fgetc(this->current_file_handler)) != EOF){
236 if (c == '\n'){
237 this->current_stream->printf("%s\n", buffer.c_str());
238 struct SerialMessage message;
239 message.message = buffer;
240 message.stream = this->current_stream;
241 // wait for the queue to have enough room that a serial message could still be received before sending
242 this->kernel->player->wait_for_queue(2);
243 this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
244 played_cnt += buffer.size();
245 buffer.clear();
246 return;
247 }else{
248 buffer += c;
249 }
250 };
251 this->playing_file = false;
252 played_cnt= 0;
253 file_size= 0;
254 fclose(this->current_file_handler);
255 }
256 }