core: allow NULL default_vals for output requests
authorBartosz Golaszewski <bartekgola@gmail.com>
Mon, 19 Feb 2018 10:32:41 +0000 (11:32 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Mon, 19 Feb 2018 10:32:41 +0000 (11:32 +0100)
Explicitly allow default_vals to be NULL for output requests, in which
case the actual default values passed to the kernel are set to 0.

Add a test case that verifies that this works correctly.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
src/lib/core.c
tests/tests-line.c

index fa276f68531d14c94c1ec29e905ec25730e5935b..22f247dc88b9231e5cb3a3a7d1c70a193bb381ec 100644 (file)
@@ -368,7 +368,9 @@ static int line_request_values(struct gpiod_line_bulk *bulk,
 
        gpiod_line_bulk_foreach_line_off(bulk, line, i) {
                req.lineoffsets[i] = gpiod_line_offset(line);
-               if (config->request_type == GPIOD_LINE_REQUEST_DIRECTION_OUTPUT)
+               if (config->request_type ==
+                               GPIOD_LINE_REQUEST_DIRECTION_OUTPUT &&
+                   default_vals)
                        req.default_values[i] = !!default_vals[i];
        }
 
index 5c1397f5ee1de79a87b9d8a7f25e64a9a695ca64..bae3451801a22cc4e8acc7e2992fc9e0883a2f38 100644 (file)
@@ -247,6 +247,44 @@ TEST_DEFINE(line_request_bulk_different_chips,
            "gpiod_line_request_bulk() - different chips",
            0, { 8, 8 });
 
+static void line_request_null_default_vals_for_output(void)
+{
+       TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL;
+       struct gpiod_line_bulk bulk = GPIOD_LINE_BULK_INITIALIZER;
+       struct gpiod_line *line;
+       int rv, vals[3];
+
+       chip = gpiod_chip_open(test_chip_path(0));
+       TEST_ASSERT_NOT_NULL(chip);
+
+       line = gpiod_chip_get_line(chip, 0);
+       gpiod_line_bulk_add(&bulk, line);
+       line = gpiod_chip_get_line(chip, 1);
+       gpiod_line_bulk_add(&bulk, line);
+       line = gpiod_chip_get_line(chip, 2);
+       gpiod_line_bulk_add(&bulk, line);
+
+       rv = gpiod_line_request_bulk_output(&bulk, TEST_CONSUMER, NULL);
+       TEST_ASSERT_RET_OK(rv);
+
+       gpiod_line_release_bulk(&bulk);
+
+       rv = gpiod_line_request_bulk_input(&bulk, TEST_CONSUMER);
+       TEST_ASSERT_RET_OK(rv);
+
+       memset(vals, 0, sizeof(vals));
+
+       rv = gpiod_line_get_value_bulk(&bulk, vals);
+       TEST_ASSERT_RET_OK(rv);
+
+       TEST_ASSERT_EQ(vals[0], 0);
+       TEST_ASSERT_EQ(vals[1], 0);
+       TEST_ASSERT_EQ(vals[2], 0);
+}
+TEST_DEFINE(line_request_null_default_vals_for_output,
+           "gpiod_line_request_bulk() - null default vals for output",
+           0, { 8 });
+
 static void line_set_value(void)
 {
        TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL;