summaryrefslogtreecommitdiff
path: root/src/mbimcli/mbimcli-link-management.c
blob: 900d09e6c0a82ad8f9bb40497f8dd73bfebe4b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * mbimcli -- Command line interface to control MBIM devices
 *
 * Copyright (C) 2021 Aleksander Morgado <aleksander@aleksander.es>
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>

#include <glib.h>
#include <glib/gprintf.h>
#include <gio/gio.h>

#include <libmbim-glib.h>

#include "mbimcli.h"
#include "mbimcli-helpers.h"

/* Options */
static gchar *link_list_str;
static gchar *link_add_str;
static gchar *link_delete_str;
static gchar *link_delete_all_str;

static GOptionEntry entries[] = {
    { "link-list", 0, 0, G_OPTION_ARG_STRING, &link_list_str,
      "List links created from a given interface",
      "[IFACE]"
    },
    { "link-add", 0, 0, G_OPTION_ARG_STRING, &link_add_str,
      "Create new network interface link",
      "[iface=IFACE,prefix=PREFIX[,session-id=N]]"
    },
    { "link-delete", 0, 0, G_OPTION_ARG_STRING, &link_delete_str,
      "Delete a given network interface link",
      "IFACE"
    },
    { "link-delete-all", 0, 0, G_OPTION_ARG_STRING, &link_delete_all_str,
      "Delete all network interface links from the given interface",
      "[IFACE]"
    },
    { NULL }
};

GOptionGroup *
mbimcli_link_management_get_option_group (void)
{
    GOptionGroup *group;

    group = g_option_group_new ("link-management",
                                "Link management options:",
                                "Show link management specific options",
                                NULL, NULL);
    g_option_group_add_entries (group, entries);

    return group;
}

gboolean
mbimcli_link_management_options_enabled (void)
{
    static guint n_actions = 0;
    static gboolean checked = FALSE;

    if (checked)
        return !!n_actions;

    n_actions = (!!link_list_str +
                 !!link_add_str +
                 !!link_delete_str +
                 !!link_delete_all_str);

    if (n_actions > 1) {
        g_printerr ("error: too many link management actions requested\n");
        exit (EXIT_FAILURE);
    }

    checked = TRUE;
    return !!n_actions;
}

/******************************************************************************/

static void
link_delete_all_ready (MbimDevice   *dev,
                       GAsyncResult *res)
{
    g_autoptr(GError) error = NULL;

    if (!mbim_device_delete_all_links_finish (dev, res, &error))
        g_printerr ("error: couldn't delete all links: %s\n", error->message);
    else
        g_print ("[%s] all links successfully deleted\n",
                 mbim_device_get_path_display (dev));

    mbimcli_async_operation_done (!error);
}

static void
device_link_delete_all (MbimDevice   *dev,
                        GCancellable *cancellable,
                        const gchar  *iface)
{
    mbim_device_delete_all_links (dev,
                                  iface,
                                  cancellable,
                                  (GAsyncReadyCallback)link_delete_all_ready,
                                  NULL);
}

/******************************************************************************/

static void
link_delete_ready (MbimDevice   *dev,
                   GAsyncResult *res)
{
    g_autoptr(GError) error = NULL;

    if (!mbim_device_delete_link_finish (dev, res, &error))
        g_printerr ("error: couldn't delete link: %s\n",
                    error->message);
    else
        g_print ("[%s] link successfully deleted\n",
                 mbim_device_get_path_display (dev));

    mbimcli_async_operation_done (!error);
}

static void
device_link_delete (MbimDevice   *dev,
                    GCancellable *cancellable,
                    const gchar  *link_iface)
{
    mbim_device_delete_link (dev,
                             link_iface,
                             cancellable,
                             (GAsyncReadyCallback)link_delete_ready,
                             NULL);
}

typedef struct {
    guint  session_id;
    gchar *iface;
    gchar *prefix;
} AddLinkProperties;

static void
link_add_ready (MbimDevice   *dev,
                GAsyncResult *res)
{
    g_autoptr(GError)  error = NULL;
    g_autofree gchar  *link_iface = NULL;
    guint              session_id;

    link_iface = mbim_device_add_link_finish (dev, res, &session_id, &error);
    if (!link_iface)
        g_printerr ("error: couldn't add link: %s\n",
                    error->message);
    else
        g_print ("[%s] link successfully added:\n"
                 "  iface name: %s\n"
                 "  session id: %u\n",
                 mbim_device_get_path_display (dev),
                 link_iface,
                 session_id);

    mbimcli_async_operation_done (!error);
}

