Modify the segmentation fault for connecting HDP
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-hdp.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 <sys/types.h>
19 #include <sys/socket.h>
20 #include <gio/gio.h>
21 #include <glib.h>
22 #include <string.h>
23 #include <gio/gunixfdlist.h>
24
25 #include "bluetooth-api.h"
26 #include "bt-common.h"
27 #include "bt-internal-types.h"
28
29 #define HDP_BUFFER_SIZE 1024
30 #define BLUEZ_HDP_MANAGER_INTERFACE  "org.bluez.HealthManager1"
31 #define BLUEZ_HDP_DEVICE_INTERFACE  "org.bluez.HealthDevice1"
32 #define BLUEZ_HDP_CHANNEL_INTERFACE  "org.bluez.HealthChannel1"
33
34 typedef struct {
35         char *obj_channel_path;
36         int fd;
37         guint watch_id;
38 } hdp_obj_info_t;
39
40 typedef struct {
41         void *app_handle;
42         GSList *obj_info;
43 } hdp_app_list_t;
44
45 /* Variable for privilege, only for write API,
46   before we should reduce time to bt-service dbus calling
47   -1 : Don't have a permission to access API
48   0 : Initial value, not yet check
49   1 : Have a permission to access API
50 */
51 static int privilege_token;
52
53
54 /**********************************************************************
55 *               Static Functions declaration                            *
56 ***********************************************************************/
57 static int __bt_hdp_internal_create_application(unsigned int data_type,
58                                                 int role,
59                                                 bt_hdp_qos_type_t channel_type,
60                                                 char **app_handle);
61
62 static void __bt_hdp_internal_event_filter(GDBusConnection *connection,
63                                         const gchar *sender_name,
64                                         const gchar *object_path,
65                                         const gchar *interface_name,
66                                         const gchar *signal_name,
67                                         GVariant *parameters,
68                                         gpointer user_data);
69
70 static void __bt_hdp_internal_handle_connect(GVariant *parameters);
71
72 static void __bt_hdp_internal_handle_disconnect(GVariant *parameters,
73                                 const gchar *object_path);
74
75 static void __bt_hdp_internal_handle_property_changed(GVariant *parameters);
76
77 static int __bt_hdp_internal_add_filter(void);
78
79 static int __bt_hdp_internal_acquire_fd(const char *path);
80
81 static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path);
82
83 static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
84                                                 GIOCondition cond,
85                                                 gpointer data);
86
87 static int __bt_hdp_internal_destroy_application(const char *app_handle);
88
89 static void __bt_hdp_internal_remove_filter(void);
90
91 static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle);
92
93 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd);
94
95 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path);
96
97 /*Global Variables*/
98 static GDBusConnection *g_hdp_dus_conn;
99
100 static GSList *g_app_list = NULL;
101
102 /**********************************************************************
103 *                       Health device APIs (HDP)                        *
104 ***********************************************************************/
105
106 BT_EXPORT_API int bluetooth_hdp_activate(unsigned short data_type,
107                                         bt_hdp_role_type_t role,
108                                         bt_hdp_qos_type_t channel_type,
109                                         char **app_handle)
110 {
111         int result = BLUETOOTH_ERROR_NONE;
112
113         BT_DBG("+");
114
115         BT_CHECK_ENABLED(return);
116
117         /*For source role is mandatory */
118         if (role == HDP_ROLE_SOURCE && channel_type == HDP_QOS_ANY) {
119                 BT_ERR("For source, type is mandatory - Reliable/Streaming");
120                 return BLUETOOTH_ERROR_INVALID_PARAM;
121         }
122
123         result = __bt_hdp_internal_create_application(data_type, role,
124                                                 channel_type, app_handle);
125
126         return result;
127 }
128
129 static void __bt_hdp_obj_info_free(hdp_obj_info_t *info)
130 {
131         if (info) {
132                 g_source_remove(info->watch_id);
133                 close(info->fd);
134                 g_free(info->obj_channel_path);
135                 g_free(info);
136         }
137 }
138
139 static int __bt_hdp_internal_create_application(unsigned int data_type,
140                                         int role,
141                                         bt_hdp_qos_type_t channel_type,
142                                         char **app_handle)
143 {
144         GDBusProxy *proxy = NULL;
145         GVariant *reply = NULL;
146         GVariantBuilder *builder;
147         const char *svalue;
148         const char *key_type;
149         char *app_path;
150         hdp_app_list_t *list;
151         GError *err = NULL;
152         guint16 value;
153         GDBusConnection *conn;
154         int ret = BLUETOOTH_ERROR_NONE;
155
156         BT_DBG("+");
157
158         conn = _bt_gdbus_get_system_gconn();
159         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
160
161         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
162                                         NULL,
163                                         BT_BLUEZ_NAME,
164                                         "/org/bluez",
165                                         BLUEZ_HDP_MANAGER_INTERFACE,
166                                         NULL, &err);
167
168         if (!proxy) {
169                 BT_ERR("Unable to create proxy: %s", err->message);
170                 g_clear_error(&err);
171                 return BLUETOOTH_ERROR_INTERNAL;
172         }
173
174         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
175
176         key_type = "DataType";
177         value = (guint16) data_type;
178         g_variant_builder_add(builder, "{sv}",
179                         key_type, g_variant_new("q",
180                                 value));
181
182         key_type = "Role";
183         /*0-Source,1-Sink*/
184         svalue = (role == HDP_ROLE_SINK) ? "Sink" : "Source";
185         g_variant_builder_add(builder, "{sv}",
186                         key_type, g_variant_new("s",
187                                 svalue));
188
189         key_type = "Description";
190         svalue = "Health Device";
191         g_variant_builder_add(builder, "{sv}",
192                         key_type, g_variant_new("s",
193                                 svalue));
194
195         if (role == HDP_ROLE_SOURCE) {
196                 key_type = "ChannelType";
197                 if (channel_type == HDP_QOS_RELIABLE)
198                         svalue = "reliable";
199                 else if (channel_type == HDP_QOS_STREAMING)
200                         svalue = "streaming";
201
202                 g_variant_builder_add(builder, "{sv}",
203                         key_type, g_variant_new("s",
204                                 svalue));
205         }
206
207         reply = g_dbus_proxy_call_sync(proxy, "CreateApplication",
208                                         g_variant_new("(a{sv})", builder),
209                                         G_DBUS_CALL_FLAGS_NONE, -1,
210                                         NULL, &err);
211
212         g_variant_builder_unref(builder);
213         g_object_unref(proxy);
214
215         if (!reply) {
216                 BT_ERR(" HDP:dbus Can't create application");
217                 if (err) {
218                         BT_ERR("%s", err->message);
219                         if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
220                                 ret = BLUETOOTH_ERROR_ACCESS_DENIED;
221                         else
222                                 ret = BLUETOOTH_ERROR_INTERNAL;
223                         g_clear_error(&err);
224                 }
225                 return ret;
226         }
227
228         g_variant_get(reply, "(&o)", &app_path);
229         BT_DBG("Created health application: %s", (char *)app_path);
230
231         ret = __bt_hdp_internal_add_filter();
232
233         if (ret != BLUETOOTH_ERROR_NONE) {
234                 BT_ERR("Funtion failed");
235                 return ret;
236         }
237
238         list = g_new0(hdp_app_list_t, 1);
239         list->app_handle = (void *)g_strdup(app_path);
240         *app_handle = list->app_handle;
241
242         g_app_list = g_slist_append(g_app_list, list);
243
244         g_variant_unref(reply);
245         return BLUETOOTH_ERROR_NONE;
246 }
247
248 static int __bt_hdp_add_filter_subscribe_signal(GDBusConnection *conn,
249                 gboolean subscribe)
250 {
251         static guint subs_add_filter_id = 0;
252
253         if (conn == NULL)
254                 return BLUETOOTH_ERROR_INVALID_PARAM;
255
256         if (subscribe) {
257                 if (subs_add_filter_id == 0) {
258                         subs_add_filter_id = g_dbus_connection_signal_subscribe(
259                                 conn, NULL, BLUEZ_HDP_DEVICE_INTERFACE,
260                                 NULL, NULL, NULL, 0,
261                                 __bt_hdp_internal_event_filter, NULL, NULL);
262                 }
263         } else {
264                 if (subs_add_filter_id > 0) {
265                         g_dbus_connection_signal_unsubscribe(conn,
266                                         subs_add_filter_id);
267                         subs_add_filter_id = 0;
268                 }
269         }
270         return BLUETOOTH_ERROR_NONE;
271 }
272
273 static int __bt_hdp_internal_add_filter(void)
274 {
275         BT_DBG("+");
276
277         /*Single process only one signal registration is required */
278         if (g_hdp_dus_conn) {
279                 BT_ERR("g_hdp_dus_conn already exist");
280                 return BLUETOOTH_ERROR_NONE;
281         }
282
283         g_hdp_dus_conn = _bt_gdbus_get_system_gconn();
284         retv_if(g_hdp_dus_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
285
286
287         return __bt_hdp_add_filter_subscribe_signal(g_hdp_dus_conn, TRUE);
288
289         BT_DBG("-\n");
290 }
291
292 static void __bt_hdp_internal_event_filter(GDBusConnection *connection,
293                                         const gchar *sender_name,
294                                         const gchar *object_path,
295                                         const gchar *interface_name,
296                                         const gchar *signal_name,
297                                         GVariant *parameters,
298                                         gpointer user_data)
299 {
300         BT_DBG("Path = %s\n", object_path);
301         if (object_path == NULL || g_strcmp0(object_path, "/") == 0)
302                 return;
303
304         if (signal_name == NULL)
305                 return;
306
307         if (strcasecmp(signal_name, "ChannelConnected") == 0) 
308                 __bt_hdp_internal_handle_connect(parameters);
309
310         else if (strcasecmp(signal_name, "ChannelDeleted") == 0)
311                 __bt_hdp_internal_handle_disconnect(parameters, object_path);
312
313         else if (strcasecmp(signal_name, "PropertyChanged") == 0)
314                 __bt_hdp_internal_handle_property_changed(parameters);
315
316         return;
317 }
318
319 static void __bt_hdp_internal_handle_connect(GVariant *parameters)
320 {
321         BT_DBG("+");
322         const char *obj_channel_path;
323         bt_user_info_t *user_info;
324         int ret;
325
326         BT_INFO("*********Signal - ChannelConnected******\n\n");
327         g_variant_get(parameters, "(&o)", &obj_channel_path);
328
329         BT_INFO("Channel connected, Path = %s", obj_channel_path);
330
331         user_info = _bt_get_user_data(BT_COMMON);
332         if (user_info == NULL || user_info->cb == NULL)
333                 return;
334
335         ret = __bt_hdp_internal_acquire_fd(obj_channel_path);
336         if (ret != BLUETOOTH_ERROR_NONE) {
337                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
338                                 BLUETOOTH_ERROR_CONNECTION_ERROR, NULL,
339                                 user_info->cb, user_info->user_data);
340         }
341         BT_DBG("-");
342 }
343
344 static void __bt_hdp_internal_handle_disconnect(GVariant *parameters,
345                                         const gchar *object_path)
346 {
347         const char *obj_channel_path;
348         char address[BT_ADDRESS_STRING_SIZE] = { 0, };
349         bluetooth_device_address_t device_addr = { {0} };
350         bt_hdp_disconnected_t dis_ind;
351         hdp_obj_info_t *info;
352         bt_user_info_t *user_info;
353
354         BT_INFO("+********Signal - ChannelDeleted ******\n\n");
355         BT_DBG("Path = %s", object_path);
356
357         g_variant_get(parameters, "(&o)", &obj_channel_path);
358
359         BT_INFO("Channel Deleted, Path = %s", obj_channel_path);
360
361         info = __bt_hdp_internal_gslist_obj_find_using_path(obj_channel_path);
362         if (!info) {
363                 BT_ERR("No obj info for ob_channel_path [%s]\n", obj_channel_path);
364                 return;
365         }
366
367         /*Since bluetoothd is not sending the ChannelDeleted signal */
368         _bt_convert_device_path_to_address(object_path, address);
369
370         _bt_convert_addr_string_to_type(device_addr.addr, address);
371
372         dis_ind.channel_id = info->fd;
373         dis_ind.device_address = device_addr;
374
375         user_info = _bt_get_user_data(BT_COMMON);
376
377         if (user_info->cb) {
378                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
379                                 BLUETOOTH_ERROR_NONE, &dis_ind,
380                                 user_info->cb, user_info->user_data);
381         }
382
383         BT_DBG(" Removed connection from list\n");
384
385         __bt_hdp_obj_info_free(info);
386
387 }
388
389 static void __bt_hdp_internal_handle_property_changed(GVariant *parameters)
390 {
391         char *property = NULL;
392         GVariant *value = NULL;
393         gsize len;
394         char *obj_main_channel_path = NULL;
395         GVariantIter *property_iter;
396
397         BT_DBG("+*******Signal - PropertyChanged*******\n");
398
399         g_variant_get (parameters, "(a{sv})", &property_iter);
400
401         while (g_variant_iter_loop(property_iter, "{sv}", &property, &value)) {
402                 if (g_strcmp0("MainChannel", property) == 0) {
403                         BT_INFO("Property MainChannel received");
404                         obj_main_channel_path = g_variant_dup_string (value, &len);
405                         BT_DBG("Main Channel  Path = %s", obj_main_channel_path);
406                         break;
407                 }
408         }
409         g_free(property);
410         g_variant_unref(value);
411         g_free(obj_main_channel_path);
412         BT_DBG("-*************\n");
413 }
414
415 static int __bt_hdp_internal_acquire_fd(const char *path)
416 {
417         char address[BT_ADDRESS_STRING_SIZE] = { 0, };
418         bluetooth_device_address_t device_addr = { {0} };
419         const char *property;
420         GVariant *value = NULL;
421         char *type_qos = NULL;
422         char *device = NULL;
423         char *app_handle = NULL;
424         hdp_app_list_t *list = NULL;
425         GDBusProxy *proxy = NULL;
426         GVariant *reply = NULL;
427         GDBusConnection *conn;
428         bt_hdp_connected_t conn_ind;
429         GError *err = NULL;
430         int fd;
431         bt_user_info_t *user_info;
432         char *dev_path;
433         GUnixFDList *out_fd_list;
434         int index;
435         GVariantIter *property_iter;
436         gsize len;
437
438         BT_DBG("+");
439
440         conn = _bt_gdbus_get_system_gconn();
441         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
442
443         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
444                                         NULL,
445                                         BT_BLUEZ_NAME,
446                                         path,
447                                         BLUEZ_HDP_CHANNEL_INTERFACE,
448                                         NULL, &err);
449
450         if (!proxy) {
451                 BT_ERR("Unable to create proxy: %s", err->message);
452                 g_clear_error(&err);
453                 return BLUETOOTH_ERROR_INTERNAL;
454         }
455
456         reply = g_dbus_proxy_call_with_unix_fd_list_sync (proxy,
457                                                    "Acquire",
458                                                    NULL,
459                                                    G_DBUS_CALL_FLAGS_NONE,
460                                                    -1,
461                                                    NULL,
462                                                    &out_fd_list,
463                                                    NULL,
464                                                   &err);
465
466         g_object_unref(proxy);
467
468         if (!reply) {
469                 BT_ERR(" HDP:****** dbus Can't create application ****");
470
471                 if (err) {
472                         BT_ERR("%s", err->message);;
473                         g_clear_error(&err);
474                 }
475
476                 return BLUETOOTH_ERROR_INTERNAL;
477         }
478
479         g_variant_get (reply, "(h)", &index);
480         fd = g_unix_fd_list_get(out_fd_list, index, NULL);
481
482         g_variant_unref(reply);
483
484         BT_DBG("File Descriptor = %d, Dev_path = %s \n", fd, path);
485
486         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
487                                         NULL,
488                                         BT_BLUEZ_NAME,
489                                         path,
490                                         BT_PROPERTIES_INTERFACE,
491                                         NULL, &err);
492
493         if (!proxy) {
494                 BT_ERR("Unable to create proxy: %s", err->message);
495                 g_clear_error(&err);
496                 return BLUETOOTH_ERROR_INTERNAL;
497         }
498
499         dev_path = g_strdup(BLUEZ_HDP_CHANNEL_INTERFACE);
500
501         reply = g_dbus_proxy_call_sync(proxy, "GetAll",
502                                 g_variant_new("(s)", dev_path),
503                                 G_DBUS_CALL_FLAGS_NONE,
504                                 -1,
505                                 NULL,
506                                 &err);
507
508         g_free(dev_path);
509         g_object_unref(proxy);
510
511         if (!reply) {
512                 BT_ERR(" HDP:dbus Can't get the reply");
513
514                 if (err) {
515                         BT_ERR("%s", err->message);;
516                         g_clear_error(&err);
517                 }
518
519                 return BLUETOOTH_ERROR_INTERNAL;
520         }
521
522         g_variant_get (reply, "(a{sv})", &property_iter);
523
524         while (g_variant_iter_loop(property_iter, "{sv}", &property, &value)) {
525                 BT_DBG("String received = %s\n", property);
526
527                 if (g_strcmp0("Type", property) == 0) {
528                         type_qos = g_variant_dup_string (value, &len);
529                 } else if (g_strcmp0("Device", property) == 0) {
530                         device = g_variant_dup_string (value, &len);
531                 } else if (g_strcmp0("Application", property) == 0) {
532                         app_handle = g_variant_dup_string (value, &len);
533                 }
534         }
535
536         g_variant_unref(reply);
537         BT_DBG("QOS = %s, Device = %s, Apphandler = %s",
538                         type_qos, device, app_handle);
539
540         if (NULL == type_qos || NULL == app_handle) {
541                 BT_ERR("Pasing failed\n");
542                 goto error;
543         }
544
545         list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
546
547         /*Only process register with app handle receive the Connected event */
548         if (NULL == list) {
549                 BT_ERR("**** Could not locate the list for %s*****\n", app_handle);
550                 goto error;
551         }
552
553         hdp_obj_info_t *info = g_new0(hdp_obj_info_t, 1);
554         info->fd = fd;
555         info->obj_channel_path = g_strdup(path);
556         info->watch_id = __bt_hdp_internal_watch_fd(fd, info->obj_channel_path);
557         list->obj_info = g_slist_append(list->obj_info, info);
558
559         _bt_convert_device_path_to_address(path, address);
560
561         _bt_convert_addr_string_to_type(device_addr.addr, address);
562
563         conn_ind.app_handle = app_handle;
564         conn_ind.channel_id = fd;
565         conn_ind.device_address = device_addr;
566         conn_ind.type = (g_strcmp0(type_qos, "Reliable") == 0) ?
567                         HDP_QOS_RELIABLE : HDP_QOS_STREAMING;
568
569         BT_DBG("Going to give callback\n");
570
571         user_info = _bt_get_user_data(BT_COMMON);
572
573         if (user_info->cb) {
574                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
575                                 BLUETOOTH_ERROR_NONE, &conn_ind,
576                                 user_info->cb, user_info->user_data);
577         }
578
579         BT_DBG("Updated fd in the list*\n");
580         BT_DBG("-\n");
581
582         g_free(type_qos);
583         g_free(device);
584         g_free(app_handle);
585         return BLUETOOTH_ERROR_NONE;
586 error:
587         g_free(type_qos);
588         g_free(device);
589         g_free(app_handle);
590         return BLUETOOTH_ERROR_INTERNAL;
591 }
592
593 static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path)
594 {
595         GIOChannel *gio;
596         guint id;
597
598         BT_DBG("+");
599
600         gio = g_io_channel_unix_new(file_desc);
601
602         g_io_channel_set_close_on_unref(gio, TRUE);
603
604         id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
605                         __bt_hdp_internal_data_received, (void *)path);
606         BT_DBG("-");
607         return id;
608 }
609
610
611 static void __bt_hdp_internal_handle_disconnect_cb(int sk, const char *path)
612 {
613         char address[BT_ADDRESS_STRING_SIZE] = { 0, };
614         bluetooth_device_address_t device_addr = { {0} };
615         bt_hdp_disconnected_t dis_ind;
616         hdp_obj_info_t *info;
617         bt_user_info_t *user_info;
618
619         BT_INFO("******** Socket Error  ******\n");
620
621         info = __bt_hdp_internal_gslist_obj_find_using_path(path);
622         ret_if(info == NULL);
623
624         /*Since bluetoothd is not sending the ChannelDeleted signal */
625         _bt_convert_device_path_to_address(path, address);
626
627         _bt_convert_addr_string_to_type(device_addr.addr, address);
628
629         dis_ind.channel_id = sk;
630         dis_ind.device_address = device_addr;
631
632         user_info = _bt_get_user_data(BT_COMMON);
633
634         if (user_info->cb) {
635                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
636                                 BLUETOOTH_ERROR_NONE, &dis_ind,
637                                 user_info->cb, user_info->user_data);
638         }
639
640         BT_DBG(" Removed connection from list\n");
641
642         __bt_hdp_obj_info_free(info);
643 }
644
645 static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
646                                         GIOCondition cond, gpointer data)
647 {
648         char buff[HDP_BUFFER_SIZE] = { 0, };
649         int sk;
650         int act_read;
651         bt_hdp_data_ind_t data_ind = { 0, };
652         const char *path = (const char *)data;
653         bt_user_info_t *user_info;
654
655         BT_DBG("+");
656
657         sk = g_io_channel_unix_get_fd(gio);
658
659         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
660                 BT_DBG("GIOCondition %d.............path = %s\n", cond, path);
661                 g_io_channel_shutdown(gio, TRUE, NULL);
662                 g_io_channel_unref(gio);
663                  __bt_hdp_internal_handle_disconnect_cb(sk, path);
664                 return FALSE;
665         }
666
667         act_read = recv(sk, (void *)buff, sizeof(buff), 0);
668
669         if (act_read > 0) {
670                 BT_DBG("Received data of %d\n", act_read);
671         } else {
672                 BT_ERR("Read failed.....\n");
673                 return FALSE;
674         }
675
676         data_ind.channel_id = sk;
677         data_ind.buffer = buff;
678         data_ind.size = act_read;
679
680         user_info = _bt_get_user_data(BT_COMMON);
681
682         if (user_info->cb) {
683                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DATA_RECEIVED,
684                                 BLUETOOTH_ERROR_NONE, &data_ind,
685                                 user_info->cb, user_info->user_data);
686         }
687
688         BT_DBG("-\n");
689
690         return TRUE;
691 }
692
693 BT_EXPORT_API int bluetooth_hdp_deactivate(const char *app_handle)
694 {
695         BT_DBG("+");
696
697         BT_CHECK_ENABLED(return);
698         BT_CHECK_PARAMETER(app_handle, return);
699
700         return __bt_hdp_internal_destroy_application(app_handle);
701 }
702
703 static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle)
704 {
705         GSList *l;
706
707         retv_if(g_app_list == NULL, NULL);
708
709         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
710
711         for (l = g_app_list; l != NULL; l = l->next) {
712                 hdp_app_list_t *list = l->data;
713
714                 if (list) {
715                         if (0 == g_strcmp0((char *)list->app_handle,
716                                                 (char *)app_handle))
717                                 return list;
718                 }
719         }
720         return NULL;
721 }
722
723 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd)
724 {
725         GSList *l;
726         GSList *iter;
727
728         retv_if(g_app_list == NULL, NULL);
729
730         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
731
732         for (l = g_app_list; l != NULL; l = l->next) {
733                 hdp_app_list_t *list = l->data;
734                 if (!list)
735                         return NULL;
736
737                 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
738                         hdp_obj_info_t *info = iter->data;
739                         if (!info)
740                                 return NULL;
741
742                         if (fd == info->fd)
743                                 return info;
744                 }
745         }
746         return NULL;
747 }
748
749 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path)
750 {
751         GSList *l;
752         GSList *iter;
753         hdp_obj_info_t *info = NULL;
754
755         retv_if(g_app_list == NULL, NULL);
756
757         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
758         for (l = g_app_list; l != NULL; l = l->next) {
759                 hdp_app_list_t *list = l->data;
760                 if (!list)
761                         return NULL;
762
763                 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
764                          info = iter->data;
765                         if (!info)
766                                 return NULL;
767
768                         if (0 == g_strcmp0(info->obj_channel_path, obj_channel_path)) {
769                                 list->obj_info = g_slist_remove(list->obj_info, info);
770                                 return info;
771                         }
772                 }
773         }
774         return NULL;
775 }
776
777 static gboolean  __bt_hdp_internal_destroy_application_cb(gpointer data)
778 {
779         const char *app_handle;
780         hdp_app_list_t *list = NULL;
781         app_handle = (const char *)data;
782
783         BT_DBG("+");
784
785         list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
786         if (NULL == list) {
787                 BT_ERR("**** list not found for %s ******\n", app_handle);
788                 return FALSE;
789         }
790
791         g_app_list = g_slist_remove(g_app_list, list);
792
793         g_free(list->app_handle);
794         g_slist_foreach(list->obj_info, (GFunc)__bt_hdp_obj_info_free, NULL);
795         g_free(list);
796
797         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
798
799         if (0 == g_slist_length(g_app_list))
800                 __bt_hdp_internal_remove_filter();
801         BT_DBG("-");
802         return FALSE;
803 }
804
805 static int __bt_hdp_internal_destroy_application(const char *app_handle)
806 {
807         GDBusProxy *proxy = NULL;
808         GVariant *reply = NULL;
809         GError *err = NULL;
810         GDBusConnection *conn;
811         int result = BLUETOOTH_ERROR_NONE;
812
813         conn = _bt_gdbus_get_system_gconn();
814         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
815
816         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
817                                         NULL,
818                                         BT_BLUEZ_NAME,
819                                         "/org/bluez",
820                                         BLUEZ_HDP_MANAGER_INTERFACE,
821                                         NULL, &err);
822
823         if (!proxy) {
824                 BT_ERR("Unable to create proxy: %s", err->message);
825                 g_clear_error(&err);
826                 return BLUETOOTH_ERROR_INTERNAL;
827         }
828
829         reply = g_dbus_proxy_call_sync(proxy, "DestroyApplication",
830                                 g_variant_new("o", app_handle),
831                                 G_DBUS_CALL_FLAGS_NONE,
832                                 -1,
833                                 NULL,
834                                 &err);
835
836         g_object_unref(proxy);
837         if (!reply) {
838                 BT_ERR(" HDP:dbus Can't Destroy application");
839
840                 if (err) {
841                         BT_ERR("%s", err->message);
842                         if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
843                                 result  = BLUETOOTH_ERROR_ACCESS_DENIED;
844                         else
845                                 result  = BLUETOOTH_ERROR_INTERNAL;
846                         g_clear_error(&err);
847                 }
848                 return result ;
849         }
850
851         g_variant_unref(reply);
852
853         BT_DBG("Destroyed health application: %s", (char *)app_handle);
854
855         g_idle_add(__bt_hdp_internal_destroy_application_cb,
856                         (gpointer)app_handle);
857
858         return BLUETOOTH_ERROR_NONE;
859 }
860
861 static void __bt_hdp_internal_remove_filter(void)
862 {
863         BT_DBG("+");
864
865         ret_if(g_hdp_dus_conn == NULL);
866
867         __bt_hdp_add_filter_subscribe_signal(g_hdp_dus_conn, FALSE);
868
869         g_hdp_dus_conn = NULL;  /*should not unref here, bcz no ++reff */
870
871         BT_DBG("-");
872 }
873
874 BT_EXPORT_API int bluetooth_hdp_send_data(unsigned int channel_id,
875                                             const char *buffer,
876                                             unsigned int size)
877 {
878         int wbytes = 0;
879         int written = 0;
880         int result;
881
882         BT_DBG("+");
883
884         BT_CHECK_ENABLED(return);
885
886         if ((channel_id == 0) || (NULL == buffer) || (size == 0)) {
887                 BT_ERR("Invalid arguments..\n");
888                 return BLUETOOTH_ERROR_INVALID_PARAM;
889         }
890
891         switch (privilege_token) {
892         case 0:
893                 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_SEND_DATA);
894
895                 if (result == BLUETOOTH_ERROR_NONE) {
896                         privilege_token = 1; /* Have a permission */
897                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
898                         BT_ERR("Don't have a privilege to use this API");
899                         privilege_token = -1; /* Don't have a permission */
900                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
901                 } else {
902                         /* Just break - It is not related with permission error */
903                 }
904                 break;
905         case 1:
906                 /* Already have a privilege */
907                 break;
908         case -1:
909                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
910         default:
911                 /* Invalid privilge token value */
912                 return BLUETOOTH_ERROR_INTERNAL;
913         }
914
915         while (wbytes < size) {
916                 written = write(channel_id, buffer + wbytes, size - wbytes);
917                 if (written <= 0) {
918                         BT_ERR("write failed..\n");
919                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
920                 }
921                 wbytes += written;
922         }
923
924         return BLUETOOTH_ERROR_NONE;
925 }
926
927 static void __bt_hdp_connect_request_cb(GDBusProxy *hdp_proxy,
928                                 GAsyncResult *res, gpointer user_data)
929 {
930         GError *err = NULL;
931         char *obj_connect_path = NULL;
932         bt_hdp_connected_t *conn_ind = user_data;
933         bt_user_info_t *user_info;
934         GVariant *reply = NULL;
935
936         reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
937
938         g_object_unref(hdp_proxy);
939
940         if (!reply) {
941                 if (err) {
942                         BT_ERR("HDP connection  Dbus Call Error: %s\n", err->message);
943                         g_clear_error(&err);
944                 }
945
946                 user_info = _bt_get_user_data(BT_COMMON);
947
948                 if (user_info->cb) {
949                         _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
950                                         BLUETOOTH_ERROR_CONNECTION_ERROR, conn_ind,
951                                         user_info->cb, user_info->user_data);
952                 }
953         } else {
954                 g_variant_get(reply, "(&o)", &obj_connect_path);
955                 BT_DBG("Obj Path returned = %s\n", obj_connect_path);
956         }
957         g_free((void *)conn_ind->app_handle);
958         g_free(conn_ind);
959 }
960
961
962 BT_EXPORT_API int bluetooth_hdp_connect(const char *app_handle,
963                         bt_hdp_qos_type_t channel_type,
964                         const bluetooth_device_address_t *device_address)
965 {
966         GError *err = NULL;
967         GDBusConnection *conn = NULL;
968         GDBusProxy *hdp_proxy = NULL;
969         bt_hdp_connected_t *param;
970         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
971         char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
972         char *dev_path = NULL;
973         char *role;
974
975         BT_DBG("+");
976
977         BT_CHECK_ENABLED(return);
978         BT_CHECK_PARAMETER(app_handle, return);
979         BT_CHECK_PARAMETER(device_address, return);
980
981         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_CONNECT)
982              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
983                 BT_ERR("Don't have a privilege to use this API");
984                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
985         }
986
987         if (channel_type == HDP_QOS_RELIABLE) {
988                 role = "Reliable";
989         } else if (channel_type == HDP_QOS_STREAMING) {
990                 role = "Streaming";
991         } else if (channel_type == HDP_QOS_ANY) {
992                 role = "Any";
993         } else {
994                 BT_ERR("Invalid channel_type %d", channel_type);
995                 return BLUETOOTH_ERROR_ACCESS_DENIED;
996         }
997
998         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
999
1000         if (err) {
1001                 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1002                 g_clear_error(&err);
1003                 return BLUETOOTH_ERROR_INTERNAL;
1004         }
1005
1006         /* If the adapter path is wrong, we can think the BT is not enabled. */
1007         if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1008                                         default_adapter_path) < 0) {
1009                 BT_ERR("Could not get adapter path\n");
1010                 g_object_unref(conn);
1011                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1012         }
1013
1014         _bt_convert_addr_type_to_string(address,
1015                                 (unsigned char *)device_address->addr);
1016
1017         BT_DBG("create conection to %s", address);
1018
1019         dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1020
1021         if (dev_path == NULL) {
1022                 g_object_unref(conn);
1023                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1024         }
1025
1026         g_strdelimit(dev_path, ":", '_');
1027
1028         BT_DBG("path: %s", dev_path);
1029
1030         hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1031                                                 NULL, BT_BLUEZ_NAME,
1032                                                 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1033                                                 NULL, NULL);
1034         g_object_unref(conn);
1035
1036         if (hdp_proxy == NULL) {
1037                 BT_ERR("Failed to get the HDP server proxy\n");
1038                 g_free(dev_path);
1039                 return BLUETOOTH_ERROR_NOT_PAIRED;
1040         }
1041
1042         BT_DBG("app path %s\n", app_handle);
1043
1044         param = g_new0(bt_hdp_connected_t, 1);
1045         param->app_handle = g_strdup(app_handle);
1046         memcpy(&param->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1047         param->type = channel_type;
1048
1049         g_dbus_proxy_call(hdp_proxy, "CreateChannel",
1050                                 g_variant_new("(os)", app_handle, role),
1051                                 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1052                                 (GAsyncReadyCallback)__bt_hdp_connect_request_cb,
1053                                 param);
1054
1055         g_free(dev_path);
1056
1057         return BLUETOOTH_ERROR_NONE;
1058 }
1059
1060 static void __bt_hdp_disconnect_request_cb(GDBusProxy *hdp_proxy,
1061                         GAsyncResult *res, gpointer user_data)
1062 {
1063         GError *err = NULL;
1064         bt_hdp_disconnected_t *disconn_ind = user_data;
1065         bt_user_info_t *user_info;
1066         GVariant *reply = NULL;
1067
1068         reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
1069         g_object_unref(hdp_proxy);
1070
1071         user_info = _bt_get_user_data(BT_COMMON);
1072         if (user_info == NULL || user_info->cb == NULL) {
1073                 g_free(disconn_ind);
1074                 if (err) {
1075                                 g_clear_error(&err);
1076                         return;
1077                 }
1078                 g_variant_unref(reply);
1079                 return;
1080         }
1081
1082         if (!reply) {
1083                 if (err) {
1084                         BT_ERR("HDP disconnection Dbus Call Error: %s\n",
1085                                                         err->message);
1086                         g_clear_error(&err);
1087                 }
1088
1089                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1090                                 BLUETOOTH_ERROR_CONNECTION_ERROR, disconn_ind,
1091                                 user_info->cb, user_info->user_data);
1092         } else {
1093                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1094                                 BLUETOOTH_ERROR_NONE, disconn_ind,
1095                                 user_info->cb, user_info->user_data);
1096                 BT_INFO("HDP disconnection Dbus Call is done\n");
1097                 g_variant_unref(reply);
1098         }
1099
1100         g_free(disconn_ind);
1101 }
1102
1103 BT_EXPORT_API int bluetooth_hdp_disconnect(unsigned int channel_id,
1104                         const bluetooth_device_address_t *device_address)
1105 {
1106         GError *err = NULL;
1107         GDBusConnection *conn = NULL;
1108         GDBusProxy *hdp_proxy = NULL;
1109         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1110         char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
1111         char *dev_path = NULL;
1112         bt_hdp_disconnected_t *param;
1113
1114         BT_DBG("+\n");
1115
1116         BT_CHECK_ENABLED(return);
1117         BT_CHECK_PARAMETER(device_address, return);
1118
1119         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_DISCONNECT)
1120              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1121                 BT_ERR("Don't have a privilege to use this API");
1122                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1123         }
1124
1125         hdp_obj_info_t *info =
1126                 __bt_hdp_internal_gslist_obj_find_using_fd(channel_id);
1127         if (NULL == info) {
1128                 BT_ERR("*** Could not locate the list for %d*****\n",
1129                                                         channel_id);
1130                 return BLUETOOTH_ERROR_INVALID_PARAM;
1131         }
1132
1133         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1134
1135         if (err) {
1136                 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1137                 g_clear_error(&err);
1138                 return BLUETOOTH_ERROR_INTERNAL;
1139         }
1140
1141         /* If the adapter path is wrong, we can think the BT is not enabled. */
1142         if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1143                                         default_adapter_path) < 0) {
1144                 BT_ERR("Could not get adapter path\n");
1145                 g_object_unref(conn);
1146                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1147         }
1148
1149         _bt_convert_addr_type_to_string(address,
1150                                 (unsigned char *)device_address->addr);
1151
1152         BT_DBG("create conection to  %s\n", address);
1153
1154         dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1155
1156         if (dev_path == NULL) {
1157                 g_object_unref(conn);
1158                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1159         }
1160
1161         g_strdelimit(dev_path, ":", '_');
1162
1163         BT_DBG("path  %s\n", dev_path);
1164
1165         hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1166                                                 NULL, BT_BLUEZ_NAME,
1167                                                 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1168                                                 NULL, NULL);
1169
1170         g_object_unref(conn);
1171
1172         if (hdp_proxy == NULL) {
1173                 BT_ERR("Failed to get the HDP proxy\n");
1174                 g_free(dev_path);
1175                 return BLUETOOTH_ERROR_NOT_PAIRED;
1176         }
1177
1178         param = g_new0(bt_hdp_disconnected_t, 1);
1179         param->channel_id = channel_id;
1180         memcpy(&param->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1181
1182         g_dbus_proxy_call(hdp_proxy, "DestroyChannel",
1183                                 g_variant_new("o", info->obj_channel_path),
1184                                 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1185                                 (GAsyncReadyCallback)__bt_hdp_disconnect_request_cb,
1186                                 param);
1187
1188         g_free(dev_path);
1189         g_free(param);
1190         g_object_unref(hdp_proxy);
1191
1192         return BLUETOOTH_ERROR_NONE;
1193
1194 }