| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <errno.h> |
| 6 | #include <gpiod.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include "internal.h" |
| 11 | |
| 12 | struct gpiod_request_config { |
| 13 | char consumer[GPIO_MAX_NAME_SIZE]; |
| 14 | size_t event_buffer_size; |
| 15 | }; |
| 16 | |
| 17 | GPIOD_API struct gpiod_request_config *gpiod_request_config_new(void) |
| 18 | { |
| 19 | struct gpiod_request_config *config; |
| 20 | |
| 21 | config = malloc(sizeof(*config)); |
| 22 | if (!config) |
| 23 | return NULL; |
| 24 | |
| 25 | memset(config, 0, sizeof(*config)); |
| 26 | |
| 27 | return config; |
| 28 | } |
| 29 | |
| 30 | GPIOD_API void gpiod_request_config_free(struct gpiod_request_config *config) |
| 31 | { |
| 32 | free(config); |
| 33 | } |
| 34 | |
| 35 | GPIOD_API void |
| 36 | gpiod_request_config_set_consumer(struct gpiod_request_config *config, |
| 37 | const char *consumer) |
| 38 | { |
| 39 | assert(config); |
| 40 | |
| 41 | if (!consumer) { |
| 42 | config->consumer[0] = '\0'; |
| 43 | } else { |
| 44 | strncpy(config->consumer, consumer, GPIO_MAX_NAME_SIZE - 1); |
| 45 | config->consumer[GPIO_MAX_NAME_SIZE - 1] = '\0'; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | GPIOD_API const char * |
| 50 | gpiod_request_config_get_consumer(struct gpiod_request_config *config) |
| 51 | { |
| 52 | assert(config); |
| 53 | |
| 54 | return config->consumer[0] == '\0' ? NULL : config->consumer; |
| 55 | } |
| 56 | |
| 57 | GPIOD_API void |
| 58 | gpiod_request_config_set_event_buffer_size(struct gpiod_request_config *config, |
| 59 | size_t event_buffer_size) |
| 60 | { |
| 61 | assert(config); |
| 62 | |
| 63 | config->event_buffer_size = event_buffer_size; |
| 64 | } |
| 65 | |
| 66 | GPIOD_API size_t |
| 67 | gpiod_request_config_get_event_buffer_size(struct gpiod_request_config *config) |
| 68 | { |
| 69 | assert(config); |
| 70 | |
| 71 | return config->event_buffer_size; |
| 72 | } |
| 73 | |
| 74 | void gpiod_request_config_to_uapi(struct gpiod_request_config *config, |
| 75 | struct gpio_v2_line_request *uapi_req) |
| 76 | { |
| 77 | strcpy(uapi_req->consumer, config->consumer); |
| 78 | uapi_req->event_buffer_size = config->event_buffer_size; |
| 79 | } |