3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
6 * Copyright (C) 2011 BMW Car IT GmbH. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <gatresult.h>
38 #include <ofono/log.h>
39 #include <ofono/modem.h>
40 #include <ofono/handsfree.h>
45 static const char *binp_prefix[] = { "+BINP:", NULL };
46 static const char *bvra_prefix[] = { "+BVRA:", NULL };
50 unsigned int ag_features;
52 guint register_source;
55 static void hf_generic_set_cb(gboolean ok, GAtResult *result,
58 struct cb_data *cbd = user_data;
59 ofono_handsfree_cb_t cb = cbd->cb;
60 struct ofono_error error;
62 decode_at_error(&error, g_at_result_final_response(result));
64 cb(&error, cbd->data);
67 static void bsir_notify(GAtResult *result, gpointer user_data)
69 struct ofono_handsfree *hf = user_data;
73 g_at_result_iter_init(&iter, result);
75 if (!g_at_result_iter_next(&iter, "+BSIR:"))
78 if (!g_at_result_iter_next_number(&iter, &value))
81 ofono_handsfree_set_inband_ringing(hf, (ofono_bool_t) value);
84 static void bvra_notify(GAtResult *result, gpointer user_data)
86 struct ofono_handsfree *hf = user_data;
90 g_at_result_iter_init(&iter, result);
92 if (!g_at_result_iter_next(&iter, "+BVRA:"))
95 if (!g_at_result_iter_next_number(&iter, &value))
98 ofono_handsfree_voice_recognition_notify(hf, (ofono_bool_t) value);
101 static void ciev_notify(GAtResult *result, gpointer user_data)
103 struct ofono_handsfree *hf = user_data;
104 struct hf_data *hd = ofono_handsfree_get_data(hf);
109 g_at_result_iter_init(&iter, result);
111 if (!g_at_result_iter_next(&iter, "+CIEV:"))
114 if (!g_at_result_iter_next_number(&iter, &index))
117 if (index != hd->battchg_index)
120 if (!g_at_result_iter_next_number(&iter, &value))
123 ofono_handsfree_battchg_notify(hf, value);
126 static gboolean hfp_handsfree_register(gpointer user_data)
128 struct ofono_handsfree *hf = user_data;
129 struct hf_data *hd = ofono_handsfree_get_data(hf);
131 hd->register_source = 0;
133 g_at_chat_register(hd->chat, "+BSIR:", bsir_notify, FALSE, hf, NULL);
134 g_at_chat_register(hd->chat, "+BVRA:", bvra_notify, FALSE, hf, NULL);
135 g_at_chat_register(hd->chat, "+CIEV:", ciev_notify, FALSE, hf, NULL);
137 if (hd->ag_features & HFP_AG_FEATURE_IN_BAND_RING_TONE)
138 ofono_handsfree_set_inband_ringing(hf, TRUE);
140 ofono_handsfree_set_ag_features(hf, hd->ag_features);
141 ofono_handsfree_register(hf);
146 static int hfp_handsfree_probe(struct ofono_handsfree *hf,
147 unsigned int vendor, void *data)
149 struct hfp_slc_info *info = data;
153 hd = g_new0(struct hf_data, 1);
154 hd->chat = g_at_chat_clone(info->chat);
155 hd->ag_features = info->ag_features;
157 ofono_handsfree_set_data(hf, hd);
159 hd->battchg_index = info->cind_pos[HFP_INDICATOR_BATTCHG];
160 ofono_handsfree_battchg_notify(hf,
161 info->cind_val[HFP_INDICATOR_BATTCHG]);
163 hd->register_source = g_idle_add(hfp_handsfree_register, hf);
168 static void hfp_handsfree_remove(struct ofono_handsfree *hf)
170 struct hf_data *hd = ofono_handsfree_get_data(hf);
172 if (hd->register_source != 0)
173 g_source_remove(hd->register_source);
175 ofono_handsfree_set_data(hf, NULL);
177 g_at_chat_unref(hd->chat);
181 static void hfp_request_phone_number_cb(gboolean ok, GAtResult *result,
184 struct cb_data *cbd = user_data;
185 ofono_handsfree_phone_cb_t cb = cbd->cb;
187 struct ofono_error error;
190 struct ofono_phone_number phone_number;
192 decode_at_error(&error, g_at_result_final_response(result));
195 cb(&error, NULL, cbd->data);
199 g_at_result_iter_init(&iter, result);
201 if (!g_at_result_iter_next(&iter, "+BINP:"))
204 if (!g_at_result_iter_next_string(&iter, &num))
207 if (!g_at_result_iter_next_number(&iter, &type))
210 DBG("AT+BINP=1 response: %s %d", num, type);
212 strncpy(phone_number.number, num,
213 OFONO_MAX_PHONE_NUMBER_LENGTH);
214 phone_number.number[OFONO_MAX_PHONE_NUMBER_LENGTH] = '\0';
215 phone_number.type = type;
217 cb(&error, &phone_number, cbd->data);
221 CALLBACK_WITH_FAILURE(cb, NULL, cbd->data);
224 static void hfp_request_phone_number(struct ofono_handsfree *hf,
225 ofono_handsfree_phone_cb_t cb,
228 struct hf_data *hd = ofono_handsfree_get_data(hf);
229 struct cb_data *cbd = cb_data_new(cb, data);
231 if (g_at_chat_send(hd->chat, "AT+BINP=1", binp_prefix,
232 hfp_request_phone_number_cb,
238 CALLBACK_WITH_FAILURE(cb, NULL, data);
241 static void hfp_voice_recognition(struct ofono_handsfree *hf,
242 ofono_bool_t enabled,
243 ofono_handsfree_cb_t cb, void *data)
245 struct hf_data *hd = ofono_handsfree_get_data(hf);
246 struct cb_data *cbd = cb_data_new(cb, data);
249 snprintf(buf, sizeof(buf), "AT+BVRA=%d",
252 if (g_at_chat_send(hd->chat, buf, bvra_prefix,
259 CALLBACK_WITH_FAILURE(cb, data);
262 static struct ofono_handsfree_driver driver = {
264 .probe = hfp_handsfree_probe,
265 .remove = hfp_handsfree_remove,
266 .request_phone_number = hfp_request_phone_number,
267 .voice_recognition = hfp_voice_recognition,
270 void hfp_handsfree_init(void)
272 ofono_handsfree_driver_register(&driver);
275 void hfp_handsfree_exit(void)
277 ofono_handsfree_driver_unregister(&driver);