Debian Codegraph @

src/cbd-channel-manager.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2025 The Phosh.mobi e.V.
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * Author: Guido Günther <[email protected]>
7 */
8
9 #define G_LOG_DOMAIN "cbd-channel-manager"
10
11 #include "cbd-config.h"
12
13 #include "cbd-channel-manager.h"
14 #include "cbd-service-providers.h"
15
16 #include "lcb-enums.h"
17 #include "lcb-enum-types.h"
18
19 #include <gio/gio.h>
20
21 /**
22 * CbdChannelManager:
23 *
24 * Determine the current channels that should be set on a modem. If
25 * the manager is in `country` mode it tracks the channel based on the
26 * set country. If it is in `none` mode it ignores any changes to the
27 * country.
28 *
29 * TODO: As per 3GPP TS 22.268 we should also take CBMs language into account
30 * but that information is not in any released modem manager version yet
31 * (see https://gitlab.freedesktop.org/mobile-broadband/ModemManager/-/merge_requests/1361)
32 * and we hence ignore that information for now.
33 */
34
35 enum {
36 PROP_0,
37 PROP_SERVICE_PROVIDERS_DATABASE,
38 PROP_MODE,
39 PROP_COUNTRY,
40 PROP_CHANNELS,
41 PROP_LAST_PROP
42 };
43 static GParamSpec *props[PROP_LAST_PROP];
44
45 struct _CbdChannelManager {
46 GObject parent;
47
48 char *service_providers_database;
49
50 GSettings *settings;
51 LcbChannelMode mode;
52 char *country;
53 GArray *channels; /* element-type: CbdChannelsRange */
54 };
55
56 G_DEFINE_TYPE (CbdChannelManager, cbd_channel_manager, G_TYPE_OBJECT)
57
58
59 static const CbdChannelsRange default_channels[] = {
60 {
61 .level = LCB_SEVERITY_LEVEL_PRESIDENTIAL,
62 .start = 4370,
63 .end = 4370,
64 },
65 {
66 .level = LCB_SEVERITY_LEVEL_EXTREME,
67 .start = 4371,
68 .end = 4372,
69 },
70 {
71 .level = LCB_SEVERITY_LEVEL_SEVERE,
72 .start = 4373,
73 .end = 4378,
74 },
75 {
76 .level = LCB_SEVERITY_LEVEL_PUBLIC_SAFETY,
77 .start = 4397,
78 .end = 4397,
79 },
80 {
81 .level = LCB_SEVERITY_LEVEL_AMBER,
82 .start = 4379,
83 .end = 4379,
84 },
85 };
86
87
88 static void
89 set_default_channels (CbdChannelManager *self)
90 {
91 g_debug ("Setting default channel list");
92 g_clear_pointer (&self->channels, g_array_unref);
93 self->channels = g_array_new_take (g_memdup2 (default_channels, sizeof (default_channels)),
94 G_N_ELEMENTS (default_channels),
95 FALSE,
96 sizeof (CbdChannelsRange));
97 }
98
99
100 static void
101 cbd_channel_manager_update_channels (CbdChannelManager *self)
102 {
103 g_autoptr (GError) err = NULL;
104
105 if (self->mode != LCB_CHANNEL_MODE_COUNTRY)
106 return;
107
108 if (!self->service_providers_database) {
109 set_default_channels (self);
110 goto out;
111 }
112
113 g_clear_pointer (&self->channels, g_array_unref);
114 self->channels = cbd_service_providers_get_channels_sync (self->service_providers_database,
115 self->country,
116 &err);
117 if (!self->channels || self->channels->len == 0) {
118 g_warning ("Failed to get channels: %s, using built-in defaults", err->message);
119 set_default_channels (self);
120 }
121
122 out:
123 g_object_notify_by_pspec (G_OBJECT (self), props[PROP_CHANNELS]);
124 }
125
126
127 static void
128 cbd_channel_manager_set_property (GObject *object,
129 guint property_id,
130 const GValue *value,
131 GParamSpec *pspec)
132 {
133 CbdChannelManager *self = CBD_CHANNEL_MANAGER (object);
134
135 switch (property_id) {
136 case PROP_SERVICE_PROVIDERS_DATABASE:
137 self->service_providers_database = g_value_dup_string (value);
138 break;
139 case PROP_MODE:
140 cbd_channel_manager_set_mode (self, g_value_get_enum (value));
141 break;
142 case PROP_COUNTRY:
143 cbd_channel_manager_set_country (self, g_value_get_string (value));
144 break;
145 default:
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
147 break;
148 }
149 }
150
151
152 static void
153 cbd_channel_manager_get_property (GObject *object,
154 guint property_id,
155 GValue *value,
156 GParamSpec *pspec)
157 {
158 CbdChannelManager *self = CBD_CHANNEL_MANAGER (object);
159
160 switch (property_id) {
161 case PROP_SERVICE_PROVIDERS_DATABASE:
162 g_value_set_string (value, self->service_providers_database);
163 break;
164 case PROP_MODE:
165 g_value_set_enum (value, cbd_channel_manager_get_mode (self));
166 break;
167 case PROP_COUNTRY:
168 g_value_set_string (value, cbd_channel_manager_get_country (self));
169 break;
170 case PROP_CHANNELS:
171 g_value_set_boxed (value, cbd_channel_manager_get_channels (self));
172 break;
173 default:
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
175 break;
176 }
177 }
178
179
180 static void
181 cbd_channel_manager_finalize (GObject *object)
182 {
183 CbdChannelManager *self = CBD_CHANNEL_MANAGER (object);
184
185 g_clear_pointer (&self->channels, g_array_unref);
186 g_clear_object (&self->settings);
187 g_clear_pointer (&self->service_providers_database, g_free);
188 g_clear_pointer (&self->country, g_free);
189
190 G_OBJECT_CLASS (cbd_channel_manager_parent_class)->finalize (object);
191 }
192
193
194 static void
195 cbd_channel_manager_class_init (CbdChannelManagerClass *klass)
196 {
197 GObjectClass *object_class = G_OBJECT_CLASS (klass);
198
199 object_class->get_property = cbd_channel_manager_get_property;
200 object_class->set_property = cbd_channel_manager_set_property;
201 object_class->finalize = cbd_channel_manager_finalize;
202
203 /**
204 * CbdChannelManager:service-providers-database:
205 *
206 * Path to the service provider database
207 */
208 props[PROP_SERVICE_PROVIDERS_DATABASE] =
209 g_param_spec_string ("service-providers-database", "", "",
210 CBD_SERVICE_PROVIDER_DATABASE,
211 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
212 /**
213 * CbdChannelManager:mode:
214 *
215 * How the current list of channels should be determined
216 */
217 props[PROP_MODE] =
218 g_param_spec_enum ("mode", "", "",
219 LCB_TYPE_CHANNEL_MODE,
220 LCB_CHANNEL_MODE_NONE,
221 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
222 /**
223 * CbdChannelManager:country:
224 *
225 * The country to determine the channel list for
226 */
227 props[PROP_COUNTRY] =
228 g_param_spec_string ("country", "", "",
229 NULL,
230 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
231 /**
232 * CbdChannelManager:channels:
233 *
234 * The current channel list suitable for country
235 */
236 props[PROP_CHANNELS] =
237 g_param_spec_boxed ("channels", "", "",
238 G_TYPE_ARRAY,
239 G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
240
241 g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
242 }
243
244
245 static void
246 cbd_channel_manager_init (CbdChannelManager *self)
247 {
248 set_default_channels (self);
249
250 self->settings = g_settings_new ("org.freedesktop.cbd");
251 g_settings_bind (self->settings,
252 "channel-mode",
253 self,
254 "mode",
255 G_SETTINGS_BIND_GET);
256 }
257
258
259 CbdChannelManager *
260 cbd_channel_manager_new (const char *service_providers_database)
261 {
262 return g_object_new (CBD_TYPE_CHANNEL_MANAGER,
263 "service-providers-database", service_providers_database,
264 NULL);
265 }
266
267
268 const char *
269 cbd_channel_manager_get_service_providers_database (CbdChannelManager *self)
270 {
271 g_assert (CBD_IS_CHANNEL_MANAGER (self));
272
273 return self->service_providers_database;
274 }
275
276
277 LcbChannelMode
278 cbd_channel_manager_get_mode (CbdChannelManager *self)
279 {
280 g_assert (CBD_IS_CHANNEL_MANAGER (self));
281
282 return self->mode;
283 }
284
285
286 void
287 cbd_channel_manager_set_mode (CbdChannelManager *self, LcbChannelMode mode)
288 {
289 g_assert (CBD_IS_CHANNEL_MANAGER (self));
290
291 if (self->mode == mode)
292 return;
293
294 self->mode = mode;
295 g_object_notify_by_pspec (G_OBJECT (self), props[PROP_MODE]);
296
297 cbd_channel_manager_update_channels (self);
298 }
299
300
301 const char *
302 cbd_channel_manager_get_country (CbdChannelManager *self)
303 {
304 g_assert (CBD_IS_CHANNEL_MANAGER (self));
305
306 return self->country;
307 }
308
309
310 void
311 cbd_channel_manager_set_country (CbdChannelManager *self, const char *country)
312 {
313 g_assert (CBD_IS_CHANNEL_MANAGER (self));
314
315 if (!g_strcmp0 (self->country, country))
316 return;
317
318 g_set_str (&self->country, country);
319 g_object_notify_by_pspec (G_OBJECT (self), props[PROP_COUNTRY]);
320
321 cbd_channel_manager_update_channels (self);
322 }
323
324
325 LcbSeverityLevel
326 cbd_channel_manager_lookup_level (CbdChannelManager *self, int channel)
327 {
328 g_assert (CBD_IS_CHANNEL_MANAGER (self));
329
330 if (!self->channels)
331 return LCB_SEVERITY_LEVEL_UNKNOWN;
332
333 for (int i = 0; i < self->channels->len; i++) {
334 CbdChannelsRange range;
335
336 range = g_array_index (self->channels, CbdChannelsRange, i);
337 if (range.start <= channel && channel <= range.end)
338 return range.level;
339 }
340
341 return LCB_SEVERITY_LEVEL_UNKNOWN;
342 }
343
344 /**
345 * cbd_channel_manager_get_channels:
346 * @self: The channel manager
347 *
348 * Get the current channels
349 *
350 * Returns: (transfer none): The channels
351 */
352 GArray *
353 cbd_channel_manager_get_channels (CbdChannelManager *self)
354 {
355 g_assert (CBD_IS_CHANNEL_MANAGER (self));
356
357 return self->channels;
358 }