remove on_config_reload event
[clinton/Smoothieware.git] / src / modules / utils / configurator / Configurator.cpp
CommitLineData
6c9499fd 1/*
d3a6143b
L
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
d3a6143b
L
6*/
7
8
9#include "libs/Kernel.h"
6c9499fd 10#include "Configurator.h"
d3a6143b
L
11#include "libs/nuts_bolts.h"
12#include "libs/utils.h"
13#include "libs/SerialMessage.h"
14#include "libs/StreamOutput.h"
61134a65
JM
15#include "checksumm.h"
16#include "Config.h"
17#include "Gcode.h"
7af0714f
JM
18#include "ConfigSource.h"
19#include "FileConfigSource.h"
66383b80 20#include "ConfigValue.h"
a2f7633f 21#include "ConfigCache.h"
d3a6143b 22
577414f6
JM
23#define CONF_NONE 0
24#define CONF_ROM 1
25#define CONF_SD 2
26#define CONF_EEPROM 3
27
a2f7633f
JM
28void Configurator::on_module_loaded()
29{
2b394436 30 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
a2f7633f
JM
31 // this->register_for_event(ON_GCODE_RECEIVED);
32 // this->register_for_event(ON_MAIN_LOOP);
d3a6143b
L
33}
34
35// When a new line is received, check if it is a command, and if it is, act upon it
a2f7633f
JM
36void Configurator::on_console_line_received( void *argument )
37{
38 SerialMessage new_message = *static_cast<SerialMessage *>(argument);
7f613782 39
7e81f138
JM
40 // ignore comments and blank lines and if this is a G code then also ignore it
41 char first_char = new_message.message[0];
42 if(strchr(";( \n\rGMTN", first_char) != NULL) return;
7f613782 43
d3a6143b 44 string possible_command = new_message.message;
7e81f138 45 string cmd = shift_parameter(possible_command);
d3a6143b
L
46
47 // Act depending on command
57714b5c 48 if (cmd == "config-get"){
7e81f138 49 this->config_get_command( possible_command, new_message.stream );
57714b5c 50 } else if (cmd == "config-set"){
7e81f138 51 this->config_set_command( possible_command, new_message.stream );
57714b5c 52 } else if (cmd == "config-load"){
7e81f138
JM
53 this->config_load_command( possible_command, new_message.stream );
54 }
d3a6143b
L
55}
56
57// Process and respond to eeprom gcodes (M50x)
a2f7633f
JM
58void Configurator::on_gcode_received(void *argument)
59{
60 Gcode *gcode = static_cast<Gcode *>(argument);
61 if( gcode->has_letter('G') ) {
62e5b0ca 62 int code = gcode->get_value('G');
a2f7633f 63 switch( code ) {
62e5b0ca 64 }
a2f7633f 65 } else if( gcode->has_letter('M') ) {
d3a6143b 66 int code = gcode->get_value('M');
a2f7633f 67 switch( code ) {
d3a6143b
L
68 }
69 }
70}
71
a2f7633f 72void Configurator::on_main_loop(void *argument) {}
2b394436
L
73
74// Output a ConfigValue from the specified ConfigSource to the stream
a2f7633f
JM
75void Configurator::config_get_command( string parameters, StreamOutput *stream )
76{
5efaa1b1
L
77 string source = shift_parameter(parameters);
78 string setting = shift_parameter(parameters);
a2f7633f 79 if (setting == "") { // output settings from the config-cache
5efaa1b1
L
80 setting = source;
81 source = "";
4464301d
AW
82 uint16_t setting_checksums[3];
83 get_checksums(setting_checksums, setting );
a2f7633f
JM
84 THEKERNEL->config->config_cache_load(); // need to load config cache first as it is unloaded after booting
85 ConfigValue *cv = THEKERNEL->config->value(setting_checksums);
86 if(cv != NULL && cv->found) {
87 string value = cv->as_string();
88 stream->printf( "cached: %s is set to %s\r\n", setting.c_str(), value.c_str() );
89 } else {
90 stream->printf( "cached: %s is not in config\r\n", setting.c_str());
577414f6 91 }
a2f7633f 92 THEKERNEL->config->config_cache_clear();
577414f6 93
a2f7633f 94 } else { // output setting from specified source by parsing the config file
5efaa1b1 95 uint16_t source_checksum = get_checksum( source );
4464301d
AW
96 uint16_t setting_checksums[3];
97 get_checksums(setting_checksums, setting );
a2f7633f
JM
98 for(unsigned int i = 0; i < THEKERNEL->config->config_sources.size(); i++) {
99 if( THEKERNEL->config->config_sources[i]->is_named(source_checksum) ) {
314ab8f7 100 string value = THEKERNEL->config->config_sources[i]->read(setting_checksums);
a2f7633f 101 if(value.empty()) {
577414f6 102 stream->printf( "%s: %s is not in config\r\n", source.c_str(), setting.c_str() );
a2f7633f 103 } else {
577414f6
JM
104 stream->printf( "%s: %s is set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
105 }
5efaa1b1
L
106 break;
107 }
108 }
2b394436
L
109 }
110}
111
112// Write the specified setting to the specified ConfigSource
a2f7633f
JM
113void Configurator::config_set_command( string parameters, StreamOutput *stream )
114{
2b394436
L
115 string source = shift_parameter(parameters);
116 string setting = shift_parameter(parameters);
117 string value = shift_parameter(parameters);
a2f7633f 118 if(source.empty() || setting.empty() || value.empty()) {
d8cff5af
JM
119 stream->printf( "Usage: config-set source setting value # where source is sd, setting is the key and value is the new value\r\n" );
120 return;
121 }
122
123 uint16_t source_checksum = get_checksum(source);
a2f7633f
JM
124 for(unsigned int i = 0; i < THEKERNEL->config->config_sources.size(); i++) {
125 if( THEKERNEL->config->config_sources[i]->is_named(source_checksum) ) {
d8cff5af 126 if(THEKERNEL->config->config_sources[i]->write(setting, value)) {
ced5fce8 127 stream->printf( "%s: %s has been set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
a2f7633f 128 } else {
d8cff5af 129 stream->printf( "%s: %s not enough space to overwrite existing key/value\r\n", source.c_str(), setting.c_str() );
ced5fce8 130 }
d8cff5af 131 return;
ced5fce8 132 }
2b394436 133 }
d8cff5af
JM
134 stream->printf( "%s source does not exist\r\n", source.c_str());
135
136 /* Live setting not really supported anymore as the cache is never left loaded
137 if (value == "") {
138 if(!THEKERNEL->config->config_cache_loaded) {
139 stream->printf( "live: setting not allowed as config cache is not loaded\r\n" );
140 return;
141 }
142 value = setting;
143 setting = source;
144 source = "";
145 THEKERNEL->config->set_string(setting, value);
146 stream->printf( "live: %s has been set to %s\r\n", setting.c_str(), value.c_str() );
147 */
d3a6143b
L
148}
149
a2f7633f
JM
150// Reload config values from the specified ConfigSource, NOTE this probably does not work anymore
151// also used for debugging by dumping the config-cache
152void Configurator::config_load_command( string parameters, StreamOutput *stream )
153{
89c31ddb 154 string source = shift_parameter(parameters);
a2f7633f
JM
155 if(source == "load") {
156 THEKERNEL->config->config_cache_load();
34a6a1d6 157 stream->printf( "config cache loaded\r\n");
a2f7633f
JM
158
159 } else if(source == "unload") {
160 THEKERNEL->config->config_cache_clear();
161 stream->printf( "config cache unloaded\r\n" );
162
163 } else if(source == "dump") {
164 THEKERNEL->config->config_cache_load();
165 THEKERNEL->config->config_cache->dump(stream);
166 THEKERNEL->config->config_cache_clear();
167
168 } else if(source == "checksum") {
169 string key = shift_parameter(parameters);
170 uint16_t cs[3];
171 get_checksums(cs, key);
172 stream->printf( "checksum of %s = %02X %02X %02X\n", key.c_str(), cs[0], cs[1], cs[2]);
173
a2f7633f 174
89c31ddb 175 } else {
c8bd4492 176 stream->printf( "unsupported option\n" );
89c31ddb
L
177 }
178}
41424456 179