~phablet-team/ofono/ofono-bug-updates

« back to all changes in this revision

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

  • Committer: Denis Kenzior
  • Author(s): Lucas De Marchi
  • Date: 2011-03-18 23:31:14 UTC
  • Revision ID: git-v1:888e07863b24026413bac8f449de377c879e1044
message: add cancelled state

Based on patch from Yang Gu <gyagp0@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 *  oFono - Open Source Telephony
4
 
 *
5
 
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6
 
 *  Copyright (C) 2013 Jolla Ltd
7
 
 *  Contact: Jussi Kangas <jussi.kangas@tieto.com>
8
 
 *  Copyright (C) 2014 Canonical Ltd.
9
 
 *
10
 
 *  This program is free software; you can redistribute it and/or modify
11
 
 *  it under the terms of the GNU General Public License version 2 as
12
 
 *  published by the Free Software Foundation.
13
 
 *
14
 
 *  This program is distributed in the hope that it will be useful,
15
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 *  GNU General Public License for more details.
18
 
 *
19
 
 *  You should have received a copy of the GNU General Public License
20
 
 *  along with this program; if not, write to the Free Software
21
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22
 
 *
23
 
 */
24
 
#ifdef HAVE_CONFIG_H
25
 
#include <config.h>
26
 
#endif
27
 
 
28
 
#define _GNU_SOURCE
29
 
#include <stdio.h>
30
 
#include <stdlib.h>
31
 
#include <string.h>
32
 
#include <errno.h>
33
 
 
34
 
#include <glib.h>
35
 
 
36
 
#include <ofono/log.h>
37
 
#include <ofono/modem.h>
38
 
#include <ofono/call-forwarding.h>
39
 
 
40
 
#include "gril.h"
41
 
#include "grilrequest.h"
42
 
#include "grilreply.h"
43
 
#include "grilunsol.h"
44
 
 
45
 
#include "rilmodem.h"
46
 
 
47
 
#include "common.h"
48
 
 
49
 
enum cf_action {
50
 
        CF_ACTION_DISABLE,
51
 
        CF_ACTION_ENABLE,
52
 
        CF_ACTION_INTERROGATE,
53
 
        CF_ACTION_REGISTRATION,
54
 
        CF_ACTION_ERASURE,
55
 
};
56
 
 
57
 
struct forw_data {
58
 
        GRil *ril;
59
 
        enum cf_action last_action;
60
 
        int last_cls;
61
 
};
62
 
 
63
 
static const char *cf_action_to_string(enum cf_action action)
64
 
{
65
 
        switch (action) {
66
 
        case CF_ACTION_DISABLE:
67
 
                return "DISABLE";
68
 
        case CF_ACTION_ENABLE:
69
 
                return "ENABLE";
70
 
        case CF_ACTION_INTERROGATE:
71
 
                return "INTERROGATE";
72
 
        case CF_ACTION_REGISTRATION:
73
 
                return "REGISTRATION";
74
 
        case CF_ACTION_ERASURE:
75
 
                return "ERASURE";
76
 
        }
77
 
 
78
 
        return NULL;
79
 
}
80
 
 
81
 
static void ril_query_call_fwd_cb(struct ril_msg *message, gpointer user_data)
82
 
{
83
 
        struct cb_data *cbd = user_data;
84
 
        struct forw_data *fd = ofono_call_forwarding_get_data(cbd->user);
85
 
        ofono_call_forwarding_query_cb_t cb = cbd->cb;
86
 
        struct ofono_call_forwarding_condition *list;
87
 
        unsigned int list_size;
88
 
 
89
 
        if (message->error != RIL_E_SUCCESS) {
90
 
                ofono_error("%s: rild error: %s", __func__,
91
 
                                ril_error_to_string(message->error));
92
 
                goto error;
93
 
        }
94
 
 
95
 
        list = g_ril_reply_parse_query_call_fwd(fd->ril, message, &list_size);
96
 
        /*
97
 
         * From atmodem:
98
 
         *
99
 
         * Specification is really unclear about this
100
 
         * generate status=0 for all classes just in case
101
 
         */
102
 
        if (list_size == 0) {
103
 
                list = g_new0(struct ofono_call_forwarding_condition, 1);
104
 
                list_size = 1;
105
 
 
106
 
                list->status = 0;
107
 
                list->cls = fd->last_cls;
108
 
        } else if (list == NULL) {
109
 
                goto error;
110
 
        }
111
 
 
112
 
        CALLBACK_WITH_SUCCESS(cb, (int) list_size, list, cbd->data);
113
 
        g_free(list);
114
 
        return;
115
 
 
116
 
error:
117
 
        CALLBACK_WITH_FAILURE(cb, 0, NULL, cbd->data);
118
 
}
119
 
 
120
 
