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"
28 #include "bt-internal-types.h"
30 #define BT_SCO_TIMEOUT 3000
32 #define BT_CVSD_CODEC_ID 1
33 #define BT_MSBC_CODEC_ID 2
36 GDBusConnection *conn;
38 GDBusProxy *dbus_proxy;
39 GDBusProxy *manager_proxy;
40 } telephony_dbus_info_t;
43 bt_telephony_func_ptr cb;
44 unsigned int call_count;
46 char address[BT_ADDRESS_STR_LEN];
47 char call_path[BT_AUDIO_CALL_PATH_LEN];
48 bluetooth_headset_state_t headset_state;
50 } bt_telephony_info_t;
53 char *src_addr = NULL;
56 #define BLUETOOTH_TELEPHONY_ERROR (__bluetooth_telephony_error_quark())
58 #define BLUEZ_SERVICE_NAME "org.bluez"
59 #define BLUEZ_HEADSET_INTERFACE "org.bluez.Headset"
61 #define BLUEZ_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager"
62 #define BLUEZ_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
63 #define BLUEZ_ADAPTER_INTERFACE "org.bluez.Adapter1"
64 #define BLUEZ_DEVICE_INTERFACE "org.bluez.Device1"
65 #define HFP_AGENT_SERVICE "org.bluez.ag_agent"
68 #define HFP_AGENT_PATH "/org/bluez/hfp_agent"
69 #define HFP_AGENT_INTERFACE "Org.Hfp.App.Interface"
71 #define TELEPHONY_APP_INTERFACE "org.tizen.csd.Call.Instance"
72 #define CSD_CALL_APP_PATH "/org/tizen/csd/%d"
73 #define HFP_NREC_STATUS_CHANGE "NrecStatusChanged"
74 #define HFP_ANSWER_CALL "Answer"
75 #define HFP_REJECT_CALL "Reject"
76 #define HFP_RELEASE_CALL "Release"
77 #define HFP_THREEWAY_CALL "Threeway"
79 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
81 /*Below Inrospection data is exposed to bluez from agent*/
82 static const gchar bt_telephony_introspection_xml[] =
84 " <interface name='org.tizen.csd.Call.Instance'>"
85 " <method name='SendDtmf'>"
86 " <arg type='s' name='dtmf' direction='in'/>"
88 " <method name='VendorCmd'>"
89 " <arg type='s' name='vendor' direction='in'/>"
94 #define BT_TELEPHONY_CHECK_ENABLED() \
96 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) { \
97 BT_ERR("BT is not enabled"); \
98 return BLUETOOTH_TELEPHONY_ERROR_NOT_ENABLED; \
102 static gboolean is_initialized = FALSE;
103 #define BT_TELEPHONY_CHECK_INITIALIZED() \
105 if (is_initialized == FALSE) { \
106 BT_ERR("Bluetooth telephony not initilized"); \
107 return BLUETOOTH_TELEPHONY_ERROR_NOT_INITIALIZED; \
111 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
112 const gchar *sender_name,
113 const gchar *object_path,
114 const gchar *interface_name,
115 const gchar *signal_name,
116 GVariant *parameters,
119 static int __bt_telephony_get_src_addr(GVariant *value);
121 static bt_telephony_info_t telephony_info;
122 static telephony_dbus_info_t telephony_dbus_info;
123 static gboolean is_active = FALSE;
125 /*Function Declaration*/
126 static int __bt_telephony_get_error(const char *error_message);
127 static void __bt_telephony_event_cb(int event, int result, void *param_data);
128 static GQuark __bluetooth_telephony_error_quark(void);
129 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
130 const char *interface, const char *method,
131 GError **err, GVariant *parameters);
132 static int __bluetooth_telephony_send_call_status(
133 bt_telephony_call_status_t call_status,
134 unsigned int call_id, const char *ph_number);
135 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
136 bluetooth_telephony_error_t error, const char *err_msg);
138 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
139 const gchar *sender_name,
140 const gchar *object_path,
141 const gchar *interface_name,
142 const gchar *signal_name,
143 GVariant *parameters,
146 static int __bluetooth_telephony_proxy_init(void);
147 static void __bluetooth_telephony_proxy_deinit(void);
148 static int __bluetooth_telephony_register(void);
149 static int __bluetooth_telephony_unregister(void);
151 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class);
152 static int __bluetooth_telephony_get_connected_device(void);
153 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void);
155 /*Function Definition*/
156 static void __bt_telephony_method(GDBusConnection *connection,
158 const gchar *object_path,
159 const gchar *interface_name,
160 const gchar *method_name,
161 GVariant *parameters,
162 GDBusMethodInvocation *invocation,
167 BT_INFO("method %s", method_name);
168 BT_INFO("object_path %s", object_path);
170 if (g_strcmp0(method_name, "SendDtmf") == 0) {
172 telephony_event_dtmf_t call_data = { 0, };
174 g_variant_get(parameters, "(&s)", &dtmf);
177 BT_ERR("Number dial failed");
178 __bluetooth_telephony_error(invocation,
179 BLUETOOTH_TELEPHONY_ERROR_INVALID_DTMF,
182 DBG_SECURE("Dtmf = %s", dtmf);
184 call_data.dtmf = g_strdup(dtmf);
185 __bt_telephony_event_cb(
186 BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF,
187 BLUETOOTH_TELEPHONY_ERROR_NONE,
190 g_free(call_data.dtmf);
192 g_dbus_method_invocation_return_value(invocation, NULL);
194 } else if (g_strcmp0(method_name, "VendorCmd") == 0) {
197 g_variant_get(parameters, "(&s)", &at_cmd);
198 BT_INFO("Vendor %s", at_cmd);
199 if (at_cmd == NULL) {
200 BT_ERR("Vendor command is NULL\n");
201 __bluetooth_telephony_error(invocation,
202 BLUETOOTH_TELEPHONY_ERROR_APPLICATION,
203 "Invalid at vendor cmd");
205 DBG_SECURE("Vendor AT cmd = %s", at_cmd);
207 __bt_telephony_event_cb(
208 BLUETOOTH_EVENT_TELEPHONY_VENDOR_AT_CMD,
209 BLUETOOTH_TELEPHONY_ERROR_NONE,
212 g_dbus_method_invocation_return_value(invocation, NULL);
219 static const GDBusInterfaceVTable method_table = {
220 __bt_telephony_method,
225 static int __bt_telephony_get_error(const char *error_message)
227 if (error_message == NULL) {
228 BT_ERR("Error message NULL");
229 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
232 BT_ERR("Error message = %s", error_message);
233 if (g_strcmp0(error_message, "NotAvailable") == 0)
234 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
235 else if (g_strcmp0(error_message, "NotConnected") == 0)
236 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
237 else if (g_strcmp0(error_message, "InProgress") == 0)
238 return BLUETOOTH_TELEPHONY_ERROR_BUSY;
239 else if (g_strcmp0(error_message, "InvalidArguments") == 0)
240 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
241 else if (g_strcmp0(error_message, "AlreadyExists") == 0)
242 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_EXSIST;
243 else if (g_strcmp0(error_message, "Already Connected") == 0)
244 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
245 else if (g_strcmp0(error_message, "No memory") == 0)
246 return BLUETOOTH_TELEPHONY_ERROR_NO_MEMORY;
247 else if (g_strcmp0(error_message, "I/O error") == 0)
248 return BLUETOOTH_TELEPHONY_ERROR_I_O_ERROR;
249 else if (g_strcmp0(error_message,
250 "Operation currently not available") == 0)
251 return BLUETOOTH_TELEPHONY_ERROR_OPERATION_NOT_AVAILABLE;
252 else if (g_strrstr(error_message, BT_ACCESS_DENIED_MSG))
253 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
255 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
258 static int __bt_telephony_check_privilege(void)
265 reply = __bluetooth_telephony_dbus_method_send(
266 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
267 "CheckPrivilege", &err, NULL);
270 BT_ERR("Error returned in method call");
272 g_dbus_error_strip_remote_error(err);
273 ret = __bt_telephony_get_error(err->message);
277 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
279 g_variant_unref(reply);
282 return BLUETOOTH_TELEPHONY_ERROR_NONE;
285 static void __bt_telephony_event_cb(int event, int result, void *param_data)
287 telephony_event_param_t bt_event = { 0, };
289 bt_event.event = event;
290 bt_event.result = result;
291 bt_event.param_data = param_data;
293 ret_if(telephony_info.cb == NULL);
294 telephony_info.cb(bt_event.event, &bt_event, telephony_info.user_data);
298 static GQuark __bluetooth_telephony_error_quark(void)
300 static GQuark quark = 0;
302 quark = g_quark_from_static_string("telephony");
307 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
308 const char *interface, const char *method,
309 GError **err, GVariant *parameters)
313 if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
318 GDBusConnection *conn;
322 conn = telephony_dbus_info.conn;
323 retv_if(conn == NULL, NULL);
325 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
326 NULL, HFP_AGENT_SERVICE, path, interface, NULL, err);
328 BT_ERR("Unable to allocate new proxy");
332 reply = g_dbus_proxy_call_sync(proxy, method, parameters,
333 G_DBUS_CALL_FLAGS_NONE, timeout, NULL, err);
335 g_object_unref(proxy);
340 static int __bluetooth_telephony_send_call_status(
341 bt_telephony_call_status_t call_status,
342 unsigned int call_id, const char *ph_number)
347 char *path = g_strdup(telephony_info.call_path);
353 if (NULL == ph_number)
354 phone_number = g_strdup("");
356 phone_number = g_strdup(ph_number);
358 param = g_variant_new("(ssii)", path, phone_number,
359 call_status, call_id);
360 reply = __bluetooth_telephony_dbus_method_send(
361 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
362 "ChangeCallStatus", &err, param);
365 g_free(phone_number);
368 BT_ERR("Error returned in method call");
370 g_dbus_error_strip_remote_error(err);
371 ret = __bt_telephony_get_error(err->message);
375 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
378 g_variant_unref(reply);
380 return BLUETOOTH_TELEPHONY_ERROR_NONE;
383 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
384 bluetooth_telephony_error_t error, const char *err_msg)
386 g_dbus_method_invocation_return_error(invocation,
387 BLUETOOTH_TELEPHONY_ERROR, error,
391 static void __bluetooth_telephony_answer_call(GVariant *var)
393 telephony_event_callid_t call_data = { 0, };
398 g_variant_get(var, "(u)", &callid);
399 BT_DBG("call_id = [%d]", callid);
400 call_data.callid = callid;
402 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL,
403 BLUETOOTH_TELEPHONY_ERROR_NONE,
408 static void __bluetooth_telephony_release_call(GVariant *var)
410 telephony_event_callid_t call_data = { 0, };
415 g_variant_get(var, "(u)", &callid);
416 BT_DBG("call_id = [%d]", callid);
417 call_data.callid = callid;
419 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL,
420 BLUETOOTH_TELEPHONY_ERROR_NONE,
425 static void __bluetooth_telephony_reject_call(GVariant *var)
427 telephony_event_callid_t call_data = { 0, };
432 g_variant_get(var, "(u)", &callid);
433 BT_DBG("call_id = [%d]", callid);
434 call_data.callid = callid;
436 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL,
437 BLUETOOTH_TELEPHONY_ERROR_NONE,
442 static void __bluetooth_telephony_threeway_call(GVariant *var)
445 unsigned int chld_value;
449 g_variant_get(var, "(u)", &chld_value);
450 BT_DBG("chld value = [%d]", chld_value);
452 switch (chld_value) {
454 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL;
457 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL;
460 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL;
463 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL;
466 BT_ERR("Invalid CHLD command");
470 __bt_telephony_event_cb(event,
471 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
475 static void __bluetooth_handle_nrec_status_change(GVariant *var)
477 gboolean status = FALSE;
479 g_variant_get(var, "(b)", &status);
480 BT_INFO("NREC status = %d", status);
482 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED,
483 BLUETOOTH_TELEPHONY_ERROR_NONE, (void *)&status);
487 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
488 const gchar *sender_name,
489 const gchar *object_path,
490 const gchar *interface_name,
491 const gchar *signal_name,
492 GVariant *parameters,
497 if (strcasecmp(interface_name, HFP_AGENT_SERVICE) == 0) {
498 if (strcasecmp(signal_name, HFP_NREC_STATUS_CHANGE) == 0)
499 __bluetooth_handle_nrec_status_change(parameters);
500 else if (strcasecmp(signal_name, HFP_ANSWER_CALL) == 0)
501 __bluetooth_telephony_answer_call(parameters);
502 else if (strcasecmp(signal_name, HFP_REJECT_CALL) == 0)
503 __bluetooth_telephony_reject_call(parameters);
504 else if (strcasecmp(signal_name, HFP_RELEASE_CALL) == 0)
505 __bluetooth_telephony_release_call(parameters);
506 else if (strcasecmp(signal_name, HFP_THREEWAY_CALL) == 0)
507 __bluetooth_telephony_threeway_call(parameters);
508 } else if (strcasecmp(interface_name, BLUEZ_HEADSET_INTERFACE) == 0) {
509 if (strcasecmp(signal_name, "PropertyChanged") == 0) {
513 g_variant_get(parameters, "(&sv)", &property, &values);
514 BT_DBG("Property: %s", property);
516 if (strcasecmp(property, "State") == 0) {
518 state = (gchar *)g_variant_get_string(values, NULL);
521 BT_ERR("State is null");
524 BT_DBG("state: %s", state);
525 if (g_strcmp0(state, "connected") == 0) {
526 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
527 } else if (g_strcmp0(state, "playing") == 0) {
528 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
529 } else if (g_strcmp0(state, "disconnected") == 0) {
530 /* Headset state: playing -> disconnected */
531 if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING) {
532 __bt_telephony_event_cb(
533 BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
534 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
537 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
539 } else if (strcasecmp(property, "Connected") == 0) {
540 gboolean connected = FALSE;
541 char *dev_addr = NULL;
542 connected = g_variant_get_boolean(values);
543 BT_INFO("connected %d", connected);
545 /*Get device address*/
546 if (object_path != NULL)
547 dev_addr = strstr(object_path, "dev_");
549 if (dev_addr != NULL) {
551 g_strlcpy(telephony_info.address,
553 sizeof(telephony_info.address));
554 g_strdelimit(telephony_info.address, "_", ':');
555 BT_DBG("address is %s",
556 telephony_info.address);
558 telephony_info.headset_state =
559 BLUETOOTH_STATE_CONNECTED;
561 if (telephony_dbus_info.proxy != NULL) {
562 g_object_unref(telephony_dbus_info.proxy);
563 telephony_dbus_info.proxy = NULL;
566 telephony_dbus_info.proxy =
567 __bluetooth_telephony_get_connected_device_proxy();
569 BT_INFO("Headset Connected");
571 __bt_telephony_event_cb(
572 BLUETOOTH_EVENT_TELEPHONY_HFP_CONNECTED,
573 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
575 } else { /*Device disconnected*/
576 memset(telephony_info.address, 0x00,
577 sizeof(telephony_info.address));
578 telephony_info.headset_state =
579 BLUETOOTH_STATE_DISCONNETED;
581 if (telephony_dbus_info.proxy != NULL) {
582 g_object_unref(telephony_dbus_info.proxy);
583 telephony_dbus_info.proxy = NULL;
586 BT_INFO("Headset Disconnected");
588 __bt_telephony_event_cb(
589 BLUETOOTH_EVENT_TELEPHONY_HFP_DISCONNECTED,
590 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
592 } else if (strcasecmp(property, "SpeakerGain") == 0) {
593 unsigned int spkr_gain;
594 guint16 gain = g_variant_get_uint16(values);
596 spkr_gain = (unsigned int)gain;
597 BT_DBG("spk_gain[%d]", spkr_gain);
599 __bt_telephony_event_cb(
600 BLUETOOTH_EVENT_TELEPHONY_SET_SPEAKER_GAIN,
601 BLUETOOTH_TELEPHONY_ERROR_NONE,
603 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
604 unsigned int mic_gain;
605 guint16 gain = g_variant_get_uint16(values);
607 mic_gain = (unsigned int)gain;
608 BT_DBG("mic_gain[%d]", mic_gain);
610 __bt_telephony_event_cb(
611 BLUETOOTH_EVENT_TELEPHONY_SET_MIC_GAIN,
612 BLUETOOTH_TELEPHONY_ERROR_NONE,
614 } else if (strcasecmp(property, "Playing") == 0) {
615 gboolean audio_sink_playing;
617 audio_sink_playing = g_variant_get_boolean(values);
618 if (audio_sink_playing) {
619 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
620 __bt_telephony_event_cb(
621 BLUETOOTH_EVENT_TELEPHONY_AUDIO_CONNECTED,
622 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
624 telephony_info.headset_state =
625 BLUETOOTH_STATE_CONNECTED;
626 __bt_telephony_event_cb(
627 BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
628 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
632 g_variant_unref(values);
638 static GDBusNodeInfo *__bt_telephony_create_method_node_info
639 (const gchar *introspection_data)
642 GDBusNodeInfo *node_info = NULL;
644 if (introspection_data == NULL)
647 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
650 BT_ERR("Unable to create node: %s", err->message);
657 int __bluetooth_telephony_register_object(int reg, GDBusNodeInfo *node_info)
659 static guint bt_tel_id = 0;
660 GError *error = NULL;
664 if (node_info == NULL)
665 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
667 path = g_strdup(telephony_info.call_path);
668 BT_DBG("path is [%s]", path);
670 bt_tel_id = g_dbus_connection_register_object(telephony_dbus_info.conn,
671 path, node_info->interfaces[0],
676 if (bt_tel_id == 0) {
677 BT_ERR("Failed to register: %s", error->message);
679 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
683 g_dbus_connection_unregister_object(telephony_dbus_info.conn,
689 return BLUETOOTH_TELEPHONY_ERROR_NONE;
692 static int __bluetooth_telephony_proxy_init(void)
696 GDBusNodeInfo *node_info;
698 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
699 TELEPHONY_APP_INTERFACE,
700 G_BUS_NAME_OWNER_FLAGS_NONE,
703 BT_DBG("owner_id is [%d]", owner_id);
705 node_info = __bt_telephony_create_method_node_info(
706 bt_telephony_introspection_xml);
708 if (node_info == NULL) {
709 BT_ERR("node_info NULL");
710 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
712 if (__bluetooth_telephony_register_object(TRUE, node_info) !=
713 BLUETOOTH_TELEPHONY_ERROR_NONE) {
714 BT_ERR("Registation of Method Failed");
715 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
719 return BLUETOOTH_TELEPHONY_ERROR_NONE;
722 static void __bluetooth_telephony_proxy_deinit(void)
726 __bluetooth_telephony_register_object(FALSE, NULL);
732 static int __bluetooth_telephony_register(void)
737 char *path = g_strdup(telephony_info.call_path);
742 param = g_variant_new("(ss)", path, src_addr);
743 BT_DBG("Path[%s] Src_Address[%s]", path, src_addr);
745 reply = __bluetooth_telephony_dbus_method_send(
746 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
747 "RegisterApplication", &err, param);
752 BT_ERR("Error returned in method call");
754 g_dbus_error_strip_remote_error(err);
755 ret = __bt_telephony_get_error(err->message);
756 BT_ERR("Error here %d\n", ret);
760 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
763 g_variant_unref(reply);
764 BT_DBG("__bluetooth_telephony_register completed");
766 return BLUETOOTH_TELEPHONY_ERROR_NONE;
769 static int __bluetooth_telephony_unregister(void)
774 char *path = g_strdup(telephony_info.call_path);
779 param = g_variant_new("(s)", path);
780 reply = __bluetooth_telephony_dbus_method_send(
781 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
782 "UnregisterApplication", &err, param);
787 BT_ERR("Error returned in method call");
789 g_dbus_error_strip_remote_error(err);
790 ret = __bt_telephony_get_error(err->message);
794 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
797 g_variant_unref(reply);
798 BT_DBG("__bluetooth_telephony_unregister completed");
800 return BLUETOOTH_TELEPHONY_ERROR_NONE;
803 static void __bluetooth_telephony_init_headset_state(void)
807 gboolean status = FALSE;
811 if (telephony_dbus_info.conn == NULL) {
812 BT_ERR("Bluetooth telephony not initilized");
816 reply = __bluetooth_telephony_dbus_method_send(
817 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
818 "IsConnected", &err, NULL);
820 BT_ERR("Error returned in method call\n");
822 BT_ERR("Error message = %s", err->message);
828 g_variant_get(reply, "(b)", &status);
829 g_variant_unref(reply);
831 BT_INFO("Headset Connected Status = [%d]", status);
833 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
837 if (bluetooth_telephony_is_sco_connected())
838 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
843 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class)
845 gboolean flag = FALSE;
848 switch ((device_class & 0x1f00) >> 8) {
850 switch ((device_class & 0xfc) >> 2) {
868 /* Tizen Wearable device */
870 switch ((device_class & 0xfc) >> 2) {
871 case 0x01: /* Wrist Watch */
879 BT_DBG("[%d]", flag);
884 static gboolean __bluetooth_telephony_is_headset_by_uuid(gchar **uuids)
888 unsigned int service = 0;
892 retv_if(uuids == NULL, FALSE);
894 for (i = 0; uuids[i] != NULL; i++) {
895 parts = g_strsplit(uuids[i], "-", -1);
897 if (parts == NULL || parts[0] == NULL) {
902 service = g_ascii_strtoull(parts[0], NULL, 16);
905 if (service == BLUETOOTH_HS_PROFILE_UUID ||
906 service == BLUETOOTH_HF_PROFILE_UUID)
915 static int __bluetooth_telephony_get_connected_device(void)
917 GDBusConnection *conn;
918 GDBusProxy *headset_agent_proxy = NULL;
919 GDBusProxy *manager_proxy = NULL;
920 GDBusProxy *proxy = NULL;
921 GVariant *reply = NULL;
922 GVariant *getall = NULL;
923 GVariant *isPlayingReply = NULL;
924 GVariant *isConnectedReply = NULL;
925 GVariant *param = NULL;
926 GVariant *var_path = NULL;
927 GVariant *path_values = NULL;
928 GVariant *value = NULL;
929 GError *error = NULL;
931 GVariantIter iter_path;
932 GVariantIter property_iter;
933 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
936 conn = _bt_gdbus_get_system_gconn();
937 retv_if(conn == NULL, BLUETOOTH_TELEPHONY_ERROR_INTERNAL);
939 manager_proxy = g_dbus_proxy_new_sync(
940 conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
941 BLUEZ_SERVICE_NAME, "/",
942 BLUEZ_MANAGER_INTERFACE, NULL, &error);
943 if (manager_proxy == NULL) {
944 BT_ERR("Unable to allocate new proxy \n");
945 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
947 g_dbus_error_strip_remote_error(error);
948 ret = __bt_telephony_get_error(error->message);
949 BT_ERR("Error here %d\n", ret);
955 /* Synchronous call */
956 reply = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects", NULL,
957 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
958 g_object_unref(manager_proxy);
961 BT_ERR("Can't get managed objects");
962 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
964 g_dbus_error_strip_remote_error(error);
965 ret = __bt_telephony_get_error(error->message);
966 BT_ERR("Error here %d\n", ret);
972 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
973 g_variant_iter_init(&iter, reply);
975 while ((param = g_variant_iter_next_value(&iter))) {
976 g_variant_iter_init(&iter_path, param);
978 while ((var_path = g_variant_iter_next_value(&iter_path))) {
980 uint32_t device_class = 0;
981 gboolean playing = FALSE;
982 gboolean connected = FALSE;
983 char *object_path = NULL;
984 gchar *address = NULL;
986 gchar **uuids = NULL;
987 GVariant *getall_param = NULL;
989 g_variant_get(var_path, "{&o*}", &object_path,
991 g_variant_unref(path_values); /* path_values unused*/
993 proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
994 G_DBUS_PROXY_FLAGS_NONE, NULL,
995 BLUEZ_SERVICE_NAME, object_path,
996 BLUEZ_PROPERTIES_INTERFACE, NULL, &error);
998 BT_ERR("Unable to allocate new proxy \n");
999 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1001 g_dbus_error_strip_remote_error(error);
1002 ret = __bt_telephony_get_error(error->message);
1003 BT_ERR("Error here %d\n", ret);
1004 g_error_free(error);
1010 getall_param = g_variant_new("s", BLUEZ_DEVICE_INTERFACE);
1011 getall = g_dbus_proxy_call_sync(proxy,
1012 "GetAll", getall_param,
1013 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1014 g_object_unref(proxy);
1017 BT_ERR("Can't get managed objects");
1018 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1020 g_dbus_error_strip_remote_error(error);
1021 ret = __bt_telephony_get_error(error->message);
1022 BT_ERR("Error here %d\n", ret);
1023 g_error_free(error);
1028 g_variant_iter_init(&property_iter, getall);
1030 while (g_variant_iter_loop(&property_iter, "{&sv}", &key, &value)) {
1031 if (!g_strcmp0(key, "Class")) {
1032 device_class = g_variant_get_uint32(value);
1033 BT_DBG("Device Class: %d", device_class);
1034 } else if (!g_strcmp0(key, "UUID")) {
1036 uuids = (gchar **)g_variant_get_strv(value, &len);
1037 BT_DBG_UUID(uuids, len, i);
1038 } else if (!g_strcmp0(key, "Address")) {
1039 address = (gchar *)g_variant_get_string(
1042 BT_DBG("Device Class: %s", address);
1044 g_variant_unref(value);
1046 g_variant_unref(getall);
1048 if (device_class == 0) {
1049 BT_DBG("COD is NULL (maybe paired by nfc)... Checking UUIDs");
1050 if (!__bluetooth_telephony_is_headset_by_uuid(uuids)) {
1051 BT_DBG("UUID checking completed. None HF device");
1054 BT_DBG("UUID checking completed. HF device");
1056 if (!__bluetooth_telephony_is_headset(device_class))
1060 /* this is headset; Check for Connection */
1061 headset_agent_proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1062 G_DBUS_PROXY_FLAGS_NONE, NULL,
1063 HFP_AGENT_SERVICE, object_path,
1064 HFP_AGENT_INTERFACE, NULL, &error);
1065 if (headset_agent_proxy == NULL) {
1066 BT_ERR("Unable to allocate new headset_agent_proxy");
1067 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1069 g_dbus_error_strip_remote_error(error);
1070 ret = __bt_telephony_get_error(error->message);
1071 BT_ERR("Error here %d\n", ret);
1072 g_error_free(error);
1077 isConnectedReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1078 "IsConnected", NULL,
1079 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1082 if (!isConnectedReply) {
1083 BT_ERR("Can't get managed objects");
1084 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1086 g_dbus_error_strip_remote_error(error);
1087 ret = __bt_telephony_get_error(error->message);
1088 BT_ERR("Error here %d\n", ret);
1089 g_error_free(error);
1093 connected = g_variant_get_boolean(isConnectedReply);
1094 g_variant_unref(isConnectedReply);
1097 g_strlcpy(telephony_info.address,
1099 sizeof(telephony_info.address));
1101 isPlayingReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1103 G_DBUS_CALL_FLAGS_NONE,
1105 if (!isPlayingReply) {
1106 BT_ERR("Can't get managed objects");
1107 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1109 g_dbus_error_strip_remote_error(error);
1110 ret = __bt_telephony_get_error(error->message);
1111 BT_ERR("Error here %d\n", ret);
1112 g_error_free(error);
1115 playing = g_variant_get_boolean(isPlayingReply);
1116 g_variant_unref(isPlayingReply);
1119 telephony_info.headset_state =
1120 BLUETOOTH_STATE_PLAYING;
1122 telephony_info.headset_state =
1123 BLUETOOTH_STATE_CONNECTED;
1130 g_object_unref(headset_agent_proxy);
1131 g_variant_unref(var_path);
1133 g_variant_unref(param);
1137 if (headset_agent_proxy)
1138 g_object_unref(headset_agent_proxy);
1140 g_variant_unref(reply);
1142 g_variant_unref(var_path);
1144 g_variant_unref(param);
1149 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void)
1151 GDBusProxy *proxy = NULL;
1152 GError *error = NULL;
1156 if (strlen(telephony_info.address) == 0)
1157 __bluetooth_telephony_get_connected_device();
1159 if (strlen(telephony_info.address) == 0)
1162 proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1163 G_DBUS_PROXY_FLAGS_NONE, NULL,
1164 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1165 HFP_AGENT_INTERFACE, NULL, &error);
1166 if (proxy == NULL) {
1167 BT_ERR("Unable to allocate new proxy");
1169 g_dbus_error_strip_remote_error(error);
1170 ret = __bt_telephony_get_error(error->message);
1171 BT_ERR("Error here %d\n", ret);
1172 g_error_free(error);
1181 int __bt_telephony_subscribe_adapter_signal(GDBusConnection *conn,
1187 static int subscribe_adapter_id = -1;
1188 if (subscribe == TRUE) {
1189 if (subscribe_adapter_id == -1) {
1190 subscribe_adapter_id = g_dbus_connection_signal_subscribe(conn,
1191 NULL, "org.freedesktop.DBus.ObjectManager",
1192 "InterfacesAdded", NULL, NULL, 0,
1193 __bt_telephony_adapter_filter,
1196 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1198 if (subscribe_adapter_id != -1) {
1199 g_dbus_connection_signal_unsubscribe(conn,
1200 subscribe_adapter_id);
1201 subscribe_adapter_id = -1;
1203 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1207 int __bt_telephony_event_subscribe_signal(GDBusConnection *conn,
1213 static int subscribe_event1_id = -1;
1214 static int subscribe_event2_id = -1;
1215 static int subscribe_event3_id = -1;
1216 static int subscribe_event4_id = -1;
1217 static int subscribe_event5_id = -1;
1218 static int subscribe_event6_id = -1;
1219 if (subscribe == TRUE) {
1220 if (subscribe_event1_id == -1) {
1221 subscribe_event1_id = g_dbus_connection_signal_subscribe(conn,
1222 NULL, BLUEZ_HEADSET_INTERFACE,
1223 "PropertyChanged", NULL, NULL, 0,
1224 __bluetooth_telephony_event_filter,
1227 if (subscribe_event2_id == -1) {
1228 subscribe_event2_id = g_dbus_connection_signal_subscribe(conn,
1229 NULL, HFP_AGENT_SERVICE,
1230 HFP_NREC_STATUS_CHANGE, NULL, NULL, 0,
1231 __bluetooth_telephony_event_filter,
1235 if (subscribe_event3_id == -1) {
1236 subscribe_event3_id = g_dbus_connection_signal_subscribe(conn,
1237 NULL, HFP_AGENT_SERVICE,
1238 HFP_ANSWER_CALL, NULL, NULL, 0,
1239 __bluetooth_telephony_event_filter,
1242 if (subscribe_event4_id == -1) {
1243 subscribe_event4_id = g_dbus_connection_signal_subscribe(conn,
1244 NULL, HFP_AGENT_SERVICE,
1245 HFP_REJECT_CALL, NULL, NULL, 0,
1246 __bluetooth_telephony_event_filter,
1249 if (subscribe_event5_id == -1) {
1250 subscribe_event5_id = g_dbus_connection_signal_subscribe(conn,
1251 NULL, HFP_AGENT_SERVICE,
1252 HFP_RELEASE_CALL, NULL, NULL, 0,
1253 __bluetooth_telephony_event_filter,
1256 if (subscribe_event6_id == -1) {
1257 subscribe_event6_id = g_dbus_connection_signal_subscribe(conn,
1258 NULL, HFP_AGENT_SERVICE,
1259 HFP_THREEWAY_CALL, NULL, NULL, 0,
1260 __bluetooth_telephony_event_filter,
1264 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1266 if (subscribe_event1_id != -1) {
1267 g_dbus_connection_signal_unsubscribe(conn,
1268 subscribe_event1_id);
1269 subscribe_event1_id = -1;
1271 if (subscribe_event2_id != -1) {
1272 g_dbus_connection_signal_unsubscribe(conn,
1273 subscribe_event2_id);
1274 subscribe_event2_id = -1;
1276 if (subscribe_event3_id != -1) {
1277 g_dbus_connection_signal_unsubscribe(conn,
1278 subscribe_event3_id);
1279 subscribe_event3_id = -1;
1281 if (subscribe_event4_id != -1) {
1282 g_dbus_connection_signal_unsubscribe(conn,
1283 subscribe_event4_id);
1284 subscribe_event4_id = -1;
1286 if (subscribe_event5_id != -1) {
1287 g_dbus_connection_signal_unsubscribe(conn,
1288 subscribe_event5_id);
1289 subscribe_event5_id = -1;
1291 if (subscribe_event6_id != -1) {
1292 g_dbus_connection_signal_unsubscribe(conn,
1293 subscribe_event6_id);
1294 subscribe_event6_id = -1;
1296 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1300 BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
1303 bluetooth_device_address_t loc_address = { {0} };
1304 char src_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1305 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
1306 GError *error = NULL;
1310 if (is_initialized == TRUE) {
1311 BT_ERR("Bluetooth telephony already initilized");
1312 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1315 is_initialized = TRUE;
1317 telephony_dbus_info.conn = _bt_gdbus_init_system_gconn();
1318 if (!telephony_dbus_info.conn) {
1319 is_initialized = FALSE;
1320 BT_ERR("Could not get DBus Connection");
1321 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1325 snprintf(telephony_info.call_path, sizeof(telephony_info.call_path),
1326 CSD_CALL_APP_PATH, getpid());
1327 BT_INFO("Call Path = %s", telephony_info.call_path);
1328 memset(telephony_info.address, 0x00, sizeof(telephony_info.address));
1330 if (__bluetooth_telephony_proxy_init()) {
1331 BT_ERR("__bluetooth_telephony_proxy_init failed\n");
1332 telephony_dbus_info.conn = NULL;
1333 is_initialized = FALSE;
1334 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1337 telephony_dbus_info.manager_proxy = g_dbus_proxy_new_sync(
1338 telephony_dbus_info.conn,
1339 G_DBUS_PROXY_FLAGS_NONE, NULL,
1340 BLUEZ_SERVICE_NAME, "/",
1341 BLUEZ_MANAGER_INTERFACE, NULL, &error);
1342 if (telephony_dbus_info.manager_proxy == NULL) {
1343 BT_ERR("Could not create a manager proxy");
1344 __bluetooth_telephony_proxy_deinit();
1345 telephony_dbus_info.conn = NULL;
1346 is_initialized = FALSE;
1348 g_dbus_error_strip_remote_error(error);
1349 ret = __bt_telephony_get_error(error->message);
1350 BT_ERR("Error here %d\n", ret);
1351 g_error_free(error);
1354 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1357 telephony_dbus_info.dbus_proxy = g_dbus_proxy_new_sync(
1358 telephony_dbus_info.conn,
1359 G_DBUS_PROXY_FLAGS_NONE, NULL,
1360 BT_EVENT_FREEDESKTOP, BT_FREEDESKTOP_PATH,
1361 BT_EVENT_FREEDESKTOP, NULL, &error);
1362 if (NULL == telephony_dbus_info.dbus_proxy) {
1363 __bluetooth_telephony_proxy_deinit();
1364 telephony_dbus_info.conn = NULL;
1365 g_object_unref(telephony_dbus_info.manager_proxy);
1366 telephony_dbus_info.manager_proxy = NULL;
1367 is_initialized = FALSE;
1368 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1371 if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, TRUE) != 0) {
1372 BT_ERR("Fail to Subscribe Adapter Signal");
1376 /*Callback and user applicaton data*/
1377 telephony_info.cb = cb;
1378 telephony_info.user_data = user_data;
1379 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1381 if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, TRUE) != 0) {
1382 BT_ERR("Fail to Subscribe telephony event Signal");
1386 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED)
1387 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1389 /*Bluetooth is active, therefore set the flag */
1392 ret = bluetooth_get_local_address(&loc_address);
1393 if (ret != BLUETOOTH_ERROR_NONE) {
1394 BT_ERR("Fail to get local address\n");
1395 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1398 _bt_convert_addr_type_to_string(src_address, loc_address.addr);
1399 src_addr = g_strdup(src_address);
1401 ret = __bluetooth_telephony_register();
1402 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1403 BT_ERR("__bluetooth_telephony_register failed\n");
1407 if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
1408 __bluetooth_telephony_init_headset_state();
1413 bluetooth_telephony_deinit();
1417 BT_EXPORT_API int bluetooth_telephony_deinit(void)
1420 BT_TELEPHONY_CHECK_INITIALIZED();
1422 is_initialized = FALSE;
1424 if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, FALSE) != 0)
1425 BT_ERR("Fail to UnSubscribe telephony event Signal");
1427 if (bluetooth_check_adapter() != BLUETOOTH_ADAPTER_DISABLED ||
1428 bluetooth_check_adapter_le() != BLUETOOTH_ADAPTER_LE_DISABLED)
1429 __bluetooth_telephony_unregister();
1431 __bluetooth_telephony_proxy_deinit();
1433 telephony_info.cb = NULL;
1434 telephony_info.user_data = NULL;
1435 telephony_info.call_count = 0;
1436 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1438 /* Remove BT enabled signal */
1439 if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, FALSE) != 0)
1440 BT_ERR("Fail to UnSubscribe Adapter event Signal");
1445 if (telephony_dbus_info.manager_proxy != NULL) {
1446 g_object_unref(telephony_dbus_info.manager_proxy);
1447 telephony_dbus_info.manager_proxy = NULL;
1450 if (telephony_dbus_info.conn != NULL)
1451 telephony_dbus_info.conn = NULL;
1453 if (telephony_dbus_info.dbus_proxy != NULL) {
1454 g_object_unref(telephony_dbus_info.dbus_proxy);
1455 telephony_dbus_info.dbus_proxy = NULL;
1459 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1462 BT_EXPORT_API gboolean bluetooth_telephony_is_sco_connected(void)
1466 gboolean status = FALSE;
1470 retv_if(is_initialized == FALSE, FALSE);
1471 retv_if(bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED, FALSE);
1473 reply = __bluetooth_telephony_dbus_method_send(
1474 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1475 "IsPlaying", &err, NULL);
1478 BT_ERR("Error returned in method call\n");
1480 BT_ERR("Error message = %s", err->message);
1485 g_variant_get(reply, "(b)", &status);
1486 g_variant_unref(reply);
1488 if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
1489 if (status == TRUE && telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1490 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
1492 BT_INFO("SCO Connected Status = [%d]", status);
1496 BT_EXPORT_API int bluetooth_telephony_is_nrec_enabled(gboolean *status)
1501 GVariant *param_inner;
1505 BT_TELEPHONY_CHECK_INITIALIZED();
1506 BT_TELEPHONY_CHECK_ENABLED();
1509 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1511 if (telephony_info.headset_state == BLUETOOTH_STATE_DISCONNETED)
1512 return BLUETOOTH_TELEPHONY_ERROR_AUDIO_NOT_CONNECTED;
1514 reply = __bluetooth_telephony_dbus_method_send(
1515 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1516 "GetProperties", &err, NULL);
1519 BT_ERR("Error returned in method call\n");
1521 BT_DBG("Error message = %s", err->message);
1524 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1527 g_variant_iter_init(&iter, reply);
1528 while ((param_inner = g_variant_iter_next_value(&iter))) {
1531 value = g_variant_lookup_value(param_inner,
1532 "nrec", G_VARIANT_TYPE_BOOLEAN);
1534 BT_DBG("Property NREC Found");
1535 *status = g_variant_get_boolean(value);
1536 BT_DBG("NREC status = [%d]", *status);
1537 g_variant_unref(value);
1538 g_variant_unref(param_inner);
1541 g_variant_unref(param_inner);
1543 BT_DBG("NREC status = [%d]", *status);
1544 g_variant_unref(reply);
1547 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1550 BT_EXPORT_API int bluetooth_telephony_is_wbs_mode(gboolean *status)
1556 GVariant *param_inner;
1560 BT_TELEPHONY_CHECK_INITIALIZED();
1561 BT_TELEPHONY_CHECK_ENABLED();
1564 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1568 reply = __bluetooth_telephony_dbus_method_send(
1569 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1570 "GetProperties", &err, NULL);
1573 BT_ERR("Error returned in method call");
1575 BT_ERR("Error message = %s", err->message);
1578 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1581 g_variant_iter_init(&iter, reply);
1582 while ((param_inner = g_variant_iter_next_value(&iter))) {
1585 value = g_variant_lookup_value(param_inner,
1586 "codec", G_VARIANT_TYPE_UINT32);
1588 BT_DBG("Property CODEC Found");
1589 codec = g_variant_get_uint32(value);
1590 g_variant_unref(value);
1591 BT_DBG("Codec = [%d]", codec);
1593 *status = codec == BT_MSBC_CODEC_ID ? TRUE : FALSE;
1594 BT_DBG("NREC status = [%d]", *status);
1595 g_variant_unref(value);
1596 g_variant_unref(param_inner);
1599 g_variant_unref(param_inner);
1602 g_variant_unref(reply);
1603 BT_DBG("MSBC status = [%d]", *status);
1606 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1609 BT_EXPORT_API int bluetooth_telephony_send_vendor_cmd(const char *cmd)
1611 GError *error = NULL;
1612 GVariant *reply, *parameters;
1616 BT_TELEPHONY_CHECK_INITIALIZED();
1617 BT_TELEPHONY_CHECK_ENABLED();
1619 BT_DBG("Send Vendor %s", cmd);
1621 if (telephony_dbus_info.proxy == NULL)
1622 telephony_dbus_info.proxy =
1623 __bluetooth_telephony_get_connected_device_proxy();
1625 if (telephony_dbus_info.proxy == NULL)
1626 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1628 parameters = g_variant_new("s", cmd);
1629 reply = g_dbus_proxy_call_sync(telephony_dbus_info.proxy,
1630 "SendVendorAtCmd", parameters,
1631 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1633 g_variant_unref(reply);
1636 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1639 BT_EXPORT_API int bluetooth_telephony_start_voice_recognition(void)
1645 gboolean state = TRUE;
1649 BT_TELEPHONY_CHECK_INITIALIZED();
1650 BT_TELEPHONY_CHECK_ENABLED();
1652 param = g_variant_new("(b)", &state);
1653 reply = __bluetooth_telephony_dbus_method_send(
1654 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1655 "SetVoiceDial", &err, param);
1658 BT_ERR("Error returned in method call\n");
1660 g_dbus_error_strip_remote_error(err);
1661 ret = __bt_telephony_get_error(err->message);
1665 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1668 g_variant_unref(reply);
1670 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1673 BT_EXPORT_API int bluetooth_telephony_stop_voice_recognition(void)
1679 gboolean state = FALSE;
1683 BT_TELEPHONY_CHECK_INITIALIZED();
1684 BT_TELEPHONY_CHECK_ENABLED();
1686 param = g_variant_new("(b)", &state);
1687 reply = __bluetooth_telephony_dbus_method_send(
1688 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1689 "SetVoiceDial", &err, param);
1692 BT_ERR("Error returned in method call\n");
1694 g_dbus_error_strip_remote_error(err);
1695 ret = __bt_telephony_get_error(err->message);
1699 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1702 g_variant_unref(reply);
1705 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1708 static void __bluetooth_telephony_sco_start_cb(GDBusProxy *proxy,
1709 GAsyncResult *res, gpointer user_data)
1711 GError *error = NULL;
1714 value = g_dbus_proxy_call_finish(proxy, res, &error);
1715 if (value == NULL) {
1716 if (error != NULL) {
1717 BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1718 error->code, error->message);
1719 g_clear_error(&error);
1721 BT_ERR("SCo Start Failed");
1725 BT_DBG("sco_start_cb : -");
1726 g_object_unref(proxy);
1727 g_variant_unref(value);
1730 BT_EXPORT_API int bluetooth_telephony_audio_open(void)
1732 GDBusConnection *conn;
1739 BT_TELEPHONY_CHECK_INITIALIZED();
1740 BT_TELEPHONY_CHECK_ENABLED();
1742 /* Because this API is async call, so can't use dbus SMACK */
1743 if (__bt_telephony_check_privilege() ==
1744 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1745 BT_ERR("Don't have a privilege to use this API");
1746 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1749 conn = _bt_gdbus_get_system_gconn();
1751 BT_DBG("No System Bus found\n");
1752 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1755 if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING)
1756 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
1758 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1759 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1760 HFP_AGENT_INTERFACE, NULL, &err);
1761 if (proxy == NULL) {
1762 BT_ERR("Unable to allocate new proxy");
1764 g_dbus_error_strip_remote_error(err);
1765 ret = __bt_telephony_get_error(err->message);
1766 BT_ERR("Error here %d\n", ret);
1770 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1773 g_dbus_proxy_call(proxy, "Play", NULL, G_DBUS_CALL_FLAGS_NONE,
1774 -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_start_cb, NULL);
1777 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1780 static void __bluetooth_telephony_sco_close_cb(GDBusProxy *proxy,
1781 GAsyncResult *res, gpointer user_data)
1783 GError *error = NULL;
1786 value = g_dbus_proxy_call_finish(proxy, res, &error);
1787 if (value == NULL) {
1788 if (error != NULL) {
1789 BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1790 error->code, error->message);
1791 g_clear_error(&error);
1793 BT_ERR("SCo close Failed");
1797 BT_DBG("sco_close_cb : -");
1798 g_object_unref(proxy);
1799 g_variant_unref(value);
1801 BT_EXPORT_API int bluetooth_telephony_audio_close(void)
1803 GDBusConnection *conn;
1810 BT_TELEPHONY_CHECK_INITIALIZED();
1811 BT_TELEPHONY_CHECK_ENABLED();
1813 /* Because this API is async call, so can't use dbus SMACK */
1814 if (__bt_telephony_check_privilege() ==
1815 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1816 BT_ERR("Don't have a privilege to use this API");
1817 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1820 conn = _bt_gdbus_get_system_gconn();
1822 BT_DBG("No System Bus found\n");
1823 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1826 if (telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1827 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
1829 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1830 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1831 HFP_AGENT_INTERFACE, NULL, &err);
1832 if (proxy == NULL) {
1833 BT_ERR("Unable to allocate new proxy");
1835 g_dbus_error_strip_remote_error(err);
1836 ret = __bt_telephony_get_error(err->message);
1837 BT_ERR("Error here %d\n", ret);
1841 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1844 g_dbus_proxy_call(proxy, "Stop", NULL, G_DBUS_CALL_FLAGS_NONE,
1845 -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_close_cb, NULL);
1847 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
1850 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1853 BT_EXPORT_API int bluetooth_telephony_call_remote_ringing(unsigned int call_id)
1857 BT_TELEPHONY_CHECK_INITIALIZED();
1858 BT_TELEPHONY_CHECK_ENABLED();
1861 BT_DBG("call_id = [%d]", call_id);
1863 /*Make sure SCO is already connected */
1864 ret = __bluetooth_telephony_send_call_status(
1865 CSD_CALL_STATUS_MO_ALERTING, call_id, NULL);
1866 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1867 BT_ERR("send call status Failed = [%d]", ret);
1868 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1871 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1874 BT_EXPORT_API int bluetooth_telephony_call_answered(unsigned int call_id,
1875 unsigned int bt_audio)
1880 BT_DBG("call_id = [%d]", call_id);
1882 BT_TELEPHONY_CHECK_INITIALIZED();
1883 BT_TELEPHONY_CHECK_ENABLED();
1885 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_ACTIVE,
1887 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1888 BT_ERR("send call status Failed = [%d]", ret);
1893 if (!bluetooth_telephony_is_sco_connected()) {
1894 ret = bluetooth_telephony_audio_open();
1896 BT_ERR("Audio connection call Failed[%d]", ret);
1897 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1906 BT_EXPORT_API int bluetooth_telephony_call_end(unsigned int call_id)
1911 BT_DBG("call_id = [%d]", call_id);
1913 BT_TELEPHONY_CHECK_INITIALIZED();
1914 BT_TELEPHONY_CHECK_ENABLED();
1916 if (telephony_info.call_count > 0)
1917 telephony_info.call_count = telephony_info.call_count - 1;
1919 if (telephony_info.call_count == 0) {
1920 if (bluetooth_telephony_is_sco_connected()) {
1921 ret = bluetooth_telephony_audio_close();
1922 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1923 BT_ERR(" Failed = [%d]", ret);
1927 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_MT_RELEASE,
1929 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1930 BT_ERR("send call status Failed = [%d]", ret);
1938 BT_EXPORT_API int bluetooth_telephony_call_held(unsigned int call_id)
1943 BT_DBG("call_id = [%d]", call_id);
1945 BT_TELEPHONY_CHECK_INITIALIZED();
1946 BT_TELEPHONY_CHECK_ENABLED();
1948 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_HOLD,
1950 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1951 BT_ERR("send call status Failed = [%d]", ret);
1957 BT_EXPORT_API int bluetooth_telephony_call_retrieved(unsigned int call_id)
1962 BT_DBG("call_id = [%d]", call_id);
1964 BT_TELEPHONY_CHECK_INITIALIZED();
1965 BT_TELEPHONY_CHECK_ENABLED();
1967 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_ACTIVE,
1969 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1970 BT_ERR("send call status Failed = [%d]", ret);
1976 BT_EXPORT_API int bluetooth_telephony_call_swapped(void *call_list,
1977 unsigned int call_count)
1981 GList *list = call_list;
1982 bt_telephony_call_status_info_t *call_status;
1986 BT_TELEPHONY_CHECK_INITIALIZED();
1987 BT_TELEPHONY_CHECK_ENABLED();
1990 BT_ERR("call_list is invalid");
1991 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1994 /* Because this API is async call, so can't use dbus SMACK */
1995 if (__bt_telephony_check_privilege() ==
1996 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1997 BT_ERR("Don't have a privilege to use this API");
1998 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
2001 BT_DBG(" call_count = [%d]", call_count);
2003 for (i = 0; i < call_count; i++) {
2004 call_status = g_list_nth_data(list, i);
2006 if (NULL == call_status)
2009 BT_DBG(" %d : Call id [%d] status[%d]", i,
2010 call_status->call_id,
2011 call_status->call_status);
2013 if (NULL != call_status->phone_number)
2014 DBG_SECURE("Number [%s]", call_status->phone_number);
2016 switch (call_status->call_status) {
2017 case BLUETOOTH_CALL_STATE_HELD:
2018 ret = __bluetooth_telephony_send_call_status(
2019 CSD_CALL_STATUS_HOLD,
2020 call_status->call_id,
2021 call_status->phone_number);
2022 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2023 BT_ERR("Failed = %d", ret);
2028 case BLUETOOTH_CALL_STATE_CONNECTED:
2029 ret = __bluetooth_telephony_send_call_status(
2030 CSD_CALL_STATUS_ACTIVE,
2031 call_status->call_id,
2032 call_status->phone_number);
2033 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2034 BT_ERR("Failed = [%d]", ret);
2040 if ((call_status->call_status <
2041 BLUETOOTH_CALL_STATE_NONE) ||
2042 (call_status->call_status >=
2043 BLUETOOTH_CALL_STATE_ERROR)) {
2044 BT_ERR("Unknown Call state");
2045 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
2051 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2054 BT_EXPORT_API int bluetooth_telephony_set_call_status(void *call_list,
2055 unsigned int call_count)
2061 ret = bluetooth_telephony_call_swapped(call_list, call_count);
2063 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2064 BT_ERR("Failed = [%d]", ret);
2068 telephony_info.call_count = call_count;
2071 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2074 BT_EXPORT_API int bluetooth_telephony_indicate_outgoing_call(
2075 const char *ph_number, unsigned int call_id,
2076 unsigned int bt_audio)
2081 const char *path = telephony_info.call_path;
2086 BT_TELEPHONY_CHECK_INITIALIZED();
2087 BT_TELEPHONY_CHECK_ENABLED();
2089 if (NULL == ph_number)
2090 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2092 param = g_variant_new("(ssi)", path, ph_number, call_id);
2093 reply = __bluetooth_telephony_dbus_method_send(
2094 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2095 "OutgoingCall", &err, param);
2098 BT_ERR("Error returned in method call\n");
2100 g_dbus_error_strip_remote_error(err);
2101 ret = __bt_telephony_get_error(err->message);
2105 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2108 g_variant_unref(reply);
2110 telephony_info.call_count++;
2111 BT_DBG(" ag_info.ag_call_count = [%d]", telephony_info.call_count);
2114 if (!bluetooth_telephony_is_sco_connected()) {
2115 ret = bluetooth_telephony_audio_open();
2117 BT_ERR(" Audio connection Failed = %d", ret);
2118 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2124 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2127 BT_EXPORT_API int bluetooth_telephony_indicate_incoming_call(
2128 const char *ph_number, unsigned int call_id)
2133 const char *path = telephony_info.call_path;
2138 BT_TELEPHONY_CHECK_INITIALIZED();
2139 BT_TELEPHONY_CHECK_ENABLED();
2141 if (NULL == ph_number)
2142 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2144 param = g_variant_new("(ssi)", path, ph_number, call_id);
2145 reply = __bluetooth_telephony_dbus_method_send(
2146 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2147 "IncomingCall", &err, param);
2150 BT_ERR("Error returned in method call\n");
2152 g_dbus_error_strip_remote_error(err);
2153 ret = __bt_telephony_get_error(err->message);
2157 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2160 g_variant_unref(reply);
2161 telephony_info.call_count++;
2162 BT_DBG("telephony_info.call_count = [%d]", telephony_info.call_count);
2164 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2167 BT_EXPORT_API int bluetooth_telephony_set_speaker_gain(
2168 unsigned short speaker_gain)
2177 BT_TELEPHONY_CHECK_INITIALIZED();
2178 BT_TELEPHONY_CHECK_ENABLED();
2180 BT_DBG("set speaker_gain= [%d]", speaker_gain);
2182 param = g_variant_new("(q)", speaker_gain);
2183 reply = __bluetooth_telephony_dbus_method_send(
2184 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2185 "SetSpeakerGain", &err, param);
2188 BT_ERR("Error returned in method call\n");
2190 g_dbus_error_strip_remote_error(err);
2191 ret = __bt_telephony_get_error(err->message);
2195 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2198 g_variant_unref(reply);
2200 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2203 BT_EXPORT_API int bluetooth_telephony_get_headset_volume(
2204 unsigned int *speaker_gain)
2213 BT_TELEPHONY_CHECK_INITIALIZED();
2214 BT_TELEPHONY_CHECK_ENABLED();
2216 reply = __bluetooth_telephony_dbus_method_send(
2217 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2218 "GetSpeakerGain", &err, NULL);
2221 BT_ERR("Error returned in method call\n");
2223 g_dbus_error_strip_remote_error(err);
2224 ret = __bt_telephony_get_error(err->message);
2228 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2230 g_variant_get(reply, "(q)", &gain);
2231 *speaker_gain = gain;
2232 BT_DBG("Get speaker_gain= [%d]", *speaker_gain);
2234 g_variant_unref(reply);
2237 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2240 BT_EXPORT_API int bluetooth_telephony_is_connected(gboolean *ag_connected)
2245 gboolean ag_connected_from_bt_agent;
2247 BT_CHECK_ENABLED(return);
2249 reply = __bluetooth_telephony_dbus_method_send(
2250 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2251 "IsConnected", &err, NULL);
2254 BT_ERR("Error returned in method call\n");
2256 g_dbus_error_strip_remote_error(err);
2257 ret = __bt_telephony_get_error(err->message);
2261 return BLUETOOTH_ERROR_INTERNAL;
2263 g_variant_get(reply, "(b)", &ag_connected_from_bt_agent);
2264 *ag_connected = ag_connected_from_bt_agent;
2266 BT_DBG("Conn Status: %s", *ag_connected ? "Connected" : "Disconnected");
2268 g_variant_unref(reply);
2270 return BLUETOOTH_ERROR_NONE;
2273 BT_EXPORT_API int bluetooth_telephony_set_active_headset(const char *remote_addr)
2275 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
2281 BT_CHECK_ENABLED(return);
2283 if (NULL == remote_addr)
2284 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2286 param = g_variant_new("(s)", remote_addr);
2287 reply = __bluetooth_telephony_dbus_method_send(
2288 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2289 "SwapHeadset", &err, param);
2292 BT_ERR("Error returned in method call\n");
2294 g_dbus_error_strip_remote_error(err);
2295 ret = __bt_telephony_get_error(err->message);
2299 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2302 g_variant_unref(reply);
2303 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2305 return BLUETOOTH_ERROR_NOT_SUPPORT;
2309 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
2310 const gchar *sender_name,
2311 const gchar *object_path,
2312 const gchar *interface_name,
2313 const gchar *signal_name,
2314 GVariant *parameters,
2321 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2322 GVariant *optional_param = NULL;
2324 g_variant_get(parameters, "(&o@a{sa{sv}})",
2325 &path, &optional_param);
2328 BT_ERR("Invalid adapter path");
2332 BT_INFO("Adapter Path = [%s]", path);
2333 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
2334 if (__bt_telephony_get_src_addr(optional_param))
2335 BT_ERR("Fail to get the local adapter address");
2337 ret = __bluetooth_telephony_register();
2338 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
2339 BT_ERR("__bluetooth_telephony_register failed");
2346 static int __bt_telephony_get_src_addr(GVariant *value)
2349 GVariant *param = NULL;
2352 /* signature a{sa{sv}} */
2353 g_variant_iter_init(&iter, value);
2354 while ((param = g_variant_iter_next_value(&iter))) {
2355 char *interface_name;
2356 GVariant *interface_var = NULL;
2357 GVariant *param_inner = NULL;
2359 g_variant_get(param, "{&s*}", &interface_name, &interface_var);
2360 g_variant_unref(param);
2362 BT_DBG("interface_name: %s", interface_name);
2363 /* format of interface_var: a{sv}*/
2364 if (strcasecmp(interface_name, BLUEZ_ADAPTER_INTERFACE) == 0) {
2365 GVariantIter iter_inner;
2367 g_variant_iter_init(&iter_inner, interface_var);
2368 while ((param_inner = g_variant_iter_next_value(&iter_inner))) {
2369 char *property_name;
2370 GVariant *property_var;
2372 g_variant_get(param_inner, "{&sv}",
2375 g_variant_unref(param_inner);
2377 if (strcasecmp(property_name, "Address") == 0) {
2378 const gchar *bd_addr;
2380 bd_addr = g_variant_get_string(
2383 src_addr = g_strdup(bd_addr);
2384 BT_DBG("Address: %s", src_addr);
2386 g_variant_unref(interface_var);
2387 g_variant_unref(property_var);
2390 g_variant_unref(property_var);
2393 g_variant_unref(interface_var);
2397 return BLUETOOTH_ERROR_NONE;