Skip to content

Commit 13b60c2

Browse files
authored
Merge pull request #8673 from kegilbert/config-range-limits-dev
Add Mbed Configuration Option Range Limits
2 parents 8dc54f1 + c26c258 commit 13b60c2

File tree

11 files changed

+208
-5
lines changed

11 files changed

+208
-5
lines changed

tools/config/__init__.py

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import json
2222
import six
2323
import os
24+
import re
2425
from os.path import dirname, abspath, exists, join, isabs
2526
import sys
2627
from collections import namedtuple
@@ -101,15 +102,19 @@ def __init__(self, name, data, unit_name, unit_kind):
101102
data - the data associated with the configuration parameter
102103
unit_name - the unit (target/library/application) that defines this
103104
parameter
104-
unit_ kind - the kind of the unit ("target", "library" or "application")
105+
unit_kind - the kind of the unit ("target", "library" or "application")
105106
"""
107+
106108
self.name = self.get_full_name(name, unit_name, unit_kind,
107109
allow_prefix=False)
108110
self.defined_by = self.get_display_name(unit_name, unit_kind)
109111
self.set_value(data.get("value", None), unit_name, unit_kind)
110-
self.help_text = data.get("help", None)
111-
self.required = data.get("required", False)
112-
self.macro_name = data.get("macro_name", "MBED_CONF_%s" %
112+
self.value_min = data.get("value_min")
113+
self.value_max = data.get("value_max")
114+
self.accepted_values = data.get("accepted_values")
115+
self.help_text = data.get("help", None)
116+
self.required = data.get("required", False)
117+
self.macro_name = data.get("macro_name", "MBED_CONF_%s" %
113118
self.sanitize(self.name.upper()))
114119
self.config_errors = []
115120

@@ -1058,9 +1063,61 @@ def validate_config(self):
10581063
10591064
Arguments: None
10601065
"""
1066+
10611067
params, _ = self.get_config_data()
1068+
err_msg = ""
1069+
1070+
for name, param in sorted(params.items()):
1071+
min = param.value_min
1072+
max = param.value_max
1073+
accepted = param.accepted_values
1074+
value = param.value
1075+
1076+
# Config parameters that are only defined but do not have a default
1077+
# value should not be range limited
1078+
if value is not None:
1079+
if (min is not None or max is not None) and (accepted is not None):
1080+
err_msg += "\n%s has both a range and list of accepted values specified. Please only "\
1081+
"specify either value_min and/or value_max, or accepted_values"\
1082+
% param
1083+
else:
1084+
if re.match(r'^(0[xX])[A-Fa-f0-9]+$|^[0-9]+$', str(value)):
1085+
# Value is a hexadecimal or numerical string value
1086+
# Convert to a python integer and range check/compare to
1087+
# accepted list accordingly
1088+
1089+
if min is not None or max is not None:
1090+
# Numerical range check
1091+
# Convert hex strings to integers for range checks
1092+
1093+
value = int(str(value), 0)
1094+
min = int(str(min), 0) if min is not None else None
1095+
max = int(str(max), 0) if max is not None else None
1096+
1097+
if (value < min or (value > max if max is not None else False)):
1098+
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
1099+
% (param,
1100+
min if min is not None else "-inf",
1101+
max if max is not None else "inf")
1102+
1103+
# Numerical accepted value check
1104+
elif accepted is not None and value not in accepted:
1105+
err_msg += "\nInvalid value for %s, is not an accepted value: %s"\
1106+
% (param, ", ".join(map(str, accepted)))
1107+
else:
1108+
if min is not None or max is not None:
1109+
err_msg += "\nInvalid config range settings for %s. Range specifiers are not "\
1110+
"applicable to non-decimal/hexadecimal string values" % param
1111+
1112+
if accepted is not None and value not in accepted:
1113+
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
1114+
% (param, ", ".join(accepted))
1115+
1116+
if (err_msg):
1117+
raise ConfigException(err_msg)
1118+
10621119
for error in self.config_errors:
1063-
if (isinstance(error, UndefinedParameter) and
1120+
if (isinstance(error, UndefinedParameter) and
10641121
error.param in params):
10651122
continue
10661123
else:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "lib1",
3+
"config": {
4+
"config1": {
5+
"help": "The default value should pass as it is in the list of accepted values",
6+
"value": 5,
7+
"accepted_values": [0, 5, 10]
8+
},
9+
"config2": {
10+
"help": "The default value should pass as it is in the range of accepted values",
11+
"value": 7,
12+
"value_min": 0,
13+
"value_max": 10
14+
},
15+
"config3": {
16+
"help": "The default value should pass as it is in the range of accepted values",
17+
"value": "foo",
18+
"accepted_values": ["foo", "bar"]
19+
},
20+
"config4": {
21+
"help": "The default value should pass as it is in the range of accepted values",
22+
"value": "0x1000",
23+
"value_min": "0x10",
24+
"value_max": "0x8000"
25+
},
26+
"config5": {
27+
"help": "The default value should pass as it is in the range of accepted values",
28+
"value": "0x2000",
29+
"value_min": 0,
30+
"value_max": "0x8000"
31+
},
32+
"config6": {
33+
"help": "The default value should pass as it is in the list of accepted values",
34+
"value": "0x8000",
35+
"accepted_values": ["0x1000", "0x8000", "0x12000"]
36+
}
37+
}
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test_target": {
3+
"core": "Cortex-M0",
4+
"extra_labels": [],
5+
"features": [],
6+
"default_lib": "std",
7+
"supported_toolchains": ["GCC_ARM"]
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"test_target": {
3+
"lib1.config1": 5,
4+
"lib1.config2": 7,
5+
"lib1.config3": "foo",
6+
"lib1.config4": "0x1000",
7+
"lib1.config5": "0x2000",
8+
"lib1.config6": "0x8000"
9+
}
10+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "lib1",
3+
"config": {
4+
"config1": {
5+
"help": "The default value should fail as it is not in the range of accepted values",
6+
"value": 99,
7+
"accepted_values": [0, 5, 10]
8+
},
9+
"config2": {
10+
"help": "The default value should fail as it is not in the range of accepted values",
11+
"value": 100,
12+
"value_min": 0,
13+
"value_max": 10
14+
},
15+
"config3": {
16+
"help": "The default value should fail as it is not in the range of accepted values",
17+
"value": 101,
18+
"value_min": 102
19+
},
20+
"config4": {
21+
"help": "The default value should fail as it is not in the range of accepted values",
22+
"value": 102,
23+
"value_max": 101
24+
},
25+
"config5": {
26+
"help": "The default value should fail as it specified both a range and list of accepted values",
27+
"value": 103,
28+
"value_max": 104,
29+
"accepted_values": ["103"]
30+
},
31+
"config6": {
32+
"help": "The default value should fail as it is not in the range of accepted values",
33+
"value": "0x1000",
34+
"value_max": "0x500"
35+
},
36+
"config7": {
37+
"help": "The default value should fail as it is a non-decimal string with a max value",
38+
"value": "test",
39+
"value_max": "?"
40+
}
41+
}
42+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test_target": {
3+
"core": "Cortex-M0",
4+
"extra_labels": [],
5+
"features": [],
6+
"default_lib": "std",
7+
"supported_toolchains": ["GCC_ARM"]
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"test_target": {
3+
"exception_msg": "\nInvalid value for lib1.config1 = 99 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not an accepted value: 0, 5, 10\nInvalid config range for lib1.config2 = 100 (macro name: \"MBED_CONF_LIB1_CONFIG2\"), is not in the required range: [0:10]\nInvalid config range for lib1.config3 = 101 (macro name: \"MBED_CONF_LIB1_CONFIG3\"), is not in the required range: [102:inf]\nInvalid config range for lib1.config4 = 102 (macro name: \"MBED_CONF_LIB1_CONFIG4\"), is not in the required range: [-inf:101]\nlib1.config5 = 103 (macro name: \"MBED_CONF_LIB1_CONFIG5\") has both a range and list of accepted values specified. Please only specify either value_min and/or value_max, or accepted_values\nInvalid config range for lib1.config6 = 0x1000 (macro name: \"MBED_CONF_LIB1_CONFIG6\"), is not in the required range: [-inf:1280]\nInvalid config range settings for lib1.config7 = test (macro name: \"MBED_CONF_LIB1_CONFIG7\"). Range specifiers are not applicable to non-decimal/hexadecimal string values"
4+
}
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "lib1",
3+
"config": {
4+
"config1": {
5+
"help": "The default value should pass, but will be overridden out of bounds and should error",
6+
"value": 8,
7+
"value_min": 0,
8+
"value_max": 10
9+
}
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"target_overrides": {
3+
"test_target": {
4+
"lib1.config1": 12
5+
}
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test_target": {
3+
"core": "Cortex-M0",
4+
"extra_labels": [],
5+
"features": [],
6+
"default_lib": "std",
7+
"supported_toolchains": ["GCC_ARM"]
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"test_target": {
3+
"lib1.config1": 12,
4+
"exception_msg": "Invalid config range for lib1.config1 = 12 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not in the required range: [0:10]"
5+
}
6+
}

0 commit comments

Comments
 (0)