| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <glib.h> |
| 5 |
#include <gpiod.h> |
| 6 |
#include <poll.h> |
| 7 |
#include <gpiod-test.h> |
| 8 |
#include <gpiod-test-common.h> |
| 9 |
#include <gpiosim-glib.h> |
| 10 |
|
| 11 |
#include "helpers.h" |
| 12 |
|
| 13 |
#define GPIOD_TEST_GROUP "kernel-uapi" |
| 14 |
|
| 15 |
static gpointer falling_and_rising_edge_events(gpointer data) |
| 16 |
{ |
| 17 |
GPIOSimChip *sim = data; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
g_usleep(50000); |
| 24 |
|
| 25 |
g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP); |
| 26 |
|
| 27 |
g_usleep(50000); |
| 28 |
|
| 29 |
g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN); |
| 30 |
|
| 31 |
return NULL; |
| 32 |
} |
| 33 |
|
| 34 |
GPIOD_TEST_CASE(enable_debounce_then_edge_detection) |
| 35 |
{ |
| 36 |
static const guint offset = 2; |
| 37 |
|
| 38 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 39 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 40 |
g_autoptr(struct_gpiod_line_settings) settings = NULL; |
| 41 |
g_autoptr(struct_gpiod_line_config) line_cfg = NULL; |
| 42 |
g_autoptr(struct_gpiod_line_request) request = NULL; |
| 43 |
g_autoptr(GThread) thread = NULL; |
| 44 |
g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL; |
| 45 |
struct gpiod_edge_event *event; |
| 46 |
guint64 ts_rising, ts_falling; |
| 47 |
gint ret; |
| 48 |
|
| 49 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 50 |
settings = gpiod_test_create_line_settings_or_fail(); |
| 51 |
line_cfg = gpiod_test_create_line_config_or_fail(); |
| 52 |
buffer = gpiod_test_create_edge_event_buffer_or_fail(64); |
| 53 |
|
| 54 |
gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT); |
| 55 |
gpiod_line_settings_set_debounce_period_us(settings, 10); |
| 56 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 57 |
settings); |
| 58 |
request = gpiod_test_chip_request_lines_or_fail(chip, NULL, line_cfg); |
| 59 |
|
| 60 |
gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH); |
| 61 |
gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1, |
| 62 |
settings); |
| 63 |
gpiod_test_line_request_reconfigure_lines_or_fail(request, line_cfg); |
| 64 |
|
| 65 |
thread = g_thread_new("request-release", |
| 66 |
falling_and_rising_edge_events, sim); |
| 67 |
g_thread_ref(thread); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
ret = gpiod_line_request_wait_edge_events(request, 1000000000); |
| 72 |
g_assert_cmpint(ret, >, 0); |
| 73 |
gpiod_test_join_thread_and_return_if_failed(thread); |
| 74 |
|
| 75 |
ret = gpiod_line_request_read_edge_events(request, buffer, 1); |
| 76 |
g_assert_cmpint(ret, ==, 1); |
| 77 |
gpiod_test_join_thread_and_return_if_failed(thread); |
| 78 |
|
| 79 |
g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1); |
| 80 |
event = gpiod_edge_event_buffer_get_event(buffer, 0); |
| 81 |
g_assert_nonnull(event); |
| 82 |
gpiod_test_join_thread_and_return_if_failed(thread); |
| 83 |
|
| 84 |
g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==, |
| 85 |
GPIOD_EDGE_EVENT_RISING_EDGE); |
| 86 |
g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2); |
| 87 |
ts_rising = gpiod_edge_event_get_timestamp_ns(event); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
ret = gpiod_line_request_wait_edge_events(request, 1000000000); |
| 92 |
g_assert_cmpint(ret, >, 0); |
| 93 |
gpiod_test_join_thread_and_return_if_failed(thread); |
| 94 |
|
| 95 |
ret = gpiod_line_request_read_edge_events(request, buffer, 1); |
| 96 |
g_assert_cmpint(ret, ==, 1); |
| 97 |
gpiod_test_join_thread_and_return_if_failed(thread); |
| 98 |
|
| 99 |
g_assert_cmpuint(gpiod_edge_event_buffer_get_num_events(buffer), ==, 1); |
| 100 |
event = gpiod_edge_event_buffer_get_event(buffer, 0); |
| 101 |
g_assert_nonnull(event); |
| 102 |
gpiod_test_join_thread_and_return_if_failed(thread); |
| 103 |
|
| 104 |
g_assert_cmpint(gpiod_edge_event_get_event_type(event), ==, |
| 105 |
GPIOD_EDGE_EVENT_FALLING_EDGE); |
| 106 |
g_assert_cmpuint(gpiod_edge_event_get_line_offset(event), ==, 2); |
| 107 |
ts_falling = gpiod_edge_event_get_timestamp_ns(event); |
| 108 |
|
| 109 |
g_thread_join(thread); |
| 110 |
|
| 111 |
g_assert_cmpuint(ts_falling, >, ts_rising); |
| 112 |
} |