| 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) 2023 Daniele Palmas <[email protected]> |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <locale.h> |
| 26 |
#include <string.h> |
| 27 |
#include <assert.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_ATR |
| 38 |
|
| 39 |
/* Context */ |
| 40 |
typedef struct { |
| 41 |
QmiDevice *device; |
| 42 |
QmiClientAtr *client; |
| 43 |
GCancellable *cancellable; |
| 44 |
guint timeout_id; |
| 45 |
guint received_indication_id; |
| 46 |
gboolean monitor; |
| 47 |
} Context; |
| 48 |
static Context *ctx; |
| 49 |
|
| 50 |
/* Options */ |
| 51 |
static gchar *send_str; |
| 52 |
static gchar *send_only_str; |
| 53 |
static gboolean monitor_indications_flag; |
| 54 |
static gboolean noop_flag; |
| 55 |
|
| 56 |
static GOptionEntry entries[] = { |
| 57 |
#if defined HAVE_QMI_MESSAGE_ATR_SEND && defined HAVE_QMI_INDICATION_ATR_RECEIVED |
| 58 |
{ "atr-send", 0, 0, G_OPTION_ARG_STRING, &send_str, |
| 59 |
"Send an AT command and wait for the reply", |
| 60 |
"[AT command]" |
| 61 |
}, |
| 62 |
#endif |
| 63 |
#if defined HAVE_QMI_MESSAGE_ATR_SEND |
| 64 |
{ "atr-send-only", 0, 0, G_OPTION_ARG_STRING, &send_only_str, |
| 65 |
"Send an AT command without waiting for the reply", |
| 66 |
"[AT command]" |
| 67 |
}, |
| 68 |
#endif |
| 69 |
#if defined HAVE_QMI_MESSAGE_ATR_SEND && defined HAVE_QMI_INDICATION_ATR_RECEIVED |
| 70 |
{ "atr-monitor", 0, 0, G_OPTION_ARG_NONE, &monitor_indications_flag, |
| 71 |
"Watch for unsolicited indications", |
| 72 |
NULL |
| 73 |
}, |
| 74 |
#endif |
| 75 |
{ "atr-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag, |
| 76 |
"Just allocate or release an ATR 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_atr_get_option_group (void) |
| 84 |
{ |
| 85 |
GOptionGroup *group; |
| 86 |
|
| 87 |
group = g_option_group_new ("atr", |
| 88 |
"ATR options:", |
| 89 |
"Show AT Relay Service options", |
| 90 |
NULL, |
| 91 |
NULL); |
| 92 |
g_option_group_add_entries (group, entries); |
| 93 |
|
| 94 |
return group; |
| 95 |
} |
| 96 |
|
| 97 |
gboolean |
| 98 |
qmicli_atr_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 = (!!send_str + |
| 107 |
!!send_only_str + |
| 108 |
monitor_indications_flag + |
| 109 |
noop_flag); |
| 110 |
|
| 111 |
if (n_actions > 1) { |
| 112 |
g_printerr ("error: too many ATR actions requested\n"); |
| 113 |
exit (EXIT_FAILURE); |
| 114 |
} |
| 115 |
|
| 116 |
/* Actions that require receiving QMI indication messages must specify that |
| 117 |
* indications are expected. */ |
| 118 |
if (!!send_str || monitor_indications_flag) |
| 119 |
qmicli_expect_indications (); |
| 120 |
|
| 121 |
checked = TRUE; |
| 122 |
return !!n_actions; |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
context_free (Context *context) |
| 127 |
{ |
| 128 |
if (!context) |
| 129 |
return; |
| 130 |
|
| 131 |
if (context->timeout_id) |
| 132 |
g_source_remove (context->timeout_id); |
| 133 |
|
| 134 |
if (context->received_indication_id) |
| 135 |
g_signal_handler_disconnect (context->client, context->received_indication_id); |
| 136 |
|
| 137 |
if (context->cancellable) |
| 138 |
g_object_unref (context->cancellable); |
| 139 |
if (context->device) |
| 140 |
g_object_unref (context->device); |
| 141 |
if (context->client) |
| 142 |
g_object_unref (context->client); |
| 143 |
g_slice_free (Context, context); |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
operation_shutdown (gboolean operation_status) |
| 148 |
{ |
| 149 |
/* Cleanup context and finish async operation */ |
| 150 |
context_free (ctx); |
| 151 |
qmicli_async_operation_done (operation_status, FALSE); |
| 152 |
} |
| 153 |
|
| 154 |
#if defined HAVE_QMI_MESSAGE_ATR_SEND && defined HAVE_QMI_INDICATION_ATR_RECEIVED |
| 155 |
|
| 156 |
/******************************************************************************/ |
| 157 |
/* Send */ |
| 158 |
|
| 159 |
static QmiMessageAtrSendInput * |
| 160 |
send_input_create (const gchar *str) |
| 161 |
{ |
| 162 |
QmiMessageAtrSendInput *input = NULL; |
| 163 |
|
| 164 |
input = qmi_message_atr_send_input_new (); |
| 165 |
qmi_message_atr_send_input_set_message (input, str, NULL); |
| 166 |
return input; |
| 167 |
} |
| 168 |
|
| 169 |
static gboolean |
| 170 |
send_timed_out (void) |
| 171 |
{ |
| 172 |
ctx->timeout_id = 0; |
| 173 |
g_printerr ("error: operation failed: timeout\n"); |
| 174 |
operation_shutdown (FALSE); |
| 175 |
return G_SOURCE_REMOVE; |
| 176 |
} |
| 177 |
|
| 178 |
static gboolean |
| 179 |
is_final_response (const gchar *reply) |
| 180 |
{ |
| 181 |
/* The following regexes are taken from MM serial parser */ |
| 182 |
GRegexCompileFlags flags = G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW | G_REGEX_OPTIMIZE; |
| 183 |
g_autoptr(GRegex) regex_ok = NULL; |
| 184 |
g_autoptr(GRegex) regex_connect = NULL; |
| 185 |
g_autoptr(GRegex) regex_cme_error = NULL; |
| 186 |
g_autoptr(GRegex) regex_cms_error = NULL; |
| 187 |
g_autoptr(GRegex) regex_unknown_error = NULL; |
| 188 |
g_autoptr(GRegex) regex_connect_failed = NULL; |
| 189 |
|
| 190 |
regex_ok = g_regex_new ("\\r\\nOK(\\r\\n)+", flags, 0, NULL); |
| 191 |
regex_connect = g_regex_new ("\\r\\nCONNECT.*\\r\\n", flags, 0, NULL); |
| 192 |
regex_cme_error = g_regex_new ("\\r\\n\\+CME ERROR*\\r\\n", flags, 0, NULL); |
| 193 |
regex_cms_error = g_regex_new ("\\r\\n\\+CMS ERROR*\\r\\n", flags, 0, NULL); |
| 194 |
regex_unknown_error = g_regex_new ("\\r\\n(ERROR)|(COMMAND NOT SUPPORT)\\r\\n", flags, 0, NULL); |
| 195 |
regex_connect_failed = g_regex_new ("\\r\\n(NO CARRIER)|(BUSY)|(NO ANSWER)|(NO DIALTONE)\\r\\n", flags, 0, NULL); |
| 196 |
|
| 197 |
if (g_regex_match_full (regex_ok, |
| 198 |
reply, strlen (reply), |
| 199 |
0, 0, NULL, NULL) || |
| 200 |
g_regex_match_full (regex_unknown_error, |
| 201 |
reply, strlen (reply), |
| 202 |
0, 0, NULL, NULL) || |
| 203 |
g_regex_match_full (regex_cme_error, |
| 204 |
reply, strlen (reply), |
| 205 |
0, 0, NULL, NULL) || |
| 206 |
g_regex_match_full (regex_cms_error, |
| 207 |
reply, strlen (reply), |
| 208 |
0, 0, NULL, NULL) || |
| 209 |
g_regex_match_full (regex_connect, |
| 210 |
reply, strlen (reply), |
| 211 |
0, 0, NULL, NULL) || |
| 212 |
g_regex_match_full (regex_connect_failed, |
| 213 |
reply, strlen (reply), |
| 214 |
0, 0, NULL, NULL)) |
| 215 |
return TRUE; |
| 216 |
|
| 217 |
return FALSE; |
| 218 |
} |
| 219 |
|
| 220 |
static void |
| 221 |
indication_received (QmiClientAtr *client, |
| 222 |
QmiIndicationAtrReceivedOutput *output) |
| 223 |
{ |
| 224 |
const gchar *received; |
| 225 |
g_autoptr(GError) error = NULL; |
| 226 |
|
| 227 |
if (!qmi_indication_atr_received_output_get_message (output, &received, &error)) { |
| 228 |
g_printerr ("error: couldn't get indication message: %s\n", error->message); |
| 229 |
operation_shutdown (FALSE); |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
if (ctx->monitor) { |
| 234 |
/* When monitoring we don't want to print final responses |
| 235 |
* e.g. the initial ATE0 reply or the ones for the commands sent with |
| 236 |
* atr-send-only using the same cid |
| 237 |
* The comparison with "ATE0" is for avoid printing the echo of the |
| 238 |
* mandatory initial command */ |
| 239 |
if (!is_final_response (received) && g_strcmp0 (received, "ATE0\r")) |
| 240 |
g_print ("%s", received); |
| 241 |
} else { |
| 242 |
/* No need to print an additional '\n', since the indication already has '\r\n' */ |
| 243 |
g_print ("%s", received); |
| 244 |
/* The reply can arrive with multiple indications, so we need to check if the |
| 245 |
* indication has the final response */ |
| 246 |
if (is_final_response (received)) { |
| 247 |
g_print ("Successfully received final response\n"); |
| 248 |
operation_shutdown (TRUE); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
monitoring_cancelled (GCancellable *cancellable) |
| 255 |
{ |
| 256 |
operation_shutdown (TRUE); |
| 257 |
} |
| 258 |
|
| 259 |
static void |
| 260 |
send_ready (QmiClientAtr *client, |
| 261 |
GAsyncResult *res) |
| 262 |
{ |
| 263 |
g_autoptr(QmiMessageAtrSendOutput) output = NULL; |
| 264 |
g_autoptr(GError) error = NULL; |
| 265 |
|
| 266 |
output = qmi_client_atr_send_finish (client, res, &error); |
| 267 |
if (!output) { |
| 268 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 269 |
operation_shutdown (FALSE); |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
if (!qmi_message_atr_send_output_get_result (output, &error)) { |
| 274 |
g_printerr ("error: couldn't send AT command: %s\n", error->message); |
| 275 |
operation_shutdown (FALSE); |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
if (ctx->monitor) { |
| 280 |
/* User can use Ctrl+C to cancel the monitoring at any time */ |
| 281 |
g_cancellable_connect (ctx->cancellable, |
| 282 |
G_CALLBACK (monitoring_cancelled), |
| 283 |
NULL, |
| 284 |
NULL); |
| 285 |
g_print ("Monitoring unsolicited indications: press Ctrl+C to stop\n"); |
| 286 |
} else |
| 287 |
/* Wait for the response asynchronously: 120 seconds should be enough also |
| 288 |
* for long-lasting commands (e.g. AT+COPS=?) */ |
| 289 |
ctx->timeout_id = g_timeout_add_seconds (120, |
| 290 |
(GSourceFunc) send_timed_out, |
| 291 |
NULL); |
| 292 |
|
| 293 |
ctx->received_indication_id = g_signal_connect (ctx->client, |
| 294 |
"received", |
| 295 |
G_CALLBACK (indication_received), |
| 296 |
NULL); |
| 297 |
} |
| 298 |
|
| 299 |
#endif /* HAVE_QMI_MESSAGE_ATR_SEND |
| 300 |
* HAVE_QMI_INDICATION_ATR_RECEIVED */ |
| 301 |
|
| 302 |
#if defined HAVE_QMI_MESSAGE_ATR_SEND |
| 303 |
|
| 304 |
/******************************************************************************/ |
| 305 |
/* Send and don't wait for the reply */ |
| 306 |
|
| 307 |
static void |
| 308 |
send_only_ready (QmiClientAtr *client, |
| 309 |
GAsyncResult *res) |
| 310 |
{ |
| 311 |
g_autoptr(QmiMessageAtrSendOutput) output = NULL; |
| 312 |
g_autoptr(GError) error = NULL; |
| 313 |
|
| 314 |
output = qmi_client_atr_send_finish (client, res, &error); |
| 315 |
if (!output) { |
| 316 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 317 |
operation_shutdown (FALSE); |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
if (!qmi_message_atr_send_output_get_result (output, &error)) { |
| 322 |
g_printerr ("error: couldn't send AT command: %s\n", error->message); |
| 323 |
operation_shutdown (FALSE); |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
g_print ("Successfully sent AT command\n"); |
| 328 |
operation_shutdown (TRUE); |
| 329 |
} |
| 330 |
|
| 331 |
static void |
| 332 |
generic_send (const gchar *cmd, |
| 333 |
GAsyncReadyCallback cb) |
| 334 |
{ |
| 335 |
g_autofree gchar *at_cmd = NULL; |
| 336 |
g_autoptr(QmiMessageAtrSendInput) input = NULL; |
| 337 |
|
| 338 |
g_debug ("Asynchronously sending AT command..."); |
| 339 |
|
| 340 |
at_cmd = g_strconcat (cmd, "\r", NULL); |
| 341 |
input = send_input_create (at_cmd); |
| 342 |
if (!input) { |
| 343 |
operation_shutdown (FALSE); |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
qmi_client_atr_send (ctx->client, |
| 348 |
input, |
| 349 |
10, |
| 350 |
ctx->cancellable, |
| 351 |
cb, |
| 352 |
NULL); |
| 353 |
} |
| 354 |
|
| 355 |
#endif /* HAVE_QMI_MESSAGE_ATR_SEND */ |
| 356 |
|
| 357 |
/******************************************************************************/ |
| 358 |
/* Common */ |
| 359 |
|
| 360 |
static gboolean |
| 361 |
noop_cb (gpointer unused) |
| 362 |
{ |
| 363 |
operation_shutdown (TRUE); |
| 364 |
return FALSE; |
| 365 |
} |
| 366 |
|
| 367 |
void |
| 368 |
qmicli_atr_run (QmiDevice *device, |
| 369 |
QmiClientAtr *client, |
| 370 |
GCancellable *cancellable) |
| 371 |
{ |
| 372 |
/* Initialize context */ |
| 373 |
ctx = g_slice_new0 (Context); |
| 374 |
ctx->device = g_object_ref (device); |
| 375 |
ctx->client = g_object_ref (client); |
| 376 |
ctx->cancellable = g_object_ref (cancellable); |
| 377 |
|
| 378 |
#if defined HAVE_QMI_MESSAGE_ATR_SEND && defined HAVE_QMI_INDICATION_ATR_RECEIVED |
| 379 |
if (send_str) { |
| 380 |
generic_send (send_str, (GAsyncReadyCallback)send_ready); |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
if (monitor_indications_flag) { |
| 385 |
/* To really open the AT communication and receive unsolicited |
| 386 |
* indications it is mandatory to send an initial AT command. |
| 387 |
* The command ATE0 has been chosen so that, if needed, commands |
| 388 |
* can be sent by a different qmicli instance reusing the monitoring |
| 389 |
* cid with --atr-send-only. This avoids also polluting the monitoring |
| 390 |
* operation with the comands echo */ |
| 391 |
ctx->monitor = TRUE; |
| 392 |
generic_send ("ATE0", (GAsyncReadyCallback)send_ready); |
| 393 |
return; |
| 394 |
} |
| 395 |
#endif |
| 396 |
|
| 397 |
#if defined HAVE_QMI_MESSAGE_ATR_SEND |
| 398 |
if (send_only_str) { |
| 399 |
generic_send (send_only_str, (GAsyncReadyCallback)send_only_ready); |
| 400 |
return; |
| 401 |
} |
| 402 |
#endif |
| 403 |
|
| 404 |
/* Just client allocate/release? */ |
| 405 |
if (noop_flag) { |
| 406 |
g_idle_add (noop_cb, NULL); |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
g_warn_if_reached (); |
| 411 |
} |
| 412 |
|
| 413 |
#endif /* HAVE_QMI_SERVICE_ATR */ |