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