3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "gatserver.h"
37 #define RING_TIMEOUT 3
39 struct ofono_emulator {
40 struct ofono_atom *atom;
41 enum ofono_emulator_type type;
49 unsigned char cmee_mode;
51 guint callsetup_source;
67 static void emulator_debug(const char *str, void *data)
69 ofono_info("%s: %s\n", (char *)data, str);
72 static void emulator_disconnect(gpointer user_data)
74 struct ofono_emulator *em = user_data;
78 ofono_emulator_remove(em);
81 static void ppp_connect(const char *iface, const char *local,
83 const char *dns1, const char *dns2,
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);
93 static void cleanup_ppp(struct ofono_emulator *em)
97 g_at_ppp_unref(em->ppp);
100 __ofono_private_network_release(em->pns_id);
103 g_at_server_resume(em->server);
104 g_at_server_send_final(em->server, G_AT_SERVER_RESULT_NO_CARRIER);
107 static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
109 struct ofono_emulator *em = user_data;
114 static void ppp_suspend(gpointer user_data)
116 struct ofono_emulator *em = user_data;
120 g_at_server_resume(em->server);
123 static void suspend_server(gpointer user_data)
125 struct ofono_emulator *em = user_data;
126 GAtIO *io = g_at_server_get_io(em->server);
128 g_at_server_suspend(em->server);
130 if (g_at_ppp_listen(em->ppp, io) == FALSE)
134 static void request_private_network_cb(
135 const struct ofono_private_network_settings *pns,
138 struct ofono_emulator *em = data;
139 GAtIO *io = g_at_server_get_io(em->server);
144 em->ppp = g_at_ppp_server_new_full(pns->server_ip, pns->fd);
145 if (em->ppp == NULL) {
150 g_at_ppp_set_server_info(em->ppp, pns->peer_ip,
151 pns->primary_dns, pns->secondary_dns);
153 g_at_ppp_set_acfc_enabled(em->ppp, TRUE);
154 g_at_ppp_set_pfc_enabled(em->ppp, TRUE);
156 g_at_ppp_set_credentials(em->ppp, "", "");
157 g_at_ppp_set_debug(em->ppp, emulator_debug, "PPP");
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);
163 g_at_server_send_intermediate(em->server, "CONNECT");
164 g_at_io_set_write_done(io, suspend_server, em);
169 __ofono_private_network_release(em->pns_id);
173 g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
176 static gboolean dial_call(struct ofono_emulator *em, const char *dial_str)
180 DBG("dial call %s", dial_str);
182 if (c == '*' || c == '#' || c == 'T' || c == 't') {
183 if (__ofono_private_network_request(request_private_network_cb,
184 &em->pns_id, em) == FALSE)
191 static void dial_cb(GAtServer *server, GAtServerRequestType type,
192 GAtResult *result, gpointer user_data)
194 struct ofono_emulator *em = user_data;
196 const char *dial_str;
200 if (type != G_AT_SERVER_REQUEST_TYPE_SET)
203 g_at_result_iter_init(&iter, result);
205 if (!g_at_result_iter_next(&iter, ""))
208 dial_str = g_at_result_iter_raw_line(&iter);
215 if (!dial_call(em, dial_str))
221 g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
224 static void dun_ath_cb(GAtServer *server, GAtServerRequestType type,
225 GAtResult *result, gpointer user_data)
227 struct ofono_emulator *em = user_data;
234 case G_AT_SERVER_REQUEST_TYPE_SET:
235 g_at_result_iter_init(&iter, result);
236 g_at_result_iter_next(&iter, "");
238 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
246 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
250 g_at_ppp_unref(em->ppp);
253 __ofono_private_network_release(em->pns_id);
256 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
261 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
266 static void resume_ppp(gpointer user_data)
268 struct ofono_emulator *em = user_data;
270 g_at_server_suspend(em->server);
271 g_at_ppp_resume(em->ppp);
274 static void dun_ato_cb(GAtServer *server, GAtServerRequestType type,
275 GAtResult *result, gpointer user_data)
277 struct ofono_emulator *em = user_data;
278 GAtIO *io = g_at_server_get_io(em->server);
285 case G_AT_SERVER_REQUEST_TYPE_SET:
286 g_at_result_iter_init(&iter, result);
287 g_at_result_iter_next(&iter, "");
289 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
296 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
300 g_at_server_send_intermediate(em->server, "CONNECT");
301 g_at_io_set_write_done(io, resume_ppp, em);
306 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
311 static struct indicator *find_indicator(struct ofono_emulator *em,
312 const char *name, int *index)
317 for (i = 1, l = em->indicators; l; l = l->next, i++) {
318 struct indicator *ind = l->data;
320 if (g_str_equal(ind->name, name) == FALSE)
332 static struct ofono_call *find_call_with_status(struct ofono_emulator *em,
335 struct ofono_modem *modem = __ofono_atom_get_modem(em->atom);
336 struct ofono_voicecall *vc;
338 vc = __ofono_atom_find(OFONO_ATOM_TYPE_VOICECALL, modem);
342 return __ofono_voicecall_find_call_with_status(vc, status);
345 static void notify_deferred_indicators(GAtServer *server, void *user_data)
347 struct ofono_emulator *em = user_data;
351 struct indicator *ind;
353 for (i = 1, l = em->indicators; l; l = l->next, i++) {
359 if (em->events_mode == 3 && em->events_ind && em->slc &&
361 sprintf(buf, "+CIEV: %d,%d", i, ind->value);
362 g_at_server_send_unsolicited(em->server, buf);
365 ind->deferred = FALSE;
369 static gboolean notify_ccwa(void *user_data)
371 struct ofono_emulator *em = user_data;
372 struct ofono_call *c;
375 * '+CCWA: "+",' + phone number + phone type on 3 digits max
378 char str[OFONO_MAX_PHONE_NUMBER_LENGTH + 14 + 1];
380 if ((em->type == OFONO_EMULATOR_TYPE_HFP && em->slc == FALSE) ||
384 c = find_call_with_status(em, CALL_STATUS_WAITING);
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);
390 g_at_server_send_unsolicited(em->server, str);
392 g_at_server_send_unsolicited(em->server, "+CCWA: \"\",128");
395 em->callsetup_source = 0;
400 static gboolean notify_ring(void *user_data)
402 struct ofono_emulator *em = user_data;
403 struct ofono_call *c;
406 * '+CLIP: "+",' + phone number + phone type on 3 digits max
409 char str[OFONO_MAX_PHONE_NUMBER_LENGTH + 14 + 1];
411 if (em->type == OFONO_EMULATOR_TYPE_HFP && em->slc == FALSE)
414 g_at_server_send_unsolicited(em->server, "RING");
419 c = find_call_with_status(em, CALL_STATUS_INCOMING);
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);
431 case CLIP_VALIDITY_WITHHELD:
432 g_at_server_send_unsolicited(em->server, "+CLIP: \"\",128");
439 static void brsf_cb(GAtServer *server, GAtServerRequestType type,
440 GAtResult *result, gpointer user_data)
442 struct ofono_emulator *em = user_data;
448 case G_AT_SERVER_REQUEST_TYPE_SET:
449 g_at_result_iter_init(&iter, result);
450 g_at_result_iter_next(&iter, "");
452 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
455 if (val < 0 || val > 127)
458 em->r_features = val;
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);
467 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
472 static void cind_cb(GAtServer *server, GAtServerRequestType type,
473 GAtResult *result, gpointer user_data)
475 struct ofono_emulator *em = user_data;
477 struct indicator *ind;
484 case G_AT_SERVER_REQUEST_TYPE_QUERY:
486 * "+CIND: " + terminating null + number of indicators *
487 * (max of 3 digits in the value + separator)
489 size = 7 + 1 + (g_slist_length(em->indicators) * 4);
490 buf = g_try_malloc0(size);
494 len = sprintf(buf, "+CIND: ");
497 for (l = em->indicators; l; l = l->next) {
499 len = sprintf(tmp, "%s%d",
500 l == em->indicators ? "" : ",",
505 g_at_server_send_info(em->server, buf, TRUE);
507 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
510 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
512 * '+CIND: ' + terminating null + number of indicators *
513 * ( indicator name + '("",(000,000))' + separator)
517 for (l = em->indicators; l; l = l->next) {
519 size += strlen(ind->name) + 15;
522 buf = g_try_malloc0(size);
526 len = sprintf(buf, "+CIND: ");
529 for (l = em->indicators; l; l = l->next) {
531 len = sprintf(tmp, "%s(\"%s\",(%d%c%d))",
532 l == em->indicators ? "" : ",",
534 (ind->max - ind->min) == 1 ? ',' : '-',
539 g_at_server_send_info(server, buf, TRUE);
541 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
546 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
551 static void cmer_cb(GAtServer *server, GAtServerRequestType type,
552 GAtResult *result, gpointer user_data)
554 struct ofono_emulator *em = user_data;
558 case G_AT_SERVER_REQUEST_TYPE_QUERY:
559 sprintf(buf, "+CMER: %d,0,0,%d,0", em->events_mode,
561 g_at_server_send_info(em->server, buf, TRUE);
562 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
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);
571 case G_AT_SERVER_REQUEST_TYPE_SET:
574 int mode = em->events_mode;
575 int ind = em->events_ind;
578 g_at_result_iter_init(&iter, result);
579 g_at_result_iter_next(&iter, "");
582 if (!g_at_result_iter_next_number_default(&iter, mode, &mode))
585 if (mode != 0 && mode != 3)
589 if (!g_at_result_iter_next_number_default(&iter, 0, &val)) {
590 if (!g_at_result_iter_skip_next(&iter))
599 if (!g_at_result_iter_next_number_default(&iter, 0, &val)) {
600 if (!g_at_result_iter_skip_next(&iter))
609 if (!g_at_result_iter_next_number_default(&iter, ind, &ind)) {
610 if (!g_at_result_iter_skip_next(&iter))
615 if (ind != 0 && ind != 1)
619 if (!g_at_result_iter_next_number_default(&iter, 0, &val)) {
620 if (!g_at_result_iter_skip_next(&iter))
628 /* check that bfr is last parameter */
629 if (g_at_result_iter_skip_next(&iter))
633 em->events_mode = mode;
634 em->events_ind = ind;
636 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
644 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
649 static void clip_cb(GAtServer *server, GAtServerRequestType type,
650 GAtResult *result, gpointer user_data)
652 struct ofono_emulator *em = user_data;
656 if (em->slc == FALSE)
660 case G_AT_SERVER_REQUEST_TYPE_SET:
661 g_at_result_iter_init(&iter, result);
662 g_at_result_iter_next(&iter, "");
664 if (!g_at_result_iter_next_number(&iter, &val))
667 if (val != 0 && val != 1)
670 /* check this is last parameter */
671 if (g_at_result_iter_skip_next(&iter))
676 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
681 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
685 static void ccwa_cb(GAtServer *server, GAtServerRequestType type,
686 GAtResult *result, gpointer user_data)
688 struct ofono_emulator *em = user_data;
691 struct indicator *call_ind;
692 struct indicator *cs_ind;
694 if (em->slc == FALSE)
698 case G_AT_SERVER_REQUEST_TYPE_SET:
699 g_at_result_iter_init(&iter, result);
700 g_at_result_iter_next(&iter, "");
702 if (!g_at_result_iter_next_number(&iter, &val))
705 if (val != 0 && val != 1)
708 /* check this is last parameter */
709 if (g_at_result_iter_skip_next(&iter))
712 call_ind = find_indicator(em, OFONO_EMULATOR_IND_CALL, NULL);
713 cs_ind = find_indicator(em, OFONO_EMULATOR_IND_CALLSETUP, NULL);
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,
723 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
728 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
732 static void cmee_cb(GAtServer *server, GAtServerRequestType type,
733 GAtResult *result, gpointer user_data)
735 struct ofono_emulator *em = user_data;
741 case G_AT_SERVER_REQUEST_TYPE_SET:
742 g_at_result_iter_init(&iter, result);
743 g_at_result_iter_next(&iter, "");
745 if (g_at_result_iter_next_number(&iter, &val) == FALSE)
748 if (val != 0 && val != 1)
753 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
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);
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);
771 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
776 static void bia_cb(GAtServer *server, GAtServerRequestType type,
777 GAtResult *result, gpointer user_data)
779 struct ofono_emulator *em = user_data;
782 case G_AT_SERVER_REQUEST_TYPE_SET:
788 g_at_result_iter_init(&iter, result);
789 g_at_result_iter_next(&iter, "");
791 /* check validity of the request */
792 while (g_at_result_iter_next_number_default(&iter, 0, &val))
793 if (val != 0 && val != 1)
796 /* Check that we have no non-numbers in the stream */
797 if (g_at_result_iter_skip_next(&iter) == TRUE)
800 /* request is valid, update the indicator activation status */
801 g_at_result_iter_init(&iter, result);
802 g_at_result_iter_next(&iter, "");
804 for (l = em->indicators; l; l = l->next) {
805 struct indicator *ind = l->data;
807 if (g_at_result_iter_next_number_default(&iter,
808 ind->active, &val) == FALSE)
811 if (ind->mandatory == TRUE)
817 g_at_server_send_final(server, G_AT_SERVER_RESULT_OK);
823 g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
828 static void emulator_add_indicator(struct ofono_emulator *em, const char* name,
829 int min, int max, int dflt,
832 struct indicator *ind;
834 ind = g_try_new0(struct indicator, 1);
836 ofono_error("Unable to allocate indicator structure");
840 ind->name = g_strdup(name);
845 ind->mandatory = mandatory;
847 em->indicators = g_slist_append(em->indicators, ind);
850 static void emulator_unregister(struct ofono_atom *atom)
852 struct ofono_emulator *em = __ofono_atom_get_data(atom);
857 if (em->callsetup_source) {
858 g_source_remove(em->callsetup_source);
859 em->callsetup_source = 0;
862 for (l = em->indicators; l; l = l->next) {
863 struct indicator *ind = l->data;
869 g_slist_free(em->indicators);
870 em->indicators = NULL;
872 g_at_ppp_unref(em->ppp);
875 if (em->pns_id > 0) {
876 __ofono_private_network_release(em->pns_id);
880 g_at_server_unref(em->server);
884 void ofono_emulator_register(struct ofono_emulator *em, int fd)
888 DBG("%p, %d", em, fd);
893 io = g_io_channel_unix_new(fd);
895 em->server = g_at_server_new(io);
896 if (em->server == NULL)
899 g_io_channel_unref(io);
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,
907 if (em->type == OFONO_EMULATOR_TYPE_HFP) {
908 emulator_add_indicator(em, OFONO_EMULATOR_IND_SERVICE, 0, 1, 0,
910 emulator_add_indicator(em, OFONO_EMULATOR_IND_CALL, 0, 1, 0,
912 emulator_add_indicator(em, OFONO_EMULATOR_IND_CALLSETUP, 0, 3,
914 emulator_add_indicator(em, OFONO_EMULATOR_IND_CALLHELD, 0, 2,
916 emulator_add_indicator(em, OFONO_EMULATOR_IND_SIGNAL, 0, 5, 0,
918 emulator_add_indicator(em, OFONO_EMULATOR_IND_ROAMING, 0, 1, 0,
920 emulator_add_indicator(em, OFONO_EMULATOR_IND_BATTERY, 0, 5, 5,
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);
932 __ofono_atom_register(em->atom, emulator_unregister);
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);
940 case OFONO_EMULATOR_TYPE_HFP:
941 g_at_server_set_echo(em->server, FALSE);
948 static void emulator_remove(struct ofono_atom *atom)
950 struct ofono_emulator *em = __ofono_atom_get_data(atom);
952 DBG("atom: %p", atom);
957 struct ofono_emulator *ofono_emulator_create(struct ofono_modem *modem,
958 enum ofono_emulator_type type)
960 struct ofono_emulator *em;
961 enum ofono_atom_type atom_t;
963 DBG("modem: %p, type: %d", modem, type);
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;
972 em = g_try_new0(struct ofono_emulator, 1);
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 */
986 em->atom = __ofono_modem_add_atom_offline(modem, atom_t,
987 emulator_remove, em);
992 void ofono_emulator_remove(struct ofono_emulator *em)
994 __ofono_atom_free(em->atom);
997 void ofono_emulator_send_final(struct ofono_emulator *em,
998 const struct ofono_error *final)
1003 * TODO: Handle various CMEE modes and report error strings from
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);
1012 case OFONO_ERROR_TYPE_CME:
1013 switch (em->cmee_mode) {
1015 sprintf(buf, "+CME ERROR: %d", final->error);
1019 sprintf(buf, "+CME ERROR: %s",
1020 telephony_error_to_str(final));
1027 g_at_server_send_ext_final(em->server, buf);
1030 case OFONO_ERROR_TYPE_NO_ERROR:
1031 g_at_server_send_final(em->server, G_AT_SERVER_RESULT_OK);
1034 case OFONO_ERROR_TYPE_CEER:
1035 case OFONO_ERROR_TYPE_SIM:
1036 case OFONO_ERROR_TYPE_FAILURE:
1038 g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
1043 void ofono_emulator_send_unsolicited(struct ofono_emulator *em,
1046 g_at_server_send_unsolicited(em->server, result);
1049 void ofono_emulator_send_intermediate(struct ofono_emulator *em,
1052 g_at_server_send_intermediate(em->server, result);
1055 void ofono_emulator_send_info(struct ofono_emulator *em, const char *line,
1058 g_at_server_send_info(em->server, line, last);
1062 ofono_emulator_request_cb_t cb;
1064 ofono_destroy_func destroy;
1065 struct ofono_emulator *em;
1068 struct ofono_emulator_request {
1070 enum ofono_emulator_request_type type;
1073 static void handler_proxy(GAtServer *server, GAtServerRequestType type,
1074 GAtResult *result, gpointer userdata)
1076 struct handler *h = userdata;
1077 struct ofono_emulator_request req;
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);
1085 case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
1086 req.type = OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY;
1088 case G_AT_SERVER_REQUEST_TYPE_SET:
1089 req.type = OFONO_EMULATOR_REQUEST_TYPE_SET;
1091 case G_AT_SERVER_REQUEST_TYPE_QUERY:
1092 req.type = OFONO_EMULATOR_REQUEST_TYPE_QUERY;
1094 case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
1095 req.type = OFONO_EMULATOR_REQUEST_TYPE_SUPPORT;
1098 g_at_result_iter_init(&req.iter, result);
1099 g_at_result_iter_next(&req.iter, "");
1101 h->cb(h->em, &req, h->data);
1104 static void handler_destroy(gpointer userdata)
1106 struct handler *h = userdata;
1109 h->destroy(h->data);
1114 ofono_bool_t ofono_emulator_add_handler(struct ofono_emulator *em,
1116 ofono_emulator_request_cb_t cb,
1117 void *data, ofono_destroy_func destroy)
1121 h = g_new0(struct handler, 1);
1124 h->destroy = destroy;
1127 if (g_at_server_register(em->server, prefix, handler_proxy, h,
1128 handler_destroy) == TRUE)
1136 ofono_bool_t ofono_emulator_remove_handler(struct ofono_emulator *em,
1139 return g_at_server_unregister(em->server, prefix);
1142 ofono_bool_t ofono_emulator_request_next_string(
1143 struct ofono_emulator_request *req,
1146 return g_at_result_iter_next_string(&req->iter, str);
1149 ofono_bool_t ofono_emulator_request_next_number(
1150 struct ofono_emulator_request *req,
1153 return g_at_result_iter_next_number(&req->iter, number);
1156 const char *ofono_emulator_request_get_raw(struct ofono_emulator_request *req)
1158 return g_at_result_iter_raw_line(&req->iter);
1161 enum ofono_emulator_request_type ofono_emulator_request_get_type(
1162 struct ofono_emulator_request *req)
1167 void ofono_emulator_set_indicator(struct ofono_emulator *em,
1168 const char *name, int value)
1172 struct indicator *ind;
1173 struct indicator *call_ind;
1174 struct indicator *cs_ind;
1179 ind = find_indicator(em, name, &i);
1181 if (ind == NULL || ind->value == value || value < ind->min
1182 || value > ind->max)
1187 call_ind = find_indicator(em, OFONO_EMULATOR_IND_CALL, NULL);
1188 cs_ind = find_indicator(em, OFONO_EMULATOR_IND_CALLSETUP, NULL);
1190 call = ind == call_ind;
1191 callsetup = ind == cs_ind;
1194 * When callsetup indicator goes to Incoming and there is an active
1195 * call a +CCWA should be sent before +CIEV
1197 waiting = (callsetup && value == OFONO_EMULATOR_CALLSETUP_INCOMING &&
1198 call_ind->value == OFONO_EMULATOR_CALL_ACTIVE);
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);
1208 ind->deferred = TRUE;
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
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
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)
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;
1242 em->callsetup_source = g_timeout_add_seconds(RING_TIMEOUT,
1246 void __ofono_emulator_set_indicator_forced(struct ofono_emulator *em,
1247 const char *name, int value)
1250 struct indicator *ind;
1253 ind = find_indicator(em, name, &i);
1255 if (ind == NULL || value < ind->min || value > ind->max)
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);
1265 ind->deferred = TRUE;