| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <gio/gio.h> |
| 5 |
|
| 6 |
#include "internal.h" |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
struct _GpiodglibLineConfig { |
| 17 |
GObject parent_instance; |
| 18 |
struct gpiod_line_config *handle; |
| 19 |
}; |
| 20 |
|
| 21 |
typedef enum { |
| 22 |
GPIODGLIB_LINE_CONFIG_PROP_CONFIGURED_OFFSETS = 1, |
| 23 |
} GpiodglibLineConfigProp; |
| 24 |
|
| 25 |
G_DEFINE_TYPE(GpiodglibLineConfig, gpiodglib_line_config, G_TYPE_OBJECT); |
| 26 |
|
| 27 |
static void gpiodglib_line_config_get_property(GObject *obj, guint prop_id, |
| 28 |
GValue *val, GParamSpec *pspec) |
| 29 |
{ |
| 30 |
GpiodglibLineConfig *self = GPIODGLIB_LINE_CONFIG_OBJ(obj); |
| 31 |
g_autofree guint *offsets = NULL; |
| 32 |
g_autoptr(GArray) boxed = NULL; |
| 33 |
gsize num_offsets, i; |
| 34 |
|
| 35 |
switch ((GpiodglibLineConfigProp)prop_id) { |
| 36 |
case GPIODGLIB_LINE_CONFIG_PROP_CONFIGURED_OFFSETS: |
| 37 |
num_offsets = gpiod_line_config_get_num_configured_offsets( |
| 38 |
self->handle); |
| 39 |
offsets = g_malloc0(num_offsets * sizeof(guint)); |
| 40 |
gpiod_line_config_get_configured_offsets(self->handle, offsets, |
| 41 |
num_offsets); |
| 42 |
|
| 43 |
boxed = g_array_new(FALSE, TRUE, sizeof(guint)); |
| 44 |
for (i = 0; i < num_offsets; i++) |
| 45 |
g_array_append_val(boxed, offsets[i]); |
| 46 |
|
| 47 |
g_value_set_boxed(val, boxed); |
| 48 |
break; |
| 49 |
default: |
| 50 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static void gpiodglib_line_config_finalize(GObject *obj) |
| 55 |
{ |
| 56 |
GpiodglibLineConfig *self = GPIODGLIB_LINE_CONFIG_OBJ(obj); |
| 57 |
|
| 58 |
g_clear_pointer(&self->handle, gpiod_line_config_free); |
| 59 |
|
| 60 |
G_OBJECT_CLASS(gpiodglib_line_config_parent_class)->finalize(obj); |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
gpiodglib_line_config_class_init(GpiodglibLineConfigClass *line_config_class) |
| 65 |
{ |
| 66 |
GObjectClass *class = G_OBJECT_CLASS(line_config_class); |
| 67 |
|
| 68 |
class->get_property = gpiodglib_line_config_get_property; |
| 69 |
class->finalize = gpiodglib_line_config_finalize; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
g_object_class_install_property(class, |
| 77 |
GPIODGLIB_LINE_CONFIG_PROP_CONFIGURED_OFFSETS, |
| 78 |
g_param_spec_boxed("configured-offsets", "Configured Offsets", |
| 79 |
"Array of offsets for which line settings have been set.", |
| 80 |
G_TYPE_ARRAY, |
| 81 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 82 |
} |
| 83 |
|
| 84 |
static void gpiodglib_line_config_init(GpiodglibLineConfig *self) |
| 85 |
{ |
| 86 |
self->handle = gpiod_line_config_new(); |
| 87 |
if (!self->handle) |
| 88 |
|
| 89 |
g_error("Failed to allocate memory for the request-config object."); |
| 90 |
} |
| 91 |
|
| 92 |
GpiodglibLineConfig *gpiodglib_line_config_new(void) |
| 93 |
{ |
| 94 |
return GPIODGLIB_LINE_CONFIG_OBJ( |
| 95 |
g_object_new(GPIODGLIB_LINE_CONFIG_TYPE, NULL)); |
| 96 |
} |
| 97 |
|
| 98 |
void gpiodglib_line_config_reset(GpiodglibLineConfig *self) |
| 99 |
{ |
| 100 |
g_assert(self); |
| 101 |
|
| 102 |
gpiod_line_config_reset(self->handle); |
| 103 |
} |
| 104 |
|
| 105 |
gboolean |
| 106 |
gpiodglib_line_config_add_line_settings(GpiodglibLineConfig *self, |
| 107 |
const GArray *offsets, |
| 108 |
GpiodglibLineSettings *settings, |
| 109 |
GError **err) |
| 110 |
{ |
| 111 |
struct gpiod_line_settings *settings_handle; |
| 112 |
int ret; |
| 113 |
|
| 114 |
g_assert(self); |
| 115 |
|
| 116 |
if (!offsets || !offsets->len) { |
| 117 |
g_set_error(err, GPIODGLIB_ERROR, GPIODGLIB_ERR_INVAL, |
| 118 |
"at least one offset must be specified when adding line settings"); |
| 119 |
return FALSE; |
| 120 |
} |
| 121 |
|
| 122 |
settings_handle = settings ? |
| 123 |
_gpiodglib_line_settings_get_handle(settings) : NULL; |
| 124 |
ret = gpiod_line_config_add_line_settings(self->handle, |
| 125 |
(unsigned int *)offsets->data, |
| 126 |
offsets->len, |
| 127 |
settings_handle); |
| 128 |
if (ret) { |
| 129 |
_gpiodglib_set_error_from_errno(err, |
| 130 |
"failed to add line settings to line config"); |
| 131 |
return FALSE; |
| 132 |
} |
| 133 |
|
| 134 |
return TRUE; |
| 135 |
} |
| 136 |
|
| 137 |
GpiodglibLineSettings * |
| 138 |
gpiodglib_line_config_get_line_settings(GpiodglibLineConfig *self, guint offset) |
| 139 |
{ |
| 140 |
struct gpiod_line_settings *settings; |
| 141 |
|
| 142 |
g_assert(self); |
| 143 |
|
| 144 |
settings = gpiod_line_config_get_line_settings(self->handle, offset); |
| 145 |
if (!settings) { |
| 146 |
if (errno == ENOENT) |
| 147 |
return NULL; |
| 148 |
|
| 149 |
|
| 150 |
g_error("failed to retrieve line settings for offset %u: %s", |
| 151 |
offset, g_strerror(errno)); |
| 152 |
} |
| 153 |
|
| 154 |
return _gpiodglib_line_settings_new(settings); |
| 155 |
} |
| 156 |
|
| 157 |
gboolean gpiodglib_line_config_set_output_values(GpiodglibLineConfig *self, |
| 158 |
const GArray *values, |
| 159 |
GError **err) |
| 160 |
{ |
| 161 |
g_autofree enum gpiod_line_value *vals = NULL; |
| 162 |
gint ret; |
| 163 |
guint i; |
| 164 |
|
| 165 |
g_assert(self); |
| 166 |
|
| 167 |
vals = g_malloc0(sizeof(*vals) * values->len); |
| 168 |
for (i = 0; i < values->len; i++) |
| 169 |
vals[i] = _gpiodglib_line_value_to_library( |
| 170 |
g_array_index(values, GpiodglibLineValue, i)); |
| 171 |
|
| 172 |
ret = gpiod_line_config_set_output_values(self->handle, vals, |
| 173 |
values->len); |
| 174 |
if (ret) { |
| 175 |
_gpiodglib_set_error_from_errno(err, |
| 176 |
"unable to set output values"); |
| 177 |
return FALSE; |
| 178 |
} |
| 179 |
|
| 180 |
return TRUE; |
| 181 |
} |
| 182 |
|
| 183 |
GArray *gpiodglib_line_config_get_configured_offsets(GpiodglibLineConfig *self) |
| 184 |
{ |
| 185 |
return _gpiodglib_get_prop_boxed_array(G_OBJECT(self), |
| 186 |
"configured-offsets"); |
| 187 |
} |
| 188 |
|
| 189 |
struct gpiod_line_config * |
| 190 |
_gpiodglib_line_config_get_handle(GpiodglibLineConfig *line_cfg) |
| 191 |
{ |
| 192 |
return line_cfg->handle; |
| 193 |
} |