upgrade to the version 1.8
[profile/ivi/ofono.git] / src / emulator.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 <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include <glib.h>
31
32 #include "ofono.h"
33 #include "common.h"
34 #include "gatserver.h"
35 #include "gatppp.h"
36
37 #define RING_TIMEOUT 3
38
39 struct ofono_emulator {
40         struct ofono_atom *atom;
41         enum ofono_emulator_type type;
42         GAtServer *server;
43         GAtPPP *ppp;
44         gboolean slc;
45         int l_features;
46         int r_features;
47         int events_mode;
48         gboolean events_ind;
49         unsigned char cmee_mode;
50         GSList *indicators;
51         guint callsetup_source;
52         gboolean clip;
53         gboolean ccwa;
54         int pns_id;
55 };
56
57 struct indicator {
58         char *name;
59         int value;
60         int min;
61         int max;
62         gboolean deferred;
63         gboolean active;
64         gboolean mandatory;
65 };
66
67 static void emulator_debug(const char *str, void *data)
68 {
69         ofono_info("%s: %s\n", (char *)data, str);
70 }
71
72 static void emulator_disconnect(gpointer user_data)
73 {
74         struct ofono_emulator *em = user_data;
75
76         DBG("%p", em);
77
78         ofono_emulator_remove(em);
79 }
80
81 static void ppp_connect(const char *iface, const char *local,
82                         const char *remote,
83                         const char *dns1, const char *dns2,
84                         gpointer user_data)
85 {
86         DBG("Network Device: %s\n", iface);
87         DBG("IP Address: %s\n", local);
88         DBG("Remote IP Address: %s\n", remote);
89         DBG("Primary DNS Server: %s\n", dns1);
90         DBG("Secondary DNS Server: %s\n", dns2);
91 }
92
93 static void cleanup_ppp(struct ofono_emulator *em)
94 {
95         DBG("");
96
97         g_at_ppp_unref(em->ppp);
98         em->ppp = NULL;
99
100         __ofono_private_network_release(em->pns_id);
101         em->pns_id = 0;
102
103         g_at_server_resume(em->server);
104         g_at_server_send_final(em->server, G_AT_SERVER_RESULT_NO_CARRIER);
105 }
106
107 static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
108 {
109         struct ofono_emulator *em = user_data;
110
111         cleanup_ppp(em);
112 }
113
114 static void ppp_suspend(gpointer user_data)
115 {
116         struct ofono_emulator *em = user_data;
117
118         DBG("");
119
120         g_at_server_resume(em->server);
121 }
122
123 static void suspend_server(gpointer user_data)
124 {
125         struct ofono_emulator *em = user_data;
126         GAtIO *io = g_at_server_get_io(em->server);
127
128         g_at_server_suspend(em->server);
129
130         if (g_at_ppp_listen(em->ppp, io) == FALSE)
131                 cleanup_ppp(em);
132 }
133
134 static void request_private_network_cb(
135                         const struct ofono_private_network_settings *pns,
136                         void *data)
137 {
138         struct ofono_emulator *em = data;
139         GAtIO *io = g_at_server_get_io(em->server);
140
141         if (pns == NULL)
142                 goto error;
143
144         em->ppp = g_at_ppp_server_new_full(pns->server_ip, pns->fd);
145         if (em->ppp == NULL) {
146                 close(pns->fd);
147                 goto badalloc;
148         }
149
150         g_at_ppp_set_server_info(em->ppp, pns->peer_ip,
151                                         pns->primary_dns, pns->secondary_dns);
152
153         g_at_ppp_set_acfc_enabled(em->ppp, TRUE);
154         g_at_ppp_set_pfc_enabled(em->ppp, TRUE);
155
156         g_at_ppp_set_credentials(em->ppp, "", "");
157         g_at_ppp_set_debug(em->ppp, emulator_debug, "PPP");
158
159         g_at_ppp_set_connect_function(em->ppp, ppp_connect, em);
160         g_at_ppp_set_disconnect_function(em->ppp, ppp_disconnect, em);
161         g_at_ppp_set_suspend_function(em->ppp, ppp_suspend, em);
162
163         g_at_server_send_intermediate(em->server, "CONNECT");
164         g_at_io_set_write_done(io, suspend_server, em);
165
166         return;
167
168 badalloc:
169         __ofono_private_network_release(em->pns_id);
170
171 error:
172         em->pns_id = 0;
173         g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
174 }
175
176 static gboolean dial_call(struct ofono_emulator *em, const char *dial_str)
177 {
178         char c = *dial_str;
179
180         DBG("dial call %s", dial_str);
181
182         if (c == '*' || c == '#' || c == 'T' || c == 't') {
183                 if (__ofono_private_network_request(request_private_network_cb,
184                                                 &em->pns_id, em) == FALSE)
185                         return FALSE;
186         }
187
188         return TRUE;
189 }
190
191 static void dial_cb(GAtServer *server, GAtServerRequestType type,
192                                 GAtResult *result, gpointer user_data)
193 {
194         struct ofono_emulator *em = user_data;
195         GAtResultIter iter;
196         const char *dial_str;
197
198         DBG("");
199
200         if (type != G_AT_SERVER_REQUEST_TYPE_SET)
201                 goto error;
202
203         g_at_result_iter_init(&iter, result);
204
205         if (!g_at_result_iter_next(&iter, ""))
206                 goto error;
207
208         dial_str = g_at_result_iter_raw_line(&iter);
209         if (!dial_str)
210                 goto error;
211
212         if (em->ppp)
213                 goto error;
214
215         if (!dial_call(em, dial_str))
216                 goto error;
217
218         return;
219
220 error:
221         g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
222 }
223
224 static void dun_ath_cb(GAtServer *server, GAtServerRequestType type,
225                         GAtResult *result, gpointer user_data)
226 {
227         struct ofono_emulator *em = user_data;
228         GAtResultIter iter;
229         int val;
230
231         DBG("");
232
233         switch (type) {
234         case G_AT_SERVER_REQUEST_TYPE_SET:
235                 g_at_result_iter_init(&iter, result);
236                 g_at_result_iter_next(&iter, "");
237
238                 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
239                         goto error;
240
241                 if (val != 0)
242                         goto error;
243
244                 /* Fall through */
245
246         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
247                 if (em->ppp == NULL)
248                         goto error;
249
250                 g_at_ppp_unref(em->ppp);
251                 em->ppp = NULL;
252
253                 __ofono_private_network_release(em->pns_id);
254                 em->pns_id = 0;
255
256                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
257                 break;
258
259         default:
260 error:
261                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
262                 break;
263         }
264 }
265
266 static void resume_ppp(gpointer user_data)
267 {
268         struct ofono_emulator *em = user_data;
269
270         g_at_server_suspend(em->server);
271         g_at_ppp_resume(em->ppp);
272 }
273
274 static void dun_ato_cb(GAtServer *server, GAtServerRequestType type,
275                         GAtResult *result, gpointer user_data)
276 {
277         struct ofono_emulator *em = user_data;
278         GAtIO *io = g_at_server_get_io(em->server);
279         GAtResultIter iter;
280         int val;
281
282         DBG("");
283
284         switch (type) {
285         case G_AT_SERVER_REQUEST_TYPE_SET:
286                 g_at_result_iter_init(&iter, result);
287                 g_at_result_iter_next(&iter, "");
288
289                 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
290                         goto error;
291
292                 if (val != 0)
293                         goto error;
294
295                 /* Fall through */
296         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
297                 if (em->ppp == NULL)
298                         goto error;
299
300                 g_at_server_send_intermediate(em->server, "CONNECT");
301                 g_at_io_set_write_done(io, resume_ppp, em);
302                 break;
303
304         default:
305 error:
306                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
307                 break;
308         }
309 }
310
311 static struct indicator *find_indicator(struct ofono_emulator *em,
312                                                 const char *name, int *index)
313 {
314         GSList *l;
315         int i;
316
317         for (i = 1, l = em->indicators; l; l = l->next, i++) {
318                 struct indicator *ind = l->data;
319
320                 if (g_str_equal(ind->name, name) == FALSE)
321                         continue;
322
323                 if (index)
324                         *index = i;
325
326                 return ind;
327         }
328
329         return NULL;
330 }
331
332 static struct ofono_call *find_call_with_status(struct ofono_emulator *em,
333                                                                 int status)
334 {
335         struct ofono_modem *modem = __ofono_atom_get_modem(em->atom);
336         struct ofono_voicecall *vc;
337
338         vc = __ofono_atom_find(OFONO_ATOM_TYPE_VOICECALL, modem);
339         if (vc == NULL)
340                 return NULL;
341
342         return __ofono_voicecall_find_call_with_status(vc, status);
343 }
344
345 static void notify_deferred_indicators(GAtServer *server, void *user_data)
346 {
347         struct ofono_emulator *em = user_data;
348         int i;
349         char buf[20];
350         GSList *l;
351         struct indicator *ind;
352
353         for (i = 1, l = em->indicators; l; l = l->next, i++) {
354                 ind = l->data;
355
356                 if (!ind->deferred)
357                         continue;
358
359                 if (em->events_mode == 3 && em->events_ind && em->slc &&
360                                 ind->active) {
361                         sprintf(buf, "+CIEV: %d,%d", i, ind->value);
362                         g_at_server_send_unsolicited(em->server, buf);
363                 }
364
365                 ind->deferred = FALSE;
366         }
367 }
368
369 static gboolean notify_ccwa(void *user_data)
370 {
371         struct ofono_emulator *em = user_data;
372         struct ofono_call *c;
373         const char *phone;
374         /*
375          * '+CCWA: "+",' + phone number + phone type on 3 digits max
376          * + terminating null
377          */
378         char str[OFONO_MAX_PHONE_NUMBER_LENGTH + 14 + 1];
379
380         if ((em->type == OFONO_EMULATOR_TYPE_HFP && em->slc == FALSE) ||
381                         !em->ccwa)
382                 goto end;
383
384         c = find_call_with_status(em, CALL_STATUS_WAITING);
385
386         if (c && c->clip_validity == CLIP_VALIDITY_VALID) {
387                 phone = phone_number_to_string(&c->phone_number);
388                 sprintf(str, "+CCWA: \"%s\",%d", phone, c->phone_number.type);
389
390                 g_at_server_send_unsolicited(em->server, str);
391         } else
392                 g_at_server_send_unsolicited(em->server, "+CCWA: \"\",128");
393
394 end:
395         em->callsetup_source = 0;
396
397         return FALSE;
398 }
399
400 static gboolean notify_ring(void *user_data)
401 {
402         struct ofono_emulator *em = user_data;
403         struct ofono_call *c;
404         const char *phone;
405         /*
406          * '+CLIP: "+",' + phone number + phone type on 3 digits max
407          * + terminating null
408          */
409         char str[OFONO_MAX_PHONE_NUMBER_LENGTH + 14 + 1];
410
411         if (em->type == OFONO_EMULATOR_TYPE_HFP && em->slc == FALSE)
412                 return TRUE;
413
414         g_at_server_send_unsolicited(em->server, "RING");
415
416         if (!em->clip)
417                 return TRUE;
418
419         c = find_call_with_status(em, CALL_STATUS_INCOMING);
420
421         if (c == NULL)
422                 return TRUE;
423
424         switch (c->clip_validity) {
425         case CLIP_VALIDITY_VALID:
426                 phone = phone_number_to_string(&c->phone_number);
427                 sprintf(str, "+CLIP: \"%s\",%d", phone, c->phone_number.type);
428                 g_at_server_send_unsolicited(em->server, str);
429                 break;
430
431         case CLIP_VALIDITY_WITHHELD:
432                 g_at_server_send_unsolicited(em->server, "+CLIP: \"\",128");
433                 break;
434         }
435
436         return TRUE;
437 }
438
439 static void brsf_cb(GAtServer *server, GAtServerRequestType type,
440                         GAtResult *result, gpointer user_data)
441 {
442         struct ofono_emulator *em = user_data;
443         GAtResultIter iter;
444         int val;
445         char buf[16];
446
447         switch (type) {
448         case G_AT_SERVER_REQUEST_TYPE_SET:
449                 g_at_result_iter_init(&iter, result);
450                 g_at_result_iter_next(&iter, "");
451
452                 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
453                         goto fail;
454
455                 if (val < 0 || val > 127)
456                         goto fail;
457
458                 em->r_features = val;
459
460                 sprintf(buf, "+BRSF: %d", em->l_features);
461                 g_at_server_send_info(em->server, buf, TRUE);
462                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
463                 break;
464
465         default:
466 fail:
467                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
468                 break;
469         }
470 }
471
472 static void cind_cb(GAtServer *server, GAtServerRequestType type,
473                         GAtResult *result, gpointer user_data)
474 {
475         struct ofono_emulator *em = user_data;
476         GSList *l;
477         struct indicator *ind;
478         gsize size;
479         int len;
480         char *buf;
481         char *tmp;
482
483         switch (type) {
484         case G_AT_SERVER_REQUEST_TYPE_QUERY:
485                 /*
486                  * "+CIND: " + terminating null + number of indicators *
487                  * (max of 3 digits in the value + separator)
488                  */
489                 size = 7 + 1 + (g_slist_length(em->indicators) * 4);
490                 buf = g_try_malloc0(size);
491                 if (buf == NULL)
492                         goto fail;
493
494                 len = sprintf(buf, "+CIND: ");
495                 tmp = buf + len;
496
497                 for (l = em->indicators; l; l = l->next) {
498                         ind = l->data;
499                         len = sprintf(tmp, "%s%d",
500                                         l == em->indicators ? "" : ",",
501                                         ind->value);
502                         tmp = tmp + len;
503                 }
504
505                 g_at_server_send_info(em->server, buf, TRUE);
506                 g_free(buf);
507                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
508                 break;
509
510         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
511                 /*
512                  * '+CIND: ' + terminating null + number of indicators *
513                  * ( indicator name + '("",(000,000))' + separator)
514                  */
515                 size = 8;
516
517                 for (l = em->indicators; l; l = l->next) {
518                         ind = l->data;
519                         size += strlen(ind->name) + 15;
520                 }
521
522                 buf = g_try_malloc0(size);
523                 if (buf == NULL)
524                         goto fail;
525
526                 len = sprintf(buf, "+CIND: ");
527                 tmp = buf + len;
528
529                 for (l = em->indicators; l; l = l->next) {
530                         ind = l->data;
531                         len = sprintf(tmp, "%s(\"%s\",(%d%c%d))",
532                                         l == em->indicators ? "" : ",",
533                                         ind->name, ind->min,
534                                         (ind->max - ind->min) == 1 ? ',' : '-',
535                                         ind->max);
536                         tmp = tmp + len;
537                 }
538
539                 g_at_server_send_info(server, buf, TRUE);
540                 g_free(buf);
541                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
542                 break;
543
544         default:
545 fail:
546                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
547                 break;
548         }
549 }
550
551 static void cmer_cb(GAtServer *server, GAtServerRequestType type,
552                         GAtResult *result, gpointer user_data)
553 {
554         struct ofono_emulator *em = user_data;
555         char buf[32];
556
557         switch (type) {
558         case G_AT_SERVER_REQUEST_TYPE_QUERY:
559                 sprintf(buf, "+CMER: %d,0,0,%d,0", em->events_mode,
560                                                 em->events_ind);
561                 g_at_server_send_info(em->server, buf, TRUE);
562                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
563                 break;
564
565         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
566                 sprintf(buf, "+CMER: (0,3),(0),(0),(0,1),(0)");
567                 g_at_server_send_info(em->server, buf, TRUE);
568                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
569                 break;
570
571         case G_AT_SERVER_REQUEST_TYPE_SET:
572         {
573                 GAtResultIter iter;
574                 int mode = em->events_mode;
575                 int ind = em->events_ind;
576                 int val;
577
578                 g_at_result_iter_init(&iter, result);
579                 g_at_result_iter_next(&iter, "");
580
581                 /* mode */
582                 if (!g_at_result_iter_next_number_default(&iter, mode, &mode))
583                         goto fail;
584
585                 if (mode != 0 && mode != 3)
586                         goto fail;
587
588                 /* keyp */
589                 if (!g_at_result_iter_next_number_default(&iter, 0, &val)) {
590                         if (!g_at_result_iter_skip_next(&iter))
591                                 goto done;
592                         goto fail;
593                 }
594
595                 if (val != 0)
596                         goto fail;
597
598                 /* disp */
599                 if (!g_at_result_iter_next_number_default(&iter, 0, &val)) {
600                         if (!g_at_result_iter_skip_next(&iter))
601                                 goto done;
602                         goto fail;
603                 }
604
605                 if (val != 0)
606                         goto fail;
607
608                 /* ind */
609                 if (!g_at_result_iter_next_number_default(&iter, ind, &ind)) {
610                         if (!g_at_result_iter_skip_next(&iter))
611                                 goto done;
612                         goto fail;
613                 }
614
615                 if (ind != 0 && ind != 1)
616                         goto fail;
617
618                 /* bfr */
619                 if (!g_at_result_iter_next_number_default(&iter, 0, &val)) {
620                         if (!g_at_result_iter_skip_next(&iter))
621                                 goto done;
622                         goto fail;
623                 }
624
625                 if (val != 0)
626                         goto fail;
627
628                 /* check that bfr is last parameter */
629                 if (g_at_result_iter_skip_next(&iter))
630                         goto fail;
631
632 done:
633                 em->events_mode = mode;
634                 em->events_ind = ind;
635
636                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
637
638                 em->slc = TRUE;
639                 break;
640         }
641
642         default:
643 fail:
644                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
645                 break;
646         }
647 }
648
649 static void clip_cb(GAtServer *server, GAtServerRequestType type,
650                         GAtResult *result, gpointer user_data)
651 {
652         struct ofono_emulator *em = user_data;
653         GAtResultIter iter;
654         int val;
655
656         if (em->slc == FALSE)
657                 goto fail;
658
659         switch (type) {
660         case G_AT_SERVER_REQUEST_TYPE_SET:
661                 g_at_result_iter_init(&iter, result);
662                 g_at_result_iter_next(&iter, "");
663
664                 if (!g_at_result_iter_next_number(&iter, &val))
665                         goto fail;
666
667                 if (val != 0 && val != 1)
668                         goto fail;
669
670                 /* check this is last parameter */
671                 if (g_at_result_iter_skip_next(&iter))
672                         goto fail;
673
674                 em->clip = val;
675
676                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
677                 break;
678
679         default:
680 fail:
681                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
682         };
683 }
684
685 static void ccwa_cb(GAtServer *server, GAtServerRequestType type,
686                         GAtResult *result, gpointer user_data)
687 {
688         struct ofono_emulator *em = user_data;
689         GAtResultIter iter;
690         int val;
691         struct indicator *call_ind;
692         struct indicator *cs_ind;
693
694         if (em->slc == FALSE)
695                 goto fail;
696
697         switch (type) {
698         case G_AT_SERVER_REQUEST_TYPE_SET:
699                 g_at_result_iter_init(&iter, result);
700                 g_at_result_iter_next(&iter, "");
701
702                 if (!g_at_result_iter_next_number(&iter, &val))
703                         goto fail;
704
705                 if (val != 0 && val != 1)
706                         goto fail;
707
708                 /* check this is last parameter */
709                 if (g_at_result_iter_skip_next(&iter))
710                         goto fail;
711
712                 call_ind = find_indicator(em, OFONO_EMULATOR_IND_CALL, NULL);
713                 cs_ind = find_indicator(em, OFONO_EMULATOR_IND_CALLSETUP, NULL);
714
715                 if (cs_ind->value == OFONO_EMULATOR_CALLSETUP_INCOMING &&
716                                 call_ind->value == OFONO_EMULATOR_CALL_ACTIVE &&
717                                 em->ccwa == FALSE && val == 1)
718                         em->callsetup_source = g_timeout_add_seconds(0,
719                                                         notify_ccwa, em);
720
721                 em->ccwa = val;
722
723                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
724                 break;
725
726         default:
727 fail:
728                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
729         };
730 }
731
732 static void cmee_cb(GAtServer *server, GAtServerRequestType type,
733                         GAtResult *result, gpointer user_data)
734 {
735         struct ofono_emulator *em = user_data;
736         GAtResultIter iter;
737         int val;
738         char buf[16];
739
740         switch (type) {
741         case G_AT_SERVER_REQUEST_TYPE_SET:
742                 g_at_result_iter_init(&iter, result);
743                 g_at_result_iter_next(&iter, "");
744
745                 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
746                         goto fail;
747
748                 if (val != 0 && val != 1)
749                         goto fail;
750
751                 em->cmee_mode = val;
752
753                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
754                 break;
755
756         case G_AT_SERVER_REQUEST_TYPE_QUERY:
757                 sprintf(buf, "+CMEE: %d", em->cmee_mode);
758                 g_at_server_send_info(em->server, buf, TRUE);
759                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
760                 break;
761
762         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
763                 /* HFP only support 0 and 1 */
764                 sprintf(buf, "+CMEE: (0,1)");
765                 g_at_server_send_info(em->server, buf, TRUE);
766                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
767                 break;
768
769         default:
770 fail:
771                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
772                 break;
773         }
774 }
775
776 static void bia_cb(GAtServer *server, GAtServerRequestType type,
777                         GAtResult *result, gpointer user_data)
778 {
779         struct ofono_emulator *em = user_data;
780
781         switch (type) {
782         case G_AT_SERVER_REQUEST_TYPE_SET:
783         {
784                 GAtResultIter iter;
785                 GSList *l;
786                 int val;
787
788                 g_at_result_iter_init(&iter, result);
789                 g_at_result_iter_next(&iter, "");
790
791                 /* check validity of the request */
792                 while (g_at_result_iter_next_number_default(&iter, 0, &val))
793                         if (val != 0 &&  val != 1)
794                                 goto fail;
795
796                 /* Check that we have no non-numbers in the stream */
797                 if (g_at_result_iter_skip_next(&iter) == TRUE)
798                         goto fail;
799
800                 /* request is valid, update the indicator activation status */
801                 g_at_result_iter_init(&iter, result);
802                 g_at_result_iter_next(&iter, "");
803
804                 for (l = em->indicators; l; l = l->next) {
805                         struct indicator *ind = l->data;
806
807                         if (g_at_result_iter_next_number_default(&iter,
808                                                 ind->active, &val) == FALSE)
809                                 break;
810
811                         if (ind->mandatory == TRUE)
812                                 continue;
813
814                         ind->active = val;
815                 }
816
817                 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
818                 break;
819         }
820
821         default:
822 fail:
823                 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
824                 break;
825         }
826 }
827
828 static void emulator_add_indicator(struct ofono_emulator *em, const char* name,
829                                         int min, int max, int dflt,
830                                         gboolean mandatory)
831 {
832         struct indicator *ind;
833
834         ind = g_try_new0(struct indicator, 1);
835         if (ind == NULL) {
836                 ofono_error("Unable to allocate indicator structure");
837                 return;
838         }
839
840         ind->name = g_strdup(name);
841         ind->min = min;
842         ind->max = max;
843         ind->value = dflt;
844         ind->active = TRUE;
845         ind->mandatory = mandatory;
846
847         em->indicators = g_slist_append(em->indicators, ind);
848 }
849
850 static void emulator_unregister(struct ofono_atom *atom)
851 {
852         struct ofono_emulator *em = __ofono_atom_get_data(atom);
853         GSList *l;
854
855         DBG("%p", em);
856
857         if (em->callsetup_source) {
858                 g_source_remove(em->callsetup_source);
859                 em->callsetup_source = 0;
860         }
861
862         for (l = em->indicators; l; l = l->next) {
863                 struct indicator *ind = l->data;
864
865                 g_free(ind->name);
866                 g_free(ind);
867         }
868
869         g_slist_free(em->indicators);
870         em->indicators = NULL;
871
872         g_at_ppp_unref(em->ppp);
873         em->ppp = NULL;
874
875         if (em->pns_id > 0) {
876                 __ofono_private_network_release(em->pns_id);
877                 em->pns_id = 0;
878         }
879
880         g_at_server_unref(em->server);
881         em->server = NULL;
882 }
883
884 void ofono_emulator_register(struct ofono_emulator *em, int fd)
885 {
886         GIOChannel *io;
887
888         DBG("%p, %d", em, fd);
889
890         if (fd < 0)
891                 return;
892
893         io = g_io_channel_unix_new(fd);
894
895         em->server = g_at_server_new(io);
896         if (em->server == NULL)
897                 return;
898
899         g_io_channel_unref(io);
900
901         g_at_server_set_debug(em->server, emulator_debug, "Server");
902         g_at_server_set_disconnect_function(em->server,
903                                                 emulator_disconnect, em);
904         g_at_server_set_finish_callback(em->server, notify_deferred_indicators,
905                                                 em);
906
907         if (em->type == OFONO_EMULATOR_TYPE_HFP) {
908                 emulator_add_indicator(em, OFONO_EMULATOR_IND_SERVICE, 0, 1, 0,
909                                                                         FALSE);
910                 emulator_add_indicator(em, OFONO_EMULATOR_IND_CALL, 0, 1, 0,
911                                                                         TRUE);
912                 emulator_add_indicator(em, OFONO_EMULATOR_IND_CALLSETUP, 0, 3,
913                                                                 0, TRUE);
914                 emulator_add_indicator(em, OFONO_EMULATOR_IND_CALLHELD, 0, 2,
915                                                                 0, TRUE);
916                 emulator_add_indicator(em, OFONO_EMULATOR_IND_SIGNAL, 0, 5, 0,
917                                                                         FALSE);
918                 emulator_add_indicator(em, OFONO_EMULATOR_IND_ROAMING, 0, 1, 0,
919                                                                         FALSE);
920                 emulator_add_indicator(em, OFONO_EMULATOR_IND_BATTERY, 0, 5, 5,
921                                                                         FALSE);
922
923                 g_at_server_register(em->server, "+BRSF", brsf_cb, em, NULL);
924                 g_at_server_register(em->server, "+CIND", cind_cb, em, NULL);
925                 g_at_server_register(em->server, "+CMER", cmer_cb, em, NULL);
926                 g_at_server_register(em->server, "+CLIP", clip_cb, em, NULL);
927                 g_at_server_register(em->server, "+CCWA", ccwa_cb, em, NULL);
928                 g_at_server_register(em->server, "+CMEE", cmee_cb, em, NULL);
929                 g_at_server_register(em->server, "+BIA", bia_cb, em, NULL);
930         }
931
932         __ofono_atom_register(em->atom, emulator_unregister);
933
934         switch (em->type) {
935         case OFONO_EMULATOR_TYPE_DUN:
936                 g_at_server_register(em->server, "D", dial_cb, em, NULL);
937                 g_at_server_register(em->server, "H", dun_ath_cb, em, NULL);
938                 g_at_server_register(em->server, "O", dun_ato_cb, em, NULL);
939                 break;
940         case OFONO_EMULATOR_TYPE_HFP:
941                 g_at_server_set_echo(em->server, FALSE);
942                 break;
943         default:
944                 break;
945         }
946 }
947
948 static void emulator_remove(struct ofono_atom *atom)
949 {
950         struct ofono_emulator *em = __ofono_atom_get_data(atom);
951
952         DBG("atom: %p", atom);
953
954         g_free(em);
955 }
956
957 struct ofono_emulator *ofono_emulator_create(struct ofono_modem *modem,
958                                                 enum ofono_emulator_type type)
959 {
960         struct ofono_emulator *em;
961         enum ofono_atom_type atom_t;
962
963         DBG("modem: %p, type: %d", modem, type);
964
965         if (type == OFONO_EMULATOR_TYPE_DUN)
966                 atom_t = OFONO_ATOM_TYPE_EMULATOR_DUN;
967         else if (type == OFONO_EMULATOR_TYPE_HFP)
968                 atom_t = OFONO_ATOM_TYPE_EMULATOR_HFP;
969         else
970                 return NULL;
971
972         em = g_try_new0(struct ofono_emulator, 1);
973
974         if (em == NULL)
975                 return NULL;
976
977         em->type = type;
978         em->l_features |= HFP_AG_FEATURE_3WAY;
979         em->l_features |= HFP_AG_FEATURE_REJECT_CALL;
980         em->l_features |= HFP_AG_FEATURE_ENHANCED_CALL_STATUS;
981         em->l_features |= HFP_AG_FEATURE_ENHANCED_CALL_CONTROL;
982         em->l_features |= HFP_AG_FEATURE_EXTENDED_RES_CODE;
983         em->events_mode = 3;    /* default mode is forwarding events */
984         em->cmee_mode = 0;      /* CME ERROR disabled by default */
985
986         em->atom = __ofono_modem_add_atom_offline(modem, atom_t,
987                                                         emulator_remove, em);
988
989         return em;
990 }
991
992 void ofono_emulator_remove(struct ofono_emulator *em)
993 {
994         __ofono_atom_free(em->atom);
995 }
996
997 void ofono_emulator_send_final(struct ofono_emulator *em,
998                                 const struct ofono_error *final)
999 {
1000         char buf[256];
1001
1002         /*
1003          * TODO: Handle various CMEE modes and report error strings from
1004          * common.c
1005          */
1006         switch (final->type) {
1007         case OFONO_ERROR_TYPE_CMS:
1008                 sprintf(buf, "+CMS ERROR: %d", final->error);
1009                 g_at_server_send_ext_final(em->server, buf);
1010                 break;
1011
1012         case OFONO_ERROR_TYPE_CME:
1013                 switch (em->cmee_mode) {
1014                 case 1:
1015                         sprintf(buf, "+CME ERROR: %d", final->error);
1016                         break;
1017
1018                 case 2:
1019                         sprintf(buf, "+CME ERROR: %s",
1020                                                 telephony_error_to_str(final));
1021                         break;
1022
1023                 default:
1024                         goto failure;
1025                 }
1026
1027                 g_at_server_send_ext_final(em->server, buf);
1028                 break;
1029
1030         case OFONO_ERROR_TYPE_NO_ERROR:
1031                 g_at_server_send_final(em->server, G_AT_SERVER_RESULT_OK);
1032                 break;
1033
1034         case OFONO_ERROR_TYPE_CEER:
1035         case OFONO_ERROR_TYPE_SIM:
1036         case OFONO_ERROR_TYPE_FAILURE:
1037 failure:
1038                 g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
1039                 break;
1040         };
1041 }
1042
1043 void ofono_emulator_send_unsolicited(struct ofono_emulator *em,
1044                                         const char *result)
1045 {
1046         g_at_server_send_unsolicited(em->server, result);
1047 }
1048
1049 void ofono_emulator_send_intermediate(struct ofono_emulator *em,
1050                                         const char *result)
1051 {
1052         g_at_server_send_intermediate(em->server, result);
1053 }
1054
1055 void ofono_emulator_send_info(struct ofono_emulator *em, const char *line,
1056                                 ofono_bool_t last)
1057 {
1058         g_at_server_send_info(em->server, line, last);
1059 }
1060
1061 struct handler {
1062         ofono_emulator_request_cb_t cb;
1063         void *data;
1064         ofono_destroy_func destroy;
1065         struct ofono_emulator *em;
1066 };
1067
1068 struct ofono_emulator_request {
1069         GAtResultIter iter;
1070         enum ofono_emulator_request_type type;
1071 };
1072
1073 static void handler_proxy(GAtServer *server, GAtServerRequestType type,
1074                                 GAtResult *result, gpointer userdata)
1075 {
1076         struct handler *h = userdata;
1077         struct ofono_emulator_request req;
1078
1079         if (h->em->type == OFONO_EMULATOR_TYPE_HFP && h->em->slc == FALSE) {
1080                 g_at_server_send_final(h->em->server, G_AT_SERVER_RESULT_ERROR);
1081                 return;
1082         }
1083
1084         switch (type) {
1085         case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
1086                 req.type = OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY;
1087                 break;
1088         case G_AT_SERVER_REQUEST_TYPE_SET:
1089                 req.type = OFONO_EMULATOR_REQUEST_TYPE_SET;
1090                 break;
1091         case G_AT_SERVER_REQUEST_TYPE_QUERY:
1092                 req.type = OFONO_EMULATOR_REQUEST_TYPE_QUERY;
1093                 break;
1094         case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
1095                 req.type = OFONO_EMULATOR_REQUEST_TYPE_SUPPORT;
1096         }
1097
1098         g_at_result_iter_init(&req.iter, result);
1099         g_at_result_iter_next(&req.iter, "");
1100
1101         h->cb(h->em, &req, h->data);
1102 }
1103
1104 static void handler_destroy(gpointer userdata)
1105 {
1106         struct handler *h = userdata;
1107
1108         if (h->destroy)
1109                 h->destroy(h->data);
1110
1111         g_free(h);
1112 }
1113
1114 ofono_bool_t ofono_emulator_add_handler(struct ofono_emulator *em,
1115                                         const char *prefix,
1116                                         ofono_emulator_request_cb_t cb,
1117                                         void *data, ofono_destroy_func destroy)
1118 {
1119         struct handler *h;
1120
1121         h = g_new0(struct handler, 1);
1122         h->cb = cb;
1123         h->data = data;
1124         h->destroy = destroy;
1125         h->em = em;
1126
1127         if (g_at_server_register(em->server, prefix, handler_proxy, h,
1128                                         handler_destroy) == TRUE)
1129                 return TRUE;
1130
1131         g_free(h);
1132
1133         return FALSE;
1134 }
1135
1136 ofono_bool_t ofono_emulator_remove_handler(struct ofono_emulator *em,
1137                                                 const char *prefix)
1138 {
1139         return g_at_server_unregister(em->server, prefix);
1140 }
1141
1142 ofono_bool_t ofono_emulator_request_next_string(
1143                                         struct ofono_emulator_request *req,
1144                                         const char **str)
1145 {
1146         return g_at_result_iter_next_string(&req->iter, str);
1147 }
1148
1149 ofono_bool_t ofono_emulator_request_next_number(
1150                                         struct ofono_emulator_request *req,
1151                                         int *number)
1152 {
1153         return g_at_result_iter_next_number(&req->iter, number);
1154 }
1155
1156 const char *ofono_emulator_request_get_raw(struct ofono_emulator_request *req)
1157 {
1158         return g_at_result_iter_raw_line(&req->iter);
1159 }
1160
1161 enum ofono_emulator_request_type ofono_emulator_request_get_type(
1162                                         struct ofono_emulator_request *req)
1163 {
1164         return req->type;
1165 }
1166
1167 void ofono_emulator_set_indicator(struct ofono_emulator *em,
1168                                         const char *name, int value)
1169 {
1170         int i;
1171         char buf[20];
1172         struct indicator *ind;
1173         struct indicator *call_ind;
1174         struct indicator *cs_ind;
1175         gboolean call;
1176         gboolean callsetup;
1177         gboolean waiting;
1178
1179         ind = find_indicator(em, name, &i);
1180
1181         if (ind == NULL || ind->value == value || value < ind->min
1182                         || value > ind->max)
1183                 return;
1184
1185         ind->value = value;
1186
1187         call_ind = find_indicator(em, OFONO_EMULATOR_IND_CALL, NULL);
1188         cs_ind = find_indicator(em, OFONO_EMULATOR_IND_CALLSETUP, NULL);
1189
1190         call = ind == call_ind;
1191         callsetup = ind == cs_ind;
1192
1193         /*
1194          * When callsetup indicator goes to Incoming and there is an active
1195          * call a +CCWA should be sent before +CIEV
1196          */
1197         waiting = (callsetup && value == OFONO_EMULATOR_CALLSETUP_INCOMING &&
1198                         call_ind->value == OFONO_EMULATOR_CALL_ACTIVE);
1199
1200         if (waiting)
1201                 notify_ccwa(em);
1202
1203         if (em->events_mode == 3 && em->events_ind && em->slc && ind->active) {
1204                 if (!g_at_server_command_pending(em->server)) {
1205                         sprintf(buf, "+CIEV: %d,%d", i, ind->value);
1206                         g_at_server_send_unsolicited(em->server, buf);
1207                 } else
1208                         ind->deferred = TRUE;
1209         }
1210
1211         /*
1212          * Ring timer should be started when:
1213          * - callsetup indicator is set to Incoming and there is no active call
1214          *   (not a waiting call)
1215          * - or call indicator is set to inactive while callsetup is already
1216          *   set to Incoming.
1217          * In those cases, a first RING should be sent just after the +CIEV
1218          * Ring timer should be stopped for all other values of callsetup
1219          */
1220         if (waiting)
1221                 return;
1222
1223         /* Call state went from active/held + waiting -> incoming */
1224         if (call && value == OFONO_EMULATOR_CALL_INACTIVE &&
1225                         cs_ind->value == OFONO_EMULATOR_CALLSETUP_INCOMING)
1226                 goto start_ring;
1227
1228         if (!callsetup)
1229                 return;
1230
1231         if (value != OFONO_EMULATOR_CALLSETUP_INCOMING) {
1232                 if (em->callsetup_source > 0) {
1233                         g_source_remove(em->callsetup_source);
1234                         em->callsetup_source = 0;
1235                 }
1236
1237                 return;
1238         }
1239
1240 start_ring:
1241         notify_ring(em);
1242         em->callsetup_source = g_timeout_add_seconds(RING_TIMEOUT,
1243                                                         notify_ring, em);
1244 }
1245
1246 void __ofono_emulator_set_indicator_forced(struct ofono_emulator *em,
1247                                                 const char *name, int value)
1248 {
1249         int i;
1250         struct indicator *ind;
1251         char buf[20];
1252
1253         ind = find_indicator(em, name, &i);
1254
1255         if (ind == NULL || value < ind->min || value > ind->max)
1256                 return;
1257
1258         ind->value = value;
1259
1260         if (em->events_mode == 3 && em->events_ind && em->slc && ind->active) {
1261                 if (!g_at_server_command_pending(em->server)) {
1262                         sprintf(buf, "+CIEV: %d,%d", i, ind->value);
1263                         g_at_server_send_unsolicited(em->server, buf);
1264                 } else
1265                         ind->deferred = TRUE;
1266         }
1267 }