cash2ecash

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

gpiod_wrapper.c (3758B)


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