Skip to content

Commit 7c635e5

Browse files
committed
Add support for libgpiod v2+ API
libgpiod in version 2 or above introduced an API change which results in compile error with the current code. This commit adds some glue magic for the newer versions and tries to detect the used libgpiod version based on the information available in the pkg-config files. At the moment, this eliminates the possibility to statically link against this library. Signed-off-by: Michael Heimpold <[email protected]>
1 parent 34366a6 commit 7c635e5

File tree

3 files changed

+185
-9
lines changed

3 files changed

+185
-9
lines changed

CMakeLists.txt

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ if(USE_STATIC_LIBS)
126126
set(PREFERRED_LIBFTDI1 libftdi1.a ftdi1)
127127
set(PREFERRED_LIBREADLINE libreadline.a readline)
128128
set(PREFERRED_LIBSERIALPORT libserialport.a serialport)
129-
set(PREFERRED_LIBGPIOD libgpiod.a gpiod)
130129
else()
131130
set(PREFERRED_LIBELF elf)
132131
set(PREFERRED_LIBUSB usb)
@@ -136,7 +135,6 @@ else()
136135
set(PREFERRED_LIBFTDI1 ftdi1)
137136
set(PREFERRED_LIBREADLINE readline)
138137
set(PREFERRED_LIBSERIALPORT serialport)
139-
set(PREFERRED_LIBGPIOD gpiod)
140138
endif()
141139

142140
# -------------------------------------
@@ -237,12 +235,32 @@ if(HAVE_LIBSERIALPORT)
237235
endif()
238236

239237
# -------------------------------------
240-
# Find libgpiod, if needed
238+
# Find libgpiod using pkg-config, if needed
241239
if(HAVE_LINUXGPIO)
242-
find_library(HAVE_LIBGPIOD NAMES ${PREFERRED_LIBGPIOD})
243-
if(HAVE_LIBGPIOD)
244-
set(LIB_LIBGPIOD ${HAVE_LIBGPIOD})
245-
set(CMAKE_REQUIRED_LIBRARIES ${LIB_LIBGPIOD})
240+
# defaults/fallbacks
241+
set(HAVE_LIBGPIOD 0)
242+
set(HAVE_LIBGPIOD_V2 0)
243+
244+
find_package(PkgConfig REQUIRED)
245+
if(PKG_CONFIG_FOUND)
246+
# check whether we have version >= 2.0
247+
pkg_check_modules(LIBGPIODV2 libgpiod>=2.0)
248+
if(LIBGPIODV2_FOUND)
249+
set(HAVE_LIBGPIOD 1)
250+
set(HAVE_LIBGPIOD_V2 1)
251+
set(CMAKE_REQUIRED_LIBRARIES ${LIBGPIODV2_LIBRARIES})
252+
set(LIB_LIBGPIOD ${LIBGPIODV2_LINK_LIBRARIES})
253+
else()
254+
# check whether we have at least an older version
255+
pkg_check_modules(LIBGPIOD libgpiod)
256+
if(LIBGPIOD_FOUND)
257+
set(HAVE_LIBGPIOD 1)
258+
set(CMAKE_REQUIRED_LIBRARIES ${LIBGPIOD_LIBRARIES})
259+
set(LIB_LIBGPIOD ${LIBGPIOD_LINK_LIBRARIES})
260+
endif()
261+
endif()
262+
else()
263+
message(WARNING "For using libgpiod, pkg-config would be required which is not available.")
246264
endif()
247265
endif()
248266

