Debian Codegraph @

dbus/manager/gpio-manager.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2022-2024 Bartosz Golaszewski <[email protected]>
3
4 #include <gio/gio.h>
5 #include <glib.h>
6 #include <glib-unix.h>
7 #include <gpiod-glib.h>
8 #include <gpiodbus.h>
9 #include <stdlib.h>
10
11 #include "daemon.h"
12
13 static const gchar *const debug_domains[] = {
14 "gpio-manager",
15 "gpiodglib",
16 NULL
17 };
18
19 static gboolean stop_main_loop_on_sig(gpointer data, const gchar *signame)
20 {
21 GMainLoop *loop = data;
22
23 g_debug("%s received", signame);
24
25 g_main_loop_quit(loop);
26
27 return G_SOURCE_REMOVE;
28 }
29
30 static gboolean on_sigterm(gpointer data)
31 {
32 return stop_main_loop_on_sig(data, "SIGTERM");
33 }
34
35 static gboolean on_sigint(gpointer data)
36 {
37 return stop_main_loop_on_sig(data, "SIGINT");
38 }
39
40 static gboolean on_sighup(gpointer data G_GNUC_UNUSED)
41 {
42 g_debug("SIGHUB received, ignoring");
43
44 return G_SOURCE_CONTINUE;
45 }
46
47 static void on_bus_acquired(GDBusConnection *con,
48 const gchar *name G_GNUC_UNUSED,
49 gpointer data)
50 {
51 GpiodbusDaemon *daemon = data;
52
53 g_debug("D-Bus connection acquired");
54
55 gpiodbus_daemon_start(daemon, con);
56 }
57
58 static void on_name_acquired(GDBusConnection *con G_GNUC_UNUSED,
59 const gchar *name, gpointer data G_GNUC_UNUSED)
60 {
61 g_debug("D-Bus name acquired: '%s'", name);
62 }
63
64 static void on_name_lost(GDBusConnection *con,
65 const gchar *name, gpointer data G_GNUC_UNUSED)
66 {
67 g_debug("D-Bus name lost: '%s'", name);
68
69 if (!con)
70 g_error("unable to make connection to the bus");
71
72 if (g_dbus_connection_is_closed(con))
73 g_error("connection to the bus closed");
74
75 g_error("name '%s' lost on the bus", name);
76 }
77
78 static void print_version_and_exit(void)
79 {
80 g_print("%s (libgpiod) v%s\nD-Bus API version: %d\n",
81 g_get_prgname(), gpiodglib_api_version(), GPIODBUS_API_VERSION);
82
83 exit(EXIT_SUCCESS);
84 }
85
86 static void parse_opts(int argc, char **argv)
87 {
88 static const char *const description =
89 "The gpio-manager is the reference implementation of the GPIO D-Bus API\n"
90 "built on top of libgpiod. It serves as the central authority managing GPIO\n"
91 "chips and lines, exposing their functionalities to other applications\n"
92 "through the D-Bus interface. It supports operations such as requesting,\n"
93 "releasing, and setting line values, as well as monitoring events like rising\n"
94 "or falling edges and line property changes.";
95
96 gboolean ret, opt_debug = FALSE, opt_version = FALSE;
97 g_autoptr(GOptionContext) ctx = NULL;
98 g_auto(GStrv) remaining = NULL;
99 g_autoptr(GError) err = NULL;
100
101 const GOptionEntry opts[] = {
102 {
103 .long_name = "debug",
104 .short_name = 'd',
105 .flags = G_OPTION_FLAG_NONE,
106 .arg = G_OPTION_ARG_NONE,
107 .arg_data = &opt_debug,
108 .description = "Emit additional debug log messages.",
109 },
110 {
111 .long_name = "version",
112 .short_name = 'v',
113 .flags = G_OPTION_FLAG_NONE,
114 .arg = G_OPTION_ARG_NONE,
115 .arg_data = &opt_version,
116 .description = "Print version and exit.",
117 },
118 {
119 .long_name = G_OPTION_REMAINING,
120 .flags = G_OPTION_FLAG_NONE,
121 .arg = G_OPTION_ARG_STRING_ARRAY,
122 .arg_data = &remaining,
123 },
124 { }
125 };
126
127 ctx = g_option_context_new(NULL);
128 g_option_context_set_summary(ctx, "D-Bus daemon managing GPIOs.");
129 g_option_context_set_description(ctx, description);
130 g_option_context_add_main_entries(ctx, opts, NULL);
131
132 ret = g_option_context_parse(ctx, &argc, &argv, &err);
133 if (!ret) {
134 g_printerr("Option parsing failed: %s\n\nUse %s --help\n",
135 err->message, g_get_prgname());
136 exit(EXIT_FAILURE);
137 }
138
139 if (remaining) {
140 g_printerr("Option parsing failed: additional arguments are not allowed\n");
141 exit(EXIT_FAILURE);
142 }
143
144 if (opt_version)
145 print_version_and_exit();
146
147 if (opt_debug)
148 g_log_writer_default_set_debug_domains(debug_domains);
149 }
150
151 int main(int argc, char **argv)
152 {
153 g_autoptr(GpiodbusDaemon) daemon = NULL;
154 g_autofree gchar *basename = NULL;
155 g_autoptr(GMainLoop) loop = NULL;
156 guint bus_id;
157
158 basename = g_path_get_basename(argv[0]);
159 g_set_prgname(basename);
160 parse_opts(argc, argv);
161
162 g_message("initializing %s", g_get_prgname());
163
164 loop = g_main_loop_new(NULL, FALSE);
165 daemon = gpiodbus_daemon_new();
166
167 g_unix_signal_add(SIGTERM, on_sigterm, loop);
168 g_unix_signal_add(SIGINT, on_sigint, loop);
169 g_unix_signal_add(SIGHUP, on_sighup, NULL); /* Ignore SIGHUP. */
170
171 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, "io.gpiod1",
172 G_BUS_NAME_OWNER_FLAGS_NONE, on_bus_acquired,
173 on_name_acquired, on_name_lost, daemon, NULL);
174
175 g_message("%s started", g_get_prgname());
176
177 g_main_loop_run(loop);
178
179 g_bus_unown_name(bus_id);
180
181 g_message("%s exiting", g_get_prgname());
182
183 return EXIT_SUCCESS;
184 }