| 1 | // SPDX-License-Identifier: LGPL-2.1-or-latercxx . . $ `<file>/lib/internal.c`/ |
| 2 | // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <errno.h> |
| 5 | #include <poll.h> |
| 6 | #include <stdint.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <sys/ioctl.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/sysmacros.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "internal.h"cxx . . $ `<file>/lib/internal.h`/ |
| 17 | |
| 18 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! gpiod_check_gpiochip_devicecxx . . $ gpiod_check_gpiochip_device(e96b1cf346ae2c6).(const char *path, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! set_errno) |
| 19 | { |
| 20 | char *realname, *sysfsp, devpath[64]; |
| 21 | struct statcxx . . $ stat# statbuf; |
| 22 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! ret = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 23 | int rv; |
| 24 | |
| 25 | if (!path) { |
| 26 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 27 | goto out; |
| 28 | } |
| 29 | |
| 30 | rv = lstatcxx . . $ lstat(a9cd1e23e96f423c).(path, &statbuf); |
| 31 | if (rv) |
| 32 | goto out; |
| 33 | |
| 34 | /* |
| 35 | * Is it a symbolic link? We have to resolve it before checking |
| 36 | * the rest. |
| 37 | */ |
| 38 | realname = S_ISLNKcxx . . $ `<external>/531885356569766532/stat.h:133:10`!(statbuf.st_mode) ? realpathcxx . . $ realpath(ed8193f262e4eda).(path, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) : |
| 39 | strdupcxx . . $ strdup(a8b8e32347182212).(path); |
| 40 | if (realname == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) |
| 41 | goto out; |
| 42 | |
| 43 | rv = statcxx . . $ stat(a9cd1e23e96f423c).(realname, &statbuf); |
| 44 | if (rv) |
| 45 | goto out_free_realname; |
| 46 | |
| 47 | /* Is it a character device? */ |
| 48 | if (!S_ISCHRcxx . . $ `<external>/531885356569766532/stat.h:126:9`!(statbuf.st_mode)) { |
| 49 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOTTYcxx . . $ `<external>/8987895939074593727/errno-base.h:29:9`!; |
| 50 | goto out_free_realname; |
| 51 | } |
| 52 | |
| 53 | /* Is the device associated with the GPIO subsystem? */ |
| 54 | snprintfcxx . . $ snprintf(ea502d73e74c754c).(devpath, sizeof(devpath), "/sys/dev/char/%u:%u/subsystem", |
| 55 | majorcxx . . $ `<external>/12452211444218344808/sysmacros.h:60:9`!(statbuf.st_rdev), minorcxx . . $ `<external>/12452211444218344808/sysmacros.h:61:9`!(statbuf.st_rdev)); |
| 56 | |
| 57 | sysfsp = realpathcxx . . $ realpath(ed8193f262e4eda).(devpath, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 58 | if (!sysfsp) |
| 59 | goto out_free_realname; |
| 60 | |
| 61 | /* |
| 62 | * In glibc, if any of the underlying readlink() calls fail (which is |
| 63 | * perfectly normal when resolving paths), errno is not cleared. |
| 64 | */ |
| 65 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 66 | |
| 67 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(sysfsp, "/sys/bus/gpio") != 0) { |
| 68 | /* This is a character device but not the one we're after. */ |
| 69 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENODEVcxx . . $ `<external>/8987895939074593727/errno-base.h:23:9`!; |
| 70 | goto out_free_sysfsp; |
| 71 | } |
| 72 | |
| 73 | ret = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 74 | |
| 75 | out_free_sysfsp: |
| 76 | freecxx . . $ free(5d22bdacc48458e8).(sysfsp); |
| 77 | out_free_realname: |
| 78 | freecxx . . $ free(5d22bdacc48458e8).(realname); |
| 79 | out: |
| 80 | if (!set_errno) |
| 81 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0; |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | int gpiod_poll_fdcxx . . $ gpiod_poll_fd(bba9d6e9fb34fad7).(int fd, int64_tcxx . . $ int64_t# timeout_ns) |
| 86 | { |
| 87 | struct timespeccxx . . $ timespec# ts; |
| 88 | struct pollfdcxx . . $ pollfd# pfd; |
| 89 | int ret; |
| 90 | |
| 91 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(&pfd, 0, sizeof(pfd)); |
| 92 | pfd.fdcxx . . $ pollfd#fd. = fd; |
| 93 | pfd.eventscxx . . $ pollfd#events. = POLLINcxx . . $ `<external>/17223861378049612178/poll.h:25:9`! | POLLPRIcxx . . $ `<external>/17223861378049612178/poll.h:26:9`!; |
| 94 | |
| 95 | if (timeout_ns >= 0) { |
| 96 | ts.tv_seccxx . . $ timespec#tv_sec. = timeout_ns / 1000000000ULL; |
| 97 | ts.tv_nseccxx . . $ timespec#tv_nsec. = timeout_ns % 1000000000ULL; |
| 98 | } |
| 99 | |
| 100 | ret = ppollcxx . . $ ppoll(472204401a07f0f1).(&pfd, 1, timeout_ns < 0 ? NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! : &ts, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 101 | if (ret < 0) |
| 102 | return -1; |
| 103 | else if (ret == 0) |
| 104 | return 0; |
| 105 | |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | int gpiod_set_output_valuecxx . . $ gpiod_set_output_value(eb3e60c472d1bd0b).(enum gpiod_line_valuecxx . . $ gpiod_line_value# in, enum gpiod_line_valuecxx . . $ gpiod_line_value# *out) |
| 110 | { |
| 111 | switch (in) { |
| 112 | case GPIOD_LINE_VALUE_INACTIVEcxx . . $ GPIOD_LINE_VALUE_INACTIVE.: |
| 113 | case GPIOD_LINE_VALUE_ACTIVEcxx . . $ GPIOD_LINE_VALUE_ACTIVE.: |
| 114 | *out = in; |
| 115 | break; |
| 116 | default: |
| 117 | *out = GPIOD_LINE_VALUE_INACTIVEcxx . . $ GPIOD_LINE_VALUE_INACTIVE.; |
| 118 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int gpiod_ioctlcxx . . $ gpiod_ioctl(abf9bb955e3c4896).(int fd, unsigned long request, void *arg) |
| 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | ret = ioctlcxx . . $ ioctl(67fa1c773b05068c).(fd, request, arg); |
| 130 | if (ret <= 0) |
| 131 | return ret; |
| 132 | |
| 133 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EBADEcxx . . $ `<external>/17922322188510340118/errno.h:33:9`!; |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | void gpiod_line_mask_zerocxx . . $ gpiod_line_mask_zero(386f4d2be08c489b).(uint64_tcxx . . $ uint64_t# *mask) |
| 138 | { |
| 139 | *mask = 0ULL; |
| 140 | } |
| 141 | |
| 142 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! gpiod_line_mask_test_bitcxx . . $ gpiod_line_mask_test_bit(1e1585c25ba0c69a).(const uint64_tcxx . . $ uint64_t# *mask, unsigned int nr) |
| 143 | { |
| 144 | return *mask & (1ULL << nr); |
| 145 | } |
| 146 | |
| 147 | void gpiod_line_mask_set_bitcxx . . $ gpiod_line_mask_set_bit(5e61894bc0b8b7).(uint64_tcxx . . $ uint64_t# *mask, unsigned int nr) |
| 148 | { |
| 149 | *mask |= (1ULL << nr); |
| 150 | } |
| 151 | |
| 152 | void gpiod_line_mask_assign_bitcxx . . $ gpiod_line_mask_assign_bit(aece0c19fb2b7302).(uint64_tcxx . . $ uint64_t# *mask, unsigned int nr, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! value) |
| 153 | { |
| 154 | if (value) |
| 155 | gpiod_line_mask_set_bitcxx . . $ gpiod_line_mask_set_bit(5e61894bc0b8b7).(mask, nr); |
| 156 | else |
| 157 | *mask &= ~(1ULL << nr); |
| 158 | } |