Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Smoothie by
libs/Config.cpp@2:1df0b61d3b5a, 2014-02-28 (annotated)
- Committer:
- Michael J. Spencer
- Date:
- Fri Feb 28 18:52:52 2014 -0800
- Revision:
- 2:1df0b61d3b5a
- Parent:
- 0:31e91bb0ef3c
Update to latest Smoothie.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Michael J. Spencer |
2:1df0b61d3b5a | 1 | /* |
| scachat | 0:31e91bb0ef3c | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
| scachat | 0:31e91bb0ef3c | 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. |
| scachat | 0:31e91bb0ef3c | 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. |
| Michael J. Spencer |
2:1df0b61d3b5a | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
| scachat | 0:31e91bb0ef3c | 6 | */ |
| scachat | 0:31e91bb0ef3c | 7 | |
| scachat | 0:31e91bb0ef3c | 8 | using namespace std; |
| scachat | 0:31e91bb0ef3c | 9 | #include <vector> |
| scachat | 0:31e91bb0ef3c | 10 | #include <string> |
| scachat | 0:31e91bb0ef3c | 11 | |
| scachat | 0:31e91bb0ef3c | 12 | #include "libs/Kernel.h" |
| scachat | 0:31e91bb0ef3c | 13 | #include "Config.h" |
| scachat | 0:31e91bb0ef3c | 14 | #include "ConfigValue.h" |
| scachat | 0:31e91bb0ef3c | 15 | #include "ConfigSource.h" |
| scachat | 0:31e91bb0ef3c | 16 | #include "ConfigCache.h" |
| scachat | 0:31e91bb0ef3c | 17 | #include "libs/nuts_bolts.h" |
| scachat | 0:31e91bb0ef3c | 18 | #include "libs/utils.h" |
| scachat | 0:31e91bb0ef3c | 19 | #include "libs/SerialMessage.h" |
| scachat | 0:31e91bb0ef3c | 20 | #include "libs/ConfigSources/FileConfigSource.h" |
| Michael J. Spencer |
2:1df0b61d3b5a | 21 | #include "libs/ConfigSources/FirmConfigSource.h" |
| scachat | 0:31e91bb0ef3c | 22 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 23 | // Add various config sources. Config can be fetched from several places. |
| Michael J. Spencer |
2:1df0b61d3b5a | 24 | // All values are read into a cache, that is then used by modules to read their configuration |
| scachat | 0:31e91bb0ef3c | 25 | Config::Config(){ |
| scachat | 0:31e91bb0ef3c | 26 | this->config_cache_loaded = false; |
| scachat | 0:31e91bb0ef3c | 27 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 28 | // Config source for firm config found in src/config.default |
| Michael J. Spencer |
2:1df0b61d3b5a | 29 | this->config_sources.push_back( new FirmConfigSource() ); |
| Michael J. Spencer |
2:1df0b61d3b5a | 30 | |
| scachat | 0:31e91bb0ef3c | 31 | // Config source for */config files |
| Michael J. Spencer |
2:1df0b61d3b5a | 32 | FileConfigSource* fcs = NULL; |
| Michael J. Spencer |
2:1df0b61d3b5a | 33 | if( file_exists("/local/config") ) |
| Michael J. Spencer |
2:1df0b61d3b5a | 34 | fcs = new FileConfigSource("/local/config", LOCAL_CONFIGSOURCE_CHECKSUM); |
| Michael J. Spencer |
2:1df0b61d3b5a | 35 | else if( file_exists("/local/config.txt") ) |
| Michael J. Spencer |
2:1df0b61d3b5a | 36 | fcs = new FileConfigSource("/local/config.txt", LOCAL_CONFIGSOURCE_CHECKSUM); |
| Michael J. Spencer |
2:1df0b61d3b5a | 37 | if( fcs != NULL ){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 38 | this->config_sources.push_back( fcs ); |
| Michael J. Spencer |
2:1df0b61d3b5a | 39 | fcs = NULL; |
| Michael J. Spencer |
2:1df0b61d3b5a | 40 | } |
| Michael J. Spencer |
2:1df0b61d3b5a | 41 | if( file_exists("/sd/config") ) |
| Michael J. Spencer |
2:1df0b61d3b5a | 42 | fcs = new FileConfigSource("/sd/config", SD_CONFIGSOURCE_CHECKSUM ); |
| Michael J. Spencer |
2:1df0b61d3b5a | 43 | else if( file_exists("/sd/config.txt") ) |
| Michael J. Spencer |
2:1df0b61d3b5a | 44 | fcs = new FileConfigSource("/sd/config.txt", SD_CONFIGSOURCE_CHECKSUM ); |
| Michael J. Spencer |
2:1df0b61d3b5a | 45 | if( fcs != NULL ) |
| Michael J. Spencer |
2:1df0b61d3b5a | 46 | this->config_sources.push_back( fcs ); |
| scachat | 0:31e91bb0ef3c | 47 | |
| scachat | 0:31e91bb0ef3c | 48 | // Pre-load the config cache |
| scachat | 0:31e91bb0ef3c | 49 | this->config_cache_load(); |
| scachat | 0:31e91bb0ef3c | 50 | |
| scachat | 0:31e91bb0ef3c | 51 | } |
| scachat | 0:31e91bb0ef3c | 52 | |
| scachat | 0:31e91bb0ef3c | 53 | void Config::on_module_loaded(){} |
| scachat | 0:31e91bb0ef3c | 54 | |
| scachat | 0:31e91bb0ef3c | 55 | void Config::on_console_line_received( void* argument ){} |
| scachat | 0:31e91bb0ef3c | 56 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 57 | // Set a value in the config cache, but not in any config source |
| scachat | 0:31e91bb0ef3c | 58 | void Config::set_string( string setting, string value ){ |
| scachat | 0:31e91bb0ef3c | 59 | ConfigValue* cv = new ConfigValue; |
| scachat | 0:31e91bb0ef3c | 60 | cv->found = true; |
| Michael J. Spencer |
2:1df0b61d3b5a | 61 | get_checksums(cv->check_sums, setting); |
| scachat | 0:31e91bb0ef3c | 62 | cv->value = value; |
| scachat | 0:31e91bb0ef3c | 63 | |
| scachat | 0:31e91bb0ef3c | 64 | this->config_cache.replace_or_push_back(cv); |
| scachat | 0:31e91bb0ef3c | 65 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 66 | THEKERNEL->call_event(ON_CONFIG_RELOAD); |
| scachat | 0:31e91bb0ef3c | 67 | } |
| scachat | 0:31e91bb0ef3c | 68 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 69 | // Get a list of modules, used by module "pools" that look for the "enable" keyboard to find things like "moduletype.modulename.enable" as the marker of a new instance of a module |
| Michael J. Spencer |
2:1df0b61d3b5a | 70 | void Config::get_module_list(vector<uint16_t>* list, uint16_t family){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 71 | for( unsigned int i=1; i<this->config_cache.size(); i++){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 72 | ConfigValue* value = this->config_cache.at(i); |
| Michael J. Spencer |
2:1df0b61d3b5a | 73 | //if( value->check_sums.size() == 3 && value->check_sums.at(2) == CHECKSUM("enable") && value->check_sums.at(0) == family ){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 74 | if( value->check_sums[2] == CHECKSUM("enable") && value->check_sums[0] == family ){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 75 | // We found a module enable for this family, add it's number |
| Michael J. Spencer |
2:1df0b61d3b5a | 76 | list->push_back(value->check_sums[1]); |
| Michael J. Spencer |
2:1df0b61d3b5a | 77 | } |
| scachat | 0:31e91bb0ef3c | 78 | } |
| scachat | 0:31e91bb0ef3c | 79 | } |
| scachat | 0:31e91bb0ef3c | 80 | |
| scachat | 0:31e91bb0ef3c | 81 | |
| scachat | 0:31e91bb0ef3c | 82 | // Command to load config cache into buffer for multiple reads during init |
| scachat | 0:31e91bb0ef3c | 83 | void Config::config_cache_load(){ |
| scachat | 0:31e91bb0ef3c | 84 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 85 | // First clear the cache |
| scachat | 0:31e91bb0ef3c | 86 | this->config_cache_clear(); |
| scachat | 0:31e91bb0ef3c | 87 | |
| scachat | 0:31e91bb0ef3c | 88 | // First element is a special empty ConfigValue for values not found |
| scachat | 0:31e91bb0ef3c | 89 | ConfigValue* result = new ConfigValue; |
| scachat | 0:31e91bb0ef3c | 90 | this->config_cache.push_back(result); |
| Michael J. Spencer |
2:1df0b61d3b5a | 91 | |
| scachat | 0:31e91bb0ef3c | 92 | // For each ConfigSource in our stack |
| scachat | 0:31e91bb0ef3c | 93 | for( unsigned int i = 0; i < this->config_sources.size(); i++ ){ |
| scachat | 0:31e91bb0ef3c | 94 | ConfigSource* source = this->config_sources[i]; |
| scachat | 0:31e91bb0ef3c | 95 | source->transfer_values_to_cache(&this->config_cache); |
| scachat | 0:31e91bb0ef3c | 96 | } |
| scachat | 0:31e91bb0ef3c | 97 | |
| scachat | 0:31e91bb0ef3c | 98 | this->config_cache_loaded = true; |
| scachat | 0:31e91bb0ef3c | 99 | } |
| scachat | 0:31e91bb0ef3c | 100 | |
| scachat | 0:31e91bb0ef3c | 101 | // Command to clear the config cache after init |
| scachat | 0:31e91bb0ef3c | 102 | void Config::config_cache_clear(){ |
| scachat | 0:31e91bb0ef3c | 103 | while( ! this->config_cache.empty() ){ |
| scachat | 0:31e91bb0ef3c | 104 | delete this->config_cache.back(); |
| scachat | 0:31e91bb0ef3c | 105 | this->config_cache.pop_back(); |
| scachat | 0:31e91bb0ef3c | 106 | } |
| scachat | 0:31e91bb0ef3c | 107 | this->config_cache_loaded = false; |
| scachat | 0:31e91bb0ef3c | 108 | } |
| scachat | 0:31e91bb0ef3c | 109 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 110 | // Three ways to read a value from the config, depending on adress length |
| scachat | 0:31e91bb0ef3c | 111 | ConfigValue* Config::value(uint16_t check_sum_a, uint16_t check_sum_b, uint16_t check_sum_c ){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 112 | uint16_t check_sums[3]; |
| Michael J. Spencer |
2:1df0b61d3b5a | 113 | check_sums[0] = check_sum_a; |
| Michael J. Spencer |
2:1df0b61d3b5a | 114 | check_sums[1] = check_sum_b; |
| Michael J. Spencer |
2:1df0b61d3b5a | 115 | check_sums[2] = check_sum_c; |
| scachat | 0:31e91bb0ef3c | 116 | return this->value(check_sums); |
| Michael J. Spencer |
2:1df0b61d3b5a | 117 | } |
| scachat | 0:31e91bb0ef3c | 118 | |
| scachat | 0:31e91bb0ef3c | 119 | ConfigValue* Config::value(uint16_t check_sum_a, uint16_t check_sum_b){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 120 | uint16_t check_sums[3]; |
| Michael J. Spencer |
2:1df0b61d3b5a | 121 | check_sums[0] = check_sum_a; |
| Michael J. Spencer |
2:1df0b61d3b5a | 122 | check_sums[1] = check_sum_b; |
| Michael J. Spencer |
2:1df0b61d3b5a | 123 | check_sums[2] = 0x0000; |
| scachat | 0:31e91bb0ef3c | 124 | return this->value(check_sums); |
| Michael J. Spencer |
2:1df0b61d3b5a | 125 | } |
| scachat | 0:31e91bb0ef3c | 126 | |
| scachat | 0:31e91bb0ef3c | 127 | ConfigValue* Config::value(uint16_t check_sum){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 128 | uint16_t check_sums[3]; |
| Michael J. Spencer |
2:1df0b61d3b5a | 129 | check_sums[0] = check_sum; |
| Michael J. Spencer |
2:1df0b61d3b5a | 130 | check_sums[1] = 0x0000; |
| Michael J. Spencer |
2:1df0b61d3b5a | 131 | check_sums[2] = 0x0000; |
| scachat | 0:31e91bb0ef3c | 132 | return this->value(check_sums); |
| Michael J. Spencer |
2:1df0b61d3b5a | 133 | } |
| Michael J. Spencer |
2:1df0b61d3b5a | 134 | |
| scachat | 0:31e91bb0ef3c | 135 | // Get a value from the configuration as a string |
| scachat | 0:31e91bb0ef3c | 136 | // 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 |
| scachat | 0:31e91bb0ef3c | 137 | // See get_checksum |
| Michael J. Spencer |
2:1df0b61d3b5a | 138 | ConfigValue* Config::value(uint16_t check_sums[]){ |
| scachat | 0:31e91bb0ef3c | 139 | ConfigValue* result = this->config_cache[0]; |
| scachat | 0:31e91bb0ef3c | 140 | bool cache_preloaded = this->config_cache_loaded; |
| scachat | 0:31e91bb0ef3c | 141 | if( !cache_preloaded ){ this->config_cache_load(); } |
| Michael J. Spencer |
2:1df0b61d3b5a | 142 | |
| Michael J. Spencer |
2:1df0b61d3b5a | 143 | for( unsigned int i=1; i<this->config_cache.size(); i++){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 144 | // If this line matches the checksum |
| scachat | 0:31e91bb0ef3c | 145 | bool match = true; |
| Michael J. Spencer |
2:1df0b61d3b5a | 146 | unsigned int counter = 0; |
| Michael J. Spencer |
2:1df0b61d3b5a | 147 | while(check_sums[counter] != 0x0000 && counter <= 2 ){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 148 | if(this->config_cache[i]->check_sums[counter] != check_sums[counter] ){ |
| scachat | 0:31e91bb0ef3c | 149 | match = false; |
| Michael J. Spencer |
2:1df0b61d3b5a | 150 | break; |
| scachat | 0:31e91bb0ef3c | 151 | } |
| Michael J. Spencer |
2:1df0b61d3b5a | 152 | counter++; |
| Michael J. Spencer |
2:1df0b61d3b5a | 153 | } |
| Michael J. Spencer |
2:1df0b61d3b5a | 154 | if( match == false ){ |
| Michael J. Spencer |
2:1df0b61d3b5a | 155 | continue; |
| scachat | 0:31e91bb0ef3c | 156 | } |
| scachat | 0:31e91bb0ef3c | 157 | result = this->config_cache[i]; |
| scachat | 0:31e91bb0ef3c | 158 | break; |
| scachat | 0:31e91bb0ef3c | 159 | } |
| Michael J. Spencer |
2:1df0b61d3b5a | 160 | |
| scachat | 0:31e91bb0ef3c | 161 | if( !cache_preloaded ){ |
| scachat | 0:31e91bb0ef3c | 162 | this->config_cache_clear(); |
| scachat | 0:31e91bb0ef3c | 163 | } |
| scachat | 0:31e91bb0ef3c | 164 | return result; |
| scachat | 0:31e91bb0ef3c | 165 | } |
| scachat | 0:31e91bb0ef3c | 166 | |
| scachat | 0:31e91bb0ef3c | 167 | |
| scachat | 0:31e91bb0ef3c | 168 |
