| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#define LIBCELLBROADCAST_USE_UNSTABLE_API |
| 10 |
#include "libcellbroadcast.h" |
| 11 |
|
| 12 |
#include <locale.h> |
| 13 |
#include <glib.h> |
| 14 |
|
| 15 |
int |
| 16 |
main (int argc, char *argv[0]) |
| 17 |
{ |
| 18 |
g_autoptr (GListModel) messages = NULL; |
| 19 |
g_autoptr (GOptionContext) opt_context = NULL; |
| 20 |
g_autoptr (GError) err = NULL; |
| 21 |
gboolean success = TRUE; |
| 22 |
gboolean do_list = FALSE; |
| 23 |
const GOptionEntry options [] = { |
| 24 |
{"list-messages", 'm', 0, G_OPTION_ARG_NONE, &do_list, |
| 25 |
"Lists all stored cell broadcast messages", NULL}, |
| 26 |
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } |
| 27 |
}; |
| 28 |
|
| 29 |
setlocale (LC_ALL, ""); |
| 30 |
opt_context = g_option_context_new ("- A cli for cellbroadcastd"); |
| 31 |
g_option_context_add_main_entries (opt_context, options, NULL); |
| 32 |
if (!g_option_context_parse (opt_context, &argc, &argv, &err)) { |
| 33 |
g_warning ("%s", err->message); |
| 34 |
return 1; |
| 35 |
} |
| 36 |
|
| 37 |
if (!lcb_init (&err)) { |
| 38 |
g_print ("Failed to init libcellbroadcast: %s\n", err->message); |
| 39 |
return 1; |
| 40 |
} |
| 41 |
|
| 42 |
if (!do_list) |
| 43 |
do_list = TRUE; |
| 44 |
|
| 45 |
if (do_list) { |
| 46 |
guint n_messages; |
| 47 |
|
| 48 |
messages = lcb_cbd_get_messages_sync (&err); |
| 49 |
success = !!messages; |
| 50 |
if (!messages) { |
| 51 |
g_printerr ("Failed to retrieve cell broadcast messages: %s\n", err->message); |
| 52 |
goto exit; |
| 53 |
} |
| 54 |
|
| 55 |
n_messages = g_list_model_get_n_items (messages); |
| 56 |
for (guint i = 0; i < n_messages; i++) { |
| 57 |
g_autoptr (LcbMessage) msg = g_list_model_get_item (messages, i); |
| 58 |
g_autofree char *severity = NULL; |
| 59 |
|
| 60 |
severity = g_flags_to_string (LCB_TYPE_SEVERITY_LEVEL, |
| 61 |
lcb_message_get_severity (msg)); |
| 62 |
|
| 63 |
g_print ("Channel : %" G_GUINT16_FORMAT "\n" |
| 64 |
"Message code : %" G_GUINT16_FORMAT "\n" |
| 65 |
"Update : %" G_GUINT16_FORMAT "\n" |
| 66 |
"Timestamp : %" G_GUINT64_FORMAT "\n" |
| 67 |
"Severity : %s\n" |
| 68 |
"Text : %s\n", |
| 69 |
lcb_message_get_channel (msg), |
| 70 |
lcb_message_get_msg_code (msg), |
| 71 |
lcb_message_get_update (msg), |
| 72 |
lcb_message_get_timestamp (msg), |
| 73 |
severity, |
| 74 |
lcb_message_get_text (msg)); |
| 75 |
|
| 76 |
if (i < n_messages - 1) |
| 77 |
g_print ("\n"); |
| 78 |
} |
| 79 |
|
| 80 |
if (g_list_model_get_n_items (messages) == 0) |
| 81 |
g_print ("No cell broadcast messages have yet been received\n"); |
| 82 |
} |
| 83 |
|
| 84 |
exit: |
| 85 |
|
| 86 |
lcb_uninit (); |
| 87 |
return !success; |
| 88 |
} |