static void ril_set_forward_cb(struct ril_msg *message, gpointer user_data)
121
 
{
122
 
        struct cb_data *cbd = user_data;
123
 
        ofono_call_forwarding_set_cb_t cb = cbd->cb;
124
 
        struct forw_data *fd = ofono_call_forwarding_get_data(cbd->user);
125
 
 
126
 
        if (message->error == RIL_E_SUCCESS) {
127
 
                g_ril_print_response_no_args(fd->ril, message);
128
 
                CALLBACK_WITH_SUCCESS(cb, cbd->data);
129
 
        } else {
130
 
                ofono_error("%s: CF %s failed; rild error: %s", __func__,
131
 
                                cf_action_to_string(fd->last_action),
132
 
                                ril_error_to_string(message->error));
133
 
                CALLBACK_WITH_FAILURE(cb, cbd->data);
134
 
        }
135
 
}
136
 
 
137
 
static int ril_send_forward_cmd(int type, int cls,
138
 
                                const struct ofono_phone_number *number,
139
 
                                int time,
140
 
                                struct cb_data *cbd,
141
 
                                enum cf_action action)
142
 
{
143
 
        struct ofono_call_forwarding *cf = cbd->user;
144
 
        struct forw_data *fd = ofono_call_forwarding_get_data(cf);
145
 
        struct parcel rilp;
146
 
        struct req_call_fwd fwd_req;
147
 
        int ret = 0, request;
148
 
        GRilResponseFunc response_func;
149
 
 
150
 
        if (action == CF_ACTION_INTERROGATE) {
151
 
                request = RIL_REQUEST_QUERY_CALL_FORWARD_STATUS;
152
 
                response_func = ril_query_call_fwd_cb;
153
 
        } else {
154
 
                request = RIL_REQUEST_SET_CALL_FORWARD;
155
 
                response_func = ril_set_forward_cb;
156
 
        }
157
 
 
158
 
        DBG("%s - %s", ril_request_id_to_string(request),
159
 
                cf_action_to_string(action));
160
 
 
161
 
        /*
162
 
         * Modem seems to respond with error to all queries
163
 
         * or settings made with bearer class
164
 
         * BEARER_CLASS_DEFAULT. Design decision: If given
165
 
         * class is BEARER_CLASS_DEFAULT let's map it to
166
 
         * SERVICE_CLASS_NONE as with it e.g. ./send-ussd '*21*<phone_number>#'
167
 
         * returns cls:53 i.e. 1+4+16+32 as service class.
168
 
        */
169
 
        if (cls == BEARER_CLASS_DEFAULT)
170
 
                cls = SERVICE_CLASS_NONE;
171
 
 
172
 
        fd->last_action = action;
173
 
        fd->last_cls = cls;
174
 
 
175
 
        fwd_req.action = (int) action;
176
 
        fwd_req.type = type;
177
 
        fwd_req.cls = cls;
178
 
        fwd_req.number = number;
179
 
 
180
 
        /*
181
 
         * time has no real meaing for action commands other
182
 
         * then registration, so if not needed, set arbitrary
183
 
         * 60s time so rild doesn't return an error.
184
 
         */
185
 
        if (time == -1)
186
 
                fwd_req.time = 60;
187
 
        else
188
 
                fwd_req.time = time;
189
 
 
190
 
        g_ril_request_call_fwd(fd->ril, &fwd_req, &rilp);
191
 
 
192
 
        ret = g_ril_send(fd->ril, request, &rilp, response_func, cbd, g_free);
193
 
        if (ret == 0)
194
 
                ofono_error("%s: CF action %s failed", __func__,
195
 
                                cf_action_to_string(action));
196
 
        return ret;
197
 
}
198
 
 
199
 
static void ril_activate(struct ofono_call_forwarding *cf,
200
 
                                int type, int cls,
201
 
                                ofono_call_forwarding_set_cb_t cb, void *data)
202
 
{
203
 
        struct cb_data *cbd = cb_data_new(cb, data, cf);
204
 
 
205
 
        if (ril_send_forward_cmd(type, cls, NULL, -1, cbd,
206
 
                                        CF_ACTION_ENABLE) == 0) {
207
 
                CALLBACK_WITH_FAILURE(cb, cbd->data);
208
 
                g_free(cbd);
209
 
        }
210
 
}
211
 
 
212
 
static void ril_erasure(struct ofono_call_forwarding *cf,
213
 
                                int type, int cls,
214
 
                                ofono_call_forwarding_set_cb_t cb, void *data)
215
 
