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