Debian Codegraph @

src/mbimcli/mbimcli-ms-firmware-id.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) 2014 Google, Inc.
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
23 /* Context */
24 typedef struct {
25 MbimDevice *device;
26 GCancellable *cancellable;
27 } Context;
28 static Context *ctx;
29
30 /* Options */
31 static gboolean query_firmware_id_flag;
32
33 static GOptionEntry entries[] = {
34 { "ms-query-firmware-id", 0, 0, G_OPTION_ARG_NONE, &query_firmware_id_flag,
35 "Query firmware ID",
36 NULL
37 },
38 { NULL, 0, 0, 0, NULL, NULL, NULL }
39 };
40
41 GOptionGroup *
42 mbimcli_ms_firmware_id_get_option_group (void)
43 {
44 GOptionGroup *group;
45
46 group = g_option_group_new ("ms-firmware-id",
47 "Microsoft Firmware ID options:",
48 "Show Microsoft Firmware ID Service options",
49 NULL,
50 NULL);
51 g_option_group_add_entries (group, entries);
52
53 return group;
54 }
55
56 gboolean
57 mbimcli_ms_firmware_id_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_firmware_id_flag;
66
67 if (n_actions > 1) {
68 g_printerr ("error: too many Microsoft Firmware ID 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 query_firmware_id_ready (MbimDevice *device,
99 GAsyncResult *res)
100 {
101 g_autoptr(MbimMessage) response = NULL;
102 g_autoptr(GError) error = NULL;
103 const MbimUuid *firmware_id;
104 g_autofree gchar *firmware_id_str = NULL;
105
106 response = mbim_device_command_finish (device, res, &error);
107 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
108 g_printerr ("error: operation failed: %s\n", error->message);
109 shutdown (FALSE);
110 return;
111 }
112
113 if (!mbim_message_ms_firmware_id_get_response_parse (response, &firmware_id, &error)) {
114 g_printerr ("error: couldn't parse response message: %s\n", error->message);
115 shutdown (FALSE);
116 return;
117 }
118
119 firmware_id_str = firmware_id ? mbim_uuid_get_printable (firmware_id) : NULL;
120
121 g_print ("[%s] Firmware ID retrieved: '%s'\n\n",
122 mbim_device_get_path_display (device),
123 VALIDATE_UNKNOWN (firmware_id_str));
124
125 shutdown (TRUE);
126 }
127
128 void
129 mbimcli_ms_firmware_id_run (MbimDevice *device,
130 GCancellable *cancellable)
131 {
132 g_autoptr(MbimMessage) request = NULL;
133
134 /* Initialize context */
135 ctx = g_slice_new (Context);
136 ctx->device = g_object_ref (device);
137 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
138
139 /* Request to get firmware ID? */
140 if (query_firmware_id_flag) {
141 g_debug ("Asynchronously querying firmware ID...");
142 request = (mbim_message_ms_firmware_id_get_query_new (NULL));
143 mbim_device_command (ctx->device,
144 request,
145 10,
146 ctx->cancellable,
147 (GAsyncReadyCallback)query_firmware_id_ready,
148 NULL);
149 return;
150 }
151
152 g_warn_if_reached ();
153 }