gpiod_wrapper.c (4519B)
1 /* 2 This file is part of TALER cash2ecash 3 Copyright (C) 2026 GNUnet e.V. 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU Affero General Public License as 7 published by the Free Software Foundation, either version 3 of the 8 License, or (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <https://www.gnu.org/licenses/>. 18 */ 19 20 /** 21 * @file gpiod_wrapper.c 22 * @brief controll gpio pins 23 * @author Reto Tellenbach 24 */ 25 26 #include "gpiod_wrapper.h" 27 28 struct gpiod_chip *gpiod_chip_open_by_name(const char *name) 29 { 30 struct gpiod_chip *chip; 31 char *path; 32 int ret; 33 34 ret = asprintf(&path, "/dev/%s", name); 35 if (ret < 0) 36 { 37 perror("asprint() memory-allocation failed"); 38 return NULL; 39 } 40 41 42 chip = gpiod_chip_open(path); 43 free(path); 44 45 return chip; 46 } 47 48 struct gpiod_line_settings* gpiod_make_settings(enum gpiod_line_direction direction,enum gpiod_line_bias bias, enum gpiod_line_drive drive, enum gpiod_line_drive active_low) 49 { 50 int ret = 0; 51 struct gpiod_line_settings* settings = gpiod_line_settings_new(); 52 if (!settings) 53 { 54 perror("gpiod_line_settings_new() failed"); 55 return NULL; 56 } 57 58 ret |= gpiod_line_settings_set_direction(settings, direction); 59 ret |= gpiod_line_settings_set_bias(settings, bias); 60 ret |= gpiod_line_settings_set_drive(settings, drive); 61 gpiod_line_settings_set_active_low(settings, active_low); 62 if (ret) 63 { 64 perror("gpiod_line_settings_set....() failed"); 65 return NULL; 66 } 67 68 69 return settings; 70 } 71 72 struct gpiod_line_request* gpiod_make_line_request_by_name(const char* chipname, const char* linename, struct gpiod_line_settings* settings) 73 { 74 struct gpiod_request_config* request_cfg = NULL; //for kernel settings, not used here 75 struct gpiod_line_request* request = NULL; 76 struct gpiod_line_config* line_cfg; 77 struct gpiod_chip* chip; 78 int ret = 0; 79 80 81 chip = gpiod_chip_open_by_name(chipname); 82 if (!chip) 83 { 84 perror("gpiod_chip_open_by_name() failed"); 85 goto free_settings; 86 } 87 88 89 const int line_offset = gpiod_chip_get_line_offset_from_name(chip, linename); 90 if(line_offset<0) 91 { 92 perror("gpiod_chip_get_line_offset_from_name() failed"); 93 goto close_chip; 94 } 95 96 const unsigned int u_line_offset = (unsigned int)line_offset; 97 98 line_cfg = gpiod_line_config_new(); 99 if (!line_cfg) 100 { 101 perror("gpiod_line_config_new() failed"); 102 goto close_chip; 103 } 104 105 106 ret = gpiod_line_config_add_line_settings(line_cfg, &u_line_offset, 1, settings); 107 if (ret) 108 { 109 perror("gpiod_line_config_add_line_settings() failed"); 110 goto free_line_cfg; 111 } 112 113 114 request = gpiod_chip_request_lines(chip, request_cfg, line_cfg); 115 116 free_line_cfg: 117 gpiod_line_config_free(line_cfg); 118 119 close_chip: 120 gpiod_chip_close(chip); 121 122 free_settings: 123 gpiod_line_settings_free(settings); 124 125 return request; //NULL on error 126 } 127 128 struct gpiod_line_request* gpiod_make_line_request(const char* chip_path, const unsigned int line_offset, struct gpiod_line_settings* settings) 129 { 130 struct gpiod_line_request* request = NULL; 131 struct gpiod_line_config* line_cfg; 132 struct gpiod_chip* chip; 133 int ret = 0; 134 135 chip = gpiod_chip_open(chip_path); 136 if (!chip) 137 { 138 perror("gpiod_chip_open_by_name() failed"); 139 goto free_settings; 140 } 141 142 143 line_cfg = gpiod_line_config_new(); 144 if (!line_cfg) 145 { 146 perror("gpiod_line_config_new() failed"); 147 goto close_chip; 148 } 149 150 151 ret = gpiod_line_config_add_line_settings(line_cfg, &line_offset, 1, settings); 152 if (ret) 153 { 154 perror("gpiod_line_config_add_line_settings() failed"); 155 goto free_line_cfg; 156 } 157 158 159 request = gpiod_chip_request_lines(chip, NULL, line_cfg); 160 161 free_line_cfg: 162 gpiod_line_config_free(line_cfg); 163 164 close_chip: 165 gpiod_chip_close(chip); 166 167 free_settings: 168 gpiod_line_settings_free(settings); 169 170 return request; //NULL on error 171 }