Change ID: c784ac023826f2a1734e5d7af7ae8e6cc78acdc7
[profile/ivi/ofono.git] / src / voicecall.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <time.h>
29 #include <errno.h>
30 #include <stdint.h>
31
32 #include <glib.h>
33 #include <gdbus.h>
34
35 #include "ofono.h"
36
37 #include "common.h"
38 #include "simutil.h"
39 #include "smsutil.h"
40 #include "storage.h"
41
42 #define MAX_VOICE_CALLS 16
43
44 #define VOICECALL_FLAG_SIM_ECC_READY 0x1
45 #define VOICECALL_FLAG_STK_MODEM_CALLSETUP 0x2
46
47 #define SETTINGS_STORE "voicecall"
48 #define SETTINGS_GROUP "Settings"
49
50 GSList *g_drivers = NULL;
51
52 struct ofono_voicecall {
53         GSList *call_list;
54         GSList *release_list;
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 */
60         DBusMessage *pending;
61         uint32_t flags;
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;
67         void *driver_data;
68         struct ofono_atom *atom;
69         struct dial_request *dial_req;
70         GQueue *toneq;
71         guint tone_source;
72         unsigned int hfp_watch;
73         GKeyFile *settings;
74         char *imsi;
75         ofono_voicecall_cb_t release_queue_done_cb;
76         struct ofono_emulator *pending_em;
77         unsigned int pending_id;
78 };
79
80 struct voicecall {
81         struct ofono_call *call;
82         struct ofono_voicecall *vc;
83         time_t start_time;
84         time_t detect_time;
85         char *message;
86         uint8_t icon_id;
87         gboolean untracked;
88         gboolean dial_result_handled;
89         ofono_bool_t remote_held;
90         ofono_bool_t remote_multiparty;
91 };
92
93 struct dial_request {
94         struct ofono_voicecall *vc;
95         char *message;
96         uint8_t icon_id;
97         enum ofono_voicecall_interaction interaction;
98         ofono_voicecall_dial_cb_t cb;
99         void *user_data;
100         struct voicecall *call;
101         struct ofono_phone_number ph;
102 };
103
104 struct tone_queue_entry {
105         char *tone_str;
106         char *left;
107         ofono_voicecall_tone_cb_t cb;
108         void *user_data;
109         ofono_destroy_func destroy;
110         int id;
111 };
112
113 struct emulator_status {
114         struct ofono_voicecall *vc;
115         int status;
116 };
117
118 static const char *default_en_list[] = { "911", "112", NULL };
119 static const char *default_en_list_no_sim[] = { "119", "118", "999", "110",
120                                                 "08", "000", NULL };
121
122 static void send_ciev_after_swap_callback(const struct ofono_error *error,
123                                                                 void *data);
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);
128
129 static gint call_compare_by_id(gconstpointer a, gconstpointer b)
130 {
131         const struct ofono_call *call = ((struct voicecall *)a)->call;
132         unsigned int id = GPOINTER_TO_UINT(b);
133
134         if (id < call->id)
135                 return -1;
136
137         if (id > call->id)
138                 return 1;
139
140         return 0;
141 }
142
143 static gint call_compare(gconstpointer a, gconstpointer b)
144 {
145         const struct voicecall *ca = a;
146         const struct voicecall *cb = b;
147
148         if (ca->call->id < cb->call->id)
149                 return -1;
150
151         if (ca->call->id > cb->call->id)
152                 return 1;
153
154         return 0;
155 }
156
157 static void add_to_en_list(struct ofono_voicecall *vc, char **list)
158 {
159         int i = 0;
160
161         while (list[i])
162                 g_hash_table_insert(vc->en_list, g_strdup(list[i++]), NULL);
163 }
164
165 static const char *disconnect_reason_to_string(enum ofono_disconnect_reason r)
166 {
167         switch (r) {
168         case OFONO_DISCONNECT_REASON_LOCAL_HANGUP:
169                 return "local";
170         case OFONO_DISCONNECT_REASON_REMOTE_HANGUP:
171                 return "remote";
172         default:
173                 return "network";
174         }
175 }
176
177 static const char *call_status_to_string(int status)
178 {
179         switch (status) {
180         case CALL_STATUS_ACTIVE:
181                 return "active";
182         case CALL_STATUS_HELD:
183                 return "held";
184         case CALL_STATUS_DIALING:
185                 return "dialing";
186         case CALL_STATUS_ALERTING:
187                 return "alerting";
188         case CALL_STATUS_INCOMING:
189                 return "incoming";
190         case CALL_STATUS_WAITING:
191                 return "waiting";
192         default:
193                 return "disconnected";
194         }
195 }
196
197 static const char *phone_and_clip_to_string(const struct ofono_phone_number *n,
198                                                 int clip_validity)
199 {
200         if (clip_validity == CLIP_VALIDITY_WITHHELD && !strlen(n->number))
201                 return "withheld";
202
203         if (clip_validity == CLIP_VALIDITY_NOT_AVAILABLE)
204                 return "";
205
206         return phone_number_to_string(n);
207 }
208
209 static const char *cnap_to_string(const char *name, int cnap_validity)
210 {
211         if (cnap_validity == CNAP_VALIDITY_WITHHELD && !strlen(name))
212                 return "withheld";
213
214         if (cnap_validity == CNAP_VALIDITY_NOT_AVAILABLE)
215                 return "";
216
217         return name;
218 }
219
220 static const char *time_to_str(const time_t *t)
221 {
222         static char buf[128];
223         struct tm tm;
224
225         strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", localtime_r(t, &tm));
226         buf[127] = '\0';
227
228         return buf;
229 }
230
231 static unsigned int voicecalls_num_with_status(struct ofono_voicecall *vc,
232                                                 int status)
233 {
234         GSList *l;
235         struct voicecall *v;
236         int num = 0;
237
238         for (l = vc->call_list; l; l = l->next) {
239                 v = l->data;
240
241                 if (v->call->status == status)
242                         num += 1;
243         }
244
245         return num;
246 }
247
248 static unsigned int voicecalls_num_active(struct ofono_voicecall *vc)
249 {
250         return voicecalls_num_with_status(vc, CALL_STATUS_ACTIVE);
251 }
252
253 static unsigned int voicecalls_num_held(struct ofono_voicecall *vc)
254 {
255         return voicecalls_num_with_status(vc, CALL_STATUS_HELD);
256 }
257
258 static unsigned int voicecalls_num_connecting(struct ofono_voicecall *vc)
259 {
260         unsigned int r = 0;
261
262         r += voicecalls_num_with_status(vc, CALL_STATUS_DIALING);
263         r += voicecalls_num_with_status(vc, CALL_STATUS_ALERTING);
264
265         return r;
266 }
267
268 static gboolean voicecalls_have_active(struct ofono_voicecall *vc)
269 {
270         GSList *l;
271         struct voicecall *v;
272
273         for (l = vc->call_list; l; l = l->next) {
274                 v = l->data;
275
276                 if (v->call->status == CALL_STATUS_ACTIVE ||
277                                 v->call->status == CALL_STATUS_DIALING ||
278                                 v->call->status == CALL_STATUS_ALERTING)
279                         return TRUE;
280         }
281
282         return FALSE;
283 }
284
285 static gboolean voicecalls_have_with_status(struct ofono_voicecall *vc,
286                                                 int status)
287 {
288         GSList *l;
289         struct voicecall *v;
290
291         for (l = vc->call_list; l; l = l->next) {
292                 v = l->data;
293
294                 if (v->call->status == status)
295                         return TRUE;
296         }
297
298         return FALSE;
299 }
300
301 static gboolean voicecalls_have_held(struct ofono_voicecall *vc)
302 {
303         return voicecalls_have_with_status(vc, CALL_STATUS_HELD);
304 }
305
306 static gboolean voicecalls_have_waiting(struct ofono_voicecall *vc)
307 {
308         return voicecalls_have_with_status(vc, CALL_STATUS_WAITING);
309 }
310
311 static gboolean voicecalls_have_incoming(struct ofono_voicecall *vc)
312 {
313         return voicecalls_have_with_status(vc, CALL_STATUS_INCOMING);
314 }
315
316 static void dial_request_finish(struct ofono_voicecall *vc)
317 {
318         struct dial_request *dial_req = vc->dial_req;
319
320         if (dial_req->cb)
321                 dial_req->cb(dial_req->call ? dial_req->call->call : NULL,
322                                 dial_req->user_data);
323
324         g_free(dial_req->message);
325         g_free(dial_req);
326         vc->dial_req = NULL;
327 }
328
329 static gboolean voicecalls_can_dtmf(struct ofono_voicecall *vc)
330 {
331         GSList *l;
332         struct voicecall *v;
333
334         for (l = vc->call_list; l; l = l->next) {
335                 v = l->data;
336
337                 if (v->call->status == CALL_STATUS_ACTIVE)
338                         return TRUE;
339
340                 /* Connected for 2nd stage dialing */
341                 if (v->call->status == CALL_STATUS_ALERTING)
342                         return TRUE;
343         }
344
345         return FALSE;
346 }
347
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)
351 {
352         struct tone_queue_entry *entry;
353         int id = 1;
354         int n = 0;
355         int i;
356
357         /*
358          * Tones can be 0-9, *, #, A-D according to 27.007 C.2.11,
359          * and p for Pause.
360          */
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' ||
365                                 tone_str[i] > 'D'))
366                         return -EINVAL;
367
368         while ((entry = g_queue_peek_nth(vc->toneq, n++)) != NULL)
369                 if (entry->id >= id)
370                         id = entry->id + 1;
371
372         entry = g_try_new0(struct tone_queue_entry, 1);
373         if (entry == NULL)
374                 return -ENOMEM;
375
376         entry->tone_str = g_strdup(tone_str);
377         entry->left = entry->tone_str;
378         entry->cb = cb;
379         entry->user_data = data;
380         entry->destroy = destroy;
381         entry->id = id;
382
383         g_queue_push_tail(vc->toneq, entry);
384
385         if (g_queue_get_length(vc->toneq) == 1)
386                 g_timeout_add(0, tone_request_run, vc);
387
388         return id;
389 }
390
391 static void tone_request_finish(struct ofono_voicecall *vc,
392                                 struct tone_queue_entry *entry,
393                                 int error, gboolean callback)
394 {
395         g_queue_remove(vc->toneq, entry);
396
397         if (callback)
398                 entry->cb(error, entry->user_data);
399
400         if (entry->destroy)
401                 entry->destroy(entry->user_data);
402
403         g_free(entry->tone_str);
404         g_free(entry);
405 }
406
407 static gboolean is_emergency_number(struct ofono_voicecall *vc,
408                                         const char *number)
409 {
410         return g_hash_table_lookup_extended(vc->en_list, number, NULL, NULL);
411 }
412
413 static void append_voicecall_properties(struct voicecall *v,
414                                         DBusMessageIter *dict)
415 {
416         struct ofono_call *call = v->call;
417         const char *status;
418         const char *callerid;
419         const char *timestr;
420         const char *name;
421         ofono_bool_t mpty;
422         dbus_bool_t emergency_call;
423
424         status = call_status_to_string(call->status);
425
426         ofono_dbus_dict_append(dict, "State", DBUS_TYPE_STRING, &status);
427
428         if (call->direction == CALL_DIRECTION_MOBILE_TERMINATED)
429                 callerid = phone_and_clip_to_string(&call->phone_number,
430                                                         call->clip_validity);
431         else
432                 callerid = phone_number_to_string(&call->phone_number);
433
434         ofono_dbus_dict_append(dict, "LineIdentification",
435                                         DBUS_TYPE_STRING, &callerid);
436
437         if (call->called_number.number[0] != '\0') {
438                 const char *calledid;
439
440                 calledid = phone_number_to_string(&call->called_number);
441
442                 ofono_dbus_dict_append(dict, "IncomingLine",
443                                                 DBUS_TYPE_STRING, &calledid);
444         }
445
446         name = cnap_to_string(call->name, call->cnap_validity);
447
448         ofono_dbus_dict_append(dict, "Name", DBUS_TYPE_STRING, &name);
449
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);
455
456                 ofono_dbus_dict_append(dict, "StartTime", DBUS_TYPE_STRING,
457                                         &timestr);
458         }
459
460         if (g_slist_find_custom(v->vc->multiparty_list,
461                                 GINT_TO_POINTER(call->id), call_compare_by_id))
462                 mpty = TRUE;
463         else
464                 mpty = FALSE;
465
466         ofono_dbus_dict_append(dict, "Multiparty", DBUS_TYPE_BOOLEAN, &mpty);
467
468         ofono_dbus_dict_append(dict, "RemoteHeld", DBUS_TYPE_BOOLEAN,
469                                 &v->remote_held);
470
471         ofono_dbus_dict_append(dict, "RemoteMultiparty", DBUS_TYPE_BOOLEAN,
472                                 &v->remote_multiparty);
473
474         if (v->message)
475                 ofono_dbus_dict_append(dict, "Information",
476                                                 DBUS_TYPE_STRING, &v->message);
477
478         if (v->icon_id)
479                 ofono_dbus_dict_append(dict, "Icon",
480                                                 DBUS_TYPE_BYTE, &v->icon_id);
481
482         if (is_emergency_number(v->vc, callerid) == TRUE)
483                 emergency_call = TRUE;
484         else
485                 emergency_call = FALSE;
486
487         ofono_dbus_dict_append(dict, "Emergency",
488                                         DBUS_TYPE_BOOLEAN, &emergency_call);
489
490 }
491
492 static DBusMessage *voicecall_get_properties(DBusConnection *conn,
493                                                 DBusMessage *msg, void *data)
494 {
495         struct voicecall *v = data;
496         DBusMessage *reply;
497         DBusMessageIter iter;
498         DBusMessageIter dict;
499
500         reply = dbus_message_new_method_return(msg);
501         if (reply == NULL)
502                 return NULL;
503
504         dbus_message_iter_init_append(reply, &iter);
505
506         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
507                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
508                                         &dict);
509         append_voicecall_properties(v, &dict);
510         dbus_message_iter_close_container(&iter, &dict);
511
512         return reply;
513 }
514
515 static DBusMessage *voicecall_deflect(DBusConnection *conn,
516                                         DBusMessage *msg, void *data)
517 {
518         struct voicecall *v = data;
519         struct ofono_voicecall *vc = v->vc;
520         struct ofono_call *call = v->call;
521
522         struct ofono_phone_number ph;
523         const char *number;
524
525         if (call->status != CALL_STATUS_INCOMING &&
526                         call->status != CALL_STATUS_WAITING)
527                 return __ofono_error_failed(msg);
528
529         if (vc->driver->deflect == NULL)
530                 return __ofono_error_not_implemented(msg);
531
532         if (vc->pending || vc->dial_req || vc->pending_em)
533                 return __ofono_error_busy(msg);
534
535         if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &number,
536                                         DBUS_TYPE_INVALID) == FALSE)
537                 return __ofono_error_invalid_args(msg);
538
539         if (!valid_phone_number_format(number))
540                 return __ofono_error_invalid_format(msg);
541
542         vc->pending = dbus_message_ref(msg);
543
544         string_to_phone_number(number, &ph);
545
546         vc->driver->deflect(vc, &ph, generic_callback, vc);
547
548         return NULL;
549 }
550
551 static DBusMessage *voicecall_hangup(DBusConnection *conn,
552                                         DBusMessage *msg, void *data)
553 {
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;
558
559         if (vc->pending || vc->pending_em)
560                 return __ofono_error_busy(msg);
561
562         if (vc->dial_req && vc->dial_req->call != v)
563                 return __ofono_error_busy(msg);
564
565         switch (call->status) {
566         case CALL_STATUS_DISCONNECTED:
567                 return __ofono_error_failed(msg);
568
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);
573
574                 vc->pending = dbus_message_ref(msg);
575
576                 if (vc->driver->hangup_all)
577                         vc->driver->hangup_all(vc, generic_callback, vc);
578                 else
579                         vc->driver->hangup_active(vc, generic_callback, vc);
580
581                 return NULL;
582
583         case CALL_STATUS_WAITING:
584                 if (vc->driver->set_udub == NULL)
585                         return __ofono_error_not_implemented(msg);
586
587                 vc->pending = dbus_message_ref(msg);
588                 vc->driver->set_udub(vc, generic_callback, vc);
589
590                 return NULL;
591
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);
598
599                         return NULL;
600                 }
601
602                 break;
603
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);
609
610                         return NULL;
611                 }
612
613                 /*
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
617                  */
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);
622
623                         return NULL;
624                 }
625
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);
630
631                         return NULL;
632                 }
633
634                 break;
635         }
636
637         if (vc->driver->release_specific == NULL)
638                 return __ofono_error_not_implemented(msg);
639
640         vc->pending = dbus_message_ref(msg);
641         vc->driver->release_specific(vc, call->id,
642                                         generic_callback, vc);
643
644         return NULL;
645 }
646
647 static DBusMessage *voicecall_answer(DBusConnection *conn,
648                                         DBusMessage *msg, void *data)
649 {
650         struct voicecall *v = data;
651         struct ofono_voicecall *vc = v->vc;
652         struct ofono_call *call = v->call;
653
654         if (call->status != CALL_STATUS_INCOMING)
655                 return __ofono_error_failed(msg);
656
657         if (vc->driver->answer == NULL)
658                 return __ofono_error_not_implemented(msg);
659
660         if (vc->pending || vc->dial_req || vc->pending_em)
661                 return __ofono_error_busy(msg);
662
663         vc->pending = dbus_message_ref(msg);
664
665         vc->driver->answer(vc, generic_callback, vc);
666
667         return NULL;
668 }
669
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) },
678         { }
679 };
680
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" })) },
686         { }
687 };
688
689 static struct voicecall *voicecall_create(struct ofono_voicecall *vc,
690                                                 struct ofono_call *call)
691 {
692         struct voicecall *v;
693
694         v = g_try_new0(struct voicecall, 1);
695         if (v == NULL)
696                 return NULL;
697
698         v->call = call;
699         v->vc = vc;
700
701         return v;
702 }
703
704 static void voicecall_destroy(gpointer userdata)
705 {
706         struct voicecall *voicecall = (struct voicecall *)userdata;
707
708         g_free(voicecall->call);
709         g_free(voicecall->message);
710
711         g_free(voicecall);
712 }
713
714 static const char *voicecall_build_path(struct ofono_voicecall *vc,
715                                         const struct ofono_call *call)
716 {
717         static char path[256];
718
719         snprintf(path, sizeof(path), "%s/voicecall%02d",
720                         __ofono_atom_get_path(vc->atom), call->id);
721
722         return path;
723 }
724
725 static void voicecall_emit_disconnect_reason(struct voicecall *call,
726                                         enum ofono_disconnect_reason reason)
727 {
728         DBusConnection *conn = ofono_dbus_get_connection();
729         const char *path;
730         const char *reason_str;
731
732         reason_str = disconnect_reason_to_string(reason);
733         path = voicecall_build_path(call->vc, call->call);
734
735         g_dbus_emit_signal(conn, path, OFONO_VOICECALL_INTERFACE,
736                                 "DisconnectReason",
737                                 DBUS_TYPE_STRING, &reason_str,
738                                 DBUS_TYPE_INVALID);
739 }
740
741 static void voicecall_emit_multiparty(struct voicecall *call, gboolean mpty)
742 {
743         DBusConnection *conn = ofono_dbus_get_connection();
744         const char *path = voicecall_build_path(call->vc, call->call);
745         dbus_bool_t val = mpty;
746
747         ofono_dbus_signal_property_changed(conn, path,
748                                                 OFONO_VOICECALL_INTERFACE,
749                                                 "Multiparty", DBUS_TYPE_BOOLEAN,
750                                                 &val);
751 }
752
753 static void emulator_set_indicator_forced(struct ofono_voicecall *vc,
754                                                 const char *name, int value)
755 {
756         struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
757         struct ofono_emulator *em;
758
759         em = __ofono_atom_find(OFONO_ATOM_TYPE_EMULATOR_HFP, modem);
760         if (em)
761                 __ofono_emulator_set_indicator_forced(em, name, value);
762 }
763
764 static void emulator_call_status_cb(struct ofono_atom *atom, void *data)
765 {
766         struct ofono_emulator *em = __ofono_atom_get_data(atom);
767         struct emulator_status *s = data;
768
769         ofono_emulator_set_indicator(em, OFONO_EMULATOR_IND_CALL, s->status);
770 }
771
772 static void emulator_callsetup_status_cb(struct ofono_atom *atom, void *data)
773 {
774         struct ofono_emulator *em = __ofono_atom_get_data(atom);
775         struct emulator_status *s = data;
776
777         ofono_emulator_set_indicator(em, OFONO_EMULATOR_IND_CALLSETUP,
778                                         s->status);
779 }
780
781 static void emulator_callheld_status_cb(struct ofono_atom *atom, void *data)
782 {
783         struct ofono_emulator *em = __ofono_atom_get_data(atom);
784         struct emulator_status *s = data;
785
786         ofono_emulator_set_indicator(em, OFONO_EMULATOR_IND_CALLHELD,
787                                         s->status);
788 }
789
790 static void notify_emulator_call_status(struct ofono_voicecall *vc)
791 {
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;
803         GSList *l;
804         struct voicecall *v;
805         struct emulator_status data;
806
807         data.vc = vc;
808
809         for (l = vc->call_list; l; l = l->next) {
810                 v = l->data;
811
812                 switch (v->call->status) {
813                 case CALL_STATUS_ACTIVE:
814                         call = TRUE;
815                         if (g_slist_find_custom(vc->multiparty_list,
816                                                 GINT_TO_POINTER(v->call->id),
817                                                 call_compare_by_id))
818                                 multiparty = TRUE;
819                         else
820                                 non_mpty++;
821                         break;
822
823                 case CALL_STATUS_HELD:
824                         held = TRUE;
825                         if (g_slist_find_custom(vc->multiparty_list,
826                                                 GINT_TO_POINTER(v->call->id),
827                                                 call_compare_by_id))
828                                 multiparty_held = TRUE;
829                         else
830                                 non_mpty_held++;
831                         break;
832
833                 case CALL_STATUS_DIALING:
834                         dialing = TRUE;
835                         break;
836
837                 case CALL_STATUS_ALERTING:
838                         alerting = TRUE;
839                         break;
840
841                 case CALL_STATUS_INCOMING:
842                         incoming = TRUE;
843                         break;
844
845                 case CALL_STATUS_WAITING:
846                         waiting = TRUE;
847                         break;
848                 }
849         }
850
851         /*
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
856          */
857
858         if (incoming && (held || call))
859                 return;
860
861         if (waiting && (held == FALSE && call == FALSE))
862                 return;
863
864         if (non_mpty > 1 || (non_mpty && multiparty))
865                 return;
866
867         if (non_mpty_held > 1 || (non_mpty_held && multiparty_held))
868                 return;
869
870         if (multiparty && multiparty_held)
871                 return;
872
873         data.status = call || held ? OFONO_EMULATOR_CALL_ACTIVE :
874                                         OFONO_EMULATOR_CALL_INACTIVE;
875
876         __ofono_modem_foreach_registered_atom(modem,
877                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
878                                                 emulator_call_status_cb, &data);
879
880         if (incoming)
881                 data.status = OFONO_EMULATOR_CALLSETUP_INCOMING;
882         else if (dialing)
883                 data.status = OFONO_EMULATOR_CALLSETUP_OUTGOING;
884         else if (alerting)
885                 data.status = OFONO_EMULATOR_CALLSETUP_ALERTING;
886         else if (waiting)
887                 data.status = OFONO_EMULATOR_CALLSETUP_INCOMING;
888         else
889                 data.status = OFONO_EMULATOR_CALLSETUP_INACTIVE;
890
891         __ofono_modem_foreach_registered_atom(modem,
892                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
893                                                 emulator_callsetup_status_cb,
894                                                 &data);
895
896         if (held)
897                 data.status = call ? OFONO_EMULATOR_CALLHELD_MULTIPLE :
898                                         OFONO_EMULATOR_CALLHELD_ON_HOLD;
899         else
900                 data.status = OFONO_EMULATOR_CALLHELD_NONE;
901
902         __ofono_modem_foreach_registered_atom(modem,
903                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
904                                                 emulator_callheld_status_cb,
905                                                 &data);
906 }
907
908 static void voicecall_set_call_status(struct voicecall *call, int status)
909 {
910         DBusConnection *conn = ofono_dbus_get_connection();
911         const char *path;
912         const char *status_str;
913         int old_status;
914
915         if (call->call->status == status)
916                 return;
917
918         old_status = call->call->status;
919
920         call->call->status = status;
921
922         status_str = call_status_to_string(status);
923         path = voicecall_build_path(call->vc, call->call);
924
925         ofono_dbus_signal_property_changed(conn, path,
926                                                 OFONO_VOICECALL_INTERFACE,
927                                                 "State", DBUS_TYPE_STRING,
928                                                 &status_str);
929
930         notify_emulator_call_status(call->vc);
931
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)) {
937                 const char *timestr;
938
939                 call->start_time = time(NULL);
940                 timestr = time_to_str(&call->start_time);
941
942                 ofono_dbus_signal_property_changed(conn, path,
943                                                 OFONO_VOICECALL_INTERFACE,
944                                                 "StartTime", DBUS_TYPE_STRING,
945                                                 &timestr);
946
947                 if (call->vc->dial_req && call == call->vc->dial_req->call)
948                         dial_request_finish(call->vc);
949         }
950
951         if (status == CALL_STATUS_DISCONNECTED && call->vc->dial_req &&
952                         call == call->vc->dial_req->call)
953                 dial_request_finish(call->vc);
954
955         if (!voicecalls_can_dtmf(call->vc)) {
956                 struct tone_queue_entry *entry;
957
958                 while ((entry = g_queue_peek_head(call->vc->toneq)))
959                         tone_request_finish(call->vc, entry, ENOENT, TRUE);
960         }
961 }
962
963 static void voicecall_set_call_lineid(struct voicecall *v,
964                                         const struct ofono_phone_number *ph,
965                                         int clip_validity)
966 {
967         struct ofono_call *call = v->call;
968         DBusConnection *conn = ofono_dbus_get_connection();
969         const char *path;
970         const char *lineid_str;
971
972         if (!strcmp(call->phone_number.number, ph->number) &&
973                         call->phone_number.type == ph->type &&
974                         call->clip_validity == clip_validity)
975                 return;
976
977         /*
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
982          */
983
984         /* For plugins that don't keep state, ignore */
985         if (call->clip_validity == CLIP_VALIDITY_VALID &&
986                         clip_validity == CLIP_VALIDITY_NOT_AVAILABLE)
987                 return;
988
989         strcpy(call->phone_number.number, ph->number);
990         call->clip_validity = clip_validity;
991         call->phone_number.type = ph->type;
992
993         path = voicecall_build_path(v->vc, call);
994
995         if (call->direction == CALL_DIRECTION_MOBILE_TERMINATED)
996                 lineid_str = phone_and_clip_to_string(ph, clip_validity);
997         else
998                 lineid_str = phone_number_to_string(ph);
999
1000         ofono_dbus_signal_property_changed(conn, path,
1001                                                 OFONO_VOICECALL_INTERFACE,
1002                                                 "LineIdentification",
1003                                                 DBUS_TYPE_STRING, &lineid_str);
1004
1005         if (is_emergency_number(v->vc, lineid_str)) {
1006                 dbus_bool_t emergency_call = TRUE;
1007
1008                 ofono_dbus_signal_property_changed(conn, path,
1009                                                 OFONO_VOICECALL_INTERFACE,
1010                                                 "Emergency",
1011                                                 DBUS_TYPE_BOOLEAN,
1012                                                 &emergency_call);
1013         }
1014 }
1015
1016 static void voicecall_set_call_calledid(struct voicecall *v,
1017                                         const struct ofono_phone_number *ph)
1018 {
1019         struct ofono_call *call = v->call;
1020         DBusConnection *conn = ofono_dbus_get_connection();
1021         const char *path;
1022         const char *calledid_str;
1023
1024         if (!strcmp(call->called_number.number, ph->number) &&
1025                                         call->called_number.type == ph->type)
1026                 return;
1027
1028         strcpy(call->called_number.number, ph->number);
1029         call->called_number.type = ph->type;
1030
1031         path = voicecall_build_path(v->vc, call);
1032         calledid_str = phone_number_to_string(ph);
1033
1034         ofono_dbus_signal_property_changed(conn, path,
1035                                                 OFONO_VOICECALL_INTERFACE,
1036                                                 "IncomingLine",
1037                                                 DBUS_TYPE_STRING,
1038                                                 &calledid_str);
1039 }
1040
1041
1042 static void voicecall_set_call_name(struct voicecall *v,
1043                                         const char *name,
1044                                         int cnap_validity)
1045 {
1046         struct ofono_call *call = v->call;
1047         DBusConnection *conn = ofono_dbus_get_connection();
1048         const char *path;
1049         const char *name_str;
1050
1051         if (!strcmp(call->name, name) && call->cnap_validity == cnap_validity)
1052                 return;
1053
1054         /* For plugins that don't keep state, ignore */
1055         if (call->cnap_validity == CNAP_VALIDITY_VALID &&
1056                         cnap_validity == CNAP_VALIDITY_NOT_AVAILABLE)
1057                 return;
1058
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;
1062
1063         path = voicecall_build_path(v->vc, call);
1064
1065         name_str = cnap_to_string(name, cnap_validity);
1066
1067         ofono_dbus_signal_property_changed(conn, path,
1068                                                 OFONO_VOICECALL_INTERFACE,
1069                                                 "Name",
1070                                                 DBUS_TYPE_STRING, &name_str);
1071 }
1072
1073 static gboolean voicecall_dbus_register(struct voicecall *v)
1074 {
1075         DBusConnection *conn = ofono_dbus_get_connection();
1076         const char *path;
1077
1078         if (v == NULL)
1079                 return FALSE;
1080
1081         path = voicecall_build_path(v->vc, v->call);
1082
1083         if (!g_dbus_register_interface(conn, path, OFONO_VOICECALL_INTERFACE,
1084                                         voicecall_methods,
1085                                         voicecall_signals,
1086                                         NULL, v, voicecall_destroy)) {
1087                 ofono_error("Could not register VoiceCall %s", path);
1088                 voicecall_destroy(v);
1089
1090                 return FALSE;
1091         }
1092
1093         return TRUE;
1094 }
1095
1096 static gboolean voicecall_dbus_unregister(struct ofono_voicecall *vc,
1097                                                 struct voicecall *v)
1098 {
1099         DBusConnection *conn = ofono_dbus_get_connection();
1100         const char *path = voicecall_build_path(vc, v->call);
1101
1102         return g_dbus_unregister_interface(conn, path,
1103                                                 OFONO_VOICECALL_INTERFACE);
1104 }
1105
1106
1107 static int voicecalls_path_list(struct ofono_voicecall *vc, GSList *call_list,
1108                                 char ***objlist)
1109 {
1110         GSList *l;
1111         int i;
1112         struct voicecall *v;
1113
1114         *objlist = g_new0(char *, g_slist_length(call_list) + 1);
1115
1116         if (*objlist == NULL)
1117                 return -1;
1118
1119         for (i = 0, l = call_list; l; l = l->next, i++) {
1120                 v = l->data;
1121                 (*objlist)[i] = g_strdup(voicecall_build_path(vc, v->call));
1122         }
1123
1124         return 0;
1125 }
1126
1127 static GSList *voicecalls_held_list(struct ofono_voicecall *vc)
1128 {
1129         GSList *l;
1130         GSList *r = NULL;
1131         struct voicecall *v;
1132
1133         for (l = vc->call_list; l; l = l->next) {
1134                 v = l->data;
1135
1136                 if (v->call->status == CALL_STATUS_HELD)
1137                         r = g_slist_prepend(r, v);
1138         }
1139
1140         if (r)
1141                 r = g_slist_reverse(r);
1142
1143         return r;
1144 }
1145
1146 /*
1147  * Intended to be used for multiparty, which cannot be incoming,
1148  * alerting or dialing
1149  */
1150 static GSList *voicecalls_active_list(struct ofono_voicecall *vc)
1151 {
1152         GSList *l;
1153         GSList *r = NULL;
1154         struct voicecall *v;
1155
1156         for (l = vc->call_list; l; l = l->next) {
1157                 v = l->data;
1158
1159                 if (v->call->status == CALL_STATUS_ACTIVE)
1160                         r = g_slist_prepend(r, v);
1161         }
1162
1163         if (r)
1164                 r = g_slist_reverse(r);
1165
1166         return r;
1167 }
1168
1169 struct ofono_call *__ofono_voicecall_find_call_with_status(
1170                                 struct ofono_voicecall *vc, int status)
1171 {
1172         GSList *l;
1173         struct voicecall *v;
1174
1175         for (l = vc->call_list; l; l = l->next) {
1176                 v = l->data;
1177
1178                 if (v->call->status == status)
1179                         return v->call;
1180         }
1181
1182         return NULL;
1183 }
1184
1185 static void voicecalls_multiparty_changed(GSList *old, GSList *new)
1186 {
1187         GSList *o, *n;
1188         struct voicecall *nc, *oc;
1189
1190         n = new;
1191         o = old;
1192
1193         while (n || o) {
1194                 nc = n ? n->data : NULL;
1195                 oc = o ? o->data : NULL;
1196
1197                 if (oc && (nc == NULL || (nc->call->id > oc->call->id))) {
1198                         voicecall_emit_multiparty(oc, FALSE);
1199                         o = o->next;
1200                 } else if (nc && (oc == NULL || (nc->call->id < oc->call->id))) {
1201                         voicecall_emit_multiparty(nc, TRUE);
1202                         n = n->next;
1203                 } else {
1204                         n = n->next;
1205                         o = o->next;
1206                 }
1207         }
1208 }
1209
1210 static void voicecalls_emit_call_removed(struct ofono_voicecall *vc,
1211                                                 struct voicecall *v)
1212 {
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);
1216
1217         g_dbus_emit_signal(conn, atompath, OFONO_VOICECALL_MANAGER_INTERFACE,
1218                                 "CallRemoved", DBUS_TYPE_OBJECT_PATH, &path,
1219                                 DBUS_TYPE_INVALID);
1220 }
1221
1222 static void voicecalls_emit_call_added(struct ofono_voicecall *vc,
1223                                         struct voicecall *v)
1224 {
1225         DBusMessage *signal;
1226         DBusMessageIter iter;
1227         DBusMessageIter dict;
1228         const char *path;
1229
1230         notify_emulator_call_status(vc);
1231
1232         path = __ofono_atom_get_path(vc->atom);
1233
1234         signal = dbus_message_new_signal(path,
1235                                         OFONO_VOICECALL_MANAGER_INTERFACE,
1236                                         "CallAdded");
1237
1238         if (signal == NULL)
1239                 return;
1240
1241         dbus_message_iter_init_append(signal, &iter);
1242
1243         path = voicecall_build_path(vc, v->call);
1244         dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
1245
1246         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1247                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
1248                                         &dict);
1249         append_voicecall_properties(v, &dict);
1250         dbus_message_iter_close_container(&iter, &dict);
1251
1252         g_dbus_send_message(ofono_dbus_get_connection(), signal);
1253 }
1254
1255 static void voicecalls_release_queue(struct ofono_voicecall *vc, GSList *calls,
1256                                         ofono_voicecall_cb_t cb,
1257                                         ofono_bool_t skip_held)
1258 {
1259         GSList *l;
1260         struct voicecall *call;
1261
1262         g_slist_free(vc->release_list);
1263         vc->release_list = NULL;
1264
1265         for (l = calls; l; l = l->next) {
1266                 call = l->data;
1267
1268                 if (call->call->status == CALL_STATUS_WAITING)
1269                         continue;
1270
1271                 if (skip_held && call->call->status == CALL_STATUS_HELD)
1272                         continue;
1273
1274                 vc->release_list = g_slist_prepend(vc->release_list, l->data);
1275         }
1276
1277         vc->release_queue_done_cb = cb;
1278 }
1279
1280 static void voicecalls_release_next(struct ofono_voicecall *vc)
1281 {
1282         struct voicecall *call;
1283
1284         if (vc->release_list == NULL)
1285                 return;
1286
1287         call = vc->release_list->data;
1288
1289         vc->release_list = g_slist_remove(vc->release_list, call);
1290
1291         if (vc->driver->hangup_active == NULL)
1292                 goto fallback;
1293
1294         if (call->call->status == CALL_STATUS_ACTIVE &&
1295                                         voicecalls_num_active(vc) == 1) {
1296                 vc->driver->hangup_active(vc, multirelease_callback, vc);
1297                 return;
1298         }
1299
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);
1304                 return;
1305         }
1306
1307 fallback:
1308         vc->driver->release_specific(vc, call->call->id,
1309                                         multirelease_callback, vc);
1310 }
1311
1312 static void voicecalls_release_done(const struct ofono_error *error, void *data)
1313 {
1314         struct ofono_voicecall *vc = data;
1315         DBusMessage *reply;
1316
1317         reply = dbus_message_new_method_return(vc->pending);
1318         __ofono_dbus_pending_reply(&vc->pending, reply);
1319 }
1320
1321 static DBusMessage *manager_get_properties(DBusConnection *conn,
1322                                                 DBusMessage *msg, void *data)
1323 {
1324         struct ofono_voicecall *vc = data;
1325         DBusMessage *reply;
1326         DBusMessageIter iter;
1327         DBusMessageIter dict;
1328         int i;
1329         char **list;
1330         GHashTableIter ht_iter;
1331         gpointer key, value;
1332
1333         reply = dbus_message_new_method_return(msg);
1334         if (reply == NULL)
1335                 return NULL;
1336
1337         dbus_message_iter_init_append(reply, &iter);
1338
1339         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1340                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
1341                                         &dict);
1342
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);
1346
1347         for (i = 0; g_hash_table_iter_next(&ht_iter, &key, &value); i++)
1348                 list[i] = key;
1349
1350         ofono_dbus_dict_append_array(&dict, "EmergencyNumbers",
1351                                         DBUS_TYPE_STRING, &list);
1352         g_free(list);
1353
1354         dbus_message_iter_close_container(&iter, &dict);
1355
1356         return reply;
1357 }
1358
1359 static ofono_bool_t clir_string_to_clir(const char *clirstr,
1360                                         enum ofono_clir_option *clir)
1361 {
1362         if (strlen(clirstr) == 0 || !strcmp(clirstr, "default")) {
1363                 *clir = OFONO_CLIR_OPTION_DEFAULT;
1364                 return TRUE;
1365         } else if (!strcmp(clirstr, "disabled")) {
1366                 *clir = OFONO_CLIR_OPTION_SUPPRESSION;
1367                 return TRUE;
1368         } else if (!strcmp(clirstr, "enabled")) {
1369                 *clir = OFONO_CLIR_OPTION_INVOCATION;
1370                 return TRUE;
1371         } else {
1372                 return FALSE;
1373         }
1374 }
1375
1376 static struct ofono_call *synthesize_outgoing_call(struct ofono_voicecall *vc,
1377                                                         const char *number)
1378 {
1379         struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
1380         struct ofono_call *call;
1381
1382         call = g_try_new0(struct ofono_call, 1);
1383         if (call == NULL)
1384                 return call;
1385
1386         call->id = __ofono_modem_callid_next(modem);
1387
1388         if (call->id == 0) {
1389                 ofono_error("Failed to alloc callid, too many calls");
1390                 g_free(call);
1391                 return NULL;
1392         }
1393
1394         __ofono_modem_callid_hold(modem, call->id);
1395
1396         if (number)
1397                 string_to_phone_number(number, &call->phone_number);
1398
1399         call->direction = CALL_DIRECTION_MOBILE_ORIGINATED;
1400         call->status = CALL_STATUS_DIALING;
1401         call->clip_validity = CLIP_VALIDITY_VALID;
1402
1403         return call;
1404 }
1405
1406 static struct voicecall *dial_handle_result(struct ofono_voicecall *vc,
1407                                                 const struct ofono_error *error,
1408                                                 const char *number,
1409                                                 gboolean *need_to_emit)
1410 {
1411         GSList *l;
1412         struct voicecall *v;
1413         struct ofono_call *call;
1414
1415         *need_to_emit = FALSE;
1416
1417         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
1418                 DBG("Dial callback returned error: %s",
1419                         telephony_error_to_str(error));
1420
1421                 return NULL;
1422         }
1423
1424         /*
1425          * Two things can happen, the call notification arrived before dial
1426          * callback or dial callback was first. Handle here
1427          */
1428         for (l = vc->call_list; l; l = l->next) {
1429                 v = l->data;
1430
1431                 if (v->call->status == CALL_STATUS_DIALING ||
1432                                 v->call->status == CALL_STATUS_ALERTING)
1433                         goto handled;
1434
1435                 /*
1436                  * Dial request may return before existing active call
1437                  * is put on hold or after dialed call has got active
1438                  */
1439                 if (v->call->status == CALL_STATUS_ACTIVE &&
1440                                 v->call->direction ==
1441                                 CALL_DIRECTION_MOBILE_ORIGINATED &&
1442                                 !v->dial_result_handled)
1443                         goto handled;
1444         }
1445
1446         call = synthesize_outgoing_call(vc, number);
1447         if (call == NULL)
1448                 return NULL;
1449
1450         v = voicecall_create(vc, call);
1451         if (v == NULL)
1452                 return NULL;
1453
1454         v->detect_time = time(NULL);
1455
1456         DBG("Registering new call: %d", call->id);
1457         voicecall_dbus_register(v);
1458
1459         vc->call_list = g_slist_insert_sorted(vc->call_list, v,
1460                                 call_compare);
1461
1462         *need_to_emit = TRUE;
1463
1464 handled:
1465         v->dial_result_handled = TRUE;
1466
1467         return v;
1468 }
1469
1470 static void manager_dial_callback(const struct ofono_error *error, void *data)
1471 {
1472         struct ofono_voicecall *vc = data;
1473         DBusMessage *reply;
1474         const char *number;
1475         gboolean need_to_emit;
1476         struct voicecall *v;
1477
1478         if (dbus_message_get_args(vc->pending, NULL, DBUS_TYPE_STRING, &number,
1479                                         DBUS_TYPE_INVALID) == FALSE)
1480                 number = NULL;
1481
1482         v = dial_handle_result(vc, error, number, &need_to_emit);
1483
1484         if (v) {
1485                 const char *path = voicecall_build_path(vc, v->call);
1486
1487                 reply = dbus_message_new_method_return(vc->pending);
1488
1489                 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
1490                                                 DBUS_TYPE_INVALID);
1491         } else {
1492                 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
1493
1494                 if (is_emergency_number(vc, number) == TRUE)
1495                         __ofono_modem_dec_emergency_mode(modem);
1496
1497                 reply = __ofono_error_failed(vc->pending);
1498         }
1499
1500         __ofono_dbus_pending_reply(&vc->pending, reply);
1501
1502         if (need_to_emit)
1503                 voicecalls_emit_call_added(vc, v);
1504 }
1505
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)
1509 {
1510         struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
1511         struct ofono_phone_number ph;
1512
1513         if (g_slist_length(vc->call_list) >= MAX_VOICE_CALLS)
1514                 return -EPERM;
1515
1516         if (!valid_long_phone_number_format(number))
1517                 return -EINVAL;
1518
1519         if (ofono_modem_get_online(modem) == FALSE)
1520                 return -ENETDOWN;
1521
1522         if (vc->driver->dial == NULL)
1523                 return -ENOTSUP;
1524
1525         if (voicecalls_have_incoming(vc))
1526                 return -EBUSY;
1527
1528         /* We can't have two dialing/alerting calls, reject outright */
1529         if (voicecalls_num_connecting(vc) > 0)
1530                 return -EBUSY;
1531
1532         if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1533                 return -EBUSY;
1534
1535         if (is_emergency_number(vc, number) == TRUE)
1536                 __ofono_modem_inc_emergency_mode(modem);
1537
1538         string_to_phone_number(number, &ph);
1539
1540         if (vc->settings) {
1541                 g_key_file_set_string(vc->settings, SETTINGS_GROUP,
1542                                         "Number", number);
1543                 storage_sync(vc->imsi, SETTINGS_STORE, vc->settings);
1544         }
1545
1546         vc->driver->dial(vc, &ph, clir, cb, vc);
1547
1548         return 0;
1549 }
1550
1551 static DBusMessage *manager_dial(DBusConnection *conn,
1552                                         DBusMessage *msg, void *data)
1553 {
1554         struct ofono_voicecall *vc = data;
1555         const char *number;
1556         const char *clirstr;
1557         enum ofono_clir_option clir;
1558         int err;
1559
1560         if (vc->pending || vc->dial_req || vc->pending_em)
1561                 return __ofono_error_busy(msg);
1562
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);
1567
1568         if (clir_string_to_clir(clirstr, &clir) == FALSE)
1569                 return __ofono_error_invalid_format(msg);
1570
1571         vc->pending = dbus_message_ref(msg);
1572
1573         err = voicecall_dial(vc, number, clir, manager_dial_callback, vc);
1574
1575         if (err >= 0)
1576                 return NULL;
1577
1578         vc->pending = NULL;
1579         dbus_message_unref(msg);
1580
1581         switch (err) {
1582         case -EINVAL:
1583                 return __ofono_error_invalid_format(msg);
1584
1585         case -ENETDOWN:
1586                 return __ofono_error_not_available(msg);
1587
1588         case -ENOTSUP:
1589                 return __ofono_error_not_implemented(msg);
1590         }
1591
1592         return __ofono_error_failed(msg);
1593 }
1594
1595 static DBusMessage *manager_transfer(DBusConnection *conn,
1596                                         DBusMessage *msg, void *data)
1597 {
1598         struct ofono_voicecall *vc = data;
1599         int numactive;
1600         int numheld;
1601
1602         if (vc->pending || vc->dial_req || vc->pending_em)
1603                 return __ofono_error_busy(msg);
1604
1605         numactive = voicecalls_num_active(vc);
1606
1607         /*
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.
1611          */
1612         numactive += voicecalls_num_connecting(vc);
1613
1614         numheld = voicecalls_num_held(vc);
1615
1616         if (numactive != 1 || numheld != 1)
1617                 return __ofono_error_failed(msg);
1618
1619         if (vc->driver->transfer == NULL)
1620                 return __ofono_error_not_implemented(msg);
1621
1622         vc->pending = dbus_message_ref(msg);
1623
1624         vc->driver->transfer(vc, generic_callback, vc);
1625
1626         return NULL;
1627 }
1628
1629 static DBusMessage *manager_swap_without_accept(DBusConnection *conn,
1630                                                 DBusMessage *msg, void *data)
1631 {
1632         struct ofono_voicecall *vc = data;
1633         ofono_voicecall_cb_t cb;
1634
1635         if (vc->pending || vc->dial_req || vc->pending_em)
1636                 return __ofono_error_busy(msg);
1637
1638         vc->pending = dbus_message_ref(msg);
1639
1640         if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1641                 cb = send_ciev_after_swap_callback;
1642         else
1643                 cb = generic_callback;
1644
1645         vc->driver->swap_without_accept(vc, cb, vc);
1646
1647         return NULL;
1648 }
1649
1650
1651 static DBusMessage *manager_swap_calls(DBusConnection *conn,
1652                                         DBusMessage *msg, void *data)
1653 {
1654         struct ofono_voicecall *vc = data;
1655         ofono_voicecall_cb_t cb;
1656
1657         if (vc->driver->swap_without_accept)
1658                 return manager_swap_without_accept(conn, msg, data);
1659
1660         if (vc->pending || vc->dial_req || vc->pending_em)
1661                 return __ofono_error_busy(msg);
1662
1663         if (voicecalls_have_waiting(vc))
1664                 return __ofono_error_failed(msg);
1665
1666         if (vc->driver->hold_all_active == NULL)
1667                 return __ofono_error_not_implemented(msg);
1668
1669         vc->pending = dbus_message_ref(msg);
1670
1671         if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1672                 cb = send_ciev_after_swap_callback;
1673         else
1674                 cb = generic_callback;
1675
1676         vc->driver->hold_all_active(vc, cb, vc);
1677
1678         return NULL;
1679 }
1680
1681 static DBusMessage *manager_release_and_answer(DBusConnection *conn,
1682                                                 DBusMessage *msg, void *data)
1683 {
1684         struct ofono_voicecall *vc = data;
1685
1686         if (vc->pending || vc->dial_req || vc->pending_em)
1687                 return __ofono_error_busy(msg);
1688
1689         if (!voicecalls_have_waiting(vc))
1690                 return __ofono_error_failed(msg);
1691
1692         if (vc->driver->release_all_active == NULL)
1693                 return __ofono_error_not_implemented(msg);
1694
1695         vc->pending = dbus_message_ref(msg);
1696
1697         vc->driver->release_all_active(vc, generic_callback, vc);
1698
1699         return NULL;
1700 }
1701
1702 static DBusMessage *manager_hold_and_answer(DBusConnection *conn,
1703                                                 DBusMessage *msg, void *data)
1704 {
1705         struct ofono_voicecall *vc = data;
1706
1707         if (vc->pending || vc->dial_req || vc->pending_em)
1708                 return __ofono_error_busy(msg);
1709
1710         if (voicecalls_have_waiting(vc) == FALSE)
1711                 return __ofono_error_failed(msg);
1712
1713         /*
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.
1716          */
1717         if (voicecalls_have_active(vc) && voicecalls_have_held(vc))
1718                 return __ofono_error_failed(msg);
1719
1720         if (vc->driver->hold_all_active == NULL)
1721                 return __ofono_error_not_implemented(msg);
1722
1723         vc->pending = dbus_message_ref(msg);
1724
1725         vc->driver->hold_all_active(vc, generic_callback, vc);
1726
1727         return NULL;
1728 }
1729
1730 static DBusMessage *manager_hangup_all(DBusConnection *conn,
1731                                         DBusMessage *msg, void *data)
1732 {
1733         struct ofono_voicecall *vc = data;
1734
1735         if (vc->pending || vc->pending_em)
1736                 return __ofono_error_busy(msg);
1737
1738         if (vc->dial_req && vc->dial_req->call == NULL)
1739                 return __ofono_error_busy(msg);
1740
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);
1745
1746         if (vc->call_list == NULL) {
1747                 DBusMessage *reply = dbus_message_new_method_return(msg);
1748                 return reply;
1749         }
1750
1751         vc->pending = dbus_message_ref(msg);
1752
1753         if (vc->driver->hangup_all) {
1754                 vc->driver->hangup_all(vc, generic_callback, vc);
1755                 return NULL;
1756         }
1757
1758         if (voicecalls_num_held(vc) > 0)
1759                 vc->driver->hangup_active(vc, hangup_all_active, vc);
1760         else
1761                 vc->driver->hangup_active(vc, generic_callback, vc);
1762
1763         return NULL;
1764 }
1765
1766 static void multiparty_callback_common(struct ofono_voicecall *vc,
1767                                         DBusMessage *reply)
1768 {
1769         DBusMessageIter iter;
1770         DBusMessageIter array_iter;
1771         char **objpath_list;
1772         int i;
1773
1774         voicecalls_path_list(vc, vc->multiparty_list, &objpath_list);
1775
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);
1779
1780         for (i = 0; objpath_list[i]; i++)
1781                 dbus_message_iter_append_basic(&array_iter,
1782                         DBUS_TYPE_OBJECT_PATH, &objpath_list[i]);
1783
1784         dbus_message_iter_close_container(&iter, &array_iter);
1785
1786         g_strfreev(objpath_list);
1787 }
1788
1789 static void private_chat_callback(const struct ofono_error *error, void *data)
1790 {
1791         struct ofono_voicecall *vc = data;
1792         DBusMessage *reply;
1793         const char *callpath;
1794         const char *c;
1795         int id;
1796         GSList *l;
1797         GSList *old;
1798
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));
1804                 return;
1805         }
1806
1807         dbus_message_get_args(vc->pending, NULL,
1808                                 DBUS_TYPE_OBJECT_PATH, &callpath,
1809                                 DBUS_TYPE_INVALID);
1810
1811         c = strrchr(callpath, '/');
1812         sscanf(c, "/voicecall%2u", &id);
1813
1814         old = g_slist_copy(vc->multiparty_list);
1815
1816         l = g_slist_find_custom(vc->multiparty_list, GINT_TO_POINTER(id),
1817                                 call_compare_by_id);
1818
1819         if (l) {
1820                 vc->multiparty_list =
1821                         g_slist_remove(vc->multiparty_list, l->data);
1822
1823                 if (vc->multiparty_list->next == NULL) {
1824                         g_slist_free(vc->multiparty_list);
1825                         vc->multiparty_list = NULL;
1826                 }
1827         }
1828
1829         reply = dbus_message_new_method_return(vc->pending);
1830         multiparty_callback_common(vc, reply);
1831         __ofono_dbus_pending_reply(&vc->pending, reply);
1832
1833         voicecalls_multiparty_changed(old, vc->multiparty_list);
1834         g_slist_free(old);
1835 }
1836
1837 static DBusMessage *multiparty_private_chat(DBusConnection *conn,
1838                                                 DBusMessage *msg, void *data)
1839 {
1840         struct ofono_voicecall *vc = data;
1841         const char *path = __ofono_atom_get_path(vc->atom);
1842         const char *callpath;
1843         const char *c;
1844         unsigned int id;
1845         GSList *l;
1846
1847         if (vc->pending || vc->dial_req || vc->pending_em)
1848                 return __ofono_error_busy(msg);
1849
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);
1853
1854         if (strlen(callpath) == 0)
1855                 return __ofono_error_invalid_format(msg);
1856
1857         c = strrchr(callpath, '/');
1858
1859         if (c == NULL || strncmp(path, callpath, c-callpath))
1860                 return __ofono_error_not_found(msg);
1861
1862         if (!sscanf(c, "/voicecall%2u", &id))
1863                 return __ofono_error_not_found(msg);
1864
1865         for (l = vc->multiparty_list; l; l = l->next) {
1866                 struct voicecall *v = l->data;
1867                 if (v->call->id == id)
1868                         break;
1869         }
1870
1871         if (l == NULL)
1872                 return __ofono_error_not_found(msg);
1873
1874         /*
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
1877          * held calls
1878          */
1879         if (voicecalls_have_held(vc))
1880                 return __ofono_error_failed(msg);
1881
1882         if (vc->driver->private_chat == NULL)
1883                 return __ofono_error_not_implemented(msg);
1884
1885         vc->pending = dbus_message_ref(msg);
1886
1887         vc->driver->private_chat(vc, id, private_chat_callback, vc);
1888
1889         return NULL;
1890 }
1891
1892 static void multiparty_create_callback(const struct ofono_error *error,
1893                                         void *data)
1894 {
1895         struct ofono_voicecall *vc = data;
1896         DBusMessage *reply;
1897         GSList *old;
1898
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));
1904                 return;
1905         }
1906
1907         /*
1908          * We just created a multiparty call, gather all held
1909          * active calls and add them to the multiparty list
1910          */
1911         old = vc->multiparty_list;
1912         vc->multiparty_list = NULL;
1913
1914         vc->multiparty_list = g_slist_concat(vc->multiparty_list,
1915                                                 voicecalls_held_list(vc));
1916
1917         vc->multiparty_list = g_slist_concat(vc->multiparty_list,
1918                                                 voicecalls_active_list(vc));
1919
1920         vc->multiparty_list = g_slist_sort(vc->multiparty_list,
1921                                                 call_compare);
1922
1923         if (g_slist_length(vc->multiparty_list) < 2) {
1924                 ofono_error("Created multiparty call, but size is less than 2"
1925                                 " panic!");
1926
1927                 __ofono_dbus_pending_reply(&vc->pending,
1928                                         __ofono_error_failed(vc->pending));
1929                 return;
1930         }
1931
1932         reply = dbus_message_new_method_return(vc->pending);
1933         multiparty_callback_common(vc, reply);
1934         __ofono_dbus_pending_reply(&vc->pending, reply);
1935
1936         voicecalls_multiparty_changed(old, vc->multiparty_list);
1937         g_slist_free(old);
1938 }
1939
1940 static DBusMessage *multiparty_create(DBusConnection *conn,
1941                                                 DBusMessage *msg, void *data)
1942 {
1943         struct ofono_voicecall *vc = data;
1944
1945         if (vc->pending || vc->dial_req || vc->pending_em)
1946                 return __ofono_error_busy(msg);
1947
1948         if (!voicecalls_have_held(vc) || !voicecalls_have_active(vc))
1949                 return __ofono_error_failed(msg);
1950
1951         if (vc->driver->create_multiparty == NULL)
1952                 return __ofono_error_not_implemented(msg);
1953
1954         vc->pending = dbus_message_ref(msg);
1955
1956         vc->driver->create_multiparty(vc, multiparty_create_callback, vc);
1957
1958         return NULL;
1959 }
1960
1961 static DBusMessage *multiparty_hangup(DBusConnection *conn,
1962                                                 DBusMessage *msg, void *data)
1963 {
1964         struct ofono_voicecall *vc = data;
1965
1966         if (vc->pending || vc->dial_req || vc->pending_em)
1967                 return __ofono_error_busy(msg);
1968
1969         if (vc->driver->release_specific == NULL)
1970                 return __ofono_error_not_implemented(msg);
1971
1972         if (vc->driver->release_all_held == NULL)
1973                 return __ofono_error_not_implemented(msg);
1974
1975         if (vc->driver->release_all_active == NULL)
1976                 return __ofono_error_not_implemented(msg);
1977
1978         if (vc->multiparty_list == NULL) {
1979                 DBusMessage *reply = dbus_message_new_method_return(msg);
1980                 return reply;
1981         }
1982
1983         vc->pending = dbus_message_ref(msg);
1984
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;
1988
1989                 if (v->call->status == CALL_STATUS_HELD) {
1990                         vc->driver->release_all_held(vc, generic_callback,
1991                                                         vc);
1992                         goto out;
1993                 }
1994
1995                 /*
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
1999                  */
2000                 if (!voicecalls_have_held(vc)) {
2001                         vc->driver->release_all_active(vc, generic_callback,
2002                                                                 vc);
2003                         goto out;
2004                 }
2005         }
2006
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);
2011
2012 out:
2013         return NULL;
2014 }
2015
2016 static void tone_callback(int error, void *data)
2017 {
2018         struct ofono_voicecall *vc = data;
2019         DBusMessage *reply;
2020
2021         if (error)
2022                 reply = __ofono_error_failed(vc->pending);
2023         else
2024                 reply = dbus_message_new_method_return(vc->pending);
2025
2026         __ofono_dbus_pending_reply(&vc->pending, reply);
2027 }
2028
2029 static DBusMessage *manager_tone(DBusConnection *conn,
2030                                         DBusMessage *msg, void *data)
2031 {
2032         struct ofono_voicecall *vc = data;
2033         const char *in_tones;
2034         char *tones;
2035         int err, len;
2036
2037         if (vc->pending)
2038                 return __ofono_error_busy(msg);
2039
2040         if (vc->driver->send_tones == NULL)
2041                 return __ofono_error_not_implemented(msg);
2042
2043         /* Send DTMFs only if we have at least one connected call */
2044         if (!voicecalls_can_dtmf(vc))
2045                 return __ofono_error_failed(msg);
2046
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);
2050
2051         len = strlen(in_tones);
2052
2053         if (len == 0)
2054                 return __ofono_error_invalid_format(msg);
2055
2056         tones = g_ascii_strup(in_tones, len);
2057
2058         err = tone_queue(vc, tones, tone_callback, vc, NULL);
2059
2060         g_free(tones);
2061
2062         if (err < 0)
2063                 return __ofono_error_invalid_format(msg);
2064
2065         vc->pending = dbus_message_ref(msg);
2066
2067         return NULL;
2068 }
2069
2070 static DBusMessage *manager_get_calls(DBusConnection *conn,
2071                                         DBusMessage *msg, void *data)
2072 {
2073         struct ofono_voicecall *vc = data;
2074         DBusMessage *reply;
2075         DBusMessageIter iter;
2076         DBusMessageIter array;
2077         DBusMessageIter entry, dict;
2078         const char *path;
2079         GSList *l;
2080         struct voicecall *v;
2081
2082         reply = dbus_message_new_method_return(msg);
2083         if (reply == NULL)
2084                 return NULL;
2085
2086         dbus_message_iter_init_append(reply, &iter);
2087
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,
2097                                         &array);
2098
2099         for (l = vc->call_list; l; l = l->next) {
2100                 v = l->data;
2101
2102                 path = voicecall_build_path(vc, v->call);
2103
2104                 dbus_message_iter_open_container(&array, DBUS_TYPE_STRUCT,
2105                                                         NULL, &entry);
2106                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
2107                                                 &path);
2108                 dbus_message_iter_open_container(&entry, DBUS_TYPE_ARRAY,
2109                                         OFONO_PROPERTIES_ARRAY_SIGNATURE,
2110                                         &dict);
2111
2112                 append_voicecall_properties(v, &dict);
2113                 dbus_message_iter_close_container(&entry, &dict);
2114                 dbus_message_iter_close_container(&array, &entry);
2115         }
2116
2117         dbus_message_iter_close_container(&iter, &array);
2118
2119         return reply;
2120 }
2121
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" }),
2129                 manager_dial) },
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,
2148                                 manager_tone) },
2149         { GDBUS_METHOD("GetCalls",
2150                 NULL, GDBUS_ARGS({ "calls_with_properties", "a(oa{sv})" }),
2151                 manager_get_calls) },
2152         { }
2153 };
2154
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"})) },
2163         { }
2164 };
2165
2166 void ofono_voicecall_disconnected(struct ofono_voicecall *vc, int id,
2167                                 enum ofono_disconnect_reason reason,
2168                                 const struct ofono_error *error)
2169 {
2170         struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
2171         GSList *l;
2172         struct voicecall *call;
2173         time_t ts;
2174         enum call_status prev_status;
2175         const char *number;
2176
2177         DBG("Got disconnection event for id: %d, reason: %d", id, reason);
2178
2179         __ofono_modem_callid_release(modem, id);
2180
2181         l = g_slist_find_custom(vc->call_list, GUINT_TO_POINTER(id),
2182                                 call_compare_by_id);
2183
2184         if (l == NULL) {
2185                 ofono_error("Plugin notified us of call disconnect for"
2186                                 " unknown call");
2187                 return;
2188         }
2189
2190         call = l->data;
2191
2192         ts = time(NULL);
2193         prev_status = call->call->status;
2194
2195         l = g_slist_find_custom(vc->multiparty_list, GUINT_TO_POINTER(id),
2196                                 call_compare_by_id);
2197
2198         if (l) {
2199                 vc->multiparty_list =
2200                         g_slist_remove(vc->multiparty_list, call);
2201
2202                 if (vc->multiparty_list->next == NULL) { /* Size == 1 */
2203                         struct voicecall *v = vc->multiparty_list->data;
2204
2205                         voicecall_emit_multiparty(v, FALSE);
2206                         g_slist_free(vc->multiparty_list);
2207                         vc->multiparty_list = NULL;
2208                 }
2209         }
2210
2211         vc->release_list = g_slist_remove(vc->release_list, call);
2212
2213         if (reason != OFONO_DISCONNECT_REASON_UNKNOWN)
2214                 voicecall_emit_disconnect_reason(call, reason);
2215
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);
2219
2220         voicecall_set_call_status(call, CALL_STATUS_DISCONNECTED);
2221
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);
2226                 else
2227                         __ofono_history_call_ended(modem, call->call,
2228                                                         call->detect_time, ts);
2229         }
2230
2231         voicecalls_emit_call_removed(vc, call);
2232
2233         voicecall_dbus_unregister(vc, call);
2234
2235         vc->call_list = g_slist_remove(vc->call_list, call);
2236 }
2237
2238 void ofono_voicecall_notify(struct ofono_voicecall *vc,
2239                                 const struct ofono_call *call)
2240 {
2241         struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
2242         GSList *l;
2243         struct voicecall *v = NULL;
2244         struct ofono_call *newcall;
2245
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);
2250
2251         l = g_slist_find_custom(vc->call_list, GUINT_TO_POINTER(call->id),
2252                                 call_compare_by_id);
2253
2254         if (l) {
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);
2262
2263                 return;
2264         }
2265
2266         DBG("Did not find a call with id: %d", call->id);
2267
2268         __ofono_modem_callid_hold(modem, call->id);
2269
2270         newcall = g_memdup(call, sizeof(struct ofono_call));
2271         if (newcall == NULL) {
2272                 ofono_error("Unable to allocate call");
2273                 goto error;
2274         }
2275
2276         v = voicecall_create(vc, newcall);
2277         if (v == NULL) {
2278                 ofono_error("Unable to allocate voicecall_data");
2279                 goto error;
2280         }
2281
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);
2285
2286                 if (!strcmp(number, "112")) {
2287                         struct ofono_modem *modem =
2288                                         __ofono_atom_get_modem(vc->atom);
2289
2290                         __ofono_modem_inc_emergency_mode(modem);
2291                 }
2292
2293                 if (v->call->clip_validity == CLIP_VALIDITY_NOT_AVAILABLE) {
2294                         char *number = v->call->phone_number.number;
2295
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';
2301                 }
2302
2303                 v->message = req->message;
2304                 v->icon_id = req->icon_id;
2305
2306                 req->message = NULL;
2307                 req->call = v;
2308
2309                 /*
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)
2313                  */
2314                 v->untracked = TRUE;
2315                 vc->flags &= ~VOICECALL_FLAG_STK_MODEM_CALLSETUP;
2316         }
2317
2318         v->detect_time = time(NULL);
2319
2320         if (!voicecall_dbus_register(v)) {
2321                 ofono_error("Unable to register voice call");
2322                 goto error;
2323         }
2324
2325         vc->call_list = g_slist_insert_sorted(vc->call_list, v, call_compare);
2326
2327         voicecalls_emit_call_added(vc, v);
2328
2329         return;
2330
2331 error:
2332         if (newcall)
2333                 g_free(newcall);
2334
2335         if (v)
2336                 g_free(v);
2337 }
2338
2339 static void send_ciev_after_swap_callback(const struct ofono_error *error,
2340                                                                 void *data)
2341 {
2342         struct ofono_voicecall *vc = data;
2343         DBusMessage *reply;
2344
2345         if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2346                 DBG("command failed with error: %s",
2347                                 telephony_error_to_str(error));
2348
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);
2353         } else
2354                 reply = __ofono_error_failed(vc->pending);
2355
2356         __ofono_dbus_pending_reply(&vc->pending, reply);
2357 }
2358
2359 static void generic_callback(const struct ofono_error *error, void *data)
2360 {
2361         struct ofono_voicecall *vc = data;
2362         DBusMessage *reply;
2363
2364         if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2365                 DBG("command failed with error: %s",
2366                                 telephony_error_to_str(error));
2367
2368         if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
2369                 reply = dbus_message_new_method_return(vc->pending);
2370         else
2371                 reply = __ofono_error_failed(vc->pending);
2372
2373         __ofono_dbus_pending_reply(&vc->pending, reply);
2374 }
2375
2376 static void hangup_all_active(const struct ofono_error *error, void *data)
2377 {
2378         struct ofono_voicecall *vc = data;
2379
2380         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
2381                 __ofono_dbus_pending_reply(&vc->pending,
2382                                         __ofono_error_failed(vc->pending));
2383                 return;
2384         }
2385
2386         /*
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
2391          * remain.
2392          */
2393         if (vc->driver->release_all_held == NULL ||
2394                         voicecalls_have_waiting(vc)) {
2395                 GSList *held = voicecalls_held_list(vc);
2396
2397                 voicecalls_release_queue(vc, held,
2398                                                 voicecalls_release_done, FALSE);
2399                 voicecalls_release_next(vc);
2400
2401                 g_slist_free(held);
2402         } else
2403                 vc->driver->release_all_held(vc, generic_callback, vc);
2404 }
2405
2406 static void multirelease_callback(const struct ofono_error *error, void *data)
2407 {
2408         struct ofono_voicecall *vc = data;
2409
2410         if (vc->release_list != NULL) {
2411                 voicecalls_release_next(vc);
2412                 return;
2413         }
2414
2415         vc->release_queue_done_cb(error, vc);
2416 }
2417
2418 static void emit_en_list_changed(struct ofono_voicecall *vc)
2419 {
2420         DBusConnection *conn = ofono_dbus_get_connection();
2421         const char *path = __ofono_atom_get_path(vc->atom);
2422         char **list;
2423         int i;
2424         GHashTableIter iter;
2425         gpointer key, value;
2426
2427         list = g_new0(char *, g_hash_table_size(vc->en_list) + 1);
2428         g_hash_table_iter_init(&iter, vc->en_list);
2429
2430         for (i = 0; g_hash_table_iter_next(&iter, &key, &value); i++)
2431                 list[i] = key;
2432
2433         ofono_dbus_signal_array_property_changed(conn, path,
2434                                 OFONO_VOICECALL_MANAGER_INTERFACE,
2435                                 "EmergencyNumbers", DBUS_TYPE_STRING, &list);
2436
2437         g_free(list);
2438 }
2439
2440 static void set_new_ecc(struct ofono_voicecall *vc)
2441 {
2442         g_hash_table_destroy(vc->en_list);
2443
2444         vc->en_list = g_hash_table_new_full(g_str_hash, g_str_equal,
2445                                                         g_free, NULL);
2446
2447         /* Emergency numbers from modem/network */
2448         if (vc->nw_en_list)
2449                 add_to_en_list(vc, vc->nw_en_list);
2450
2451         /* Emergency numbers read from SIM */
2452         if (vc->flags & VOICECALL_FLAG_SIM_ECC_READY) {
2453                 GSList *l;
2454
2455                 for (l = vc->sim_en_list; l; l = l->next)
2456                         g_hash_table_insert(vc->en_list, g_strdup(l->data),
2457                                                         NULL);
2458         } else
2459                 add_to_en_list(vc, (char **) default_en_list_no_sim);
2460
2461         /* Default emergency numbers */
2462         add_to_en_list(vc, (char **) default_en_list);
2463
2464         emit_en_list_changed(vc);
2465 }
2466
2467 static void free_sim_ecc_numbers(struct ofono_voicecall *vc, gboolean old_only)
2468 {
2469         /*
2470          * Free the currently being read EN list, just in case the
2471          * we're still reading them
2472          */
2473         if (old_only == FALSE) {
2474                 if (vc->new_sim_en_list) {
2475                         g_slist_foreach(vc->new_sim_en_list, (GFunc) g_free,
2476                                         NULL);
2477                         g_slist_free(vc->new_sim_en_list);
2478                         vc->new_sim_en_list = NULL;
2479                 }
2480
2481                 vc->flags &= ~VOICECALL_FLAG_SIM_ECC_READY;
2482         }
2483
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;
2488         }
2489 }
2490
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)
2494 {
2495         struct ofono_voicecall *vc = userdata;
2496         char en[7];
2497
2498         DBG("%d", ok);
2499
2500         if (!ok)
2501                 return;
2502
2503         if (total_length < 3) {
2504                 ofono_error("Unable to read emergency numbers from SIM");
2505                 return;
2506         }
2507
2508         free_sim_ecc_numbers(vc, TRUE);
2509
2510         total_length /= 3;
2511         while (total_length--) {
2512                 extract_bcd_number(data, 3, en);
2513                 data += 3;
2514
2515                 if (en[0] != '\0')
2516                         vc->sim_en_list = g_slist_prepend(vc->sim_en_list,
2517                                                                 g_strdup(en));
2518         }
2519
2520         vc->flags |= VOICECALL_FLAG_SIM_ECC_READY;
2521
2522         set_new_ecc(vc);
2523 }
2524
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)
2528 {
2529         struct ofono_voicecall *vc = userdata;
2530         int total;
2531         char en[7];
2532
2533         DBG("%d", ok);
2534
2535         if (!ok)
2536                 goto check;
2537
2538         if (record_length < 4 || total_length < record_length) {
2539                 ofono_error("Unable to read emergency numbers from SIM");
2540                 return;
2541         }
2542
2543         total = total_length / record_length;
2544         extract_bcd_number(data, 3, en);
2545
2546         if (en[0] != '\0')
2547                 vc->new_sim_en_list = g_slist_prepend(vc->new_sim_en_list,
2548                                                         g_strdup(en));
2549
2550         if (record != total)
2551                 return;
2552
2553 check:
2554         if (!ok && vc->new_sim_en_list == NULL)
2555                 return;
2556
2557         free_sim_ecc_numbers(vc, TRUE);
2558         vc->sim_en_list = vc->new_sim_en_list;
2559         vc->new_sim_en_list = NULL;
2560
2561         vc->flags |= VOICECALL_FLAG_SIM_ECC_READY;
2562
2563         set_new_ecc(vc);
2564 }
2565
2566 void ofono_voicecall_en_list_notify(struct ofono_voicecall *vc,
2567                                                 char **nw_en_list)
2568 {
2569         g_strfreev(vc->nw_en_list);
2570
2571         vc->nw_en_list = g_strdupv(nw_en_list);
2572         set_new_ecc(vc);
2573 }
2574
2575 int ofono_voicecall_driver_register(const struct ofono_voicecall_driver *d)
2576 {
2577         DBG("driver: %p, name: %s", d, d->name);
2578
2579         if (d->probe == NULL)
2580                 return -EINVAL;
2581
2582         g_drivers = g_slist_prepend(g_drivers, (void *) d);
2583
2584         return 0;
2585 }
2586
2587 void ofono_voicecall_driver_unregister(const struct ofono_voicecall_driver *d)
2588 {
2589         DBG("driver: %p, name: %s", d, d->name);
2590
2591         g_drivers = g_slist_remove(g_drivers, (void *) d);
2592 }
2593
2594 static void emulator_remove_handler(struct ofono_atom *atom, void *data)
2595 {
2596         struct ofono_emulator *em = __ofono_atom_get_data(atom);
2597
2598         ofono_emulator_remove_handler(em, data);
2599 }
2600
2601 static void emulator_hfp_unregister(struct ofono_atom *atom)
2602 {
2603         struct ofono_voicecall *vc = __ofono_atom_get_data(atom);
2604         struct ofono_modem *modem = __ofono_atom_get_modem(atom);
2605
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,
2612                                                 0);
2613         __ofono_modem_foreach_registered_atom(modem,
2614                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2615                                                 emulator_callheld_status_cb, 0);
2616
2617         __ofono_modem_foreach_registered_atom(modem,
2618                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2619                                                 emulator_remove_handler,
2620                                                 "A");
2621         __ofono_modem_foreach_registered_atom(modem,
2622                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2623                                                 emulator_remove_handler,
2624                                                 "+CHUP");
2625         __ofono_modem_foreach_registered_atom(modem,
2626                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2627                                                 emulator_remove_handler,
2628                                                 "+CLCC");
2629         __ofono_modem_foreach_registered_atom(modem,
2630                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2631                                                 emulator_remove_handler,
2632                                                 "+CHLD");
2633         __ofono_modem_foreach_registered_atom(modem,
2634                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2635                                                 emulator_remove_handler,
2636                                                 "+VTS");
2637         __ofono_modem_foreach_registered_atom(modem,
2638                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2639                                                 emulator_remove_handler,
2640                                                 "D");
2641         __ofono_modem_foreach_registered_atom(modem,
2642                                                 OFONO_ATOM_TYPE_EMULATOR_HFP,
2643                                                 emulator_remove_handler,
2644                                                 "+BLDN");
2645
2646         __ofono_modem_remove_atom_watch(modem, vc->hfp_watch);
2647 }
2648
2649 static void voicecall_load_settings(struct ofono_voicecall *vc)
2650 {
2651         const char *imsi;
2652
2653         imsi = ofono_sim_get_imsi(vc->sim);
2654         if (imsi == NULL)
2655                 return;
2656
2657         vc->settings = storage_open(imsi, SETTINGS_STORE);
2658
2659         if (vc->settings == NULL)
2660                 return;
2661
2662         vc->imsi = g_strdup(imsi);
2663 }
2664
2665 static void voicecall_close_settings(struct ofono_voicecall *vc)
2666 {
2667         if (vc->settings) {
2668                 storage_close(vc->imsi, SETTINGS_STORE, vc->settings, TRUE);
2669
2670                 g_free(vc->imsi);
2671                 vc->imsi = NULL;
2672                 vc->settings = NULL;
2673         }
2674 }
2675
2676 static void voicecall_unregister(struct ofono_atom *atom)
2677 {
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);
2682         GSList *l;
2683
2684         emulator_hfp_unregister(atom);
2685
2686         voicecall_close_settings(vc);
2687
2688         if (vc->sim_state_watch) {
2689                 ofono_sim_remove_state_watch(vc->sim, vc->sim_state_watch);
2690                 vc->sim_state_watch = 0;
2691         }
2692
2693         if (vc->sim_watch) {
2694                 __ofono_modem_remove_atom_watch(modem, vc->sim_watch);
2695                 vc->sim_watch = 0;
2696         }
2697
2698         vc->sim = NULL;
2699
2700         free_sim_ecc_numbers(vc, FALSE);
2701
2702         if (vc->nw_en_list) {
2703                 g_strfreev(vc->nw_en_list);
2704                 vc->nw_en_list = NULL;
2705         }
2706
2707         g_hash_table_destroy(vc->en_list);
2708         vc->en_list = NULL;
2709
2710         if (vc->dial_req)
2711                 dial_request_finish(vc);
2712
2713         for (l = vc->call_list; l; l = l->next)
2714                 voicecall_dbus_unregister(vc, l->data);
2715
2716         g_slist_free(vc->call_list);
2717         vc->call_list = NULL;
2718
2719         ofono_modem_remove_interface(modem, OFONO_VOICECALL_MANAGER_INTERFACE);
2720         g_dbus_unregister_interface(conn, path,
2721                                         OFONO_VOICECALL_MANAGER_INTERFACE);
2722 }
2723
2724 static void voicecall_remove(struct ofono_atom *atom)
2725 {
2726         struct ofono_voicecall *vc = __ofono_atom_get_data(atom);
2727
2728         DBG("atom: %p", atom);
2729
2730         if (vc == NULL)
2731                 return;
2732
2733         if (vc->driver && vc->driver->remove)
2734                 vc->driver->remove(vc);
2735
2736         if (vc->tone_source) {
2737                 g_source_remove(vc->tone_source);
2738                 vc->tone_source = 0;
2739         }
2740
2741         if (vc->toneq) {
2742                 struct tone_queue_entry *entry;
2743
2744                 while ((entry = g_queue_peek_head(vc->toneq)))
2745                         tone_request_finish(vc, entry, ESHUTDOWN, TRUE);
2746
2747                 g_queue_free(vc->toneq);
2748         }
2749
2750         g_free(vc);
2751 }
2752
2753 struct ofono_voicecall *ofono_voicecall_create(struct ofono_modem *modem,
2754                                                 unsigned int vendor,
2755                                                 const char *driver,
2756                                                 void *data)
2757 {
2758         struct ofono_voicecall *vc;
2759         GSList *l;
2760
2761         if (driver == NULL)
2762                 return NULL;
2763
2764         vc = g_try_new0(struct ofono_voicecall, 1);
2765
2766         if (vc == NULL)
2767                 return NULL;
2768
2769         vc->toneq = g_queue_new();
2770
2771         vc->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_VOICECALL,
2772                                                 voicecall_remove, vc);
2773
2774         for (l = g_drivers; l; l = l->next) {
2775                 const struct ofono_voicecall_driver *drv = l->data;
2776
2777                 if (g_strcmp0(drv->name, driver))
2778                         continue;
2779
2780                 if (drv->probe(vc, vendor, data) < 0)
2781                         continue;
2782
2783                 vc->driver = drv;
2784                 break;
2785         }
2786
2787         return vc;
2788 }
2789
2790 static void read_sim_ecc_numbers(int id, void *userdata)
2791 {
2792         struct ofono_voicecall *vc = userdata;
2793
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);
2801 }
2802
2803 static void sim_state_watch(enum ofono_sim_state new_state, void *user)
2804 {
2805         struct ofono_voicecall *vc = user;
2806
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);
2811
2812                 read_sim_ecc_numbers(SIM_EFECC_FILEID, vc);
2813
2814                 ofono_sim_add_file_watch(vc->sim_context, SIM_EFECC_FILEID,
2815                                                 read_sim_ecc_numbers, vc, NULL);
2816                 break;
2817         case OFONO_SIM_STATE_NOT_PRESENT:
2818                 /* TODO: Must release all non-emergency calls */
2819
2820                 if (vc->sim_context) {
2821                         ofono_sim_context_free(vc->sim_context);
2822                         vc->sim_context = NULL;
2823                 }
2824
2825                 free_sim_ecc_numbers(vc, FALSE);
2826                 set_new_ecc(vc);
2827
2828                 voicecall_close_settings(vc);
2829                 break;
2830         case OFONO_SIM_STATE_READY:
2831                 voicecall_load_settings(vc);
2832                 break;
2833         case OFONO_SIM_STATE_LOCKED_OUT:
2834                 voicecall_close_settings(vc);
2835                 break;
2836         }
2837 }
2838
2839 static void sim_watch(struct ofono_atom *atom,
2840                         enum ofono_atom_watch_condition cond, void *data)
2841 {
2842         struct ofono_voicecall *vc = data;
2843         struct ofono_sim *sim = __ofono_atom_get_data(atom);
2844
2845         if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
2846                 voicecall_close_settings(vc);
2847                 vc->sim_state_watch = 0;
2848                 vc->sim = NULL;
2849                 return;
2850         }
2851
2852         vc->sim = sim;
2853         vc->sim_state_watch = ofono_sim_add_state_watch(sim,
2854                                                         sim_state_watch,
2855                                                         vc, NULL);
2856
2857         sim_state_watch(ofono_sim_get_state(sim), vc);
2858 }
2859
2860 static void emulator_send_ciev_after_swap_cb(const struct ofono_error *error,
2861                                                                 void *data)
2862 {
2863         struct ofono_voicecall *vc = data;
2864
2865         if (vc->pending_em == NULL)
2866                 return;
2867
2868         ofono_emulator_send_final(vc->pending_em, error);
2869
2870         if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
2871                 emulator_set_indicator_forced(vc, OFONO_EMULATOR_IND_CALLHELD,
2872                                         OFONO_EMULATOR_CALLHELD_MULTIPLE);
2873
2874         vc->pending_em = NULL;
2875 }
2876
2877 static void emulator_generic_cb(const struct ofono_error *error, void *data)
2878 {
2879         struct ofono_voicecall *vc = data;
2880
2881         if (vc->pending_em == NULL)
2882                 return;
2883
2884         ofono_emulator_send_final(vc->pending_em, error);
2885         vc->pending_em = NULL;
2886 }
2887
2888 static void emulator_mpty_join_cb(const struct ofono_error *error, void *data)
2889 {
2890         struct ofono_voicecall *vc = data;
2891         GSList *old;
2892
2893         if (vc->pending_em != NULL)
2894                 ofono_emulator_send_final(vc->pending_em, error);
2895
2896         vc->pending_em = NULL;
2897
2898         if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2899                 return;
2900
2901         /*
2902          * We just created a multiparty call, gather all held
2903          * active calls and add them to the multiparty list
2904          */
2905         old = vc->multiparty_list;
2906         vc->multiparty_list = NULL;
2907
2908         vc->multiparty_list = g_slist_concat(vc->multiparty_list,
2909                                                 voicecalls_held_list(vc));
2910
2911         vc->multiparty_list = g_slist_concat(vc->multiparty_list,
2912                                                 voicecalls_active_list(vc));
2913
2914         vc->multiparty_list = g_slist_sort(vc->multiparty_list,
2915                                                 call_compare);
2916
2917         if (g_slist_length(vc->multiparty_list) < 2) {
2918                 ofono_error("Created multiparty call, but size is less than 2"
2919                                 " panic!");
2920                 g_slist_free(old);
2921                 return;
2922         }
2923
2924         voicecalls_multiparty_changed(old, vc->multiparty_list);
2925         g_slist_free(old);
2926 }
2927
2928 static void emulator_mpty_private_chat_cb(const struct ofono_error *error,
2929                                                         void *data)
2930 {
2931         struct ofono_voicecall *vc = data;
2932         GSList *old;
2933         GSList *l;
2934
2935         if (vc->pending_em != NULL)
2936                 ofono_emulator_send_final(vc->pending_em, error);
2937
2938         vc->pending_em = NULL;
2939
2940         if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
2941                 return;
2942
2943         old = g_slist_copy(vc->multiparty_list);
2944
2945         l = g_slist_find_custom(vc->multiparty_list,
2946                         GINT_TO_POINTER(vc->pending_id), call_compare_by_id);
2947
2948         if (l) {
2949                 vc->multiparty_list =
2950                         g_slist_remove(vc->multiparty_list, l->data);
2951
2952                 if (vc->multiparty_list->next == NULL) {
2953                         g_slist_free(vc->multiparty_list);
2954                         vc->multiparty_list = NULL;
2955                 }
2956         }
2957
2958         voicecalls_multiparty_changed(old, vc->multiparty_list);
2959         g_slist_free(old);
2960 }
2961
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);         \
2967         }                                                       \
2968
2969 static void emulator_ata_cb(struct ofono_emulator *em,
2970                                 struct ofono_emulator_request *req,
2971                                 void *userdata)
2972 {
2973         struct ofono_voicecall *vc = userdata;
2974         struct ofono_error result;
2975
2976         result.error = 0;
2977
2978         switch (ofono_emulator_request_get_type(req)) {
2979         case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
2980                 CHECK_BUSY(vc, em, result)
2981
2982                 if (!voicecalls_have_incoming(vc))
2983                         goto fail;
2984
2985                 if (vc->driver->answer == NULL)
2986                         goto fail;
2987
2988                 vc->pending_em = em;
2989                 vc->driver->answer(vc, emulator_generic_cb, vc);
2990                 break;
2991
2992         default:
2993 fail:
2994                 result.type = OFONO_ERROR_TYPE_FAILURE;
2995                 ofono_emulator_send_final(em, &result);
2996         };
2997 }
2998
2999 static void emulator_chup_cb(struct ofono_emulator *em,
3000                         struct ofono_emulator_request *req, void *userdata)
3001 {
3002         struct ofono_voicecall *vc = userdata;
3003         struct ofono_error result;
3004
3005         result.error = 0;
3006
3007         switch (ofono_emulator_request_get_type(req)) {
3008         case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
3009                 if (vc->pending || vc->pending_em)
3010                         goto fail;
3011
3012                 if (vc->dial_req && vc->dial_req->call == NULL)
3013                         goto fail;
3014
3015                 if (vc->driver->release_specific == NULL &&
3016                                 vc->driver->hangup_active == NULL)
3017                         goto fail;
3018
3019                 if (vc->driver->hangup_active) {
3020                         vc->pending_em = em;
3021                         vc->driver->hangup_active(vc, emulator_generic_cb, vc);
3022                         goto done;
3023                 }
3024
3025                 if (voicecalls_have_active(vc) == FALSE &&
3026                                 voicecalls_have_incoming(vc) == FALSE)
3027                         goto fail;
3028
3029                 vc->pending_em = em;
3030                 voicecalls_release_queue(vc, vc->call_list,
3031                                                 emulator_generic_cb, TRUE);
3032                 voicecalls_release_next(vc);
3033
3034 done:
3035                 break;
3036
3037         default:
3038 fail:
3039                 result.type = OFONO_ERROR_TYPE_FAILURE;
3040                 ofono_emulator_send_final(em, &result);
3041         };
3042 }
3043
3044 static void emulator_clcc_cb(struct ofono_emulator *em,
3045                         struct ofono_emulator_request *req, void *userdata)
3046 {
3047         struct ofono_voicecall *vc = userdata;
3048         struct ofono_error result;
3049         GSList *l;
3050         /*
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
3054          */
3055         char buf[20 + OFONO_MAX_PHONE_NUMBER_LENGTH + 3 + 1];
3056
3057         result.error = 0;
3058
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 = "";
3064                         int type = 128;
3065                         gboolean mpty;
3066
3067                         if (g_slist_find_custom(vc->multiparty_list,
3068                                                 GINT_TO_POINTER(v->call->id),
3069                                                 call_compare_by_id))
3070                                 mpty = TRUE;
3071                         else
3072                                 mpty = FALSE;
3073
3074                         if (v->call->clip_validity == CLIP_VALIDITY_VALID) {
3075                                 number = v->call->phone_number.number;
3076                                 type = v->call->phone_number.type;
3077                         }
3078
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 ?
3083                                                         TRUE : FALSE);
3084                 }
3085
3086                 result.type = OFONO_ERROR_TYPE_NO_ERROR;
3087                 break;
3088
3089         default:
3090                 result.type = OFONO_ERROR_TYPE_FAILURE;
3091         }
3092
3093         ofono_emulator_send_final(em, &result);
3094 }
3095
3096 #define ADD_CHLD_SUPPORT(cond, x)                       \
3097         if (cond) {                                     \
3098                 if (info[-1] != '(')                    \
3099                         *info++ = ',';                  \
3100                                                         \
3101                 *info++ = x[0];                         \
3102                                                         \
3103                 if (x[1])                               \
3104                         *info++ = x[1];                 \
3105         }                                               \
3106
3107 static void emulator_chld_cb(struct ofono_emulator *em,
3108                         struct ofono_emulator_request *req, void *userdata)
3109 {
3110         struct ofono_voicecall *vc = userdata;
3111         struct ofono_error result;
3112         char buf[64];
3113         char *info;
3114         int chld;
3115         ofono_voicecall_cb_t cb;
3116
3117         result.error = 0;
3118
3119         switch (ofono_emulator_request_get_type(req)) {
3120         case OFONO_EMULATOR_REQUEST_TYPE_SET:
3121                 if (!ofono_emulator_request_next_number(req, &chld))
3122                         goto fail;
3123
3124                 CHECK_BUSY(vc, em, result)
3125
3126                 switch (chld) {
3127                 case 0:
3128                         if (vc->driver->set_udub == NULL)
3129                                 goto fail;
3130
3131                         if (vc->driver->release_all_held == NULL)
3132                                 goto fail;
3133
3134                         vc->pending_em = em;
3135
3136                         if (voicecalls_have_waiting(vc)) {
3137                                 vc->driver->set_udub(vc,
3138                                                 emulator_generic_cb, vc);
3139                                 return;
3140                         }
3141
3142                         vc->driver->release_all_held(vc,
3143                                         emulator_generic_cb, vc);
3144                         return;
3145                 case 1:
3146                         if (vc->driver->release_all_active == NULL)
3147                                 goto fail;
3148
3149                         vc->pending_em = em;
3150                         vc->driver->release_all_active(vc,
3151                                         emulator_generic_cb, vc);
3152                         return;
3153                 case 2:
3154                         if (vc->driver->hold_all_active == NULL)
3155                                 goto fail;
3156
3157                         if (voicecalls_have_active(vc) &&
3158                                         voicecalls_have_held(vc))
3159                                 cb = emulator_send_ciev_after_swap_cb;
3160                         else
3161                                 cb = emulator_generic_cb;
3162
3163                         vc->pending_em = em;
3164                         vc->driver->hold_all_active(vc, cb, vc);
3165                         return;
3166                 case 3:
3167                         if (vc->driver->create_multiparty == NULL)
3168                                 goto fail;
3169
3170                         if (!voicecalls_have_held(vc) ||
3171                                         !voicecalls_have_active(vc))
3172                                 goto fail;
3173
3174                         vc->pending_em = em;
3175                         vc->driver->create_multiparty(vc,
3176                                         emulator_mpty_join_cb, vc);
3177                         return;
3178                 case 4:
3179                         if (vc->driver->transfer == NULL)
3180                                 goto fail;
3181
3182                         vc->pending_em = em;
3183                         vc->driver->transfer(vc,
3184                                         emulator_generic_cb, vc);
3185                         return;
3186                 default:
3187                         break;
3188                 }
3189
3190                 if (chld >= 11 && chld <= 17) {
3191                         if (vc->driver->release_specific == NULL)
3192                                 goto fail;
3193
3194                         vc->pending_em = em;
3195                         vc->driver->release_specific(vc, chld - 10,
3196                                                 emulator_generic_cb, vc);
3197                         return;
3198                 }
3199
3200                 if (chld >= 21 && chld <= 27) {
3201                         GSList *l;
3202                         unsigned int id = chld - 20;
3203
3204                         if (vc->driver->private_chat == NULL)
3205                                 goto fail;
3206
3207                         for (l = vc->multiparty_list; l; l = l->next) {
3208                                 struct voicecall *v = l->data;
3209                                 if (v->call->id == id)
3210                                         break;
3211                         }
3212
3213                         if (l == NULL)
3214                                 goto fail;
3215
3216                         if (voicecalls_have_held(vc))
3217                                 goto fail;
3218
3219                         vc->pending_em = em;
3220                         vc->pending_id = id;
3221
3222                         vc->driver->private_chat(vc, id,
3223                                         emulator_mpty_private_chat_cb, vc);
3224                         return;
3225                 }
3226
3227                 goto fail;
3228
3229         case OFONO_EMULATOR_REQUEST_TYPE_SUPPORT:
3230                 memcpy(buf, "+CHLD: (", 8);
3231                 info = buf + 8;
3232
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")
3241
3242                 *info++ = ')';
3243                 *info++ = '\0';
3244
3245                 ofono_emulator_send_info(em, buf, TRUE);
3246                 result.type = OFONO_ERROR_TYPE_NO_ERROR;
3247                 break;
3248
3249         case OFONO_EMULATOR_REQUEST_TYPE_QUERY:
3250         case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
3251 fail:
3252                 result.type = OFONO_ERROR_TYPE_FAILURE;
3253         }
3254
3255         ofono_emulator_send_final(em, &result);
3256 }
3257
3258 static void vts_tone_cb(int error, void *data)
3259 {
3260         struct ofono_emulator *em = data;
3261         struct ofono_error result;
3262
3263         result.error = 0;
3264         result.type = error ? OFONO_ERROR_TYPE_FAILURE :
3265                                                 OFONO_ERROR_TYPE_NO_ERROR;
3266
3267         ofono_emulator_send_final(em, &result);
3268 }
3269
3270 static void emulator_vts_cb(struct ofono_emulator *em,
3271                         struct ofono_emulator_request *req, void *userdata)
3272 {
3273         struct ofono_voicecall *vc = userdata;
3274         struct ofono_error result;
3275         const char *str;
3276
3277         switch (ofono_emulator_request_get_type(req)) {
3278         case OFONO_EMULATOR_REQUEST_TYPE_SET:
3279                 str = ofono_emulator_request_get_raw(req);
3280                 if (str == NULL)
3281                         break;
3282
3283                 if (!g_ascii_isdigit(str[0]) && str[0] != '*' &&
3284                                 str[0] != '#' && (str[0] < 'A' || str[0] > 'D'))
3285                         break;
3286
3287                 if (str[1] != '\0')
3288                         break;
3289
3290                 if (__ofono_voicecall_tone_send(vc, str, vts_tone_cb, em) >= 0)
3291                         return;
3292
3293                 break;
3294
3295         default:
3296                 break;
3297         }
3298
3299         result.error = 0;
3300         result.type = OFONO_ERROR_TYPE_FAILURE;
3301
3302         ofono_emulator_send_final(em, &result);
3303 }
3304
3305 static void emulator_dial_callback(const struct ofono_error *error, void *data)
3306 {
3307         struct ofono_voicecall *vc = data;
3308         gboolean need_to_emit;
3309         struct voicecall *v;
3310         const char *number;
3311         GError *err = NULL;
3312
3313         number = g_key_file_get_string(vc->settings, SETTINGS_GROUP,
3314                                         "Number", &err);
3315
3316         v = dial_handle_result(vc, error, number, &need_to_emit);
3317
3318         if (v == NULL) {
3319                 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
3320
3321                 if (is_emergency_number(vc, number) == TRUE)
3322                         __ofono_modem_dec_emergency_mode(modem);
3323         }
3324
3325         if (vc->pending_em)
3326                 ofono_emulator_send_final(vc->pending_em, error);
3327
3328         vc->pending_em = NULL;
3329
3330         if (need_to_emit)
3331                 voicecalls_emit_call_added(vc, v);
3332 }
3333
3334 static void emulator_dial(struct ofono_emulator *em, struct ofono_voicecall *vc,
3335                                 const char *number)
3336 {
3337         struct ofono_error result;
3338         int err;
3339
3340         result.error = 0;
3341
3342         if (vc->pending || vc->dial_req || vc->pending_em) {
3343                 result.type = OFONO_ERROR_TYPE_FAILURE;
3344                 goto send;
3345         }
3346
3347         vc->pending_em = em;
3348
3349         err = voicecall_dial(vc, number, OFONO_CLIR_OPTION_DEFAULT,
3350                                         emulator_dial_callback, vc);
3351
3352         if (err >= 0)
3353                 return;
3354
3355         vc->pending_em = NULL;
3356
3357         switch (err) {
3358         case -ENETDOWN:
3359                 result.error = 30;
3360                 result.type = OFONO_ERROR_TYPE_CME;
3361                 break;
3362
3363         default:
3364                 result.type = OFONO_ERROR_TYPE_FAILURE;
3365         }
3366
3367 send:
3368         ofono_emulator_send_final(em, &result);
3369 }
3370
3371 static void emulator_atd_cb(struct ofono_emulator *em,
3372                         struct ofono_emulator_request *req, void *userdata)
3373 {
3374         struct ofono_voicecall *vc = userdata;
3375         struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
3376         const char *str;
3377         size_t len;
3378         char number[OFONO_MAX_PHONE_NUMBER_LENGTH + 1];
3379         struct ofono_error result;
3380
3381         switch (ofono_emulator_request_get_type(req)) {
3382         case OFONO_EMULATOR_REQUEST_TYPE_SET:
3383                 str = ofono_emulator_request_get_raw(req);
3384
3385                 if (str == NULL || str[0] == '\0')
3386                         goto fail;
3387
3388                 len = strlen(str);
3389
3390                 if (len > OFONO_MAX_PHONE_NUMBER_LENGTH + 1 ||
3391                                 str[len - 1] != ';')
3392                         goto fail;
3393
3394                 if (len == 3 && str[0] == '>' && str[1] == '1') {
3395                         struct ofono_message_waiting *mw;
3396                         const struct ofono_phone_number *ph;
3397                         const char *num;
3398
3399                         mw = __ofono_atom_find(OFONO_ATOM_TYPE_MESSAGE_WAITING,
3400                                                 modem);
3401                         if (mw == NULL)
3402                                 goto fail;
3403
3404                         ph = __ofono_message_waiting_get_mbdn(mw, 0);
3405
3406                         if (ph == NULL)
3407                                 goto fail;
3408
3409                         num = phone_number_to_string(ph);
3410
3411                         emulator_dial(em, vc, num);
3412                 } else {
3413                         strncpy(number, str, len - 1);
3414                         number[len - 1] = '\0';
3415
3416                         emulator_dial(em, vc, number);
3417                 }
3418
3419                 break;
3420
3421         default:
3422 fail:
3423                 result.error = 0;
3424                 result.type = OFONO_ERROR_TYPE_FAILURE;
3425                 ofono_emulator_send_final(em, &result);
3426         };
3427 }
3428
3429 static void emulator_bldn_cb(struct ofono_emulator *em,
3430                         struct ofono_emulator_request *req, void *userdata)
3431 {
3432         struct ofono_voicecall *vc = userdata;
3433         const char *number;
3434         struct ofono_error result;
3435         GError *error = NULL;
3436
3437         switch (ofono_emulator_request_get_type(req)) {
3438         case OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY:
3439                 if (vc->settings == NULL)
3440                         goto fail;
3441
3442                 number = g_key_file_get_string(vc->settings, SETTINGS_GROUP,
3443                                                 "Number", &error);
3444                 if (number == NULL || number[0] == '\0')
3445                         goto fail;
3446
3447                 emulator_dial(em, vc, number);
3448                 break;
3449
3450         default:
3451 fail:
3452                 result.error = 0;
3453                 result.type = OFONO_ERROR_TYPE_FAILURE;
3454                 ofono_emulator_send_final(em, &result);
3455         };
3456 }
3457
3458 static void emulator_hfp_watch(struct ofono_atom *atom,
3459                                 enum ofono_atom_watch_condition cond,
3460                                 void *data)
3461 {
3462         struct ofono_emulator *em = __ofono_atom_get_data(atom);
3463         struct ofono_voicecall *vc = data;
3464
3465         switch (cond) {
3466         case OFONO_ATOM_WATCH_CONDITION_UNREGISTERED:
3467                 if (vc->pending_em == em)
3468                         vc->pending_em = NULL;
3469
3470                 return;
3471         case OFONO_ATOM_WATCH_CONDITION_REGISTERED:
3472                 break;
3473         }
3474
3475         notify_emulator_call_status(vc);
3476
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);
3484 }
3485
3486 void ofono_voicecall_register(struct ofono_voicecall *vc)
3487 {
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);
3491
3492         if (!g_dbus_register_interface(conn, path,
3493                                         OFONO_VOICECALL_MANAGER_INTERFACE,
3494                                         manager_methods, manager_signals, NULL,
3495                                         vc, NULL)) {
3496                 ofono_error("Could not create %s interface",
3497                                 OFONO_VOICECALL_MANAGER_INTERFACE);
3498
3499                 return;
3500         }
3501
3502         ofono_modem_add_interface(modem, OFONO_VOICECALL_MANAGER_INTERFACE);
3503
3504         vc->en_list = g_hash_table_new_full(g_str_hash, g_str_equal,
3505                                                         g_free, NULL);
3506
3507         /*
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
3510          */
3511         add_to_en_list(vc, (char **) default_en_list_no_sim);
3512         add_to_en_list(vc, (char **) default_en_list);
3513
3514         vc->sim_watch = __ofono_modem_add_atom_watch(modem,
3515                                                 OFONO_ATOM_TYPE_SIM,
3516                                                 sim_watch, vc, NULL);
3517
3518         __ofono_atom_register(vc->atom, voicecall_unregister);
3519
3520         vc->hfp_watch = __ofono_modem_add_atom_watch(modem,
3521                                         OFONO_ATOM_TYPE_EMULATOR_HFP,
3522                                         emulator_hfp_watch, vc, NULL);
3523 }
3524
3525 void ofono_voicecall_remove(struct ofono_voicecall *vc)
3526 {
3527         __ofono_atom_free(vc->atom);
3528 }
3529
3530 void ofono_voicecall_set_data(struct ofono_voicecall *vc, void *data)
3531 {
3532         vc->driver_data = data;
3533 }
3534
3535 void *ofono_voicecall_get_data(struct ofono_voicecall *vc)
3536 {
3537         return vc->driver_data;
3538 }
3539
3540 int ofono_voicecall_get_next_callid(struct ofono_voicecall *vc)
3541 {
3542         struct ofono_modem *modem;
3543         if (vc == NULL || vc->atom == NULL)
3544                 return 0;
3545
3546         modem = __ofono_atom_get_modem(vc->atom);
3547
3548         return __ofono_modem_callid_next(modem);
3549 }
3550
3551 ofono_bool_t __ofono_voicecall_is_busy(struct ofono_voicecall *vc,
3552                                         enum ofono_voicecall_interaction type)
3553 {
3554         if (vc->pending || vc->dial_req || vc->pending_em)
3555                 return TRUE;
3556
3557         switch (type) {
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))
3563                         return FALSE;
3564
3565                 return TRUE;
3566         case OFONO_VOICECALL_INTERACTION_PUT_ON_HOLD:
3567                 if (voicecalls_num_active(vc) == g_slist_length(vc->call_list))
3568                         return FALSE;
3569
3570                 if (voicecalls_num_held(vc) == g_slist_length(vc->call_list))
3571                         return FALSE;
3572
3573                 return TRUE;
3574         }
3575
3576         return TRUE;
3577 }
3578
3579 static void dial_request_cb(const struct ofono_error *error, void *data)
3580 {
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;
3585
3586         v = dial_handle_result(vc, error, number, &need_to_emit);
3587
3588         if (v == NULL) {
3589                 if (is_emergency_number(vc, number) == TRUE) {
3590                         struct ofono_modem *modem =
3591                                 __ofono_atom_get_modem(vc->atom);
3592
3593                         __ofono_modem_dec_emergency_mode(modem);
3594                 }
3595
3596                 dial_request_finish(vc);
3597                 return;
3598         }
3599
3600         v->message = vc->dial_req->message;
3601         v->icon_id = vc->dial_req->icon_id;
3602
3603         vc->dial_req->message = NULL;
3604         vc->dial_req->call = v;
3605
3606         /*
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)
3610          */
3611         v->untracked = TRUE;
3612
3613         if (v->call->status == CALL_STATUS_ACTIVE)
3614                 dial_request_finish(vc);
3615
3616         if (need_to_emit)
3617                 voicecalls_emit_call_added(vc, v);
3618 }
3619
3620 static void dial_request(struct ofono_voicecall *vc)
3621 {
3622         const char *number = phone_number_to_string(&vc->dial_req->ph);
3623
3624         if (is_emergency_number(vc, number) == TRUE) {
3625                 struct ofono_modem *modem = __ofono_atom_get_modem(vc->atom);
3626
3627                 __ofono_modem_inc_emergency_mode(modem);
3628         }
3629
3630         vc->driver->dial(vc, &vc->dial_req->ph, OFONO_CLIR_OPTION_DEFAULT,
3631                                 dial_request_cb, vc);
3632 }
3633
3634 static void dial_req_disconnect_cb(const struct ofono_error *error, void *data)
3635 {
3636         struct ofono_voicecall *vc = data;
3637
3638         if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
3639                 dial_request_finish(vc);
3640                 return;
3641         }
3642
3643         /*
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.
3647          */
3648         dial_request(vc);
3649 }
3650
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)
3656 {
3657         struct dial_request *req;
3658
3659         if (!valid_phone_number_format(addr))
3660                 return -EINVAL;
3661
3662         if (vc->driver->dial == NULL)
3663                 return -ENOSYS;
3664
3665         if (interaction == OFONO_VOICECALL_INTERACTION_DISCONNECT &&
3666                         vc->driver->release_all_active == NULL)
3667                 return -ENOSYS;
3668
3669         if (__ofono_voicecall_is_busy(vc, interaction) == TRUE)
3670                 return -EBUSY;
3671
3672         /*
3673          * TODO: if addr starts with "112", possibly translate into the
3674          * technology-specific emergency number.
3675          */
3676
3677         req = g_try_new0(struct dial_request, 1);
3678         if (req == NULL)
3679                 return -ENOMEM;
3680
3681         req->message = g_strdup(message);
3682         req->icon_id = icon_id;
3683         req->interaction = interaction;
3684         req->cb = cb;
3685         req->user_data = user_data;
3686
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);
3690
3691         vc->dial_req = req;
3692
3693         switch (interaction) {
3694         case OFONO_VOICECALL_INTERACTION_NONE:
3695                 dial_request(vc);
3696                 break;
3697
3698         case OFONO_VOICECALL_INTERACTION_PUT_ON_HOLD:
3699                 /* Note: dialling automatically puts active calls on hold */
3700                 dial_request(vc);
3701                 break;
3702
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);
3707                 else
3708                         dial_request(vc);
3709
3710                 break;
3711         }
3712
3713         return 0;
3714 }
3715
3716 void __ofono_voicecall_dial_cancel(struct ofono_voicecall *vc)
3717 {
3718         if (vc->dial_req == NULL || vc->dial_req->cb == NULL)
3719                 return;
3720
3721         vc->dial_req->cb = NULL;
3722 }
3723
3724 static void tone_request_cb(const struct ofono_error *error, void *data)
3725 {
3726         struct ofono_voicecall *vc = data;
3727         struct tone_queue_entry *entry = g_queue_peek_head(vc->toneq);
3728         int len = 0;
3729
3730         if (entry == NULL)
3731                 return;
3732
3733         /*
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.
3737          */
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));
3742
3743                 tone_request_finish(vc, entry, EIO, TRUE);
3744
3745                 goto done;
3746         }
3747
3748         if (*entry->left == '\0') {
3749                 tone_request_finish(vc, entry, 0, TRUE);
3750
3751                 goto done;
3752         }
3753
3754         len = strspn(entry->left, "pP");
3755         entry->left += len;
3756
3757 done:
3758         /*
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.
3763          */
3764         vc->tone_source = g_timeout_add_seconds(len * 3, tone_request_run, vc);
3765 }
3766
3767 static gboolean tone_request_run(gpointer user_data)
3768 {
3769         struct ofono_voicecall *vc = user_data;
3770         struct tone_queue_entry *entry = g_queue_peek_head(vc->toneq);
3771         char final;
3772         unsigned len;
3773
3774         vc->tone_source = 0;
3775
3776         if (entry == NULL)
3777                 return FALSE;
3778
3779         len = strcspn(entry->left, "pP");
3780
3781         if (len) {
3782                 if (len > 8) /* Arbitrary length limit per request */
3783                         len = 8;
3784
3785                 /* Temporarily move the end of the string */
3786                 final = entry->left[len];
3787                 entry->left[len] = '\0';
3788
3789                 vc->driver->send_tones(vc, entry->left, tone_request_cb, vc);
3790
3791                 entry->left += len;
3792                 entry->left[0] = final;
3793         } else
3794                 tone_request_cb(NULL, vc);
3795
3796         return FALSE;
3797 }
3798
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)
3802 {
3803         if (vc->driver->send_tones == NULL)
3804                 return -ENOSYS;
3805
3806         /* Send DTMFs only if we have at least one connected call */
3807         if (!voicecalls_can_dtmf(vc))
3808                 return -ENOENT;
3809
3810         return tone_queue(vc, tone_str, cb, user_data, NULL);
3811 }
3812
3813 void __ofono_voicecall_tone_cancel(struct ofono_voicecall *vc, int id)
3814 {
3815         struct tone_queue_entry *entry;
3816         int n = 0;
3817
3818         while ((entry = g_queue_peek_nth(vc->toneq, n++)) != NULL)
3819                 if (entry->id == id)
3820                         break;
3821
3822         tone_request_finish(vc, entry, 0, FALSE);
3823
3824         /*
3825          * If we were in the middle of a PAUSE, wake queue up
3826          * now, else wake up when current tone finishes.
3827          */
3828         if (n == 1 && vc->tone_source) {
3829                 g_source_remove(vc->tone_source);
3830                 tone_request_run(vc);
3831         }
3832 }
3833
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)
3838 {
3839         struct dial_request *req;
3840
3841         req = g_new0(struct dial_request, 1);
3842
3843         req->message = g_strdup(message);
3844         req->icon_id = icon_id;
3845
3846         req->ph.type = addr_type;
3847         strncpy(req->ph.number, addr, OFONO_MAX_PHONE_NUMBER_LENGTH);
3848
3849         vc->dial_req = req;
3850
3851         vc->flags |= VOICECALL_FLAG_STK_MODEM_CALLSETUP;
3852 }
3853
3854 void __ofono_voicecall_clear_alpha_and_icon_id(struct ofono_voicecall *vc)
3855 {
3856         g_free(vc->dial_req->message);
3857         vc->dial_req->message = NULL;
3858
3859         g_free(vc->dial_req);
3860         vc->dial_req = NULL;
3861
3862         vc->flags &= ~VOICECALL_FLAG_STK_MODEM_CALLSETUP;
3863 }
3864
3865 static void ssn_mt_forwarded_notify(struct ofono_voicecall *vc,
3866                                         unsigned int id, int code,
3867                                         const struct ofono_phone_number *ph)
3868 {
3869         DBusConnection *conn = ofono_dbus_get_connection();
3870         const char *path = __ofono_atom_get_path(vc->atom);
3871         char *info = "incoming";
3872
3873         g_dbus_emit_signal(conn, path, OFONO_VOICECALL_MANAGER_INTERFACE,
3874                                 "Forwarded",
3875                                 DBUS_TYPE_STRING, &info,
3876                                 DBUS_TYPE_INVALID);
3877 }
3878
3879 static struct voicecall *voicecall_select(struct ofono_voicecall *vc,
3880                                                 unsigned int id)
3881 {
3882         if (id != 0) {
3883                 GSList *l = g_slist_find_custom(vc->call_list,
3884                                                 GUINT_TO_POINTER(id),
3885                                                 call_compare_by_id);
3886
3887                 if (l == NULL)
3888                         return NULL;
3889
3890                 return l->data;
3891         }
3892
3893         if (g_slist_length(vc->call_list) == 1)
3894                 return vc->call_list->data;
3895
3896         return NULL;
3897 }
3898
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)
3902 {
3903         struct voicecall *v = voicecall_select(vc, id);
3904         DBusConnection *conn = ofono_dbus_get_connection();
3905         const char *path;
3906
3907         if (v == NULL)
3908                 return;
3909
3910         if (v->remote_held == held)
3911                 return;
3912
3913         v->remote_held = held;
3914         path = voicecall_build_path(vc, v->call);
3915
3916         ofono_dbus_signal_property_changed(conn, path,
3917                                                 OFONO_VOICECALL_INTERFACE,
3918                                                 "RemoteHeld", DBUS_TYPE_BOOLEAN,
3919                                                 &v->remote_held);
3920 }
3921
3922 static void ssn_mt_remote_multiparty_notify(struct ofono_voicecall *vc,
3923                                         unsigned int id,
3924                                         const struct ofono_phone_number *ph)
3925 {
3926         struct voicecall *v = voicecall_select(vc, id);
3927         DBusConnection *conn = ofono_dbus_get_connection();
3928         const char *path;
3929
3930         if (v == NULL)
3931                 return;
3932
3933         if (v->remote_multiparty == TRUE)
3934                 return;
3935
3936         v->remote_multiparty = TRUE;
3937
3938         path = voicecall_build_path(vc, v->call);
3939
3940         ofono_dbus_signal_property_changed(conn, path,
3941                                         OFONO_VOICECALL_INTERFACE,
3942                                         "RemoteMultiparty", DBUS_TYPE_BOOLEAN,
3943                                         &v->remote_multiparty);
3944 }
3945
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)
3949 {
3950         switch (code) {
3951         case SS_MT_CALL_FORWARDED:
3952                 ssn_mt_forwarded_notify(vc, id, code, ph);
3953                 break;
3954         case SS_MT_VOICECALL_ON_HOLD:
3955                 ssn_mt_remote_held_notify(vc, id, TRUE, ph);
3956                 break;
3957         case SS_MT_VOICECALL_RETRIEVED:
3958                 ssn_mt_remote_held_notify(vc, id, FALSE, ph);
3959                 break;
3960         case SS_MT_MULTIPARTY_VOICECALL:
3961                 ssn_mt_remote_multiparty_notify(vc, id, ph);
3962                 break;
3963         }
3964 }
3965
3966 static void ssn_mo_call_barred_notify(struct ofono_voicecall *vc,
3967                                         unsigned int id, int code)
3968 {
3969         DBusConnection *conn = ofono_dbus_get_connection();
3970         const char *path = __ofono_atom_get_path(vc->atom);
3971         const char *info;
3972
3973         if (code == SS_MO_INCOMING_BARRING)
3974                 info = "remote";
3975         else
3976                 info = "local";
3977
3978         g_dbus_emit_signal(conn, path, OFONO_VOICECALL_MANAGER_INTERFACE,
3979                                 "BarringActive",
3980                                 DBUS_TYPE_STRING, &info,
3981                                 DBUS_TYPE_INVALID);
3982 }
3983
3984 static void ssn_mo_forwarded_notify(struct ofono_voicecall *vc,
3985                                         unsigned int id, int code)
3986 {
3987         DBusConnection *conn = ofono_dbus_get_connection();
3988         const char *path = __ofono_atom_get_path(vc->atom);
3989         char *info = "outgoing";
3990
3991         g_dbus_emit_signal(conn, path, OFONO_VOICECALL_MANAGER_INTERFACE,
3992                                 "Forwarded",
3993                                 DBUS_TYPE_STRING, &info,
3994                                 DBUS_TYPE_INVALID);
3995 }
3996
3997 void ofono_voicecall_ssn_mo_notify(struct ofono_voicecall *vc,
3998                                         unsigned int id, int code, int index)
3999 {
4000         switch (code) {
4001         case SS_MO_OUTGOING_BARRING:
4002         case SS_MO_INCOMING_BARRING:
4003                 ssn_mo_call_barred_notify(vc, id, code);
4004                 break;
4005         case SS_MO_CALL_FORWARDED:
4006                 ssn_mo_forwarded_notify(vc, id, code);
4007                 break;
4008         }
4009 }