Fix bug in Line number handling
[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
8#include <string>
9using std::string;
4cff3ded
AW
10#include "libs/Module.h"
11#include "libs/Kernel.h"
12#include "utils/Gcode.h"
436a2cd1 13#include "libs/nuts_bolts.h"
4cff3ded 14#include "GcodeDispatch.h"
3fceb8eb 15#include "modules/robot/Conveyor.h"
423df6df 16#include "libs/SerialMessage.h"
838b33b4 17#include "libs/StreamOutput.h"
f2f0dfed 18#include "libs/FileStream.h"
61134a65 19#include "Config.h"
7af0714f 20#include "checksumm.h"
8d54c34c 21#include "ConfigValue.h"
4cff3ded 22
adf89655 23GcodeDispatch::GcodeDispatch() {}
4cff3ded
AW
24
25// Called when the module has just been loaded
adf89655
JM
26void GcodeDispatch::on_module_loaded()
27{
314ab8f7 28 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
4cff3ded 29 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
534e17b9 30 currentline = -1;
adf89655 31 uploading = false;
c2885de8 32 last_g= 255;
4cff3ded
AW
33}
34
35// When a command is received, if it is a Gcode, dispatch it as an object via an event
adf89655
JM
36void GcodeDispatch::on_console_line_received(void *line)
37{
38 SerialMessage new_message = *static_cast<SerialMessage *>(line);
de91760a 39 string possible_command = new_message.message;
b6c86164 40
62567509
MC
41 int ln = 0;
42 int cs = 0;
c4c70e85
JM
43
44try_again:
45
46 char first_char = possible_command[0];
c2885de8 47 unsigned int n;
adf89655 48 if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ) {
9a29161f 49
62567509 50 //Get linenumber
adf89655 51 if ( first_char == 'N' ) {
aae5a789 52 Gcode full_line = Gcode(possible_command, new_message.stream, false);
534e17b9
MC
53 ln = (int) full_line.get_value('N');
54 int chksum = (int) full_line.get_value('*');
62567509 55
f337d8fd 56 //Catch message if it is M110: Set Current Line Number
b9b1bb25
JM
57 if ( full_line.has_m ) {
58 if ( full_line.m == 110 ) {
f337d8fd
MC
59 currentline = ln;
60 new_message.stream->printf("ok\r\n");
61 return;
62 }
63 }
64
534e17b9 65 //Strip checksum value from possible_command
62567509 66 size_t chkpos = possible_command.find_first_of("*");
de91760a 67 possible_command = possible_command.substr(0, chkpos);
534e17b9 68 //Calculate checksum
adf89655
JM
69 if ( chkpos != string::npos ) {
70 for (auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++)
7a2ca498 71 cs = cs ^ *c;
62567509 72 cs &= 0xff; // Defensive programming...
534e17b9 73 cs -= chksum;
62567509 74 }
534e17b9 75 //Strip line number value from possible_command
9883efb8 76 size_t lnsize = possible_command.find_first_not_of("N0123456789.,- ");
de91760a 77 possible_command = possible_command.substr(lnsize);
f337d8fd 78
adf89655 79 } else {
534e17b9
MC
80 //Assume checks succeeded
81 cs = 0x00;
82 ln = currentline + 1;
9a29161f
MC
83 }
84
436a2cd1 85 //Remove comments
30c006fd 86 size_t comment = possible_command.find_first_of(";(");
adf89655
JM
87 if( comment != string::npos ) {
88 possible_command = possible_command.substr(0, comment);
89 }
436a2cd1 90
62567509
MC
91 //If checksum passes then process message, else request resend
92 int nextline = currentline + 1;
adf89655 93 if( cs == 0x00 && ln == nextline ) {
f337d8fd 94 if( first_char == 'N' ) {
534e17b9 95 currentline = nextline;
f337d8fd
MC
96 }
97
d6190d18 98 while(possible_command.size() > 0) {
43c443ef 99 size_t nextcmd = possible_command.find_first_of("GM", possible_command.find_first_of("GM") + 1);
d6190d18 100 string single_command;
85bb8e89 101 if(nextcmd == string::npos) {
d6190d18
MC
102 single_command = possible_command;
103 possible_command = "";
adf89655
JM
104 } else {
105 single_command = possible_command.substr(0, nextcmd);
2c4285ec 106 possible_command = possible_command.substr(nextcmd);
d6190d18 107 }
702023f3 108
44e8c359
JM
109 if(!uploading) {
110 //Prepare gcode for dispatch
adf89655 111 Gcode *gcode = new Gcode(single_command, new_message.stream);
c4c70e85 112
c2885de8
JM
113 if(gcode->has_g) {
114 last_g= gcode->g;
115 }
adf89655
JM
116 if(gcode->has_m) {
117 switch (gcode->m) {
118 case 28: // start upload command
119 delete gcode;
120
121 this->upload_filename = "/sd/" + single_command.substr(4); // rest of line is filename
122 // open file
123 upload_fd = fopen(this->upload_filename.c_str(), "w");
124 if(upload_fd != NULL) {
125 this->uploading = true;
126 new_message.stream->printf("Writing to file: %s\r\n", this->upload_filename.c_str());
127 } else {
128 new_message.stream->printf("open failed, File: %s.\r\n", this->upload_filename.c_str());
129 }
130 //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd);
131 continue;
132
133 case 500: // M500 save volatile settings to config-override
f2f0dfed 134 // replace stream with one that writes to config-override file
347854ff 135 gcode->stream = new FileStream(THEKERNEL->config_override_filename());
adf89655 136 // dispatch the M500 here so we can free up the stream when done
314ab8f7 137 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
adf89655
JM
138 delete gcode->stream;
139 delete gcode;
347854ff 140 new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename());
adf89655
JM
141 continue;
142
618c9b0f 143 case 502: // M502 deletes config-override so everything defaults to what is in config
347854ff
MM
144 remove(THEKERNEL->config_override_filename());
145 new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename());
adf89655
JM
146 delete gcode;
147 continue;
148
149 case 503: { // M503 display live settings and indicates if there is an override file
347854ff 150 FILE *fd = fopen(THEKERNEL->config_override_filename(), "r");
adf89655
JM
151 if(fd != NULL) {
152 fclose(fd);
347854ff 153 new_message.stream->printf("; config override present: %s\n", THEKERNEL->config_override_filename());
adf89655
JM
154
155 } else {
156 new_message.stream->printf("; No config override\n");
157 }
618c9b0f 158 break; // fall through to process by modules
adf89655 159 }
44e8c359 160 }
33e4cc02
JM
161 }
162
163 //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
44e8c359 164 //Dispatch message!
314ab8f7 165 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
adf89655 166 if(gcode->add_nl)
44e8c359
JM
167 new_message.stream->printf("\r\n");
168
adf89655 169 if( return_error_on_unhandled_gcode == true && gcode->accepted_by_module == false)
44e8c359
JM
170 new_message.stream->printf("ok (command unclaimed)\r\n");
171 else if(!gcode->txt_after_ok.empty()) {
172 new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str());
173 gcode->txt_after_ok.clear();
adf89655 174 } else
44e8c359
JM
175 new_message.stream->printf("ok\r\n");
176
177 delete gcode;
178
adf89655 179 } else {
44e8c359
JM
180 // we are uploading a file so save it
181 if(single_command.substr(0, 3) == "M29") {
182 // done uploading, close file
183 fclose(upload_fd);
adf89655
JM
184 upload_fd = NULL;
185 uploading = false;
44e8c359
JM
186 upload_filename.clear();
187 new_message.stream->printf("Done saving file.\r\n");
188 continue;
189 }
adf89655 190
44e8c359
JM
191 if(upload_fd == NULL) {
192 // error detected writing to file so discard everything until it stops
193 new_message.stream->printf("ok\r\n");
194 continue;
195 }
adf89655 196
44e8c359 197 single_command.append("\n");
adf89655 198 static int cnt = 0;
44e8c359
JM
199 if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) {
200 // error writing to file
201 new_message.stream->printf("Error:error writing to file.\r\n");
202 fclose(upload_fd);
adf89655 203 upload_fd = NULL;
44e8c359 204 continue;
adf89655
JM
205
206 } else {
207 cnt += single_command.size();
208 if (cnt > 400) {
44e8c359
JM
209 // HACK ALERT to get around fwrite corruption close and re open for append
210 fclose(upload_fd);
adf89655
JM
211 upload_fd = fopen(upload_filename.c_str(), "a");
212 cnt = 0;
44e8c359
JM
213 }
214 new_message.stream->printf("ok\r\n");
215 //printf("uploading file write ok\n");
216 }
217 }
d6190d18 218 }
adf89655
JM
219
220 } else {
62567509 221 //Request resend
f337d8fd 222 new_message.stream->printf("rs N%d\r\n", nextline);
62567509 223 }
2bb8b390 224
c2885de8 225 } else if( (n=possible_command.find_first_of("XYZF")) == 0 || (first_char == ' ' && n != string::npos) ) {
c4c70e85
JM
226 // handle pycam syntax, use last G0 or G1 and resubmit if an X Y Z or F is found on its own line
227 if(last_g != 0 && last_g != 1) {
228 //if no last G1 or G0 ignore
229 //THEKERNEL->streams->printf("ignored: %s\r\n", possible_command.c_str());
230 return;
231 }
c2885de8
JM
232 char buf[6];
233 snprintf(buf, sizeof(buf), "G%d ", last_g);
234 possible_command.insert(0, buf);
235 goto try_again;
236
adf89655
JM
237 // Ignore comments and blank lines
238 } else if ( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ) {
b6c86164 239 new_message.stream->printf("ok\r\n");
4cff3ded
AW
240 }
241}
242