| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 3 |
/* |
| 4 |
* mbimcli -- Command line interface to control MBIM devices |
| 5 |
* |
| 6 |
* Copyright (C) 2021 Aleksander Morgado <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "config.h" |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <locale.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <glib.h> |
| 17 |
#include <glib/gprintf.h> |
| 18 |
#include <gio/gio.h> |
| 19 |
|
| 20 |
#include <libmbim-glib.h> |
| 21 |
|
| 22 |
#include "mbimcli.h" |
| 23 |
#include "mbimcli-helpers.h" |
| 24 |
|
| 25 |
/* Options */ |
| 26 |
static gchar *link_list_str; |
| 27 |
static gchar *link_add_str; |
| 28 |
static gchar *link_delete_str; |
| 29 |
static gchar *link_delete_all_str; |
| 30 |
|
| 31 |
static GOptionEntry entries[] = { |
| 32 |
{ "link-list", 0, 0, G_OPTION_ARG_STRING, &link_list_str, |
| 33 |
"List links created from a given interface", |
| 34 |
"[IFACE]" |
| 35 |
}, |
| 36 |
{ "link-add", 0, 0, G_OPTION_ARG_STRING, &link_add_str, |
| 37 |
"Create new network interface link", |
| 38 |
"[iface=IFACE,prefix=PREFIX[,session-id=N]]" |
| 39 |
}, |
| 40 |
{ "link-delete", 0, 0, G_OPTION_ARG_STRING, &link_delete_str, |
| 41 |
"Delete a given network interface link", |
| 42 |
"IFACE" |
| 43 |
}, |
| 44 |
{ "link-delete-all", 0, 0, G_OPTION_ARG_STRING, &link_delete_all_str, |
| 45 |
"Delete all network interface links from the given interface", |
| 46 |
"[IFACE]" |
| 47 |
}, |
| 48 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 49 |
}; |
| 50 |
|
| 51 |
GOptionGroup * |
| 52 |
mbimcli_link_management_get_option_group (void) |
| 53 |
{ |
| 54 |
GOptionGroup *group; |
| 55 |
|
| 56 |
group = g_option_group_new ("link-management", |
| 57 |
"Link management options:", |
| 58 |
"Show link management specific options", |
| 59 |
NULL, NULL); |
| 60 |
g_option_group_add_entries (group, entries); |
| 61 |
|
| 62 |
return group; |
| 63 |
} |
| 64 |
|
| 65 |
gboolean |
| 66 |
mbimcli_link_management_options_enabled (void) |
| 67 |
{ |
| 68 |
static guint n_actions = 0; |
| 69 |
static gboolean checked = FALSE; |
| 70 |
|
| 71 |
if (checked) |
| 72 |
return !!n_actions; |
| 73 |
|
| 74 |
n_actions = (!!link_list_str + |
| 75 |
!!link_add_str + |
| 76 |
!!link_delete_str + |
| 77 |
!!link_delete_all_str); |
| 78 |
|
| 79 |
if (n_actions > 1) { |
| 80 |
g_printerr ("error: too many link management actions requested\n"); |
| 81 |
exit (EXIT_FAILURE); |
| 82 |
} |
| 83 |
|
| 84 |
checked = TRUE; |
| 85 |
return !!n_actions; |
| 86 |
} |
| 87 |
|
| 88 |
/******************************************************************************/ |
| 89 |
|
| 90 |
static void |
| 91 |
link_delete_all_ready (MbimDevice *dev, |
| 92 |
GAsyncResult *res) |
| 93 |
{ |
| 94 |
g_autoptr(GError) error = NULL; |
| 95 |
|
| 96 |
if (!mbim_device_delete_all_links_finish (dev, res, &error)) |
| 97 |
g_printerr ("error: couldn't delete all links: %s\n", error->message); |
| 98 |
else |
| 99 |
g_print ("[%s] all links successfully deleted\n", |
| 100 |
mbim_device_get_path_display (dev)); |
| 101 |
|
| 102 |
mbimcli_async_operation_done (!error); |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
device_link_delete_all (MbimDevice *dev, |
| 107 |
GCancellable *cancellable, |
| 108 |
const gchar *iface) |
| 109 |
{ |
| 110 |
mbim_device_delete_all_links (dev, |
| 111 |
iface, |
| 112 |
cancellable, |
| 113 |
(GAsyncReadyCallback)link_delete_all_ready, |
| 114 |
NULL); |
| 115 |
} |
| 116 |
|
| 117 |
/******************************************************************************/ |
| 118 |
|
| 119 |
static void |
| 120 |
link_delete_ready (MbimDevice *dev, |
| 121 |
GAsyncResult *res) |
| 122 |
{ |
| 123 |
g_autoptr(GError) error = NULL; |
| 124 |
|
| 125 |
if (!mbim_device_delete_link_finish (dev, res, &error)) |
| 126 |
g_printerr ("error: couldn't delete link: %s\n", |
| 127 |
error->message); |
| 128 |
else |
| 129 |
g_print ("[%s] link successfully deleted\n", |
| 130 |
mbim_device_get_path_display (dev)); |
| 131 |
|
| 132 |
mbimcli_async_operation_done (!error); |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
device_link_delete (MbimDevice *dev, |
| 137 |
GCancellable *cancellable, |
| 138 |
const gchar *link_iface) |
| 139 |
{ |
| 140 |
mbim_device_delete_link (dev, |
| 141 |
link_iface, |
| 142 |
cancellable, |
| 143 |
(GAsyncReadyCallback)link_delete_ready, |
| 144 |
NULL); |
| 145 |
} |
| 146 |
|
| 147 |
typedef struct { |
| 148 |
guint session_id; |
| 149 |
gchar *iface; |
| 150 |
gchar *prefix; |
| 151 |
} AddLinkProperties; |
| 152 |
|
| 153 |
static void |
| 154 |
link_add_ready (MbimDevice *dev, |
| 155 |
GAsyncResult *res) |
| 156 |
{ |
| 157 |
g_autoptr(GError) error = NULL; |
| 158 |
g_autofree gchar *link_iface = NULL; |
| 159 |
guint session_id; |
| 160 |
|
| 161 |
link_iface = mbim_device_add_link_finish (dev, res, &session_id, &error); |
| 162 |
if (!link_iface) |
| 163 |
g_printerr ("error: couldn't add link: %s\n", |
| 164 |
error->message); |
| 165 |
else |
| 166 |
g_print ("[%s] link successfully added:\n" |
| 167 |
" iface name: %s\n" |
| 168 |
" session id: %u\n", |
| 169 |
mbim_device_get_path_display (dev), |
| 170 |
link_iface, |
| 171 |
session_id); |
| 172 |
|
| 173 |
mbimcli_async_operation_done (!error); |
| 174 |
} |
| 175 |
|
| 176 |
static gboolean |
| 177 |
add_link_properties_handle (const gchar *key, |
| 178 |
const gchar *value, |
| 179 |
GError **error, |
| 180 |
AddLinkProperties *props) |
| 181 |
{ |
| 182 |
if (g_ascii_strcasecmp (key, "session-id") == 0 && props->session_id == MBIM_DEVICE_SESSION_ID_AUTOMATIC) { |
| 183 |
if (!mbimcli_read_uint_from_string (value, &props->session_id)) { |
| 184 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED, |
| 185 |
"invalid session-id given: '%s'", value); |
| 186 |
return FALSE; |
| 187 |
} |
| 188 |
return TRUE; |
| 189 |
} |
| 190 |
|
| 191 |
if (g_ascii_strcasecmp (key, "iface") == 0 && !props->iface) { |
| 192 |
props->iface = g_strdup (value); |
| 193 |
return TRUE; |
| 194 |
} |
| 195 |
|
| 196 |
if (g_ascii_strcasecmp (key, "prefix") == 0 && !props->prefix) { |
| 197 |
props->prefix = g_strdup (value); |
| 198 |
return TRUE; |
| 199 |
} |
| 200 |
|
| 201 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED, |
| 202 |
"unrecognized or duplicate option '%s'", key); |
| 203 |
return FALSE; |
| 204 |
} |
| 205 |
|
| 206 |
static void |
| 207 |
device_link_add (MbimDevice *dev, |
| 208 |
GCancellable *cancellable, |
| 209 |
const gchar *add_settings) |
| 210 |
{ |
| 211 |
g_autoptr(GError) error = NULL; |
| 212 |
AddLinkProperties props = { |
| 213 |
.session_id = MBIM_DEVICE_SESSION_ID_AUTOMATIC, |
| 214 |
.iface = NULL, |
| 215 |
.prefix = NULL, |
| 216 |
}; |
| 217 |
|
| 218 |
if (!mbimcli_parse_key_value_string (add_settings, |
| 219 |
&error, |
| 220 |
(MbimParseKeyValueForeachFn)add_link_properties_handle, |
| 221 |
&props)) { |
| 222 |
g_printerr ("error: couldn't parse input add link settings: %s\n", |
| 223 |
error->message); |
| 224 |
mbimcli_async_operation_done (FALSE); |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
if (!props.iface) { |
| 229 |
g_printerr ("error: missing mandatory 'iface' setting\n"); |
| 230 |
mbimcli_async_operation_done (FALSE); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
if (!props.prefix) |
| 235 |
props.prefix = g_strdup_printf ("%s.", props.iface); |
| 236 |
|
| 237 |
if ((props.session_id != MBIM_DEVICE_SESSION_ID_AUTOMATIC) && |
| 238 |
(props.session_id > MBIM_DEVICE_SESSION_ID_MAX)) { |
| 239 |
g_printerr ("error: session id %u out of range [%u,%u]\n", |
| 240 |
props.session_id, MBIM_DEVICE_SESSION_ID_MIN, MBIM_DEVICE_SESSION_ID_MAX); |
| 241 |
mbimcli_async_operation_done (FALSE); |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
mbim_device_add_link (dev, |
| 246 |
props.session_id, |
| 247 |
props.iface, |
| 248 |
props.prefix, |
| 249 |
cancellable, |
| 250 |
(GAsyncReadyCallback)link_add_ready, |
| 251 |
NULL); |
| 252 |
|
| 253 |
g_free (props.iface); |
| 254 |
g_free (props.prefix); |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
device_link_list (MbimDevice *dev, |
| 259 |
GCancellable *cancellable, |
| 260 |
const gchar *iface) |
| 261 |
{ |
| 262 |
g_autoptr(GError) error = NULL; |
| 263 |
g_autoptr(GPtrArray) links = NULL; |
| 264 |
|
| 265 |
if (!mbim_device_list_links (dev, iface, &links, &error)) |
| 266 |
g_printerr ("error: couldn't list links: %s\n", error->message); |
| 267 |
else { |
| 268 |
guint i; |
| 269 |
guint n_links; |
| 270 |
|
| 271 |
n_links = (links ? links->len : 0); |
| 272 |
|
| 273 |
g_print ("[%s] found %u links%s\n", |
| 274 |
mbim_device_get_path_display (dev), |
| 275 |
n_links, |
| 276 |
n_links > 0 ? ":" : ""); |
| 277 |
for (i = 0; i < n_links; i++) |
| 278 |
g_print (" [%u] %s\n", i, (const gchar *) g_ptr_array_index (links, i)); |
| 279 |
} |
| 280 |
|
| 281 |
mbimcli_async_operation_done (!error); |
| 282 |
} |
| 283 |
|
| 284 |
/******************************************************************************/ |
| 285 |
/* Common */ |
| 286 |
|
| 287 |
void |
| 288 |
mbimcli_link_management_run (MbimDevice *dev, |
| 289 |
GCancellable *cancellable) |
| 290 |
{ |
| 291 |
if (link_list_str) |
| 292 |
device_link_list (dev, cancellable, link_list_str); |
| 293 |
else if (link_add_str) |
| 294 |
device_link_add (dev, cancellable, link_add_str); |
| 295 |
else if (link_delete_str) |
| 296 |
device_link_delete (dev, cancellable, link_delete_str); |
| 297 |
else if (link_delete_all_str) |
| 298 |
device_link_delete_all (dev, cancellable, link_delete_all_str); |
| 299 |
else |
| 300 |
g_warn_if_reached (); |
| 301 |
} |