@@ -339,7 +357,8 @@ if (DEBUG_CMAKE)
339357
message(STATUS "HAVE_LIBUSB_1_0_LIBUSB_H: ${HAVE_LIBUSB_1_0_LIBUSB_H}")
340358
message(STATUS "HAVE_HIDAPI_HIDAPI_H: ${HAVE_HIDAPI_HIDAPI_H}")
341359
message(STATUS "LIBUSB_COMPAT_DIR: ${LIBUSB_COMPAT_DIR}")
342-
message(STATUS "HAVE_LIBGPIOD: ${HAVE_LIBGPIOD}")
360+
message(STATUS "LIBGPIODV2_FOUND: ${LIBGPIODV2_FOUND}")
361+
message(STATUS "LIBGPIOD_FOUND: ${LIBGPIOD_FOUND}")
343362
message(STATUS "----------------------")
344363
endif()
345364

@@ -409,7 +428,9 @@ endif()
409428

410429
if(HAVE_LINUXGPIO)
411430
message(STATUS "ENABLED linuxgpio")
412-
if (HAVE_LIBGPIOD)
431+
if (LIBGPIODV2_FOUND)
432+
message(STATUS "DO HAVE libgpiod (v2.x)")
433+
elseif(LIBGPIOD_FOUND)
413434
message(STATUS "DO HAVE libgpiod")
414435
else()
415436
message(STATUS "DON'T HAVE libgpiod")

src/cmake_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
/* Let linuxgpio know if libgpiod is available. */
3131
#cmakedefine HAVE_LIBGPIOD
3232

33+
/* Let linuxgpio know whether v2 of libgpiod is available. */
34+
#cmakedefine HAVE_LIBGPIOD_V2
35+
3336
/* Linux SPI support enabled */
3437
#cmakedefine HAVE_LINUXSPI 1
3538

src/linuxgpio.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,158 @@ static void linuxgpio_sysfs_close(PROGRAMMER *pgm)
337337

338338
#ifdef HAVE_LIBGPIOD
339339

