| 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) 2020 Google Inc. |
| 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 |
|
| 31 |
#include <libqmi-glib.h> |
| 32 |
|
| 33 |
#include "qmicli.h" |
| 34 |
#include "qmicli-helpers.h" |
| 35 |
|
| 36 |
#if defined HAVE_QMI_SERVICE_SAR |
| 37 |
|
| 38 |
/* Context */ |
| 39 |
typedef struct { |
| 40 |
QmiDevice *device; |
| 41 |
QmiClientSar *client; |
| 42 |
GCancellable *cancellable; |
| 43 |
} Context; |
| 44 |
static Context *ctx; |
| 45 |
|
| 46 |
/* Options */ |
| 47 |
static gchar *rf_set_state_str; |
| 48 |
static gboolean rf_get_state_flag; |
| 49 |
static gboolean noop_flag; |
| 50 |
|
| 51 |
#undef VALIDATE_UNKNOWN |
| 52 |
#define VALIDATE_UNKNOWN(str) (str ? str : "unknown") |
| 53 |
|
| 54 |
static GOptionEntry entries[] = { |
| 55 |
#if defined HAVE_QMI_MESSAGE_SAR_RF_GET_STATE |
| 56 |
{ "sar-rf-get-state", 0, 0, G_OPTION_ARG_NONE, &rf_get_state_flag, |
| 57 |
"Get RF state", |
| 58 |
NULL |
| 59 |
}, |
| 60 |
#endif |
| 61 |
#if defined HAVE_QMI_MESSAGE_SAR_RF_SET_STATE |
| 62 |
{ "sar-rf-set-state", 0, 0, G_OPTION_ARG_STRING, &rf_set_state_str, |
| 63 |
"Set RF state.", |
| 64 |
"[(state number)]" |
| 65 |
}, |
| 66 |
#endif |
| 67 |
{ "sar-noop", 0, 0, G_OPTION_ARG_NONE, &noop_flag, |
| 68 |
"Just allocate or release a SAR client. Use with `--client-no-release-cid' and/or `--client-cid'", |
| 69 |
NULL |
| 70 |
}, |
| 71 |
{ NULL, 0, 0, 0, NULL, NULL, NULL } |
| 72 |
}; |
| 73 |
|
| 74 |
GOptionGroup * |
| 75 |
qmicli_sar_get_option_group (void) |
| 76 |
{ |
| 77 |
GOptionGroup *group; |
| 78 |
|
| 79 |
group = g_option_group_new ("sar", |
| 80 |
"SAR options:", |
| 81 |
"Show Specific Absorption Rate options", |
| 82 |
NULL, |
| 83 |
NULL); |
| 84 |
g_option_group_add_entries (group, entries); |
| 85 |
|
| 86 |
return group; |
| 87 |
} |
| 88 |
|
| 89 |
gboolean |
| 90 |
qmicli_sar_options_enabled (void) |
| 91 |
{ |
| 92 |
static guint n_actions = 0; |
| 93 |
static gboolean checked = FALSE; |
| 94 |
|
| 95 |
if (checked) |
| 96 |
return !!n_actions; |
| 97 |
|
| 98 |
n_actions = (!!rf_set_state_str + |
| 99 |
rf_get_state_flag + |
| 100 |
noop_flag); |
| 101 |
|
| 102 |
if (n_actions > 1) { |
| 103 |
g_printerr ("error: too many SAR actions requested\n"); |
| 104 |
exit (EXIT_FAILURE); |
| 105 |
} |
| 106 |
|
| 107 |
checked = TRUE; |
| 108 |
return !!n_actions; |
| 109 |
} |
| 110 |
|
| 111 |
static void |
| 112 |
context_free (Context *context) |
| 113 |
{ |
| 114 |
if (!context) |
| 115 |
return; |
| 116 |
|
| 117 |
if (context->client) |
| 118 |
g_object_unref (context->client); |
| 119 |
g_object_unref (context->cancellable); |
| 120 |
g_object_unref (context->device); |
| 121 |
g_slice_free (Context, context); |
| 122 |
} |
| 123 |
|
| 124 |
static void |
| 125 |
operation_shutdown (gboolean operation_status) |
| 126 |
{ |
| 127 |
/* Cleanup context and finish async operation */ |
| 128 |
context_free (ctx); |
| 129 |
qmicli_async_operation_done (operation_status, FALSE); |
| 130 |
} |
| 131 |
|
| 132 |
#if defined HAVE_QMI_MESSAGE_SAR_RF_GET_STATE |
| 133 |
|
| 134 |
static void |
| 135 |
rf_get_state_ready (QmiClientSar *client, |
| 136 |
GAsyncResult *res) |
| 137 |
{ |
| 138 |
QmiMessageSarRfGetStateOutput *output; |
| 139 |
GError *error = NULL; |
| 140 |
QmiSarRfState rf_state; |
| 141 |
|
| 142 |
output = qmi_client_sar_rf_get_state_finish (client, res, &error); |
| 143 |
if (!output) { |
| 144 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 145 |
g_error_free (error); |
| 146 |
operation_shutdown (FALSE); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
if (!qmi_message_sar_rf_get_state_output_get_result (output, &error)) { |
| 151 |
g_printerr ("error: couldn't get SAR RF state: %s\n", error->message); |
| 152 |
g_error_free (error); |
| 153 |
qmi_message_sar_rf_get_state_output_unref (output); |
| 154 |
operation_shutdown (FALSE); |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
qmi_message_sar_rf_get_state_output_get_state (output, &rf_state, NULL); |
| 160 |
g_print ("[%s] Successfully got SAR RF state: %s\n", |
| 161 |
qmi_device_get_path_display (ctx->device), |
| 162 |
qmi_sar_rf_state_get_string (rf_state)); |
| 163 |
|
| 164 |
qmi_message_sar_rf_get_state_output_unref (output); |
| 165 |
operation_shutdown (TRUE); |
| 166 |
} |
| 167 |
|
| 168 |
#endif /* HAVE_QMI_MESSAGE_SAR_RF_GET_STATE */ |
| 169 |
|
| 170 |
#if defined HAVE_QMI_MESSAGE_SAR_RF_SET_STATE |
| 171 |
|
| 172 |
static QmiMessageSarRfSetStateInput * |
| 173 |
rf_set_state_input_create (const gchar *str) |
| 174 |
{ |
| 175 |
QmiMessageSarRfSetStateInput *input = NULL; |
| 176 |
QmiSarRfState rf_state; |
| 177 |
|
| 178 |
if (qmicli_read_sar_rf_state_from_string (str, &rf_state)) { |
| 179 |
GError *error = NULL; |
| 180 |
|
| 181 |
input = qmi_message_sar_rf_set_state_input_new (); |
| 182 |
if (!qmi_message_sar_rf_set_state_input_set_state ( |
| 183 |
input, |
| 184 |
rf_state, |
| 185 |
&error)) { |
| 186 |
g_printerr ("error: couldn't create input data: '%s'\n", |
| 187 |
error->message); |
| 188 |
g_error_free (error); |
| 189 |
qmi_message_sar_rf_set_state_input_unref (input); |
| 190 |
input = NULL; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return input; |
| 195 |
} |
| 196 |
|
| 197 |
static void |
| 198 |
rf_set_state_ready (QmiClientSar *client, |
| 199 |
GAsyncResult *res) |
| 200 |
{ |
| 201 |
QmiMessageSarRfSetStateOutput *output; |
| 202 |
GError *error = NULL; |
| 203 |
|
| 204 |
output = qmi_client_sar_rf_set_state_finish (client, res, &error); |
| 205 |
if (!output) { |
| 206 |
g_printerr ("error: operation failed: %s\n", error->message); |
| 207 |
g_error_free (error); |
| 208 |
operation_shutdown (FALSE); |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
if (!qmi_message_sar_rf_set_state_output_get_result (output, &error)) { |
| 213 |
g_printerr ("error: couldn't set RF state: %s\n", error->message); |
| 214 |
g_error_free (error); |
| 215 |
|
| 216 |
qmi_message_sar_rf_set_state_output_unref (output); |
| 217 |
operation_shutdown (FALSE); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
g_print ("[%s] RF state set successfully\n", |
| 222 |
qmi_device_get_path_display (ctx->device)); |
| 223 |
|
| 224 |
qmi_message_sar_rf_set_state_output_unref (output); |
| 225 |
operation_shutdown (TRUE); |
| 226 |
} |
| 227 |
|
| 228 |
#endif /* HAVE_QMI_MESSAGE_SAR_RF_SET_STATE */ |
| 229 |
|
| 230 |
static gboolean |
| 231 |
noop_cb (gpointer unused) |
| 232 |
{ |
| 233 |
operation_shutdown (TRUE); |
| 234 |
return FALSE; |
| 235 |
} |
| 236 |
|
| 237 |
void |
| 238 |
qmicli_sar_run (QmiDevice *device, |
| 239 |
QmiClientSar *client, |
| 240 |
GCancellable *cancellable) |
| 241 |
{ |
| 242 |
/* Initialize context */ |
| 243 |
ctx = g_slice_new0 (Context); |
| 244 |
ctx->device = g_object_ref (device); |
| 245 |
ctx->client = g_object_ref (client); |
| 246 |
ctx->cancellable = g_object_ref (cancellable); |
| 247 |
|
| 248 |
#if defined HAVE_QMI_MESSAGE_SAR_RF_GET_STATE |
| 249 |
|
| 250 |
if (rf_get_state_flag) { |
| 251 |
g_debug ("Asynchronously getting RF power state..."); |
| 252 |
qmi_client_sar_rf_get_state (ctx->client, |
| 253 |
NULL, |
| 254 |
10, |
| 255 |
ctx->cancellable, |
| 256 |
(GAsyncReadyCallback) rf_get_state_ready, |
| 257 |
NULL); |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
#endif |
| 262 |
|
| 263 |
#if defined HAVE_QMI_MESSAGE_SAR_RF_SET_STATE |
| 264 |
|
| 265 |
if (rf_set_state_str) { |
| 266 |
QmiMessageSarRfSetStateInput *input; |
| 267 |
|
| 268 |
g_debug ("Asynchronously setting RF power state..."); |
| 269 |
input = rf_set_state_input_create (rf_set_state_str); |
| 270 |
if (!input) { |
| 271 |
operation_shutdown (FALSE); |
| 272 |
return; |
| 273 |
} |
| 274 |
qmi_client_sar_rf_set_state (ctx->client, |
| 275 |
input, |
| 276 |
10, |
| 277 |
ctx->cancellable, |
| 278 |
(GAsyncReadyCallback) rf_set_state_ready, |
| 279 |
NULL); |
| 280 |
qmi_message_sar_rf_set_state_input_unref (input); |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
#endif /* HAVE_QMI_MESSAGE_SAR_RF_SET_STATE */ |
| 285 |
|
| 286 |
/* Just client allocate/release? */ |
| 287 |
if (noop_flag) { |
| 288 |
g_idle_add (noop_cb, NULL); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
g_warn_if_reached (); |
| 293 |
} |
| 294 |
|
| 295 |
#endif /* HAVE_QMI_SERVICE_SAR */ |