1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2019 Aleksander Morgado <aleksander@aleksander.es>*
* Copyright (C) 2024 Guido Günther <agx@sigxcpu.org>
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <glib.h>
#include <glib-unix.h>
#include <gio/gio.h>
#include <libmm-glib.h>
#define PROGRAM_NAME "mmcbmmonitor"
#define PROGRAM_VERSION PACKAGE_VERSION
/* Globals */
static GMainLoop *loop;
static GList *monitored_cbm_list;
/* Context */
static gboolean version_flag;
static GOptionEntry main_entries[] = {
{ "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag,
"Print version",
NULL
},
{ NULL }
};
static gboolean
signals_handler (void)
{
if (loop && g_main_loop_is_running (loop)) {
g_printerr ("%s\n",
"cancelling the main loop...\n");
g_main_loop_quit (loop);
}
return TRUE;
}
static void
print_version_and_exit (void)
{
g_print ("\n"
PROGRAM_NAME " " PROGRAM_VERSION "\n"
"Copyright (2019) Aleksander Morgado\n"
" (2024) Guido Günther\n"
"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n");
exit (EXIT_SUCCESS);
}
static void
print_cbm (MMCbm *cbm)
{
g_print ("[%s] new cbm: %s\n",
mm_cbm_get_path (cbm),
mm_cbm_state_get_string (mm_cbm_get_state (cbm)));
if (mm_cbm_get_state (cbm) == MM_CBM_STATE_RECEIVED)
g_print(" %u: %s\n", mm_cbm_get_channel (cbm), mm_cbm_get_text (cbm));
}
static void
cbm_state_updated (MMCbm *cbm)
{
print_cbm (cbm);
}
static gboolean
cbm_added (MMModemCellBroadcast *modem_cell_broadcast,
const gchar *cbm_path)
{
g_autolist (MMObject) cbm_list = NULL;
GList *l;
MMCbm *new_cbm = NULL;
cbm_list = mm_modem_cell_broadcast_list_sync (modem_cell_broadcast, NULL, NULL);
for (l = cbm_list; l && !new_cbm; l = g_list_next (l)) {
MMCbm *l_cbm = MM_CBM (l->data);
if (g_strcmp0 (mm_cbm_get_path (l_cbm), cbm_path) == 0)
new_cbm = l_cbm;
}
g_assert (new_cbm);
print_cbm (new_cbm);
g_signal_connect (new_cbm, "notify::state", G_CALLBACK (cbm_state_updated), NULL);
monitored_cbm_list = g_list_append (monitored_cbm_list, g_object_ref (new_cbm));
return TRUE;
}
static void
list_all_cbm_found (MMModemCellBroadcast *modem_cell_broadcast)
{
g_autolist (MMObject) cbm_list= NULL;
GList *l;
cbm_list = mm_modem_cell_broadcast_list_sync (modem_cell_broadcast, NULL, NULL);
for (l = cbm_list; l; l = g_list_next (l)) {
MMCbm *l_cbm = MM_CBM (l->data);
print_cbm (l_cbm);
g_signal_connect (l_cbm, "notify::state", G_CALLBACK (cbm_state_updated), NULL);
monitored_cbm_list = g_list_append (monitored_cbm_list, g_object_ref (l_cbm));
}
}
static void
on_modem_added (MMManager *manager,
MMObject *object,
gpointer unused)
{
MMModemCellBroadcast *modem_cell_broadcast;
g_print ("Modem found at path: %s\n", mm_object_get_path (object));
modem_cell_broadcast = MM_MODEM_CELL_BROADCAST (mm_object_peek_modem_cell_broadcast (object));
g_signal_connect (modem_cell_broadcast, "added", G_CALLBACK (cbm_added), NULL);
list_all_cbm_found (modem_cell_broadcast);
}
int main (int argc, char **argv)
{
g_autoptr (GOptionContext) context = NULL;
g_autoptr (GDBusConnection) connection = NULL;
g_autoptr (MMManager) manager = NULL;
g_autofree gchar *name_owner = NULL;
g_autoptr (GError) error = NULL;
g_autolist (MMObject) modem_list = NULL;
GList *l;
setlocale (LC_ALL, "");
/* Setup option context, process it and destroy it */
context = g_option_context_new ("- ModemManager CBM monitor");
g_option_context_add_main_entries (context, main_entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
if (version_flag)
print_version_and_exit ();
/* Setup dbus connection to use */
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
if (!connection) {
g_printerr ("error: couldn't get bus: %s\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
manager = mm_manager_new_sync (connection,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START,
NULL,
&error);
if (!manager) {
g_printerr ("error: couldn't create manager: %s\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (manager));
if (!name_owner) {
g_printerr ("error: couldn't find the ModemManager process in the bus\n");
exit (EXIT_FAILURE);
}
modem_list = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager));
for (l = modem_list; l; l = g_list_next (l))
on_modem_added (manager, MM_OBJECT (l->data), NULL);
g_signal_connect (manager, "object-added", G_CALLBACK (on_modem_added), NULL);
g_unix_signal_add (SIGINT, (GSourceFunc) signals_handler, NULL);
g_unix_signal_add (SIGHUP, (GSourceFunc) signals_handler, NULL);
g_unix_signal_add (SIGTERM, (GSourceFunc) signals_handler, NULL);
/* Setup main loop and schedule start in idle */
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
/* Cleanup */
g_list_free_full (monitored_cbm_list, g_object_unref);
g_main_loop_unref (loop);
return EXIT_SUCCESS;
}
|