Debian Codegraph @

src/cbd-service-providers.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-service-providers"
10
11 #include "cbd-channel-manager.h"
12 #include "cbd-service-providers.h"
13 #include "lcb-enums.h"
14
15 #include <gio/gio.h>
16 #include <gmobile.h>
17
18 typedef enum {
19 PARSER_TOPLEVEL = 0,
20 PARSER_COUNTRY,
21 PARSER_CBS,
22 PARSER_LEVEL,
23 PARSER_DONE,
24 PARSER_ERROR
25 } GsdParseContextState;
26
27
28 typedef struct {
29 GMarkupParseContext *ctx;
30 char *country;
31 char buffer[4096];
32
33 char *text_buffer;
34 GsdParseContextState state;
35
36 gboolean country_found;
37 LcbSeverityLevel current_level;
38 GArray *channels;
39 } CbdParseContext;
40
41
42 typedef struct
43 {
44 GAsyncResult *res;
45 GMainLoop *loop;
46 } GetChannelsSyncData;
47
48
49 static void
50 cbd_parse_context_free (CbdParseContext *parse_context)
51 {
52 g_markup_parse_context_free (parse_context->ctx);
53 g_free (parse_context->country);
54 g_clear_pointer (&parse_context->channels, g_array_unref);
55 g_clear_pointer (&parse_context->text_buffer, g_free);
56
57 g_free (parse_context);
58 }
59
60
61 static void
62 parser_toplevel_start (CbdParseContext *parse_context,
63 const char *name,
64 const char **attribute_names,
65 const char **attribute_values)
66 {
67 if (g_str_equal (name, "serviceproviders")) {
68 for (int i = 0; !gm_str_is_null_or_empty (attribute_names[i]); i++) {
69 if (g_str_equal (attribute_names[i], "format")) {
70 if (!g_str_equal (attribute_values[i], "2.0")) {
71 g_warning ("Mobile broadband provider database format '%s' not supported.",
72 attribute_values[i]);
73 parse_context->state = PARSER_ERROR;
74 break;
75 }
76 }
77 }
78 } else if (g_str_equal (name, "country")) {
79 parse_context->state = PARSER_COUNTRY;
80 parse_context->country_found = FALSE;
81
82 for (int i = 0; !gm_str_is_null_or_empty (attribute_names[i]); i++) {
83 if (g_str_equal (attribute_names[i], "code")) {
84 if (g_ascii_strcasecmp (attribute_values[i], parse_context->country) == 0) {
85 parse_context->country_found = TRUE;
86 break;
87 }
88 }
89 }
90 }
91 }
92
93
94 static void
95 parser_country_start (CbdParseContext *parse_context,
96 const char *name,
97 const char **attribute_names,
98 const char **attribute_values)
99 {
100 if (g_str_equal (name, "cbs"))
101 parse_context->state = PARSER_CBS;
102 }
103
104
105 static LcbSeverityLevel
106 type_to_level (const char *type)
107 {
108 g_debug ("%s", type);
109
110 if (g_str_equal (type, "presidential")) {
111 return LCB_SEVERITY_LEVEL_PRESIDENTIAL;
112 } else if (g_str_equal (type, "extreme")) {
113 return LCB_SEVERITY_LEVEL_EXTREME;
114 } else if (g_str_equal (type, "severe")) {
115 return LCB_SEVERITY_LEVEL_SEVERE;
116 } else if (g_str_equal (type, "public-safety")) {
117 return LCB_SEVERITY_LEVEL_PUBLIC_SAFETY;
118 } else if (g_str_equal (type, "amber")) {
119 return LCB_SEVERITY_LEVEL_AMBER;
120 } else if (g_str_equal (type, "test")) {
121 return LCB_SEVERITY_LEVEL_TEST;
122 }
123
124 return LCB_SEVERITY_LEVEL_UNKNOWN;
125 }
126
127
128 static void
129 parser_cbs_start (CbdParseContext *parse_context,
130 const char *name,
131 const char **attribute_names,
132 const char **attribute_values)
133 {
134 const char *type = NULL;
135
136 parse_context->current_level = LCB_SEVERITY_LEVEL_UNKNOWN;
137 if (g_str_equal (name, "level")) {
138 for (int i = 0; !gm_str_is_null_or_empty (attribute_names[i]); i++) {
139 if (g_str_equal (attribute_names[i], "type")) {
140 type = attribute_values[i];
141 parse_context->current_level = type_to_level (type);
142 break;
143 }
144 }
145 parse_context->state = PARSER_LEVEL;
146 }
147 }
148
149
150 static void
151 parser_level_start (CbdParseContext *parse_context,
152 const char *name,
153 const char **attribute_names,
154 const char **attribute_values)
155 {
156 if (g_str_equal (name, "channels")) {
157 const char *start_str = NULL, *end_str = NULL;
158
159 for (int i = 0; !gm_str_is_null_or_empty (attribute_names[i]); i++) {
160 if (g_str_equal (attribute_names[i], "start"))
161 start_str = attribute_values[i];
162 else if (g_str_equal (attribute_names[i], "end"))
163 end_str = attribute_values[i];
164
165 if (parse_context->country_found && start_str && end_str) {
166 gint64 start, end;
167 CbdChannelsRange ch;
168
169 start = g_ascii_strtoll (start_str, NULL, 10);
170 if (start > G_MAXINT16) {
171 g_warning ("%" G_GINT64_FORMAT " is not a valid channel number", start);
172 continue;
173 } else
174 ch.start = start;
175
176 end = g_ascii_strtoll (end_str, NULL, 10);
177 if (end > G_MAXINT16) {
178 g_warning ("%" G_GINT64_FORMAT " is not a valid channel number", end);
179 continue;
180 } else
181 ch.end = end;
182
183 ch.level = parse_context->current_level;
184 g_array_append_val (parse_context->channels, ch);
185 break;
186 }
187 }
188 }
189 }
190
191
192 static void
193 parser_start_element (GMarkupParseContext *context,
194 const char *element_name,
195 const char **attribute_names,
196 const char **attribute_values,
197 gpointer user_data,
198 GError **error)
199 {
200 CbdParseContext *parse_context = user_data;
201
202 g_clear_pointer (&parse_context->text_buffer, g_free);
203
204 switch (parse_context->state) {
205 case PARSER_TOPLEVEL:
206 parser_toplevel_start (parse_context, element_name, attribute_names, attribute_values);
207 break;
208 case PARSER_COUNTRY:
209 parser_country_start (parse_context, element_name, attribute_names, attribute_values);
210 break;
211 case PARSER_CBS:
212 parser_cbs_start (parse_context, element_name, attribute_names, attribute_values);
213 break;
214 case PARSER_LEVEL:
215 parser_level_start (parse_context, element_name, attribute_names, attribute_values);
216 break;
217 case PARSER_ERROR:
218 break;
219 case PARSER_DONE:
220 break;
221 }
222 }
223
224
225 static void
226 parser_country_end (CbdParseContext *parse_context, const char *name)
227 {
228 if (g_str_equal (name, "country")) {
229 g_clear_pointer (&parse_context->text_buffer, g_free);
230 parse_context->state = PARSER_TOPLEVEL;
231 }
232 }
233
234
235 static void
236 parser_cbs_end (CbdParseContext *parse_context, const char *name)
237 {
238 if (g_str_equal (name, "cbs")) {
239 g_clear_pointer (&parse_context->text_buffer, g_free);
240 parse_context->state = PARSER_COUNTRY;
241 }
242
243 if (parse_context->country_found)
244 parse_context->state = PARSER_DONE;
245 }
246
247
248 static void
249 parser_level_end (CbdParseContext *parse_context, const char *name)
250 {
251 if (g_str_equal (name, "level")) {
252 parse_context->current_level = LCB_SEVERITY_LEVEL_UNKNOWN;
253 g_clear_pointer (&parse_context->text_buffer, g_free);
254 parse_context->state = PARSER_CBS;
255 }
256 }
257
258
259 static void
260 parser_end_element (GMarkupParseContext *context,
261 const char *element_name,
262 gpointer user_data,
263 GError **error)
264 {
265 CbdParseContext *parse_context = user_data;
266
267 switch (parse_context->state) {
268 case PARSER_TOPLEVEL:
269 break;
270 case PARSER_COUNTRY:
271 parser_country_end (parse_context, element_name);
272 break;
273 case PARSER_CBS:
274 parser_cbs_end (parse_context, element_name);
275 break;
276 case PARSER_LEVEL:
277 parser_level_end (parse_context, element_name);
278 break;
279 case PARSER_ERROR:
280 break;
281 case PARSER_DONE:
282 break;
283 }
284 }
285
286
287 static void
288 parser_text (GMarkupParseContext *context,
289 const char *text,
290 gsize text_len,
291 gpointer user_data,
292 GError **error)
293 {
294 CbdParseContext *parse_context = user_data;
295
296 g_free (parse_context->text_buffer);
297 parse_context->text_buffer = g_strdup (text);
298 }
299
300
301 static const GMarkupParser parser = {
302 .start_element = parser_start_element,
303 .end_element = parser_end_element,
304 .text = parser_text,
305 .passthrough = NULL,
306 .error = NULL,
307 };
308
309
310 static void read_next_chunk (GInputStream *stream, GTask *task);
311
312
313 static void
314 on_stream_read_ready (GObject *source_object, GAsyncResult *res, gpointer user_data)
315 {
316 GInputStream *stream = G_INPUT_STREAM (source_object);
317 g_autoptr (GTask) task = G_TASK (user_data);
318 CbdParseContext *parse_context = g_task_get_task_data (task);
319 gssize len;
320 GError *error = NULL;
321
322 len = g_input_stream_read_finish (stream, res, &error);
323 if (len == -1) {
324 g_prefix_error (&error, "Error reading service provider database: ");
325 g_task_return_error (task, error);
326 return;
327 }
328
329 if (len == 0) {
330 g_set_error (&error,
331 G_IO_ERROR,
332 G_IO_ERROR_FAILED,
333 "Information for '%s' not found in service provider database",
334 parse_context->country);
335 g_task_return_error (task, error);
336 return;
337 }
338
339 if (!g_markup_parse_context_parse (parse_context->ctx, parse_context->buffer, len, &error)) {
340 g_prefix_error (&error, "Error parsing service provider database: ");
341 g_task_return_error (task, error);
342 return;
343 }
344
345 if (parse_context->state == PARSER_DONE) {
346 g_task_return_pointer (task,
347 g_steal_pointer (&parse_context->channels),
348 (GDestroyNotify)g_array_unref);
349 return;
350 }
351
352 read_next_chunk (stream, g_steal_pointer (&task));
353 }
354
355
356 static void
357 read_next_chunk (GInputStream *stream, GTask *task)
358 {
359 CbdParseContext *parse_context = g_task_get_task_data (task);
360
361 g_input_stream_read_async (stream,
362 parse_context->buffer,
363 sizeof (parse_context->buffer),
364 G_PRIORITY_DEFAULT,
365 g_task_get_cancellable (task),
366 on_stream_read_ready,
367 task);
368 }
369
370
371 static void
372 on_file_read_ready (GObject *source_object, GAsyncResult *res, gpointer user_data)
373 {
374 g_autoptr (GTask) task = G_TASK (user_data);
375 g_autoptr (GFileInputStream) stream = NULL;
376 GError *error = NULL;
377 GFile *file = G_FILE (source_object);
378
379 stream = g_file_read_finish (file, res, &error);
380 if (!stream) {
381 g_prefix_error (&error, "Error opening service provider database: ");
382 g_task_return_error (task, error);
383 return;
384 }
385
386 read_next_chunk (G_INPUT_STREAM (stream), g_steal_pointer (&task));
387 }
388
389
390 GArray *
391 cbd_service_providers_get_channels_finish (GAsyncResult *res,
392 GError **error)
393 {
394 g_assert (G_IS_TASK (res));
395 g_assert (g_task_get_source_tag(G_TASK (res)) == cbd_service_providers_get_channels);
396
397 return g_task_propagate_pointer (G_TASK (res), error);
398 }
399
400
401 void
402 cbd_service_providers_get_channels (const char *serviceproviders,
403 const char *country,
404 GCancellable *cancellable,
405 GAsyncReadyCallback callback,
406 gpointer user_data)
407 {
408 g_autoptr (GFile) file = NULL;
409 g_autoptr (GTask) task = g_task_new (NULL, cancellable, callback, user_data);
410 CbdParseContext *parse_context = g_new0 (CbdParseContext, 1);
411
412 g_assert (serviceproviders);
413 g_assert (country);
414
415 parse_context->country = g_strdup (country);
416 parse_context->ctx = g_markup_parse_context_new (&parser, 0, parse_context, NULL);
417 parse_context->channels = g_array_new (FALSE, FALSE, sizeof (CbdChannelsRange));
418
419 g_task_set_task_data (task, parse_context, (GDestroyNotify)cbd_parse_context_free);
420 g_task_set_source_tag (task, cbd_service_providers_get_channels);
421
422 file = g_file_new_for_path (serviceproviders);
423 g_file_read_async (file, G_PRIORITY_DEFAULT,
424 cancellable,
425 on_file_read_ready,
426 g_steal_pointer (&task));
427 }
428
429
430 static void
431 on_get_channels_ready (GObject *object, GAsyncResult *res, gpointer user_data)
432 {
433 GetChannelsSyncData *data = user_data;
434
435 g_assert (data->res == NULL);
436 data->res = g_object_ref (res);
437 g_main_loop_quit (data->loop);
438 }
439
440
441 GArray *
442 cbd_service_providers_get_channels_sync (const char *serviceproviders,
443 const char *country,
444 GError **error)
445 {
446 GArray *channels;
447 GetChannelsSyncData data;
448 g_autoptr (GMainContext) context = g_main_context_new ();
449 g_autoptr (GMainLoop) loop = NULL;
450
451 g_main_context_push_thread_default (context);
452 loop = g_main_loop_new (context, FALSE);
453
454 data = (GetChannelsSyncData) {
455 .loop = loop,
456 .res = NULL,
457 };
458
459 cbd_service_providers_get_channels (serviceproviders,
460 country,
461 NULL,
462 on_get_channels_ready,
463 &data);
464 g_main_loop_run (data.loop);
465
466 channels = cbd_service_providers_get_channels_finish (data.res, error);
467
468 g_clear_object (&data.res);
469 g_main_context_pop_thread_default (context);
470
471 return channels;
472 }