| 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 "qmi-net-port-manager.h" |
| 24 |
#include "qmi-device.h" |
| 25 |
#include "qmi-helpers.h" |
| 26 |
|
| 27 |
G_DEFINE_ABSTRACT_TYPE (QmiNetPortManager, qmi_net_port_manager, G_TYPE_OBJECT) |
| 28 |
|
| 29 |
/*****************************************************************************/ |
| 30 |
|
| 31 |
void |
| 32 |
qmi_net_port_manager_add_link (QmiNetPortManager *self, |
| 33 |
guint mux_id, |
| 34 |
guint initial_mux_id, |
| 35 |
const gchar *base_ifname, |
| 36 |
const gchar *ifname_prefix, |
| 37 |
QmiDeviceAddLinkFlags flags, |
| 38 |
guint timeout, |
| 39 |
GCancellable *cancellable, |
| 40 |
GAsyncReadyCallback callback, |
| 41 |
gpointer user_data) |
| 42 |
{ |
| 43 |
QMI_NET_PORT_MANAGER_GET_CLASS (self)->add_link (self, |
| 44 |
mux_id, |
| 45 |
initial_mux_id, |
| 46 |
base_ifname, |
| 47 |
ifname_prefix, |
| 48 |
flags, |
| 49 |
timeout, |
| 50 |
cancellable, |
| 51 |
callback, |
| 52 |
user_data); |
| 53 |
} |
| 54 |
|
| 55 |
gchar * |
| 56 |
qmi_net_port_manager_add_link_finish (QmiNetPortManager *self, |
| 57 |
guint *mux_id, |
| 58 |
GAsyncResult *res, |
| 59 |
GError **error) |
| 60 |
{ |
| 61 |
return QMI_NET_PORT_MANAGER_GET_CLASS (self)->add_link_finish (self, mux_id, res, error); |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
qmi_net_port_manager_del_link (QmiNetPortManager *self, |
| 66 |
const gchar *ifname, |
| 67 |
guint mux_id, |
| 68 |
guint timeout, |
| 69 |
GCancellable *cancellable, |
| 70 |
GAsyncReadyCallback callback, |
| 71 |
gpointer user_data) |
| 72 |
{ |
| 73 |
QMI_NET_PORT_MANAGER_GET_CLASS (self)->del_link (self, |
| 74 |
ifname, |
| 75 |
mux_id, |
| 76 |
timeout, |
| 77 |
cancellable, |
| 78 |
callback, |
| 79 |
user_data); |
| 80 |
} |
| 81 |
|
| 82 |
gboolean |
| 83 |
qmi_net_port_manager_del_link_finish (QmiNetPortManager *self, |
| 84 |
GAsyncResult *res, |
| 85 |
GError **error) |
| 86 |
{ |
| 87 |
return QMI_NET_PORT_MANAGER_GET_CLASS (self)->del_link_finish (self, res, error); |
| 88 |
} |
| 89 |
|
| 90 |
void |
| 91 |
qmi_net_port_manager_del_all_links (QmiNetPortManager *self, |
| 92 |
const gchar *base_ifname, |
| 93 |
GCancellable *cancellable, |
| 94 |
GAsyncReadyCallback callback, |
| 95 |
gpointer user_data) |
| 96 |
{ |
| 97 |
QMI_NET_PORT_MANAGER_GET_CLASS (self)->del_all_links (self, |
| 98 |
base_ifname, |
| 99 |
cancellable, |
| 100 |
callback, |
| 101 |
user_data); |
| 102 |
} |
| 103 |
|
| 104 |
gboolean |
| 105 |
qmi_net_port_manager_del_all_links_finish (QmiNetPortManager *self, |
| 106 |
GAsyncResult *res, |
| 107 |
GError **error) |
| 108 |
{ |
| 109 |
return QMI_NET_PORT_MANAGER_GET_CLASS (self)->del_all_links_finish (self, res, error); |
| 110 |
} |
| 111 |
|
| 112 |
gboolean |
| 113 |
qmi_net_port_manager_list_links (QmiNetPortManager *self, |
| 114 |
const gchar *base_ifname, |
| 115 |
GPtrArray **out_links, |
| 116 |
GError **error) |
| 117 |
{ |
| 118 |
return QMI_NET_PORT_MANAGER_GET_CLASS (self)->list_links (self, base_ifname, out_links, error); |
| 119 |
} |
| 120 |
|
| 121 |
/*****************************************************************************/ |
| 122 |
/* Default implementations */ |
| 123 |
|
| 124 |
static gboolean |
| 125 |
net_port_manager_list_links (QmiNetPortManager *self, |
| 126 |
const gchar *base_ifname, |
| 127 |
GPtrArray **out_links, |
| 128 |
GError **error) |
| 129 |
{ |
| 130 |
g_autoptr(GFile) sysfs_file = NULL; |
| 131 |
g_autofree gchar *sysfs_path = NULL; |
| 132 |
|
| 133 |
sysfs_path = g_strdup_printf ("/sys/class/net/%s", base_ifname); |
| 134 |
sysfs_file = g_file_new_for_path (sysfs_path); |
| 135 |
|
| 136 |
return qmi_helpers_list_links (sysfs_file, NULL, NULL, out_links, error); |
| 137 |
} |
| 138 |
|
| 139 |
typedef struct { |
| 140 |
GPtrArray *links; |
| 141 |
guint link_i; |
| 142 |
} DelAllLinksContext; |
| 143 |
|
| 144 |
static void |
| 145 |
del_all_links_context_free (DelAllLinksContext *ctx) |
| 146 |
{ |
| 147 |
g_clear_pointer (&ctx->links, g_ptr_array_unref); |
| 148 |
g_slice_free (DelAllLinksContext, ctx); |
| 149 |
} |
| 150 |
|
| 151 |
static gboolean |
| 152 |
net_port_manager_del_all_links_finish (QmiNetPortManager *self, |
| 153 |
GAsyncResult *res, |
| 154 |
GError **error) |
| 155 |
{ |
| 156 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 157 |
} |
| 158 |
|
| 159 |
static void delete_next_link (GTask *task); |
| 160 |
|
| 161 |
static void |
| 162 |
port_manager_del_link_ready (QmiNetPortManager *self, |
| 163 |
GAsyncResult *res, |
| 164 |
GTask *task) |
| 165 |
{ |
| 166 |
DelAllLinksContext *ctx; |
| 167 |
GError *error = NULL; |
| 168 |
|
| 169 |
ctx = g_task_get_task_data (task); |
| 170 |
|
| 171 |
if (!qmi_net_port_manager_del_link_finish (self, res, &error)) { |
| 172 |
g_task_return_error (task, error); |
| 173 |
g_object_unref (task); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
g_ptr_array_remove_index_fast (ctx->links, 0); |
| 178 |
delete_next_link (task); |
| 179 |
} |
| 180 |
|
| 181 |
static void |
| 182 |
delete_next_link (GTask *task) |
| 183 |
{ |
| 184 |
QmiNetPortManager *self; |
| 185 |
DelAllLinksContext *ctx; |
| 186 |
|
| 187 |
self = g_task_get_source_object (task); |
| 188 |
ctx = g_task_get_task_data (task); |
| 189 |
|
| 190 |
if (!ctx->links || ctx->links->len == 0) { |
| 191 |
g_task_return_boolean (task, TRUE); |
| 192 |
g_object_unref (task); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
qmi_net_port_manager_del_link (self, |
| 197 |
g_ptr_array_index (ctx->links, 0), |
| 198 |
QMI_DEVICE_MUX_ID_UNBOUND, |
| 199 |
5, |
| 200 |
g_task_get_cancellable (task), |
| 201 |
(GAsyncReadyCallback)port_manager_del_link_ready, |
| 202 |
task); |
| 203 |
} |
| 204 |
|
| 205 |
static void |
| 206 |
net_port_manager_del_all_links (QmiNetPortManager *self, |
| 207 |
const gchar *base_ifname, |
| 208 |
GCancellable *cancellable, |
| 209 |
GAsyncReadyCallback callback, |
| 210 |
gpointer user_data) |
| 211 |
{ |
| 212 |
GTask *task; |
| 213 |
DelAllLinksContext *ctx; |
| 214 |
GError *error = NULL; |
| 215 |
|
| 216 |
task = g_task_new (self, cancellable, callback, user_data); |
| 217 |
ctx = g_slice_new0 (DelAllLinksContext); |
| 218 |
g_task_set_task_data (task, ctx, (GDestroyNotify)del_all_links_context_free); |
| 219 |
|
| 220 |
if (!qmi_net_port_manager_list_links (self, base_ifname, &ctx->links, &error)) { |
| 221 |
g_task_return_error (task, error); |
| 222 |
g_object_unref (task); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
delete_next_link (task); |
| 227 |
} |
| 228 |
|
| 229 |
/*****************************************************************************/ |
| 230 |
|
| 231 |
static void |
| 232 |
qmi_net_port_manager_init (QmiNetPortManager *self) |
| 233 |
{ |
| 234 |
} |
| 235 |
|
| 236 |
static void |
| 237 |
qmi_net_port_manager_class_init (QmiNetPortManagerClass *klass) |
| 238 |
{ |
| 239 |
klass->list_links = net_port_manager_list_links; |
| 240 |
klass->del_all_links = net_port_manager_del_all_links; |
| 241 |
klass->del_all_links_finish = net_port_manager_del_all_links_finish; |
| 242 |
} |