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"
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 char *src_addr = NULL;
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 TELEPHONY_APP_INTERFACE "org.tizen.csd.Call.Instance"
71 #define CSD_CALL_APP_PATH "/org/tizen/csd/%d"
72 #define HFP_NREC_STATUS_CHANGE "NrecStatusChanged"
73 #define HFP_ANSWER_CALL "Answer"
74 #define HFP_REJECT_CALL "Reject"
75 #define HFP_RELEASE_CALL "Release"
76 #define HFP_THREEWAY_CALL "Threeway"
78 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
80 /*Below Inrospection data is exposed to bluez from agent*/
81 static const gchar bt_telephony_introspection_xml[] =
83 " <interface name='org.tizen.csd.Call.Instance'>"
84 " <method name='SendDtmf'>"
85 " <arg type='s' name='dtmf' direction='in'/>"
87 " <method name='VendorCmd'>"
88 " <arg type='s' name='vendor' direction='in'/>"
93 #define BT_TELEPHONY_CHECK_ENABLED() \
95 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) { \
96 BT_ERR("BT is not enabled"); \
97 return BLUETOOTH_TELEPHONY_ERROR_NOT_ENABLED; \
101 static gboolean is_initialized = FALSE;
102 #define BT_TELEPHONY_CHECK_INITIALIZED() \
104 if (is_initialized == FALSE) { \
105 BT_ERR("Bluetooth telephony not initilized"); \
106 return BLUETOOTH_TELEPHONY_ERROR_NOT_INITIALIZED; \
110 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
111 const gchar *sender_name,
112 const gchar *object_path,
113 const gchar *interface_name,
114 const gchar *signal_name,
115 GVariant *parameters,
118 static int __bt_telephony_get_src_addr(GVariant *value);
120 static bt_telephony_info_t telephony_info;
121 static telephony_dbus_info_t telephony_dbus_info;
122 static gboolean is_active = FALSE;
124 /*Function Declaration*/
125 static int __bt_telephony_get_error(const char *error_message);
126 static void __bt_telephony_event_cb(int event, int result, void *param_data);
127 static GQuark __bluetooth_telephony_error_quark(void);
128 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
129 const char *interface, const char *method,
130 GError **err, GVariant *parameters);
131 static int __bluetooth_telephony_send_call_status(
132 bt_telephony_call_status_t call_status,
133 unsigned int call_id, const char *ph_number);
134 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
135 bluetooth_telephony_error_t error, const char *err_msg);
137 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
138 const gchar *sender_name,
139 const gchar *object_path,
140 const gchar *interface_name,
141 const gchar *signal_name,
142 GVariant *parameters,
145 static int __bluetooth_telephony_proxy_init(void);
146 static void __bluetooth_telephony_proxy_deinit(void);
147 static int __bluetooth_telephony_register(void);
148 static int __bluetooth_telephony_unregister(void);
150 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class);
151 static int __bluetooth_telephony_get_connected_device(void);
152 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void);
154 /*Function Definition*/
155 static void __bt_telephony_method(GDBusConnection *connection,
157 const gchar *object_path,
158 const gchar *interface_name,
159 const gchar *method_name,
160 GVariant *parameters,
161 GDBusMethodInvocation *invocation,
166 BT_INFO("method %s", method_name);
167 BT_INFO("object_path %s", object_path);
169 if (g_strcmp0(method_name, "SendDtmf") == 0) {
171 telephony_event_dtmf_t call_data = { 0, };
173 g_variant_get(parameters, "(&s)", &dtmf);
176 BT_ERR("Number dial failed");
177 __bluetooth_telephony_error(invocation,
178 BLUETOOTH_TELEPHONY_ERROR_INVALID_DTMF,
181 DBG_SECURE("Dtmf = %s", dtmf);
183 call_data.dtmf = g_strdup(dtmf);
184 __bt_telephony_event_cb(
185 BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF,
186 BLUETOOTH_TELEPHONY_ERROR_NONE,
189 g_free(call_data.dtmf);
191 g_dbus_method_invocation_return_value(invocation, NULL);
193 } else if (g_strcmp0(method_name, "VendorCmd") == 0) {
196 g_variant_get(parameters, "(&s)", &at_cmd);
197 BT_INFO("Vendor %s", at_cmd);
198 if (at_cmd == NULL) {
199 BT_ERR("Vendor command is NULL\n");
200 __bluetooth_telephony_error(invocation,
201 BLUETOOTH_TELEPHONY_ERROR_APPLICATION,
202 "Invalid at vendor cmd");
204 DBG_SECURE("Vendor AT cmd = %s", at_cmd);
206 __bt_telephony_event_cb(
207 BLUETOOTH_EVENT_TELEPHONY_VENDOR_AT_CMD,
208 BLUETOOTH_TELEPHONY_ERROR_NONE,
211 g_dbus_method_invocation_return_value(invocation, NULL);
218 static const GDBusInterfaceVTable method_table = {
219 __bt_telephony_method,
224 static int __bt_telephony_get_error(const char *error_message)
226 if (error_message == NULL) {
227 BT_ERR("Error message NULL");
228 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
231 BT_ERR("Error message = %s", error_message);
232 if (g_strcmp0(error_message, "NotAvailable") == 0)
233 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
234 else if (g_strcmp0(error_message, "NotConnected") == 0)
235 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
236 else if (g_strcmp0(error_message, "InProgress") == 0)
237 return BLUETOOTH_TELEPHONY_ERROR_BUSY;
238 else if (g_strcmp0(error_message, "InvalidArguments") == 0)
239 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
240 else if (g_strcmp0(error_message, "AlreadyExists") == 0)
241 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_EXSIST;
242 else if (g_strcmp0(error_message, "Already Connected") == 0)
243 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
244 else if (g_strcmp0(error_message, "No memory") == 0)
245 return BLUETOOTH_TELEPHONY_ERROR_NO_MEMORY;
246 else if (g_strcmp0(error_message, "I/O error") == 0)
247 return BLUETOOTH_TELEPHONY_ERROR_I_O_ERROR;
248 else if (g_strcmp0(error_message,
249 "Operation currently not available") == 0)
250 return BLUETOOTH_TELEPHONY_ERROR_OPERATION_NOT_AVAILABLE;
251 else if (g_strrstr(error_message, BT_ACCESS_DENIED_MSG))
252 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
254 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
257 static int __bt_telephony_check_privilege(void)
264 reply = __bluetooth_telephony_dbus_method_send(
265 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
266 "CheckPrivilege", &err, NULL);
269 BT_ERR("Error returned in method call");
271 g_dbus_error_strip_remote_error(err);
272 ret = __bt_telephony_get_error(err->message);
276 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
278 g_variant_unref(reply);
281 return BLUETOOTH_TELEPHONY_ERROR_NONE;
284 static void __bt_telephony_event_cb(int event, int result, void *param_data)
286 telephony_event_param_t bt_event = { 0, };
288 bt_event.event = event;
289 bt_event.result = result;
290 bt_event.param_data = param_data;
292 ret_if(telephony_info.cb == NULL);
293 telephony_info.cb(bt_event.event, &bt_event, telephony_info.user_data);
297 static GQuark __bluetooth_telephony_error_quark(void)
299 static GQuark quark = 0;
301 quark = g_quark_from_static_string("telephony");
306 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
307 const char *interface, const char *method,
308 GError **err, GVariant *parameters)
310 #if defined(TIZEN_PROFILE_WEARABLE) || defined(TIZEN_PROFILE_IVI)
317 GDBusConnection *conn;
321 conn = telephony_dbus_info.conn;
322 retv_if(conn == NULL, NULL);
324 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
325 NULL, HFP_AGENT_SERVICE, path, interface, NULL, err);
327 BT_ERR("Unable to allocate new proxy");
331 reply = g_dbus_proxy_call_sync(proxy, method, parameters,
332 G_DBUS_CALL_FLAGS_NONE, timeout, NULL, err);
334 g_object_unref(proxy);
339 static int __bluetooth_telephony_send_call_status(
340 bt_telephony_call_status_t call_status,
341 unsigned int call_id, const char *ph_number)
346 char *path = g_strdup(telephony_info.call_path);
352 if (NULL == ph_number)
353 phone_number = g_strdup("");
355 phone_number = g_strdup(ph_number);
357 param = g_variant_new("(ssii)", path, phone_number,
358 call_status, call_id);
359 reply = __bluetooth_telephony_dbus_method_send(
360 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
361 "ChangeCallStatus", &err, param);
364 g_free(phone_number);
367 BT_ERR("Error returned in method call");
369 g_dbus_error_strip_remote_error(err);
370 ret = __bt_telephony_get_error(err->message);
374 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
377 g_variant_unref(reply);
379 return BLUETOOTH_TELEPHONY_ERROR_NONE;
382 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
383 bluetooth_telephony_error_t error, const char *err_msg)
385 g_dbus_method_invocation_return_error(invocation,
386 BLUETOOTH_TELEPHONY_ERROR, error,
390 static void __bluetooth_telephony_answer_call(GVariant *var)
392 telephony_event_callid_t call_data = { 0, };
397 g_variant_get(var, "(u)", &callid);
398 BT_DBG("call_id = [%d]", callid);
399 call_data.callid = callid;
401 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL,
402 BLUETOOTH_TELEPHONY_ERROR_NONE,
407 static void __bluetooth_telephony_release_call(GVariant *var)
409 telephony_event_callid_t call_data = { 0, };
414 g_variant_get(var, "(u)", &callid);
415 BT_DBG("call_id = [%d]", callid);
416 call_data.callid = callid;
418 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL,
419 BLUETOOTH_TELEPHONY_ERROR_NONE,
424 static void __bluetooth_telephony_reject_call(GVariant *var)
426 telephony_event_callid_t call_data = { 0, };
431 g_variant_get(var, "(u)", &callid);
432 BT_DBG("call_id = [%d]", callid);
433 call_data.callid = callid;
435 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL,
436 BLUETOOTH_TELEPHONY_ERROR_NONE,
441 static void __bluetooth_telephony_threeway_call(GVariant *var)
444 unsigned int chld_value;
448 g_variant_get(var, "(u)", &chld_value);
449 BT_DBG("chld value = [%d]", chld_value);
451 switch (chld_value) {
453 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL;
456 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL;
459 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL;
462 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL;
465 BT_ERR("Invalid CHLD command");
469 __bt_telephony_event_cb(event,
470 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
474 static void __bluetooth_handle_nrec_status_change(GVariant *var)
476 gboolean status = FALSE;
478 g_variant_get(var, "(b)", &status);
479 BT_INFO("NREC status = %d", status);
481 __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED,
482 BLUETOOTH_TELEPHONY_ERROR_NONE, (void *)&status);
486 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
487 const gchar *sender_name,
488 const gchar *object_path,
489 const gchar *interface_name,
490 const gchar *signal_name,
491 GVariant *parameters,
496 if (strcasecmp(interface_name, HFP_AGENT_SERVICE) == 0) {
497 if (strcasecmp(signal_name, HFP_NREC_STATUS_CHANGE) == 0)
498 __bluetooth_handle_nrec_status_change(parameters);
499 else if (strcasecmp(signal_name, HFP_ANSWER_CALL) == 0)
500 __bluetooth_telephony_answer_call(parameters);
501 else if (strcasecmp(signal_name, HFP_REJECT_CALL) == 0)
502 __bluetooth_telephony_reject_call(parameters);
503 else if (strcasecmp(signal_name, HFP_RELEASE_CALL) == 0)
504 __bluetooth_telephony_release_call(parameters);
505 else if (strcasecmp(signal_name, HFP_THREEWAY_CALL) == 0)
506 __bluetooth_telephony_threeway_call(parameters);
507 } else if (strcasecmp(interface_name, BLUEZ_HEADSET_INTERFACE) == 0) {
508 if (strcasecmp(signal_name, "PropertyChanged") == 0) {
512 g_variant_get(parameters, "(&sv)", &property, &values);
513 BT_DBG("Property: %s", property);
515 if (strcasecmp(property, "State") == 0) {
517 state = (gchar *)g_variant_get_string(values, NULL);
520 BT_ERR("State is null");
523 BT_DBG("state: %s", state);
524 if (g_strcmp0(state, "connected") == 0) {
525 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
526 } else if (g_strcmp0(state, "playing") == 0) {
527 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
528 } else if (g_strcmp0(state, "disconnected") == 0) {
529 /* Headset state: playing -> disconnected */
530 if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING) {
531 __bt_telephony_event_cb(
532 BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
533 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
536 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
538 } else if (strcasecmp(property, "Connected") == 0) {
539 gboolean connected = FALSE;
540 char *dev_addr = NULL;
541 connected = g_variant_get_boolean(values);
542 BT_INFO("connected %d", connected);
544 /*Get device address*/
545 if (object_path != NULL)
546 dev_addr = strstr(object_path, "dev_");
548 if (dev_addr != NULL) {
550 g_strlcpy(telephony_info.address,
552 sizeof(telephony_info.address));
553 g_strdelimit(telephony_info.address, "_", ':');
554 BT_DBG("address is %s",
555 telephony_info.address);
557 telephony_info.headset_state =
558 BLUETOOTH_STATE_CONNECTED;
560 if (telephony_dbus_info.proxy != NULL) {
561 g_object_unref(telephony_dbus_info.proxy);
562 telephony_dbus_info.proxy = NULL;
565 telephony_dbus_info.proxy =
566 __bluetooth_telephony_get_connected_device_proxy();
568 BT_INFO("Headset Connected");
570 __bt_telephony_event_cb(
571 BLUETOOTH_EVENT_TELEPHONY_HFP_CONNECTED,
572 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
574 } else { /*Device disconnected*/
575 memset(telephony_info.address, 0x00,
576 sizeof(telephony_info.address));
577 telephony_info.headset_state =
578 BLUETOOTH_STATE_DISCONNETED;
580 if (telephony_dbus_info.proxy != NULL) {
581 g_object_unref(telephony_dbus_info.proxy);
582 telephony_dbus_info.proxy = NULL;
585 BT_INFO("Headset Disconnected");
587 __bt_telephony_event_cb(
588 BLUETOOTH_EVENT_TELEPHONY_HFP_DISCONNECTED,
589 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
591 } else if (strcasecmp(property, "SpeakerGain") == 0) {
592 unsigned int spkr_gain;
593 guint16 gain = g_variant_get_uint16(values);
595 spkr_gain = (unsigned int)gain;
596 BT_DBG("spk_gain[%d]", spkr_gain);
598 __bt_telephony_event_cb(
599 BLUETOOTH_EVENT_TELEPHONY_SET_SPEAKER_GAIN,
600 BLUETOOTH_TELEPHONY_ERROR_NONE,
602 } else if (strcasecmp(property, "MicrophoneGain") == 0) {
603 unsigned int mic_gain;
604 guint16 gain = g_variant_get_uint16(values);
606 mic_gain = (unsigned int)gain;
607 BT_DBG("mic_gain[%d]", mic_gain);
609 __bt_telephony_event_cb(
610 BLUETOOTH_EVENT_TELEPHONY_SET_MIC_GAIN,
611 BLUETOOTH_TELEPHONY_ERROR_NONE,
613 } else if (strcasecmp(property, "Playing") == 0) {
614 gboolean audio_sink_playing;
616 audio_sink_playing = g_variant_get_boolean(values);
617 if (audio_sink_playing) {
618 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
619 __bt_telephony_event_cb(
620 BLUETOOTH_EVENT_TELEPHONY_AUDIO_CONNECTED,
621 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
623 telephony_info.headset_state =
624 BLUETOOTH_STATE_CONNECTED;
625 __bt_telephony_event_cb(
626 BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
627 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
631 g_variant_unref(values);
637 static GDBusNodeInfo *__bt_telephony_create_method_node_info
638 (const gchar *introspection_data)
641 GDBusNodeInfo *node_info = NULL;
643 if (introspection_data == NULL)
646 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
649 BT_ERR("Unable to create node: %s", err->message);
656 int __bluetooth_telephony_register_object(int reg, GDBusNodeInfo *node_info)
658 static guint bt_tel_id = 0;
659 GError *error = NULL;
663 if (node_info == NULL)
664 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
666 path = g_strdup(telephony_info.call_path);
667 BT_DBG("path is [%s]", path);
669 bt_tel_id = g_dbus_connection_register_object(telephony_dbus_info.conn,
670 path, node_info->interfaces[0],
675 if (bt_tel_id == 0) {
676 BT_ERR("Failed to register: %s", error->message);
678 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
682 g_dbus_connection_unregister_object(telephony_dbus_info.conn,
688 return BLUETOOTH_TELEPHONY_ERROR_NONE;
691 static int __bluetooth_telephony_proxy_init(void)
695 GDBusNodeInfo *node_info;
697 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
698 TELEPHONY_APP_INTERFACE,
699 G_BUS_NAME_OWNER_FLAGS_NONE,
702 BT_DBG("owner_id is [%d]", owner_id);
704 node_info = __bt_telephony_create_method_node_info(
705 bt_telephony_introspection_xml);
707 if (node_info == NULL) {
708 BT_ERR("node_info NULL");
709 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
711 if (__bluetooth_telephony_register_object(TRUE, node_info) !=
712 BLUETOOTH_TELEPHONY_ERROR_NONE) {
713 BT_ERR("Registation of Method Failed");
714 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
718 return BLUETOOTH_TELEPHONY_ERROR_NONE;
721 static void __bluetooth_telephony_proxy_deinit(void)
725 __bluetooth_telephony_register_object(FALSE, NULL);
731 static int __bluetooth_telephony_register(void)
736 char *path = g_strdup(telephony_info.call_path);
741 param = g_variant_new("(ss)", path, src_addr);
742 BT_DBG("Path[%s] Src_Address[%s]", path, src_addr);
744 reply = __bluetooth_telephony_dbus_method_send(
745 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
746 "RegisterApplication", &err, param);
751 BT_ERR("Error returned in method call");
753 g_dbus_error_strip_remote_error(err);
754 ret = __bt_telephony_get_error(err->message);
755 BT_ERR("Error here %d\n", ret);
759 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
762 g_variant_unref(reply);
763 BT_DBG("__bluetooth_telephony_register completed");
765 return BLUETOOTH_TELEPHONY_ERROR_NONE;
768 static int __bluetooth_telephony_unregister(void)
773 char *path = g_strdup(telephony_info.call_path);
778 param = g_variant_new("(s)", path);
779 reply = __bluetooth_telephony_dbus_method_send(
780 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
781 "UnregisterApplication", &err, param);
786 BT_ERR("Error returned in method call");
788 g_dbus_error_strip_remote_error(err);
789 ret = __bt_telephony_get_error(err->message);
793 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
796 g_variant_unref(reply);
797 BT_DBG("__bluetooth_telephony_unregister completed");
799 return BLUETOOTH_TELEPHONY_ERROR_NONE;
802 #if defined(TIZEN_PROFILE_WEARABLE) || defined(TIZEN_PROFILE_IVI)
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;
844 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class)
846 gboolean flag = FALSE;
849 switch ((device_class & 0x1f00) >> 8) {
851 switch ((device_class & 0xfc) >> 2) {
869 /* Tizen Wearable device */
871 switch ((device_class & 0xfc) >> 2) {
872 case 0x01: /* Wrist Watch */
880 BT_DBG("[%d]", flag);
885 static gboolean __bluetooth_telephony_is_headset_by_uuid(gchar **uuids)
889 unsigned int service = 0;
893 retv_if(uuids == NULL, FALSE);
895 for (i = 0; uuids[i] != NULL; i++) {
896 parts = g_strsplit(uuids[i], "-", -1);
898 if (parts == NULL || parts[0] == NULL) {
903 service = g_ascii_strtoull(parts[0], NULL, 16);
906 if (service == BLUETOOTH_HS_PROFILE_UUID ||
907 service == BLUETOOTH_HF_PROFILE_UUID)
916 static int __bluetooth_telephony_get_connected_device(void)
918 GDBusConnection *conn;
919 GDBusProxy *headset_agent_proxy = NULL;
920 GDBusProxy *manager_proxy = NULL;
921 GDBusProxy *proxy = NULL;
922 GVariant *reply = NULL;
923 GVariant *getall = NULL;
924 GVariant *isPlayingReply = NULL;
925 GVariant *isConnectedReply = NULL;
926 GVariant *param = NULL;
927 GVariant *var_path = NULL;
928 GVariant *path_values = NULL;
929 GVariant *value = NULL;
930 GError *error = NULL;
932 GVariantIter iter_path;
933 GVariantIter property_iter;
934 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
937 conn = _bt_gdbus_get_system_gconn();
938 retv_if(conn == NULL, BLUETOOTH_TELEPHONY_ERROR_INTERNAL);
940 manager_proxy = g_dbus_proxy_new_sync(
941 conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
942 BLUEZ_SERVICE_NAME, "/",
943 BLUEZ_MANAGER_INTERFACE, NULL, &error);
944 if (manager_proxy == NULL) {
945 BT_ERR("Unable to allocate new proxy \n");
946 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
948 g_dbus_error_strip_remote_error(error);
949 ret = __bt_telephony_get_error(error->message);
950 BT_ERR("Error here %d\n", ret);
956 /* Synchronous call */
957 reply = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects", NULL,
958 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
959 g_object_unref(manager_proxy);
962 BT_ERR("Can't get managed objects");
963 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
965 g_dbus_error_strip_remote_error(error);
966 ret = __bt_telephony_get_error(error->message);
967 BT_ERR("Error here %d\n", ret);
973 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
974 g_variant_iter_init(&iter, reply);
976 while ((param = g_variant_iter_next_value(&iter))) {
977 g_variant_iter_init(&iter_path, param);
979 while ((var_path = g_variant_iter_next_value(&iter_path))) {
981 uint32_t device_class = 0;
982 gboolean playing = FALSE;
983 gboolean connected = FALSE;
984 char *object_path = NULL;
985 gchar *address = NULL;
987 gchar **uuids = NULL;
988 GVariant *getall_param = NULL;
990 g_variant_get(var_path, "{&o*}", &object_path,
992 g_variant_unref(path_values); /* path_values unused*/
994 proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
995 G_DBUS_PROXY_FLAGS_NONE, NULL,
996 BLUEZ_SERVICE_NAME, object_path,
997 BLUEZ_PROPERTIES_INTERFACE, NULL, &error);
999 BT_ERR("Unable to allocate new proxy \n");
1000 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1002 g_dbus_error_strip_remote_error(error);
1003 ret = __bt_telephony_get_error(error->message);
1004 BT_ERR("Error here %d\n", ret);
1005 g_error_free(error);
1011 getall_param = g_variant_new("s", BLUEZ_DEVICE_INTERFACE);
1012 getall = g_dbus_proxy_call_sync(proxy,
1013 "GetAll", getall_param,
1014 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1015 g_object_unref(proxy);
1018 BT_ERR("Can't get managed objects");
1019 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1021 g_dbus_error_strip_remote_error(error);
1022 ret = __bt_telephony_get_error(error->message);
1023 BT_ERR("Error here %d\n", ret);
1024 g_error_free(error);
1029 g_variant_iter_init(&property_iter, getall);
1031 while (g_variant_iter_loop(&property_iter, "{&sv}", &key, &value)) {
1032 if (!g_strcmp0(key, "Class")) {
1033 device_class = g_variant_get_uint32(value);
1034 BT_DBG("Device Class: %d", device_class);
1035 } else if (!g_strcmp0(key, "UUID")) {
1037 uuids = (gchar **)g_variant_get_strv(value, &len);
1038 BT_DBG_UUID(uuids, len, i);
1039 } else if (!g_strcmp0(key, "Address")) {
1040 address = (gchar *)g_variant_get_string(
1043 BT_DBG("Device Class: %s", address);
1045 g_variant_unref(value);
1047 g_variant_unref(getall);
1049 if (device_class == 0) {
1050 BT_DBG("COD is NULL (maybe paired by nfc)... Checking UUIDs");
1051 if (!__bluetooth_telephony_is_headset_by_uuid(uuids)) {
1052 BT_DBG("UUID checking completed. None HF device");
1055 BT_DBG("UUID checking completed. HF device");
1057 if (!__bluetooth_telephony_is_headset(device_class))
1061 /* this is headset; Check for Connection */
1062 headset_agent_proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1063 G_DBUS_PROXY_FLAGS_NONE, NULL,
1064 HFP_AGENT_SERVICE, object_path,
1065 HFP_AGENT_INTERFACE, NULL, &error);
1066 if (headset_agent_proxy == NULL) {
1067 BT_ERR("Unable to allocate new headset_agent_proxy");
1068 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1070 g_dbus_error_strip_remote_error(error);
1071 ret = __bt_telephony_get_error(error->message);
1072 BT_ERR("Error here %d\n", ret);
1073 g_error_free(error);
1078 isConnectedReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1079 "IsConnected", NULL,
1080 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1083 if (!isConnectedReply) {
1084 BT_ERR("Can't get managed objects");
1085 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1087 g_dbus_error_strip_remote_error(error);
1088 ret = __bt_telephony_get_error(error->message);
1089 BT_ERR("Error here %d\n", ret);
1090 g_error_free(error);
1094 connected = g_variant_get_boolean(isConnectedReply);
1095 g_variant_unref(isConnectedReply);
1098 g_strlcpy(telephony_info.address,
1100 sizeof(telephony_info.address));
1102 isPlayingReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1104 G_DBUS_CALL_FLAGS_NONE,
1106 if (!isPlayingReply) {
1107 BT_ERR("Can't get managed objects");
1108 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1110 g_dbus_error_strip_remote_error(error);
1111 ret = __bt_telephony_get_error(error->message);
1112 BT_ERR("Error here %d\n", ret);
1113 g_error_free(error);
1116 playing = g_variant_get_boolean(isPlayingReply);
1117 g_variant_unref(isPlayingReply);
1120 telephony_info.headset_state =
1121 BLUETOOTH_STATE_PLAYING;
1123 telephony_info.headset_state =
1124 BLUETOOTH_STATE_CONNECTED;
1131 g_object_unref(headset_agent_proxy);
1132 g_variant_unref(var_path);
1134 g_variant_unref(param);
1138 if (headset_agent_proxy)
1139 g_object_unref(headset_agent_proxy);
1141 g_variant_unref(reply);
1143 g_variant_unref(var_path);
1145 g_variant_unref(param);
1150 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void)
1152 GDBusProxy *proxy = NULL;
1153 GError *error = NULL;
1157 if (strlen(telephony_info.address) == 0)
1158 __bluetooth_telephony_get_connected_device();
1160 if (strlen(telephony_info.address) == 0)
1163 proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1164 G_DBUS_PROXY_FLAGS_NONE, NULL,
1165 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1166 HFP_AGENT_INTERFACE, NULL, &error);
1167 if (proxy == NULL) {
1168 BT_ERR("Unable to allocate new proxy");
1170 g_dbus_error_strip_remote_error(error);
1171 ret = __bt_telephony_get_error(error->message);
1172 BT_ERR("Error here %d\n", ret);
1173 g_error_free(error);
1182 int __bt_telephony_subscribe_adapter_signal(GDBusConnection *conn,
1188 static int subscribe_adapter_id = -1;
1189 if (subscribe == TRUE) {
1190 if (subscribe_adapter_id == -1) {
1191 subscribe_adapter_id = g_dbus_connection_signal_subscribe(conn,
1192 NULL, "org.freedesktop.DBus.ObjectManager",
1193 "InterfacesAdded", NULL, NULL, 0,
1194 __bt_telephony_adapter_filter,
1197 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1199 if (subscribe_adapter_id != -1) {
1200 g_dbus_connection_signal_unsubscribe(conn,
1201 subscribe_adapter_id);
1202 subscribe_adapter_id = -1;
1204 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1208 int __bt_telephony_event_subscribe_signal(GDBusConnection *conn,
1214 static int subscribe_event1_id = -1;
1215 static int subscribe_event2_id = -1;
1216 static int subscribe_event3_id = -1;
1217 static int subscribe_event4_id = -1;
1218 static int subscribe_event5_id = -1;
1219 static int subscribe_event6_id = -1;
1220 if (subscribe == TRUE) {
1221 if (subscribe_event1_id == -1) {
1222 subscribe_event1_id = g_dbus_connection_signal_subscribe(conn,
1223 NULL, BLUEZ_HEADSET_INTERFACE,
1224 "PropertyChanged", NULL, NULL, 0,
1225 __bluetooth_telephony_event_filter,
1228 if (subscribe_event2_id == -1) {
1229 subscribe_event2_id = g_dbus_connection_signal_subscribe(conn,
1230 NULL, HFP_AGENT_SERVICE,
1231 HFP_NREC_STATUS_CHANGE, NULL, NULL, 0,
1232 __bluetooth_telephony_event_filter,
1236 if (subscribe_event3_id == -1) {
1237 subscribe_event3_id = g_dbus_connection_signal_subscribe(conn,
1238 NULL, HFP_AGENT_SERVICE,
1239 HFP_ANSWER_CALL, NULL, NULL, 0,
1240 __bluetooth_telephony_event_filter,
1243 if (subscribe_event4_id == -1) {
1244 subscribe_event4_id = g_dbus_connection_signal_subscribe(conn,
1245 NULL, HFP_AGENT_SERVICE,
1246 HFP_REJECT_CALL, NULL, NULL, 0,
1247 __bluetooth_telephony_event_filter,
1250 if (subscribe_event5_id == -1) {
1251 subscribe_event5_id = g_dbus_connection_signal_subscribe(conn,
1252 NULL, HFP_AGENT_SERVICE,
1253 HFP_RELEASE_CALL, NULL, NULL, 0,
1254 __bluetooth_telephony_event_filter,
1257 if (subscribe_event6_id == -1) {
1258 subscribe_event6_id = g_dbus_connection_signal_subscribe(conn,
1259 NULL, HFP_AGENT_SERVICE,
1260 HFP_THREEWAY_CALL, NULL, NULL, 0,
1261 __bluetooth_telephony_event_filter,
1265 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1267 if (subscribe_event1_id != -1) {
1268 g_dbus_connection_signal_unsubscribe(conn,
1269 subscribe_event1_id);
1270 subscribe_event1_id = -1;
1272 if (subscribe_event2_id != -1) {
1273 g_dbus_connection_signal_unsubscribe(conn,
1274 subscribe_event2_id);
1275 subscribe_event2_id = -1;
1277 if (subscribe_event3_id != -1) {
1278 g_dbus_connection_signal_unsubscribe(conn,
1279 subscribe_event3_id);
1280 subscribe_event3_id = -1;
1282 if (subscribe_event4_id != -1) {
1283 g_dbus_connection_signal_unsubscribe(conn,
1284 subscribe_event4_id);
1285 subscribe_event4_id = -1;
1287 if (subscribe_event5_id != -1) {
1288 g_dbus_connection_signal_unsubscribe(conn,
1289 subscribe_event5_id);
1290 subscribe_event5_id = -1;
1292 if (subscribe_event6_id != -1) {
1293 g_dbus_connection_signal_unsubscribe(conn,
1294 subscribe_event6_id);
1295 subscribe_event6_id = -1;
1297 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1301 BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
1304 bluetooth_device_address_t loc_address = { {0} };
1305 char src_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1306 int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
1307 GError *error = NULL;
1311 if (is_initialized == TRUE) {
1312 BT_ERR("Bluetooth telephony already initilized");
1313 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1316 is_initialized = TRUE;
1318 telephony_dbus_info.conn = _bt_gdbus_init_system_gconn();
1319 if (!telephony_dbus_info.conn) {
1320 is_initialized = FALSE;
1321 BT_ERR("Could not get DBus Connection");
1322 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1326 snprintf(telephony_info.call_path, sizeof(telephony_info.call_path),
1327 CSD_CALL_APP_PATH, getpid());
1328 BT_INFO("Call Path = %s", telephony_info.call_path);
1329 memset(telephony_info.address, 0x00, sizeof(telephony_info.address));
1331 if (__bluetooth_telephony_proxy_init()) {
1332 BT_ERR("__bluetooth_telephony_proxy_init failed\n");
1333 telephony_dbus_info.conn = NULL;
1334 is_initialized = FALSE;
1335 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1338 telephony_dbus_info.manager_proxy = g_dbus_proxy_new_sync(
1339 telephony_dbus_info.conn,
1340 G_DBUS_PROXY_FLAGS_NONE, NULL,
1341 BLUEZ_SERVICE_NAME, "/",
1342 BLUEZ_MANAGER_INTERFACE, NULL, &error);
1343 if (telephony_dbus_info.manager_proxy == NULL) {
1344 BT_ERR("Could not create a manager proxy");
1345 __bluetooth_telephony_proxy_deinit();
1346 telephony_dbus_info.conn = NULL;
1347 is_initialized = FALSE;
1349 g_dbus_error_strip_remote_error(error);
1350 ret = __bt_telephony_get_error(error->message);
1351 BT_ERR("Error here %d\n", ret);
1352 g_error_free(error);
1355 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1358 telephony_dbus_info.dbus_proxy = g_dbus_proxy_new_sync(
1359 telephony_dbus_info.conn,
1360 G_DBUS_PROXY_FLAGS_NONE, NULL,
1361 BT_EVENT_FREEDESKTOP, BT_FREEDESKTOP_PATH,
1362 BT_EVENT_FREEDESKTOP, NULL, &error);
1363 if (NULL == telephony_dbus_info.dbus_proxy) {
1364 __bluetooth_telephony_proxy_deinit();
1365 telephony_dbus_info.conn = NULL;
1366 g_object_unref(telephony_dbus_info.manager_proxy);
1367 telephony_dbus_info.manager_proxy = NULL;
1368 is_initialized = FALSE;
1369 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1372 if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, TRUE) != 0) {
1373 BT_ERR("Fail to Subscribe Adapter Signal");
1377 /*Callback and user applicaton data*/
1378 telephony_info.cb = cb;
1379 telephony_info.user_data = user_data;
1380 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1382 if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, TRUE) != 0) {
1383 BT_ERR("Fail to Subscribe telephony event Signal");
1387 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED)
1388 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1390 /*Bluetooth is active, therefore set the flag */
1393 ret = bluetooth_get_local_address(&loc_address);
1394 if (ret != BLUETOOTH_ERROR_NONE) {
1395 BT_ERR("Fail to get local address\n");
1396 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1399 _bt_convert_addr_type_to_string(src_address, loc_address.addr);
1400 src_addr = g_strdup(src_address);
1402 ret = __bluetooth_telephony_register();
1403 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1404 BT_ERR("__bluetooth_telephony_register failed\n");
1408 #if defined(TIZEN_PROFILE_WEARABLE) || defined(TIZEN_PROFILE_IVI)
1409 __bluetooth_telephony_init_headset_state();
1415 bluetooth_telephony_deinit();
1419 BT_EXPORT_API int bluetooth_telephony_deinit(void)
1422 BT_TELEPHONY_CHECK_INITIALIZED();
1424 is_initialized = FALSE;
1426 if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, FALSE) != 0)
1427 BT_ERR("Fail to UnSubscribe telephony event Signal");
1429 if (bluetooth_check_adapter() != BLUETOOTH_ADAPTER_DISABLED ||
1430 bluetooth_check_adapter_le() != BLUETOOTH_ADAPTER_LE_DISABLED)
1431 __bluetooth_telephony_unregister();
1433 __bluetooth_telephony_proxy_deinit();
1435 telephony_info.cb = NULL;
1436 telephony_info.user_data = NULL;
1437 telephony_info.call_count = 0;
1438 telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1440 /* Remove BT enabled signal */
1441 if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, FALSE) != 0)
1442 BT_ERR("Fail to UnSubscribe Adapter event Signal");
1447 if (telephony_dbus_info.manager_proxy != NULL) {
1448 g_object_unref(telephony_dbus_info.manager_proxy);
1449 telephony_dbus_info.manager_proxy = NULL;
1452 if (telephony_dbus_info.conn != NULL)
1453 telephony_dbus_info.conn = NULL;
1455 if (telephony_dbus_info.dbus_proxy != NULL) {
1456 g_object_unref(telephony_dbus_info.dbus_proxy);
1457 telephony_dbus_info.dbus_proxy = NULL;
1461 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1464 BT_EXPORT_API gboolean bluetooth_telephony_is_sco_connected(void)
1468 gboolean status = FALSE;
1472 retv_if(is_initialized == FALSE, FALSE);
1473 retv_if(bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED, FALSE);
1475 reply = __bluetooth_telephony_dbus_method_send(
1476 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1477 "IsPlaying", &err, NULL);
1480 BT_ERR("Error returned in method call\n");
1482 BT_ERR("Error message = %s", err->message);
1487 g_variant_get(reply, "(b)", &status);
1488 g_variant_unref(reply);
1490 #if defined(TIZEN_PROFILE_WEARABLE) || defined(TIZEN_PROFILE_IVI)
1491 if (status == TRUE && telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1492 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
1495 BT_INFO("SCO Connected Status = [%d]", status);
1499 BT_EXPORT_API int bluetooth_telephony_is_nrec_enabled(gboolean *status)
1504 GVariant *param_inner;
1508 BT_TELEPHONY_CHECK_INITIALIZED();
1509 BT_TELEPHONY_CHECK_ENABLED();
1512 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1514 if (telephony_info.headset_state == BLUETOOTH_STATE_DISCONNETED)
1515 return BLUETOOTH_TELEPHONY_ERROR_AUDIO_NOT_CONNECTED;
1517 reply = __bluetooth_telephony_dbus_method_send(
1518 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1519 "GetProperties", &err, NULL);
1522 BT_ERR("Error returned in method call\n");
1524 BT_DBG("Error message = %s", err->message);
1527 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1530 g_variant_iter_init(&iter, reply);
1531 while ((param_inner = g_variant_iter_next_value(&iter))) {
1534 value = g_variant_lookup_value(param_inner,
1535 "nrec", G_VARIANT_TYPE_BOOLEAN);
1537 BT_DBG("Property NREC Found");
1538 *status = g_variant_get_boolean(value);
1539 BT_DBG("NREC status = [%d]", *status);
1540 g_variant_unref(value);
1541 g_variant_unref(param_inner);
1544 g_variant_unref(param_inner);
1546 BT_DBG("NREC status = [%d]", *status);
1547 g_variant_unref(reply);
1550 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1553 BT_EXPORT_API int bluetooth_telephony_is_wbs_mode(gboolean *status)
1559 GVariant *param_inner;
1563 BT_TELEPHONY_CHECK_INITIALIZED();
1564 BT_TELEPHONY_CHECK_ENABLED();
1567 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1571 reply = __bluetooth_telephony_dbus_method_send(
1572 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1573 "GetProperties", &err, NULL);
1576 BT_ERR("Error returned in method call");
1578 BT_ERR("Error message = %s", err->message);
1581 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1584 g_variant_iter_init(&iter, reply);
1585 while ((param_inner = g_variant_iter_next_value(&iter))) {
1588 value = g_variant_lookup_value(param_inner,
1589 "codec", G_VARIANT_TYPE_UINT32);
1591 BT_DBG("Property CODEC Found");
1592 codec = g_variant_get_uint32(value);
1593 g_variant_unref(value);
1594 BT_DBG("Codec = [%d]", codec);
1596 *status = codec == BT_MSBC_CODEC_ID ? TRUE : FALSE;
1597 BT_DBG("NREC status = [%d]", *status);
1598 g_variant_unref(value);
1599 g_variant_unref(param_inner);
1602 g_variant_unref(param_inner);
1605 g_variant_unref(reply);
1606 BT_DBG("MSBC status = [%d]", *status);
1609 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1612 BT_EXPORT_API int bluetooth_telephony_send_vendor_cmd(const char *cmd)
1614 GError *error = NULL;
1615 GVariant *reply, *parameters;
1619 BT_TELEPHONY_CHECK_INITIALIZED();
1620 BT_TELEPHONY_CHECK_ENABLED();
1622 BT_DBG("Send Vendor %s", cmd);
1624 if (telephony_dbus_info.proxy == NULL)
1625 telephony_dbus_info.proxy =
1626 __bluetooth_telephony_get_connected_device_proxy();
1628 if (telephony_dbus_info.proxy == NULL)
1629 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1631 parameters = g_variant_new("s", cmd);
1632 reply = g_dbus_proxy_call_sync(telephony_dbus_info.proxy,
1633 "SendVendorAtCmd", parameters,
1634 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1636 g_variant_unref(reply);
1639 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1642 BT_EXPORT_API int bluetooth_telephony_start_voice_recognition(void)
1648 gboolean state = TRUE;
1652 BT_TELEPHONY_CHECK_INITIALIZED();
1653 BT_TELEPHONY_CHECK_ENABLED();
1655 param = g_variant_new("(b)", &state);
1656 reply = __bluetooth_telephony_dbus_method_send(
1657 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1658 "SetVoiceDial", &err, param);
1661 BT_ERR("Error returned in method call\n");
1663 g_dbus_error_strip_remote_error(err);
1664 ret = __bt_telephony_get_error(err->message);
1668 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1671 g_variant_unref(reply);
1673 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1676 BT_EXPORT_API int bluetooth_telephony_stop_voice_recognition(void)
1682 gboolean state = FALSE;
1686 BT_TELEPHONY_CHECK_INITIALIZED();
1687 BT_TELEPHONY_CHECK_ENABLED();
1689 param = g_variant_new("(b)", &state);
1690 reply = __bluetooth_telephony_dbus_method_send(
1691 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1692 "SetVoiceDial", &err, param);
1695 BT_ERR("Error returned in method call\n");
1697 g_dbus_error_strip_remote_error(err);
1698 ret = __bt_telephony_get_error(err->message);
1702 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1705 g_variant_unref(reply);
1708 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1711 static void __bluetooth_telephony_sco_start_cb(GDBusProxy *proxy,
1712 GAsyncResult *res, gpointer user_data)
1714 GError *error = NULL;
1717 value = g_dbus_proxy_call_finish(proxy, res, &error);
1718 if (value == NULL) {
1719 if (error != NULL) {
1720 BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1721 error->code, error->message);
1722 g_clear_error(&error);
1724 BT_ERR("SCo Start Failed");
1728 BT_DBG("sco_start_cb : -");
1729 g_object_unref(proxy);
1730 g_variant_unref(value);
1733 BT_EXPORT_API int bluetooth_telephony_audio_open(void)
1735 GDBusConnection *conn;
1742 BT_TELEPHONY_CHECK_INITIALIZED();
1743 BT_TELEPHONY_CHECK_ENABLED();
1745 /* Because this API is async call, so can't use dbus SMACK */
1746 if (__bt_telephony_check_privilege() ==
1747 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1748 BT_ERR("Don't have a privilege to use this API");
1749 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1752 conn = _bt_gdbus_get_system_gconn();
1754 BT_DBG("No System Bus found\n");
1755 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1758 if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING)
1759 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
1761 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1762 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1763 HFP_AGENT_INTERFACE, NULL, &err);
1764 if (proxy == NULL) {
1765 BT_ERR("Unable to allocate new proxy");
1767 g_dbus_error_strip_remote_error(err);
1768 ret = __bt_telephony_get_error(err->message);
1769 BT_ERR("Error here %d\n", ret);
1773 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1776 g_dbus_proxy_call(proxy, "Play", NULL, G_DBUS_CALL_FLAGS_NONE,
1777 -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_start_cb, NULL);
1780 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1783 static void __bluetooth_telephony_sco_close_cb(GDBusProxy *proxy,
1784 GAsyncResult *res, gpointer user_data)
1786 GError *error = NULL;
1789 value = g_dbus_proxy_call_finish(proxy, res, &error);
1790 if (value == NULL) {
1791 if (error != NULL) {
1792 BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1793 error->code, error->message);
1794 g_clear_error(&error);
1796 BT_ERR("SCo close Failed");
1800 BT_DBG("sco_close_cb : -");
1801 g_object_unref(proxy);
1802 g_variant_unref(value);
1804 BT_EXPORT_API int bluetooth_telephony_audio_close(void)
1806 GDBusConnection *conn;
1813 BT_TELEPHONY_CHECK_INITIALIZED();
1814 BT_TELEPHONY_CHECK_ENABLED();
1816 /* Because this API is async call, so can't use dbus SMACK */
1817 if (__bt_telephony_check_privilege() ==
1818 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1819 BT_ERR("Don't have a privilege to use this API");
1820 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1823 conn = _bt_gdbus_get_system_gconn();
1825 BT_DBG("No System Bus found\n");
1826 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1829 if (telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1830 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
1832 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1833 HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1834 HFP_AGENT_INTERFACE, NULL, &err);
1835 if (proxy == NULL) {
1836 BT_ERR("Unable to allocate new proxy");
1838 g_dbus_error_strip_remote_error(err);
1839 ret = __bt_telephony_get_error(err->message);
1840 BT_ERR("Error here %d\n", ret);
1844 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1847 g_dbus_proxy_call(proxy, "Stop", NULL, G_DBUS_CALL_FLAGS_NONE,
1848 -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_close_cb, NULL);
1850 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
1853 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1856 BT_EXPORT_API int bluetooth_telephony_call_remote_ringing(unsigned int call_id)
1860 BT_TELEPHONY_CHECK_INITIALIZED();
1861 BT_TELEPHONY_CHECK_ENABLED();
1864 BT_DBG("call_id = [%d]", call_id);
1866 /*Make sure SCO is already connected */
1867 ret = __bluetooth_telephony_send_call_status(
1868 CSD_CALL_STATUS_MO_ALERTING, call_id, NULL);
1869 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1870 BT_ERR("send call status Failed = [%d]", ret);
1871 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1874 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1877 BT_EXPORT_API int bluetooth_telephony_call_answered(unsigned int call_id,
1878 unsigned int bt_audio)
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_ACTIVE,
1890 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1891 BT_ERR("send call status Failed = [%d]", ret);
1896 if (!bluetooth_telephony_is_sco_connected()) {
1897 ret = bluetooth_telephony_audio_open();
1899 BT_ERR("Audio connection call Failed[%d]", ret);
1900 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1909 BT_EXPORT_API int bluetooth_telephony_call_end(unsigned int call_id)
1914 BT_DBG("call_id = [%d]", call_id);
1916 BT_TELEPHONY_CHECK_INITIALIZED();
1917 BT_TELEPHONY_CHECK_ENABLED();
1919 if (telephony_info.call_count > 0)
1920 telephony_info.call_count = telephony_info.call_count - 1;
1922 if (telephony_info.call_count == 0) {
1923 if (bluetooth_telephony_is_sco_connected()) {
1924 ret = bluetooth_telephony_audio_close();
1925 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1926 BT_ERR(" Failed = [%d]", ret);
1930 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_MT_RELEASE,
1932 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1933 BT_ERR("send call status Failed = [%d]", ret);
1941 BT_EXPORT_API int bluetooth_telephony_call_held(unsigned int call_id)
1946 BT_DBG("call_id = [%d]", call_id);
1948 BT_TELEPHONY_CHECK_INITIALIZED();
1949 BT_TELEPHONY_CHECK_ENABLED();
1951 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_HOLD,
1953 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1954 BT_ERR("send call status Failed = [%d]", ret);
1960 BT_EXPORT_API int bluetooth_telephony_call_retrieved(unsigned int call_id)
1965 BT_DBG("call_id = [%d]", call_id);
1967 BT_TELEPHONY_CHECK_INITIALIZED();
1968 BT_TELEPHONY_CHECK_ENABLED();
1970 ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_ACTIVE,
1972 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1973 BT_ERR("send call status Failed = [%d]", ret);
1979 BT_EXPORT_API int bluetooth_telephony_call_swapped(void *call_list,
1980 unsigned int call_count)
1984 GList *list = call_list;
1985 bt_telephony_call_status_info_t *call_status;
1989 BT_TELEPHONY_CHECK_INITIALIZED();
1990 BT_TELEPHONY_CHECK_ENABLED();
1993 BT_ERR("call_list is invalid");
1994 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1997 /* Because this API is async call, so can't use dbus SMACK */
1998 if (__bt_telephony_check_privilege() ==
1999 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
2000 BT_ERR("Don't have a privilege to use this API");
2001 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
2004 BT_DBG(" call_count = [%d]", call_count);
2006 for (i = 0; i < call_count; i++) {
2007 call_status = g_list_nth_data(list, i);
2009 if (NULL == call_status)
2012 BT_DBG(" %d : Call id [%d] status[%d]", i,
2013 call_status->call_id,
2014 call_status->call_status);
2016 if (NULL != call_status->phone_number)
2017 DBG_SECURE("Number [%s]", call_status->phone_number);
2019 switch (call_status->call_status) {
2020 case BLUETOOTH_CALL_STATE_HELD:
2021 ret = __bluetooth_telephony_send_call_status(
2022 CSD_CALL_STATUS_HOLD,
2023 call_status->call_id,
2024 call_status->phone_number);
2025 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2026 BT_ERR("Failed = %d", ret);
2031 case BLUETOOTH_CALL_STATE_CONNECTED:
2032 ret = __bluetooth_telephony_send_call_status(
2033 CSD_CALL_STATUS_ACTIVE,
2034 call_status->call_id,
2035 call_status->phone_number);
2036 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2037 BT_ERR("Failed = [%d]", ret);
2043 if ((call_status->call_status <
2044 BLUETOOTH_CALL_STATE_NONE) ||
2045 (call_status->call_status >=
2046 BLUETOOTH_CALL_STATE_ERROR)) {
2047 BT_ERR("Unknown Call state");
2048 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
2054 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2057 BT_EXPORT_API int bluetooth_telephony_set_call_status(void *call_list,
2058 unsigned int call_count)
2064 ret = bluetooth_telephony_call_swapped(call_list, call_count);
2066 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2067 BT_ERR("Failed = [%d]", ret);
2071 telephony_info.call_count = call_count;
2074 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2077 BT_EXPORT_API int bluetooth_telephony_indicate_outgoing_call(
2078 const char *ph_number, unsigned int call_id,
2079 unsigned int bt_audio)
2084 const char *path = telephony_info.call_path;
2089 BT_TELEPHONY_CHECK_INITIALIZED();
2090 BT_TELEPHONY_CHECK_ENABLED();
2092 if (NULL == ph_number)
2093 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2095 param = g_variant_new("(ssi)", path, ph_number, call_id);
2096 reply = __bluetooth_telephony_dbus_method_send(
2097 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2098 "OutgoingCall", &err, param);
2101 BT_ERR("Error returned in method call\n");
2103 g_dbus_error_strip_remote_error(err);
2104 ret = __bt_telephony_get_error(err->message);
2108 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2111 g_variant_unref(reply);
2113 telephony_info.call_count++;
2114 BT_DBG(" ag_info.ag_call_count = [%d]", telephony_info.call_count);
2117 if (!bluetooth_telephony_is_sco_connected()) {
2118 ret = bluetooth_telephony_audio_open();
2120 BT_ERR(" Audio connection Failed = %d", ret);
2121 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2127 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2130 BT_EXPORT_API int bluetooth_telephony_indicate_incoming_call(
2131 const char *ph_number, unsigned int call_id)
2136 const char *path = telephony_info.call_path;
2141 BT_TELEPHONY_CHECK_INITIALIZED();
2142 BT_TELEPHONY_CHECK_ENABLED();
2144 if (NULL == ph_number)
2145 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2147 param = g_variant_new("(ssi)", path, ph_number, call_id);
2148 reply = __bluetooth_telephony_dbus_method_send(
2149 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2150 "IncomingCall", &err, param);
2153 BT_ERR("Error returned in method call\n");
2155 g_dbus_error_strip_remote_error(err);
2156 ret = __bt_telephony_get_error(err->message);
2160 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2163 g_variant_unref(reply);
2164 telephony_info.call_count++;
2165 BT_DBG("telephony_info.call_count = [%d]", telephony_info.call_count);
2167 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2170 BT_EXPORT_API int bluetooth_telephony_set_speaker_gain(
2171 unsigned short speaker_gain)
2180 BT_TELEPHONY_CHECK_INITIALIZED();
2181 BT_TELEPHONY_CHECK_ENABLED();
2183 BT_DBG("set speaker_gain= [%d]", speaker_gain);
2185 param = g_variant_new("(q)", speaker_gain);
2186 reply = __bluetooth_telephony_dbus_method_send(
2187 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2188 "SetSpeakerGain", &err, param);
2191 BT_ERR("Error returned in method call\n");
2193 g_dbus_error_strip_remote_error(err);
2194 ret = __bt_telephony_get_error(err->message);
2198 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2201 g_variant_unref(reply);
2203 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2206 BT_EXPORT_API int bluetooth_telephony_get_headset_volume(
2207 unsigned int *speaker_gain)
2216 BT_TELEPHONY_CHECK_INITIALIZED();
2217 BT_TELEPHONY_CHECK_ENABLED();
2219 reply = __bluetooth_telephony_dbus_method_send(
2220 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2221 "GetSpeakerGain", &err, NULL);
2224 BT_ERR("Error returned in method call\n");
2226 g_dbus_error_strip_remote_error(err);
2227 ret = __bt_telephony_get_error(err->message);
2231 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2233 g_variant_get(reply, "(q)", &gain);
2234 *speaker_gain = gain;
2235 BT_DBG("Get speaker_gain= [%d]", *speaker_gain);
2237 g_variant_unref(reply);
2240 return BLUETOOTH_TELEPHONY_ERROR_NONE;
2243 BT_EXPORT_API int bluetooth_telephony_is_connected(gboolean *ag_connected)
2248 gboolean ag_connected_from_bt_agent;
2250 BT_CHECK_ENABLED(return);
2252 reply = __bluetooth_telephony_dbus_method_send(
2253 HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2254 "IsConnected", &err, NULL);
2257 BT_ERR("Error returned in method call\n");
2259 g_dbus_error_strip_remote_error(err);
2260 ret = __bt_telephony_get_error(err->message);
2264 return BLUETOOTH_ERROR_INTERNAL;
2266 g_variant_get(reply, "(b)", &ag_connected_from_bt_agent);
2267 *ag_connected = ag_connected_from_bt_agent;
2269 BT_DBG("Conn Status: %s", *ag_connected ? "Connected" : "Disconnected");
2271 g_variant_unref(reply);
2273 return BLUETOOTH_ERROR_NONE;
2276 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
2277 const gchar *sender_name,
2278 const gchar *object_path,
2279 const gchar *interface_name,
2280 const gchar *signal_name,
2281 GVariant *parameters,
2288 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2289 GVariant *optional_param = NULL;
2291 g_variant_get(parameters, "(&o@a{sa{sv}})",
2292 &path, &optional_param);
2295 BT_ERR("Invalid adapter path");
2299 BT_INFO("Adapter Path = [%s]", path);
2300 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
2301 if (__bt_telephony_get_src_addr(optional_param))
2302 BT_ERR("Fail to get the local adapter address");
2304 ret = __bluetooth_telephony_register();
2305 if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
2306 BT_ERR("__bluetooth_telephony_register failed");
2313 static int __bt_telephony_get_src_addr(GVariant *value)
2316 GVariant *param = NULL;
2319 /* signature a{sa{sv}} */
2320 g_variant_iter_init(&iter, value);
2321 while ((param = g_variant_iter_next_value(&iter))) {
2322 char *interface_name;
2323 GVariant *interface_var = NULL;
2324 GVariant *param_inner = NULL;
2326 g_variant_get(param, "{&s*}", &interface_name, &interface_var);
2327 g_variant_unref(param);
2329 BT_DBG("interface_name: %s", interface_name);
2330 /* format of interface_var: a{sv}*/
2331 if (strcasecmp(interface_name, BLUEZ_ADAPTER_INTERFACE) == 0) {
2332 GVariantIter iter_inner;
2334 g_variant_iter_init(&iter_inner, interface_var);
2335 while ((param_inner = g_variant_iter_next_value(&iter_inner))) {
2336 char *property_name;
2337 GVariant *property_var;
2339 g_variant_get(param_inner, "{&sv}",
2342 g_variant_unref(param_inner);
2344 if (strcasecmp(property_name, "Address") == 0) {
2345 const gchar *bd_addr;
2347 bd_addr = g_variant_get_string(
2350 src_addr = g_strdup(bd_addr);
2351 BT_DBG("Address: %s", src_addr);
2353 g_variant_unref(interface_var);
2354 g_variant_unref(property_var);
2357 g_variant_unref(property_var);
2360 g_variant_unref(interface_var);
2364 return BLUETOOTH_ERROR_NONE;