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"
32 #include "bluetooth-agent-profile.h"
34 #include <TapiUtility.h>
36 #include <ITapiModem.h>
37 #include <TelNetwork.h>
40 #include <system_info.h>
45 static GMainLoop *gmain_loop = NULL;
46 static GDBusProxy *service_gproxy;
47 static guint interface_added_sig_id = 0;
48 static guint interface_removed_sig_id = 0;
49 static guint name_owner_sig_id = 0;
50 GDBusConnection *ag_dbus_conn = NULL;
51 gchar *remote_dev_path = NULL;
55 static TapiHandle *tapi_handle;
56 extern wbs_options wbs_opts;
57 GSList *active_devices = NULL;
58 static gchar *local_addr = NULL;
59 static GDBusProxy *app_gproxy;
60 static gboolean call_launch_requested = FALSE;
61 static gchar* sco_owner = NULL;
62 static guint sco_open_timer_id = 0;
63 static gboolean sco_open_request = FALSE;
64 static guint hf_bluez_id;
65 static guint hs_bluez_id;
67 #ifdef TIZEN_FEATURE_BT_MEDIA_ENHANCE
68 static guint media_sig_id = 0;
69 static guint media_state_sig_id = 0;
70 static bt_ag_media_transport_state_t transport_state;
73 #define HSP_AG_UUID "00001112-0000-1000-8000-00805f9b34fb"
74 #define HFP_AG_UUID "0000111f-0000-1000-8000-00805f9b34fb"
75 #ifdef TIZEN_FEATURE_BT_MEDIA_ENHANCE
76 #define A2DP_SINK_UUID "0000110b-0000-1000-8000-00805f9b34fb"
78 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
79 #define VCONF_KEY_BT_LUNAR_ENABLED "db/wms/bt_loop_device_hfp_connected"
81 #if defined(TIZEN_PROFILE_WEARABLE) && defined(TIZEN_FEATURE_BT_HFP_AG)
82 #define CALL_APP_ID "org.tizen.call-ui"
85 #if defined(TIZEN_SUPPORT_DUAL_HF)
86 #define VCONF_KEY_BT_HOST_BT_MAC_ADDR "db/wms/host_bt_mac"
87 #define MAX_CONNECTED_DEVICES 2
89 #define MAX_CONNECTED_DEVICES 1
92 #define BT_AG_SIG_NUM 3
93 static struct sigaction bt_ag_sigoldact[BT_AG_SIG_NUM];
94 static int bt_ag_sig_to_handle[] = { SIGABRT, SIGSEGV, SIGTERM };
96 /*Below Inrospection data is exposed to bluez from agent*/
97 static const gchar ag_agent_bluez_introspection_xml[] =
99 " <interface name='org.bluez.Profile1'>"
100 " <method name='NewConnection'>"
101 " <arg type='o' name='device' direction='in'/>"
102 " <arg type='h' name='fd' direction='in'/>"
103 " <arg type='a{sv}' name='options' direction='in'/>"
105 " <method name='RequestDisconnection'>"
106 " <arg type='o' name='device' direction='in'/>"
111 /*Below Introspection data is exposed to application from agent*/
112 static const gchar ag_agent_app_introspection_xml[] =
114 " <interface name='Org.Hfp.App.Interface'>"
115 " <method name='RegisterApplication'>"
116 " <arg type='s' name='path' direction='in'/>"
117 " <arg type='s' name='address' direction='in'/>"
119 " <method name='UnregisterApplication'>"
120 " <arg type='s' name='path' direction='in'/>"
122 " <method name='IncomingCall'>"
123 " <arg type='s' name='path' direction='in'/>"
124 " <arg type='s' name='number' direction='in'/>"
125 " <arg type='i' name='id' direction='in'/>"
127 " <method name='OutgoingCall'>"
128 " <arg type='s' name='path' direction='in'/>"
129 " <arg type='s' name='number' direction='in'/>"
130 " <arg type='i' name='id' direction='in'/>"
132 " <method name='ChangeCallStatus'>"
133 " <arg type='s' name='path' direction='in'/>"
134 " <arg type='s' name='number' direction='in'/>"
135 " <arg type='i' name='status' direction='in'/>"
136 " <arg type='i' name='id' direction='in'/>"
138 " <method name='GetProperties'>"
139 " <arg type='a{sv}' name='properties' direction='out'/>"
141 " <method name='Disconnect'>"
143 " <method name='IsConnected'>"
144 " <arg type='b' name='connected' direction='out'/>"
146 " <method name='IndicateCall'>"
148 " <method name='CancelCall'>"
150 " <method name='Play'>"
152 " <method name='Stop'>"
154 " <method name='IsPlaying'>"
155 " <arg type='b' name='playing' direction='out'/>"
157 " <method name='GetSpeakerGain'>"
158 " <arg type='q' name='gain' direction='out'/>"
160 " <method name='GetMicrophoneGain'>"
161 " <arg type='q' name='gain' direction='out'/>"
163 " <method name='SetSpeakerGain'>"
164 " <arg type='q' name='gain' direction='in'/>"
166 " <method name='SetMicrophoneGain'>"
167 " <arg type='q' name='gain' direction='in'/>"
169 " <method name='SetVoiceDial'>"
170 " <arg type='b' name='enable' direction='in'/>"
172 " <method name='CheckPrivilege'>"
174 " <method name='SwapHeadset'>"
175 " <arg type='s' name='remote_addr' direction='in'/>"
182 int (*callback)(bt_ag_info_t *hs, const char *buf);
185 struct sco_socket_addr {
186 sa_family_t sco_family;
195 bt_ag_info_t *bt_ag_info;
199 bt_ag_status_t ag = {
200 .telephony_ready = FALSE,
204 .rh = BT_RSP_HOLD_NOT_SUPPORTED,
210 static void __bt_ag_agent_sigterm_handler(int signo);
211 static gboolean __bt_ag_agent_connection(gint32 fd, const gchar *device_path,
212 const gchar *object_path);
213 static gboolean __bt_ag_agent_connection_release(bt_ag_info_t *hs);
214 static gboolean __bt_ag_event_handler(GIOChannel *channel, GIOCondition cond,
216 static int __bt_ag_sco_connect(bt_ag_info_t *hs);
217 void _bt_ag_set_headset_state(bt_ag_info_t *hs, hs_state_t state);
218 static void __bt_ag_agent_reg_sim_event(TapiHandle *handle, void *user_data);
219 static void __bt_ag_agent_dereg_sim_event(TapiHandle *handle);
220 static void __bt_ag_name_owner_changed_cb(GDBusConnection *connection,
221 const gchar *sender_name,
222 const gchar *object_path,
223 const gchar *interface_name,
224 const gchar *signal_name,
225 GVariant *parameters,
228 static void __bt_convert_addr_type_to_rev_string(char *address,
231 ret_if(address == NULL);
232 ret_if(addr == NULL);
234 g_snprintf(address, BT_ADDRESS_STRING_SIZE,
235 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
236 addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
239 static GDBusProxy *__bt_ag_gdbus_init_service_proxy(const gchar *service,
240 const gchar *path, const gchar *interface)
247 if (ag_dbus_conn == NULL)
248 ag_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
252 ERR("Unable to connect to gdbus: %s", err->message);
258 proxy = g_dbus_proxy_new_sync(ag_dbus_conn,
259 G_DBUS_PROXY_FLAGS_NONE, NULL,
261 interface, NULL, &err);
265 ERR("Unable to create proxy: %s", err->message);
275 static GDBusProxy *__bt_ag_gdbus_get_app_proxy(const gchar *service,
276 const gchar *path, const gchar *interface)
278 return (app_gproxy) ? app_gproxy :
279 __bt_ag_gdbus_init_service_proxy(service,
283 static int __bt_ag_agent_gdbus_method_send(const char *service,
284 const gchar *path, const char *interface,
285 const char *method, gboolean response,
286 GVariant *parameters)
292 GError *error = NULL;
294 proxy = __bt_ag_gdbus_get_app_proxy(service, path, interface);
296 return BT_HFP_AGENT_ERROR_INTERNAL;
299 ret = g_dbus_proxy_call_sync(proxy,
301 G_DBUS_CALL_FLAGS_NONE, -1,
304 /* dBUS-RPC is failed */
305 ERR("dBUS-RPC is failed");
307 /* dBUS gives error cause */
308 ERR("D-Bus API failure: errCode[%x], message[%s]",
309 error->code, error->message);
311 g_clear_error(&error);
313 return BT_HFP_AGENT_ERROR_INTERNAL;
316 g_variant_unref(ret);
318 g_dbus_proxy_call(proxy,
320 G_DBUS_CALL_FLAGS_NONE, 2000,
323 return BT_HFP_AGENT_ERROR_NONE;
326 gboolean _bt_ag_agent_emit_signal(
327 GDBusConnection *connection,
329 const char *interface,
335 GError *error = NULL;
337 ret = g_dbus_connection_emit_signal(connection,
338 NULL, path, interface,
343 /* dBUS gives error cause */
344 ERR("D-Bus API failure: errCode[%x], message[%s]",
345 error->code, error->message);
346 g_clear_error(&error);
349 INFO_C("Emit Signal done = [%s]", name);
355 gboolean _bt_ag_agent_emit_property_changed(
356 GDBusConnection *connection,
358 const char *interface,
367 var_data = g_variant_new("(sv)", name, property);
369 ret = _bt_ag_agent_emit_signal(connection,
371 "PropertyChanged", var_data);
376 static void __bt_ag_agent_start_watch(bt_ag_info_t *bt_ag_info)
378 bt_ag_info->watch_id = g_io_add_watch(bt_ag_info->io_chan,
379 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
380 (GIOFunc) __bt_ag_event_handler, bt_ag_info);
383 static void __bt_ag_agent_remove_watch(guint *watch_id)
385 DBG("Remove IO watch ID %d", *watch_id);
387 g_source_remove(*watch_id);
392 #if defined(TIZEN_SUPPORT_DUAL_HF)
393 gboolean __bt_ag_agent_is_companion_device(const char *addr)
395 #if defined(TIZEN_PROFILE_WEARABLE)
396 char *host_device_address = NULL;
397 host_device_address = vconf_get_str(VCONF_KEY_BT_HOST_BT_MAC_ADDR);
399 if (!host_device_address) {
400 INFO("Failed to get a companion device address");
404 if (g_strcmp0(host_device_address, addr) == 0) {
405 INFO("addr[%s] is companion device", addr);
411 /* TODO : Need to add companion device check condition for Phone models */
416 void __bt_convert_device_path_to_address(const gchar *device_path,
417 char *device_address)
419 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
422 ret_if(device_path == NULL);
423 ret_if(device_address == NULL);
425 dev_addr = strstr(device_path, "dev_");
426 if (dev_addr != NULL) {
429 g_strlcpy(address, dev_addr, sizeof(address));
431 while ((pos = strchr(address, '_')) != NULL)
434 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
438 static gboolean __bt_ag_agent_is_companion_device_connected(void)
442 for (l = active_devices ; l; l = l->next) {
443 bt_ag_info_t *data = l->data;
445 if (data->is_companion_device) {
446 DBG("Companion device found");
454 gboolean __bt_ag_agent_check_dual_hf_condition(const gchar *device_path)
456 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
457 gboolean is_companion_device;
459 __bt_convert_device_path_to_address(device_path, device_address);
460 is_companion_device = __bt_ag_agent_is_companion_device(device_address);
462 DBG(" device_address[%s]", device_address);
463 DBG(" is_companion_device[%d]", is_companion_device);
465 if (__bt_ag_agent_is_companion_device_connected()) {
466 if (is_companion_device)
469 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_PROFILE_WEARABLE) && defined(TIZEN_FEATURE_BT_HFP_AG)
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_PROFILE_WEARABLE) && defined(TIZEN_FEATURE_BT_HFP_AG)
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 static gboolean __bt_ag_agent_launch_call_app(const char *number)
562 DBG_SECURE("number(%s)", number);
566 ERR("bundle_create() Failed");
570 bundle_add(b, "launch-type", "MO");
571 bundle_add(b, "dial-type", "HEADSET");
573 if (strlen(number) != 0)
574 bundle_add(b, "number", number);
576 aul_launch_app_async(CALL_APP_ID, b);
583 static void *__bt_ag_agent_launch_call_req(void *arg)
586 bundle *b = (bundle *)arg;
587 if (appsvc_run_service(b, 0, NULL, NULL) < 0)
588 ERR("Unable to run app svc");
590 call_launch_requested = FALSE;
595 static gboolean __bt_ag_agent_make_call(const char *number)
599 char telnum[BT_MAX_TEL_NUM_STRING];
603 if (TIZEN_PROFILE_WEARABLE)
604 return __bt_ag_agent_launch_call_app(number);
606 if (call_launch_requested == TRUE) {
607 DBG("Launch request is in progress");
615 appsvc_set_operation(b, APPSVC_OPERATION_CALL);
616 snprintf(telnum, sizeof(telnum), "tel:%s", number);
617 appsvc_set_uri(b, telnum);
618 appsvc_add_data(b, "ctindex", "-1");
620 call_launch_requested = TRUE;
621 if (pthread_create(&thread_id, NULL,
622 (void *)&__bt_ag_agent_launch_call_req,
624 ERR("pthread_create() is failed");
625 call_launch_requested = FALSE;
628 if (pthread_detach(thread_id) < 0)
629 ERR("pthread_detach() is failed");
635 static gboolean __bt_ag_agent_make_video_call(const char *mo_number)
640 kb = bundle_create();
644 bundle_add(kb, "KEY_CALL_TYPE", "MO");
645 bundle_add(kb, "number", mo_number);
646 aul_launch_app("org.tizen.vtmain", kb);
653 gboolean _bt_ag_agent_answer_call(unsigned int call_id,
654 const gchar *path, const gchar *sender)
658 if (path == NULL || sender == NULL) {
659 DBG("Invalid Arguments");
663 DBG("Application path = %s", path);
664 DBG("Call Id = %d", call_id);
665 DBG("Sender = %s", sender);
667 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
668 BT_AG_SERVICE_NAME, "Answer",
669 g_variant_new("(u)", call_id));
674 gboolean _bt_ag_agent_reject_call(unsigned int call_id,
675 const gchar *path, const gchar *sender)
679 if (path == NULL || sender == NULL) {
680 DBG("Invalid Arguments");
684 DBG("Application path = %s", path);
685 DBG("Call Id = %d", call_id);
686 DBG("Sender = %s", sender);
688 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
689 BT_AG_SERVICE_NAME, "Reject",
690 g_variant_new("(u)", call_id));
695 gboolean _bt_ag_agent_release_call(unsigned int call_id,
696 const gchar *path, const gchar *sender)
700 if (path == NULL || sender == NULL) {
701 DBG("Invalid Arguments");
705 DBG("Application path = %s", path);
706 DBG("Call Id = %d", call_id);
707 DBG("Sender = %s", sender);
709 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
710 BT_AG_SERVICE_NAME, "Release",
711 g_variant_new("(u)", call_id));
717 bt_hfp_agent_error_t _bt_ag_agent_dial_num(const gchar *number, guint flags)
719 bt_hfp_agent_error_t error_code = BT_HFP_AGENT_ERROR_NONE;
721 int phone_lock_state;
726 if (number == NULL) {
727 ERR("Invalid Argument");
728 error_code = BT_HFP_AGENT_ERROR_INVALID_PARAM;
732 DBG("Number = %s", number);
733 DBG("flags = %d", flags);
735 if (!__bt_is_phone_locked(&phone_lock_state)) {
736 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
740 if (!__bt_get_outgoing_callapp_type(&callapp_type)) {
741 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
745 if (!__bt_get_outgoing_call_condition(&condition)) {
746 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
750 if (condition == BT_MO_ONLY_UNLOCKED && phone_lock_state ==
751 VCONFKEY_IDLE_LOCK) {
752 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
756 if (callapp_type == BT_VIDEO_CALL) {
757 if (!__bt_ag_agent_make_video_call(number)) {
758 ERR("Problem launching application");
759 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
763 if (!__bt_ag_agent_make_call(number)) {
764 ERR("Problem launching application");
765 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
775 bt_hfp_agent_error_t _bt_ag_agent_dial_memory(unsigned int location)
777 bt_hfp_agent_error_t error_code = BT_HFP_AGENT_ERROR_NONE;
779 contacts_filter_h filter = NULL;
780 contacts_query_h query = NULL;
781 contacts_list_h list = NULL;
782 contacts_record_h record = NULL;
783 unsigned int projections[] = {
784 _contacts_speeddial.number,
789 DBG("location = %d", location);
791 /*Get number from contacts location*/
792 if (contacts_connect() != CONTACTS_ERROR_NONE) {
793 ERR(" contacts_connect failed");
794 return BT_HFP_AGENT_ERROR_INTERNAL;
797 contacts_filter_create(_contacts_speeddial._uri, &filter);
802 if (contacts_filter_add_int(filter,
803 _contacts_speeddial.speeddial_number,
804 CONTACTS_MATCH_EQUAL, location) !=
805 CONTACTS_ERROR_NONE) {
806 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
810 contacts_query_create(_contacts_speeddial._uri, &query);
815 contacts_query_set_filter(query, filter);
817 if (contacts_query_set_projection(query, projections,
818 sizeof(projections)/sizeof(unsigned int)) !=
819 CONTACTS_ERROR_NONE) {
820 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
824 if (contacts_db_get_records_with_query(query, 0, 1, &list) !=
825 CONTACTS_ERROR_NONE) {
826 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
830 if (contacts_list_first(list) != CONTACTS_ERROR_NONE) {
831 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
835 if (contacts_list_get_current_record_p(list, &record) !=
836 CONTACTS_ERROR_NONE) {
837 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
844 if (contacts_record_get_str(record, _contacts_speeddial.number, &number)
845 != CONTACTS_ERROR_NONE) {
846 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
850 if (number == NULL) {
851 ERR("No number at the location");
852 error_code = BT_HFP_AGENT_ERROR_INVALID_MEMORY_INDEX;
856 DBG("number %s", number);
859 if (!__bt_ag_agent_make_call(number)) {
860 ERR("Problem launching application");
861 error_code = BT_HFP_AGENT_ERROR_INTERNAL;
867 contacts_list_destroy(list, TRUE);
870 contacts_filter_destroy(filter);
873 contacts_query_destroy(query);
875 contacts_disconnect();
882 bt_hfp_agent_error_t _bt_ag_agent_send_dtmf(const gchar *dtmf,
883 const gchar *path, const gchar *sender)
885 bt_hfp_agent_error_t ret;
889 if (dtmf == NULL || path == NULL || sender == NULL) {
890 ERR("Invalid Argument");
891 return BT_HFP_AGENT_ERROR_INVALID_PARAM;
894 DBG("Dtmf = %s", dtmf);
895 DBG("Application path = %s", path);
896 DBG("Sender = %s", sender);
898 ret = __bt_ag_agent_gdbus_method_send(sender,
899 path, TELEPHONY_APP_INTERFACE,
901 g_variant_new("(s)", dtmf));
906 gboolean _bt_ag_agent_threeway_call(unsigned int chld_value,
907 const gchar *path, const gchar *sender)
911 if (path == NULL || sender == NULL) {
912 DBG("Invalid Arguments");
916 DBG("Application path = %s", path);
917 DBG("Value = %d", chld_value);
918 DBG("Sender = %s", sender);
920 #if defined(TIZEN_PROFILE_WEARABLE) && defined(TIZEN_FEATURE_BT_HFP_AG)
921 /* Check if AG supports (i.e. ag_chld_str = "0,1,2") the requested CHLD;
922 if not return FALSE */
923 if (chld_value != 0 && chld_value != 1 && chld_value != 2)
926 if (chld_value != 0 && chld_value != 1 && chld_value != 2 &&
930 _bt_ag_agent_emit_signal(ag_dbus_conn, path,
931 BT_AG_SERVICE_NAME, "Threeway",
932 g_variant_new("(u)", chld_value));
937 bt_hfp_agent_error_t _bt_ag_agent_dial_last_num(void *device)
939 bt_hfp_agent_error_t err_code = BT_HFP_AGENT_ERROR_NONE;
940 char *last_num = NULL;
943 int phone_lock_state;
945 contacts_list_h list = NULL;
946 contacts_query_h query = NULL;
947 contacts_filter_h filter = NULL;
948 contacts_record_h record = NULL;
949 unsigned int projections[] = {
950 _contacts_phone_log.address,
951 _contacts_phone_log.log_type,
956 if (contacts_connect() != CONTACTS_ERROR_NONE) {
957 ERR(" contacts_connect failed");
958 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
962 contacts_filter_create(_contacts_phone_log._uri, &filter);
967 if (contacts_filter_add_int(filter, _contacts_phone_log.log_type,
968 CONTACTS_MATCH_EQUAL,
969 CONTACTS_PLOG_TYPE_VOICE_OUTGOING) !=
970 CONTACTS_ERROR_NONE) {
971 ERR(" contacts_filter_add_int failed");
972 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
976 if (contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR) !=
977 CONTACTS_ERROR_NONE) {
978 ERR(" contacts_filter_add_operator failed");
979 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
983 if (contacts_filter_add_int(filter, _contacts_phone_log.log_type,
984 CONTACTS_MATCH_EQUAL,
985 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING) !=
986 CONTACTS_ERROR_NONE) {
987 ERR(" contacts_filter_add_int failed");
988 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
992 contacts_query_create(_contacts_phone_log._uri, &query);
997 contacts_query_set_filter(query, filter);
999 if (contacts_query_set_projection(query, projections,
1000 sizeof(projections)/sizeof(unsigned int)) !=
1001 CONTACTS_ERROR_NONE) {
1002 ERR(" contacts_query_set_projection failed");
1003 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1007 if (contacts_query_set_sort(query, _contacts_phone_log.log_time, false)
1008 != CONTACTS_ERROR_NONE) {
1009 ERR(" contacts_query_set_sort failed");
1010 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1014 if (contacts_db_get_records_with_query(query, 0, 1, &list) !=
1015 CONTACTS_ERROR_NONE) {
1016 ERR(" contacts_db_get_records_with_query failed");
1017 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1021 if (contacts_list_first(list) != CONTACTS_ERROR_NONE) {
1022 ERR(" contacts_list_first failed");
1023 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1027 if (contacts_list_get_current_record_p(list, &record) !=
1028 CONTACTS_ERROR_NONE) {
1029 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1036 if (contacts_record_get_str(record, _contacts_phone_log.address,
1037 &last_num) != CONTACTS_ERROR_NONE) {
1038 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1042 if (last_num == NULL) {
1043 ERR("No last number");
1044 err_code = BT_HFP_AGENT_ERROR_NO_CALL_LOGS;
1048 if (!__bt_is_phone_locked(&phone_lock_state)) {
1049 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1053 if (!__bt_get_outgoing_callapp_type(&callapp_type)) {
1054 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1058 if (!__bt_get_outgoing_call_condition(&condition)) {
1059 ERR(" Failed to get the call condition");
1060 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1064 if (condition == BT_MO_ONLY_UNLOCKED &&
1065 phone_lock_state == VCONFKEY_IDLE_LOCK) {
1066 ERR(" call condition and phone lock state check fail");
1067 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1071 switch (callapp_type) {
1073 if (!__bt_ag_agent_make_call(last_num)) {
1074 ERR("Problem launching application");
1075 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1079 if (!__bt_ag_agent_make_video_call(last_num)) {
1080 ERR("Problem launching application");
1081 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1084 case BT_FOLLOW_CALL_LOG:
1085 if (contacts_record_get_int(record,
1086 _contacts_phone_log.log_type,
1087 &type) != CONTACTS_ERROR_NONE) {
1088 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1091 if (type == CONTACTS_PLOG_TYPE_VOICE_OUTGOING) {
1092 if (!__bt_ag_agent_make_call(last_num)) {
1093 ERR("Problem launching application");
1094 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1096 } else if (type == CONTACTS_PLOG_TYPE_VIDEO_OUTGOING) {
1097 if (!__bt_ag_agent_make_video_call(last_num)) {
1098 ERR("Problem launching application");
1099 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1102 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1106 err_code = BT_HFP_AGENT_ERROR_INTERNAL;
1113 contacts_list_destroy(list, TRUE);
1116 contacts_filter_destroy(filter);
1119 contacts_query_destroy(query);
1121 contacts_disconnect();
1123 if (last_num != NULL)
1131 bt_hfp_agent_error_t _bt_ag_agent_vendor_cmd(const gchar *cmd,
1132 const gchar *path, const gchar *sender)
1134 bt_hfp_agent_error_t ret;
1138 if (cmd == NULL || path == NULL || sender == NULL) {
1139 ERR("Invalid Argument");
1140 return BT_HFP_AGENT_ERROR_INVALID_PARAM;
1143 DBG("cmd = %s", cmd);
1144 DBG("Application path = %s", path);
1145 DBG("Sender = %s", sender);
1147 ret = __bt_ag_agent_gdbus_method_send(sender,
1148 path, TELEPHONY_APP_INTERFACE,
1150 g_variant_new("(s)", cmd));
1155 gboolean _bt_ag_agent_get_signal_quality(void *device)
1161 if (vconf_get_int(VCONFKEY_TELEPHONY_RSSI, &rssi)) {
1162 DBG("VCONFKEY_TELEPHONY_RSSI failed\n");
1166 DBG("RSSI : %d", rssi);
1168 _bt_hfp_signal_quality_reply(rssi, BT_SIGNAL_QUALITY_BER,
1175 _bt_hfp_signal_quality_reply(-1, -1, device);
1179 gboolean _bt_ag_agent_get_battery_status(void *device)
1181 gint battery_chrg_status;
1182 gint battery_capacity;
1186 if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW,
1187 &battery_chrg_status)) {
1188 DBG("VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW failed\n");
1192 DBG("Status : %d\n", battery_chrg_status);
1194 if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
1195 &battery_capacity)) {
1196 DBG("VCONFKEY_SYSMAN_BATTERY_CAPACITY failed\n");
1200 DBG("Capacity : %d\n", battery_capacity);
1202 _bt_hfp_battery_property_reply(device,
1203 battery_chrg_status, battery_capacity);
1208 _bt_hfp_battery_property_reply(device, -1, -1);
1213 gboolean _bt_ag_agent_get_operator_name(void *device)
1215 char *operator_name;
1218 operator_name = vconf_get_str(VCONFKEY_TELEPHONY_NWNAME);
1219 if (NULL == operator_name) {
1220 DBG("vconf_get_str failed");
1221 _bt_hfp_operator_reply(NULL, device);
1225 DBG("operator_name = [%s]", operator_name);
1227 _bt_hfp_operator_reply(operator_name, device);
1229 free(operator_name);
1235 gboolean _bt_hfp_agent_nrec_status(gboolean status,
1239 bt_ag_info_t *hs = (bt_ag_info_t *)t_device;
1241 DBG("NREC status = %d", status);
1243 hs->nrec_status = FALSE;
1245 hs->nrec_status = TRUE;
1247 _bt_ag_agent_emit_signal(ag_dbus_conn, hs->path,
1248 BT_AG_SERVICE_NAME, "NrecStatusChanged",
1249 g_variant_new("(b)", status));
1254 gboolean _bt_ag_agent_get_imei_number(void *device)
1259 imei_number = tel_get_misc_me_imei_sync(tapi_handle);
1260 if (NULL == imei_number) {
1261 ERR("tel_get_misc_me_imei_sync for imei_number failed");
1265 if (!g_utf8_validate(imei_number, -1, NULL)) {
1267 ERR("get_imei_number : invalid UTF8");
1271 DBG_SECURE("imei_number = [%s]", imei_number);
1272 _bt_hfp_get_imei_number_reply(imei_number, device);
1278 _bt_hfp_get_imei_number_reply(NULL, device);
1283 void _bt_ag_agent_get_manufacturer_name(void *device)
1286 char *manufacturer_name;
1289 ret = system_info_get_platform_string("http://tizen.org/system/manufacturer",
1290 &manufacturer_name);
1291 if (SYSTEM_INFO_ERROR_NONE != ret) {
1292 ERR("Get manufacturer_name failed : %d", ret);
1293 if (NULL != manufacturer_name)
1294 free(manufacturer_name);
1296 manufacturer_name = g_strdup("Unknown");
1297 } else if (!g_utf8_validate(manufacturer_name, -1, NULL)) {
1298 free(manufacturer_name);
1299 manufacturer_name = g_strdup("Unknown");
1300 ERR("get_manufacturer_name : invalid UTF8");
1303 DBG_SECURE("manufacturer_name = [%s]", manufacturer_name);
1304 _bt_hfp_get_device_manufacturer_reply(manufacturer_name, device);
1305 free(manufacturer_name);
1309 void _bt_ag_agent_get_imsi(void *device)
1312 TelSimImsiInfo_t imsi;
1313 memset(&imsi, 0, sizeof(TelSimImsiInfo_t));
1314 if (tel_get_sim_imsi(tapi_handle, &imsi) != TAPI_API_SUCCESS) {
1315 ERR("tel_get_sim_imsi failed");
1318 DBG_SECURE("tapi values %s %s %s", imsi.szMcc, imsi.szMnc, imsi.szMsin);
1320 _bt_hfp_get_imsi_reply(imsi.szMcc, imsi.szMnc, imsi.szMsin, device);
1324 _bt_hfp_get_imsi_reply(NULL, NULL, NULL, device);
1328 int _bt_ag_agent_registration_status_convert(int result)
1331 case TAPI_NETWORK_SERVICE_LEVEL_NO:
1332 return BT_AGENT_NETWORK_REG_STATUS_NOT_REGISTER;
1333 case TAPI_NETWORK_SERVICE_LEVEL_EMERGENCY:
1334 return BT_AGENT_NETWORK_REG_STATUS_EMERGENCY;
1335 case TAPI_NETWORK_SERVICE_LEVEL_FULL:
1336 return BT_AGENT_NETWORK_REG_STATUS_REGISTER_HOME_NETWORK;
1337 case TAPI_NETWORK_SERVICE_LEVEL_SEARCH:
1338 return BT_AGENT_NETWORK_REG_STATUS_SEARCH;
1340 return BT_AGENT_NETWORK_REG_STATUS_UNKNOWN;
1345 void _bt_ag_agent_get_creg_status(void *device)
1351 int registration_status = 0;
1352 int roam_status = 0;
1354 ret = tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_CIRCUIT_STATUS,
1356 if (ret != TAPI_API_SUCCESS) {
1357 ERR("tel_get_property_int failed");
1360 registration_status =
1361 _bt_ag_agent_registration_status_convert(result);
1363 DBG_SECURE("Registration status %d", result);
1364 DBG_SECURE("Mapped Status %d", registration_status);
1365 if (registration_status ==
1366 BT_AGENT_NETWORK_REG_STATUS_REGISTER_HOME_NETWORK) {
1367 ret = vconf_get_int(VCONFKEY_TELEPHONY_SVC_ROAM, &roam_status);
1369 ERR("Get roaming status failed err = %d\n", ret);
1372 DBG_SECURE("Roam status %d", roam_status);
1373 if (roam_status == 1) {
1374 registration_status =
1375 BT_AGENT_NETWORK_REG_STATUS_REGISTERED_ROAMING;
1379 _bt_hfp_get_creg_status_reply(n, registration_status, device);
1385 void _bt_ag_agent_get_model_name(void *device)
1391 ret = system_info_get_platform_string("http://tizen.org/system/model_name", &model_name);
1392 if (SYSTEM_INFO_ERROR_NONE != ret) {
1393 ERR("Get model_name failed: %d", ret);
1394 if (NULL != model_name)
1397 model_name = g_strdup("Unknown");
1398 } else if (!g_utf8_validate(model_name, -1, NULL)) {
1400 model_name = g_strdup("Unknown");
1401 ERR("get_model_name : invalid UTF8");
1404 DBG_SECURE("model_name = [%s]", model_name);
1405 _bt_hfp_get_model_info_reply(model_name, device);
1410 void _bt_ag_agent_get_revision_information(void *device)
1413 char *revision_info;
1416 ret = system_info_get_platform_string("http://tizen.org/system/build.string",
1418 if (SYSTEM_INFO_ERROR_NONE != ret) {
1419 ERR("Get revision_info failed: %d", ret);
1420 if (NULL != revision_info)
1421 free(revision_info);
1423 revision_info = g_strdup("Unknown");
1424 } else if (!g_utf8_validate(revision_info, -1, NULL)) {
1425 free(revision_info);
1426 revision_info = g_strdup("Unknown");
1427 ERR("get_revision_info: invalid UTF8");
1430 DBG_SECURE("revision_info = [%s]", revision_info);
1431 _bt_hfp_get_revision_info_reply(revision_info, device);
1432 free(revision_info);
1436 #if defined(TIZEN_PROFILE_WEARABLE) && defined(TIZEN_FEATURE_BT_HFP_AG)
1437 static gboolean __bt_ag_agent_launch_voice_dial(gboolean activate)
1442 b = bundle_create();
1444 ERR("bundle_create() Failed");
1448 bundle_add(b, "domain", "bt_headset");
1450 bundle_add(b, "action_type", "deactivate");
1452 aul_launch_app_async("org.tizen.svoice", b);
1458 static gboolean __bt_ag_agent_launch_voice_dial(gboolean activate)
1461 app_control_h service = NULL;
1463 app_control_create(&service);
1465 if (service == NULL) {
1466 ERR("Service create failed");
1470 app_control_set_app_id(service, "org.tizen.svoice");
1471 app_control_set_operation(service, APP_CONTROL_OPERATION_DEFAULT);
1472 if (app_control_add_extra_data(service, "domain", "bt_headset")
1473 != APP_CONTROL_ERROR_NONE) {
1474 ERR("app_control_add_extra_data failed");
1475 app_control_destroy(service);
1480 if (app_control_add_extra_data(service, "action_type", "deactivate")
1481 != APP_CONTROL_ERROR_NONE) {
1482 ERR("app_control_add_extra_data failed");
1483 app_control_destroy(service);
1487 if (app_control_send_launch_request(service, NULL, NULL) !=
1488 APP_CONTROL_ERROR_NONE) {
1489 ERR("launch failed");
1490 app_control_destroy(service);
1494 app_control_destroy(service);
1500 gboolean _bt_ag_agent_voice_dial(gboolean activate)
1502 DBG("Activate = %d", activate);
1504 return __bt_ag_agent_launch_voice_dial(activate);
1507 static void __bt_ag_codec_negotiation_info_reset(bt_ag_info_t *hs,
1510 hs->codec_info.is_negotiating = FALSE;
1511 hs->codec_info.requested_by_hf = FALSE;
1512 hs->codec_info.sending_codec = 0;
1514 hs->codec_info.remote_codecs = 0;
1515 hs->codec_info.final_codec = 0;
1516 hs->nrec_status = FALSE;
1519 if (hs->codec_info.nego_timer) {
1520 g_source_remove(hs->codec_info.nego_timer);
1521 hs->codec_info.nego_timer = 0;
1523 wbs_opts.wbs_enable = wbs_en;
1526 static gboolean __bt_ag_codec_negotiation_finished(gpointer user_data)
1528 struct ag_codec *data = (struct ag_codec *)user_data;
1530 if (g_strcmp0(data->codec_status, "finish") == 0) {
1531 DBG("Codec negotiation finished");
1532 __bt_ag_sco_connect(data->bt_ag_info);
1533 __bt_ag_codec_negotiation_info_reset(data->bt_ag_info, FALSE);
1534 g_free(data->codec_status);
1537 } else if (g_strcmp0(data->codec_status, "timeout") == 0) {
1538 DBG("Timeout is occured in codec negotiation");
1541 if (data->bt_ag_info->codec_info.requested_by_hf) {
1542 __bt_ag_codec_negotiation_info_reset(data->bt_ag_info, FALSE);
1544 __bt_ag_sco_connect(data->bt_ag_info);
1545 __bt_ag_codec_negotiation_info_reset(data->bt_ag_info, FALSE);
1547 g_free(data->codec_status);
1553 static bt_hfp_agent_error_t __bt_ag_set_codec(void *device, char *method)
1558 bt_ag_info_t *bt_ag_info = (bt_ag_info_t *)device;
1560 proxy = g_dbus_proxy_new_sync(ag_dbus_conn,
1561 G_DBUS_PROXY_FLAGS_NONE, NULL,
1562 BLUEZ_SERVICE_NAME, DEFAULT_ADAPTER_OBJECT_PATH,
1563 BT_ADAPTER_INTERFACE, NULL, &err);
1567 ERR("Unable to create proxy: %s", err->message);
1568 g_clear_error(&err);
1570 return BT_HFP_AGENT_ERROR_INTERNAL;
1573 ret = g_dbus_proxy_call_sync(proxy, method,
1574 g_variant_new("(ss)", "Gateway", bt_ag_info->remote_addr),
1575 G_DBUS_CALL_FLAGS_NONE, -1,
1578 /* dBUS-RPC is failed */
1579 ERR("dBUS-RPC is failed");
1581 /* dBUS gives error cause */
1582 ERR("D-Bus API failure: errCode[%x], message[%s]",
1583 err->code, err->message);
1585 g_clear_error(&err);
1587 return BT_HFP_AGENT_ERROR_INTERNAL;
1589 g_variant_unref(ret);
1591 return BT_HFP_AGENT_ERROR_NONE;
1594 static bt_hfp_agent_error_t __bt_ag_codec_selection_setup(bt_ag_info_t *hs,
1597 bt_hfp_agent_error_t err = BT_HFP_AGENT_ERROR_NONE;
1599 DBG("Codec setup [%x]", codec);
1601 /* 1. Compare sending codec & recieved code */
1602 if (hs->codec_info.sending_codec != codec)
1603 err = BT_HFP_AGENT_ERROR_INTERNAL;
1605 /* 2. Send WB or NB command */
1607 case BT_CVSD_CODEC_ID:
1608 err = __bt_ag_set_codec(hs, "SetNbParameters");
1610 case BT_MSBC_CODEC_ID:
1611 err = __bt_ag_set_codec(hs, "SetWbsParameters");
1614 err = BT_HFP_AGENT_ERROR_INTERNAL;
1618 /* If the vendor specific calling returns error or codec is not correct,
1619 * we send CVSD Codec parameter to MM module. and also returns
1620 * normal value to HF
1622 if (err != BT_HFP_AGENT_ERROR_NONE)
1623 codec = BT_CVSD_CODEC_ID;
1625 hs->codec_info.final_codec = codec;
1630 static bt_hfp_agent_error_t __bt_hfp_send_bcs_command(bt_ag_info_t *hs,
1631 gboolean init_by_hf)
1634 struct ag_codec *data = NULL;;
1636 if (TIZEN_MODEL_NAME_TM1) {
1637 codec = BT_CVSD_CODEC_ID;
1639 if (hs->codec_info.remote_codecs & BT_MSBC_CODEC_MASK)
1640 codec = BT_MSBC_CODEC_ID;
1642 codec = BT_CVSD_CODEC_ID;
1645 if (wbs_opts.wbs_enable == FALSE)
1646 codec = BT_CVSD_CODEC_ID;
1650 if (_bt_ag_send_at(hs, "\r\n+BCS: %d\r\n", codec) < 0)
1651 return BT_HFP_AGENT_ERROR_INTERNAL;
1653 DBG("Send +BCS:%d\n", codec);
1655 /* Send +BCS command to HF, and wait some times */
1656 hs->codec_info.is_negotiating = TRUE;
1657 hs->codec_info.sending_codec = codec;
1658 hs->codec_info.requested_by_hf = init_by_hf;
1659 hs->codec_info.final_codec = codec;
1661 data = g_new0(struct ag_codec, 1);
1663 return BT_HFP_AGENT_ERROR_NO_MEMORY;
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_FEATURE_BT_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);
1987 if (hs->sco_incoming_id)
1988 __bt_ag_agent_remove_watch(&hs->sco_incoming_id);
1991 static gboolean __bt_ag_sco_server_conn_cb(GIOChannel *chan,
1992 GIOCondition cond, gpointer user_data)
1994 bt_ag_info_t *ag_info = user_data;
1997 if (cond & G_IO_NVAL)
2000 if (cond & (G_IO_HUP | G_IO_ERR)) {
2001 ag_info->sco = NULL;
2002 if (ag_info->sco_id)
2003 __bt_ag_agent_remove_watch(&ag_info->sco_id);
2005 if (ag_info->sco_incoming_id)
2006 __bt_ag_agent_remove_watch(&ag_info->sco_incoming_id);
2008 if (ag_info->watch_id)
2009 _bt_ag_set_headset_state(ag_info, HEADSET_STATE_CONNECTED);
2015 static gboolean __bt_ag_sco_server_cb(GIOChannel *chan,
2016 GIOCondition cond, gpointer user_data)
2018 bt_ag_info_t *ag_info = user_data;
2022 bt_ag_slconn_t *slconn = ag_info->slc;
2026 if ((cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) ||
2027 __bt_ag_check_nval(chan)) {
2028 ERR("cond or chan is not valid");
2032 INFO_C("Incoming SCO....");
2034 if (ag_info->state < HEADSET_STATE_CONNECTED)
2035 return BT_HFP_AGENT_ERROR_NOT_CONNECTED;
2037 sco_skt = g_io_channel_unix_get_fd(chan);
2039 cli_sco_sock = accept(sco_skt, NULL, NULL);
2040 if (cli_sco_sock < 0) {
2041 ERR("accept is failed");
2045 sco_io = g_io_channel_unix_new(cli_sco_sock);
2046 g_io_channel_set_close_on_unref(sco_io, TRUE);
2047 g_io_channel_set_encoding(sco_io, NULL, NULL);
2048 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2049 g_io_channel_set_buffered(sco_io, FALSE);
2051 if ((ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) &&
2052 (slconn && (slconn->hs_features &
2053 BT_HF_FEATURE_CODEC_NEGOTIATION)) &&
2054 wbs_opts.wbs_enable == TRUE) {
2055 bt_vo.setting = (ag_info->codec == BT_MSBC_CODEC_ID) ?
2056 BT_HFP_MSBC_VOICE : BT_HFP_CVSD_VOICE;
2058 DBG("set Bluetooth voice: %d", bt_vo.setting);
2059 err = setsockopt(cli_sco_sock, BT_SOCKET_LEVEL,
2060 BT_VOICE_NUM, &bt_vo, sizeof(bt_vo));
2062 ERR("Sco socket set socket option failed");
2063 close(cli_sco_sock);
2068 ag_info->sco = sco_io;
2069 ag_info->sco_incoming_id = g_io_add_watch(sco_io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
2070 __bt_ag_sco_server_conn_cb, ag_info);
2072 if (remote_dev_path)
2073 g_free(remote_dev_path);
2075 remote_dev_path = g_strdup(ag_info->path);
2077 _bt_ag_set_headset_state(ag_info, HEADSET_STATE_ON_CALL);
2082 static int __bt_ag_start_sco_server(bt_ag_info_t *hs)
2084 DBG("Start SCO server");
2085 struct sco_socket_addr addr;
2088 bdaddr_t bd_addr = {{0},};
2090 if (hs->sco_server_started) {
2091 DBG("Already exsist");
2092 return BT_HFP_AGENT_ERROR_ALREADY_EXSIST;
2096 sco_skt = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BT_SCO_PRTCL);
2098 ERR("Can't create socket:\n");
2099 return BT_HFP_AGENT_ERROR_INTERNAL;
2102 /* Bind to local address */
2103 memset(&addr, 0, sizeof(addr));
2104 addr.sco_family = AF_BLUETOOTH;
2106 _bt_convert_addr_string_to_type_rev(bd_addr.b, hs->remote_addr);
2107 DBG("Bind to address %s", hs->remote_addr);
2108 memcpy(&addr.sco_bdaddr, &bd_addr, sizeof(bdaddr_t));
2110 if (bind(sco_skt, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2111 ERR("Can't bind socket:\n");
2115 if (listen(sco_skt, 1)) {
2116 ERR("Can not listen on the socket:\n");
2120 sco_io = g_io_channel_unix_new(sco_skt);
2121 g_io_channel_set_close_on_unref(sco_io, TRUE);
2122 g_io_channel_set_encoding(sco_io, NULL, NULL);
2123 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2124 g_io_channel_set_buffered(sco_io, FALSE);
2126 hs->sco_server = sco_io;
2127 hs->sco_watch_id = g_io_add_watch(sco_io,
2128 G_IO_IN | G_IO_HUP | G_IO_ERR |
2129 G_IO_NVAL, __bt_ag_sco_server_cb, hs);
2131 hs->sco_server_started = TRUE;
2132 return BT_HFP_AGENT_ERROR_NONE;
2136 return BT_HFP_AGENT_ERROR_INTERNAL;
2139 void __bt_ag_stop_sco_server(bt_ag_info_t *hs)
2141 DBG("Stop SCO server");
2142 if (hs->sco_server) {
2143 g_io_channel_shutdown(hs->sco_server, TRUE, NULL);
2144 g_io_channel_unref(hs->sco_server);
2145 hs->sco_server = NULL;
2147 hs->sco_server_started = FALSE;
2150 static int __bt_ag_headset_close_rfcomm(bt_ag_info_t *hs)
2152 GIOChannel *rfcomm = hs->rfcomm;
2155 g_io_channel_shutdown(rfcomm, TRUE, NULL);
2156 g_io_channel_unref(rfcomm);
2166 static gboolean __bt_ag_sco_cb(GIOChannel *chan, GIOCondition cond,
2169 if (cond & G_IO_NVAL)
2172 if (name_owner_sig_id != -1)
2173 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
2175 name_owner_sig_id = -1;
2179 DBG("Audio connection disconnected");
2180 _bt_ag_set_headset_state(hs, HEADSET_STATE_CONNECTED);
2185 void _bt_ag_set_headset_state(bt_ag_info_t *hs, hs_state_t state)
2187 bt_ag_slconn_t *slconn = hs->slc;
2188 const char *hs_state;
2189 hs_state_t org_state = hs->state;
2190 gboolean val = FALSE;
2192 if (org_state == state)
2195 hs_state = __bt_ag_state2str(state);
2198 case HEADSET_STATE_CONNECTING:
2199 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2201 BT_HEADSET_INTERFACE, "State",
2202 g_variant_new("s", hs_state));
2206 case HEADSET_STATE_CONNECTED:
2207 if (hs->state != HEADSET_STATE_PLAY_IN_PROGRESS)
2208 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2210 BT_HEADSET_INTERFACE, "State",
2211 g_variant_new("s", hs_state));
2213 if (hs->state < state) {
2215 active_devices = g_slist_append(active_devices, hs);
2216 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2218 BT_HEADSET_INTERFACE,
2220 g_variant_new("b", val));
2222 DBG("Device %s connected\n", hs->remote_addr);
2223 #if defined(TIZEN_SUPPORT_DUAL_HF)
2224 if (!hs->is_companion_device)
2225 __bt_ag_start_sco_server(hs);
2227 __bt_ag_start_sco_server(hs);
2230 /* Set default code as Gateway NB */
2231 __bt_ag_set_codec(hs, "SetNbParameters");
2232 } else if (hs->state == HEADSET_STATE_ON_CALL) {
2234 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2236 BT_HEADSET_INTERFACE,
2238 g_variant_new("b", val));
2243 case HEADSET_STATE_DISCONNECTED:
2244 __bt_ag_close_sco(hs);
2245 __bt_ag_headset_close_rfcomm(hs);
2247 if (hs->state == HEADSET_STATE_ON_CALL) {
2249 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2251 BT_HEADSET_INTERFACE,
2253 g_variant_new("b", val));
2257 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2259 BT_HEADSET_INTERFACE,
2261 g_variant_new("b", val));
2262 if (hs->state > HEADSET_STATE_CONNECTING)
2263 _bt_hfp_device_disconnected(hs);
2265 active_devices = g_slist_remove(active_devices, hs);
2267 __bt_ag_codec_negotiation_info_reset(hs, TRUE);
2268 #if 0 /* SCO is crashed if below is called when SCO is opened by hf-agent */
2269 __bt_ag_set_codec(hs, "SetNbParameters");
2273 /* Since SCO server is binded on remote address */
2274 /* Need to stop SCO server once heasdet disconencted*/
2275 if (hs->sco_server_started)
2276 __bt_ag_stop_sco_server(hs);
2278 g_free(hs->remote_addr);
2282 case HEADSET_STATE_PLAY_IN_PROGRESS:
2283 case HEADSET_STATE_ON_CALL:
2285 _bt_ag_agent_emit_property_changed(ag_dbus_conn,
2287 BT_HEADSET_INTERFACE, "State",
2288 g_variant_new("s", hs_state));
2290 /*add watch for sco data */
2291 hs->sco_id = g_io_add_watch(hs->sco,
2292 G_IO_ERR | G_IO_NVAL,
2293 (GIOFunc) __bt_ag_sco_cb, hs);
2295 _bt_ag_agent_emit_property_changed(
2296 ag_dbus_conn, hs->path,
2297 BT_HEADSET_INTERFACE, "Playing",
2298 g_variant_new("b", val));
2300 if (slconn->microphone_gain >= 0)
2301 _bt_ag_send_at(hs, "\r\n+VGM=%u\r\n",
2302 slconn->microphone_gain);
2304 if (slconn->speaker_gain >= 0)
2305 _bt_ag_send_at(hs, "\r\n+VGS=%u\r\n",
2306 slconn->speaker_gain);
2316 INFO("STATE CHANGED from [%s(%d)] to [%s(%d)]",
2317 __bt_ag_state2str(org_state), org_state, __bt_ag_state2str(state), state);
2320 static struct event at_event_callbacks[] = {
2321 { "AT+BRSF", _bt_hfp_supported_features },
2322 { "AT+CIND", _bt_hfp_report_indicators },
2323 { "AT+CMER", _bt_hfp_enable_indicators },
2324 { "AT+CHLD", _bt_hfp_call_hold },
2325 { "ATA", _bt_hfp_answer_call },
2326 { "ATD", _bt_hfp_dial_number },
2327 { "AT+VG", _bt_hfp_signal_gain_setting },
2328 { "AT+CHUP", _bt_hfp_terminate_call },
2329 { "AT+CKPD", _bt_hfp_key_press },
2330 { "AT+CLIP", _bt_hfp_cli_notification },
2331 { "AT+BTRH", _bt_hfp_response_and_hold },
2332 { "AT+BLDN", _bt_hfp_last_dialed_number },
2333 { "AT+VTS", _bt_hfp_dtmf_tone },
2334 { "AT+CNUM", _bt_hfp_subscriber_number },
2335 { "AT+CLCC", _bt_hfp_list_current_calls },
2336 { "AT+CMEE", _bt_hfp_extended_errors },
2337 { "AT+CCWA", _bt_hfp_call_waiting_notify },
2338 { "AT+COPS", _bt_hfp_operator_selection },
2339 { "AT+NREC", _bt_hfp_nr_and_ec },
2340 { "AT+BVRA", _bt_hfp_voice_dial },
2341 { "AT+XAPL", _bt_hfp_apl_command },
2342 { "AT+IPHONEACCEV", _bt_hfp_apl_command },
2343 { "AT+BIA", _bt_hfp_indicators_activation },
2344 { "AT+CPBS", _bt_hfp_select_pb_memory },
2345 { "AT+CPBR", _bt_hfp_read_pb_entries},
2346 { "AT+CPBF", _bt_hfp_find_pb_entires },
2347 { "AT+CSCS", _bt_hfp_select_character_set },
2348 { "AT+CSQ", _bt_hfp_get_signal_quality },
2349 { "AT+CBC", _bt_hfp_get_battery_charge_status },
2350 { "AT+CPAS", _bt_hfp_get_activity_status },
2351 { "AT+CGSN", _bt_hfp_get_equipment_identity },
2352 { "AT+CGMM", _bt_hfp_get_model_information },
2353 { "AT+CGMI", _bt_hfp_get_device_manufacturer },
2354 { "AT+CGMR", _bt_hfp_get_revision_information },
2355 { "AT+BAC", __bt_hfp_available_codecs },
2356 { "AT+BCC", __bt_hfp_codec_connection },
2357 { "AT+BCS", __bt_hfp_codec_selection },
2358 { "AT+XSAT", _bt_hfp_vendor_cmd },
2359 { "AT+CIMI", _bt_hfp_get_imsi },
2360 { "AT+CREG", _bt_hfp_get_creg_status },
2364 int num_of_secure_command = 4;
2365 static const char* secure_command[] = {"CLCC", "CLIP", "CPBR", "CCWA"};
2367 static void __bt_ag_agent_print_at_buffer(char *message, const char *buf)
2371 char s[MAX_BUFFER_SIZE] = {0, };
2372 gboolean hide = FALSE;
2374 gboolean is_security_command = FALSE;
2377 strncpy(s, buf, MAX_BUFFER_SIZE - 1);
2379 for (i = 0; i < num_of_secure_command; i++) {
2380 if (strstr(buf, secure_command[i])) {
2381 is_security_command = TRUE;
2386 /* +XSAT: 11,DISC */
2387 xsat_ptr = strstr(s, "11,DISC,");
2389 xsat_ptr = xsat_ptr + 8;
2391 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
2397 /* AT+XSAT=11,Q_CT,X,XXXX */
2398 xsat_ptr = strstr(s, "11,Q_CT,");
2400 xsat_ptr = xsat_ptr + 8;
2402 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
2403 if (x > 1) /* ignore 0 and 1 position */
2410 while (s[i] != '\0') {
2411 if (s[i] == '\r' || s[i] == '\n') {
2415 hide = hide ? FALSE : TRUE;
2416 else if (is_security_command && hide) {
2424 INFO("%s Buffer = [%s], Len(%d)", message, s, strlen(s));
2429 static int __bt_ag_at_handler(bt_ag_info_t *hs, const char *buf)
2433 __bt_ag_agent_print_at_buffer("[AG AT CMD][RCVD] :", buf);
2435 for (ev = at_event_callbacks; ev->cmd; ev++) {
2436 if (!strncmp(buf, ev->cmd, strlen(ev->cmd)))
2437 return ev->callback(hs, buf);
2443 static int __bt_ag_send_at_valist(bt_ag_info_t *hdset, va_list list,
2446 ssize_t final_written, count;
2447 char rsp_buffer[MAX_BUFFER_SIZE];
2451 count = vsnprintf(rsp_buffer, sizeof(rsp_buffer), list_format, list);
2453 ERR("count is %d", count);
2457 if (!hdset->io_chan) {
2458 ERR("__bt_ag_send_at_valist: headset not connected");
2464 fd = g_io_channel_unix_get_fd(hdset->io_chan);
2467 while (final_written < count) {
2471 written = write(fd, rsp_buffer + final_written,
2472 count - final_written);
2473 } while (written < 0 && errno == EINTR);
2477 ERR("write failed : %s (%d)", strerror(-err), -err);
2481 final_written += written;
2484 /* Synchronize the sending buffer */
2488 ERR("FD is 0. remote_addr : %s", hdset->remote_addr);
2492 __bt_ag_agent_print_at_buffer("[AG AT CMD][SENT]", rsp_buffer);
2497 int __attribute__((format(printf, 2, 3)))
2498 _bt_ag_send_at(bt_ag_info_t *hs, char *format, ...)
2503 va_start(ap, format);
2504 ret = __bt_ag_send_at_valist(hs, ap, format);
2510 void __attribute__((format(printf, 3, 4)))
2511 _bt_ag_send_foreach_headset(GSList *devices,
2512 int (*cmp) (bt_ag_info_t *hs),
2518 for (l = devices; l != NULL; l = l->next) {
2519 bt_ag_info_t *hs = l->data;
2522 if (cmp && cmp(hs) != 0)
2525 va_start(ap, format);
2526 ret = __bt_ag_send_at_valist(hs, ap, format);
2528 ERR("Failed to send to headset: %s (%d)",
2529 strerror(-ret), -ret);
2534 int _bt_ag_send_response(bt_ag_info_t *hs, hfp_state_manager_err_t err)
2536 if ((err != HFP_STATE_MNGR_ERR_NONE) && hs->slc->is_cme_enabled)
2537 return _bt_ag_send_at(hs, "\r\n+CME ERROR: %d\r\n", err);
2540 case HFP_STATE_MNGR_ERR_NONE:
2541 return _bt_ag_send_at(hs, "\r\nOK\r\n");
2542 case HFP_STATE_MNGR_ERR_NO_NETWORK_SERVICE:
2543 return _bt_ag_send_at(hs, "\r\nNO CARRIER\r\n");
2545 return _bt_ag_send_at(hs, "\r\nERROR\r\n");
2549 static gboolean __bt_ag_event_handler(GIOChannel *channel,
2550 GIOCondition cond, void *user_data)
2552 bt_ag_slconn_t *slconn;
2553 unsigned char event_buf[MAX_BUFFER_SIZE];
2555 size_t available_buffer;
2557 bt_ag_info_t *bt_ag_info = (bt_ag_info_t *)user_data;
2561 if (cond & G_IO_NVAL)
2564 slconn = bt_ag_info->slc;
2565 if (cond & (G_IO_ERR | G_IO_HUP)) {
2566 if (bt_ag_info->watch_id)
2567 __bt_ag_agent_remove_watch(&bt_ag_info->watch_id);
2569 ERR("ERR or HUP on RFCOMM socket");
2570 INFO_C("Disconnected [AG role] [Terminated by remote dev]");
2574 fd = g_io_channel_unix_get_fd(channel);
2575 len = read(fd, event_buf, sizeof(event_buf) - 1);
2579 available_buffer = sizeof(slconn->buffer) - (slconn->start) -
2580 (slconn->length) - 1;
2581 if (available_buffer < (size_t) len) {
2582 ERR("Buffer over flow");
2586 memcpy(&slconn->buffer[slconn->start + slconn->length], event_buf, len);
2587 slconn->length += len;
2589 slconn->buffer[slconn->start + slconn->length] = '\0';
2591 while (slconn->length > 0) {
2596 get_cr = strchr(&slconn->buffer[slconn->start], '\r');
2598 ERR("Broken AT command received, break");
2602 cmd_len = 1 + (off_t) get_cr -
2603 (off_t) &slconn->buffer[slconn->start];
2607 DBG("Call AT handler");
2608 err = __bt_ag_at_handler(bt_ag_info,
2609 &slconn->buffer[slconn->start]);
2611 ERR("Failed to call AT handler");
2618 err_return = HFP_STATE_MNGR_ERR_NOT_SUPPORTED;
2621 err_return = HFP_STATE_MNGR_ERR_NOT_ALLOWED;
2624 err_return = HFP_STATE_MNGR_ERR_NOT_SUPPORTED;
2627 ERR("Error handling command %s: %s (%d)",
2628 &slconn->buffer[slconn->start],
2629 strerror(-err), -err);
2630 err = _bt_ag_send_response(bt_ag_info,
2636 slconn->start += cmd_len;
2637 slconn->length -= cmd_len;
2639 if (slconn->length <= 0)
2644 ERR("Failed in event handler - SLC Disconnect");
2645 _bt_ag_set_headset_state(bt_ag_info,
2646 HEADSET_STATE_DISCONNECTED);
2650 static gboolean __bt_ag_agent_connection(gint32 fd, const gchar *device_path,
2651 const gchar *object_path)
2655 bt_ag_info_t *bt_ag_info = g_new0(bt_ag_info_t, 1);
2656 struct sockaddr_remote address;
2657 socklen_t address_len;
2659 INFO_C("Connected [AG role]");
2660 bt_ag_info->rfcomm = NULL;
2661 bt_ag_info->slc = NULL;
2662 bt_ag_info->hfp_active = TRUE;
2663 bt_ag_info->vr_blacklisted = FALSE;
2664 bt_ag_info->state = HEADSET_STATE_DISCONNECTED;
2665 bt_ag_info->sco_server_started = FALSE;
2666 __bt_ag_codec_negotiation_info_reset(bt_ag_info, TRUE);
2668 bt_ag_info->path = device_path;
2669 DBG("device_path = [%s]", device_path);
2671 address_len = sizeof(address);
2672 if (getpeername(fd, (struct sockaddr *) &address, &address_len) != 0)
2673 ERR("BD_ADDR is NULL");
2675 DBG("RFCOMM connection for HFP/HSP is completed. Fd = [%d]", fd);
2676 bt_ag_info->fd = fd;
2677 bt_ag_info->io_chan = g_io_channel_unix_new(bt_ag_info->fd);
2678 flags = g_io_channel_get_flags(bt_ag_info->io_chan);
2680 flags &= ~G_IO_FLAG_NONBLOCK;
2681 flags &= G_IO_FLAG_MASK;
2682 g_io_channel_set_flags(bt_ag_info->io_chan, flags, NULL);
2683 g_io_channel_set_encoding(bt_ag_info->io_chan, NULL, NULL);
2684 g_io_channel_set_buffered(bt_ag_info->io_chan, FALSE);
2686 bt_ag_info->rfcomm = g_io_channel_ref(bt_ag_info->io_chan);
2688 bt_ag_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2689 __bt_convert_addr_type_to_rev_string(bt_ag_info->remote_addr,
2690 address.remote_bdaddr.b);
2692 #if defined(TIZEN_SUPPORT_DUAL_HF)
2693 bt_ag_info->is_companion_device =
2694 __bt_ag_agent_is_companion_device(bt_ag_info->remote_addr);
2697 DBG("remote Device Address = [%s]", bt_ag_info->remote_addr);
2699 if (g_strcmp0(object_path, BT_HS_AG_AGENT_OBJECT_PATH) == 0) {
2700 DBG("HSP connection completed");
2701 _bt_ag_set_headset_state(bt_ag_info,
2702 HEADSET_STATE_CONNECTED);
2704 DBG("HFP connection connecting");
2705 _bt_ag_set_headset_state(bt_ag_info,
2706 HEADSET_STATE_CONNECTING);
2709 __bt_ag_agent_start_watch(bt_ag_info);
2711 bt_ag_info->slc = g_new0(bt_ag_slconn_t, 1);
2712 bt_ag_info->slc->speaker_gain = 15;
2713 bt_ag_info->slc->microphone_gain = 15;
2714 bt_ag_info->slc->is_nrec = TRUE;
2719 static gboolean __bt_ag_agent_is_device_vr_blacklisted(const char *lap_addr)
2728 fp = fopen(AGENT_VR_BLACKLIST_FILE, "r");
2731 ERR("Unable to open VR blacklist file");
2735 fseek(fp, 0, SEEK_END);
2738 ERR("size is not a positive number");
2745 buffer = g_malloc0(sizeof(char) * size);
2746 if (buffer == NULL) {
2747 ERR("g_malloc0 is failed");
2751 result = fread((char *)buffer, 1, size, fp);
2753 if (result != size) {
2759 token = strtok_r(buffer, "=", &saveptr);
2760 if (token == NULL) {
2765 while ((token = strtok_r(NULL, ",", &saveptr))) {
2766 if (strlen(token) > 8)
2768 if (0 == g_strcmp0(token, lap_addr)) {
2769 INFO("Voice Recognition blacklisted");
2778 static gboolean __bt_sco_open_delay_timeout_cb(gpointer user_data)
2780 bt_ag_info_t *bt_ag_info = (bt_ag_info_t *)user_data;
2781 sco_open_timer_id = 0;
2782 DBG("sco_open_request (%d)", sco_open_request);
2784 if (sco_open_request && bt_ag_info->state == HEADSET_STATE_CONNECTED) {
2785 bt_ag_slconn_t *slconn = bt_ag_info->slc;
2787 INFO("try to open SCO");
2788 sco_open_request = FALSE;
2790 if ((ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) &&
2791 (slconn && (slconn->hs_features &
2792 BT_HF_FEATURE_CODEC_NEGOTIATION))) {
2793 switch (bt_ag_info->codec_info.final_codec) {
2794 case BT_CVSD_CODEC_ID:
2795 __bt_ag_set_codec(bt_ag_info, "SetNbParameters");
2796 __bt_ag_sco_connect(bt_ag_info);
2798 case BT_MSBC_CODEC_ID:
2799 __bt_ag_set_codec(bt_ag_info, "SetWbsParameters");
2800 __bt_ag_sco_connect(bt_ag_info);
2803 __bt_hfp_codec_connection_setup(bt_ag_info, FALSE);
2807 __bt_ag_sco_connect(bt_ag_info);
2814 * Service level connection complete
2815 * indication and state management
2817 void _bt_ag_slconn_complete(bt_ag_info_t *hs)
2819 char lap_address[BT_LOWER_ADDRESS_LENGTH];
2821 DBG("HFP Service Level Connection established\n");
2823 /* Check device Voice Recognition blacklist status */
2824 g_strlcpy(lap_address, hs->remote_addr, sizeof(lap_address));
2825 hs->vr_blacklisted =
2826 __bt_ag_agent_is_device_vr_blacklisted(lap_address);
2828 if (sco_open_timer_id > 0) {
2829 g_source_remove(sco_open_timer_id);
2830 sco_open_timer_id = 0;
2833 sco_open_request = FALSE;
2834 sco_open_timer_id = g_timeout_add(BT_SCO_OPEN_DELAY_TIMER,
2835 __bt_sco_open_delay_timeout_cb, hs);
2837 _bt_ag_set_headset_state(hs, HEADSET_STATE_CONNECTED);
2840 static gboolean __bt_ag_agent_connection_release(bt_ag_info_t *hs)
2843 g_io_channel_shutdown(hs->io_chan, TRUE, NULL);
2844 g_io_channel_unref(hs->io_chan);
2848 __bt_ag_close_sco(hs);
2849 _bt_ag_set_headset_state(hs, HEADSET_STATE_CONNECTED);
2851 __bt_ag_agent_remove_watch(&hs->watch_id);
2853 _bt_ag_set_headset_state(hs, HEADSET_STATE_DISCONNECTED);
2857 static GQuark __bt_ag_agent_error_quark(void)
2861 static GQuark quark = 0;
2863 quark = g_quark_from_static_string("ag-agent");
2869 static GError *__bt_ag_agent_set_error(bt_hfp_agent_error_t error)
2872 ERR("error[%d]\n", error);
2875 case BT_HFP_AGENT_ERROR_NOT_AVAILABLE:
2876 return g_error_new(BT_AG_AGENT_ERROR, error,
2877 BT_ERROR_NOT_AVAILABLE);
2878 case BT_HFP_AGENT_ERROR_NOT_CONNECTED:
2879 return g_error_new(BT_AG_AGENT_ERROR, error,
2880 BT_ERROR_NOT_CONNECTED);
2881 case BT_HFP_AGENT_ERROR_BUSY:
2882 return g_error_new(BT_AG_AGENT_ERROR, error,
2884 case BT_HFP_AGENT_ERROR_INVALID_PARAM:
2885 return g_error_new(BT_AG_AGENT_ERROR, error,
2886 BT_ERROR_INVALID_PARAM);
2887 case BT_HFP_AGENT_ERROR_ALREADY_EXSIST:
2888 return g_error_new(BT_AG_AGENT_ERROR, error,
2889 BT_ERROR_ALREADY_EXSIST);
2890 case BT_HFP_AGENT_ERROR_ALREADY_CONNECTED:
2891 return g_error_new(BT_AG_AGENT_ERROR, error,
2892 BT_ERROR_ALREADY_CONNECTED);
2893 case BT_HFP_AGENT_ERROR_NO_MEMORY:
2894 return g_error_new(BT_AG_AGENT_ERROR, error,
2895 BT_ERROR_NO_MEMORY);
2896 case BT_HFP_AGENT_ERROR_I_O_ERROR:
2897 return g_error_new(BT_AG_AGENT_ERROR, error,
2898 BT_ERROR_I_O_ERROR);
2899 case BT_HFP_AGENT_ERROR_OPERATION_NOT_AVAILABLE:
2900 return g_error_new(BT_AG_AGENT_ERROR, error,
2901 BT_ERROR_OPERATION_NOT_AVAILABLE);
2902 case BT_HFP_AGENT_ERROR_BATTERY_STATUS:
2903 return g_error_new(BT_AG_AGENT_ERROR, error,
2905 case BT_HFP_AGENT_ERROR_SIGNAL_STATUS:
2906 return g_error_new(BT_AG_AGENT_ERROR, error,
2908 case BT_HFP_AGENT_ERROR_NO_CALL_LOGS:
2909 return g_error_new(BT_AG_AGENT_ERROR, error,
2910 BT_ERROR_NO_CALL_LOG);
2911 case BT_HFP_AGENT_ERROR_INTERNAL:
2913 return g_error_new(BT_AG_AGENT_ERROR, error,
2919 static bt_ag_info_t *__bt_get_active_headset(const gchar *device_path)
2923 for (l = active_devices ; l; l = l->next) {
2924 bt_ag_info_t *data = l->data;
2925 if (device_path == NULL) /*just to avoid crash incase of "play" when dailed from device[NEEDS TO BE CHANGED]*/
2927 if (g_strcmp0(data->path, device_path) == 0) {
2928 INFO("Active device found");
2933 INFO("Active device not found");
2937 static void __bt_ag_agent_method(GDBusConnection *connection,
2938 const gchar *sender,
2939 const gchar *object_path,
2940 const gchar *interface_name,
2941 const gchar *method_name,
2942 GVariant *parameters,
2943 GDBusMethodInvocation *invocation,
2948 INFO("method %s", method_name);
2949 INFO("object_path %s", object_path);
2950 int ret = BT_HFP_AGENT_ERROR_NONE;
2952 const gchar *device_path = NULL;
2954 if (g_strcmp0(method_name, "NewConnection") == 0) {
2958 GUnixFDList *fd_list;
2959 GVariant *options = NULL;
2960 int device_count = 0;
2962 device_count = g_slist_length(active_devices);
2964 INFO("device_count %d", device_count);
2966 if (device_count >= MAX_CONNECTED_DEVICES) {
2967 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2971 g_variant_get(parameters, "(oha{sv})",
2972 &device_path, &index, &options);
2974 #if defined(TIZEN_SUPPORT_DUAL_HF) && defined(TIZEN_PROFILE_WEARABLE)
2976 * Below code is not required for dual HF support for
2980 __bt_ag_agent_check_dual_hf_condition(device_path) == FALSE) {
2981 INFO("not allow to connect 2nd HF connection");
2982 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2986 msg = g_dbus_method_invocation_get_message(invocation);
2987 fd_list = g_dbus_message_get_unix_fd_list(msg);
2988 if (fd_list == NULL) {
2989 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2993 fd = g_unix_fd_list_get(fd_list, index, NULL);
2995 ret = BT_HFP_AGENT_ERROR_INTERNAL;
2999 DBG("FD is = [%d], device_path = [%s]\n", fd, device_path);
3001 if (!__bt_ag_agent_connection(fd, device_path, object_path)) {
3002 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3006 g_dbus_method_invocation_return_value(invocation, NULL);
3007 } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
3010 g_variant_get(parameters, "(o)", &device_path);
3011 INFO("device_path %s", device_path);
3013 for (l = active_devices; l; l = l->next) {
3014 bt_ag_info_t *data = l->data;
3016 INFO("data->path %s", data->path);
3017 if (g_strcmp0(data->path, device_path) == 0) {
3018 if (!__bt_ag_agent_connection_release(data)) {
3019 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3022 INFO_C("Disconnected [AG role] [Terminated by local host]");
3023 g_dbus_method_invocation_return_value(invocation, NULL);
3026 } else if (g_strcmp0(method_name, "RegisterApplication") == 0) {
3028 gchar *address = NULL;
3029 g_variant_get(parameters, "(&s&s)", &path, &address);
3030 /*local_addr = malloc(strlen(address));
3031 memcpy(local_addr, address, strlen(address));*/
3033 DBG("Sender = %s, Application path = %s\n", sender, path);
3034 ret = _bt_hfp_register_telephony_agent(TRUE, path, sender);
3039 local_addr = g_strdup(address);
3040 DBG("Address = %s\n", local_addr);
3041 g_dbus_method_invocation_return_value(invocation, NULL);
3042 } else if (g_strcmp0(method_name, "UnregisterApplication") == 0) {
3044 g_variant_get(parameters, "(&s)", &path);
3046 DBG("Application path = %s\n", path);
3047 DBG("Sender = %s\n", sender);
3049 ret = _bt_hfp_register_telephony_agent(FALSE, path, sender);
3053 g_dbus_method_invocation_return_value(invocation, NULL);
3054 } else if (g_strcmp0(method_name, "IncomingCall") == 0) {
3056 gchar *number = NULL;
3059 g_variant_get(parameters, "(&s&si)", &path, &number, &call_id);
3061 DBG("Application path = %s", path);
3062 DBG_SECURE("Phone number = %s", number);
3063 DBG("Call id = %d", call_id);
3065 DBG("Sender = %s", sender);
3067 ret = _bt_hfp_incoming_call(path, number, call_id, sender);
3070 g_dbus_method_invocation_return_value(invocation, NULL);
3071 } else if (g_strcmp0(method_name, "OutgoingCall") == 0) {
3073 gchar *number = NULL;
3076 g_variant_get(parameters, "(&s&si)", &path, &number, &call_id);
3078 DBG("Application path = %s", path);
3079 DBG_SECURE("Phone number = %s", number);
3080 DBG("Call id = %d", call_id);
3082 DBG("Sender = %s", sender);
3084 ret = _bt_hfp_outgoing_call(path, number, call_id, sender);
3087 g_dbus_method_invocation_return_value(invocation, NULL);
3088 } else if (g_strcmp0(method_name, "ChangeCallStatus") == 0) {
3090 gchar *number = NULL;
3095 g_variant_get(parameters, "(&s&sii)",
3096 &path, &number, &status, &call_id);
3097 DBG("Application path = %s\n", path);
3098 DBG_SECURE("Number = %s\n", number);
3099 DBG("Status = %d\n", status);
3100 DBG("Call id = %d\n", call_id);
3101 DBG("Sender = %s\n", sender);
3103 ret = _bt_hfp_change_call_status(path,
3104 number, status, call_id, sender);
3106 if (_bt_hfp_is_call_exist() == FALSE) {
3107 for (l = active_devices; l; l = l->next) {
3108 bt_ag_info_t *data = l->data;
3110 if (data->state == HEADSET_STATE_ON_CALL) {
3111 __bt_ag_close_sco(data);
3112 _bt_ag_set_headset_state(data,
3113 HEADSET_STATE_CONNECTED);
3120 g_dbus_method_invocation_return_value(invocation, NULL);
3121 } else if (g_strcmp0(method_name, "GetProperties") == 0) {
3122 GVariantBuilder *builder;
3124 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3127 gchar *codec = g_strdup("codec");
3128 gchar *nrec = g_strdup("nrec");
3130 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
3132 g_variant_builder_add(builder, "{sv}",
3133 codec, g_variant_new("u", bt_ag_info->codec_info.final_codec));
3134 g_variant_builder_add(builder, "{sv}",
3135 nrec, g_variant_new("b", bt_ag_info->nrec_status));
3137 var_data = g_variant_new("(a{sv})", builder);
3138 g_variant_builder_unref(builder);
3139 g_dbus_method_invocation_return_value(invocation, var_data);
3144 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3147 } else if (g_strcmp0(method_name, "Disconnect") == 0) {
3148 char hdset_address[18] = { 0, };
3151 for (l = active_devices; l; l = l->next) {
3152 bt_ag_info_t *data = l->data;
3154 __bt_convert_addr_type_to_rev_string(hdset_address,
3155 (unsigned char *)data->remote_addr);
3157 DBG("Disconnect Headset %s, %s\n",
3158 hdset_address, data->path);
3159 _bt_ag_set_headset_state(data,
3160 HEADSET_STATE_DISCONNECTED);
3162 g_dbus_method_invocation_return_value(invocation, NULL);
3163 } else if (g_strcmp0(method_name, "IsConnected") == 0) {
3164 gboolean is_connected = FALSE;
3167 for (l = active_devices; l; l = l->next) {
3168 bt_ag_info_t *data = l->data;
3170 if (data->state == HEADSET_STATE_CONNECTED)
3171 is_connected = TRUE;
3173 DBG("is_connected : %s",
3174 is_connected ? "Connected" : "Disconnected");
3176 g_dbus_method_invocation_return_value(invocation,
3177 g_variant_new("(b)", is_connected));
3178 } else if (g_strcmp0(method_name, "IndicateCall") == 0) {
3181 if (0 == g_slist_length(active_devices)) {
3182 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3186 if (ag.ring_timer) {
3187 DBG("IndicateCall received when already indicating");
3188 g_dbus_method_invocation_return_value(invocation, NULL);
3191 for (l = active_devices; l; l = l->next) {
3192 bt_ag_info_t *data = l->data;
3194 if (data->state >= HEADSET_STATE_CONNECTED)
3195 _bt_ag_send_at(data, "\r\nRING\r\n");
3198 __bt_ring_timer_cb(NULL);
3199 ag.ring_timer = g_timeout_add(AG_RING_INTERVAL,
3200 __bt_ring_timer_cb, NULL);
3201 g_dbus_method_invocation_return_value(invocation, NULL);
3202 } else if (g_strcmp0(method_name, "CancelCall") == 0) {
3203 if (0 == g_slist_length(active_devices)) {
3204 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3208 if (ag.ring_timer) {
3209 g_source_remove(ag.ring_timer);
3212 DBG("Got CancelCall method call but no call is active");
3214 g_dbus_method_invocation_return_value(invocation, NULL);
3215 } else if (g_strcmp0(method_name, "Play") == 0) {
3216 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3217 bt_ag_slconn_t *slconn = NULL;
3220 slconn = bt_ag_info->slc;
3222 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3226 #ifndef __TIZEN_OPEN__
3229 if (slconn && FALSE == slconn->is_voice_recognition_running &&
3230 mdm_get_service() == MDM_RESULT_SUCCESS) {
3231 mode = mdm_get_allow_bluetooth_outgoing_call();
3232 mdm_release_service();
3234 if (mode == MDM_RESTRICTED) {
3235 ERR("[MDM] Not allow the outgoing call");
3236 ret = BT_HFP_AGENT_ERROR_OPERATION_NOT_AVAILABLE;
3243 switch (bt_ag_info->state) {
3244 case HEADSET_STATE_CONNECTING:
3245 case HEADSET_STATE_DISCONNECTED:
3246 ERR("HEADSET_STATE ERROR");
3247 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3249 case HEADSET_STATE_CONNECTED:
3251 case HEADSET_STATE_PLAY_IN_PROGRESS:
3252 ERR("Play In Progress");
3253 ret = BT_HFP_AGENT_ERROR_BUSY;
3261 if (sco_open_timer_id > 0) {
3262 INFO("SCO open delay");
3263 sco_open_request = TRUE;
3264 g_dbus_method_invocation_return_value(invocation, NULL);
3268 if ((ag.features & BT_AG_FEATURE_CODEC_NEGOTIATION) &&
3269 (slconn && (slconn->hs_features &
3270 BT_HF_FEATURE_CODEC_NEGOTIATION))) {
3271 switch (bt_ag_info->codec_info.final_codec) {
3272 case BT_CVSD_CODEC_ID:
3273 __bt_ag_set_codec(bt_ag_info, "SetNbParameters");
3274 ret = __bt_ag_sco_connect(bt_ag_info);
3276 case BT_MSBC_CODEC_ID:
3277 __bt_ag_set_codec(bt_ag_info, "SetWbsParameters");
3278 ret = __bt_ag_sco_connect(bt_ag_info);
3281 ret = __bt_hfp_codec_connection_setup(bt_ag_info, FALSE);
3285 ret = __bt_ag_sco_connect(bt_ag_info);
3291 sco_owner = g_strdup(sender);
3293 g_dbus_method_invocation_return_value(invocation, NULL);
3295 name_owner_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
3296 NULL, NULL, "NameOwnerChanged", NULL, NULL, 0,
3297 __bt_ag_name_owner_changed_cb, NULL, NULL);
3298 } else if (g_strcmp0(method_name, "Stop") == 0) {
3301 for (l = active_devices; l; l = l->next) {
3302 bt_ag_info_t *data = l->data;
3304 if (data->state > HEADSET_STATE_CONNECTED) {
3305 __bt_ag_close_sco(data);
3306 _bt_ag_set_headset_state(data,
3307 HEADSET_STATE_CONNECTED);
3311 g_dbus_method_invocation_return_value(invocation, NULL);
3312 } else if (g_strcmp0(method_name, "IsPlaying") == 0) {
3313 gboolean is_playing = FALSE;
3316 for (l = active_devices; l; l = l->next) {
3317 bt_ag_info_t *data = l->data;
3319 if (data->state == HEADSET_STATE_ON_CALL)
3322 DBG("is_playing : %s", is_playing ? "Playing" : "Not Playing");
3324 g_dbus_method_invocation_return_value(invocation,
3325 g_variant_new("(b)", is_playing));
3326 } else if (g_strcmp0(method_name, "GetSpeakerGain") == 0) {
3327 bt_ag_slconn_t *slconn = NULL;
3328 guint16 gain_value = 0;
3329 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3331 if (bt_ag_info == NULL) {
3332 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3336 if (bt_ag_info->state < HEADSET_STATE_CONNECTED) {
3337 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3341 slconn = bt_ag_info->slc;
3343 gain_value = (guint16) slconn->speaker_gain;
3345 g_dbus_method_invocation_return_value(invocation,
3346 g_variant_new("(q)", gain_value));
3347 } else if (g_strcmp0(method_name, "SetSpeakerGain") == 0) {
3349 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3351 g_variant_get(parameters, "(q)", &gain);
3352 DBG("Speaker gain = %d\n", gain);
3354 if (bt_ag_info == NULL) {
3355 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3359 ret = _bt_hfp_set_speaker_gain(bt_ag_info, gain);
3362 g_dbus_method_invocation_return_value(invocation, NULL);
3363 } else if (g_strcmp0(method_name, "GetMicrophoneGain") == 0) {
3364 bt_ag_slconn_t *slconn = NULL;
3365 guint16 gain_value = 0;
3366 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3368 if (bt_ag_info == NULL) {
3369 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3373 if (bt_ag_info->state < HEADSET_STATE_CONNECTED) {
3374 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3378 slconn = bt_ag_info->slc;
3380 gain_value = (guint16) slconn->microphone_gain;
3382 g_dbus_method_invocation_return_value(invocation,
3383 g_variant_new("(q)", gain_value));
3384 } else if (g_strcmp0(method_name, "SetMicrophoneGain") == 0) {
3386 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3388 g_variant_get(parameters, "(q)", &gain);
3389 DBG("Microphone gain = %d\n", gain);
3391 if (bt_ag_info == NULL) {
3392 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3396 ret = _bt_hfp_set_microphone_gain(bt_ag_info, gain);
3399 g_dbus_method_invocation_return_value(invocation, NULL);
3400 } else if (g_strcmp0(method_name, "SetVoiceDial") == 0) {
3401 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3402 if (bt_ag_info == NULL) {
3403 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3407 bt_ag_slconn_t *slconn = bt_ag_info->slc;
3410 g_variant_get(parameters, "(b)", &enable);
3411 DBG("VoiceDail enable = %d\n", enable);
3413 if ((slconn && !(slconn->hs_features &
3414 BT_HF_FEATURE_VOICE_RECOGNITION)))
3415 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3416 else if (bt_ag_info->vr_blacklisted)
3417 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3419 ret = _bt_hfp_set_voice_dial(bt_ag_info, enable);
3422 slconn->is_voice_recognition_running = enable;
3426 g_dbus_method_invocation_return_value(invocation, NULL);
3427 } else if (g_strcmp0(method_name, "SendVendorAtCmd") == 0) {
3429 bt_ag_info_t *bt_ag_info = __bt_get_active_headset(remote_dev_path);
3430 if (bt_ag_info == NULL) {
3431 ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
3435 g_variant_get(parameters, "(&s)", &cmd);
3437 ret = BT_HFP_AGENT_ERROR_INVALID_PARAM;
3441 DBG("vendor cmd = %s", cmd);
3443 ret = _bt_hfp_send_vendor_cmd(bt_ag_info, cmd);
3446 g_dbus_method_invocation_return_value(invocation, NULL);
3447 } else if (g_strcmp0(method_name, "CheckPrivilege") == 0) {
3448 DBG("Already pass dbus SMACK for bt-service::platform");
3449 /* Return success */
3450 g_dbus_method_invocation_return_value(invocation, NULL);
3451 } else if (g_strcmp0(method_name, "SwapHeadset") == 0) {
3454 char address[BT_ADDRESS_STRING_SIZE];
3455 char remote_addr[BT_ADDRESS_STRING_SIZE];
3456 gboolean device_found = FALSE;
3458 g_variant_get(parameters, "(s)", &addr);
3459 g_strlcpy(address, addr, sizeof(address));
3460 DBG("Sender = %s", sender);
3462 /* Loop through connected headset list
3463 * If found, update the remote_dev_path.
3465 for (l = active_devices ; l; l = l->next) {
3466 bt_ag_info_t *data = l->data;
3467 g_strlcpy(remote_addr, data->remote_addr, sizeof(remote_addr));
3468 if (g_strcmp0(remote_addr, address) == 0) {
3469 DBG("Active device found");
3470 if (data->path == NULL) {
3471 DBG("device path is null");
3472 ret = BT_HFP_AGENT_ERROR_INTERNAL;
3475 remote_dev_path = g_strdup(data->path);
3476 DBG("Setting device path %s as active device path", remote_dev_path);
3477 device_found = TRUE;
3482 if (!device_found) {
3483 ret = BT_HFP_AGENT_ERROR_NOT_CONNECTED;
3487 g_dbus_method_invocation_return_value(invocation, NULL);
3494 err = __bt_ag_agent_set_error(ret);
3495 g_dbus_method_invocation_return_gerror(invocation, err);
3500 static const GDBusInterfaceVTable method_table = {
3501 __bt_ag_agent_method,
3506 static GDBusNodeInfo *__bt_ag_create_method_node_info
3507 (const gchar *introspection_data)
3510 GDBusNodeInfo *node_info = NULL;
3512 if (introspection_data == NULL)
3515 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
3518 ERR("Unable to create node: %s", err->message);
3519 g_clear_error(&err);
3524 static GDBusConnection *__bt_ag_get_gdbus_connection(void)
3530 if (ag_dbus_conn == NULL)
3531 ag_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
3533 if (!ag_dbus_conn) {
3535 ERR("Unable to connect to dbus: %s", err->message);
3536 g_clear_error(&err);
3542 return ag_dbus_conn;
3545 static gboolean __bt_ag_register_profile_methods(void)
3548 GError *error = NULL;
3550 GDBusNodeInfo *node_info = NULL;
3553 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
3555 G_BUS_NAME_OWNER_FLAGS_NONE,
3559 DBG("owner_id is [%d]", owner_id);
3561 node_info = __bt_ag_create_method_node_info(
3562 ag_agent_bluez_introspection_xml);
3563 if (node_info == NULL)
3566 path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
3567 DBG("path is [%s]", path);
3569 hf_bluez_id = g_dbus_connection_register_object(ag_dbus_conn, path,
3570 node_info->interfaces[0],
3572 NULL, NULL, &error);
3573 if (hf_bluez_id == 0) {
3574 ERR("Failed to register: %s", error->message);
3575 g_error_free(error);
3577 g_dbus_node_info_unref(node_info);
3582 /* Ag register profile methods for HSP*/
3584 path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
3585 DBG("path is [%s]", path);
3587 hs_bluez_id = g_dbus_connection_register_object(ag_dbus_conn, path,
3588 node_info->interfaces[0],
3590 NULL, NULL, &error);
3591 if (hs_bluez_id == 0) {
3592 ERR("Failed to register: %s", error->message);
3593 g_error_free(error);
3595 g_dbus_node_info_unref(node_info);
3599 g_dbus_node_info_unref(node_info);
3601 node_info = __bt_ag_create_method_node_info
3602 (ag_agent_app_introspection_xml);
3603 if (node_info == NULL)
3606 path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
3607 DBG("path is [%s]", path);
3609 app_id = g_dbus_connection_register_object(ag_dbus_conn, path,
3610 node_info->interfaces[0],
3612 NULL, NULL, &error);
3614 ERR("Failed to register: %s", error->message);
3615 g_error_free(error);
3617 g_dbus_node_info_unref(node_info);
3621 g_dbus_node_info_unref(node_info);
3627 static void __bt_ag_unregister_profile_methods(void)
3631 if (hf_bluez_id > 0) {
3632 g_dbus_connection_unregister_object(ag_dbus_conn,
3637 if (hs_bluez_id > 0) {
3638 g_dbus_connection_unregister_object(ag_dbus_conn,
3644 g_dbus_connection_unregister_object(ag_dbus_conn,
3650 static GDBusProxy *__bt_ag_gdbus_get_service_proxy(const gchar *service,
3651 const gchar *path, const gchar *interface)
3653 return (service_gproxy) ? service_gproxy :
3654 __bt_ag_gdbus_init_service_proxy(service,
3658 static void __bt_ag_agent_register(gchar *path, uint16_t profile_version,
3659 char *profile_uuid, const char* profile_name)
3664 GError *error = NULL;
3665 GVariantBuilder *builder;
3667 proxy = __bt_ag_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME,
3668 "/org/bluez", BLUEZ_PROFILE_MGMT_INTERFACE);
3673 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
3675 g_variant_builder_add(builder, "{sv}",
3676 "Name", g_variant_new("s",
3678 g_variant_builder_add(builder, "{sv}",
3679 "Version", g_variant_new("q", profile_version));
3680 /*g_variant_builder_add(builder, "{sv}",
3681 "Role", g_variant_new("s","client"));*/
3682 if (g_strcmp0(path, BT_AG_AGENT_OBJECT_PATH) == 0) {
3683 g_variant_builder_add(builder, "{sv}",
3684 "features", g_variant_new("q", ag.sdp_features));
3687 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
3688 g_variant_new("(osa{sv})", path,
3689 profile_uuid, builder),
3690 G_DBUS_CALL_FLAGS_NONE, -1,
3692 g_variant_builder_unref(builder);
3693 /* set the name and role for the profile*/
3695 /* dBUS-RPC is failed */
3696 ERR("dBUS-RPC is failed");
3698 if (error != NULL) {
3699 /* dBUS gives error cause */
3700 ERR("D-Bus API failure: errCode[%x], message[%s]",
3701 error->code, error->message);
3703 g_clear_error(&error);
3708 g_variant_unref(ret);
3715 static void __bt_ag_agent_unregister(gchar *path)
3720 GError *error = NULL;
3722 proxy = __bt_ag_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME,
3723 "/org/bluez", BLUEZ_PROFILE_MGMT_INTERFACE);
3728 ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
3729 g_variant_new("(o)", path),
3730 G_DBUS_CALL_FLAGS_NONE, -1,
3733 /* set the name and role for the profile*/
3735 /* dBUS-RPC is failed */
3736 ERR("dBUS-RPC is failed");
3738 if (error != NULL) {
3739 /* dBUS gives error cause */
3740 ERR("D-Bus API failure: errCode[%x], message[%s]",
3741 error->code, error->message);
3743 g_clear_error(&error);
3747 g_variant_unref(ret);
3757 static void __bt_ag_agent_battery_status_cb(keynode_t *node)
3759 int batt = vconf_keynode_get_int(node);
3761 _bt_hfp_set_property_value("BatteryBarsChanged", batt);
3764 static void __bt_ag_agent_network_signal_status_cb(keynode_t *node)
3766 int signal_bar = vconf_keynode_get_int(node);
3768 BT_CHECK_SIGNAL_STRENGTH(signal_bar);
3769 _bt_hfp_set_property_value("SignalBarsChanged", signal_bar);
3772 static void __bt_ag_agent_lunar_connection_status_cb(keynode_t *node)
3774 gboolean status = vconf_keynode_get_bool(node);
3776 DBG("status = %d", status);
3778 if (status == TRUE) {
3779 for (l = active_devices; l; l = l->next) {
3780 bt_ag_info_t *data = l->data;
3781 _bt_ag_send_at(data, "\r\n+BSIR:1\r\n");
3786 static void __bt_ag_agent_network_register_status_cb(keynode_t *node)
3788 int service = vconf_keynode_get_int(node);
3789 bt_hfp_agent_network_registration_status_t network_service;
3793 DBG("Current Signal Level = [%d] \n", service);
3796 case VCONFKEY_TELEPHONY_SVCTYPE_NONE:
3797 case VCONFKEY_TELEPHONY_SVCTYPE_NOSVC:
3798 case VCONFKEY_TELEPHONY_SVCTYPE_SEARCH:
3806 ret = vconf_get_int(VCONFKEY_TELEPHONY_SVC_ROAM, &roam_status);
3808 ERR("Get roaming status failed err = %d\n", ret);
3812 if (roam_status == 0 && service == 1)
3813 network_service = BT_AGENT_NETWORK_REG_STATUS_HOME;
3814 else if (roam_status == 1 && service == 1)
3815 network_service = BT_AGENT_NETWORK_REG_STATUS_ROAMING;
3817 network_service = BT_AGENT_NETWORK_REG_STATUS_UNKOWN;
3819 _bt_hfp_set_property_value("RegistrationChanged", network_service);
3822 static void __bt_ag_agent_subscribe_vconf_updates(void)
3828 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
3829 (void *)__bt_ag_agent_battery_status_cb, NULL);
3831 ERR("Subsrciption to battery status failed err = [%d]\n", ret);
3833 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_RSSI,
3834 (void *)__bt_ag_agent_network_signal_status_cb, NULL);
3836 ERR("Subsrciption to netowrk signal failed err = [%d]\n", ret);
3838 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_SVCTYPE,
3839 (void *)__bt_ag_agent_network_register_status_cb, NULL);
3841 ERR("Subsrciption to network failed err = [%d]\n", ret);
3843 if (TIZEN_PROFILE_WEARABLE) {
3844 ret = vconf_notify_key_changed(VCONF_KEY_BT_LUNAR_ENABLED,
3845 (void *)__bt_ag_agent_lunar_connection_status_cb, NULL);
3847 ERR("Subsrciption to lunar connection failed err = [%d]\n", ret);
3851 static void __bt_ag_agent_release_vconf_updates(void)
3857 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
3858 (vconf_callback_fn)__bt_ag_agent_battery_status_cb);
3860 ERR("vconf_ignore_key_changed failed\n");
3862 ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_RSSI,
3863 (vconf_callback_fn)__bt_ag_agent_network_signal_status_cb);
3865 ERR("vconf_ignore_key_changed failed\n");
3867 ret = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_SVCTYPE,
3868 (vconf_callback_fn)__bt_ag_agent_network_register_status_cb);
3870 ERR("vconf_ignore_key_changed failed\n");
3873 static gboolean __bt_ag_agent_send_subscriber_number_changed(
3876 const char *property = g_strdup("SubscriberNumberChanged");
3880 DBG("Number is %s", number);
3882 if (!_bt_hfp_set_property_name(property, number)) {
3883 DBG("Error- set property for subscriber no change - ERROR\n");
3884 g_free((void *)property);
3887 g_free((void *)property);
3891 static void __bt_ag_agent_sigterm_handler(int signo)
3896 ERR_C("***** Signal handler came with signal %d *****", signo);
3898 for (l = active_devices ; l; l = l->next) {
3899 bt_ag_info_t *data = l->data;
3900 if (!__bt_ag_agent_connection_release(data))
3901 ERR("__bt_ag_agent_connection_release failed");
3904 g_dbus_connection_flush(ag_dbus_conn, NULL, NULL, NULL);
3907 g_main_loop_quit(gmain_loop);
3911 INFO("Terminating AG agent");
3915 if (signo == SIGTERM)
3918 for (i = 0; i < BT_AG_SIG_NUM; i++)
3919 sigaction(bt_ag_sig_to_handle[i], &(bt_ag_sigoldact[i]), NULL);
3924 static void __bt_ag_agent_tel_cb(TapiHandle *handle,
3929 TelSimMsisdnList_t *number;
3930 gchar *subscriber_number;
3932 ERR("*********** result = %d", result);
3934 if (result == TAPI_API_SIM_LOCKED ||
3935 result == TAPI_API_SIM_NOT_INITIALIZED ||
3936 result == TAPI_API_SERVICE_NOT_READY) {
3937 DBG("initializing the tapi event for SIM status");
3938 __bt_ag_agent_reg_sim_event(handle, user_data);
3945 number = (TelSimMsisdnList_t *)data;
3946 subscriber_number = g_strdup(number->list[0].num);
3947 __bt_ag_agent_send_subscriber_number_changed(subscriber_number);
3948 g_free(subscriber_number);
3951 static void __bt_ag_agent_on_noti_sim_status(TapiHandle *handle,
3952 const char *noti_id, void *data, void *user_data)
3954 TelSimCardStatus_t *status = data;
3957 DBG("event TAPI_NOTI_SIM_STATUS received!! status[%d]", *status);
3959 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
3960 __bt_ag_agent_dereg_sim_event(handle);
3961 tapi_result = tel_get_sim_msisdn(handle, __bt_ag_agent_tel_cb,
3963 if (tapi_result != TAPI_API_SUCCESS)
3964 ERR("Fail to get sim info: %d", tapi_result);
3968 static void __bt_ag_agent_reg_sim_event(TapiHandle *handle, void *user_data)
3971 ret = tel_register_noti_event(handle, TAPI_NOTI_SIM_STATUS,
3972 __bt_ag_agent_on_noti_sim_status, user_data);
3974 if (ret != TAPI_API_SUCCESS)
3975 ERR("event register failed(%d)", ret);
3978 static void __bt_ag_agent_dereg_sim_event(TapiHandle *handle)
3981 ret = tel_deregister_noti_event(handle, TAPI_NOTI_SIM_STATUS);
3983 if (ret != TAPI_API_SUCCESS)
3984 ERR("event deregister failed(%d)", ret);
3987 static void __bt_ag_name_owner_changed_cb(GDBusConnection *connection,
3988 const gchar *sender_name,
3989 const gchar *object_path,
3990 const gchar *interface_name,
3991 const gchar *signal_name,
3992 GVariant *parameters,
3996 char *name_owner = NULL;
3997 char *old_owner = NULL;
3998 char *new_owner = NULL;
4000 if (strcasecmp(signal_name, "NameOwnerChanged") == 0) {
4003 g_variant_get(parameters, "(sss)", &name_owner, &old_owner, &new_owner);
4005 _bt_hfp_release_all_calls_by_sender(name_owner);
4007 if (sco_owner == NULL)
4010 if (strcasecmp(name_owner, sco_owner) == 0) {
4011 if (name_owner_sig_id != -1)
4012 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4014 name_owner_sig_id = -1;
4018 for (l = active_devices ; l; l = l->next) {
4019 bt_ag_info_t *data = l->data;
4022 __bt_ag_close_sco(data);
4023 _bt_ag_set_headset_state(data,
4024 HEADSET_STATE_CONNECTED);
4031 static void __bt_ag_agent_filter_cb(GDBusConnection *connection,
4032 const gchar *sender_name,
4033 const gchar *object_path,
4034 const gchar *interface_name,
4035 const gchar *signal_name,
4036 GVariant *parameters,
4041 GVariant *optional_param = NULL;
4043 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
4044 g_variant_get(parameters, "(&o@a{sa{sv}})", &path, &optional_param);
4047 g_variant_unref(optional_param);
4048 ERR("Invalid adapter path");
4052 INFO("Adapter Path = [%s]", path);
4053 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
4054 gchar *obj_path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
4055 __bt_ag_agent_register(obj_path, hfp_ver,
4056 HFP_AG_UUID, "Hands-Free Audio Gateway");
4058 obj_path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
4059 __bt_ag_agent_register(obj_path, hsp_ver,
4060 HSP_AG_UUID, "Headset Audio Gateway");
4062 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
4063 g_variant_get(parameters, "(&o@as)", &path, &optional_param);
4066 g_variant_unref(optional_param);
4067 ERR("Invalid adapter path");
4071 INFO("Adapter Path = [%s]", path);
4072 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
4073 gchar *obj_path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
4074 __bt_ag_agent_unregister(obj_path);
4076 obj_path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
4077 __bt_ag_agent_unregister(obj_path);
4082 g_variant_unref(optional_param);
4087 static void __bt_ag_agent_dbus_deinit(void)
4090 if (service_gproxy) {
4091 g_object_unref(service_gproxy);
4092 service_gproxy = NULL;
4096 g_object_unref(app_gproxy);
4101 __bt_ag_unregister_profile_methods();
4103 if (interface_added_sig_id)
4104 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4105 interface_added_sig_id);
4107 if (interface_removed_sig_id)
4108 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4109 interface_removed_sig_id);
4111 if (name_owner_sig_id)
4112 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4114 #ifdef TIZEN_FEATURE_BT_MEDIA_ENHANCE
4116 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4120 if (media_state_sig_id)
4121 g_dbus_connection_signal_unsubscribe(ag_dbus_conn,
4122 media_state_sig_id);
4123 media_state_sig_id = 0;
4126 interface_added_sig_id = 0;
4127 interface_removed_sig_id = 0;
4128 name_owner_sig_id = 0;
4132 g_object_unref(ag_dbus_conn);
4133 ag_dbus_conn = NULL;
4138 static int __bt_ag_agent_get_adapter_path(GDBusConnection *conn, char *path)
4141 GDBusProxy *manager_proxy = NULL;
4142 GVariant *result = NULL;
4143 char *adapter_path = NULL;
4146 return BT_HFP_AGENT_ERROR_INTERNAL;
4148 manager_proxy = g_dbus_proxy_new_sync(conn,
4149 G_DBUS_PROXY_FLAGS_NONE, NULL,
4152 BT_MANAGER_INTERFACE,
4155 if (!manager_proxy) {
4156 ERR("Unable to create proxy: %s", err->message);
4160 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
4161 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
4164 ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
4166 ERR("Fail to get DefaultAdapter");
4171 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
4172 ERR("Incorrect result\n");
4176 g_variant_get(result, "(&o)", &adapter_path);
4178 if (adapter_path == NULL ||
4179 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
4180 ERR("Adapter path is inproper\n");
4185 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
4187 g_variant_unref(result);
4188 g_object_unref(manager_proxy);
4193 g_clear_error(&err);
4196 g_variant_unref(result);
4199 g_object_unref(manager_proxy);
4201 return BT_HFP_AGENT_ERROR_INTERNAL;
4205 #ifdef TIZEN_FEATURE_BT_MEDIA_ENHANCE
4206 void _bt_ag_agent_check_transport_state(void)
4210 if (transport_state == MEDIA_TRANSPORT_STATE_PLAYING) {
4214 proxy = g_dbus_proxy_new_sync(ag_dbus_conn,
4215 G_DBUS_PROXY_FLAGS_NONE, NULL,
4216 "org.PulseAudio2", A2DP_SOURCE_ENDPOINT,
4217 BLUEZ_MEDIA_ENDPOINT_INTERFACE, NULL, &err);
4221 ERR("Unable to create proxy: %s", err->message);
4222 g_clear_error(&err);
4226 INFO_C("SuspendMedia initiated");
4228 g_dbus_proxy_call(proxy,
4229 "SuspendMedia", NULL,
4230 G_DBUS_CALL_FLAGS_NONE, 2000,
4232 transport_state = MEDIA_TRANSPORT_STATE_IDLE;
4238 static void __bt_ag_agent_transport_state_update(const char *value)
4241 if (!g_strcmp0(value, "idle"))
4242 transport_state = MEDIA_TRANSPORT_STATE_IDLE;
4243 else if (!g_strcmp0(value, "pending") || !g_strcmp0(value, "active"))
4244 transport_state = MEDIA_TRANSPORT_STATE_PLAYING;
4246 transport_state = MEDIA_TRANSPORT_STATE_DISCONNECTED;
4248 INFO_C("transport_state %d", transport_state);
4251 static void __bt_ag_agent_media_filter_cb(GDBusConnection *connection,
4252 const gchar *sender_name,
4253 const gchar *object_path,
4254 const gchar *interface_name,
4255 const gchar *signal_name,
4256 GVariant *parameters,
4261 GVariant *dict_param = NULL;
4262 GVariant *optional_param = NULL;
4264 if (strcasecmp(signal_name, "PropertiesChanged") == 0) {
4265 if (g_strcmp0(g_variant_get_type_string(parameters),
4267 ERR("Incorrect parameters\n");
4271 g_variant_get(parameters, "(&s@a{sv}@as)",
4272 &inter, &dict_param, &optional_param);
4273 if (dict_param && (!g_strcmp0(inter,
4274 BLUEZ_MEDIA_TRANSPORT_INTERFACE))){
4275 GVariantIter *iter = NULL;
4276 const gchar *key = NULL;
4277 GVariant *value_var = NULL;
4278 gchar *value = NULL;
4279 g_variant_get(dict_param, "a{sv}", &iter);
4280 while (g_variant_iter_loop(
4281 iter, "{sv}", &key, &value_var)) {
4283 if (g_strcmp0(key, "State") == 0) {
4284 value = (gchar *)g_variant_get_string(
4287 DBG("value %s", value);
4288 __bt_ag_agent_transport_state_update(value);
4292 g_variant_iter_free(iter);
4294 } else if (strcasecmp(signal_name, "ProfileStateChanged") == 0) {
4295 char *profile_uuid = NULL;
4298 g_variant_get(parameters, "(&si)", &profile_uuid, &state);
4299 if ((g_strcmp0(profile_uuid, A2DP_SINK_UUID) == 0) &&
4300 (state == BT_PROFILE_STATE_DISCONNECTED)) {
4301 DBG("Updating the transport state");
4302 __bt_ag_agent_transport_state_update("Disconnect");
4307 g_variant_unref(dict_param);
4310 g_variant_unref(optional_param);
4315 static void __bt_ag_agent_dbus_init(void)
4319 if (__bt_ag_get_gdbus_connection() == NULL) {
4320 ERR("Error in creating the gdbus connection\n");
4323 if (!__bt_ag_register_profile_methods()) {
4324 ERR("Error in HFP / HSP register_profile_methods\n");
4328 if (!__bt_ag_agent_get_adapter_path(ag_dbus_conn , NULL)) {
4330 gchar *path = g_strdup(BT_AG_AGENT_OBJECT_PATH);
4331 __bt_ag_agent_register(path, hfp_ver,
4332 HFP_AG_UUID, "Hands-Free Audio Gateway");
4334 path = g_strdup(BT_HS_AG_AGENT_OBJECT_PATH);
4335 __bt_ag_agent_register(path, hsp_ver,
4336 HSP_AG_UUID, "Headset Audio Gateway");
4339 interface_added_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
4340 NULL, BT_MANAGER_INTERFACE,
4341 BT_INTERFACES_ADDED, NULL, NULL, 0,
4342 __bt_ag_agent_filter_cb, NULL, NULL);
4344 interface_removed_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
4345 NULL, BT_MANAGER_INTERFACE,
4346 BT_INTERFACES_REMOVED, NULL, NULL, 0,
4347 __bt_ag_agent_filter_cb, NULL, NULL);
4349 #ifdef TIZEN_FEATURE_BT_MEDIA_ENHANCE
4350 media_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
4351 NULL, BT_PROPERTIES_INTERFACE, NULL, NULL,
4352 NULL, 0, __bt_ag_agent_media_filter_cb,
4355 media_state_sig_id = g_dbus_connection_signal_subscribe(ag_dbus_conn,
4356 NULL, BLUEZ_DEVICE_INTERFACE, NULL, NULL,
4357 NULL, 0, __bt_ag_agent_media_filter_cb,
4360 transport_state = MEDIA_TRANSPORT_STATE_DISCONNECTED;
4366 static uint32_t __bt_ag_agent_get_ag_features(void)
4369 uint32_t ag_features;
4371 if (TIZEN_MODEL_NAME_TM1) {
4372 ag_features = BT_AG_FEATURE_EC_AND_NR |
4373 BT_AG_FEATURE_REJECT_CALL |
4374 BT_AG_FEATURE_ENHANCED_CALL_STATUS |
4375 BT_AG_FEATURE_THREE_WAY_CALL |
4376 BT_AG_FEATURE_EXTENDED_ERROR_RESULT_CODES;
4378 ag_features = BT_AG_FEATURE_EC_AND_NR |
4379 BT_AG_FEATURE_REJECT_CALL |
4380 BT_AG_FEATURE_ENHANCED_CALL_STATUS |
4381 BT_AG_FEATURE_THREE_WAY_CALL |
4382 BT_AG_FEATURE_VOICE_RECOGNITION |
4383 BT_AG_FEATURE_EXTENDED_ERROR_RESULT_CODES;
4387 #if defined(TIZEN_FEATURE_BT_HFP_AG)
4388 hfp_ver = HFP_VERSION_1_7;
4390 hfp_ver = HFP_VERSION_1_5;
4392 hsp_ver = HSP_VERSION_1_2;
4394 if (hfp_ver > HFP_VERSION_1_5)
4395 ag_features |= BT_AG_FEATURE_CODEC_NEGOTIATION;
4396 if (hfp_ver == HFP_VERSION_1_7)
4397 ag_features |= BT_AG_FEATURE_ESCO_S4_T2_SUPPORT;
4402 void *__bt_ag_agent_telephony_init(void *arg)
4406 uint32_t ag_features = *((uint32_t *)arg);
4408 INFO_C("Initializing the telephony info");
4410 _bt_hfp_initialize_telephony_manager(ag_features);
4411 __bt_ag_agent_subscribe_vconf_updates();
4413 tapi_handle = tel_init(NULL);
4414 tapi_result = tel_get_sim_msisdn(tapi_handle, __bt_ag_agent_tel_cb,
4416 if (tapi_result != TAPI_API_SUCCESS)
4417 ERR("Fail to get sim info: %d", tapi_result);
4424 uint32_t ag_features;
4425 struct sigaction sa;
4426 pthread_t thread_id = 0;
4428 INFO_C("### Starting Bluetooth AG agent");
4430 ag_features = __bt_ag_agent_get_ag_features();
4432 ag.sdp_features = (uint16_t) ag_features & 0x1F;
4434 if (hfp_ver >= HFP_VERSION_1_6 && wbs_en == TRUE)
4435 ag.sdp_features |= BT_AG_FEATURE_SDP_WIDEBAND_SPEECH;
4437 memset(&sa, 0, sizeof(sa));
4438 sa.sa_flags = SA_NOCLDSTOP;
4439 sa.sa_handler = __bt_ag_agent_sigterm_handler;
4441 for (i = 0; i < BT_AG_SIG_NUM; i++)
4442 sigaction(bt_ag_sig_to_handle[i], &sa, &(bt_ag_sigoldact[i]));
4444 gmain_loop = g_main_loop_new(NULL, FALSE);
4446 if (gmain_loop == NULL) {
4447 ERR("GMainLoop create failed");
4448 return EXIT_FAILURE;
4451 __bt_ag_agent_dbus_init();
4452 if (pthread_create(&thread_id, NULL,
4453 (void *)&__bt_ag_agent_telephony_init,
4454 &ag_features) < 0) {
4455 ERR("pthread_create() is failed");
4456 return EXIT_FAILURE;
4459 if (pthread_detach(thread_id) < 0)
4460 ERR("pthread_detach() is failed");
4462 g_main_loop_run(gmain_loop);
4466 tel_deinit(tapi_handle);
4468 __bt_ag_agent_dbus_deinit();
4469 _bt_hfp_deinitialize_telephony_manager();
4470 __bt_ag_agent_release_vconf_updates();
4472 if (remote_dev_path)
4473 g_free(remote_dev_path);
4476 g_main_loop_unref(gmain_loop);
4478 INFO_C("### Terminating Bluetooth AG agent");