cash2ecash

cash2ecash: cash acceptor that issues digital cash (experimental)
Log | Files | Refs | README | LICENSE

gpiod_wrapper.c (4429B)


      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 #include "gpiod_wrapper.h"
     21 
     22 struct gpiod_chip *gpiod_chip_open_by_name(const char *name)
     23 {
     24     struct gpiod_chip *chip;
     25     char *path;
     26     int ret;
     27 
     28     ret = asprintf(&path, "/dev/%s", name);
     29     if (ret < 0)
     30     {
     31         perror("asprint() memory-allocation failed");
     32         return NULL;
     33     }
     34         
     35 
     36     chip = gpiod_chip_open(path);
     37     free(path);
     38 
     39     return chip;
     40 }
     41 
     42 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)
     43 {
     44     int ret = 0;
     45     struct gpiod_line_settings* settings = gpiod_line_settings_new();
     46     if (!settings)
     47     {
     48         perror("gpiod_line_settings_new() failed");
     49         return NULL;
     50     }
     51 		
     52     ret |= gpiod_line_settings_set_direction(settings, direction);
     53     ret |= gpiod_line_settings_set_bias(settings, bias);
     54     ret |= gpiod_line_settings_set_drive(settings, drive);
     55            gpiod_line_settings_set_active_low(settings, active_low);
     56     if (ret)
     57     {
     58         perror("gpiod_line_settings_set....() failed");
     59         return NULL;
     60     }
     61 		
     62     
     63     return settings;
     64 }
     65 
     66 struct gpiod_line_request* gpiod_make_line_request_by_name(const char* chipname, const char* linename, struct gpiod_line_settings* settings)
     67 {
     68     struct gpiod_request_config* request_cfg = NULL; //for kernel settings, not used here
     69 	struct gpiod_line_request*  request = NULL;
     70 	struct gpiod_line_config*   line_cfg;
     71 	struct gpiod_chip*          chip;
     72 	int ret = 0;
     73 
     74 
     75     chip = gpiod_chip_open_by_name(chipname);
     76     if (!chip)
     77     {
     78         perror("gpiod_chip_open_by_name() failed");
     79         goto free_settings; 
     80     }
     81         
     82     
     83     const int line_offset = gpiod_chip_get_line_offset_from_name(chip, linename);
     84     if(line_offset<0)
     85     {
     86         perror("gpiod_chip_get_line_offset_from_name() failed");
     87         goto close_chip;
     88     }
     89         
     90     const unsigned int u_line_offset = (unsigned int)line_offset;
     91 
     92     line_cfg = gpiod_line_config_new();
     93     if (!line_cfg)
     94     {
     95         perror("gpiod_line_config_new() failed");
     96         goto close_chip;
     97     }
     98         
     99     
    100     ret = gpiod_line_config_add_line_settings(line_cfg, &u_line_offset, 1, settings);
    101      if (ret)
    102      {
    103         perror("gpiod_line_config_add_line_settings() failed");
    104         goto free_line_cfg;
    105      }
    106 		
    107 
    108     request = gpiod_chip_request_lines(chip, request_cfg, line_cfg);
    109 
    110 free_line_cfg:
    111 	gpiod_line_config_free(line_cfg);
    112 
    113 close_chip:
    114 	gpiod_chip_close(chip);
    115 
    116 free_settings:
    117 	gpiod_line_settings_free(settings);
    118 
    119     return request; //NULL on error
    120 }
    121 
    122 struct gpiod_line_request* gpiod_make_line_request(const char* chip_path, const unsigned int line_offset, struct gpiod_line_settings* settings)
    123 {
    124     struct gpiod_line_request*  request = NULL;
    125 	struct gpiod_line_config*   line_cfg;
    126 	struct gpiod_chip*          chip;
    127 	int ret = 0;
    128 
    129     chip = gpiod_chip_open(chip_path);
    130     if (!chip)
    131     {
    132         perror("gpiod_chip_open_by_name() failed");
    133         goto free_settings; 
    134     }
    135 
    136 
    137     line_cfg = gpiod_line_config_new();
    138     if (!line_cfg)
    139     {
    140         perror("gpiod_line_config_new() failed");
    141         goto close_chip;
    142     }
    143         
    144     
    145     ret = gpiod_line_config_add_line_settings(line_cfg, &line_offset, 1, settings);
    146      if (ret)
    147      {
    148         perror("gpiod_line_config_add_line_settings() failed");
    149         goto free_line_cfg;
    150      }
    151 		
    152 
    153     request = gpiod_chip_request_lines(chip, NULL, line_cfg);
    154 
    155 free_line_cfg:
    156 	gpiod_line_config_free(line_cfg);
    157 
    158 close_chip:
    159 	gpiod_chip_close(chip);
    160 
    161 free_settings:
    162 	gpiod_line_settings_free(settings);
    163 
    164     return request; //NULL on error
    165 }