static gboolean
add_link_properties_handle (const gchar        *key,
                            const gchar        *value,
                            GError            **error,
                            AddLinkProperties  *props)
{
    if (g_ascii_strcasecmp (key, "session-id") == 0 && props->session_id == MBIM_DEVICE_SESSION_ID_AUTOMATIC) {
        if (!mbimcli_read_uint_from_string (value, &props->session_id)) {
            g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
                         "invalid session-id given: '%s'", value);
            return FALSE;
        }
        return TRUE;
    }

    if (g_ascii_strcasecmp (key, "iface") == 0 && !props->iface) {
        props->iface = g_strdup (value);
        return TRUE;
    }

    if (g_ascii_strcasecmp (key, "prefix") == 0 && !props->prefix) {
        props->prefix = g_strdup (value);
        return TRUE;
    }

    g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_FAILED,
                 "unrecognized or duplicate option '%s'", key);
    return FALSE;
}

static void
device_link_add (MbimDevice   *dev,
                 GCancellable *cancellable,
                 const gchar  *add_settings)
{
    g_autoptr(GError) error = NULL;
    AddLinkProperties props = {
        .session_id = MBIM_DEVICE_SESSION_ID_AUTOMATIC,
        .iface = NULL,
        .prefix = NULL,
    };

    if (!mbimcli_parse_key_value_string (add_settings,
                                         &error,
                                         (MbimParseKeyValueForeachFn)add_link_properties_handle,
                                         &props)) {
        g_printerr ("error: couldn't parse input add link settings: %s\n",
                    error->message);
        mbimcli_async_operation_done (FALSE);
        return;
    }

    if (!props.iface) {
        g_printerr ("error: missing mandatory 'iface' setting\n");
        mbimcli_async_operation_done (FALSE);
        return;
    }

    if (!props.prefix)
        props.prefix = g_strdup_printf ("%s.", props.iface);

    if ((props.session_id != MBIM_DEVICE_SESSION_ID_AUTOMATIC) &&
        (props.session_id > MBIM_DEVICE_SESSION_ID_MAX)) {
        g_printerr ("error: session id %u out of range [%u,%u]\n",
                    props.session_id, MBIM_DEVICE_SESSION_ID_MIN, MBIM_DEVICE_SESSION_ID_MAX);
        mbimcli_async_operation_done (FALSE);
        return;
    }

    mbim_device_add_link (dev,
                          props.session_id,
                          props.iface,
                          props.prefix,
                          cancellable,
                          (GAsyncReadyCallback)link_add_ready,
                          NULL);

    g_free (props.iface);
    g_free (props.prefix);
}

static void
device_link_list (MbimDevice   *dev,
                  GCancellable *cancellable,
                  const gchar  *iface)
{
    g_autoptr(GError) error = NULL;
    g_autoptr(GPtrArray) links = NULL;

    if (!mbim_device_list_links (dev, iface, &links, &error))
        g_printerr ("error: couldn't list links: %s\n", error->message);
    else {
        guint i;
        guint n_links;

        n_links = (links ? links->len : 0);

        g_print ("[%s] found %u links%s\n",
                 mbim_device_get_path_display (dev),
                 n_links,
                 n_links > 0 ? ":" : "");
        for (i = 0; i < n_links; i++)
            g_print ("  [%u] %s\n", i, (const gchar *) g_ptr_array_index (links, i));
    }

    mbimcli_async_operation_done (!error);
}

/******************************************************************************/
/* Common */

void
mbimcli_link_management_run (MbimDevice   *dev,
                             GCancellable *cancellable)
{
    if (link_list_str)
        device_link_list (dev, cancellable, link_list_str);
    else if (link_add_str)
        device_link_add (dev, cancellable, link_add_str);
    else if (link_delete_str)
        device_link_delete (dev, cancellable, link_delete_str);
    else if (link_delete_all_str)
        device_link_delete_all (dev, cancellable, link_delete_all_str);
    else
      g_warn_if_reached ();
}