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 <ofono/log.h>
37 #include <ofono/modem.h>
38 #include <ofono/handsfree.h>
45 static GSList *g_drivers = NULL;
47 #define HANDSFREE_FLAG_CACHED 0x1
49 struct ofono_handsfree {
51 ofono_bool_t inband_ringing;
52 ofono_bool_t voice_recognition;
53 ofono_bool_t voice_recognition_pending;
54 unsigned int ag_features;
55 unsigned int ag_chld_features;
56 unsigned char battchg;
57 GSList *subscriber_numbers;
59 const struct ofono_handsfree_driver *driver;
61 struct ofono_atom *atom;
66 static const char **ag_features_list(unsigned int features,
67 unsigned int chld_features)
69 static const char *list[10];
72 if (features & HFP_AG_FEATURE_3WAY)
73 list[i++] = "three-way-calling";
75 if (features & HFP_AG_FEATURE_ECNR)
76 list[i++] = "echo-canceling-and-noise-reduction";
78 if (features & HFP_AG_FEATURE_VOICE_RECOG)
79 list[i++] = "voice-recognition";
81 if (features & HFP_AG_FEATURE_ATTACH_VOICE_TAG)
82 list[i++] = "attach-voice-tag";
84 if (chld_features & HFP_AG_CHLD_0)
85 list[i++] = "release-all-held";
87 if (chld_features & HFP_AG_CHLD_1x)
88 list[i++] = "release-specified-active-call";
90 if (chld_features & HFP_AG_CHLD_2x)
91 list[i++] = "private-chat";
93 if (chld_features & HFP_AG_CHLD_3)
94 list[i++] = "create-multiparty";
96 if (chld_features & HFP_AG_CHLD_4)
97 list[i++] = "transfer";
104 void ofono_handsfree_set_inband_ringing(struct ofono_handsfree *hf,
105 ofono_bool_t enabled)
107 DBusConnection *conn = ofono_dbus_get_connection();
108 const char *path = __ofono_atom_get_path(hf->atom);
109 dbus_bool_t dbus_enabled = enabled;
111 if (hf->inband_ringing == enabled)
114 hf->inband_ringing = enabled;
116 if (__ofono_atom_get_registered(hf->atom) == FALSE)
119 ofono_dbus_signal_property_changed(conn, path,
120 OFONO_HANDSFREE_INTERFACE,
121 "InbandRinging", DBUS_TYPE_BOOLEAN,
125 void ofono_handsfree_voice_recognition_notify(struct ofono_handsfree *hf,
126 ofono_bool_t enabled)
128 DBusConnection *conn = ofono_dbus_get_connection();
129 const char *path = __ofono_atom_get_path(hf->atom);
130 dbus_bool_t dbus_enabled = enabled;
132 if (hf->voice_recognition == enabled)
135 hf->voice_recognition = enabled;
137 ofono_dbus_signal_property_changed(conn, path,
138 OFONO_HANDSFREE_INTERFACE,
139 "VoiceRecognition", DBUS_TYPE_BOOLEAN,
143 void ofono_handsfree_set_ag_features(struct ofono_handsfree *hf,
144 unsigned int ag_features)
149 hf->ag_features = ag_features;
152 void ofono_handsfree_set_ag_chld_features(struct ofono_handsfree *hf,
153 unsigned int ag_chld_features)
158 hf->ag_chld_features = ag_chld_features;
161 void ofono_handsfree_battchg_notify(struct ofono_handsfree *hf,
164 DBusConnection *conn = ofono_dbus_get_connection();
165 const char *path = __ofono_atom_get_path(hf->atom);
170 if (hf->battchg == level)
175 if (__ofono_atom_get_registered(hf->atom) == FALSE)
178 ofono_dbus_signal_property_changed(conn, path,
179 OFONO_HANDSFREE_INTERFACE,
180 "BatteryChargeLevel", DBUS_TYPE_BYTE,
184 static gboolean ofono_handsfree_is_busy(struct ofono_handsfree *hf)
186 return hf->pending ? TRUE : FALSE;
189 static void ofono_append_subscriber_numbers(GSList *subscriber_numbers,
190 DBusMessageIter *iter)
192 DBusMessageIter entry;
193 DBusMessageIter variant, array;
196 const char *subscriber_number_string;
198 char *subscriber_number_text = "SubscriberNumbers";
200 arraysig[0] = DBUS_TYPE_ARRAY;
201 arraysig[1] = DBUS_TYPE_STRING;
204 dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY,
206 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING,
207 &subscriber_number_text);
208 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
210 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
211 DBUS_TYPE_STRING_AS_STRING, &array);
213 for (i = 0, l = subscriber_numbers; l; l = l->next, i++) {
214 subscriber_number_string = phone_number_to_string(l->data);
215 dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING,
216 &subscriber_number_string);
219 dbus_message_iter_close_container(&variant, &array);
221 dbus_message_iter_close_container(&entry, &variant);
222 dbus_message_iter_close_container(iter, &entry);
225 static DBusMessage *generate_get_properties_reply(struct ofono_handsfree *hf,
229 DBusMessageIter iter;
230 DBusMessageIter dict;
231 dbus_bool_t inband_ringing;
232 dbus_bool_t voice_recognition;
233 const char **features;
235 reply = dbus_message_new_method_return(msg);
239 dbus_message_iter_init_append(reply, &iter);
241 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
242 OFONO_PROPERTIES_ARRAY_SIGNATURE,
245 inband_ringing = hf->inband_ringing;
246 ofono_dbus_dict_append(&dict, "InbandRinging", DBUS_TYPE_BOOLEAN,
249 if (hf->ag_features & HFP_AG_FEATURE_ECNR)
250 ofono_dbus_dict_append(&dict, "EchoCancelingNoiseReduction",
251 DBUS_TYPE_BOOLEAN, &hf->nrec);
253 voice_recognition = hf->voice_recognition;
254 ofono_dbus_dict_append(&dict, "VoiceRecognition", DBUS_TYPE_BOOLEAN,
257 features = ag_features_list(hf->ag_features, hf->ag_chld_features);
258 ofono_dbus_dict_append_array(&dict, "Features", DBUS_TYPE_STRING,
261 ofono_dbus_dict_append(&dict, "BatteryChargeLevel", DBUS_TYPE_BYTE,
264 if (hf->subscriber_numbers)
265 ofono_append_subscriber_numbers(hf->subscriber_numbers, &dict);
267 dbus_message_iter_close_container(&iter, &dict);
272 static void hf_cnum_callback(const struct ofono_error *error, int total,
273 const struct ofono_phone_number *numbers,
276 struct ofono_handsfree *hf = data;
278 struct ofono_phone_number *subscriber_number_string;
280 if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
283 for (num = 0; num < total; num++) {
284 subscriber_number_string = g_new0(struct ofono_phone_number, 1);
286 subscriber_number_string->type = numbers[num].type;
287 strncpy(subscriber_number_string->number, numbers[num].number,
288 OFONO_MAX_PHONE_NUMBER_LENGTH+1);
290 hf->subscriber_numbers = g_slist_prepend(hf->subscriber_numbers,
291 subscriber_number_string);
294 hf->subscriber_numbers = g_slist_reverse(hf->subscriber_numbers);
297 hf->flags |= HANDSFREE_FLAG_CACHED;
301 generate_get_properties_reply(hf, hf->pending);
302 __ofono_dbus_pending_reply(&hf->pending, reply);
306 static void query_cnum(struct ofono_handsfree *hf)
308 if (hf->driver->cnum_query == NULL) {
311 generate_get_properties_reply(hf, hf->pending);
312 __ofono_dbus_pending_reply(&hf->pending, reply);
318 hf->driver->cnum_query(hf, hf_cnum_callback, hf);
321 static DBusMessage *handsfree_get_properties(DBusConnection *conn,
322 DBusMessage *msg, void *data)
324 struct ofono_handsfree *hf = data;
326 if (ofono_handsfree_is_busy(hf))
327 return __ofono_error_busy(msg);
329 if (hf->flags & HANDSFREE_FLAG_CACHED)
330 return generate_get_properties_reply(hf, msg);
332 /* Query the settings and report back */
333 hf->pending = dbus_message_ref(msg);
340 static void voicerec_set_cb(const struct ofono_error *error, void *data)
342 struct ofono_handsfree *hf = data;
343 DBusConnection *conn = ofono_dbus_get_connection();
344 const char *path = __ofono_atom_get_path(hf->atom);
346 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
347 __ofono_dbus_pending_reply(&hf->pending,
348 __ofono_error_failed(hf->pending));
352 hf->voice_recognition = hf->voice_recognition_pending;
354 __ofono_dbus_pending_reply(&hf->pending,
355 dbus_message_new_method_return(hf->pending));
357 ofono_dbus_signal_property_changed(conn, path,
358 OFONO_HANDSFREE_INTERFACE,
361 &hf->voice_recognition);
364 static void nrec_set_cb(const struct ofono_error *error, void *data)
366 struct ofono_handsfree *hf = data;
367 DBusConnection *conn = ofono_dbus_get_connection();
368 const char *path = __ofono_atom_get_path(hf->atom);
370 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
371 __ofono_dbus_pending_reply(&hf->pending,
372 __ofono_error_failed(hf->pending));
378 __ofono_dbus_pending_reply(&hf->pending,
379 dbus_message_new_method_return(hf->pending));
381 ofono_dbus_signal_property_changed(conn, path,
382 OFONO_HANDSFREE_INTERFACE,
383 "EchoCancelingNoiseReduction",
388 static DBusMessage *handsfree_set_property(DBusConnection *conn,
389 DBusMessage *msg, void *data)
391 struct ofono_handsfree *hf = data;
392 DBusMessageIter iter, var;
393 ofono_bool_t enabled;
397 return __ofono_error_busy(msg);
399 if (dbus_message_iter_init(msg, &iter) == FALSE)
400 return __ofono_error_invalid_args(msg);
402 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
403 return __ofono_error_invalid_args(msg);
405 dbus_message_iter_get_basic(&iter, &name);
406 dbus_message_iter_next(&iter);
408 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
409 return __ofono_error_invalid_args(msg);
411 dbus_message_iter_recurse(&iter, &var);
413 if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
414 return __ofono_error_invalid_args(msg);
416 dbus_message_iter_get_basic(&var, &enabled);
418 if (g_str_equal(name, "VoiceRecognition") == TRUE) {
420 if (!hf->driver->voice_recognition)
421 return __ofono_error_not_implemented(msg);
423 if (hf->voice_recognition == enabled)
424 return dbus_message_new_method_return(msg);
426 hf->voice_recognition_pending = enabled;
427 hf->pending = dbus_message_ref(msg);
428 hf->driver->voice_recognition(hf, enabled, voicerec_set_cb, hf);
429 } else if (g_str_equal(name, "EchoCancelingNoiseReduction") == TRUE) {
431 if (!(hf->ag_features & HFP_AG_FEATURE_ECNR))
432 return __ofono_error_not_supported(msg);
434 if (!hf->driver->disable_nrec || enabled == TRUE)
435 return __ofono_error_not_implemented(msg);
437 if (hf->nrec == FALSE)
438 return dbus_message_new_method_return(msg);
440 hf->pending = dbus_message_ref(msg);
441 hf->driver->disable_nrec(hf, nrec_set_cb, hf);
443 return __ofono_error_invalid_args(msg);
448 static void request_phone_number_cb(const struct ofono_error *error,
449 const struct ofono_phone_number *number,
452 struct ofono_handsfree *hf = data;
454 const char *phone_number;
456 if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
457 DBG("Phone number request callback returned error: %s",
458 telephony_error_to_str(error));
460 reply = __ofono_error_failed(hf->pending);
461 __ofono_dbus_pending_reply(&hf->pending, reply);
465 phone_number = phone_number_to_string(number);
466 reply = dbus_message_new_method_return(hf->pending);
467 dbus_message_append_args(reply, DBUS_TYPE_STRING, &phone_number,
469 __ofono_dbus_pending_reply(&hf->pending, reply);
472 static DBusMessage *handsfree_request_phone_number(DBusConnection *conn,
473 DBusMessage *msg, void *data)
475 struct ofono_handsfree *hf = data;
478 return __ofono_error_busy(msg);
480 if (!hf->driver->request_phone_number)
481 return __ofono_error_not_supported(msg);
483 hf->pending = dbus_message_ref(msg);
484 hf->driver->request_phone_number(hf, request_phone_number_cb, hf);
489 static const GDBusMethodTable handsfree_methods[] = {
490 { GDBUS_METHOD("GetProperties",
491 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
492 handsfree_get_properties) },
493 { GDBUS_ASYNC_METHOD("SetProperty",
494 GDBUS_ARGS({ "property", "s" }, { "value", "v" }),
495 NULL, handsfree_set_property) },
496 { GDBUS_ASYNC_METHOD("RequestPhoneNumber",
497 NULL, GDBUS_ARGS({ "number", "s" }),
498 handsfree_request_phone_number) },
502 static const GDBusSignalTable handsfree_signals[] = {
503 { GDBUS_SIGNAL("PropertyChanged",
504 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
508 static void handsfree_remove(struct ofono_atom *atom)
510 struct ofono_handsfree *hf = __ofono_atom_get_data(atom);
512 DBG("atom: %p", atom);
517 if (hf->driver != NULL && hf->driver->remove != NULL)
518 hf->driver->remove(hf);
523 struct ofono_handsfree *ofono_handsfree_create(struct ofono_modem *modem,
528 struct ofono_handsfree *hf;
534 hf = g_try_new0(struct ofono_handsfree, 1);
538 hf->atom = __ofono_modem_add_atom(modem,
539 OFONO_ATOM_TYPE_HANDSFREE,
540 handsfree_remove, hf);
543 for (l = g_drivers; l; l = l->next) {
544 const struct ofono_handsfree_driver *drv = l->data;
546 if (g_strcmp0(drv->name, driver))
549 if (drv->probe(hf, vendor, data) < 0)
559 static void handsfree_unregister(struct ofono_atom *atom)
561 DBusConnection *conn = ofono_dbus_get_connection();
562 struct ofono_modem *modem = __ofono_atom_get_modem(atom);
563 const char *path = __ofono_atom_get_path(atom);
564 struct ofono_handsfree *hf = __ofono_atom_get_data(atom);
567 DBusMessage *reply = __ofono_error_failed(hf->pending);
568 __ofono_dbus_pending_reply(&hf->pending, reply);
571 g_slist_foreach(hf->subscriber_numbers, (GFunc) g_free, NULL);
572 g_slist_free(hf->subscriber_numbers);
573 hf->subscriber_numbers = NULL;
575 ofono_modem_remove_interface(modem, OFONO_HANDSFREE_INTERFACE);
576 g_dbus_unregister_interface(conn, path,
577 OFONO_HANDSFREE_INTERFACE);
580 void ofono_handsfree_register(struct ofono_handsfree *hf)
582 DBusConnection *conn = ofono_dbus_get_connection();
583 struct ofono_modem *modem = __ofono_atom_get_modem(hf->atom);
584 const char *path = __ofono_atom_get_path(hf->atom);
586 if (!g_dbus_register_interface(conn, path,
587 OFONO_HANDSFREE_INTERFACE,
588 handsfree_methods, handsfree_signals,
590 ofono_error("Could not create %s interface",
591 OFONO_HANDSFREE_INTERFACE);
596 ofono_modem_add_interface(modem, OFONO_HANDSFREE_INTERFACE);
598 __ofono_atom_register(hf->atom, handsfree_unregister);
601 int ofono_handsfree_driver_register(const struct ofono_handsfree_driver *d)
603 DBG("driver: %p, name: %s", d, d->name);
605 if (d->probe == NULL)
608 g_drivers = g_slist_prepend(g_drivers, (void *) d);
613 void ofono_handsfree_driver_unregister(
614 const struct ofono_handsfree_driver *d)
616 DBG("driver: %p, name: %s", d, d->name);
618 g_drivers = g_slist_remove(g_drivers, (void *) d);
621 void ofono_handsfree_remove(struct ofono_handsfree *hf)
623 __ofono_atom_free(hf->atom);
626 void ofono_handsfree_set_data(struct ofono_handsfree *hf, void *data)
628 hf->driver_data = data;
631 void *ofono_handsfree_get_data(struct ofono_handsfree *hf)
633 return hf->driver_data;