| 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 gchar *link_list_str; |
| 39 |
static gchar *link_add_str; |
| 40 |
static gchar *link_delete_str; |
| 41 |
static gchar *link_delete_all_str; |
| 42 |
|
| 43 |
static GOptionEntry entries[] = { |
| 44 |
{ "link-list", 0, 0, G_OPTION_ARG_STRING, &link_list_str, |
| 45 |
"List links created from a given interface", |
| 46 |
"[IFACE]" |
| 47 |
}, |
| 48 |
{ "link-add", 0, 0, G_OPTION_ARG_STRING, &link_add_str, |
| 49 |
"Create new network interface link", |
| 50 |
"[iface=IFACE,prefix=PREFIX[,mux-id=N[,initial-mux-id=M]][,flags=FLAGS]]" |
| 51 |
}, |
| 52 |
{ "link-delete", 0, 0, G_OPTION_ARG_STRING, &link_delete_str, |
| 53 |
"Delete a given network interface link", |
| 54 |
"[link-iface=IFACE][,[mux-id=N]]" |
| 55 |
}, |
| 56 |
{ "link-delete-all", 0, 0, G_OPTION_ARG_STRING, &link_delete_all_str, |
| 57 |
"Delete all network interface links from the given interface", |
| 58 |
"[IFACE]" |
| 59 |
}, |
| 60 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 61 |
}; |
| 62 |
|
| 63 |
GOptionGroup * |
| 64 |
qmicli_link_management_get_option_group (void) |
| 65 |
{ |
| 66 |
GOptionGroup *group; |
| 67 |
|
| 68 |
group = g_option_group_new ("link-management", |
| 69 |
"Link management options:", |
| 70 |
"Show link management specific options", |
| 71 |
NULL, NULL); |
| 72 |
g_option_group_add_entries (group, entries); |
| 73 |
|
| 74 |
return group; |
| 75 |
} |
| 76 |
|
| 77 |
gboolean |
| 78 |
qmicli_link_management_options_enabled (void) |
| 79 |
{ |
| 80 |
static guint n_actions = 0; |
| 81 |
static gboolean checked = FALSE; |
| 82 |
|
| 83 |
if (checked) |
| 84 |
return !!n_actions; |
| 85 |
|
| 86 |
n_actions = (!!link_list_str + |
| 87 |
!!link_add_str + |
| 88 |
!!link_delete_str + |
| 89 |
!!link_delete_all_str); |
| 90 |
|
| 91 |
if (n_actions > 1) { |
| 92 |
g_printerr ("error: too many link management actions requested\n"); |
| 93 |
exit (EXIT_FAILURE); |
| 94 |
} |
| 95 |
|
| 96 |
checked = TRUE; |
| 97 |
return !!n_actions; |
| 98 |
} |
| 99 |
|
| 100 |
/******************************************************************************/ |
| 101 |
|
| 102 |
static void |
| 103 |
link_delete_all_ready (QmiDevice *dev, |
| 104 |
GAsyncResult *res) |
| 105 |
{ |
| 106 |
g_autoptr(GError) error = NULL; |
| 107 |
|
| 108 |
if (!qmi_device_delete_all_links_finish (dev, res, &error)) |
| 109 |
g_printerr ("error: couldn't delete all links: %s\n", error->message); |
| 110 |
else |
| 111 |
g_print ("[%s] all links successfully deleted\n", |
| 112 |
qmi_device_get_path_display (dev)); |
| 113 |
|
| 114 |
qmicli_async_operation_done (!error, FALSE); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
device_link_delete_all (QmiDevice *dev, |
| 119 |
GCancellable *cancellable, |
| 120 |
const gchar *iface) |
| 121 |
{ |
| 122 |
qmi_device_delete_all_links (dev, |
| 123 |
iface, |
| 124 |
cancellable, |
| 125 |
(GAsyncReadyCallback)link_delete_all_ready, |
| 126 |
NULL); |
| 127 |
} |
| 128 |
|
| 129 |
typedef struct { |
| 130 |
guint mux_id; |
| 131 |
gchar *link_iface; |
| 132 |
} DeleteLinkProperties; |
| 133 |
|
| 134 |
static void |
| 135 |
link_delete_ready (QmiDevice *dev, |
| 136 |
GAsyncResult *res) |
| 137 |
{ |
| 138 |
g_autoptr(GError) error = NULL; |
| 139 |
|
| 140 |
if (!qmi_device_delete_link_finish (dev, res, &error)) |
| 141 |
g_printerr ("error: couldn't delete link: %s\n", |
| 142 |
error->message); |
| 143 |
else |
| 144 |
g_print ("[%s] link successfully deleted\n", |
| 145 |
qmi_device_get_path_display (dev)); |
| 146 |
|
| 147 |
qmicli_async_operation_done (!error, FALSE); |
| 148 |
} |
| 149 |
|
| 150 |
static gboolean |
| 151 |
del_link_properties_handle (const gchar *key, |
| 152 |
const gchar *value, |
| 153 |
GError **error, |
| 154 |
DeleteLinkProperties *props) |
| 155 |
{ |
| 156 |
if (g_ascii_strcasecmp (key, "mux-id") == 0 && props->mux_id == QMI_DEVICE_MUX_ID_UNBOUND) { |
| 157 |
if (!qmicli_read_uint_from_string (value, &props->mux_id)) { |
| 158 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 159 |
"invalid mux-id given: '%s'", value); |
| 160 |
return FALSE; |
| 161 |
} |
| 162 |
return TRUE; |
| 163 |
} |
| 164 |
|
| 165 |
if (g_ascii_strcasecmp (key, "link-iface") == 0 && !props->link_iface) { |
| 166 |
props->link_iface = g_strdup (value); |
| 167 |
return TRUE; |
| 168 |
} |
| 169 |
|
| 170 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 171 |
"unrecognized or duplicate option '%s'", key); |
| 172 |
return FALSE; |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
device_link_delete (QmiDevice *dev, |
| 177 |
GCancellable *cancellable, |
| 178 |
const gchar *del_settings) |
| 179 |
{ |
| 180 |
g_autoptr(GError) error = NULL; |
| 181 |
DeleteLinkProperties props = { |
| 182 |
.mux_id = QMI_DEVICE_MUX_ID_UNBOUND, |
| 183 |
.link_iface = NULL, |
| 184 |
}; |
| 185 |
|
| 186 |
if (!qmicli_parse_key_value_string (del_settings, |
| 187 |
&error, |
| 188 |
(QmiParseKeyValueForeachFn)del_link_properties_handle, |
| 189 |
&props)) { |
| 190 |
g_printerr ("error: couldn't parse input add link settings: %s\n", |
| 191 |
error->message); |
| 192 |
qmicli_async_operation_done (FALSE, FALSE); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
if (!props.link_iface) { |
| 197 |
g_printerr ("error: missing mandatory 'link-iface' setting\n"); |
| 198 |
qmicli_async_operation_done (FALSE, FALSE); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
if ((props.mux_id != QMI_DEVICE_MUX_ID_UNBOUND) && |
| 203 |
(props.mux_id < QMI_DEVICE_MUX_ID_MIN || props.mux_id > QMI_DEVICE_MUX_ID_MAX)) { |
| 204 |
g_printerr ("error: mux id %u out of range [%u,%u]\n", |
| 205 |
props.mux_id, QMI_DEVICE_MUX_ID_MIN, QMI_DEVICE_MUX_ID_MAX); |
| 206 |
qmicli_async_operation_done (FALSE, FALSE); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
qmi_device_delete_link (dev, |
| 211 |
props.link_iface, |
| 212 |
props.mux_id, |
| 213 |
cancellable, |
| 214 |
(GAsyncReadyCallback)link_delete_ready, |
| 215 |
NULL); |
| 216 |
|
| 217 |
g_free (props.link_iface); |
| 218 |
} |
| 219 |
|
| 220 |
typedef struct { |
| 221 |
guint mux_id; |
| 222 |
guint initial_mux_id; |
| 223 |
gchar *iface; |
| 224 |
gchar *prefix; |
| 225 |
QmiDeviceAddLinkFlags flags; |
| 226 |
} AddLinkProperties; |
| 227 |
|
| 228 |
static void |
| 229 |
link_add_ready (QmiDevice *dev, |
| 230 |
GAsyncResult *res) |
| 231 |
{ |
| 232 |
g_autoptr(GError) error = NULL; |
| 233 |
g_autofree gchar *link_iface = NULL; |
| 234 |
guint mux_id; |
| 235 |
|
| 236 |
link_iface = qmi_device_add_link_with_flags_finish (dev, res, &mux_id, &error); |
| 237 |
if (!link_iface) |
| 238 |
g_printerr ("error: couldn't add link: %s\n", |
| 239 |
error->message); |
| 240 |
else |
| 241 |
g_print ("[%s] link successfully added:\n" |
| 242 |
" iface name: %s\n" |
| 243 |
" mux-id: %u\n", |
| 244 |
qmi_device_get_path_display (dev), |
| 245 |
link_iface, |
| 246 |
mux_id); |
| 247 |
|
| 248 |
qmicli_async_operation_done (!error, FALSE); |
| 249 |
} |
| 250 |
|
| 251 |
static void |
| 252 |
link_add_with_initial_mux_id_ready (QmiDevice *dev, |
| 253 |
GAsyncResult *res) |
| 254 |
{ |
| 255 |
g_autoptr(GError) error = NULL; |
| 256 |
g_autofree gchar *link_iface = NULL; |
| 257 |
guint mux_id; |
| 258 |
|
| 259 |
link_iface = qmi_device_add_link_with_flags_and_initial_mux_id_finish (dev, res, &mux_id, &error); |
| 260 |
if (!link_iface) |
| 261 |
g_printerr ("error: couldn't add link: %s\n", |
| 262 |
error->message); |
| 263 |
else |
| 264 |
g_print ("[%s] link successfully added:\n" |
| 265 |
" iface name: %s\n" |
| 266 |
" mux-id: %u\n", |
| 267 |
qmi_device_get_path_display (dev), |
| 268 |
link_iface, |
| 269 |
mux_id); |
| 270 |
|
| 271 |
qmicli_async_operation_done (!error, FALSE); |
| 272 |
} |
| 273 |
|
| 274 |
static gboolean |
| 275 |
add_link_properties_handle (const gchar *key, |
| 276 |
const gchar *value, |
| 277 |
GError **error, |
| 278 |
AddLinkProperties *props) |
| 279 |
{ |
| 280 |
if (g_ascii_strcasecmp (key, "mux-id") == 0 && props->mux_id == QMI_DEVICE_MUX_ID_AUTOMATIC) { |
| 281 |
if (!qmicli_read_uint_from_string (value, &props->mux_id)) { |
| 282 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 283 |
"invalid mux-id given: '%s'", value); |
| 284 |
return FALSE; |
| 285 |
} |
| 286 |
return TRUE; |
| 287 |
} |
| 288 |
|
| 289 |
if (g_ascii_strcasecmp (key, "initial-mux-id") == 0 && props->mux_id == QMI_DEVICE_MUX_ID_AUTOMATIC) { |
| 290 |
if (!qmicli_read_uint_from_string (value, &props->initial_mux_id)) { |
| 291 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 292 |
"invalid initial-mux-id given: '%s'", value); |
| 293 |
return FALSE; |
| 294 |
} |
| 295 |
return TRUE; |
| 296 |
} |
| 297 |
|
| 298 |
if (g_ascii_strcasecmp (key, "iface") == 0 && !props->iface) { |
| 299 |
props->iface = g_strdup (value); |
| 300 |
return TRUE; |
| 301 |
} |
| 302 |
|
| 303 |
if (g_ascii_strcasecmp (key, "prefix") == 0 && !props->prefix) { |
| 304 |
props->prefix = g_strdup (value); |
| 305 |
return TRUE; |
| 306 |
} |
| 307 |
|
| 308 |
if (g_ascii_strcasecmp (key, "flags") == 0 && !props->flags) { |
| 309 |
if (!qmicli_read_device_add_link_flags_from_string (value, &props->flags)) { |
| 310 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 311 |
"invalid flags given: '%s'", value); |
| 312 |
return FALSE; |
| 313 |
} |
| 314 |
return TRUE; |
| 315 |
} |
| 316 |
|
| 317 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 318 |
"unrecognized or duplicate option '%s'", key); |
| 319 |
return FALSE; |
| 320 |
} |
| 321 |
|
| 322 |
static void |
| 323 |
device_link_add (QmiDevice *dev, |
| 324 |
GCancellable *cancellable, |
| 325 |
const gchar *add_settings) |
| 326 |
{ |
| 327 |
g_autoptr(GError) error = NULL; |
| 328 |
AddLinkProperties props = { |
| 329 |
.mux_id = QMI_DEVICE_MUX_ID_AUTOMATIC, |
| 330 |
.initial_mux_id = QMI_DEVICE_MUX_ID_MIN, |
| 331 |
.iface = NULL, |
| 332 |
.prefix = NULL, |
| 333 |
.flags = QMI_DEVICE_ADD_LINK_FLAGS_NONE, |
| 334 |
}; |
| 335 |
|
| 336 |
if (!qmicli_parse_key_value_string (add_settings, |
| 337 |
&error, |
| 338 |
(QmiParseKeyValueForeachFn)add_link_properties_handle, |
| 339 |
&props)) { |
| 340 |
g_printerr ("error: couldn't parse input add link settings: %s\n", |
| 341 |
error->message); |
| 342 |
qmicli_async_operation_done (FALSE, FALSE); |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
if (!props.iface) { |
| 347 |
g_printerr ("error: missing mandatory 'iface' setting\n"); |
| 348 |
qmicli_async_operation_done (FALSE, FALSE); |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
if (!props.prefix) |
| 353 |
props.prefix = g_strdup_printf ("%s.", props.iface); |
| 354 |
|
| 355 |
if ((props.mux_id != QMI_DEVICE_MUX_ID_AUTOMATIC) && |
| 356 |
(props.mux_id < QMI_DEVICE_MUX_ID_MIN || props.mux_id > QMI_DEVICE_MUX_ID_MAX)) { |
| 357 |
g_printerr ("error: mux id %u out of range [%u,%u]\n", |
| 358 |
props.mux_id, QMI_DEVICE_MUX_ID_MIN, QMI_DEVICE_MUX_ID_MAX); |
| 359 |
qmicli_async_operation_done (FALSE, FALSE); |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
if ((props.mux_id == QMI_DEVICE_MUX_ID_AUTOMATIC) && |
| 364 |
(props.initial_mux_id < QMI_DEVICE_MUX_ID_MIN || props.initial_mux_id > QMI_DEVICE_MUX_ID_MAX)) { |
| 365 |
g_printerr ("error: initial mux id %u out of range [%u,%u]\n", |
| 366 |
props.initial_mux_id, QMI_DEVICE_MUX_ID_MIN, QMI_DEVICE_MUX_ID_MAX); |
| 367 |
qmicli_async_operation_done (FALSE, FALSE); |
| 368 |
return; |
| 369 |
} |
| 370 |
|
| 371 |
if (props.mux_id != QMI_DEVICE_MUX_ID_AUTOMATIC) |
| 372 |
qmi_device_add_link_with_flags (dev, |
| 373 |
props.mux_id, |
| 374 |
props.iface, |
| 375 |
props.prefix, |
| 376 |
props.flags, |
| 377 |
cancellable, |
| 378 |
(GAsyncReadyCallback)link_add_ready, |
| 379 |
NULL); |
| 380 |
else |
| 381 |
qmi_device_add_link_with_flags_and_initial_mux_id (dev, |
| 382 |
props.initial_mux_id, |
| 383 |
props.iface, |
| 384 |
props.prefix, |
| 385 |
props.flags, |
| 386 |
cancellable, |
| 387 |
(GAsyncReadyCallback)link_add_with_initial_mux_id_ready, |
| 388 |
NULL); |
| 389 |
|
| 390 |
g_free (props.iface); |
| 391 |
g_free (props.prefix); |
| 392 |
} |
| 393 |
|
| 394 |
static void |
| 395 |
device_link_list (QmiDevice *dev, |
| 396 |
GCancellable *cancellable, |
| 397 |
const gchar *iface) |
| 398 |
{ |
| 399 |
g_autoptr(GError) error = NULL; |
| 400 |
g_autoptr(GPtrArray) links = NULL; |
| 401 |
|
| 402 |
if (!qmi_device_list_links (dev, iface, &links, &error)) |
| 403 |
g_printerr ("error: couldn't list links: %s\n", error->message); |
| 404 |
else { |
| 405 |
guint i; |
| 406 |
guint n_links; |
| 407 |
|
| 408 |
n_links = (links ? links->len : 0); |
| 409 |
|
| 410 |
g_print ("[%s] found %u links%s\n", |
| 411 |
qmi_device_get_path_display (dev), |
| 412 |
n_links, |
| 413 |
n_links > 0 ? ":" : ""); |
| 414 |
for (i = 0; i < n_links; i++) |
| 415 |
g_print (" [%u] %s\n", i, (const gchar *) g_ptr_array_index (links, i)); |
| 416 |
} |
| 417 |
|
| 418 |
qmicli_async_operation_done (!error, FALSE); |
| 419 |
} |
| 420 |
|
| 421 |
/******************************************************************************/ |
| 422 |
/* Common */ |
| 423 |
|
| 424 |
void |
| 425 |
qmicli_link_management_run (QmiDevice *dev, |
| 426 |
GCancellable *cancellable) |
| 427 |
{ |
| 428 |
if (link_list_str) |
| 429 |
device_link_list (dev, cancellable, link_list_str); |
| 430 |
else if (link_add_str) |
| 431 |
device_link_add (dev, cancellable, link_add_str); |
| 432 |
else if (link_delete_str) |
| 433 |
device_link_delete (dev, cancellable, link_delete_str); |
| 434 |
else if (link_delete_all_str) |
| 435 |
device_link_delete_all (dev, cancellable, link_delete_all_str); |
| 436 |
else |
| 437 |
g_warn_if_reached (); |
| 438 |
} |