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