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