From 89fb104747a50d4bda1039f8fe3c78c78526cf4e Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 22 Jan 2020 17:43:40 +1100 Subject: [PATCH] confserver: Standardize and document the handling of hex values Previously, server always sent back "hex" types as JSON integers but would only accept setting them as a JSON string of hex digits. This still works, but also possible to use JSON integers in both directions. Add tests for both representations, add a note in the README about types. --- tools/kconfig_new/README.md | 7 +++++++ tools/kconfig_new/confserver.py | 7 +++++++ tools/kconfig_new/test/confserver/testcases_v2.txt | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/tools/kconfig_new/README.md b/tools/kconfig_new/README.md index b03ffd3272..9240715acf 100644 --- a/tools/kconfig_new/README.md +++ b/tools/kconfig_new/README.md @@ -82,6 +82,13 @@ After a request is processed, a response is printed to stdout similar to this: * `visible` contains any visibility changes, where the visible config symbols have changed. * `values` contains any value changes, where a config symbol value has changed. This may be due to an explicit change (ie the client `set` this value), or a change caused by some other change in the config system. Note that a change which is set by the client may not be reflected exactly the same in the response, due to restrictions on allowed values which are enforced by the config server. Invalid changes are ignored by the config server. +### KConfig Item Types + +* `string` types are represented as JSON strings. +* `bool` and `tristate` types are represented as JSON Booleans, the third `tristate` state is not supported. +* `int` types are represented as JSON integers +* `hex` types are also represented as JSON integers, clients should read the separate metadata file to know if the UI representation is `int` or `hex`. It is possible to set a `hex` item by sending the server a JSON string of hex digits (no prefix) as the value, but the server always sends `hex` values as JSON integers. + ### Error Responses In some cases, a request may lead to an error message. In this case, the error message is printed to stderr but an array of errors is also returned in the `error` key of the response: diff --git a/tools/kconfig_new/confserver.py b/tools/kconfig_new/confserver.py index 92f25c76a9..536f43ed52 100755 --- a/tools/kconfig_new/confserver.py +++ b/tools/kconfig_new/confserver.py @@ -224,6 +224,13 @@ def handle_set(config, error, to_set): sym.set_value(0) else: error.append("Boolean symbol %s only accepts true/false values" % sym.name) + elif sym.type == kconfiglib.HEX: + try: + if not isinstance(val, int): + val = int(val, 16) # input can be a decimal JSON value or a string of hex digits + sym.set_value("%x" % val) + except ValueError: + error.append("Hex symbol %s can accept a decimal integer or a string of hex digits, only") else: sym.set_value(str(val)) print("Set %s" % sym.name) diff --git a/tools/kconfig_new/test/confserver/testcases_v2.txt b/tools/kconfig_new/test/confserver/testcases_v2.txt index 7a735fd1a6..b0ca9397c0 100644 --- a/tools/kconfig_new/test/confserver/testcases_v2.txt +++ b/tools/kconfig_new/test/confserver/testcases_v2.txt @@ -45,3 +45,11 @@ * Enabling submenuconfig item re-shows its children > { "SUBMENU_CONFIG": true } < { "values" : { "SUBMENU_CONFIG_ITEM": true, "SUBMENU_CONFIG" : true }, "visible": { "SUBMENU_CONFIG_ITEM": true } } + +* Read/write hex values as decimal integers +> { "TEST_CONDITIONAL_HEX_RANGES": 140 } +< { "values" : { "TEST_CONDITIONAL_HEX_RANGES" : 140 } } + +* Can write hex values as hex strings, but the result is a decimal integer +> { "TEST_CONDITIONAL_HEX_RANGES": "90" } +< { "values" : { "TEST_CONDITIONAL_HEX_RANGES" : 144 } }