340+
#ifdef HAVE_LIBGPIOD_V2
341+
342+
struct gpiod_line {
343+
struct gpiod_chip *chip;
344+
struct gpiod_line_request *line_request;
345+
unsigned int gpio_num;
346+
};
347+
348+
struct gpiod_line *gpiod_line_get(const char *port, int gpio_num) {
349+
struct gpiod_line *rv;
350+
351+
rv = calloc(sizeof(struct gpiod_line), 1);
352+
if (!rv)
353+
return NULL;
354+
355+
rv->gpio_num = gpio_num;
356+
357+
rv->chip = gpiod_chip_open(port);
358+
if (!rv->chip) {
359+
free(rv);
360+
return NULL;
361+
}
362+
363+
return rv;
364+
}
365+
366+
int gpiod_line_request_input(struct gpiod_line *gpio_line, const char *consumer) {
367+
struct gpiod_line_settings *line_settings = NULL;
368+
struct gpiod_line_config *line_config = NULL;
369+
struct gpiod_request_config *req_cfg = NULL;
370+
int retval = -1;
371+
372+
line_settings = gpiod_line_settings_new();
373+
line_config = gpiod_line_config_new();
374+
req_cfg = gpiod_request_config_new();
375+
376+
if (!line_settings || !line_config || !req_cfg)
377+
goto err_out;
378+
379+
retval = gpiod_line_settings_set_direction(line_settings, GPIOD_LINE_DIRECTION_INPUT);
380+
if (retval != 0)
381+
goto err_out;
382+
383+
retval = gpiod_line_config_add_line_settings(line_config, &gpio_line->gpio_num, 1, line_settings);
384+
if (retval != 0)
385+
goto err_out;
386+
387+
gpiod_request_config_set_consumer(req_cfg, consumer);
388+
389+
gpio_line->line_request = gpiod_chip_request_lines(gpio_line->chip, req_cfg, line_config);
390+
if (!gpio_line->line_request)
391+
goto err_out;
392+
393+
retval = 0;
394+
395+
err_out:
396+
gpiod_line_settings_free(line_settings);
397+
gpiod_line_config_free(line_config);
398+
gpiod_request_config_free(req_cfg);
399+
return retval;
400+
}
401+
402+
int gpiod_line_request_output(struct gpiod_line *gpio_line, const char *consumer, int value) {
403+
struct gpiod_line_settings *line_settings = NULL;
404+
struct gpiod_line_config *line_config = NULL;
405+
struct gpiod_request_config *req_cfg = NULL;
406+
int retval = -1;
407+
408+
line_settings = gpiod_line_settings_new();
409+
line_config = gpiod_line_config_new();
410+
req_cfg = gpiod_request_config_new();
411+
412+
if (!line_settings || !line_config || !req_cfg)
413+
goto err_out;
414+
415+
retval = gpiod_line_settings_set_direction(line_settings, GPIOD_LINE_DIRECTION_OUTPUT);
416+
if (retval != 0)
417+
goto err_out;
418+
419+
retval = gpiod_line_settings_set_output_value(line_settings, value ? GPIOD_LINE_VALUE_ACTIVE : GPIOD_LINE_VALUE_INACTIVE);
420+
if (retval != 0)
421+
goto err_out;
422+
423+
retval = gpiod_line_config_add_line_settings(line_config, &gpio_line->gpio_num, 1, line_settings);
424+
if (retval != 0)
425+
goto err_out;
426+
427+
gpiod_request_config_set_consumer(req_cfg, consumer);
428+
429+
gpio_line->line_request = gpiod_chip_request_lines(gpio_line->chip, req_cfg, line_config);
430+
if (!gpio_line->line_request)
431+
goto err_out;
432+
433+
retval = 0;
434+
435+
err_out:
436+
gpiod_line_settings_free(line_settings);
437+
gpiod_line_config_free(line_config);
438+
gpiod_request_config_free(req_cfg);
439+
return retval;
440+
}
441+
442+
int gpiod_line_set_direction_input(struct gpiod_line *gpio_line) {
443+
struct gpiod_line_settings *line_settings = NULL;
444+
struct gpiod_line_config *line_config = NULL;
445+
int retval = -1;
446+
447+
line_settings = gpiod_line_settings_new();
448+
line_config = gpiod_line_config_new();
449+
450+
if (!line_settings || !line_config)
451+
goto err_out;
452+
453+
retval = gpiod_line_settings_set_direction(line_settings, GPIOD_LINE_DIRECTION_INPUT);
454+
if (retval != 0)
455+
goto err_out;
456+
457+
retval = gpiod_line_config_add_line_settings(line_config, &gpio_line->gpio_num, 1, line_settings);
458+
if (retval != 0)
459+
goto err_out;
460+
461+
retval = gpiod_line_request_reconfigure_lines(gpio_line->line_request, line_config);
462+
463+
err_out:
464+
gpiod_line_settings_free(line_settings);
465+
gpiod_line_config_free(line_config);
466+
return retval;
467+
}
468+
469+
/* this helper is not thread safe, but we are not using threads... */
470+
char *gpiod_line_name(struct gpiod_line *gpio_line) {
471+
static char buffer[16];
472+
snprintf(buffer, sizeof(buffer), "%u", gpio_line->gpio_num);
473+
return buffer;
474+
}
475+
476+
void gpiod_line_release(struct gpiod_line *gpio_line) {
477+
gpiod_line_request_release(gpio_line->line_request);
478+
gpiod_chip_close(gpio_line->chip);
479+
free(gpio_line);
480+
}
481+
482+
static inline int gpiod_line_set_value(struct gpiod_line *gpio_line, int value) {
483+
return gpiod_line_request_set_value(gpio_line->line_request, gpio_line->gpio_num, value);
484+
}
485+
486+
static inline int gpiod_line_get_value(struct gpiod_line *gpio_line) {
487+
return gpiod_line_request_get_value(gpio_line->line_request, gpio_line->gpio_num);
488+
}
489+
490+
#endif
491+
340492
struct gpiod_line * linuxgpio_libgpiod_lines[N_PINS];
341493

342494
// Try to tell if libgpiod is going to work.

0 commit comments

Comments
 (0)