| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gpiod-glib.h> |
| 5 |
#include <gpiodbus.h> |
| 6 |
#include <gudev/gudev.h> |
| 7 |
|
| 8 |
#include "daemon.h" |
| 9 |
#include "helpers.h" |
| 10 |
|
| 11 |
struct _GpiodbusDaemon { |
| 12 |
GObject parent; |
| 13 |
GDBusConnection *con; |
| 14 |
GUdevClient *udev; |
| 15 |
GDBusObjectManagerServer *chip_manager; |
| 16 |
GDBusObjectManagerServer *request_manager; |
| 17 |
GHashTable *chips; |
| 18 |
GHashTable *requests; |
| 19 |
GTree *req_id_root; |
| 20 |
}; |
| 21 |
|
| 22 |
G_DEFINE_TYPE(GpiodbusDaemon, gpiodbus_daemon, G_TYPE_OBJECT); |
| 23 |
|
| 24 |
typedef struct { |
| 25 |
GpiodglibChip *chip; |
| 26 |
GpiodbusChip *dbus_chip; |
| 27 |
GpiodbusDaemon *daemon; |
| 28 |
GDBusObjectManagerServer *line_manager; |
| 29 |
GHashTable *lines; |
| 30 |
} GpiodbusDaemonChipData; |
| 31 |
|
| 32 |
typedef struct { |
| 33 |
GpiodglibLineRequest *request; |
| 34 |
GpiodbusRequest *dbus_request; |
| 35 |
gint id; |
| 36 |
GpiodbusDaemonChipData *chip_data; |
| 37 |
} GpiodbusDaemonRequestData; |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
GpiodbusLine *dbus_line; |
| 41 |
GpiodbusDaemonChipData *chip_data; |
| 42 |
GpiodbusDaemonRequestData *req_data; |
| 43 |
} GpiodbusDaemonLineData; |
| 44 |
|
| 45 |
static const gchar *const gpiodbus_daemon_udev_subsystems[] = { "gpio", NULL }; |
| 46 |
|
| 47 |
static void gpiodbus_daemon_dispose(GObject *obj) |
| 48 |
{ |
| 49 |
GpiodbusDaemon *self = GPIODBUS_DAEMON(obj); |
| 50 |
|
| 51 |
g_debug("disposing of the GPIO daemon"); |
| 52 |
|
| 53 |
g_clear_pointer(&self->chips, g_hash_table_unref); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
g_clear_pointer(&self->requests, g_hash_table_unref); |
| 60 |
g_clear_pointer(&self->req_id_root, g_tree_destroy); |
| 61 |
g_clear_object(&self->con); |
| 62 |
|
| 63 |
G_OBJECT_CLASS(gpiodbus_daemon_parent_class)->dispose(obj); |
| 64 |
} |
| 65 |
|
| 66 |
static void gpiodbus_daemon_finalize(GObject *obj) |
| 67 |
{ |
| 68 |
GpiodbusDaemon *self = GPIODBUS_DAEMON(obj); |
| 69 |
|
| 70 |
g_debug("finalizing GPIO daemon"); |
| 71 |
|
| 72 |
g_clear_object(&self->request_manager); |
| 73 |
g_clear_object(&self->chip_manager); |
| 74 |
g_clear_object(&self->udev); |
| 75 |
|
| 76 |
G_OBJECT_CLASS(gpiodbus_daemon_parent_class)->finalize(obj); |
| 77 |
} |
| 78 |
|
| 79 |
static void gpiodbus_daemon_class_init(GpiodbusDaemonClass *daemon_class) |
| 80 |
{ |
| 81 |
GObjectClass *class = G_OBJECT_CLASS(daemon_class); |
| 82 |
|
| 83 |
class->dispose = gpiodbus_daemon_dispose; |
| 84 |
class->finalize = gpiodbus_daemon_finalize; |
| 85 |
} |
| 86 |
|
| 87 |
static gboolean |
| 88 |
gpiodbus_remove_request_if_chip_matches(gpointer key G_GNUC_UNUSED, |
| 89 |
gpointer value, gpointer user_data) |
| 90 |
{ |
| 91 |
GpiodbusDaemonChipData *chip_data = user_data; |
| 92 |
GpiodbusDaemonRequestData *req_data = value; |
| 93 |
|
| 94 |
return req_data->chip_data == chip_data; |
| 95 |
} |
| 96 |
|
| 97 |
static void gpiodbus_daemon_chip_data_free(gpointer data) |
| 98 |
{ |
| 99 |
GpiodbusDaemonChipData *chip_data = data; |
| 100 |
const gchar *obj_path; |
| 101 |
|
| 102 |
obj_path = g_dbus_interface_skeleton_get_object_path( |
| 103 |
G_DBUS_INTERFACE_SKELETON(chip_data->dbus_chip)); |
| 104 |
|
| 105 |
g_debug("unexporting object for GPIO chip: '%s'", obj_path); |
| 106 |
|
| 107 |
g_hash_table_foreach_remove(chip_data->daemon->requests, |
| 108 |
gpiodbus_remove_request_if_chip_matches, |
| 109 |
chip_data); |
| 110 |
|
| 111 |
g_dbus_object_manager_server_unexport(chip_data->daemon->chip_manager, |
| 112 |
obj_path); |
| 113 |
|
| 114 |
g_hash_table_unref(chip_data->lines); |
| 115 |
g_object_unref(chip_data->line_manager); |
| 116 |
g_object_unref(chip_data->chip); |
| 117 |
g_object_unref(chip_data->dbus_chip); |
| 118 |
g_free(chip_data); |
| 119 |
} |
| 120 |
|
| 121 |
static void gpiodbus_daemon_line_data_free(gpointer data) |
| 122 |
{ |
| 123 |
GpiodbusDaemonLineData *line_data = data; |
| 124 |
const gchar *obj_path; |
| 125 |
|
| 126 |
obj_path = g_dbus_interface_skeleton_get_object_path( |
| 127 |
G_DBUS_INTERFACE_SKELETON(line_data->dbus_line)); |
| 128 |
|
| 129 |
g_debug("unexporting object for GPIO line: '%s'", |
| 130 |
obj_path); |
| 131 |
|
| 132 |
g_dbus_object_manager_server_unexport( |
| 133 |
line_data->chip_data->line_manager, obj_path); |
| 134 |
|
| 135 |
g_object_unref(line_data->dbus_line); |
| 136 |
g_free(line_data); |
| 137 |
} |
| 138 |
|
| 139 |
static void gpiodbus_lines_set_managed(GpiodbusDaemonRequestData *req_data, |
| 140 |
gboolean managed) |
| 141 |
{ |
| 142 |
g_autoptr(GDBusObject) obj = NULL; |
| 143 |
const gchar *const *line_paths; |
| 144 |
GpiodbusLine *line; |
| 145 |
const gchar *path; |
| 146 |
guint i; |
| 147 |
|
| 148 |
line_paths = gpiodbus_request_get_line_paths(req_data->dbus_request); |
| 149 |
|
| 150 |
for (path = line_paths[0], i = 0; path; path = line_paths[++i]) { |
| 151 |
obj = g_dbus_object_manager_get_object( |
| 152 |
G_DBUS_OBJECT_MANAGER( |
| 153 |
req_data->chip_data->line_manager), path); |
| 154 |
line = gpiodbus_object_peek_line(GPIODBUS_OBJECT(obj)); |
| 155 |
|
| 156 |
g_debug("Setting line %u on chip object '%s' to '%s'", |
| 157 |
gpiodbus_line_get_offset(line), |
| 158 |
g_dbus_interface_skeleton_get_object_path( |
| 159 |
G_DBUS_INTERFACE_SKELETON( |
| 160 |
req_data->chip_data->dbus_chip)), |
| 161 |
managed ? "managed" : "unmanaged"); |
| 162 |
|
| 163 |
gpiodbus_line_set_managed(line, managed); |
| 164 |
gpiodbus_line_set_request_path(line, |
| 165 |
managed ? g_dbus_interface_skeleton_get_object_path( |
| 166 |
G_DBUS_INTERFACE_SKELETON( |
| 167 |
req_data->dbus_request)) : NULL); |
| 168 |
g_dbus_interface_skeleton_flush( |
| 169 |
G_DBUS_INTERFACE_SKELETON(line)); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
static void gpiodbus_daemon_request_data_free(gpointer data) |
| 174 |
{ |
| 175 |
GpiodbusDaemonRequestData *req_data = data; |
| 176 |
const gchar *obj_path; |
| 177 |
|
| 178 |
obj_path = g_dbus_interface_skeleton_get_object_path( |
| 179 |
G_DBUS_INTERFACE_SKELETON(req_data->dbus_request)); |
| 180 |
|
| 181 |
g_debug("unexporting object for GPIO request: '%s'", obj_path); |
| 182 |
|
| 183 |
g_dbus_object_manager_server_unexport( |
| 184 |
req_data->chip_data->daemon->request_manager, obj_path); |
| 185 |
|
| 186 |
gpiodbus_lines_set_managed(req_data, FALSE); |
| 187 |
gpiodbus_id_free(req_data->chip_data->daemon->req_id_root, |
| 188 |
req_data->id); |
| 189 |
g_object_unref(req_data->request); |
| 190 |
g_object_unref(req_data->dbus_request); |
| 191 |
g_free(req_data); |
| 192 |
} |
| 193 |
|
| 194 |
static void gpiodbus_daemon_init(GpiodbusDaemon *self) |
| 195 |
{ |
| 196 |
g_debug("initializing GPIO D-Bus daemon"); |
| 197 |
|
| 198 |
self->con = NULL; |
| 199 |
self->udev = g_udev_client_new(gpiodbus_daemon_udev_subsystems); |
| 200 |
self->chip_manager = |
| 201 |
g_dbus_object_manager_server_new("/io/gpiod1/chips"); |
| 202 |
self->request_manager = |
| 203 |
g_dbus_object_manager_server_new("/io/gpiod1/requests"); |
| 204 |
self->chips = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
| 205 |
gpiodbus_daemon_chip_data_free); |
| 206 |
self->requests = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
| 207 |
gpiodbus_daemon_request_data_free); |
| 208 |
self->req_id_root = g_tree_new_full(gpiodbus_id_cmp, NULL, |
| 209 |
g_free, NULL); |
| 210 |
} |
| 211 |
|
| 212 |
GpiodbusDaemon *gpiodbus_daemon_new(void) |
| 213 |
{ |
| 214 |
return GPIODBUS_DAEMON(g_object_new(GPIODBUS_DAEMON_TYPE, NULL)); |
| 215 |
} |
| 216 |
|
| 217 |
static void gpiodbus_daemon_on_info_event(GpiodglibChip *chip G_GNUC_UNUSED, |
| 218 |
GpiodglibInfoEvent *event, |
| 219 |
gpointer data) |
| 220 |
{ |
| 221 |
GpiodbusDaemonChipData *chip_data = data; |
| 222 |
g_autoptr(GpiodglibLineInfo) info = NULL; |
| 223 |
GpiodbusDaemonLineData *line_data; |
| 224 |
guint offset; |
| 225 |
|
| 226 |
info = gpiodglib_info_event_get_line_info(event); |
| 227 |
offset = gpiodglib_line_info_get_offset(info); |
| 228 |
|
| 229 |
g_debug("line info event received for offset %u on chip '%s'", |
| 230 |
offset, |
| 231 |
g_dbus_interface_skeleton_get_object_path( |
| 232 |
G_DBUS_INTERFACE_SKELETON(chip_data->dbus_chip))); |
| 233 |
|
| 234 |
line_data = g_hash_table_lookup(chip_data->lines, |
| 235 |
GINT_TO_POINTER(offset)); |
| 236 |
if (!line_data) |
| 237 |
g_error("failed to retrieve line data - programming bug?"); |
| 238 |
|
| 239 |
gpiodbus_line_set_props(line_data->dbus_line, info); |
| 240 |
} |
| 241 |
|
| 242 |
static void gpiodbus_daemon_export_line(GpiodbusDaemon *self, |
| 243 |
GpiodbusDaemonChipData *chip_data, |
| 244 |
GpiodglibLineInfo *info) |
| 245 |
{ |
| 246 |
g_autofree GpiodbusDaemonLineData *line_data = NULL; |
| 247 |
g_autoptr(GpiodbusObjectSkeleton) skeleton = NULL; |
| 248 |
g_autoptr(GpiodbusLine) dbus_line = NULL; |
| 249 |
g_autofree gchar *obj_path = NULL; |
| 250 |
const gchar *obj_prefix; |
| 251 |
guint line_offset; |
| 252 |
gboolean ret; |
| 253 |
|
| 254 |
obj_prefix = g_dbus_object_manager_get_object_path( |
| 255 |
G_DBUS_OBJECT_MANAGER(chip_data->line_manager)); |
| 256 |
line_offset = gpiodglib_line_info_get_offset(info); |
| 257 |
dbus_line = gpiodbus_line_skeleton_new(); |
| 258 |
obj_path = g_strdup_printf("%s/line%u", obj_prefix, line_offset); |
| 259 |
|
| 260 |
gpiodbus_line_set_props(dbus_line, info); |
| 261 |
|
| 262 |
skeleton = gpiodbus_object_skeleton_new(obj_path); |
| 263 |
gpiodbus_object_skeleton_set_line(skeleton, GPIODBUS_LINE(dbus_line)); |
| 264 |
|
| 265 |
g_debug("exporting object for GPIO line: '%s'", obj_path); |
| 266 |
|
| 267 |
g_dbus_object_manager_server_export(chip_data->line_manager, |
| 268 |
G_DBUS_OBJECT_SKELETON(skeleton)); |
| 269 |
g_dbus_object_manager_server_set_connection(chip_data->line_manager, |
| 270 |
self->con); |
| 271 |
|
| 272 |
line_data = g_malloc0(sizeof(*line_data)); |
| 273 |
line_data->dbus_line = g_steal_pointer(&dbus_line); |
| 274 |
line_data->chip_data = chip_data; |
| 275 |
|
| 276 |
ret = g_hash_table_insert(chip_data->lines, |
| 277 |
GUINT_TO_POINTER(line_offset), |
| 278 |
g_steal_pointer(&line_data)); |
| 279 |
|
| 280 |
g_assert(ret); |
| 281 |
} |
| 282 |
|
| 283 |
static gboolean gpiodbus_daemon_export_lines(GpiodbusDaemon *self, |
| 284 |
GpiodbusDaemonChipData *chip_data) |
| 285 |
{ |
| 286 |
g_autoptr(GpiodglibChipInfo) chip_info = NULL; |
| 287 |
GpiodglibChip *chip = chip_data->chip; |
| 288 |
g_autoptr(GError) err = NULL; |
| 289 |
guint i, num_lines; |
| 290 |
gint j; |
| 291 |
|
| 292 |
chip_info = gpiodglib_chip_get_info(chip, &err); |
| 293 |
if (!chip_info) { |
| 294 |
g_critical("failed to read chip info: %s", err->message); |
| 295 |
return FALSE; |
| 296 |
} |
| 297 |
|
| 298 |
num_lines = gpiodglib_chip_info_get_num_lines(chip_info); |
| 299 |
|
| 300 |
g_signal_connect(chip, "info-event", |
| 301 |
G_CALLBACK(gpiodbus_daemon_on_info_event), chip_data); |
| 302 |
|
| 303 |
for (i = 0; i < num_lines; i++) { |
| 304 |
g_autoptr(GpiodglibLineInfo) linfo = NULL; |
| 305 |
|
| 306 |
linfo = gpiodglib_chip_watch_line_info(chip, i, &err); |
| 307 |
if (!linfo) { |
| 308 |
g_critical("failed to setup a line-info watch: %s", |
| 309 |
err->message); |
| 310 |
for (j = i; j >= 0; j--) |
| 311 |
gpiodglib_chip_unwatch_line_info(chip, j, NULL); |
| 312 |
return FALSE; |
| 313 |
} |
| 314 |
|
| 315 |
gpiodbus_daemon_export_line(self, chip_data, linfo); |
| 316 |
} |
| 317 |
|
| 318 |
return TRUE; |
| 319 |
} |
| 320 |
|
| 321 |
static gboolean |
| 322 |
gpiodbus_daemon_handle_release_lines(GpiodbusRequest *request, |
| 323 |
GDBusMethodInvocation *invocation, |
| 324 |
gpointer user_data) |
| 325 |
{ |
| 326 |
GpiodbusDaemonRequestData *req_data = user_data; |
| 327 |
g_autofree gchar *obj_path = NULL; |
| 328 |
gboolean ret; |
| 329 |
|
| 330 |
obj_path = g_strdup(g_dbus_interface_skeleton_get_object_path( |
| 331 |
G_DBUS_INTERFACE_SKELETON(request))); |
| 332 |
|
| 333 |
g_debug("release call received on request '%s'", obj_path); |
| 334 |
|
| 335 |
ret = g_hash_table_remove(req_data->chip_data->daemon->requests, |
| 336 |
obj_path); |
| 337 |
|
| 338 |
if (!ret) |
| 339 |
g_warning("request '%s' is not registered - logic error?", |
| 340 |
obj_path); |
| 341 |
|
| 342 |
g_dbus_method_invocation_return_value(invocation, NULL); |
| 343 |
|
| 344 |
return G_SOURCE_CONTINUE; |
| 345 |
} |
| 346 |
|
| 347 |
static gboolean |
| 348 |
gpiodbus_daemon_handle_reconfigure_lines(GpiodbusRequest *request, |
| 349 |
GDBusMethodInvocation *invocation, |
| 350 |
GVariant *arg_line_cfg, |
| 351 |
gpointer user_data) |
| 352 |
{ |
| 353 |
GpiodbusDaemonRequestData *req_data = user_data; |
| 354 |
g_autoptr(GpiodglibLineConfig) line_cfg = NULL; |
| 355 |
g_autofree gchar *line_cfg_str = NULL; |
| 356 |
g_autoptr(GError) err = NULL; |
| 357 |
const gchar *obj_path; |
| 358 |
gboolean ret; |
| 359 |
|
| 360 |
obj_path = g_dbus_interface_skeleton_get_object_path( |
| 361 |
G_DBUS_INTERFACE_SKELETON(request)); |
| 362 |
line_cfg_str = g_variant_print(arg_line_cfg, FALSE); |
| 363 |
|
| 364 |
g_debug("reconfigure call received on request '%s', line config: %s", |
| 365 |
obj_path, line_cfg_str); |
| 366 |
|
| 367 |
line_cfg = gpiodbus_line_config_from_variant(arg_line_cfg); |
| 368 |
if (!line_cfg) { |
| 369 |
g_critical("failed to convert method call arguments '%s' to line config", |
| 370 |
line_cfg_str); |
| 371 |
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, |
| 372 |
G_DBUS_ERROR_INVALID_ARGS, |
| 373 |
"Invalid line configuration"); |
| 374 |
goto out; |
| 375 |
} |
| 376 |
|
| 377 |
ret = gpiodglib_line_request_reconfigure_lines(req_data->request, |
| 378 |
line_cfg, &err); |
| 379 |
if (!ret) { |
| 380 |
g_critical("failed to reconfigure GPIO lines on request '%s': %s", |
| 381 |
obj_path, err->message); |
| 382 |
g_dbus_method_invocation_return_dbus_error(invocation, |
| 383 |
"io.gpiod1.ReconfigureFailed", |
| 384 |
err->message); |
| 385 |
goto out; |
| 386 |
} |
| 387 |
|
| 388 |
g_dbus_method_invocation_return_value(invocation, NULL); |
| 389 |
|
| 390 |
out: |
| 391 |
return G_SOURCE_CONTINUE; |
| 392 |
} |
| 393 |
|
| 394 |
static gboolean |
| 395 |
gpiodbus_daemon_handle_get_values(GpiodbusRequest *request, |
| 396 |
GDBusMethodInvocation *invocation, |
| 397 |
GVariant *arg_offsets, gpointer user_data) |
| 398 |
{ |
| 399 |
GpiodbusDaemonRequestData *req_data = user_data; |
| 400 |
g_autoptr(GVariant) out_values = NULL; |
| 401 |
g_autofree gchar *offsets_str = NULL; |
| 402 |
g_autoptr(GVariant) response = NULL; |
| 403 |
g_autoptr(GArray) offsets = NULL; |
| 404 |
g_autoptr(GArray) values = NULL; |
| 405 |
g_autoptr(GError) err = NULL; |
| 406 |
GVariantBuilder builder; |
| 407 |
const gchar *obj_path; |
| 408 |
GVariantIter iter; |
| 409 |
gsize num_offsets; |
| 410 |
guint offset, i; |
| 411 |
gboolean ret; |
| 412 |
|
| 413 |
obj_path = g_dbus_interface_skeleton_get_object_path( |
| 414 |
G_DBUS_INTERFACE_SKELETON(request)); |
| 415 |
offsets_str = g_variant_print(arg_offsets, FALSE); |
| 416 |
num_offsets = g_variant_n_children(arg_offsets); |
| 417 |
|
| 418 |
g_debug("get-values call received on request '%s' for offsets: %s", |
| 419 |
obj_path, offsets_str); |
| 420 |
|
| 421 |
if (num_offsets == 0) { |
| 422 |
ret = gpiodglib_line_request_get_values(req_data->request, |
| 423 |
&values, &err); |
| 424 |
} else { |
| 425 |
offsets = g_array_sized_new(FALSE, TRUE, sizeof(offset), |
| 426 |
num_offsets); |
| 427 |
g_variant_iter_init(&iter, arg_offsets); |
| 428 |
while (g_variant_iter_next(&iter, "u", &offset)) |
| 429 |
g_array_append_val(offsets, offset); |
| 430 |
|
| 431 |
ret = gpiodglib_line_request_get_values_subset( |
| 432 |
req_data->request, offsets, &values, &err); |
| 433 |
} |
| 434 |
if (!ret) { |
| 435 |
g_critical("failed to get GPIO line values on request '%s': %s", |
| 436 |
obj_path, err->message); |
| 437 |
g_dbus_method_invocation_return_dbus_error(invocation, |
| 438 |
"io.gpiod1.GetValuesFailed", |
| 439 |
err->message); |
| 440 |
goto out; |
| 441 |
} |
| 442 |
|
| 443 |
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY); |
| 444 |
for (i = 0; i < values->len; i++) |
| 445 |
g_variant_builder_add(&builder, "i", |
| 446 |
g_array_index(values, gint, i)); |
| 447 |
out_values = g_variant_ref_sink(g_variant_builder_end(&builder)); |
| 448 |
|
| 449 |
g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE); |
| 450 |
g_variant_builder_add_value(&builder, out_values); |
| 451 |
response = g_variant_ref_sink(g_variant_builder_end(&builder)); |
| 452 |
|
| 453 |
g_dbus_method_invocation_return_value(invocation, response); |
| 454 |
|
| 455 |
out: |
| 456 |
return G_SOURCE_CONTINUE; |
| 457 |
} |
| 458 |
|
| 459 |
static gboolean |
| 460 |
gpiodbus_daemon_handle_set_values(GpiodbusRequest *request, |
| 461 |
GDBusMethodInvocation *invocation, |
| 462 |
GVariant *arg_values, gpointer user_data) |
| 463 |
{ |
| 464 |
GpiodbusDaemonRequestData *req_data = user_data; |
| 465 |
g_autofree gchar *values_str = NULL; |
| 466 |
g_autoptr(GArray) offsets = NULL; |
| 467 |
g_autoptr(GArray) values = NULL; |
| 468 |
g_autoptr(GError) err = NULL; |
| 469 |
const gchar *obj_path; |
| 470 |
GVariantIter iter; |
| 471 |
gsize num_values; |
| 472 |
guint offset; |
| 473 |
gboolean ret; |
| 474 |
gint value; |
| 475 |
|
| 476 |
obj_path = g_dbus_interface_skeleton_get_object_path( |
| 477 |
G_DBUS_INTERFACE_SKELETON(request)); |
| 478 |
values_str = g_variant_print(arg_values, FALSE); |
| 479 |
num_values = g_variant_n_children(arg_values); |
| 480 |
|
| 481 |
g_debug("set-values call received on request '%s': %s", |
| 482 |
obj_path, values_str); |
| 483 |
|
| 484 |
if (num_values == 0) { |
| 485 |
g_critical("Client passed no offset to value mappings"); |
| 486 |
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, |
| 487 |
G_DBUS_ERROR_INVALID_ARGS, |
| 488 |
"No offset <-> value mappings specified"); |
| 489 |
goto out; |
| 490 |
} |
| 491 |
|
| 492 |
offsets = g_array_sized_new(FALSE, TRUE, sizeof(offset), num_values); |
| 493 |
values = g_array_sized_new(FALSE, TRUE, sizeof(value), num_values); |
| 494 |
|
| 495 |
g_variant_iter_init(&iter, arg_values); |
| 496 |
while (g_variant_iter_next(&iter, "{ui}", &offset, &value)) { |
| 497 |
g_array_append_val(offsets, offset); |
| 498 |
g_array_append_val(values, value); |
| 499 |
} |
| 500 |
|
| 501 |
ret = gpiodglib_line_request_set_values_subset(req_data->request, |
| 502 |
offsets, values, &err); |
| 503 |
if (!ret) { |
| 504 |
g_critical("failed to set GPIO line values on request '%s': %s", |
| 505 |
obj_path, err->message); |
| 506 |
g_dbus_method_invocation_return_dbus_error(invocation, |
| 507 |
"io.gpiod1.SetValuesFailed", |
| 508 |
err->message); |
| 509 |
goto out; |
| 510 |
} |
| 511 |
|
| 512 |
g_dbus_method_invocation_return_value(invocation, NULL); |
| 513 |
|
| 514 |
out: |
| 515 |
return G_SOURCE_CONTINUE; |
| 516 |
} |
| 517 |
|
| 518 |
static void |
| 519 |
gpiodbus_daemon_on_edge_event(GpiodglibLineRequest *request G_GNUC_UNUSED, |
| 520 |
GpiodglibEdgeEvent *event, gpointer user_data) |
| 521 |
{ |
| 522 |
GpiodbusDaemonRequestData *req_data = user_data; |
| 523 |
guint64 line_seqno, global_seqno, timestamp; |
| 524 |
GpiodbusDaemonLineData *line_data; |
| 525 |
GpiodglibEdgeEventType edge; |
| 526 |
guint offset; |
| 527 |
gint32 val; |
| 528 |
|
| 529 |
edge = gpiodglib_edge_event_get_event_type(event); |
| 530 |
offset = gpiodglib_edge_event_get_line_offset(event); |
| 531 |
timestamp = gpiodglib_edge_event_get_timestamp_ns(event); |
| 532 |
global_seqno = gpiodglib_edge_event_get_global_seqno(event); |
| 533 |
line_seqno = gpiodglib_edge_event_get_line_seqno(event); |
| 534 |
|
| 535 |
val = edge == GPIODGLIB_EDGE_EVENT_RISING_EDGE ? 1 : 0; |
| 536 |
|
| 537 |
g_debug("%s edge event received for offset %u on request '%s'", |
| 538 |
val ? "rising" : "falling", offset, |
| 539 |
g_dbus_interface_skeleton_get_object_path( |
| 540 |
G_DBUS_INTERFACE_SKELETON(req_data->dbus_request))); |
| 541 |
|
| 542 |
line_data = g_hash_table_lookup(req_data->chip_data->lines, |
| 543 |
GINT_TO_POINTER(offset)); |
| 544 |
if (!line_data) |
| 545 |
g_error("failed to retrieve line data - programming bug?"); |
| 546 |
|
| 547 |
gpiodbus_line_emit_edge_event(line_data->dbus_line, |
| 548 |
g_variant_new("(ittt)", val, timestamp, |
| 549 |
global_seqno, line_seqno)); |
| 550 |
} |
| 551 |
|
| 552 |
static void |
| 553 |
gpiodbus_daemon_export_request(GpiodbusDaemon *self, |
| 554 |
GpiodglibLineRequest *request, |
| 555 |
GpiodbusDaemonChipData *chip_data, gint id) |
| 556 |
{ |
| 557 |
g_autofree GpiodbusDaemonRequestData *req_data = NULL; |
| 558 |
g_autoptr(GpiodbusObjectSkeleton) skeleton = NULL; |
| 559 |
g_autoptr(GpiodbusRequest) dbus_req = NULL; |
| 560 |
g_autofree gchar *obj_path = NULL; |
| 561 |
gboolean ret; |
| 562 |
|
| 563 |
dbus_req = gpiodbus_request_skeleton_new(); |
| 564 |
obj_path = g_strdup_printf("/io/gpiod1/requests/request%d", id); |
| 565 |
|
| 566 |
gpiodbus_request_set_props(dbus_req, request, chip_data->dbus_chip, |
| 567 |
G_DBUS_OBJECT_MANAGER(chip_data->line_manager)); |
| 568 |
|
| 569 |
skeleton = gpiodbus_object_skeleton_new(obj_path); |
| 570 |
gpiodbus_object_skeleton_set_request(skeleton, |
| 571 |
GPIODBUS_REQUEST(dbus_req)); |
| 572 |
|
| 573 |
g_debug("exporting object for GPIO request: '%s'", obj_path); |
| 574 |
|
| 575 |
g_dbus_object_manager_server_export(self->request_manager, |
| 576 |
G_DBUS_OBJECT_SKELETON(skeleton)); |
| 577 |
|
| 578 |
req_data = g_malloc0(sizeof(*req_data)); |
| 579 |
req_data->chip_data = chip_data; |
| 580 |
req_data->dbus_request = g_steal_pointer(&dbus_req); |
| 581 |
req_data->id = id; |
| 582 |
req_data->request = g_object_ref(request); |
| 583 |
|
| 584 |
g_signal_connect(req_data->dbus_request, "handle-release", |
| 585 |
G_CALLBACK(gpiodbus_daemon_handle_release_lines), |
| 586 |
req_data); |
| 587 |
g_signal_connect(req_data->dbus_request, "handle-reconfigure-lines", |
| 588 |
G_CALLBACK(gpiodbus_daemon_handle_reconfigure_lines), |
| 589 |
req_data); |
| 590 |
g_signal_connect(req_data->dbus_request, "handle-get-values", |
| 591 |
G_CALLBACK(gpiodbus_daemon_handle_get_values), |
| 592 |
req_data); |
| 593 |
g_signal_connect(req_data->dbus_request, "handle-set-values", |
| 594 |
G_CALLBACK(gpiodbus_daemon_handle_set_values), |
| 595 |
req_data); |
| 596 |
g_signal_connect(req_data->request, "edge-event", |
| 597 |
G_CALLBACK(gpiodbus_daemon_on_edge_event), req_data); |
| 598 |
|
| 599 |
gpiodbus_lines_set_managed(req_data, TRUE); |
| 600 |
|
| 601 |
ret = g_hash_table_insert(self->requests, g_steal_pointer(&obj_path), |
| 602 |
g_steal_pointer(&req_data)); |
| 603 |
|
| 604 |
g_assert(ret); |
| 605 |
} |
| 606 |
|
| 607 |
static gboolean |
| 608 |
gpiodbus_daemon_handle_request_lines(GpiodbusChip *chip, |
| 609 |
GDBusMethodInvocation *invocation, |
| 610 |
GVariant *arg_line_cfg, |
| 611 |
GVariant *arg_req_cfg, |
| 612 |
gpointer user_data) |
| 613 |
{ |
| 614 |
GpiodbusDaemonChipData *chip_data = user_data; |
| 615 |
g_autoptr(GpiodglibRequestConfig) req_cfg = NULL; |
| 616 |
g_autoptr(GpiodglibLineRequest) request = NULL; |
| 617 |
g_autoptr(GpiodglibLineConfig) line_cfg = NULL; |
| 618 |
g_autofree gchar *line_cfg_str = NULL; |
| 619 |
g_autofree gchar *req_cfg_str = NULL; |
| 620 |
g_autofree gchar *response = NULL; |
| 621 |
g_autoptr(GError) err = NULL; |
| 622 |
const gchar *obj_path; |
| 623 |
guint id; |
| 624 |
|
| 625 |
obj_path = g_dbus_interface_skeleton_get_object_path( |
| 626 |
G_DBUS_INTERFACE_SKELETON(chip)); |
| 627 |
line_cfg_str = g_variant_print(arg_line_cfg, FALSE); |
| 628 |
req_cfg_str = g_variant_print(arg_req_cfg, FALSE); |
| 629 |
|
| 630 |
g_debug("line request received on chip '%s', line config: %s, request_config: %s", |
| 631 |
obj_path, line_cfg_str, req_cfg_str); |
| 632 |
|
| 633 |
line_cfg = gpiodbus_line_config_from_variant(arg_line_cfg); |
| 634 |
if (!line_cfg) { |
| 635 |
g_critical("failed to convert method call arguments '%s' to line config", |
| 636 |
line_cfg_str); |
| 637 |
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, |
| 638 |
G_DBUS_ERROR_INVALID_ARGS, |
| 639 |
"Invalid line configuration"); |
| 640 |
goto out; |
| 641 |
} |
| 642 |
|
| 643 |
req_cfg = gpiodbus_request_config_from_variant(arg_req_cfg); |
| 644 |
if (!req_cfg) { |
| 645 |
g_critical("failed to convert method call arguments '%s' to request config", |
| 646 |
req_cfg_str); |
| 647 |
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR, |
| 648 |
G_DBUS_ERROR_INVALID_ARGS, |
| 649 |
"Invalid request configuration"); |
| 650 |
goto out; |
| 651 |
} |
| 652 |
|
| 653 |
request = gpiodglib_chip_request_lines(chip_data->chip, req_cfg, |
| 654 |
line_cfg, &err); |
| 655 |
if (err) { |
| 656 |
g_critical("failed to request GPIO lines on chip '%s': %s", |
| 657 |
obj_path, err->message); |
| 658 |
g_dbus_method_invocation_return_dbus_error(invocation, |
| 659 |
"io.gpiod1.RequestFailed", err->message); |
| 660 |
goto out; |
| 661 |
} |
| 662 |
|
| 663 |
g_debug("line request succeeded on chip '%s'", obj_path); |
| 664 |
|
| 665 |
id = gpiodbus_id_alloc(chip_data->daemon->req_id_root); |
| 666 |
gpiodbus_daemon_export_request(chip_data->daemon, request, |
| 667 |
chip_data, id); |
| 668 |
|
| 669 |
response = g_strdup_printf("/io/gpiod1/requests/request%d", id); |
| 670 |
g_dbus_method_invocation_return_value(invocation, |
| 671 |
g_variant_new("(o)", response)); |
| 672 |
|
| 673 |
out: |
| 674 |
return G_SOURCE_CONTINUE; |
| 675 |
} |
| 676 |
|
| 677 |
static void gpiodbus_daemon_export_chip(GpiodbusDaemon *self, GUdevDevice *dev) |
| 678 |
{ |
| 679 |
g_autofree GpiodbusDaemonChipData *chip_data = NULL; |
| 680 |
g_autoptr(GDBusObjectManagerServer) manager = NULL; |
| 681 |
g_autoptr(GpiodbusObjectSkeleton) skeleton = NULL; |
| 682 |
const gchar *devname, *devpath, *obj_prefix; |
| 683 |
g_autoptr(GpiodbusChip) dbus_chip = NULL; |
| 684 |
g_autoptr(GpiodglibChip) chip = NULL; |
| 685 |
g_autoptr(GHashTable) lines = NULL; |
| 686 |
g_autofree gchar *obj_path = NULL; |
| 687 |
g_autoptr(GError) err = NULL; |
| 688 |
gboolean ret; |
| 689 |
|
| 690 |
devname = g_udev_device_get_name(dev); |
| 691 |
|
| 692 |
if (g_hash_table_contains(self->chips, devname)) { |
| 693 |
g_debug("chip %s is already exported", devname); |
| 694 |
return; |
| 695 |
} |
| 696 |
|
| 697 |
devpath = g_udev_device_get_device_file(dev); |
| 698 |
obj_prefix = g_dbus_object_manager_get_object_path( |
| 699 |
G_DBUS_OBJECT_MANAGER(self->chip_manager)); |
| 700 |
|
| 701 |
chip = gpiodglib_chip_new(devpath, &err); |
| 702 |
if (!chip) { |
| 703 |
g_critical("failed to open GPIO chip %s: %s", |
| 704 |
devpath, err->message); |
| 705 |
return; |
| 706 |
} |
| 707 |
|
| 708 |
dbus_chip = gpiodbus_chip_skeleton_new(); |
| 709 |
obj_path = g_strdup_printf("%s/%s", obj_prefix, devname); |
| 710 |
|
| 711 |
ret = gpiodbus_chip_set_props(dbus_chip, chip, &err); |
| 712 |
if (!ret) { |
| 713 |
g_critical("failed to set chip properties: %s", err->message); |
| 714 |
return; |
| 715 |
} |
| 716 |
|
| 717 |
skeleton = gpiodbus_object_skeleton_new(obj_path); |
| 718 |
gpiodbus_object_skeleton_set_chip(skeleton, GPIODBUS_CHIP(dbus_chip)); |
| 719 |
|
| 720 |
g_debug("exporting object for GPIO chip: '%s'", obj_path); |
| 721 |
|
| 722 |
g_dbus_object_manager_server_export(self->chip_manager, |
| 723 |
G_DBUS_OBJECT_SKELETON(skeleton)); |
| 724 |
|
| 725 |
lines = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, |
| 726 |
gpiodbus_daemon_line_data_free); |
| 727 |
manager = g_dbus_object_manager_server_new(obj_path); |
| 728 |
|
| 729 |
chip_data = g_malloc0(sizeof(*chip_data)); |
| 730 |
chip_data->daemon = self; |
| 731 |
chip_data->chip = g_steal_pointer(&chip); |
| 732 |
chip_data->dbus_chip = g_steal_pointer(&dbus_chip); |
| 733 |
chip_data->lines = g_steal_pointer(&lines); |
| 734 |
chip_data->line_manager = g_steal_pointer(&manager); |
| 735 |
|
| 736 |
ret = gpiodbus_daemon_export_lines(self, chip_data); |
| 737 |
if (!ret) { |
| 738 |
g_dbus_object_manager_server_unexport(self->chip_manager, |
| 739 |
obj_path); |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
g_signal_connect(chip_data->dbus_chip, "handle-request-lines", |
| 744 |
G_CALLBACK(gpiodbus_daemon_handle_request_lines), |
| 745 |
chip_data); |
| 746 |
|
| 747 |
ret = g_hash_table_insert(self->chips, g_strdup(devname), |
| 748 |
g_steal_pointer(&chip_data)); |
| 749 |
g_assert(ret); |
| 750 |
} |
| 751 |
|
| 752 |
static void gpiodbus_daemon_unexport_chip(GpiodbusDaemon *self, |
| 753 |
GUdevDevice *dev) |
| 754 |
{ |
| 755 |
const gchar *name = g_udev_device_get_name(dev); |
| 756 |
gboolean ret; |
| 757 |
|
| 758 |
ret = g_hash_table_remove(self->chips, name); |
| 759 |
|
| 760 |
if (!ret) |
| 761 |
g_warning("chip '%s' is not registered - exporting failed?", |
| 762 |
name); |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
static gboolean gpiodbus_daemon_is_gpiochip_device(GUdevDevice *dev) |
| 772 |
{ |
| 773 |
return g_udev_device_get_device_file(dev) != NULL; |
| 774 |
} |
| 775 |
|
| 776 |
static void gpiodbus_daemon_on_uevent(GUdevClient *udev G_GNUC_UNUSED, |
| 777 |
const gchar *action, GUdevDevice *dev, |
| 778 |
gpointer data) |
| 779 |
{ |
| 780 |
GpiodbusDaemon *self = data; |
| 781 |
|
| 782 |
if (!gpiodbus_daemon_is_gpiochip_device(dev)) |
| 783 |
return; |
| 784 |
|
| 785 |
g_debug("uevent: %s action on %s device", |
| 786 |
action, g_udev_device_get_name(dev)); |
| 787 |
|
| 788 |
if (g_strcmp0(action, "add") == 0) |
| 789 |
gpiodbus_daemon_export_chip(self, dev); |
| 790 |
else if (g_strcmp0(action, "remove") == 0) |
| 791 |
gpiodbus_daemon_unexport_chip(self, dev); |
| 792 |
} |
| 793 |
|
| 794 |
static void gpiodbus_daemon_process_chip_dev(gpointer data, gpointer user_data) |
| 795 |
{ |
| 796 |
GpiodbusDaemon *daemon = user_data; |
| 797 |
GUdevDevice *dev = data; |
| 798 |
|
| 799 |
if (gpiodbus_daemon_is_gpiochip_device(dev)) |
| 800 |
gpiodbus_daemon_export_chip(daemon, dev); |
| 801 |
} |
| 802 |
|
| 803 |
void gpiodbus_daemon_start(GpiodbusDaemon *self, GDBusConnection *con) |
| 804 |
{ |
| 805 |
g_autolist(GUdevDevice) devs = NULL; |
| 806 |
|
| 807 |
g_assert(self); |
| 808 |
g_assert(!self->con); |
| 809 |
|
| 810 |
self->con = g_object_ref(con); |
| 811 |
|
| 812 |
|
| 813 |
g_signal_connect(self->udev, "uevent", |
| 814 |
G_CALLBACK(gpiodbus_daemon_on_uevent), self); |
| 815 |
|
| 816 |
devs = g_udev_client_query_by_subsystem(self->udev, "gpio"); |
| 817 |
g_list_foreach(devs, gpiodbus_daemon_process_chip_dev, self); |
| 818 |
|
| 819 |
g_dbus_object_manager_server_set_connection(self->chip_manager, |
| 820 |
self->con); |
| 821 |
g_dbus_object_manager_server_set_connection(self->request_manager, |
| 822 |
self->con); |
| 823 |
|
| 824 |
g_debug("GPIO daemon now listening"); |
| 825 |
} |