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