[BT-Frwk] Fixed build warnings.
[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         const char *obj_channel_path;
322         bt_user_info_t *user_info;
323         int ret;
324
325         BT_INFO("+********Signal - ChannelConnected******\n\n");
326         g_variant_get(parameters, "(&o)", &obj_channel_path);
327
328         BT_INFO("Channel connected, Path = %s", obj_channel_path);
329
330         user_info = _bt_get_user_data(BT_COMMON);
331         if (user_info == NULL || user_info->cb == NULL)
332                 return;
333
334         ret = __bt_hdp_internal_acquire_fd(obj_channel_path);
335         if (ret != BLUETOOTH_ERROR_NONE) {
336                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
337                                 BLUETOOTH_ERROR_CONNECTION_ERROR, NULL,
338                                 user_info->cb, user_info->user_data);
339         } else {
340                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
341                                 BLUETOOTH_ERROR_NONE, NULL,
342                                 user_info->cb, user_info->user_data);
343         }
344
345         BT_DBG("-");
346 }
347
348 static void __bt_hdp_internal_handle_disconnect(GVariant *parameters,
349                                         const gchar *object_path)
350 {
351         const char *obj_channel_path;
352         char address[BT_ADDRESS_STRING_SIZE] = { 0, };
353         bluetooth_device_address_t device_addr = { {0} };
354         bt_hdp_disconnected_t dis_ind;
355         hdp_obj_info_t *info;
356         bt_user_info_t *user_info;
357
358         BT_INFO("+********Signal - ChannelDeleted ******\n\n");
359         BT_DBG("Path = %s", object_path);
360
361         g_variant_get(parameters, "(&o)", &obj_channel_path);
362
363         BT_INFO("Channel Deleted, Path = %s", obj_channel_path);
364
365         info = __bt_hdp_internal_gslist_obj_find_using_path(obj_channel_path);
366         if (!info) {
367                 BT_ERR("No obj info for ob_channel_path [%s]\n", obj_channel_path);
368                 return;
369         }
370
371         /*Since bluetoothd is not sending the ChannelDeleted signal */
372         _bt_convert_device_path_to_address(object_path, address);
373
374         _bt_convert_addr_string_to_type(device_addr.addr, address);
375
376         dis_ind.channel_id = info->fd;
377         dis_ind.device_address = device_addr;
378
379         user_info = _bt_get_user_data(BT_COMMON);
380
381         if (user_info->cb) {
382                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
383                                 BLUETOOTH_ERROR_NONE, &dis_ind,
384                                 user_info->cb, user_info->user_data);
385         }
386
387         BT_DBG(" Removed connection from list\n");
388
389         __bt_hdp_obj_info_free(info);
390
391 }
392
393 static void __bt_hdp_internal_handle_property_changed(GVariant *parameters)
394 {
395         char *property = NULL;
396         GVariant *value = NULL;
397         gsize len;
398         char *obj_main_channel_path = NULL;
399         GVariantIter *property_iter;
400
401         BT_DBG("+*******Signal - PropertyChanged*******\n");
402
403         g_variant_get (parameters, "(a{sv})", &property_iter);
404
405         while (g_variant_iter_loop(property_iter, "{sv}", &property, &value)) {
406                 if (g_strcmp0("MainChannel", property) == 0) {
407                         BT_INFO("Property MainChannel received");
408                         obj_main_channel_path = g_variant_dup_string (value, &len);
409                         BT_DBG("Main Channel  Path = %s", obj_main_channel_path);
410                         break;
411                 }
412         }
413         g_free(property);
414         g_variant_unref(value);
415         g_free(obj_main_channel_path);
416         BT_DBG("-*************\n");
417 }
418
419 static int __bt_hdp_internal_acquire_fd(const char *path)
420 {
421         char address[BT_ADDRESS_STRING_SIZE] = { 0, };
422         bluetooth_device_address_t device_addr = { {0} };
423         const char *property;
424         GVariant *value = NULL;
425         char *type_qos = NULL;
426         char *device = NULL;
427         char *app_handle = NULL;
428         hdp_app_list_t *list = NULL;
429         GDBusProxy *proxy = NULL;
430         GVariant *reply = NULL;
431         GDBusConnection *conn;
432         bt_hdp_connected_t conn_ind;
433         GError *err = NULL;
434         int fd;
435         bt_user_info_t *user_info;
436         char *dev_path;
437         GUnixFDList *out_fd_list;
438         int index;
439         GVariantIter *property_iter;
440         gsize len;
441
442         BT_DBG("+");
443
444         conn = _bt_gdbus_get_system_gconn();
445         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
446
447         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
448                                         NULL,
449                                         BT_BLUEZ_NAME,
450                                         path,
451                                         BLUEZ_HDP_CHANNEL_INTERFACE,
452                                         NULL, &err);
453
454         if (!proxy) {
455                 BT_ERR("Unable to create proxy: %s", err->message);
456                 g_clear_error(&err);
457                 return BLUETOOTH_ERROR_INTERNAL;
458         }
459
460         reply = g_dbus_proxy_call_with_unix_fd_list_sync (proxy,
461                                                    "Acquire",
462                                                    NULL,
463                                                    G_DBUS_CALL_FLAGS_NONE,
464                                                    -1,
465                                                    NULL,
466                                                    &out_fd_list,
467                                                    NULL,
468                                                   &err);
469
470         g_object_unref(proxy);
471
472         if (!reply) {
473                 BT_ERR(" HDP:****** dbus Can't create application ****");
474
475                 if (err) {
476                         BT_ERR("%s", err->message);;
477                         g_clear_error(&err);
478                 }
479
480                 return BLUETOOTH_ERROR_INTERNAL;
481         }
482
483         g_variant_get (reply, "(h)", &index);
484         fd = g_unix_fd_list_get(out_fd_list, index, NULL);
485
486         g_variant_unref(reply);
487
488         BT_DBG("File Descriptor = %d, Dev_path = %s \n", fd, path);
489
490         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
491                                         NULL,
492                                         BT_BLUEZ_NAME,
493                                         path,
494                                         BT_PROPERTIES_INTERFACE,
495                                         NULL, &err);
496
497         if (!proxy) {
498                 BT_ERR("Unable to create proxy: %s", err->message);
499                 g_clear_error(&err);
500                 return BLUETOOTH_ERROR_INTERNAL;
501         }
502
503         dev_path = g_strdup(BLUEZ_HDP_CHANNEL_INTERFACE);
504
505         reply = g_dbus_proxy_call_sync(proxy, "GetAll",
506                                 g_variant_new("(s)", dev_path),
507                                 G_DBUS_CALL_FLAGS_NONE,
508                                 -1,
509                                 NULL,
510                                 &err);
511
512         g_free(dev_path);
513         g_object_unref(proxy);
514
515         if (!reply) {
516                 BT_ERR(" HDP:dbus Can't get the reply");
517
518                 if (err) {
519                         BT_ERR("%s", err->message);;
520                         g_clear_error(&err);
521                 }
522
523                 return BLUETOOTH_ERROR_INTERNAL;
524         }
525
526         g_variant_get (reply, "(a{sv})", &property_iter);
527
528         while (g_variant_iter_loop(property_iter, "{sv}", &property, &value)) {
529                 BT_DBG("String received = %s\n", property);
530
531                 if (g_strcmp0("Type", property) == 0) {
532                         type_qos = g_variant_dup_string (value, &len);
533                 } else if (g_strcmp0("Device", property) == 0) {
534                         device = g_variant_dup_string (value, &len);
535                 } else if (g_strcmp0("Application", property) == 0) {
536                         app_handle = g_variant_dup_string (value, &len);
537                 }
538         }
539
540         g_variant_unref(reply);
541         BT_DBG("QOS = %s, Device = %s, Apphandler = %s",
542                         type_qos, device, app_handle);
543
544         if (NULL == type_qos || NULL == app_handle) {
545                 BT_ERR("Pasing failed\n");
546                 goto error;
547         }
548
549         list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
550
551         /*Only process register with app handle receive the Connected event */
552         if (NULL == list) {
553                 BT_ERR("**** Could not locate the list for %s*****\n", app_handle);
554                 goto error;
555         }
556
557         hdp_obj_info_t *info = g_new0(hdp_obj_info_t, 1);
558         info->fd = fd;
559         info->obj_channel_path = g_strdup(path);
560         info->watch_id = __bt_hdp_internal_watch_fd(fd, info->obj_channel_path);
561         list->obj_info = g_slist_append(list->obj_info, info);
562
563         _bt_convert_device_path_to_address(path, address);
564
565         _bt_convert_addr_string_to_type(device_addr.addr, address);
566
567         conn_ind.app_handle = app_handle;
568         conn_ind.channel_id = fd;
569         conn_ind.device_address = device_addr;
570         conn_ind.type = (g_strcmp0(type_qos, "Reliable") == 0) ?
571                         HDP_QOS_RELIABLE : HDP_QOS_STREAMING;
572
573         BT_DBG("Going to give callback\n");
574
575         user_info = _bt_get_user_data(BT_COMMON);
576
577         if (user_info->cb) {
578                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
579                                 BLUETOOTH_ERROR_NONE, &conn_ind,
580                                 user_info->cb, user_info->user_data);
581         }
582
583         BT_DBG("Updated fd in the list*\n");
584         BT_DBG("-\n");
585
586         g_free(type_qos);
587         g_free(device);
588         g_free(app_handle);
589         return BLUETOOTH_ERROR_NONE;
590 error:
591         g_free(type_qos);
592         g_free(device);
593         g_free(app_handle);
594         return BLUETOOTH_ERROR_INTERNAL;
595 }
596
597 static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path)
598 {
599         GIOChannel *gio;
600         guint id;
601
602         BT_DBG("+");
603
604         gio = g_io_channel_unix_new(file_desc);
605
606         g_io_channel_set_close_on_unref(gio, TRUE);
607
608         id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
609                         __bt_hdp_internal_data_received, (void *)path);
610         BT_DBG("-");
611         return id;
612 }
613
614
615 static void __bt_hdp_internal_handle_disconnect_cb(int sk, const char *path)
616 {
617         char address[BT_ADDRESS_STRING_SIZE] = { 0, };
618         bluetooth_device_address_t device_addr = { {0} };
619         bt_hdp_disconnected_t dis_ind;
620         hdp_obj_info_t *info;
621         bt_user_info_t *user_info;
622
623         BT_INFO("******** Socket Error  ******\n");
624
625         info = __bt_hdp_internal_gslist_obj_find_using_path(path);
626         ret_if(info == NULL);
627
628         /*Since bluetoothd is not sending the ChannelDeleted signal */
629         _bt_convert_device_path_to_address(path, address);
630
631         _bt_convert_addr_string_to_type(device_addr.addr, address);
632
633         dis_ind.channel_id = sk;
634         dis_ind.device_address = device_addr;
635
636         user_info = _bt_get_user_data(BT_COMMON);
637
638         if (user_info->cb) {
639                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
640                                 BLUETOOTH_ERROR_NONE, &dis_ind,
641                                 user_info->cb, user_info->user_data);
642         }
643
644         BT_DBG(" Removed connection from list\n");
645
646         __bt_hdp_obj_info_free(info);
647 }
648
649 static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
650                                         GIOCondition cond, gpointer data)
651 {
652         char buff[HDP_BUFFER_SIZE] = { 0, };
653         int sk;
654         int act_read;
655         bt_hdp_data_ind_t data_ind = { 0, };
656         const char *path = (const char *)data;
657         bt_user_info_t *user_info;
658
659         BT_DBG("+");
660
661         sk = g_io_channel_unix_get_fd(gio);
662
663         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
664                 BT_DBG("GIOCondition %d.............path = %s\n", cond, path);
665                 g_io_channel_shutdown(gio, TRUE, NULL);
666                 g_io_channel_unref(gio);
667                  __bt_hdp_internal_handle_disconnect_cb(sk, path);
668                 return FALSE;
669         }
670
671         act_read = recv(sk, (void *)buff, sizeof(buff), 0);
672
673         if (act_read > 0) {
674                 BT_DBG("Received data of %d\n", act_read);
675         } else {
676                 BT_ERR("Read failed.....\n");
677                 return FALSE;
678         }
679
680         data_ind.channel_id = sk;
681         data_ind.buffer = buff;
682         data_ind.size = act_read;
683
684         user_info = _bt_get_user_data(BT_COMMON);
685
686         if (user_info->cb) {
687                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DATA_RECEIVED,
688                                 BLUETOOTH_ERROR_NONE, &data_ind,
689                                 user_info->cb, user_info->user_data);
690         }
691
692         BT_DBG("-\n");
693
694         return TRUE;
695 }
696
697 BT_EXPORT_API int bluetooth_hdp_deactivate(const char *app_handle)
698 {
699         BT_DBG("+");
700
701         BT_CHECK_ENABLED(return);
702         BT_CHECK_PARAMETER(app_handle, return);
703
704         return __bt_hdp_internal_destroy_application(app_handle);
705 }
706
707 static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle)
708 {
709         GSList *l;
710
711         retv_if(g_app_list == NULL, NULL);
712
713         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
714
715         for (l = g_app_list; l != NULL; l = l->next) {
716                 hdp_app_list_t *list = l->data;
717
718                 if (list) {
719                         if (0 == g_strcmp0((char *)list->app_handle,
720                                                 (char *)app_handle))
721                                 return list;
722                 }
723         }
724         return NULL;
725 }
726
727 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd)
728 {
729         GSList *l;
730         GSList *iter;
731
732         retv_if(g_app_list == NULL, NULL);
733
734         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
735
736         for (l = g_app_list; l != NULL; l = l->next) {
737                 hdp_app_list_t *list = l->data;
738                 if (!list)
739                         return NULL;
740
741                 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
742                         hdp_obj_info_t *info = iter->data;
743                         if (!info)
744                                 return NULL;
745
746                         if (fd == info->fd)
747                                 return info;
748                 }
749         }
750         return NULL;
751 }
752
753 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path)
754 {
755         GSList *l;
756         GSList *iter;
757         hdp_obj_info_t *info = NULL;
758
759         retv_if(g_app_list == NULL, NULL);
760
761         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
762         for (l = g_app_list; l != NULL; l = l->next) {
763                 hdp_app_list_t *list = l->data;
764                 if (!list)
765                         return NULL;
766
767                 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
768                          info = iter->data;
769                         if (!info)
770                                 return NULL;
771
772                         if (0 == g_strcmp0(info->obj_channel_path, obj_channel_path)) {
773                                 list->obj_info = g_slist_remove(list->obj_info, info);
774                                 return info;
775                         }
776                 }
777         }
778         return NULL;
779 }
780
781 static gboolean  __bt_hdp_internal_destroy_application_cb(gpointer data)
782 {
783         const char *app_handle;
784         hdp_app_list_t *list = NULL;
785         app_handle = (const char *)data;
786
787         BT_DBG("+");
788
789         list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
790         if (NULL == list) {
791                 BT_ERR("**** list not found for %s ******\n", app_handle);
792                 return FALSE;
793         }
794
795         g_app_list = g_slist_remove(g_app_list, list);
796
797         g_free(list->app_handle);
798         g_slist_foreach(list->obj_info, (GFunc)__bt_hdp_obj_info_free, NULL);
799         g_free(list);
800
801         BT_DBG("List length = %d\n", g_slist_length(g_app_list));
802
803         if (0 == g_slist_length(g_app_list))
804                 __bt_hdp_internal_remove_filter();
805         BT_DBG("-");
806         return FALSE;
807 }
808
809 static int __bt_hdp_internal_destroy_application(const char *app_handle)
810 {
811         GDBusProxy *proxy = NULL;
812         GVariant *reply = NULL;
813         GError *err = NULL;
814         GDBusConnection *conn;
815         int result = BLUETOOTH_ERROR_NONE;
816
817         conn = _bt_gdbus_get_system_gconn();
818         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
819
820         proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
821                                         NULL,
822                                         BT_BLUEZ_NAME,
823                                         "/org/bluez",
824                                         BLUEZ_HDP_MANAGER_INTERFACE,
825                                         NULL, &err);
826
827         if (!proxy) {
828                 BT_ERR("Unable to create proxy: %s", err->message);
829                 g_clear_error(&err);
830                 return BLUETOOTH_ERROR_INTERNAL;
831         }
832
833         reply = g_dbus_proxy_call_sync(proxy, "DestroyApplication",
834                                 g_variant_new("o", app_handle),
835                                 G_DBUS_CALL_FLAGS_NONE,
836                                 -1,
837                                 NULL,
838                                 &err);
839
840         g_object_unref(proxy);
841         if (!reply) {
842                 BT_ERR(" HDP:dbus Can't Destroy application");
843
844                 if (err) {
845                         BT_ERR("%s", err->message);
846                         if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
847                                 result  = BLUETOOTH_ERROR_ACCESS_DENIED;
848                         else
849                                 result  = BLUETOOTH_ERROR_INTERNAL;
850                         g_clear_error(&err);
851                 }
852                 return result ;
853         }
854
855         g_variant_unref(reply);
856
857         BT_DBG("Destroyed health application: %s", (char *)app_handle);
858
859         g_idle_add(__bt_hdp_internal_destroy_application_cb,
860                         (gpointer)app_handle);
861
862         return BLUETOOTH_ERROR_NONE;
863 }
864
865 static void __bt_hdp_internal_remove_filter(void)
866 {
867         BT_DBG("+");
868
869         ret_if(g_hdp_dus_conn == NULL);
870
871         __bt_hdp_add_filter_subscribe_signal(g_hdp_dus_conn, FALSE);
872
873         g_hdp_dus_conn = NULL;  /*should not unref here, bcz no ++reff */
874
875         BT_DBG("-");
876 }
877
878 BT_EXPORT_API int bluetooth_hdp_send_data(unsigned int channel_id,
879                                             const char *buffer,
880                                             unsigned int size)
881 {
882         int wbytes = 0;
883         int written = 0;
884         int result;
885
886         BT_DBG("+");
887
888         BT_CHECK_ENABLED(return);
889
890         if ((channel_id == 0) || (NULL == buffer) || (size == 0)) {
891                 BT_ERR("Invalid arguments..\n");
892                 return BLUETOOTH_ERROR_INVALID_PARAM;
893         }
894
895         switch (privilege_token) {
896         case 0:
897                 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_SEND_DATA);
898
899                 if (result == BLUETOOTH_ERROR_NONE) {
900                         privilege_token = 1; /* Have a permission */
901                 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
902                         BT_ERR("Don't have a privilege to use this API");
903                         privilege_token = -1; /* Don't have a permission */
904                         return BLUETOOTH_ERROR_PERMISSION_DEINED;
905                 } else {
906                         /* Just break - It is not related with permission error */
907                 }
908                 break;
909         case 1:
910                 /* Already have a privilege */
911                 break;
912         case -1:
913                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
914         default:
915                 /* Invalid privilge token value */
916                 return BLUETOOTH_ERROR_INTERNAL;
917         }
918
919         while (wbytes < size) {
920                 written = write(channel_id, buffer + wbytes, size - wbytes);
921                 if (written <= 0) {
922                         BT_ERR("write failed..\n");
923                         return BLUETOOTH_ERROR_NOT_IN_OPERATION;
924                 }
925                 wbytes += written;
926         }
927
928         return BLUETOOTH_ERROR_NONE;
929 }
930
931 static void __bt_hdp_connect_request_cb(GDBusProxy *hdp_proxy,
932                                 GAsyncResult *res, gpointer user_data)
933 {
934         GError *err = NULL;
935         char *obj_connect_path = NULL;
936         bt_hdp_connected_t *conn_ind = user_data;
937         bt_user_info_t *user_info;
938         GVariant *reply = NULL;
939         int ret = BLUETOOTH_ERROR_NONE;
940
941         reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
942
943         g_object_unref(hdp_proxy);
944
945         if (!reply) {
946                 if (err) {
947                         BT_ERR("HDP connection  Dbus Call Error: %s\n", err->message);
948                         g_clear_error(&err);
949                 }
950
951                 user_info = _bt_get_user_data(BT_COMMON);
952
953                 if (user_info->cb) {
954                         _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
955                                         BLUETOOTH_ERROR_CONNECTION_ERROR, conn_ind,
956                                         user_info->cb, user_info->user_data);
957                 }
958         } else {
959                 g_variant_get(reply, "(&o)", &obj_connect_path);
960
961                 BT_DBG("Obj Path returned = %s\n", obj_connect_path);
962                 user_info = _bt_get_user_data(BT_COMMON);
963
964                 ret = __bt_hdp_internal_acquire_fd(obj_connect_path);
965                 if (ret != BLUETOOTH_ERROR_NONE) {
966                         user_info = _bt_get_user_data(BT_COMMON);
967                         if (user_info->cb) {
968                                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
969                                                 BLUETOOTH_ERROR_CONNECTION_ERROR, NULL,
970                                                         user_info->cb, user_info->user_data);
971                         }
972                 }
973                 g_variant_unref(reply);
974         }
975         g_free((void *)conn_ind->app_handle);
976         g_free(conn_ind);
977 }
978
979
980 BT_EXPORT_API int bluetooth_hdp_connect(const char *app_handle,
981                         bt_hdp_qos_type_t channel_type,
982                         const bluetooth_device_address_t *device_address)
983 {
984         GError *err = NULL;
985         GDBusConnection *conn = NULL;
986         GDBusProxy *hdp_proxy = NULL;
987         bt_hdp_connected_t *param;
988         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
989         char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
990         char *dev_path = NULL;
991         char *role;
992
993         BT_DBG("+");
994
995         BT_CHECK_ENABLED(return);
996         BT_CHECK_PARAMETER(app_handle, return);
997         BT_CHECK_PARAMETER(device_address, return);
998
999         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_CONNECT)
1000              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1001                 BT_ERR("Don't have a privilege to use this API");
1002                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1003         }
1004
1005         if (channel_type == HDP_QOS_RELIABLE) {
1006                 role = "Reliable";
1007         } else if (channel_type == HDP_QOS_STREAMING) {
1008                 role = "Streaming";
1009         } else if (channel_type == HDP_QOS_ANY) {
1010                 role = "Any";
1011         } else {
1012                 BT_ERR("Invalid channel_type %d", channel_type);
1013                 return BLUETOOTH_ERROR_ACCESS_DENIED;
1014         }
1015
1016         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1017
1018         if (err) {
1019                 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1020                 g_clear_error(&err);
1021                 return BLUETOOTH_ERROR_INTERNAL;
1022         }
1023
1024         /* If the adapter path is wrong, we can think the BT is not enabled. */
1025         if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1026                                         default_adapter_path) < 0) {
1027                 BT_ERR("Could not get adapter path\n");
1028                 g_object_unref(conn);
1029                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1030         }
1031
1032         _bt_convert_addr_type_to_string(address,
1033                                 (unsigned char *)device_address->addr);
1034
1035         BT_DBG("create conection to %s", address);
1036
1037         dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1038
1039         if (dev_path == NULL) {
1040                 g_object_unref(conn);
1041                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1042         }
1043
1044         g_strdelimit(dev_path, ":", '_');
1045
1046         BT_DBG("path: %s", dev_path);
1047
1048         hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1049                                                 NULL, BT_BLUEZ_NAME,
1050                                                 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1051                                                 NULL, NULL);
1052         g_object_unref(conn);
1053
1054         if (hdp_proxy == NULL) {
1055                 BT_ERR("Failed to get the HDP server proxy\n");
1056                 g_free(dev_path);
1057                 return BLUETOOTH_ERROR_NOT_PAIRED;
1058         }
1059
1060         BT_DBG("app path %s\n", app_handle);
1061
1062         param = g_new0(bt_hdp_connected_t, 1);
1063         param->app_handle = g_strdup(app_handle);
1064         memcpy(&param->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1065         param->type = channel_type;
1066
1067         g_dbus_proxy_call(hdp_proxy, "CreateChannel",
1068                                 g_variant_new("(os)", app_handle, role),
1069                                 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1070                                 (GAsyncReadyCallback)__bt_hdp_connect_request_cb,
1071                                 param);
1072
1073         g_free(dev_path);
1074         g_free((void *)param->app_handle);
1075         g_free(param);
1076         g_object_unref(hdp_proxy);
1077
1078         return BLUETOOTH_ERROR_NONE;
1079 }
1080
1081 static void __bt_hdp_disconnect_request_cb(GDBusProxy *hdp_proxy,
1082                         GAsyncResult *res, gpointer user_data)
1083 {
1084         GError *err = NULL;
1085         bt_hdp_disconnected_t *disconn_ind = user_data;
1086         bt_user_info_t *user_info;
1087         GVariant *reply = NULL;
1088
1089         reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
1090         g_object_unref(hdp_proxy);
1091
1092         user_info = _bt_get_user_data(BT_COMMON);
1093         if (user_info == NULL || user_info->cb == NULL) {
1094                 g_free(disconn_ind);
1095                 if (err) {
1096                                 g_clear_error(&err);
1097                         return;
1098                 }
1099                 g_variant_unref(reply);
1100                 return;
1101         }
1102
1103         if (!reply) {
1104                 if (err) {
1105                         BT_ERR("HDP disconnection Dbus Call Error: %s\n",
1106                                                         err->message);
1107                         g_clear_error(&err);
1108                 }
1109
1110                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1111                                 BLUETOOTH_ERROR_CONNECTION_ERROR, disconn_ind,
1112                                 user_info->cb, user_info->user_data);
1113         } else {
1114                 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1115                                 BLUETOOTH_ERROR_NONE, disconn_ind,
1116                                 user_info->cb, user_info->user_data);
1117                 BT_INFO("HDP disconnection Dbus Call is done\n");
1118                 g_variant_unref(reply);
1119         }
1120
1121         g_free(disconn_ind);
1122 }
1123
1124 BT_EXPORT_API int bluetooth_hdp_disconnect(unsigned int channel_id,
1125                         const bluetooth_device_address_t *device_address)
1126 {
1127         GError *err = NULL;
1128         GDBusConnection *conn = NULL;
1129         GDBusProxy *hdp_proxy = NULL;
1130         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1131         char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
1132         char *dev_path = NULL;
1133         bt_hdp_disconnected_t *param;
1134
1135         BT_DBG("+\n");
1136
1137         BT_CHECK_ENABLED(return);
1138         BT_CHECK_PARAMETER(device_address, return);
1139
1140         if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_DISCONNECT)
1141              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1142                 BT_ERR("Don't have a privilege to use this API");
1143                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1144         }
1145
1146         hdp_obj_info_t *info =
1147                 __bt_hdp_internal_gslist_obj_find_using_fd(channel_id);
1148         if (NULL == info) {
1149                 BT_ERR("*** Could not locate the list for %d*****\n",
1150                                                         channel_id);
1151                 return BLUETOOTH_ERROR_INVALID_PARAM;
1152         }
1153
1154         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1155
1156         if (err) {
1157                 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1158                 g_clear_error(&err);
1159                 return BLUETOOTH_ERROR_INTERNAL;
1160         }
1161
1162         /* If the adapter path is wrong, we can think the BT is not enabled. */
1163         if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1164                                         default_adapter_path) < 0) {
1165                 BT_ERR("Could not get adapter path\n");
1166                 g_object_unref(conn);
1167                 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1168         }
1169
1170         _bt_convert_addr_type_to_string(address,
1171                                 (unsigned char *)device_address->addr);
1172
1173         BT_DBG("create conection to  %s\n", address);
1174
1175         dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1176
1177         if (dev_path == NULL) {
1178                 g_object_unref(conn);
1179                 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1180         }
1181
1182         g_strdelimit(dev_path, ":", '_');
1183
1184         BT_DBG("path  %s\n", dev_path);
1185
1186         hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1187                                                 NULL, BT_BLUEZ_NAME,
1188                                                 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1189                                                 NULL, NULL);
1190
1191         g_object_unref(conn);
1192
1193         if (hdp_proxy == NULL) {
1194                 BT_ERR("Failed to get the HDP proxy\n");
1195                 g_free(dev_path);
1196                 return BLUETOOTH_ERROR_NOT_PAIRED;
1197         }
1198
1199         param = g_new0(bt_hdp_disconnected_t, 1);
1200         param->channel_id = channel_id;
1201         memcpy(&param->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1202
1203         g_dbus_proxy_call(hdp_proxy, "DestroyChannel",
1204                                 g_variant_new("o", info->obj_channel_path),
1205                                 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1206                                 (GAsyncReadyCallback)__bt_hdp_disconnect_request_cb,
1207                                 param);
1208
1209         g_free(dev_path);
1210         g_free(param);
1211         g_object_unref(hdp_proxy);
1212
1213         return BLUETOOTH_ERROR_NONE;
1214
1215 }