Debian Codegraph @

dbus/client/gpiocli.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2022-2023 Bartosz Golaszewski <[email protected]>
3
4 #include <glib/gstdio.h>
5 #include <gpiodbus.h>
6
7 #include "common.h"
8
9 typedef struct {
10 gchar *name;
11 int (*main_func)(int argc, char **argv);
12 gchar *descr;
13 } GPIOCliCmd;
14
15 int gpiocli_detect_main(int argc, char **argv);
16 int gpiocli_find_main(int argc, char **argv);
17 int gpiocli_info_main(int argc, char **argv);
18 int gpiocli_get_main(int argc, char **argv);
19 int gpiocli_monitor_main(int argc, char **argv);
20 int gpiocli_notify_main(int argc, char **argv);
21 int gpiocli_reconfigure_main(int argc, char **argv);
22 int gpiocli_release_main(int argc, char **argv);
23 int gpiocli_request_main(int argc, char **argv);
24 int gpiocli_requests_main(int argc, char **argv);
25 int gpiocli_set_main(int argc, char **argv);
26 int gpiocli_wait_main(int argc, char **argv);
27
28 static const GPIOCliCmd cli_cmds[] = {
29 {
30 .name = "detect",
31 .main_func = gpiocli_detect_main,
32 .descr = "list GPIO chips and print their properties",
33 },
34 {
35 .name = "find",
36 .main_func = gpiocli_find_main,
37 .descr = "take a line name and find its parent chip's name and offset within it",
38 },
39 {
40 .name = "info",
41 .main_func = gpiocli_info_main,
42 .descr = "print information about GPIO lines",
43 },
44 {
45 .name = "get",
46 .main_func = gpiocli_get_main,
47 .descr = "get values of GPIO lines",
48 },
49 {
50 .name = "monitor",
51 .main_func = gpiocli_monitor_main,
52 .descr = "notify the user about edge events",
53 },
54 {
55 .name = "notify",
56 .main_func = gpiocli_notify_main,
57 .descr = "notify the user about line property changes",
58 },
59 {
60 .name = "reconfigure",
61 .main_func = gpiocli_reconfigure_main,
62 .descr = "change the line configuration for an existing request",
63 },
64 {
65 .name = "release",
66 .main_func = gpiocli_release_main,
67 .descr = "release one of the line requests controlled by the manager",
68 },
69 {
70 .name = "request",
71 .main_func = gpiocli_request_main,
72 .descr = "request a set of GPIO lines for exclusive usage by the manager",
73 },
74 {
75 .name = "requests",
76 .main_func = gpiocli_requests_main,
77 .descr = "list all line requests controlled by the manager",
78 },
79 {
80 .name = "set",
81 .main_func = gpiocli_set_main,
82 .descr = "set values of GPIO lines",
83 },
84 {
85 .name = "wait",
86 .main_func = gpiocli_wait_main,
87 .descr = "wait for the gpio-manager interface to appear",
88 },
89 { }
90 };
91
92 static GHashTable *make_cmd_table(void)
93 {
94 GHashTable *cmd_table = g_hash_table_new_full(g_str_hash, g_str_equal,
95 NULL, NULL);
96 const GPIOCliCmd *cmd;
97
98 for (cmd = &cli_cmds[0]; cmd->name; cmd++)
99 g_hash_table_insert(cmd_table, cmd->name, cmd->main_func);
100
101 return cmd_table;
102 }
103
104 static gchar *make_description(void)
105 {
106 g_autoptr(GString) descr = g_string_new("Available commands:\n");
107 const GPIOCliCmd *cmd;
108
109 for (cmd = &cli_cmds[0]; cmd->name; cmd++)
110 g_string_append_printf(descr, " - %s:\n\t%s\n",
111 cmd->name, cmd->descr);
112
113 g_string_truncate(descr, descr->len - 1);
114 return g_strdup(descr->str);
115 }
116
117 static void show_version_and_exit(void)
118 {
119 g_print("gpiocli (libgpiod) v%s\nD-Bus API version: %d\n",
120 GPIOD_VERSION_STR, GPIODBUS_API_VERSION);
121
122 exit(EXIT_SUCCESS);
123 }
124
125 int main(int argc, char **argv)
126 {
127 static const gchar *const summary =
128 "Simple command-line client for controlling gpio-manager.";
129
130 g_autoptr(GHashTable) cmd_table = make_cmd_table();
131 g_autofree gchar *description = make_description();
132 g_autofree gchar *basename = NULL;
133 g_autofree gchar *cmd_name = NULL;
134 gint (*cmd_func)(gint, gchar **);
135 g_auto(GStrv) cmd_args = NULL;
136 gboolean show_version = FALSE;
137
138 const GOptionEntry opts[] = {
139 {
140 .long_name = "version",
141 .short_name = 'v',
142 .flags = G_OPTION_FLAG_NONE,
143 .arg = G_OPTION_ARG_NONE,
144 .arg_data = &show_version,
145 .description = "Show version and exit.",
146 },
147 {
148 .long_name = G_OPTION_REMAINING,
149 .flags = G_OPTION_FLAG_NONE,
150 .arg = G_OPTION_ARG_STRING_ARRAY,
151 .arg_data = &cmd_args,
152 .arg_description = "CMD [ARGS?] ...",
153 },
154 { }
155 };
156
157 basename = g_path_get_basename(argv[0]);
158 g_set_prgname(basename);
159
160 parse_options(opts, summary, description, &argc, &argv);
161
162 if (show_version)
163 show_version_and_exit();
164
165 if (!cmd_args)
166 die_parsing_opts("Command must be specified.");
167
168 cmd_func = g_hash_table_lookup(cmd_table, cmd_args[0]);
169 if (!cmd_func)
170 die_parsing_opts("Unknown command: %s.", cmd_args[0]);
171
172 cmd_name = g_strdup_printf("%s %s", basename, cmd_args[0]);
173 g_set_prgname(cmd_name);
174
175 return cmd_func(g_strv_length(cmd_args), cmd_args);
176 }