| 1 | // SPDX-License-Identifier: LGPL-2.1-or-latercxx . . $ `<file>/lib/line-request.c`/ |
| 2 | // SPDX-FileCopyrightText: 2021 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <assert.h> |
| 5 | #include <errno.h> |
| 6 | #include <gpiod.h>cxx . . $ `<file>/include/gpiod.h`/ |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/param.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "internal.h"cxx . . $ `<file>/lib/internal.h`/ |
| 13 | |
| 14 | struct gpiod_line_requestcxx . . $ gpiod_line_request# { |
| 15 | char *chip_namecxx . . $ gpiod_line_request#chip_name.; |
| 16 | unsigned int offsetscxx . . $ gpiod_line_request#offsets.[GPIO_V2_LINES_MAXcxx . . $ `lib/uapi/gpio.h:45:9`!]; |
| 17 | size_t num_linescxx . . $ gpiod_line_request#num_lines.; |
| 18 | int fdcxx . . $ gpiod_line_request#fd.; |
| 19 | }; |
| 20 | |
| 21 | struct gpiod_line_requestcxx . . $ gpiod_line_request# * |
| 22 | gpiod_line_request_from_uapicxx . . $ gpiod_line_request_from_uapi(aba1763f564611d6).(struct gpio_v2_line_requestcxx . . $ gpio_v2_line_request# *uapi_req, |
| 23 | const char *chip_name) |
| 24 | { |
| 25 | struct gpiod_line_requestcxx . . $ gpiod_line_request# *request; |
| 26 | |
| 27 | if (uapi_req->num_linescxx . . $ gpio_v2_line_request#num_lines. > GPIO_V2_LINES_MAXcxx . . $ `lib/uapi/gpio.h:45:9`!) { |
| 28 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 29 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 30 | } |
| 31 | |
| 32 | request = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(*request)); |
| 33 | if (!request) |
| 34 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 35 | |
| 36 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(request, 0, sizeof(*request)); |
| 37 | |
| 38 | request->chip_namecxx . . $ gpiod_line_request#chip_name. = strdupcxx . . $ strdup(a8b8e32347182212).(chip_name); |
| 39 | if (!request->chip_namecxx . . $ gpiod_line_request#chip_name.) { |
| 40 | freecxx . . $ free(5d22bdacc48458e8).(request); |
| 41 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 42 | } |
| 43 | |
| 44 | request->fdcxx . . $ gpiod_line_request#fd. = uapi_req->fdcxx . . $ gpio_v2_line_request#fd.; |
| 45 | request->num_linescxx . . $ gpiod_line_request#num_lines. = uapi_req->num_linescxx . . $ gpio_v2_line_request#num_lines.; |
| 46 | memcpycxx . . $ memcpy(cc817cb175f282d).(request->offsetscxx . . $ gpiod_line_request#offsets., uapi_req->offsetscxx . . $ gpio_v2_line_request#offsets., |
| 47 | sizeof(*request->offsetscxx . . $ gpiod_line_request#offsets.) * request->num_linescxx . . $ gpiod_line_request#num_lines.); |
| 48 | |
| 49 | return request; |
| 50 | } |
| 51 | |
| 52 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! void gpiod_line_request_releasecxx . . $ gpiod_line_request_release(19d596ef973cbf9).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request) |
| 53 | { |
| 54 | if (!request) |
| 55 | return; |
| 56 | |
| 57 | closecxx . . $ close(7864480464b09eea).(request->fdcxx . . $ gpiod_line_request#fd.); |
| 58 | freecxx . . $ free(5d22bdacc48458e8).(request->chip_namecxx . . $ gpiod_line_request#chip_name.); |
| 59 | freecxx . . $ free(5d22bdacc48458e8).(request); |
| 60 | } |
| 61 | |
| 62 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! const char * |
| 63 | gpiod_line_request_get_chip_namecxx . . $ gpiod_line_request_get_chip_name(3b6a9a1ba2c24295).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request) |
| 64 | { |
| 65 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 66 | |
| 67 | return request->chip_namecxx . . $ gpiod_line_request#chip_name.; |
| 68 | } |
| 69 | |
| 70 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! size_t |
| 71 | gpiod_line_request_get_num_requested_linescxx . . $ gpiod_line_request_get_num_requested_lines(2742e372a206b2f9).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request) |
| 72 | { |
| 73 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 74 | |
| 75 | return request->num_linescxx . . $ gpiod_line_request#num_lines.; |
| 76 | } |
| 77 | |
| 78 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! size_t |
| 79 | gpiod_line_request_get_requested_offsetscxx . . $ gpiod_line_request_get_requested_offsets(4d5791c4c69f835e).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 80 | unsigned int *offsets, |
| 81 | size_t max_offsets) |
| 82 | { |
| 83 | size_t num_offsets; |
| 84 | |
| 85 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 86 | |
| 87 | if (!offsets || !max_offsets) |
| 88 | return 0; |
| 89 | |
| 90 | num_offsets = MINcxx . . $ `<external>/1231830743887005719/param.h:102:9`!(request->num_lines, max_offsets); |
| 91 | |
| 92 | memcpycxx . . $ memcpy(cc817cb175f282d).(offsets, request->offsetscxx . . $ gpiod_line_request#offsets., sizeof(*offsets) * num_offsets); |
| 93 | |
| 94 | return num_offsets; |
| 95 | } |
| 96 | |
| 97 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! enum gpiod_line_valuecxx . . $ gpiod_line_value# |
| 98 | gpiod_line_request_get_valuecxx . . $ gpiod_line_request_get_value(3e7ca62f60f0a4a).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 99 | unsigned int offset) |
| 100 | { |
| 101 | enum gpiod_line_valuecxx . . $ gpiod_line_value# val; |
| 102 | int ret; |
| 103 | |
| 104 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 105 | |
| 106 | ret = gpiod_line_request_get_values_subsetcxx . . $ gpiod_line_request_get_values_subset(9e2f5f452f9d5486).(request, 1, &offset, &val); |
| 107 | if (ret) |
| 108 | return GPIOD_LINE_VALUE_ERRORcxx . . $ GPIOD_LINE_VALUE_ERROR.; |
| 109 | |
| 110 | return val; |
| 111 | } |
| 112 | |
| 113 | static int offset_to_bitcxx . . $ offset_to_bit(ff0b482876130f1d).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 114 | unsigned int offset) |
| 115 | { |
| 116 | size_t i; |
| 117 | |
| 118 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 119 | |
| 120 | for (i = 0; i < request->num_linescxx . . $ gpiod_line_request#num_lines.; i++) { |
| 121 | if (offset == request->offsetscxx . . $ gpiod_line_request#offsets.[i]) |
| 122 | return i; |
| 123 | } |
| 124 | |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int |
| 129 | gpiod_line_request_get_values_subsetcxx . . $ gpiod_line_request_get_values_subset(9e2f5f452f9d5486).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 130 | size_t num_values, |
| 131 | const unsigned int *offsets, |
| 132 | enum gpiod_line_valuecxx . . $ gpiod_line_value# *values) |
| 133 | { |
| 134 | struct gpio_v2_line_valuescxx . . $ gpio_v2_line_values# uapi_values; |
| 135 | uint64_tcxx . . $ uint64_t# mask = 0, bits = 0; |
| 136 | size_t i; |
| 137 | int bit, ret; |
| 138 | |
| 139 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 140 | |
| 141 | if (!offsets || !values) { |
| 142 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | uapi_values.bitscxx . . $ gpio_v2_line_values#bits. = 0; |
| 147 | |
| 148 | for (i = 0; i < num_values; i++) { |
| 149 | bit = offset_to_bitcxx . . $ offset_to_bit(ff0b482876130f1d).(request, offsets[i]); |
| 150 | if (bit < 0) { |
| 151 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | gpiod_line_mask_set_bitcxx . . $ gpiod_line_mask_set_bit(5e61894bc0b8b7).(&mask, bit); |
| 156 | } |
| 157 | |
| 158 | uapi_values.maskcxx . . $ gpio_v2_line_values#mask. = mask; |
| 159 | |
| 160 | ret = gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(request->fdcxx . . $ gpiod_line_request#fd., GPIO_V2_LINE_GET_VALUES_IOCTLcxx . . $ `lib/uapi/gpio.h:515:9`!, |
| 161 | &uapi_values); |
| 162 | if (ret) |
| 163 | return -1; |
| 164 | |
| 165 | bits = uapi_values.bitscxx . . $ gpio_v2_line_values#bits.; |
| 166 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(values, 0, sizeof(*values) * num_values); |
| 167 | |
| 168 | for (i = 0; i < num_values; i++) { |
| 169 | bit = offset_to_bitcxx . . $ offset_to_bit(ff0b482876130f1d).(request, offsets[i]); |
| 170 | values[i] = gpiod_line_mask_test_bitcxx . . $ gpiod_line_mask_test_bit(1e1585c25ba0c69a).(&bits, bit) ? 1 : 0; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_line_request_get_valuescxx . . $ gpiod_line_request_get_values(9c708df08c1c906d).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 177 | enum gpiod_line_valuecxx . . $ gpiod_line_value# *values) |
| 178 | { |
| 179 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 180 | |
| 181 | return gpiod_line_request_get_values_subsetcxx . . $ gpiod_line_request_get_values_subset(9e2f5f452f9d5486).(request, request->num_linescxx . . $ gpiod_line_request#num_lines., |
| 182 | request->offsetscxx . . $ gpiod_line_request#offsets., values); |
| 183 | } |
| 184 | |
| 185 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_line_request_set_valuecxx . . $ gpiod_line_request_set_value(6abdf3763b5f4ea4).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 186 | unsigned int offset, |
| 187 | enum gpiod_line_valuecxx . . $ gpiod_line_value# value) |
| 188 | { |
| 189 | return gpiod_line_request_set_values_subsetcxx . . $ gpiod_line_request_set_values_subset(e1023b5aab8aaa1b).(request, 1, |
| 190 | &offset, &value); |
| 191 | } |
| 192 | |
| 193 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int |
| 194 | gpiod_line_request_set_values_subsetcxx . . $ gpiod_line_request_set_values_subset(e1023b5aab8aaa1b).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 195 | size_t num_values, |
| 196 | const unsigned int *offsets, |
| 197 | const enum gpiod_line_valuecxx . . $ gpiod_line_value# *values) |
| 198 | { |
| 199 | struct gpio_v2_line_valuescxx . . $ gpio_v2_line_values# uapi_values; |
| 200 | uint64_tcxx . . $ uint64_t# mask = 0, bits = 0; |
| 201 | size_t i; |
| 202 | int bit; |
| 203 | |
| 204 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 205 | |
| 206 | if (!offsets || !values) { |
| 207 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | for (i = 0; i < num_values; i++) { |
| 212 | bit = offset_to_bitcxx . . $ offset_to_bit(ff0b482876130f1d).(request, offsets[i]); |
| 213 | if (bit < 0) { |
| 214 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | gpiod_line_mask_set_bitcxx . . $ gpiod_line_mask_set_bit(5e61894bc0b8b7).(&mask, bit); |
| 219 | gpiod_line_mask_assign_bitcxx . . $ gpiod_line_mask_assign_bit(aece0c19fb2b7302).(&bits, bit, values[i]); |
| 220 | } |
| 221 | |
| 222 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&uapi_values, 0, sizeof(uapi_values)); |
| 223 | uapi_values.maskcxx . . $ gpio_v2_line_values#mask. = mask; |
| 224 | uapi_values.bitscxx . . $ gpio_v2_line_values#bits. = bits; |
| 225 | |
| 226 | return gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(request->fdcxx . . $ gpiod_line_request#fd., GPIO_V2_LINE_SET_VALUES_IOCTLcxx . . $ `lib/uapi/gpio.h:516:9`!, |
| 227 | &uapi_values); |
| 228 | } |
| 229 | |
| 230 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_line_request_set_valuescxx . . $ gpiod_line_request_set_values(9c3972a6384194b2).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 231 | const enum gpiod_line_valuecxx . . $ gpiod_line_value# *values) |
| 232 | { |
| 233 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 234 | |
| 235 | return gpiod_line_request_set_values_subsetcxx . . $ gpiod_line_request_set_values_subset(e1023b5aab8aaa1b).(request, request->num_linescxx . . $ gpiod_line_request#num_lines., |
| 236 | request->offsetscxx . . $ gpiod_line_request#offsets., values); |
| 237 | } |
| 238 | |
| 239 | static boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! offsets_equalcxx . . $ offsets_equal(359cbf0ac40a3f03).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 240 | struct gpio_v2_line_requestcxx . . $ gpio_v2_line_request# *uapi_cfg) |
| 241 | { |
| 242 | size_t i; |
| 243 | |
| 244 | if (request->num_linescxx . . $ gpiod_line_request#num_lines. != uapi_cfg->num_linescxx . . $ gpio_v2_line_request#num_lines.) |
| 245 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 246 | |
| 247 | for (i = 0; i < request->num_linescxx . . $ gpiod_line_request#num_lines.; i++) { |
| 248 | if (request->offsetscxx . . $ gpiod_line_request#offsets.[i] != uapi_cfg->offsetscxx . . $ gpio_v2_line_request#offsets.[i]) |
| 249 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 250 | } |
| 251 | |
| 252 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 253 | } |
| 254 | |
| 255 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int |
| 256 | gpiod_line_request_reconfigure_linescxx . . $ gpiod_line_request_reconfigure_lines(2055b6483a431be9).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 257 | struct gpiod_line_configcxx . . $ gpiod_line_config# *config) |
| 258 | { |
| 259 | struct gpio_v2_line_requestcxx . . $ gpio_v2_line_request# uapi_cfg; |
| 260 | int ret; |
| 261 | |
| 262 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 263 | |
| 264 | if (!config) { |
| 265 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&uapi_cfg, 0, sizeof(uapi_cfg)); |
| 270 | |
| 271 | ret = gpiod_line_config_to_uapicxx . . $ gpiod_line_config_to_uapi(180b8730a233f01).(config, &uapi_cfg); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | if (!offsets_equalcxx . . $ offsets_equal(359cbf0ac40a3f03).(request, &uapi_cfg)) { |
| 276 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | ret = gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(request->fdcxx . . $ gpiod_line_request#fd., GPIO_V2_LINE_SET_CONFIG_IOCTLcxx . . $ `lib/uapi/gpio.h:514:9`!, |
| 281 | &uapi_cfg.configcxx . . $ gpio_v2_line_request#config.); |
| 282 | if (ret) |
| 283 | return ret; |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int gpiod_line_request_get_fdcxx . . $ gpiod_line_request_get_fd(4bcc054dff07e178).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request) |
| 289 | { |
| 290 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 291 | |
| 292 | return request->fdcxx . . $ gpiod_line_request#fd.; |
| 293 | } |
| 294 | |
| 295 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int |
| 296 | gpiod_line_request_wait_edge_eventscxx . . $ gpiod_line_request_wait_edge_events(e2842b52c17809bc).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 297 | int64_tcxx . . $ int64_t# timeout_ns) |
| 298 | { |
| 299 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 300 | |
| 301 | return gpiod_poll_fdcxx . . $ gpiod_poll_fd(bba9d6e9fb34fad7).(request->fdcxx . . $ gpiod_line_request#fd., timeout_ns); |
| 302 | } |
| 303 | |
| 304 | GPIOD_APIcxx . . $ `lib/internal.h:15:9`! int |
| 305 | gpiod_line_request_read_edge_eventscxx . . $ gpiod_line_request_read_edge_events(87e5cb81a69dea7d).(struct gpiod_line_requestcxx . . $ gpiod_line_request# *request, |
| 306 | struct gpiod_edge_event_buffercxx . . $ gpiod_edge_event_buffer# *buffer, |
| 307 | size_t max_events) |
| 308 | { |
| 309 | assertcxx . . $ `<external>/4398592474888995393/assert.h:156:12`!(request); |
| 310 | |
| 311 | return gpiod_edge_event_buffer_read_fdcxx . . $ gpiod_edge_event_buffer_read_fd(968ac207299343e2).(request->fdcxx . . $ gpiod_line_request#fd., buffer, max_events); |
| 312 | } |