| 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) 2020 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 <gio/gio.h> |
| 18 |
|
| 19 |
#include <libmbim-glib.h> |
| 20 |
|
| 21 |
#include "mbimcli.h" |
| 22 |
#include "mbimcli-helpers.h" |
| 23 |
|
| 24 |
/* Context */ |
| 25 |
typedef struct { |
| 26 |
MbimDevice *device; |
| 27 |
GCancellable *cancellable; |
| 28 |
} Context; |
| 29 |
static Context *ctx; |
| 30 |
|
| 31 |
/* Options */ |
| 32 |
static gchar *set_sar_config_str; |
| 33 |
static gboolean query_sar_config_flag; |
| 34 |
static gchar *set_transmission_status_str; |
| 35 |
static gboolean query_transmission_status_flag; |
| 36 |
|
| 37 |
static GOptionEntry entries[] = { |
| 38 |
{ "ms-set-sar-config", 0, 0, G_OPTION_ARG_STRING, &set_sar_config_str, |
| 39 |
"Set SAR config", |
| 40 |
"[(device|os),(enabled|disabled)[,[{antenna_index,backoff_index}...]]]" |
| 41 |
}, |
| 42 |
{ "ms-query-sar-config", 0, 0, G_OPTION_ARG_NONE, &query_sar_config_flag, |
| 43 |
"Query SAR config", |
| 44 |
NULL |
| 45 |
}, |
| 46 |
{ "ms-set-transmission-status", 0, 0, G_OPTION_ARG_STRING, &set_transmission_status_str, |
| 47 |
"Set transmission status and hysteresis timer (in seconds)", |
| 48 |
"[(enabled|disabled),(timer)]" |
| 49 |
}, |
| 50 |
{ "ms-query-transmission-status", 0, 0, G_OPTION_ARG_NONE, &query_transmission_status_flag, |
| 51 |
"Query transmission status", |
| 52 |
NULL |
| 53 |
}, |
| 54 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 55 |
}; |
| 56 |
|
| 57 |
GOptionGroup * |
| 58 |
mbimcli_ms_sar_get_option_group (void) |
| 59 |
{ |
| 60 |
GOptionGroup *group; |
| 61 |
|
| 62 |
group = g_option_group_new ("ms-sar", |
| 63 |
"Microsoft SAR options:", |
| 64 |
"Show Microsoft SAR Service options", |
| 65 |
NULL, |
| 66 |
NULL); |
| 67 |
g_option_group_add_entries (group, entries); |
| 68 |
|
| 69 |
return group; |
| 70 |
} |
| 71 |
|
| 72 |
gboolean |
| 73 |
mbimcli_ms_sar_options_enabled (void) |
| 74 |
{ |
| 75 |
static guint n_actions = 0; |
| 76 |
static gboolean checked = FALSE; |
| 77 |
|
| 78 |
if (checked) |
| 79 |
return !!n_actions; |
| 80 |
|
| 81 |
n_actions = !!set_sar_config_str + |
| 82 |
query_sar_config_flag + |
| 83 |
!!set_transmission_status_str + |
| 84 |
query_transmission_status_flag; |
| 85 |
|
| 86 |
if (n_actions > 1) { |
| 87 |
g_printerr ("error: too many Microsoft SAR actions requested\n"); |
| 88 |
exit (EXIT_FAILURE); |
| 89 |
} |
| 90 |
|
| 91 |
checked = TRUE; |
| 92 |
return !!n_actions; |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
context_free (Context *context) |
| 97 |
{ |
| 98 |
if (!context) |
| 99 |
return; |
| 100 |
|
| 101 |
if (context->cancellable) |
| 102 |
g_object_unref (context->cancellable); |
| 103 |
if (context->device) |
| 104 |
g_object_unref (context->device); |
| 105 |
g_slice_free (Context, context); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
shutdown (gboolean operation_status) |
| 110 |
{ |
| 111 |
/* Cleanup context and finish async operation */ |
| 112 |
context_free (ctx); |
| 113 |
mbimcli_async_operation_done (operation_status); |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
ms_sar_ready (MbimDevice *device, |
| 118 |
GAsyncResult *res) |
| 119 |
{ |
| 120 |
g_autoptr(MbimMessage) response = NULL; |
| 121 |
g_autoptr(GError) error = NULL; |
| 122 |
MbimSarControlMode mode; |
| 123 |
const gchar *mode_str; |
| 124 |
MbimSarBackoffState backoff_state; |
| 125 |
const gchar *backoff_state_str; |
| 126 |
MbimSarWifiHardwareState wifi_integration; |
| 127 |
const gchar *wifi_integration_str; |
| 128 |
guint32 config_states_count; |
| 129 |
g_autoptr(MbimSarConfigStateArray) config_states = NULL; |
| 130 |
guint32 i; |
| 131 |
|
| 132 |
response = mbim_device_command_finish (device, res, &error); |
| 133 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 134 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 135 |
shutdown (FALSE); |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if (!mbim_message_ms_sar_config_response_parse ( |
| 140 |
response, |
| 141 |
&mode, |
| 142 |
&backoff_state, |
| 143 |
&wifi_integration, |
| 144 |
&config_states_count, |
| 145 |
&config_states, |
| 146 |
&error)) { |
| 147 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 148 |
shutdown (FALSE); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
mode_str = mbim_sar_control_mode_get_string (mode); |
| 153 |
backoff_state_str = mbim_sar_backoff_state_get_string (backoff_state); |
| 154 |
wifi_integration_str = mbim_sar_wifi_hardware_state_get_string (wifi_integration); |
| 155 |
|
| 156 |
g_print ("[%s] SAR config:\n" |
| 157 |
"\t Mode: %s\n" |
| 158 |
"\t Backoff state: %s\n" |
| 159 |
"\tWi-Fi hardware state: %s\n" |
| 160 |
"\t Config states: %u\n", |
| 161 |
mbim_device_get_path_display (device), |
| 162 |
VALIDATE_UNKNOWN (mode_str), |
| 163 |
VALIDATE_UNKNOWN (backoff_state_str), |
| 164 |
VALIDATE_UNKNOWN (wifi_integration_str), |
| 165 |
config_states_count); |
| 166 |
|
| 167 |
|
| 168 |
for (i = 0; i < config_states_count; i++) { |
| 169 |
g_print ("\t\t[%u]\n", i); |
| 170 |
if (config_states[i]->antenna_index == 0xFFFFFFFF) |
| 171 |
g_print ("\t\t Antenna index: all\n"); |
| 172 |
else |
| 173 |
g_print ("\t\t Antenna index: %u\n", config_states[i]->antenna_index); |
| 174 |
g_print ("\t\t Backoff index: %u\n", config_states[i]->backoff_index); |
| 175 |
} |
| 176 |
|
| 177 |
shutdown (TRUE); |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
modem_transmission_status_ready (MbimDevice *device, |
| 182 |
GAsyncResult *res) |
| 183 |
{ |
| 184 |
g_autoptr(MbimMessage) response = NULL; |
| 185 |
g_autoptr(GError) error = NULL; |
| 186 |
MbimTransmissionNotificationStatus channel_notification; |
| 187 |
const gchar *channel_notification_str; |
| 188 |
MbimTransmissionState transmission_status; |
| 189 |
const gchar *transmission_status_str; |
| 190 |
guint32 hysteresis_timer; |
| 191 |
|
| 192 |
response = mbim_device_command_finish (device, res, &error); |
| 193 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 194 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 195 |
shutdown (FALSE); |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
if (!mbim_message_ms_sar_transmission_status_response_parse ( |
| 200 |
response, |
| 201 |
&channel_notification, |
| 202 |
&transmission_status, |
| 203 |
&hysteresis_timer, |
| 204 |
&error)) { |
| 205 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 206 |
shutdown (FALSE); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
channel_notification_str = mbim_transmission_notification_status_get_string (channel_notification); |
| 211 |
transmission_status_str = mbim_transmission_state_get_string (transmission_status); |
| 212 |
|
| 213 |
g_print ("[%s] Transmission status:\n" |
| 214 |
"\t notification: %s\n" |
| 215 |
"\t status: %s\n" |
| 216 |
"\t hysteresis timer: (%u)\n", |
| 217 |
mbim_device_get_path_display (device), |
| 218 |
VALIDATE_UNKNOWN (channel_notification_str), |
| 219 |
VALIDATE_UNKNOWN (transmission_status_str), |
| 220 |
hysteresis_timer); |
| 221 |
|
| 222 |
shutdown (TRUE); |
| 223 |
} |
| 224 |
|
| 225 |
static gboolean |
| 226 |
sar_config_input_parse (const gchar *str, |
| 227 |
MbimSarControlMode *mode, |
| 228 |
MbimSarBackoffState *state, |
| 229 |
GPtrArray **states_array, |
| 230 |
GError **error) |
| 231 |
{ |
| 232 |
g_auto(GStrv) split = NULL; |
| 233 |
|
| 234 |
g_assert (mode != NULL); |
| 235 |
g_assert (state != NULL); |
| 236 |
g_assert (states_array!= NULL); |
| 237 |
|
| 238 |
/* Format of the string is: |
| 239 |
* "(mode:device or os),(state: enabled or disabled)[,[{antenna_index,backoff_index}...]]" |
| 240 |
* i.e. array of {antenna_index,backoff_index} is optional |
| 241 |
*/ |
| 242 |
split = g_strsplit (str, ",", 3); |
| 243 |
|
| 244 |
if (g_strv_length (split) < 2) { |
| 245 |
g_set_error_literal (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS, |
| 246 |
"couldn't parse input string, missing arguments"); |
| 247 |
return FALSE; |
| 248 |
} |
| 249 |
|
| 250 |
if (!mbimcli_read_sar_control_mode_from_string (split[0], mode, error)) { |
| 251 |
return FALSE; |
| 252 |
} |
| 253 |
|
| 254 |
if (!mbimcli_read_sar_backoff_state_from_string (split[1], state, error)) { |
| 255 |
return FALSE; |
| 256 |
} |
| 257 |
|
| 258 |
/* Check whether we have the optional item array: [{antenna_index,backoff_index}...] */ |
| 259 |
if (split[2]) { |
| 260 |
const gchar *array_begin; |
| 261 |
const gchar *array_end; |
| 262 |
g_autofree gchar *array_str = NULL; |
| 263 |
|
| 264 |
array_begin = strchr (split[2], '['); |
| 265 |
array_end = strchr (split[2], ']'); |
| 266 |
|
| 267 |
if (!array_begin || !array_end || (array_begin > array_end)) { |
| 268 |
g_set_error (error, MBIM_CORE_ERROR, MBIM_CORE_ERROR_INVALID_ARGS, |
| 269 |
"invalid SAR config state array: '%s'", split[2]); |
| 270 |
return FALSE; |
| 271 |
} |
| 272 |
|
| 273 |
array_str = g_strndup (&array_begin[1], array_end - array_begin - 1); |
| 274 |
if (!mbimcli_parse_sar_config_state_array (array_str, states_array, error)) { |
| 275 |
return FALSE; |
| 276 |
} |
| 277 |
} else |
| 278 |
*states_array = NULL; |
| 279 |
|
| 280 |
return TRUE; |
| 281 |
} |
| 282 |
|
| 283 |
static gboolean |
| 284 |
transmission_status_input_parse (const gchar *str, |
| 285 |
MbimTransmissionNotificationStatus *notification, |
| 286 |
guint *hysteresis_timer) |
| 287 |
{ |
| 288 |
g_auto(GStrv) split = NULL; |
| 289 |
|
| 290 |
g_assert (notification != NULL); |
| 291 |
g_assert (hysteresis_timer != NULL); |
| 292 |
|
| 293 |
/* Format of the string is: |
| 294 |
* "(notification: enabled or disabled),(seconds: 1~5)" |
| 295 |
*/ |
| 296 |
split = g_strsplit (str, ",", -1); |
| 297 |
|
| 298 |
if (g_strv_length (split) < 2) { |
| 299 |
g_printerr ("error: couldn't parse input string, missing arguments\n"); |
| 300 |
return FALSE; |
| 301 |
} |
| 302 |
|
| 303 |
if (g_ascii_strcasecmp (split[0], "disabled") == 0) { |
| 304 |
*notification = MBIM_TRANSMISSION_NOTIFICATION_STATUS_DISABLED; |
| 305 |
} else if (g_ascii_strcasecmp (split[0], "enabled") == 0) { |
| 306 |
*notification = MBIM_TRANSMISSION_NOTIFICATION_STATUS_ENABLED; |
| 307 |
} else { |
| 308 |
g_printerr ("error: invalid state: '%s', it must be enabled or disabled\n", split[0]); |
| 309 |
return FALSE; |
| 310 |
} |
| 311 |
|
| 312 |
if (!mbimcli_read_uint_from_string (split[1], hysteresis_timer)) { |
| 313 |
g_printerr ("error: couldn't parse input string, invalid seconds '%s'\n", split[1]); |
| 314 |
return FALSE; |
| 315 |
} |
| 316 |
|
| 317 |
if (*hysteresis_timer < 1 || *hysteresis_timer > 5) { |
| 318 |
g_printerr ("error: the seconds of hysteresis_timer is %u, it must in range [1,5]\n", *hysteresis_timer); |
| 319 |
return FALSE; |
| 320 |
} |
| 321 |
|
| 322 |
return TRUE; |
| 323 |
} |
| 324 |
|
| 325 |
void |
| 326 |
mbimcli_ms_sar_run (MbimDevice *device, |
| 327 |
GCancellable *cancellable) |
| 328 |
{ |
| 329 |
g_autoptr(MbimMessage) request = NULL; |
| 330 |
g_autoptr(GError) error = NULL; |
| 331 |
|
| 332 |
/* Initialize context */ |
| 333 |
ctx = g_slice_new (Context); |
| 334 |
ctx->device = g_object_ref (device); |
| 335 |
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; |
| 336 |
|
| 337 |
/* Request to set SAR config */ |
| 338 |
if (set_sar_config_str) { |
| 339 |
g_autoptr(GPtrArray) states_array = NULL; |
| 340 |
MbimSarControlMode mode; |
| 341 |
MbimSarBackoffState state; |
| 342 |
|
| 343 |
g_debug ("Asynchronously setting SAR config"); |
| 344 |
if (!sar_config_input_parse (set_sar_config_str, &mode, &state, &states_array, &error)) { |
| 345 |
g_printerr ("error: couldn't parse SAR config: %s\n", error->message); |
| 346 |
shutdown (FALSE); |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
request = mbim_message_ms_sar_config_set_new (mode, |
| 351 |
state, |
| 352 |
states_array ? states_array->len : 0, |
| 353 |
states_array ? (const MbimSarConfigState **)states_array->pdata : NULL, |
| 354 |
NULL); |
| 355 |
mbim_device_command (ctx->device, |
| 356 |
request, |
| 357 |
10, |
| 358 |
ctx->cancellable, |
| 359 |
(GAsyncReadyCallback)ms_sar_ready, |
| 360 |
NULL); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
/* Request to querying SAR config */ |
| 365 |
if (query_sar_config_flag) { |
| 366 |
g_debug ("Asynchronously querying SAR config..."); |
| 367 |
request = (mbim_message_ms_sar_config_query_new (NULL)); |
| 368 |
mbim_device_command (ctx->device, |
| 369 |
request, |
| 370 |
10, |
| 371 |
ctx->cancellable, |
| 372 |
(GAsyncReadyCallback)ms_sar_ready, |
| 373 |
NULL); |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
/* Request to set transmission status */ |
| 378 |
if (set_transmission_status_str) { |
| 379 |
MbimTransmissionNotificationStatus notification = MBIM_TRANSMISSION_NOTIFICATION_STATUS_DISABLED; |
| 380 |
guint32 hysteresis_timer = 0; |
| 381 |
g_debug ("Asynchronously set transmission status"); |
| 382 |
|
| 383 |
if (!transmission_status_input_parse (set_transmission_status_str, |
| 384 |
¬ification, |
| 385 |
&hysteresis_timer)) { |
| 386 |
shutdown (FALSE); |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
request = mbim_message_ms_sar_transmission_status_set_new (notification, hysteresis_timer, NULL); |
| 391 |
mbim_device_command (ctx->device, |
| 392 |
request, |
| 393 |
10, |
| 394 |
ctx->cancellable, |
| 395 |
(GAsyncReadyCallback)modem_transmission_status_ready, |
| 396 |
NULL); |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
/* Request to query transmission status */ |
| 401 |
if (query_transmission_status_flag) { |
| 402 |
g_debug ("Asynchronously query transmission status"); |
| 403 |
request = mbim_message_ms_sar_transmission_status_query_new (NULL); |
| 404 |
mbim_device_command (ctx->device, |
| 405 |
request, |
| 406 |
10, |
| 407 |
ctx->cancellable, |
| 408 |
(GAsyncReadyCallback)modem_transmission_status_ready, |
| 409 |
NULL); |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
g_warn_if_reached (); |
| 414 |
} |