/cli/mmcli-modem.c
C | 1262 lines | 1006 code | 185 blank | 71 comment | 83 complexity | f918ddb2aa4cbdd8fc446f069380cfe7 MD5 | raw file
Possible License(s): GPL-2.0
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2/* 3 * mmcli -- Control modem status & access information from the command line 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 * Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org> 19 */ 20 21#include "config.h" 22 23#include <stdio.h> 24#include <stdlib.h> 25#include <locale.h> 26 27#include <glib.h> 28#include <gio/gio.h> 29 30#define _LIBMM_INSIDE_MMCLI 31#include <libmm-glib.h> 32 33#include "mmcli.h" 34#include "mmcli-common.h" 35 36/* Context */ 37typedef struct { 38 MMManager *manager; 39 GCancellable *cancellable; 40 MMObject *object; 41 MMModem *modem; 42 MMModem3gpp *modem_3gpp; 43 MMModemCdma *modem_cdma; 44} Context; 45static Context *ctx; 46 47/* Options */ 48static gboolean info_flag; /* set when no action found */ 49static gboolean monitor_state_flag; 50static gboolean enable_flag; 51static gboolean disable_flag; 52static gboolean set_power_state_on_flag; 53static gboolean set_power_state_low_flag; 54static gboolean reset_flag; 55static gchar *factory_reset_str; 56static gchar *command_str; 57static gboolean list_bearers_flag; 58static gchar *create_bearer_str; 59static gchar *delete_bearer_str; 60static gchar *set_allowed_modes_str; 61static gchar *set_preferred_mode_str; 62static gchar *set_bands_str; 63 64static GOptionEntry entries[] = { 65 { "monitor-state", 'w', 0, G_OPTION_ARG_NONE, &monitor_state_flag, 66 "Monitor state of a given modem", 67 NULL 68 }, 69 { "enable", 'e', 0, G_OPTION_ARG_NONE, &enable_flag, 70 "Enable a given modem", 71 NULL 72 }, 73 { "disable", 'd', 0, G_OPTION_ARG_NONE, &disable_flag, 74 "Disable a given modem", 75 NULL 76 }, 77 { "set-power-state-on", 0, 0, G_OPTION_ARG_NONE, &set_power_state_on_flag, 78 "Set full power state in the modem", 79 NULL 80 }, 81 { "set-power-state-low", 0, 0, G_OPTION_ARG_NONE, &set_power_state_low_flag, 82 "Set low power state in the modem", 83 NULL 84 }, 85 { "reset", 'r', 0, G_OPTION_ARG_NONE, &reset_flag, 86 "Reset a given modem", 87 NULL 88 }, 89 { "factory-reset", 0, 0, G_OPTION_ARG_STRING, &factory_reset_str, 90 "Reset a given modem to its factory state", 91 "[CODE]" 92 }, 93 { "command", 0, 0, G_OPTION_ARG_STRING, &command_str, 94 "Send an AT command to the modem", 95 "[COMMAND]" 96 }, 97 { "list-bearers", 0, 0, G_OPTION_ARG_NONE, &list_bearers_flag, 98 "List packet data bearers available in a given modem", 99 NULL 100 }, 101 { "create-bearer", 0, 0, G_OPTION_ARG_STRING, &create_bearer_str, 102 "Create a new packet data bearer in a given modem", 103 "[\"key=value,...\"]" 104 }, 105 { "delete-bearer", 0, 0, G_OPTION_ARG_STRING, &delete_bearer_str, 106 "Delete a data bearer from a given modem", 107 "[PATH]" 108 }, 109 { "set-allowed-modes", 0, 0, G_OPTION_ARG_STRING, &set_allowed_modes_str, 110 "Set allowed modes in a given modem.", 111 "[MODE1|MODE2...]" 112 }, 113 { "set-bands", 0, 0, G_OPTION_ARG_STRING, &set_bands_str, 114 "Set bands to be used by a given modem.", 115 "[BAND1|BAND2...]" 116 }, 117 { "set-preferred-mode", 0, 0, G_OPTION_ARG_STRING, &set_preferred_mode_str, 118 "Set preferred mode in a given modem (Must give allowed modes with --set-allowed-modes)", 119 "[MODE]" 120 }, 121 { NULL } 122}; 123 124GOptionGroup * 125mmcli_modem_get_option_group (void) 126{ 127 GOptionGroup *group; 128 129 /* Status options */ 130 group = g_option_group_new ("modem", 131 "Modem options", 132 "Show modem options", 133 NULL, 134 NULL); 135 g_option_group_add_entries (group, entries); 136 137 return group; 138} 139 140gboolean 141mmcli_modem_options_enabled (void) 142{ 143 static guint n_actions = 0; 144 static gboolean checked = FALSE; 145 146 if (checked) 147 return !!n_actions; 148 149 n_actions = (monitor_state_flag + 150 enable_flag + 151 disable_flag + 152 set_power_state_on_flag + 153 set_power_state_low_flag + 154 reset_flag + 155 list_bearers_flag + 156 !!create_bearer_str + 157 !!delete_bearer_str + 158 !!factory_reset_str + 159 !!command_str + 160 !!set_allowed_modes_str + 161 !!set_preferred_mode_str + 162 !!set_bands_str); 163 164 if (n_actions == 0 && mmcli_get_common_modem_string ()) { 165 /* default to info */ 166 info_flag = TRUE; 167 n_actions++; 168 } 169 170 if (set_preferred_mode_str) { 171 if (!set_allowed_modes_str) { 172 g_printerr ("error: setting preferred mode requires list of allowed modes\n"); 173 exit (EXIT_FAILURE); 174 } 175 n_actions--; 176 } 177 178 if (n_actions > 1) { 179 g_printerr ("error: too many modem actions requested\n"); 180 exit (EXIT_FAILURE); 181 } 182 183 if (monitor_state_flag) 184 mmcli_force_async_operation (); 185 186 if (info_flag) 187 mmcli_force_sync_operation (); 188 189 checked = TRUE; 190 return !!n_actions; 191} 192 193static void 194context_free (Context *ctx) 195{ 196 if (!ctx) 197 return; 198 199 if (ctx->cancellable) 200 g_object_unref (ctx->cancellable); 201 if (ctx->modem) 202 g_object_unref (ctx->modem); 203 if (ctx->modem_3gpp) 204 g_object_unref (ctx->modem_3gpp); 205 if (ctx->modem_cdma) 206 g_object_unref (ctx->modem_cdma); 207 if (ctx->object) 208 g_object_unref (ctx->object); 209 if (ctx->manager) 210 g_object_unref (ctx->manager); 211 g_free (ctx); 212} 213 214void 215mmcli_modem_shutdown (void) 216{ 217 context_free (ctx); 218} 219 220static void 221cancelled (GCancellable *cancellable) 222{ 223 mmcli_async_operation_done (); 224} 225 226static void 227print_bearer_short_info (MMBearer *bearer) 228{ 229 g_print ("\t%s\n", 230 mm_bearer_get_path (bearer)); 231} 232 233static void 234print_modem_info (void) 235{ 236 gchar *drivers_string; 237 gchar *prefixed_revision; 238 gchar *modem_capabilities_string; 239 gchar *current_capabilities_string; 240 gchar *access_technologies_string; 241 gchar *supported_modes_string; 242 gchar *allowed_modes_string; 243 gchar *preferred_mode_string; 244 gchar *supported_bands_string; 245 gchar *bands_string; 246 gchar *unlock_retries_string; 247 gchar *own_numbers_string; 248 MMModemBand *bands = NULL; 249 MMUnlockRetries *unlock_retries; 250 guint n_bands = 0; 251 guint signal_quality = 0; 252 gboolean signal_quality_recent = FALSE; 253 254 /* Not the best thing to do, as we may be doing _get() calls twice, but 255 * easiest to maintain */ 256#undef VALIDATE_UNKNOWN 257#define VALIDATE_UNKNOWN(str) (str ? str : "unknown") 258#undef VALIDATE_PATH 259#define VALIDATE_PATH(str) ((str && !g_str_equal (str, "/")) ? str : "none") 260 261 /* Strings in heap */ 262 modem_capabilities_string = mm_modem_capability_build_string_from_mask ( 263 mm_modem_get_modem_capabilities (ctx->modem)); 264 current_capabilities_string = mm_modem_capability_build_string_from_mask ( 265 mm_modem_get_current_capabilities (ctx->modem)); 266 access_technologies_string = mm_modem_access_technology_build_string_from_mask ( 267 mm_modem_get_access_technologies (ctx->modem)); 268 mm_modem_get_bands (ctx->modem, &bands, &n_bands); 269 bands_string = mm_common_build_bands_string (bands, n_bands); 270 g_free (bands); 271 mm_modem_get_supported_bands (ctx->modem, &bands, &n_bands); 272 supported_bands_string = mm_common_build_bands_string (bands, n_bands); 273 g_free (bands); 274 allowed_modes_string = mm_modem_mode_build_string_from_mask ( 275 mm_modem_get_allowed_modes (ctx->modem)); 276 preferred_mode_string = mm_modem_mode_build_string_from_mask ( 277 mm_modem_get_preferred_mode (ctx->modem)); 278 supported_modes_string = mm_modem_mode_build_string_from_mask ( 279 mm_modem_get_supported_modes (ctx->modem)); 280 281 unlock_retries = mm_modem_get_unlock_retries (ctx->modem); 282 unlock_retries_string = mm_unlock_retries_build_string (unlock_retries); 283 g_object_unref (unlock_retries); 284 285 if (mm_modem_get_own_numbers (ctx->modem)) { 286 own_numbers_string = g_strjoinv (", ", (gchar **)mm_modem_get_own_numbers (ctx->modem)); 287 if (!own_numbers_string[0]) { 288 g_free (own_numbers_string); 289 own_numbers_string = NULL; 290 } 291 } else 292 own_numbers_string = NULL; 293 294 if (mm_modem_get_drivers (ctx->modem)) { 295 drivers_string = g_strjoinv (", ", (gchar **)mm_modem_get_drivers (ctx->modem)); 296 if (!drivers_string[0]) { 297 g_free (drivers_string); 298 drivers_string = NULL; 299 } 300 } else 301 drivers_string = NULL; 302 303 /* Rework possible multiline strings */ 304 if (mm_modem_get_revision (ctx->modem)) 305 prefixed_revision = mmcli_prefix_newlines (" | ", 306 mm_modem_get_revision (ctx->modem)); 307 else 308 prefixed_revision = NULL; 309 310 /* Get signal quality info */ 311 signal_quality = mm_modem_get_signal_quality (ctx->modem, &signal_quality_recent); 312 313 /* Global IDs */ 314 g_print ("\n" 315 "%s (device id '%s')\n", 316 VALIDATE_UNKNOWN (mm_modem_get_path (ctx->modem)), 317 VALIDATE_UNKNOWN (mm_modem_get_device_identifier (ctx->modem))); 318 319 /* Hardware related stuff */ 320 g_print (" -------------------------\n" 321 " Hardware | manufacturer: '%s'\n" 322 " | model: '%s'\n" 323 " | revision: '%s'\n" 324 " | capabilities: '%s'\n" 325 " | current: '%s'\n" 326 " | equipment id: '%s'\n", 327 VALIDATE_UNKNOWN (mm_modem_get_manufacturer (ctx->modem)), 328 VALIDATE_UNKNOWN (mm_modem_get_model (ctx->modem)), 329 VALIDATE_UNKNOWN (prefixed_revision), 330 VALIDATE_UNKNOWN (modem_capabilities_string), 331 VALIDATE_UNKNOWN (current_capabilities_string), 332 VALIDATE_UNKNOWN (mm_modem_get_equipment_identifier (ctx->modem))); 333 334 /* System related stuff */ 335 g_print (" -------------------------\n" 336 " System | device: '%s'\n" 337 " | drivers: '%s'\n" 338 " | plugin: '%s'\n" 339 " | primary port: '%s'\n", 340 VALIDATE_UNKNOWN (mm_modem_get_device (ctx->modem)), 341 VALIDATE_UNKNOWN (drivers_string), 342 VALIDATE_UNKNOWN (mm_modem_get_plugin (ctx->modem)), 343 VALIDATE_UNKNOWN (mm_modem_get_primary_port (ctx->modem))); 344 345 /* Numbers related stuff */ 346 g_print (" -------------------------\n" 347 " Numbers | own : '%s'\n", 348 VALIDATE_UNKNOWN (own_numbers_string)); 349 350 /* Status related stuff */ 351 g_print (" -------------------------\n" 352 " Status | lock: '%s'\n" 353 " | unlock retries: '%s'\n" 354 " | state: '%s'\n", 355 mm_modem_lock_get_string (mm_modem_get_unlock_required (ctx->modem)), 356 VALIDATE_UNKNOWN (unlock_retries_string), 357 VALIDATE_UNKNOWN (mm_modem_state_get_string (mm_modem_get_state (ctx->modem)))); 358 359 if (mm_modem_get_state (ctx->modem) == MM_MODEM_STATE_FAILED) 360 g_print (" | failed reason: '%s'\n", 361 VALIDATE_UNKNOWN (mm_modem_state_failed_reason_get_string (mm_modem_get_state_failed_reason (ctx->modem)))); 362 363 g_print (" | power state: '%s'\n" 364 " | access tech: '%s'\n" 365 " | signal quality: '%u' (%s)\n", 366 VALIDATE_UNKNOWN (mm_modem_power_state_get_string (mm_modem_get_power_state (ctx->modem))), 367 VALIDATE_UNKNOWN (access_technologies_string), 368 signal_quality, signal_quality_recent ? "recent" : "cached"); 369 370 /* Modes */ 371 g_print (" -------------------------\n" 372 " Modes | supported: '%s'\n" 373 " | allowed: '%s'\n" 374 " | preferred: '%s'\n", 375 VALIDATE_UNKNOWN (supported_modes_string), 376 VALIDATE_UNKNOWN (allowed_modes_string), 377 VALIDATE_UNKNOWN (preferred_mode_string)); 378 379 /* Band related stuff */ 380 g_print (" -------------------------\n" 381 " Bands | supported: '%s'\n" 382 " | current: '%s'\n", 383 VALIDATE_UNKNOWN (supported_bands_string), 384 VALIDATE_UNKNOWN (bands_string)); 385 386 /* If available, 3GPP related stuff */ 387 if (ctx->modem_3gpp) { 388 gchar *facility_locks; 389 390 facility_locks = (mm_modem_3gpp_facility_build_string_from_mask ( 391 mm_modem_3gpp_get_enabled_facility_locks (ctx->modem_3gpp))); 392 g_print (" -------------------------\n" 393 " 3GPP | imei: '%s'\n" 394 " | enabled locks: '%s'\n" 395 " | operator id: '%s'\n" 396 " | operator name: '%s'\n" 397 " | registration: '%s'\n", 398 VALIDATE_UNKNOWN (mm_modem_3gpp_get_imei (ctx->modem_3gpp)), 399 facility_locks, 400 VALIDATE_UNKNOWN (mm_modem_3gpp_get_operator_code (ctx->modem_3gpp)), 401 VALIDATE_UNKNOWN (mm_modem_3gpp_get_operator_name (ctx->modem_3gpp)), 402 mm_modem_3gpp_registration_state_get_string ( 403 mm_modem_3gpp_get_registration_state ((ctx->modem_3gpp)))); 404 405 g_free (facility_locks); 406 } 407 408 /* If available, CDMA related stuff */ 409 if (ctx->modem_cdma) { 410 guint sid; 411 guint nid; 412 gchar *sid_str; 413 gchar *nid_str; 414 415 sid = mm_modem_cdma_get_sid (ctx->modem_cdma); 416 sid_str = (sid != MM_MODEM_CDMA_SID_UNKNOWN ? 417 g_strdup_printf ("%u", sid) : 418 NULL); 419 nid = mm_modem_cdma_get_nid (ctx->modem_cdma); 420 nid_str = (nid != MM_MODEM_CDMA_NID_UNKNOWN ? 421 g_strdup_printf ("%u", nid) : 422 NULL); 423 424 g_print (" -------------------------\n" 425 " CDMA | meid: '%s'\n" 426 " | esn: '%s'\n" 427 " | sid: '%s'\n" 428 " | nid: '%s'\n" 429 " | registration: CDMA1x '%s'\n" 430 " | EV-DO '%s'\n", 431 VALIDATE_UNKNOWN (mm_modem_cdma_get_meid (ctx->modem_cdma)), 432 VALIDATE_UNKNOWN (mm_modem_cdma_get_esn (ctx->modem_cdma)), 433 VALIDATE_UNKNOWN (sid_str), 434 VALIDATE_UNKNOWN (nid_str), 435 mm_modem_cdma_registration_state_get_string ( 436 mm_modem_cdma_get_cdma1x_registration_state ((ctx->modem_cdma))), 437 mm_modem_cdma_registration_state_get_string ( 438 mm_modem_cdma_get_evdo_registration_state ((ctx->modem_cdma)))); 439 440 g_free (sid_str); 441 g_free (nid_str); 442 } 443 444 /* SIM */ 445 g_print (" -------------------------\n" 446 " SIM | path: '%s'\n", 447 VALIDATE_PATH (mm_modem_get_sim_path (ctx->modem))); 448 g_print ("\n"); 449 450 g_free (bands_string); 451 g_free (supported_bands_string); 452 g_free (access_technologies_string); 453 g_free (modem_capabilities_string); 454 g_free (current_capabilities_string); 455 g_free (prefixed_revision); 456 g_free (allowed_modes_string); 457 g_free (preferred_mode_string); 458 g_free (supported_modes_string); 459 g_free (unlock_retries_string); 460 g_free (own_numbers_string); 461 g_free (drivers_string); 462} 463 464static void 465enable_process_reply (gboolean result, 466 const GError *error) 467{ 468 if (!result) { 469 g_printerr ("error: couldn't enable the modem: '%s'\n", 470 error ? error->message : "unknown error"); 471 exit (EXIT_FAILURE); 472 } 473 474 g_print ("successfully enabled the modem\n"); 475} 476 477static void 478enable_ready (MMModem *modem, 479 GAsyncResult *result, 480 gpointer nothing) 481{ 482 gboolean operation_result; 483 GError *error = NULL; 484 485 operation_result = mm_modem_enable_finish (modem, result, &error); 486 enable_process_reply (operation_result, error); 487 488 mmcli_async_operation_done (); 489} 490 491static void 492disable_process_reply (gboolean result, 493 const GError *error) 494{ 495 if (!result) { 496 g_printerr ("error: couldn't disable the modem: '%s'\n", 497 error ? error->message : "unknown error"); 498 exit (EXIT_FAILURE); 499 } 500 501 g_print ("successfully disabled the modem\n"); 502} 503 504static void 505disable_ready (MMModem *modem, 506 GAsyncResult *result, 507 gpointer nothing) 508{ 509 gboolean operation_result; 510 GError *error = NULL; 511 512 operation_result = mm_modem_disable_finish (modem, result, &error); 513 disable_process_reply (operation_result, error); 514 515 mmcli_async_operation_done (); 516} 517 518static void 519set_power_state_process_reply (gboolean result, 520 const GError *error) 521{ 522 if (!result) { 523 g_printerr ("error: couldn't set new power state in the modem: '%s'\n", 524 error ? error->message : "unknown error"); 525 exit (EXIT_FAILURE); 526 } 527 528 g_print ("successfully set new power state in the modem\n"); 529} 530 531static void 532set_power_state_ready (MMModem *modem, 533 GAsyncResult *result, 534 gpointer nothing) 535{ 536 gboolean operation_result; 537 GError *error = NULL; 538 539 operation_result = mm_modem_set_power_state_finish (modem, result, &error); 540 set_power_state_process_reply (operation_result, error); 541 542 mmcli_async_operation_done (); 543} 544 545static void 546reset_process_reply (gboolean result, 547 const GError *error) 548{ 549 if (!result) { 550 g_printerr ("error: couldn't reset the modem: '%s'\n", 551 error ? error->message : "unknown error"); 552 exit (EXIT_FAILURE); 553 } 554 555 g_print ("successfully reseted the modem\n"); 556} 557 558static void 559reset_ready (MMModem *modem, 560 GAsyncResult *result, 561 gpointer nothing) 562{ 563 gboolean operation_result; 564 GError *error = NULL; 565 566 operation_result = mm_modem_reset_finish (modem, result, &error); 567 reset_process_reply (operation_result, error); 568 569 mmcli_async_operation_done (); 570} 571 572static void 573factory_reset_process_reply (gboolean result, 574 const GError *error) 575{ 576 if (!result) { 577 g_printerr ("error: couldn't reset the modem to factory state: '%s'\n", 578 error ? error->message : "unknown error"); 579 exit (EXIT_FAILURE); 580 } 581 582 g_print ("successfully reseted the modem to factory state\n"); 583} 584 585static void 586factory_reset_ready (MMModem *modem, 587 GAsyncResult *result, 588 gpointer nothing) 589{ 590 gboolean operation_result; 591 GError *error = NULL; 592 593 operation_result = mm_modem_factory_reset_finish (modem, result, &error); 594 factory_reset_process_reply (operation_result, error); 595 596 mmcli_async_operation_done (); 597} 598 599 600static void 601command_process_reply (gchar *result, 602 const GError *error) 603{ 604 if (!result) { 605 g_printerr ("error: command failed: '%s'\n", 606 error ? error->message : "unknown error"); 607 exit (EXIT_FAILURE); 608 } 609 610 g_print ("response: '%s'\n", result); 611 g_free (result); 612} 613 614static void 615command_ready (MMModem *modem, 616 GAsyncResult *result, 617 gpointer nothing) 618{ 619 gchar * operation_result; 620 GError *error = NULL; 621 622 operation_result = mm_modem_command_finish (modem, result, &error); 623 command_process_reply (operation_result, error); 624 625 mmcli_async_operation_done (); 626} 627 628static guint 629command_get_timeout (MMModem *modem) 630{ 631 gint timeout; 632 633 /* If --timeout was given, it should already have been set in the proxy */ 634 timeout = (g_dbus_proxy_get_default_timeout (G_DBUS_PROXY (modem)) / 1000) - 1; 635 if (timeout <= 0) { 636 g_printerr ("error: timeout is too short (%d)\n", timeout); 637 exit (EXIT_FAILURE); 638 } 639 640 return (guint)timeout; 641} 642 643static void 644list_bearers_process_reply (GList *result, 645 const GError *error) 646{ 647 if (error) { 648 g_printerr ("error: couldn't list bearers: '%s'\n", 649 error->message); 650 exit (EXIT_FAILURE); 651 } 652 653 g_print ("\n"); 654 if (!result) { 655 g_print ("No bearers were found\n"); 656 } else { 657 GList *l; 658 659 g_print ("Found %u bearers:\n", g_list_length (result)); 660 for (l = result; l; l = g_list_next (l)) { 661 MMBearer *bearer = MM_BEARER (l->data); 662 663 g_print ("\n"); 664 print_bearer_short_info (bearer); 665 g_object_unref (bearer); 666 } 667 g_list_free (result); 668 } 669} 670 671static void 672list_bearers_ready (MMModem *modem, 673 GAsyncResult *result, 674 gpointer nothing) 675{ 676 GList *operation_result; 677 GError *error = NULL; 678 679 operation_result = mm_modem_list_bearers_finish (modem, result, &error); 680 list_bearers_process_reply (operation_result, error); 681 682 mmcli_async_operation_done (); 683} 684 685static void 686create_bearer_process_reply (MMBearer *bearer, 687 const GError *error) 688{ 689 if (!bearer) { 690 g_printerr ("error: couldn't create new bearer: '%s'\n", 691 error ? error->message : "unknown error"); 692 exit (EXIT_FAILURE); 693 } 694 695 g_print ("Successfully created new bearer in modem:\n"); 696 print_bearer_short_info (bearer); 697 g_object_unref (bearer); 698} 699 700static void 701create_bearer_ready (MMModem *modem, 702 GAsyncResult *result, 703 gpointer nothing) 704{ 705 MMBearer *bearer; 706 GError *error = NULL; 707 708 bearer = mm_modem_create_bearer_finish (modem, result, &error); 709 create_bearer_process_reply (bearer, error); 710 711 mmcli_async_operation_done (); 712} 713 714static void 715delete_bearer_process_reply (gboolean result, 716 const GError *error) 717{ 718 if (!result) { 719 g_printerr ("error: couldn't delete bearer: '%s'\n", 720 error ? error->message : "unknown error"); 721 exit (EXIT_FAILURE); 722 } 723 724 g_print ("successfully deleted bearer from modem\n"); 725} 726 727static void 728delete_bearer_ready (MMModem *modem, 729 GAsyncResult *result, 730 gpointer nothing) 731{ 732 gboolean operation_result; 733 GError *error = NULL; 734 735 operation_result = mm_modem_delete_bearer_finish (modem, result, &error); 736 delete_bearer_process_reply (operation_result, error); 737 738 mmcli_async_operation_done (); 739} 740 741static void 742set_allowed_modes_process_reply (gboolean result, 743 const GError *error) 744{ 745 if (!result) { 746 g_printerr ("error: couldn't set allowed modes: '%s'\n", 747 error ? error->message : "unknown error"); 748 exit (EXIT_FAILURE); 749 } 750 751 g_print ("successfully set allowed modes in the modem\n"); 752} 753 754static void 755set_allowed_modes_ready (MMModem *modem, 756 GAsyncResult *result, 757 gpointer nothing) 758{ 759 gboolean operation_result; 760 GError *error = NULL; 761 762 operation_result = mm_modem_set_allowed_modes_finish (modem, result, &error); 763 set_allowed_modes_process_reply (operation_result, error); 764 765 mmcli_async_operation_done (); 766} 767 768static void 769parse_modes (MMModemMode *allowed, 770 MMModemMode *preferred) 771{ 772 GError *error = NULL; 773 774 *allowed = mm_common_get_modes_from_string (set_allowed_modes_str, &error); 775 if (error) { 776 g_printerr ("error: couldn't parse list of allowed modes: '%s'\n", 777 error->message); 778 exit (EXIT_FAILURE); 779 } 780 781 *preferred = (set_preferred_mode_str ? 782 mm_common_get_modes_from_string (set_preferred_mode_str, &error) : 783 MM_MODEM_MODE_NONE); 784 if (error) { 785 g_printerr ("error: couldn't parse preferred mode: '%s'\n", 786 error->message); 787 exit (EXIT_FAILURE); 788 } 789} 790 791static void 792set_bands_process_reply (gboolean result, 793 const GError *error) 794{ 795 if (!result) { 796 g_printerr ("error: couldn't set bands: '%s'\n", 797 error ? error->message : "unknown error"); 798 exit (EXIT_FAILURE); 799 } 800 801 g_print ("successfully set bands in the modem\n"); 802} 803 804static void 805set_bands_ready (MMModem *modem, 806 GAsyncResult *result, 807 gpointer nothing) 808{ 809 gboolean operation_result; 810 GError *error = NULL; 811 812 operation_result = mm_modem_set_bands_finish (modem, result, &error); 813 set_bands_process_reply (operation_result, error); 814 815 mmcli_async_operation_done (); 816} 817 818static void 819parse_bands (MMModemBand **bands, 820 guint *n_bands) 821{ 822 GError *error = NULL; 823 824 mm_common_get_bands_from_string (set_bands_str, 825 bands, 826 n_bands, 827 &error); 828 if (error) { 829 g_printerr ("error: couldn't parse list of bands: '%s'\n", 830 error->message); 831 exit (EXIT_FAILURE); 832 } 833} 834 835 836static void 837state_changed (MMModem *modem, 838 MMModemState old_state, 839 MMModemState new_state, 840 MMModemStateChangeReason reason) 841{ 842 g_print ("\t%s: State changed, '%s' --> '%s' (Reason: %s)\n", 843 mm_modem_get_path (modem), 844 mm_modem_state_get_string (old_state), 845 mm_modem_state_get_string (new_state), 846 mmcli_get_state_reason_string (reason)); 847 fflush (stdout); 848} 849 850static void 851get_modem_ready (GObject *source, 852 GAsyncResult *result, 853 gpointer none) 854{ 855 ctx->object = mmcli_get_modem_finish (result, &ctx->manager); 856 ctx->modem = mm_object_get_modem (ctx->object); 857 ctx->modem_3gpp = mm_object_get_modem_3gpp (ctx->object); 858 ctx->modem_cdma = mm_object_get_modem_cdma (ctx->object); 859 860 /* Setup operation timeout */ 861 if (ctx->modem) 862 mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem)); 863 if (ctx->modem_3gpp) 864 mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem_3gpp)); 865 if (ctx->modem_cdma) 866 mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem_cdma)); 867 868 if (info_flag) 869 g_assert_not_reached (); 870 871 /* Request to monitor modems? */ 872 if (monitor_state_flag) { 873 MMModemState current; 874 875 g_signal_connect (ctx->modem, 876 "state-changed", 877 G_CALLBACK (state_changed), 878 NULL); 879 880 current = mm_modem_get_state (ctx->modem); 881 g_print ("\t%s: Initial state, '%s'\n", 882 mm_object_get_path (ctx->object), 883 mm_modem_state_get_string (current)); 884 885 /* If we get cancelled, operation done */ 886 g_cancellable_connect (ctx->cancellable, 887 G_CALLBACK (cancelled), 888 NULL, 889 NULL); 890 return; 891 } 892 893 /* Request to enable the modem? */ 894 if (enable_flag) { 895 g_debug ("Asynchronously enabling modem..."); 896 mm_modem_enable (ctx->modem, 897 ctx->cancellable, 898 (GAsyncReadyCallback)enable_ready, 899 NULL); 900 return; 901 } 902 903 /* Request to disable the modem? */ 904 if (disable_flag) { 905 g_debug ("Asynchronously disabling modem..."); 906 mm_modem_disable (ctx->modem, 907 ctx->cancellable, 908 (GAsyncReadyCallback)disable_ready, 909 NULL); 910 return; 911 } 912 913 /* Request to full power the modem? */ 914 if (set_power_state_on_flag) { 915 g_debug ("Asynchronously setting full power..."); 916 mm_modem_set_power_state (ctx->modem, 917 MM_MODEM_POWER_STATE_ON, 918 ctx->cancellable, 919 (GAsyncReadyCallback)set_power_state_ready, 920 NULL); 921 return; 922 } 923 924 /* Request to low power the modem? */ 925 if (set_power_state_low_flag) { 926 g_debug ("Asynchronously setting low power..."); 927 mm_modem_set_power_state (ctx->modem, 928 MM_MODEM_POWER_STATE_LOW, 929 ctx->cancellable, 930 (GAsyncReadyCallback)set_power_state_ready, 931 NULL); 932 return; 933 } 934 935 /* Request to reset the modem? */ 936 if (reset_flag) { 937 g_debug ("Asynchronously reseting modem..."); 938 mm_modem_reset (ctx->modem, 939 ctx->cancellable, 940 (GAsyncReadyCallback)reset_ready, 941 NULL); 942 return; 943 } 944 945 /* Request to reset the modem to factory state? */ 946 if (factory_reset_str) { 947 g_debug ("Asynchronously factory-reseting modem..."); 948 mm_modem_factory_reset (ctx->modem, 949 factory_reset_str, 950 ctx->cancellable, 951 (GAsyncReadyCallback)factory_reset_ready, 952 NULL); 953 return; 954 } 955 956 /* Request to send a command to the modem? */ 957 if (command_str) { 958 guint timeout; 959 960 timeout = command_get_timeout (ctx->modem); 961 962 g_debug ("Asynchronously sending a command to the modem (%u seconds timeout)...", 963 timeout); 964 965 mm_modem_command (ctx->modem, 966 command_str, 967 timeout, 968 ctx->cancellable, 969 (GAsyncReadyCallback)command_ready, 970 NULL); 971 return; 972 } 973 974 /* Request to list bearers? */ 975 if (list_bearers_flag) { 976 g_debug ("Asynchronously listing bearers in modem..."); 977 mm_modem_list_bearers (ctx->modem, 978 ctx->cancellable, 979 (GAsyncReadyCallback)list_bearers_ready, 980 NULL); 981 return; 982 } 983 984 /* Request to create a new bearer? */ 985 if (create_bearer_str) { 986 GError *error = NULL; 987 MMBearerProperties *properties; 988 989 properties = mm_bearer_properties_new_from_string (create_bearer_str, &error); 990 if (!properties) { 991 g_printerr ("Error parsing properties string: '%s'\n", error->message); 992 exit (EXIT_FAILURE); 993 } 994 995 g_debug ("Asynchronously creating new bearer in modem..."); 996 mm_modem_create_bearer (ctx->modem, 997 properties, 998 ctx->cancellable, 999 (GAsyncReadyCallback)create_bearer_ready, 1000 NULL); 1001 g_object_unref (properties); 1002 return; 1003 } 1004 1005 /* Request to delete a given bearer? */ 1006 if (delete_bearer_str) { 1007 mm_modem_delete_bearer (ctx->modem, 1008 delete_bearer_str, 1009 ctx->cancellable, 1010 (GAsyncReadyCallback)delete_bearer_ready, 1011 NULL); 1012 return; 1013 } 1014 1015 /* Request to set allowed modes in a given modem? */ 1016 if (set_allowed_modes_str) { 1017 MMModemMode allowed; 1018 MMModemMode preferred; 1019 1020 parse_modes (&allowed, &preferred); 1021 mm_modem_set_allowed_modes (ctx->modem, 1022 allowed, 1023 preferred, 1024 ctx->cancellable, 1025 (GAsyncReadyCallback)set_allowed_modes_ready, 1026 NULL); 1027 return; 1028 } 1029 1030 /* Request to set allowed bands in a given modem? */ 1031 if (set_bands_str) { 1032 MMModemBand *bands; 1033 guint n_bands; 1034 1035 parse_bands (&bands, &n_bands); 1036 mm_modem_set_bands (ctx->modem, 1037 bands, 1038 n_bands, 1039 ctx->cancellable, 1040 (GAsyncReadyCallback)set_bands_ready, 1041 NULL); 1042 g_free (bands); 1043 return; 1044 } 1045 1046 g_warn_if_reached (); 1047} 1048 1049void 1050mmcli_modem_run_asynchronous (GDBusConnection *connection, 1051 GCancellable *cancellable) 1052{ 1053 /* Initialize context */ 1054 ctx = g_new0 (Context, 1); 1055 if (cancellable) 1056 ctx->cancellable = g_object_ref (cancellable); 1057 1058 /* Get proper modem */ 1059 mmcli_get_modem (connection, 1060 mmcli_get_common_modem_string (), 1061 cancellable, 1062 (GAsyncReadyCallback)get_modem_ready, 1063 NULL); 1064} 1065 1066void 1067mmcli_modem_run_synchronous (GDBusConnection *connection) 1068{ 1069 GError *error = NULL; 1070 1071 if (monitor_state_flag) 1072 g_assert_not_reached (); 1073 1074 /* Initialize context */ 1075 ctx = g_new0 (Context, 1); 1076 ctx->object = mmcli_get_modem_sync (connection, 1077 mmcli_get_common_modem_string (), 1078 &ctx->manager); 1079 ctx->modem = mm_object_get_modem (ctx->object); 1080 ctx->modem_3gpp = mm_object_get_modem_3gpp (ctx->object); 1081 ctx->modem_cdma = mm_object_get_modem_cdma (ctx->object); 1082 1083 /* Setup operation timeout */ 1084 if (ctx->modem) 1085 mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem)); 1086 if (ctx->modem_3gpp) 1087 mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem_3gpp)); 1088 if (ctx->modem_cdma) 1089 mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem_cdma)); 1090 1091 /* Request to get info from modem? */ 1092 if (info_flag) { 1093 g_debug ("Printing modem info..."); 1094 print_modem_info (); 1095 return; 1096 } 1097 1098 /* Request to enable the modem? */ 1099 if (enable_flag) { 1100 gboolean result; 1101 1102 g_debug ("Synchronously enabling modem..."); 1103 result = mm_modem_enable_sync (ctx->modem, NULL, &error); 1104 enable_process_reply (result, error); 1105 return; 1106 } 1107 1108 /* Request to disable the modem? */ 1109 if (disable_flag) { 1110 gboolean result; 1111 1112 g_debug ("Synchronously disabling modem..."); 1113 result = mm_modem_disable_sync (ctx->modem, NULL, &error); 1114 disable_process_reply (result, error); 1115 return; 1116 } 1117 1118 /* Request to set full power state? */ 1119 if (set_power_state_on_flag) { 1120 gboolean result; 1121 1122 g_debug ("Synchronously setting full power..."); 1123 result = mm_modem_set_power_state_sync (ctx->modem, MM_MODEM_POWER_STATE_ON, NULL, &error); 1124 set_power_state_process_reply (result, error); 1125 return; 1126 } 1127 1128 /* Request to set low power state? */ 1129 if (set_power_state_low_flag) { 1130 gboolean result; 1131 1132 g_debug ("Synchronously setting low power..."); 1133 result = mm_modem_set_power_state_sync (ctx->modem, MM_MODEM_POWER_STATE_LOW, NULL, &error); 1134 set_power_state_process_reply (result, error); 1135 return; 1136 } 1137 1138 /* Request to reset the modem? */ 1139 if (reset_flag) { 1140 gboolean result; 1141 1142 g_debug ("Synchronously reseting modem..."); 1143 result = mm_modem_reset_sync (ctx->modem, NULL, &error); 1144 reset_process_reply (result, error); 1145 return; 1146 } 1147 1148 /* Request to reset the modem to factory state? */ 1149 if (factory_reset_str) { 1150 gboolean result; 1151 1152 g_debug ("Synchronously factory-reseting modem..."); 1153 result = mm_modem_factory_reset_sync (ctx->modem, 1154 factory_reset_str, 1155 NULL, 1156 &error); 1157 factory_reset_process_reply (result, error); 1158 return; 1159 } 1160 1161 1162 /* Request to send a command to the modem? */ 1163 if (command_str) { 1164 gchar *result; 1165 guint timeout; 1166 1167 timeout = command_get_timeout (ctx->modem); 1168 1169 g_debug ("Synchronously sending command to modem (%u seconds timeout)...", 1170 timeout); 1171 1172 result = mm_modem_command_sync (ctx->modem, 1173 command_str, 1174 timeout, 1175 NULL, 1176 &error); 1177 command_process_reply (result, error); 1178 return; 1179 } 1180 1181 /* Request to list the bearers? */ 1182 if (list_bearers_flag) { 1183 GList *result; 1184 1185 g_debug ("Synchronously listing bearers..."); 1186 result = mm_modem_list_bearers_sync (ctx->modem, NULL, &error); 1187 list_bearers_process_reply (result, error); 1188 return; 1189 } 1190 1191 /* Request to create a new bearer? */ 1192 if (create_bearer_str) { 1193 MMBearer *bearer; 1194 GError *error = NULL; 1195 MMBearerProperties *properties; 1196 1197 properties = mm_bearer_properties_new_from_string (create_bearer_str, &error); 1198 if (!properties) { 1199 g_printerr ("Error parsing properties string: '%s'\n", error->message); 1200 exit (EXIT_FAILURE); 1201 } 1202 1203 g_debug ("Synchronously creating new bearer in modem..."); 1204 bearer = mm_modem_create_bearer_sync (ctx->modem, 1205 properties, 1206 NULL, 1207 &error); 1208 g_object_unref (properties); 1209 1210 create_bearer_process_reply (bearer, error); 1211 return; 1212 } 1213 1214 /* Request to delete a given bearer? */ 1215 if (delete_bearer_str) { 1216 gboolean result; 1217 1218 result = mm_modem_delete_bearer_sync (ctx->modem, 1219 delete_bearer_str, 1220 NULL, 1221 &error); 1222 1223 delete_bearer_process_reply (result, error); 1224 return; 1225 } 1226 1227 /* Request to set allowed modes in a given modem? */ 1228 if (set_allowed_modes_str) { 1229 MMModemMode allowed; 1230 MMModemMode preferred; 1231 gboolean result; 1232 1233 parse_modes (&allowed, &preferred); 1234 result = mm_modem_set_allowed_modes_sync (ctx->modem, 1235 allowed, 1236 preferred, 1237 NULL, 1238 &error); 1239 1240 set_allowed_modes_process_reply (result, error); 1241 return; 1242 } 1243 1244 /* Request to set allowed bands in a given modem? */ 1245 if (set_bands_str) { 1246 gboolean result; 1247 MMModemBand *bands; 1248 guint n_bands; 1249 1250 parse_bands (&bands, &n_bands); 1251 result = mm_modem_set_bands_sync (ctx->modem, 1252 bands, 1253 n_bands, 1254 NULL, 1255 &error); 1256 g_free (bands); 1257 set_bands_process_reply (result, error); 1258 return; 1259 } 1260 1261 g_warn_if_reached (); 1262}