| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <glib.h> |
| 5 |
#include <gpiod.h> |
| 6 |
#include <gpiod-test.h> |
| 7 |
#include <gpiod-test-common.h> |
| 8 |
#include <gpiosim-glib.h> |
| 9 |
|
| 10 |
#include "helpers.h" |
| 11 |
|
| 12 |
#define GPIOD_TEST_GROUP "line-request" |
| 13 |
|
| 14 |
GPIOD_TEST_CASE(request_fails_with_no_offsets) |
| 15 |
{ |
| 16 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 17 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 18 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 19 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 20 |
|
| 21 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 22 |
|
| 23 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 24 |
|
| 25 |
request = gpiod_chip_request_lines(chip, NULL, line_cfg); |
| 26 |
g_assert_null(request); |
| 27 |
gpiod_test_expect_errno(EINVAL); |
| 28 |
} |
| 29 |
|
| 30 |
GPIOD_TEST_CASE(request_fails_with_no_line_config) |
| 31 |
{ |
| 32 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 33 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 34 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 35 |
|
| 36 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 37 |
|
| 38 |
request = gpiod_chip_request_lines(chip, NULL, NULL); |
| 39 |
g_assert_null(request); |
| 40 |
gpiod_test_expect_errno(EINVAL); |
| 41 |
} |
| 42 |
|
| 43 |
GPIOD_TEST_CASE(request_fails_with_offset_out_of_bounds) |
| 44 |
{ |
| 45 |
static const guint offsets[] = { 2, 6 }; |
| 46 |
|
| 47 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 48 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 49 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 50 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 51 |
|
| 52 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 53 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 54 |
|
| 55 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 2, |
| 56 |
NULL); |
| 57 |
|
| 58 |
request = gpiod_chip_request_lines(chip, NULL, line_cfg); |
| 59 |
g_assert_null(request); |
| 60 |
gpiod_test_expect_errno(EINVAL); |
| 61 |
} |
| 62 |
|
| 63 |
GPIOD_TEST_CASE(set_consumer) |
| 64 |
{ |
| 65 |
static const guint offset = 2; |
| 66 |
static const gchar *const consumer = "foobar"; |
| 67 |
|
| 68 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 69 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 70 |
g_autoptr(struct_gpiod_request_config) req_cfg = NULL; |
| 71 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 72 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 73 |
g_autoptr(struct_gpiod_line_info) info = NULL; |
| 74 |
|
| 75 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 76 |
req_cfg = gpiod_test_create_request_config_or_fail(); |
| 77 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 78 |
|
| 79 |
gpiod_request_config_set_consumer(req_cfg, consumer); |
| 80 |
|
| 81 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 82 |
NULL); |
| 83 |
|
| 84 |
request = gpiod_test_chip_request_lines_or_fail(chip, |
| 85 |
req_cfg, line_cfg); |
| 86 |
|
| 87 |
info = gpiod_test_chip_get_line_info_or_fail(chip, offset); |
| 88 |
|
| 89 |
g_assert_true(gpiod_line_info_is_used(info)); |
| 90 |
g_assert_cmpstr(gpiod_line_info_get_consumer(info), ==, consumer); |
| 91 |
} |
| 92 |
|
| 93 |
GPIOD_TEST_CASE(empty_consumer) |
| 94 |
{ |
| 95 |
static const guint offset = 2; |
| 96 |
|
| 97 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 98 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 99 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 100 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 101 |
g_autoptr(struct_gpiod_line_info) info = NULL; |
| 102 |
|
| 103 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 104 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 105 |
|
| 106 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 107 |
NULL); |
| 108 |
|
| 109 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 110 |
|
| 111 |
info = gpiod_test_chip_get_line_info_or_fail(chip, offset); |
| 112 |
|
| 113 |
g_assert_cmpstr(gpiod_line_info_get_consumer(info), ==, "?"); |
| 114 |
} |
| 115 |
|
| 116 |
GPIOD_TEST_CASE(default_output_value) |
| 117 |
{ |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
static const guint offsets[] = { 0, 1, 3, 4 }; |
| 123 |
|
| 124 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 125 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 126 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 127 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 128 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 129 |
guint i; |
| 130 |
|
| 131 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 132 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 133 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 134 |
|
| 135 |
gpiod_line_settings_set_direction(settings, |
| 136 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 137 |
gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE); |
| 138 |
|
| 139 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 4, |
| 140 |
settings); |
| 141 |
|
| 142 |
g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN); |
| 143 |
|
| 144 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 145 |
|
| 146 |
for (i = 0; i < 4; i++) |
| 147 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, offsets[i]), ==, |
| 148 |
G_GPIOSIM_VALUE_ACTIVE); |
| 149 |
|
| 150 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, |
| 151 |
G_GPIOSIM_VALUE_INACTIVE); |
| 152 |
} |
| 153 |
|
| 154 |
GPIOD_TEST_CASE(read_all_values) |
| 155 |
{ |
| 156 |
static const guint offsets[] = { 0, 2, 4, 5, 7 }; |
| 157 |
static const gint pulls[] = { 0, 1, 0, 1, 1 }; |
| 158 |
|
| 159 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 160 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 161 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 162 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 163 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 164 |
enum gpiod_line_value values[5]; |
| 165 |
gint ret; |
| 166 |
guint i; |
| 167 |
|
| 168 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 169 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 170 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 171 |
|
| 172 |
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT); |
| 173 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 5, |
| 174 |
settings); |
| 175 |
|
| 176 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 177 |
|
| 178 |
for (i = 0; i < 5; i++) |
| 179 |
g_gpiosim_chip_set_pull(sim, offsets[i], |
| 180 |
pulls[i] ? G_GPIOSIM_PULL_UP : |
| 181 |
G_GPIOSIM_PULL_DOWN); |
| 182 |
|
| 183 |
ret = gpiod_line_request_get_values(request, values); |
| 184 |
g_assert_cmpint(ret, ==, 0); |
| 185 |
gpiod_test_return_if_failed(); |
| 186 |
|
| 187 |
for (i = 0; i < 5; i++) |
| 188 |
g_assert_cmpint(values[i], ==, pulls[i]); |
| 189 |
} |
| 190 |
|
| 191 |
GPIOD_TEST_CASE(request_multiple_values_but_read_one) |
| 192 |
{ |
| 193 |
static const guint offsets[] = { 0, 2, 4, 5, 7 }; |
| 194 |
static const gint pulls[] = { 0, 1, 0, 1, 1 }; |
| 195 |
|
| 196 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 197 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 198 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 199 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 200 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 201 |
gint ret; |
| 202 |
guint i; |
| 203 |
|
| 204 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 205 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 206 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 207 |
|
| 208 |
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT); |
| 209 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 5, |
| 210 |
settings); |
| 211 |
|
| 212 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 213 |
|
| 214 |
for (i = 0; i < 5; i++) |
| 215 |
g_gpiosim_chip_set_pull(sim, offsets[i], |
| 216 |
pulls[i] ? G_GPIOSIM_PULL_UP : |
| 217 |
G_GPIOSIM_PULL_DOWN); |
| 218 |
|
| 219 |
ret = gpiod_line_request_get_value(request, 5); |
| 220 |
g_assert_cmpint(ret, ==, 1); |
| 221 |
} |
| 222 |
|
| 223 |
GPIOD_TEST_CASE(set_all_values) |
| 224 |
{ |
| 225 |
static const guint offsets[] = { 0, 2, 4, 5, 6 }; |
| 226 |
static const enum gpiod_line_value values[] = { |
| 227 |
GPIOD_LINE_VALUE_ACTIVE, |
| 228 |
GPIOD_LINE_VALUE_INACTIVE, |
| 229 |
GPIOD_LINE_VALUE_ACTIVE, |
| 230 |
GPIOD_LINE_VALUE_ACTIVE, |
| 231 |
GPIOD_LINE_VALUE_ACTIVE |
| 232 |
}; |
| 233 |
static const GPIOSimValue sim_values[] = { |
| 234 |
G_GPIOSIM_VALUE_ACTIVE, |
| 235 |
G_GPIOSIM_VALUE_INACTIVE, |
| 236 |
G_GPIOSIM_VALUE_ACTIVE, |
| 237 |
G_GPIOSIM_VALUE_ACTIVE, |
| 238 |
G_GPIOSIM_VALUE_ACTIVE |
| 239 |
}; |
| 240 |
|
| 241 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 242 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 243 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 244 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 245 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 246 |
gint ret; |
| 247 |
guint i; |
| 248 |
|
| 249 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 250 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 251 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 252 |
|
| 253 |
gpiod_line_settings_set_direction(settings, |
| 254 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 255 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 5, |
| 256 |
settings); |
| 257 |
|
| 258 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 259 |
|
| 260 |
ret = gpiod_line_request_set_values(request, values); |
| 261 |
g_assert_cmpint(ret, ==, 0); |
| 262 |
gpiod_test_return_if_failed(); |
| 263 |
|
| 264 |
for (i = 0; i < 5; i++) |
| 265 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, offsets[i]), ==, |
| 266 |
sim_values[i]); |
| 267 |
} |
| 268 |
|
| 269 |
GPIOD_TEST_CASE(set_values_subset_of_lines) |
| 270 |
{ |
| 271 |
static const guint offsets[] = { 0, 1, 2, 3 }; |
| 272 |
static const guint offsets_to_set[] = { 0, 1, 3 }; |
| 273 |
static const enum gpiod_line_value values[] = { |
| 274 |
GPIOD_LINE_VALUE_ACTIVE, |
| 275 |
GPIOD_LINE_VALUE_INACTIVE, |
| 276 |
GPIOD_LINE_VALUE_ACTIVE |
| 277 |
}; |
| 278 |
|
| 279 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 280 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 281 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 282 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 283 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 284 |
gint ret; |
| 285 |
|
| 286 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 287 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 288 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 289 |
|
| 290 |
gpiod_line_settings_set_direction(settings, |
| 291 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 292 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 4, |
| 293 |
settings); |
| 294 |
|
| 295 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 296 |
|
| 297 |
ret = gpiod_line_request_set_values_subset(request, 3, offsets_to_set, |
| 298 |
values); |
| 299 |
g_assert_cmpint(ret, ==, 0); |
| 300 |
gpiod_test_return_if_failed(); |
| 301 |
|
| 302 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, |
| 303 |
G_GPIOSIM_VALUE_ACTIVE); |
| 304 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, |
| 305 |
G_GPIOSIM_VALUE_INACTIVE); |
| 306 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, |
| 307 |
G_GPIOSIM_VALUE_ACTIVE); |
| 308 |
} |
| 309 |
|
| 310 |
GPIOD_TEST_CASE(set_line_after_requesting) |
| 311 |
{ |
| 312 |
static const guint offsets[] = { 0, 1, 3, 4 }; |
| 313 |
|
| 314 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 315 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 316 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 317 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 318 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 319 |
|
| 320 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 321 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 322 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 323 |
|
| 324 |
gpiod_line_settings_set_direction(settings, |
| 325 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 326 |
gpiod_line_settings_set_output_value(settings, |
| 327 |
GPIOD_LINE_VALUE_INACTIVE); |
| 328 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 4, |
| 329 |
settings); |
| 330 |
|
| 331 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 332 |
|
| 333 |
gpiod_line_request_set_value(request, 1, GPIOD_LINE_VALUE_ACTIVE); |
| 334 |
|
| 335 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, |
| 336 |
G_GPIOSIM_VALUE_INACTIVE); |
| 337 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, |
| 338 |
G_GPIOSIM_VALUE_ACTIVE); |
| 339 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, |
| 340 |
G_GPIOSIM_VALUE_INACTIVE); |
| 341 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 4), ==, |
| 342 |
G_GPIOSIM_VALUE_INACTIVE); |
| 343 |
} |
| 344 |
|
| 345 |
GPIOD_TEST_CASE(request_survives_parent_chip) |
| 346 |
{ |
| 347 |
static const guint offset = 0; |
| 348 |
|
| 349 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 350 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 351 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 352 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 353 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 354 |
gint ret; |
| 355 |
|
| 356 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 357 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 358 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 359 |
|
| 360 |
gpiod_line_settings_set_direction(settings, |
| 361 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 362 |
gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE); |
| 363 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 364 |
settings); |
| 365 |
|
| 366 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 367 |
|
| 368 |
g_assert_cmpint(gpiod_line_request_get_value(request, offset), ==, |
| 369 |
GPIOD_LINE_VALUE_ACTIVE); |
| 370 |
|
| 371 |
gpiod_chip_close(chip); |
| 372 |
chip = NULL; |
| 373 |
|
| 374 |
ret = gpiod_line_request_set_value(request, offset, |
| 375 |
GPIOD_LINE_VALUE_ACTIVE); |
| 376 |
g_assert_cmpint(ret, ==, 0); |
| 377 |
gpiod_test_return_if_failed(); |
| 378 |
|
| 379 |
ret = gpiod_line_request_set_value(request, offset, |
| 380 |
GPIOD_LINE_VALUE_ACTIVE); |
| 381 |
g_assert_cmpint(ret, ==, 0); |
| 382 |
gpiod_test_return_if_failed(); |
| 383 |
} |
| 384 |
|
| 385 |
GPIOD_TEST_CASE(num_lines_and_offsets) |
| 386 |
{ |
| 387 |
static const guint offsets[] = { 0, 1, 2, 3, 7, 8, 11, 14 }; |
| 388 |
|
| 389 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 16, NULL); |
| 390 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 391 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 392 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 393 |
guint read_back[8], i; |
| 394 |
|
| 395 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 396 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 397 |
|
| 398 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 8, |
| 399 |
NULL); |
| 400 |
|
| 401 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 402 |
|
| 403 |
g_assert_cmpuint(gpiod_line_request_get_num_requested_lines(request), |
| 404 |
==, 8); |
| 405 |
gpiod_test_return_if_failed(); |
| 406 |
gpiod_line_request_get_requested_offsets(request, read_back, 8); |
| 407 |
for (i = 0; i < 8; i++) |
| 408 |
g_assert_cmpuint(read_back[i], ==, offsets[i]); |
| 409 |
} |
| 410 |
|
| 411 |
GPIOD_TEST_CASE(active_low_read_value) |
| 412 |
{ |
| 413 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 414 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 415 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 416 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 417 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 418 |
guint offset; |
| 419 |
gint value; |
| 420 |
|
| 421 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 422 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 423 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 424 |
|
| 425 |
gpiod_line_settings_set_active_low(settings, true); |
| 426 |
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT); |
| 427 |
offset = 2; |
| 428 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 429 |
settings); |
| 430 |
gpiod_line_settings_set_direction(settings, |
| 431 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 432 |
gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE); |
| 433 |
offset = 3; |
| 434 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 435 |
settings); |
| 436 |
|
| 437 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 438 |
|
| 439 |
g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN); |
| 440 |
value = gpiod_line_request_get_value(request, 2); |
| 441 |
g_assert_cmpint(value, ==, GPIOD_LINE_VALUE_ACTIVE); |
| 442 |
|
| 443 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, |
| 444 |
G_GPIOSIM_VALUE_INACTIVE); |
| 445 |
} |
| 446 |
|
| 447 |
GPIOD_TEST_CASE(reconfigure_lines) |
| 448 |
{ |
| 449 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 4, NULL); |
| 450 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 451 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 452 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 453 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 454 |
guint offsets[2]; |
| 455 |
gint ret; |
| 456 |
|
| 457 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 458 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 459 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 460 |
|
| 461 |
gpiod_line_settings_set_direction(settings, |
| 462 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 463 |
|
| 464 |
gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE); |
| 465 |
offsets[0] = 0; |
| 466 |
offsets[1] = 2; |
| 467 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 2, |
| 468 |
settings); |
| 469 |
gpiod_line_settings_set_output_value(settings, |
| 470 |
GPIOD_LINE_VALUE_INACTIVE); |
| 471 |
offsets[0] = 1; |
| 472 |
offsets[1] = 3; |
| 473 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 2, |
| 474 |
settings); |
| 475 |
|
| 476 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 477 |
|
| 478 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, |
| 479 |
G_GPIOSIM_VALUE_ACTIVE); |
| 480 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, |
| 481 |
G_GPIOSIM_VALUE_INACTIVE); |
| 482 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, |
| 483 |
G_GPIOSIM_VALUE_ACTIVE); |
| 484 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, |
| 485 |
G_GPIOSIM_VALUE_INACTIVE); |
| 486 |
|
| 487 |
gpiod_line_config_reset(line_cfg); |
| 488 |
|
| 489 |
gpiod_line_settings_set_output_value(settings, |
| 490 |
GPIOD_LINE_VALUE_INACTIVE); |
| 491 |
offsets[0] = 0; |
| 492 |
offsets[1] = 2; |
| 493 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 2, |
| 494 |
settings); |
| 495 |
gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE); |
| 496 |
offsets[0] = 1; |
| 497 |
offsets[1] = 3; |
| 498 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 2, |
| 499 |
settings); |
| 500 |
|
| 501 |
ret = gpiod_line_request_reconfigure_lines(request, line_cfg); |
| 502 |
g_assert_cmpint(ret, ==, 0); |
| 503 |
gpiod_test_return_if_failed(); |
| 504 |
|
| 505 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, |
| 506 |
G_GPIOSIM_VALUE_INACTIVE); |
| 507 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, |
| 508 |
G_GPIOSIM_VALUE_ACTIVE); |
| 509 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, |
| 510 |
G_GPIOSIM_VALUE_INACTIVE); |
| 511 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, |
| 512 |
G_GPIOSIM_VALUE_ACTIVE); |
| 513 |
} |
| 514 |
|
| 515 |
GPIOD_TEST_CASE(reconfigure_lines_null_config) |
| 516 |
{ |
| 517 |
static const guint offsets[] = { 0, 1, 2, 3 }; |
| 518 |
|
| 519 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 520 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 521 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 522 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 523 |
gint ret; |
| 524 |
|
| 525 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 526 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 527 |
|
| 528 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 4, |
| 529 |
NULL); |
| 530 |
|
| 531 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 532 |
|
| 533 |
ret = gpiod_line_request_reconfigure_lines(request, NULL); |
| 534 |
g_assert_cmpint(ret, ==, -1); |
| 535 |
gpiod_test_expect_errno(EINVAL); |
| 536 |
} |
| 537 |
|
| 538 |
GPIOD_TEST_CASE(reconfigure_lines_different_offsets) |
| 539 |
{ |
| 540 |
static const guint offsets0[] = { 0, 1, 2, 3 }; |
| 541 |
static const guint offsets1[] = { 2, 4, 5 }; |
| 542 |
|
| 543 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 544 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 545 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 546 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 547 |
gint ret; |
| 548 |
|
| 549 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 550 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 551 |
|
| 552 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets0, 4, |
| 553 |
NULL); |
| 554 |
|
| 555 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 556 |
|
| 557 |
gpiod_line_config_reset(line_cfg); |
| 558 |
|
| 559 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets1, 3, |
| 560 |
NULL); |
| 561 |
|
| 562 |
ret = gpiod_line_request_reconfigure_lines(request, line_cfg); |
| 563 |
g_assert_cmpint(ret, ==, -1); |
| 564 |
gpiod_test_expect_errno(EINVAL); |
| 565 |
} |
| 566 |
|
| 567 |
GPIOD_TEST_CASE(request_lines_with_unordered_offsets) |
| 568 |
{ |
| 569 |
static const guint offsets[] = { 5, 1, 7, 2, 0, 6 }; |
| 570 |
|
| 571 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 572 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 573 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 574 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 575 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 576 |
enum gpiod_line_value values[4]; |
| 577 |
guint set_offsets[4]; |
| 578 |
gint ret; |
| 579 |
|
| 580 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 581 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 582 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 583 |
|
| 584 |
gpiod_line_settings_set_direction(settings, |
| 585 |
GPIOD_LINE_DIRECTION_OUTPUT); |
| 586 |
gpiod_line_settings_set_output_value(settings, GPIOD_LINE_VALUE_ACTIVE); |
| 587 |
|
| 588 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 6, |
| 589 |
settings); |
| 590 |
|
| 591 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 592 |
|
| 593 |
values[0] = 0; |
| 594 |
values[1] = 1; |
| 595 |
values[2] = 0; |
| 596 |
values[3] = 0; |
| 597 |
set_offsets[0] = 7; |
| 598 |
set_offsets[1] = 1; |
| 599 |
set_offsets[2] = 6; |
| 600 |
set_offsets[3] = 0; |
| 601 |
ret = gpiod_line_request_set_values_subset(request, 4, set_offsets, values); |
| 602 |
g_assert_cmpint(ret, ==, 0); |
| 603 |
gpiod_test_return_if_failed(); |
| 604 |
|
| 605 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 0), ==, |
| 606 |
G_GPIOSIM_VALUE_INACTIVE); |
| 607 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 1), ==, |
| 608 |
G_GPIOSIM_VALUE_ACTIVE); |
| 609 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 2), ==, |
| 610 |
G_GPIOSIM_VALUE_ACTIVE); |
| 611 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 5), ==, |
| 612 |
G_GPIOSIM_VALUE_ACTIVE); |
| 613 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 6), ==, |
| 614 |
G_GPIOSIM_VALUE_INACTIVE); |
| 615 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 7), ==, |
| 616 |
G_GPIOSIM_VALUE_INACTIVE); |
| 617 |
} |
| 618 |
|
| 619 |
GPIOD_TEST_CASE(request_with_bias_set_to_pull_up) |
| 620 |
{ |
| 621 |
static const guint offset = 3; |
| 622 |
|
| 623 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 624 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 625 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 626 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 627 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 628 |
|
| 629 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 630 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 631 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 632 |
|
| 633 |
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT); |
| 634 |
gpiod_line_settings_set_bias(settings, GPIOD_LINE_BIAS_PULL_UP); |
| 635 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 636 |
settings); |
| 637 |
|
| 638 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 639 |
|
| 640 |
g_assert_cmpint(g_gpiosim_chip_get_value(sim, 3), ==, |
| 641 |
G_GPIOSIM_VALUE_ACTIVE); |
| 642 |
} |
| 643 |
|
| 644 |
GPIOD_TEST_CASE(get_requested_offsets_less_and_more) |
| 645 |
{ |
| 646 |
static const guint offsets[] = { 0, 1, 2, 3 }; |
| 647 |
|
| 648 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 649 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 650 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 651 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 652 |
size_t num_retrieved; |
| 653 |
guint retrieved[6]; |
| 654 |
|
| 655 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 656 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 657 |
|
| 658 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, offsets, 4, |
| 659 |
NULL); |
| 660 |
|
| 661 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 662 |
|
| 663 |
num_retrieved = gpiod_line_request_get_requested_offsets(request, |
| 664 |
retrieved, 3); |
| 665 |
|
| 666 |
g_assert_cmpuint(num_retrieved, ==, 3); |
| 667 |
g_assert_cmpuint(retrieved[0], ==, 0); |
| 668 |
g_assert_cmpuint(retrieved[1], ==, 1); |
| 669 |
g_assert_cmpuint(retrieved[2], ==, 2); |
| 670 |
|
| 671 |
memset(retrieved, 0, sizeof(retrieved)); |
| 672 |
|
| 673 |
num_retrieved = gpiod_line_request_get_requested_offsets(request, |
| 674 |
retrieved, 6); |
| 675 |
|
| 676 |
g_assert_cmpuint(num_retrieved, ==, 4); |
| 677 |
g_assert_cmpuint(retrieved[0], ==, 0); |
| 678 |
g_assert_cmpuint(retrieved[1], ==, 1); |
| 679 |
g_assert_cmpuint(retrieved[2], ==, 2); |
| 680 |
g_assert_cmpuint(retrieved[3], ==, 3); |
| 681 |
} |
| 682 |
|
| 683 |
GPIOD_TEST_CASE(get_chip_name) |
| 684 |
{ |
| 685 |
static const guint offset = 4; |
| 686 |
|
| 687 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 688 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 689 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 690 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 691 |
|
| 692 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 693 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 694 |
|
| 695 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 696 |
NULL); |
| 697 |
|
| 698 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 699 |
|
| 700 |
g_assert_cmpstr(g_gpiosim_chip_get_name(sim), ==, |
| 701 |
gpiod_line_request_get_chip_name(request)); |
| 702 |
} |