| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* libqmi-glib -- GLib/GIO based library to control QMI devices |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library 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 GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
* Boston, MA 02110-1301 USA. |
| 19 |
* |
| 20 |
* Copyright (C) 2021 Aleksander Morgado <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <limits.h> |
| 24 |
#include <stdlib.h> |
| 25 |
|
| 26 |
#include "qmi-net-port-manager-qmiwwan.h" |
| 27 |
#include "qmi-enum-types.h" |
| 28 |
#include "qmi-flag-types.h" |
| 29 |
#include "qmi-error-types.h" |
| 30 |
#include "qmi-errors.h" |
| 31 |
#include "qmi-helpers.h" |
| 32 |
|
| 33 |
|
| 34 |
G_DEFINE_TYPE (QmiNetPortManagerQmiwwan, qmi_net_port_manager_qmiwwan, QMI_TYPE_NET_PORT_MANAGER) |
| 35 |
|
| 36 |
struct _QmiNetPortManagerQmiwwanPrivate { |
| 37 |
gchar *iface; |
| 38 |
gchar *sysfs_path; |
| 39 |
GFile *sysfs_file; |
| 40 |
gchar *add_mux_sysfs_path; |
| 41 |
gchar *del_mux_sysfs_path; |
| 42 |
|
| 43 |
/* mux id tracking table */ |
| 44 |
GHashTable *mux_id_map; |
| 45 |
}; |
| 46 |
|
| 47 |
/*****************************************************************************/ |
| 48 |
/* The qmap/mux_id attribute was introduced in a newer kernel version. If |
| 49 |
* we don't have this info, try to keep the track of what iface applies to |
| 50 |
* what mux id manually here. |
| 51 |
* |
| 52 |
* Not perfect, but works if the MM doesn't crash and loses the info. |
| 53 |
* This legacy logic won't make any sense on plain qmicli operations, though. |
| 54 |
*/ |
| 55 |
|
| 56 |
static gboolean |
| 57 |
track_mux_id (QmiNetPortManagerQmiwwan *self, |
| 58 |
const gchar *link_iface, |
| 59 |
const gchar *mux_id, |
| 60 |
GError **error) |
| 61 |
{ |
| 62 |
if (g_hash_table_lookup (self->priv->mux_id_map, link_iface)) { |
| 63 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, "Already exists"); |
| 64 |
return FALSE; |
| 65 |
} |
| 66 |
|
| 67 |
g_hash_table_insert (self->priv->mux_id_map, |
| 68 |
g_strdup (link_iface), |
| 69 |
g_strdup (mux_id)); |
| 70 |
return TRUE; |
| 71 |
} |
| 72 |
|
| 73 |
static gboolean |
| 74 |
untrack_mux_id (QmiNetPortManagerQmiwwan *self, |
| 75 |
const gchar *link_iface, |
| 76 |
GError **error) |
| 77 |
{ |
| 78 |
if (!g_hash_table_remove (self->priv->mux_id_map, link_iface)) { |
| 79 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, "Not found"); |
| 80 |
return FALSE; |
| 81 |
} |
| 82 |
return TRUE; |
| 83 |
} |
| 84 |
|
| 85 |
static const gchar * |
| 86 |
get_tracked_mux_id (QmiNetPortManagerQmiwwan *self, |
| 87 |
const gchar *link_iface, |
| 88 |
GError **error) |
| 89 |
{ |
| 90 |
const gchar *found; |
| 91 |
|
| 92 |
found = g_hash_table_lookup (self->priv->mux_id_map, link_iface); |
| 93 |
if (!found) { |
| 94 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, "Not found"); |
| 95 |
return NULL; |
| 96 |
} |
| 97 |
return found; |
| 98 |
} |
| 99 |
|
| 100 |
/*****************************************************************************/ |
| 101 |
|
| 102 |
static gchar * |
| 103 |
read_link_mux_id (const gchar *link_iface, |
| 104 |
GError **error) |
| 105 |
{ |
| 106 |
g_autofree gchar *link_mux_id_sysfs_path = NULL; |
| 107 |
g_autofree gchar *link_mux_id = NULL; |
| 108 |
|
| 109 |
/* mux id expected as an hex integer between 0x01 and 0xfe */ |
| 110 |
link_mux_id = g_malloc0 (5); |
| 111 |
link_mux_id_sysfs_path = g_strdup_printf ("/sys/class/net/%s/qmap/mux_id", link_iface); |
| 112 |
|
| 113 |
if (!qmi_helpers_read_sysfs_file (link_mux_id_sysfs_path, link_mux_id, 4, error)) |
| 114 |
return NULL; |
| 115 |
|
| 116 |
return g_steal_pointer (&link_mux_id); |
| 117 |
} |
| 118 |
|
| 119 |
static gboolean |
| 120 |
lookup_mux_id_in_links (GPtrArray *links, |
| 121 |
const gchar *mux_id, |
| 122 |
gchar **out_link, |
| 123 |
GError **error) |
| 124 |
{ |
| 125 |
guint i; |
| 126 |
|
| 127 |
for (i = 0; links && i < links->len; i++) { |
| 128 |
const gchar *link_iface; |
| 129 |
g_autofree gchar *link_mux_id = NULL; |
| 130 |
|
| 131 |
link_iface = g_ptr_array_index (links, i); |
| 132 |
link_mux_id = read_link_mux_id (link_iface, error); |
| 133 |
if (!link_mux_id) |
| 134 |
return FALSE; |
| 135 |
|
| 136 |
if (g_strcmp0 (mux_id, link_mux_id) == 0) { |
| 137 |
*out_link = g_strdup (link_iface); |
| 138 |
return TRUE; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
*out_link = NULL; |
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
static gchar * |
| 147 |
lookup_first_new_link (GPtrArray *links_before, |
| 148 |
GPtrArray *links_after) |
| 149 |
{ |
| 150 |
guint i; |
| 151 |
|
| 152 |
if (!links_after) |
| 153 |
return NULL; |
| 154 |
|
| 155 |
for (i = 0; i < links_after->len; i++) { |
| 156 |
const gchar *link_iface; |
| 157 |
|
| 158 |
link_iface = g_ptr_array_index (links_after, i); |
| 159 |
|
| 160 |
if (!links_before || !g_ptr_array_find_with_equal_func (links_before, link_iface, g_str_equal, NULL)) |
| 161 |
return g_strdup (link_iface); |
| 162 |
} |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
/*****************************************************************************/ |
| 167 |
|
| 168 |
static gint |
| 169 |
cmpuint (const guint *a, |
| 170 |
const guint *b) |
| 171 |
{ |
| 172 |
return ((*a > *b) ? 1 : ((*b > *a) ? -1 : 0)); |
| 173 |
} |
| 174 |
|
| 175 |
static guint |
| 176 |
get_first_free_mux_id (QmiNetPortManagerQmiwwan *self, |
| 177 |
guint initial_mux_id, |
| 178 |
GPtrArray *links, |
| 179 |
GError **error) |
| 180 |
{ |
| 181 |
guint i; |
| 182 |
g_autoptr(GArray) existing_mux_ids = NULL; |
| 183 |
guint next_mux_id; |
| 184 |
static const guint max_mux_id_upper_threshold = QMI_DEVICE_MUX_ID_MAX + 1; |
| 185 |
|
| 186 |
if (!links) |
| 187 |
return initial_mux_id; |
| 188 |
|
| 189 |
existing_mux_ids = g_array_new (FALSE, FALSE, sizeof (guint)); |
| 190 |
|
| 191 |
for (i = 0; i < links->len; i++) { |
| 192 |
const gchar *link_iface; |
| 193 |
g_autofree gchar *link_mux_id = NULL; |
| 194 |
gulong link_mux_id_num; |
| 195 |
|
| 196 |
link_iface = g_ptr_array_index (links, i); |
| 197 |
link_mux_id = read_link_mux_id (link_iface, NULL); |
| 198 |
if (!link_mux_id) { |
| 199 |
const gchar *tracked_link_mux_id; |
| 200 |
|
| 201 |
g_debug ("Couldn't read mux id from sysfs for link '%s': unsupported by driver", link_iface); |
| 202 |
/* fallback to use our internal tracking table... far from perfect */ |
| 203 |
tracked_link_mux_id = get_tracked_mux_id (self, link_iface, NULL); |
| 204 |
if (!tracked_link_mux_id) { |
| 205 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_UNSUPPORTED, |
| 206 |
"Couldn't get tracked mux id for link '%s'", link_iface); |
| 207 |
return QMI_DEVICE_MUX_ID_UNBOUND; |
| 208 |
} |
| 209 |
link_mux_id_num = strtoul (tracked_link_mux_id, NULL, 16); |
| 210 |
} else |
| 211 |
link_mux_id_num = strtoul (link_mux_id, NULL, 16); |
| 212 |
|
| 213 |
if (!link_mux_id_num) { |
| 214 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 215 |
"Couldn't parse mux id '%s'", link_mux_id); |
| 216 |
return QMI_DEVICE_MUX_ID_UNBOUND; |
| 217 |
} |
| 218 |
|
| 219 |
g_array_append_val (existing_mux_ids, link_mux_id_num); |
| 220 |
} |
| 221 |
|
| 222 |
/* add upper level threshold, so that if we end up out of the loop |
| 223 |
* below, it means we have exhausted all mux ids */ |
| 224 |
g_array_append_val (existing_mux_ids, max_mux_id_upper_threshold); |
| 225 |
g_array_sort (existing_mux_ids, (GCompareFunc)cmpuint); |
| 226 |
|
| 227 |
for (next_mux_id = initial_mux_id, i = 0; i < existing_mux_ids->len; next_mux_id++, i++) { |
| 228 |
guint existing; |
| 229 |
|
| 230 |
existing = g_array_index (existing_mux_ids, guint, i); |
| 231 |
if (next_mux_id < existing) |
| 232 |
return next_mux_id; |
| 233 |
g_assert_cmpuint (next_mux_id, ==, existing); |
| 234 |
} |
| 235 |
|
| 236 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, "No mux ids left"); |
| 237 |
return QMI_DEVICE_MUX_ID_UNBOUND; |
| 238 |
} |
| 239 |
|
| 240 |
/*****************************************************************************/ |
| 241 |
|
| 242 |
static gchar * |
| 243 |
net_port_manager_add_link_finish (QmiNetPortManager *self, |
| 244 |
guint *mux_id, |
| 245 |
GAsyncResult *res, |
| 246 |
GError **error) |
| 247 |
{ |
| 248 |
gchar *link_name; |
| 249 |
|
| 250 |
link_name = g_task_propagate_pointer (G_TASK (res), error); |
| 251 |
if (!link_name) |
| 252 |
return NULL; |
| 253 |
|
| 254 |
if (mux_id) |
| 255 |
*mux_id = GPOINTER_TO_UINT (g_task_get_task_data (G_TASK (res))); |
| 256 |
|
| 257 |
return link_name; |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
net_port_manager_add_link (QmiNetPortManager *_self, |
| 262 |
guint mux_id, |
| 263 |
guint initial_mux_id, |
| 264 |
const gchar *base_ifname, |
| 265 |
const gchar *ifname_prefix, |
| 266 |
QmiDeviceAddLinkFlags flags, |
| 267 |
guint timeout, |
| 268 |
GCancellable *cancellable, |
| 269 |
GAsyncReadyCallback callback, |
| 270 |
gpointer user_data) |
| 271 |
{ |
| 272 |
QmiNetPortManagerQmiwwan *self = QMI_NET_PORT_MANAGER_QMIWWAN (_self); |
| 273 |
GTask *task; |
| 274 |
GError *error = NULL; |
| 275 |
g_autoptr(GPtrArray) links_before = NULL; |
| 276 |
g_autoptr(GPtrArray) links_after = NULL; |
| 277 |
g_autofree gchar *link_name = NULL; |
| 278 |
g_autofree gchar *mux_id_str = NULL; |
| 279 |
|
| 280 |
g_debug ("Net port manager based on qmi_wwan ignores the ifname prefix '%s'", ifname_prefix); |
| 281 |
g_debug ("Running add link operation..."); |
| 282 |
|
| 283 |
task = g_task_new (self, cancellable, callback, user_data); |
| 284 |
|
| 285 |
if (flags != QMI_DEVICE_ADD_LINK_FLAGS_NONE) { |
| 286 |
g_autofree gchar *flags_str = NULL; |
| 287 |
|
| 288 |
flags_str = qmi_device_add_link_flags_build_string_from_mask (flags); |
| 289 |
g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_UNSUPPORTED, |
| 290 |
"Adding link with flags '%s' is not supported", flags_str); |
| 291 |
g_object_unref (task); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
if (!qmi_helpers_list_links (self->priv->sysfs_file, |
| 296 |
cancellable, |
| 297 |
NULL, |
| 298 |
&links_before, |
| 299 |
&error)) { |
| 300 |
g_prefix_error (&error, "Couldn't enumerate files in the sysfs directory before link addition: "); |
| 301 |
g_task_return_error (task, error); |
| 302 |
g_object_unref (task); |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
if (mux_id == QMI_DEVICE_MUX_ID_AUTOMATIC) { |
| 307 |
mux_id = get_first_free_mux_id (self, initial_mux_id, links_before, &error); |
| 308 |
if (mux_id == QMI_DEVICE_MUX_ID_UNBOUND) { |
| 309 |
g_prefix_error (&error, "Couldn't add link with automatic mux id: "); |
| 310 |
g_task_return_error (task, error); |
| 311 |
g_object_unref (task); |
| 312 |
return; |
| 313 |
} |
| 314 |
g_debug ("Using mux id %u", mux_id); |
| 315 |
} |
| 316 |
|
| 317 |
g_task_set_task_data (task, GUINT_TO_POINTER (mux_id), NULL); |
| 318 |
mux_id_str = g_strdup_printf ("0x%02x", mux_id); |
| 319 |
|
| 320 |
if (!qmi_helpers_write_sysfs_file (self->priv->add_mux_sysfs_path, mux_id_str, &error)) { |
| 321 |
g_prefix_error (&error, "Couldn't add create link with mux id %s: ", mux_id_str); |
| 322 |
g_task_return_error (task, error); |
| 323 |
g_object_unref (task); |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
if (!qmi_helpers_list_links (self->priv->sysfs_file, |
| 328 |
cancellable, |
| 329 |
links_before, |
| 330 |
&links_after, |
| 331 |
&error)) { |
| 332 |
g_prefix_error (&error, "Couldn't enumerate files in the sysfs directory after link addition: "); |
| 333 |
g_task_return_error (task, error); |
| 334 |
g_object_unref (task); |
| 335 |
return; |
| 336 |
} |
| 337 |
|
| 338 |
if (!lookup_mux_id_in_links (links_after, mux_id_str, &link_name, NULL)) { |
| 339 |
/* Now, assume this is because the mux_id attribute was added in a newer |
| 340 |
* kernel. As a fallback, we'll try to detect the first new link listed, |
| 341 |
* even if this is definitely very racy. */ |
| 342 |
link_name = lookup_first_new_link (links_before, links_after); |
| 343 |
if (!link_name) { |
| 344 |
g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 345 |
"No new link detected for mux id %s", mux_id_str); |
| 346 |
g_object_unref (task); |
| 347 |
return; |
| 348 |
} |
| 349 |
g_debug ("Found first new link '%s' (unknown mux id)", link_name); |
| 350 |
} else |
| 351 |
g_debug ("Found link '%s' associated to mux id '%s'", link_name, mux_id_str); |
| 352 |
|
| 353 |
|
| 354 |
if (!track_mux_id (self, link_name, mux_id_str, &error)) { |
| 355 |
g_warning ("Couldn't track mux id: %s", error->message); |
| 356 |
g_clear_error (&error); |
| 357 |
} |
| 358 |
|
| 359 |
g_task_return_pointer (task, g_steal_pointer (&link_name), g_free); |
| 360 |
g_object_unref (task); |
| 361 |
} |
| 362 |
|
| 363 |
/*****************************************************************************/ |
| 364 |
|
| 365 |
static gboolean |
| 366 |
net_port_manager_del_link_finish (QmiNetPortManager *self, |
| 367 |
GAsyncResult *res, |
| 368 |
GError **error) |
| 369 |
{ |
| 370 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 371 |
} |
| 372 |
|
| 373 |
static void |
| 374 |
net_port_manager_del_link (QmiNetPortManager *_self, |
| 375 |
const gchar *ifname, |
| 376 |
guint mux_id, |
| 377 |
guint timeout, |
| 378 |
GCancellable *cancellable, |
| 379 |
GAsyncReadyCallback callback, |
| 380 |
gpointer user_data) |
| 381 |
{ |
| 382 |
QmiNetPortManagerQmiwwan *self = QMI_NET_PORT_MANAGER_QMIWWAN (_self); |
| 383 |
GTask *task; |
| 384 |
GError *error = NULL; |
| 385 |
g_autoptr(GPtrArray) links_before = NULL; |
| 386 |
g_autoptr(GPtrArray) links_after = NULL; |
| 387 |
g_autofree gchar *mux_id_str = NULL; |
| 388 |
|
| 389 |
g_debug ("Running del link (%s) operation...", ifname); |
| 390 |
|
| 391 |
task = g_task_new (self, cancellable, callback, user_data); |
| 392 |
|
| 393 |
if (!qmi_helpers_list_links (self->priv->sysfs_file, |
| 394 |
cancellable, |
| 395 |
NULL, |
| 396 |
&links_before, |
| 397 |
&error)) { |
| 398 |
g_prefix_error (&error, "Couldn't enumerate files in the sysfs directory before link deletion: "); |
| 399 |
g_task_return_error (task, error); |
| 400 |
g_object_unref (task); |
| 401 |
return; |
| 402 |
} |
| 403 |
|
| 404 |
if (!links_before || !g_ptr_array_find_with_equal_func (links_before, ifname, g_str_equal, NULL)) { |
| 405 |
g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_INVALID_ARGS, |
| 406 |
"Cannot delete link '%s': interface not found", |
| 407 |
ifname); |
| 408 |
g_object_unref (task); |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
/* Try to guess mux id if not given as input */ |
| 413 |
if (mux_id != QMI_DEVICE_MUX_ID_UNBOUND) |
| 414 |
mux_id_str = g_strdup_printf ("0x%02x", mux_id); |
| 415 |
else { |
| 416 |
mux_id_str = read_link_mux_id (ifname, NULL); |
| 417 |
if (!mux_id_str) { |
| 418 |
mux_id_str = g_strdup (get_tracked_mux_id (self, ifname, NULL)); |
| 419 |
if (!mux_id_str) { |
| 420 |
/* This unsupported error allows us to flag when del_all_links() |
| 421 |
* needs to switch to the fallback mechanism */ |
| 422 |
g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_UNSUPPORTED, |
| 423 |
"Cannot delete link '%s': unknown mux id", |
| 424 |
ifname); |
| 425 |
g_object_unref (task); |
| 426 |
return; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
if (!qmi_helpers_write_sysfs_file (self->priv->del_mux_sysfs_path, mux_id_str, &error)) { |
| 432 |
g_prefix_error (&error, "Couldn't delete link with mux id %s: ", mux_id_str); |
| 433 |
g_task_return_error (task, error); |
| 434 |
g_object_unref (task); |
| 435 |
return; |
| 436 |
} |
| 437 |
|
| 438 |
if (!qmi_helpers_list_links (self->priv->sysfs_file, |
| 439 |
cancellable, |
| 440 |
links_before, |
| 441 |
&links_after, |
| 442 |
&error)) { |
| 443 |
g_prefix_error (&error, "Couldn't enumerate files in the sysfs directory after link deletion: "); |
| 444 |
g_task_return_error (task, error); |
| 445 |
g_object_unref (task); |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
if (links_after && g_ptr_array_find_with_equal_func (links_after, ifname, g_str_equal, NULL)) { |
| 450 |
g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 451 |
"link '%s' still detected", ifname); |
| 452 |
g_object_unref (task); |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
if (!untrack_mux_id (self, ifname, &error)) { |
| 457 |
g_debug ("couldn't untrack mux id: %s", error->message); |
| 458 |
g_clear_error (&error); |
| 459 |
} |
| 460 |
g_task_return_boolean (task, TRUE); |
| 461 |
g_object_unref (task); |
| 462 |
} |
| 463 |
|
| 464 |
/*****************************************************************************/ |
| 465 |
|
| 466 |
static gboolean |
| 467 |
net_port_manager_del_all_links_finish (QmiNetPortManager *self, |
| 468 |
GAsyncResult *res, |
| 469 |
GError **error) |
| 470 |
{ |
| 471 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 472 |
} |
| 473 |
|
| 474 |
static void |
| 475 |
fallback_del_all_links (GTask *task) |
| 476 |
{ |
| 477 |
QmiNetPortManagerQmiwwan *self; |
| 478 |
guint i; |
| 479 |
g_autoptr(GPtrArray) links_before = NULL; |
| 480 |
g_autoptr(GPtrArray) links_after = NULL; |
| 481 |
GError *error = NULL; |
| 482 |
guint n_deleted = 0; |
| 483 |
|
| 484 |
self = g_task_get_source_object (task); |
| 485 |
|
| 486 |
g_debug ("Running fallback link deletion logic..."); |
| 487 |
|
| 488 |
if (!qmi_helpers_list_links (self->priv->sysfs_file, |
| 489 |
g_task_get_cancellable (task), |
| 490 |
NULL, |
| 491 |
&links_before, |
| 492 |
&error)) { |
| 493 |
g_prefix_error (&error, "Couldn't list links before deleting all: "); |
| 494 |
g_task_return_error (task, error); |
| 495 |
g_object_unref (task); |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
if (!links_before) { |
| 500 |
g_task_return_boolean (task, TRUE); |
| 501 |
g_object_unref (task); |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
for (i = QMI_DEVICE_MUX_ID_MIN; i <= QMI_DEVICE_MUX_ID_MAX; i++) { |
| 506 |
g_autofree gchar *mux_id = NULL; |
| 507 |
|
| 508 |
mux_id = g_strdup_printf ("0x%02x", i); |
| 509 |
|
| 510 |
/* attempt to delete link with the given mux id; if there is no such link |
| 511 |
* the kernel will complain with a harmless "mux_id not present" warning */ |
| 512 |
if ((qmi_helpers_write_sysfs_file (self->priv->del_mux_sysfs_path, mux_id, NULL) && |
| 513 |
(++n_deleted == links_before->len))) { |
| 514 |
/* early break if all N links deleted already */ |
| 515 |
break; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
if (!qmi_helpers_list_links (self->priv->sysfs_file, |
| 520 |
g_task_get_cancellable (task), |
| 521 |
NULL, |
| 522 |
&links_after, |
| 523 |
&error)) { |
| 524 |
g_prefix_error (&error, "Couldn't list links after deleting all: "); |
| 525 |
g_task_return_error (task, error); |
| 526 |
} else if (links_after) |
| 527 |
g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 528 |
"Not all links were deleted"); |
| 529 |
else |
| 530 |
g_task_return_boolean (task, TRUE); |
| 531 |
g_object_unref (task); |
| 532 |
} |
| 533 |
|
| 534 |
static void |
| 535 |
parent_del_all_links_ready (QmiNetPortManager *self, |
| 536 |
GAsyncResult *res, |
| 537 |
GTask *task) |
| 538 |
{ |
| 539 |
g_autoptr(GError) error = NULL; |
| 540 |
|
| 541 |
if (!QMI_NET_PORT_MANAGER_CLASS (qmi_net_port_manager_qmiwwan_parent_class)->del_all_links_finish (self, res, &error)) { |
| 542 |
if (g_error_matches (error, QMI_CORE_ERROR, QMI_CORE_ERROR_UNSUPPORTED)) { |
| 543 |
fallback_del_all_links (task); |
| 544 |
return; |
| 545 |
} |
| 546 |
g_task_return_error (task, g_steal_pointer (&error)); |
| 547 |
} else |
| 548 |
g_task_return_boolean (task, TRUE); |
| 549 |
g_object_unref (task); |
| 550 |
} |
| 551 |
|
| 552 |
static void |
| 553 |
net_port_manager_del_all_links (QmiNetPortManager *_self, |
| 554 |
const gchar *base_ifname, |
| 555 |
GCancellable *cancellable, |
| 556 |
GAsyncReadyCallback callback, |
| 557 |
gpointer user_data) |
| 558 |
{ |
| 559 |
QmiNetPortManagerQmiwwan *self = QMI_NET_PORT_MANAGER_QMIWWAN (_self); |
| 560 |
GTask *task; |
| 561 |
|
| 562 |
task = g_task_new (self, cancellable, callback, user_data); |
| 563 |
|
| 564 |
/* validate base ifname before doing anything else */ |
| 565 |
if (base_ifname && !g_str_equal (base_ifname, self->priv->iface)) { |
| 566 |
g_task_return_new_error (task, QMI_CORE_ERROR, QMI_CORE_ERROR_INVALID_ARGS, |
| 567 |
"Invalid base interface given: '%s' (must be '%s')", |
| 568 |
base_ifname, self->priv->iface); |
| 569 |
g_object_unref (task); |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
QMI_NET_PORT_MANAGER_CLASS (qmi_net_port_manager_qmiwwan_parent_class)->del_all_links (_self, |
| 574 |
base_ifname, |
| 575 |
cancellable, |
| 576 |
(GAsyncReadyCallback)parent_del_all_links_ready, |
| 577 |
task); |
| 578 |
} |
| 579 |
|
| 580 |
/*****************************************************************************/ |
| 581 |
|
| 582 |
QmiNetPortManagerQmiwwan * |
| 583 |
qmi_net_port_manager_qmiwwan_new (const gchar *iface, |
| 584 |
GError **error) |
| 585 |
{ |
| 586 |
g_autoptr(QmiNetPortManagerQmiwwan) self = NULL; |
| 587 |
|
| 588 |
self = QMI_NET_PORT_MANAGER_QMIWWAN (g_object_new (QMI_TYPE_NET_PORT_MANAGER_QMIWWAN, NULL)); |
| 589 |
|
| 590 |
self->priv->iface = g_strdup (iface); |
| 591 |
|
| 592 |
self->priv->sysfs_path = g_strdup_printf ("/sys/class/net/%s", iface); |
| 593 |
self->priv->sysfs_file = g_file_new_for_path (self->priv->sysfs_path); |
| 594 |
|
| 595 |
self->priv->add_mux_sysfs_path = g_strdup_printf ("%s/qmi/add_mux", self->priv->sysfs_path); |
| 596 |
self->priv->del_mux_sysfs_path = g_strdup_printf ("%s/qmi/del_mux", self->priv->sysfs_path); |
| 597 |
|
| 598 |
if (!g_file_test (self->priv->add_mux_sysfs_path, G_FILE_TEST_EXISTS) || |
| 599 |
!g_file_test (self->priv->del_mux_sysfs_path, G_FILE_TEST_EXISTS)) { |
| 600 |
g_set_error (error, QMI_CORE_ERROR, QMI_CORE_ERROR_FAILED, |
| 601 |
"No support for multiplexing in the interface"); |
| 602 |
return NULL; |
| 603 |
} |
| 604 |
|
| 605 |
return g_steal_pointer (&self); |
| 606 |
} |
| 607 |
|
| 608 |
/*****************************************************************************/ |
| 609 |
|
| 610 |
static void |
| 611 |
qmi_net_port_manager_qmiwwan_init (QmiNetPortManagerQmiwwan *self) |
| 612 |
{ |
| 613 |
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, |
| 614 |
QMI_TYPE_NET_PORT_MANAGER_QMIWWAN, |
| 615 |
QmiNetPortManagerQmiwwanPrivate); |
| 616 |
|
| 617 |
self->priv->mux_id_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
| 618 |
} |
| 619 |
|
| 620 |
static void |
| 621 |
finalize (GObject *object) |
| 622 |
{ |
| 623 |
QmiNetPortManagerQmiwwan *self = QMI_NET_PORT_MANAGER_QMIWWAN (object); |
| 624 |
|
| 625 |
g_hash_table_unref (self->priv->mux_id_map); |
| 626 |
g_free (self->priv->iface); |
| 627 |
g_object_unref (self->priv->sysfs_file); |
| 628 |
g_free (self->priv->sysfs_path); |
| 629 |
g_free (self->priv->del_mux_sysfs_path); |
| 630 |
g_free (self->priv->add_mux_sysfs_path); |
| 631 |
|
| 632 |
G_OBJECT_CLASS (qmi_net_port_manager_qmiwwan_parent_class)->finalize (object); |
| 633 |
} |
| 634 |
|
| 635 |
static void |
| 636 |
qmi_net_port_manager_qmiwwan_class_init (QmiNetPortManagerQmiwwanClass *klass) |
| 637 |
{ |
| 638 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 639 |
QmiNetPortManagerClass *net_port_manager_class = QMI_NET_PORT_MANAGER_CLASS (klass); |
| 640 |
|
| 641 |
g_type_class_add_private (object_class, sizeof (QmiNetPortManagerQmiwwanPrivate)); |
| 642 |
|
| 643 |
object_class->finalize = finalize; |
| 644 |
|
| 645 |
net_port_manager_class->add_link = net_port_manager_add_link; |
| 646 |
net_port_manager_class->add_link_finish = net_port_manager_add_link_finish; |
| 647 |
net_port_manager_class->del_link = net_port_manager_del_link; |
| 648 |
net_port_manager_class->del_link_finish = net_port_manager_del_link_finish; |
| 649 |
net_port_manager_class->del_all_links = net_port_manager_del_all_links; |
| 650 |
net_port_manager_class->del_all_links_finish = net_port_manager_del_all_links_finish; |
| 651 |
} |