4 * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Chethan T N <chethan.tn@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
9 * Rakesh MK <rakesh.mk@samsung.com>
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
28 #include <gio/gunixfdlist.h>
29 #include <bundle_internal.h>
30 #include "bluetooth-ag-agent.h"
31 #include "bluetooth-ag-handler.h"
33 #include <TapiUtility.h>
35 #include <ITapiModem.h>
36 #include <TelNetwork.h>
39 #include <system_info.h>
44 static GMainLoop *gmain_loop = NULL;
45 static GDBusProxy *service_gproxy;
46 static int owner_sig_id = -1;
47 static int name_owner_sig_id = -1;
48 GDBusConnection *ag_dbus_conn = NULL;
49 gchar *remote_dev_path = NULL;
53 static TapiHandle *tapi_handle;
54 extern wbs_options wbs_opts;
55 GSList *active_devices = NULL;
56 static gchar *local_addr = NULL;
57 static GDBusProxy *app_gproxy;
58 static gboolean call_launch_requested = FALSE;
59 static gchar* sco_owner = NULL;
60 static guint sco_open_timer_id = 0;
61 static gboolean sco_open_request = FALSE;
62 static guint hf_bluez_id;
63 static guint hs_bluez_id;
65 #ifdef TIZEN_MEDIA_ENHANCE
66 static int media_sig_id = -1;
67 static int media_state_sig_id = -1;
68 static bt_ag_media_transport_state_t transport_state;
71 #define HSP_AG_UUID "00001112-0000-1000-8000-00805f9b34fb"
72 #define HFP_AG_UUID "0000111f-0000-1000-8000-00805f9b34fb"
73 #ifdef TIZEN_MEDIA_ENHANCE
74 #define A2DP_SINK_UUID "0000110b-0000-1000-8000-00805f9b34fb"
76 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
78 #ifdef TIZEN_SUPPORT_LUNAR_DEVICE
79 #define VCONF_KEY_BT_LUNAR_ENABLED "db/wms/bt_loop_device_hfp_connected"
82 #if defined(TIZEN_WEARABLE) && defined(TIZEN_BT_HFP_AG_ENABLE)
83 #define CALL_APP_ID "org.tizen.call-ui"
86 #if defined(TIZEN_SUPPORT_DUAL_HF)
87 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
88 #define MAX_CONNECTED_DEVICES 2
90 #define MAX_CONNECTED_DEVICES 1
93 #define BT_AG_SIG_NUM 3
94 static struct sigaction bt_ag_sigoldact[BT_AG_SIG_NUM];
95 static int bt_ag_sig_to_handle[] = { SIGABRT, SIGSEGV, SIGTERM };
97 /*Below Inrospection data is exposed to bluez from agent*/
98 static const gchar ag_agent_bluez_introspection_xml[] =
100 " <interface name='org.bluez.Profile1'>"
101 " <method name='NewConnection'>"
102 " <arg type='o' name='device' direction='in'/>"
103 " <arg type='h' name='fd' direction='in'/>"
104 " <arg type='a{sv}' name='options' direction='in'/>"
106 " <method name='RequestDisconnection'>"
107 " <arg type='o' name='device' direction='in'/>"
112 /*Below Introspection data is exposed to application from agent*/
113 static const gchar ag_agent_app_introspection_xml[] =
115 " <interface name='Org.Hfp.App.Interface'>"
116 " <method name='RegisterApplication'>"
117 " <arg type='s' name='path' direction='in'/>"
118 " <arg type='s' name='address' direction='in'/>"
120 " <method name='UnregisterApplication'>"
121 " <arg type='s' name='path' direction='in'/>"
123 " <method name='IncomingCall'>"
124 " <arg type='s' name='path' direction='in'/>"
125 " <arg type='s' name='number' direction='in'/>"
126 " <arg type='i' name='id' direction='in'/>"
128 " <method name='OutgoingCall'>"
129 " <arg type='s' name='path' direction='in'/>"
130 " <arg type='s' name='number' direction='in'/>"
131 " <arg type='i' name='id' direction='in'/>"
133 " <method name='ChangeCallStatus'>"
134 " <arg type='s' name='path' direction='in'/>"
135 " <arg type='s' name='number' direction='in'/>"
136 " <arg type='i' name='status' direction='in'/>"
137 " <arg type='i' name='id' direction='in'/>"
139 " <method name='GetProperties'>"
140 " <arg type='a{sv}' name='properties' direction='out'/>"
142 " <method name='Disconnect'>"
144 " <method name='IsConnected'>"
145 " <arg type='b' name='connected' direction='out'/>"
147 " <method name='IndicateCall'>"
149 " <method name='CancelCall'>"
151 " <method name='Play'>"
153 " <method name='Stop'>"
155 " <method name='IsPlaying'>"
156 " <arg type='b' name='playing' direction='out'/>"
158 " <method name='GetSpeakerGain'>"
159 " <arg type='q' name='gain' direction='out'/>"
161 " <method name='GetMicrophoneGain'>"
162 " <arg type='q' name='gain' direction='out'/>"
164 " <method name='SetSpeakerGain'>"
165 " <arg type='q' name='gain' direction='in'/>"
167 " <method name='SetMicrophoneGain'>"
168 " <arg type='q' name='gain' direction='in'/>"
170 " <method name='SetVoiceDial'>"
171 " <arg type='b' name='enable' direction='in'/>"
173 " <method name='CheckPrivilege'>"
180 int (*callback)(bt_ag_info_t *hs, const char *buf);
183 struct sco_socket_addr {
184 sa_family_t sco_family;
193 bt_ag_info_t *bt_ag_info;
197 bt_ag_status_t ag = {
198 .telephony_ready = FALSE,
202 .rh = BT_RSP_HOLD_NOT_SUPPORTED,
208 static void __bt_ag_agent_sigterm_handler(int signo);
209 static gboolean __bt_ag_agent_connection(gint32 fd, const gchar *device_path,
210 const gchar *object_path);
211 static gboolean __bt_ag_agent_connection_release(bt_ag_info_t *hs);
212 static gboolean __bt_ag_event_handler(GIOChannel *channel, GIOCondition cond,
214 static int __bt_ag_sco_connect(bt_ag_info_t *hs);
215 void _bt_ag_set_headset_state(bt_ag_info_t *hs, hs_state_t state);
216 static void __bt_ag_agent_reg_sim_event(TapiHandle *handle, void *user_data);
217 static void __bt_ag_agent_dereg_sim_event(TapiHandle *handle);
218 static void __bt_ag_name_owner_changed_cb(GDBusConnection *connection,
219 const gchar *sender_name,
220 const gchar *object_path,
221 const gchar *interface_name,
222 const gchar *signal_name,
223 GVariant *parameters,
226 static void __bt_convert_addr_type_to_rev_string(char *address,
229 ret_if(address == NULL);
230 ret_if(addr == NULL);
232 g_snprintf(address, BT_ADDRESS_STRING_SIZE,
233 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
234 addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
237 static GDBusProxy *__bt_ag_gdbus_init_service_proxy(const gchar *service,
238 const gchar *path, const gchar *interface)
245 if (ag_dbus_conn == NULL)
246 ag_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
250 ERR("Unable to connect to gdbus: %s", err->message);
256 proxy = g_dbus_proxy_new_sync(ag_dbus_conn,
257 G_DBUS_PROXY_FLAGS_NONE, NULL,
259 interface, NULL, &err);
263 ERR("Unable to create proxy: %s", err->message);
273 static GDBusProxy *__bt_ag_gdbus_get_app_proxy(const gchar *service,
274 const gchar *path, const gchar *interface)
276 return (app_gproxy) ? app_gproxy :
277 __bt_ag_gdbus_init_service_proxy(service,
281 static int __bt_ag_agent_gdbus_method_send(const char *service,
282 const gchar *path, const char *interface,
283 const char *method, gboolean response,
284 GVariant *parameters)
290 GError *error = NULL;
292 proxy = __bt_ag_gdbus_get_app_proxy(service, path, interface);
294 return BT_HFP_AGENT_ERROR_INTERNAL;
297 ret = g_dbus_proxy_call_sync(proxy,
299 G_DBUS_CALL_FLAGS_NONE, -1,
302 /* dBUS-RPC is failed */
303 ERR("dBUS-RPC is failed");
305 /* dBUS gives error cause */
306 ERR("D-Bus API failure: errCode[%x], message[%s]",
307 error->code, error->message);
309 g_clear_error(&error);
311 return BT_HFP_AGENT_ERROR_INTERNAL;
314 g_variant_unref(ret);
316 g_dbus_proxy_call(proxy,
318 G_DBUS_CALL_FLAGS_NONE, 2000,
321 return BT_HFP_AGENT_ERROR_NONE;
324 gboolean _bt_ag_agent_emit_signal(
325 GDBusConnection *connection,
327 const char *interface,
333 GError *error = NULL;
335 ret = g_dbus_connection_emit_signal(connection,
336 NULL, path, interface,
341 /* dBUS gives error cause */
342 ERR("D-Bus API failure: errCode[%x], message[%s]",
343 error->code, error->message);
344 g_clear_error(&error);
347 INFO_C("Emit Signal done = [%s]", name);
353 gboolean _bt_ag_agent_emit_property_changed(
354 GDBusConnection *connection,
356 const char *interface,
365 var_data = g_variant_new("(sv)", name, property);
367 ret = _bt_ag_agent_emit_signal(connection,
369 "PropertyChanged", var_data);
374 static void __bt_ag_agent_start_watch(bt_ag_info_t *bt_ag_info)
376 bt_ag_info->watch_id = g_io_add_watch(bt_ag_info->io_chan,
377 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
378 (GIOFunc) __bt_ag_event_handler, bt_ag_info);
381 static void __bt_ag_agent_remove_watch(guint *watch_id)
383 DBG("Remove IO watch ID %d", *watch_id);
385 g_source_remove(*watch_id);
390 #if defined(TIZEN_SUPPORT_DUAL_HF)
391 gboolean __bt_ag_agent_is_companion_device(const char *addr)
393 #if defined(TIZEN_WEARABLE)
394 char *host_device_address = NULL;
395 host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
397 if (!host_device_address) {
398 INFO("Failed to get a companion device address");
402 if (g_strcmp0(host_device_address, addr) == 0) {
403 INFO("addr[%s] is companion device", addr);
409 /* TODO : Need to add companion device check condition for Phone models */
414 void __bt_convert_device_path_to_address(const gchar *device_path,
415 char *device_address)
417 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
420 ret_if(device_path == NULL);
421 ret_if(device_address == NULL);
423 dev_addr = strstr(device_path, "dev_");
424 if (dev_addr != NULL) {
427 g_strlcpy(address, dev_addr, sizeof(address));
429 while ((pos = strchr(address, '_')) != NULL) {
433 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
437 static gboolean __bt_ag_agent_is_companion_device_connected(void)
441 for (l = active_devices ; l; l = l->next) {
442 bt_ag_info_t *data = l->data;
444 if (data->is_companion_device) {
445 DBG("Companion device found");
453 gboolean __bt_ag_agent_check_dual_hf_condition(const gchar *device_path)
455 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
456 gboolean is_companion_device;
458 __bt_convert_device_path_to_address(device_path, device_address);
459 is_companion_device = __bt_ag_agent_is_companion_device(device_address);
461 DBG(" device_address[%s]", device_address);
462 DBG(" is_companion_device[%d]", is_companion_device);
464 if (__bt_ag_agent_is_companion_device_connected()) {
465 if (is_companion_device)
468 if (!is_companion_device)
474 #endif /* TIZEN_SUPPORT_DUAL_HF */
476 static gboolean __bt_is_phone_locked(int *phone_lock_state)
481 if (NULL == phone_lock_state)
484 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, phone_lock_state);
486 ERR("Failed to read [%s]\n", VCONFKEY_IDLE_LOCK_STATE);
494 static gboolean __bt_get_outgoing_callapp_type(int *callapp_type)
498 if (NULL == callapp_type)
501 #if defined(TIZEN_WEARABLE) && defined(TIZEN_BT_HFP_AG_ENABLE)
502 *callapp_type = BT_VOICE_CALL;
509 VCONFKEY_CISSAPPL_OUTGOING_CALL_TYPE_INT,
512 ERR("Failed to read [%s]\n",
513 VCONFKEY_CISSAPPL_OUTGOING_CALL_TYPE_INT);
517 INFO(" [%s] = [%d]\n",
518 VCONFKEY_CISSAPPL_OUTGOING_CALL_TYPE_INT, *callapp_type);
520 /* The vconf value does not include in platform. */
521 *callapp_type = BT_VOICE_CALL;
527 static gboolean __bt_get_outgoing_call_condition(int *condition)
531 if (NULL == condition)
534 #if defined(TIZEN_WEARABLE) && defined(TIZEN_BT_HFP_AG_ENABLE)
535 *condition = BT_MO_ONLY_UNLOCKED;
542 VCONFKEY_CISSAPPL_OUTGOING_CALL_CONDITIONS_INT,
545 ERR("Failed to read [%s]\n",
546 VCONFKEY_CISSAPPL_OUTGOING_CALL_CONDITIONS_INT);
550 /* The vconf value does not include in platform. */
551 *condition = BT_MO_ONLY_UNLOCKED;
557 #if defined(TIZEN_WEARABLE) && defined(TIZEN_TELEPHONY_ENABLED)
558 static gboolean __bt_ag_agent_launch_call_app(const char *number)
563 DBG_SECURE("number(%s)", number);
567 ERR("bundle_create() Failed");
571 bundle_add(b, "launch-type", "MO");
572 bundle_add(b, "dial-type", "HEADSET");
574 if (strlen(number) != 0)
575 bundle_add(b, "number", number);
577 aul_launch_app_async(CALL_APP_ID, b);
585 static void *__bt_ag_agent_launch_call_req(void *arg)
588 bundle *b = (bundle *)arg;
589 if (appsvc_run_service(b, 0, NULL, NULL) < 0)
590 ERR("Unable to run app svc");
592 call_launch_requested = FALSE;
597 static gboolean __bt_ag_agent_make_call(const char *number)
601 #if defined(TIZEN_WEARABLE) && defined(TIZEN_BT_HFP_AG_ENABLE)
603 return __bt_ag_agent_launch_call_app(number);
605 char telnum[BT_MAX_TEL_NUM_STRING];
609 if (call_launch_requested == TRUE) {
610 DBG("Launch request is in progress");
618 appsvc_set_operation(b, APPSVC_OPERATION_CALL);
619 snprintf(telnum, sizeof(telnum), "tel:%s", number);
620 appsvc_set_uri(b, telnum);
621 appsvc_add_data(b, "ctindex", "-1");
623 call_launch_requested = TRUE;
624 if (pthread_create(&thread_id, NULL,
625 (void *)&__bt_ag_agent_launch_call_req,
627 ERR("pthread_create() is failed");
628 call_launch_requested = FALSE;
631 if (pthread_detach(thread_id) < 0)
632 ERR("pthread_detach() is failed");
639 static gboolean __bt_ag_agent_make_video_call(const char *mo_number)
644 kb = bundle_create();
648 bundle_add(kb, "KEY_CALL_TYPE", "MO");
649 bundle_add(kb, "number", mo_number);
650 aul_launch_app("org.tizen.vtmain", kb);
657 gboolean _bt_ag_agent_answer_call(unsigned int call_id,
658 const gchar *path, const gchar *sender)
662 if (path == NULL || sender == NULL) {
663 DBG("Invalid Arguments");
667 DBG("Application path = %s", path);
668 DBG("Call Id = %d", call_id);
669 DBG("Sender = %s", sender);
671 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
672 BT_AG_SERVICE_NAME, "Answer",
673 g_variant_new("(u)", call_id));
678 gboolean _bt_ag_agent_reject_call(unsigned int call_id,
679 const gchar *path, const gchar *sender)
683 if (path == NULL || sender == NULL) {
684 DBG("Invalid Arguments");
688 DBG("Application path = %s", path);
689 DBG("Call Id = %d", call_id);
690 DBG("Sender = %s", sender);
692 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
693 BT_AG_SERVICE_NAME, "Reject",
694 g_variant_new("(u)", call_id));
699 gboolean _bt_ag_agent_release_call(unsigned int call_id,
700 const gchar *path, const gchar *sender)
704 if (path == NULL || sender == NULL) {
705 DBG("Invalid Arguments");
709 DBG("Application path = %s", path);
710 DBG("Call Id = %d", call_id);
711 DBG("Sender = %s", sender);
713 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
714 BT_AG_SERVICE_NAME, "Release",
715 g_variant_new("(u)", call_id));
721 bt_hfp_agent_error_t _bt_ag_agent_dial_num(const gchar *number, guint flags)
723 bt_hfp_agent_error_t error_code = BT_HFP_AGENT_ERROR_NONE;
725 int phone_lock_state;
730 if (number == NULL) {
731 ERR("Invalid Argument");
732 error_code = BT_HFP_AGENT_ERROR_INVALID_PARAM;
736 DBG("Number = %s", number);
737 DBG("flags = %d", flags);
739 if (!__bt_is_phone_locked(&phone_lock_state)) {
740 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
744 if (!__bt_get_outgoing_callapp_type(&callapp_type)) {
745 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
749 if (!__bt_get_outgoing_call_condition(&condition)) {
750 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
754 if (condition == BT_MO_ONLY_UNLOCKED && phone_lock_state ==
755 VCONFKEY_IDLE_LOCK) {
756 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
760 if (callapp_type == BT_VIDEO_CALL) {
761 if (!__bt_ag_agent_make_video_call(number)) {
762 ERR("Problem launching application");
763 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
767 if (!__bt_ag_agent_make_call(number)) {
768 ERR("Problem launching application");
769 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
779 bt_hfp_agent_error_t _bt_ag_agent_dial_memory(unsigned int location)
781 bt_hfp_agent_error_t error_code = BT_HFP_AGENT_ERROR_NONE;
783 contacts_filter_h filter = NULL;
784 contacts_query_h query = NULL;
785 contacts_list_h list = NULL;
786 contacts_record_h record = NULL;
787 unsigned int projections[] = {
788 _contacts_speeddial.number,
793 DBG("location = %d", location);
795 /*Get number from contacts location*/
796 if (contacts_connect() != CONTACTS_ERROR_NONE) {
797 ERR(" contacts_connect failed");
798 return BT_HFP_AGENT_ERROR_INTERNAL;
801 contacts_filter_create(_contacts_speeddial._uri, &filter);
806 if (contacts_filter_add_int(filter,
807 _contacts_speeddial.speeddial_number,
808 CONTACTS_MATCH_EQUAL, location) !=
809 CONTACTS_ERROR_NONE) {
810 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
814 contacts_query_create(_contacts_speeddial._uri, &query);
819 contacts_query_set_filter(query, filter);
821 if (contacts_query_set_projection(query, projections,
822 sizeof(projections)/sizeof(unsigned int)) !=
823 CONTACTS_ERROR_NONE) {
824 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
828 if (contacts_db_get_records_with_query(query, 0, 1, &list) !=
829 CONTACTS_ERROR_NONE) {
830 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
834 if (contacts_list_first(list) != CONTACTS_ERROR_NONE) {
835 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
839 if (contacts_list_get_current_record_p(list, &record) !=
840 CONTACTS_ERROR_NONE) {
841 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
848 if (contacts_record_get_str(record, _contacts_speeddial.number, &number)
849 != CONTACTS_ERROR_NONE) {
850 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
854 if (number == NULL) {
855 ERR("No number at the location");
856 error_code = BT_HFP_AGENT_ERROR_INVALID_MEMORY_INDEX;
860 DBG("number %s", number);
863 if (!__bt_ag_agent_make_call(number)) {
864 ERR("Problem launching application");
865 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
871 contacts_list_destroy(list, TRUE);
874 contacts_filter_destroy(filter);
877 contacts_query_destroy(query);
879 contacts_disconnect();
886 bt_hfp_agent_error_t _bt_ag_agent_send_dtmf(const gchar *dtmf,
887 const gchar *path, const gchar *sender)
889 bt_hfp_agent_error_t ret;
893 if (dtmf == NULL || path == NULL || sender == NULL) {
894 ERR("Invalid Argument");
898 DBG("Dtmf = %s", dtmf);
899 DBG("Application path = %s", path);
900 DBG("Sender = %s", sender);
902 ret = __bt_ag_agent_gdbus_method_send(sender,
903 path, TELEPHONY_APP_INTERFACE,
905 g_variant_new("(s)", dtmf));
910 gboolean _bt_ag_agent_threeway_call(unsigned int chld_value,
911 const gchar *path, const gchar *sender)
915 if (path == NULL || sender == NULL) {
916 DBG("Invalid Arguments");
920 DBG("Application path = %s", path);
921 DBG("Value = %d", chld_value);
922 DBG("Sender = %s", sender);
924 #if defined(TIZEN_WEARABLE) && defined(TIZEN_BT_HFP_AG_ENABLE)
925 /* Check if AG supports (i.e. ag_chld_str = "0,1,2") the requested CHLD;
926 if not return FALSE */
927 if (chld_value != 0 && chld_value != 1 && chld_value != 2)
930 if (chld_value != 0 && chld_value != 1 && chld_value != 2 &&
934 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
935 BT_AG_SERVICE_NAME, "Threeway",
936 g_variant_new("(u)", chld_value));
941 bt_hfp_agent_error_t _bt_ag_agent_dial_last_num(void *device)
943 bt_hfp_agent_error_t err_code = BT_HFP_AGENT_ERROR_NONE;
944 char *last_num = NULL;
947 int phone_lock_state;
949 contacts_list_h list = NULL;
950 contacts_query_h query = NULL;
951 contacts_filter_h filter = NULL;
952 contacts_record_h record = NULL;
953 unsigned int projections[] = {
954 _contacts_phone_log.address,
955 _contacts_phone_log.log_type,
960 if (contacts_connect() != CONTACTS_ERROR_NONE) {
961 ERR(" contacts_connect failed");
962 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
966 contacts_filter_create(_contacts_phone_log._uri, &filter);
971 if (contacts_filter_add_int(filter, _contacts_phone_log.log_type,
972 CONTACTS_MATCH_EQUAL,
973 CONTACTS_PLOG_TYPE_VOICE_OUTGOING) !=
974 CONTACTS_ERROR_NONE) {
975 ERR(" contacts_filter_add_int failed");
976 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
980 if (contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR) !=
981 CONTACTS_ERROR_NONE) {
982 ERR(" contacts_filter_add_operator failed");
983 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
987 if (contacts_filter_add_int(filter, _contacts_phone_log.log_type,
988 CONTACTS_MATCH_EQUAL,
989 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING) !=
990 CONTACTS_ERROR_NONE) {
991 ERR(" contacts_filter_add_int failed");
992 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
996 contacts_query_create(_contacts_phone_log._uri, &query);
1001 contacts_query_set_filter(query, filter);
1003 if (contacts_query_set_projection(query, projections,
1004 sizeof(projections)/sizeof(unsigned int)) !=
1005 CONTACTS_ERROR_NONE) {
1006 ERR(" contacts_query_set_projection failed");
1007 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1011 if (contacts_query_set_sort(query, _contacts_phone_log.log_time, false)
1012 != CONTACTS_ERROR_NONE) {
1013 ERR(" contacts_query_set_sort failed");
1014 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1018 if (contacts_db_get_records_with_query(query, 0, 1, &list) !=
1019 CONTACTS_ERROR_NONE) {
1020 ERR(" contacts_db_get_records_with_query failed");
1021 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1025 if (contacts_list_first(list) != CONTACTS_ERROR_NONE) {
1026 ERR(" contacts_list_first failed");
1027 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1031 if (contacts_list_get_current_record_p(list, &record) !=
1032 CONTACTS_ERROR_NONE) {
1033 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1040 if (contacts_record_get_str(record, _contacts_phone_log.address,
1041 &last_num) != CONTACTS_ERROR_NONE) {
1042 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1046 if (last_num == NULL) {
1047 ERR("No last number");
1048 err_code = BT_HFP_AGENT_ERROR_NO_CALL_LOGS;
1052 if (!__bt_is_phone_locked(&phone_lock_state)) {
1053 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1057 if (!__bt_get_outgoing_callapp_type(&callapp_type)) {
1058 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1062 if (!__bt_get_outgoing_call_condition(&condition)) {
1063 ERR(" Failed to get the call condition");
1064 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1068 if (condition == BT_MO_ONLY_UNLOCKED &&
1069 phone_lock_state == VCONFKEY_IDLE_LOCK) {
1070 ERR(" call condition and phone lock state check fail");
1071 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1075 switch (callapp_type) {
1077 if (!__bt_ag_agent_make_call(last_num)) {
1078 ERR("Problem launching application");
1079 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1083 if (!__bt_ag_agent_make_video_call(last_num)) {
1084 ERR("Problem launching application");
1085 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1088 case BT_FOLLOW_CALL_LOG:
1089 if (contacts_record_get_int(record,
1090 _contacts_phone_log.log_type,
1091 &type) != CONTACTS_ERROR_NONE) {
1092 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1095 if (type == CONTACTS_PLOG_TYPE_VOICE_OUTGOING) {
1096 if (!__bt_ag_agent_make_call(last_num)) {
1097 ERR("Problem launching application");
1098 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1100 } else if (type == CONTACTS_PLOG_TYPE_VIDEO_OUTGOING) {
1101 if (!__bt_ag_agent_make_video_call(last_num)) {
1102 ERR("Problem launching application");
1103 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1106 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1110 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1117 contacts_list_destroy(list, TRUE);
1120 contacts_filter_destroy(filter);
1123 contacts_query_destroy(query);
1125 contacts_disconnect();
1127 if (last_num != NULL)
1135 bt_hfp_agent_error_t _bt_ag_agent_vendor_cmd(const gchar *cmd,
1136 const gchar *path, const gchar *sender)
1138 bt_hfp_agent_error_t ret;
1142 if (cmd == NULL || path == NULL || sender == NULL) {
1143 ERR("Invalid Argument");
1144 return BT_HFP_AGENT_ERROR_INVALID_PARAM;
1147 DBG("cmd = %s", cmd);
1148 DBG("Application path = %s", path);
1149 DBG("Sender = %s", sender);
1151 ret = __bt_ag_agent_gdbus_method_send(sender,
1152 path, TELEPHONY_APP_INTERFACE,
1154 g_variant_new("(s)", cmd));
1159 gboolean _bt_ag_agent_get_signal_quality(void *device)
1165 if (vconf_get_int(VCONFKEY_TELEPHONY_RSSI, &rssi)) {
1166 DBG("VCONFKEY_TELEPHONY_RSSI failed\n");
1170 DBG("RSSI : %d", rssi);
1172 _bt_hfp_signal_quality_reply(rssi, BT_SIGNAL_QUALITY_BER,
1179 _bt_hfp_signal_quality_reply(-1, -1, device);
1183 gboolean _bt_ag_agent_get_battery_status(void *device)
1185 gint battery_chrg_status;
1186 gint battery_capacity;
1190 if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
1191 &battery_chrg_status)) {
1192 DBG("VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW failed\n");
1196 DBG("Status : %d\n", battery_chrg_status);
1198 if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
1199 &battery_capacity)) {
1200 DBG("VCONFKEY_SYSMAN_BATTERY_CAPACITY failed\n");
1204 DBG("Capacity : %d\n", battery_capacity);
1206 _bt_hfp_battery_property_reply(device,
1207 battery_chrg_status, battery_capacity);
1212 _bt_hfp_battery_property_reply(device, -1, -1);
1217 gboolean _bt_ag_agent_get_operator_name(void *device)
1219 char *operator_name;
1222 operator_name = vconf_get_str(VCONFKEY_TELEPHONY_NWNAME);
1223 if (NULL == operator_name) {
1224 DBG("vconf_get_str failed");
1225 _bt_hfp_operator_reply(NULL, device);
1229 DBG("operator_name = [%s]", operator_name);
1231 _bt_hfp_operator_reply(operator_name, device);
1233 free(operator_name);
1239 gboolean _bt_hfp_agent_nrec_status(gboolean status,
1243 bt_ag_info_t *hs = (bt_ag_info_t *)t_device;
1245 DBG("NREC status = %d", status);
1247 hs->nrec_status = FALSE;
1249 hs->nrec_status = TRUE;
1251 _bt_ag_agent_emit_signal(ag_dbus_conn, hs->path,
1252 BT_AG_SERVICE_NAME, "NrecStatusChanged",
1253 g_variant_new("(b)", status));
1258 gboolean _bt_ag_agent_get_imei_number(void *device)
1263 imei_number = tel_get_misc_me_imei_sync(tapi_handle);
1264 if (NULL == imei_number) {
1265 ERR("tel_get_misc_me_imei_sync for imei_number failed");
1269 if (!g_utf8_validate(imei_number, -1, NULL)) {
1271 ERR("get_imei_number : invalid UTF8");
1275 DBG_SECURE("imei_number = [%s]", imei_number);
1276 _bt_hfp_get_imei_number_reply(imei_number, device);
1282 _bt_hfp_get_imei_number_reply(NULL, device);
1287 void _bt_ag_agent_get_manufacturer_name(void *device)
1290 char *manufacturer_name;
1293 ret = system_info_get_platform_string("http://tizen.org/system/manufacturer",
1294 &manufacturer_name);
1295 if (SYSTEM_INFO_ERROR_NONE != ret) {
1296 ERR("Get manufacturer_name failed : %d", ret);
1297 if (NULL != manufacturer_name)
1298 free(manufacturer_name);
1300 manufacturer_name = g_strdup("Unknown");
1301 } else if (!g_utf8_validate(manufacturer_name, -1, NULL)) {
1302 free(manufacturer_name);
1303 manufacturer_name = g_strdup("Unknown");
1304 ERR("get_manufacturer_name : invalid UTF8");
1307 DBG_SECURE("manufacturer_name = [%s]", manufacturer_name);
1308 _bt_hfp_get_device_manufacturer_reply(manufacturer_name, device);
1309 free(manufacturer_name);
1313 void _bt_ag_agent_get_imsi(void *device)
1316 TelSimImsiInfo_t imsi;
1317 memset(&imsi, 0, sizeof(TelSimImsiInfo_t));
1318 if (tel_get_sim_imsi(tapi_handle, &imsi) != TAPI_API_SUCCESS) {
1319 ERR("tel_get_sim_imsi failed");
1322 DBG_SECURE("tapi values %s %s %s", imsi.szMcc, imsi.szMnc, imsi.szMsin);
1324 _bt_hfp_get_imsi_reply(imsi.szMcc, imsi.szMnc, imsi.szMsin, device);
1328 _bt_hfp_get_imsi_reply(NULL, NULL, NULL, device);
1332 int _bt_ag_agent_registration_status_convert(int result)
1335 case TAPI_NETWORK_SERVICE_LEVEL_NO:
1336 return BT_AGENT_NETWORK_REG_STATUS_NOT_REGISTER;
1337 case TAPI_NETWORK_SERVICE_LEVEL_EMERGENCY:
1338 return BT_AGENT_NETWORK_REG_STATUS_EMERGENCY;
1339 case TAPI_NETWORK_SERVICE_LEVEL_FULL:
1340 return BT_AGENT_NETWORK_REG_STATUS_REGISTER_HOME_NETWORK;
1341 case TAPI_NETWORK_SERVICE_LEVEL_SEARCH:
1342 return BT_AGENT_NETWORK_REG_STATUS_SEARCH;
1344 return BT_AGENT_NETWORK_REG_STATUS_UNKNOWN;
1349 void _bt_ag_agent_get_creg_status(void *device)
1355 int registration_status = 0;
1356 int roam_status = 0;
1358 ret = tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_CIRCUIT_STATUS,
1360 if (ret != TAPI_API_SUCCESS) {
1361 ERR("tel_get_property_int failed");
1364 registration_status =
1365 _bt_ag_agent_registration_status_convert(result);
1367 DBG_SECURE("Registration status %d", result);
1368 DBG_SECURE("Mapped Status %d", registration_status);
1369 if (registration_status ==
1370 BT_AGENT_NETWORK_REG_STATUS_REGISTER_HOME_NETWORK) {
1371 ret = vconf_get_int(VCONFKEY_TELEPHONY_SVC_ROAM, &roam_status);
1373 ERR("Get roaming status failed err = %d\n", ret);
1376 DBG_SECURE("Roam status %d", roam_status);
1377 if (roam_status == 1) {
1378 registration_status =
1379 BT_AGENT_NETWORK_REG_STATUS_REGISTERED_ROAMING;
1383 _bt_hfp_get_creg_status_reply(n, registration_status, device);
1389 void _bt_ag_agent_get_model_name(void *device)
1395 ret = system_info_get_platform_string("http://tizen.org/system/model_name", &model_name);
1396 if (SYSTEM_INFO_ERROR_NONE != ret) {
1397 ERR("Get model_name failed: %d", ret);
1398 if (NULL != model_name)
1401 model_name = g_strdup("Unknown");
1402 } else if (!g_utf8_validate(model_name, -1, NULL)) {
1404 model_name = g_strdup("Unknown");
1405 ERR("get_model_name : invalid UTF8");
1408 DBG_SECURE("model_name = [%s]", model_name);
1409 _bt_hfp_get_model_info_reply(model_name, device);
1414 void _bt_ag_agent_get_revision_information(void *device)
1417 char *revision_info;
1420 ret = system_info_get_platform_string("http://tizen.org/system/build.string",
1422 if (SYSTEM_INFO_ERROR_NONE != ret) {
1423 ERR("Get revision_info failed: %d", ret);
1424 if (NULL != revision_info)
1425 free(revision_info);
1427 revision_info = g_strdup("Unknown");
1428 } else if (!g_utf8_validate(revision_info, -1, NULL)) {
1429 free(revision_info);
1430 revision_info = g_strdup("Unknown");
1431 ERR("get_revision_info: invalid UTF8");
1434 DBG_SECURE("revision_info = [%s]", revision_info);
1435 _bt_hfp_get_revision_info_reply(revision_info, device);
1436 free(revision_info);
1440 #if defined(TIZEN_WEARABLE) && defined(TIZEN_BT_HFP_AG_ENABLE)
1441 static gboolean __bt_ag_agent_launch_voice_dial(gboolean activate)
1446 b = bundle_create();
1448 ERR("bundle_create() Failed");
1452 bundle_add(b, "domain", "bt_headset");
1454 bundle_add(b, "action_type", "deactivate");
1456 aul_launch_app_async("org.tizen.svoice", b);
1462 static gboolean __bt_ag_agent_launch_voice_dial(gboolean activate)
1465 app_control_h service = NULL;
1467 app_control_create(&service);
1469 if (service == NULL) {
1470 ERR("Service create failed");
1474 app_control_set_app_id(service, "org.tizen.svoice");
1475 app_control_set_operation(service, APP_CONTROL_OPERATION_DEFAULT);
1476 if (app_control_add_extra_data(service, "domain", "bt_headset")
1477 != APP_CONTROL_ERROR_NONE) {
1478 ERR("app_control_add_extra_data failed");
1479 app_control_destroy(service);
1484 if (app_control_add_extra_data(service, "action_type", "deactivate")
1485 != APP_CONTROL_ERROR_NONE) {
1486 ERR("app_control_add_extra_data failed");
1487 app_control_destroy(service);
1491 if (app_control_send_launch_request(service, NULL, NULL) !=
1492 APP_CONTROL_ERROR_NONE) {
1493 ERR("launch failed");
1494 app_control_destroy(service);
1498 app_control_destroy(service);
1504 gboolean _bt_ag_agent_voice_dial(gboolean activate)
1506 DBG("Activate = %d", activate);
1508 return __bt_ag_agent_launch_voice_dial(activate);
1511 static void __bt_ag_codec_negotiation_info_reset(bt_ag_info_t *hs,
1514 hs->codec_info.is_negotiating = FALSE;
1515 hs->codec_info.requested_by_hf = FALSE;
1516 hs->codec_info.sending_codec = 0;
1518 hs->codec_info.remote_codecs = 0;
1519 hs->codec_info.final_codec = 0;
1520 hs->nrec_status = FALSE;
1523 if (hs->codec_info.nego_timer) {
1524 g_source_remove(hs->codec_info.nego_timer);
1525 hs->codec_info.nego_timer = 0;
1527 wbs_opts.wbs_enable = wbs_en;
1530 static gboolean __bt_ag_codec_negotiation_finished(gpointer user_data)
1532 struct ag_codec *data = (struct ag_codec *)user_data;
1534 if (g_strcmp0(data->codec_status, "finish") == 0) {
1535 DBG("Codec negotiation finished");
1536 __bt_ag_sco_connect(data->bt_ag_info);
1537 __bt_ag_codec_negotiation_info_reset(data->bt_ag_info, FALSE);
1538 g_free(data->codec_status);
1541 } else if (g_strcmp0(data->codec_status, "timeout") == 0) {
1542 DBG("Timeout is occured in codec negotiation");
1545 if (data->bt_ag_info->codec_info.requested_by_hf) {
1546 __bt_ag_codec_negotiation_info_reset(data->bt_ag_info, FALSE);
1548 __bt_ag_sco_connect(data->bt_ag_info);
1549 __bt_ag_codec_negotiation_info_reset(data->bt_ag_info, FALSE);
1551 g_free(data->codec_status);
1557 static bt_hfp_agent_error_t __bt_ag_set_codec(void *device, char *method)
1562 bt_ag_info_t *bt_ag_info = (bt_ag_info_t *)device;
1564 proxy = g_dbus_proxy_new_sync(ag_dbus_conn,
1565 G_DBUS_PROXY_FLAGS_NONE, NULL,
1566 BLUEZ_SERVICE_NAME, DEFAULT_ADAPTER_OBJECT_PATH,
1567 BT_ADAPTER_INTERFACE, NULL, &err);
1571 ERR("Unable to create proxy: %s", err->message);
1572 g_clear_error(&err);
1574 return BT_HFP_AGENT_ERROR_INTERNAL;
1577 ret = g_dbus_proxy_call_sync(proxy, method,
1578 g_variant_new("(ss)", "Gateway", bt_ag_info->remote_addr),
1579 G_DBUS_CALL_FLAGS_NONE, -1,
1582 /* dBUS-RPC is failed */
1583 ERR("dBUS-RPC is failed");
1585 /* dBUS gives error cause */
1586 ERR("D-Bus API failure: errCode[%x], message[%s]",
1587 err->code, err->message);
1589 g_clear_error(&err);
1591 return BT_HFP_AGENT_ERROR_INTERNAL;
1593 g_variant_unref(ret);
1595 return BT_HFP_AGENT_ERROR_NONE;
1598 static bt_hfp_agent_error_t __bt_ag_codec_selection_setup(bt_ag_info_t *hs,
1601 bt_hfp_agent_error_t err = BT_HFP_AGENT_ERROR_NONE;
1603 DBG("Codec setup [%x]", codec);
1605 /* 1. Compare sending codec & recieved code */
1606 if (hs->codec_info.sending_codec != codec)
1607 err = BT_HFP_AGENT_ERROR_INTERNAL;
1609 /* 2. Send WB or NB command */
1611 case BT_CVSD_CODEC_ID:
1612 err = __bt_ag_set_codec(hs, "SetNbParameters");
1614 case BT_MSBC_CODEC_ID:
1615 err = __bt_ag_set_codec(hs, "SetWbsParameters");
1618 err = BT_HFP_AGENT_ERROR_INTERNAL;
1622 /* If the vendor specific calling returns error or codec is not correct,
1623 * we send CVSD Codec parameter to MM module. and also returns
1624 * normal value to HF
1626 if (err != BT_HFP_AGENT_ERROR_NONE)
1627 codec = BT_CVSD_CODEC_ID;
1629 hs->codec_info.final_codec = codec;
1634 static bt_hfp_agent_error_t __bt_hfp_send_bcs_command(bt_ag_info_t *hs,
1635 gboolean init_by_hf)
1638 struct ag_codec *data = g_new0(struct ag_codec, 1);
1641 codec = BT_CVSD_CODEC_ID;
1643 if (hs->codec_info.remote_codecs & BT_MSBC_CODEC_MASK)
1644 codec = BT_MSBC_CODEC_ID;
1646 codec = BT_CVSD_CODEC_ID;
1649 if (wbs_opts.wbs_enable == FALSE)
1650 codec = BT_CVSD_CODEC_ID;
1654 if (_bt_ag_send_at(hs, "\r\n+BCS: %d\r\n", codec) < 0)
1655 return BT_HFP_AGENT_ERROR_INTERNAL;
1657 DBG("Send +BCS:%d\n", codec);
1659 /* Send +BCS command to HF, and wait some times */
1660 hs->codec_info.is_negotiating = TRUE;
1661 hs->codec_info.sending_codec = codec;
1662 hs->codec_info.requested_by_hf = init_by_hf;
1663 hs->codec_info.final_codec = codec;
1665 data->bt_ag_info = hs;
1666 data->codec_status = g_strdup("timeout");
1668 hs->codec_info.nego_timer = g_timeout_add_seconds(
1669 HFP_CODEC_NEGOTIATION_TIMEOUT,
1670 (GSourceFunc)__bt_ag_codec_negotiation_finished,
1673 return BT_HFP_AGENT_ERROR_NONE;
1677 static bt_hfp_agent_error_t __bt_hfp_codec_connection_setup(
1678 bt_ag_info_t *hs, gboolean init_by_hf)
1680 DBG("Request to codec connection by %s", init_by_hf ? "HF" : "AG");
1682 if (hs->state < HEADSET_STATE_CONNECTED)
1683 return BT_HFP_AGENT_ERROR_NOT_CONNECTED;
1685 if (hs->codec_info.is_negotiating == TRUE) {
1686 /* In codec negotiation, return and wait */
1687 ERR("Codec nogotiation is in progress");
1688 return BT_HFP_AGENT_ERROR_BUSY;
1691 /* Not support Codec Negotiation or Not recieved BAC command */
1692 if (!(ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) ||
1693 hs->codec_info.remote_codecs == 0) {
1694 ERR("No support for Codec Negotiation or receive BAC command");
1696 return BT_HFP_AGENT_ERROR_INTERNAL;
1698 __bt_ag_sco_connect(hs);
1699 return BT_HFP_AGENT_ERROR_INTERNAL;
1703 /* If HF initiated codec connection setup, it should send OK command
1704 * before +BCS command transmission.
1707 return HFP_STATE_MNGR_ERR_NONE;
1709 return __bt_hfp_send_bcs_command(hs, init_by_hf);
1713 static int __hfp_parse_available_codecs(const char *cmd, uint32_t *codecs)
1716 *codecs = 0x00000000;
1718 str = strchr(cmd, '=');
1722 while (str != NULL) {
1725 if (atoi(str) == BT_CVSD_CODEC_ID)
1726 *codecs |= BT_CVSD_CODEC_MASK;
1727 else if (atoi(str) == BT_MSBC_CODEC_ID)
1728 *codecs |= BT_MSBC_CODEC_MASK;
1730 str = strchr(str, ',');
1733 if (*codecs == 0x00000000)
1739 /* AT+BAC (Bluetooth Available Codecs) */
1740 static int __bt_hfp_available_codecs(bt_ag_info_t *hs, const char *buf)
1742 uint32_t codecs = 0x00000000;
1743 hfp_state_manager_err_t err = HFP_STATE_MNGR_ERR_NONE;
1745 if (!(ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION)) {
1746 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1747 } else if (__hfp_parse_available_codecs(buf, &codecs) < 0) {
1748 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1750 DBG("Update remote available codecs [%x]", codecs);
1751 hs->codec_info.remote_codecs = codecs;
1754 _bt_ag_send_response(hs, err);
1756 /* Reset codec information and
1757 * restart codec connection setup by AG
1759 hs->codec_info.final_codec = 0;
1760 if (hs->codec_info.nego_timer) {
1761 hs->codec_info.is_negotiating = FALSE;
1762 hs->codec_info.requested_by_hf = FALSE;
1763 hs->codec_info.sending_codec = 0;
1764 g_source_remove(hs->codec_info.nego_timer);
1765 __bt_hfp_codec_connection_setup(hs, FALSE);
1771 /* AT+BCC (Bluetooth Codec Connection) */
1772 static int __bt_hfp_codec_connection(bt_ag_info_t *hs, const char *buf)
1774 hfp_state_manager_err_t err = HFP_STATE_MNGR_ERR_NONE;
1776 err = __bt_hfp_codec_connection_setup(hs, TRUE);
1778 _bt_ag_send_response(hs, err);
1780 if (err == HFP_STATE_MNGR_ERR_NONE)
1781 err = __bt_hfp_send_bcs_command(hs, TRUE);
1783 if (err != HFP_STATE_MNGR_ERR_NONE)
1784 ERR("Fail to request codec connection setup");
1789 /* AT+BCS (Bluetooth Codec Selection) */
1790 static int __bt_hfp_codec_selection(bt_ag_info_t *hs, const char *buf)
1792 uint32_t codec = 0x00000000;
1793 hfp_state_manager_err_t err = HFP_STATE_MNGR_ERR_NONE;
1794 struct ag_codec *data = g_new0(struct ag_codec, 1);
1797 if (hs->codec_info.nego_timer) {
1798 g_source_remove(hs->codec_info.nego_timer);
1799 hs->codec_info.nego_timer = 0;
1802 if (!(ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION))
1803 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1804 else if (__hfp_parse_available_codecs(buf, &codec) < 0)
1805 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1806 else if (__bt_ag_codec_selection_setup(hs, codec) !=
1807 BT_HFP_AGENT_ERROR_NONE)
1808 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1810 data->bt_ag_info = hs;
1811 data->codec_status = g_strdup("finish");
1812 _bt_ag_send_response(hs, err);
1813 __bt_ag_codec_negotiation_finished(data);
1818 static void __bt_ag_str2ba(const char *str, bt_addr *ba)
1821 for (i = 5; i >= 0; i--, str += 3)
1822 ba->b[i] = strtol(str, NULL, 16);
1825 static const char *__bt_ag_state2str(hs_state_t state)
1828 case HEADSET_STATE_DISCONNECTED:
1829 return "disconnected";
1830 case HEADSET_STATE_CONNECTING:
1831 return "connecting";
1832 case HEADSET_STATE_CONNECTED:
1834 case HEADSET_STATE_PLAY_IN_PROGRESS:
1835 return "Play In Progress";
1836 case HEADSET_STATE_ON_CALL:
1843 void _bt_convert_addr_string_to_type_rev(unsigned char *addr,
1844 const char *address)
1849 ret_if(address == NULL);
1850 ret_if(addr == NULL);
1852 for (i = 0; i < 6; i++) {
1853 addr[5 - i] = strtol(address, &ptr, 16);
1854 if (ptr[0] != '\0') {
1863 static gboolean __bt_ag_check_nval(GIOChannel *chan)
1865 struct pollfd file_desc;
1867 memset(&file_desc, 0, sizeof(file_desc));
1868 file_desc.fd = g_io_channel_unix_get_fd(chan);
1869 file_desc.events = POLLNVAL;
1871 if (poll(&file_desc, 1, 0) > 0 && (POLLNVAL & file_desc.revents))
1877 static int __bt_ag_sco_connect(bt_ag_info_t *hs)
1879 struct sco_socket_addr sco_addr;
1884 bt_ag_slconn_t *slconn = hs->slc;
1887 if (hs->state == HEADSET_STATE_ON_CALL)
1888 return BT_HFP_AGENT_ERROR_ALREADY_CONNECTED;
1890 if (hs->state != HEADSET_STATE_CONNECTED)
1891 return BT_HFP_AGENT_ERROR_NOT_CONNECTED;
1892 #ifdef TIZEN_MEDIA_ENHANCE
1893 _bt_ag_agent_check_transport_state();
1896 /* Create Sco socket */
1897 sco_skt = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BT_SCO_PRTCL);
1899 ERR("ERROR: Create socket failed.\n");
1900 return BT_HFP_AGENT_ERROR_INTERNAL;
1903 /* Bind Sco Socket to Local BD addr */
1904 memset(&sco_addr, 0, sizeof(sco_addr));
1905 sco_addr.sco_family = AF_BLUETOOTH;
1907 __bt_ag_str2ba(local_addr, &sco_addr.sco_bdaddr);
1908 DBG("Local BD address: %s", local_addr);
1910 err = bind(sco_skt, (struct sockaddr *) &sco_addr, sizeof(sco_addr));
1912 ERR("ERROR: sco socket binding failed");
1913 ERR("Close SCO skt");
1915 return BT_HFP_AGENT_ERROR_INTERNAL;
1918 DBG("Socket FD : %d", sco_skt);
1920 io = g_io_channel_unix_new(sco_skt);
1921 g_io_channel_set_close_on_unref(io, TRUE);
1922 /*g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
1923 g_io_channel_set_buffered(io, FALSE);
1924 g_io_channel_set_encoding(io, NULL, NULL);*/
1926 if ((ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) &&
1927 (slconn && (slconn->hs_features &
1928 BT_HF_FEATURE_CODEC_NEGOTIATION)) &&
1929 wbs_opts.wbs_enable == TRUE) {
1930 bt_vo.setting = (hs->codec == BT_MSBC_CODEC_ID) ?
1931 BT_HFP_MSBC_VOICE : BT_HFP_CVSD_VOICE;
1933 DBG("set Bluetooth voice: %d", bt_vo.setting);
1934 err = setsockopt(sco_skt, BT_SOCKET_LEVEL,
1935 BT_VOICE_NUM, &bt_vo, sizeof(bt_vo));
1937 ERR("ERROR: sco socket set socket option failed");
1938 ERR("Close SCO skt");
1939 g_io_channel_unref(io);
1941 return BT_HFP_AGENT_ERROR_INTERNAL;
1944 DBG("Set NB codec parameter");
1945 __bt_ag_set_codec(hs, "SetNbParameters");
1948 memset(&sco_addr, 0, sizeof(sco_addr));
1949 sco_addr.sco_family = AF_BLUETOOTH;
1950 __bt_ag_str2ba(hs->remote_addr, &sco_addr.sco_bdaddr);
1951 DBG("remotel BD address: %s", hs->remote_addr);
1953 err = connect(sco_skt, (struct sockaddr *) &sco_addr, sizeof(sco_addr));
1954 if (err < 0 && !(errno == EINPROGRESS || errno == EAGAIN)) {
1955 ERR("ERROR: sco socket connect failed : %d", err);
1956 ERR("Close SCO skt");
1957 g_io_channel_unref(io);
1959 return BT_HFP_AGENT_ERROR_INTERNAL;
1962 /* Disabling the watch since SCO is connected */
1963 /*watch_id = __bt_ag_set_watch(io,
1964 (GIOFunc) __bt_ag_sco_connect_cb, hs);
1966 DBG("SCO watch set Success");*/
1970 _bt_ag_set_headset_state(hs, HEADSET_STATE_ON_CALL);
1971 return BT_HFP_AGENT_ERROR_NONE;
1974 static void __bt_ag_close_sco(bt_ag_info_t *hs)
1978 int sock = g_io_channel_unix_get_fd(hs->sco);
1979 shutdown(sock, SHUT_RDWR);
1980 g_io_channel_unref(hs->sco);
1985 __bt_ag_agent_remove_watch(&hs->sco_id);
1988 static gboolean __bt_ag_sco_server_conn_cb(GIOChannel *chan,
1989 GIOCondition cond, gpointer user_data)
1991 bt_ag_info_t *ag_info = user_data;
1994 if (cond & G_IO_NVAL)
1997 if (cond & (G_IO_HUP | G_IO_ERR)) {
1998 ag_info->sco = NULL;
1999 if (ag_info->sco_id)
2000 __bt_ag_agent_remove_watch(&ag_info->sco_id);
2002 if (ag_info->watch_id)
2003 _bt_ag_set_headset_state(ag_info, HEADSET_STATE_CONNECTED);
2009 static gboolean __bt_ag_sco_server_cb(GIOChannel *chan,
2010 GIOCondition cond, gpointer user_data)
2012 bt_ag_info_t *ag_info = user_data;
2016 bt_ag_slconn_t *slconn = ag_info->slc;
2020 if ((cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) ||
2021 __bt_ag_check_nval(chan)) {
2022 ERR("cond or chan is not valid");
2026 INFO_C("Incoming SCO....");
2028 if (ag_info->state < HEADSET_STATE_CONNECTED)
2029 return BT_HFP_AGENT_ERROR_NOT_CONNECTED;
2031 sco_skt = g_io_channel_unix_get_fd(chan);
2033 cli_sco_sock = accept(sco_skt, NULL, NULL);
2034 if (cli_sco_sock < 0) {
2035 ERR("accept is failed");
2039 sco_io = g_io_channel_unix_new(cli_sco_sock);
2040 g_io_channel_set_close_on_unref(sco_io, TRUE);
2041 g_io_channel_set_encoding(sco_io, NULL, NULL);
2042 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2043 g_io_channel_set_buffered(sco_io, FALSE);
2045 if ((ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) &&
2046 (slconn && (slconn->hs_features &
2047 BT_HF_FEATURE_CODEC_NEGOTIATION)) &&
2048 wbs_opts.wbs_enable == TRUE) {
2049 bt_vo.setting = (ag_info->codec == BT_MSBC_CODEC_ID) ?
2050 BT_HFP_MSBC_VOICE : BT_HFP_CVSD_VOICE;
2052 DBG("set Bluetooth voice: %d", bt_vo.setting);
2053 err = setsockopt(cli_sco_sock, BT_SOCKET_LEVEL,
2054 BT_VOICE_NUM, &bt_vo, sizeof(bt_vo));
2056 ERR("Sco socket set socket option failed");
2057 close(cli_sco_sock);
2062 ag_info->sco = sco_io;
2063 ag_info->sco_id = g_io_add_watch(sco_io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
2064 __bt_ag_sco_server_conn_cb, ag_info);
2066 if (remote_dev_path)
2067 g_free(remote_dev_path);
2069 remote_dev_path = g_strdup(ag_info->path);
2071 _bt_ag_set_headset_state(ag_info, HEADSET_STATE_ON_CALL);
2076 static int __bt_ag_start_sco_server(bt_ag_info_t *hs)
2078 DBG("Start SCO server");
2079 struct sco_socket_addr addr;
2082 bdaddr_t bd_addr = {{0},};
2084 if (hs->sco_server_started) {
2085 DBG("Already exsist");
2086 return BT_HFP_AGENT_ERROR_ALREADY_EXSIST;
2090 sco_skt = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BT_SCO_PRTCL);
2092 ERR("Can't create socket:\n");
2093 return BT_HFP_AGENT_ERROR_INTERNAL;
2096 /* Bind to local address */
2097 memset(&addr, 0, sizeof(addr));
2098 addr.sco_family = AF_BLUETOOTH;
2100 _bt_convert_addr_string_to_type_rev(bd_addr.b, hs->remote_addr);
2101 DBG("Bind to address %s", hs->remote_addr);
2102 memcpy(&addr.sco_bdaddr, &bd_addr, sizeof(bdaddr_t));
2104 if (bind(sco_skt, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2105 ERR("Can't bind socket:\n");
2109 if (listen(sco_skt, 1)) {
2110 ERR("Can not listen on the socket:\n");
2114 sco_io = g_io_channel_unix_new(sco_skt);
2115 g_io_channel_set_close_on_unref(sco_io, TRUE);
2116 g_io_channel_set_encoding(sco_io, NULL, NULL);
2117 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2118 g_io_channel_set_buffered(sco_io, FALSE);
2120 hs->sco_server = sco_io;
2121 hs->sco_watch_id = g_io_add_watch(sco_io,
2122 G_IO_IN | G_IO_HUP | G_IO_ERR |
2123 G_IO_NVAL, __bt_ag_sco_server_cb, hs);
2125 hs->sco_server_started = TRUE;
2126 return BT_HFP_AGENT_ERROR_NONE;
2130 return BT_HFP_AGENT_ERROR_INTERNAL;
2133 void __bt_ag_stop_sco_server(bt_ag_info_t *hs)
2135 DBG("Stop SCO server");
2136 if (hs->sco_server) {
2137 g_io_channel_shutdown(hs->sco_server, TRUE, NULL);
2138 g_io_channel_unref(hs->sco_server);
2139 hs->sco_server = NULL;
2141 hs->sco_server_started = FALSE;
2144 static int __bt_ag_headset_close_rfcomm(bt_ag_info_t *hs)
2146 GIOChannel *rfcomm = hs->rfcomm;
2149 g_io_channel_shutdown(rfcomm, TRUE, NULL);
2150 g_io_channel_unref(rfcomm);
2160 static gboolean __bt_ag_sco_cb(GIOChannel *chan, GIOCondition cond,
2163 if (cond & G_IO_NVAL)
2166 if (name_owner_sig_id != -1)
2167 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
2169 name_owner_sig_id = -1;
2173 DBG("Audio connection disconnected");
2174 _bt_ag_set_headset_state(hs, HEADSET_STATE_CONNECTED);
2179 void _bt_ag_set_headset_state(bt_ag_info_t *hs, hs_state_t state)
2181 bt_ag_slconn_t *slconn = hs->slc;
2182 const char *hs_state;
2183 hs_state_t org_state = hs->state;
2184 gboolean val = FALSE;
2186 if (org_state == state)
2189 hs_state = __bt_ag_state2str(state);
2192 case HEADSET_STATE_CONNECTING:
2193 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2195 BT_HEADSET_INTERFACE, "State",
2196 g_variant_new("s", hs_state));
2200 case HEADSET_STATE_CONNECTED:
2201 if (hs->state != HEADSET_STATE_PLAY_IN_PROGRESS)
2202 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2204 BT_HEADSET_INTERFACE, "State",
2205 g_variant_new("s", hs_state));
2207 if (hs->state < state) {
2209 active_devices = g_slist_append(active_devices, hs);
2210 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2212 BT_HEADSET_INTERFACE,
2214 g_variant_new("b", val));
2216 DBG("Device %s connected\n", hs->remote_addr);
2217 #if defined(TIZEN_SUPPORT_DUAL_HF)
2218 if (!hs->is_companion_device)
2219 __bt_ag_start_sco_server(hs);
2221 __bt_ag_start_sco_server(hs);
2224 /* Set default code as Gateway NB */
2225 __bt_ag_set_codec(hs, "SetNbParameters");
2226 } else if (hs->state == HEADSET_STATE_ON_CALL) {
2228 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2230 BT_HEADSET_INTERFACE,
2232 g_variant_new("b", val));
2237 case HEADSET_STATE_DISCONNECTED:
2238 __bt_ag_close_sco(hs);
2239 __bt_ag_headset_close_rfcomm(hs);
2241 if (hs->state == HEADSET_STATE_ON_CALL) {
2243 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2245 BT_HEADSET_INTERFACE,
2247 g_variant_new("b", val));
2251 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2253 BT_HEADSET_INTERFACE,
2255 g_variant_new("b", val));
2256 if (hs->state > HEADSET_STATE_CONNECTING)
2257 _bt_hfp_device_disconnected(hs);
2259 active_devices = g_slist_remove(active_devices, hs);
2261 __bt_ag_codec_negotiation_info_reset(hs, TRUE);
2262 #if 0 /* SCO is crashed if below is called when SCO is opened by hf-agent */
2263 __bt_ag_set_codec(hs, "SetNbParameters");
2267 /* Since SCO server is binded on remote address */
2268 /* Need to stop SCO server once heasdet disconencted*/
2269 if (hs->sco_server_started)
2270 __bt_ag_stop_sco_server(hs);
2272 g_free(hs->remote_addr);
2276 case HEADSET_STATE_PLAY_IN_PROGRESS:
2277 case HEADSET_STATE_ON_CALL:
2279 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2281 BT_HEADSET_INTERFACE, "State",
2282 g_variant_new("s", hs_state));
2284 /*add watch for sco data */
2285 hs->sco_id = g_io_add_watch(hs->sco,
2286 G_IO_ERR | G_IO_NVAL,
2287 (GIOFunc) __bt_ag_sco_cb, hs);
2289 _bt_ag_agent_emit_property_changed(
2290 ag_dbus_conn, hs->path,
2291 BT_HEADSET_INTERFACE, "Playing",
2292 g_variant_new("b", val));
2294 if (slconn->microphone_gain >= 0)
2295 _bt_ag_send_at(hs, "\r\n+VGM=%u\r\n",
2296 slconn->microphone_gain);
2298 if (slconn->speaker_gain >= 0)
2299 _bt_ag_send_at(hs, "\r\n+VGS=%u\r\n",
2300 slconn->speaker_gain);
2310 INFO("STATE CHANGED from [%s(%d)] to [%s(%d)]",
2311 __bt_ag_state2str(org_state), org_state, __bt_ag_state2str(state), state);
2314 static struct event at_event_callbacks[] = {
2315 { "AT+BRSF", _bt_hfp_supported_features },
2316 { "AT+CIND", _bt_hfp_report_indicators },
2317 { "AT+CMER", _bt_hfp_enable_indicators },
2318 { "AT+CHLD", _bt_hfp_call_hold },
2319 { "ATA", _bt_hfp_answer_call },
2320 { "ATD", _bt_hfp_dial_number },
2321 { "AT+VG", _bt_hfp_signal_gain_setting },
2322 { "AT+CHUP", _bt_hfp_terminate_call },
2323 { "AT+CKPD", _bt_hfp_key_press },
2324 { "AT+CLIP", _bt_hfp_cli_notification },
2325 { "AT+BTRH", _bt_hfp_response_and_hold },
2326 { "AT+BLDN", _bt_hfp_last_dialed_number },
2327 { "AT+VTS", _bt_hfp_dtmf_tone },
2328 { "AT+CNUM", _bt_hfp_subscriber_number },
2329 { "AT+CLCC", _bt_hfp_list_current_calls },
2330 { "AT+CMEE", _bt_hfp_extended_errors },
2331 { "AT+CCWA", _bt_hfp_call_waiting_notify },
2332 { "AT+COPS", _bt_hfp_operator_selection },
2333 { "AT+NREC", _bt_hfp_nr_and_ec },
2334 { "AT+BVRA", _bt_hfp_voice_dial },
2335 { "AT+XAPL", _bt_hfp_apl_command },
2336 { "AT+IPHONEACCEV", _bt_hfp_apl_command },
2337 { "AT+BIA", _bt_hfp_indicators_activation },
2338 { "AT+CPBS", _bt_hfp_select_pb_memory },
2339 { "AT+CPBR", _bt_hfp_read_pb_entries},
2340 { "AT+CPBF", _bt_hfp_find_pb_entires },
2341 { "AT+CSCS", _bt_hfp_select_character_set },
2342 { "AT+CSQ", _bt_hfp_get_signal_quality },
2343 { "AT+CBC", _bt_hfp_get_battery_charge_status },
2344 { "AT+CPAS", _bt_hfp_get_activity_status },
2345 { "AT+CGSN", _bt_hfp_get_equipment_identity },
2346 { "AT+CGMM", _bt_hfp_get_model_information },
2347 { "AT+CGMI", _bt_hfp_get_device_manufacturer },
2348 { "AT+CGMR", _bt_hfp_get_revision_information },
2349 { "AT+BAC", __bt_hfp_available_codecs },
2350 { "AT+BCC", __bt_hfp_codec_connection },
2351 { "AT+BCS", __bt_hfp_codec_selection },
2352 { "AT+XSAT", _bt_hfp_vendor_cmd },
2353 { "AT+CIMI", _bt_hfp_get_imsi },
2354 { "AT+CREG", _bt_hfp_get_creg_status },
2358 int num_of_secure_command = 4;
2359 static const char* secure_command[] = {"CLCC", "CLIP", "CPBR", "CCWA"};
2361 void __bt_ag_agent_print_at_buffer(char *message, const char *buf)
2365 char s[MAX_BUFFER_SIZE] = {0, };
2366 gboolean hide = FALSE;
2368 gboolean is_security_command = FALSE;
2371 strncpy(s, buf, MAX_BUFFER_SIZE - 1);
2373 for (i = 0; i < num_of_secure_command; i++) {
2374 if (strstr(buf, secure_command[i])) {
2375 is_security_command = TRUE;
2380 /* +XSAT: 11,DISC */
2381 xsat_ptr = strstr(s, "11,DISC,");
2383 xsat_ptr = xsat_ptr + 8;
2385 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
2391 /* AT+XSAT=11,Q_CT,X,XXXX */
2392 xsat_ptr = strstr(s, "11,Q_CT,");
2394 xsat_ptr = xsat_ptr + 8;
2396 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
2397 if (x > 1) /* ignore 0 and 1 position */
2404 while (s[i] != '\0') {
2405 if (s[i] == '\r' || s[i] == '\n') {
2409 hide = hide ? FALSE : TRUE;
2410 else if (is_security_command && hide) {
2418 INFO("%s Buffer = [%s], Len(%d)", message, s, strlen(s));
2423 static int __bt_ag_at_handler(bt_ag_info_t *hs, const char *buf)
2427 __bt_ag_agent_print_at_buffer("[AG AT CMD][RCVD] :", buf);
2429 for (ev = at_event_callbacks; ev->cmd; ev++) {
2430 if (!strncmp(buf, ev->cmd, strlen(ev->cmd)))
2431 return ev->callback(hs, buf);
2437 static int __bt_ag_send_at_valist(bt_ag_info_t *hdset, va_list list,
2440 ssize_t final_written, count;
2441 char rsp_buffer[MAX_BUFFER_SIZE];
2445 count = vsnprintf(rsp_buffer, sizeof(rsp_buffer), list_format, list);
2447 ERR("count is %d", count);
2451 if (!hdset->io_chan) {
2452 ERR("__bt_ag_send_at_valist: headset not connected");
2458 fd = g_io_channel_unix_get_fd(hdset->io_chan);
2461 while (final_written < count) {
2465 written = write(fd, rsp_buffer + final_written,
2466 count - final_written);
2467 } while (written < 0 && errno == EINTR);
2471 ERR("write failed : %s (%d)", strerror(-err), -err);
2475 final_written += written;
2478 /* Synchronize the sending buffer */
2482 ERR("FD is 0. remote_addr : %s", hdset->remote_addr);
2486 __bt_ag_agent_print_at_buffer("[AG AT CMD][SENT]", rsp_buffer);
2491 int __attribute__((format(printf, 2, 3)))
2492 _bt_ag_send_at(bt_ag_info_t *hs, char *format, ...)
2497 va_start(ap, format);
2498 ret = __bt_ag_send_at_valist(hs, ap, format);
2504 void __attribute__((format(printf, 3, 4)))
2505 _bt_ag_send_foreach_headset(GSList *devices,
2506 int (*cmp) (bt_ag_info_t *hs),
2512 for (l = devices; l != NULL; l = l->next) {
2513 bt_ag_info_t *hs = l->data;
2516 if (cmp && cmp(hs) != 0)
2519 va_start(ap, format);
2520 ret = __bt_ag_send_at_valist(hs, ap, format);
2522 ERR("Failed to send to headset: %s (%d)",
2523 strerror(-ret), -ret);
2528 int _bt_ag_send_response(bt_ag_info_t *hs, hfp_state_manager_err_t err)
2530 if ((err != HFP_STATE_MNGR_ERR_NONE) && hs->slc->is_cme_enabled)
2531 return _bt_ag_send_at(hs, "\r\n+CME ERROR: %d\r\n", err);
2534 case HFP_STATE_MNGR_ERR_NONE:
2535 return _bt_ag_send_at(hs, "\r\nOK\r\n");
2536 case HFP_STATE_MNGR_ERR_NO_NETWORK_SERVICE:
2537 return _bt_ag_send_at(hs, "\r\nNO CARRIER\r\n");
2539 return _bt_ag_send_at(hs, "\r\nERROR\r\n");
2543 static gboolean __bt_ag_event_handler(GIOChannel *channel,
2544 GIOCondition cond, void *user_data)
2546 bt_ag_slconn_t *slconn;
2547 unsigned char event_buf[MAX_BUFFER_SIZE];
2549 size_t available_buffer;
2551 bt_ag_info_t *bt_ag_info = (bt_ag_info_t *)user_data;
2554 if (cond & G_IO_NVAL)
2557 slconn = bt_ag_info->slc;
2558 if (cond & (G_IO_ERR | G_IO_HUP)) {
2559 if (bt_ag_info->watch_id)
2560 __bt_ag_agent_remove_watch(&bt_ag_info->watch_id);
2562 ERR("ERR or HUP on RFCOMM socket");
2563 INFO_C("Disconnected [AG role] [Terminated by remote dev]");
2567 fd = g_io_channel_unix_get_fd(channel);
2568 len = read(fd, event_buf, sizeof(event_buf) - 1);
2572 available_buffer = sizeof(slconn->buffer) - (slconn->start) -
2573 (slconn->length) - 1;
2574 if (available_buffer < (size_t) len) {
2575 ERR("Buffer over flow");
2579 memcpy(&slconn->buffer[slconn->start], event_buf, len);
2580 slconn->length += len;
2582 slconn->buffer[slconn->start + slconn->length] = '\0';
2584 while (slconn->length > 0) {
2589 get_cr = strchr(&slconn->buffer[slconn->start], '\r');
2593 cmd_len = 1 + (off_t) get_cr -
2594 (off_t) &slconn->buffer[slconn->start];
2598 DBG("Call AT handler");
2599 err = __bt_ag_at_handler(bt_ag_info,
2600 &slconn->buffer[slconn->start]);
2602 ERR("Failed to call AT handler");
2606 if (err == -EINVAL) {
2607 ERR("Unrecognized command: %s",
2608 &slconn->buffer[slconn->start]);
2609 err = _bt_ag_send_response(bt_ag_info,
2610 HFP_STATE_MNGR_ERR_NOT_SUPPORTED);
2614 ERR("Error handling command %s: %s (%d)",
2615 &slconn->buffer[slconn->start],
2616 strerror(-err), -err);
2618 slconn->start += cmd_len;
2619 slconn->length -= cmd_len;
2621 if (!slconn->length)
2626 ERR("Failed in event handler - SLC Disconnect");
2627 _bt_ag_set_headset_state(bt_ag_info,
2628 HEADSET_STATE_DISCONNECTED);
2632 static gboolean __bt_ag_agent_connection(gint32 fd, const gchar *device_path,
2633 const gchar *object_path)
2637 bt_ag_info_t *bt_ag_info = g_new0(bt_ag_info_t, 1);
2638 struct sockaddr_remote address;
2639 socklen_t address_len;
2641 INFO_C("Connected [AG role]");
2642 bt_ag_info->rfcomm = NULL;
2643 bt_ag_info->slc = NULL;
2644 bt_ag_info->hfp_active = TRUE;
2645 bt_ag_info->vr_blacklisted = FALSE;
2646 bt_ag_info->state = HEADSET_STATE_DISCONNECTED;
2647 bt_ag_info->sco_server_started = FALSE;
2648 __bt_ag_codec_negotiation_info_reset(bt_ag_info, TRUE);
2650 bt_ag_info->path = device_path;
2651 DBG("device_path = [%s]", device_path);
2653 address_len = sizeof(address);
2654 if (getpeername(fd, (struct sockaddr *) &address, &address_len) != 0)
2655 ERR("BD_ADDR is NULL");
2657 DBG("RFCOMM connection for HFP/HSP is completed. Fd = [%d]", fd);
2658 bt_ag_info->fd = fd;
2659 bt_ag_info->io_chan = g_io_channel_unix_new(bt_ag_info->fd);
2660 flags = g_io_channel_get_flags(bt_ag_info->io_chan);
2662 flags &= ~G_IO_FLAG_NONBLOCK;
2663 flags &= G_IO_FLAG_MASK;
2664 g_io_channel_set_flags(bt_ag_info->io_chan, flags, NULL);
2665 g_io_channel_set_encoding(bt_ag_info->io_chan, NULL, NULL);
2666 g_io_channel_set_buffered(bt_ag_info->io_chan, FALSE);
2668 bt_ag_info->rfcomm = g_io_channel_ref(bt_ag_info->io_chan);
2670 bt_ag_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2671 __bt_convert_addr_type_to_rev_string(bt_ag_info->remote_addr,
2672 address.remote_bdaddr.b);
2674 #if defined(TIZEN_SUPPORT_DUAL_HF)
2675 bt_ag_info->is_companion_device =
2676 __bt_ag_agent_is_companion_device(bt_ag_info->remote_addr);
2679 DBG("remote Device Address = [%s]", bt_ag_info->remote_addr);
2681 if (g_strcmp0(object_path, BT_HS_AG_AGENT_OBJECT_PATH) == 0) {
2682 DBG("HSP connection completed");
2683 _bt_ag_set_headset_state(bt_ag_info,
2684 HEADSET_STATE_CONNECTED);
2686 DBG("HFP connection connecting");
2687 _bt_ag_set_headset_state(bt_ag_info,
2688 HEADSET_STATE_CONNECTING);
2691 __bt_ag_agent_start_watch(bt_ag_info);
2693 bt_ag_info->slc = g_new0(bt_ag_slconn_t, 1);
2694 bt_ag_info->slc->speaker_gain = 15;
2695 bt_ag_info->slc->microphone_gain = 15;
2696 bt_ag_info->slc->is_nrec = TRUE;
2701 static gboolean __bt_ag_agent_is_device_vr_blacklisted(const char *lap_addr)
2710 fp = fopen(AGENT_VR_BLACKLIST_FILE, "r");
2713 ERR("Unable to open VR blacklist file");
2717 fseek(fp, 0, SEEK_END);
2720 ERR("size is not a positive number");
2727 buffer = g_malloc0(sizeof(char) * size);
2728 if (buffer == NULL) {
2729 ERR("g_malloc0 is failed");
2733 result = fread((char *)buffer, 1, size, fp);
2735 if (result != size) {
2741 token = strtok_r(buffer, "=", &saveptr);
2742 if (token == NULL) {
2747 while ((token = strtok_r(NULL, ",", &saveptr))) {
2748 if (strlen(token) > 8)
2750 if (0 == g_strcmp0(token, lap_addr)) {
2751 INFO("Voice Recognition blacklisted");
2760 static gboolean __bt_sco_open_delay_timeout_cb(gpointer user_data)
2762 bt_ag_info_t *bt_ag_info = (bt_ag_info_t *)user_data;
2763 sco_open_timer_id = 0;
2764 DBG("sco_open_request (%d)", sco_open_request);
2766 if (sco_open_request && bt_ag_info->state == HEADSET_STATE_CONNECTED) {
2767 bt_ag_slconn_t *slconn = bt_ag_info->slc;
2769 INFO("try to open SCO");
2770 sco_open_request = FALSE;
2772 if ((ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) &&
2773 (slconn && (slconn->hs_features &
2774 BT_HF_FEATURE_CODEC_NEGOTIATION))) {
2775 switch (bt_ag_info->codec_info.final_codec) {
2776 case BT_CVSD_CODEC_ID:
2777 __bt_ag_set_codec(bt_ag_info, "SetNbParameters");
2778 __bt_ag_sco_connect(bt_ag_info);
2780 case BT_MSBC_CODEC_ID:
2781 __bt_ag_set_codec(bt_ag_info, "SetWbsParameters");
2782 __bt_ag_sco_connect(bt_ag_info);
2785 __bt_hfp_codec_connection_setup(bt_ag_info, FALSE);
2789 __bt_ag_sco_connect(bt_ag_info);
2796 * Service level connection complete
2797 * indication and state management
2799 void _bt_ag_slconn_complete(bt_ag_info_t *hs)
2801 char lap_address[BT_LOWER_ADDRESS_LENGTH];
2803 DBG("HFP Service Level Connection established\n");
2805 /* Check device Voice Recognition blacklist status */
2806 g_strlcpy(lap_address, hs->remote_addr, sizeof(lap_address));
2807 hs->vr_blacklisted =
2808 __bt_ag_agent_is_device_vr_blacklisted(lap_address);
2810 if (sco_open_timer_id > 0) {
2811 g_source_remove(sco_open_timer_id);
2812 sco_open_timer_id = 0;
2815 sco_open_request = FALSE;
2816 sco_open_timer_id = g_timeout_add(BT_SCO_OPEN_DELAY_TIMER,
2817 __bt_sco_open_delay_timeout_cb, hs);
2819 _bt_ag_set_headset_state(hs, HEADSET_STATE_CONNECTED);
2822 static gboolean __bt_ag_agent_connection_release(bt_ag_info_t *hs)
2825 g_io_channel_shutdown(hs->io_chan, TRUE, NULL);
2826 g_io_channel_unref(hs->io_chan);
2830 __bt_ag_close_sco(hs);
2831 _bt_ag_set_headset_state(hs, HEADSET_STATE_CONNECTED);
2834 __bt_ag_agent_remove_watch(&hs->watch_id);
2836 _bt_ag_set_headset_state(hs, HEADSET_STATE_DISCONNECTED);
2840 static GQuark __bt_ag_agent_error_quark(void)
2844 static GQuark quark = 0;
2846 quark = g_quark_from_static_string("ag-agent");
2852 static GError *__bt_ag_agent_set_error(bt_hfp_agent_error_t error)
2855 ERR("error[%d]\n", error);
2858 case BT_HFP_AGENT_ERROR_NOT_AVAILABLE:
2859 return g_error_new(BT_AG_AGENT_ERROR, error,
2860 BT_ERROR_NOT_AVAILABLE);
2861 case BT_HFP_AGENT_ERROR_NOT_CONNECTED:
2862 return g_error_new(BT_AG_AGENT_ERROR, error,
2863 BT_ERROR_NOT_CONNECTED);
2864 case BT_HFP_AGENT_ERROR_BUSY:
2865 return g_error_new(BT_AG_AGENT_ERROR, error,
2867 case BT_HFP_AGENT_ERROR_INVALID_PARAM:
2868 return g_error_new(BT_AG_AGENT_ERROR, error,
2869 BT_ERROR_INVALID_PARAM);
2870 case BT_HFP_AGENT_ERROR_ALREADY_EXSIST:
2871 return g_error_new(BT_AG_AGENT_ERROR, error,
2872 BT_ERROR_ALREADY_EXSIST);
2873 case BT_HFP_AGENT_ERROR_ALREADY_CONNECTED:
2874 return g_error_new(BT_AG_AGENT_ERROR, error,
2875 BT_ERROR_ALREADY_CONNECTED);
2876 case BT_HFP_AGENT_ERROR_NO_MEMORY:
2877 return g_error_new(BT_AG_AGENT_ERROR, error,
2878 BT_ERROR_NO_MEMORY);
2879 case BT_HFP_AGENT_ERROR_I_O_ERROR:
2880 return g_error_new(BT_AG_AGENT_ERROR, error,
2881 BT_ERROR_I_O_ERROR);
2882 case BT_HFP_AGENT_ERROR_OPERATION_NOT_AVAILABLE:
2883 return g_error_new(BT_AG_AGENT_ERROR, error,
2884 BT_ERROR_OPERATION_NOT_AVAILABLE);
2885 case BT_HFP_AGENT_ERROR_BATTERY_STATUS:
2886 return g_error_new(BT_AG_AGENT_ERROR, error,
2888 case BT_HFP_AGENT_ERROR_SIGNAL_STATUS:
2889 return g_error_new(BT_AG_AGENT_ERROR, error,
2891 case BT_HFP_AGENT_ERROR_NO_CALL_LOGS:
2892 return g_error_new(BT_AG_AGENT_ERROR, error,
2893 BT_ERROR_NO_CALL_LOG);
2894 case BT_HFP_AGENT_ERROR_INTERNAL:
2896 return g_error_new(BT_AG_AGENT_ERROR, error,
2902 static bt_ag_info_t *__bt_get_active_headset(const gchar *device_path)
2906 for (l = active_devices ; l; l = l->next) {
2907 bt_ag_info_t *data = l->data;
2908 if (device_path == NULL) /*just to avoid crash incase of "play" when dailed from device[NEEDS TO BE CHANGED]*/
2910 if (g_strcmp0(data->path, device_path) == 0) {
2911 INFO("Active device found");
2916 INFO("Active device not found");
2920 static void __bt_ag_agent_method(GDBusConnection *connection,
2921 const gchar *sender,
2922 const gchar *object_path,
2923 const gchar *interface_name,
2924 const gchar *method_name,
2925 GVariant *parameters,
2926 GDBusMethodInvocation *invocation,
2931 INFO("method %s", method_name);
2932 INFO("object_path %s", object_path);
2933 int ret = BT_HFP_AGENT_ERROR_NONE;
2935 const gchar *device_path = NULL;
2937 if (g_strcmp0(method_name, "NewConnection") == 0) {
2941 GUnixFDList *fd_list;
2942 GVariant *options = NULL;
2943 int device_count = 0;
2945 device_count = g_slist_length(active_devices);
2947 INFO("device_count %d", device_count);
2949 if (device_count >= MAX_CONNECTED_DEVICES) {
2950 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2954 g_variant_get(parameters, "(oha{sv})",
2955 &device_path, &index, &options);
2956 #if defined(TIZEN_SUPPORT_DUAL_HF)
2958 __bt_ag_agent_check_dual_hf_condition(device_path) == FALSE) {
2959 INFO("not allow to connect 2nd HF connection");
2960 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2964 msg = g_dbus_method_invocation_get_message(invocation);
2965 fd_list = g_dbus_message_get_unix_fd_list(msg);
2966 if (fd_list == NULL) {
2967 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2971 fd = g_unix_fd_list_get(fd_list, index, NULL);
2973 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2977 DBG("FD is = [%d], device_path = [%s]\n", fd, device_path);
2979 if (!__bt_ag_agent_connection(fd, device_path, object_path)) {
2980 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2984 g_dbus_method_invocation_return_value(invocation, NULL);
2985 } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
2988 g_variant_get(parameters, "(o)", &device_path);
2989 INFO("device_path %s", device_path);
2991 for (l = active_devices; l; l = l->next) {
2992 bt_ag_info_t *data = l->data;
2994 INFO("data->path %s", data->path);
2995 if (g_strcmp0(data->path, device_path) == 0) {
2996 if (!__bt_ag_agent_connection_release(data)) {
2997 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3000 INFO_C("Disconnected [AG role] [Terminated by local host]");
3001 g_dbus_method_invocation_return_value(invocation, NULL);
3004 } else if (g_strcmp0(method_name, "RegisterApplication") == 0) {
3006 gchar *address = NULL;
3007 g_variant_get(parameters, "(&s&s)", &path, &address);
3008 /*local_addr = malloc(strlen(address));
3009 memcpy(local_addr, address, strlen(address));*/
3011 DBG("Sender = %s, Application path = %s\n", sender, path);
3012 ret = _bt_hfp_register_telephony_agent(TRUE, path, sender);
3017 local_addr = g_strdup(address);
3018 DBG("Address = %s\n", local_addr);
3019 g_dbus_method_invocation_return_value(invocation, NULL);
3020 } else if (g_strcmp0(method_name, "UnregisterApplication") == 0) {
3022 g_variant_get(parameters, "(&s)", &path);
3024 DBG("Application path = %s\n", path);
3025 DBG("Sender = %s\n", sender);
3027 ret = _bt_hfp_register_telephony_agent(FALSE, path, sender);
3031 g_dbus_method_invocation_return_value(invocation, NULL);
3032 } else if (g_strcmp0(method_name, "IncomingCall") == 0) {
3034 gchar *number = NULL;
3037 g_variant_get(parameters, "(&s&si)", &path, &number, &call_id);
3039 DBG("Application path = %s", path);
3040 DBG_SECURE("Phone number = %s", number);
3041 DBG("Call id = %d", call_id);
3043 DBG("Sender = %s", sender);
3045 ret = _bt_hfp_incoming_call(path, number, call_id, sender);
3048 g_dbus_method_invocation_return_value(invocation, NULL);
3049 } else if (g_strcmp0(method_name, "OutgoingCall") == 0) {
3051 gchar *number = NULL;
3054 g_variant_get(parameters, "(&s&si)", &path, &number, &call_id);
3056 DBG("Application path = %s", path);
3057 DBG_SECURE("Phone number = %s", number);
3058 DBG("Call id = %d", call_id);
3060 DBG("Sender = %s", sender);
3062 ret = _bt_hfp_outgoing_call(path, number, call_id, sender);
3065 g_dbus_method_invocation_return_value(invocation, NULL);
3066 } else if (g_strcmp0(method_name, "ChangeCallStatus") == 0) {
3068 gchar *number = NULL;
3073 g_variant_get(parameters, "(&s&sii)",
3074 &path, &number, &status, &call_id);
3075 DBG("Application path = %s\n", path);
3076 DBG_SECURE("Number = %s\n", number);
3077 DBG("Status = %d\n", status);
3078 DBG("Call id = %d\n", call_id);
3079 DBG("Sender = %s\n", sender);
3081 ret = _bt_hfp_change_call_status(path,
3082 number, status, call_id, sender);
3084 if (_bt_hfp_is_call_exist() == FALSE) {
3085 for (l = active_devices; l; l = l->next) {
3086 bt_ag_info_t *data = l->data;
3088 if (data->state == HEADSET_STATE_ON_CALL) {
3089 __bt_ag_close_sco(data);
3090 _bt_ag_set_headset_state(data,
3091 HEADSET_STATE_CONNECTED);
3098 g_dbus_method_invocation_return_value(invocation, NULL);
3099 } else if (g_strcmp0(method_name, "GetProperties") == 0) {
3100 GVariantBuilder *builder;
3102 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3105 gchar *codec = g_strdup("codec");
3106 gchar *nrec = g_strdup("nrec");
3108 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
3110 g_variant_builder_add(builder, "{sv}",
3111 codec, g_variant_new("u", bt_ag_info->codec_info.final_codec));
3112 g_variant_builder_add(builder, "{sv}",
3113 nrec, g_variant_new("b", bt_ag_info->nrec_status));
3115 var_data = g_variant_new("(a{sv})", builder);
3116 g_variant_builder_unref(builder);
3117 g_dbus_method_invocation_return_value(invocation, var_data);
3122 } else if (g_strcmp0(method_name, "Disconnect") == 0) {
3123 char hdset_address[18] = { 0, };
3126 for (l = active_devices; l; l = l->next) {
3127 bt_ag_info_t *data = l->data;
3129 __bt_convert_addr_type_to_rev_string(hdset_address,
3130 (unsigned char *)data->remote_addr);
3132 DBG("Disconnect Headset %s, %s\n",
3133 hdset_address, data->path);
3134 _bt_ag_set_headset_state(data,
3135 HEADSET_STATE_DISCONNECTED);
3137 g_dbus_method_invocation_return_value(invocation, NULL);
3138 } else if (g_strcmp0(method_name, "IsConnected") == 0) {
3139 gboolean is_connected = FALSE;
3142 for (l = active_devices; l; l = l->next) {
3143 bt_ag_info_t *data = l->data;
3145 if (data->state == HEADSET_STATE_CONNECTED)
3146 is_connected = TRUE;
3148 DBG("is_connected : %s",
3149 is_connected ? "Connected" : "Disconnected");
3151 g_dbus_method_invocation_return_value(invocation,
3152 g_variant_new("(b)", is_connected));
3153 } else if (g_strcmp0(method_name, "IndicateCall") == 0) {
3156 if (0 == g_slist_length(active_devices)) {
3157 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3161 if (ag.ring_timer) {
3162 DBG("IndicateCall received when already indicating");
3163 g_dbus_method_invocation_return_value(invocation, NULL);
3166 for (l = active_devices; l; l = l->next) {
3167 bt_ag_info_t *data = l->data;
3169 if (data->state >= HEADSET_STATE_CONNECTED)
3170 _bt_ag_send_at(data, "\r\nRING\r\n");
3173 __bt_ring_timer_cb(NULL);
3174 ag.ring_timer = g_timeout_add(AG_RING_INTERVAL,
3175 __bt_ring_timer_cb, NULL);
3176 g_dbus_method_invocation_return_value(invocation, NULL);
3177 } else if (g_strcmp0(method_name, "CancelCall") == 0) {
3178 if (0 == g_slist_length(active_devices)) {
3179 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3183 if (ag.ring_timer) {
3184 g_source_remove(ag.ring_timer);
3187 DBG("Got CancelCall method call but no call is active");
3189 g_dbus_method_invocation_return_value(invocation, NULL);
3190 } else if (g_strcmp0(method_name, "Play") == 0) {
3191 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3192 bt_ag_slconn_t *slconn = NULL;
3195 slconn = bt_ag_info->slc;
3197 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3201 #ifndef __TIZEN_OPEN__
3204 if (slconn && FALSE == slconn->is_voice_recognition_running &&
3205 mdm_get_service() == MDM_RESULT_SUCCESS) {
3206 mode = mdm_get_allow_bluetooth_outgoing_call();
3207 mdm_release_service();
3209 if (mode == MDM_RESTRICTED) {
3210 ERR("[MDM] Not allow the outgoing call");
3211 ret = BT_HFP_AGENT_ERROR_OPERATION_NOT_AVAILABLE;
3218 switch (bt_ag_info->state) {
3219 case HEADSET_STATE_CONNECTING:
3220 case HEADSET_STATE_DISCONNECTED:
3221 ERR("HEADSET_STATE ERROR");
3222 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3224 case HEADSET_STATE_CONNECTED:
3226 case HEADSET_STATE_PLAY_IN_PROGRESS:
3227 ERR("Play In Progress");
3228 ret = BT_HFP_AGENT_ERROR_BUSY;
3236 if (sco_open_timer_id > 0) {
3237 INFO("SCO open delay");
3238 sco_open_request = TRUE;
3239 g_dbus_method_invocation_return_value(invocation, NULL);
3243 if ((ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) &&
3244 (slconn && (slconn->hs_features &
3245 BT_HF_FEATURE_CODEC_NEGOTIATION))) {
3246 switch (bt_ag_info->codec_info.final_codec) {
3247 case BT_CVSD_CODEC_ID:
3248 __bt_ag_set_codec(bt_ag_info, "SetNbParameters");
3249 ret = __bt_ag_sco_connect(bt_ag_info);
3251 case BT_MSBC_CODEC_ID:
3252 __bt_ag_set_codec(bt_ag_info, "SetWbsParameters");
3253 ret = __bt_ag_sco_connect(bt_ag_info);
3256 ret = __bt_hfp_codec_connection_setup(bt_ag_info, FALSE);
3260 ret = __bt_ag_sco_connect(bt_ag_info);
3266 sco_owner = g_strdup(sender);
3268 g_dbus_method_invocation_return_value(invocation, NULL);
3270 name_owner_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
3271 NULL, NULL, "NameOwnerChanged", NULL, NULL, 0,
3272 __bt_ag_name_owner_changed_cb, NULL, NULL);
3273 } else if (g_strcmp0(method_name, "Stop") == 0) {
3276 for (l = active_devices; l; l = l->next) {
3277 bt_ag_info_t *data = l->data;
3279 if (data->state > HEADSET_STATE_CONNECTED) {
3280 __bt_ag_close_sco(data);
3281 _bt_ag_set_headset_state(data,
3282 HEADSET_STATE_CONNECTED);
3286 g_dbus_method_invocation_return_value(invocation, NULL);
3287 } else if (g_strcmp0(method_name, "IsPlaying") == 0) {
3288 gboolean is_playing = FALSE;
3291 for (l = active_devices; l; l = l->next) {
3292 bt_ag_info_t *data = l->data;
3294 if (data->state == HEADSET_STATE_ON_CALL)
3297 DBG("is_playing : %s", is_playing ? "Playing" : "Not Playing");
3299 g_dbus_method_invocation_return_value(invocation,
3300 g_variant_new("(b)", is_playing));
3301 } else if (g_strcmp0(method_name, "GetSpeakerGain") == 0) {
3302 bt_ag_slconn_t *slconn = NULL;
3303 guint16 gain_value = 0;
3304 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3306 if (bt_ag_info == NULL) {
3307 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3311 if (bt_ag_info->state < HEADSET_STATE_CONNECTED) {
3312 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3316 slconn = bt_ag_info->slc;
3318 gain_value = (guint16) slconn->speaker_gain;
3320 g_dbus_method_invocation_return_value(invocation,
3321 g_variant_new("(q)", gain_value));
3322 } else if (g_strcmp0(method_name, "SetSpeakerGain") == 0) {
3324 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3326 g_variant_get(parameters, "(q)", &gain);
3327 DBG("Speaker gain = %d\n", gain);
3329 if (bt_ag_info == NULL) {
3330 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3334 ret = _bt_hfp_set_speaker_gain(bt_ag_info, gain);
3337 g_dbus_method_invocation_return_value(invocation, NULL);
3338 } else if (g_strcmp0(method_name, "GetMicrophoneGain") == 0) {
3339 bt_ag_slconn_t *slconn = NULL;
3340 guint16 gain_value = 0;
3341 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3343 if (bt_ag_info == NULL) {
3344 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3348 if (bt_ag_info->state < HEADSET_STATE_CONNECTED) {
3349 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3353 slconn = bt_ag_info->slc;
3355 gain_value = (guint16) slconn->microphone_gain;
3357 g_dbus_method_invocation_return_value(invocation,
3358 g_variant_new("(q)", gain_value));
3359 } else if (g_strcmp0(method_name, "SetMicrophoneGain") == 0) {
3361 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3363 g_variant_get(parameters, "(q)", &gain);
3364 DBG("Microphone gain = %d\n", gain);
3366 if (bt_ag_info == NULL) {
3367 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3371 ret = _bt_hfp_set_microphone_gain(bt_ag_info, gain);
3374 g_dbus_method_invocation_return_value(invocation, NULL);
3375 } else if (g_strcmp0(method_name, "SetVoiceDial") == 0) {
3376 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3377 if (bt_ag_info == NULL) {
3378 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3382 bt_ag_slconn_t *slconn = bt_ag_info->slc;
3385 g_variant_get(parameters, "(b)", &enable);
3386 DBG("VoiceDail enable = %d\n", enable);
3388 if ((slconn && !(slconn->hs_features &
3389 BT_HF_FEATURE_VOICE_RECOGNITION)))
3390 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3391 else if (bt_ag_info->vr_blacklisted)
3392 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3394 ret = _bt_hfp_set_voice_dial(bt_ag_info, enable);
3397 slconn->is_voice_recognition_running = enable;
3401 g_dbus_method_invocation_return_value(invocation, NULL);
3402 } else if (g_strcmp0(method_name, "SendVendorAtCmd") == 0) {
3404 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3405 if (bt_ag_info == NULL) {
3406 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3410 g_variant_get(parameters, "(&s)", &cmd);
3412 ret = BT_HFP_AGENT_ERROR_INVALID_PARAM;
3416 DBG("vendor cmd = %s", cmd);
3418 ret = _bt_hfp_send_vendor_cmd(bt_ag_info, cmd);
3421 g_dbus_method_invocation_return_value(invocation, NULL);
3422 } else if (g_strcmp0(method_name, "CheckPrivilege") == 0) {
3423 DBG("Already pass dbus SMACK for bt-service::platform");
3424 /* Return success */
3425 g_dbus_method_invocation_return_value(invocation, NULL);
3432 err = __bt_ag_agent_set_error(ret);
3433 g_dbus_method_invocation_return_gerror(invocation, err);
3438 static const GDBusInterfaceVTable method_table = {
3439 __bt_ag_agent_method,
3444 static GDBusNodeInfo *__bt_ag_create_method_node_info
3445 (const gchar *introspection_data)
3448 GDBusNodeInfo *node_info = NULL;
3450 if (introspection_data == NULL)
3453 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
3456 ERR("Unable to create node: %s", err->message);
3457 g_clear_error(&err);
3462 static GDBusConnection *__bt_ag_get_gdbus_connection(void)
3468 if (ag_dbus_conn == NULL)
3469 ag_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
3471 if (!ag_dbus_conn) {
3473 ERR("Unable to connect to dbus: %s", err->message);
3474 g_clear_error(&err);
3480 return ag_dbus_conn;
3483 static gboolean __bt_ag_register_profile_methods(void)
3486 GError *error = NULL;
3488 GDBusNodeInfo *node_info = NULL;
3491 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
3493 G_BUS_NAME_OWNER_FLAGS_NONE,
3497 DBG("owner_id is [%d]", owner_id);
3499 node_info = __bt_ag_create_method_node_info(
3500 ag_agent_bluez_introspection_xml);
3501 if (node_info == NULL)
3504 path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
3505 DBG("path is [%s]", path);
3507 hf_bluez_id = g_dbus_connection_register_object(ag_dbus_conn, path,
3508 node_info->interfaces[0],
3510 NULL, NULL, &error);
3511 if (hf_bluez_id == 0) {
3512 ERR("Failed to register: %s", error->message);
3513 g_error_free(error);
3515 g_dbus_node_info_unref(node_info);
3520 /* Ag register profile methods for HSP*/
3522 path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
3523 DBG("path is [%s]", path);
3525 hs_bluez_id = g_dbus_connection_register_object(ag_dbus_conn, path,
3526 node_info->interfaces[0],
3528 NULL, NULL, &error);
3529 if (hs_bluez_id == 0) {
3530 ERR("Failed to register: %s", error->message);
3531 g_error_free(error);
3533 g_dbus_node_info_unref(node_info);
3537 g_dbus_node_info_unref(node_info);
3539 node_info = __bt_ag_create_method_node_info
3540 (ag_agent_app_introspection_xml);
3541 if (node_info == NULL)
3544 path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
3545 DBG("path is [%s]", path);
3547 app_id = g_dbus_connection_register_object(ag_dbus_conn, path,
3548 node_info->interfaces[0],
3550 NULL, NULL, &error);
3552 ERR("Failed to register: %s", error->message);
3553 g_error_free(error);
3555 g_dbus_node_info_unref(node_info);
3559 g_dbus_node_info_unref(node_info);
3565 static void __bt_ag_unregister_profile_methods(void)
3569 if (hf_bluez_id > 0) {
3570 g_dbus_connection_unregister_object(ag_dbus_conn,
3575 if (hs_bluez_id > 0) {
3576 g_dbus_connection_unregister_object(ag_dbus_conn,
3582 g_dbus_connection_unregister_object(ag_dbus_conn,
3588 static GDBusProxy *__bt_ag_gdbus_get_service_proxy(const gchar *service,
3589 const gchar *path, const gchar *interface)
3591 return (service_gproxy) ? service_gproxy :
3592 __bt_ag_gdbus_init_service_proxy(service,
3596 static void __bt_ag_agent_register(gchar *path, uint16_t profile_version,
3597 char *profile_uuid, const char* profile_name)
3602 GError *error = NULL;
3603 GVariantBuilder *builder;
3605 proxy = __bt_ag_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME,
3606 "/org/bluez", BLUEZ_PROFILE_MGMT_INTERFACE);
3611 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
3613 g_variant_builder_add(builder, "{sv}",
3614 "Name", g_variant_new("s",
3616 g_variant_builder_add(builder, "{sv}",
3617 "Version", g_variant_new("q", profile_version));
3618 /*g_variant_builder_add(builder, "{sv}",
3619 "Role", g_variant_new("s","client"));*/
3620 if (g_strcmp0(path, BT_AG_AGENT_OBJECT_PATH) == 0) {
3621 g_variant_builder_add(builder, "{sv}",
3622 "features", g_variant_new("q", ag.sdp_features));
3625 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
3626 g_variant_new("(osa{sv})", path,
3627 profile_uuid, builder),
3628 G_DBUS_CALL_FLAGS_NONE, -1,
3630 g_variant_builder_unref(builder);
3631 /* set the name and role for the profile*/
3633 /* dBUS-RPC is failed */
3634 ERR("dBUS-RPC is failed");
3636 if (error != NULL) {
3637 /* dBUS gives error cause */
3638 ERR("D-Bus API failure: errCode[%x], message[%s]",
3639 error->code, error->message);
3641 g_clear_error(&error);
3646 g_variant_unref(ret);
3653 static void __bt_ag_agent_unregister(gchar *path)
3658 GError *error = NULL;
3660 proxy = __bt_ag_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME,
3661 "/org/bluez", BLUEZ_PROFILE_MGMT_INTERFACE);
3666 ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
3667 g_variant_new("(o)", path),
3668 G_DBUS_CALL_FLAGS_NONE, -1,
3671 /* set the name and role for the profile*/
3673 /* dBUS-RPC is failed */
3674 ERR("dBUS-RPC is failed");
3676 if (error != NULL) {
3677 /* dBUS gives error cause */
3678 ERR("D-Bus API failure: errCode[%x], message[%s]",
3679 error->code, error->message);
3681 g_clear_error(&error);
3685 g_variant_unref(ret);
3695 static void __bt_ag_agent_battery_status_cb(keynode_t *node)
3697 int batt = vconf_keynode_get_int(node);
3699 _bt_hfp_set_property_value("BatteryBarsChanged", batt);
3702 static void __bt_ag_agent_network_signal_status_cb(keynode_t *node)
3704 int signal_bar = vconf_keynode_get_int(node);
3706 BT_CHECK_SIGNAL_STRENGTH(signal_bar);
3707 _bt_hfp_set_property_value("SignalBarsChanged", signal_bar);
3710 #ifdef TIZEN_SUPPORT_LUNAR_DEVICE
3711 static void __bt_ag_agent_lunar_connection_status_cb(keynode_t *node)
3713 gboolean status = vconf_keynode_get_bool(node);
3715 DBG("status = %d", status);
3717 if (status == TRUE) {
3718 for (l = active_devices; l; l = l->next) {
3719 bt_ag_info_t *data = l->data;
3720 _bt_ag_send_at(data, "\r\n+BSIR:1\r\n");
3726 static void __bt_ag_agent_network_register_status_cb(keynode_t *node)
3728 int service = vconf_keynode_get_int(node);
3729 bt_hfp_agent_network_registration_status_t network_service;
3733 DBG("Current Signal Level = [%d] \n", service);
3736 case VCONFKEY_TELEPHONY_SVCTYPE_NONE:
3737 case VCONFKEY_TELEPHONY_SVCTYPE_NOSVC:
3738 case VCONFKEY_TELEPHONY_SVCTYPE_SEARCH:
3746 ret = vconf_get_int(VCONFKEY_TELEPHONY_SVC_ROAM, &roam_status);
3748 ERR("Get roaming status failed err = %d\n", ret);
3752 if (roam_status == 0 && service == 1)
3753 network_service = BT_AGENT_NETWORK_REG_STATUS_HOME;
3754 else if (roam_status == 1 && service == 1)
3755 network_service = BT_AGENT_NETWORK_REG_STATUS_ROAMING;
3757 network_service = BT_AGENT_NETWORK_REG_STATUS_UNKOWN;
3759 _bt_hfp_set_property_value("RegistrationChanged", network_service);
3762 static void __bt_ag_agent_subscribe_vconf_updates(void)
3768 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
3769 (void *)__bt_ag_agent_battery_status_cb, NULL);
3771 ERR("Subsrciption to battery status failed err = [%d]\n", ret);
3774 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_RSSI,
3775 (void *)__bt_ag_agent_network_signal_status_cb, NULL);
3777 ERR("Subsrciption to netowrk signal failed err = [%d]\n", ret);
3780 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_SVCTYPE,
3781 (void *)__bt_ag_agent_network_register_status_cb, NULL);
3783 ERR("Subsrciption to network failed err = [%d]\n", ret);
3785 #ifdef TIZEN_SUPPORT_LUNAR_DEVICE
3786 ret = vconf_notify_key_changed(VCONF_KEY_BT_LUNAR_ENABLED,
3787 (void *)__bt_ag_agent_lunar_connection_status_cb, NULL);
3789 ERR("Subsrciption to lunar connection failed err = [%d]\n", ret);
3794 static void __bt_ag_agent_release_vconf_updates(void)
3800 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
3801 (vconf_callback_fn)__bt_ag_agent_battery_status_cb);
3803 ERR("vconf_ignore_key_changed failed\n");
3806 ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_RSSI,
3807 (vconf_callback_fn)__bt_ag_agent_network_signal_status_cb);
3809 ERR("vconf_ignore_key_changed failed\n");
3812 ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SVCTYPE,
3813 (vconf_callback_fn)__bt_ag_agent_network_register_status_cb);
3815 ERR("vconf_ignore_key_changed failed\n");
3819 static gboolean __bt_ag_agent_send_subscriber_number_changed(
3822 const char *property = g_strdup("SubscriberNumberChanged");
3826 DBG("Number is %s", number);
3828 if (!_bt_hfp_set_property_name(property, number)) {
3829 DBG("Error- set property for subscriber no change - ERROR\n");
3830 g_free((void *)property);
3833 g_free((void *)property);
3837 static void __bt_ag_agent_sigterm_handler(int signo)
3842 ERR_C("***** Signal handler came with signal %d *****", signo);
3844 for (l = active_devices ; l; l = l->next) {
3845 bt_ag_info_t *data = l->data;
3846 if (!__bt_ag_agent_connection_release(data))
3847 ERR("__bt_ag_agent_connection_release failed");
3850 g_dbus_connection_flush(ag_dbus_conn, NULL, NULL, NULL);
3853 g_main_loop_quit(gmain_loop);
3857 INFO("Terminating AG agent");
3861 if (signo == SIGTERM)
3864 for (i = 0; i < BT_AG_SIG_NUM; i++)
3865 sigaction(bt_ag_sig_to_handle[i], &(bt_ag_sigoldact[i]), NULL);
3870 static void __bt_ag_agent_tel_cb(TapiHandle *handle,
3875 TelSimMsisdnList_t *number;
3876 gchar *subscriber_number;
3878 ERR("*********** result = %d", result);
3880 if (result == TAPI_API_SIM_LOCKED ||
3881 result == TAPI_API_SIM_NOT_INITIALIZED ||
3882 result == TAPI_API_SERVICE_NOT_READY) {
3883 DBG("initializing the tapi event for SIM status");
3884 __bt_ag_agent_reg_sim_event(handle, user_data);
3891 number = (TelSimMsisdnList_t *)data;
3892 subscriber_number = g_strdup(number->list[0].num);
3893 __bt_ag_agent_send_subscriber_number_changed(subscriber_number);
3894 g_free(subscriber_number);
3897 static void __bt_ag_agent_on_noti_sim_status(TapiHandle *handle,
3898 const char *noti_id, void *data, void *user_data)
3900 TelSimCardStatus_t *status = data;
3903 DBG("event TAPI_NOTI_SIM_STATUS received!! status[%d]", *status);
3905 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
3906 __bt_ag_agent_dereg_sim_event(handle);
3907 tapi_result = tel_get_sim_msisdn(handle, __bt_ag_agent_tel_cb,
3909 if (tapi_result != TAPI_API_SUCCESS)
3910 ERR("Fail to get sim info: %d", tapi_result);
3914 static void __bt_ag_agent_reg_sim_event(TapiHandle *handle, void *user_data)
3917 ret = tel_register_noti_event(handle, TAPI_NOTI_SIM_STATUS,
3918 __bt_ag_agent_on_noti_sim_status, user_data);
3920 if (ret != TAPI_API_SUCCESS)
3921 ERR("event register failed(%d)", ret);
3924 static void __bt_ag_agent_dereg_sim_event(TapiHandle *handle)
3927 ret = tel_deregister_noti_event(handle, TAPI_NOTI_SIM_STATUS);
3929 if (ret != TAPI_API_SUCCESS)
3930 ERR("event deregister failed(%d)", ret);
3933 static void __bt_ag_name_owner_changed_cb(GDBusConnection *connection,
3934 const gchar *sender_name,
3935 const gchar *object_path,
3936 const gchar *interface_name,
3937 const gchar *signal_name,
3938 GVariant *parameters,
3942 char *name_owner = NULL;
3943 char *old_owner = NULL;
3944 char *new_owner = NULL;
3946 if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
3949 g_variant_get(parameters, "(sss)", &name_owner, &old_owner, &new_owner);
3951 _bt_hfp_release_all_calls_by_sender(name_owner);
3953 if (sco_owner == NULL)
3956 if (strcasecmp(name_owner, sco_owner) == 0) {
3957 if (name_owner_sig_id != -1)
3958 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
3960 name_owner_sig_id = -1;
3964 for (l = active_devices ; l; l = l->next) {
3965 bt_ag_info_t *data = l->data;
3968 __bt_ag_close_sco(data);
3969 _bt_ag_set_headset_state(data,
3970 HEADSET_STATE_CONNECTED);
3977 static void __bt_ag_agent_filter_cb(GDBusConnection *connection,
3978 const gchar *sender_name,
3979 const gchar *object_path,
3980 const gchar *interface_name,
3981 const gchar *signal_name,
3982 GVariant *parameters,
3987 GVariant *optional_param = NULL;
3989 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
3991 g_variant_get(parameters, "(&o@a{sa{sv}})",
3992 &path, &optional_param);
3995 g_variant_unref(optional_param);
3996 ERR("Invalid adapter path");
4000 INFO("Adapter Path = [%s]", path);
4001 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
4002 gchar *path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
4003 __bt_ag_agent_register(path, hfp_ver,
4004 HFP_AG_UUID, "Hands-Free Audio Gateway");
4006 path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
4007 __bt_ag_agent_register(path, hsp_ver,
4008 HSP_AG_UUID, "Headset Audio Gateway");
4010 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
4011 g_variant_get(parameters, "(&o@as)", &path, &optional_param);
4014 g_variant_unref(optional_param);
4015 ERR("Invalid adapter path");
4019 INFO("Adapter Path = [%s]", path);
4020 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
4021 gchar *path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
4022 __bt_ag_agent_unregister(path);
4024 path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
4025 __bt_ag_agent_unregister(path);
4030 g_variant_unref(optional_param);
4035 static void __bt_ag_agent_dbus_deinit(void)
4038 if (service_gproxy) {
4039 g_object_unref(service_gproxy);
4040 service_gproxy = NULL;
4044 g_object_unref(app_gproxy);
4049 __bt_ag_unregister_profile_methods();
4051 if (owner_sig_id != -1)
4052 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4055 if (name_owner_sig_id != -1)
4056 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4058 #ifdef TIZEN_MEDIA_ENHANCE
4059 if (media_sig_id != -1)
4060 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4063 if (media_state_sig_id != -1)
4064 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4065 media_state_sig_id);
4067 name_owner_sig_id = -1;
4071 g_object_unref(ag_dbus_conn);
4072 ag_dbus_conn = NULL;
4077 static int __bt_ag_agent_get_adapter_path(GDBusConnection *conn, char *path)
4080 GDBusProxy *manager_proxy = NULL;
4081 GVariant *result = NULL;
4082 char *adapter_path = NULL;
4085 return BT_HFP_AGENT_ERROR_INTERNAL;
4087 manager_proxy = g_dbus_proxy_new_sync(conn,
4088 G_DBUS_PROXY_FLAGS_NONE, NULL,
4091 BT_MANAGER_INTERFACE,
4094 if (!manager_proxy) {
4095 ERR("Unable to create proxy: %s", err->message);
4099 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
4100 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
4103 ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
4105 ERR("Fail to get DefaultAdapter");
4110 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
4111 ERR("Incorrect result\n");
4115 g_variant_get(result, "(&o)", &adapter_path);
4117 if (adapter_path == NULL ||
4118 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
4119 ERR("Adapter path is inproper\n");
4124 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
4126 g_variant_unref(result);
4127 g_object_unref(manager_proxy);
4132 g_clear_error(&err);
4135 g_variant_unref(result);
4138 g_object_unref(manager_proxy);
4140 return BT_HFP_AGENT_ERROR_INTERNAL;
4144 #ifdef TIZEN_MEDIA_ENHANCE
4145 void _bt_ag_agent_check_transport_state(void)
4149 if (transport_state == MEDIA_TRANSPORT_STATE_PLAYING) {
4153 proxy = g_dbus_proxy_new_sync(ag_dbus_conn,
4154 G_DBUS_PROXY_FLAGS_NONE, NULL,
4155 "org.PulseAudio2", A2DP_SOURCE_ENDPOINT,
4156 BLUEZ_MEDIA_ENDPOINT_INTERFACE, NULL, &err);
4160 ERR("Unable to create proxy: %s", err->message);
4161 g_clear_error(&err);
4165 INFO_C("SuspendMedia initiated");
4167 g_dbus_proxy_call(proxy,
4168 "SuspendMedia", NULL,
4169 G_DBUS_CALL_FLAGS_NONE, 2000,
4171 transport_state = MEDIA_TRANSPORT_STATE_IDLE;
4177 static void __bt_ag_agent_transport_state_update(const char *value)
4180 if (!g_strcmp0(value, "idle"))
4181 transport_state = MEDIA_TRANSPORT_STATE_IDLE;
4182 else if (!g_strcmp0(value, "pending") || !g_strcmp0(value, "active"))
4183 transport_state = MEDIA_TRANSPORT_STATE_PLAYING;
4185 transport_state = MEDIA_TRANSPORT_STATE_DISCONNECTED;
4187 INFO_C("transport_state %d", transport_state);
4190 static void __bt_ag_agent_media_filter_cb(GDBusConnection *connection,
4191 const gchar *sender_name,
4192 const gchar *object_path,
4193 const gchar *interface_name,
4194 const gchar *signal_name,
4195 GVariant *parameters,
4200 GVariant *dict_param = NULL;
4201 GVariant *optional_param = NULL;
4203 if (strcasecmp(signal_name, "PropertiesChanged") == 0) {
4204 if (g_strcmp0(g_variant_get_type_string(parameters),
4206 ERR("Incorrect parameters\n");
4210 g_variant_get(parameters, "(&s@a{sv}@as)",
4211 &inter, &dict_param, &optional_param);
4212 if (dict_param && (!g_strcmp0(inter,
4213 BLUEZ_MEDIA_TRANSPORT_INTERFACE))){
4214 GVariantIter *iter = NULL;
4215 const gchar *key = NULL;
4216 GVariant *value_var = NULL;
4217 gchar *value = NULL;
4218 g_variant_get(dict_param, "a{sv}", &iter);
4219 while (g_variant_iter_loop(
4220 iter, "{sv}", &key, &value_var)) {
4222 if (g_strcmp0(key, "State") == 0) {
4223 value = (gchar *)g_variant_get_string(
4226 DBG("value %s", value);
4227 __bt_ag_agent_transport_state_update(value);
4231 g_variant_iter_free(iter);
4232 g_variant_unref(dict_param);
4234 } else if (strcasecmp(signal_name, "ProfileStateChanged") == 0) {
4235 char *profile_uuid = NULL;
4238 g_variant_get(parameters, "(&si)", &profile_uuid, &state);
4239 if ((g_strcmp0(profile_uuid, A2DP_SINK_UUID) == 0) &&
4240 (state == BT_PROFILE_STATE_DISCONNECTED)) {
4241 DBG("Updating the transport state");
4242 __bt_ag_agent_transport_state_update("Disconnect");
4247 g_variant_unref(dict_param);
4250 g_variant_unref(optional_param);
4255 static void __bt_ag_agent_dbus_init(void)
4259 if (__bt_ag_get_gdbus_connection() == NULL) {
4260 ERR("Error in creating the gdbus connection\n");
4263 if (!__bt_ag_register_profile_methods()) {
4264 ERR("Error in HFP / HSP register_profile_methods\n");
4268 if (!__bt_ag_agent_get_adapter_path(ag_dbus_conn , NULL)) {
4270 gchar *path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
4271 __bt_ag_agent_register(path, hfp_ver,
4272 HFP_AG_UUID, "Hands-Free Audio Gateway");
4274 path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
4275 __bt_ag_agent_register(path, hsp_ver,
4276 HSP_AG_UUID, "Headset Audio Gateway");
4279 owner_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
4280 NULL, BT_MANAGER_INTERFACE, NULL, NULL, NULL, 0,
4281 __bt_ag_agent_filter_cb, NULL, NULL);
4282 #ifdef TIZEN_MEDIA_ENHANCE
4283 media_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
4284 NULL, BT_PROPERTIES_INTERFACE, NULL, NULL,
4285 NULL, 0, __bt_ag_agent_media_filter_cb,
4288 media_state_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
4289 NULL, BLUEZ_DEVICE_INTERFACE, NULL, NULL,
4290 NULL, 0, __bt_ag_agent_media_filter_cb,
4293 transport_state = MEDIA_TRANSPORT_STATE_DISCONNECTED;
4299 static uint32_t __bt_ag_agent_get_ag_features(void)
4302 uint32_t ag_features = BT_AG_FEATURE_EC_AND_NR |
4303 BT_AG_FEATURE_REJECT_CALL |
4304 BT_AG_FEATURE_ENHANCED_CALL_STATUS |
4305 BT_AG_FEATURE_THREE_WAY_CALL |
4307 BT_AG_FEATURE_VOICE_RECOGNITION |
4309 BT_AG_FEATURE_EXTENDED_ERROR_RESULT_CODES;
4312 #if defined(TIZEN_BT_HFP_AG_ENABLE)
4313 hfp_ver = HFP_VERSION_1_6;
4315 hfp_ver = HFP_VERSION_1_5;
4317 hsp_ver = HSP_VERSION_1_2;
4319 if (hfp_ver == HFP_VERSION_1_6)
4320 ag_features |= BT_AG_FEATURE_CODEC_NEGOTIATION;
4324 void *__bt_ag_agent_telephony_init(void *arg)
4328 uint32_t ag_features = *((uint32_t *)arg);
4330 INFO_C("Initializing the telephony info");
4332 _bt_hfp_initialize_telephony_manager(ag_features);
4333 __bt_ag_agent_subscribe_vconf_updates();
4335 tapi_handle = tel_init(NULL);
4336 tapi_result = tel_get_sim_msisdn(tapi_handle, __bt_ag_agent_tel_cb,
4338 if (tapi_result != TAPI_API_SUCCESS)
4339 ERR("Fail to get sim info: %d", tapi_result);
4346 uint32_t ag_features;
4347 struct sigaction sa;
4348 pthread_t thread_id;
4350 INFO_C("Starting Bluetooth AG agent");
4352 ag_features = __bt_ag_agent_get_ag_features();
4354 ag.sdp_features = (uint16_t) ag_features & 0x1F;
4356 if (hfp_ver == HFP_VERSION_1_6 && wbs_en == TRUE)
4357 ag.sdp_features |= BT_AG_FEATURE_SDP_WIDEBAND_SPEECH;
4359 memset(&sa, 0, sizeof(sa));
4360 sa.sa_flags = SA_NOCLDSTOP;
4361 sa.sa_handler = __bt_ag_agent_sigterm_handler;
4363 for (i = 0; i < BT_AG_SIG_NUM; i++)
4364 sigaction(bt_ag_sig_to_handle[i], &sa, &(bt_ag_sigoldact[i]));
4366 gmain_loop = g_main_loop_new(NULL, FALSE);
4368 if (gmain_loop == NULL) {
4369 ERR("GMainLoop create failed");
4370 return EXIT_FAILURE;
4373 __bt_ag_agent_dbus_init();
4374 if (pthread_create(&thread_id, NULL,
4375 (void *)&__bt_ag_agent_telephony_init,
4376 &ag_features) < 0) {
4377 ERR("pthread_create() is failed");
4378 return EXIT_FAILURE;
4381 if (pthread_detach(thread_id) < 0) {
4382 ERR("pthread_detach() is failed");
4385 g_main_loop_run(gmain_loop);
4389 tel_deinit(tapi_handle);
4391 __bt_ag_agent_dbus_deinit();
4392 _bt_hfp_deinitialize_telephony_manager();
4393 __bt_ag_agent_release_vconf_updates();
4395 if (remote_dev_path)
4396 g_free(remote_dev_path);
4399 g_main_loop_unref(gmain_loop);
4401 INFO_C("Terminating Bluetooth AG agent");