reorganized and fixed several issues with checksum validation
[clinton/Smoothieware.git] / src / modules / communication / GcodeDispatch.cpp
CommitLineData
4cff3ded
AW
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>
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"
13e4a3f9 15#include "modules/robot/Player.h"
423df6df 16#include "libs/SerialMessage.h"
838b33b4 17#include "libs/StreamOutput.h"
4cff3ded
AW
18
19GcodeDispatch::GcodeDispatch(){}
20
21// Called when the module has just been loaded
22void GcodeDispatch::on_module_loaded() {
23 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
534e17b9 24 currentline = -1;
4cff3ded
AW
25}
26
27// When a command is received, if it is a Gcode, dispatch it as an object via an event
28void GcodeDispatch::on_console_line_received(void * line){
b6c86164
AW
29 SerialMessage new_message = *static_cast<SerialMessage*>(line);
30 string possible_command = new_message.message;
31
4cff3ded 32 char first_char = possible_command[0];
62567509
MC
33 int ln = 0;
34 int cs = 0;
35 if( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'S' || first_char == 'N' ){
9a29161f 36
62567509 37 //Get linenumber
9a29161f 38 if( first_char == 'N' ){
534e17b9
MC
39 Gcode full_line = Gcode();
40 full_line.command = possible_command;
41 full_line.stream = new_message.stream;
42 ln = (int) full_line.get_value('N');
43 int chksum = (int) full_line.get_value('*');
62567509 44
534e17b9 45 //Strip checksum value from possible_command
62567509 46 size_t chkpos = possible_command.find_first_of("*");
534e17b9
MC
47 possible_command = possible_command.substr(0, chkpos);
48 //Calculate checksum
62567509
MC
49 if( chkpos != string::npos ){
50 for(int i = 0; possible_command[i] != '*' && possible_command[i] != NULL; i++)
51 cs = cs ^ possible_command[i];
52 cs &= 0xff; // Defensive programming...
534e17b9 53 cs -= chksum;
62567509 54 }
534e17b9
MC
55 //Strip line number value from possible_command
56 size_t lnsize = possible_command.find_first_of(" ") + 1;
57 possible_command = possible_command.substr(lnsize);
58 }else{
59 //Assume checks succeeded
60 cs = 0x00;
61 ln = currentline + 1;
9a29161f
MC
62 }
63
436a2cd1
AW
64 //Remove comments
65 size_t comment = possible_command.find_first_of(";");
66 if( comment != string::npos ){ possible_command = possible_command.substr(0, comment); }
67
62567509 68 //Prepare gcode for dispatch
4cff3ded
AW
69 Gcode gcode = Gcode();
70 gcode.command = possible_command;
3c132bd0 71 gcode.stream = new_message.stream;
62567509 72
534e17b9
MC
73 //Catch message if it is M110: Set Current Line Number
74 if( gcode.has_letter('M') ){
75 if( ((int) gcode.get_value('M')) == 110 ){
76 currentline = ln;
77 new_message.stream->printf("ok\r\n");
78 return;
79 }
80 }
81
62567509
MC
82 //If checksum passes then process message, else request resend
83 int nextline = currentline + 1;
84 if( cs == 0x00 && ln == nextline ){
534e17b9
MC
85 if( first_char == 'N' )
86 currentline = nextline;
87 //Dispatch message!
88 this->kernel->call_event(ON_GCODE_RECEIVED, &gcode );
62567509
MC
89 new_message.stream->printf("ok\r\n");
90 }else{
91 //Request resend
534e17b9 92 new_message.stream->printf("rs N%d\r\n", nextline, ((int) gcode.get_value('M')), possible_command.c_str());
62567509 93 }
2bb8b390 94
9a29161f
MC
95 // Ignore comments and blank lines
96 }else if( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ){
b6c86164 97 new_message.stream->printf("ok\r\n");
4cff3ded
AW
98 }
99}
100