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>
46 static const char *binp_prefix[] = { "+BINP:", NULL };
47 static const char *bvra_prefix[] = { "+BVRA:", NULL };
51 unsigned int ag_features;
52 unsigned int ag_chld_features;
54 guint register_source;
57 static void hf_generic_set_cb(gboolean ok, GAtResult *result,
60 struct cb_data *cbd = user_data;
61 ofono_handsfree_cb_t cb = cbd->cb;
62 struct ofono_error error;
64 decode_at_error(&error, g_at_result_final_response(result));
66 cb(&error, cbd->data);
69 static void bsir_notify(GAtResult *result, gpointer user_data)
71 struct ofono_handsfree *hf = user_data;
75 g_at_result_iter_init(&iter, result);
77 if (!g_at_result_iter_next(&iter, "+BSIR:"))
80 if (!g_at_result_iter_next_number(&iter, &value))
83 ofono_handsfree_set_inband_ringing(hf, (ofono_bool_t) value);
86 static void bvra_notify(GAtResult *result, gpointer user_data)
88 struct ofono_handsfree *hf = user_data;
92 g_at_result_iter_init(&iter, result);
94 if (!g_at_result_iter_next(&iter, "+BVRA:"))
97 if (!g_at_result_iter_next_number(&iter, &value))
100 ofono_handsfree_voice_recognition_notify(hf, (ofono_bool_t) value);
103 static void ciev_notify(GAtResult *result, gpointer user_data)
105 struct ofono_handsfree *hf = user_data;
106 struct hf_data *hd = ofono_handsfree_get_data(hf);
111 g_at_result_iter_init(&iter, result);
113 if (!g_at_result_iter_next(&iter, "+CIEV:"))
116 if (!g_at_result_iter_next_number(&iter, &index))
119 if (index != hd->battchg_index)
122 if (!g_at_result_iter_next_number(&iter, &value))
125 ofono_handsfree_battchg_notify(hf, value);
128 static gboolean hfp_handsfree_register(gpointer user_data)
130 struct ofono_handsfree *hf = user_data;
131 struct hf_data *hd = ofono_handsfree_get_data(hf);
133 hd->register_source = 0;
135 g_at_chat_register(hd->chat, "+BSIR:", bsir_notify, FALSE, hf, NULL);
136 g_at_chat_register(hd->chat, "+BVRA:", bvra_notify, FALSE, hf, NULL);
137 g_at_chat_register(hd->chat, "+CIEV:", ciev_notify, FALSE, hf, NULL);
139 if (hd->ag_features & HFP_AG_FEATURE_IN_BAND_RING_TONE)
140 ofono_handsfree_set_inband_ringing(hf, TRUE);
142 ofono_handsfree_set_ag_features(hf, hd->ag_features);
143 ofono_handsfree_set_ag_chld_features(hf, hd->ag_chld_features);
144 ofono_handsfree_register(hf);
149 static int hfp_handsfree_probe(struct ofono_handsfree *hf,
150 unsigned int vendor, void *data)
152 struct hfp_slc_info *info = data;
156 hd = g_new0(struct hf_data, 1);
157 hd->chat = g_at_chat_clone(info->chat);
158 hd->ag_features = info->ag_features;
159 hd->ag_chld_features = info->ag_mpty_features;
161 ofono_handsfree_set_data(hf, hd);
163 hd->battchg_index = info->cind_pos[HFP_INDICATOR_BATTCHG];
164 ofono_handsfree_battchg_notify(hf,
165 info->cind_val[HFP_INDICATOR_BATTCHG]);
167 hd->register_source = g_idle_add(hfp_handsfree_register, hf);
172 static void hfp_handsfree_remove(struct ofono_handsfree *hf)
174 struct hf_data *hd = ofono_handsfree_get_data(hf);
176 if (hd->register_source != 0)
177 g_source_remove(hd->register_source);
179 ofono_handsfree_set_data(hf, NULL);
181 g_at_chat_unref(hd->chat);
185 static void hfp_request_phone_number_cb(gboolean ok, GAtResult *result,
188 struct cb_data *cbd = user_data;
189 ofono_handsfree_phone_cb_t cb = cbd->cb;
191 struct ofono_error error;
194 struct ofono_phone_number phone_number;
196 decode_at_error(&error, g_at_result_final_response(result));
199 cb(&error, NULL, cbd->data);
203 g_at_result_iter_init(&iter, result);
205 if (!g_at_result_iter_next(&iter, "+BINP:"))
208 if (!g_at_result_iter_next_string(&iter, &num))
211 if (!g_at_result_iter_next_number(&iter, &type))
214 DBG("AT+BINP=1 response: %s %d", num, type);
216 strncpy(phone_number.number, num,
217 OFONO_MAX_PHONE_NUMBER_LENGTH);
218 phone_number.number[OFONO_MAX_PHONE_NUMBER_LENGTH] = '\0';
219 phone_number.type = type;
221 cb(&error, &phone_number, cbd->data);
225 CALLBACK_WITH_FAILURE(cb, NULL, cbd->data);
228 static void hfp_request_phone_number(struct ofono_handsfree *hf,
229 ofono_handsfree_phone_cb_t cb,
232 struct hf_data *hd = ofono_handsfree_get_data(hf);
233 struct cb_data *cbd = cb_data_new(cb, data);
235 if (g_at_chat_send(hd->chat, "AT+BINP=1", binp_prefix,
236 hfp_request_phone_number_cb,
242 CALLBACK_WITH_FAILURE(cb, NULL, data);
245 static void hfp_voice_recognition(struct ofono_handsfree *hf,
246 ofono_bool_t enabled,
247 ofono_handsfree_cb_t cb, void *data)
249 struct hf_data *hd = ofono_handsfree_get_data(hf);
250 struct cb_data *cbd = cb_data_new(cb, data);
253 snprintf(buf, sizeof(buf), "AT+BVRA=%d",
256 if (g_at_chat_send(hd->chat, buf, bvra_prefix,
263 CALLBACK_WITH_FAILURE(cb, data);
266 static void hfp_disable_nrec(struct ofono_handsfree *hf,
267 ofono_handsfree_cb_t cb, void *data)
269 struct hf_data *hd = ofono_handsfree_get_data(hf);
270 struct cb_data *cbd = cb_data_new(cb, data);
271 const char *buf = "AT+NREC=0";
273 if (g_at_chat_send(hd->chat, buf, NULL, hf_generic_set_cb,
279 CALLBACK_WITH_FAILURE(cb, data);
282 static struct ofono_handsfree_driver driver = {
284 .probe = hfp_handsfree_probe,
285 .remove = hfp_handsfree_remove,
286 .request_phone_number = hfp_request_phone_number,
287 .voice_recognition = hfp_voice_recognition,
288 .disable_nrec = hfp_disable_nrec,
291 void hfp_handsfree_init(void)
293 ofono_handsfree_driver_register(&driver);
296 void hfp_handsfree_exit(void)
298 ofono_handsfree_driver_unregister(&driver);