aboutsummaryrefslogtreecommitdiff
path: root/src/mm-at.c
blob: 812353772d145a8cbcd9d03c2550fc3be46d832f (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details:
 *
 * Copyright (C) 2011 Aleksander Morgado <aleksander@gnu.org>
 */

#include <glib.h>
#include <glib-object.h>

#include <ModemManager.h>

#include "mm-at.h"
#include "mm-errors-types.h"

typedef struct {
    GObject *owner;
    MMAtSerialPort *port;
    GCancellable *cancellable;
    MMAtCommand *current;
    MMAtCommand *sequence;
    gboolean sequence_free;
    GSimpleAsyncResult *result;
    gchar *result_signature;
    gpointer response_processor_context;
} AtSequenceContext;

static void
at_sequence_free (MMAtCommand *sequence)
{
    MMAtCommand *it;

    for (it = sequence; it->command; it++)
        g_free (it->command);

    g_free (sequence);
}

static void
at_sequence_context_free (AtSequenceContext *ctx)
{
    if (ctx->sequence_free)
        at_sequence_free (ctx->sequence);
    if (ctx->owner)
        g_object_unref (ctx->owner);
    if (ctx->cancellable)
        g_object_unref (ctx->cancellable);
    g_free (ctx->result_signature);
    g_object_unref (ctx->port);
    g_object_unref (ctx->result);
    g_free (ctx);
}

static void
at_sequence_parse_response (MMAtSerialPort *port,
                            GString *response,
                            GError *error,
                            AtSequenceContext *ctx)
{
    GVariant *result = NULL;
    GError *result_error = NULL;
    gboolean continue_sequence;

    /* Cancelled? */
    if (g_cancellable_is_cancelled (ctx->cancellable)) {
        g_simple_async_result_set_error (ctx->result,
                                         MM_CORE_ERROR,
                                         MM_CORE_ERROR_CANCELLED,
                                         "AT sequence was cancelled");
        g_simple_async_result_complete (ctx->result);
        at_sequence_context_free (ctx);
        return;
    }

    /* Translate the AT response into a GVariant with the expected signature */
    if (!ctx->current->response_processor)
        /* No need to process response, go on to next command */
        continue_sequence = TRUE;
    else {
        /* Response processor will tell us if we need to keep on the sequence */
        continue_sequence = !ctx->current->response_processor (
            ctx->owner,
            ctx->response_processor_context,
            ctx->current->command,
            response->str,
            error,
            &result,
            &result_error);
        /* Were we told to abort the sequence? */
        if (result_error) {
            g_assert (result == NULL);
            g_simple_async_result_take_error (ctx->result, result_error);
            g_simple_async_result_complete (ctx->result);
            at_sequence_context_free (ctx);
            return;
        }
    }

    if (continue_sequence) {
        g_assert (result == NULL);
        ctx->current++;
        if (ctx->current->command) {
            /* Schedule the next command in the probing group */
            mm_at_serial_port_queue_command (
                ctx->port,
                ctx->current->command,
                ctx->current->timeout,
                (MMAtSerialResponseFn)at_sequence_parse_response,
                ctx);
            return;
        }

        /* On last command, end. */
    }

    /* Maybe we do not expect/need any result */
    if (!ctx->result_signature) {
        g_assert (result == NULL);
        g_simple_async_result_set_op_res_gpointer (ctx->result, NULL, NULL);
    }
    /* If we do expect one but got none, set error */
    else if (!result) {
        g_simple_async_result_set_error (ctx->result,
                                         MM_CORE_ERROR,
                                         MM_CORE_ERROR_FAILED,
                                         "Expecting result type '%s', got none",
                                         ctx->result_signature);
    }
    /* Check if the result we got is of the type we expected */
    else if (!g_str_equal (g_variant_get_type_string (result),
                           ctx->result_signature)) {
        g_simple_async_result_set_error (ctx->result,
                                         MM_CORE_ERROR,
                                         MM_CORE_ERROR_FAILED,
                                         "Expecting result type '%s', got '%s'",
                                         ctx->result_signature,
                                         g_variant_get_type_string (result));
    }
    /* We got a valid expected reply */
    else {
        g_simple_async_result_set_op_res_gpointer (ctx->result,
                                                   g_variant_ref (result),
                                                   (GDestroyNotify)g_variant_unref);
    }

    if (result)
        g_variant_unref (result);
    g_simple_async_result_complete (ctx->result);
    at_sequence_context_free (ctx);
}

GVariant *
mm_at_sequence_finish (GObject *owner,
                       GAsyncResult *res,
                       GError **error)
{
    GVariant *result;

    if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res),
                                               error))
        return NULL;

    result = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res));

    return (result ? g_variant_ref (result) : NULL);
}

void
mm_at_sequence (GObject *owner,
                MMAtSerialPort *port,
                MMAtCommand *sequence,
                gpointer response_processor_context,
                gboolean sequence_free,
                const gchar *result_signature,
                GCancellable *cancellable,
                GAsyncReadyCallback callback,
                gpointer user_data)
{
    AtSequenceContext *ctx;

    g_return_if_fail (MM_IS_AT_SERIAL_PORT (port));
    g_return_if_fail (sequence != NULL);
    g_return_if_fail (callback != NULL);

    ctx = g_new (AtSequenceContext, 1);
    ctx->owner = owner ? g_object_ref (owner) : NULL;
    ctx->port = g_object_ref (port);
    ctx->result_signature = g_strdup (result_signature);
    ctx->cancellable = (cancellable ?
                        g_object_ref (cancellable) :
                        NULL);
    ctx->result = g_simple_async_result_new (owner,
                                             callback,
                                             user_data,
                                             mm_at_sequence);
    ctx->current = ctx->sequence = sequence;
    ctx->sequence_free = sequence_free;
    ctx->response_processor_context = response_processor_context;

    mm_at_serial_port_queue_command (
        ctx->port,
        ctx->current->command,
        ctx->current->timeout,
        (MMAtSerialResponseFn)at_sequence_parse_response,
        ctx);
}

GVariant *
mm_at_command_finish (GObject *owner,
                      GAsyncResult *res,
                      GError **error)
{
    return mm_at_sequence_finish (owner, res, error);
}

void
mm_at_command (GObject *owner,
               MMAtSerialPort *port,
               const gchar *command,
               guint timeout,
               const MMAtResponseProcessor response_processor,
               gpointer response_processor_context,
               const gchar *result_signature,
               GCancellable *cancellable,
               GAsyncReadyCallback callback,
               gpointer user_data)
{
    MMAtCommand *sequence;

    g_return_if_fail (MM_IS_AT_SERIAL_PORT (port));
    g_return_if_fail (command != NULL);
    g_return_if_fail (response_processor != NULL ||
                      result_signature == NULL);
    g_return_if_fail (callback != NULL);

    sequence = g_new0 (MMAtCommand, 2);
    sequence->command = g_strdup (command);
    sequence->timeout = timeout;
    sequence->response_processor = response_processor;

    mm_at_sequence (owner,
                    port,
                    sequence,
                    response_processor_context,
                    TRUE,
                    result_signature,
                    cancellable,
                    callback,
                    user_data);
}