| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* qmicli -- Command line interface to control QMI devices |
| 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 2 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) 2021 Aleksander Morgado <[email protected]> |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <locale.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include <glib.h> |
| 29 |
#include <glib/gprintf.h> |
| 30 |
#include <gio/gio.h> |
| 31 |
|
| 32 |
#include <libqmi-glib.h> |
| 33 |
|
| 34 |
#include "qmicli.h" |
| 35 |
#include "qmicli-helpers.h" |
| 36 |
|
| 37 |
/* Options */ |
| 38 |
static gboolean get_wwan_iface_flag; |
| 39 |
static gboolean get_expected_data_format_flag; |
| 40 |
static gchar *set_expected_data_format_str; |
| 41 |
|
| 42 |
static GOptionEntry entries[] = { |
| 43 |
{ "get-wwan-iface", 'w', 0, G_OPTION_ARG_NONE, &get_wwan_iface_flag, |
| 44 |
"Get the associated WWAN iface name", |
| 45 |
NULL |
| 46 |
}, |
| 47 |
{ "get-expected-data-format", 'e', 0, G_OPTION_ARG_NONE, &get_expected_data_format_flag, |
| 48 |
"Get the expected data format in the WWAN iface", |
| 49 |
NULL |
| 50 |
}, |
| 51 |
{ "set-expected-data-format", 'E', 0, G_OPTION_ARG_STRING, &set_expected_data_format_str, |
| 52 |
"Set the expected data format in the WWAN iface", |
| 53 |
"[802-3|raw-ip|qmap-pass-through]" |
| 54 |
}, |
| 55 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 56 |
}; |
| 57 |
|
| 58 |
GOptionGroup * |
| 59 |
qmicli_qmiwwan_get_option_group (void) |
| 60 |
{ |
| 61 |
GOptionGroup *group; |
| 62 |
|
| 63 |
group = g_option_group_new ("qmiwwan", |
| 64 |
"qmi_wwan specific options:", |
| 65 |
"Show qmi_wwan driver specific options", NULL, NULL); |
| 66 |
g_option_group_add_entries (group, entries); |
| 67 |
|
| 68 |
return group; |
| 69 |
} |
| 70 |
|
| 71 |
gboolean |
| 72 |
qmicli_qmiwwan_options_enabled (void) |
| 73 |
{ |
| 74 |
static guint n_actions = 0; |
| 75 |
static gboolean checked = FALSE; |
| 76 |
|
| 77 |
if (checked) |
| 78 |
return !!n_actions; |
| 79 |
|
| 80 |
n_actions = (get_wwan_iface_flag + |
| 81 |
get_expected_data_format_flag + |
| 82 |
!!set_expected_data_format_str); |
| 83 |
|
| 84 |
if (n_actions > 1) { |
| 85 |
g_printerr ("error: too many qmi_wwan specific actions requested\n"); |
| 86 |
exit (EXIT_FAILURE); |
| 87 |
} |
| 88 |
|
| 89 |
checked = TRUE; |
| 90 |
return !!n_actions; |
| 91 |
} |
| 92 |
|
| 93 |
/******************************************************************************/ |
| 94 |
|
| 95 |
static gboolean |
| 96 |
device_set_expected_data_format_cb (QmiDevice *dev) |
| 97 |
{ |
| 98 |
QmiDeviceExpectedDataFormat expected; |
| 99 |
GError *error = NULL; |
| 100 |
|
| 101 |
if (!qmicli_read_device_expected_data_format_from_string (set_expected_data_format_str, &expected) || |
| 102 |
expected == QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN) |
| 103 |
g_printerr ("error: invalid requested data format: %s", set_expected_data_format_str); |
| 104 |
else if (!qmi_device_set_expected_data_format (dev, expected, &error)) { |
| 105 |
g_printerr ("error: cannot set expected data format: %s\n", error->message); |
| 106 |
g_error_free (error); |
| 107 |
} else |
| 108 |
g_print ("[%s] expected data format set to: %s\n", |
| 109 |
qmi_device_get_path_display (dev), |
| 110 |
qmi_device_expected_data_format_get_string (expected)); |
| 111 |
|
| 112 |
/* We're done now */ |
| 113 |
qmicli_async_operation_done (!error, FALSE); |
| 114 |
|
| 115 |
g_object_unref (dev); |
| 116 |
return FALSE; |
| 117 |
} |
| 118 |
|
| 119 |
static void |
| 120 |
device_set_expected_data_format (QmiDevice *dev) |
| 121 |
{ |
| 122 |
g_debug ("Setting expected WWAN data format this control port..."); |
| 123 |
g_idle_add ((GSourceFunc) device_set_expected_data_format_cb, g_object_ref (dev)); |
| 124 |
} |
| 125 |
|
| 126 |
static gboolean |
| 127 |
device_get_expected_data_format_cb (QmiDevice *dev) |
| 128 |
{ |
| 129 |
QmiDeviceExpectedDataFormat expected; |
| 130 |
GError *error = NULL; |
| 131 |
|
| 132 |
expected = qmi_device_get_expected_data_format (dev, &error); |
| 133 |
if (expected == QMI_DEVICE_EXPECTED_DATA_FORMAT_UNKNOWN) { |
| 134 |
g_printerr ("error: cannot get expected data format: %s\n", error->message); |
| 135 |
g_error_free (error); |
| 136 |
} else |
| 137 |
g_print ("%s\n", qmi_device_expected_data_format_get_string (expected)); |
| 138 |
|
| 139 |
/* We're done now */ |
| 140 |
qmicli_async_operation_done (!error, FALSE); |
| 141 |
|
| 142 |
g_object_unref (dev); |
| 143 |
return FALSE; |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
device_get_expected_data_format (QmiDevice *dev) |
| 148 |
{ |
| 149 |
g_debug ("Getting expected WWAN data format this control port..."); |
| 150 |
g_idle_add ((GSourceFunc) device_get_expected_data_format_cb, g_object_ref (dev)); |
| 151 |
} |
| 152 |
|
| 153 |
static gboolean |
| 154 |
device_get_wwan_iface_cb (QmiDevice *dev) |
| 155 |
{ |
| 156 |
const gchar *wwan_iface; |
| 157 |
|
| 158 |
wwan_iface = qmi_device_get_wwan_iface (dev); |
| 159 |
if (!wwan_iface) |
| 160 |
g_printerr ("error: cannot get WWAN interface name\n"); |
| 161 |
else |
| 162 |
g_print ("%s\n", wwan_iface); |
| 163 |
|
| 164 |
/* We're done now */ |
| 165 |
qmicli_async_operation_done (!!wwan_iface, FALSE); |
| 166 |
|
| 167 |
g_object_unref (dev); |
| 168 |
return FALSE; |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
device_get_wwan_iface (QmiDevice *dev) |
| 173 |
{ |
| 174 |
g_debug ("Getting WWAN iface for this control port..."); |
| 175 |
g_idle_add ((GSourceFunc) device_get_wwan_iface_cb, g_object_ref (dev)); |
| 176 |
} |
| 177 |
|
| 178 |
/******************************************************************************/ |
| 179 |
/* Common */ |
| 180 |
|
| 181 |
void |
| 182 |
qmicli_qmiwwan_run (QmiDevice *device, |
| 183 |
GCancellable *cancellable) |
| 184 |
{ |
| 185 |
if (get_wwan_iface_flag) |
| 186 |
device_get_wwan_iface (device); |
| 187 |
else if (get_expected_data_format_flag) |
| 188 |
device_get_expected_data_format (device); |
| 189 |
else if (set_expected_data_format_str) |
| 190 |
device_set_expected_data_format (device); |
| 191 |
else |
| 192 |
g_warn_if_reached (); |
| 193 |
} |