Merge "code clean up" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-telephony.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <vconf.h>
23 #include <vconf-keys.h>
24
25 #include "bt-common.h"
26 #include "bluetooth-telephony-api.h"
27 #include "bt-internal-types.h"
28
29 #define BT_SCO_TIMEOUT 3000
30
31 #define BT_CVSD_CODEC_ID 1
32 #define BT_MSBC_CODEC_ID 2
33
34 typedef struct {
35         GDBusConnection *conn;
36         GDBusProxy *proxy;
37         GDBusProxy *dbus_proxy;
38         GDBusProxy *manager_proxy;
39 } telephony_dbus_info_t;
40
41 typedef struct {
42         bt_telephony_func_ptr cb;
43         unsigned int call_count;
44         char *obj_path;
45         char address[BT_ADDRESS_STR_LEN];
46         char call_path[BT_AUDIO_CALL_PATH_LEN];
47         bluetooth_headset_state_t headset_state;
48         void *user_data;
49 } bt_telephony_info_t;
50
51
52 static char *src_addr = NULL;
53 static guint owner_id = 0;
54
55 #define BLUETOOTH_TELEPHONY_ERROR (__bluetooth_telephony_error_quark())
56
57 #define BLUEZ_SERVICE_NAME "org.bluez"
58 #define BLUEZ_HEADSET_INTERFACE "org.bluez.Headset"
59
60 #define BLUEZ_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager"
61 #define BLUEZ_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
62 #define BLUEZ_ADAPTER_INTERFACE "org.bluez.Adapter1"
63 #define BLUEZ_DEVICE_INTERFACE "org.bluez.Device1"
64 #define HFP_AGENT_SERVICE "org.bluez.ag_agent"
65
66
67 #define HFP_AGENT_PATH "/org/bluez/hfp_agent"
68 #define HFP_AGENT_INTERFACE "Org.Hfp.App.Interface"
69
70 #define CSD_CALL_APP_PATH "/org/tizen/csd/%d"
71 #define HFP_NREC_STATUS_CHANGE "NrecStatusChanged"
72 #define HFP_ANSWER_CALL "Answer"
73 #define HFP_REJECT_CALL "Reject"
74 #define HFP_RELEASE_CALL "Release"
75 #define HFP_THREEWAY_CALL "Threeway"
76
77 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
78
79 /*Below Inrospection data is exposed to bluez from agent*/
80 static const gchar bt_telephony_introspection_xml[] =
81 "<node name='/'>"
82 " <interface name='org.tizen.csd.Call.Instance'>"
83 "     <method name='SendDtmf'>"
84 "          <arg type='s' name='dtmf' direction='in'/>"
85 "     </method>"
86 "     <method name='VendorCmd'>"
87 "          <arg type='s' name='vendor' direction='in'/>"
88 "     </method>"
89 " </interface>"
90 "</node>";
91
92 #define BT_TELEPHONY_CHECK_ENABLED() \
93         do { \
94                 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) { \
95                         BT_ERR("BT is not enabled"); \
96                         return BLUETOOTH_TELEPHONY_ERROR_NOT_ENABLED; \
97                 } \
98         } while (0)
99
100 static gboolean is_initialized = FALSE;
101 #define BT_TELEPHONY_CHECK_INITIALIZED() \
102         do { \
103                 if (is_initialized == FALSE) { \
104                         BT_ERR("Bluetooth telephony not initilized"); \
105                         return BLUETOOTH_TELEPHONY_ERROR_NOT_INITIALIZED; \
106                 } \
107         } while (0)
108
109 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
110                                         const gchar *sender_name,
111                                         const gchar *object_path,
112                                         const gchar *interface_name,
113                                         const gchar *signal_name,
114                                         GVariant *parameters,
115                                         gpointer user_data);
116
117 static int __bt_telephony_get_src_addr(GVariant *value);
118
119 static bt_telephony_info_t telephony_info;
120 static telephony_dbus_info_t telephony_dbus_info;
121 static gboolean is_active = FALSE;
122
123 /*Function Declaration*/
124 static int __bt_telephony_get_error(const char *error_message);
125 static void __bt_telephony_event_cb(int event, int result, void *param_data);
126 static GQuark __bluetooth_telephony_error_quark(void);
127 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
128                 const char *interface, const char *method,
129                 GError **err, GVariant *parameters);
130 static int __bluetooth_telephony_send_call_status(
131                         bt_telephony_call_status_t call_status,
132                         unsigned int call_id, const char *ph_number);
133 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
134                 bluetooth_telephony_error_t error, const char *err_msg);
135
136 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
137                                                  const gchar *sender_name,
138                                                  const gchar *object_path,
139                                                  const gchar *interface_name,
140                                                  const gchar *signal_name,
141                                                  GVariant *parameters,
142                                                  gpointer user_data);
143
144 static int __bluetooth_telephony_register(void);
145 static int __bluetooth_telephony_unregister(void);
146
147 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class);
148 static int __bluetooth_telephony_get_connected_device(void);
149 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void);
150
151 /*Function Definition*/
152 static void __bt_telephony_method(GDBusConnection *connection,
153                         const gchar *sender,
154                         const gchar *object_path,
155                         const gchar *interface_name,
156                         const gchar *method_name,
157                         GVariant *parameters,
158                         GDBusMethodInvocation *invocation,
159                         gpointer user_data)
160 {
161         FN_START;
162
163         BT_INFO("method %s", method_name);
164         BT_INFO("object_path %s", object_path);
165
166         if (g_strcmp0(method_name, "SendDtmf") == 0) {
167                 gchar *dtmf;
168                 telephony_event_dtmf_t call_data = { 0, };
169
170                 g_variant_get(parameters, "(&s)", &dtmf);
171
172                 if (dtmf == NULL) {
173                         BT_ERR("Number dial failed");
174                         __bluetooth_telephony_error(invocation,
175                                         BLUETOOTH_TELEPHONY_ERROR_INVALID_DTMF,
176                                         "Invalid dtmf");
177                 } else {
178                         DBG_SECURE("Dtmf = %s", dtmf);
179
180                         call_data.dtmf = g_strdup(dtmf);
181                         __bt_telephony_event_cb(
182                                 BLUETOOTH_EVENT_TELEPHONY_SEND_DTMF,
183                                 BLUETOOTH_TELEPHONY_ERROR_NONE,
184                                 (void *)&call_data);
185
186                         g_free(call_data.dtmf);
187
188                         g_dbus_method_invocation_return_value(invocation, NULL);
189                 }
190         } else if (g_strcmp0(method_name, "VendorCmd") == 0) {
191                 gchar *at_cmd;
192
193                 g_variant_get(parameters, "(&s)", &at_cmd);
194                 BT_INFO("Vendor %s", at_cmd);
195                 if (at_cmd == NULL) {
196                         BT_ERR("Vendor command is NULL\n");
197                         __bluetooth_telephony_error(invocation,
198                                         BLUETOOTH_TELEPHONY_ERROR_APPLICATION,
199                                         "Invalid at vendor cmd");
200                 } else {
201                         DBG_SECURE("Vendor AT cmd = %s", at_cmd);
202
203                         __bt_telephony_event_cb(
204                                 BLUETOOTH_EVENT_TELEPHONY_VENDOR_AT_CMD,
205                                 BLUETOOTH_TELEPHONY_ERROR_NONE,
206                                 at_cmd);
207
208                         g_dbus_method_invocation_return_value(invocation, NULL);
209                 }
210         }
211
212         BT_INFO("-");
213 }
214
215 static const GDBusInterfaceVTable method_table = {
216         __bt_telephony_method,
217         NULL,
218         NULL,
219 };
220
221 static int __bt_telephony_get_error(const char *error_message)
222 {
223         if (error_message == NULL) {
224                 BT_ERR("Error message NULL");
225                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
226         }
227
228         BT_ERR("Error message = %s", error_message);
229         if (g_strcmp0(error_message, "NotAvailable") == 0)
230                 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
231         else if (g_strcmp0(error_message, "NotConnected") == 0)
232                 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
233         else if (g_strcmp0(error_message, "InProgress") == 0)
234                 return BLUETOOTH_TELEPHONY_ERROR_BUSY;
235         else if (g_strcmp0(error_message, "InvalidArguments") == 0)
236                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
237         else if (g_strcmp0(error_message, "AlreadyExists") == 0)
238                 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_EXSIST;
239         else if (g_strcmp0(error_message, "Already Connected") == 0)
240                 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
241         else if (g_strcmp0(error_message, "No memory") == 0)
242                 return BLUETOOTH_TELEPHONY_ERROR_NO_MEMORY;
243         else if (g_strcmp0(error_message, "I/O error") == 0)
244                 return BLUETOOTH_TELEPHONY_ERROR_I_O_ERROR;
245         else if (g_strcmp0(error_message,
246                         "Operation currently not available") == 0)
247                 return BLUETOOTH_TELEPHONY_ERROR_OPERATION_NOT_AVAILABLE;
248         else if (g_strrstr(error_message, BT_ACCESS_DENIED_MSG))
249                 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
250         else
251                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
252 }
253
254 static int __bt_telephony_check_privilege(void)
255 {
256         GVariant *reply;
257         GError *err = NULL;
258         int ret;
259
260         FN_START;
261         reply = __bluetooth_telephony_dbus_method_send(
262                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
263                         "CheckPrivilege", &err, NULL);
264
265         if (!reply) {
266                 BT_ERR("Error returned in method call");
267                 if (err) {
268                         g_dbus_error_strip_remote_error(err);
269                         ret = __bt_telephony_get_error(err->message);
270                         g_error_free(err);
271                         return ret;
272                 }
273                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
274         }
275         g_variant_unref(reply);
276
277         FN_END;
278         return BLUETOOTH_TELEPHONY_ERROR_NONE;
279 }
280
281 static void __bt_telephony_event_cb(int event, int result, void *param_data)
282 {
283         telephony_event_param_t bt_event = { 0, };
284
285         bt_event.event = event;
286         bt_event.result = result;
287         bt_event.param_data = param_data;
288
289         ret_if(telephony_info.cb == NULL);
290         telephony_info.cb(bt_event.event, &bt_event, telephony_info.user_data);
291         return;
292 }
293
294 static GQuark __bluetooth_telephony_error_quark(void)
295 {
296         static GQuark quark = 0;
297
298         quark = g_quark_from_static_string("telephony");
299
300         return quark;
301 }
302
303 static GVariant *__bluetooth_telephony_dbus_method_send(const char *path,
304                 const char *interface, const char *method,
305                 GError **err, GVariant *parameters)
306 {
307         int timeout = -1;
308
309         if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
310                 timeout = 4000;
311
312         GVariant *reply;
313         GDBusProxy *proxy;
314         GDBusConnection *conn;
315
316         FN_START;
317
318         conn = telephony_dbus_info.conn;
319         retv_if(conn == NULL, NULL);
320
321         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
322                         NULL, HFP_AGENT_SERVICE, path, interface, NULL, err);
323         if (proxy == NULL) {
324                 BT_ERR("Unable to allocate new proxy");
325                 return NULL;
326         }
327
328         reply = g_dbus_proxy_call_sync(proxy, method, parameters,
329                                 G_DBUS_CALL_FLAGS_NONE, timeout, NULL, err);
330
331         g_object_unref(proxy);
332         FN_END;
333         return reply;
334 }
335
336 static int __bluetooth_telephony_send_call_status(
337                         bt_telephony_call_status_t call_status,
338                         unsigned int call_id, const char *ph_number)
339 {
340         GVariant *reply;
341         GVariant *param;
342         GError *err = NULL;
343         char *path = g_strdup(telephony_info.call_path);
344         char *phone_number;
345         int ret;
346
347         FN_START;
348
349         if (NULL == ph_number)
350                 phone_number = g_strdup("");
351         else
352                 phone_number = g_strdup(ph_number);
353
354         param = g_variant_new("(ssii)", path, phone_number,
355                         call_status, call_id);
356         reply = __bluetooth_telephony_dbus_method_send(
357                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
358                         "ChangeCallStatus", &err, param);
359
360         g_free(path);
361         g_free(phone_number);
362
363         if (!reply) {
364                 BT_ERR("Error returned in method call");
365                 if (err) {
366                         g_dbus_error_strip_remote_error(err);
367                         ret = __bt_telephony_get_error(err->message);
368                         g_error_free(err);
369                         return ret;
370                 }
371                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
372         }
373
374         g_variant_unref(reply);
375
376         return BLUETOOTH_TELEPHONY_ERROR_NONE;
377 }
378
379 static void __bluetooth_telephony_error(GDBusMethodInvocation *invocation,
380                 bluetooth_telephony_error_t error, const char *err_msg)
381 {
382         g_dbus_method_invocation_return_error(invocation,
383                         BLUETOOTH_TELEPHONY_ERROR, error,
384                         err_msg, NULL);
385 }
386
387 static void __bluetooth_telephony_answer_call(GVariant *var)
388 {
389         telephony_event_callid_t call_data = { 0, };
390         unsigned int callid;
391
392         FN_START;
393
394         g_variant_get(var, "(u)", &callid);
395         BT_DBG("call_id = [%d]", callid);
396         call_data.callid = callid;
397
398         __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_ANSWER_CALL,
399                                         BLUETOOTH_TELEPHONY_ERROR_NONE,
400                                         (void *)&call_data);
401         FN_END;
402 }
403
404 static void __bluetooth_telephony_release_call(GVariant *var)
405 {
406         telephony_event_callid_t call_data = { 0, };
407         unsigned int callid;
408
409         FN_START;
410
411         g_variant_get(var, "(u)", &callid);
412         BT_DBG("call_id = [%d]", callid);
413         call_data.callid = callid;
414
415         __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_RELEASE_CALL,
416                                         BLUETOOTH_TELEPHONY_ERROR_NONE,
417                                         (void *)&call_data);
418         FN_END;
419 }
420
421 static void __bluetooth_telephony_reject_call(GVariant *var)
422 {
423         telephony_event_callid_t call_data = { 0, };
424         unsigned int callid;
425
426         FN_START;
427
428         g_variant_get(var, "(u)", &callid);
429         BT_DBG("call_id = [%d]", callid);
430         call_data.callid = callid;
431
432         __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_REJECT_CALL,
433                                         BLUETOOTH_TELEPHONY_ERROR_NONE,
434                                         (void  *)&call_data);
435         FN_END;
436 }
437
438 static void __bluetooth_telephony_threeway_call(GVariant *var)
439 {
440         int event = 0;
441         unsigned int chld_value;
442
443         FN_START;
444
445         g_variant_get(var, "(u)", &chld_value);
446         BT_DBG("chld value  = [%d]", chld_value);
447
448         switch (chld_value) {
449         case 0:
450                 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_0_RELEASE_ALL_HELD_CALL;
451                 break;
452         case 1:
453                 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_1_RELEASE_ALL_ACTIVE_CALL;
454                 break;
455         case 2:
456                 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_2_ACTIVE_HELD_CALL;
457                 break;
458         case 3:
459                 event = BLUETOOTH_EVENT_TELEPHONY_CHLD_3_MERGE_CALL;
460                 break;
461         default:
462                 BT_ERR("Invalid CHLD command");
463                 return;
464         }
465
466         __bt_telephony_event_cb(event,
467                         BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
468         FN_END;
469 }
470
471 static void __bluetooth_handle_nrec_status_change(GVariant *var)
472 {
473         gboolean status = FALSE;
474
475         g_variant_get(var, "(b)", &status);
476         BT_INFO("NREC status = %d", status);
477
478         __bt_telephony_event_cb(BLUETOOTH_EVENT_TELEPHONY_NREC_CHANGED,
479                 BLUETOOTH_TELEPHONY_ERROR_NONE, (void *)&status);
480
481 }
482
483 static void __bluetooth_telephony_event_filter(GDBusConnection *connection,
484                 const gchar *sender_name,
485                 const gchar *object_path,
486                 const gchar *interface_name,
487                 const gchar *signal_name,
488                 GVariant *parameters,
489                 gpointer user_data)
490 {
491         BT_DBG("+");
492
493         if (strcasecmp(interface_name, BLUEZ_HEADSET_INTERFACE) == 0) {
494                 if (strcasecmp(signal_name, "PropertyChanged") == 0) {
495                         GVariant *values;
496                         gchar *property;
497
498                         g_variant_get(parameters, "(&sv)", &property, &values);
499                         BT_DBG("Property: %s", property);
500
501                         if (strcasecmp(property, "State") == 0) {
502                                 gchar *state;
503                                 state = (gchar *)g_variant_get_string(values, NULL);
504
505                                 if (NULL == state) {
506                                         BT_ERR("State is null");
507                                         return;
508                                 }
509                                 BT_DBG("state: %s", state);
510                                 if (g_strcmp0(state, "connected") == 0) {
511                                         telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
512                                 } else if (g_strcmp0(state, "playing") == 0) {
513                                         telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
514                                 } else if (g_strcmp0(state, "disconnected") == 0) {
515                                         /* Headset state: playing -> disconnected */
516                                         if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING) {
517                                                 __bt_telephony_event_cb(
518                                                                 BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
519                                                                 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
520                                         }
521
522                                         telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
523                                 }
524                         } else if (strcasecmp(property, "Connected") == 0) {
525                                 gboolean connected = FALSE;
526                                 char *dev_addr = NULL;
527                                 connected = g_variant_get_boolean(values);
528                                 BT_INFO("connected %d", connected);
529                                 if (connected) {
530                                         /*Get device address*/
531                                         if (object_path != NULL)
532                                                 dev_addr = strstr(object_path, "dev_");
533
534                                         if (dev_addr != NULL) {
535                                                 dev_addr += 4;
536                                                 g_strlcpy(telephony_info.address,
537                                                                 dev_addr,
538                                                                 sizeof(telephony_info.address));
539                                                 g_strdelimit(telephony_info.address, "_", ':');
540                                                 BT_DBG("address is %s",
541                                                                 telephony_info.address);
542
543                                                 telephony_info.headset_state =
544                                                                 BLUETOOTH_STATE_CONNECTED;
545
546                                                 if (telephony_dbus_info.proxy != NULL) {
547                                                         g_object_unref(telephony_dbus_info.proxy);
548                                                         telephony_dbus_info.proxy = NULL;
549                                                 }
550
551                                                 telephony_dbus_info.proxy =
552                                                                 __bluetooth_telephony_get_connected_device_proxy();
553
554                                                 BT_INFO("Headset Connected");
555
556                                                 __bt_telephony_event_cb(
557                                                                 BLUETOOTH_EVENT_TELEPHONY_HFP_CONNECTED,
558                                                                 BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
559                                         }
560                                 } else { /*Device disconnected*/
561                                         memset(telephony_info.address, 0x00,
562                                                         sizeof(telephony_info.address));
563                                         telephony_info.headset_state =
564                                                         BLUETOOTH_STATE_DISCONNETED;
565
566                                         if (telephony_dbus_info.proxy != NULL) {
567                                                 g_object_unref(telephony_dbus_info.proxy);
568                                                 telephony_dbus_info.proxy = NULL;
569                                         }
570
571                                         BT_INFO("Headset Disconnected");
572
573                                         __bt_telephony_event_cb(
574                                                         BLUETOOTH_EVENT_TELEPHONY_HFP_DISCONNECTED,
575                                                         BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
576                                 }
577                         } else if (strcasecmp(property, "SpeakerGain") == 0) {
578                                 unsigned int spkr_gain;
579                                 guint16 gain = g_variant_get_uint16(values);
580
581                                 spkr_gain = (unsigned int)gain;
582                                 BT_DBG("spk_gain[%d]", spkr_gain);
583
584                                 __bt_telephony_event_cb(
585                                                 BLUETOOTH_EVENT_TELEPHONY_SET_SPEAKER_GAIN,
586                                                 BLUETOOTH_TELEPHONY_ERROR_NONE,
587                                                 (void *)&spkr_gain);
588                         } else if (strcasecmp(property, "MicrophoneGain") == 0) {
589                                 unsigned int mic_gain;
590                                 guint16 gain = g_variant_get_uint16(values);
591
592                                 mic_gain = (unsigned int)gain;
593                                 BT_DBG("mic_gain[%d]", mic_gain);
594
595                                 __bt_telephony_event_cb(
596                                                 BLUETOOTH_EVENT_TELEPHONY_SET_MIC_GAIN,
597                                                 BLUETOOTH_TELEPHONY_ERROR_NONE,
598                                                 (void *)&mic_gain);
599                         } else if (strcasecmp(property, "Playing") == 0) {
600                                 gboolean audio_sink_playing;
601
602                                 audio_sink_playing = g_variant_get_boolean(values);
603                                 if (audio_sink_playing) {
604                                         telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
605                                         __bt_telephony_event_cb(
606                                                         BLUETOOTH_EVENT_TELEPHONY_AUDIO_CONNECTED,
607                                                         BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
608                                 } else {
609                                         telephony_info.headset_state =
610                                                         BLUETOOTH_STATE_CONNECTED;
611                                         __bt_telephony_event_cb(
612                                                         BLUETOOTH_EVENT_TELEPHONY_AUDIO_DISCONNECTED,
613                                                         BLUETOOTH_TELEPHONY_ERROR_NONE, NULL);
614                                 }
615                         }
616
617                         g_variant_unref(values);
618                 } else if (strcasecmp(signal_name, HFP_NREC_STATUS_CHANGE) == 0)
619                         __bluetooth_handle_nrec_status_change(parameters);
620                 else if (strcasecmp(signal_name, HFP_ANSWER_CALL) == 0)
621                         __bluetooth_telephony_answer_call(parameters);
622                 else if (strcasecmp(signal_name, HFP_REJECT_CALL) == 0)
623                         __bluetooth_telephony_reject_call(parameters);
624                 else if (strcasecmp(signal_name, HFP_RELEASE_CALL) == 0)
625                         __bluetooth_telephony_release_call(parameters);
626                 else if (strcasecmp(signal_name, HFP_THREEWAY_CALL) == 0)
627                         __bluetooth_telephony_threeway_call(parameters);
628         }
629         BT_DBG("-");
630 }
631
632 static GDBusNodeInfo *__bt_telephony_create_method_node_info
633                                         (const gchar *introspection_data)
634 {
635         GError *err = NULL;
636         GDBusNodeInfo *node_info = NULL;
637
638         if (introspection_data == NULL)
639                 return NULL;
640
641         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
642
643         if (err) {
644                 BT_ERR("Unable to create node: %s", err->message);
645                 g_clear_error(&err);
646         }
647         return node_info;
648 }
649
650
651 int __bluetooth_telephony_register_object(int reg, GDBusNodeInfo *node_info)
652 {
653         static guint bt_tel_id = 0;
654         GError *error =  NULL;
655         gchar *path;
656
657         if (reg == TRUE) {
658                 if (node_info == NULL)
659                         return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
660
661                 path = g_strdup(telephony_info.call_path);
662
663                 bt_tel_id = g_dbus_connection_register_object(telephony_dbus_info.conn,
664                                 path, node_info->interfaces[0],
665                                 &method_table,
666                                 NULL, NULL, &error);
667
668                 g_free(path);
669                 if (bt_tel_id == 0) {
670                         BT_ERR("Failed to register: %s", error->message);
671                         g_error_free(error);
672                         return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
673                 }
674         } else {
675                 if (bt_tel_id > 0) {
676                         g_dbus_connection_unregister_object(telephony_dbus_info.conn,
677                                         bt_tel_id);
678                         bt_tel_id = 0;
679                 }
680         }
681
682         return BLUETOOTH_TELEPHONY_ERROR_NONE;
683 }
684
685 static int __bluetooth_telephony_proxy_init(void)
686 {
687         FN_START;
688         GDBusNodeInfo *node_info;
689         gchar *name;
690
691         name = g_strdup_printf("org.tizen.csd.Call.Instance.p%d", getpid());
692
693         owner_id = g_bus_own_name_on_connection(telephony_dbus_info.conn,
694                                 name, G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL);
695         BT_DBG("well-known name: %s, owner_id: %d", name, owner_id);
696         g_free(name);
697
698         node_info = __bt_telephony_create_method_node_info(
699                                 bt_telephony_introspection_xml);
700         if (node_info == NULL) {
701                 BT_ERR("node_info NULL");
702                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
703         }
704         if (__bluetooth_telephony_register_object(TRUE, node_info) !=
705                         BLUETOOTH_TELEPHONY_ERROR_NONE) {
706                 BT_ERR("Registation of Method Failed");
707                 g_dbus_node_info_unref(node_info);
708                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
709         }
710
711         FN_END;
712         g_dbus_node_info_unref(node_info);
713         return BLUETOOTH_TELEPHONY_ERROR_NONE;
714 }
715
716 static void __bluetooth_telephony_proxy_deinit(void)
717 {
718         FN_START;
719
720         __bluetooth_telephony_register_object(FALSE, NULL);
721         g_bus_unown_name(owner_id);
722
723         FN_END;
724 }
725
726 static int __bluetooth_telephony_register(void)
727 {
728         GVariant *reply;
729         GVariant *param;
730         GError *err = NULL;
731         char *path = g_strdup(telephony_info.call_path);
732         int ret;
733
734         FN_START;
735
736         param = g_variant_new("(ss)", path, src_addr);
737         BT_DBG("Path[%s] Src_Address[%s]", path, src_addr);
738
739         reply =  __bluetooth_telephony_dbus_method_send(
740                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
741                         "RegisterApplication", &err, param);
742
743         g_free(path);
744
745         if (!reply) {
746                 BT_ERR("Error returned in method call");
747                 if (err) {
748                         g_dbus_error_strip_remote_error(err);
749                         ret = __bt_telephony_get_error(err->message);
750                         BT_ERR("Error here %d\n", ret);
751                         g_error_free(err);
752                         return ret;
753                 }
754                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
755         }
756
757         g_variant_unref(reply);
758         BT_DBG("__bluetooth_telephony_register completed");
759         FN_END;
760         return BLUETOOTH_TELEPHONY_ERROR_NONE;
761 }
762
763 static  int __bluetooth_telephony_unregister(void)
764 {
765         GVariant *reply;
766         GVariant *param;
767         GError *err = NULL;
768         char *path = g_strdup(telephony_info.call_path);
769         int ret;
770
771         FN_START;
772
773         param = g_variant_new("(s)", path);
774         reply = __bluetooth_telephony_dbus_method_send(
775                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
776                         "UnregisterApplication", &err, param);
777
778         g_free(path);
779
780         if (!reply) {
781                 BT_ERR("Error returned in method call");
782                 if (err) {
783                         g_dbus_error_strip_remote_error(err);
784                         ret = __bt_telephony_get_error(err->message);
785                         g_error_free(err);
786                         return ret;
787                 }
788                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
789         }
790
791         g_variant_unref(reply);
792         BT_DBG("__bluetooth_telephony_unregister completed");
793         FN_END;
794         return BLUETOOTH_TELEPHONY_ERROR_NONE;
795 }
796
797 static void __bluetooth_telephony_init_headset_state(void)
798 {
799         GVariant *reply;
800         GError *err = NULL;
801         gboolean status = FALSE;
802
803         FN_START;
804
805         if (telephony_dbus_info.conn == NULL) {
806                 BT_ERR("Bluetooth telephony not initilized");
807                 return;
808         }
809
810         reply = __bluetooth_telephony_dbus_method_send(
811                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
812                         "IsConnected", &err, NULL);
813         if (!reply) {
814                 BT_ERR("Error returned in method call\n");
815                 if (err) {
816                         BT_ERR("Error message = %s", err->message);
817                         g_error_free(err);
818                 }
819                 return;
820         }
821
822         g_variant_get(reply, "(b)", &status);
823         g_variant_unref(reply);
824
825         BT_INFO("Headset Connected Status = [%d]", status);
826         if (status)
827                 telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
828         else
829                 return;
830
831         if (bluetooth_telephony_is_sco_connected())
832                 telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
833
834         FN_END;
835 }
836
837 static gboolean __bluetooth_telephony_is_headset(uint32_t device_class)
838 {
839         gboolean flag = FALSE;
840         FN_START;
841
842         switch ((device_class & 0x1f00) >> 8) {
843         case 0x04:
844                 switch ((device_class & 0xfc) >> 2) {
845                 case 0x01:
846                 case 0x02:
847                         flag = TRUE;
848                         break;
849                 case 0x06:
850                         flag = TRUE;
851                         break;
852                 case 0x0b:
853                 case 0x0c:
854                 case 0x0d:
855                         break;
856                 default:
857                         flag = TRUE;
858                         break;
859                 }
860                 break;
861
862         /* Tizen Wearable device */
863         case 0x07:
864                 switch ((device_class & 0xfc) >> 2) {
865                 case 0x01: /* Wrist Watch */
866                         flag = TRUE;
867                         break;
868                 default:
869                         break;
870                 }
871                 break;
872         }
873         BT_DBG("[%d]", flag);
874         FN_END;
875         return flag;
876 }
877
878 static gboolean __bluetooth_telephony_is_headset_by_uuid(gchar **uuids)
879 {
880         int i;
881         char **parts;
882         unsigned int service = 0;
883
884         FN_START;
885
886         retv_if(uuids == NULL, FALSE);
887
888         for (i = 0; uuids[i] != NULL; i++) {
889                 parts = g_strsplit(uuids[i], "-", -1);
890
891                 if (parts == NULL || parts[0] == NULL) {
892                         g_strfreev(parts);
893                         continue;
894                 }
895
896                 service = g_ascii_strtoull(parts[0], NULL, 16);
897                 g_strfreev(parts);
898
899                 if (service == BLUETOOTH_HS_PROFILE_UUID ||
900                                 service == BLUETOOTH_HF_PROFILE_UUID)
901                         return TRUE;
902         }
903
904         FN_END;
905         return FALSE;
906 }
907
908
909 static int __bluetooth_telephony_get_connected_device(void)
910 {
911         GDBusConnection *conn;
912         GDBusProxy *headset_agent_proxy = NULL;
913         GDBusProxy *manager_proxy = NULL;
914         GDBusProxy *proxy = NULL;
915         GVariant *reply = NULL;
916         GVariant *getall = NULL;
917         GVariant *isPlayingReply = NULL;
918         GVariant *isConnectedReply = NULL;
919         GVariant *param = NULL;
920         GVariant *var_path = NULL;
921         GVariant *path_values = NULL;
922         GVariant *value = NULL;
923         GError *error = NULL;
924         GVariantIter iter;
925         GVariantIter iter_path;
926         GVariantIter property_iter;
927         int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
928
929         FN_START;
930         conn = _bt_get_system_private_conn();
931         retv_if(conn == NULL, BLUETOOTH_TELEPHONY_ERROR_INTERNAL);
932
933         manager_proxy = g_dbus_proxy_new_sync(
934                         conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
935                         BLUEZ_SERVICE_NAME, "/",
936                         BLUEZ_MANAGER_INTERFACE, NULL, &error);
937         if (manager_proxy == NULL) {
938                 BT_ERR("Unable to allocate new proxy \n");
939                 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
940                 if (error) {
941                         g_dbus_error_strip_remote_error(error);
942                         ret = __bt_telephony_get_error(error->message);
943                         BT_ERR("Error here %d\n", ret);
944                         g_error_free(error);
945                 }
946                 goto done;
947         }
948
949         /* Synchronous call */
950         reply = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects", NULL,
951                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
952         g_object_unref(manager_proxy);
953
954         if (!reply) {
955                 BT_ERR("Can't get managed objects");
956                 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
957                 if (error) {
958                         g_dbus_error_strip_remote_error(error);
959                         ret = __bt_telephony_get_error(error->message);
960                         BT_ERR("Error here %d\n", ret);
961                         g_error_free(error);
962                 }
963                 goto done;
964         }
965
966         /* signature of GetManagedObjects:      a{oa{sa{sv}}} */
967         g_variant_iter_init(&iter, reply);
968
969         while ((param = g_variant_iter_next_value(&iter))) {
970                 g_variant_iter_init(&iter_path, param);
971
972                 while ((var_path = g_variant_iter_next_value(&iter_path))) {
973                         gsize len;
974                         uint32_t device_class = 0;
975                         gboolean playing = FALSE;
976                         gboolean connected = FALSE;
977                         char *object_path = NULL;
978                         gchar *address = NULL;
979                         const gchar *key;
980                         gchar **uuids = NULL;
981                         GVariant *getall_param = NULL;
982
983                         g_variant_get(var_path, "{&o*}", &object_path,
984                                         &path_values);
985                         g_variant_unref(path_values); /* path_values unused*/
986
987                         proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
988                                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
989                                         BLUEZ_SERVICE_NAME, object_path,
990                                         BLUEZ_PROPERTIES_INTERFACE, NULL, &error);
991                         if (proxy == NULL) {
992                                 BT_ERR("Unable to allocate new proxy \n");
993                                 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
994                                 if (error) {
995                                         g_dbus_error_strip_remote_error(error);
996                                         ret = __bt_telephony_get_error(error->message);
997                                         BT_ERR("Error here %d\n", ret);
998                                         g_error_free(error);
999                                 }
1000                                 goto done;
1001                         }
1002
1003
1004                         getall_param = g_variant_new("s", BLUEZ_DEVICE_INTERFACE);
1005                         getall = g_dbus_proxy_call_sync(proxy,
1006                                         "GetAll", getall_param,
1007                                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1008                         g_object_unref(proxy);
1009
1010                         if (!getall) {
1011                                 BT_ERR("Can't get managed objects");
1012                                 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1013                                 if (error) {
1014                                         g_dbus_error_strip_remote_error(error);
1015                                         ret = __bt_telephony_get_error(error->message);
1016                                         BT_ERR("Error here %d\n", ret);
1017                                         g_error_free(error);
1018                                 }
1019                                 goto done;
1020                         }
1021
1022                         g_variant_iter_init(&property_iter, getall);
1023
1024                         while (g_variant_iter_loop(&property_iter, "{&sv}", &key, &value)) {
1025                                 if (!g_strcmp0(key, "Class")) {
1026                                         device_class = g_variant_get_uint32(value);
1027                                         BT_DBG("Device Class: %d", device_class);
1028                                 } else if (!g_strcmp0(key, "UUID")) {
1029                                         int i = 0;
1030                                         uuids = (gchar **)g_variant_get_strv(value, &len);
1031                                         BT_DBG_UUID(uuids, len, i);
1032                                 } else if (!g_strcmp0(key, "Address")) {
1033                                         address = (gchar *)g_variant_get_string(
1034                                                                         value,
1035                                                                         NULL);
1036                                         BT_DBG("Device Class: %s", address);
1037                                 }
1038                                 g_variant_unref(value);
1039                         }
1040                         g_variant_unref(getall);
1041
1042                         if (device_class == 0) {
1043                                 BT_DBG("COD is NULL (maybe paired by nfc)...  Checking UUIDs");
1044                                 if (!__bluetooth_telephony_is_headset_by_uuid(uuids)) {
1045                                         BT_DBG("UUID checking completed. None HF device");
1046                                         continue;
1047                                 }
1048                                 BT_DBG("UUID checking completed. HF device");
1049                         } else {
1050                                 if (!__bluetooth_telephony_is_headset(device_class))
1051                                         continue;
1052                         }
1053
1054                         /* this is headset; Check for Connection */
1055                         headset_agent_proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1056                                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1057                                         HFP_AGENT_SERVICE, object_path,
1058                                         HFP_AGENT_INTERFACE, NULL, &error);
1059                         if (headset_agent_proxy == NULL) {
1060                                 BT_ERR("Unable to allocate new headset_agent_proxy");
1061                                 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1062                                 if (error) {
1063                                         g_dbus_error_strip_remote_error(error);
1064                                         ret = __bt_telephony_get_error(error->message);
1065                                         BT_ERR("Error here %d\n", ret);
1066                                         g_error_free(error);
1067                                 }
1068                                 goto done;
1069                         }
1070
1071                         isConnectedReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1072                                         "IsConnected", NULL,
1073                                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1074
1075
1076                         if (!isConnectedReply) {
1077                                 BT_ERR("Can't get managed objects");
1078                                 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1079                                 if (error) {
1080                                         g_dbus_error_strip_remote_error(error);
1081                                         ret = __bt_telephony_get_error(error->message);
1082                                         BT_ERR("Error here %d\n", ret);
1083                                         g_error_free(error);
1084                                 }
1085                                 goto done;
1086                         } else {
1087                                 connected = g_variant_get_boolean(isConnectedReply);
1088                                 g_variant_unref(isConnectedReply);
1089
1090                                 if (connected) {
1091                                         g_strlcpy(telephony_info.address,
1092                                                         address,
1093                                                         sizeof(telephony_info.address));
1094
1095                                         isPlayingReply = g_dbus_proxy_call_sync(headset_agent_proxy,
1096                                                         "IsPlaying", NULL,
1097                                                         G_DBUS_CALL_FLAGS_NONE,
1098                                                         -1, NULL, &error);
1099                                         if (!isPlayingReply) {
1100                                                 BT_ERR("Can't get managed objects");
1101                                                 ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1102                                                 if (error) {
1103                                                         g_dbus_error_strip_remote_error(error);
1104                                                         ret = __bt_telephony_get_error(error->message);
1105                                                         BT_ERR("Error here %d\n", ret);
1106                                                         g_error_free(error);
1107                                                 }
1108                                         } else {
1109                                                 playing = g_variant_get_boolean(isPlayingReply);
1110                                                 g_variant_unref(isPlayingReply);
1111
1112                                                 if (playing)
1113                                                         telephony_info.headset_state =
1114                                                                 BLUETOOTH_STATE_PLAYING;
1115                                                 else
1116                                                         telephony_info.headset_state =
1117                                                                 BLUETOOTH_STATE_CONNECTED;
1118                                         }
1119
1120                                         goto done;
1121                                 }
1122                         }
1123
1124                         g_object_unref(headset_agent_proxy);
1125                         g_variant_unref(var_path);
1126                 }
1127                 g_variant_unref(param);
1128         }
1129
1130 done:
1131         if (headset_agent_proxy)
1132                 g_object_unref(headset_agent_proxy);
1133         if (reply)
1134                 g_variant_unref(reply);
1135         if (var_path)
1136                 g_variant_unref(var_path);
1137         if (param)
1138                 g_variant_unref(param);
1139         FN_END;
1140         return ret;
1141 }
1142
1143 static GDBusProxy *__bluetooth_telephony_get_connected_device_proxy(void)
1144 {
1145         GDBusProxy *proxy = NULL;
1146         GError *error = NULL;
1147         int ret;
1148         FN_START;
1149
1150         if (strlen(telephony_info.address) == 0)
1151                 __bluetooth_telephony_get_connected_device();
1152
1153         if (strlen(telephony_info.address) == 0)
1154                 return NULL;
1155
1156         proxy = g_dbus_proxy_new_sync(telephony_dbus_info.conn,
1157                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1158                         HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1159                         HFP_AGENT_INTERFACE, NULL, &error);
1160         if (proxy == NULL) {
1161                 BT_ERR("Unable to allocate new proxy");
1162                 if (error) {
1163                         g_dbus_error_strip_remote_error(error);
1164                         ret = __bt_telephony_get_error(error->message);
1165                         BT_ERR("Error here %d\n", ret);
1166                         g_error_free(error);
1167                 }
1168                 return NULL;
1169         }
1170
1171         FN_END;
1172         return proxy;
1173 }
1174
1175 int __bt_telephony_subscribe_adapter_signal(GDBusConnection *conn,
1176                 int subscribe)
1177 {
1178         if (conn == NULL)
1179                 return -1;
1180
1181         static guint subscribe_adapter_id = 0;
1182
1183         if (subscribe == TRUE) {
1184                 if (subscribe_adapter_id == 0) {
1185                         subscribe_adapter_id = g_dbus_connection_signal_subscribe(conn,
1186                                         NULL, "org.freedesktop.DBus.ObjectManager",
1187                                         "InterfacesAdded", NULL, NULL, 0,
1188                                         __bt_telephony_adapter_filter,
1189                                         NULL, NULL);
1190                 }
1191                 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1192         } else {
1193                 if (subscribe_adapter_id > 0) {
1194                         g_dbus_connection_signal_unsubscribe(conn,
1195                                         subscribe_adapter_id);
1196                         subscribe_adapter_id = 0;
1197                 }
1198                 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1199         }
1200 }
1201
1202 int __bt_telephony_event_subscribe_signal(GDBusConnection *conn,
1203                 int subscribe)
1204 {
1205         if (conn == NULL)
1206                 return -1;
1207
1208         static guint event_id = 0;
1209
1210         if (subscribe == TRUE) {
1211                 if (event_id == 0) {
1212                         event_id = g_dbus_connection_signal_subscribe(conn,
1213                                         NULL, BLUEZ_HEADSET_INTERFACE,
1214                                         NULL, NULL, NULL, 0,
1215                                         __bluetooth_telephony_event_filter,
1216                                         NULL, NULL);
1217                 }
1218                 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1219         } else {
1220                 if (event_id > 0) {
1221                         g_dbus_connection_signal_unsubscribe(conn,
1222                                         event_id);
1223                         event_id = 0;
1224                 }
1225                 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1226         }
1227 }
1228
1229 BT_EXPORT_API int bluetooth_telephony_init(bt_telephony_func_ptr cb,
1230                                                         void  *user_data)
1231 {
1232         bluetooth_device_address_t loc_address = { {0} };
1233         char src_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1234         int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
1235         GError *error = NULL;
1236
1237         FN_START;
1238
1239         if (is_initialized == TRUE) {
1240                 BT_ERR("Bluetooth telephony already initilized");
1241                 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1242         }
1243
1244         is_initialized = TRUE;
1245
1246         /* As a result of discussion with system team, private connection is not
1247          * suitable in here. It is better to use shared connection. */
1248         //telephony_dbus_info.conn = _bt_gdbus_init_system_gconn();
1249         telephony_dbus_info.conn = _bt_get_system_shared_conn();
1250         if (!telephony_dbus_info.conn) {
1251                 is_initialized = FALSE;
1252                 BT_ERR("Could not get DBus Connection");
1253                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1254         }
1255
1256         /* Call Path */
1257         snprintf(telephony_info.call_path, sizeof(telephony_info.call_path),
1258                                         CSD_CALL_APP_PATH, getpid());
1259         BT_INFO("Call Path = %s", telephony_info.call_path);
1260         memset(telephony_info.address, 0x00, sizeof(telephony_info.address));
1261
1262         if (__bluetooth_telephony_proxy_init()) {
1263                 BT_ERR("__bluetooth_telephony_proxy_init failed\n");
1264                 telephony_dbus_info.conn = NULL;
1265                 is_initialized = FALSE;
1266                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1267         }
1268
1269         telephony_dbus_info.manager_proxy = g_dbus_proxy_new_sync(
1270                         telephony_dbus_info.conn,
1271                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1272                         BLUEZ_SERVICE_NAME, "/",
1273                         BLUEZ_MANAGER_INTERFACE, NULL, &error);
1274         if (telephony_dbus_info.manager_proxy == NULL) {
1275                 BT_ERR("Could not create a manager proxy");
1276                 __bluetooth_telephony_proxy_deinit();
1277                 telephony_dbus_info.conn = NULL;
1278                 is_initialized = FALSE;
1279                 if (error) {
1280                         g_dbus_error_strip_remote_error(error);
1281                         ret = __bt_telephony_get_error(error->message);
1282                         BT_ERR("Error here %d\n", ret);
1283                         g_error_free(error);
1284                         return ret;
1285                 }
1286                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1287         }
1288
1289         telephony_dbus_info.dbus_proxy = g_dbus_proxy_new_sync(
1290                         telephony_dbus_info.conn,
1291                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1292                         BT_EVENT_FREEDESKTOP, BT_FREEDESKTOP_PATH,
1293                         BT_EVENT_FREEDESKTOP, NULL, &error);
1294         if (NULL == telephony_dbus_info.dbus_proxy) {
1295                 __bluetooth_telephony_proxy_deinit();
1296                 telephony_dbus_info.conn = NULL;
1297                 g_object_unref(telephony_dbus_info.manager_proxy);
1298                 telephony_dbus_info.manager_proxy = NULL;
1299                 is_initialized = FALSE;
1300                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1301         }
1302
1303         if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, TRUE) != 0) {
1304                 BT_ERR("Fail to Subscribe Adapter Signal");
1305                 goto fail;
1306         }
1307
1308         /*Callback and user applicaton data*/
1309         telephony_info.cb = cb;
1310         telephony_info.user_data = user_data;
1311         telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1312
1313         if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, TRUE) != 0) {
1314                 BT_ERR("Fail to Subscribe telephony event Signal");
1315                 goto fail;
1316         }
1317
1318         if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED)
1319                 return BLUETOOTH_TELEPHONY_ERROR_NONE;
1320
1321         /*Bluetooth is active, therefore set the flag */
1322         is_active = TRUE;
1323         if (!src_addr) {
1324                 ret = bluetooth_get_local_address(&loc_address);
1325                 if (ret != BLUETOOTH_ERROR_NONE) {
1326                         BT_ERR("Fail to get local address\n");
1327                         ret = BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1328                         goto fail;
1329                 }
1330                 _bt_convert_addr_type_to_string(src_address, loc_address.addr);
1331                 src_addr = g_strdup(src_address);
1332         }
1333         ret = __bluetooth_telephony_register();
1334         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1335                 BT_ERR("__bluetooth_telephony_register failed\n");
1336                 goto fail;
1337         }
1338
1339         if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
1340                 __bluetooth_telephony_init_headset_state();
1341
1342         FN_END;
1343         return ret;
1344 fail:
1345         bluetooth_telephony_deinit();
1346         return ret;
1347 }
1348
1349 BT_EXPORT_API int bluetooth_telephony_deinit(void)
1350 {
1351         FN_START;
1352         BT_TELEPHONY_CHECK_INITIALIZED();
1353
1354         is_initialized = FALSE;
1355
1356         if (__bt_telephony_event_subscribe_signal(telephony_dbus_info.conn, FALSE) != 0)
1357                 BT_ERR("Fail to UnSubscribe telephony event Signal");
1358
1359         if (bluetooth_check_adapter() != BLUETOOTH_ADAPTER_DISABLED ||
1360                 bluetooth_check_adapter_le() != BLUETOOTH_ADAPTER_LE_DISABLED)
1361                 __bluetooth_telephony_unregister();
1362
1363         __bluetooth_telephony_proxy_deinit();
1364
1365         telephony_info.cb = NULL;
1366         telephony_info.user_data = NULL;
1367         telephony_info.call_count = 0;
1368         telephony_info.headset_state = BLUETOOTH_STATE_DISCONNETED;
1369
1370         /* Remove BT enabled signal */
1371         if (__bt_telephony_subscribe_adapter_signal(telephony_dbus_info.conn, FALSE) != 0)
1372                 BT_ERR("Fail to UnSubscribe Adapter event Signal");
1373
1374         g_free(src_addr);
1375         src_addr = NULL;
1376
1377         if (telephony_dbus_info.manager_proxy != NULL) {
1378                 g_object_unref(telephony_dbus_info.manager_proxy);
1379                 telephony_dbus_info.manager_proxy = NULL;
1380         }
1381
1382         if (telephony_dbus_info.conn != NULL)
1383                 telephony_dbus_info.conn = NULL;
1384
1385         if (telephony_dbus_info.dbus_proxy != NULL) {
1386                 g_object_unref(telephony_dbus_info.dbus_proxy);
1387                 telephony_dbus_info.dbus_proxy = NULL;
1388         }
1389
1390         FN_END;
1391         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1392 }
1393
1394 BT_EXPORT_API gboolean bluetooth_telephony_is_sco_connected(void)
1395 {
1396         GVariant *reply;
1397         GError *err = NULL;
1398         gboolean status = FALSE;
1399
1400         FN_START;
1401
1402         retv_if(is_initialized == FALSE, FALSE);
1403         retv_if(bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED, FALSE);
1404
1405         reply = __bluetooth_telephony_dbus_method_send(
1406                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1407                         "IsPlaying", &err, NULL);
1408
1409         if (!reply) {
1410                 BT_ERR("Error returned in method call\n");
1411                 if (err) {
1412                         BT_ERR("Error message = %s", err->message);
1413                         g_error_free(err);
1414                 }
1415                 return FALSE;
1416         }
1417         g_variant_get(reply, "(b)", &status);
1418         g_variant_unref(reply);
1419
1420         if (TIZEN_PROFILE_WEARABLE || TIZEN_PROFILE_IVI)
1421                 if (status == TRUE && telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1422                         telephony_info.headset_state = BLUETOOTH_STATE_PLAYING;
1423
1424         BT_INFO("SCO Connected Status = [%d]", status);
1425         return status;
1426 }
1427
1428 BT_EXPORT_API int bluetooth_telephony_is_nrec_enabled(gboolean *status)
1429 {
1430         GVariant *reply;
1431         GError *err = NULL;
1432         GVariantIter iter;
1433         GVariant *param_inner;
1434
1435         FN_START;
1436
1437         BT_TELEPHONY_CHECK_INITIALIZED();
1438         BT_TELEPHONY_CHECK_ENABLED();
1439
1440         if (status == NULL)
1441                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1442
1443         if (telephony_info.headset_state == BLUETOOTH_STATE_DISCONNETED)
1444                 return BLUETOOTH_TELEPHONY_ERROR_AUDIO_NOT_CONNECTED;
1445
1446         reply = __bluetooth_telephony_dbus_method_send(
1447                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1448                         "GetProperties", &err, NULL);
1449
1450         if (!reply) {
1451                 BT_ERR("Error returned in method call\n");
1452                 if (err) {
1453                         BT_DBG("Error message = %s", err->message);
1454                         g_error_free(err);
1455                 }
1456                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1457         }
1458
1459         g_variant_iter_init(&iter, reply);
1460         while ((param_inner = g_variant_iter_next_value(&iter))) {
1461                 GVariant *value;
1462
1463                 value = g_variant_lookup_value(param_inner,
1464                                         "nrec", G_VARIANT_TYPE_BOOLEAN);
1465                 if (value) {
1466                         BT_DBG("Property NREC Found");
1467                         *status = g_variant_get_boolean(value);
1468                         BT_DBG("NREC status = [%d]", *status);
1469                         g_variant_unref(value);
1470                         g_variant_unref(param_inner);
1471                         break;
1472                 }
1473                 g_variant_unref(param_inner);
1474         }
1475         BT_DBG("NREC status = [%d]", *status);
1476         g_variant_unref(reply);
1477
1478         FN_END;
1479         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1480 }
1481
1482 BT_EXPORT_API int bluetooth_telephony_is_wbs_mode(gboolean *status)
1483 {
1484         GVariant *reply;
1485         GError *err = NULL;
1486         unsigned int codec;
1487         GVariantIter iter;
1488         GVariant *param_inner;
1489
1490         FN_START;
1491
1492         BT_TELEPHONY_CHECK_INITIALIZED();
1493         BT_TELEPHONY_CHECK_ENABLED();
1494
1495         if (status == NULL)
1496                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1497
1498         *status = FALSE;
1499
1500         reply = __bluetooth_telephony_dbus_method_send(
1501                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1502                         "GetProperties", &err, NULL);
1503
1504         if (!reply) {
1505                 BT_ERR("Error returned in method call");
1506                 if (err) {
1507                         BT_ERR("Error message = %s", err->message);
1508                         g_error_free(err);
1509                 }
1510                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1511         }
1512
1513         g_variant_iter_init(&iter, reply);
1514         while ((param_inner = g_variant_iter_next_value(&iter))) {
1515                 GVariant *value;
1516
1517                 value = g_variant_lookup_value(param_inner,
1518                                         "codec", G_VARIANT_TYPE_UINT32);
1519                 if (value) {
1520                         BT_DBG("Property CODEC Found");
1521                         codec = g_variant_get_uint32(value);
1522                         g_variant_unref(value);
1523                         BT_DBG("Codec = [%d]", codec);
1524
1525                         *status = codec == BT_MSBC_CODEC_ID ? TRUE : FALSE;
1526                         BT_DBG("NREC status = [%d]", *status);
1527                         g_variant_unref(value);
1528                         g_variant_unref(param_inner);
1529                         break;
1530                 }
1531                 g_variant_unref(param_inner);
1532         }
1533
1534         g_variant_unref(reply);
1535         BT_DBG("MSBC status = [%d]", *status);
1536
1537         FN_END;
1538         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1539 }
1540
1541 BT_EXPORT_API int bluetooth_telephony_send_vendor_cmd(const char *cmd)
1542 {
1543         GError *error = NULL;
1544         GVariant *reply, *parameters;
1545         int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
1546
1547         FN_START;
1548
1549         BT_TELEPHONY_CHECK_INITIALIZED();
1550         BT_TELEPHONY_CHECK_ENABLED();
1551
1552         BT_DBG("Send Vendor %s", cmd);
1553
1554         if (telephony_dbus_info.proxy == NULL)
1555                 telephony_dbus_info.proxy =
1556                         __bluetooth_telephony_get_connected_device_proxy();
1557
1558         if (telephony_dbus_info.proxy == NULL)
1559                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1560
1561         parameters = g_variant_new("s", cmd);
1562         reply = g_dbus_proxy_call_sync(telephony_dbus_info.proxy,
1563                         "SendVendorAtCmd", parameters,
1564                                 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1565
1566         g_variant_unref(reply);
1567
1568         if (error) {
1569                 g_dbus_error_strip_remote_error(error);
1570                 ret = __bt_telephony_get_error(error->message);
1571                 BT_ERR("Error here %d\n", ret);
1572                 g_error_free(error);
1573         }
1574
1575         FN_END;
1576         return ret;
1577 }
1578
1579 BT_EXPORT_API int bluetooth_telephony_start_voice_recognition(void)
1580 {
1581         GVariant *reply;
1582         GVariant *param;
1583         GError *err = NULL;
1584         int ret;
1585         gboolean state = TRUE;
1586
1587         FN_START;
1588
1589         BT_TELEPHONY_CHECK_INITIALIZED();
1590         BT_TELEPHONY_CHECK_ENABLED();
1591
1592         param = g_variant_new("(b)", &state);
1593         reply = __bluetooth_telephony_dbus_method_send(
1594                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1595                         "SetVoiceDial", &err, param);
1596
1597         if (!reply) {
1598                 BT_ERR("Error returned in method call\n");
1599                 if (err) {
1600                         g_dbus_error_strip_remote_error(err);
1601                         ret = __bt_telephony_get_error(err->message);
1602                         g_error_free(err);
1603                         return ret;
1604                 }
1605                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1606         }
1607
1608         g_variant_unref(reply);
1609         FN_END;
1610         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1611 }
1612
1613 BT_EXPORT_API int bluetooth_telephony_stop_voice_recognition(void)
1614 {
1615         GVariant *reply;
1616         GVariant *param;
1617         GError *err = NULL;
1618         int ret;
1619         gboolean state = FALSE;
1620
1621         FN_START;
1622
1623         BT_TELEPHONY_CHECK_INITIALIZED();
1624         BT_TELEPHONY_CHECK_ENABLED();
1625
1626         param = g_variant_new("(b)", &state);
1627         reply = __bluetooth_telephony_dbus_method_send(
1628                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
1629                         "SetVoiceDial", &err, param);
1630
1631         if (!reply) {
1632                 BT_ERR("Error returned in method call\n");
1633                 if (err) {
1634                         g_dbus_error_strip_remote_error(err);
1635                         ret = __bt_telephony_get_error(err->message);
1636                         g_error_free(err);
1637                         return ret;
1638                 }
1639                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1640         }
1641
1642         g_variant_unref(reply);
1643
1644         FN_END;
1645         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1646 }
1647
1648 static void __bluetooth_telephony_sco_start_cb(GDBusProxy *proxy,
1649                 GAsyncResult *res, gpointer user_data)
1650 {
1651         GError *error = NULL;
1652         GVariant *value = NULL;
1653
1654         value = g_dbus_proxy_call_finish(proxy, res, &error);
1655         if (value == NULL) {
1656                 if (error != NULL) {
1657                         BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1658                                         error->code, error->message);
1659                         g_clear_error(&error);
1660                 } else {
1661                         BT_ERR("SCo Start Failed");
1662                 }
1663                 g_object_unref(proxy);
1664                 return;
1665         }
1666
1667         BT_DBG("sco_start_cb : -");
1668         g_object_unref(proxy);
1669         g_variant_unref(value);
1670 }
1671
1672 BT_EXPORT_API int bluetooth_telephony_audio_open(void)
1673 {
1674         GDBusConnection *conn;
1675         GDBusProxy *proxy;
1676         GError *err = NULL;
1677         int ret;
1678
1679         FN_START;
1680
1681         BT_TELEPHONY_CHECK_INITIALIZED();
1682         BT_TELEPHONY_CHECK_ENABLED();
1683
1684         /* Because this API is async call, so can't use dbus SMACK */
1685         if (__bt_telephony_check_privilege() ==
1686                                 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1687                 BT_ERR("Don't have a privilege to use this API");
1688                 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1689         }
1690
1691         conn = _bt_get_system_private_conn();
1692         if (!conn) {
1693                 BT_DBG("No System Bus found\n");
1694                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1695         }
1696
1697         if (telephony_info.headset_state == BLUETOOTH_STATE_PLAYING)
1698                 return BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED;
1699
1700         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1701                         HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1702                         HFP_AGENT_INTERFACE, NULL, &err);
1703         if (proxy == NULL) {
1704                 BT_ERR("Unable to allocate new proxy");
1705                 if (err) {
1706                         g_dbus_error_strip_remote_error(err);
1707                         ret = __bt_telephony_get_error(err->message);
1708                         BT_ERR("Error here %d\n", ret);
1709                         g_error_free(err);
1710                         return ret;
1711                 }
1712                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1713         }
1714
1715         g_dbus_proxy_call(proxy, "Play", NULL, G_DBUS_CALL_FLAGS_NONE,
1716                         -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_start_cb, NULL);
1717
1718         FN_END;
1719         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1720 }
1721
1722 static void __bluetooth_telephony_sco_close_cb(GDBusProxy *proxy,
1723                 GAsyncResult *res, gpointer user_data)
1724 {
1725         GError *error = NULL;
1726         GVariant *value = NULL;
1727
1728         value = g_dbus_proxy_call_finish(proxy, res, &error);
1729         if (value == NULL) {
1730                 if (error != NULL) {
1731                         BT_ERR("sco_close_cb error. errCode[%x],message[%s]",
1732                                         error->code, error->message);
1733                         g_clear_error(&error);
1734                 } else {
1735                         BT_ERR("SCo close Failed");
1736                 }
1737                 g_object_unref(proxy);
1738                 return;
1739         }
1740
1741         BT_DBG("sco_close_cb : -");
1742         g_object_unref(proxy);
1743         g_variant_unref(value);
1744 }
1745 BT_EXPORT_API int bluetooth_telephony_audio_close(void)
1746 {
1747         GDBusConnection *conn;
1748         GDBusProxy *proxy;
1749         GError *err = NULL;
1750         int ret;
1751
1752         FN_START;
1753
1754         BT_TELEPHONY_CHECK_INITIALIZED();
1755         BT_TELEPHONY_CHECK_ENABLED();
1756
1757         /* Because this API is async call, so can't use dbus SMACK */
1758         if (__bt_telephony_check_privilege() ==
1759                                 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1760                 BT_ERR("Don't have a privilege to use this API");
1761                 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1762         }
1763
1764         conn = _bt_get_system_private_conn();
1765         if (!conn) {
1766                 BT_DBG("No System Bus found\n");
1767                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1768         }
1769
1770         if (telephony_info.headset_state != BLUETOOTH_STATE_PLAYING)
1771                 return BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED;
1772
1773         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
1774                         HFP_AGENT_SERVICE, HFP_AGENT_PATH,
1775                         HFP_AGENT_INTERFACE, NULL, &err);
1776         if (proxy == NULL) {
1777                 BT_ERR("Unable to allocate new proxy");
1778                 if (err) {
1779                         g_dbus_error_strip_remote_error(err);
1780                         ret = __bt_telephony_get_error(err->message);
1781                         BT_ERR("Error here %d\n", ret);
1782                         g_error_free(err);
1783                         return ret;
1784                 }
1785                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
1786         }
1787
1788         g_dbus_proxy_call(proxy, "Stop", NULL, G_DBUS_CALL_FLAGS_NONE,
1789                         -1, NULL, (GAsyncReadyCallback)__bluetooth_telephony_sco_close_cb, NULL);
1790
1791         telephony_info.headset_state = BLUETOOTH_STATE_CONNECTED;
1792
1793         FN_END;
1794         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1795 }
1796
1797 BT_EXPORT_API int bluetooth_telephony_call_remote_ringing(unsigned int call_id)
1798 {
1799         int ret;
1800
1801         BT_TELEPHONY_CHECK_INITIALIZED();
1802         BT_TELEPHONY_CHECK_ENABLED();
1803
1804         FN_START;
1805         BT_DBG("call_id = [%d]", call_id);
1806
1807         /*Make sure SCO is already connected */
1808         ret = __bluetooth_telephony_send_call_status(
1809                                 CSD_CALL_STATUS_MO_ALERTING, call_id, NULL);
1810
1811         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1812                 BT_ERR("send call status Failed = [%d]", ret);
1813
1814         FN_END;
1815         return ret;
1816 }
1817
1818 BT_EXPORT_API int bluetooth_telephony_call_answered(unsigned int call_id,
1819                                                         unsigned int bt_audio)
1820 {
1821         int ret;
1822
1823         FN_START;
1824         BT_DBG("call_id = [%d]", call_id);
1825
1826         BT_TELEPHONY_CHECK_INITIALIZED();
1827         BT_TELEPHONY_CHECK_ENABLED();
1828
1829         ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_ACTIVE,
1830                                                                 call_id, NULL);
1831         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1832                 BT_ERR("send call status Failed = [%d]", ret);
1833                 return ret;
1834         }
1835
1836         if (bt_audio) {
1837                 if (!bluetooth_telephony_is_sco_connected()) {
1838                         ret = bluetooth_telephony_audio_open();
1839                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1840                                 BT_ERR("Audio connection call Failed[%d]", ret);
1841                 }
1842         }
1843
1844         FN_END;
1845         return ret;
1846 }
1847
1848 BT_EXPORT_API int bluetooth_telephony_call_end(unsigned int call_id)
1849 {
1850         int ret;
1851
1852         FN_START;
1853         BT_DBG("call_id = [%d]", call_id);
1854
1855         BT_TELEPHONY_CHECK_INITIALIZED();
1856         BT_TELEPHONY_CHECK_ENABLED();
1857
1858         if (telephony_info.call_count > 0)
1859                 telephony_info.call_count = telephony_info.call_count - 1;
1860
1861         if (telephony_info.call_count  == 0) {
1862                 if (bluetooth_telephony_is_sco_connected()) {
1863                         ret = bluetooth_telephony_audio_close();
1864                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1865                                 BT_ERR(" Failed = [%d]", ret);
1866                 }
1867         }
1868
1869         ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_MT_RELEASE,
1870                                                                 call_id, NULL);
1871         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1872                 BT_ERR("send call status Failed = [%d]", ret);
1873
1874         FN_END;
1875         return ret;
1876 }
1877
1878 BT_EXPORT_API int bluetooth_telephony_call_held(unsigned int call_id)
1879 {
1880         int ret;
1881
1882         FN_START;
1883         BT_DBG("call_id = [%d]", call_id);
1884
1885         BT_TELEPHONY_CHECK_INITIALIZED();
1886         BT_TELEPHONY_CHECK_ENABLED();
1887
1888         ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_HOLD,
1889                                                                 call_id, NULL);
1890         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1891                 BT_ERR("send call status Failed = [%d]", ret);
1892
1893         FN_END;
1894         return ret;
1895 }
1896
1897 BT_EXPORT_API int bluetooth_telephony_call_retrieved(unsigned int call_id)
1898 {
1899         int ret;
1900
1901         FN_START;
1902         BT_DBG("call_id = [%d]", call_id);
1903
1904         BT_TELEPHONY_CHECK_INITIALIZED();
1905         BT_TELEPHONY_CHECK_ENABLED();
1906
1907         ret = __bluetooth_telephony_send_call_status(CSD_CALL_STATUS_ACTIVE,
1908                                                                 call_id, NULL);
1909         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
1910                 BT_ERR("send call status Failed = [%d]", ret);
1911
1912         FN_END;
1913         return ret;
1914 }
1915
1916 BT_EXPORT_API int bluetooth_telephony_call_swapped(void *call_list,
1917                                 unsigned int call_count)
1918 {
1919         int i;
1920         int ret;
1921         GList *list = call_list;
1922         bt_telephony_call_status_info_t *call_status;
1923
1924         FN_START;
1925
1926         BT_TELEPHONY_CHECK_INITIALIZED();
1927         BT_TELEPHONY_CHECK_ENABLED();
1928
1929         if (NULL == list) {
1930                 BT_ERR("call_list is invalid");
1931                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
1932         }
1933
1934         /* Because this API is async call, so can't use dbus SMACK */
1935         if (__bt_telephony_check_privilege() ==
1936                                 BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED) {
1937                 BT_ERR("Don't have a privilege to use this API");
1938                 return BLUETOOTH_TELEPHONY_ERROR_PERMISSION_DENIED;
1939         }
1940
1941         BT_DBG(" call_count = [%d]", call_count);
1942
1943         for (i = 0; i < call_count; i++) {
1944                 call_status = g_list_nth_data(list, i);
1945
1946                 if (NULL == call_status)
1947                         continue;
1948
1949                 BT_DBG(" %d : Call id [%d] status[%d]", i,
1950                                         call_status->call_id,
1951                                         call_status->call_status);
1952
1953                 if (NULL != call_status->phone_number)
1954                         DBG_SECURE("Number [%s]", call_status->phone_number);
1955
1956                 switch (call_status->call_status) {
1957                 case BLUETOOTH_CALL_STATE_HELD:
1958                         ret = __bluetooth_telephony_send_call_status(
1959                                                 CSD_CALL_STATUS_HOLD,
1960                                                 call_status->call_id,
1961                                                 call_status->phone_number);
1962                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1963                                 BT_ERR("Failed = %d", ret);
1964                                 return ret;
1965                         }
1966                 break;
1967
1968                 case BLUETOOTH_CALL_STATE_CONNECTED:
1969                         ret = __bluetooth_telephony_send_call_status(
1970                                         CSD_CALL_STATUS_ACTIVE,
1971                                         call_status->call_id,
1972                                         call_status->phone_number);
1973                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
1974                                 BT_ERR("Failed = [%d]", ret);
1975                                 return ret;
1976                         }
1977                 break;
1978
1979                 default:
1980                         if ((call_status->call_status <
1981                                         BLUETOOTH_CALL_STATE_NONE) ||
1982                                 (call_status->call_status >=
1983                                         BLUETOOTH_CALL_STATE_ERROR)) {
1984                                 BT_ERR("Unknown Call state");
1985                                 return BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE;
1986                         }
1987                 }
1988         }
1989
1990         FN_END;
1991         return BLUETOOTH_TELEPHONY_ERROR_NONE;
1992 }
1993
1994 BT_EXPORT_API int bluetooth_telephony_set_call_status(void *call_list,
1995                                 unsigned int call_count)
1996 {
1997         int ret;
1998
1999         FN_START;
2000
2001         ret = bluetooth_telephony_call_swapped(call_list, call_count);
2002
2003         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE) {
2004                 BT_ERR("Failed = [%d]", ret);
2005                 return ret;
2006         }
2007
2008         telephony_info.call_count = call_count;
2009
2010         FN_END;
2011         return BLUETOOTH_TELEPHONY_ERROR_NONE;
2012 }
2013
2014 BT_EXPORT_API int bluetooth_telephony_indicate_outgoing_call(
2015                         const char *ph_number, unsigned int call_id,
2016                         unsigned int bt_audio)
2017 {
2018         GVariant *reply;
2019         GVariant *param;
2020         GError *err = NULL;
2021         const char *path = telephony_info.call_path;
2022         int ret = BLUETOOTH_TELEPHONY_ERROR_NONE;
2023
2024         FN_START;
2025
2026         BT_TELEPHONY_CHECK_INITIALIZED();
2027         BT_TELEPHONY_CHECK_ENABLED();
2028
2029         if (NULL == ph_number)
2030                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2031
2032         param = g_variant_new("(ssi)", path, ph_number, call_id);
2033         reply = __bluetooth_telephony_dbus_method_send(
2034                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2035                         "OutgoingCall", &err, param);
2036
2037         if (!reply) {
2038                 BT_ERR("Error returned in method call\n");
2039                 if (err) {
2040                         g_dbus_error_strip_remote_error(err);
2041                         ret = __bt_telephony_get_error(err->message);
2042                         g_error_free(err);
2043                         return ret;
2044                 }
2045                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2046         }
2047
2048         g_variant_unref(reply);
2049
2050         telephony_info.call_count++;
2051         BT_DBG(" ag_info.ag_call_count = [%d]", telephony_info.call_count);
2052
2053         if (bt_audio) {
2054                 if (!bluetooth_telephony_is_sco_connected()) {
2055                         ret = bluetooth_telephony_audio_open();
2056                         if (ret != 0)
2057                                 BT_ERR(" Audio connection Failed = %d", ret);
2058                 }
2059         }
2060
2061         FN_END;
2062         return ret;
2063 }
2064
2065 BT_EXPORT_API int bluetooth_telephony_indicate_incoming_call(
2066                 const char *ph_number, unsigned int call_id)
2067 {
2068         GVariant *reply;
2069         GVariant *param;
2070         GError *err = NULL;
2071         const char *path = telephony_info.call_path;
2072         int ret;
2073
2074         FN_START;
2075
2076         BT_TELEPHONY_CHECK_INITIALIZED();
2077         BT_TELEPHONY_CHECK_ENABLED();
2078
2079         if (NULL == ph_number)
2080                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2081
2082         param = g_variant_new("(ssi)", path, ph_number, call_id);
2083         reply = __bluetooth_telephony_dbus_method_send(
2084                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2085                         "IncomingCall", &err, param);
2086
2087         if (!reply) {
2088                 BT_ERR("Error returned in method call\n");
2089                 if (err) {
2090                         g_dbus_error_strip_remote_error(err);
2091                         ret = __bt_telephony_get_error(err->message);
2092                         g_error_free(err);
2093                         return ret;
2094                 }
2095                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2096         }
2097
2098         g_variant_unref(reply);
2099         telephony_info.call_count++;
2100         BT_DBG("telephony_info.call_count = [%d]", telephony_info.call_count);
2101         FN_END;
2102         return BLUETOOTH_TELEPHONY_ERROR_NONE;
2103 }
2104
2105 BT_EXPORT_API int bluetooth_telephony_set_speaker_gain(
2106                                                 unsigned short speaker_gain)
2107 {
2108         GVariant *reply;
2109         GVariant *param;
2110         GError *err = NULL;
2111         int ret;
2112
2113         FN_START;
2114
2115         BT_TELEPHONY_CHECK_INITIALIZED();
2116         BT_TELEPHONY_CHECK_ENABLED();
2117
2118         BT_DBG("set speaker_gain= [%d]", speaker_gain);
2119
2120         param = g_variant_new("(q)", speaker_gain);
2121         reply = __bluetooth_telephony_dbus_method_send(
2122                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2123                         "SetSpeakerGain", &err, param);
2124
2125         if (!reply) {
2126                 BT_ERR("Error returned in method call\n");
2127                 if (err) {
2128                         g_dbus_error_strip_remote_error(err);
2129                         ret = __bt_telephony_get_error(err->message);
2130                         g_error_free(err);
2131                         return ret;
2132                 }
2133                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2134         }
2135
2136         g_variant_unref(reply);
2137         FN_END;
2138         return BLUETOOTH_TELEPHONY_ERROR_NONE;
2139 }
2140
2141 BT_EXPORT_API int bluetooth_telephony_get_headset_volume(
2142                                                 unsigned int *speaker_gain)
2143 {
2144         GVariant *reply;
2145         GError *err = NULL;
2146         int ret;
2147         guint16 gain;
2148
2149         FN_START;
2150
2151         BT_TELEPHONY_CHECK_INITIALIZED();
2152         BT_TELEPHONY_CHECK_ENABLED();
2153
2154         reply = __bluetooth_telephony_dbus_method_send(
2155                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2156                         "GetSpeakerGain", &err, NULL);
2157
2158         if (!reply) {
2159                 BT_ERR("Error returned in method call\n");
2160                 if (err) {
2161                         g_dbus_error_strip_remote_error(err);
2162                         ret = __bt_telephony_get_error(err->message);
2163                         g_error_free(err);
2164                         return ret;
2165                 }
2166                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2167         }
2168         g_variant_get(reply, "(q)", &gain);
2169         *speaker_gain = gain;
2170         BT_DBG("Get speaker_gain= [%d]", *speaker_gain);
2171
2172         g_variant_unref(reply);
2173
2174         FN_END;
2175         return BLUETOOTH_TELEPHONY_ERROR_NONE;
2176 }
2177
2178 BT_EXPORT_API int bluetooth_telephony_is_connected(gboolean *ag_connected)
2179 {
2180         GVariant *reply;
2181         GError *err = NULL;
2182         int ret;
2183         gboolean ag_connected_from_bt_agent;
2184
2185         BT_CHECK_ENABLED(return);
2186
2187         reply = __bluetooth_telephony_dbus_method_send(
2188                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2189                         "IsConnected", &err, NULL);
2190
2191         if (!reply) {
2192                 BT_ERR("Error returned in method call\n");
2193                 if (err) {
2194                         g_dbus_error_strip_remote_error(err);
2195                         ret = __bt_telephony_get_error(err->message);
2196                         g_error_free(err);
2197                         return ret;
2198                 }
2199                 return BLUETOOTH_ERROR_INTERNAL;
2200         }
2201         g_variant_get(reply, "(b)", &ag_connected_from_bt_agent);
2202         *ag_connected = ag_connected_from_bt_agent;
2203
2204         BT_DBG("Conn Status: %s", *ag_connected ? "Connected" : "Disconnected");
2205
2206         g_variant_unref(reply);
2207
2208         return BLUETOOTH_ERROR_NONE;
2209 }
2210
2211 BT_EXPORT_API int bluetooth_telephony_set_active_headset(const char *remote_addr)
2212 {
2213 #ifdef TIZEN_BT_DUAL_HEADSET_CONNECT
2214         GVariant *reply;
2215         GVariant *param;
2216         GError *err = NULL;
2217         int ret;
2218
2219         BT_CHECK_ENABLED(return);
2220
2221         if (NULL == remote_addr)
2222                 return BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM;
2223
2224         param = g_variant_new("(s)", remote_addr);
2225         reply = __bluetooth_telephony_dbus_method_send(
2226                         HFP_AGENT_PATH, HFP_AGENT_INTERFACE,
2227                         "SwapHeadset", &err, param);
2228
2229         if (!reply) {
2230                 BT_ERR("Error returned in method call\n");
2231                 if (err) {
2232                         g_dbus_error_strip_remote_error(err);
2233                         ret = __bt_telephony_get_error(err->message);
2234                         g_error_free(err);
2235                         return ret;
2236                 }
2237                 return BLUETOOTH_TELEPHONY_ERROR_INTERNAL;
2238         }
2239
2240         g_variant_unref(reply);
2241         return BLUETOOTH_TELEPHONY_ERROR_NONE;
2242 #else
2243         return BLUETOOTH_ERROR_NOT_SUPPORT;
2244 #endif
2245 }
2246
2247 static void __bt_telephony_adapter_filter(GDBusConnection *connection,
2248                                         const gchar *sender_name,
2249                                         const gchar *object_path,
2250                                         const gchar *interface_name,
2251                                         const gchar *signal_name,
2252                                         GVariant *parameters,
2253                                         gpointer user_data)
2254 {
2255         FN_START;
2256         int ret;
2257         char *path = NULL;
2258
2259         if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2260                 GVariant *optional_param = NULL;
2261
2262                 g_variant_get(parameters, "(&o@a{sa{sv}})",
2263                                                         &path, &optional_param);
2264
2265                 if (!path) {
2266                         BT_ERR("Invalid adapter path");
2267                         return;
2268                 }
2269
2270                 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
2271                         if (__bt_telephony_get_src_addr(optional_param))
2272                                 BT_ERR("Fail to get the local adapter address");
2273
2274                         ret = __bluetooth_telephony_register();
2275                         if (ret != BLUETOOTH_TELEPHONY_ERROR_NONE)
2276                                 BT_ERR("__bluetooth_telephony_register failed");
2277                 }
2278
2279                 g_variant_unref(optional_param);
2280         }
2281
2282         FN_END;
2283 }
2284
2285 static int __bt_telephony_get_src_addr(GVariant *value)
2286 {
2287         GVariantIter iter;
2288         GVariant *param = NULL;
2289         FN_START;
2290
2291         /* signature a{sa{sv}} */
2292         g_variant_iter_init(&iter, value);
2293         while ((param = g_variant_iter_next_value(&iter))) {
2294                 char *interface_name;
2295                 GVariant *interface_var = NULL;
2296                 GVariant *param_inner = NULL;
2297
2298                 g_variant_get(param, "{&s*}", &interface_name, &interface_var);
2299                 g_variant_unref(param);
2300
2301                 BT_DBG("interface_name: %s", interface_name);
2302                 /* format of interface_var: a{sv}*/
2303                 if (strcasecmp(interface_name, BLUEZ_ADAPTER_INTERFACE) == 0) {
2304                         GVariantIter iter_inner;
2305
2306                         g_variant_iter_init(&iter_inner, interface_var);
2307                         while ((param_inner = g_variant_iter_next_value(&iter_inner))) {
2308                                 char *property_name;
2309                                 GVariant *property_var;
2310
2311                                 g_variant_get(param_inner, "{&sv}",
2312                                                 &property_name,
2313                                                 &property_var);
2314                                 g_variant_unref(param_inner);
2315
2316                                 if (strcasecmp(property_name, "Address") == 0) {
2317                                         const gchar *bd_addr;
2318
2319                                         bd_addr = g_variant_get_string(
2320                                                                 property_var,
2321                                                                 NULL);
2322                                         src_addr = g_strdup(bd_addr);
2323                                         BT_DBG("Address: %s", src_addr);
2324
2325                                         g_variant_unref(interface_var);
2326                                         g_variant_unref(property_var);
2327                                         goto done;
2328                                 }
2329                                 g_variant_unref(property_var);
2330                         }
2331                 }
2332                 g_variant_unref(interface_var);
2333         }
2334
2335 done:
2336         return BLUETOOTH_ERROR_NONE;
2337 }