summaryrefslogtreecommitdiff
path: root/src/mbimcli/mbimcli-google.c
blob: 69948d61689d49bbb46bcba4038323858612287a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * mbimcli -- Command line interface to control MBIM devices
 *
 * Copyright (C) 2023 Intel Corporation
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>

#include <glib.h>
#include <gio/gio.h>

#include <libmbim-glib.h>

#include "mbimcli.h"
#include "mbimcli-helpers.h"

/* Context */
typedef struct {
    MbimDevice   *device;
    GCancellable *cancellable;
} Context;
static Context *ctx;

/* Options */
static gchar    *set_carrier_lock_str;
static gboolean  query_carrier_lock_flag;

static GOptionEntry entries[] = {
    { "google-set-carrier-lock", 0, 0, G_OPTION_ARG_STRING, &set_carrier_lock_str,
      "Set Google Carrier Lock",
      "[(Data)]"
    },
    { "google-query-carrier-lock", 0, 0, G_OPTION_ARG_NONE, &query_carrier_lock_flag,
      "Query Google Carrier Lock",
      NULL
    },
    { NULL }
};

GOptionGroup *
mbimcli_google_get_option_group (void)
{
    GOptionGroup *group;

    group = g_option_group_new ("google",
                                "Google options:",
                                "Show Google Service options",
                                NULL,
                                NULL);
    g_option_group_add_entries (group, entries);

    return group;
}

gboolean
mbimcli_google_options_enabled (void)
{
    static guint    n_actions = 0;
    static gboolean checked = FALSE;

    if (checked)
        return !!n_actions;

    n_actions = (!!set_carrier_lock_str +
                 query_carrier_lock_flag);

    if (n_actions > 1) {
        g_printerr ("error: too many google actions requested\n");
        exit (EXIT_FAILURE);
    }

    checked = TRUE;
    return !!n_actions;
}

static void
context_free (Context *context)
{
    if (!context)
        return;

    if (context->cancellable)
        g_object_unref (context->cancellable);
    if (context->device)
        g_object_unref (context->device);
    g_slice_free (Context, context);
}

static void
shutdown (gboolean operation_status)
{
    /* Cleanup context and finish async operation */
    context_free (ctx);
    mbimcli_async_operation_done (operation_status);
}

static void
set_carrier_lock_ready (MbimDevice   *device,
                        GAsyncResult *res)
{
    g_autoptr(MbimMessage)  response = NULL;
    g_autoptr(GError)       error = NULL;

    response = mbim_device_command_finish (device, res, &error);
    if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
        g_printerr ("error: operation failed: %s\n", error->message);
        shutdown (FALSE);
        return;
    }

    g_print ("[%s] Successfully set carrier lock: \n",
             mbim_device_get_path_display (device));

    shutdown (TRUE);
}

static void
query_carrier_lock_ready (MbimDevice   *device,
                          GAsyncResult *res)
{
    g_autoptr(MbimMessage)     response = NULL;
    g_autoptr(GError)          error = NULL;
    const gchar               *carrier_lock_status_str;
    const gchar               *carrier_lock_modem_state_str;
    const gchar               *carrier_lock_cause_str;
    MbimCarrierLockStatus      carrier_lock_status;
    MbimCarrierLockModemState  carrier_lock_modem_state;
    MbimCarrierLockCause       carrier_lock_cause;

    response = mbim_device_command_finish (device, res, &error);
    if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error)) {
        g_printerr ("error: operation failed: %s\n", error->message);
        shutdown (FALSE);
        return;
    }

    if (!mbim_message_google_carrier_lock_response_parse (
            response,
            &carrier_lock_status,
            &carrier_lock_modem_state,
            &carrier_lock_cause,
            &error)) {
        g_printerr ("error: couldn't parse response message: %s\n", error->message);
        return;
    }

    carrier_lock_status_str      = mbim_carrier_lock_status_get_string (carrier_lock_status);
    carrier_lock_modem_state_str = mbim_carrier_lock_modem_state_get_string (carrier_lock_modem_state);
    carrier_lock_cause_str       = mbim_carrier_lock_cause_get_string (carrier_lock_cause);

    g_print ("[%s] Successfully queried carrier lock: \n"
             "\t     Carrier lock status: '%s'\n"
             "\tCarrier lock modem state: '%s'\n"
             "\t      Carrier lock cause: '%s'\n",
             mbim_device_get_path_display (device),
             VALIDATE_UNKNOWN (carrier_lock_status_str),
             VALIDATE_UNKNOWN (carrier_lock_modem_state_str),
             VALIDATE_UNKNOWN (carrier_lock_cause_str));

    shutdown (TRUE);
}

void
mbimcli_google_run (MbimDevice   *device,
                    GCancellable *cancellable)
{
    g_autoptr(MbimMessage) request = NULL;
    g_autoptr(GError)      error = NULL;

    /* Initialize context */
    ctx = g_slice_new (Context);
    ctx->device = g_object_ref (device);
    ctx->cancellable = cancellable ? g_object_ref (cancellable) : NULL;

    /* Request to set carrier lock */
    if (set_carrier_lock_str) {
        gsize               data_size = 0;
        g_autofree guint8  *data      = NULL;

        data = mbimcli_read_buffer_from_string (set_carrier_lock_str, -1, &data_size, &error);
        if (!data) {
            g_printerr ("Failed to read data: %s\n", error->message);
            shutdown (FALSE);
            return;
        }

        g_debug ("Asynchronously setting carrier lock...");
        request = mbim_message_google_carrier_lock_set_new ((guint32)data_size, data, &error);
        if (!request) {
            g_printerr ("error: couldn't create request: %s\n", error->message);
            shutdown (FALSE);
            return;
        }

        mbim_device_command (ctx->device,
                             request,
                             10,
                             ctx->cancellable,
                             (GAsyncReadyCallback)set_carrier_lock_ready,
                             NULL);
        return;
    }

    /* Query carrier lock information */
    if (query_carrier_lock_flag) {
        request = mbim_message_google_carrier_lock_query_new (&error);
        if (!request) {
            g_printerr ("error: couldn't create request: %s\n", error->message);
            shutdown (FALSE);
            return;
        }

        mbim_device_command (ctx->device,
                             request,
                             10,
                             ctx->cancellable,
                             (GAsyncReadyCallback)query_carrier_lock_ready,
                             NULL);
        return;
    }

    g_warn_if_reached ();
}