File: helpers.h

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (173 lines) | stat: -rw-r--r-- 5,194 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <bartosz.golaszewski@linaro.org> */

#ifndef __GPIOD_TEST_HELPERS_H__
#define __GPIOD_TEST_HELPERS_H__

#include <glib.h>
#include <gpiod.h>
#include <gpiod-test-common.h>

/*
 * These typedefs are needed to make g_autoptr work - it doesn't accept
 * regular 'struct typename' syntax.
 */

typedef struct gpiod_chip struct_gpiod_chip;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_chip, gpiod_chip_close);

typedef struct gpiod_chip_info struct_gpiod_chip_info;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_chip_info, gpiod_chip_info_free);

typedef struct gpiod_line_info struct_gpiod_line_info;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_info, gpiod_line_info_free);

typedef struct gpiod_info_event struct_gpiod_info_event;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_info_event, gpiod_info_event_free);

typedef struct gpiod_line_config struct_gpiod_line_config;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_config, gpiod_line_config_free);

typedef struct gpiod_line_settings struct_gpiod_line_settings;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_settings,
			      gpiod_line_settings_free);

typedef struct gpiod_request_config struct_gpiod_request_config;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_request_config,
			      gpiod_request_config_free);

typedef struct gpiod_line_request struct_gpiod_line_request;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_line_request,
			      gpiod_line_request_release);

typedef struct gpiod_edge_event struct_gpiod_edge_event;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event, gpiod_edge_event_free);

typedef struct gpiod_edge_event_buffer struct_gpiod_edge_event_buffer;
G_DEFINE_AUTOPTR_CLEANUP_FUNC(struct_gpiod_edge_event_buffer,
			      gpiod_edge_event_buffer_free);

#define gpiod_test_open_chip_or_fail(_path) \
	({ \
		struct gpiod_chip *_chip = gpiod_chip_open(_path); \
		g_assert_nonnull(_chip); \
		gpiod_test_return_if_failed(); \
		_chip; \
	})

#define gpiod_test_chip_get_info_or_fail(_chip) \
	({ \
		struct gpiod_chip_info *_info = gpiod_chip_get_info(_chip); \
		g_assert_nonnull(_info); \
		gpiod_test_return_if_failed(); \
		_info; \
	})

#define gpiod_test_chip_get_line_info_or_fail(_chip, _offset) \
	({ \
		struct gpiod_line_info *_info = \
				gpiod_chip_get_line_info(_chip, _offset); \
		g_assert_nonnull(_info); \
		gpiod_test_return_if_failed(); \
		_info; \
	})

#define gpiod_test_chip_watch_line_info_or_fail(_chip, _offset) \
	({ \
		struct gpiod_line_info *_info = \
				gpiod_chip_watch_line_info(_chip, _offset); \
		g_assert_nonnull(_info); \
		gpiod_test_return_if_failed(); \
		_info; \
	})

#define gpiod_test_create_line_settings_or_fail() \
	({ \
		struct gpiod_line_settings *_settings = \
				gpiod_line_settings_new(); \
		g_assert_nonnull(_settings); \
		gpiod_test_return_if_failed(); \
		_settings; \
	})

#define gpiod_test_create_line_config_or_fail() \
	({ \
		struct gpiod_line_config *_config = \
				gpiod_line_config_new(); \
		g_assert_nonnull(_config); \
		gpiod_test_return_if_failed(); \
		_config; \
	})

#define gpiod_test_create_edge_event_buffer_or_fail(_capacity) \
	({ \
		struct gpiod_edge_event_buffer *_buffer = \
				gpiod_edge_event_buffer_new(_capacity); \
		g_assert_nonnull(_buffer); \
		gpiod_test_return_if_failed(); \
		_buffer; \
	})

#define gpiod_test_line_config_add_line_settings_or_fail(_line_cfg, _offsets, \
							 _num_offsets, \
							 _settings) \
	do { \
		gint _ret = gpiod_line_config_add_line_settings(_line_cfg, \
								_offsets,  \
								_num_offsets, \
								_settings); \
		g_assert_cmpint(_ret, ==, 0); \
		gpiod_test_return_if_failed(); \
	} while (0)

#define gpiod_test_line_config_get_line_settings_or_fail(_line_cfg, _offset) \
	({ \
		struct gpiod_line_settings *_settings = \
			gpiod_line_config_get_line_settings(_line_cfg, \
							    _offset); \
		g_assert_nonnull(_settings); \
		gpiod_test_return_if_failed(); \
		_settings; \
	})

#define gpiod_test_line_config_set_output_values_or_fail(_line_cfg, _values, \
							 _num_values) \
	do { \
		gint _ret = gpiod_line_config_set_output_values(_line_cfg, \
								_values, \
								_num_values); \
		g_assert_cmpint(_ret, ==, 0); \
		gpiod_test_return_if_failed(); \
	} while (0)

#define gpiod_test_create_request_config_or_fail() \
	({ \
		struct gpiod_request_config *_config = \
				gpiod_request_config_new(); \
		g_assert_nonnull(_config); \
		gpiod_test_return_if_failed(); \
		_config; \
	})

#define gpiod_test_chip_request_lines_or_fail(_chip, _req_cfg, _line_cfg) \
	({ \
		struct gpiod_line_request *_request = \
			gpiod_chip_request_lines(_chip, \
						 _req_cfg, _line_cfg); \
		g_assert_nonnull(_request); \
		gpiod_test_return_if_failed(); \
		_request; \
	})

#define gpiod_test_line_request_reconfigure_lines_or_fail(_request, _line_cfg) \
	do { \
		gint _ret = gpiod_line_request_reconfigure_lines(_request, \
								 _line_cfg); \
		g_assert_cmpint(_ret, ==, 0); \
		gpiod_test_return_if_failed(); \
	} while (0)

#define gpiod_test_expect_errno(_expected) \
	g_assert_cmpint((_expected), ==, errno)

#endif /* __GPIOD_TEST_HELPERS_H__ */