1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00

libgpiosim: new library for controlling the gpio-sim module

Add a C library for controlling the gpio-sim kernel module from various
libgpiod test suites. This aims at replacing the old gpio-mockup module
and its user-space library - libgpio-mockup - in the project's tree.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
This commit is contained in:
Bartosz Golaszewski
2021-04-12 11:45:57 +02:00
parent 0caea00bf1
commit a2f6e8cd35
7 changed files with 1283 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
# SPDX-FileCopyrightText: 2017-2022 Bartosz Golaszewski <brgl@bgdev.pl>
AC_PREREQ([2.69])
@@ -31,6 +31,7 @@ AC_SUBST(ABI_CXX_VERSION, [2.1.1])
# ABI version for libgpiomockup (we need this since it can be installed if we
# enable install-tests).
AC_SUBST(ABI_MOCKUP_VERSION, [0.1.0])
AC_SUBST(ABI_GPIOSIM_VERSION, [0.1.0])
AC_CONFIG_AUX_DIR([autostuff])
AC_CONFIG_MACRO_DIRS([m4])
@@ -126,10 +127,11 @@ AC_DEFUN([FUNC_NOT_FOUND_TESTS],
if test "x$with_tests" = xtrue
then
# For libgpiomockup
# For libgpiomockup & libgpiosim
AC_CHECK_FUNC([qsort], [], [FUNC_NOT_FOUND_TESTS([qsort])])
PKG_CHECK_MODULES([KMOD], [libkmod >= 18])
PKG_CHECK_MODULES([UDEV], [libudev >= 215])
PKG_CHECK_MODULES([MOUNT], [mount >= 2.33.1])
# For core library tests
PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.50])
@@ -224,6 +226,7 @@ AC_CONFIG_FILES([Makefile
tools/Makefile
tests/Makefile
tests/mockup/Makefile
tests/gpiosim/Makefile
bindings/cxx/libgpiodcxx.pc
bindings/Makefile
bindings/cxx/Makefile

View File

@@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2017-2021 Bartosz Golaszewski <bartekgola@gmail.com>
# SPDX-FileCopyrightText: 2017-2022 Bartosz Golaszewski <brgl@bgdev.pl>
SUBDIRS = mockup
SUBDIRS = mockup gpiosim
AM_CFLAGS = -I$(top_srcdir)/include/ -I$(top_srcdir)/tests/mockup/
AM_CFLAGS += -include $(top_builddir)/config.h

4
tests/gpiosim/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2017-2022 Bartosz Golaszewski <brgl@bgdev.pl>
gpiosim-selftest

16
tests/gpiosim/Makefile.am Normal file
View File

@@ -0,0 +1,16 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <brgl@bgdev.pl>
lib_LTLIBRARIES = libgpiosim.la
noinst_PROGRAMS = gpiosim-selftest
AM_CFLAGS = -Wall -Wextra -g -fvisibility=hidden -std=gnu89
AM_CFLAGS += -include $(top_builddir)/config.h
libgpiosim_la_SOURCES = gpiosim.c gpiosim.h
libgpiosim_la_CFLAGS = $(AM_CFLAGS) $(KMOD_CFLAGS) $(MOUNT_CFLAGS)
libgpiosim_la_LDFLAGS = -version-info $(subst .,:,$(ABI_GPIOSIM_VERSION))
libgpiosim_la_LDFLAGS += $(KMOD_LIBS) $(MOUNT_LIBS)
gpiosim_selftest_SOURCES = gpiosim-selftest.c
gpiosim_selftest_LDADD = libgpiosim.la

View File

@@ -0,0 +1,157 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <brgl@bgdev.pl>
#include <stdio.h>
#include <stdlib.h>
#include "gpiosim.h"
#define UNUSED __attribute__((unused))
static const char *const line_names[] = {
"foo",
"bar",
"foobar",
NULL,
"barfoo",
};
int main(int argc UNUSED, char **argv UNUSED)
{
struct gpiosim_bank *bank0, *bank1;
struct gpiosim_dev *dev;
struct gpiosim_ctx *ctx;
int ret, i;
printf("Creating gpiosim context\n");
ctx = gpiosim_ctx_new();
if (!ctx) {
perror("unable to create the gpios-sim context");
return EXIT_FAILURE;
}
printf("Creating a chip with random name\n");
dev = gpiosim_dev_new(ctx, NULL);
if (!dev) {
perror("Unable to create a chip with random name");
return EXIT_FAILURE;
}
printf("Creating a bank with a random name\n");
bank0 = gpiosim_bank_new(dev, NULL);
if (!bank0) {
perror("Unable to create a bank with random name");
return EXIT_FAILURE;
}
printf("Creating a bank with a specific name\n");
bank1 = gpiosim_bank_new(dev, "foobar");
if (!bank1) {
perror("Unable to create a bank with a specific name");
return EXIT_FAILURE;
}
printf("Setting the label of bank #2 to foobar\n");
ret = gpiosim_bank_set_label(bank1, "foobar");
if (ret) {
perror("Unable to set the label of bank #2");
return EXIT_FAILURE;
}
printf("Setting the number of lines in bank #1 to 16\n");
ret = gpiosim_bank_set_num_lines(bank0, 16);
if (ret) {
perror("Unable to set the number of lines");
return EXIT_FAILURE;
}
printf("Setting the number of lines in bank #2 to 8\n");
ret = gpiosim_bank_set_num_lines(bank1, 8);
if (ret) {
perror("Unable to set the number of lines");
return EXIT_FAILURE;
}
printf("Setting names for some lines in bank #1\n");
for (i = 0; i < 5; i++) {
ret = gpiosim_bank_set_line_name(bank0, i, line_names[i]);
if (ret) {
perror("Unable to set line names");
return EXIT_FAILURE;
}
}
printf("Hog a line on bank #2\n");
ret = gpiosim_bank_hog_line(bank1, 3, "xyz",
GPIOSIM_HOG_DIR_OUTPUT_HIGH);
if (ret) {
perror("Unable to hog a line");
return EXIT_FAILURE;
}
printf("Enabling the GPIO device\n");
ret = gpiosim_dev_enable(dev);
if (ret) {
perror("Unable to enable the device");
return EXIT_FAILURE;
}
printf("Setting the pull of a single line to pull-up\n");
ret = gpiosim_bank_set_pull(bank0, 6, GPIOSIM_PULL_UP);
if (ret) {
perror("Unable to set the pull");
return EXIT_FAILURE;
}
printf("Reading the pull back\n");
ret = gpiosim_bank_get_pull(bank0, 6);
if (ret < 0) {
perror("Unable to read the pull");
return EXIT_FAILURE;
}
if (ret != GPIOSIM_PULL_UP) {
fprintf(stderr, "Invalid pull value read\n");
return EXIT_FAILURE;
}
printf("Reading the value\n");
ret = gpiosim_bank_get_value(bank0, 6);
if (ret < 0) {
perror("Unable to read the value");
return EXIT_FAILURE;
}
if (ret != 1) {
fprintf(stderr, "Invalid value read\n");
return EXIT_FAILURE;
}
printf("Disabling the GPIO device\n");
ret = gpiosim_dev_disable(dev);
if (ret) {
perror("Error while disabling the device");
return EXIT_FAILURE;
}
gpiosim_bank_unref(bank1);
gpiosim_bank_unref(bank0);
gpiosim_dev_unref(dev);
gpiosim_ctx_unref(ctx);
return EXIT_SUCCESS;
}

1030
tests/gpiosim/gpiosim.c Normal file

File diff suppressed because it is too large Load Diff

69
tests/gpiosim/gpiosim.h Normal file
View File

@@ -0,0 +1,69 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/* SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <brgl@bgdev.pl> */
#ifndef __GPIOD_GPIOSIM_H__
#define __GPIOD_GPIOSIM_H__
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
struct gpiosim_ctx;
struct gpiosim_dev;
struct gpiosim_bank;
enum {
GPIOSIM_PULL_DOWN = 1,
GPIOSIM_PULL_UP,
};
enum {
GPIOSIM_HOG_DIR_INPUT = 1,
GPIOSIM_HOG_DIR_OUTPUT_HIGH,
GPIOSIM_HOG_DIR_OUTPUT_LOW,
};
struct gpiosim_ctx *gpiosim_ctx_new(void);
struct gpiosim_ctx *gpiosim_ctx_ref(struct gpiosim_ctx *ctx);
void gpiosim_ctx_unref(struct gpiosim_ctx *ctx);
struct gpiosim_dev *
gpiosim_dev_new(struct gpiosim_ctx *ctx, const char *name);
struct gpiosim_dev *gpiosim_dev_ref(struct gpiosim_dev *dev);
void gpiosim_dev_unref(struct gpiosim_dev *dev);
struct gpiosim_ctx *gpiosim_dev_get_ctx(struct gpiosim_dev *dev);
const char *gpiosim_dev_get_name(struct gpiosim_dev *dev);
int gpiosim_dev_enable(struct gpiosim_dev *dev);
int gpiosim_dev_disable(struct gpiosim_dev *dev);
bool gpiosim_dev_is_live(struct gpiosim_dev *dev);
struct gpiosim_bank*
gpiosim_bank_new(struct gpiosim_dev *dev, const char *name);
struct gpiosim_bank *gpiosim_bank_ref(struct gpiosim_bank *bank);
void gpiosim_bank_unref(struct gpiosim_bank *bank);
struct gpiosim_dev *gpiosim_bank_get_dev(struct gpiosim_bank *bank);
const char *gpiosim_bank_get_chip_name(struct gpiosim_bank *bank);
const char *gpiosim_bank_get_dev_path(struct gpiosim_bank *bank);
int gpiosim_bank_set_label(struct gpiosim_bank *bank, const char *label);
int gpiosim_bank_set_num_lines(struct gpiosim_bank *bank,
unsigned int num_lines);
int gpiosim_bank_set_line_name(struct gpiosim_bank *bank,
unsigned int offset, const char *name);
int gpiosim_bank_hog_line(struct gpiosim_bank *bank, unsigned int offset,
const char *name, int direction);
int gpiosim_bank_clear_hog(struct gpiosim_bank *bank, unsigned int offset);
int gpiosim_bank_get_value(struct gpiosim_bank *bank, unsigned int offset);
int gpiosim_bank_get_pull(struct gpiosim_bank *bank, unsigned int offset);
int gpiosim_bank_set_pull(struct gpiosim_bank *bank,
unsigned int offset, int pull);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __GPIOD_GPIOSIM_H__ */