| 1 | // SPDX-License-Identifier: LGPL-2.1-or-latercxx . . $ `<file>/lib/line-info.c`/ |
| 2 | // SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <gpiod.h>cxx . . $ `<file>/include/gpiod.h`/ |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include "internal.h"cxx . . $ `<file>/lib/internal.h`/ |
| 10 | |
| 11 | struct gpiod_line_infocxx . . $ gpiod_line_info# { |
| 12 | unsigned int offsetcxx . . $ gpiod_line_info#offset.; |
| 13 | char namecxx . . $ gpiod_line_info#name.[GPIO_MAX_NAME_SIZEcxx . . $ `lib/uapi/gpio.h:23:9`! + 1]; |
| 14 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! usedcxx . . $ gpiod_line_info#used.; |
| 15 | char consumercxx . . $ gpiod_line_info#consumer.[GPIO_MAX_NAME_SIZEcxx . . $ `lib/uapi/gpio.h:23:9`! + 1]; |
| 16 | enum gpiod_line_directioncxx . . $ gpiod_line_direction# directioncxx . . $ gpiod_line_info#direction.; |
| 17 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! active_lowcxx . . $ gpiod_line_info#active_low.; |
| 18 | enum gpiod_line_biascxx . . $ gpiod_line_bias# biascxx . . $ gpiod_line_info#bias.; |
| 19 | enum gpiod_line_drivecxx . . $ gpiod_line_drive# drivecxx . . $ gpiod_line_info#drive.; |
| 20 | enum gpiod_line_edgecxx . . $ gpiod_line_edge# edgecxx . . $ gpiod_line_info#edge.; |
| 21 | enum gpiod_line_clockcxx . . $ gpiod_line_clock# event_clockcxx . . $ gpiod_line_info#event_clock.; |
| 22 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! debouncedcxx . . $ gpiod_line_info#debounced.; |
| 23 | unsigned long debounce_period_uscxx . . $ gpiod_line_info#debounce_period_us.; |
| 24 | }; |
| 25 | |
| 26 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! void gpiod_line_info_freecxx . . $ gpiod_line_info_free(518ebc771aa28e67).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 27 | { |
| 28 | freecxx . . $ free(5d22bdacc48458e8).(info); |
| 29 | } |
| 30 | |
| 31 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! struct gpiod_line_infocxx . . $ gpiod_line_info# * |
| 32 | gpiod_line_info_copycxx . . $ gpiod_line_info_copy(e3402e05641aad97).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 33 | { |
| 34 | struct gpiod_line_infocxx . . $ gpiod_line_info# *copy; |
| 35 | |
| 36 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 37 | |
| 38 | copy = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(*info)); |
| 39 | if (!copy) |
| 40 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 41 | |
| 42 | memcpycxx . . $ memcpy(cc817cb175f282d).(copy, info, sizeof(*info)); |
| 43 | |
| 44 | return copy; |
| 45 | } |
| 46 | |
| 47 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! unsigned int gpiod_line_info_get_offsetcxx . . $ gpiod_line_info_get_offset(8484268bf5ee9fd7).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 48 | { |
| 49 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 50 | |
| 51 | return info->offsetcxx . . $ gpiod_line_info#offset.; |
| 52 | } |
| 53 | |
| 54 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! const char *gpiod_line_info_get_namecxx . . $ gpiod_line_info_get_name(c703ef49ff36b039).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 55 | { |
| 56 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 57 | |
| 58 | return info->namecxx . . $ gpiod_line_info#name.[0] == '\0' ? NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! : info->namecxx . . $ gpiod_line_info#name.; |
| 59 | } |
| 60 | |
| 61 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! gpiod_line_info_is_usedcxx . . $ gpiod_line_info_is_used(91783f844a8958f2).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 62 | { |
| 63 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 64 | |
| 65 | return info->usedcxx . . $ gpiod_line_info#used.; |
| 66 | } |
| 67 | |
| 68 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! const char *gpiod_line_info_get_consumercxx . . $ gpiod_line_info_get_consumer(c703ef49ff36b039).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 69 | { |
| 70 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 71 | |
| 72 | return info->consumercxx . . $ gpiod_line_info#consumer.[0] == '\0' ? NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! : info->consumercxx . . $ gpiod_line_info#consumer.; |
| 73 | } |
| 74 | |
| 75 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! enum gpiod_line_directioncxx . . $ gpiod_line_direction# |
| 76 | gpiod_line_info_get_directioncxx . . $ gpiod_line_info_get_direction(69aa2c48cc1c4137).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 77 | { |
| 78 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 79 | |
| 80 | return info->directioncxx . . $ gpiod_line_info#direction.; |
| 81 | } |
| 82 | |
| 83 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! gpiod_line_info_is_active_lowcxx . . $ gpiod_line_info_is_active_low(91783f844a8958f2).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 84 | { |
| 85 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 86 | |
| 87 | return info->active_lowcxx . . $ gpiod_line_info#active_low.; |
| 88 | } |
| 89 | |
| 90 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! enum gpiod_line_biascxx . . $ gpiod_line_bias# |
| 91 | gpiod_line_info_get_biascxx . . $ gpiod_line_info_get_bias(d94a245f3b6a5bdb).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 92 | { |
| 93 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 94 | |
| 95 | return info->biascxx . . $ gpiod_line_info#bias.; |
| 96 | } |
| 97 | |
| 98 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! enum gpiod_line_drivecxx . . $ gpiod_line_drive# |
| 99 | gpiod_line_info_get_drivecxx . . $ gpiod_line_info_get_drive(61aa9d1ba8c26ca9).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 100 | { |
| 101 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 102 | |
| 103 | return info->drivecxx . . $ gpiod_line_info#drive.; |
| 104 | } |
| 105 | |
| 106 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! enum gpiod_line_edgecxx . . $ gpiod_line_edge# |
| 107 | gpiod_line_info_get_edge_detectioncxx . . $ gpiod_line_info_get_edge_detection(6d43fb4ce13a7cb2).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 108 | { |
| 109 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 110 | |
| 111 | return info->edgecxx . . $ gpiod_line_info#edge.; |
| 112 | } |
| 113 | |
| 114 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! enum gpiod_line_clockcxx . . $ gpiod_line_clock# |
| 115 | gpiod_line_info_get_event_clockcxx . . $ gpiod_line_info_get_event_clock(330e0c8dea661564).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 116 | { |
| 117 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 118 | |
| 119 | return info->event_clockcxx . . $ gpiod_line_info#event_clock.; |
| 120 | } |
| 121 | |
| 122 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! gpiod_line_info_is_debouncedcxx . . $ gpiod_line_info_is_debounced(91783f844a8958f2).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 123 | { |
| 124 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 125 | |
| 126 | return info->debouncedcxx . . $ gpiod_line_info#debounced.; |
| 127 | } |
| 128 | |
| 129 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! unsigned long |
| 130 | gpiod_line_info_get_debounce_period_uscxx . . $ gpiod_line_info_get_debounce_period_us(8e8e7b8471850159).(struct gpiod_line_infocxx . . $ gpiod_line_info# *info) |
| 131 | { |
| 132 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(info); |
| 133 | |
| 134 | return info->debounce_period_uscxx . . $ gpiod_line_info#debounce_period_us.; |
| 135 | } |
| 136 | |
| 137 | struct gpiod_line_infocxx . . $ gpiod_line_info# * |
| 138 | gpiod_line_info_from_uapicxx . . $ gpiod_line_info_from_uapi(273b4a357bdfb836).(struct gpio_v2_line_infocxx . . $ gpio_v2_line_info# *uapi_info) |
| 139 | { |
| 140 | struct gpio_v2_line_attributecxx . . $ gpio_v2_line_attribute# *attr; |
| 141 | struct gpiod_line_infocxx . . $ gpiod_line_info# *info; |
| 142 | size_t i; |
| 143 | |
| 144 | info = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(*info)); |
| 145 | if (!info) |
| 146 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 147 | |
| 148 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(info, 0, sizeof(*info)); |
| 149 | |
| 150 | info->offsetcxx . . $ gpiod_line_info#offset. = uapi_info->offsetcxx . . $ gpio_v2_line_info#offset.; |
| 151 | strncpycxx . . $ strncpy(255158a415b673b0).(info->namecxx . . $ gpiod_line_info#name., uapi_info->namecxx . . $ gpio_v2_line_info#name., GPIO_MAX_NAME_SIZEcxx . . $ `lib/uapi/gpio.h:23:9`!); |
| 152 | info->namecxx . . $ gpiod_line_info#name.[GPIO_MAX_NAME_SIZEcxx . . $ `lib/uapi/gpio.h:23:9`!] = '\0'; |
| 153 | |
| 154 | info->usedcxx . . $ gpiod_line_info#used. = !!(uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_USEDcxx . . $ GPIO_V2_LINE_FLAG_USED.); |
| 155 | strncpycxx . . $ strncpy(255158a415b673b0).(info->consumercxx . . $ gpiod_line_info#consumer., uapi_info->consumercxx . . $ gpio_v2_line_info#consumer., GPIO_MAX_NAME_SIZEcxx . . $ `lib/uapi/gpio.h:23:9`!); |
| 156 | info->consumercxx . . $ gpiod_line_info#consumer.[GPIO_MAX_NAME_SIZEcxx . . $ `lib/uapi/gpio.h:23:9`!] = '\0'; |
| 157 | |
| 158 | if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_OUTPUTcxx . . $ GPIO_V2_LINE_FLAG_OUTPUT.) |
| 159 | info->directioncxx . . $ gpiod_line_info#direction. = GPIOD_LINE_DIRECTION_OUTPUTcxx . . $ GPIOD_LINE_DIRECTION_OUTPUT.; |
| 160 | else |
| 161 | info->directioncxx . . $ gpiod_line_info#direction. = GPIOD_LINE_DIRECTION_INPUTcxx . . $ GPIOD_LINE_DIRECTION_INPUT.; |
| 162 | |
| 163 | if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_ACTIVE_LOWcxx . . $ GPIO_V2_LINE_FLAG_ACTIVE_LOW.) |
| 164 | info->active_lowcxx . . $ gpiod_line_info#active_low. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 165 | |
| 166 | if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_BIAS_PULL_UPcxx . . $ GPIO_V2_LINE_FLAG_BIAS_PULL_UP.) |
| 167 | info->biascxx . . $ gpiod_line_info#bias. = GPIOD_LINE_BIAS_PULL_UPcxx . . $ GPIOD_LINE_BIAS_PULL_UP.; |
| 168 | else if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_BIAS_PULL_DOWNcxx . . $ GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN.) |
| 169 | info->biascxx . . $ gpiod_line_info#bias. = GPIOD_LINE_BIAS_PULL_DOWNcxx . . $ GPIOD_LINE_BIAS_PULL_DOWN.; |
| 170 | else if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_BIAS_DISABLEDcxx . . $ GPIO_V2_LINE_FLAG_BIAS_DISABLED.) |
| 171 | info->biascxx . . $ gpiod_line_info#bias. = GPIOD_LINE_BIAS_DISABLEDcxx . . $ GPIOD_LINE_BIAS_DISABLED.; |
| 172 | else |
| 173 | info->biascxx . . $ gpiod_line_info#bias. = GPIOD_LINE_BIAS_UNKNOWNcxx . . $ GPIOD_LINE_BIAS_UNKNOWN.; |
| 174 | |
| 175 | if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_OPEN_DRAINcxx . . $ GPIO_V2_LINE_FLAG_OPEN_DRAIN.) |
| 176 | info->drivecxx . . $ gpiod_line_info#drive. = GPIOD_LINE_DRIVE_OPEN_DRAINcxx . . $ GPIOD_LINE_DRIVE_OPEN_DRAIN.; |
| 177 | else if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_OPEN_SOURCEcxx . . $ GPIO_V2_LINE_FLAG_OPEN_SOURCE.) |
| 178 | info->drivecxx . . $ gpiod_line_info#drive. = GPIOD_LINE_DRIVE_OPEN_SOURCEcxx . . $ GPIOD_LINE_DRIVE_OPEN_SOURCE.; |
| 179 | else |
| 180 | info->drivecxx . . $ gpiod_line_info#drive. = GPIOD_LINE_DRIVE_PUSH_PULLcxx . . $ GPIOD_LINE_DRIVE_PUSH_PULL.; |
| 181 | |
| 182 | if ((uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_EDGE_RISINGcxx . . $ GPIO_V2_LINE_FLAG_EDGE_RISING.) && |
| 183 | (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_EDGE_FALLINGcxx . . $ GPIO_V2_LINE_FLAG_EDGE_FALLING.)) |
| 184 | info->edgecxx . . $ gpiod_line_info#edge. = GPIOD_LINE_EDGE_BOTHcxx . . $ GPIOD_LINE_EDGE_BOTH.; |
| 185 | else if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_EDGE_RISINGcxx . . $ GPIO_V2_LINE_FLAG_EDGE_RISING.) |
| 186 | info->edgecxx . . $ gpiod_line_info#edge. = GPIOD_LINE_EDGE_RISINGcxx . . $ GPIOD_LINE_EDGE_RISING.; |
| 187 | else if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_EDGE_FALLINGcxx . . $ GPIO_V2_LINE_FLAG_EDGE_FALLING.) |
| 188 | info->edgecxx . . $ gpiod_line_info#edge. = GPIOD_LINE_EDGE_FALLINGcxx . . $ GPIOD_LINE_EDGE_FALLING.; |
| 189 | else |
| 190 | info->edgecxx . . $ gpiod_line_info#edge. = GPIOD_LINE_EDGE_NONEcxx . . $ GPIOD_LINE_EDGE_NONE.; |
| 191 | |
| 192 | if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIMEcxx . . $ GPIO_V2_LINE_FLAG_EVENT_CLOCK_REALTIME.) |
| 193 | info->event_clockcxx . . $ gpiod_line_info#event_clock. = GPIOD_LINE_CLOCK_REALTIMEcxx . . $ GPIOD_LINE_CLOCK_REALTIME.; |
| 194 | else if (uapi_info->flagscxx . . $ gpio_v2_line_info#flags. & GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTEcxx . . $ GPIO_V2_LINE_FLAG_EVENT_CLOCK_HTE.) |
| 195 | info->event_clockcxx . . $ gpiod_line_info#event_clock. = GPIOD_LINE_CLOCK_HTEcxx . . $ GPIOD_LINE_CLOCK_HTE.; |
| 196 | else |
| 197 | info->event_clockcxx . . $ gpiod_line_info#event_clock. = GPIOD_LINE_CLOCK_MONOTONICcxx . . $ GPIOD_LINE_CLOCK_MONOTONIC.; |
| 198 | |
| 199 | /* |
| 200 | * We assume that the kernel returns correct configuration and that no |
| 201 | * attributes repeat. |
| 202 | */ |
| 203 | for (i = 0; i < uapi_info->num_attrscxx . . $ gpio_v2_line_info#num_attrs.; i++) { |
| 204 | attr = &uapi_info->attrscxx . . $ gpio_v2_line_info#attrs.[i]; |
| 205 | |
| 206 | if (attr->idcxx . . $ gpio_v2_line_attribute#id. == GPIO_V2_LINE_ATTR_ID_DEBOUNCEcxx . . $ GPIO_V2_LINE_ATTR_ID_DEBOUNCE.) { |
| 207 | info->debouncedcxx . . $ gpiod_line_info#debounced. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 208 | info->debounce_period_uscxx . . $ gpiod_line_info#debounce_period_us. = attr->debounce_period_uscxx . . $ gpio_v2_line_attribute#$anonymous_type_0#debounce_period_us.; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return info; |
| 213 | } |