2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <vconf-keys.h>
25 #include "bt-common.h"
26 #include "bluetooth-telephony-api.h"
27 #include "bt-internal-types.h"
29 #define BT_SCO_TIMEOUT 3000
31 #define BT_CVSD_CODEC_ID 1
32 #define BT_MSBC_CODEC_ID 2
35 GDBusConnection *conn;
37 GDBusProxy *dbus_proxy;
38 GDBusProxy *manager_proxy;
39 } telephony_dbus_info_t;
42 bt_telephony_func_ptr cb;
43 unsigned int call_count;
45 char address[BT_ADDRESS_STR_LEN];
46 char call_path[BT_AUDIO_CALL_PATH_LEN];
47 bluetooth_headset_state_t headset_state;
49 } bt_telephony_info_t;
52 static char *src_addr = NULL;
53 static guint owner_id = 0;
55 #define BLUETOOTH_TELEPHONY_ERROR (__bluetooth_telephony_error_quark())
57 #define BLUEZ_SERVICE_NAME "org.bluez"
58 #define BLUEZ_HEADSET_INTERFACE "org.bluez.Headset"
60 #define BLUEZ_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager"
61 #define BLUEZ_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
62 #define BLUEZ_ADAPTER_INTERFACE "org.bluez.Adapter1"
63 #define BLUEZ_DEVICE_INTERFACE "org.bluez.Device1"
64 #define HFP_AGENT_SERVICE "org.bluez.ag_agent"
67 #define HFP_AGENT_PATH "/org/bluez/hfp_agent"
68 #define HFP_AGENT_INTERFACE "Org.Hfp.App.Interface"
70 #define CSD_CALL_APP_PATH "/org/tizen/csd/%d"
71 #define HFP_NREC_STATUS_CHANGE "NrecStatusChanged"
72 #define HFP_ANSWER_CALL "Answer"
73 #define HFP_REJECT_CALL "Reject"
74 #define HFP_RELEASE_CALL "Release"
75 #define HFP_THREEWAY_CALL "Threeway"
77 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
79 /*Below Inrospection data is exposed to bluez from agent*/
80 static const gchar bt_telephony_introspection_xml[] =
82 " <interface name='org.tizen.csd.Call.Instance'>"
83 " <method name='SendDtmf'>"
84 " <arg type='s' name='dtmf' direction='in'/>"
86 " <method name='VendorCmd'>"
87 " <arg type='s' name='vendor' direction='in'/>"
92 #define BT_TELEPHONY_CHECK_ENABLED() \
94 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) { \
95 BT_ERR("BT is not enabled"); \
96 return BLUETOOTH_TELEPHONY_ERROR_NOT_ENABLED; \
100 static gboolean is_initialized = FALSE;
101 #define BT_TELEPHONY_CHECK_INITIALIZED() \
103 if (is_initialized == FALSE) { \
104 BT_ERR("Bluetooth telephony not initilized"); \
105 return BLUETOOTH_TELEPHONY_ERROR_NOT_INITIALIZED; \
109 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
110 const gchar *sender_name,
111 const gchar *object_path,
112 const gchar *interface_name,
113 const gchar *signal_name,
114 GVariant *parameters,
117 static int __bt_telephony_get_src_addr(GVariant *value);
119 static bt_telephony_info_t telephony_info;
120 static telephony_dbus_info_t telephony_dbus_info;
121 static gboolean is_active = FALSE;
123 /*Function Declaration*/
124 static int __bt_telephony_get_error(const char *error_message);
125 static void __bt_telephony_event_cb(int event, int result, void *param_data);
126 static GQuark __bluetooth_telephony_error_quark(void);
127 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
128 const char *interface, const char *method,
129 GError **err, GVariant *parameters);
130 static int __bluetooth_telephony_send_call_status(
131 bt_telephony_call_status_t call_status,
132 unsigned int call_id, const char *ph_number);
133 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
134 bluetooth_telephony_error_t error, const char *err_msg);
136 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
137 const gchar *sender_name,
138 const gchar *object_path,
139 const gchar *interface_name,
140 const gchar *signal_name,
141 GVariant *parameters,
144 static int __bluetooth_telephony_register(void);
145 static int __bluetooth_telephony_unregister(void);
147 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class);
148 static int __bluetooth_telephony_get_connected_device(void);
149 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void);
151 /*Function Definition*/
152 static void __bt_telephony_method(GDBusConnection *connection,
154 const gchar *object_path,
155 const gchar *interface_name,
156 const gchar *method_name,
157 GVariant *parameters,
158 GDBusMethodInvocation *invocation,
163 BT_INFO("method %s", method_name);
164 BT_INFO("object_path %s", object_path);
166 if (g_strcmp0(method_name, "SendDtmf") == 0) {
168 telephony_event_dtmf_t call_data = { 0, };
170 g_variant_get(parameters, "(&s)", &dtmf);
173 BT_ERR("Number dial failed");
174 __bluetooth_telephony_error(invocation,
175 BLUETOOTH_TELEPHONY_ERROR_INVALID_DTMF,
178 DBG_SECURE("Dtmf = %s", dtmf);
180 call_data.dtmf = g_strdup(dtmf);
181 __bt_telephony_event_cb(
182 BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF,
183 BLUETOOTH_TELEPHONY_ERROR_NONE,
186 g_free(call_data.dtmf);
188 g_dbus_method_invocation_return_value(invocation, NULL);
190 } else if (g_strcmp0(method_name, "VendorCmd") == 0) {
193 g_variant_get(parameters, "(&s)", &at_cmd);
194 BT_INFO("Vendor %s", at_cmd);
195 if (at_cmd == NULL) {
196 BT_ERR("Vendor command is NULL\n");
197 __bluetooth_telephony_error(invocation,
198 BLUETOOTH_TELEPHONY_ERROR_APPLICATION,
199 "Invalid at vendor cmd");
201 DBG_SECURE("Vendor AT cmd = %s", at_cmd);
203 __bt_telephony_event_cb(
204 BLUETOOTH_EVENT_TELEPHONY_VENDOR_AT_CMD,
205 BLUETOOTH_TELEPHONY_ERROR_NONE,
208 g_dbus_method_invocation_return_value(invocation, NULL);
215 static const GDBusInterfaceVTable method_table = {
216 __bt_telephony_method,
221 static int __bt_telephony_get_error(const char *error_message)
223 if (error_message == NULL) {
224 BT_ERR("Error message NULL");
225 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
228 BT_ERR("Error message = %s", error_message);
229 if (g_strcmp0(error_message, "NotAvailable") == 0)
230 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
231 else if (g_strcmp0(error_message, "NotConnected") == 0)
232 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
233 else if (g_strcmp0(error_message, "InProgress") == 0)
234 return BLUETOOTH_TELEPHONY_ERROR_BUSY;
235 else if (g_strcmp0(error_message, "InvalidArguments") == 0)
236 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
237 else if (g_strcmp0(error_message, "AlreadyExists") == 0)
238 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_EXSIST;
239 else if (g_strcmp0(error_message, "Already Connected") == 0)
240 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
241 else if (g_strcmp0(error_message, "No memory") == 0)
242 return BLUETOOTH_TELEPHONY_ERROR_NO_MEMORY;
243 else if (g_strcmp0(error_message, "I/O error") == 0)
244 return BLUETOOTH_TELEPHONY_ERROR_I_O_ERROR;
245 else if (g_strcmp0(error_message,
246 "Operation currently not available") == 0)
247 return BLUETOOTH_TELEPHONY_ERROR_OPERATION_NOT_AVAILABLE;
248 else if (g_strrstr(error_message, BT_ACCESS_DENIED_MSG))
249 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
251 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
254 static int __bt_telephony_check_privilege(void)
261 reply = __bluetooth_telephony_dbus_method_send(
262 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
263 "CheckPrivilege", &err, NULL);
266 BT_ERR("Error returned in method call");
268 g_dbus_error_strip_remote_error(err);
269 ret = __bt_telephony_get_error(err->message);
273 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
275 g_variant_unref(reply);
278 return BLUETOOTH_TELEPHONY_ERROR_NONE;
281 static void __bt_telephony_event_cb(int event, int result, void *param_data)
283 telephony_event_param_t bt_event = { 0, };
285 bt_event.event = event;
286 bt_event.result = result;
287 bt_event.param_data = param_data;
289 ret_if(telephony_info.cb == NULL);
290 telephony_info.cb(bt_event.event, &bt_event, telephony_info.user_data);
294 static GQuark __bluetooth_telephony_error_quark(void)
296 static GQuark quark = 0;
298 quark = g_quark_from_static_string("telephony");
303 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
304 const char *interface, const char *method,
305 GError **err, GVariant *parameters)
309 if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
314 GDBusConnection *conn;
318 conn = telephony_dbus_info.conn;
319 retv_if(conn == NULL, NULL);
321 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
322 NULL, HFP_AGENT_SERVICE, path, interface, NULL, err);
324 BT_ERR("Unable to allocate new proxy");
328 reply = g_dbus_proxy_call_sync(proxy, method, parameters,
329 G_DBUS_CALL_FLAGS_NONE, timeout, NULL, err);
331 g_object_unref(proxy);
336 static int __bluetooth_telephony_send_call_status(
337 bt_telephony_call_status_t call_status,
338 unsigned int call_id, const char *ph_number)
343 char *path = g_strdup(telephony_info.call_path);
349 if (NULL == ph_number)
350 phone_number = g_strdup("");
352 phone_number = g_strdup(ph_number);
354 param = g_variant_new("(ssii)", path, phone_number,
355 call_status, call_id);
356 reply = __bluetooth_telephony_dbus_method_send(
357 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
358 "ChangeCallStatus", &err, param);
361 g_free(phone_number);
364 BT_ERR("Error returned in method call");
366 g_dbus_error_strip_remote_error(err);
367 ret = __bt_telephony_get_error(err->message);
371 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
374 g_variant_unref(reply);
376 return BLUETOOTH_TELEPHONY_ERROR_NONE;
379 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
380 bluetooth_telephony_error_t error, const char *err_msg)
382 g_dbus_method_invocation_return_error(invocation,
383 BLUETOOTH_TELEPHONY_ERROR, error,
387 static void __bluetooth_telephony_answer_call(GVariant *var)
389 telephony_event_callid_t call_data = { 0, };
394 g_variant_get(var, "(u)", &callid);
395 BT_DBG("call_id = [%d]", callid);
396 call_data.callid = callid;
398 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL,
399 BLUETOOTH_TELEPHONY_ERROR_NONE,
404 static void __bluetooth_telephony_release_call(GVariant *var)
406 telephony_event_callid_t call_data = { 0, };
411 g_variant_get(var, "(u)", &callid);
412 BT_DBG("call_id = [%d]", callid);
413 call_data.callid = callid;
415 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL,
416 BLUETOOTH_TELEPHONY_ERROR_NONE,
421 static void __bluetooth_telephony_reject_call(GVariant *var)
423 telephony_event_callid_t call_data = { 0, };
428 g_variant_get(var, "(u)", &callid);
429 BT_DBG("call_id = [%d]", callid);
430 call_data.callid = callid;
432 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL,
433 BLUETOOTH_TELEPHONY_ERROR_NONE,
438 static void __bluetooth_telephony_threeway_call(GVariant *var)
441 unsigned int chld_value;
445 g_variant_get(var, "(u)", &chld_value);
446 BT_DBG("chld value = [%d]", chld_value);
448 switch (chld_value) {
450 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL;
453 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL;
456 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL;
459 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL;
462 BT_ERR("Invalid CHLD command");
466 __bt_telephony_event_cb(event,
467 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
471 static void __bluetooth_handle_nrec_status_change(GVariant *var)
473 gboolean status = FALSE;
475 g_variant_get(var, "(b)", &status);
476 BT_INFO("NREC status = %d", status);
478 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED,
479 BLUETOOTH_TELEPHONY_ERROR_NONE, (void *)&status);
483 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
484 const gchar *sender_name,
485 const gchar *object_path,
486 const gchar *interface_name,
487 const gchar *signal_name,
488 GVariant *parameters,
493 if (strcasecmp(interface_name, BLUEZ_HEADSET_INTERFACE) == 0) {
494 if (strcasecmp(signal_name, "PropertyChanged") == 0) {
498 g_variant_get(parameters, "(&sv)", &property, &values);
499 BT_DBG("Property: %s", property);
501 if (strcasecmp(property, "State") == 0) {
503 state = (gchar *)g_variant_get_string(values, NULL);
506 BT_ERR("State is null");
509 BT_DBG("state: %s", state);
510 if (g_strcmp0(state, "connected") == 0) {
511 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
512 } else if (g_strcmp0(state, "playing") == 0) {
513 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
514 } else if (g_strcmp0(state, "disconnected") == 0) {
515 /* Headset state: playing -> disconnected */
516 if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING) {
517 __bt_telephony_event_cb(
518 BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
519 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
522 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
524 } else if (strcasecmp(property, "Connected") == 0) {
525 gboolean connected = FALSE;
526 char *dev_addr = NULL;
527 connected = g_variant_get_boolean(values);
528 BT_INFO("connected %d", connected);
530 /*Get device address*/
531 if (object_path != NULL)
532 dev_addr = strstr(object_path, "dev_");
534 if (dev_addr != NULL) {
536 g_strlcpy(telephony_info.address,
538 sizeof(telephony_info.address));
539 g_strdelimit(telephony_info.address, "_", ':');
540 BT_DBG("address is %s",
541 telephony_info.address);
543 telephony_info.headset_state =
544 BLUETOOTH_STATE_CONNECTED;
546 if (telephony_dbus_info.proxy != NULL) {
547 g_object_unref(telephony_dbus_info.proxy);
548 telephony_dbus_info.proxy = NULL;
551 telephony_dbus_info.proxy =
552 __bluetooth_telephony_get_connected_device_proxy();
554 BT_INFO("Headset Connected");
556 __bt_telephony_event_cb(
557 BLUETOOTH_EVENT_TELEPHONY_HFP_CONNECTED,
558 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
560 } else { /*Device disconnected*/
561 memset(telephony_info.address, 0x00,
562 sizeof(telephony_info.address));
563 telephony_info.headset_state =
564 BLUETOOTH_STATE_DISCONNETED;
566 if (telephony_dbus_info.proxy != NULL) {
567 g_object_unref(telephony_dbus_info.proxy);
568 telephony_dbus_info.proxy = NULL;
571 BT_INFO("Headset Disconnected");
573 __bt_telephony_event_cb(
574 BLUETOOTH_EVENT_TELEPHONY_HFP_DISCONNECTED,
575 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
577 } else if (strcasecmp(property, "SpeakerGain") == 0) {
578 unsigned int spkr_gain;
579 guint16 gain = g_variant_get_uint16(values);
581 spkr_gain = (unsigned int)gain;
582 BT_DBG("spk_gain[%d]", spkr_gain);
584 __bt_telephony_event_cb(
585 BLUETOOTH_EVENT_TELEPHONY_SET_SPEAKER_GAIN,
586 BLUETOOTH_TELEPHONY_ERROR_NONE,
588 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
589 unsigned int mic_gain;
590 guint16 gain = g_variant_get_uint16(values);
592 mic_gain = (unsigned int)gain;
593 BT_DBG("mic_gain[%d]", mic_gain);
595 __bt_telephony_event_cb(
596 BLUETOOTH_EVENT_TELEPHONY_SET_MIC_GAIN,
597 BLUETOOTH_TELEPHONY_ERROR_NONE,
599 } else if (strcasecmp(property, "Playing") == 0) {
600 gboolean audio_sink_playing;
602 audio_sink_playing = g_variant_get_boolean(values);
603 if (audio_sink_playing) {
604 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
605 __bt_telephony_event_cb(
606 BLUETOOTH_EVENT_TELEPHONY_AUDIO_CONNECTED,
607 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
609 telephony_info.headset_state =
610 BLUETOOTH_STATE_CONNECTED;
611 __bt_telephony_event_cb(
612 BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
613 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
617 g_variant_unref(values);
618 } else if (strcasecmp(signal_name, HFP_NREC_STATUS_CHANGE) == 0)
619 __bluetooth_handle_nrec_status_change(parameters);
620 else if (strcasecmp(signal_name, HFP_ANSWER_CALL) == 0)
621 __bluetooth_telephony_answer_call(parameters);
622 else if (strcasecmp(signal_name, HFP_REJECT_CALL) == 0)
623 __bluetooth_telephony_reject_call(parameters);
624 else if (strcasecmp(signal_name, HFP_RELEASE_CALL) == 0)
625 __bluetooth_telephony_release_call(parameters);
626 else if (strcasecmp(signal_name, HFP_THREEWAY_CALL) == 0)
627 __bluetooth_telephony_threeway_call(parameters);
632 static GDBusNodeInfo *__bt_telephony_create_method_node_info
633 (const gchar *introspection_data)
636 GDBusNodeInfo *node_info = NULL;
638 if (introspection_data == NULL)
641 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
644 BT_ERR("Unable to create node: %s", err->message);
651 int __bluetooth_telephony_register_object(int reg, GDBusNodeInfo *node_info)
653 static guint bt_tel_id = 0;
654 GError *error = NULL;
658 if (node_info == NULL)
659 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
661 path = g_strdup(telephony_info.call_path);
663 bt_tel_id = g_dbus_connection_register_object(telephony_dbus_info.conn,
664 path, node_info->interfaces[0],
669 if (bt_tel_id == 0) {
670 BT_ERR("Failed to register: %s", error->message);
672 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
676 g_dbus_connection_unregister_object(telephony_dbus_info.conn,
682 return BLUETOOTH_TELEPHONY_ERROR_NONE;
685 static int __bluetooth_telephony_proxy_init(void)
688 GDBusNodeInfo *node_info;
691 name = g_strdup_printf("org.tizen.csd.Call.Instance.p%d", getpid());
693 owner_id = g_bus_own_name_on_connection(telephony_dbus_info.conn,
694 name, G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
695 BT_DBG("well-known name: %s, owner_id: %d", name, owner_id);
698 node_info = __bt_telephony_create_method_node_info(
699 bt_telephony_introspection_xml);
700 if (node_info == NULL) {
701 BT_ERR("node_info NULL");
702 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
704 if (__bluetooth_telephony_register_object(TRUE, node_info) !=
705 BLUETOOTH_TELEPHONY_ERROR_NONE) {
706 BT_ERR("Registation of Method Failed");
707 g_dbus_node_info_unref(node_info);
708 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
712 g_dbus_node_info_unref(node_info);
713 return BLUETOOTH_TELEPHONY_ERROR_NONE;
716 static void __bluetooth_telephony_proxy_deinit(void)
720 __bluetooth_telephony_register_object(FALSE, NULL);
721 g_bus_unown_name(owner_id);
726 static int __bluetooth_telephony_register(void)
731 char *path = g_strdup(telephony_info.call_path);
736 param = g_variant_new("(ss)", path, src_addr);
737 BT_DBG("Path[%s] Src_Address[%s]", path, src_addr);
739 reply = __bluetooth_telephony_dbus_method_send(
740 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
741 "RegisterApplication", &err, param);
746 BT_ERR("Error returned in method call");
748 g_dbus_error_strip_remote_error(err);
749 ret = __bt_telephony_get_error(err->message);
750 BT_ERR("Error here %d\n", ret);
754 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
757 g_variant_unref(reply);
758 BT_DBG("__bluetooth_telephony_register completed");
760 return BLUETOOTH_TELEPHONY_ERROR_NONE;
763 static int __bluetooth_telephony_unregister(void)
768 char *path = g_strdup(telephony_info.call_path);
773 param = g_variant_new("(s)", path);
774 reply = __bluetooth_telephony_dbus_method_send(
775 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
776 "UnregisterApplication", &err, param);
781 BT_ERR("Error returned in method call");
783 g_dbus_error_strip_remote_error(err);
784 ret = __bt_telephony_get_error(err->message);
788 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
791 g_variant_unref(reply);
792 BT_DBG("__bluetooth_telephony_unregister completed");
794 return BLUETOOTH_TELEPHONY_ERROR_NONE;
797 static void __bluetooth_telephony_init_headset_state(void)
801 gboolean status = FALSE;
805 if (telephony_dbus_info.conn == NULL) {
806 BT_ERR("Bluetooth telephony not initilized");
810 reply = __bluetooth_telephony_dbus_method_send(
811 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
812 "IsConnected", &err, NULL);
814 BT_ERR("Error returned in method call\n");
816 BT_ERR("Error message = %s", err->message);
822 g_variant_get(reply, "(b)", &status);
823 g_variant_unref(reply);
825 BT_INFO("Headset Connected Status = [%d]", status);
827 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
831 if (bluetooth_telephony_is_sco_connected())
832 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
837 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class)
839 gboolean flag = FALSE;
842 switch ((device_class & 0x1f00) >> 8) {
844 switch ((device_class & 0xfc) >> 2) {
862 /* Tizen Wearable device */
864 switch ((device_class & 0xfc) >> 2) {
865 case 0x01: /* Wrist Watch */
873 BT_DBG("[%d]", flag);
878 static gboolean __bluetooth_telephony_is_headset_by_uuid(gchar **uuids)
882 unsigned int service = 0;
886 retv_if(uuids == NULL, FALSE);
888 for (i = 0; uuids[i] != NULL; i++) {
889 parts = g_strsplit(uuids[i], "-", -1);
891 if (parts == NULL || parts[0] == NULL) {
896 service = g_ascii_strtoull(parts[0], NULL, 16);
899 if (service == BLUETOOTH_HS_PROFILE_UUID ||
900 service == BLUETOOTH_HF_PROFILE_UUID)
909 static int __bluetooth_telephony_get_connected_device(void)
911 GDBusConnection *conn;
912 GDBusProxy *headset_agent_proxy = NULL;
913 GDBusProxy *manager_proxy = NULL;
914 GDBusProxy *proxy = NULL;
915 GVariant *reply = NULL;
916 GVariant *getall = NULL;
917 GVariant *isPlayingReply = NULL;
918 GVariant *isConnectedReply = NULL;
919 GVariant *param = NULL;
920 GVariant *var_path = NULL;
921 GVariant *path_values = NULL;
922 GVariant *value = NULL;
923 GError *error = NULL;
925 GVariantIter iter_path;
926 GVariantIter property_iter;
927 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
930 conn = _bt_get_system_private_conn();
931 retv_if(conn == NULL, BLUETOOTH_TELEPHONY_ERROR_INTERNAL);
933 manager_proxy = g_dbus_proxy_new_sync(
934 conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
935 BLUEZ_SERVICE_NAME, "/",
936 BLUEZ_MANAGER_INTERFACE, NULL, &error);
937 if (manager_proxy == NULL) {
938 BT_ERR("Unable to allocate new proxy \n");
939 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
941 g_dbus_error_strip_remote_error(error);
942 ret = __bt_telephony_get_error(error->message);
943 BT_ERR("Error here %d\n", ret);
949 /* Synchronous call */
950 reply = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects", NULL,
951 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
952 g_object_unref(manager_proxy);
955 BT_ERR("Can't get managed objects");
956 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
958 g_dbus_error_strip_remote_error(error);
959 ret = __bt_telephony_get_error(error->message);
960 BT_ERR("Error here %d\n", ret);
966 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
967 g_variant_iter_init(&iter, reply);
969 while ((param = g_variant_iter_next_value(&iter))) {
970 g_variant_iter_init(&iter_path, param);
972 while ((var_path = g_variant_iter_next_value(&iter_path))) {
974 uint32_t device_class = 0;
975 gboolean playing = FALSE;
976 gboolean connected = FALSE;
977 char *object_path = NULL;
978 gchar *address = NULL;
980 gchar **uuids = NULL;
981 GVariant *getall_param = NULL;
983 g_variant_get(var_path, "{&o*}", &object_path,
985 g_variant_unref(path_values); /* path_values unused*/
987 proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
988 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
989 BLUEZ_SERVICE_NAME, object_path,
990 BLUEZ_PROPERTIES_INTERFACE, NULL, &error);
992 BT_ERR("Unable to allocate new proxy \n");
993 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
995 g_dbus_error_strip_remote_error(error);
996 ret = __bt_telephony_get_error(error->message);
997 BT_ERR("Error here %d\n", ret);
1004 getall_param = g_variant_new("s", BLUEZ_DEVICE_INTERFACE);
1005 getall = g_dbus_proxy_call_sync(proxy,
1006 "GetAll", getall_param,
1007 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1008 g_object_unref(proxy);
1011 BT_ERR("Can't get managed objects");
1012 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1014 g_dbus_error_strip_remote_error(error);
1015 ret = __bt_telephony_get_error(error->message);
1016 BT_ERR("Error here %d\n", ret);
1017 g_error_free(error);
1022 g_variant_iter_init(&property_iter, getall);
1024 while (g_variant_iter_loop(&property_iter, "{&sv}", &key, &value)) {
1025 if (!g_strcmp0(key, "Class")) {
1026 device_class = g_variant_get_uint32(value);
1027 BT_DBG("Device Class: %d", device_class);
1028 } else if (!g_strcmp0(key, "UUID")) {
1030 uuids = (gchar **)g_variant_get_strv(value, &len);
1031 BT_DBG_UUID(uuids, len, i);
1032 } else if (!g_strcmp0(key, "Address")) {
1033 address = (gchar *)g_variant_get_string(
1036 BT_DBG("Device Class: %s", address);
1038 g_variant_unref(value);
1040 g_variant_unref(getall);
1042 if (device_class == 0) {
1043 BT_DBG("COD is NULL (maybe paired by nfc)... Checking UUIDs");
1044 if (!__bluetooth_telephony_is_headset_by_uuid(uuids)) {
1045 BT_DBG("UUID checking completed. None HF device");
1048 BT_DBG("UUID checking completed. HF device");
1050 if (!__bluetooth_telephony_is_headset(device_class))
1054 /* this is headset; Check for Connection */
1055 headset_agent_proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1056 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1057 HFP_AGENT_SERVICE, object_path,
1058 HFP_AGENT_INTERFACE, NULL, &error);
1059 if (headset_agent_proxy == NULL) {
1060 BT_ERR("Unable to allocate new headset_agent_proxy");
1061 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1063 g_dbus_error_strip_remote_error(error);
1064 ret = __bt_telephony_get_error(error->message);
1065 BT_ERR("Error here %d\n", ret);
1066 g_error_free(error);
1071 isConnectedReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1072 "IsConnected", NULL,
1073 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1076 if (!isConnectedReply) {
1077 BT_ERR("Can't get managed objects");
1078 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1080 g_dbus_error_strip_remote_error(error);
1081 ret = __bt_telephony_get_error(error->message);
1082 BT_ERR("Error here %d\n", ret);
1083 g_error_free(error);
1087 connected = g_variant_get_boolean(isConnectedReply);
1088 g_variant_unref(isConnectedReply);
1091 g_strlcpy(telephony_info.address,
1093 sizeof(telephony_info.address));
1095 isPlayingReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1097 G_DBUS_CALL_FLAGS_NONE,
1099 if (!isPlayingReply) {
1100 BT_ERR("Can't get managed objects");
1101 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1103 g_dbus_error_strip_remote_error(error);
1104 ret = __bt_telephony_get_error(error->message);
1105 BT_ERR("Error here %d\n", ret);
1106 g_error_free(error);
1109 playing = g_variant_get_boolean(isPlayingReply);
1110 g_variant_unref(isPlayingReply);
1113 telephony_info.headset_state =
1114 BLUETOOTH_STATE_PLAYING;
1116 telephony_info.headset_state =
1117 BLUETOOTH_STATE_CONNECTED;
1124 g_object_unref(headset_agent_proxy);
1125 g_variant_unref(var_path);
1127 g_variant_unref(param);
1131 if (headset_agent_proxy)
1132 g_object_unref(headset_agent_proxy);
1134 g_variant_unref(reply);
1136 g_variant_unref(var_path);
1138 g_variant_unref(param);
1143 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void)
1145 GDBusProxy *proxy = NULL;
1146 GError *error = NULL;
1150 if (strlen(telephony_info.address) == 0)
1151 __bluetooth_telephony_get_connected_device();
1153 if (strlen(telephony_info.address) == 0)
1156 proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1157 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1158 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1159 HFP_AGENT_INTERFACE, NULL, &error);
1160 if (proxy == NULL) {
1161 BT_ERR("Unable to allocate new proxy");
1163 g_dbus_error_strip_remote_error(error);
1164 ret = __bt_telephony_get_error(error->message);
1165 BT_ERR("Error here %d\n", ret);
1166 g_error_free(error);
1175 int __bt_telephony_subscribe_adapter_signal(GDBusConnection *conn,
1181 static guint subscribe_adapter_id = 0;
1183 if (subscribe == TRUE) {
1184 if (subscribe_adapter_id == 0) {
1185 subscribe_adapter_id = g_dbus_connection_signal_subscribe(conn,
1186 NULL, "org.freedesktop.DBus.ObjectManager",
1187 "InterfacesAdded", NULL, NULL, 0,
1188 __bt_telephony_adapter_filter,
1191 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1193 if (subscribe_adapter_id > 0) {
1194 g_dbus_connection_signal_unsubscribe(conn,
1195 subscribe_adapter_id);
1196 subscribe_adapter_id = 0;
1198 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1202 int __bt_telephony_event_subscribe_signal(GDBusConnection *conn,
1208 static guint event_id = 0;
1210 if (subscribe == TRUE) {
1211 if (event_id == 0) {
1212 event_id = g_dbus_connection_signal_subscribe(conn,
1213 NULL, BLUEZ_HEADSET_INTERFACE,
1214 NULL, NULL, NULL, 0,
1215 __bluetooth_telephony_event_filter,
1218 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1221 g_dbus_connection_signal_unsubscribe(conn,
1225 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1229 BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
1232 bluetooth_device_address_t loc_address = { {0} };
1233 char src_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1234 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
1235 GError *error = NULL;
1239 if (is_initialized == TRUE) {
1240 BT_ERR("Bluetooth telephony already initilized");
1241 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1244 is_initialized = TRUE;
1246 /* As a result of discussion with system team, private connection is not
1247 * suitable in here. It is better to use shared connection. */
1248 //telephony_dbus_info.conn = _bt_gdbus_init_system_gconn();
1249 telephony_dbus_info.conn = _bt_get_system_shared_conn();
1250 if (!telephony_dbus_info.conn) {
1251 is_initialized = FALSE;
1252 BT_ERR("Could not get DBus Connection");
1253 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1257 snprintf(telephony_info.call_path, sizeof(telephony_info.call_path),
1258 CSD_CALL_APP_PATH, getpid());
1259 BT_INFO("Call Path = %s", telephony_info.call_path);
1260 memset(telephony_info.address, 0x00, sizeof(telephony_info.address));
1262 if (__bluetooth_telephony_proxy_init()) {
1263 BT_ERR("__bluetooth_telephony_proxy_init failed\n");
1264 telephony_dbus_info.conn = NULL;
1265 is_initialized = FALSE;
1266 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1269 telephony_dbus_info.manager_proxy = g_dbus_proxy_new_sync(
1270 telephony_dbus_info.conn,
1271 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1272 BLUEZ_SERVICE_NAME, "/",
1273 BLUEZ_MANAGER_INTERFACE, NULL, &error);
1274 if (telephony_dbus_info.manager_proxy == NULL) {
1275 BT_ERR("Could not create a manager proxy");
1276 __bluetooth_telephony_proxy_deinit();
1277 telephony_dbus_info.conn = NULL;
1278 is_initialized = FALSE;
1280 g_dbus_error_strip_remote_error(error);
1281 ret = __bt_telephony_get_error(error->message);
1282 BT_ERR("Error here %d\n", ret);
1283 g_error_free(error);
1286 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1289 telephony_dbus_info.dbus_proxy = g_dbus_proxy_new_sync(
1290 telephony_dbus_info.conn,
1291 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1292 BT_EVENT_FREEDESKTOP, BT_FREEDESKTOP_PATH,
1293 BT_EVENT_FREEDESKTOP, NULL, &error);
1294 if (NULL == telephony_dbus_info.dbus_proxy) {
1295 __bluetooth_telephony_proxy_deinit();
1296 telephony_dbus_info.conn = NULL;
1297 g_object_unref(telephony_dbus_info.manager_proxy);
1298 telephony_dbus_info.manager_proxy = NULL;
1299 is_initialized = FALSE;
1300 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1303 if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, TRUE) != 0) {
1304 BT_ERR("Fail to Subscribe Adapter Signal");
1308 /*Callback and user applicaton data*/
1309 telephony_info.cb = cb;
1310 telephony_info.user_data = user_data;
1311 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1313 if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, TRUE) != 0) {
1314 BT_ERR("Fail to Subscribe telephony event Signal");
1318 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED)
1319 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1321 /*Bluetooth is active, therefore set the flag */
1324 ret = bluetooth_get_local_address(&loc_address);
1325 if (ret != BLUETOOTH_ERROR_NONE) {
1326 BT_ERR("Fail to get local address\n");
1327 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1330 _bt_convert_addr_type_to_string(src_address, loc_address.addr);
1331 src_addr = g_strdup(src_address);
1333 ret = __bluetooth_telephony_register();
1334 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1335 BT_ERR("__bluetooth_telephony_register failed\n");
1339 if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
1340 __bluetooth_telephony_init_headset_state();
1345 bluetooth_telephony_deinit();
1349 BT_EXPORT_API int bluetooth_telephony_deinit(void)
1352 BT_TELEPHONY_CHECK_INITIALIZED();
1354 is_initialized = FALSE;
1356 if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, FALSE) != 0)
1357 BT_ERR("Fail to UnSubscribe telephony event Signal");
1359 if (bluetooth_check_adapter() != BLUETOOTH_ADAPTER_DISABLED ||
1360 bluetooth_check_adapter_le() != BLUETOOTH_ADAPTER_LE_DISABLED)
1361 __bluetooth_telephony_unregister();
1363 __bluetooth_telephony_proxy_deinit();
1365 telephony_info.cb = NULL;
1366 telephony_info.user_data = NULL;
1367 telephony_info.call_count = 0;
1368 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1370 /* Remove BT enabled signal */
1371 if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, FALSE) != 0)
1372 BT_ERR("Fail to UnSubscribe Adapter event Signal");
1377 if (telephony_dbus_info.manager_proxy != NULL) {
1378 g_object_unref(telephony_dbus_info.manager_proxy);
1379 telephony_dbus_info.manager_proxy = NULL;
1382 if (telephony_dbus_info.conn != NULL)
1383 telephony_dbus_info.conn = NULL;
1385 if (telephony_dbus_info.dbus_proxy != NULL) {
1386 g_object_unref(telephony_dbus_info.dbus_proxy);
1387 telephony_dbus_info.dbus_proxy = NULL;
1391 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1394 BT_EXPORT_API gboolean bluetooth_telephony_is_sco_connected(void)
1398 gboolean status = FALSE;
1402 retv_if(is_initialized == FALSE, FALSE);
1403 retv_if(bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED, FALSE);
1405 reply = __bluetooth_telephony_dbus_method_send(
1406 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1407 "IsPlaying", &err, NULL);
1410 BT_ERR("Error returned in method call\n");
1412 BT_ERR("Error message = %s", err->message);
1417 g_variant_get(reply, "(b)", &status);
1418 g_variant_unref(reply);
1420 if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
1421 if (status == TRUE && telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1422 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
1424 BT_INFO("SCO Connected Status = [%d]", status);
1428 BT_EXPORT_API int bluetooth_telephony_is_nrec_enabled(gboolean *status)
1433 GVariant *param_inner;
1437 BT_TELEPHONY_CHECK_INITIALIZED();
1438 BT_TELEPHONY_CHECK_ENABLED();
1441 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1443 if (telephony_info.headset_state == BLUETOOTH_STATE_DISCONNETED)
1444 return BLUETOOTH_TELEPHONY_ERROR_AUDIO_NOT_CONNECTED;
1446 reply = __bluetooth_telephony_dbus_method_send(
1447 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1448 "GetProperties", &err, NULL);
1451 BT_ERR("Error returned in method call\n");
1453 BT_DBG("Error message = %s", err->message);
1456 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1459 g_variant_iter_init(&iter, reply);
1460 while ((param_inner = g_variant_iter_next_value(&iter))) {
1463 value = g_variant_lookup_value(param_inner,
1464 "nrec", G_VARIANT_TYPE_BOOLEAN);
1466 BT_DBG("Property NREC Found");
1467 *status = g_variant_get_boolean(value);
1468 BT_DBG("NREC status = [%d]", *status);
1469 g_variant_unref(value);
1470 g_variant_unref(param_inner);
1473 g_variant_unref(param_inner);
1475 BT_DBG("NREC status = [%d]", *status);
1476 g_variant_unref(reply);
1479 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1482 BT_EXPORT_API int bluetooth_telephony_is_wbs_mode(gboolean *status)
1488 GVariant *param_inner;
1492 BT_TELEPHONY_CHECK_INITIALIZED();
1493 BT_TELEPHONY_CHECK_ENABLED();
1496 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1500 reply = __bluetooth_telephony_dbus_method_send(
1501 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1502 "GetProperties", &err, NULL);
1505 BT_ERR("Error returned in method call");
1507 BT_ERR("Error message = %s", err->message);
1510 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1513 g_variant_iter_init(&iter, reply);
1514 while ((param_inner = g_variant_iter_next_value(&iter))) {
1517 value = g_variant_lookup_value(param_inner,
1518 "codec", G_VARIANT_TYPE_UINT32);
1520 BT_DBG("Property CODEC Found");
1521 codec = g_variant_get_uint32(value);
1522 g_variant_unref(value);
1523 BT_DBG("Codec = [%d]", codec);
1525 *status = codec == BT_MSBC_CODEC_ID ? TRUE : FALSE;
1526 BT_DBG("NREC status = [%d]", *status);
1527 g_variant_unref(value);
1528 g_variant_unref(param_inner);
1531 g_variant_unref(param_inner);
1534 g_variant_unref(reply);
1535 BT_DBG("MSBC status = [%d]", *status);
1538 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1541 BT_EXPORT_API int bluetooth_telephony_send_vendor_cmd(const char *cmd)
1543 GError *error = NULL;
1544 GVariant *reply, *parameters;
1545 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
1549 BT_TELEPHONY_CHECK_INITIALIZED();
1550 BT_TELEPHONY_CHECK_ENABLED();
1552 BT_DBG("Send Vendor %s", cmd);
1554 if (telephony_dbus_info.proxy == NULL)
1555 telephony_dbus_info.proxy =
1556 __bluetooth_telephony_get_connected_device_proxy();
1558 if (telephony_dbus_info.proxy == NULL)
1559 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1561 parameters = g_variant_new("s", cmd);
1562 reply = g_dbus_proxy_call_sync(telephony_dbus_info.proxy,
1563 "SendVendorAtCmd", parameters,
1564 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1566 g_variant_unref(reply);
1569 g_dbus_error_strip_remote_error(error);
1570 ret = __bt_telephony_get_error(error->message);
1571 BT_ERR("Error here %d\n", ret);
1572 g_error_free(error);
1579 BT_EXPORT_API int bluetooth_telephony_start_voice_recognition(void)
1585 gboolean state = TRUE;
1589 BT_TELEPHONY_CHECK_INITIALIZED();
1590 BT_TELEPHONY_CHECK_ENABLED();
1592 param = g_variant_new("(b)", &state);
1593 reply = __bluetooth_telephony_dbus_method_send(
1594 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1595 "SetVoiceDial", &err, param);
1598 BT_ERR("Error returned in method call\n");
1600 g_dbus_error_strip_remote_error(err);
1601 ret = __bt_telephony_get_error(err->message);
1605 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1608 g_variant_unref(reply);
1610 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1613 BT_EXPORT_API int bluetooth_telephony_stop_voice_recognition(void)
1619 gboolean state = FALSE;
1623 BT_TELEPHONY_CHECK_INITIALIZED();
1624 BT_TELEPHONY_CHECK_ENABLED();
1626 param = g_variant_new("(b)", &state);
1627 reply = __bluetooth_telephony_dbus_method_send(
1628 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1629 "SetVoiceDial", &err, param);
1632 BT_ERR("Error returned in method call\n");
1634 g_dbus_error_strip_remote_error(err);
1635 ret = __bt_telephony_get_error(err->message);
1639 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1642 g_variant_unref(reply);
1645 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1648 static void __bluetooth_telephony_sco_start_cb(GDBusProxy *proxy,
1649 GAsyncResult *res, gpointer user_data)
1651 GError *error = NULL;
1652 GVariant *value = NULL;
1654 value = g_dbus_proxy_call_finish(proxy, res, &error);
1655 if (value == NULL) {
1656 if (error != NULL) {
1657 BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1658 error->code, error->message);
1659 g_clear_error(&error);
1661 BT_ERR("SCo Start Failed");
1663 g_object_unref(proxy);
1667 BT_DBG("sco_start_cb : -");
1668 g_object_unref(proxy);
1669 g_variant_unref(value);
1672 BT_EXPORT_API int bluetooth_telephony_audio_open(void)
1674 GDBusConnection *conn;
1681 BT_TELEPHONY_CHECK_INITIALIZED();
1682 BT_TELEPHONY_CHECK_ENABLED();
1684 /* Because this API is async call, so can't use dbus SMACK */
1685 if (__bt_telephony_check_privilege() ==
1686 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1687 BT_ERR("Don't have a privilege to use this API");
1688 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1691 conn = _bt_get_system_private_conn();
1693 BT_DBG("No System Bus found\n");
1694 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1697 if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING)
1698 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
1700 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1701 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1702 HFP_AGENT_INTERFACE, NULL, &err);
1703 if (proxy == NULL) {
1704 BT_ERR("Unable to allocate new proxy");
1706 g_dbus_error_strip_remote_error(err);
1707 ret = __bt_telephony_get_error(err->message);
1708 BT_ERR("Error here %d\n", ret);
1712 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1715 g_dbus_proxy_call(proxy, "Play", NULL, G_DBUS_CALL_FLAGS_NONE,
1716 -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_start_cb, NULL);
1719 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1722 static void __bluetooth_telephony_sco_close_cb(GDBusProxy *proxy,
1723 GAsyncResult *res, gpointer user_data)
1725 GError *error = NULL;
1726 GVariant *value = NULL;
1728 value = g_dbus_proxy_call_finish(proxy, res, &error);
1729 if (value == NULL) {
1730 if (error != NULL) {
1731 BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1732 error->code, error->message);
1733 g_clear_error(&error);
1735 BT_ERR("SCo close Failed");
1737 g_object_unref(proxy);
1741 BT_DBG("sco_close_cb : -");
1742 g_object_unref(proxy);
1743 g_variant_unref(value);
1745 BT_EXPORT_API int bluetooth_telephony_audio_close(void)
1747 GDBusConnection *conn;
1754 BT_TELEPHONY_CHECK_INITIALIZED();
1755 BT_TELEPHONY_CHECK_ENABLED();
1757 /* Because this API is async call, so can't use dbus SMACK */
1758 if (__bt_telephony_check_privilege() ==
1759 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1760 BT_ERR("Don't have a privilege to use this API");
1761 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1764 conn = _bt_get_system_private_conn();
1766 BT_DBG("No System Bus found\n");
1767 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1770 if (telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1771 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
1773 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1774 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1775 HFP_AGENT_INTERFACE, NULL, &err);
1776 if (proxy == NULL) {
1777 BT_ERR("Unable to allocate new proxy");
1779 g_dbus_error_strip_remote_error(err);
1780 ret = __bt_telephony_get_error(err->message);
1781 BT_ERR("Error here %d\n", ret);
1785 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1788 g_dbus_proxy_call(proxy, "Stop", NULL, G_DBUS_CALL_FLAGS_NONE,
1789 -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_close_cb, NULL);
1791 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
1794 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1797 BT_EXPORT_API int bluetooth_telephony_call_remote_ringing(unsigned int call_id)
1801 BT_TELEPHONY_CHECK_INITIALIZED();
1802 BT_TELEPHONY_CHECK_ENABLED();
1805 BT_DBG("call_id = [%d]", call_id);
1807 /*Make sure SCO is already connected */
1808 ret = __bluetooth_telephony_send_call_status(
1809 CSD_CALL_STATUS_MO_ALERTING, call_id, NULL);
1811 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1812 BT_ERR("send call status Failed = [%d]", ret);
1818 BT_EXPORT_API int bluetooth_telephony_call_answered(unsigned int call_id,
1819 unsigned int bt_audio)
1824 BT_DBG("call_id = [%d]", call_id);
1826 BT_TELEPHONY_CHECK_INITIALIZED();
1827 BT_TELEPHONY_CHECK_ENABLED();
1829 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_ACTIVE,
1831 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1832 BT_ERR("send call status Failed = [%d]", ret);
1837 if (!bluetooth_telephony_is_sco_connected()) {
1838 ret = bluetooth_telephony_audio_open();
1839 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1840 BT_ERR("Audio connection call Failed[%d]", ret);
1848 BT_EXPORT_API int bluetooth_telephony_call_end(unsigned int call_id)
1853 BT_DBG("call_id = [%d]", call_id);
1855 BT_TELEPHONY_CHECK_INITIALIZED();
1856 BT_TELEPHONY_CHECK_ENABLED();
1858 if (telephony_info.call_count > 0)
1859 telephony_info.call_count = telephony_info.call_count - 1;
1861 if (telephony_info.call_count == 0) {
1862 if (bluetooth_telephony_is_sco_connected()) {
1863 ret = bluetooth_telephony_audio_close();
1864 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1865 BT_ERR(" Failed = [%d]", ret);
1869 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_MT_RELEASE,
1871 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1872 BT_ERR("send call status Failed = [%d]", ret);
1878 BT_EXPORT_API int bluetooth_telephony_call_held(unsigned int call_id)
1883 BT_DBG("call_id = [%d]", call_id);
1885 BT_TELEPHONY_CHECK_INITIALIZED();
1886 BT_TELEPHONY_CHECK_ENABLED();
1888 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_HOLD,
1890 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1891 BT_ERR("send call status Failed = [%d]", ret);
1897 BT_EXPORT_API int bluetooth_telephony_call_retrieved(unsigned int call_id)
1902 BT_DBG("call_id = [%d]", call_id);
1904 BT_TELEPHONY_CHECK_INITIALIZED();
1905 BT_TELEPHONY_CHECK_ENABLED();
1907 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_ACTIVE,
1909 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1910 BT_ERR("send call status Failed = [%d]", ret);
1916 BT_EXPORT_API int bluetooth_telephony_call_swapped(void *call_list,
1917 unsigned int call_count)
1921 GList *list = call_list;
1922 bt_telephony_call_status_info_t *call_status;
1926 BT_TELEPHONY_CHECK_INITIALIZED();
1927 BT_TELEPHONY_CHECK_ENABLED();
1930 BT_ERR("call_list is invalid");
1931 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1934 /* Because this API is async call, so can't use dbus SMACK */
1935 if (__bt_telephony_check_privilege() ==
1936 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1937 BT_ERR("Don't have a privilege to use this API");
1938 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1941 BT_DBG(" call_count = [%d]", call_count);
1943 for (i = 0; i < call_count; i++) {
1944 call_status = g_list_nth_data(list, i);
1946 if (NULL == call_status)
1949 BT_DBG(" %d : Call id [%d] status[%d]", i,
1950 call_status->call_id,
1951 call_status->call_status);
1953 if (NULL != call_status->phone_number)
1954 DBG_SECURE("Number [%s]", call_status->phone_number);
1956 switch (call_status->call_status) {
1957 case BLUETOOTH_CALL_STATE_HELD:
1958 ret = __bluetooth_telephony_send_call_status(
1959 CSD_CALL_STATUS_HOLD,
1960 call_status->call_id,
1961 call_status->phone_number);
1962 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1963 BT_ERR("Failed = %d", ret);
1968 case BLUETOOTH_CALL_STATE_CONNECTED:
1969 ret = __bluetooth_telephony_send_call_status(
1970 CSD_CALL_STATUS_ACTIVE,
1971 call_status->call_id,
1972 call_status->phone_number);
1973 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1974 BT_ERR("Failed = [%d]", ret);
1980 if ((call_status->call_status <
1981 BLUETOOTH_CALL_STATE_NONE) ||
1982 (call_status->call_status >=
1983 BLUETOOTH_CALL_STATE_ERROR)) {
1984 BT_ERR("Unknown Call state");
1985 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
1991 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1994 BT_EXPORT_API int bluetooth_telephony_set_call_status(void *call_list,
1995 unsigned int call_count)
2001 ret = bluetooth_telephony_call_swapped(call_list, call_count);
2003 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2004 BT_ERR("Failed = [%d]", ret);
2008 telephony_info.call_count = call_count;
2011 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2014 BT_EXPORT_API int bluetooth_telephony_indicate_outgoing_call(
2015 const char *ph_number, unsigned int call_id,
2016 unsigned int bt_audio)
2021 const char *path = telephony_info.call_path;
2022 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
2026 BT_TELEPHONY_CHECK_INITIALIZED();
2027 BT_TELEPHONY_CHECK_ENABLED();
2029 if (NULL == ph_number)
2030 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2032 param = g_variant_new("(ssi)", path, ph_number, call_id);
2033 reply = __bluetooth_telephony_dbus_method_send(
2034 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2035 "OutgoingCall", &err, param);
2038 BT_ERR("Error returned in method call\n");
2040 g_dbus_error_strip_remote_error(err);
2041 ret = __bt_telephony_get_error(err->message);
2045 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2048 g_variant_unref(reply);
2050 telephony_info.call_count++;
2051 BT_DBG(" ag_info.ag_call_count = [%d]", telephony_info.call_count);
2054 if (!bluetooth_telephony_is_sco_connected()) {
2055 ret = bluetooth_telephony_audio_open();
2057 BT_ERR(" Audio connection Failed = %d", ret);
2065 BT_EXPORT_API int bluetooth_telephony_indicate_incoming_call(
2066 const char *ph_number, unsigned int call_id)
2071 const char *path = telephony_info.call_path;
2076 BT_TELEPHONY_CHECK_INITIALIZED();
2077 BT_TELEPHONY_CHECK_ENABLED();
2079 if (NULL == ph_number)
2080 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2082 param = g_variant_new("(ssi)", path, ph_number, call_id);
2083 reply = __bluetooth_telephony_dbus_method_send(
2084 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2085 "IncomingCall", &err, param);
2088 BT_ERR("Error returned in method call\n");
2090 g_dbus_error_strip_remote_error(err);
2091 ret = __bt_telephony_get_error(err->message);
2095 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2098 g_variant_unref(reply);
2099 telephony_info.call_count++;
2100 BT_DBG("telephony_info.call_count = [%d]", telephony_info.call_count);
2102 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2105 BT_EXPORT_API int bluetooth_telephony_set_speaker_gain(
2106 unsigned short speaker_gain)
2115 BT_TELEPHONY_CHECK_INITIALIZED();
2116 BT_TELEPHONY_CHECK_ENABLED();
2118 BT_DBG("set speaker_gain= [%d]", speaker_gain);
2120 param = g_variant_new("(q)", speaker_gain);
2121 reply = __bluetooth_telephony_dbus_method_send(
2122 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2123 "SetSpeakerGain", &err, param);
2126 BT_ERR("Error returned in method call\n");
2128 g_dbus_error_strip_remote_error(err);
2129 ret = __bt_telephony_get_error(err->message);
2133 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2136 g_variant_unref(reply);
2138 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2141 BT_EXPORT_API int bluetooth_telephony_get_headset_volume(
2142 unsigned int *speaker_gain)
2151 BT_TELEPHONY_CHECK_INITIALIZED();
2152 BT_TELEPHONY_CHECK_ENABLED();
2154 reply = __bluetooth_telephony_dbus_method_send(
2155 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2156 "GetSpeakerGain", &err, NULL);
2159 BT_ERR("Error returned in method call\n");
2161 g_dbus_error_strip_remote_error(err);
2162 ret = __bt_telephony_get_error(err->message);
2166 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2168 g_variant_get(reply, "(q)", &gain);
2169 *speaker_gain = gain;
2170 BT_DBG("Get speaker_gain= [%d]", *speaker_gain);
2172 g_variant_unref(reply);
2175 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2178 BT_EXPORT_API int bluetooth_telephony_is_connected(gboolean *ag_connected)
2183 gboolean ag_connected_from_bt_agent;
2185 BT_CHECK_ENABLED(return);
2187 reply = __bluetooth_telephony_dbus_method_send(
2188 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2189 "IsConnected", &err, NULL);
2192 BT_ERR("Error returned in method call\n");
2194 g_dbus_error_strip_remote_error(err);
2195 ret = __bt_telephony_get_error(err->message);
2199 return BLUETOOTH_ERROR_INTERNAL;
2201 g_variant_get(reply, "(b)", &ag_connected_from_bt_agent);
2202 *ag_connected = ag_connected_from_bt_agent;
2204 BT_DBG("Conn Status: %s", *ag_connected ? "Connected" : "Disconnected");
2206 g_variant_unref(reply);
2208 return BLUETOOTH_ERROR_NONE;
2211 BT_EXPORT_API int bluetooth_telephony_set_active_headset(const char *remote_addr)
2213 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
2219 BT_CHECK_ENABLED(return);
2221 if (NULL == remote_addr)
2222 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2224 param = g_variant_new("(s)", remote_addr);
2225 reply = __bluetooth_telephony_dbus_method_send(
2226 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2227 "SwapHeadset", &err, param);
2230 BT_ERR("Error returned in method call\n");
2232 g_dbus_error_strip_remote_error(err);
2233 ret = __bt_telephony_get_error(err->message);
2237 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2240 g_variant_unref(reply);
2241 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2243 return BLUETOOTH_ERROR_NOT_SUPPORT;
2247 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
2248 const gchar *sender_name,
2249 const gchar *object_path,
2250 const gchar *interface_name,
2251 const gchar *signal_name,
2252 GVariant *parameters,
2259 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2260 GVariant *optional_param = NULL;
2262 g_variant_get(parameters, "(&o@a{sa{sv}})",
2263 &path, &optional_param);
2266 BT_ERR("Invalid adapter path");
2270 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
2271 if (__bt_telephony_get_src_addr(optional_param))
2272 BT_ERR("Fail to get the local adapter address");
2274 ret = __bluetooth_telephony_register();
2275 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
2276 BT_ERR("__bluetooth_telephony_register failed");
2279 g_variant_unref(optional_param);
2285 static int __bt_telephony_get_src_addr(GVariant *value)
2288 GVariant *param = NULL;
2291 /* signature a{sa{sv}} */
2292 g_variant_iter_init(&iter, value);
2293 while ((param = g_variant_iter_next_value(&iter))) {
2294 char *interface_name;
2295 GVariant *interface_var = NULL;
2296 GVariant *param_inner = NULL;
2298 g_variant_get(param, "{&s*}", &interface_name, &interface_var);
2299 g_variant_unref(param);
2301 BT_DBG("interface_name: %s", interface_name);
2302 /* format of interface_var: a{sv}*/
2303 if (strcasecmp(interface_name, BLUEZ_ADAPTER_INTERFACE) == 0) {
2304 GVariantIter iter_inner;
2306 g_variant_iter_init(&iter_inner, interface_var);
2307 while ((param_inner = g_variant_iter_next_value(&iter_inner))) {
2308 char *property_name;
2309 GVariant *property_var;
2311 g_variant_get(param_inner, "{&sv}",
2314 g_variant_unref(param_inner);
2316 if (strcasecmp(property_name, "Address") == 0) {
2317 const gchar *bd_addr;
2319 bd_addr = g_variant_get_string(
2322 src_addr = g_strdup(bd_addr);
2323 BT_DBG("Address: %s", src_addr);
2325 g_variant_unref(interface_var);
2326 g_variant_unref(property_var);
2329 g_variant_unref(property_var);
2332 g_variant_unref(interface_var);
2336 return BLUETOOTH_ERROR_NONE;