| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* qmicli -- Command line interface to control QMI devices |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program 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 |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* Copyright (C) 2019 Aleksander Morgado <[email protected]> |
| 19 |
* Copyright (c) 2022 Qualcomm Innovation Center, Inc. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <locale.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include <glib.h> |
| 30 |
#include <gio/gio.h> |
| 31 |
|
| 32 |
#include <libqmi-glib.h> |
| 33 |
|
| 34 |
#include "qmicli.h" |
| 35 |
#include "qmicli-helpers.h" |
| 36 |
|
| 37 |
#if defined HAVE_QMI_SERVICE_DSD |
| 38 |
|
| 39 |
#undef VALIDATE_MASK_NONE |
| 40 |
#define VALIDATE_MASK_NONE(str) (str ? str : "none") |
| 41 |
|
| 42 |
/* Context */ |
| 43 |
typedef struct { |
| 44 |
QmiDevice *device; |
| 45 |
QmiClientDsd *client; |
| 46 |
GCancellable *cancellable; |
| 47 |
} Context; |
| 48 |
static Context *ctx; |
| 49 |
|
| 50 |
/* Options */ |
| 51 |
static gchar *get_apn_info_str; |
| 52 |
static gchar *set_apn_type_str; |
| 53 |
static gboolean get_system_status_flag; |
| 54 |
static gboolean noop_flag; |
| 55 |
|
| 56 |
static GOptionEntry entries[] = { |
| 57 |
#if defined HAVE_QMI_MESSAGE_DSD_GET_APN_INFO |
| 58 |
{ "dsd-get-apn-info", 0, 0, G_OPTION_ARG_STRING, &get_apn_info_str, |
| 59 |
"Gets the settings associated to a given APN type", |
| 60 |
"[(type)]" |
| 61 |
}, |
| 62 |
#endif |
| 63 |
#if defined HAVE_QMI_MESSAGE_DSD_SET_APN_TYPE |
| 64 |
{ "dsd-set-apn-type", 0, 0, G_OPTION_ARG_STRING, &set_apn_type_str, |
| 65 |
"Sets the types associated to a given APN name", |
| 66 |
"[(name), (type1|type2|type3...)]" |
| 67 |
}, |
| 68 |
#endif |
| 69 |
#if defined HAVE_QMI_MESSAGE_DSD_GET_SYSTEM_STATUS |
| 70 |
{ "dsd-get-system-status", 0, 0, G_OPTION_ARG_NONE, &get_system_status_flag, |
| 71 |
"Gets system status", |
| 72 |
NULL |
| 73 |
}, |
| 74 |
#endif |
| 75 |
{ "dsd-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag, |
| 76 |
"Just allocate or release a DSD client. Use with `--client-no-release-cid' and/or `--client-cid'", |
| 77 |
NULL |
| 78 |
}, |
| 79 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 80 |
}; |
| 81 |
|
| 82 |
GOptionGroup * |
| 83 |
qmicli_dsd_get_option_group (void) |
| 84 |
{ |
| 85 |
GOptionGroup *group; |
| 86 |
|
| 87 |
group = g_option_group_new ("dsd", |
| 88 |
"DSD options:", |
| 89 |
"Show Data System Determination options", |
| 90 |
NULL, |
| 91 |
NULL); |
| 92 |
g_option_group_add_entries (group, entries); |
| 93 |
|
| 94 |
return group; |
| 95 |
} |
| 96 |
|
| 97 |
gboolean |
| 98 |
qmicli_dsd_options_enabled (void) |
| 99 |
{ |
| 100 |
static guint n_actions = 0; |
| 101 |
static gboolean checked = FALSE; |
| 102 |
|
| 103 |
if (checked) |
| 104 |
return !!n_actions; |
| 105 |
|
| 106 |
n_actions = (!!get_apn_info_str + |
| 107 |
!!set_apn_type_str + |
| 108 |
get_system_status_flag + |
| 109 |
noop_flag); |
| 110 |
|
| 111 |
if (n_actions > 1) { |
| 112 |
g_printerr ("error: too many DSD actions requested\n"); |
| 113 |
exit (EXIT_FAILURE); |
| 114 |
} |
| 115 |
|
| 116 |
checked = TRUE; |
| 117 |
return !!n_actions; |
| 118 |
} |
| 119 |
|
| 120 |
static void |
| 121 |
context_free (Context *context) |
| 122 |
{ |
| 123 |
if (!context) |
| 124 |
return; |
| 125 |
|
| 126 |
if (context->cancellable) |
| 127 |
g_object_unref (context->cancellable); |
| 128 |
if (context->device) |
| 129 |
g_object_unref (context->device); |
| 130 |
if (context->client) |
| 131 |
g_object_unref (context->client); |
| 132 |
g_slice_free (Context, context); |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
operation_shutdown (gboolean operation_status) |
| 137 |
{ |
| 138 |
/* Cleanup context and finish async operation */ |
| 139 |
context_free (ctx); |
| 140 |
qmicli_async_operation_done (operation_status, FALSE); |
| 141 |
} |
| 142 |
|
| 143 |
#if defined HAVE_QMI_MESSAGE_DSD_GET_APN_INFO |
| 144 |
|
| 145 |
static void |
| 146 |
get_apn_info_ready (QmiClientDsd *client, |
| 147 |
GAsyncResult *res) |
| 148 |
{ |
| 149 |
QmiMessageDsdGetApnInfoOutput *output; |
| 150 |
GError *error = NULL; |
| 151 |
const gchar *apn_name = NULL; |
| 152 |
|
| 153 |
output = qmi_client_dsd_get_apn_info_finish (client, res, &error); |
| 154 |
if (!output) { |
| 155 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 156 |
g_error_free (error); |
| 157 |
operation_shutdown (FALSE); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if (!qmi_message_dsd_get_apn_info_output_get_result (output, &error)) { |
| 162 |
g_printerr ("error: couldn't get APN info: %s\n", error->message); |
| 163 |
g_error_free (error); |
| 164 |
qmi_message_dsd_get_apn_info_output_unref (output); |
| 165 |
operation_shutdown (FALSE); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
g_print ("APN info found:\n"); |
| 170 |
if (qmi_message_dsd_get_apn_info_output_get_apn_name (output, &apn_name, NULL)) |
| 171 |
g_print ("APN name: %s\n", apn_name); |
| 172 |
else |
| 173 |
g_print ("APN name: n/a\n"); |
| 174 |
|
| 175 |
qmi_message_dsd_get_apn_info_output_unref (output); |
| 176 |
operation_shutdown (TRUE); |
| 177 |
} |
| 178 |
|
| 179 |
static QmiMessageDsdGetApnInfoInput * |
| 180 |
get_apn_info_input_create (const gchar *str) |
| 181 |
{ |
| 182 |
QmiMessageDsdGetApnInfoInput *input = NULL; |
| 183 |
GError *error = NULL; |
| 184 |
QmiDsdApnType apn_type; |
| 185 |
|
| 186 |
if (!qmicli_read_dsd_apn_type_from_string (str, &apn_type)) { |
| 187 |
g_printerr ("error: couldn't parse input string as APN type: '%s'\n", str); |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
|
| 191 |
input = qmi_message_dsd_get_apn_info_input_new (); |
| 192 |
if (!qmi_message_dsd_get_apn_info_input_set_apn_type (input, apn_type, &error)) { |
| 193 |
g_printerr ("error: couldn't create input data bundle: '%s'\n", |
| 194 |
error->message); |
| 195 |
g_error_free (error); |
| 196 |
qmi_message_dsd_get_apn_info_input_unref (input); |
| 197 |
input = NULL; |
| 198 |
} |
| 199 |
|
| 200 |
return input; |
| 201 |
} |
| 202 |
|
| 203 |
#endif /* HAVE_QMI_MESSAGE_DSD_GET_APN_INFO */ |
| 204 |
|
| 205 |
#if defined HAVE_QMI_MESSAGE_DSD_SET_APN_TYPE |
| 206 |
|
| 207 |
static void |
| 208 |
set_apn_type_ready (QmiClientDsd *client, |
| 209 |
GAsyncResult *res) |
| 210 |
{ |
| 211 |
QmiMessageDsdSetApnTypeOutput *output; |
| 212 |
GError *error = NULL; |
| 213 |
|
| 214 |
output = qmi_client_dsd_set_apn_type_finish (client, res, &error); |
| 215 |
if (!output) { |
| 216 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 217 |
g_error_free (error); |
| 218 |
operation_shutdown (FALSE); |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
if (!qmi_message_dsd_set_apn_type_output_get_result (output, &error)) { |
| 223 |
g_printerr ("error: couldn't set APN type: %s\n", error->message); |
| 224 |
g_error_free (error); |
| 225 |
qmi_message_dsd_set_apn_type_output_unref (output); |
| 226 |
operation_shutdown (FALSE); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
g_print ("APN type set\n"); |
| 231 |
|
| 232 |
qmi_message_dsd_set_apn_type_output_unref (output); |
| 233 |
operation_shutdown (TRUE); |
| 234 |
} |
| 235 |
|
| 236 |
static QmiMessageDsdSetApnTypeInput * |
| 237 |
set_apn_type_input_create (const gchar *str) |
| 238 |
{ |
| 239 |
QmiMessageDsdSetApnTypeInput *input = NULL; |
| 240 |
GError *error = NULL; |
| 241 |
QmiDsdApnTypePreference apn_type_preference; |
| 242 |
gchar **split; |
| 243 |
|
| 244 |
split = g_strsplit_set (str, ",", 0); |
| 245 |
if (g_strv_length (split) != 2) { |
| 246 |
g_printerr ("input string requires 2 values, %u given: '%s'\n", g_strv_length (split), str); |
| 247 |
g_strfreev (split); |
| 248 |
return NULL; |
| 249 |
} |
| 250 |
|
| 251 |
g_strstrip (split[0]); |
| 252 |
g_strstrip (split[1]); |
| 253 |
|
| 254 |
if (!qmicli_read_dsd_apn_type_preference_from_string (split[1], &apn_type_preference)) { |
| 255 |
g_printerr ("error: couldn't parse input string as APN type preference mask: '%s'\n", split[1]); |
| 256 |
g_strfreev (split); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
input = qmi_message_dsd_set_apn_type_input_new (); |
| 261 |
if (!qmi_message_dsd_set_apn_type_input_set_apn_type (input, split[0], apn_type_preference, &error)) { |
| 262 |
g_printerr ("error: couldn't create input data bundle: '%s'\n", |
| 263 |
error->message); |
| 264 |
g_error_free (error); |
| 265 |
qmi_message_dsd_set_apn_type_input_unref (input); |
| 266 |
input = NULL; |
| 267 |
} |
| 268 |
|
| 269 |
g_strfreev (split); |
| 270 |
|
| 271 |
return input; |
| 272 |
} |
| 273 |
|
| 274 |
#endif /* HAVE_QMI_MESSAGE_DSD_SET_APN_TYPE */ |
| 275 |
|
| 276 |
#if defined HAVE_QMI_MESSAGE_DSD_GET_SYSTEM_STATUS |
| 277 |
|
| 278 |
static void |
| 279 |
get_system_status_ready (QmiClientDsd *client, |
| 280 |
GAsyncResult *res) |
| 281 |
{ |
| 282 |
QmiMessageDsdGetSystemStatusOutput *output; |
| 283 |
GError *error = NULL; |
| 284 |
GArray *available_systems = NULL; |
| 285 |
guint i; |
| 286 |
|
| 287 |
output = qmi_client_dsd_get_system_status_finish (client, res, &error); |
| 288 |
if (!output) { |
| 289 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 290 |
g_error_free (error); |
| 291 |
operation_shutdown (FALSE); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
if (!qmi_message_dsd_get_system_status_output_get_result (output, &error)) { |
| 296 |
g_printerr ("error: couldn't get system status: %s\n", error->message); |
| 297 |
g_error_free (error); |
| 298 |
qmi_message_dsd_get_system_status_output_unref (output); |
| 299 |
operation_shutdown (FALSE); |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
qmi_message_dsd_get_system_status_output_get_available_systems (output, &available_systems, NULL); |
| 304 |
|
| 305 |
if (!available_systems || !available_systems->len) { |
| 306 |
g_print ("No available data system\n"); |
| 307 |
} else { |
| 308 |
g_print ("Available data systems retrieved:\n"); |
| 309 |
for (i = 0; i < available_systems->len; i++) { |
| 310 |
QmiMessageDsdGetSystemStatusOutputAvailableSystemsSystem *system; |
| 311 |
g_autofree gchar *so_mask_str = NULL; |
| 312 |
|
| 313 |
system = &g_array_index (available_systems, QmiMessageDsdGetSystemStatusOutputAvailableSystemsSystem, i); |
| 314 |
so_mask_str = qmi_dsd_so_mask_build_string_from_mask ((QmiDsdSoMask)system->so_mask); |
| 315 |
g_print ("System [%u]%s:\n" |
| 316 |
"\tNetwork type: '%s'\n" |
| 317 |
"\tRAT: '%s'\n" |
| 318 |
"\tService option: '%s'\n", |
| 319 |
i, |
| 320 |
i > 0 ? "" : " (current preferred)", |
| 321 |
qmi_dsd_data_system_network_type_get_string (system->technology), |
| 322 |
qmi_dsd_radio_access_technology_get_string (system->rat), |
| 323 |
VALIDATE_MASK_NONE (so_mask_str)); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
qmi_message_dsd_get_system_status_output_unref (output); |
| 328 |
operation_shutdown (TRUE); |
| 329 |
} |
| 330 |
|
| 331 |
#endif /* HAVE_QMI_MESSAGE_DSD_GET_SYSTEM_STATUS */ |
| 332 |
|
| 333 |
static gboolean |
| 334 |
noop_cb (gpointer unused) |
| 335 |
{ |
| 336 |
operation_shutdown (TRUE); |
| 337 |
return FALSE; |
| 338 |
} |
| 339 |
|
| 340 |
void |
| 341 |
qmicli_dsd_run (QmiDevice *device, |
| 342 |
QmiClientDsd *client, |
| 343 |
GCancellable *cancellable) |
| 344 |
{ |
| 345 |
/* Initialize context */ |
| 346 |
ctx = g_slice_new (Context); |
| 347 |
ctx->device = g_object_ref (device); |
| 348 |
ctx->client = g_object_ref (client); |
| 349 |
ctx->cancellable = g_object_ref (cancellable); |
| 350 |
|
| 351 |
#if defined HAVE_QMI_MESSAGE_DSD_GET_APN_INFO |
| 352 |
if (get_apn_info_str) { |
| 353 |
QmiMessageDsdGetApnInfoInput *input; |
| 354 |
|
| 355 |
g_debug ("Asynchronously getting APN info..."); |
| 356 |
input = get_apn_info_input_create (get_apn_info_str); |
| 357 |
if (!input) { |
| 358 |
operation_shutdown (FALSE); |
| 359 |
return; |
| 360 |
} |
| 361 |
qmi_client_dsd_get_apn_info (ctx->client, |
| 362 |
input, |
| 363 |
10, |
| 364 |
ctx->cancellable, |
| 365 |
(GAsyncReadyCallback)get_apn_info_ready, |
| 366 |
NULL); |
| 367 |
qmi_message_dsd_get_apn_info_input_unref (input); |
| 368 |
return; |
| 369 |
} |
| 370 |
#endif |
| 371 |
|
| 372 |
#if defined HAVE_QMI_MESSAGE_DSD_SET_APN_TYPE |
| 373 |
if (set_apn_type_str) { |
| 374 |
QmiMessageDsdSetApnTypeInput *input; |
| 375 |
|
| 376 |
g_debug ("Asynchronously setting APN type..."); |
| 377 |
input = set_apn_type_input_create (set_apn_type_str); |
| 378 |
if (!input) { |
| 379 |
operation_shutdown (FALSE); |
| 380 |
return; |
| 381 |
} |
| 382 |
qmi_client_dsd_set_apn_type (ctx->client, |
| 383 |
input, |
| 384 |
10, |
| 385 |
ctx->cancellable, |
| 386 |
(GAsyncReadyCallback)set_apn_type_ready, |
| 387 |
NULL); |
| 388 |
qmi_message_dsd_set_apn_type_input_unref (input); |
| 389 |
return; |
| 390 |
} |
| 391 |
#endif |
| 392 |
|
| 393 |
#if defined HAVE_QMI_MESSAGE_DSD_GET_SYSTEM_STATUS |
| 394 |
if (get_system_status_flag) { |
| 395 |
g_debug ("Asynchronously getting system status..."); |
| 396 |
qmi_client_dsd_get_system_status (ctx->client, |
| 397 |
NULL, |
| 398 |
10, |
| 399 |
ctx->cancellable, |
| 400 |
(GAsyncReadyCallback)get_system_status_ready, |
| 401 |
NULL); |
| 402 |
return; |
| 403 |
} |
| 404 |
#endif |
| 405 |
|
| 406 |
/* Just client allocate/release? */ |
| 407 |
if (noop_flag) { |
| 408 |
g_idle_add (noop_cb, NULL); |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
g_warn_if_reached (); |
| 413 |
} |
| 414 |
|
| 415 |
#endif /* HAVE_QMI_SERVICE_DSD */ |