Debian Codegraph @

src/mbimcli/mbimcli-intel-thermal-rf.c

raw
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 #include <errno.h>
28
29 #include <glib.h>
30 #include <gio/gio.h>
31
32 #include <libmbim-glib.h>
33
34 #include "mbim-common.h"
35 #include "mbimcli.h"
36 #include "mbimcli-helpers.h"
37
38 /* Context */
39 typedef struct {
40 MbimDevice *device;
41 GCancellable *cancellable;
42 } Context;
43 static Context *ctx;
44
45 /* Options */
46 static gboolean query_rfim_flag;
47 static gchar *set_rfim_str;
48
49 static GOptionEntry entries[] = {
50 { "intel-query-rfim", 0, 0, G_OPTION_ARG_NONE, &query_rfim_flag,
51 "Query RFIM frequency information",
52 NULL
53 },
54 { "intel-set-rfim", 0, 0, G_OPTION_ARG_STRING, &set_rfim_str,
55 "Enable or disable RFIM (disabled by default)",
56 "[(on|off)]"
57 },
58 { NULL, 0, 0, 0, NULL, NULL, NULL }
59 };
60
61 GOptionGroup *
62 mbimcli_intel_thermal_rf_get_option_group (void)
63 {
64 GOptionGroup *group;
65
66 group = g_option_group_new ("intel-thermal-rf",
67 "Intel Thermal RF Service options:",
68 "Show Intel Thermal RF Service options",
69 NULL,
70 NULL);
71 g_option_group_add_entries (group, entries);
72
73 return group;
74 }
75
76 gboolean
77 mbimcli_intel_thermal_rf_options_enabled (void)
78 {
79 static guint n_actions = 0;
80 static gboolean checked = FALSE;
81
82 if (checked)
83 return !!n_actions;
84
85 n_actions = (query_rfim_flag +
86 !!set_rfim_str);
87
88 if (n_actions > 1) {
89 g_printerr ("error: too many Intel Thermal RF actions requested\n");
90 exit (EXIT_FAILURE);
91 }
92
93 checked = TRUE;
94 return !!n_actions;
95 }
96
97 static void
98 context_free (Context *context)
99 {
100 if (!context)
101 return;
102
103 if (context->cancellable)
104 g_object_unref (context->cancellable);
105 if (context->device)
106 g_object_unref (context->device);
107 g_slice_free (Context, context);
108 }
109
110 static void
111 shutdown (gboolean operation_status)
112 {
113 /* Cleanup context and finish async operation */
114 context_free (ctx);
115 mbimcli_async_operation_done (operation_status);
116 }
117
118 static void
119 query_rfim_ready (MbimDevice *device,
120 GAsyncResult *res,
121 gpointer user_data)
122 {
123 guint32 i = 0;
124 g_autoptr(MbimMessage) response = NULL;
125 g_autoptr(GError) error = NULL;
126 guint32 element_count;
127 MbimIntelRfimFrequencyValueArray *rfim_frequency;
128
129 response = mbim_device_command_finish (device, res, &error);
130 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
131 g_printerr ("error: operation failed: %s\n", error->message);
132 shutdown (FALSE);
133 return;
134 }
135
136 if (!mbim_message_intel_thermal_rf_rfim_response_parse (response,
137 &element_count,
138 &rfim_frequency,
139 &error)) {
140 g_printerr ("error: couldn't parse response message: %s\n", error->message);
141 shutdown (FALSE);
142 return;
143 }
144
145 g_print ("[%s] RFIM frequency values (%u):\n",
146 mbim_device_get_path_display (device),
147 element_count);
148
149 for (i = 0; i < element_count; i++) {
150 g_autofree gchar *rssi_str = NULL;
151 g_autofree gchar *sinr_str = NULL;
152 g_autofree gchar *rsrq_str = NULL;
153 g_autofree gchar *rsrp_str = NULL;
154
155 if (rfim_frequency[i]->rssi <= 31)
156 rssi_str = g_strdup_printf ("%d dBm", -113 + (2 * rfim_frequency[i]->rssi));
157 else
158 rssi_str = g_strdup_printf ("n/a");
159
160 if (rfim_frequency[i]->rsrq == 0)
161 rsrq_str = g_strdup_printf ("< -19.5 dB");
162 else if (rfim_frequency[i]->rsrq < 34)
163 rsrq_str = g_strdup_printf ("%.2lf dB", -19.5 + ((gdouble)rfim_frequency[i]->rsrq / 2));
164 else if (rfim_frequency[i]->rsrq == 34)
165 rsrq_str = g_strdup_printf (">= -2.5 dB");
166 else
167 rsrq_str = g_strdup_printf ("n/a");
168
169 if (rfim_frequency[i]->rsrp == 0)
170 rsrp_str = g_strdup_printf ("< -140 dBm");
171 else if (rfim_frequency[i]->rsrp < 97)
172 rsrp_str = g_strdup_printf ("%d dBm", -140 + rfim_frequency[i]->rsrp);
173 else if (rfim_frequency[i]->rsrp == 97)
174 rsrp_str = g_strdup_printf (">= -43 dBm");
175 else
176 rsrp_str = g_strdup_printf ("n/a");
177
178 if (rfim_frequency[i]->sinr == 0)
179 sinr_str = g_strdup_printf ("< -23 dB");
180 else if (rfim_frequency[i]->sinr < 97)
181 sinr_str = g_strdup_printf ("%.21f dB", -23 + ((gdouble)rfim_frequency[i]->sinr / 2));
182 else if (rfim_frequency[i]->sinr == 97)
183 sinr_str = g_strdup_printf (">= 40 dB");
184 else
185 sinr_str = g_strdup_printf ("n/a");
186
187 g_print ("\tElement Number: %u\n"
188 "\t Serving cell info: %s\n"
189 "\t Center frequency: %" G_GUINT64_FORMAT " Hz\n"
190 "\t Bandwidth: %u Hz\n"
191 "\t RSRP: %s\n"
192 "\t RSRQ: %s\n"
193 "\t SINR: %s\n"
194 "\t RSSI: %s\n"
195 "\t Connected: %s\n",
196 i + 1,
197 mbim_intel_serving_cell_info_get_string (rfim_frequency[i]->serving_cell_info),
198 rfim_frequency[i]->center_frequency,
199 rfim_frequency[i]->bandwidth,
200 rsrp_str,
201 rsrq_str,
202 sinr_str,
203 rssi_str,
204 rfim_frequency[i]->connection_status ? "yes" : "no");
205 }
206
207 shutdown(TRUE);
208 }
209
210 static void
211 set_rfim_state_ready (MbimDevice *device,
212 GAsyncResult *res,
213 gpointer user_data)
214 {
215 g_autoptr(MbimMessage) response = NULL;
216 g_autoptr(GError) error = NULL;
217
218 response = mbim_device_command_finish (device, res, &error);
219 if (!response) {
220 g_printerr ("error: operation failed: %s\n", error->message);
221 shutdown (FALSE);
222 return;
223 }
224
225 g_print ("[%s] Successfully requested modem to set RFIM state\n",
226 mbim_device_get_path_display (device));
227
228 shutdown (TRUE);
229 }
230
231 void
232 mbimcli_intel_thermal_rf_run (MbimDevice *device,
233 GCancellable *cancellable)
234 {
235 g_autoptr(MbimMessage) request = NULL;
236
237 /* Initialize context */
238 ctx = g_slice_new (Context);
239 ctx->device = g_object_ref (device);
240 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
241
242 /* Query RFIM Frequency information*/
243 if (query_rfim_flag) {
244 g_debug ("Asynchronously querying RFIM frequency information...");
245 request = mbim_message_intel_thermal_rf_rfim_query_new (NULL);
246 mbim_device_command (ctx->device,
247 request,
248 10,
249 ctx->cancellable,
250 (GAsyncReadyCallback)query_rfim_ready,
251 NULL);
252 return;
253 }
254
255 /* Request to set rfim activation state */
256 if (set_rfim_str) {
257 gboolean activation_state;
258
259 g_debug ("Asynchronously setting RFIM activation state...");
260
261 if (g_ascii_strcasecmp (set_rfim_str, "on") == 0)
262 activation_state = TRUE;
263 else if (g_ascii_strcasecmp (set_rfim_str, "off") == 0)
264 activation_state = FALSE;
265 else {
266 g_printerr ("error: invalid RFIM state: '%s'\n", set_rfim_str);
267 shutdown (FALSE);
268 return;
269 }
270
271 request = mbim_message_intel_thermal_rf_rfim_set_new (activation_state, NULL);
272 mbim_device_command (ctx->device,
273 request,
274 10,
275 ctx->cancellable,
276 (GAsyncReadyCallback)set_rfim_state_ready,
277 NULL);
278 return;
279 }
280
281 g_warn_if_reached ();
282 }