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