| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gio/gio.h> |
| 5 |
|
| 6 |
#include "internal.h" |
| 7 |
|
| 8 |
static const gsize event_buf_size = 64; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
struct _GpiodglibLineRequest { |
| 16 |
GObject parent_instance; |
| 17 |
struct gpiod_line_request *handle; |
| 18 |
struct gpiod_edge_event_buffer *event_buf; |
| 19 |
GSource *edge_event_src; |
| 20 |
guint edge_event_src_id; |
| 21 |
enum gpiod_line_value *val_buf; |
| 22 |
gboolean released; |
| 23 |
}; |
| 24 |
|
| 25 |
typedef enum { |
| 26 |
GPIODGLIB_LINE_REQUEST_PROP_CHIP_NAME = 1, |
| 27 |
GPIODGLIB_LINE_REQUEST_PROP_REQUESTED_OFFSETS, |
| 28 |
} GpiodglibLineRequestProp; |
| 29 |
|
| 30 |
enum { |
| 31 |
GPIODGLIB_LINE_REQUEST_SIGNAL_EDGE_EVENT, |
| 32 |
GPIODGLIB_LINE_REQUEST_SIGNAL_LAST, |
| 33 |
}; |
| 34 |
|
| 35 |
static guint signals[GPIODGLIB_LINE_REQUEST_SIGNAL_LAST]; |
| 36 |
|
| 37 |
G_DEFINE_TYPE(GpiodglibLineRequest, gpiodglib_line_request, G_TYPE_OBJECT); |
| 38 |
|
| 39 |
static gboolean |
| 40 |
gpiodglib_line_request_on_edge_event(GIOChannel *source G_GNUC_UNUSED, |
| 41 |
GIOCondition condition G_GNUC_UNUSED, |
| 42 |
gpointer data) |
| 43 |
{ |
| 44 |
struct gpiod_edge_event *event_handle, *event_copy; |
| 45 |
GpiodglibLineRequest *self = data; |
| 46 |
gint ret, i; |
| 47 |
|
| 48 |
ret = gpiod_line_request_read_edge_events(self->handle, |
| 49 |
self->event_buf, |
| 50 |
event_buf_size); |
| 51 |
if (ret < 0) |
| 52 |
return TRUE; |
| 53 |
|
| 54 |
for (i = 0; i < ret; i++) { |
| 55 |
g_autoptr(GpiodglibEdgeEvent) event = NULL; |
| 56 |
|
| 57 |
event_handle = gpiod_edge_event_buffer_get_event( |
| 58 |
self->event_buf, i); |
| 59 |
event_copy = gpiod_edge_event_copy(event_handle); |
| 60 |
if (!event_copy) |
| 61 |
g_error("failed to copy the edge event"); |
| 62 |
|
| 63 |
event = _gpiodglib_edge_event_new(event_copy); |
| 64 |
|
| 65 |
g_signal_emit(self, |
| 66 |
signals[GPIODGLIB_LINE_REQUEST_SIGNAL_EDGE_EVENT], |
| 67 |
0, |
| 68 |
event); |
| 69 |
} |
| 70 |
|
| 71 |
return TRUE; |
| 72 |
} |
| 73 |
|
| 74 |
static void gpiodglib_line_request_get_property(GObject *obj, guint prop_id, |
| 75 |
GValue *val, GParamSpec *pspec) |
| 76 |
{ |
| 77 |
GpiodglibLineRequest *self = GPIODGLIB_LINE_REQUEST_OBJ(obj); |
| 78 |
g_autofree guint *offsets = NULL; |
| 79 |
g_autoptr(GArray) boxed = NULL; |
| 80 |
gsize num_offsets; |
| 81 |
|
| 82 |
g_assert(self->handle); |
| 83 |
|
| 84 |
switch ((GpiodglibLineRequestProp)prop_id) { |
| 85 |
case GPIODGLIB_LINE_REQUEST_PROP_CHIP_NAME: |
| 86 |
if (gpiodglib_line_request_is_released(self)) |
| 87 |
g_value_set_static_string(val, NULL); |
| 88 |
else |
| 89 |
g_value_set_string(val, |
| 90 |
gpiod_line_request_get_chip_name(self->handle)); |
| 91 |
break; |
| 92 |
case GPIODGLIB_LINE_REQUEST_PROP_REQUESTED_OFFSETS: |
| 93 |
boxed = g_array_new(FALSE, TRUE, sizeof(guint)); |
| 94 |
|
| 95 |
if (!gpiodglib_line_request_is_released(self)) { |
| 96 |
num_offsets = |
| 97 |
gpiod_line_request_get_num_requested_lines( |
| 98 |
self->handle); |
| 99 |
offsets = g_malloc0(num_offsets * sizeof(guint)); |
| 100 |
gpiod_line_request_get_requested_offsets(self->handle, |
| 101 |
offsets, |
| 102 |
num_offsets); |
| 103 |
g_array_append_vals(boxed, offsets, num_offsets); |
| 104 |
} |
| 105 |
|
| 106 |
g_value_set_boxed(val, boxed); |
| 107 |
break; |
| 108 |
default: |
| 109 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
static void gpiodglib_line_request_dispose(GObject *obj) |
| 114 |
{ |
| 115 |
GpiodglibLineRequest *self = GPIODGLIB_LINE_REQUEST_OBJ(obj); |
| 116 |
|
| 117 |
if (self->edge_event_src_id) |
| 118 |
g_source_remove(self->edge_event_src_id); |
| 119 |
|
| 120 |
G_OBJECT_CLASS(gpiodglib_line_request_parent_class)->dispose(obj); |
| 121 |
} |
| 122 |
|
| 123 |
static void gpiodglib_line_request_finalize(GObject *obj) |
| 124 |
{ |
| 125 |
GpiodglibLineRequest *self = GPIODGLIB_LINE_REQUEST_OBJ(obj); |
| 126 |
|
| 127 |
if (!self->released) |
| 128 |
gpiodglib_line_request_release(self); |
| 129 |
|
| 130 |
g_clear_pointer(&self->event_buf, gpiod_edge_event_buffer_free); |
| 131 |
g_clear_pointer(&self->val_buf, g_free); |
| 132 |
|
| 133 |
G_OBJECT_CLASS(gpiodglib_line_request_parent_class)->finalize(obj); |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
gpiodglib_line_request_class_init(GpiodglibLineRequestClass *line_request_class) |
| 138 |
{ |
| 139 |
GObjectClass *class = G_OBJECT_CLASS(line_request_class); |
| 140 |
|
| 141 |
class->get_property = gpiodglib_line_request_get_property; |
| 142 |
class->dispose = gpiodglib_line_request_dispose; |
| 143 |
class->finalize = gpiodglib_line_request_finalize; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
g_object_class_install_property(class, |
| 151 |
GPIODGLIB_LINE_REQUEST_PROP_CHIP_NAME, |
| 152 |
g_param_spec_string("chip-name", "Chip Name", |
| 153 |
"Name of the GPIO chip this request was made on.", |
| 154 |
NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
g_object_class_install_property(class, |
| 162 |
GPIODGLIB_LINE_REQUEST_PROP_REQUESTED_OFFSETS, |
| 163 |
g_param_spec_boxed("requested-offsets", "Requested offsets", |
| 164 |
"Array of requested offsets.", |
| 165 |
G_TYPE_ARRAY, |
| 166 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
signals[GPIODGLIB_LINE_REQUEST_SIGNAL_EDGE_EVENT] = |
| 177 |
g_signal_new("edge-event", |
| 178 |
G_TYPE_FROM_CLASS(line_request_class), |
| 179 |
G_SIGNAL_RUN_LAST, |
| 180 |
0, |
| 181 |
NULL, |
| 182 |
NULL, |
| 183 |
g_cclosure_marshal_generic, |
| 184 |
G_TYPE_NONE, |
| 185 |
1, |
| 186 |
GPIODGLIB_EDGE_EVENT_TYPE); |
| 187 |
} |
| 188 |
|
| 189 |
static void gpiodglib_line_request_init(GpiodglibLineRequest *self) |
| 190 |
{ |
| 191 |
self->handle = NULL; |
| 192 |
self->event_buf = NULL; |
| 193 |
self->edge_event_src = NULL; |
| 194 |
self->released = FALSE; |
| 195 |
} |
| 196 |
|
| 197 |
void gpiodglib_line_request_release(GpiodglibLineRequest *self) |
| 198 |
{ |
| 199 |
g_assert(self); |
| 200 |
|
| 201 |
g_clear_pointer(&self->edge_event_src, g_source_unref); |
| 202 |
gpiod_line_request_release(self->handle); |
| 203 |
self->released = TRUE; |
| 204 |
} |
| 205 |
|
| 206 |
gboolean gpiodglib_line_request_is_released(GpiodglibLineRequest *self) |
| 207 |
{ |
| 208 |
g_assert(self); |
| 209 |
|
| 210 |
return self->released; |
| 211 |
} |
| 212 |
|
| 213 |
static void set_err_request_released(GError **err) |
| 214 |
{ |
| 215 |
g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_REQUEST_RELEASED, |
| 216 |
"line request was released and cannot be used"); |
| 217 |
} |
| 218 |
|
| 219 |
gchar *gpiodglib_line_request_dup_chip_name(GpiodglibLineRequest *self) |
| 220 |
{ |
| 221 |
return _gpiodglib_dup_prop_string(G_OBJECT(self), "chip-name"); |
| 222 |
} |
| 223 |
|
| 224 |
GArray *gpiodglib_line_request_get_requested_offsets(GpiodglibLineRequest *self) |
| 225 |
{ |
| 226 |
return _gpiodglib_get_prop_boxed_array(G_OBJECT(self), |
| 227 |
"requested-offsets"); |
| 228 |
} |
| 229 |
|
| 230 |
gboolean gpiodglib_line_request_reconfigure_lines(GpiodglibLineRequest *self, |
| 231 |
GpiodglibLineConfig *config, |
| 232 |
GError **err) |
| 233 |
{ |
| 234 |
struct gpiod_line_config *config_handle; |
| 235 |
gint ret; |
| 236 |
|
| 237 |
g_assert(self && self->handle); |
| 238 |
|
| 239 |
if (gpiodglib_line_request_is_released(self)) { |
| 240 |
set_err_request_released(err); |
| 241 |
return FALSE; |
| 242 |
} |
| 243 |
|
| 244 |
if (!config) { |
| 245 |
g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL, |
| 246 |
"line-config is required to reconfigure lines"); |
| 247 |
return FALSE; |
| 248 |
} |
| 249 |
|
| 250 |
config_handle = _gpiodglib_line_config_get_handle(config); |
| 251 |
|
| 252 |
ret = gpiod_line_request_reconfigure_lines(self->handle, config_handle); |
| 253 |
if (ret) { |
| 254 |
_gpiodglib_set_error_from_errno(err, |
| 255 |
"failed to reconfigure lines"); |
| 256 |
return FALSE; |
| 257 |
} |
| 258 |
|
| 259 |
return TRUE; |
| 260 |
} |
| 261 |
|
| 262 |
gboolean |
| 263 |
gpiodglib_line_request_get_value(GpiodglibLineRequest *self, guint offset, |
| 264 |
GpiodglibLineValue *value, GError **err) |
| 265 |
{ |
| 266 |
enum gpiod_line_value val; |
| 267 |
|
| 268 |
g_assert(self && self->handle); |
| 269 |
|
| 270 |
if (gpiodglib_line_request_is_released(self)) { |
| 271 |
set_err_request_released(err); |
| 272 |
return FALSE; |
| 273 |
} |
| 274 |
|
| 275 |
val = gpiod_line_request_get_value(self->handle, offset); |
| 276 |
if (val == GPIOD_LINE_VALUE_ERROR) { |
| 277 |
_gpiodglib_set_error_from_errno(err, |
| 278 |
"failed to get line value for offset %u", offset); |
| 279 |
return FALSE; |
| 280 |
} |
| 281 |
|
| 282 |
*value = _gpiodglib_line_value_from_library(val); |
| 283 |
return TRUE; |
| 284 |
} |
| 285 |
|
| 286 |
gboolean gpiodglib_line_request_get_values_subset(GpiodglibLineRequest *self, |
| 287 |
const GArray *offsets, |
| 288 |
GArray **values, GError **err) |
| 289 |
{ |
| 290 |
guint i; |
| 291 |
int ret; |
| 292 |
|
| 293 |
g_assert(self && self->handle); |
| 294 |
|
| 295 |
if (gpiodglib_line_request_is_released(self)) { |
| 296 |
set_err_request_released(err); |
| 297 |
return FALSE; |
| 298 |
} |
| 299 |
|
| 300 |
if (!offsets || !values) { |
| 301 |
g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL, |
| 302 |
"offsets and values must not be NULL"); |
| 303 |
return FALSE; |
| 304 |
} |
| 305 |
|
| 306 |
ret = gpiod_line_request_get_values_subset(self->handle, offsets->len, |
| 307 |
(const unsigned int *)offsets->data, |
| 308 |
self->val_buf); |
| 309 |
if (ret) { |
| 310 |
_gpiodglib_set_error_from_errno(err, "failed to read line values"); |
| 311 |
return FALSE; |
| 312 |
} |
| 313 |
|
| 314 |
if (!(*values)) { |
| 315 |
*values = g_array_sized_new(FALSE, TRUE, |
| 316 |
sizeof(GpiodglibLineValue), |
| 317 |
offsets->len); |
| 318 |
} |
| 319 |
|
| 320 |
g_array_set_size(*values, offsets->len); |
| 321 |
|
| 322 |
for (i = 0; i < offsets->len; i++) { |
| 323 |
GpiodglibLineValue *val = &g_array_index(*values, |
| 324 |
GpiodglibLineValue, i); |
| 325 |
*val = _gpiodglib_line_value_from_library(self->val_buf[i]); |
| 326 |
} |
| 327 |
|
| 328 |
return TRUE; |
| 329 |
} |
| 330 |
|
| 331 |
gboolean gpiodglib_line_request_get_values(GpiodglibLineRequest *self, |
| 332 |
GArray **values, GError **err) |
| 333 |
{ |
| 334 |
g_autoptr(GArray) offsets = NULL; |
| 335 |
|
| 336 |
offsets = gpiodglib_line_request_get_requested_offsets(self); |
| 337 |
|
| 338 |
return gpiodglib_line_request_get_values_subset(self, offsets, |
| 339 |
values, err); |
| 340 |
} |
| 341 |
|
| 342 |
gboolean gpiodglib_line_request_set_value(GpiodglibLineRequest *self, |
| 343 |
guint offset, |
| 344 |
GpiodglibLineValue value, |
| 345 |
GError **err) |
| 346 |
{ |
| 347 |
int ret; |
| 348 |
|
| 349 |
g_assert(self && self->handle); |
| 350 |
|
| 351 |
if (gpiodglib_line_request_is_released(self)) { |
| 352 |
set_err_request_released(err); |
| 353 |
return FALSE; |
| 354 |
} |
| 355 |
|
| 356 |
ret = gpiod_line_request_set_value(self->handle, offset, |
| 357 |
_gpiodglib_line_value_to_library(value)); |
| 358 |
if (ret) { |
| 359 |
_gpiodglib_set_error_from_errno(err, |
| 360 |
"failed to set line value for offset: %u", offset); |
| 361 |
return FALSE; |
| 362 |
} |
| 363 |
|
| 364 |
return TRUE; |
| 365 |
} |
| 366 |
|
| 367 |
gboolean gpiodglib_line_request_set_values_subset(GpiodglibLineRequest *self, |
| 368 |
const GArray *offsets, |
| 369 |
const GArray *values, |
| 370 |
GError **err) |
| 371 |
{ |
| 372 |
guint i; |
| 373 |
int ret; |
| 374 |
|
| 375 |
g_assert(self && self->handle); |
| 376 |
|
| 377 |
if (gpiodglib_line_request_is_released(self)) { |
| 378 |
set_err_request_released(err); |
| 379 |
return FALSE; |
| 380 |
} |
| 381 |
|
| 382 |
if (!offsets || !values) { |
| 383 |
g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL, |
| 384 |
"offsets and values must not be NULL"); |
| 385 |
return FALSE; |
| 386 |
} |
| 387 |
|
| 388 |
if (offsets->len != values->len) { |
| 389 |
g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL, |
| 390 |
"offsets and values must have the sme size"); |
| 391 |
return FALSE; |
| 392 |
} |
| 393 |
|
| 394 |
for (i = 0; i < values->len; i++) |
| 395 |
self->val_buf[i] = _gpiodglib_line_value_to_library( |
| 396 |
g_array_index(values, |
| 397 |
GpiodglibLineValue, i)); |
| 398 |
|
| 399 |
ret = gpiod_line_request_set_values_subset(self->handle, |
| 400 |
offsets->len, |
| 401 |
(unsigned int *)offsets->data, |
| 402 |
self->val_buf); |
| 403 |
if (ret) { |
| 404 |
_gpiodglib_set_error_from_errno(err, |
| 405 |
"failed to set line values"); |
| 406 |
return FALSE; |
| 407 |
} |
| 408 |
|
| 409 |
return TRUE; |
| 410 |
} |
| 411 |
|
| 412 |
gboolean gpiodglib_line_request_set_values(GpiodglibLineRequest *self, |
| 413 |
GArray *values, GError **err) |
| 414 |
{ |
| 415 |
g_autoptr(GArray) offsets = NULL; |
| 416 |
|
| 417 |
offsets = gpiodglib_line_request_get_requested_offsets(self); |
| 418 |
|
| 419 |
return gpiodglib_line_request_set_values_subset(self, offsets, |
| 420 |
values, err); |
| 421 |
} |
| 422 |
|
| 423 |
GpiodglibLineRequest * |
| 424 |
_gpiodglib_line_request_new(struct gpiod_line_request *handle) |
| 425 |
{ |
| 426 |
g_autoptr(GIOChannel) channel = NULL; |
| 427 |
GpiodglibLineRequest *req; |
| 428 |
gsize num_lines; |
| 429 |
|
| 430 |
req = GPIODGLIB_LINE_REQUEST_OBJ( |
| 431 |
g_object_new(GPIODGLIB_LINE_REQUEST_TYPE, NULL)); |
| 432 |
req->handle = handle; |
| 433 |
|
| 434 |
req->event_buf = gpiod_edge_event_buffer_new(event_buf_size); |
| 435 |
if (!req->event_buf) |
| 436 |
g_error("failed to allocate the edge event buffer"); |
| 437 |
|
| 438 |
channel = g_io_channel_unix_new( |
| 439 |
gpiod_line_request_get_fd(req->handle)); |
| 440 |
req->edge_event_src = g_io_create_watch(channel, G_IO_IN); |
| 441 |
g_source_set_callback( |
| 442 |
req->edge_event_src, |
| 443 |
G_SOURCE_FUNC(gpiodglib_line_request_on_edge_event), |
| 444 |
req, NULL); |
| 445 |
req->edge_event_src_id = g_source_attach(req->edge_event_src, NULL); |
| 446 |
|
| 447 |
num_lines = gpiod_line_request_get_num_requested_lines(req->handle); |
| 448 |
req->val_buf = g_malloc0(sizeof(enum gpiod_line_value) * num_lines); |
| 449 |
|
| 450 |
|
| 451 |
return req; |
| 452 |
} |