Commit | Line | Data |
---|---|---|
de91760a | 1 | /* |
4cff3ded AW |
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. | |
de91760a | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
4cff3ded AW |
6 | */ |
7 | ||
b14f84e0 JM |
8 | #include "GcodeDispatch.h" |
9 | ||
4cff3ded AW |
10 | #include "libs/Kernel.h" |
11 | #include "utils/Gcode.h" | |
436a2cd1 | 12 | #include "libs/nuts_bolts.h" |
3fceb8eb | 13 | #include "modules/robot/Conveyor.h" |
423df6df | 14 | #include "libs/SerialMessage.h" |
838b33b4 | 15 | #include "libs/StreamOutput.h" |
17704c93 | 16 | #include "libs/StreamOutputPool.h" |
c4d4e3f8 | 17 | #include "libs/FileStream.h" |
825f27de | 18 | #include "libs/AppendFileStream.h" |
61134a65 | 19 | #include "Config.h" |
7af0714f | 20 | #include "checksumm.h" |
8d54c34c | 21 | #include "ConfigValue.h" |
c89338bd JM |
22 | #include "PublicDataRequest.h" |
23 | #include "PublicData.h" | |
5c051155 JM |
24 | #include "SimpleShell.h" |
25 | #include "utils.h" | |
4cff3ded | 26 | |
b375ba1d | 27 | #define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode") |
c89338bd JM |
28 | #define panel_display_message_checksum CHECKSUM("display_message") |
29 | #define panel_checksum CHECKSUM("panel") | |
b375ba1d | 30 | |
4fb7087f | 31 | // goes in Flash, list of Mxxx codes that are allowed when in Halted state |
96aba4af | 32 | static const int allowed_mcodes[]= {105,114,119}; // get temp, get pos, get endstops |
4fb7087f JM |
33 | static bool is_allowed_mcode(int m) { |
34 | for (size_t i = 0; i < sizeof(allowed_mcodes)/sizeof(int); ++i) { | |
35 | if(allowed_mcodes[i] == m) return true; | |
36 | } | |
37 | return false; | |
38 | } | |
39 | ||
b375ba1d JM |
40 | GcodeDispatch::GcodeDispatch() |
41 | { | |
b375ba1d JM |
42 | uploading = false; |
43 | currentline = -1; | |
44 | last_g= 255; | |
45 | } | |
4cff3ded AW |
46 | |
47 | // Called when the module has just been loaded | |
adf89655 JM |
48 | void GcodeDispatch::on_module_loaded() |
49 | { | |
4cff3ded AW |
50 | this->register_for_event(ON_CONSOLE_LINE_RECEIVED); |
51 | } | |
52 | ||
53 | // When a command is received, if it is a Gcode, dispatch it as an object via an event | |
adf89655 JM |
54 | void GcodeDispatch::on_console_line_received(void *line) |
55 | { | |
56 | SerialMessage new_message = *static_cast<SerialMessage *>(line); | |
de91760a | 57 | string possible_command = new_message.message; |
b6c86164 | 58 | |
62567509 MC |
59 | int ln = 0; |
60 | int cs = 0; | |
c4c70e85 JM |
61 | |
62 | try_again: | |
63 | ||
64 | char first_char = possible_command[0]; | |
c2885de8 | 65 | unsigned int n; |
adf89655 | 66 | if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ) { |
9a29161f | 67 | |
62567509 | 68 | //Get linenumber |
adf89655 | 69 | if ( first_char == 'N' ) { |
aae5a789 | 70 | Gcode full_line = Gcode(possible_command, new_message.stream, false); |
534e17b9 MC |
71 | ln = (int) full_line.get_value('N'); |
72 | int chksum = (int) full_line.get_value('*'); | |
62567509 | 73 | |
f337d8fd | 74 | //Catch message if it is M110: Set Current Line Number |
b9b1bb25 JM |
75 | if ( full_line.has_m ) { |
76 | if ( full_line.m == 110 ) { | |
f337d8fd MC |
77 | currentline = ln; |
78 | new_message.stream->printf("ok\r\n"); | |
79 | return; | |
80 | } | |
81 | } | |
82 | ||
534e17b9 | 83 | //Strip checksum value from possible_command |
62567509 | 84 | size_t chkpos = possible_command.find_first_of("*"); |
de91760a | 85 | possible_command = possible_command.substr(0, chkpos); |
534e17b9 | 86 | //Calculate checksum |
adf89655 JM |
87 | if ( chkpos != string::npos ) { |
88 | for (auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++) | |
7a2ca498 | 89 | cs = cs ^ *c; |
62567509 | 90 | cs &= 0xff; // Defensive programming... |
534e17b9 | 91 | cs -= chksum; |
62567509 | 92 | } |
534e17b9 | 93 | //Strip line number value from possible_command |
9883efb8 | 94 | size_t lnsize = possible_command.find_first_not_of("N0123456789.,- "); |
de91760a | 95 | possible_command = possible_command.substr(lnsize); |
f337d8fd | 96 | |
adf89655 | 97 | } else { |
534e17b9 MC |
98 | //Assume checks succeeded |
99 | cs = 0x00; | |
100 | ln = currentline + 1; | |
9a29161f MC |
101 | } |
102 | ||
436a2cd1 | 103 | //Remove comments |
30c006fd | 104 | size_t comment = possible_command.find_first_of(";("); |
adf89655 JM |
105 | if( comment != string::npos ) { |
106 | possible_command = possible_command.substr(0, comment); | |
107 | } | |
436a2cd1 | 108 | |
62567509 MC |
109 | //If checksum passes then process message, else request resend |
110 | int nextline = currentline + 1; | |
adf89655 | 111 | if( cs == 0x00 && ln == nextline ) { |
f337d8fd | 112 | if( first_char == 'N' ) { |
534e17b9 | 113 | currentline = nextline; |
f337d8fd MC |
114 | } |
115 | ||
d6190d18 | 116 | while(possible_command.size() > 0) { |
49d2ca86 JM |
117 | // assumes G or M are always the first on the line |
118 | size_t nextcmd = possible_command.find_first_of("GM", 2); | |
d6190d18 | 119 | string single_command; |
85bb8e89 | 120 | if(nextcmd == string::npos) { |
d6190d18 MC |
121 | single_command = possible_command; |
122 | possible_command = ""; | |
adf89655 JM |
123 | } else { |
124 | single_command = possible_command.substr(0, nextcmd); | |
2c4285ec | 125 | possible_command = possible_command.substr(nextcmd); |
d6190d18 | 126 | } |
702023f3 | 127 | |
b375ba1d | 128 | |
44e8c359 JM |
129 | if(!uploading) { |
130 | //Prepare gcode for dispatch | |
adf89655 | 131 | Gcode *gcode = new Gcode(single_command, new_message.stream); |
c4c70e85 | 132 | |
73706276 | 133 | if(THEKERNEL->is_halted()) { |
4fb7087f | 134 | // we ignore all commands until M999, unless it is in the exceptions list (like M105 get temp) |
b375ba1d | 135 | if(gcode->has_m && gcode->m == 999) { |
728477c4 | 136 | THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt |
73706276 | 137 | |
76217df5 | 138 | // fall through and pass onto other modules |
4fb7087f JM |
139 | |
140 | }else if(!is_allowed_mcode(gcode->m)) { | |
76217df5 | 141 | // ignore everything, return error string to host |
b375ba1d | 142 | new_message.stream->printf("!!\r\n"); |
76217df5 JM |
143 | delete gcode; |
144 | continue; | |
b375ba1d | 145 | } |
b375ba1d JM |
146 | } |
147 | ||
c2885de8 JM |
148 | if(gcode->has_g) { |
149 | last_g= gcode->g; | |
150 | } | |
b375ba1d | 151 | |
adf89655 JM |
152 | if(gcode->has_m) { |
153 | switch (gcode->m) { | |
154 | case 28: // start upload command | |
155 | delete gcode; | |
156 | ||
157 | this->upload_filename = "/sd/" + single_command.substr(4); // rest of line is filename | |
158 | // open file | |
159 | upload_fd = fopen(this->upload_filename.c_str(), "w"); | |
160 | if(upload_fd != NULL) { | |
161 | this->uploading = true; | |
6d91d256 | 162 | new_message.stream->printf("Writing to file: %s\r\nok\r\n", this->upload_filename.c_str()); |
adf89655 | 163 | } else { |
6d91d256 | 164 | new_message.stream->printf("open failed, File: %s.\r\nok\r\n", this->upload_filename.c_str()); |
adf89655 JM |
165 | } |
166 | //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd); | |
167 | continue; | |
168 | ||
17704c93 JM |
169 | case 112: // emergency stop, do the best we can with this |
170 | // TODO this really needs to be handled out-of-band | |
b375ba1d | 171 | // disables heaters and motors, ignores further incoming Gcode and clears block queue |
728477c4 | 172 | THEKERNEL->call_event(ON_HALT, nullptr); |
b375ba1d JM |
173 | THEKERNEL->streams->printf("ok Emergency Stop Requested - reset or M999 required to continue\r\n"); |
174 | delete gcode; | |
17704c93 JM |
175 | return; |
176 | ||
c89338bd JM |
177 | case 117: // M117 is a special non compliant Gcode as it allows arbitrary text on the line following the command |
178 | { // concatenate the command again and send to panel if enabled | |
179 | string str= single_command.substr(4) + possible_command; | |
180 | PublicData::set_value( panel_checksum, panel_display_message_checksum, &str ); | |
181 | delete gcode; | |
182 | new_message.stream->printf("ok\r\n"); | |
183 | return; | |
184 | } | |
185 | ||
5c051155 JM |
186 | case 1000: // M1000 is a special comanad that will pass thru the raw lowercased command to the simpleshell (for hosts that do not allow such things) |
187 | { | |
188 | // reconstruct entire command line again | |
189 | string str= single_command.substr(5) + possible_command; | |
190 | while(is_whitespace(str.front())){ str= str.substr(1); } // strip leading whitespace | |
191 | ||
192 | delete gcode; | |
193 | ||
194 | if(str.empty()) { | |
195 | SimpleShell::parse_command("help", "", new_message.stream); | |
196 | ||
197 | }else{ | |
198 | string args= lc(str); | |
199 | string cmd = shift_parameter(args); | |
200 | // find command and execute it | |
201 | if(!SimpleShell::parse_command(cmd.c_str(), args, new_message.stream)) { | |
202 | new_message.stream->printf("Command not found: %s\n", cmd.c_str()); | |
203 | } | |
204 | } | |
205 | ||
206 | new_message.stream->printf("ok\r\n"); | |
207 | return; | |
208 | } | |
209 | ||
adf89655 | 210 | case 500: // M500 save volatile settings to config-override |
b14f84e0 | 211 | THEKERNEL->conveyor->wait_for_empty_queue(); //just to be safe as it can take a while to run |
c4d4e3f8 JM |
212 | //remove(THEKERNEL->config_override_filename()); // seems to cause a hang every now and then |
213 | { | |
214 | FileStream fs(THEKERNEL->config_override_filename()); | |
215 | fs.printf("; DO NOT EDIT THIS FILE\n"); | |
216 | // this also will truncate the existing file instead of deleting it | |
217 | } | |
f2f0dfed | 218 | // replace stream with one that writes to config-override file |
825f27de | 219 | gcode->stream = new AppendFileStream(THEKERNEL->config_override_filename()); |
adf89655 | 220 | // dispatch the M500 here so we can free up the stream when done |
314ab8f7 | 221 | THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode ); |
adf89655 JM |
222 | delete gcode->stream; |
223 | delete gcode; | |
347854ff | 224 | new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename()); |
adf89655 JM |
225 | continue; |
226 | ||
618c9b0f | 227 | case 502: // M502 deletes config-override so everything defaults to what is in config |
347854ff MM |
228 | remove(THEKERNEL->config_override_filename()); |
229 | new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename()); | |
adf89655 JM |
230 | delete gcode; |
231 | continue; | |
232 | ||
233 | case 503: { // M503 display live settings and indicates if there is an override file | |
347854ff | 234 | FILE *fd = fopen(THEKERNEL->config_override_filename(), "r"); |
adf89655 JM |
235 | if(fd != NULL) { |
236 | fclose(fd); | |
347854ff | 237 | new_message.stream->printf("; config override present: %s\n", THEKERNEL->config_override_filename()); |
adf89655 JM |
238 | |
239 | } else { | |
240 | new_message.stream->printf("; No config override\n"); | |
241 | } | |
08d9afc6 | 242 | gcode->add_nl= true; |
618c9b0f | 243 | break; // fall through to process by modules |
adf89655 | 244 | } |
44e8c359 | 245 | } |
33e4cc02 JM |
246 | } |
247 | ||
248 | //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m); | |
44e8c359 | 249 | //Dispatch message! |
314ab8f7 | 250 | THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode ); |
adf89655 | 251 | if(gcode->add_nl) |
44e8c359 JM |
252 | new_message.stream->printf("\r\n"); |
253 | ||
6e92ab91 | 254 | if(!gcode->txt_after_ok.empty()) { |
44e8c359 JM |
255 | new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str()); |
256 | gcode->txt_after_ok.clear(); | |
adf89655 | 257 | } else |
44e8c359 JM |
258 | new_message.stream->printf("ok\r\n"); |
259 | ||
260 | delete gcode; | |
261 | ||
adf89655 | 262 | } else { |
44e8c359 JM |
263 | // we are uploading a file so save it |
264 | if(single_command.substr(0, 3) == "M29") { | |
265 | // done uploading, close file | |
266 | fclose(upload_fd); | |
adf89655 JM |
267 | upload_fd = NULL; |
268 | uploading = false; | |
44e8c359 | 269 | upload_filename.clear(); |
6d91d256 | 270 | new_message.stream->printf("Done saving file.\r\nok\r\n"); |
44e8c359 JM |
271 | continue; |
272 | } | |
adf89655 | 273 | |
44e8c359 JM |
274 | if(upload_fd == NULL) { |
275 | // error detected writing to file so discard everything until it stops | |
276 | new_message.stream->printf("ok\r\n"); | |
277 | continue; | |
278 | } | |
adf89655 | 279 | |
44e8c359 | 280 | single_command.append("\n"); |
adf89655 | 281 | static int cnt = 0; |
44e8c359 JM |
282 | if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) { |
283 | // error writing to file | |
284 | new_message.stream->printf("Error:error writing to file.\r\n"); | |
285 | fclose(upload_fd); | |
adf89655 | 286 | upload_fd = NULL; |
44e8c359 | 287 | continue; |
adf89655 JM |
288 | |
289 | } else { | |
290 | cnt += single_command.size(); | |
291 | if (cnt > 400) { | |
44e8c359 JM |
292 | // HACK ALERT to get around fwrite corruption close and re open for append |
293 | fclose(upload_fd); | |
adf89655 JM |
294 | upload_fd = fopen(upload_filename.c_str(), "a"); |
295 | cnt = 0; | |
44e8c359 JM |
296 | } |
297 | new_message.stream->printf("ok\r\n"); | |
298 | //printf("uploading file write ok\n"); | |
299 | } | |
300 | } | |
d6190d18 | 301 | } |
adf89655 JM |
302 | |
303 | } else { | |
62567509 | 304 | //Request resend |
f337d8fd | 305 | new_message.stream->printf("rs N%d\r\n", nextline); |
62567509 | 306 | } |
2bb8b390 | 307 | |
c2885de8 | 308 | } else if( (n=possible_command.find_first_of("XYZF")) == 0 || (first_char == ' ' && n != string::npos) ) { |
c4c70e85 JM |
309 | // handle pycam syntax, use last G0 or G1 and resubmit if an X Y Z or F is found on its own line |
310 | if(last_g != 0 && last_g != 1) { | |
311 | //if no last G1 or G0 ignore | |
312 | //THEKERNEL->streams->printf("ignored: %s\r\n", possible_command.c_str()); | |
313 | return; | |
314 | } | |
c2885de8 JM |
315 | char buf[6]; |
316 | snprintf(buf, sizeof(buf), "G%d ", last_g); | |
317 | possible_command.insert(0, buf); | |
318 | goto try_again; | |
319 | ||
adf89655 JM |
320 | // Ignore comments and blank lines |
321 | } else if ( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ) { | |
b6c86164 | 322 | new_message.stream->printf("ok\r\n"); |
4cff3ded AW |
323 | } |
324 | } | |
325 |