212684a3e88ef398550b68b5ca000cfd2cd43bf2
[platform/upstream/ofono.git] / drivers / hfpmodem / voicecall.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  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 #include <gatchat.h>
33 #include <gatresult.h>
34
35 #include <ofono/log.h>
36 #include <ofono/modem.h>
37 #include <ofono/voicecall.h>
38
39 #include "common.h"
40 #include "hfp.h"
41
42 #include "hfpmodem.h"
43 #include "slc.h"
44
45 #define POLL_CLCC_INTERVAL 2000
46 #define POLL_CLCC_DELAY 50
47 #define EXPECT_RELEASE_DELAY 50
48 #define CLIP_TIMEOUT 500
49
50 static const char *none_prefix[] = { NULL };
51 static const char *clcc_prefix[] = { "+CLCC:", NULL };
52
53 struct voicecall_data {
54         GAtChat *chat;
55         GSList *calls;
56         unsigned int ag_features;
57         unsigned int ag_mpty_features;
58         unsigned char cind_pos[HFP_INDICATOR_LAST];
59         int cind_val[HFP_INDICATOR_LAST];
60         unsigned int local_release;
61         unsigned int clcc_source;
62         unsigned int expect_release_source;
63         unsigned int clip_source;
64 };
65
66 struct release_id_req {
67         struct ofono_voicecall *vc;
68         ofono_voicecall_cb_t cb;
69         void *data;
70         int id;
71 };
72
73 struct change_state_req {
74         struct ofono_voicecall *vc;
75         ofono_voicecall_cb_t cb;
76         void *data;
77         int affected_types;
78 };
79
80 static gboolean poll_clcc(gpointer user_data);
81
82 static GSList *find_dialing(GSList *calls)
83 {
84         GSList *c;
85
86         c = g_slist_find_custom(calls, GINT_TO_POINTER(CALL_STATUS_DIALING),
87                                 at_util_call_compare_by_status);
88
89         if (c == NULL)
90                 c = g_slist_find_custom(calls,
91                                         GINT_TO_POINTER(CALL_STATUS_ALERTING),
92                                         at_util_call_compare_by_status);
93
94         return c;
95 }
96
97 static void voicecall_notify(gpointer value, gpointer user)
98 {
99         struct ofono_call *call = value;
100         struct ofono_voicecall *vc = user;
101
102         ofono_voicecall_notify(vc, call);
103 }
104
105 static struct ofono_call *create_call(struct ofono_voicecall *vc, int type,
106                                         int direction, int status,
107                                         const char *num, int num_type, int clip)
108 {
109         struct voicecall_data *d = ofono_voicecall_get_data(vc);
110         struct ofono_call *call;
111
112         /* Generate a call structure for the waiting call */
113         call = g_try_new(struct ofono_call, 1);
114         if (call == NULL)
115                 return NULL;
116
117         ofono_call_init(call);
118
119         call->id = ofono_voicecall_get_next_callid(vc);
120         call->type = type;
121         call->direction = direction;
122         call->status = status;
123
124         if (clip != 2) {
125                 strncpy(call->phone_number.number, num,
126                         OFONO_MAX_PHONE_NUMBER_LENGTH);
127                 call->phone_number.type = num_type;
128         }
129
130         d->calls = g_slist_insert_sorted(d->calls, call, at_util_call_compare);
131
132         call->clip_validity = clip;
133
134         return call;
135 }
136
137 static void release_call(struct ofono_voicecall *vc, struct ofono_call *call)
138 {
139         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
140         enum ofono_disconnect_reason reason;
141
142         if (call == NULL)
143                 return;
144
145         if (vd->local_release & (1 << call->id))
146                 reason = OFONO_DISCONNECT_REASON_LOCAL_HANGUP;
147         else
148                 reason = OFONO_DISCONNECT_REASON_REMOTE_HANGUP;
149
150         ofono_voicecall_disconnected(vc, call->id, reason, NULL);
151         vd->local_release &= ~(1 << call->id);
152
153         g_free(call);
154 }
155
156 static void release_all_calls(struct ofono_voicecall *vc)
157 {
158         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
159         GSList *l;
160         struct ofono_call *call;
161
162         for (l = vd->calls; l; l = l->next) {
163                 call = l->data;
164
165                 release_call(vc, call);
166         }
167
168         g_slist_free(vd->calls);
169         vd->calls = NULL;
170 }
171
172 static void release_with_status(struct ofono_voicecall *vc, int status)
173 {
174         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
175         GSList *p = NULL;
176         GSList *c = vd->calls;
177         GSList *t;
178         struct ofono_call *call;
179
180         while (c) {
181                 call = c->data;
182
183                 if (call->status != status) {
184                         p = c;
185                         c = c->next;
186                         continue;
187                 }
188
189                 release_call(vc, call);
190
191                 if (p)
192                         p->next = c->next;
193                 else
194                         vd->calls = c->next;
195
196                 t = c;
197                 c = c->next;
198                 g_slist_free_1(t);
199         }
200
201         if (vd->expect_release_source) {
202                 g_source_remove(vd->expect_release_source);
203                 vd->expect_release_source = 0;
204         }
205 }
206
207 static void clcc_poll_cb(gboolean ok, GAtResult *result, gpointer user_data)
208 {
209         struct ofono_voicecall *vc = user_data;
210         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
211         GSList *calls;
212         GSList *n, *o;
213         struct ofono_call *nc, *oc;
214         unsigned int num_active = 0;
215         unsigned int num_held = 0;
216         GSList *notify_calls = NULL;
217         unsigned int mpty_ids;
218
219         if (!ok)
220                 return;
221
222         calls = at_util_parse_clcc(result, &mpty_ids);
223
224         n = calls;
225         o = vd->calls;
226
227         while (n || o) {
228                 nc = n ? n->data : NULL;
229                 oc = o ? o->data : NULL;
230
231                 if (nc && (nc->status == CALL_STATUS_ACTIVE))
232                         num_active++;
233
234                 if (nc && (nc->status == CALL_STATUS_HELD))
235                         num_held++;
236
237                 if (oc && (nc == NULL || (nc->id > oc->id))) {
238                         enum ofono_disconnect_reason reason;
239
240                         if (vd->local_release & (1 << oc->id))
241                                 reason = OFONO_DISCONNECT_REASON_LOCAL_HANGUP;
242                         else
243                                 reason = OFONO_DISCONNECT_REASON_REMOTE_HANGUP;
244
245                         if (!oc->type)
246                                 ofono_voicecall_disconnected(vc, oc->id,
247                                                                 reason, NULL);
248
249                         vd->local_release &= ~(1 << oc->id);
250
251                         o = o->next;
252                 } else if (nc && (oc == NULL || (nc->id < oc->id))) {
253                         /* new call, signal it */
254                         if (nc->type == 0)
255                                 notify_calls = g_slist_append(notify_calls, nc);
256
257                         n = n->next;
258                 } else {
259                         /* Always use the clip_validity from old call
260                          * the only place this is truly told to us is
261                          * in the CLIP notify, the rest are fudged
262                          * anyway.  Useful when RING, CLIP is used,
263                          * and we're forced to use CLCC and clip_validity
264                          * is 1
265                          */
266                         nc->clip_validity = oc->clip_validity;
267
268                         if (memcmp(nc, oc, sizeof(struct ofono_call)) &&
269                                         !nc->type)
270                                 notify_calls = g_slist_prepend(notify_calls,
271                                                                         nc);
272
273                         n = n->next;
274                         o = o->next;
275                 }
276         }
277
278         /*
279          * Disconnections were already reported, so process the rest of the
280          * notifications. Note that the new calls are placed at the end of the
281          * list, after other state changes
282          */
283         g_slist_foreach(notify_calls, voicecall_notify, vc);
284         g_slist_free(notify_calls);
285
286         ofono_voicecall_mpty_hint(vc, mpty_ids);
287
288         g_slist_foreach(vd->calls, (GFunc) g_free, NULL);
289         g_slist_free(vd->calls);
290
291         vd->calls = calls;
292
293         /* If either active/held call is more than 1, we are in mpty calls.
294          * we won't get indicator update if any of them is released by CHLD=1x.
295          * So we have to poll it.
296          */
297         if (num_active > 1 || num_held > 1)
298                 vd->clcc_source = g_timeout_add(POLL_CLCC_INTERVAL, poll_clcc,
299                                                         vc);
300 }
301
302 static gboolean poll_clcc(gpointer user_data)
303 {
304         struct ofono_voicecall *vc = user_data;
305         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
306
307         g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
308                                 clcc_poll_cb, vc, NULL);
309
310         vd->clcc_source = 0;
311
312         return FALSE;
313 }
314
315 static void generic_cb(gboolean ok, GAtResult *result, gpointer user_data)
316 {
317         struct change_state_req *req = user_data;
318         struct voicecall_data *vd = ofono_voicecall_get_data(req->vc);
319         struct ofono_error error;
320
321         decode_at_error(&error, g_at_result_final_response(result));
322
323         if (ok && req->affected_types) {
324                 GSList *l;
325                 struct ofono_call *call;
326
327                 for (l = vd->calls; l; l = l->next) {
328                         call = l->data;
329
330                         if (req->affected_types & (1 << call->status))
331                                 vd->local_release |= (1 << call->id);
332                 }
333         }
334
335         req->cb(&error, req->data);
336 }
337
338 static void atd_cb(gboolean ok, GAtResult *result, gpointer user_data)
339 {
340         struct cb_data *cbd = user_data;
341         struct ofono_voicecall *vc = cbd->user;
342         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
343         ofono_voicecall_cb_t cb = cbd->cb;
344         int type = 128;
345         int validity = 2;
346         struct ofono_error error;
347         struct ofono_call *call;
348         GSList *l;
349
350         decode_at_error(&error, g_at_result_final_response(result));
351
352         if (!ok)
353                 goto out;
354
355         /* On a success, make sure to put all active calls on hold */
356         for (l = vd->calls; l; l = l->next) {
357                 call = l->data;
358
359                 if (call->status != CALL_STATUS_ACTIVE)
360                         continue;
361
362                 call->status = CALL_STATUS_HELD;
363                 ofono_voicecall_notify(vc, call);
364         }
365
366         call = create_call(vc, 0, 0, CALL_STATUS_DIALING, NULL, type, validity);
367         if (call == NULL) {
368                 ofono_error("Unable to allocate call, "
369                                 "call tracking will fail!");
370                 return;
371         }
372
373 out:
374         cb(&error, cbd->data);
375 }
376
377 static void hfp_dial(struct ofono_voicecall *vc,
378                         const struct ofono_phone_number *ph,
379                         enum ofono_clir_option clir, ofono_voicecall_cb_t cb,
380                         void *data)
381 {
382         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
383         struct cb_data *cbd = cb_data_new(cb, data);
384         char buf[256];
385
386         cbd->user = vc;
387         if (ph->type == 145)
388                 snprintf(buf, sizeof(buf), "ATD+%s", ph->number);
389         else
390                 snprintf(buf, sizeof(buf), "ATD%s", ph->number);
391
392         strcat(buf, ";");
393
394         if (g_at_chat_send(vd->chat, buf, none_prefix,
395                                 atd_cb, cbd, g_free) > 0)
396                 return;
397
398         g_free(cbd);
399
400         CALLBACK_WITH_FAILURE(cb, data);
401 }
402
403 static void hfp_template(const char *cmd, struct ofono_voicecall *vc,
404                         GAtResultFunc result_cb, unsigned int affected_types,
405                         ofono_voicecall_cb_t cb, void *data)
406 {
407         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
408         struct change_state_req *req = g_try_new0(struct change_state_req, 1);
409
410         if (req == NULL)
411                 goto error;
412
413         req->vc = vc;
414         req->cb = cb;
415         req->data = data;
416         req->affected_types = affected_types;
417
418         if (g_at_chat_send(vd->chat, cmd, none_prefix,
419                                 result_cb, req, g_free) > 0)
420                 return;
421
422 error:
423         g_free(req);
424
425         CALLBACK_WITH_FAILURE(cb, data);
426 }
427
428 static void hfp_answer(struct ofono_voicecall *vc,
429                         ofono_voicecall_cb_t cb, void *data)
430 {
431         hfp_template("ATA", vc, generic_cb, 0, cb, data);
432 }
433
434 static void hfp_hangup(struct ofono_voicecall *vc,
435                         ofono_voicecall_cb_t cb, void *data)
436 {
437         unsigned int affected = (1 << CALL_STATUS_INCOMING) |
438                                 (1 << CALL_STATUS_DIALING) |
439                                 (1 << CALL_STATUS_ALERTING) |
440                                 (1 << CALL_STATUS_ACTIVE);
441
442         /* Hangup current active call */
443         hfp_template("AT+CHUP", vc, generic_cb, affected, cb, data);
444 }
445
446 static void hfp_hold_all_active(struct ofono_voicecall *vc,
447                                 ofono_voicecall_cb_t cb, void *data)
448 {
449         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
450
451         if (vd->ag_mpty_features & HFP_AG_CHLD_2) {
452                 hfp_template("AT+CHLD=2", vc, generic_cb, 0, cb, data);
453                 return;
454         }
455
456         CALLBACK_WITH_FAILURE(cb, data);
457 }
458
459 static void hfp_release_all_held(struct ofono_voicecall *vc,
460                                 ofono_voicecall_cb_t cb, void *data)
461 {
462         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
463         unsigned int held_status = 1 << CALL_STATUS_HELD;
464
465         if (vd->ag_mpty_features & HFP_AG_CHLD_0) {
466                 hfp_template("AT+CHLD=0", vc, generic_cb, held_status,
467                                 cb, data);
468                 return;
469         }
470
471         CALLBACK_WITH_FAILURE(cb, data);
472 }
473
474 static void hfp_set_udub(struct ofono_voicecall *vc,
475                         ofono_voicecall_cb_t cb, void *data)
476 {
477         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
478         unsigned int incoming_or_waiting = 1 << CALL_STATUS_WAITING;
479
480         if (vd->ag_mpty_features & HFP_AG_CHLD_0) {
481                 hfp_template("AT+CHLD=0", vc, generic_cb, incoming_or_waiting,
482                                 cb, data);
483                 return;
484         }
485
486         CALLBACK_WITH_FAILURE(cb, data);
487 }
488
489 static gboolean expect_release(gpointer user_data)
490 {
491         struct ofono_voicecall *vc = user_data;
492         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
493
494         g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
495                                 clcc_poll_cb, vc, NULL);
496
497         vd->expect_release_source = 0;
498
499         return FALSE;
500 }
501
502 static void release_all_active_cb(gboolean ok, GAtResult *result,
503                                                         gpointer user_data)
504 {
505         struct change_state_req *req = user_data;
506         struct voicecall_data *vd = ofono_voicecall_get_data(req->vc);
507
508         if (!ok)
509                 goto out;
510
511         if (vd->expect_release_source)
512                 g_source_remove(vd->expect_release_source);
513
514         /*
515          * Some phones, like Nokia 500, do not send CIEV after accepting
516          * the CHLD=1 command, even though the spec states that they should.
517          * So simply poll to force the status update if the AG is misbehaving.
518          */
519         vd->expect_release_source = g_timeout_add(EXPECT_RELEASE_DELAY,
520                                                         expect_release,
521                                                         req->vc);
522
523 out:
524         generic_cb(ok, result, user_data);
525 }
526
527 static void hfp_release_all_active(struct ofono_voicecall *vc,
528                                         ofono_voicecall_cb_t cb, void *data)
529 {
530         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
531
532         if (vd->ag_mpty_features & HFP_AG_CHLD_1) {
533                 hfp_template("AT+CHLD=1", vc, release_all_active_cb, 0x1, cb,
534                                                                         data);
535                 return;
536         }
537
538         CALLBACK_WITH_FAILURE(cb, data);
539 }
540
541 static void release_id_cb(gboolean ok, GAtResult *result,
542                                 gpointer user_data)
543 {
544         struct release_id_req *req = user_data;
545         struct voicecall_data *vd = ofono_voicecall_get_data(req->vc);
546         struct ofono_error error;
547
548         decode_at_error(&error, g_at_result_final_response(result));
549
550         if (ok)
551                 vd->local_release |= (1 << req->id);
552
553         req->cb(&error, req->data);
554 }
555
556 static void hfp_release_specific(struct ofono_voicecall *vc, int id,
557                                 ofono_voicecall_cb_t cb, void *data)
558 {
559         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
560         struct release_id_req *req = NULL;
561         char buf[32];
562
563         if (!(vd->ag_mpty_features & HFP_AG_CHLD_1x))
564                 goto error;
565
566         req = g_try_new0(struct release_id_req, 1);
567
568         if (req == NULL)
569                 goto error;
570
571         req->vc = vc;
572         req->cb = cb;
573         req->data = data;
574         req->id = id;
575
576         snprintf(buf, sizeof(buf), "AT+CHLD=1%d", id);
577
578         if (g_at_chat_send(vd->chat, buf, none_prefix,
579                                 release_id_cb, req, g_free) > 0)
580                 return;
581
582 error:
583         g_free(req);
584
585         CALLBACK_WITH_FAILURE(cb, data);
586 }
587
588 static void hfp_private_chat(struct ofono_voicecall *vc, int id,
589                                 ofono_voicecall_cb_t cb, void *data)
590 {
591         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
592         char buf[32];
593
594         if (vd->ag_mpty_features & HFP_AG_CHLD_2x) {
595                 snprintf(buf, sizeof(buf), "AT+CHLD=2%d", id);
596
597                 hfp_template(buf, vc, generic_cb, 0, cb, data);
598
599                 return;
600         }
601
602         CALLBACK_WITH_FAILURE(cb, data);
603 }
604
605 static void hfp_create_multiparty(struct ofono_voicecall *vc,
606                                         ofono_voicecall_cb_t cb, void *data)
607 {
608         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
609
610         if (vd->ag_mpty_features & HFP_AG_CHLD_3) {
611                 hfp_template("AT+CHLD=3", vc, generic_cb, 0, cb, data);
612
613                 return;
614         }
615
616         CALLBACK_WITH_FAILURE(cb, data);
617 }
618
619 static void hfp_transfer(struct ofono_voicecall *vc,
620                         ofono_voicecall_cb_t cb, void *data)
621 {
622         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
623         /* Transfer can puts held & active calls together and disconnects
624          * from both.  However, some networks support transferring of
625          * dialing/ringing calls as well.
626          */
627         unsigned int transfer = 0x1 | 0x2 | 0x4 | 0x8;
628
629         if (vd->ag_mpty_features & HFP_AG_CHLD_4) {
630                 hfp_template("AT+CHLD=4", vc, generic_cb, transfer, cb, data);
631
632                 return;
633         }
634
635         CALLBACK_WITH_FAILURE(cb, data);
636 }
637
638 static void hfp_send_dtmf(struct ofono_voicecall *vc, const char *dtmf,
639                         ofono_voicecall_cb_t cb, void *data)
640 {
641         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
642         struct change_state_req *req = g_try_new0(struct change_state_req, 1);
643         char *buf;
644         int s;
645
646         if (req == NULL)
647                 goto error;
648
649         req->vc = vc;
650         req->cb = cb;
651         req->data = data;
652         req->affected_types = 0;
653
654         /* strlen("AT+VTS=) = 7 + NULL */
655         buf = g_try_new(char, strlen(dtmf) + 8);
656         if (buf == NULL)
657                 goto error;
658
659         sprintf(buf, "AT+VTS=%s", dtmf);
660
661         s = g_at_chat_send(vd->chat, buf, none_prefix,
662                                 generic_cb, req, g_free);
663
664         g_free(buf);
665
666         if (s > 0)
667                 return;
668
669 error:
670         g_free(req);
671
672         CALLBACK_WITH_FAILURE(cb, data);
673 }
674
675 static void no_carrier_notify(GAtResult *result, gpointer user_data)
676 {
677         DBG("");
678 }
679
680 static void ccwa_notify(GAtResult *result, gpointer user_data)
681 {
682         struct ofono_voicecall *vc = user_data;
683         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
684         GAtResultIter iter;
685         const char *num;
686         int num_type, validity;
687         struct ofono_call *call;
688
689         /* CCWA can repeat, ignore if we already have an waiting call */
690         if (g_slist_find_custom(vd->calls,
691                                 GINT_TO_POINTER(CALL_STATUS_WAITING),
692                                 at_util_call_compare_by_status))
693                 return;
694
695         g_at_result_iter_init(&iter, result);
696
697         if (!g_at_result_iter_next(&iter, "+CCWA:"))
698                 return;
699
700         if (!g_at_result_iter_next_string(&iter, &num))
701                 return;
702
703         if (!g_at_result_iter_next_number(&iter, &num_type))
704                 return;
705
706         if (strlen(num) > 0)
707                 validity = 0;
708         else
709                 validity = 2;
710
711         DBG("ccwa_notify: %s %d %d", num, num_type, validity);
712
713         call = create_call(vc, 0, 1, CALL_STATUS_WAITING, num, num_type,
714                                 validity);
715
716         if (call == NULL) {
717                 ofono_error("malloc call struct failed.  "
718                                 "Call management is fubar");
719                 return;
720         }
721
722         ofono_voicecall_notify(vc, call);
723 }
724
725 static gboolean clip_timeout(gpointer user_data)
726 {
727         struct ofono_voicecall *vc = user_data;
728         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
729         GSList *l;
730         struct ofono_call *call;
731
732         l = g_slist_find_custom(vd->calls,
733                                 GINT_TO_POINTER(CALL_STATUS_INCOMING),
734                                 at_util_call_compare_by_status);
735
736         if (l == NULL)
737                 return FALSE;
738
739         call = l->data;
740
741         ofono_voicecall_notify(vc, call);
742
743         vd->clip_source = 0;
744
745         return FALSE;
746 }
747
748 static void ring_notify(GAtResult *result, gpointer user_data)
749 {
750         struct ofono_voicecall *vc = user_data;
751         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
752         struct ofono_call *call;
753         GSList *waiting;
754
755         /* RING can repeat, ignore if we already have an incoming call */
756         if (g_slist_find_custom(vd->calls,
757                                 GINT_TO_POINTER(CALL_STATUS_INCOMING),
758                                 at_util_call_compare_by_status))
759                 return;
760
761         waiting = g_slist_find_custom(vd->calls,
762                                         GINT_TO_POINTER(CALL_STATUS_WAITING),
763                                         at_util_call_compare_by_status);
764
765         /* If we started receiving RINGS but have a waiting call, most
766          * likely all other calls were dropped and we just didn't get
767          * notified yet, drop all other calls and update the status to
768          * incoming
769          */
770         if (waiting) {
771                 DBG("Triggering waiting -> incoming cleanup code");
772
773                 vd->calls = g_slist_remove_link(vd->calls, waiting);
774                 release_all_calls(vc);
775                 vd->calls = waiting;
776
777                 call = waiting->data;
778                 call->status = CALL_STATUS_INCOMING;
779                 ofono_voicecall_notify(vc, call);
780
781                 return;
782         }
783
784         /* Generate an incoming call of voice type */
785         call = create_call(vc, 0, 1, CALL_STATUS_INCOMING, NULL, 128, 2);
786
787         if (call == NULL)
788                 ofono_error("Couldn't create call, call management is fubar!");
789
790         /* We don't know the number must wait for CLIP to arrive before
791          * announcing the call. If timeout, we notify the call as it is.
792          */
793         vd->clip_source = g_timeout_add(CLIP_TIMEOUT, clip_timeout, vc);
794 }
795
796 static void clip_notify(GAtResult *result, gpointer user_data)
797 {
798         struct ofono_voicecall *vc = user_data;
799         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
800         GAtResultIter iter;
801         const char *num;
802         int type, validity;
803         GSList *l;
804         struct ofono_call *call;
805
806         l = g_slist_find_custom(vd->calls,
807                                 GINT_TO_POINTER(CALL_STATUS_INCOMING),
808                                 at_util_call_compare_by_status);
809
810         if (l == NULL) {
811                 ofono_error("CLIP for unknown call");
812                 return;
813         }
814
815         g_at_result_iter_init(&iter, result);
816
817         if (!g_at_result_iter_next(&iter, "+CLIP:"))
818                 return;
819
820         if (!g_at_result_iter_next_string(&iter, &num))
821                 return;
822
823         if (!g_at_result_iter_next_number(&iter, &type))
824                 return;
825
826         if (strlen(num) > 0)
827                 validity = 0;
828         else
829                 validity = 2;
830
831         /* Skip subaddr, satype, alpha and validity */
832         g_at_result_iter_skip_next(&iter);
833         g_at_result_iter_skip_next(&iter);
834         g_at_result_iter_skip_next(&iter);
835         g_at_result_iter_skip_next(&iter);
836
837         DBG("clip_notify: %s %d %d", num, type, validity);
838
839         call = l->data;
840
841         strncpy(call->phone_number.number, num,
842                 OFONO_MAX_PHONE_NUMBER_LENGTH);
843         call->phone_number.number[OFONO_MAX_PHONE_NUMBER_LENGTH] = '\0';
844         call->phone_number.type = type;
845         call->clip_validity = validity;
846
847         ofono_voicecall_notify(vc, call);
848
849         if (vd->clip_source) {
850                 g_source_remove(vd->clip_source);
851                 vd->clip_source = 0;
852         }
853 }
854
855 static void ciev_call_notify(struct ofono_voicecall *vc,
856                                 unsigned int value)
857 {
858         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
859         struct ofono_call *call;
860
861         switch (value) {
862         case 0:
863                 /* If call goes to 0, then we have no held or active calls
864                  * in the system.  The waiting calls are promoted to incoming
865                  * calls, dialing calls are kept.  This also handles the
866                  * situation when dialing and waiting calls exist
867                  */
868                 release_with_status(vc, CALL_STATUS_HELD);
869                 release_with_status(vc, CALL_STATUS_ACTIVE);
870
871                 /* Promote waiting to incoming if it is the last call */
872                 if (vd->calls && vd->calls->next == NULL) {
873                         call = vd->calls->data;
874
875                         if (call->status == CALL_STATUS_WAITING) {
876                                 call->status = CALL_STATUS_INCOMING;
877                                 ofono_voicecall_notify(vc, call);
878                         }
879                 }
880
881                 break;
882
883         case 1:
884         {
885                 GSList *l;
886
887                 /* In this case either dialing/alerting or the incoming call
888                  * is promoted to active
889                  */
890                 for (l = vd->calls; l; l = l->next) {
891                         call = l->data;
892
893                         if (call->status == CALL_STATUS_DIALING ||
894                                         call->status == CALL_STATUS_ALERTING ||
895                                         call->status == CALL_STATUS_INCOMING) {
896                                 call->status = CALL_STATUS_ACTIVE;
897                                 ofono_voicecall_notify(vc, call);
898                         }
899                 }
900
901                 break;
902         }
903
904         default:
905                 break;
906         }
907
908         vd->cind_val[HFP_INDICATOR_CALL] = value;
909 }
910
911 static void ciev_callsetup_notify(struct ofono_voicecall *vc,
912                                         unsigned int value)
913 {
914         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
915         unsigned int ciev_call = vd->cind_val[HFP_INDICATOR_CALL];
916         unsigned int ciev_callheld = vd->cind_val[HFP_INDICATOR_CALLHELD];
917         GSList *dialing;
918         GSList *waiting;
919
920         dialing = find_dialing(vd->calls);
921
922         waiting = g_slist_find_custom(vd->calls,
923                                         GINT_TO_POINTER(CALL_STATUS_WAITING),
924                                         at_util_call_compare_by_status);
925
926         /* This is a truly bizarre case not covered at all by the specification
927          * (yes, they are complete idiots).  Here we assume the other side is
928          * semi sane and will send callsetup updates in case the dialing call
929          * connects or the call waiting drops.  In which case we must poll
930          */
931         if (waiting && dialing) {
932                 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
933                                 clcc_poll_cb, vc, NULL);
934                 goto out;
935         }
936
937         switch (value) {
938         case 0:
939                 /* call=0 and callsetup=1: reject an incoming call
940                  * call=0 and callsetup=2,3: interrupt an outgoing call
941                  */
942                 if (ciev_call == 0) {
943                         release_all_calls(vc);
944                         goto out;
945                 }
946
947                 /* If call=1 and no call is waiting or dialing, the call is
948                  * active and we moved it to active state when call=1 arrived
949                  */
950                 if (waiting == NULL && dialing == NULL)
951                         goto out;
952
953                 /*
954                  * If call=1, in the waiting case we have to poll, since we
955                  * have no idea whether a waiting call gave up or we accepted
956                  * using release+accept or hold+accept
957                  *
958                  * If call=1, in the dialing + held case we have to poll as
959                  * well, we have no idea whether the call connected, or released
960                  */
961                 if (waiting == NULL && ciev_callheld == 0) {
962                         struct ofono_call *call = dialing->data;
963
964                         /* We assume that the implementation follows closely
965                          * the sequence of events in Figure 4.21.  That is
966                          * call=1 arrives first, then callsetup=0
967                          */
968
969                         call->status = CALL_STATUS_ACTIVE;
970                         ofono_voicecall_notify(vc, call);
971                 } else {
972                         g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
973                                         clcc_poll_cb, vc, NULL);
974                 }
975
976                 break;
977
978         case 1:
979                 /* Handled in RING/CCWA */
980                 break;
981
982         case 2:
983                 /* two cases of outgoing call: dial from HF or AG.
984                  * from HF: query and sync the phone number.
985                  * from AG: query and create call.
986                  */
987                 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
988                                 clcc_poll_cb, vc, NULL);
989                 break;
990
991         case 3:
992         {
993                 GSList *o = g_slist_find_custom(vd->calls,
994                                         GINT_TO_POINTER(CALL_STATUS_DIALING),
995                                         at_util_call_compare_by_status);
996
997                 if (o) {
998                         struct ofono_call *call = o->data;
999
1000                         call->status = CALL_STATUS_ALERTING;
1001                         ofono_voicecall_notify(vc, call);
1002                 }
1003
1004                 break;
1005         }
1006
1007         default:
1008                 break;
1009         }
1010
1011 out:
1012         vd->cind_val[HFP_INDICATOR_CALLSETUP] = value;
1013 }
1014
1015 static void ciev_callheld_notify(struct ofono_voicecall *vc,
1016                                         unsigned int value)
1017 {
1018         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1019         GSList *l;
1020         struct ofono_call *call;
1021         unsigned int callheld = vd->cind_val[HFP_INDICATOR_CALLHELD];
1022
1023         switch (value) {
1024         case 0:
1025                 /* We have to poll here, we have no idea whether the call was
1026                  * dropped using CHLD=0 or simply retrieved, or the two calls
1027                  * were merged
1028                  */
1029                 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
1030                                 clcc_poll_cb, vc, NULL);
1031                 break;
1032
1033         case 1:
1034                 if (vd->clcc_source) {
1035                         g_source_remove(vd->clcc_source);
1036                         vd->clcc_source = 0;
1037                 }
1038
1039                 /* We have to poll here, we have no idea whether the call was
1040                  * accepted by CHLD=1 or swapped by CHLD=2 or one call was
1041                  * chosed for private chat by CHLD=2x
1042                  */
1043                 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
1044                                 clcc_poll_cb, vc, NULL);
1045                 break;
1046         case 2:
1047                 if (callheld == 0) {
1048                         for (l = vd->calls; l; l = l->next) {
1049                                 call = l->data;
1050
1051                                 if (call->status != CALL_STATUS_ACTIVE)
1052                                         continue;
1053
1054                                 call->status = CALL_STATUS_HELD;
1055                                 ofono_voicecall_notify(vc, call);
1056                         }
1057                 } else if (callheld == 1) {
1058                         if (vd->clcc_source)
1059                                 g_source_remove(vd->clcc_source);
1060
1061                         /* We have to schedule a poll here, we have no idea
1062                          * whether active call was dropped by remote or if this
1063                          * is an intermediate state during call swap
1064                          */
1065                         vd->clcc_source = g_timeout_add(POLL_CLCC_DELAY,
1066                                                         poll_clcc, vc);
1067                 }
1068         }
1069
1070         vd->cind_val[HFP_INDICATOR_CALLHELD] = value;
1071 }
1072
1073 static void ciev_notify(GAtResult *result, gpointer user_data)
1074 {
1075         struct ofono_voicecall *vc = user_data;
1076         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1077         int index;
1078         int value;
1079         GAtResultIter iter;
1080
1081         g_at_result_iter_init(&iter, result);
1082
1083         if (!g_at_result_iter_next(&iter, "+CIEV:"))
1084                 return;
1085
1086         if (!g_at_result_iter_next_number(&iter, &index))
1087                 return;
1088
1089         if (!g_at_result_iter_next_number(&iter, &value))
1090                 return;
1091
1092         if (index == vd->cind_pos[HFP_INDICATOR_CALL])
1093                 ciev_call_notify(vc, value);
1094         else if (index == vd->cind_pos[HFP_INDICATOR_CALLSETUP])
1095                 ciev_callsetup_notify(vc, value);
1096         else if (index == vd->cind_pos[HFP_INDICATOR_CALLHELD])
1097                 ciev_callheld_notify(vc, value);
1098 }
1099
1100 static void hfp_clcc_cb(gboolean ok, GAtResult *result, gpointer user_data)
1101 {
1102         struct ofono_voicecall *vc = user_data;
1103         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1104         unsigned int mpty_ids;
1105
1106         if (!ok)
1107                 return;
1108
1109         vd->calls = at_util_parse_clcc(result, &mpty_ids);
1110
1111         g_slist_foreach(vd->calls, voicecall_notify, vc);
1112         ofono_voicecall_mpty_hint(vc, mpty_ids);
1113 }
1114
1115 static void hfp_voicecall_initialized(gboolean ok, GAtResult *result,
1116                                         gpointer user_data)
1117 {
1118         struct ofono_voicecall *vc = user_data;
1119         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1120
1121         DBG("hfp_voicecall_init: registering to notifications");
1122
1123         g_at_chat_register(vd->chat, "RING", ring_notify, FALSE, vc, NULL);
1124         g_at_chat_register(vd->chat, "+CLIP:", clip_notify, FALSE, vc, NULL);
1125         g_at_chat_register(vd->chat, "+CIEV:", ciev_notify, FALSE, vc, NULL);
1126         g_at_chat_register(vd->chat, "+CCWA:", ccwa_notify, FALSE, vc, NULL);
1127
1128         g_at_chat_register(vd->chat, "NO CARRIER",
1129                                 no_carrier_notify, FALSE, vc, NULL);
1130
1131         ofono_voicecall_register(vc);
1132
1133         /* Populate the call list */
1134         g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix, hfp_clcc_cb, vc, NULL);
1135 }
1136
1137 static int hfp_voicecall_probe(struct ofono_voicecall *vc, unsigned int vendor,
1138                                 gpointer user_data)
1139 {
1140         struct hfp_slc_info *info = user_data;
1141         struct voicecall_data *vd;
1142
1143         vd = g_new0(struct voicecall_data, 1);
1144
1145         vd->chat = g_at_chat_clone(info->chat);
1146         vd->ag_features = info->ag_features;
1147         vd->ag_mpty_features = info->ag_mpty_features;
1148
1149         memcpy(vd->cind_pos, info->cind_pos, HFP_INDICATOR_LAST);
1150         memcpy(vd->cind_val, info->cind_val, HFP_INDICATOR_LAST);
1151
1152         ofono_voicecall_set_data(vc, vd);
1153
1154         g_at_chat_send(vd->chat, "AT+CLIP=1", NULL, NULL, NULL, NULL);
1155         g_at_chat_send(vd->chat, "AT+CCWA=1", NULL,
1156                                 hfp_voicecall_initialized, vc, NULL);
1157         return 0;
1158 }
1159
1160 static void hfp_voicecall_remove(struct ofono_voicecall *vc)
1161 {
1162         struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1163
1164         if (vd->clcc_source)
1165                 g_source_remove(vd->clcc_source);
1166
1167         if (vd->clip_source)
1168                 g_source_remove(vd->clip_source);
1169
1170         if (vd->expect_release_source)
1171                 g_source_remove(vd->expect_release_source);
1172
1173         g_slist_foreach(vd->calls, (GFunc) g_free, NULL);
1174         g_slist_free(vd->calls);
1175
1176         ofono_voicecall_set_data(vc, NULL);
1177
1178         g_at_chat_unref(vd->chat);
1179         g_free(vd);
1180 }
1181
1182 static struct ofono_voicecall_driver driver = {
1183         .name                   = "hfpmodem",
1184         .probe                  = hfp_voicecall_probe,
1185         .remove                 = hfp_voicecall_remove,
1186         .dial                   = hfp_dial,
1187         .answer                 = hfp_answer,
1188         .hangup_active          = hfp_hangup,
1189         .hold_all_active        = hfp_hold_all_active,
1190         .release_all_held       = hfp_release_all_held,
1191         .set_udub               = hfp_set_udub,
1192         .release_all_active     = hfp_release_all_active,
1193         .release_specific       = hfp_release_specific,
1194         .private_chat           = hfp_private_chat,
1195         .create_multiparty      = hfp_create_multiparty,
1196         .transfer               = hfp_transfer,
1197         .deflect                = NULL,
1198         .swap_without_accept    = NULL,
1199         .send_tones             = hfp_send_dtmf
1200 };
1201
1202 void hfp_voicecall_init(void)
1203 {
1204         ofono_voicecall_driver_register(&driver);
1205 }
1206
1207 void hfp_voicecall_exit(void)
1208 {
1209         ofono_voicecall_driver_unregister(&driver);
1210 }