core: make line values unsigned
Subsystems (2)
PATCH v2 1/1 core: make line values unsigned
Line values are either 0 or 1, so remove the possibility of a value being negative by changing the type from int to unsigned int, similar to offsets.
The exception is the value returned by gpiod_line_request_get_value() which uses a negative value to indicate an error, as is common practice.
--- include/gpiod.h | 17 +++++++++-------- lib/line-config.c | 6 +++--- lib/line-request.c | 14 +++++++------- tests/tests-line-config.c | 8 ++++---- tests/tests-line-request.c | 21 ++++++++++----------- tools/gpioget.c | 4 ++-- tools/gpioset.c | 4 ++-- 7 files changed, 37 insertions(+), 37 deletions(-)
include/gpiod.h +9 -8
@@ -973,7 +973,7 @@ int gpiod_line_config_get_event_clock_offset(struct gpiod_line_config *config,*/ void gpiod_line_config_set_output_value_default (struct gpiod_line_config *config, -int value); +unsigned int value);/** * @brief Override the output value for a line.
@@ -983,7 +983,8 @@ gpiod_line_config_set_output_value_default(struct gpiod_line_config *config,*/ void gpiod_line_config_set_output_value_override (struct gpiod_line_config *config, -unsigned int offset,int value); +unsigned int offset, +unsigned int value);/** * @brief Override the output values for multiple lines.
@@ -997,7 +998,7 @@ gpiod_line_config_set_output_value_override(struct gpiod_line_config *config,void gpiod_line_config_set_output_values (struct gpiod_line_config *config,size_t num_values,const unsigned int *offsets, -const int *values); +const unsigned int *values);/** * @brief Clear the output value override for a line.
@@ -1237,7 +1238,7 @@ int gpiod_line_request_get_value(struct gpiod_line_request *request,int gpiod_line_request_get_values_subset (struct gpiod_line_request *request,size_t num_values,const unsigned int *offsets, -int *values); +unsigned int *values);/** * @brief Get the values of all requested lines.
@@ -1251,7 +1252,7 @@ int gpiod_line_request_get_values_subset(struct gpiod_line_request *request,* @return 0 on success, -1 on failure. */ int gpiod_line_request_get_values (struct gpiod_line_request *request, -int *values); +unsigned int *values);/** * @brief Set the value of a single requested line.
@@ -1260,7 +1261,7 @@ int gpiod_line_request_get_values(struct gpiod_line_request *request,* @param value Value to set. */ int gpiod_line_request_set_value (struct gpiod_line_request *request, -unsigned int offset,int value); +unsigned int offset,unsigned int value);/** * @brief Set the values of a subset of requested lines.
@@ -1277,7 +1278,7 @@ int gpiod_line_request_set_value(struct gpiod_line_request *request,int gpiod_line_request_set_values_subset (struct gpiod_line_request *request,size_t num_values,const unsigned int *offsets, -const int *values); +const unsigned int *values);/** * @brief Set the values of all lines associated with a request.
@@ -1290,7 +1291,7 @@ int gpiod_line_request_set_values_subset(struct gpiod_line_request *request,* ::gpiod_line_request_get_offsets. */ int gpiod_line_request_set_values (struct gpiod_line_request *request, -const int *values); +const unsigned int *values);/** * @brief Update the configuration of lines associated with a line request.
lib/line-config.c +3 -3
@@ -648,14 +648,14 @@ gpiod_line_config_get_event_clock_offset(struct gpiod_line_config *config, GPIOD_API voidgpiod_line_config_set_output_value_default (struct gpiod_line_config *config, -int value) +unsigned int value) { config->defaults.value = value; } GPIOD_API voidgpiod_line_config_set_output_value_override (struct gpiod_line_config *config, -unsigned int offset,int value) +unsigned int offset,unsigned int value) {struct override_config *override;
@@ -671,7 +671,7 @@ GPIOD_API void gpiod_line_config_set_output_values(struct gpiod_line_config *config, size_t num_values, const unsigned int *offsets, - const int *values) + const unsigned int *values) { size_t i;
lib/line-request.c +7 -7
@@ -60,8 +60,7 @@ gpiod_line_request_get_offsets(struct gpiod_line_request *request, GPIOD_API int gpiod_line_request_get_value(struct gpiod_line_request *request,unsigned int offset) { -unsigned int ret; -int val; +unsigned int ret, val; ret =gpiod_line_request_get_values_subset (request,1 , &offset, &val);if (ret)
@@ -86,7 +85,8 @@ static int offset_to_bit(struct gpiod_line_request *request, GPIOD_API intgpiod_line_request_get_values_subset (struct gpiod_line_request *request,size_t num_values, -const unsigned int *offsets,int *values) +const unsigned int *offsets, +unsigned int *values) {struct gpio_v2_line_values uapi_values;uint64_t mask =0 , bits =0 ;
@@ -123,14 +123,14 @@ gpiod_line_request_get_values_subset(struct gpiod_line_request *request, } GPIOD_API int gpiod_line_request_get_values(struct gpiod_line_request *request, -int *values) +unsigned int *values) {return gpiod_line_request_get_values_subset (request, request->num_lines, request->offsets, values); } GPIOD_API int gpiod_line_request_set_value(struct gpiod_line_request *request, -unsigned int offset,int value) +unsigned int offset,unsigned int value) {return gpiod_line_request_set_values_subset (request,1 , &offset, &value);
@@ -140,7 +140,7 @@ GPIOD_API int gpiod_line_request_set_values_subset(struct gpiod_line_request *request, size_t num_values, const unsigned int *offsets, - const int *values) + const unsigned int *values) { struct gpio_v2_line_values uapi_values;uint64_t mask =0 , bits =0 ;
@@ -166,7 +166,7 @@ gpiod_line_request_set_values_subset(struct gpiod_line_request *request, } GPIOD_API int gpiod_line_request_set_values(struct gpiod_line_request *request, -const int *values) +const unsigned int *values) {return gpiod_line_request_set_values_subset (request, request->num_lines, request->offsets, values);
tests/tests-line-config.c +4 -4
@@ -300,10 +300,10 @@ GPIOD_TEST_CASE(set_and_clear_output_value_override)GPIOD_TEST_CASE (set_multiple_output_values) {static const guint offsets[] = {3 ,4 ,5 ,6 }; -static const gint values[] = {GPIOD_LINE_VALUE_ACTIVE , -GPIOD_LINE_VALUE_INACTIVE , -GPIOD_LINE_VALUE_ACTIVE , -GPIOD_LINE_VALUE_INACTIVE }; +static const guint values[] = {GPIOD_LINE_VALUE_ACTIVE , +GPIOD_LINE_VALUE_INACTIVE , +GPIOD_LINE_VALUE_ACTIVE , +GPIOD_LINE_VALUE_INACTIVE };g_autoptr (struct_gpiod_line_config) config =NULL ;guint overridden_offsets[4 ], i;
tests/tests-line-request.c +10 -11
@@ -200,8 +200,8 @@ GPIOD_TEST_CASE(read_all_values)g_autoptr (struct_gpiod_request_config) req_cfg =NULL ;g_autoptr (struct_gpiod_line_config) line_cfg =NULL ;g_autoptr (struct_gpiod_line_request) request =NULL ; -gint ret, values[5 ]; -guint i; +gint ret; +guint i, values[5 ]; chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); req_cfg =gpiod_test_create_request_config_or_fail ();
@@ -259,11 +259,11 @@ GPIOD_TEST_CASE(request_multiple_values_but_read_one)GPIOD_TEST_CASE (set_all_values) {static const guint offsets[] = {0 ,2 ,4 ,5 ,6 }; -static const gint values[] = {GPIOD_LINE_VALUE_ACTIVE , -GPIOD_LINE_VALUE_INACTIVE , -GPIOD_LINE_VALUE_ACTIVE , -GPIOD_LINE_VALUE_ACTIVE , -GPIOD_LINE_VALUE_ACTIVE }; +static const guint values[] = {GPIOD_LINE_VALUE_ACTIVE , +GPIOD_LINE_VALUE_INACTIVE , +GPIOD_LINE_VALUE_ACTIVE , +GPIOD_LINE_VALUE_ACTIVE , +GPIOD_LINE_VALUE_ACTIVE };g_autoptr (GPIOSimChip) sim =g_gpiosim_chip_new ("num-lines" ,8 ,NULL );g_autoptr (struct_gpiod_chip) chip =NULL ;
@@ -440,8 +440,8 @@ GPIOD_TEST_CASE(reconfigure_lines)g_autoptr (struct_gpiod_request_config) req_cfg =NULL ;g_autoptr (struct_gpiod_line_config) line_cfg =NULL ;g_autoptr (struct_gpiod_line_request) request =NULL ; -gint values[4 ], ret; -guint i; +gint ret; +guint values[4 ], i; chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); req_cfg =gpiod_test_create_request_config_or_fail ();
@@ -487,8 +487,7 @@ GPIOD_TEST_CASE(request_lines_with_unordered_offsets)g_autoptr (struct_gpiod_request_config) req_cfg =NULL ;g_autoptr (struct_gpiod_line_config) line_cfg =NULL ;g_autoptr (struct_gpiod_line_request) request =NULL ; -guint cfg_offsets[4 ]; -gint values[4 ]; +guint cfg_offsets[4 ], values[4 ]; chip =gpiod_test_open_chip_or_fail (g_gpiosim_chip_get_dev_path (sim)); req_cfg =gpiod_test_create_request_config_or_fail ();
tools/gpioget.c +2 -2
@@ -42,13 +42,13 @@ static void print_help(void)int main (int argc,char **argv) {int direction =GPIOD_LINE_DIRECTION_INPUT ; -int optc, opti, bias =0 , ret, *values; +int optc, opti, bias =0 , ret;struct gpiod_request_config *req_cfg;struct gpiod_line_request *request;struct gpiod_line_config *line_cfg;struct gpiod_chip *chip;bool active_low = false; -unsigned int *offsets; +unsigned int *offsets, *values;size_t i, num_lines;char *device, *end;
tools/gpioset.c +2 -2
@@ -190,14 +190,14 @@ static int parse_drive(const char *option)int main (int argc,char **argv) {const struct mode_mapping *mode = &modes[MODE_EXIT ]; -int ret, optc, opti, bias =0 , drive =0 , *values; +int ret, optc, opti, bias =0 , drive =0 ;struct gpiod_request_config *req_cfg;struct gpiod_line_request *request;struct gpiod_line_config *line_cfg;struct callback_data cbdata;struct gpiod_chip *chip;bool active_low = false; -unsigned int *offsets; +unsigned int *offsets, *values;size_t i, num_lines;char *device, *end;
Signature · 1 line
2.35.1
Quoted 10 lines · Kent Gibson
Line values are either 0 or 1, so remove the possibility of a value being negative by changing the type from int to unsigned int, similar to offsets.
The exception is the value returned by
gpiod_line_request_get_value()which uses a negative value to indicate an error, as is common practice.Signed-off-by Kent Gibson <[email protected]>---
I don't see value in this. We now have an enum to represent the logical states in the C API and enum's type in C is int. This change would make it inconsistent with all other line properties for which we use ints and represent their values with enumerations. If anything - we shouldn't accept any value other than 0 and 1 but just like we do with other properties in gpiod_line_config - when we fall-back to a sane default for invalid values - we'd just use INACTIVE for 0 and ACTIVE for everything else.
Bart