~awe/phablet-extras/ofono-lp1204683

« back to all changes in this revision

Viewing changes to drivers/atmodem/call-forwarding.c

  • Committer: Bazaar Package Importer
  • Author(s): Andres Salomon
  • Date: 2009-08-15 15:55:11 UTC
  • Revision ID: james.westby@ubuntu.com-20090815155511-frst06dijguhyfi4
Tags: upstream-0.3
ImportĀ upstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  oFono - Open Source Telephony
 
4
 *
 
5
 *  Copyright (C) 2008-2009  Intel Corporation. All rights reserved.
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License version 2 as
 
9
 *  published by the Free Software Foundation.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 *
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#define _GNU_SOURCE
 
27
#include <string.h>
 
28
#include <stdlib.h>
 
29
#include <stdio.h>
 
30
 
 
31
#include <glib.h>
 
32
 
 
33
#include <ofono/log.h>
 
34
#include <ofono/modem.h>
 
35
#include "driver.h"
 
36
 
 
37
#include "gatchat.h"
 
38
#include "gatresult.h"
 
39
 
 
40
#include "at.h"
 
41
 
 
42
static const char *none_prefix[] = { NULL };
 
43
static const char *ccfc_prefix[] = { "+CCFC:", NULL };
 
44
 
 
45
static void ccfc_query_cb(gboolean ok, GAtResult *result, gpointer user_data)
 
46
{
 
47
        struct cb_data *cbd = user_data;
 
48
        ofono_call_forwarding_query_cb_t cb = cbd->cb;
 
49
        struct ofono_error error;
 
50
        GAtResultIter iter;
 
51
        int num = 0;
 
52
        struct ofono_cf_condition *list = NULL;
 
53
        int i;
 
54
        int maxlen;
 
55
 
 
56
        dump_response("ccfc_query_cb", ok, result);
 
57
        decode_at_error(&error, g_at_result_final_response(result));
 
58
 
 
59
        if (!ok)
 
60
                goto out;
 
61
 
 
62
        g_at_result_iter_init(&iter, result);
 
63
 
 
64
        while (g_at_result_iter_next(&iter, "+CCFC:"))
 
65
                num += 1;
 
66
 
 
67
        /* Specification is really unclear about this
 
68
         * generate status=0 for all classes just in case
 
69
         */
 
70
        if (num == 0) {
 
71
                list = g_new0(struct ofono_cf_condition, 1);
 
72
                num = 1;
 
73
 
 
74
                list->status = 0;
 
75
                list->cls = GPOINTER_TO_INT(cbd->user);
 
76
 
 
77
                goto out;
 
78
        }
 
79
 
 
80
        list = g_new(struct ofono_cf_condition, num);
 
81
 
 
82
        g_at_result_iter_init(&iter, result);
 
83
 
 
84
        maxlen = OFONO_MAX_PHONE_NUMBER_LENGTH;
 
85
 
 
86
        for (num = 0; g_at_result_iter_next(&iter, "+CCFC:"); num++) {
 
87
                const char *str;
 
88
 
 
89
                g_at_result_iter_next_number(&iter, &(list[num].status));
 
90
                g_at_result_iter_next_number(&iter, &(list[num].cls));
 
91
 
 
92
                list[num].phone_number.number[0] = '\0';
 
93
                list[num].phone_number.type = 129;
 
94
                list[num].time = 20;
 
95
 
 
96
                if (!g_at_result_iter_next_string(&iter, &str))
 
97
                        continue;
 
98
 
 
99
                strncpy(list[num].phone_number.number, str, maxlen);
 
100
                list[num].phone_number.number[maxlen] = '\0';
 
101
 
 
102
                g_at_result_iter_next_number(&iter,
 
103
                                                &(list[num].phone_number.type));
 
104
 
 
105
                if (!g_at_result_iter_skip_next(&iter))
 
106
                        continue;
 
107
 
 
108
                if (!g_at_result_iter_skip_next(&iter))
 
109
                        continue;
 
110
 
 
111
                g_at_result_iter_next_number(&iter, &(list[num].time));
 
112
        }
 
113
 
 
114
        for (i = 0; i < num; i++)
 
115
                ofono_debug("ccfc_cb: %d, %d, %s(%d) - %d sec",
 
116
                                list[i].status, list[i].cls,
 
117
                                list[i].phone_number.number,
 
118
                                list[i].phone_number.type, list[i].time);
 
119
 
 
120
out:
 
121
        cb(&error, num, list, cbd->data);
 
122
        g_free(list);
 
123
}
 
124
 
 
125
static void at_ccfc_query(struct ofono_modem *modem, int type, int cls,
 
126
                                ofono_call_forwarding_query_cb_t cb, void *data)
 
