Debian Codegraph @

src/mbimcli/mbimcli-helpers-quectel.c

raw
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) 2025 Dan Williams <[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-helpers.h"
22 #include "mbimcli-helpers-quectel.h"
23
24 gboolean
25 mbimcli_helpers_quectel_set_command_input_parse (const gchar *str,
26 gchar **command_str,
27 MbimQuectelCommandType *command_type)
28 {
29 g_auto(GStrv) split = NULL;
30 guint num_parts = 0;
31 g_autofree gchar *new_command = NULL;
32 MbimQuectelCommandType new_command_type = *command_type;
33
34 g_assert (command_str != NULL);
35
36 /* Format of the string is:
37 * "[\"Command\"]"
38 * or:
39 * "[(Command type),(\"Command\")]"
40 */
41 split = g_strsplit (str, ",", -1);
42 num_parts = g_strv_length (split);
43
44 if (num_parts == 0 || !split[0]) {
45 g_printerr ("error: The input string is empty, please re-enter.\n");
46 return FALSE;
47 }
48
49 /* The at command may have multiple commas, like:at+qcfg="usbcfg",0x2C7C,0x6008,0x00FF ,
50 * so we need to take the first split to see if it is the command type.
51 * If it is, then combine the remaining splits into a string.
52 * If not, combine the splits into a string. */
53
54 if (num_parts > 1) {
55 g_autofree gchar *command_type_str = g_ascii_strdown (split[0], -1);
56
57 if (mbimcli_read_quectel_command_type_from_string (command_type_str, &new_command_type, NULL)) {
58 /* Valid Quectel command-type found */
59 new_command = g_strjoinv (",", split + 1);
60 } else {
61 /* No valid Quectel command-type found; assume plain AT command */
62 new_command = g_strdup (str);
63 }
64 } else {
65 new_command = g_strdup (str);
66 }
67
68 if (!g_str_has_prefix (new_command, "AT") && !g_str_has_prefix (new_command, "at")) {
69 g_printerr ("error: Wrong AT command '%s', command must start with \"AT\".\n", new_command);
70 return FALSE;
71 }
72
73 *command_str = g_strdup (new_command);
74 *command_type = new_command_type;
75 return TRUE;
76 }