From 925f0bae11c0b49a464237c5c98921ffd68157e2 Mon Sep 17 00:00:00 2001 From: Som_SP Date: Wed, 20 Oct 2021 22:02:06 +0200 Subject: libmm-glib,mmcli: add support for the new signal 'SetupThresholds()' API Includes updates by Aleksander Morgado to fix mostly coding style issues. --- cli/mmcli-modem-signal.c | 197 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 169 insertions(+), 28 deletions(-) (limited to 'cli/mmcli-modem-signal.c') diff --git a/cli/mmcli-modem-signal.c b/cli/mmcli-modem-signal.c index 1b9b0e49..5237938a 100644 --- a/cli/mmcli-modem-signal.c +++ b/cli/mmcli-modem-signal.c @@ -47,14 +47,19 @@ static Context *ctx; /* Options */ static gboolean get_flag; static gchar *setup_str; +static gchar *setup_thresholds_str; static GOptionEntry entries[] = { { "signal-setup", 0, 0, G_OPTION_ARG_STRING, &setup_str, - "Setup extended signal information retrieval", + "Setup signal quality information polling, in seconds", "[Rate]" }, + { "signal-setup-thresholds", 0, 0, G_OPTION_ARG_STRING, &setup_thresholds_str, + "Setup signal quality information thresholds (allowed keys: rssi-threshold, error-rate-threshold)", + "[\"key=value,...\"]" + }, { "signal-get", 0, 0, G_OPTION_ARG_NONE, &get_flag, - "Get all extended signal quality information", + "Get all signal quality information", NULL }, { NULL } @@ -85,6 +90,7 @@ mmcli_modem_signal_options_enabled (void) return !!n_actions; n_actions = (!!setup_str + + !!setup_thresholds_str + get_flag); if (n_actions > 1) { @@ -139,6 +145,8 @@ print_signal_info (void) MMSignal *signal; gdouble value; gchar *refresh_rate; + gchar *rssi_threshold; + gchar *error_rate_threshold; gchar *cdma1x_rssi = NULL; gchar *cdma1x_ecio = NULL; gchar *cdma1x_error_rate = NULL; @@ -163,7 +171,9 @@ print_signal_info (void) gchar *nr5g_snr = NULL; gchar *nr5g_error_rate = NULL; - refresh_rate = g_strdup_printf ("%u", mm_modem_signal_get_rate (ctx->modem_signal)); + refresh_rate = g_strdup_printf ("%u", mm_modem_signal_get_rate (ctx->modem_signal)); + rssi_threshold = g_strdup_printf ("%u", mm_modem_signal_get_rssi_threshold (ctx->modem_signal)); + error_rate_threshold = g_strdup_printf ("%s", mm_modem_signal_get_error_rate_threshold (ctx->modem_signal) ? "yes" : "no"); signal = mm_modem_signal_peek_cdma (ctx->modem_signal); if (signal) { @@ -235,33 +245,126 @@ print_signal_info (void) nr5g_error_rate = g_strdup_printf ("%.2lf", value); } - mmcli_output_string_take_typed (MMC_F_SIGNAL_REFRESH_RATE, refresh_rate, "seconds"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_CDMA1X_RSSI, cdma1x_rssi, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_CDMA1X_ECIO, cdma1x_ecio, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_CDMA1X_ERROR_RATE, cdma1x_error_rate, "%%"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_RSSI, evdo_rssi, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_ECIO, evdo_ecio, "dB"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_SINR, evdo_sinr, "dB"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_IO, evdo_io, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_ERROR_RATE, evdo_error_rate, "%%"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_GSM_RSSI, gsm_rssi, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_GSM_ERROR_RATE, gsm_error_rate, "%%"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_RSSI, umts_rssi, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_RSCP, umts_rscp, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_ECIO, umts_ecio, "dB"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_ERROR_RATE, umts_error_rate, "%%"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_RSSI, lte_rssi, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_RSRQ, lte_rsrq, "dB"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_RSRP, lte_rsrp, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_SNR, lte_snr, "dB"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_ERROR_RATE, lte_error_rate, "%%"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_RSRQ, nr5g_rsrq, "dB"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_RSRP, nr5g_rsrp, "dBm"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_SNR, nr5g_snr, "dB"); - mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_ERROR_RATE, nr5g_error_rate, "%%"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_REFRESH_RATE, refresh_rate, "seconds"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_RSSI_THRESHOLD, rssi_threshold, "dBm"); + mmcli_output_string_take (MMC_F_SIGNAL_ERROR_RATE_THRESHOLD, error_rate_threshold); + mmcli_output_string_take_typed (MMC_F_SIGNAL_CDMA1X_RSSI, cdma1x_rssi, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_CDMA1X_ECIO, cdma1x_ecio, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_CDMA1X_ERROR_RATE, cdma1x_error_rate, "%%"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_RSSI, evdo_rssi, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_ECIO, evdo_ecio, "dB"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_SINR, evdo_sinr, "dB"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_IO, evdo_io, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_EVDO_ERROR_RATE, evdo_error_rate, "%%"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_GSM_RSSI, gsm_rssi, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_GSM_ERROR_RATE, gsm_error_rate, "%%"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_RSSI, umts_rssi, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_RSCP, umts_rscp, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_ECIO, umts_ecio, "dB"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_UMTS_ERROR_RATE, umts_error_rate, "%%"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_RSSI, lte_rssi, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_RSRQ, lte_rsrq, "dB"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_RSRP, lte_rsrp, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_SNR, lte_snr, "dB"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_LTE_ERROR_RATE, lte_error_rate, "%%"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_RSRQ, nr5g_rsrq, "dB"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_RSRP, nr5g_rsrp, "dBm"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_SNR, nr5g_snr, "dB"); + mmcli_output_string_take_typed (MMC_F_SIGNAL_5G_ERROR_RATE, nr5g_error_rate, "%%"); mmcli_output_dump (); } +typedef struct { + guint rssi_threshold; + gboolean rssi_set; + gboolean error_rate_threshold; + gboolean error_rate_set; + GError *error; +} ParseKeyValueContext; + +static gboolean +key_value_foreach (const gchar *key, + const gchar *value, + ParseKeyValueContext *parse_ctx) +{ + if (g_str_equal (key, "rssi-threshold")) { + if (!mm_get_uint_from_str (value, &parse_ctx->rssi_threshold)) { + g_set_error (&parse_ctx->error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, + "invalid RSSI threshold value given: %s", value); + return FALSE; + } + parse_ctx->rssi_set = TRUE; + } else if (g_str_equal (key, "error-rate-threshold")) { + parse_ctx->error_rate_threshold = mm_common_get_boolean_from_string (value, &parse_ctx->error); + if (parse_ctx->error) + return FALSE; + parse_ctx->error_rate_set = TRUE; + } else { + g_set_error (&parse_ctx->error, MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED, + "Invalid properties string, unsupported key '%s'", key); + return FALSE; + } + return TRUE; +} + +static GVariant * +setup_thresholds_build_input (const gchar *str) +{ + ParseKeyValueContext parse_ctx = { 0 }; + GVariantBuilder builder; + + mm_common_parse_key_value_string (setup_thresholds_str, + &parse_ctx.error, + (MMParseKeyValueForeachFn)key_value_foreach, + &parse_ctx); + /* If error, destroy the object */ + if (parse_ctx.error) { + return NULL; + } + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}")); + + if (parse_ctx.rssi_set) + g_variant_builder_add (&builder, + "{sv}", + "rssi-threshold", + g_variant_new_uint32 (parse_ctx.rssi_threshold)); + + if (parse_ctx.error_rate_set) + g_variant_builder_add (&builder, + "{sv}", + "error-rate-threshold", + g_variant_new_boolean (parse_ctx.error_rate_threshold)); + + return g_variant_ref_sink (g_variant_builder_end (&builder)); +} + +static void +setup_thresholds_process_reply (gboolean result, + const GError *error) +{ + if (!result) { + g_printerr ("error: couldn't setup signal quality information thresholds: '%s'\n", + error ? error->message : "unknown error"); + exit (EXIT_FAILURE); + } + + g_print ("Successfully setup signal quality information thresholds\n"); +} + +static void +setup_thresholds_ready (MMModemSignal *modem, + GAsyncResult *result) +{ + gboolean res; + GError *error = NULL; + + res = mm_modem_signal_setup_thresholds_finish (modem, result, &error); + setup_thresholds_process_reply (res, error); + + mmcli_async_operation_done (); +} + static void setup_process_reply (gboolean result, const GError *error) @@ -322,6 +425,25 @@ get_modem_ready (GObject *source, return; } + /* Request to setup threshold? */ + if (setup_thresholds_str) { + g_autoptr(GVariant) dictionary = NULL; + + dictionary = setup_thresholds_build_input (setup_thresholds_str); + if (!dictionary) { + g_printerr ("error: failed to parse input threshold setup settings: '%s'", setup_thresholds_str); + exit (EXIT_FAILURE); + } + + g_debug ("Asynchronously setting up threshold values..."); + mm_modem_signal_setup_thresholds (ctx->modem_signal, + dictionary, + ctx->cancellable, + (GAsyncReadyCallback)setup_thresholds_ready, + NULL); + return; + } + g_warn_if_reached (); } @@ -368,7 +490,7 @@ mmcli_modem_signal_run_synchronous (GDBusConnection *connection) /* Request to set rate? */ if (setup_str) { - guint rate; + guint rate; gboolean result; if (!mm_get_uint_from_str (setup_str, &rate)) { @@ -385,6 +507,25 @@ mmcli_modem_signal_run_synchronous (GDBusConnection *connection) return; } + /* Request to setup threshold? */ + if (setup_thresholds_str) { + g_autoptr(GVariant) dictionary = NULL; + gboolean result; + + dictionary = setup_thresholds_build_input (setup_thresholds_str); + if (!dictionary) { + g_printerr ("error: failed to parse input threshold setup settings: '%s'", setup_thresholds_str); + exit (EXIT_FAILURE); + } + + g_debug ("Asynchronously setting up threshold values..."); + result = mm_modem_signal_setup_thresholds_sync (ctx->modem_signal, + dictionary, + NULL, + &error); + setup_thresholds_process_reply (result, error); + return; + } g_warn_if_reached (); } -- cgit v1.2.3-70-g09d2