PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/cli/mmcli-modem.c

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