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
42 #define MAX_VOICE_CALLS 16
44 #define VOICECALL_FLAG_SIM_ECC_READY 0x1
45 #define VOICECALL_FLAG_STK_MODEM_CALLSETUP 0x2
47 #define SETTINGS_STORE "voicecall"
48 #define SETTINGS_GROUP "Settings"
50 GSList *g_drivers = NULL;
52 struct ofono_voicecall {
55 GSList *multiparty_list;
56 GHashTable *en_list; /* emergency number list */
57 GSList *sim_en_list; /* Emergency numbers already read from SIM */
58 GSList *new_sim_en_list; /* Emergency numbers being read from SIM */
59 char **nw_en_list; /* Emergency numbers from modem/network */
62 struct ofono_sim *sim;
63 struct ofono_sim_context *sim_context;
64 unsigned int sim_watch;
65 unsigned int sim_state_watch;
66 const struct ofono_voicecall_driver *driver;
68 struct ofono_atom *atom;
69 struct dial_request *dial_req;
72 unsigned int hfp_watch;
75 ofono_voicecall_cb_t release_queue_done_cb;
76 struct ofono_emulator *pending_em;
77 unsigned int pending_id;
81 struct ofono_call *call;
82 struct ofono_voicecall *vc;
88 gboolean dial_result_handled;
89 ofono_bool_t remote_held;
90 ofono_bool_t remote_multiparty;
94 struct ofono_voicecall *vc;
97 enum ofono_voicecall_interaction interaction;
98 ofono_voicecall_dial_cb_t cb;
100 struct voicecall *call;
101 struct ofono_phone_number ph;
104 struct tone_queue_entry {
107 ofono_voicecall_tone_cb_t cb;
109 ofono_destroy_func destroy;
113 struct emulator_status {
114 struct ofono_voicecall *vc;
118 static const char *default_en_list[] = { "911", "112", NULL };
119 static const char *default_en_list_no_sim[] = { "119", "118", "999", "110",
122 static void send_ciev_after_swap_callback(const struct ofono_error *error,
124 static void generic_callback(const struct ofono_error *error, void *data);
125 static void hangup_all_active(const struct ofono_error *error, void *data);
126 static void multirelease_callback(const struct ofono_error *err, void *data);
127 static gboolean tone_request_run(gpointer user_data);
129 static gint call_compare_by_id(gconstpointer a, gconstpointer b)
131 const struct ofono_call *call = ((struct voicecall *)a)->call;
132 unsigned int id = GPOINTER_TO_UINT(b);
143 static gint call_compare(gconstpointer a, gconstpointer b)
145 const struct voicecall *ca = a;
146 const struct voicecall *cb = b;
148 if (ca->call->id < cb->call->id)
151 if (ca->call->id > cb->call->id)
157 static void add_to_en_list(struct ofono_voicecall *vc, char **list)
162 g_hash_table_insert(vc->en_list, g_strdup(list[i++]), NULL);
165 static const char *disconnect_reason_to_string(enum ofono_disconnect_reason r)
168 case OFONO_DISCONNECT_REASON_LOCAL_HANGUP:
170 case OFONO_DISCONNECT_REASON_REMOTE_HANGUP:
177 static const char *call_status_to_string(int status)
180 case CALL_STATUS_ACTIVE:
182 case CALL_STATUS_HELD:
184 case CALL_STATUS_DIALING:
186 case CALL_STATUS_ALERTING:
188 case CALL_STATUS_INCOMING:
190 case CALL_STATUS_WAITING:
193 return "disconnected";
197 static const char *phone_and_clip_to_string(const struct ofono_phone_number *n,
200 if (clip_validity == CLIP_VALIDITY_WITHHELD && !strlen(n->number))
203 if (clip_validity == CLIP_VALIDITY_NOT_AVAILABLE)
206 return phone_number_to_string(n);
209 static const char *cnap_to_string(const char *name, int cnap_validity)
211 if (cnap_validity == CNAP_VALIDITY_WITHHELD && !strlen(name))
214 if (cnap_validity == CNAP_VALIDITY_NOT_AVAILABLE)
220 static const char *time_to_str(const time_t *t)
222 static char buf[128];
225 strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", localtime_r(t, &tm));
231 static unsigned int voicecalls_num_with_status(struct ofono_voicecall *vc,
238 for (l = vc->call_list; l; l = l->next) {
241 if (v->call->status == status)
248 static unsigned int voicecalls_num_active(struct ofono_voicecall *vc)
250 return voicecalls_num_with_status(vc, CALL_STATUS_ACTIVE);
253 static unsigned int voicecalls_num_held(struct ofono_voicecall *vc)
255 return voicecalls_num_with_status(vc, CALL_STATUS_HELD);
258 static unsigned int voicecalls_num_connecting(struct ofono_voicecall *vc)
262 r += voicecalls_num_with_status(vc, CALL_STATUS_DIALING);
263 r += voicecalls_num_with_status(vc, CALL_STATUS_ALERTING);
268 static gboolean voicecalls_have_active(struct ofono_voicecall *vc)
273 for (l = vc->call_list; l; l = l->next) {
276 if (v->call->status == CALL_STATUS_ACTIVE ||
277 v->call->status == CALL_STATUS_DIALING ||
278 v->call->status == CALL_STATUS_ALERTING)
285 static gboolean voicecalls_have_with_status(struct ofono_voicecall *vc,
291 for (l = vc->call_list; l; l = l->next) {
294 if (v->call->status == status)
301 static gboolean voicecalls_have_held(struct ofono_voicecall *vc)
303 return voicecalls_have_with_status(vc, CALL_STATUS_HELD);
306 static gboolean voicecalls_have_waiting(struct ofono_voicecall *vc)
308 return voicecalls_have_with_status(vc, CALL_STATUS_WAITING);
311 static gboolean voicecalls_have_incoming(struct ofono_voicecall *vc)
313 return voicecalls_have_with_status(vc, CALL_STATUS_INCOMING);
316 static void dial_request_finish(struct ofono_voicecall *vc)
318 struct dial_request *dial_req = vc->dial_req;
321 dial_req->cb(dial_req->call ? dial_req->call->call : NULL,
322 dial_req->user_data);
324 g_free(dial_req->message);
329 static gboolean voicecalls_can_dtmf(struct ofono_voicecall *vc)
334 for (l = vc->call_list; l; l = l->next) {
337 if (v->call->status == CALL_STATUS_ACTIVE)
340 /* Connected for 2nd stage dialing */
341 if (v->call->status == CALL_STATUS_ALERTING)
348 static int tone_queue(struct ofono_voicecall *vc, const char *tone_str,
349 ofono_voicecall_tone_cb_t cb, void *data,
350 ofono_destroy_func destroy)
352 struct tone_queue_entry *entry;
358 * Tones can be 0-9, *, #, A-D according to 27.007 C.2.11,
361 for (i = 0; tone_str[i]; i++)
362 if (!g_ascii_isdigit(tone_str[i]) && tone_str[i] != 'p' &&
363 tone_str[i] != 'P' && tone_str[i] != '*' &&
364 tone_str[i] != '#' && (tone_str[i] < 'A' ||
368 while ((entry = g_queue_peek_nth(vc->toneq, n++)) != NULL)
372 entry = g_try_new0(struct tone_queue_entry, 1);
376 entry->tone_str = g_strdup(tone_str);
377 entry->left = entry->tone_str;
379 entry->user_data = data;
380 entry->destroy = destroy;
383 g_queue_push_tail(vc->toneq, entry);
385 if (g_queue_get_length(vc->toneq) == 1)
386 g_timeout_add(0, tone_request_run, vc);
391 static void tone_request_finish(struct ofono_voicecall *vc,
392 struct tone_queue_entry *entry,
393 int error, gboolean callback)
395 g_queue_remove(vc->toneq, entry);
398 entry->cb(error, entry->user_data);
401 entry->destroy(entry->user_data);
403 g_free(entry->tone_str);
407 static gboolean is_emergency_number(struct ofono_voicecall *vc,
410 return g_hash_table_lookup_extended(vc->en_list, number, NULL, NULL);
413 static void append_voicecall_properties(struct voicecall *v,
414 DBusMessageIter *dict)
416 struct ofono_call *call = v->call;
418 const char *callerid;
422 dbus_bool_t emergency_call;
424 status = call_status_to_string(call->status);
426 ofono_dbus_dict_append(dict, "State", DBUS_TYPE_STRING, &status);
428 if (call->direction == CALL_DIRECTION_MOBILE_TERMINATED)
429 callerid = phone_and_clip_to_string(&call->phone_number,
430 call->clip_validity);
432 callerid = phone_number_to_string(&call->phone_number);
434 ofono_dbus_dict_append(dict, "LineIdentification",
435 DBUS_TYPE_STRING, &callerid);
437 if (call->called_number.number[0] != '\0') {
438 const char *calledid;
440 calledid = phone_number_to_string(&call->called_number);
442 ofono_dbus_dict_append(dict, "IncomingLine",
443 DBUS_TYPE_STRING, &calledid);
446 name = cnap_to_string(call->name, call->cnap_validity);
448 ofono_dbus_dict_append(dict, "Name", DBUS_TYPE_STRING, &name);
450 if (call->status == CALL_STATUS_ACTIVE ||
451 call->status == CALL_STATUS_HELD ||
452 (call->status == CALL_STATUS_DISCONNECTED &&
453 v->start_time != 0)) {
454 timestr = time_to_str(&v->start_time);
456 ofono_dbus_dict_append(dict, "StartTime", DBUS_TYPE_STRING,
460 if (g_slist_find_custom(v->vc->multiparty_list,
461 GINT_TO_POINTER(call->id), call_compare_by_id))
466 ofono_dbus_dict_append(dict, "Multiparty", DBUS_TYPE_BOOLEAN, &mpty);
468 ofono_dbus_dict_append(dict, "RemoteHeld", DBUS_TYPE_BOOLEAN,
471 ofono_dbus_dict_append(dict, "RemoteMultiparty", DBUS_TYPE_BOOLEAN,
472 &v->remote_multiparty);
475 ofono_dbus_dict_append(dict, "Information",
476 DBUS_TYPE_STRING, &v->message);
479 ofono_dbus_dict_append(dict, "Icon",
480 DBUS_TYPE_BYTE, &v->icon_id);
482 if (is_emergency_number(v->vc, callerid) == TRUE)
483 emergency_call = TRUE;
485 emergency_call = FALSE;
487 ofono_dbus_dict_append(dict, "Emergency",
488 DBUS_TYPE_BOOLEAN, &emergency_call);
492 static DBusMessage *voicecall_get_properties(DBusConnection *conn,
493 DBusMessage *msg, void *data)
495 struct voicecall *v = data;
497 DBusMessageIter iter;
498 DBusMessageIter dict;
500 reply = dbus_message_new_method_return(msg);
504 dbus_message_iter_init_append(reply, &iter);
506 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
507 OFONO_PROPERTIES_ARRAY_SIGNATURE,
509 append_voicecall_properties(v, &dict);
510 dbus_message_iter_close_container(&iter, &dict);
515 static DBusMessage *voicecall_deflect(DBusConnection *conn,
516 DBusMessage *msg, void *data)
518 struct voicecall *v = data;
519 struct ofono_voicecall *vc = v->vc;
520 struct ofono_call *call = v->call;
522 struct ofono_phone_number ph;
525 if (call->status != CALL_STATUS_INCOMING &&
526 call->status != CALL_STATUS_WAITING)
527 return __ofono_error_failed(msg);
529 if (vc->driver->deflect == NULL)
530 return __ofono_error_not_implemented(msg);
532 if (vc->pending || vc->dial_req || vc->pending_em)
533 return __ofono_error_busy(msg);
535 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &number,
536 DBUS_TYPE_INVALID) == FALSE)
537 return __ofono_error_invalid_args(msg);
539 if (!valid_phone_number_format(number))
540 return __ofono_error_invalid_format(msg);
542 vc->pending = dbus_message_ref(msg);
544 string_to_phone_number(number, &ph);
546 vc->driver->deflect(vc, &ph, generic_callback, vc);
551 static DBusMessage *voicecall_hangup(DBusConnection *conn,
552 DBusMessage *msg, void *data)
554 struct voicecall *v = data;
555 struct ofono_voicecall *vc = v->vc;
556 struct ofono_call *call = v->call;
557 gboolean single_call = vc->call_list->next == 0;
559 if (vc->pending || vc->pending_em)
560 return __ofono_error_busy(msg);
562 if (vc->dial_req && vc->dial_req->call != v)
563 return __ofono_error_busy(msg);
565 switch (call->status) {
566 case CALL_STATUS_DISCONNECTED:
567 return __ofono_error_failed(msg);
569 case CALL_STATUS_INCOMING:
570 if (vc->driver->hangup_all == NULL &&
571 vc->driver->hangup_active == NULL)
572 return __ofono_error_not_implemented(msg);
574 vc->pending = dbus_message_ref(msg);
576 if (vc->driver->hangup_all)
577 vc->driver->hangup_all(vc, generic_callback, vc);
579 vc->driver->hangup_active(vc, generic_callback, vc);
583 case CALL_STATUS_WAITING:
584 if (vc->driver->set_udub == NULL)
585 return __ofono_error_not_implemented(msg);
587 vc->pending = dbus_message_ref(msg);
588 vc->driver->set_udub(vc, generic_callback, vc);
592 case CALL_STATUS_HELD:
593 if (vc->driver->release_all_held &&
594 voicecalls_num_held(vc) == 1 &&
595 voicecalls_have_waiting(vc) == FALSE) {
596 vc->pending = dbus_message_ref(msg);
597 vc->driver->release_all_held(vc, generic_callback, vc);
604 case CALL_STATUS_DIALING:
605 case CALL_STATUS_ALERTING:
606 if (vc->driver->hangup_active != NULL) {
607 vc->pending = dbus_message_ref(msg);
608 vc->driver->hangup_active(vc, generic_callback, vc);
614 * Fall through, we check if we have a single alerting,
615 * dialing or active call and try to hang it up with
616 * hangup_all or hangup_active
618 case CALL_STATUS_ACTIVE:
619 if (single_call == TRUE && vc->driver->hangup_all != NULL) {
620 vc->pending = dbus_message_ref(msg);
621 vc->driver->hangup_all(vc, generic_callback, vc);
626 if (voicecalls_num_active(vc) == 1 &&
627 vc->driver->hangup_active != NULL) {
628 vc->pending = dbus_message_ref(msg);
629 vc->driver->hangup_active(vc, generic_callback, vc);
637 if (vc->driver->release_specific == NULL)
638 return __ofono_error_not_implemented(msg);
640 vc->pending = dbus_message_ref(msg);
641 vc->driver->release_specific(vc, call->id,
642 generic_callback, vc);
647 static DBusMessage *voicecall_answer(DBusConnection *conn,
648 DBusMessage *msg, void *data)
650 struct voicecall *v = data;
651 struct ofono_voicecall *vc = v->vc;
652 struct ofono_call *call = v->call;
654 if (call->status != CALL_STATUS_INCOMING)
655 return __ofono_error_failed(msg);
657 if (vc->driver->answer == NULL)
658 return __ofono_error_not_implemented(msg);
660 if (vc->pending || vc->dial_req || vc->pending_em)
661 return __ofono_error_busy(msg);
663 vc->pending = dbus_message_ref(msg);
665 vc->driver->answer(vc, generic_callback, vc);
670 static const GDBusMethodTable voicecall_methods[] = {
671 { GDBUS_METHOD("GetProperties",
672 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
673 voicecall_get_properties) },
674 { GDBUS_ASYNC_METHOD("Deflect", GDBUS_ARGS({ "number", "s" }), NULL,
675 voicecall_deflect) },
676 { GDBUS_ASYNC_METHOD("Hangup", NULL, NULL, voicecall_hangup) },
677 { GDBUS_ASYNC_METHOD("Answer", NULL, NULL, voicecall_answer) },
681 static const GDBusSignalTable voicecall_signals[] = {
682 { GDBUS_SIGNAL("PropertyChanged",
683 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
684 { GDBUS_SIGNAL("DisconnectReason",
685 GDBUS_ARGS({ "reason", "s" })) },
689 static struct voicecall *voicecall_create(struct ofono_voicecall *vc,
690 struct ofono_call *call)
694 v = g_try_new0(struct voicecall, 1);
704 static void voicecall_destroy(gpointer userdata)
706 struct voicecall *voicecall = (struct voicecall *)userdata;
708 g_free(voicecall->call);
709 g_free(voicecall->message);
714 static const char *voicecall_build_path(struct ofono_voicecall *vc,
715 const struct ofono_call *call)
717 static char path[256];
719 snprintf(path, sizeof(path), "%s/voicecall%02d",
720 __ofono_atom_get_path(vc->atom), call->id);
725 static void voicecall_emit_disconnect_reason(struct voicecall *call,
726 enum ofono_disconnect_reason reason)
728 DBusConnection *conn = ofono_dbus_get_connection();
730 const char *reason_str;
732 reason_str = disconnect_reason_to_string(reason);
733 path = voicecall_build_path(call->vc, call->call);
735 g_dbus_emit_signal(conn, path, OFONO_VOICECALL_INTERFACE,
737 DBUS_TYPE_STRING, &reason_str,
741 static void voicecall_emit_multiparty(struct voicecall *call, gboolean mpty)
743 DBusConnection *conn = ofono_dbus_get_connection();
744 const char *path = voicecall_build_path(call->vc, call->call);
745 dbus_bool_t val = mpty;
747 ofono_dbus_signal_property_changed(conn, path,
748 OFONO_VOICECALL_INTERFACE,
749 "Multiparty", DBUS_TYPE_BOOLEAN,
753 static void emulator_set_indicator_forced(struct ofono_voicecall *vc,
754 const char *name, int value)
756 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
757 struct ofono_emulator *em;
759 em = __ofono_atom_find(OFONO_ATOM_TYPE_EMULATOR_HFP, modem);
761 __ofono_emulator_set_indicator_forced(em, name, value);
764 static void emulator_call_status_cb(struct ofono_atom *atom, void *data)
766 struct ofono_emulator *em = __ofono_atom_get_data(atom);
767 struct emulator_status *s = data;
769 ofono_emulator_set_indicator(em, OFONO_EMULATOR_IND_CALL, s->status);
772 static void emulator_callsetup_status_cb(struct ofono_atom *atom, void *data)
774 struct ofono_emulator *em = __ofono_atom_get_data(atom);
775 struct emulator_status *s = data;
777 ofono_emulator_set_indicator(em, OFONO_EMULATOR_IND_CALLSETUP,
781 static void emulator_callheld_status_cb(struct ofono_atom *atom, void *data)
783 struct ofono_emulator *em = __ofono_atom_get_data(atom);
784 struct emulator_status *s = data;
786 ofono_emulator_set_indicator(em, OFONO_EMULATOR_IND_CALLHELD,
790 static void notify_emulator_call_status(struct ofono_voicecall *vc)
792 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
793 gboolean call = FALSE;
794 unsigned int non_mpty = 0;
795 gboolean multiparty = FALSE;
796 gboolean held = FALSE;
797 unsigned int non_mpty_held = 0;
798 gboolean multiparty_held = FALSE;
799 gboolean incoming = FALSE;
800 gboolean dialing = FALSE;
801 gboolean alerting = FALSE;
802 gboolean waiting = FALSE;
805 struct emulator_status data;
809 for (l = vc->call_list; l; l = l->next) {
812 switch (v->call->status) {
813 case CALL_STATUS_ACTIVE:
815 if (g_slist_find_custom(vc->multiparty_list,
816 GINT_TO_POINTER(v->call->id),
823 case CALL_STATUS_HELD:
825 if (g_slist_find_custom(vc->multiparty_list,
826 GINT_TO_POINTER(v->call->id),
828 multiparty_held = TRUE;
833 case CALL_STATUS_DIALING:
837 case CALL_STATUS_ALERTING:
841 case CALL_STATUS_INCOMING:
845 case CALL_STATUS_WAITING:
852 * Perform some basic sanity checks for transitionary states;
853 * if a transitionary state is detected, then ignore it. The call
854 * indicators will be updated properly in the follow-on calls to
855 * this function once the final state has been reached
858 if (incoming && (held || call))
861 if (waiting && (held == FALSE && call == FALSE))
864 if (non_mpty > 1 || (non_mpty && multiparty))
867 if (non_mpty_held > 1 || (non_mpty_held && multiparty_held))
870 if (multiparty && multiparty_held)
873 data.status = call || held ? OFONO_EMULATOR_CALL_ACTIVE :
874 OFONO_EMULATOR_CALL_INACTIVE;
876 __ofono_modem_foreach_registered_atom(modem,
877 OFONO_ATOM_TYPE_EMULATOR_HFP,
878 emulator_call_status_cb, &data);
881 data.status = OFONO_EMULATOR_CALLSETUP_INCOMING;
883 data.status = OFONO_EMULATOR_CALLSETUP_OUTGOING;
885 data.status = OFONO_EMULATOR_CALLSETUP_ALERTING;
887 data.status = OFONO_EMULATOR_CALLSETUP_INCOMING;
889 data.status = OFONO_EMULATOR_CALLSETUP_INACTIVE;
891 __ofono_modem_foreach_registered_atom(modem,
892 OFONO_ATOM_TYPE_EMULATOR_HFP,
893 emulator_callsetup_status_cb,
897 data.status = call ? OFONO_EMULATOR_CALLHELD_MULTIPLE :
898 OFONO_EMULATOR_CALLHELD_ON_HOLD;
900 data.status = OFONO_EMULATOR_CALLHELD_NONE;
902 __ofono_modem_foreach_registered_atom(modem,
903 OFONO_ATOM_TYPE_EMULATOR_HFP,
904 emulator_callheld_status_cb,
908 static void voicecall_set_call_status(struct voicecall *call, int status)
910 DBusConnection *conn = ofono_dbus_get_connection();
912 const char *status_str;
915 if (call->call->status == status)
918 old_status = call->call->status;
920 call->call->status = status;
922 status_str = call_status_to_string(status);
923 path = voicecall_build_path(call->vc, call->call);
925 ofono_dbus_signal_property_changed(conn, path,
926 OFONO_VOICECALL_INTERFACE,
927 "State", DBUS_TYPE_STRING,
930 notify_emulator_call_status(call->vc);
932 if (status == CALL_STATUS_ACTIVE &&
933 (old_status == CALL_STATUS_INCOMING ||
934 old_status == CALL_STATUS_DIALING ||
935 old_status == CALL_STATUS_ALERTING ||
936 old_status == CALL_STATUS_WAITING)) {
939 call->start_time = time(NULL);
940 timestr = time_to_str(&call->start_time);
942 ofono_dbus_signal_property_changed(conn, path,
943 OFONO_VOICECALL_INTERFACE,
944 "StartTime", DBUS_TYPE_STRING,
947 if (call->vc->dial_req && call == call->vc->dial_req->call)
948 dial_request_finish(call->vc);
951 if (status == CALL_STATUS_DISCONNECTED && call->vc->dial_req &&
952 call == call->vc->dial_req->call)
953 dial_request_finish(call->vc);
955 if (!voicecalls_can_dtmf(call->vc)) {
956 struct tone_queue_entry *entry;
958 while ((entry = g_queue_peek_head(call->vc->toneq)))
959 tone_request_finish(call->vc, entry, ENOENT, TRUE);
963 static void voicecall_set_call_lineid(struct voicecall *v,
964 const struct ofono_phone_number *ph,
967 struct ofono_call *call = v->call;
968 DBusConnection *conn = ofono_dbus_get_connection();
970 const char *lineid_str;
972 if (!strcmp(call->phone_number.number, ph->number) &&
973 call->phone_number.type == ph->type &&
974 call->clip_validity == clip_validity)
978 * Two cases: We get an incoming call with CLIP factored in, or
979 * CLIP comes in later as a separate event
980 * For COLP only the phone number should be checked, it can come
981 * in with the initial call event or later as a separate event
984 /* For plugins that don't keep state, ignore */
985 if (call->clip_validity == CLIP_VALIDITY_VALID &&
986 clip_validity == CLIP_VALIDITY_NOT_AVAILABLE)
989 strcpy(call->phone_number.number, ph->number);
990 call->clip_validity = clip_validity;
991 call->phone_number.type = ph->type;
993 path = voicecall_build_path(v->vc, call);
995 if (call->direction == CALL_DIRECTION_MOBILE_TERMINATED)
996 lineid_str = phone_and_clip_to_string(ph, clip_validity);
998 lineid_str = phone_number_to_string(ph);
1000 ofono_dbus_signal_property_changed(conn, path,
1001 OFONO_VOICECALL_INTERFACE,
1002 "LineIdentification",
1003 DBUS_TYPE_STRING, &lineid_str);
1005 if (is_emergency_number(v->vc, lineid_str)) {
1006 dbus_bool_t emergency_call = TRUE;
1008 ofono_dbus_signal_property_changed(conn, path,
1009 OFONO_VOICECALL_INTERFACE,
1016 static void voicecall_set_call_calledid(struct voicecall *v,
1017 const struct ofono_phone_number *ph)
1019 struct ofono_call *call = v->call;
1020 DBusConnection *conn = ofono_dbus_get_connection();
1022 const char *calledid_str;
1024 if (!strcmp(call->called_number.number, ph->number) &&
1025 call->called_number.type == ph->type)
1028 strcpy(call->called_number.number, ph->number);
1029 call->called_number.type = ph->type;
1031 path = voicecall_build_path(v->vc, call);
1032 calledid_str = phone_number_to_string(ph);
1034 ofono_dbus_signal_property_changed(conn, path,
1035 OFONO_VOICECALL_INTERFACE,
1042 static void voicecall_set_call_name(struct voicecall *v,
1046 struct ofono_call *call = v->call;
1047 DBusConnection *conn = ofono_dbus_get_connection();
1049 const char *name_str;
1051 if (!strcmp(call->name, name) && call->cnap_validity == cnap_validity)
1054 /* For plugins that don't keep state, ignore */
1055 if (call->cnap_validity == CNAP_VALIDITY_VALID &&
1056 cnap_validity == CNAP_VALIDITY_NOT_AVAILABLE)
1059 strncpy(call->name, name, OFONO_MAX_CALLER_NAME_LENGTH);
1060 call->name[OFONO_MAX_CALLER_NAME_LENGTH] = '\0';
1061 call->cnap_validity = cnap_validity;
1063 path = voicecall_build_path(v->vc, call);
1065 name_str = cnap_to_string(name, cnap_validity);
1067 ofono_dbus_signal_property_changed(conn, path,
1068 OFONO_VOICECALL_INTERFACE,
1070 DBUS_TYPE_STRING, &name_str);
1073 static gboolean voicecall_dbus_register(struct voicecall *v)
1075 DBusConnection *conn = ofono_dbus_get_connection();
1081 path = voicecall_build_path(v->vc, v->call);
1083 if (!g_dbus_register_interface(conn, path, OFONO_VOICECALL_INTERFACE,
1086 NULL, v, voicecall_destroy)) {
1087 ofono_error("Could not register VoiceCall %s", path);
1088 voicecall_destroy(v);
1096 static gboolean voicecall_dbus_unregister(struct ofono_voicecall *vc,
1097 struct voicecall *v)
1099 DBusConnection *conn = ofono_dbus_get_connection();
1100 const char *path = voicecall_build_path(vc, v->call);
1102 return g_dbus_unregister_interface(conn, path,
1103 OFONO_VOICECALL_INTERFACE);
1107 static int voicecalls_path_list(struct ofono_voicecall *vc, GSList *call_list,
1112 struct voicecall *v;
1114 *objlist = g_new0(char *, g_slist_length(call_list) + 1);
1116 if (*objlist == NULL)
1119 for (i = 0, l = call_list; l; l = l->next, i++) {
1121 (*objlist)[i] = g_strdup(voicecall_build_path(vc, v->call));
1127 static GSList *voicecalls_held_list(struct ofono_voicecall *vc)
1131 struct voicecall *v;
1133 for (l = vc->call_list; l; l = l->next) {
1136 if (v->call->status == CALL_STATUS_HELD)
1137 r = g_slist_prepend(r, v);
1141 r = g_slist_reverse(r);
1147 * Intended to be used for multiparty, which cannot be incoming,
1148 * alerting or dialing
1150 static GSList *voicecalls_active_list(struct ofono_voicecall *vc)
1154 struct voicecall *v;
1156 for (l = vc->call_list; l; l = l->next) {
1159 if (v->call->status == CALL_STATUS_ACTIVE)
1160 r = g_slist_prepend(r, v);
1164 r = g_slist_reverse(r);
1169 struct ofono_call *__ofono_voicecall_find_call_with_status(
1170 struct ofono_voicecall *vc, int status)
1173 struct voicecall *v;
1175 for (l = vc->call_list; l; l = l->next) {
1178 if (v->call->status == status)
1185 static void voicecalls_multiparty_changed(GSList *old, GSList *new)
1188 struct voicecall *nc, *oc;
1194 nc = n ? n->data : NULL;
1195 oc = o ? o->data : NULL;
1197 if (oc && (nc == NULL || (nc->call->id > oc->call->id))) {
1198 voicecall_emit_multiparty(oc, FALSE);
1200 } else if (nc && (oc == NULL || (nc->call->id < oc->call->id))) {
1201 voicecall_emit_multiparty(nc, TRUE);
1210 static void voicecalls_emit_call_removed(struct ofono_voicecall *vc,
1211 struct voicecall *v)
1213 DBusConnection *conn = ofono_dbus_get_connection();
1214 const char *atompath = __ofono_atom_get_path(vc->atom);
1215 const char *path = voicecall_build_path(vc, v->call);
1217 g_dbus_emit_signal(conn, atompath, OFONO_VOICECALL_MANAGER_INTERFACE,
1218 "CallRemoved", DBUS_TYPE_OBJECT_PATH, &path,
1222 static void voicecalls_emit_call_added(struct ofono_voicecall *vc,
1223 struct voicecall *v)
1225 DBusMessage *signal;
1226 DBusMessageIter iter;
1227 DBusMessageIter dict;
1230 notify_emulator_call_status(vc);
1232 path = __ofono_atom_get_path(vc->atom);
1234 signal = dbus_message_new_signal(path,
1235 OFONO_VOICECALL_MANAGER_INTERFACE,
1241 dbus_message_iter_init_append(signal, &iter);
1243 path = voicecall_build_path(vc, v->call);
1244 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
1246 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1247 OFONO_PROPERTIES_ARRAY_SIGNATURE,
1249 append_voicecall_properties(v, &dict);
1250 dbus_message_iter_close_container(&iter, &dict);
1252 g_dbus_send_message(ofono_dbus_get_connection(), signal);
1255 static void voicecalls_release_queue(struct ofono_voicecall *vc, GSList *calls,
1256 ofono_voicecall_cb_t cb,
1257 ofono_bool_t skip_held)
1260 struct voicecall *call;
1262 g_slist_free(vc->release_list);
1263 vc->release_list = NULL;
1265 for (l = calls; l; l = l->next) {
1268 if (call->call->status == CALL_STATUS_WAITING)
1271 if (skip_held && call->call->status == CALL_STATUS_HELD)
1274 vc->release_list = g_slist_prepend(vc->release_list, l->data);
1277 vc->release_queue_done_cb = cb;
1280 static void voicecalls_release_next(struct ofono_voicecall *vc)
1282 struct voicecall *call;
1284 if (vc->release_list == NULL)
1287 call = vc->release_list->data;
1289 vc->release_list = g_slist_remove(vc->release_list, call);
1291 if (vc->driver->hangup_active == NULL)
1294 if (call->call->status == CALL_STATUS_ACTIVE &&
1295 voicecalls_num_active(vc) == 1) {
1296 vc->driver->hangup_active(vc, multirelease_callback, vc);
1300 if (call->call->status == CALL_STATUS_ALERTING ||
1301 call->call->status == CALL_STATUS_DIALING ||
1302 call->call->status == CALL_STATUS_INCOMING) {
1303 vc->driver->hangup_active(vc, multirelease_callback, vc);
1308 vc->driver->release_specific(vc, call->call->id,
1309 multirelease_callback, vc);
1312 static void voicecalls_release_done(const struct ofono_error *error, void *data)
1314 struct ofono_voicecall *vc = data;
1317 reply = dbus_message_new_method_return(vc->pending);
1318 __ofono_dbus_pending_reply(&vc->pending, reply);
1321 static DBusMessage *manager_get_properties(DBusConnection *conn,
1322 DBusMessage *msg, void *data)
1324 struct ofono_voicecall *vc = data;
1326 DBusMessageIter iter;
1327 DBusMessageIter dict;
1330 GHashTableIter ht_iter;
1331 gpointer key, value;
1333 reply = dbus_message_new_method_return(msg);
1337 dbus_message_iter_init_append(reply, &iter);
1339 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1340 OFONO_PROPERTIES_ARRAY_SIGNATURE,
1343 /* property EmergencyNumbers */
1344 list = g_new0(char *, g_hash_table_size(vc->en_list) + 1);
1345 g_hash_table_iter_init(&ht_iter, vc->en_list);
1347 for (i = 0; g_hash_table_iter_next(&ht_iter, &key, &value); i++)
1350 ofono_dbus_dict_append_array(&dict, "EmergencyNumbers",
1351 DBUS_TYPE_STRING, &list);
1354 dbus_message_iter_close_container(&iter, &dict);
1359 static ofono_bool_t clir_string_to_clir(const char *clirstr,
1360 enum ofono_clir_option *clir)
1362 if (strlen(clirstr) == 0 || !strcmp(clirstr, "default")) {
1363 *clir = OFONO_CLIR_OPTION_DEFAULT;
1365 } else if (!strcmp(clirstr, "disabled")) {
1366 *clir = OFONO_CLIR_OPTION_SUPPRESSION;
1368 } else if (!strcmp(clirstr, "enabled")) {
1369 *clir = OFONO_CLIR_OPTION_INVOCATION;
1376 static struct ofono_call *synthesize_outgoing_call(struct ofono_voicecall *vc,
1379 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
1380 struct ofono_call *call;
1382 call = g_try_new0(struct ofono_call, 1);
1386 call->id = __ofono_modem_callid_next(modem);
1388 if (call->id == 0) {
1389 ofono_error("Failed to alloc callid, too many calls");
1394 __ofono_modem_callid_hold(modem, call->id);
1397 string_to_phone_number(number, &call->phone_number);
1399 call->direction = CALL_DIRECTION_MOBILE_ORIGINATED;
1400 call->status = CALL_STATUS_DIALING;
1401 call->clip_validity = CLIP_VALIDITY_VALID;
1406 static struct voicecall *dial_handle_result(struct ofono_voicecall *vc,
1407 const struct ofono_error *error,
1409 gboolean *need_to_emit)
1412 struct voicecall *v;
1413 struct ofono_call *call;
1415 *need_to_emit = FALSE;
1417 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1418 DBG("Dial callback returned error: %s",
1419 telephony_error_to_str(error));
1425 * Two things can happen, the call notification arrived before dial
1426 * callback or dial callback was first. Handle here
1428 for (l = vc->call_list; l; l = l->next) {
1431 if (v->call->status == CALL_STATUS_DIALING ||
1432 v->call->status == CALL_STATUS_ALERTING)
1436 * Dial request may return before existing active call
1437 * is put on hold or after dialed call has got active
1439 if (v->call->status == CALL_STATUS_ACTIVE &&
1440 v->call->direction ==
1441 CALL_DIRECTION_MOBILE_ORIGINATED &&
1442 !v->dial_result_handled)
1446 call = synthesize_outgoing_call(vc, number);
1450 v = voicecall_create(vc, call);
1454 v->detect_time = time(NULL);
1456 DBG("Registering new call: %d", call->id);
1457 voicecall_dbus_register(v);
1459 vc->call_list = g_slist_insert_sorted(vc->call_list, v,
1462 *need_to_emit = TRUE;
1465 v->dial_result_handled = TRUE;
1470 static void manager_dial_callback(const struct ofono_error *error, void *data)
1472 struct ofono_voicecall *vc = data;
1475 gboolean need_to_emit;
1476 struct voicecall *v;
1478 if (dbus_message_get_args(vc->pending, NULL, DBUS_TYPE_STRING, &number,
1479 DBUS_TYPE_INVALID) == FALSE)
1482 v = dial_handle_result(vc, error, number, &need_to_emit);
1485 const char *path = voicecall_build_path(vc, v->call);
1487 reply = dbus_message_new_method_return(vc->pending);
1489 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
1492 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
1494 if (is_emergency_number(vc, number) == TRUE)
1495 __ofono_modem_dec_emergency_mode(modem);
1497 reply = __ofono_error_failed(vc->pending);
1500 __ofono_dbus_pending_reply(&vc->pending, reply);
1503 voicecalls_emit_call_added(vc, v);
1506 static int voicecall_dial(struct ofono_voicecall *vc, const char *number,
1507 enum ofono_clir_option clir,
1508 ofono_voicecall_cb_t cb, void *data)
1510 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
1511 struct ofono_phone_number ph;
1513 if (g_slist_length(vc->call_list) >= MAX_VOICE_CALLS)
1516 if (!valid_long_phone_number_format(number))
1519 if (ofono_modem_get_online(modem) == FALSE)
1522 if (vc->driver->dial == NULL)
1525 if (voicecalls_have_incoming(vc))
1528 /* We can't have two dialing/alerting calls, reject outright */
1529 if (voicecalls_num_connecting(vc) > 0)
1532 if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1535 if (is_emergency_number(vc, number) == TRUE)
1536 __ofono_modem_inc_emergency_mode(modem);
1538 string_to_phone_number(number, &ph);
1541 g_key_file_set_string(vc->settings, SETTINGS_GROUP,
1543 storage_sync(vc->imsi, SETTINGS_STORE, vc->settings);
1546 vc->driver->dial(vc, &ph, clir, cb, vc);
1551 static DBusMessage *manager_dial(DBusConnection *conn,
1552 DBusMessage *msg, void *data)
1554 struct ofono_voicecall *vc = data;
1556 const char *clirstr;
1557 enum ofono_clir_option clir;
1560 if (vc->pending || vc->dial_req || vc->pending_em)
1561 return __ofono_error_busy(msg);
1563 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &number,
1564 DBUS_TYPE_STRING, &clirstr,
1565 DBUS_TYPE_INVALID) == FALSE)
1566 return __ofono_error_invalid_args(msg);
1568 if (clir_string_to_clir(clirstr, &clir) == FALSE)
1569 return __ofono_error_invalid_format(msg);
1571 vc->pending = dbus_message_ref(msg);
1573 err = voicecall_dial(vc, number, clir, manager_dial_callback, vc);
1579 dbus_message_unref(msg);
1583 return __ofono_error_invalid_format(msg);
1586 return __ofono_error_not_available(msg);
1589 return __ofono_error_not_implemented(msg);
1592 return __ofono_error_failed(msg);
1595 static DBusMessage *manager_transfer(DBusConnection *conn,
1596 DBusMessage *msg, void *data)
1598 struct ofono_voicecall *vc = data;
1602 if (vc->pending || vc->dial_req || vc->pending_em)
1603 return __ofono_error_busy(msg);
1605 numactive = voicecalls_num_active(vc);
1608 * According to 22.091 section 5.8, the network has the option of
1609 * implementing the call transfer operation for a call that is
1610 * still dialing/alerting.
1612 numactive += voicecalls_num_connecting(vc);
1614 numheld = voicecalls_num_held(vc);
1616 if (numactive != 1 || numheld != 1)
1617 return __ofono_error_failed(msg);
1619 if (vc->driver->transfer == NULL)
1620 return __ofono_error_not_implemented(msg);
1622 vc->pending = dbus_message_ref(msg);
1624 vc->driver->transfer(vc, generic_callback, vc);
1629 static DBusMessage *manager_swap_without_accept(DBusConnection *conn,
1630 DBusMessage *msg, void *data)
1632 struct ofono_voicecall *vc = data;
1633 ofono_voicecall_cb_t cb;
1635 if (vc->pending || vc->dial_req || vc->pending_em)
1636 return __ofono_error_busy(msg);
1638 vc->pending = dbus_message_ref(msg);
1640 if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1641 cb = send_ciev_after_swap_callback;
1643 cb = generic_callback;
1645 vc->driver->swap_without_accept(vc, cb, vc);
1651 static DBusMessage *manager_swap_calls(DBusConnection *conn,
1652 DBusMessage *msg, void *data)
1654 struct ofono_voicecall *vc = data;
1655 ofono_voicecall_cb_t cb;
1657 if (vc->driver->swap_without_accept)
1658 return manager_swap_without_accept(conn, msg, data);
1660 if (vc->pending || vc->dial_req || vc->pending_em)
1661 return __ofono_error_busy(msg);
1663 if (voicecalls_have_waiting(vc))
1664 return __ofono_error_failed(msg);
1666 if (vc->driver->hold_all_active == NULL)
1667 return __ofono_error_not_implemented(msg);
1669 vc->pending = dbus_message_ref(msg);
1671 if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1672 cb = send_ciev_after_swap_callback;
1674 cb = generic_callback;
1676 vc->driver->hold_all_active(vc, cb, vc);
1681 static DBusMessage *manager_release_and_answer(DBusConnection *conn,
1682 DBusMessage *msg, void *data)
1684 struct ofono_voicecall *vc = data;
1686 if (vc->pending || vc->dial_req || vc->pending_em)
1687 return __ofono_error_busy(msg);
1689 if (!voicecalls_have_waiting(vc))
1690 return __ofono_error_failed(msg);
1692 if (vc->driver->release_all_active == NULL)
1693 return __ofono_error_not_implemented(msg);
1695 vc->pending = dbus_message_ref(msg);
1697 vc->driver->release_all_active(vc, generic_callback, vc);
1702 static DBusMessage *manager_hold_and_answer(DBusConnection *conn,
1703 DBusMessage *msg, void *data)
1705 struct ofono_voicecall *vc = data;
1707 if (vc->pending || vc->dial_req || vc->pending_em)
1708 return __ofono_error_busy(msg);
1710 if (voicecalls_have_waiting(vc) == FALSE)
1711 return __ofono_error_failed(msg);
1714 * We have waiting call and both an active and held call. According
1715 * to 22.030 we cannot use CHLD=2 in this situation.
1717 if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1718 return __ofono_error_failed(msg);
1720 if (vc->driver->hold_all_active == NULL)
1721 return __ofono_error_not_implemented(msg);
1723 vc->pending = dbus_message_ref(msg);
1725 vc->driver->hold_all_active(vc, generic_callback, vc);
1730 static DBusMessage *manager_hangup_all(DBusConnection *conn,
1731 DBusMessage *msg, void *data)
1733 struct ofono_voicecall *vc = data;
1735 if (vc->pending || vc->pending_em)
1736 return __ofono_error_busy(msg);
1738 if (vc->dial_req && vc->dial_req->call == NULL)
1739 return __ofono_error_busy(msg);
1741 if (vc->driver->hangup_all == NULL &&
1742 (vc->driver->release_specific == NULL ||
1743 vc->driver->hangup_active == NULL))
1744 return __ofono_error_not_implemented(msg);
1746 if (vc->call_list == NULL) {
1747 DBusMessage *reply = dbus_message_new_method_return(msg);
1751 vc->pending = dbus_message_ref(msg);
1753 if (vc->driver->hangup_all) {
1754 vc->driver->hangup_all(vc, generic_callback, vc);
1758 if (voicecalls_num_held(vc) > 0)
1759 vc->driver->hangup_active(vc, hangup_all_active, vc);
1761 vc->driver->hangup_active(vc, generic_callback, vc);
1766 static void multiparty_callback_common(struct ofono_voicecall *vc,
1769 DBusMessageIter iter;
1770 DBusMessageIter array_iter;
1771 char **objpath_list;
1774 voicecalls_path_list(vc, vc->multiparty_list, &objpath_list);
1776 dbus_message_iter_init_append(reply, &iter);
1777 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1778 DBUS_TYPE_OBJECT_PATH_AS_STRING, &array_iter);
1780 for (i = 0; objpath_list[i]; i++)
1781 dbus_message_iter_append_basic(&array_iter,
1782 DBUS_TYPE_OBJECT_PATH, &objpath_list[i]);
1784 dbus_message_iter_close_container(&iter, &array_iter);
1786 g_strfreev(objpath_list);
1789 static void private_chat_callback(const struct ofono_error *error, void *data)
1791 struct ofono_voicecall *vc = data;
1793 const char *callpath;
1799 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1800 DBG("command failed with error: %s",
1801 telephony_error_to_str(error));
1802 __ofono_dbus_pending_reply(&vc->pending,
1803 __ofono_error_failed(vc->pending));
1807 dbus_message_get_args(vc->pending, NULL,
1808 DBUS_TYPE_OBJECT_PATH, &callpath,
1811 c = strrchr(callpath, '/');
1812 sscanf(c, "/voicecall%2u", &id);
1814 old = g_slist_copy(vc->multiparty_list);
1816 l = g_slist_find_custom(vc->multiparty_list, GINT_TO_POINTER(id),
1817 call_compare_by_id);
1820 vc->multiparty_list =
1821 g_slist_remove(vc->multiparty_list, l->data);
1823 if (vc->multiparty_list->next == NULL) {
1824 g_slist_free(vc->multiparty_list);
1825 vc->multiparty_list = NULL;
1829 reply = dbus_message_new_method_return(vc->pending);
1830 multiparty_callback_common(vc, reply);
1831 __ofono_dbus_pending_reply(&vc->pending, reply);
1833 voicecalls_multiparty_changed(old, vc->multiparty_list);
1837 static DBusMessage *multiparty_private_chat(DBusConnection *conn,
1838 DBusMessage *msg, void *data)
1840 struct ofono_voicecall *vc = data;
1841 const char *path = __ofono_atom_get_path(vc->atom);
1842 const char *callpath;
1847 if (vc->pending || vc->dial_req || vc->pending_em)
1848 return __ofono_error_busy(msg);
1850 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &callpath,
1851 DBUS_TYPE_INVALID) == FALSE)
1852 return __ofono_error_invalid_args(msg);
1854 if (strlen(callpath) == 0)
1855 return __ofono_error_invalid_format(msg);
1857 c = strrchr(callpath, '/');
1859 if (c == NULL || strncmp(path, callpath, c-callpath))
1860 return __ofono_error_not_found(msg);
1862 if (!sscanf(c, "/voicecall%2u", &id))
1863 return __ofono_error_not_found(msg);
1865 for (l = vc->multiparty_list; l; l = l->next) {
1866 struct voicecall *v = l->data;
1867 if (v->call->id == id)
1872 return __ofono_error_not_found(msg);
1875 * If we found id on the list of multiparty calls, then by definition
1876 * the multiparty call exists. Only thing to check is whether we have
1879 if (voicecalls_have_held(vc))
1880 return __ofono_error_failed(msg);
1882 if (vc->driver->private_chat == NULL)
1883 return __ofono_error_not_implemented(msg);
1885 vc->pending = dbus_message_ref(msg);
1887 vc->driver->private_chat(vc, id, private_chat_callback, vc);
1892 static void multiparty_create_callback(const struct ofono_error *error,
1895 struct ofono_voicecall *vc = data;
1899 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1900 DBG("command failed with error: %s",
1901 telephony_error_to_str(error));
1902 __ofono_dbus_pending_reply(&vc->pending,
1903 __ofono_error_failed(vc->pending));
1908 * We just created a multiparty call, gather all held
1909 * active calls and add them to the multiparty list
1911 old = vc->multiparty_list;
1912 vc->multiparty_list = NULL;
1914 vc->multiparty_list = g_slist_concat(vc->multiparty_list,
1915 voicecalls_held_list(vc));
1917 vc->multiparty_list = g_slist_concat(vc->multiparty_list,
1918 voicecalls_active_list(vc));
1920 vc->multiparty_list = g_slist_sort(vc->multiparty_list,
1923 if (g_slist_length(vc->multiparty_list) < 2) {
1924 ofono_error("Created multiparty call, but size is less than 2"
1927 __ofono_dbus_pending_reply(&vc->pending,
1928 __ofono_error_failed(vc->pending));
1932 reply = dbus_message_new_method_return(vc->pending);
1933 multiparty_callback_common(vc, reply);
1934 __ofono_dbus_pending_reply(&vc->pending, reply);
1936 voicecalls_multiparty_changed(old, vc->multiparty_list);
1940 static DBusMessage *multiparty_create(DBusConnection *conn,
1941 DBusMessage *msg, void *data)
1943 struct ofono_voicecall *vc = data;
1945 if (vc->pending || vc->dial_req || vc->pending_em)
1946 return __ofono_error_busy(msg);
1948 if (!voicecalls_have_held(vc) || !voicecalls_have_active(vc))
1949 return __ofono_error_failed(msg);
1951 if (vc->driver->create_multiparty == NULL)
1952 return __ofono_error_not_implemented(msg);
1954 vc->pending = dbus_message_ref(msg);
1956 vc->driver->create_multiparty(vc, multiparty_create_callback, vc);
1961 static DBusMessage *multiparty_hangup(DBusConnection *conn,
1962 DBusMessage *msg, void *data)
1964 struct ofono_voicecall *vc = data;
1966 if (vc->pending || vc->dial_req || vc->pending_em)
1967 return __ofono_error_busy(msg);
1969 if (vc->driver->release_specific == NULL)
1970 return __ofono_error_not_implemented(msg);
1972 if (vc->driver->release_all_held == NULL)
1973 return __ofono_error_not_implemented(msg);
1975 if (vc->driver->release_all_active == NULL)
1976 return __ofono_error_not_implemented(msg);
1978 if (vc->multiparty_list == NULL) {
1979 DBusMessage *reply = dbus_message_new_method_return(msg);
1983 vc->pending = dbus_message_ref(msg);
1985 /* We don't have waiting calls, as we can't use +CHLD to release */
1986 if (!voicecalls_have_waiting(vc)) {
1987 struct voicecall *v = vc->multiparty_list->data;
1989 if (v->call->status == CALL_STATUS_HELD) {
1990 vc->driver->release_all_held(vc, generic_callback,
1996 * Multiparty is currently active, if we have held calls
1997 * we shouldn't use release_all_active here since this also
1998 * has the side-effect of activating held calls
2000 if (!voicecalls_have_held(vc)) {
2001 vc->driver->release_all_active(vc, generic_callback,
2007 /* Fall back to the old-fashioned way */
2008 voicecalls_release_queue(vc, vc->multiparty_list,
2009 voicecalls_release_done, FALSE);
2010 voicecalls_release_next(vc);
2016 static void tone_callback(int error, void *data)
2018 struct ofono_voicecall *vc = data;
2022 reply = __ofono_error_failed(vc->pending);
2024 reply = dbus_message_new_method_return(vc->pending);
2026 __ofono_dbus_pending_reply(&vc->pending, reply);
2029 static DBusMessage *manager_tone(DBusConnection *conn,
2030 DBusMessage *msg, void *data)
2032 struct ofono_voicecall *vc = data;
2033 const char *in_tones;
2038 return __ofono_error_busy(msg);
2040 if (vc->driver->send_tones == NULL)
2041 return __ofono_error_not_implemented(msg);
2043 /* Send DTMFs only if we have at least one connected call */
2044 if (!voicecalls_can_dtmf(vc))
2045 return __ofono_error_failed(msg);
2047 if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &in_tones,
2048 DBUS_TYPE_INVALID) == FALSE)
2049 return __ofono_error_invalid_args(msg);
2051 len = strlen(in_tones);
2054 return __ofono_error_invalid_format(msg);
2056 tones = g_ascii_strup(in_tones, len);
2058 err = tone_queue(vc, tones, tone_callback, vc, NULL);
2063 return __ofono_error_invalid_format(msg);
2065 vc->pending = dbus_message_ref(msg);
2070 static DBusMessage *manager_get_calls(DBusConnection *conn,
2071 DBusMessage *msg, void *data)
2073 struct ofono_voicecall *vc = data;
2075 DBusMessageIter iter;
2076 DBusMessageIter array;
2077 DBusMessageIter entry, dict;
2080 struct voicecall *v;
2082 reply = dbus_message_new_method_return(msg);
2086 dbus_message_iter_init_append(reply, &iter);
2088 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
2089 DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2090 DBUS_TYPE_OBJECT_PATH_AS_STRING
2091 DBUS_TYPE_ARRAY_AS_STRING
2092 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
2093 DBUS_TYPE_STRING_AS_STRING
2094 DBUS_TYPE_VARIANT_AS_STRING
2095 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
2096 DBUS_STRUCT_END_CHAR_AS_STRING,
2099 for (l = vc->call_list; l; l = l->next) {
2102 path = voicecall_build_path(vc, v->call);
2104 dbus_message_iter_open_container(&array, DBUS_TYPE_STRUCT,
2106 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2108 dbus_message_iter_open_container(&entry, DBUS_TYPE_ARRAY,
2109 OFONO_PROPERTIES_ARRAY_SIGNATURE,
2112 append_voicecall_properties(v, &dict);
2113 dbus_message_iter_close_container(&entry, &dict);
2114 dbus_message_iter_close_container(&array, &entry);
2117 dbus_message_iter_close_container(&iter, &array);
2122 static const GDBusMethodTable manager_methods[] = {
2123 { GDBUS_METHOD("GetProperties",
2124 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
2125 manager_get_properties) },
2126 { GDBUS_ASYNC_METHOD("Dial",
2127 GDBUS_ARGS({ "number", "s" }, { "hide_callerid", "s" }),
2128 GDBUS_ARGS({ "path", "o" }),
2130 { GDBUS_ASYNC_METHOD("Transfer", NULL, NULL, manager_transfer) },
2131 { GDBUS_ASYNC_METHOD("SwapCalls", NULL, NULL, manager_swap_calls) },
2132 { GDBUS_ASYNC_METHOD("ReleaseAndAnswer", NULL, NULL,
2133 manager_release_and_answer) },
2134 { GDBUS_ASYNC_METHOD("HoldAndAnswer", NULL, NULL,
2135 manager_hold_and_answer) },
2136 { GDBUS_ASYNC_METHOD("HangupAll", NULL, NULL,
2137 manager_hangup_all) },
2138 { GDBUS_ASYNC_METHOD("PrivateChat", GDBUS_ARGS({ "call", "o" }),
2139 GDBUS_ARGS({ "calls", "ao" }),
2140 multiparty_private_chat) },
2141 { GDBUS_ASYNC_METHOD("CreateMultiparty",
2142 NULL, GDBUS_ARGS({ "calls", "o" }),
2143 multiparty_create) },
2144 { GDBUS_ASYNC_METHOD("HangupMultiparty", NULL, NULL,
2145 multiparty_hangup) },
2146 { GDBUS_ASYNC_METHOD("SendTones",
2147 GDBUS_ARGS({ "SendTones", "s" }), NULL,
2149 { GDBUS_METHOD("GetCalls",
2150 NULL, GDBUS_ARGS({ "calls_with_properties", "a(oa{sv})" }),
2151 manager_get_calls) },
2155 static const GDBusSignalTable manager_signals[] = {
2156 { GDBUS_SIGNAL("Forwarded", GDBUS_ARGS({ "type", "s" })) },
2157 { GDBUS_SIGNAL("BarringActive", GDBUS_ARGS({ "type", "s" })) },
2158 { GDBUS_SIGNAL("PropertyChanged",
2159 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
2160 { GDBUS_SIGNAL("CallAdded",
2161 GDBUS_ARGS({ "path", "o" }, { "properties", "a{sv}" })) },
2162 { GDBUS_SIGNAL("CallRemoved", GDBUS_ARGS({ "path", "o"})) },
2166 void ofono_voicecall_disconnected(struct ofono_voicecall *vc, int id,
2167 enum ofono_disconnect_reason reason,
2168 const struct ofono_error *error)
2170 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
2172 struct voicecall *call;
2174 enum call_status prev_status;
2177 DBG("Got disconnection event for id: %d, reason: %d", id, reason);
2179 __ofono_modem_callid_release(modem, id);
2181 l = g_slist_find_custom(vc->call_list, GUINT_TO_POINTER(id),
2182 call_compare_by_id);
2185 ofono_error("Plugin notified us of call disconnect for"
2193 prev_status = call->call->status;
2195 l = g_slist_find_custom(vc->multiparty_list, GUINT_TO_POINTER(id),
2196 call_compare_by_id);
2199 vc->multiparty_list =
2200 g_slist_remove(vc->multiparty_list, call);
2202 if (vc->multiparty_list->next == NULL) { /* Size == 1 */
2203 struct voicecall *v = vc->multiparty_list->data;
2205 voicecall_emit_multiparty(v, FALSE);
2206 g_slist_free(vc->multiparty_list);
2207 vc->multiparty_list = NULL;
2211 vc->release_list = g_slist_remove(vc->release_list, call);
2213 if (reason != OFONO_DISCONNECT_REASON_UNKNOWN)
2214 voicecall_emit_disconnect_reason(call, reason);
2216 number = phone_number_to_string(&call->call->phone_number);
2217 if (is_emergency_number(vc, number) == TRUE)
2218 __ofono_modem_dec_emergency_mode(modem);
2220 voicecall_set_call_status(call, CALL_STATUS_DISCONNECTED);
2222 if (!call->untracked) {
2223 if (prev_status == CALL_STATUS_INCOMING ||
2224 prev_status == CALL_STATUS_WAITING)
2225 __ofono_history_call_missed(modem, call->call, ts);
2227 __ofono_history_call_ended(modem, call->call,
2228 call->detect_time, ts);
2231 voicecalls_emit_call_removed(vc, call);
2233 voicecall_dbus_unregister(vc, call);
2235 vc->call_list = g_slist_remove(vc->call_list, call);
2238 void ofono_voicecall_notify(struct ofono_voicecall *vc,
2239 const struct ofono_call *call)
2241 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
2243 struct voicecall *v = NULL;
2244 struct ofono_call *newcall;
2246 DBG("Got a voicecall event, status: %d, id: %u, number: %s"
2247 " called_number: %s, called_name %s", call->status,
2248 call->id, call->phone_number.number,
2249 call->called_number.number, call->name);
2251 l = g_slist_find_custom(vc->call_list, GUINT_TO_POINTER(call->id),
2252 call_compare_by_id);
2255 DBG("Found call with id: %d", call->id);
2256 voicecall_set_call_status(l->data, call->status);
2257 voicecall_set_call_lineid(l->data, &call->phone_number,
2258 call->clip_validity);
2259 voicecall_set_call_calledid(l->data, &call->called_number);
2260 voicecall_set_call_name(l->data, call->name,
2261 call->cnap_validity);
2266 DBG("Did not find a call with id: %d", call->id);
2268 __ofono_modem_callid_hold(modem, call->id);
2270 newcall = g_memdup(call, sizeof(struct ofono_call));
2271 if (newcall == NULL) {
2272 ofono_error("Unable to allocate call");
2276 v = voicecall_create(vc, newcall);
2278 ofono_error("Unable to allocate voicecall_data");
2282 if (vc->flags & VOICECALL_FLAG_STK_MODEM_CALLSETUP) {
2283 struct dial_request *req = vc->dial_req;
2284 const char *number = phone_number_to_string(&req->ph);
2286 if (!strcmp(number, "112")) {
2287 struct ofono_modem *modem =
2288 __ofono_atom_get_modem(vc->atom);
2290 __ofono_modem_inc_emergency_mode(modem);
2293 if (v->call->clip_validity == CLIP_VALIDITY_NOT_AVAILABLE) {
2294 char *number = v->call->phone_number.number;
2296 v->call->phone_number.type = req->ph.type;
2297 strncpy(number, req->ph.number,
2298 OFONO_MAX_PHONE_NUMBER_LENGTH);
2299 v->call->clip_validity = CLIP_VALIDITY_VALID;
2300 number[OFONO_MAX_PHONE_NUMBER_LENGTH] = '\0';
2303 v->message = req->message;
2304 v->icon_id = req->icon_id;
2306 req->message = NULL;
2310 * TS 102 223 Section 6.4.13: The terminal shall not store
2311 * in the UICC the call set-up details (called party number
2312 * and associated parameters)
2314 v->untracked = TRUE;
2315 vc->flags &= ~VOICECALL_FLAG_STK_MODEM_CALLSETUP;
2318 v->detect_time = time(NULL);
2320 if (!voicecall_dbus_register(v)) {
2321 ofono_error("Unable to register voice call");
2325 vc->call_list = g_slist_insert_sorted(vc->call_list, v, call_compare);
2327 voicecalls_emit_call_added(vc, v);
2339 static void send_ciev_after_swap_callback(const struct ofono_error *error,
2342 struct ofono_voicecall *vc = data;
2345 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2346 DBG("command failed with error: %s",
2347 telephony_error_to_str(error));
2349 if (error->type == OFONO_ERROR_TYPE_NO_ERROR) {
2350 reply = dbus_message_new_method_return(vc->pending);
2351 emulator_set_indicator_forced(vc, OFONO_EMULATOR_IND_CALLHELD,
2352 OFONO_EMULATOR_CALLHELD_MULTIPLE);
2354 reply = __ofono_error_failed(vc->pending);
2356 __ofono_dbus_pending_reply(&vc->pending, reply);
2359 static void generic_callback(const struct ofono_error *error, void *data)
2361 struct ofono_voicecall *vc = data;
2364 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2365 DBG("command failed with error: %s",
2366 telephony_error_to_str(error));
2368 if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
2369 reply = dbus_message_new_method_return(vc->pending);
2371 reply = __ofono_error_failed(vc->pending);
2373 __ofono_dbus_pending_reply(&vc->pending, reply);
2376 static void hangup_all_active(const struct ofono_error *error, void *data)
2378 struct ofono_voicecall *vc = data;
2380 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
2381 __ofono_dbus_pending_reply(&vc->pending,
2382 __ofono_error_failed(vc->pending));
2387 * If we have waiting call, we cannot use CHLD=0 due to side effects
2388 * to that call. Instead we try to hangup all calls one by one,
2389 * which might fail if the modem / AG does not support release_specific
2390 * for held calls. In that case the waiting call and held calls will
2393 if (vc->driver->release_all_held == NULL ||
2394 voicecalls_have_waiting(vc)) {
2395 GSList *held = voicecalls_held_list(vc);
2397 voicecalls_release_queue(vc, held,
2398 voicecalls_release_done, FALSE);
2399 voicecalls_release_next(vc);
2403 vc->driver->release_all_held(vc, generic_callback, vc);
2406 static void multirelease_callback(const struct ofono_error *error, void *data)
2408 struct ofono_voicecall *vc = data;
2410 if (vc->release_list != NULL) {
2411 voicecalls_release_next(vc);
2415 vc->release_queue_done_cb(error, vc);
2418 static void emit_en_list_changed(struct ofono_voicecall *vc)
2420 DBusConnection *conn = ofono_dbus_get_connection();
2421 const char *path = __ofono_atom_get_path(vc->atom);
2424 GHashTableIter iter;
2425 gpointer key, value;
2427 list = g_new0(char *, g_hash_table_size(vc->en_list) + 1);
2428 g_hash_table_iter_init(&iter, vc->en_list);
2430 for (i = 0; g_hash_table_iter_next(&iter, &key, &value); i++)
2433 ofono_dbus_signal_array_property_changed(conn, path,
2434 OFONO_VOICECALL_MANAGER_INTERFACE,
2435 "EmergencyNumbers", DBUS_TYPE_STRING, &list);
2440 static void set_new_ecc(struct ofono_voicecall *vc)
2442 g_hash_table_destroy(vc->en_list);
2444 vc->en_list = g_hash_table_new_full(g_str_hash, g_str_equal,
2447 /* Emergency numbers from modem/network */
2449 add_to_en_list(vc, vc->nw_en_list);
2451 /* Emergency numbers read from SIM */
2452 if (vc->flags & VOICECALL_FLAG_SIM_ECC_READY) {
2455 for (l = vc->sim_en_list; l; l = l->next)
2456 g_hash_table_insert(vc->en_list, g_strdup(l->data),
2459 add_to_en_list(vc, (char **) default_en_list_no_sim);
2461 /* Default emergency numbers */
2462 add_to_en_list(vc, (char **) default_en_list);
2464 emit_en_list_changed(vc);
2467 static void free_sim_ecc_numbers(struct ofono_voicecall *vc, gboolean old_only)
2470 * Free the currently being read EN list, just in case the
2471 * we're still reading them
2473 if (old_only == FALSE) {
2474 if (vc->new_sim_en_list) {
2475 g_slist_foreach(vc->new_sim_en_list, (GFunc) g_free,
2477 g_slist_free(vc->new_sim_en_list);
2478 vc->new_sim_en_list = NULL;
2481 vc->flags &= ~VOICECALL_FLAG_SIM_ECC_READY;
2484 if (vc->sim_en_list) {
2485 g_slist_foreach(vc->sim_en_list, (GFunc) g_free, NULL);
2486 g_slist_free(vc->sim_en_list);
2487 vc->sim_en_list = NULL;
2491 static void ecc_g2_read_cb(int ok, int total_length, int record,
2492 const unsigned char *data,
2493 int record_length, void *userdata)
2495 struct ofono_voicecall *vc = userdata;
2503 if (total_length < 3) {
2504 ofono_error("Unable to read emergency numbers from SIM");
2508 free_sim_ecc_numbers(vc, TRUE);
2511 while (total_length--) {
2512 extract_bcd_number(data, 3, en);
2516 vc->sim_en_list = g_slist_prepend(vc->sim_en_list,
2520 vc->flags |= VOICECALL_FLAG_SIM_ECC_READY;
2525 static void ecc_g3_read_cb(int ok, int total_length, int record,
2526 const unsigned char *data,
2527 int record_length, void *userdata)
2529 struct ofono_voicecall *vc = userdata;
2538 if (record_length < 4 || total_length < record_length) {
2539 ofono_error("Unable to read emergency numbers from SIM");
2543 total = total_length / record_length;
2544 extract_bcd_number(data, 3, en);
2547 vc->new_sim_en_list = g_slist_prepend(vc->new_sim_en_list,
2550 if (record != total)
2554 if (!ok && vc->new_sim_en_list == NULL)
2557 free_sim_ecc_numbers(vc, TRUE);
2558 vc->sim_en_list = vc->new_sim_en_list;
2559 vc->new_sim_en_list = NULL;
2561 vc->flags |= VOICECALL_FLAG_SIM_ECC_READY;
2566 void ofono_voicecall_en_list_notify(struct ofono_voicecall *vc,
2569 g_strfreev(vc->nw_en_list);
2571 vc->nw_en_list = g_strdupv(nw_en_list);
2575 int ofono_voicecall_driver_register(const struct ofono_voicecall_driver *d)
2577 DBG("driver: %p, name: %s", d, d->name);
2579 if (d->probe == NULL)
2582 g_drivers = g_slist_prepend(g_drivers, (void *) d);
2587 void ofono_voicecall_driver_unregister(const struct ofono_voicecall_driver *d)
2589 DBG("driver: %p, name: %s", d, d->name);
2591 g_drivers = g_slist_remove(g_drivers, (void *) d);
2594 static void emulator_remove_handler(struct ofono_atom *atom, void *data)
2596 struct ofono_emulator *em = __ofono_atom_get_data(atom);
2598 ofono_emulator_remove_handler(em, data);
2601 static void emulator_hfp_unregister(struct ofono_atom *atom)
2603 struct ofono_voicecall *vc = __ofono_atom_get_data(atom);
2604 struct ofono_modem *modem = __ofono_atom_get_modem(atom);
2606 __ofono_modem_foreach_registered_atom(modem,
2607 OFONO_ATOM_TYPE_EMULATOR_HFP,
2608 emulator_call_status_cb, 0);
2609 __ofono_modem_foreach_registered_atom(modem,
2610 OFONO_ATOM_TYPE_EMULATOR_HFP,
2611 emulator_callsetup_status_cb,
2613 __ofono_modem_foreach_registered_atom(modem,
2614 OFONO_ATOM_TYPE_EMULATOR_HFP,
2615 emulator_callheld_status_cb, 0);
2617 __ofono_modem_foreach_registered_atom(modem,
2618 OFONO_ATOM_TYPE_EMULATOR_HFP,
2619 emulator_remove_handler,
2621 __ofono_modem_foreach_registered_atom(modem,
2622 OFONO_ATOM_TYPE_EMULATOR_HFP,
2623 emulator_remove_handler,
2625 __ofono_modem_foreach_registered_atom(modem,
2626 OFONO_ATOM_TYPE_EMULATOR_HFP,
2627 emulator_remove_handler,
2629 __ofono_modem_foreach_registered_atom(modem,
2630 OFONO_ATOM_TYPE_EMULATOR_HFP,
2631 emulator_remove_handler,
2633 __ofono_modem_foreach_registered_atom(modem,
2634 OFONO_ATOM_TYPE_EMULATOR_HFP,
2635 emulator_remove_handler,
2637 __ofono_modem_foreach_registered_atom(modem,
2638 OFONO_ATOM_TYPE_EMULATOR_HFP,
2639 emulator_remove_handler,
2641 __ofono_modem_foreach_registered_atom(modem,
2642 OFONO_ATOM_TYPE_EMULATOR_HFP,
2643 emulator_remove_handler,
2646 __ofono_modem_remove_atom_watch(modem, vc->hfp_watch);
2649 static void voicecall_load_settings(struct ofono_voicecall *vc)
2653 imsi = ofono_sim_get_imsi(vc->sim);
2657 vc->settings = storage_open(imsi, SETTINGS_STORE);
2659 if (vc->settings == NULL)
2662 vc->imsi = g_strdup(imsi);
2665 static void voicecall_close_settings(struct ofono_voicecall *vc)
2668 storage_close(vc->imsi, SETTINGS_STORE, vc->settings, TRUE);
2672 vc->settings = NULL;
2676 static void voicecall_unregister(struct ofono_atom *atom)
2678 DBusConnection *conn = ofono_dbus_get_connection();
2679 struct ofono_voicecall *vc = __ofono_atom_get_data(atom);
2680 struct ofono_modem *modem = __ofono_atom_get_modem(atom);
2681 const char *path = __ofono_atom_get_path(atom);
2684 emulator_hfp_unregister(atom);
2686 voicecall_close_settings(vc);
2688 if (vc->sim_state_watch) {
2689 ofono_sim_remove_state_watch(vc->sim, vc->sim_state_watch);
2690 vc->sim_state_watch = 0;
2693 if (vc->sim_watch) {
2694 __ofono_modem_remove_atom_watch(modem, vc->sim_watch);
2700 free_sim_ecc_numbers(vc, FALSE);
2702 if (vc->nw_en_list) {
2703 g_strfreev(vc->nw_en_list);
2704 vc->nw_en_list = NULL;
2707 g_hash_table_destroy(vc->en_list);
2711 dial_request_finish(vc);
2713 for (l = vc->call_list; l; l = l->next)
2714 voicecall_dbus_unregister(vc, l->data);
2716 g_slist_free(vc->call_list);
2717 vc->call_list = NULL;
2719 ofono_modem_remove_interface(modem, OFONO_VOICECALL_MANAGER_INTERFACE);
2720 g_dbus_unregister_interface(conn, path,
2721 OFONO_VOICECALL_MANAGER_INTERFACE);
2724 static void voicecall_remove(struct ofono_atom *atom)
2726 struct ofono_voicecall *vc = __ofono_atom_get_data(atom);
2728 DBG("atom: %p", atom);
2733 if (vc->driver && vc->driver->remove)
2734 vc->driver->remove(vc);
2736 if (vc->tone_source) {
2737 g_source_remove(vc->tone_source);
2738 vc->tone_source = 0;
2742 struct tone_queue_entry *entry;
2744 while ((entry = g_queue_peek_head(vc->toneq)))
2745 tone_request_finish(vc, entry, ESHUTDOWN, TRUE);
2747 g_queue_free(vc->toneq);
2753 struct ofono_voicecall *ofono_voicecall_create(struct ofono_modem *modem,
2754 unsigned int vendor,
2758 struct ofono_voicecall *vc;
2764 vc = g_try_new0(struct ofono_voicecall, 1);
2769 vc->toneq = g_queue_new();
2771 vc->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_VOICECALL,
2772 voicecall_remove, vc);
2774 for (l = g_drivers; l; l = l->next) {
2775 const struct ofono_voicecall_driver *drv = l->data;
2777 if (g_strcmp0(drv->name, driver))
2780 if (drv->probe(vc, vendor, data) < 0)
2790 static void read_sim_ecc_numbers(int id, void *userdata)
2792 struct ofono_voicecall *vc = userdata;
2794 /* Try both formats, only one or none will work */
2795 ofono_sim_read(vc->sim_context, SIM_EFECC_FILEID,
2796 OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
2797 ecc_g2_read_cb, vc);
2798 ofono_sim_read(vc->sim_context, SIM_EFECC_FILEID,
2799 OFONO_SIM_FILE_STRUCTURE_FIXED,
2800 ecc_g3_read_cb, vc);
2803 static void sim_state_watch(enum ofono_sim_state new_state, void *user)
2805 struct ofono_voicecall *vc = user;
2807 switch (new_state) {
2808 case OFONO_SIM_STATE_INSERTED:
2809 if (vc->sim_context == NULL)
2810 vc->sim_context = ofono_sim_context_create(vc->sim);
2812 read_sim_ecc_numbers(SIM_EFECC_FILEID, vc);
2814 ofono_sim_add_file_watch(vc->sim_context, SIM_EFECC_FILEID,
2815 read_sim_ecc_numbers, vc, NULL);
2817 case OFONO_SIM_STATE_NOT_PRESENT:
2818 /* TODO: Must release all non-emergency calls */
2820 if (vc->sim_context) {
2821 ofono_sim_context_free(vc->sim_context);
2822 vc->sim_context = NULL;
2825 free_sim_ecc_numbers(vc, FALSE);
2828 voicecall_close_settings(vc);
2830 case OFONO_SIM_STATE_READY:
2831 voicecall_load_settings(vc);
2833 case OFONO_SIM_STATE_LOCKED_OUT:
2834 voicecall_close_settings(vc);
2839 static void sim_watch(struct ofono_atom *atom,
2840 enum ofono_atom_watch_condition cond, void *data)
2842 struct ofono_voicecall *vc = data;
2843 struct ofono_sim *sim = __ofono_atom_get_data(atom);
2845 if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
2846 voicecall_close_settings(vc);
2847 vc->sim_state_watch = 0;
2853 vc->sim_state_watch = ofono_sim_add_state_watch(sim,
2857 sim_state_watch(ofono_sim_get_state(sim), vc);
2860 static void emulator_send_ciev_after_swap_cb(const struct ofono_error *error,
2863 struct ofono_voicecall *vc = data;
2865 if (vc->pending_em == NULL)
2868 ofono_emulator_send_final(vc->pending_em, error);
2870 if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
2871 emulator_set_indicator_forced(vc, OFONO_EMULATOR_IND_CALLHELD,
2872 OFONO_EMULATOR_CALLHELD_MULTIPLE);
2874 vc->pending_em = NULL;
2877 static void emulator_generic_cb(const struct ofono_error *error, void *data)
2879 struct ofono_voicecall *vc = data;
2881 if (vc->pending_em == NULL)
2884 ofono_emulator_send_final(vc->pending_em, error);
2885 vc->pending_em = NULL;
2888 static void emulator_mpty_join_cb(const struct ofono_error *error, void *data)
2890 struct ofono_voicecall *vc = data;
2893 if (vc->pending_em != NULL)
2894 ofono_emulator_send_final(vc->pending_em, error);
2896 vc->pending_em = NULL;
2898 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2902 * We just created a multiparty call, gather all held
2903 * active calls and add them to the multiparty list
2905 old = vc->multiparty_list;
2906 vc->multiparty_list = NULL;
2908 vc->multiparty_list = g_slist_concat(vc->multiparty_list,
2909 voicecalls_held_list(vc));
2911 vc->multiparty_list = g_slist_concat(vc->multiparty_list,
2912 voicecalls_active_list(vc));
2914 vc->multiparty_list = g_slist_sort(vc->multiparty_list,
2917 if (g_slist_length(vc->multiparty_list) < 2) {
2918 ofono_error("Created multiparty call, but size is less than 2"
2924 voicecalls_multiparty_changed(old, vc->multiparty_list);
2928 static void emulator_mpty_private_chat_cb(const struct ofono_error *error,
2931 struct ofono_voicecall *vc = data;
2935 if (vc->pending_em != NULL)
2936 ofono_emulator_send_final(vc->pending_em, error);
2938 vc->pending_em = NULL;
2940 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2943 old = g_slist_copy(vc->multiparty_list);
2945 l = g_slist_find_custom(vc->multiparty_list,
2946 GINT_TO_POINTER(vc->pending_id), call_compare_by_id);
2949 vc->multiparty_list =
2950 g_slist_remove(vc->multiparty_list, l->data);
2952 if (vc->multiparty_list->next == NULL) {
2953 g_slist_free(vc->multiparty_list);
2954 vc->multiparty_list = NULL;
2958 voicecalls_multiparty_changed(old, vc->multiparty_list);
2962 #define CHECK_BUSY(vc, em, result) \
2963 if (vc->pending || vc->dial_req || vc->pending_em) { \
2964 result.error = 126; \
2965 result.type = OFONO_ERROR_TYPE_CME; \
2966 ofono_emulator_send_final(em, &result); \
2969 static void emulator_ata_cb(struct ofono_emulator *em,
2970 struct ofono_emulator_request *req,
2973 struct ofono_voicecall *vc = userdata;
2974 struct ofono_error result;
2978 switch (ofono_emulator_request_get_type(req)) {
2979 case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
2980 CHECK_BUSY(vc, em, result)
2982 if (!voicecalls_have_incoming(vc))
2985 if (vc->driver->answer == NULL)
2988 vc->pending_em = em;
2989 vc->driver->answer(vc, emulator_generic_cb, vc);
2994 result.type = OFONO_ERROR_TYPE_FAILURE;
2995 ofono_emulator_send_final(em, &result);
2999 static void emulator_chup_cb(struct ofono_emulator *em,
3000 struct ofono_emulator_request *req, void *userdata)
3002 struct ofono_voicecall *vc = userdata;
3003 struct ofono_error result;
3007 switch (ofono_emulator_request_get_type(req)) {
3008 case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
3009 if (vc->pending || vc->pending_em)
3012 if (vc->dial_req && vc->dial_req->call == NULL)
3015 if (vc->driver->release_specific == NULL &&
3016 vc->driver->hangup_active == NULL)
3019 if (vc->driver->hangup_active) {
3020 vc->pending_em = em;
3021 vc->driver->hangup_active(vc, emulator_generic_cb, vc);
3025 if (voicecalls_have_active(vc) == FALSE &&
3026 voicecalls_have_incoming(vc) == FALSE)
3029 vc->pending_em = em;
3030 voicecalls_release_queue(vc, vc->call_list,
3031 emulator_generic_cb, TRUE);
3032 voicecalls_release_next(vc);
3039 result.type = OFONO_ERROR_TYPE_FAILURE;
3040 ofono_emulator_send_final(em, &result);
3044 static void emulator_clcc_cb(struct ofono_emulator *em,
3045 struct ofono_emulator_request *req, void *userdata)
3047 struct ofono_voicecall *vc = userdata;
3048 struct ofono_error result;
3051 * idx dir stat mode mpty
3052 * '+CLCC: <0-7>,<0-1>,<0-5>,<0-9>,<0-1>,"",' +
3053 * phone number + phone type on 3 digits + terminating null
3055 char buf[20 + OFONO_MAX_PHONE_NUMBER_LENGTH + 3 + 1];
3059 switch (ofono_emulator_request_get_type(req)) {
3060 case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
3061 for (l = vc->call_list; l; l = l->next) {
3062 struct voicecall *v = l->data;
3063 const char *number = "";
3067 if (g_slist_find_custom(vc->multiparty_list,
3068 GINT_TO_POINTER(v->call->id),
3069 call_compare_by_id))
3074 if (v->call->clip_validity == CLIP_VALIDITY_VALID) {
3075 number = v->call->phone_number.number;
3076 type = v->call->phone_number.type;
3079 sprintf(buf, "+CLCC: %d,%d,%d,0,%d,\"%s\",%d",
3080 v->call->id, v->call->direction,
3081 v->call->status, mpty, number, type);
3082 ofono_emulator_send_info(em, buf, l->next == NULL ?
3086 result.type = OFONO_ERROR_TYPE_NO_ERROR;
3090 result.type = OFONO_ERROR_TYPE_FAILURE;
3093 ofono_emulator_send_final(em, &result);
3096 #define ADD_CHLD_SUPPORT(cond, x) \
3098 if (info[-1] != '(') \
3107 static void emulator_chld_cb(struct ofono_emulator *em,
3108 struct ofono_emulator_request *req, void *userdata)
3110 struct ofono_voicecall *vc = userdata;
3111 struct ofono_error result;
3115 ofono_voicecall_cb_t cb;
3119 switch (ofono_emulator_request_get_type(req)) {
3120 case OFONO_EMULATOR_REQUEST_TYPE_SET:
3121 if (!ofono_emulator_request_next_number(req, &chld))
3124 CHECK_BUSY(vc, em, result)
3128 if (vc->driver->set_udub == NULL)
3131 if (vc->driver->release_all_held == NULL)
3134 vc->pending_em = em;
3136 if (voicecalls_have_waiting(vc)) {
3137 vc->driver->set_udub(vc,
3138 emulator_generic_cb, vc);
3142 vc->driver->release_all_held(vc,
3143 emulator_generic_cb, vc);
3146 if (vc->driver->release_all_active == NULL)
3149 vc->pending_em = em;
3150 vc->driver->release_all_active(vc,
3151 emulator_generic_cb, vc);
3154 if (vc->driver->hold_all_active == NULL)
3157 if (voicecalls_have_active(vc) &&
3158 voicecalls_have_held(vc))
3159 cb = emulator_send_ciev_after_swap_cb;
3161 cb = emulator_generic_cb;
3163 vc->pending_em = em;
3164 vc->driver->hold_all_active(vc, cb, vc);
3167 if (vc->driver->create_multiparty == NULL)
3170 if (!voicecalls_have_held(vc) ||
3171 !voicecalls_have_active(vc))
3174 vc->pending_em = em;
3175 vc->driver->create_multiparty(vc,
3176 emulator_mpty_join_cb, vc);
3179 if (vc->driver->transfer == NULL)
3182 vc->pending_em = em;
3183 vc->driver->transfer(vc,
3184 emulator_generic_cb, vc);
3190 if (chld >= 11 && chld <= 17) {
3191 if (vc->driver->release_specific == NULL)
3194 vc->pending_em = em;
3195 vc->driver->release_specific(vc, chld - 10,
3196 emulator_generic_cb, vc);
3200 if (chld >= 21 && chld <= 27) {
3202 unsigned int id = chld - 20;
3204 if (vc->driver->private_chat == NULL)
3207 for (l = vc->multiparty_list; l; l = l->next) {
3208 struct voicecall *v = l->data;
3209 if (v->call->id == id)
3216 if (voicecalls_have_held(vc))
3219 vc->pending_em = em;
3220 vc->pending_id = id;
3222 vc->driver->private_chat(vc, id,
3223 emulator_mpty_private_chat_cb, vc);
3229 case OFONO_EMULATOR_REQUEST_TYPE_SUPPORT:
3230 memcpy(buf, "+CHLD: (", 8);
3233 ADD_CHLD_SUPPORT(vc->driver->release_all_held &&
3234 vc->driver->set_udub, "0")
3235 ADD_CHLD_SUPPORT(vc->driver->release_all_active, "1")
3236 ADD_CHLD_SUPPORT(vc->driver->release_specific, "1x")
3237 ADD_CHLD_SUPPORT(vc->driver->hold_all_active, "2")
3238 ADD_CHLD_SUPPORT(vc->driver->private_chat, "2x")
3239 ADD_CHLD_SUPPORT(vc->driver->create_multiparty, "3")
3240 ADD_CHLD_SUPPORT(vc->driver->transfer, "4")
3245 ofono_emulator_send_info(em, buf, TRUE);
3246 result.type = OFONO_ERROR_TYPE_NO_ERROR;
3249 case OFONO_EMULATOR_REQUEST_TYPE_QUERY:
3250 case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
3252 result.type = OFONO_ERROR_TYPE_FAILURE;
3255 ofono_emulator_send_final(em, &result);
3258 static void vts_tone_cb(int error, void *data)
3260 struct ofono_emulator *em = data;
3261 struct ofono_error result;
3264 result.type = error ? OFONO_ERROR_TYPE_FAILURE :
3265 OFONO_ERROR_TYPE_NO_ERROR;
3267 ofono_emulator_send_final(em, &result);
3270 static void emulator_vts_cb(struct ofono_emulator *em,
3271 struct ofono_emulator_request *req, void *userdata)
3273 struct ofono_voicecall *vc = userdata;
3274 struct ofono_error result;
3277 switch (ofono_emulator_request_get_type(req)) {
3278 case OFONO_EMULATOR_REQUEST_TYPE_SET:
3279 str = ofono_emulator_request_get_raw(req);
3283 if (!g_ascii_isdigit(str[0]) && str[0] != '*' &&
3284 str[0] != '#' && (str[0] < 'A' || str[0] > 'D'))
3290 if (__ofono_voicecall_tone_send(vc, str, vts_tone_cb, em) >= 0)
3300 result.type = OFONO_ERROR_TYPE_FAILURE;
3302 ofono_emulator_send_final(em, &result);
3305 static void emulator_dial_callback(const struct ofono_error *error, void *data)
3307 struct ofono_voicecall *vc = data;
3308 gboolean need_to_emit;
3309 struct voicecall *v;
3313 number = g_key_file_get_string(vc->settings, SETTINGS_GROUP,
3316 v = dial_handle_result(vc, error, number, &need_to_emit);
3319 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
3321 if (is_emergency_number(vc, number) == TRUE)
3322 __ofono_modem_dec_emergency_mode(modem);
3326 ofono_emulator_send_final(vc->pending_em, error);
3328 vc->pending_em = NULL;
3331 voicecalls_emit_call_added(vc, v);
3334 static void emulator_dial(struct ofono_emulator *em, struct ofono_voicecall *vc,
3337 struct ofono_error result;
3342 if (vc->pending || vc->dial_req || vc->pending_em) {
3343 result.type = OFONO_ERROR_TYPE_FAILURE;
3347 vc->pending_em = em;
3349 err = voicecall_dial(vc, number, OFONO_CLIR_OPTION_DEFAULT,
3350 emulator_dial_callback, vc);
3355 vc->pending_em = NULL;
3360 result.type = OFONO_ERROR_TYPE_CME;
3364 result.type = OFONO_ERROR_TYPE_FAILURE;
3368 ofono_emulator_send_final(em, &result);
3371 static void emulator_atd_cb(struct ofono_emulator *em,
3372 struct ofono_emulator_request *req, void *userdata)
3374 struct ofono_voicecall *vc = userdata;
3375 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
3378 char number[OFONO_MAX_PHONE_NUMBER_LENGTH + 1];
3379 struct ofono_error result;
3381 switch (ofono_emulator_request_get_type(req)) {
3382 case OFONO_EMULATOR_REQUEST_TYPE_SET:
3383 str = ofono_emulator_request_get_raw(req);
3385 if (str == NULL || str[0] == '\0')
3390 if (len > OFONO_MAX_PHONE_NUMBER_LENGTH + 1 ||
3391 str[len - 1] != ';')
3394 if (len == 3 && str[0] == '>' && str[1] == '1') {
3395 struct ofono_message_waiting *mw;
3396 const struct ofono_phone_number *ph;
3399 mw = __ofono_atom_find(OFONO_ATOM_TYPE_MESSAGE_WAITING,
3404 ph = __ofono_message_waiting_get_mbdn(mw, 0);
3409 num = phone_number_to_string(ph);
3411 emulator_dial(em, vc, num);
3413 strncpy(number, str, len - 1);
3414 number[len - 1] = '\0';
3416 emulator_dial(em, vc, number);
3424 result.type = OFONO_ERROR_TYPE_FAILURE;
3425 ofono_emulator_send_final(em, &result);
3429 static void emulator_bldn_cb(struct ofono_emulator *em,
3430 struct ofono_emulator_request *req, void *userdata)
3432 struct ofono_voicecall *vc = userdata;
3434 struct ofono_error result;
3435 GError *error = NULL;
3437 switch (ofono_emulator_request_get_type(req)) {
3438 case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
3439 if (vc->settings == NULL)
3442 number = g_key_file_get_string(vc->settings, SETTINGS_GROUP,
3444 if (number == NULL || number[0] == '\0')
3447 emulator_dial(em, vc, number);
3453 result.type = OFONO_ERROR_TYPE_FAILURE;
3454 ofono_emulator_send_final(em, &result);
3458 static void emulator_hfp_watch(struct ofono_atom *atom,
3459 enum ofono_atom_watch_condition cond,
3462 struct ofono_emulator *em = __ofono_atom_get_data(atom);
3463 struct ofono_voicecall *vc = data;
3466 case OFONO_ATOM_WATCH_CONDITION_UNREGISTERED:
3467 if (vc->pending_em == em)
3468 vc->pending_em = NULL;
3471 case OFONO_ATOM_WATCH_CONDITION_REGISTERED:
3475 notify_emulator_call_status(vc);
3477 ofono_emulator_add_handler(em, "A", emulator_ata_cb, vc, NULL);
3478 ofono_emulator_add_handler(em, "+CHUP", emulator_chup_cb, vc, NULL);
3479 ofono_emulator_add_handler(em, "+CLCC", emulator_clcc_cb, vc, NULL);
3480 ofono_emulator_add_handler(em, "+CHLD", emulator_chld_cb, vc, NULL);
3481 ofono_emulator_add_handler(em, "+VTS", emulator_vts_cb, vc, NULL);
3482 ofono_emulator_add_handler(em, "D", emulator_atd_cb, vc, NULL);
3483 ofono_emulator_add_handler(em, "+BLDN", emulator_bldn_cb, vc, NULL);
3486 void ofono_voicecall_register(struct ofono_voicecall *vc)
3488 DBusConnection *conn = ofono_dbus_get_connection();
3489 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
3490 const char *path = __ofono_atom_get_path(vc->atom);
3492 if (!g_dbus_register_interface(conn, path,
3493 OFONO_VOICECALL_MANAGER_INTERFACE,
3494 manager_methods, manager_signals, NULL,
3496 ofono_error("Could not create %s interface",
3497 OFONO_VOICECALL_MANAGER_INTERFACE);
3502 ofono_modem_add_interface(modem, OFONO_VOICECALL_MANAGER_INTERFACE);
3504 vc->en_list = g_hash_table_new_full(g_str_hash, g_str_equal,
3508 * Start out with the 22.101 mandated numbers, if we have a SIM and
3509 * the SIM contains EFecc, then we update the list once we've read them
3511 add_to_en_list(vc, (char **) default_en_list_no_sim);
3512 add_to_en_list(vc, (char **) default_en_list);
3514 vc->sim_watch = __ofono_modem_add_atom_watch(modem,
3515 OFONO_ATOM_TYPE_SIM,
3516 sim_watch, vc, NULL);
3518 __ofono_atom_register(vc->atom, voicecall_unregister);
3520 vc->hfp_watch = __ofono_modem_add_atom_watch(modem,
3521 OFONO_ATOM_TYPE_EMULATOR_HFP,
3522 emulator_hfp_watch, vc, NULL);
3525 void ofono_voicecall_remove(struct ofono_voicecall *vc)
3527 __ofono_atom_free(vc->atom);
3530 void ofono_voicecall_set_data(struct ofono_voicecall *vc, void *data)
3532 vc->driver_data = data;
3535 void *ofono_voicecall_get_data(struct ofono_voicecall *vc)
3537 return vc->driver_data;
3540 int ofono_voicecall_get_next_callid(struct ofono_voicecall *vc)
3542 struct ofono_modem *modem;
3543 if (vc == NULL || vc->atom == NULL)
3546 modem = __ofono_atom_get_modem(vc->atom);
3548 return __ofono_modem_callid_next(modem);
3551 ofono_bool_t __ofono_voicecall_is_busy(struct ofono_voicecall *vc,
3552 enum ofono_voicecall_interaction type)
3554 if (vc->pending || vc->dial_req || vc->pending_em)
3558 case OFONO_VOICECALL_INTERACTION_NONE:
3559 return vc->call_list != NULL;
3560 case OFONO_VOICECALL_INTERACTION_DISCONNECT:
3561 /* Only support releasing active calls */
3562 if (voicecalls_num_active(vc) == g_slist_length(vc->call_list))
3566 case OFONO_VOICECALL_INTERACTION_PUT_ON_HOLD:
3567 if (voicecalls_num_active(vc) == g_slist_length(vc->call_list))
3570 if (voicecalls_num_held(vc) == g_slist_length(vc->call_list))
3579 static void dial_request_cb(const struct ofono_error *error, void *data)
3581 struct ofono_voicecall *vc = data;
3582 const char *number = phone_number_to_string(&vc->dial_req->ph);
3583 gboolean need_to_emit;
3584 struct voicecall *v;
3586 v = dial_handle_result(vc, error, number, &need_to_emit);
3589 if (is_emergency_number(vc, number) == TRUE) {
3590 struct ofono_modem *modem =
3591 __ofono_atom_get_modem(vc->atom);
3593 __ofono_modem_dec_emergency_mode(modem);
3596 dial_request_finish(vc);
3600 v->message = vc->dial_req->message;
3601 v->icon_id = vc->dial_req->icon_id;
3603 vc->dial_req->message = NULL;
3604 vc->dial_req->call = v;
3607 * TS 102 223 Section 6.4.13: The terminal shall not store
3608 * in the UICC the call set-up details (called party number
3609 * and associated parameters)
3611 v->untracked = TRUE;
3613 if (v->call->status == CALL_STATUS_ACTIVE)
3614 dial_request_finish(vc);
3617 voicecalls_emit_call_added(vc, v);
3620 static void dial_request(struct ofono_voicecall *vc)
3622 const char *number = phone_number_to_string(&vc->dial_req->ph);
3624 if (is_emergency_number(vc, number) == TRUE) {
3625 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
3627 __ofono_modem_inc_emergency_mode(modem);
3630 vc->driver->dial(vc, &vc->dial_req->ph, OFONO_CLIR_OPTION_DEFAULT,
3631 dial_request_cb, vc);
3634 static void dial_req_disconnect_cb(const struct ofono_error *error, void *data)
3636 struct ofono_voicecall *vc = data;
3638 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
3639 dial_request_finish(vc);
3644 * Note that the callback might come back fore we receive call
3645 * disconnection notifications. So it makes no sense to recheck
3646 * whether we can dial here. We simply dial and hope for the best.
3651 int __ofono_voicecall_dial(struct ofono_voicecall *vc,
3652 const char *addr, int addr_type,
3653 const char *message, unsigned char icon_id,
3654 enum ofono_voicecall_interaction interaction,
3655 ofono_voicecall_dial_cb_t cb, void *user_data)
3657 struct dial_request *req;
3659 if (!valid_phone_number_format(addr))
3662 if (vc->driver->dial == NULL)
3665 if (interaction == OFONO_VOICECALL_INTERACTION_DISCONNECT &&
3666 vc->driver->release_all_active == NULL)
3669 if (__ofono_voicecall_is_busy(vc, interaction) == TRUE)
3673 * TODO: if addr starts with "112", possibly translate into the
3674 * technology-specific emergency number.
3677 req = g_try_new0(struct dial_request, 1);
3681 req->message = g_strdup(message);
3682 req->icon_id = icon_id;
3683 req->interaction = interaction;
3685 req->user_data = user_data;
3687 /* TODO: parse the tones to dial after call connected */
3688 req->ph.type = addr_type;
3689 strncpy(req->ph.number, addr, OFONO_MAX_PHONE_NUMBER_LENGTH);
3693 switch (interaction) {
3694 case OFONO_VOICECALL_INTERACTION_NONE:
3698 case OFONO_VOICECALL_INTERACTION_PUT_ON_HOLD:
3699 /* Note: dialling automatically puts active calls on hold */
3703 case OFONO_VOICECALL_INTERACTION_DISCONNECT:
3704 if (voicecalls_have_active(vc))
3705 vc->driver->release_all_active(vc,
3706 dial_req_disconnect_cb, vc);
3716 void __ofono_voicecall_dial_cancel(struct ofono_voicecall *vc)
3718 if (vc->dial_req == NULL || vc->dial_req->cb == NULL)
3721 vc->dial_req->cb = NULL;
3724 static void tone_request_cb(const struct ofono_error *error, void *data)
3726 struct ofono_voicecall *vc = data;
3727 struct tone_queue_entry *entry = g_queue_peek_head(vc->toneq);
3734 * Call back with error only if the error is related to the
3735 * current entry. If the error corresponds to a cancelled
3736 * request, do nothing.
3738 if (error && error->type != OFONO_ERROR_TYPE_NO_ERROR &&
3739 entry->left > entry->tone_str) {
3740 DBG("command failed with error: %s",
3741 telephony_error_to_str(error));
3743 tone_request_finish(vc, entry, EIO, TRUE);
3748 if (*entry->left == '\0') {
3749 tone_request_finish(vc, entry, 0, TRUE);
3754 len = strspn(entry->left, "pP");
3759 * Wait 3 seconds per PAUSE, same as for DTMF separator characters
3760 * passed in a telephone number according to TS 22.101 A.21,
3761 * although 27.007 claims this delay can be set using S8 and
3762 * defaults to 2 seconds.
3764 vc->tone_source = g_timeout_add_seconds(len * 3, tone_request_run, vc);
3767 static gboolean tone_request_run(gpointer user_data)
3769 struct ofono_voicecall *vc = user_data;
3770 struct tone_queue_entry *entry = g_queue_peek_head(vc->toneq);
3774 vc->tone_source = 0;
3779 len = strcspn(entry->left, "pP");
3782 if (len > 8) /* Arbitrary length limit per request */
3785 /* Temporarily move the end of the string */
3786 final = entry->left[len];
3787 entry->left[len] = '\0';
3789 vc->driver->send_tones(vc, entry->left, tone_request_cb, vc);
3792 entry->left[0] = final;
3794 tone_request_cb(NULL, vc);
3799 int __ofono_voicecall_tone_send(struct ofono_voicecall *vc,
3800 const char *tone_str,
3801 ofono_voicecall_tone_cb_t cb, void *user_data)
3803 if (vc->driver->send_tones == NULL)
3806 /* Send DTMFs only if we have at least one connected call */
3807 if (!voicecalls_can_dtmf(vc))
3810 return tone_queue(vc, tone_str, cb, user_data, NULL);
3813 void __ofono_voicecall_tone_cancel(struct ofono_voicecall *vc, int id)
3815 struct tone_queue_entry *entry;
3818 while ((entry = g_queue_peek_nth(vc->toneq, n++)) != NULL)
3819 if (entry->id == id)
3822 tone_request_finish(vc, entry, 0, FALSE);
3825 * If we were in the middle of a PAUSE, wake queue up
3826 * now, else wake up when current tone finishes.
3828 if (n == 1 && vc->tone_source) {
3829 g_source_remove(vc->tone_source);
3830 tone_request_run(vc);
3834 void __ofono_voicecall_set_alpha_and_icon_id(struct ofono_voicecall *vc,
3835 const char *addr, int addr_type,
3836 const char *message,
3837 unsigned char icon_id)
3839 struct dial_request *req;
3841 req = g_new0(struct dial_request, 1);
3843 req->message = g_strdup(message);
3844 req->icon_id = icon_id;
3846 req->ph.type = addr_type;
3847 strncpy(req->ph.number, addr, OFONO_MAX_PHONE_NUMBER_LENGTH);
3851 vc->flags |= VOICECALL_FLAG_STK_MODEM_CALLSETUP;
3854 void __ofono_voicecall_clear_alpha_and_icon_id(struct ofono_voicecall *vc)
3856 g_free(vc->dial_req->message);
3857 vc->dial_req->message = NULL;
3859 g_free(vc->dial_req);
3860 vc->dial_req = NULL;
3862 vc->flags &= ~VOICECALL_FLAG_STK_MODEM_CALLSETUP;
3865 static void ssn_mt_forwarded_notify(struct ofono_voicecall *vc,
3866 unsigned int id, int code,
3867 const struct ofono_phone_number *ph)
3869 DBusConnection *conn = ofono_dbus_get_connection();
3870 const char *path = __ofono_atom_get_path(vc->atom);
3871 char *info = "incoming";
3873 g_dbus_emit_signal(conn, path, OFONO_VOICECALL_MANAGER_INTERFACE,
3875 DBUS_TYPE_STRING, &info,
3879 static struct voicecall *voicecall_select(struct ofono_voicecall *vc,
3883 GSList *l = g_slist_find_custom(vc->call_list,
3884 GUINT_TO_POINTER(id),
3885 call_compare_by_id);
3893 if (g_slist_length(vc->call_list) == 1)
3894 return vc->call_list->data;
3899 static void ssn_mt_remote_held_notify(struct ofono_voicecall *vc,
3900 unsigned int id, gboolean held,
3901 const struct ofono_phone_number *ph)
3903 struct voicecall *v = voicecall_select(vc, id);
3904 DBusConnection *conn = ofono_dbus_get_connection();
3910 if (v->remote_held == held)
3913 v->remote_held = held;
3914 path = voicecall_build_path(vc, v->call);
3916 ofono_dbus_signal_property_changed(conn, path,
3917 OFONO_VOICECALL_INTERFACE,
3918 "RemoteHeld", DBUS_TYPE_BOOLEAN,
3922 static void ssn_mt_remote_multiparty_notify(struct ofono_voicecall *vc,
3924 const struct ofono_phone_number *ph)
3926 struct voicecall *v = voicecall_select(vc, id);
3927 DBusConnection *conn = ofono_dbus_get_connection();
3933 if (v->remote_multiparty == TRUE)
3936 v->remote_multiparty = TRUE;
3938 path = voicecall_build_path(vc, v->call);
3940 ofono_dbus_signal_property_changed(conn, path,
3941 OFONO_VOICECALL_INTERFACE,
3942 "RemoteMultiparty", DBUS_TYPE_BOOLEAN,
3943 &v->remote_multiparty);
3946 void ofono_voicecall_ssn_mt_notify(struct ofono_voicecall *vc,
3947 unsigned int id, int code, int index,
3948 const struct ofono_phone_number *ph)
3951 case SS_MT_CALL_FORWARDED:
3952 ssn_mt_forwarded_notify(vc, id, code, ph);
3954 case SS_MT_VOICECALL_ON_HOLD:
3955 ssn_mt_remote_held_notify(vc, id, TRUE, ph);
3957 case SS_MT_VOICECALL_RETRIEVED:
3958 ssn_mt_remote_held_notify(vc, id, FALSE, ph);
3960 case SS_MT_MULTIPARTY_VOICECALL:
3961 ssn_mt_remote_multiparty_notify(vc, id, ph);
3966 static void ssn_mo_call_barred_notify(struct ofono_voicecall *vc,
3967 unsigned int id, int code)
3969 DBusConnection *conn = ofono_dbus_get_connection();
3970 const char *path = __ofono_atom_get_path(vc->atom);
3973 if (code == SS_MO_INCOMING_BARRING)
3978 g_dbus_emit_signal(conn, path, OFONO_VOICECALL_MANAGER_INTERFACE,
3980 DBUS_TYPE_STRING, &info,
3984 static void ssn_mo_forwarded_notify(struct ofono_voicecall *vc,
3985 unsigned int id, int code)
3987 DBusConnection *conn = ofono_dbus_get_connection();
3988 const char *path = __ofono_atom_get_path(vc->atom);
3989 char *info = "outgoing";
3991 g_dbus_emit_signal(conn, path, OFONO_VOICECALL_MANAGER_INTERFACE,
3993 DBUS_TYPE_STRING, &info,
3997 void ofono_voicecall_ssn_mo_notify(struct ofono_voicecall *vc,
3998 unsigned int id, int code, int index)
4001 case SS_MO_OUTGOING_BARRING:
4002 case SS_MO_INCOMING_BARRING:
4003 ssn_mo_call_barred_notify(vc, id, code);
4005 case SS_MO_CALL_FORWARDED:
4006 ssn_mo_forwarded_notify(vc, id, code);