| 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) 2014 NVIDIA CORPORATION |
| 7 |
* Copyright (C) 2014 Aleksander Morgado <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "config.h" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <locale.h> |
| 15 |
#include <string.h> |
| 16 |
#include <errno.h> |
| 17 |
|
| 18 |
#include <glib.h> |
| 19 |
#include <gio/gio.h> |
| 20 |
|
| 21 |
#include <libmbim-glib.h> |
| 22 |
|
| 23 |
#include "mbimcli.h" |
| 24 |
#include "mbimcli-helpers.h" |
| 25 |
|
| 26 |
/* Context */ |
| 27 |
typedef struct { |
| 28 |
MbimDevice *device; |
| 29 |
GCancellable *cancellable; |
| 30 |
guint32 session_id; |
| 31 |
} Context; |
| 32 |
static Context *ctx; |
| 33 |
|
| 34 |
/* Options */ |
| 35 |
static gchar *connect_str; |
| 36 |
static gchar *disconnect_str; |
| 37 |
|
| 38 |
static GOptionEntry entries[] = { |
| 39 |
{ "dss-connect", 0, 0, G_OPTION_ARG_STRING, &connect_str, |
| 40 |
"Connect DSS session", |
| 41 |
"[(UUID),(Session ID)]" |
| 42 |
}, |
| 43 |
{ "dss-disconnect", 0, 0, G_OPTION_ARG_STRING, &disconnect_str, |
| 44 |
"Disconnect DSS session", |
| 45 |
"[(UUID),(Session ID)]" |
| 46 |
}, |
| 47 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 48 |
}; |
| 49 |
|
| 50 |
GOptionGroup * |
| 51 |
mbimcli_dss_get_option_group (void) |
| 52 |
{ |
| 53 |
GOptionGroup *group; |
| 54 |
|
| 55 |
group = g_option_group_new ("dss", |
| 56 |
"Device Service Stream options:", |
| 57 |
"Show Device Service Stream options", |
| 58 |
NULL, |
| 59 |
NULL); |
| 60 |
g_option_group_add_entries (group, entries); |
| 61 |
|
| 62 |
return group; |
| 63 |
} |
| 64 |
|
| 65 |
gboolean |
| 66 |
mbimcli_dss_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 = (!!connect_str + |
| 75 |
!!disconnect_str ); |
| 76 |
|
| 77 |
if (n_actions > 1) { |
| 78 |
g_printerr ("error: too many DSS actions requested\n"); |
| 79 |
exit (EXIT_FAILURE); |
| 80 |
} |
| 81 |
|
| 82 |
checked = TRUE; |
| 83 |
return !!n_actions; |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
context_free (Context *context) |
| 88 |
{ |
| 89 |
if (!context) |
| 90 |
return; |
| 91 |
|
| 92 |
if (context->cancellable) |
| 93 |
g_object_unref (context->cancellable); |
| 94 |
if (context->device) |
| 95 |
g_object_unref (context->device); |
| 96 |
g_slice_free (Context, context); |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
shutdown (gboolean operation_status) |
| 101 |
{ |
| 102 |
/* Cleanup context and finish async operation */ |
| 103 |
context_free (ctx); |
| 104 |
mbimcli_async_operation_done (operation_status); |
| 105 |
} |
| 106 |
|
| 107 |
enum { |
| 108 |
CONNECT, |
| 109 |
DISCONNECT |
| 110 |
}; |
| 111 |
|
| 112 |
static void |
| 113 |
ip_configuration_query_ready (MbimDevice *device, |
| 114 |
GAsyncResult *res) |
| 115 |
{ |
| 116 |
g_autoptr(MbimMessage) response = NULL; |
| 117 |
g_autoptr(GError) error = NULL; |
| 118 |
gboolean success = FALSE; |
| 119 |
|
| 120 |
response = mbim_device_command_finish (device, res, &error); |
| 121 |
if (!response || |
| 122 |
!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 123 |
g_printerr ("error: couldn't get IP configuration response message: %s\n", error->message); |
| 124 |
} else { |
| 125 |
success = mbimcli_print_ip_config (device, response, &error); |
| 126 |
if (!success) |
| 127 |
g_printerr ("error: couldn't parse IP configuration response message: %s\n", error->message); |
| 128 |
} |
| 129 |
|
| 130 |
shutdown (success); |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
set_dss_ready (MbimDevice *device, |
| 135 |
GAsyncResult *res, |
| 136 |
gpointer user_data) |
| 137 |
{ |
| 138 |
g_autoptr(MbimMessage) response = NULL; |
| 139 |
g_autoptr(MbimMessage) message = NULL; |
| 140 |
g_autoptr(GError) error = NULL; |
| 141 |
|
| 142 |
response = mbim_device_command_finish (device, res, &error); |
| 143 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 144 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 145 |
shutdown (FALSE); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if (!mbim_message_dss_connect_response_parse (response, &error)) { |
| 150 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 151 |
shutdown (FALSE); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
if (GPOINTER_TO_UINT (user_data) == DISCONNECT) { |
| 156 |
g_print ("[%s] Successfully disconnected\n", mbim_device_get_path_display (device)); |
| 157 |
shutdown (TRUE); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
g_assert (GPOINTER_TO_UINT (user_data) == CONNECT); |
| 162 |
|
| 163 |
g_print ("[%s] Successfully connected\n", mbim_device_get_path_display (device)); |
| 164 |
|
| 165 |
message = (mbim_message_ip_configuration_query_new ( |
| 166 |
ctx->session_id, |
| 167 |
MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv4configurationavailable */ |
| 168 |
MBIM_IP_CONFIGURATION_AVAILABLE_FLAG_NONE, /* ipv6configurationavailable */ |
| 169 |
0, /* ipv4addresscount */ |
| 170 |
NULL, /* ipv4address */ |
| 171 |
0, /* ipv6addresscount */ |
| 172 |
NULL, /* ipv6address */ |
| 173 |
NULL, /* ipv4gateway */ |
| 174 |
NULL, /* ipv6gateway */ |
| 175 |
0, /* ipv4dnsservercount */ |
| 176 |
NULL, /* ipv4dnsserver */ |
| 177 |
0, /* ipv6dnsservercount */ |
| 178 |
NULL, /* ipv6dnsserver */ |
| 179 |
0, /* ipv4mtu */ |
| 180 |
0, /* ipv6mtu */ |
| 181 |
&error)); |
| 182 |
if (!message) { |
| 183 |
g_printerr ("error: couldn't create IP config request: %s\n", error->message); |
| 184 |
shutdown (FALSE); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
mbim_device_command (device, |
| 189 |
message, |
| 190 |
60, |
| 191 |
NULL, |
| 192 |
(GAsyncReadyCallback)ip_configuration_query_ready, |
| 193 |
NULL); |
| 194 |
} |
| 195 |
|
| 196 |
static gboolean |
| 197 |
common_parse (const gchar *str, |
| 198 |
MbimUuid *dsid, |
| 199 |
guint32 *ssid) |
| 200 |
{ |
| 201 |
g_auto(GStrv) split = NULL; |
| 202 |
gboolean status = FALSE; |
| 203 |
|
| 204 |
g_assert (dsid != NULL); |
| 205 |
g_assert (ssid != NULL); |
| 206 |
|
| 207 |
/* Format of the string is: |
| 208 |
* [(UUID),(Session ID)] |
| 209 |
*/ |
| 210 |
split = g_strsplit (str, ",", -1); |
| 211 |
|
| 212 |
if (g_strv_length (split) > 2) |
| 213 |
g_printerr ("error: couldn't parse input string, too many arguments\n"); |
| 214 |
else if (g_strv_length (split) < 1) |
| 215 |
g_printerr ("error: couldn't parse input string, missing arguments\n"); |
| 216 |
else if (!mbim_uuid_from_printable (split[0], dsid)) |
| 217 |
g_printerr ("error: couldn't parse UUID, should be xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n"); |
| 218 |
else if (!mbimcli_read_uint_from_string (split[1], ssid)) |
| 219 |
g_printerr ("error: couldn't parse Session ID, should be a number\n"); |
| 220 |
else |
| 221 |
status = TRUE; |
| 222 |
|
| 223 |
return status; |
| 224 |
} |
| 225 |
|
| 226 |
void |
| 227 |
mbimcli_dss_run (MbimDevice *device, |
| 228 |
GCancellable *cancellable) |
| 229 |
{ |
| 230 |
g_autoptr(MbimMessage) request = NULL; |
| 231 |
g_autoptr(GError) error = NULL; |
| 232 |
|
| 233 |
/* Initialize context */ |
| 234 |
ctx = g_slice_new (Context); |
| 235 |
ctx->device = g_object_ref (device); |
| 236 |
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; |
| 237 |
|
| 238 |
/* Connect? */ |
| 239 |
if (connect_str) { |
| 240 |
MbimUuid service_id; |
| 241 |
|
| 242 |
if (!common_parse (connect_str, &service_id, &ctx->session_id)) { |
| 243 |
shutdown (FALSE); |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
request = mbim_message_dss_connect_set_new (&service_id, |
| 248 |
ctx->session_id, |
| 249 |
MBIM_DSS_LINK_STATE_ACTIVATE, |
| 250 |
&error); |
| 251 |
|
| 252 |
if (!request) { |
| 253 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 254 |
shutdown (FALSE); |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
mbim_device_command (ctx->device, |
| 259 |
request, |
| 260 |
10, |
| 261 |
ctx->cancellable, |
| 262 |
(GAsyncReadyCallback)set_dss_ready, |
| 263 |
GUINT_TO_POINTER (CONNECT)); |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
/* Disconnect? */ |
| 268 |
if (disconnect_str) { |
| 269 |
MbimUuid service_id; |
| 270 |
guint32 session_id; |
| 271 |
|
| 272 |
if (!common_parse (disconnect_str, &service_id, &session_id)) { |
| 273 |
shutdown (FALSE); |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
request = mbim_message_dss_connect_set_new (&service_id, |
| 278 |
session_id, |
| 279 |
MBIM_DSS_LINK_STATE_DEACTIVATE, |
| 280 |
&error); |
| 281 |
if (!request) { |
| 282 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 283 |
shutdown (FALSE); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
mbim_device_command (ctx->device, |
| 288 |
request, |
| 289 |
10, |
| 290 |
ctx->cancellable, |
| 291 |
(GAsyncReadyCallback)set_dss_ready, |
| 292 |
GUINT_TO_POINTER (DISCONNECT)); |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
g_warn_if_reached (); |
| 297 |
} |