| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gpiod-glib.h> |
| 5 |
#include <gpiod-test.h> |
| 6 |
#include <gpiod-test-common.h> |
| 7 |
#include <gpiosim-glib.h> |
| 8 |
|
| 9 |
#include "helpers.h" |
| 10 |
|
| 11 |
#define GPIOD_TEST_GROUP "glib/edge-event" |
| 12 |
|
| 13 |
static gpointer falling_and_rising_edge_events(gpointer data) |
| 14 |
{ |
| 15 |
GPIOSimChip *sim = data; |
| 16 |
|
| 17 |
g_usleep(1000); |
| 18 |
|
| 19 |
g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP); |
| 20 |
|
| 21 |
g_usleep(1000); |
| 22 |
|
| 23 |
g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN); |
| 24 |
|
| 25 |
return NULL; |
| 26 |
} |
| 27 |
|
| 28 |
typedef struct { |
| 29 |
gboolean rising; |
| 30 |
gboolean falling; |
| 31 |
gboolean failed; |
| 32 |
guint64 falling_ts; |
| 33 |
guint64 rising_ts; |
| 34 |
guint falling_offset; |
| 35 |
guint rising_offset; |
| 36 |
} EdgeEventCallbackData; |
| 37 |
|
| 38 |
static void on_edge_event(GpiodglibLineRequest *request G_GNUC_UNUSED, |
| 39 |
GpiodglibEdgeEvent *event, gpointer data) |
| 40 |
{ |
| 41 |
EdgeEventCallbackData *cb_data = data; |
| 42 |
|
| 43 |
if (gpiodglib_edge_event_get_event_type(event) == |
| 44 |
GPIODGLIB_EDGE_EVENT_FALLING_EDGE) { |
| 45 |
cb_data->falling = TRUE; |
| 46 |
cb_data->falling_ts = |
| 47 |
gpiodglib_edge_event_get_timestamp_ns(event); |
| 48 |
cb_data->falling_offset = |
| 49 |
gpiodglib_edge_event_get_line_offset(event); |
| 50 |
} else { |
| 51 |
cb_data->rising = TRUE; |
| 52 |
cb_data->rising_ts = |
| 53 |
gpiodglib_edge_event_get_timestamp_ns(event); |
| 54 |
cb_data->rising_offset = |
| 55 |
gpiodglib_edge_event_get_line_offset(event); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static gboolean on_timeout(gpointer data) |
| 60 |
{ |
| 61 |
EdgeEventCallbackData *cb_data = data; |
| 62 |
|
| 63 |
g_test_fail_printf("timeout while waiting for edge events"); |
| 64 |
cb_data->failed = TRUE; |
| 65 |
|
| 66 |
return G_SOURCE_CONTINUE; |
| 67 |
} |
| 68 |
|
| 69 |
GPIOD_TEST_CASE(read_both_events) |
| 70 |
{ |
| 71 |
static const guint offset = 2; |
| 72 |
|
| 73 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 74 |
g_autoptr(GpiodglibChip) chip = NULL; |
| 75 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 76 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 77 |
g_autoptr(GpiodglibLineRequest) request = NULL; |
| 78 |
g_autoptr(GArray) offsets = NULL; |
| 79 |
g_autoptr(GThread) thread = NULL; |
| 80 |
EdgeEventCallbackData cb_data = { }; |
| 81 |
guint timeout_id; |
| 82 |
|
| 83 |
chip = gpiodglib_test_new_chip_or_fail( |
| 84 |
g_gpiosim_chip_get_dev_path(sim)); |
| 85 |
settings = gpiodglib_line_settings_new( |
| 86 |
"direction", GPIODGLIB_LINE_DIRECTION_INPUT, |
| 87 |
"edge-detection", GPIODGLIB_LINE_EDGE_BOTH, NULL); |
| 88 |
config = gpiodglib_line_config_new(); |
| 89 |
offsets = gpiodglib_test_array_from_const(&offset, 1, sizeof(guint)); |
| 90 |
|
| 91 |
gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets, |
| 92 |
settings); |
| 93 |
|
| 94 |
request = gpiodglib_test_chip_request_lines_or_fail(chip, NULL, config); |
| 95 |
|
| 96 |
g_signal_connect(request, "edge-event", |
| 97 |
G_CALLBACK(on_edge_event), &cb_data); |
| 98 |
timeout_id = g_timeout_add_seconds(5, on_timeout, &cb_data); |
| 99 |
|
| 100 |
thread = g_thread_new("rising-falling-edge-events", |
| 101 |
falling_and_rising_edge_events, sim); |
| 102 |
g_thread_ref(thread); |
| 103 |
|
| 104 |
while (!cb_data.failed && (!cb_data.falling || !cb_data.rising)) |
| 105 |
g_main_context_iteration(NULL, TRUE); |
| 106 |
|
| 107 |
g_source_remove(timeout_id); |
| 108 |
g_thread_join(thread); |
| 109 |
|
| 110 |
g_assert_cmpuint(cb_data.falling_ts, >, cb_data.rising_ts); |
| 111 |
g_assert_cmpuint(cb_data.falling_offset, ==, offset); |
| 112 |
g_assert_cmpuint(cb_data.rising_offset, ==, offset); |
| 113 |
} |
| 114 |
|
| 115 |
typedef struct { |
| 116 |
gboolean failed; |
| 117 |
gboolean first; |
| 118 |
gboolean second; |
| 119 |
guint first_offset; |
| 120 |
guint second_offset; |
| 121 |
gulong first_line_seqno; |
| 122 |
gulong second_line_seqno; |
| 123 |
gulong first_global_seqno; |
| 124 |
gulong second_global_seqno; |
| 125 |
} SeqnoCallbackData; |
| 126 |
|
| 127 |
static void on_seqno_edge_event(GpiodglibLineRequest *request G_GNUC_UNUSED, |
| 128 |
GpiodglibEdgeEvent *event, gpointer data) |
| 129 |
{ |
| 130 |
SeqnoCallbackData *cb_data = data; |
| 131 |
|
| 132 |
if (!cb_data->first) { |
| 133 |
cb_data->first_offset = |
| 134 |
gpiodglib_edge_event_get_line_offset(event); |
| 135 |
cb_data->first_line_seqno = |
| 136 |
gpiodglib_edge_event_get_line_seqno(event); |
| 137 |
cb_data->first_global_seqno = |
| 138 |
gpiodglib_edge_event_get_global_seqno(event); |
| 139 |
cb_data->first = TRUE; |
| 140 |
} else { |
| 141 |
cb_data->second_offset = |
| 142 |
gpiodglib_edge_event_get_line_offset(event); |
| 143 |
cb_data->second_line_seqno = |
| 144 |
gpiodglib_edge_event_get_line_seqno(event); |
| 145 |
cb_data->second_global_seqno = |
| 146 |
gpiodglib_edge_event_get_global_seqno(event); |
| 147 |
cb_data->second = TRUE; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
static gpointer rising_edge_events_on_two_offsets(gpointer data) |
| 152 |
{ |
| 153 |
GPIOSimChip *sim = data; |
| 154 |
|
| 155 |
g_usleep(1000); |
| 156 |
|
| 157 |
g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP); |
| 158 |
|
| 159 |
g_usleep(1000); |
| 160 |
|
| 161 |
g_gpiosim_chip_set_pull(sim, 3, G_GPIOSIM_PULL_UP); |
| 162 |
|
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
static gboolean on_seqno_timeout(gpointer data) |
| 167 |
{ |
| 168 |
SeqnoCallbackData *cb_data = data; |
| 169 |
|
| 170 |
g_test_fail_printf("timeout while waiting for edge events"); |
| 171 |
cb_data->failed = TRUE; |
| 172 |
|
| 173 |
return G_SOURCE_CONTINUE; |
| 174 |
} |
| 175 |
|
| 176 |
GPIOD_TEST_CASE(seqno) |
| 177 |
{ |
| 178 |
static const guint offset_vals[] = { 2, 3 }; |
| 179 |
|
| 180 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL); |
| 181 |
g_autoptr(GpiodglibChip) chip = NULL; |
| 182 |
g_autoptr(GpiodglibLineSettings) settings = NULL; |
| 183 |
g_autoptr(GpiodglibLineConfig) config = NULL; |
| 184 |
g_autoptr(GpiodglibLineRequest) request = NULL; |
| 185 |
g_autoptr(GArray) offsets = NULL; |
| 186 |
g_autoptr(GThread) thread = NULL; |
| 187 |
SeqnoCallbackData cb_data = { }; |
| 188 |
guint timeout_id; |
| 189 |
|
| 190 |
chip = gpiodglib_test_new_chip_or_fail( |
| 191 |
g_gpiosim_chip_get_dev_path(sim)); |
| 192 |
settings = gpiodglib_line_settings_new( |
| 193 |
"direction", GPIODGLIB_LINE_DIRECTION_INPUT, |
| 194 |
"edge-detection", GPIODGLIB_LINE_EDGE_BOTH, NULL); |
| 195 |
config = gpiodglib_line_config_new(); |
| 196 |
offsets = gpiodglib_test_array_from_const(offset_vals, 2, |
| 197 |
sizeof(guint)); |
| 198 |
|
| 199 |
gpiodglib_test_line_config_add_line_settings_or_fail(config, offsets, |
| 200 |
settings); |
| 201 |
|
| 202 |
request = gpiodglib_test_chip_request_lines_or_fail(chip, NULL, |
| 203 |
config); |
| 204 |
g_signal_connect(request, "edge-event", |
| 205 |
G_CALLBACK(on_seqno_edge_event), &cb_data); |
| 206 |
|
| 207 |
timeout_id = g_timeout_add_seconds(5, on_seqno_timeout, &cb_data); |
| 208 |
|
| 209 |
thread = g_thread_new("two-rising-edge-events", |
| 210 |
rising_edge_events_on_two_offsets, sim); |
| 211 |
g_thread_ref(thread); |
| 212 |
|
| 213 |
while (!cb_data.failed && (!cb_data.first || !cb_data.second)) |
| 214 |
g_main_context_iteration(NULL, TRUE); |
| 215 |
|
| 216 |
g_source_remove(timeout_id); |
| 217 |
g_thread_join(thread); |
| 218 |
|
| 219 |
g_assert_cmpuint(cb_data.first_offset, ==, 2); |
| 220 |
g_assert_cmpuint(cb_data.second_offset, ==, 3); |
| 221 |
g_assert_cmpuint(cb_data.first_line_seqno, ==, 1); |
| 222 |
g_assert_cmpuint(cb_data.second_line_seqno, ==, 1); |
| 223 |
g_assert_cmpuint(cb_data.first_global_seqno, ==, 1); |
| 224 |
g_assert_cmpuint(cb_data.second_global_seqno, ==, 2); |
| 225 |
} |