{
216
 
        struct cb_data *cbd = cb_data_new(cb, data, cf);
217
 
 
218
 
        if (ril_send_forward_cmd(type, cls, NULL, -1, cbd,
219
 
                                        CF_ACTION_ERASURE) == 0) {
220
 
                CALLBACK_WITH_FAILURE(cb, cbd->data);
221
 
                g_free(cbd);
222
 
        }
223
 
}
224
 
 
225
 
static void ril_deactivate(struct ofono_call_forwarding *cf,
226
 
                                int type, int cls,
227
 
                                ofono_call_forwarding_set_cb_t cb, void *data)
228
 
{
229
 
        struct cb_data *cbd = cb_data_new(cb, data, cf);
230
 
 
231
 
        if (ril_send_forward_cmd(type, cls, NULL, -1, cbd,
232
 
                                        CF_ACTION_DISABLE) == 0) {
233
 
                CALLBACK_WITH_FAILURE(cb, cbd->data);
234
 
                g_free(cbd);
235
 
        }
236
 
}
237
 
 
238
 
static void ril_registration(struct ofono_call_forwarding *cf, int type,
239
 
                                int cls,
240
 
                                const struct ofono_phone_number *number,
241
 
                                int time, ofono_call_forwarding_set_cb_t cb,
242
 
                                void *data)
243
 
{
244
 
        struct cb_data *cbd = cb_data_new(cb, data, cf);
245
 
 
246
 
        if (ril_send_forward_cmd(type, cls, number, time, cbd,
247
 
                                        CF_ACTION_REGISTRATION) == 0) {
248
 
                CALLBACK_WITH_FAILURE(cb, cbd->data);
249
 
                g_free(cbd);
250
 
        }
251
 
}
252
 
 
253
 
static void ril_query(struct ofono_call_forwarding *cf, int type, int cls,
254
 
                                ofono_call_forwarding_query_cb_t cb,
255
 
                                void *data)
256
 
{
257
 
        struct cb_data *cbd = cb_data_new(cb, data, cf);
258
 
 
259
 
        if (ril_send_forward_cmd(type, cls, NULL, -1, cbd,
260
 
                                        CF_ACTION_INTERROGATE) == 0) {
261
 
                CALLBACK_WITH_FAILURE(cb, 0, NULL, cbd->data);
262
 
                g_free(cbd);
263
 
        }
264
 
}
265
 
 
266
 
static gboolean ril_delayed_register(gpointer user_data)
267
 
{
268
 
        struct ofono_call_forwarding *cf = user_data;
269
 
 
270
 
        ofono_call_forwarding_register(cf);
271
 
        return FALSE;
272
 
}
273
 
 
274
 
static int ril_call_forwarding_probe(struct ofono_call_forwarding *cf,
275
 
                                        unsigned int vendor, void *user)
276
 
{
277
 
        GRil *ril = user;
278
 
        struct forw_data *fd;
279
 
 
280
 
        fd = g_try_new0(struct forw_data, 1);
281
 
        if (fd == NULL)
282
 
                return -ENOMEM;
283
 
 
284
 
        fd->ril = g_ril_clone(ril);
285
 
        ofono_call_forwarding_set_data(cf, fd);
286
 
 
287
 
        /*
288
 
         * ofono_call_forwarding_register() needs to be called after
289
 
         * the driver has been set in ofono_call_forwarding_create(),
290
 
         * which calls this function.  Most other drivers make
291
 
         * some kind of capabilities query to the modem, and then
292
 
         * call register in the callback; we use an idle event instead.
293
 
         */
294
 
        g_idle_add(ril_delayed_register, cf);
295
 
 
296
 
        return 0;
297
 
}
298
 
 
299
 
static void ril_call_forwarding_remove(struct ofono_call_forwarding *cf)
300
 
{
301
 
        struct forw_data *data = ofono_call_forwarding_get_data(cf);
302
 
        ofono_call_forwarding_set_data(cf, NULL);
303
 
 
304
 
        g_ril_unref(data->ril);
305
 
        g_free(data);
306
 
}
307
 
 
308
 
static struct ofono_call_forwarding_driver driver = {
309
 
        .name                   = RILMODEM,
310
 
        .probe                  = ril_call_forwarding_probe,
311
 
        .remove                 = ril_call_forwarding_remove,
312
 
        .erasure                = ril_erasure,
313
 
        .deactivation           = ril_deactivate,
314
 
        .query                  = ril_query,
315
 
        .registration           = ril_registration,
316
 
        .activation             = ril_activate
317
 
};
318
 
 
319
 
void ril_call_forwarding_init(void)
320
 
{
321
 
        ofono_call_forwarding_driver_register(&driver);
322
 
}
323
 
 
324
 
void ril_call_forwarding_exit(void)
325
 
{
326
 
        ofono_call_forwarding_driver_unregister(&driver);
327
 
}