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