DFU: increase detach timeout to 2 seconds
[clinton/Smoothieware.git] / src / libs / Config.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
9 using namespace std;
10 #include <vector>
11 #include <string>
12
13 #include "libs/Kernel.h"
14 #include "Config.h"
15 #include "ConfigValue.h"
16 #include "ConfigSource.h"
17 #include "ConfigCache.h"
18 #include "libs/nuts_bolts.h"
19 #include "libs/utils.h"
20 #include "libs/SerialMessage.h"
21 #include "libs/ConfigSources/FileConfigSource.h"
22
23 Config::Config(){
24 this->config_cache_loaded = false;
25
26 // Config source for */config files
27 this->config_sources.push_back( new FileConfigSource("/local/config", LOCAL_CONFIGSOURCE_CHECKSUM) );
28 this->config_sources.push_back( new FileConfigSource("/sd/config", SD_CONFIGSOURCE_CHECKSUM ) );
29
30 // Pre-load the config cache
31 this->config_cache_load();
32
33 }
34
35 void Config::on_module_loaded(){}
36
37 void Config::on_console_line_received( void* argument ){}
38
39 void Config::set_string( string setting, string value ){
40 ConfigValue* cv = new ConfigValue;
41 cv->found = true;
42 get_checksums(cv->check_sums, setting);
43 cv->value = value;
44
45 this->config_cache.replace_or_push_back(cv);
46
47 this->kernel->call_event(ON_CONFIG_RELOAD);
48 }
49
50 void Config::get_module_list(vector<uint16_t>* list, uint16_t family){
51 for( unsigned int i=1; i<this->config_cache.size(); i++){
52 ConfigValue* value = this->config_cache.at(i);
53 //if( value->check_sums.size() == 3 && value->check_sums.at(2) == 29545 && value->check_sums.at(0) == family ){
54 if( value->check_sums[2] == 29545 && value->check_sums[0] == family ){
55 // We found a module enable for this family, add it's number
56 list->push_back(value->check_sums[1]);
57 }
58 }
59 }
60
61
62 // Command to load config cache into buffer for multiple reads during init
63 void Config::config_cache_load(){
64
65 this->config_cache_clear();
66
67 // First element is a special empty ConfigValue for values not found
68 ConfigValue* result = new ConfigValue;
69 this->config_cache.push_back(result);
70
71 // For each ConfigSource in our stack
72 for( unsigned int i = 0; i < this->config_sources.size(); i++ ){
73 ConfigSource* source = this->config_sources[i];
74 source->transfer_values_to_cache(&this->config_cache);
75 }
76
77 this->config_cache_loaded = true;
78 }
79
80 // Command to clear the config cache after init
81 void Config::config_cache_clear(){
82 while( ! this->config_cache.empty() ){
83 delete this->config_cache.back();
84 this->config_cache.pop_back();
85 }
86 this->config_cache_loaded = false;
87 }
88
89
90 ConfigValue* Config::value(uint16_t check_sum_a, uint16_t check_sum_b, uint16_t check_sum_c ){
91 uint16_t check_sums[3];
92 check_sums[0] = check_sum_a;
93 check_sums[1] = check_sum_b;
94 check_sums[2] = check_sum_c;
95 return this->value(check_sums);
96 }
97
98 ConfigValue* Config::value(uint16_t check_sum_a, uint16_t check_sum_b){
99 uint16_t check_sums[3];
100 check_sums[0] = check_sum_a;
101 check_sums[1] = check_sum_b;
102 check_sums[2] = 0x0000;
103 return this->value(check_sums);
104 }
105
106 ConfigValue* Config::value(uint16_t check_sum){
107 uint16_t check_sums[3];
108 check_sums[0] = check_sum;
109 check_sums[1] = 0x0000;
110 check_sums[2] = 0x0000;
111 return this->value(check_sums);
112 }
113
114 // Get a value from the configuration as a string
115 // Because we don't like to waste space in Flash with lengthy config parameter names, we take a checksum instead so that the name does not have to be stored
116 // See get_checksum
117 ConfigValue* Config::value(uint16_t check_sums[]){
118 ConfigValue* result = this->config_cache[0];
119 bool cache_preloaded = this->config_cache_loaded;
120 if( !cache_preloaded ){ this->config_cache_load(); }
121
122 for( unsigned int i=1; i<this->config_cache.size(); i++){
123 // If this line matches the checksum
124 bool match = true;
125 unsigned int counter = 0;
126 while(check_sums[counter] != 0x0000 && counter <= 2 ){
127 if(this->config_cache[i]->check_sums[counter] != check_sums[counter] ){
128 match = false;
129 break;
130 }
131 counter++;
132 }
133 if( match == false ){
134 continue;
135 }
136 result = this->config_cache[i];
137 break;
138 }
139
140 if( !cache_preloaded ){
141 this->config_cache_clear();
142 }
143 return result;
144 }
145
146
147