Debian Codegraph @

src/mbim-proxy/mbim-proxy.c

raw
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /*
4 * mbim-proxy -- A proxy to communicate with MBIM ports
5 *
6 * Copyright (C) 2014-2023 Aleksander Morgado <[email protected]>
7 * Copyright (C) 2014 Smith Micro Software, Inc.
8 */
9
10 #include "config.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <locale.h>
15 #include <string.h>
16
17 #include <glib.h>
18 #include <glib/gprintf.h>
19 #include <gio/gio.h>
20 #include <glib-unix.h>
21
22 #include <libmbim-glib.h>
23
24 #define PROGRAM_NAME "mbim-proxy"
25 #define PROGRAM_VERSION PACKAGE_VERSION
26
27 #define EMPTY_TIMEOUT_DEFAULT 300
28
29 /* Globals */
30 static GMainLoop *loop;
31 static MbimProxy *proxy;
32 static guint timeout_id;
33 static guint client_connected_once = FALSE;
34
35 /* Main options */
36 static gboolean verbose_flag;
37 static gboolean verbose_full_flag;
38 static gboolean version_flag;
39 static gboolean no_exit_flag;
40 static gint empty_timeout = -1;
41
42 static GOptionEntry main_entries[] = {
43 { "no-exit", 0, 0, G_OPTION_ARG_NONE, &no_exit_flag,
44 "Don't exit after being idle without clients/devices",
45 NULL
46 },
47 { "empty-timeout", 0, 0, G_OPTION_ARG_INT, &empty_timeout,
48 "If no clients/devices, exit after this timeout. If set to 0, equivalent to --no-exit.",
49 "[SECS]"
50 },
51 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag,
52 "Run action with verbose logs, including the debug ones",
53 NULL
54 },
55 { "verbose-full", 0, 0, G_OPTION_ARG_NONE, &verbose_full_flag,
56 "Run action with verbose logs, including the debug ones and personal info",
57 NULL
58 },
59 { "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag,
60 "Print version",
61 NULL
62 },
63 { NULL, 0, 0, 0, NULL, NULL, NULL }
64 };
65
66 static gboolean
67 quit_cb (gpointer user_data)
68 {
69 if (loop) {
70 g_warning ("Caught signal, stopping the loop...");
71 g_idle_add ((GSourceFunc) g_main_loop_quit, loop);
72 }
73
74 return FALSE;
75 }
76
77 static void
78 log_handler (const gchar *log_domain,
79 GLogLevelFlags log_level,
80 const gchar *message,
81 gpointer user_data)
82 {
83 const gchar *log_level_str;
84 time_t now;
85 gchar time_str[64];
86 struct tm *local_time;
87 gboolean err = FALSE;
88
89 switch (log_level) {
90 case G_LOG_LEVEL_WARNING:
91 log_level_str = "-Warning **";
92 err = TRUE;
93 break;
94
95 case G_LOG_LEVEL_CRITICAL:
96 case G_LOG_FLAG_FATAL:
97 case G_LOG_LEVEL_ERROR:
98 log_level_str = "-Error **";
99 err = TRUE;
100 break;
101
102 case G_LOG_LEVEL_DEBUG:
103 log_level_str = "[Debug]";
104 break;
105
106 case G_LOG_LEVEL_MESSAGE:
107 case G_LOG_LEVEL_INFO:
108 log_level_str = "";
109 break;
110
111 case G_LOG_LEVEL_MASK:
112 case G_LOG_FLAG_RECURSION:
113 default:
114 g_assert_not_reached ();
115 }
116
117 if (!verbose_flag && !verbose_full_flag && !err)
118 return;
119
120 now = time ((time_t *) NULL);
121 local_time = localtime (&now);
122 strftime (time_str, 64, "%d %b %Y, %H:%M:%S", local_time);
123
124 g_fprintf (err ? stderr : stdout,
125 "[%s] %s %s\n",
126 time_str,
127 log_level_str,
128 message);
129 }
130
131 G_GNUC_NORETURN
132 static void
133 print_version_and_exit (void)
134 {
135 g_print ("\n"
136 PROGRAM_NAME " " PROGRAM_VERSION "\n"
137 "Copyright (C) 2013-2023 Aleksander Morgado\n"
138 "Copyright (C) 2014-2018 Greg Suarez\n"
139 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
140 "This is free software: you are free to change and redistribute it.\n"
141 "There is NO WARRANTY, to the extent permitted by law.\n"
142 "\n");
143 exit (EXIT_SUCCESS);
144 }
145
146 /*****************************************************************************/
147
148 static gboolean
149 stop_loop_cb (void)
150 {
151 timeout_id = 0;
152 if (loop)
153 g_main_loop_quit (loop);
154 return FALSE;
155 }
156
157 static void
158 proxy_n_clients_changed (MbimProxy *_proxy)
159 {
160 /* once a client has connected only exit if there are no devices */
161 if (client_connected_once)
162 return;
163
164 if (mbim_proxy_get_n_clients (proxy) == 0) {
165 g_assert (timeout_id == 0);
166 g_assert (empty_timeout > 0);
167 timeout_id = g_timeout_add_seconds (empty_timeout,
168 (GSourceFunc)stop_loop_cb,
169 NULL);
170 return;
171 }
172
173 /* At least one client, remove timeout if any */
174 if (timeout_id) {
175 g_source_remove (timeout_id);
176 timeout_id = 0;
177 }
178
179 client_connected_once = TRUE;
180 }
181
182 static void
183 proxy_n_devices_changed (MbimProxy *_proxy)
184 {
185 if (mbim_proxy_get_n_devices (proxy) == 0) {
186 g_assert (timeout_id == 0);
187 g_assert (empty_timeout > 0);
188 timeout_id = g_timeout_add_seconds (empty_timeout,
189 (GSourceFunc)stop_loop_cb,
190 NULL);
191 return;
192 }
193
194 /* At least one device, remove timeout if any */
195 if (timeout_id) {
196 g_source_remove (timeout_id);
197 timeout_id = 0;
198 }
199 }
200
201 /*****************************************************************************/
202
203 int main (int argc, char **argv)
204 {
205 g_autoptr(GError) error = NULL;
206 g_autoptr(GOptionContext) context = NULL;
207
208 setlocale (LC_ALL, "");
209
210 /* Setup option context, process it and destroy it */
211 context = g_option_context_new ("- Proxy for MBIM devices");
212 g_option_context_add_main_entries (context, main_entries, NULL);
213 if (!g_option_context_parse (context, &argc, &argv, &error)) {
214 g_printerr ("error: %s\n", error->message);
215 exit (EXIT_FAILURE);
216 }
217
218 if (version_flag)
219 print_version_and_exit ();
220
221 g_log_set_handler (NULL, G_LOG_LEVEL_MASK, log_handler, NULL);
222 g_log_set_handler ("Mbim", G_LOG_LEVEL_MASK, log_handler, NULL);
223 if (verbose_flag && verbose_full_flag) {
224 g_printerr ("error: cannot specify --verbose and --verbose-full at the same time\n");
225 exit (EXIT_FAILURE);
226 } else if (verbose_flag) {
227 mbim_utils_set_traces_enabled (TRUE);
228 mbim_utils_set_show_personal_info (FALSE);
229 } else if (verbose_full_flag) {
230 mbim_utils_set_traces_enabled (TRUE);
231 mbim_utils_set_show_personal_info (TRUE);
232 }
233
234 /* Setup signals */
235 g_unix_signal_add (SIGINT, quit_cb, NULL);
236 g_unix_signal_add (SIGHUP, quit_cb, NULL);
237 g_unix_signal_add (SIGTERM, quit_cb, NULL);
238
239 /* Setup empty timeout */
240 if (empty_timeout < 0)
241 empty_timeout = EMPTY_TIMEOUT_DEFAULT;
242
243 /* Setup proxy */
244 proxy = mbim_proxy_new (&error);
245 if (!proxy) {
246 g_printerr ("error: %s\n", error->message);
247 exit (EXIT_FAILURE);
248 }
249
250 /* Don't exit the proxy when no clients/devices are found */
251 if (!no_exit_flag && empty_timeout != 0) {
252 g_debug ("proxy will exit after %d secs if unused", empty_timeout);
253 proxy_n_clients_changed (proxy);
254 g_signal_connect (proxy,
255 "notify::" MBIM_PROXY_N_CLIENTS,
256 G_CALLBACK (proxy_n_clients_changed),
257 NULL);
258 g_signal_connect (proxy,
259 "notify::" MBIM_PROXY_N_DEVICES,
260 G_CALLBACK (proxy_n_devices_changed),
261 NULL);
262 } else
263 g_debug ("proxy will remain running if unused");
264
265 /* Loop */
266 loop = g_main_loop_new (NULL, FALSE);
267 g_main_loop_run (loop);
268 g_main_loop_unref (loop);
269
270 /* Cleanup; releases socket and such */
271 g_object_unref (proxy);
272
273 g_debug ("exiting 'mbim-proxy'...");
274
275 return EXIT_SUCCESS;
276 }