Debian Codegraph @

tests/gpiosim/gpiosim-selftest.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "gpiosim.h"
8
9 #define UNUSED __attribute__((unused))
10
11 static const char *const line_names[] = {
12 "foo",
13 "bar",
14 "foobar",
15 NULL,
16 "barfoo",
17 };
18
19 int main(int argc UNUSED, char **argv UNUSED)
20 {
21 struct gpiosim_bank *bank0, *bank1;
22 struct gpiosim_dev *dev;
23 struct gpiosim_ctx *ctx;
24 int ret, i;
25
26 printf("Creating gpiosim context\n");
27
28 ctx = gpiosim_ctx_new();
29 if (!ctx) {
30 perror("unable to create the gpios-sim context");
31 return EXIT_FAILURE;
32 }
33
34 printf("Creating a chip\n");
35
36 dev = gpiosim_dev_new(ctx);
37 if (!dev) {
38 perror("Unable to create a chip");
39 return EXIT_FAILURE;
40 }
41
42 printf("Creating a bank\n");
43
44 bank0 = gpiosim_bank_new(dev);
45 if (!bank0) {
46 perror("Unable to create a bank");
47 return EXIT_FAILURE;
48 }
49
50 printf("Creating a second bank\n");
51
52 bank1 = gpiosim_bank_new(dev);
53 if (!bank1) {
54 perror("Unable to create a bank");
55 return EXIT_FAILURE;
56 }
57
58 printf("Setting the label of bank #2 to foobar\n");
59
60 ret = gpiosim_bank_set_label(bank1, "foobar");
61 if (ret) {
62 perror("Unable to set the label of bank #2");
63 return EXIT_FAILURE;
64 }
65
66 printf("Setting the number of lines in bank #1 to 16\n");
67
68 ret = gpiosim_bank_set_num_lines(bank0, 16);
69 if (ret) {
70 perror("Unable to set the number of lines");
71 return EXIT_FAILURE;
72 }
73
74 printf("Setting the number of lines in bank #2 to 8\n");
75
76 ret = gpiosim_bank_set_num_lines(bank1, 8);
77 if (ret) {
78 perror("Unable to set the number of lines");
79 return EXIT_FAILURE;
80 }
81
82 printf("Setting names for some lines in bank #1\n");
83
84 for (i = 0; i < 5; i++) {
85 ret = gpiosim_bank_set_line_name(bank0, i, line_names[i]);
86 if (ret) {
87 perror("Unable to set line names");
88 return EXIT_FAILURE;
89 }
90 }
91
92 printf("Hog a line on bank #2\n");
93
94 ret = gpiosim_bank_hog_line(bank1, 3, "xyz",
95 GPIOSIM_DIRECTION_OUTPUT_HIGH);
96 if (ret) {
97 perror("Unable to hog a line");
98 return EXIT_FAILURE;
99 }
100
101 printf("Enabling the GPIO device\n");
102
103 ret = gpiosim_dev_enable(dev);
104 if (ret) {
105 perror("Unable to enable the device");
106 return EXIT_FAILURE;
107 }
108
109 printf("Setting the pull of a single line to pull-up\n");
110
111 ret = gpiosim_bank_set_pull(bank0, 6, GPIOSIM_PULL_UP);
112 if (ret) {
113 perror("Unable to set the pull");
114 return EXIT_FAILURE;
115 }
116
117 printf("Reading the pull back\n");
118
119 ret = gpiosim_bank_get_pull(bank0, 6);
120 if (ret < 0) {
121 perror("Unable to read the pull");
122 return EXIT_FAILURE;
123 }
124
125 if (ret != GPIOSIM_PULL_UP) {
126 fprintf(stderr, "Invalid pull value read\n");
127 return EXIT_FAILURE;
128 }
129
130 printf("Reading the value\n");
131
132 ret = gpiosim_bank_get_value(bank0, 6);
133 if (ret < 0) {
134 perror("Unable to read the value");
135 return EXIT_FAILURE;
136 }
137
138 if (ret != GPIOSIM_VALUE_ACTIVE) {
139 fprintf(stderr, "Invalid value read\n");
140 return EXIT_FAILURE;
141 }
142
143 printf("Disabling the GPIO device\n");
144
145 ret = gpiosim_dev_disable(dev);
146 if (ret) {
147 perror("Error while disabling the device");
148 return EXIT_FAILURE;
149 }
150
151 gpiosim_bank_unref(bank1);
152 gpiosim_bank_unref(bank0);
153 gpiosim_dev_unref(dev);
154 gpiosim_ctx_unref(ctx);
155
156 return EXIT_SUCCESS;
157 }