| 1 |
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* mbimcli -- Command line interface to control MBIM 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) 2022 Intel Corporation |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <locale.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include <glib.h> |
| 29 |
#include <gio/gio.h> |
| 30 |
#include <glib/gprintf.h> |
| 31 |
#include <libmbim-glib.h> |
| 32 |
|
| 33 |
#include "mbim-common.h" |
| 34 |
#include "mbimcli.h" |
| 35 |
#include "mbimcli-helpers.h" |
| 36 |
|
| 37 |
/* Context */ |
| 38 |
typedef struct { |
| 39 |
MbimDevice *device; |
| 40 |
GCancellable *cancellable; |
| 41 |
} Context; |
| 42 |
static Context *ctx; |
| 43 |
|
| 44 |
/* Options */ |
| 45 |
static char *set_trace_config_str; |
| 46 |
static char *query_trace_config_str; |
| 47 |
|
| 48 |
static GOptionEntry entries[] = { |
| 49 |
{ "intel-set-trace-config", 0, 0, G_OPTION_ARG_STRING, &set_trace_config_str, |
| 50 |
"Set trace configuration", |
| 51 |
"[(TraceCmd)|(TraceValue)]" |
| 52 |
}, |
| 53 |
{ "intel-query-trace-config", 0, 0, G_OPTION_ARG_STRING, &query_trace_config_str, |
| 54 |
"Query trace configuration", |
| 55 |
"[(TraceCmd)]" |
| 56 |
}, |
| 57 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 58 |
}; |
| 59 |
|
| 60 |
GOptionGroup * |
| 61 |
mbimcli_intel_tools_get_option_group (void) |
| 62 |
{ |
| 63 |
GOptionGroup *group; |
| 64 |
group = g_option_group_new ("intel-tools", |
| 65 |
"Intel 5G tools options", |
| 66 |
"Show Intel 5G tools options", |
| 67 |
NULL, |
| 68 |
NULL); |
| 69 |
g_option_group_add_entries (group, entries); |
| 70 |
|
| 71 |
return group; |
| 72 |
} |
| 73 |
|
| 74 |
gboolean |
| 75 |
mbimcli_intel_tools_options_enabled (void) |
| 76 |
{ |
| 77 |
static guint n_actions = 0; |
| 78 |
static gboolean checked = FALSE; |
| 79 |
|
| 80 |
if (checked) |
| 81 |
return !!n_actions; |
| 82 |
|
| 83 |
n_actions =(!!set_trace_config_str + |
| 84 |
!!query_trace_config_str); |
| 85 |
|
| 86 |
if (n_actions > 1) { |
| 87 |
g_printerr ("error: too many intel tools 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 |
query_trace_config_ready (MbimDevice *device, |
| 118 |
GAsyncResult *res) |
| 119 |
{ |
| 120 |
g_autoptr(MbimMessage) response = NULL; |
| 121 |
g_autoptr(GError) error = NULL; |
| 122 |
MbimTraceCommand trace_cmd; |
| 123 |
const gchar *trace_cmd_str; |
| 124 |
guint32 trace_result; |
| 125 |
|
| 126 |
response = mbim_device_command_finish (device, res, &error); |
| 127 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 128 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 129 |
shutdown (FALSE); |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
g_print ("[%s] Successfully retrieved trace configuration\n", |
| 134 |
mbim_device_get_path_display (device)); |
| 135 |
if (!mbim_message_intel_tools_trace_config_response_parse ( |
| 136 |
response, |
| 137 |
&trace_cmd, |
| 138 |
&trace_result, |
| 139 |
&error)) { |
| 140 |
g_printerr ("error: couldn't parse response messages: %s\n", error->message); |
| 141 |
shutdown (FALSE); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
trace_cmd_str = mbim_trace_command_get_string (trace_cmd); |
| 146 |
|
| 147 |
g_print ("[%s] Trace configuration retrieved:\n" |
| 148 |
"\t Trace Command: '%s'\n" |
| 149 |
"\t Trace Result: '%u'\n", |
| 150 |
mbim_device_get_path_display (device), |
| 151 |
VALIDATE_UNKNOWN(trace_cmd_str), |
| 152 |
trace_result); |
| 153 |
|
| 154 |
shutdown (TRUE); |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
set_trace_config_ready (MbimDevice *device, |
| 159 |
GAsyncResult *res) |
| 160 |
{ |
| 161 |
g_autoptr(MbimMessage) response = NULL; |
| 162 |
g_autoptr(GError) error = NULL; |
| 163 |
|
| 164 |
response = mbim_device_command_finish (device, res, &error); |
| 165 |
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) { |
| 166 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 167 |
shutdown (FALSE); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
g_print ("[%s] Successfully retrieved trace configuration\n", |
| 172 |
mbim_device_get_path_display (device)); |
| 173 |
|
| 174 |
shutdown (TRUE); |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
mbimcli_intel_tools_run (MbimDevice *device, |
| 179 |
GCancellable *cancellable) |
| 180 |
{ |
| 181 |
g_autoptr(MbimMessage) request = NULL; |
| 182 |
g_autoptr(GError) error = NULL; |
| 183 |
|
| 184 |
/* Initialize context */ |
| 185 |
ctx = g_slice_new (Context); |
| 186 |
ctx->device = g_object_ref (device); |
| 187 |
ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL; |
| 188 |
|
| 189 |
/* Request to set trace config? */ |
| 190 |
if (set_trace_config_str) { |
| 191 |
g_auto(GStrv) split = NULL; |
| 192 |
MbimTraceCommand trace_command; |
| 193 |
guint32 trace_value = 0; |
| 194 |
|
| 195 |
split = g_strsplit (set_trace_config_str, ",", -1); |
| 196 |
|
| 197 |
if (g_strv_length (split) > 2) { |
| 198 |
g_printerr ("error: couldn't parse input string, too many arguments\n"); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
if (g_strv_length (split) < 2) { |
| 203 |
g_printerr ("error: couldn't parse input string, missing arguments\n"); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
g_return_if_fail (split[0] && split[1]); |
| 208 |
|
| 209 |
if (!mbimcli_read_trace_command_from_string (split[0], &trace_command, &error)) { |
| 210 |
g_printerr ("error: couldn't parse input string: %s\n", error->message); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
if (!mbimcli_read_uint_from_string (split[1], &trace_value)) { |
| 215 |
g_printerr ("error: couldn't parse input string, invalid trace value '%s'\n", split[1]); |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
g_debug ("Asynchronously setting trace info..."); |
| 220 |
request = mbim_message_intel_tools_trace_config_set_new (trace_command, trace_value, NULL); |
| 221 |
mbim_device_command (ctx->device, |
| 222 |
request, |
| 223 |
10, |
| 224 |
ctx->cancellable, |
| 225 |
(GAsyncReadyCallback)set_trace_config_ready, |
| 226 |
NULL); |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
/* Request to get trace config? */ |
| 231 |
if (query_trace_config_str) { |
| 232 |
MbimTraceCommand trace_command; |
| 233 |
|
| 234 |
if (!mbimcli_read_trace_command_from_string (query_trace_config_str, &trace_command, &error)) { |
| 235 |
g_printerr ("error: couldn't parse input string: %s\n", error->message); |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
g_debug ("Asynchronously querying trace info..."); |
| 240 |
request = mbim_message_intel_tools_trace_config_query_new (trace_command, NULL); |
| 241 |
mbim_device_command (ctx->device, |
| 242 |
request, |
| 243 |
10, |
| 244 |
ctx->cancellable, |
| 245 |
(GAsyncReadyCallback)query_trace_config_ready, |
| 246 |
NULL); |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
g_warn_if_reached (); |
| 251 |
} |