3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
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.
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.
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
33 #include <gatresult.h>
35 #include <ofono/log.h>
36 #include <ofono/modem.h>
37 #include <ofono/voicecall.h>
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
51 static const char *none_prefix[] = { NULL };
52 static const char *clcc_prefix[] = { "+CLCC:", NULL };
54 struct voicecall_data {
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;
67 struct release_id_req {
68 struct ofono_voicecall *vc;
69 ofono_voicecall_cb_t cb;
74 struct change_state_req {
75 struct ofono_voicecall *vc;
76 ofono_voicecall_cb_t cb;
81 static gboolean poll_clcc(gpointer user_data);
83 static GSList *find_dialing(GSList *calls)
87 c = g_slist_find_custom(calls, GINT_TO_POINTER(CALL_STATUS_DIALING),
88 at_util_call_compare_by_status);
91 c = g_slist_find_custom(calls,
92 GINT_TO_POINTER(CALL_STATUS_ALERTING),
93 at_util_call_compare_by_status);
98 static void voicecall_notify(gpointer value, gpointer user)
100 struct ofono_call *call = value;
101 struct ofono_voicecall *vc = user;
103 ofono_voicecall_notify(vc, call);
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)
110 struct voicecall_data *d = ofono_voicecall_get_data(vc);
111 struct ofono_call *call;
113 /* Generate a call structure for the waiting call */
114 call = g_try_new(struct ofono_call, 1);
118 ofono_call_init(call);
120 call->id = ofono_voicecall_get_next_callid(vc);
122 call->direction = direction;
123 call->status = status;
126 strncpy(call->phone_number.number, num,
127 OFONO_MAX_PHONE_NUMBER_LENGTH);
128 call->phone_number.type = num_type;
131 d->calls = g_slist_insert_sorted(d->calls, call, at_util_call_compare);
133 call->clip_validity = clip;
138 static void release_call(struct ofono_voicecall *vc, struct ofono_call *call)
140 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
141 enum ofono_disconnect_reason reason;
146 if (vd->local_release & (1 << call->id))
147 reason = OFONO_DISCONNECT_REASON_LOCAL_HANGUP;
149 reason = OFONO_DISCONNECT_REASON_REMOTE_HANGUP;
151 ofono_voicecall_disconnected(vc, call->id, reason, NULL);
152 vd->local_release &= ~(1 << call->id);
157 static void release_all_calls(struct ofono_voicecall *vc)
159 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
161 struct ofono_call *call;
163 for (l = vd->calls; l; l = l->next) {
166 release_call(vc, call);
169 g_slist_free(vd->calls);
173 static void release_with_status(struct ofono_voicecall *vc, int status)
175 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
177 GSList *c = vd->calls;
179 struct ofono_call *call;
184 if (call->status != status) {
190 release_call(vc, call);
202 if (vd->expect_release_source) {
203 g_source_remove(vd->expect_release_source);
204 vd->expect_release_source = 0;
208 static void clcc_poll_cb(gboolean ok, GAtResult *result, gpointer user_data)
210 struct ofono_voicecall *vc = user_data;
211 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
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;
223 calls = at_util_parse_clcc(result, &mpty_ids);
229 nc = n ? n->data : NULL;
230 oc = o ? o->data : NULL;
232 if (nc && (nc->status == CALL_STATUS_ACTIVE))
235 if (nc && (nc->status == CALL_STATUS_HELD))
238 if (oc && (nc == NULL || (nc->id > oc->id))) {
239 enum ofono_disconnect_reason reason;
241 if (vd->local_release & (1 << oc->id))
242 reason = OFONO_DISCONNECT_REASON_LOCAL_HANGUP;
244 reason = OFONO_DISCONNECT_REASON_REMOTE_HANGUP;
247 ofono_voicecall_disconnected(vc, oc->id,
250 vd->local_release &= ~(1 << oc->id);
253 } else if (nc && (oc == NULL || (nc->id < oc->id))) {
254 /* new call, signal it */
256 notify_calls = g_slist_append(notify_calls, nc);
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
267 nc->clip_validity = oc->clip_validity;
269 if (memcmp(nc, oc, sizeof(struct ofono_call)) &&
271 notify_calls = g_slist_prepend(notify_calls,
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
284 g_slist_foreach(notify_calls, voicecall_notify, vc);
285 g_slist_free(notify_calls);
287 ofono_voicecall_mpty_hint(vc, mpty_ids);
289 g_slist_foreach(vd->calls, (GFunc) g_free, NULL);
290 g_slist_free(vd->calls);
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.
298 if (num_active > 1 || num_held > 1)
299 vd->clcc_source = g_timeout_add(POLL_CLCC_INTERVAL, poll_clcc,
303 static gboolean poll_clcc(gpointer user_data)
305 struct ofono_voicecall *vc = user_data;
306 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
308 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
309 clcc_poll_cb, vc, NULL);
316 static void generic_cb(gboolean ok, GAtResult *result, gpointer user_data)
318 struct change_state_req *req = user_data;
319 struct voicecall_data *vd = ofono_voicecall_get_data(req->vc);
320 struct ofono_error error;
322 decode_at_error(&error, g_at_result_final_response(result));
324 if (ok && req->affected_types) {
326 struct ofono_call *call;
328 for (l = vd->calls; l; l = l->next) {
331 if (req->affected_types & (1 << call->status))
332 vd->local_release |= (1 << call->id);
336 req->cb(&error, req->data);
339 static void atd_cb(gboolean ok, GAtResult *result, gpointer user_data)
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;
347 struct ofono_error error;
348 struct ofono_call *call;
351 decode_at_error(&error, g_at_result_final_response(result));
356 /* On a success, make sure to put all active calls on hold */
357 for (l = vd->calls; l; l = l->next) {
360 if (call->status != CALL_STATUS_ACTIVE)
363 call->status = CALL_STATUS_HELD;
364 ofono_voicecall_notify(vc, call);
367 call = create_call(vc, 0, 0, CALL_STATUS_DIALING, NULL, type, validity);
369 ofono_error("Unable to allocate call, "
370 "call tracking will fail!");
375 cb(&error, cbd->data);
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,
383 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
384 struct cb_data *cbd = cb_data_new(cb, data);
389 snprintf(buf, sizeof(buf), "ATD+%s", ph->number);
391 snprintf(buf, sizeof(buf), "ATD%s", ph->number);
395 if (g_at_chat_send(vd->chat, buf, none_prefix,
396 atd_cb, cbd, g_free) > 0)
401 CALLBACK_WITH_FAILURE(cb, data);
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)
408 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
409 struct change_state_req *req = g_try_new0(struct change_state_req, 1);
417 req->affected_types = affected_types;
419 if (g_at_chat_send(vd->chat, cmd, none_prefix,
420 result_cb, req, g_free) > 0)
426 CALLBACK_WITH_FAILURE(cb, data);
429 static void hfp_answer(struct ofono_voicecall *vc,
430 ofono_voicecall_cb_t cb, void *data)
432 hfp_template("ATA", vc, generic_cb, 0, cb, data);
435 static void hfp_hangup(struct ofono_voicecall *vc,
436 ofono_voicecall_cb_t cb, void *data)
438 unsigned int affected = (1 << CALL_STATUS_INCOMING) |
439 (1 << CALL_STATUS_DIALING) |
440 (1 << CALL_STATUS_ALERTING) |
441 (1 << CALL_STATUS_ACTIVE);
443 /* Hangup current active call */
444 hfp_template("AT+CHUP", vc, generic_cb, affected, cb, data);
447 static void hfp_hold_all_active(struct ofono_voicecall *vc,
448 ofono_voicecall_cb_t cb, void *data)
450 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
452 if (vd->ag_mpty_features & HFP_AG_CHLD_2) {
453 hfp_template("AT+CHLD=2", vc, generic_cb, 0, cb, data);
457 CALLBACK_WITH_FAILURE(cb, data);
460 static void hfp_release_all_held(struct ofono_voicecall *vc,
461 ofono_voicecall_cb_t cb, void *data)
463 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
464 unsigned int held_status = 1 << CALL_STATUS_HELD;
466 if (vd->ag_mpty_features & HFP_AG_CHLD_0) {
467 hfp_template("AT+CHLD=0", vc, generic_cb, held_status,
472 CALLBACK_WITH_FAILURE(cb, data);
475 static void hfp_set_udub(struct ofono_voicecall *vc,
476 ofono_voicecall_cb_t cb, void *data)
478 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
479 unsigned int incoming_or_waiting = 1 << CALL_STATUS_WAITING;
481 if (vd->ag_mpty_features & HFP_AG_CHLD_0) {
482 hfp_template("AT+CHLD=0", vc, generic_cb, incoming_or_waiting,
487 CALLBACK_WITH_FAILURE(cb, data);
490 static gboolean expect_release(gpointer user_data)
492 struct ofono_voicecall *vc = user_data;
493 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
495 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
496 clcc_poll_cb, vc, NULL);
498 vd->expect_release_source = 0;
503 static gboolean expect_ring(gpointer user_data)
505 struct ofono_voicecall *vc = user_data;
506 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
508 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
509 clcc_poll_cb, vc, NULL);
516 static void release_all_active_cb(gboolean ok, GAtResult *result,
519 struct change_state_req *req = user_data;
520 struct voicecall_data *vd = ofono_voicecall_get_data(req->vc);
525 if (vd->expect_release_source)
526 g_source_remove(vd->expect_release_source);
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.
533 vd->expect_release_source = g_timeout_add(EXPECT_RELEASE_DELAY,
538 generic_cb(ok, result, user_data);
541 static void hfp_release_all_active(struct ofono_voicecall *vc,
542 ofono_voicecall_cb_t cb, void *data)
544 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
546 if (vd->ag_mpty_features & HFP_AG_CHLD_1) {
547 hfp_template("AT+CHLD=1", vc, release_all_active_cb, 0x1, cb,
552 CALLBACK_WITH_FAILURE(cb, data);
555 static void release_id_cb(gboolean ok, GAtResult *result,
558 struct release_id_req *req = user_data;
559 struct voicecall_data *vd = ofono_voicecall_get_data(req->vc);
560 struct ofono_error error;
562 decode_at_error(&error, g_at_result_final_response(result));
565 vd->local_release |= (1 << req->id);
567 req->cb(&error, req->data);
570 static void hfp_release_specific(struct ofono_voicecall *vc, int id,
571 ofono_voicecall_cb_t cb, void *data)
573 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
574 struct release_id_req *req = NULL;
577 if (!(vd->ag_mpty_features & HFP_AG_CHLD_1x))
580 req = g_try_new0(struct release_id_req, 1);
590 snprintf(buf, sizeof(buf), "AT+CHLD=1%d", id);
592 if (g_at_chat_send(vd->chat, buf, none_prefix,
593 release_id_cb, req, g_free) > 0)
599 CALLBACK_WITH_FAILURE(cb, data);
602 static void hfp_private_chat(struct ofono_voicecall *vc, int id,
603 ofono_voicecall_cb_t cb, void *data)
605 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
608 if (vd->ag_mpty_features & HFP_AG_CHLD_2x) {
609 snprintf(buf, sizeof(buf), "AT+CHLD=2%d", id);
611 hfp_template(buf, vc, generic_cb, 0, cb, data);
616 CALLBACK_WITH_FAILURE(cb, data);
619 static void hfp_create_multiparty(struct ofono_voicecall *vc,
620 ofono_voicecall_cb_t cb, void *data)
622 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
624 if (vd->ag_mpty_features & HFP_AG_CHLD_3) {
625 hfp_template("AT+CHLD=3", vc, generic_cb, 0, cb, data);
630 CALLBACK_WITH_FAILURE(cb, data);
633 static void hfp_transfer(struct ofono_voicecall *vc,
634 ofono_voicecall_cb_t cb, void *data)
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.
641 unsigned int transfer = 0x1 | 0x2 | 0x4 | 0x8;
643 if (vd->ag_mpty_features & HFP_AG_CHLD_4) {
644 hfp_template("AT+CHLD=4", vc, generic_cb, transfer, cb, data);
649 CALLBACK_WITH_FAILURE(cb, data);
652 static void hfp_send_dtmf(struct ofono_voicecall *vc, const char *dtmf,
653 ofono_voicecall_cb_t cb, void *data)
655 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
656 struct change_state_req *req = g_try_new0(struct change_state_req, 1);
666 req->affected_types = 0;
668 /* strlen("AT+VTS=) = 7 + NULL */
669 buf = g_try_new(char, strlen(dtmf) + 8);
673 sprintf(buf, "AT+VTS=%s", dtmf);
675 s = g_at_chat_send(vd->chat, buf, none_prefix,
676 generic_cb, req, g_free);
686 CALLBACK_WITH_FAILURE(cb, data);
689 static void no_carrier_notify(GAtResult *result, gpointer user_data)
694 static void ccwa_notify(GAtResult *result, gpointer user_data)
696 struct ofono_voicecall *vc = user_data;
697 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
700 int num_type, validity;
701 struct ofono_call *call;
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))
709 g_at_result_iter_init(&iter, result);
711 if (!g_at_result_iter_next(&iter, "+CCWA:"))
714 if (!g_at_result_iter_next_string(&iter, &num))
717 if (!g_at_result_iter_next_number(&iter, &num_type))
725 DBG("ccwa_notify: %s %d %d", num, num_type, validity);
727 call = create_call(vc, 0, 1, CALL_STATUS_WAITING, num, num_type,
731 ofono_error("malloc call struct failed. "
732 "Call management is fubar");
736 ofono_voicecall_notify(vc, call);
739 static gboolean clip_timeout(gpointer user_data)
741 struct ofono_voicecall *vc = user_data;
742 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
744 struct ofono_call *call;
746 l = g_slist_find_custom(vd->calls,
747 GINT_TO_POINTER(CALL_STATUS_INCOMING),
748 at_util_call_compare_by_status);
755 ofono_voicecall_notify(vc, call);
762 static void ring_notify(GAtResult *result, gpointer user_data)
764 struct ofono_voicecall *vc = user_data;
765 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
766 struct ofono_call *call;
769 if (vd->clip_source) {
770 g_source_remove(vd->clip_source);
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))
780 waiting = g_slist_find_custom(vd->calls,
781 GINT_TO_POINTER(CALL_STATUS_WAITING),
782 at_util_call_compare_by_status);
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
790 DBG("Triggering waiting -> incoming cleanup code");
792 vd->calls = g_slist_remove_link(vd->calls, waiting);
793 release_all_calls(vc);
796 call = waiting->data;
797 call->status = CALL_STATUS_INCOMING;
798 ofono_voicecall_notify(vc, call);
803 /* Generate an incoming call of voice type */
804 call = create_call(vc, 0, 1, CALL_STATUS_INCOMING, NULL, 128, 2);
807 ofono_error("Couldn't create call, call management is fubar!");
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.
812 vd->clip_source = g_timeout_add(CLIP_TIMEOUT, clip_timeout, vc);
815 static void clip_notify(GAtResult *result, gpointer user_data)
817 struct ofono_voicecall *vc = user_data;
818 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
823 struct ofono_call *call;
825 l = g_slist_find_custom(vd->calls,
826 GINT_TO_POINTER(CALL_STATUS_INCOMING),
827 at_util_call_compare_by_status);
830 ofono_error("CLIP for unknown call");
834 g_at_result_iter_init(&iter, result);
836 if (!g_at_result_iter_next(&iter, "+CLIP:"))
839 if (!g_at_result_iter_next_string(&iter, &num))
842 if (!g_at_result_iter_next_number(&iter, &type))
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);
856 DBG("clip_notify: %s %d %d", num, type, validity);
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;
866 ofono_voicecall_notify(vc, call);
868 if (vd->clip_source) {
869 g_source_remove(vd->clip_source);
874 static void ciev_call_notify(struct ofono_voicecall *vc,
877 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
878 struct ofono_call *call;
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
887 release_with_status(vc, CALL_STATUS_HELD);
888 release_with_status(vc, CALL_STATUS_ACTIVE);
890 /* Promote waiting to incoming if it is the last call */
891 if (vd->calls && vd->calls->next == NULL) {
892 call = vd->calls->data;
894 if (call->status == CALL_STATUS_WAITING) {
895 call->status = CALL_STATUS_INCOMING;
896 ofono_voicecall_notify(vc, call);
906 /* In this case either dialing/alerting or the incoming call
907 * is promoted to active
909 for (l = vd->calls; l; l = l->next) {
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);
927 vd->cind_val[HFP_INDICATOR_CALL] = value;
930 static void ciev_callsetup_notify(struct ofono_voicecall *vc,
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];
939 dialing = find_dialing(vd->calls);
941 waiting = g_slist_find_custom(vd->calls,
942 GINT_TO_POINTER(CALL_STATUS_WAITING),
943 at_util_call_compare_by_status);
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
950 if (waiting && dialing) {
951 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
952 clcc_poll_cb, vc, NULL);
958 /* call=0 and callsetup=1: reject an incoming call
959 * call=0 and callsetup=2,3: interrupt an outgoing call
961 if (ciev_call == 0) {
962 release_all_calls(vc);
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
969 if (waiting == NULL && dialing == NULL)
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
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
980 if (waiting == NULL && ciev_callheld == 0) {
981 struct ofono_call *call = dialing->data;
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
988 call->status = CALL_STATUS_ACTIVE;
989 ofono_voicecall_notify(vc, call);
991 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
992 clcc_poll_cb, vc, NULL);
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
1005 vd->clip_source = g_timeout_add(EXPECT_RING_DELAY,
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.
1014 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
1015 clcc_poll_cb, vc, NULL);
1020 GSList *o = g_slist_find_custom(vd->calls,
1021 GINT_TO_POINTER(CALL_STATUS_DIALING),
1022 at_util_call_compare_by_status);
1025 struct ofono_call *call = o->data;
1027 call->status = CALL_STATUS_ALERTING;
1028 ofono_voicecall_notify(vc, call);
1039 vd->cind_val[HFP_INDICATOR_CALLSETUP] = value;
1042 static void ciev_callheld_notify(struct ofono_voicecall *vc,
1045 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1047 struct ofono_call *call;
1048 unsigned int callheld = vd->cind_val[HFP_INDICATOR_CALLHELD];
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
1056 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
1057 clcc_poll_cb, vc, NULL);
1061 if (vd->clcc_source) {
1062 g_source_remove(vd->clcc_source);
1063 vd->clcc_source = 0;
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
1070 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix,
1071 clcc_poll_cb, vc, NULL);
1074 if (callheld == 0) {
1075 for (l = vd->calls; l; l = l->next) {
1078 if (call->status != CALL_STATUS_ACTIVE)
1081 call->status = CALL_STATUS_HELD;
1082 ofono_voicecall_notify(vc, call);
1084 } else if (callheld == 1) {
1085 if (vd->clcc_source)
1086 g_source_remove(vd->clcc_source);
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
1092 vd->clcc_source = g_timeout_add(POLL_CLCC_DELAY,
1097 vd->cind_val[HFP_INDICATOR_CALLHELD] = value;
1100 static void ciev_notify(GAtResult *result, gpointer user_data)
1102 struct ofono_voicecall *vc = user_data;
1103 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1108 g_at_result_iter_init(&iter, result);
1110 if (!g_at_result_iter_next(&iter, "+CIEV:"))
1113 if (!g_at_result_iter_next_number(&iter, &index))
1116 if (!g_at_result_iter_next_number(&iter, &value))
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);
1127 static void hfp_clcc_cb(gboolean ok, GAtResult *result, gpointer user_data)
1129 struct ofono_voicecall *vc = user_data;
1130 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1131 unsigned int mpty_ids;
1136 vd->calls = at_util_parse_clcc(result, &mpty_ids);
1138 g_slist_foreach(vd->calls, voicecall_notify, vc);
1139 ofono_voicecall_mpty_hint(vc, mpty_ids);
1142 static void hfp_voicecall_initialized(gboolean ok, GAtResult *result,
1145 struct ofono_voicecall *vc = user_data;
1146 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1148 DBG("hfp_voicecall_init: registering to notifications");
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);
1155 g_at_chat_register(vd->chat, "NO CARRIER",
1156 no_carrier_notify, FALSE, vc, NULL);
1158 ofono_voicecall_register(vc);
1160 /* Populate the call list */
1161 g_at_chat_send(vd->chat, "AT+CLCC", clcc_prefix, hfp_clcc_cb, vc, NULL);
1164 static int hfp_voicecall_probe(struct ofono_voicecall *vc, unsigned int vendor,
1167 struct hfp_slc_info *info = user_data;
1168 struct voicecall_data *vd;
1170 vd = g_new0(struct voicecall_data, 1);
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;
1176 memcpy(vd->cind_pos, info->cind_pos, HFP_INDICATOR_LAST);
1177 memcpy(vd->cind_val, info->cind_val, HFP_INDICATOR_LAST);
1179 ofono_voicecall_set_data(vc, vd);
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);
1187 static void hfp_voicecall_remove(struct ofono_voicecall *vc)
1189 struct voicecall_data *vd = ofono_voicecall_get_data(vc);
1191 if (vd->clcc_source)
1192 g_source_remove(vd->clcc_source);
1194 if (vd->clip_source)
1195 g_source_remove(vd->clip_source);
1197 if (vd->expect_release_source)
1198 g_source_remove(vd->expect_release_source);
1200 g_slist_foreach(vd->calls, (GFunc) g_free, NULL);
1201 g_slist_free(vd->calls);
1203 ofono_voicecall_set_data(vc, NULL);
1205 g_at_chat_unref(vd->chat);
1209 static struct ofono_voicecall_driver driver = {
1211 .probe = hfp_voicecall_probe,
1212 .remove = hfp_voicecall_remove,
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,
1225 .swap_without_accept = NULL,
1226 .send_tones = hfp_send_dtmf
1229 void hfp_voicecall_init(void)
1231 ofono_voicecall_driver_register(&driver);
1234 void hfp_voicecall_exit(void)
1236 ofono_voicecall_driver_unregister(&driver);