aboutsummaryrefslogtreecommitdiff
path: root/src/mm-serial-parsers.c
blob: cc23cae8ea0edb12d71a6d304f871c5754044a73 (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
259
260
261
262
263
264
265
266
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

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

#include "mm-serial-parsers.h"
#include "mm-errors.h"

/* Clean up the response by removing control characters like <CR><LF> etc */
static void
response_clean (GString *response)
{
    char *s;

    /* Ends with '<CR><LF>' */
    s = response->str + response->len - 1;
    if (*s == '\n' && *(--s) == '\r')
        g_string_truncate (response, response->len - 2);

    /* Starts with '<CR><LF>' */
    s = response->str;
    if (*s == '\r' && *(++s) == '\n')
        g_string_erase (response, 0, 2);
}


static gboolean
remove_eval_cb (const GMatchInfo *match_info,
                GString *result,
                gpointer user_data)
{
    int *result_len = (int *) user_data;
    int start;
    int end;

    if (g_match_info_fetch_pos  (match_info, 0, &start, &end))
        *result_len -= (end - start);

    return TRUE;
}

static void
remove_matches (GRegex *r, GString *string)
{
    char *str;
    int result_len = string->len;

    str = g_regex_replace_eval (r, string->str, string->len, 0, 0,
                                remove_eval_cb, &result_len, NULL);

    g_string_truncate (string, 0);
    g_string_append_len (string, str, result_len);
    g_free (str);
}

/* FIXME: V0 parser is not finished */
#if 0
typedef struct {
    GRegex *generic_response;
    GRegex *detailed_error;
} MMSerialParserV0;

gpointer
mm_serial_parser_v0_new (void)
{
    MMSerialParserV0 *parser;
    GRegexCompileFlags flags = G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW | G_REGEX_OPTIMIZE;

    parser = g_slice_new (MMSerialParserV0);

    parser->generic_response = g_regex_new ("(\\d)\\r%", flags, 0, NULL);
    parser->detailed_error = g_regex_new ("+CME ERROR: (\\d+)\\r\\n$", flags, 0, NULL);

    return parser;
}

gboolean
mm_serial_parser_v0_parse (gpointer parser,
                           GString *response,
                           GError **error)
{
    MMSerialParserV0 *parser = (MMSerialParserV0 *) data;
    GMatchInfo *match_info;
    char *str;
    int code;
    gboolean found;

    found = g_regex_match_full (parser->generic_response, response->str, response->len, 0, 0, &match_info, NULL);
    if (found) {
        str = g_match_info_fetch (match_info, 1);
        if (str) {
            code = atoi (str);
            g_free (str);
        }

        g_match_info_free (match_info);

        return TRUE;
    }

    found = g_regex_match_full (parser->detailed_error, response->str, response->len, 0, 0, &match_info, NULL);
    if (found) {
        str = g_match_info_fetch (match_info, 1);
        if (str) {
            code = atoi (str);
            g_free (str);
        } else
            code = MM_MOBILE_ERROR_UNKNOWN;

        g_match_info_free (match_info);

        g_debug ("Got error code %d: %s", code, msg);
        g_set_error (error, MM_MOBILE_ERROR, code, "%s", msg);

        return TRUE;
    }

    return FALSE;
}

void
mm_serial_parser_v0_destroy (gpointer parser)
{
    MMSerialParserV0 *parser = (MMSerialParserV0 *) data;

    g_regex_unref (parser->generic_response);
    g_regex_unref (parser->detailed_error);

    g_slice_free (MMSerialParserV0, data);
}
#endif

typedef struct {
    GRegex *regex_ok;
    GRegex *regex_connect;
    GRegex *regex_detailed_error;
    GRegex *regex_unknown_error;
    GRegex *regex_connect_failed;
} MMSerialParserV1;

gpointer
mm_serial_parser_v1_new (void)
{
    MMSerialParserV1 *parser;
    GRegexCompileFlags flags = G_REGEX_DOLLAR_ENDONLY | G_REGEX_RAW | G_REGEX_OPTIMIZE;

    parser = g_slice_new (MMSerialParserV1);

    parser->regex_ok = g_regex_new ("\\r\\nOK\\r\\n$", flags, 0, NULL);
    parser->regex_connect = g_regex_new ("\\r\\nCONNECT\\s*\\d*\\r\\n$", flags, 0, NULL);
    parser->regex_detailed_error = g_regex_new ("\\r\\n\\+CME ERROR: (\\d+)\\r\\n$", flags, 0, NULL);
    parser->regex_unknown_error = g_regex_new ("\\r\\n(ERROR)|(COMMAND NOT SUPPORT)\\r\\n$", flags, 0, NULL);
    parser->regex_connect_failed = g_regex_new ("\\r\\n(NO CARRIER)|(BUSY)|(NO ANSWER)|(NO DIALTONE)\\r\\n$", flags, 0, NULL);

    return parser;
}

gboolean
mm_serial_parser_v1_parse (gpointer data,
                           GString *response,
                           GError **error)
{
    MMSerialParserV1 *parser = (MMSerialParserV1 *) data;
    GMatchInfo *match_info;
    const char *msg;
    int code;
    gboolean found = FALSE;

    /* First, check for successfule responses */

    found = g_regex_match_full (parser->regex_ok, response->str, response->len, 0, 0, NULL, NULL);
    if (found)
        remove_matches (parser->regex_ok, response);
    else
        found = g_regex_match_full (parser->regex_connect, response->str, response->len, 0, 0, NULL, NULL);

    if (found) {
        response_clean (response);
        return TRUE;
    }

    /* Now failures */
    code = MM_MOBILE_ERROR_UNKNOWN;

    found = g_regex_match_full (parser->regex_detailed_error,
                                response->str, response->len,
                                0, 0, &match_info, NULL);

    if (found) {
        char *str;

        str = g_match_info_fetch (match_info, 1);
        if (str) {
            code = atoi (str);
            g_free (str);
        }
        g_match_info_free (match_info);
    } else 
        found = g_regex_match_full (parser->regex_unknown_error, response->str, response->len, 0, 0, NULL, NULL);

    if (found) {
#if 0
        /* FIXME: This does not work for some reason. */
        GEnumValue *enum_value;

        enum_value = g_enum_get_value ((GEnumClass *) g_type_class_peek_static (), code);
        msg = enum_value->value_nick;
#endif
        msg = "FIXME";

        g_debug ("Got error code %d: %s", code, msg);
        g_set_error (error, MM_MOBILE_ERROR, code, "%s", msg);
    } else {
        found = g_regex_match_full (parser->regex_connect_failed,
                                    response->str, response->len,
                                    0, 0, &match_info, NULL);
        if (found) {
            char *str;

            str = g_match_info_fetch (match_info, 1);
            if (str) {
                if (!strcmp (str, "NO CARRIER")) {
                    code = MM_MODEM_CONNECT_ERROR_NO_CARRIER;
                    msg = "No carrier";
                } else if (!strcmp (str, "BUSY")) {
                    code = MM_MODEM_CONNECT_ERROR_BUSY;
                    msg = "Busy";
                } else if (!strcmp (str, "NO ANSWER")) {
                    code = MM_MODEM_CONNECT_ERROR_NO_ANSWER;
                    msg = "No answer";
                } else if (!strcmp (str, "NO DIALTONE")) {
                    code = MM_MODEM_CONNECT_ERROR_NO_DIALTONE;
                    msg = "No dialtone";
                } else {
                    g_warning ("Got matching connect failure, but can't parse it");
                    /* uhm... make something up (yes, ok, lie!). */
                    code = MM_MODEM_CONNECT_ERROR_NO_CARRIER;
                    msg = "No carrier";
                }

                g_free (str);
            }
            g_match_info_free (match_info);

            g_debug ("Got connect failure code %d: %s", code, msg);
            g_set_error (error, MM_MODEM_CONNECT_ERROR, code, "%s", msg);
        }
    }

    return found;

}

void
mm_serial_parser_v1_destroy (gpointer data)
{
    MMSerialParserV1 *parser = (MMSerialParserV1 *) data;

    g_regex_unref (parser->regex_ok);
    g_regex_unref (parser->regex_connect);
    g_regex_unref (parser->regex_detailed_error);
    g_regex_unref (parser->regex_unknown_error);
    g_regex_unref (parser->regex_connect_failed);

    g_slice_free (MMSerialParserV1, data);
}