Debian Codegraph @

src/mbimcli/mbimcli-ms-host-shutdown.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 notify_host_shutdown_flag;
32
33 static GOptionEntry entries[] = {
34 { "ms-notify-host-shutdown", 0, 0, G_OPTION_ARG_NONE, &notify_host_shutdown_flag,
35 "Notify that host is shutting down",
36 NULL
37 },
38 { NULL, 0, 0, 0, NULL, NULL, NULL }
39 };
40
41 GOptionGroup *
42 mbimcli_ms_host_shutdown_get_option_group (void)
43 {
44 GOptionGroup *group;
45
46 group = g_option_group_new ("ms-host-shutdown",
47 "Microsoft Host Shutdown options:",
48 "Show Microsoft Host Shutdown 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_host_shutdown_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 = notify_host_shutdown_flag;
66
67 if (n_actions > 1) {
68 g_printerr ("error: too many Microsoft Host Shutdown 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 ms_host_shutdown_ready (MbimDevice *device,
99 GAsyncResult *res)
100 {
101 g_autoptr(MbimMessage) response = NULL;
102 g_autoptr(GError) error = NULL;
103
104 response = mbim_device_command_finish (device, res, &error);
105 if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
106 g_printerr ("error: operation failed: %s\n", error->message);
107 shutdown (FALSE);
108 return;
109 }
110
111 g_print ("[%s] Successfully notified that host is shutting down\n\n",
112 mbim_device_get_path_display (device));
113
114 shutdown (TRUE);
115 }
116
117 void
118 mbimcli_ms_host_shutdown_run (MbimDevice *device,
119 GCancellable *cancellable)
120 {
121 g_autoptr(MbimMessage) request = NULL;
122
123 /* Initialize context */
124 ctx = g_slice_new (Context);
125 ctx->device = g_object_ref (device);
126 ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
127
128 /* Request to notify that host is shutting down */
129 if (notify_host_shutdown_flag) {
130 g_debug ("Asynchronously notifying host is shutting down...");
131 request = (mbim_message_ms_host_shutdown_notify_set_new (NULL));
132 mbim_device_command (ctx->device,
133 request,
134 10,
135 ctx->cancellable,
136 (GAsyncReadyCallback)ms_host_shutdown_ready,
137 NULL);
138 return;
139 }
140
141 g_warn_if_reached ();
142 }