Debian Codegraph @

src/mbimcli/mbimcli-intel-at-tunnel.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) 2023 chenhaotian <[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.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 *query_at_command_str;
33
34 static GOptionEntry entries[] = {
35 { "intel-at-tunnel-set-at-command", 0, 0, G_OPTION_ARG_STRING, &query_at_command_str,
36 "send AT command to modem, and receive AT response",
37 "\"<AT command>\""
38 },
39 { NULL, 0, 0, 0, NULL, NULL, NULL }
40 };
41
42 GOptionGroup *
43 mbimcli_intel_at_tunnel_get_option_group (void)
44 {
45 GOptionGroup *group;
46
47 group = g_option_group_new ("intel-at-tunnel",
48 "Intel AT Tunnel options:",
49 "Show Intel AT Tunnel Service options",
50 NULL,
51 NULL);
52 g_option_group_add_entries (group, entries);
53 return group;
54 }
55
56 gboolean
57 mbimcli_intel_at_tunnel_options_enabled (void)
58 {
59 static guint n_actions = 0;
60 static gboolean checked = FALSE;
61
62 if (checked)
63 return !!n_actions;
64
65 n_actions = !!query_at_command_str;
66
67 if (n_actions > 1) {
68 g_printerr ("error: too many Intel AT Tunnel actions requested\n");
69 exit (EXIT_FAILURE);
70 }
71
72 checked = TRUE;
73 return !!n_actions;
74 }
75
76 static void
77 context_free (Context *context)
78 {
79 if (!context)
80 return;
81
82 if (context->cancellable)
83 g_object_unref (context->cancellable);
84 if (context->device)
85 g_object_unref (context->device);
86 g_slice_free (Context, context);
87 }
88
89 static void
90 shutdown (gboolean operation_status)
91 {
92 /* Cleanup context and finish async operation */
93 context_free (ctx);
94 mbimcli_async_operation_done (operation_status);
95 }
96
97 static void
98 intel_at_tunnel_at_command_ready (MbimDevice *device,
99 GAsyncResult *res)
100 {
101 g_autoptr(GError) error = NULL;
102 guint32 ret_size = 0;
103 const guint8 *ret_str = NULL;
104 g_autoptr(MbimMessage) response = NULL;
105
106 response = mbim_device_command_finish (device, res, &error);
107
108 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
109 g_printerr ("error: operation failed: %s\n", error->message);
110 shutdown (FALSE);
111 return;
112 }
113
114 if (!mbim_message_intel_at_tunnel_at_command_response_parse (
115 response,
116 &ret_size,
117 &ret_str,
118 &error)) {
119 g_printerr ("error: couldn't parse response message: %s\n", error->message);
120 shutdown (FALSE);
121 return;
122 }
123
124 g_print ("%.*s\n", ret_size, ret_str);
125
126 shutdown (TRUE);
127 }
128
129 void
130 mbimcli_intel_at_tunnel_run (MbimDevice *device,
131 GCancellable *cancellable)
132 {
133 g_autoptr(MbimMessage) request = NULL;
134 g_autofree gchar *req_str = NULL;
135 guint32 req_size = 0;
136
137 /* Initialize context */
138 ctx = g_slice_new (Context);
139 ctx->device = g_object_ref (device);
140 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
141
142 /* Request to send AT command */
143 if (query_at_command_str) {
144 req_str = g_strdup_printf ("%s\r\n", query_at_command_str);
145 req_size = strlen (req_str);
146
147 request = mbim_message_intel_at_tunnel_at_command_set_new (req_size, (const guint8 *)req_str, NULL);
148 mbim_device_command (ctx->device,
149 request,
150 10,
151 ctx->cancellable,
152 (GAsyncReadyCallback)intel_at_tunnel_at_command_ready,
153 NULL);
154 return;
155 }
156
157 g_warn_if_reached ();
158 }