127
{
 
128
        struct at_data *at = ofono_modem_get_userdata(modem);
 
129
        struct cb_data *cbd = cb_data_new(modem, cb, data);
 
130
        char buf[64];
 
131
 
 
132
        if (!cbd)
 
133
                goto error;
 
134
 
 
135
        cbd->user = GINT_TO_POINTER(cls);
 
136
 
 
137
        if (cls == 7)
 
138
                sprintf(buf, "AT+CCFC=%d,2", type);
 
139
        else
 
140
                sprintf(buf, "AT+CCFC=%d,2,,,%d", type, cls);
 
141
 
 
142
        if (g_at_chat_send(at->parser, buf, ccfc_prefix,
 
143
                                ccfc_query_cb, cbd, g_free) > 0)
 
144
                return;
 
145
 
 
146
error:
 
147
        if (cbd)
 
148
                g_free(cbd);
 
149
 
 
150
        {
 
151
                DECLARE_FAILURE(error);
 
152
                cb(&error, 0, NULL, data);
 
153
        }
 
154
}
 
155
 
 
156
static void ccfc_set_cb(gboolean ok, GAtResult *result, gpointer user_data)
 
157
{
 
158
        struct cb_data *cbd = user_data;
 
159
        ofono_generic_cb_t cb = cbd->cb;
 
160
        struct ofono_error error;
 
161
 
 
162
        dump_response("ccfc_set_cb", ok, result);
 
163
        decode_at_error(&error, g_at_result_final_response(result));
 
164
 
 
165
        cb(&error, cbd->data);
 
166
}
 
167
 
 
168
static void at_ccfc_set(struct ofono_modem *modem, const char *buf,
 
169
                                ofono_generic_cb_t cb, void *data)
 
170
{
 
171
        struct at_data *at = ofono_modem_get_userdata(modem);
 
172
        struct cb_data *cbd = cb_data_new(modem, cb, data);
 
173
 
 
174
        if (!cbd)
 
175
                goto error;
 
176
 
 
177
        if (g_at_chat_send(at->parser, buf, none_prefix,
 
178
                                ccfc_set_cb, cbd, g_free) > 0)
 
179
                return;
 
180
 
 
181
error:
 
182
        if (cbd)
 
183
                g_free(cbd);
 
184
 
 
185
        {
 
186
                DECLARE_FAILURE(error);
 
187
                cb(&error, data);
 
188
        }
 
189
}
 
190
 
 
191
static void at_ccfc_erasure(struct ofono_modem *modem, int type, int cls,
 
192
                                ofono_generic_cb_t cb, void *data)
 
193
{
 
194
        char buf[128];
 
195
        int len;
 
196
 
 
197
        len = sprintf(buf, "AT+CCFC=%d,4", type);
 
198
 
 
199
        if (cls != 7)
 
200
                sprintf(buf + len, ",,,%d", cls);
 
201
 
 
202
        at_ccfc_set(modem, buf, cb, data);
 
203
}
 
204
 
 
205
static void at_ccfc_deactivation(struct ofono_modem *modem, int type, int cls,
 
206
                                        ofono_generic_cb_t cb, void *data)
 
207
{
 
208
        char buf[128];
 
209
        int len;
 
210
 
 
211
        len = sprintf(buf, "AT+CCFC=%d,0", type);
 
212
 
 
213
        if (cls != 7)
 
214
                sprintf(buf + len, ",,,%d", cls);
 
215
 
 
216
        at_ccfc_set(modem, buf, cb, data);
 
217
}
 
218
 
 
219
static void at_ccfc_activation(struct ofono_modem *modem, int type, int cls,
 
220
                                ofono_generic_cb_t cb, void *data)
 
221
{
 
222
        char buf[128];
 
223
        int len;
 
224
 
 
225
        len = sprintf(buf, "AT+CCFC=%d,1", type);
 
226
 
 
227
        if (cls != 7)
 
228
                sprintf(buf + len, ",,,%d", cls);
 
229
 
 
230
        at_ccfc_set(modem, buf, cb, data);
 
231
}
 
232
 
 
233
static void at_ccfc_registration(struct ofono_modem *modem, int type, int cls,
 
234
                                        const struct ofono_phone_number *ph,
 
235
                                        int time, ofono_generic_cb_t cb,
 
236
                                        void *data)
 
237
{
 
238
        char buf[128];
 
239
        int offset;
 
240
 
 
241
        offset = sprintf(buf, "AT+CCFC=%d,3,\"%s\",%d,%d", type,
 
242
                                ph->number, ph->type, cls);
 
243
 
 
244
        if (type == 2 || type == 4 || type == 5)
 
245
                sprintf(buf+offset, ",,,%d", time);
 
246
 
 
247
        at_ccfc_set(modem, buf, cb, data);
 
248
}
 
249
 
 
250
static struct ofono_call_forwarding_ops ops = {
 
251
        .registration   = at_ccfc_registration,
 
252
        .activation     = at_ccfc_activation,
 
253
        .query          = at_ccfc_query,
 
254
        .deactivation   = at_ccfc_deactivation,
 
255
        .erasure        = at_ccfc_erasure
 
256
};
 
257
 
 
258
void at_call_forwarding_init(struct ofono_modem *modem)
 
259
{
 
260
        ofono_call_forwarding_register(modem, &ops);
 
261
}
 
262
 
 
263
void at_call_forwarding_exit(struct ofono_modem *modem)
 
264
{
 
265
        ofono_call_forwarding_unregister(modem);
 
266
}