| 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) 2023 Intel Corporation |
| 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_carrier_lock_str; |
| 33 |
static gboolean query_carrier_lock_flag; |
| 34 |
|
| 35 |
static GOptionEntry entries[] = { |
| 36 |
{ "google-set-carrier-lock", 0, 0, G_OPTION_ARG_STRING, &set_carrier_lock_str, |
| 37 |
"Set Google Carrier Lock", |
| 38 |
"[(Data)]" |
| 39 |
}, |
| 40 |
{ "google-query-carrier-lock", 0, 0, G_OPTION_ARG_NONE, &query_carrier_lock_flag, |
| 41 |
"Query Google Carrier Lock", |
| 42 |
NULL |
| 43 |
}, |
| 44 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 45 |
}; |
| 46 |
|
| 47 |
GOptionGroup * |
| 48 |
mbimcli_google_get_option_group (void) |
| 49 |
{ |
| 50 |
GOptionGroup *group; |
| 51 |
|
| 52 |
group = g_option_group_new ("google", |
| 53 |
"Google options:", |
| 54 |
"Show Google Service options", |
| 55 |
NULL, |
| 56 |
NULL); |
| 57 |
g_option_group_add_entries (group, entries); |
| 58 |
|
| 59 |
return group; |
| 60 |
} |
| 61 |
|
| 62 |
gboolean |
| 63 |
mbimcli_google_options_enabled (void) |
| 64 |
{ |
| 65 |
static guint n_actions = 0; |
| 66 |
static gboolean checked = FALSE; |
| 67 |
|
| 68 |
if (checked) |
| 69 |
return !!n_actions; |
| 70 |
|
| 71 |
n_actions = (!!set_carrier_lock_str + |
| 72 |
query_carrier_lock_flag); |
| 73 |
|
| 74 |
if (n_actions > 1) { |
| 75 |
g_printerr ("error: too many google actions requested\n"); |
| 76 |
exit (EXIT_FAILURE); |
| 77 |
} |
| 78 |
|
| 79 |
checked = TRUE; |
| 80 |
return !!n_actions; |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
context_free (Context *context) |
| 85 |
{ |
| 86 |
if (!context) |
| 87 |
return; |
| 88 |
|
| 89 |
if (context->cancellable) |
| 90 |
g_object_unref (context->cancellable); |
| 91 |
if (context->device) |
| 92 |
g_object_unref (context->device); |
| 93 |
g_slice_free (Context, context); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
shutdown (gboolean operation_status) |
| 98 |
{ |
| 99 |
/* Cleanup context and finish async operation */ |
| 100 |
context_free (ctx); |
| 101 |
mbimcli_async_operation_done (operation_status); |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
set_carrier_lock_ready (MbimDevice *device, |
| 106 |
GAsyncResult *res) |
| 107 |
{ |
| 108 |
g_autoptr(MbimMessage) response = NULL; |
| 109 |
g_autoptr(GError) error = NULL; |
| 110 |
|
| 111 |
response = mbim_device_command_finish (device, res, &error); |
| 112 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 113 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 114 |
shutdown (FALSE); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
g_print ("[%s] Successfully set carrier lock: \n", |
| 119 |
mbim_device_get_path_display (device)); |
| 120 |
|
| 121 |
shutdown (TRUE); |
| 122 |
} |
| 123 |
|
| 124 |
static void |
| 125 |
query_carrier_lock_ready (MbimDevice *device, |
| 126 |
GAsyncResult *res) |
| 127 |
{ |
| 128 |
g_autoptr(MbimMessage) response = NULL; |
| 129 |
g_autoptr(GError) error = NULL; |
| 130 |
const gchar *carrier_lock_status_str; |
| 131 |
const gchar *carrier_lock_modem_state_str; |
| 132 |
const gchar *carrier_lock_cause_str; |
| 133 |
MbimCarrierLockStatus carrier_lock_status; |
| 134 |
MbimCarrierLockModemState carrier_lock_modem_state; |
| 135 |
MbimCarrierLockCause carrier_lock_cause; |
| 136 |
|
| 137 |
response = mbim_device_command_finish (device, res, &error); |
| 138 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 139 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 140 |
shutdown (FALSE); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if (!mbim_message_google_carrier_lock_response_parse ( |
| 145 |
response, |
| 146 |
&carrier_lock_status, |
| 147 |
&carrier_lock_modem_state, |
| 148 |
&carrier_lock_cause, |
| 149 |
&error)) { |
| 150 |
g_printerr ("error: couldn't parse response message: %s\n", error->message); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
carrier_lock_status_str = mbim_carrier_lock_status_get_string (carrier_lock_status); |
| 155 |
carrier_lock_modem_state_str = mbim_carrier_lock_modem_state_get_string (carrier_lock_modem_state); |
| 156 |
carrier_lock_cause_str = mbim_carrier_lock_cause_get_string (carrier_lock_cause); |
| 157 |
|
| 158 |
g_print ("[%s] Successfully queried carrier lock: \n" |
| 159 |
"\t Carrier lock status: '%s'\n" |
| 160 |
"\tCarrier lock modem state: '%s'\n" |
| 161 |
"\t Carrier lock cause: '%s'\n", |
| 162 |
mbim_device_get_path_display (device), |
| 163 |
VALIDATE_UNKNOWN (carrier_lock_status_str), |
| 164 |
VALIDATE_UNKNOWN (carrier_lock_modem_state_str), |
| 165 |
VALIDATE_UNKNOWN (carrier_lock_cause_str)); |
| 166 |
|
| 167 |
shutdown (TRUE); |
| 168 |
} |
| 169 |
|
| 170 |
void |
| 171 |
mbimcli_google_run (MbimDevice *device, |
| 172 |
GCancellable *cancellable) |
| 173 |
{ |
| 174 |
g_autoptr(MbimMessage) request = NULL; |
| 175 |
g_autoptr(GError) error = NULL; |
| 176 |
|
| 177 |
/* Initialize context */ |
| 178 |
ctx = g_slice_new (Context); |
| 179 |
ctx->device = g_object_ref (device); |
| 180 |
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; |
| 181 |
|
| 182 |
/* Request to set carrier lock */ |
| 183 |
if (set_carrier_lock_str) { |
| 184 |
gsize data_size = 0; |
| 185 |
g_autofree guint8 *data = NULL; |
| 186 |
|
| 187 |
data = mbimcli_read_buffer_from_string (set_carrier_lock_str, -1, &data_size, &error); |
| 188 |
if (!data) { |
| 189 |
g_printerr ("Failed to read data: %s\n", error->message); |
| 190 |
shutdown (FALSE); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
g_debug ("Asynchronously setting carrier lock..."); |
| 195 |
request = mbim_message_google_carrier_lock_set_new ((guint32)data_size, data, &error); |
| 196 |
if (!request) { |
| 197 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 198 |
shutdown (FALSE); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
mbim_device_command (ctx->device, |
| 203 |
request, |
| 204 |
10, |
| 205 |
ctx->cancellable, |
| 206 |
(GAsyncReadyCallback)set_carrier_lock_ready, |
| 207 |
NULL); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
/* Query carrier lock information */ |
| 212 |
if (query_carrier_lock_flag) { |
| 213 |
request = mbim_message_google_carrier_lock_query_new (&error); |
| 214 |
if (!request) { |
| 215 |
g_printerr ("error: couldn't create request: %s\n", error->message); |
| 216 |
shutdown (FALSE); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
mbim_device_command (ctx->device, |
| 221 |
request, |
| 222 |
10, |
| 223 |
ctx->cancellable, |
| 224 |
(GAsyncReadyCallback)query_carrier_lock_ready, |
| 225 |
NULL); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
g_warn_if_reached (); |
| 230 |
} |