2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <sys/types.h>
19 #include <sys/socket.h>
23 #include <gio/gunixfdlist.h>
25 #include "bluetooth-api.h"
26 #include "bt-common.h"
27 #include "bt-internal-types.h"
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"
35 char *obj_channel_path;
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
51 static int privilege_token;
54 /**********************************************************************
55 * Static Functions declaration *
56 ***********************************************************************/
57 static int __bt_hdp_internal_create_application(unsigned int data_type,
59 bt_hdp_qos_type_t channel_type,
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,
70 static void __bt_hdp_internal_handle_connect(GVariant *parameters);
72 static void __bt_hdp_internal_handle_disconnect(GVariant *parameters,
73 const gchar *object_path);
75 static void __bt_hdp_internal_handle_property_changed(GVariant *parameters);
77 static int __bt_hdp_internal_add_filter(void);
79 static int __bt_hdp_internal_acquire_fd(const char *path);
81 static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path);
83 static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
87 static int __bt_hdp_internal_destroy_application(const char *app_handle);
89 static void __bt_hdp_internal_remove_filter(void);
91 static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle);
93 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd);
95 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path);
98 static GDBusConnection *g_hdp_dus_conn;
100 static GSList *g_app_list = NULL;
102 /**********************************************************************
103 * Health device APIs (HDP) *
104 ***********************************************************************/
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,
111 int result = BLUETOOTH_ERROR_NONE;
115 BT_CHECK_ENABLED(return);
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;
123 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_REGISTER_SINK_APP)
124 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
125 BT_ERR("Don't have a privilege to use this API");
126 return BLUETOOTH_ERROR_PERMISSION_DEINED;
129 result = __bt_hdp_internal_create_application(data_type, role,
130 channel_type, app_handle);
135 static void __bt_hdp_obj_info_free(hdp_obj_info_t *info)
138 g_source_remove(info->watch_id);
140 g_free(info->obj_channel_path);
145 static int __bt_hdp_internal_create_application(unsigned int data_type,
147 bt_hdp_qos_type_t channel_type,
150 GDBusProxy *proxy = NULL;
151 GVariant *reply = NULL;
152 GVariantBuilder *builder;
154 const char *key_type;
156 hdp_app_list_t *list;
159 GDBusConnection *conn;
160 int ret = BLUETOOTH_ERROR_NONE;
164 conn = _bt_gdbus_get_system_gconn();
165 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
167 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
171 BLUEZ_HDP_MANAGER_INTERFACE,
175 BT_ERR("Unable to create proxy: %s", err->message);
177 return BLUETOOTH_ERROR_INTERNAL;
180 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
182 key_type = "DataType";
183 value = (guint16) data_type;
184 g_variant_builder_add(builder, "{sv}",
185 key_type, g_variant_new("q",
190 svalue = (role == HDP_ROLE_SINK) ? "Sink" : "Source";
191 g_variant_builder_add(builder, "{sv}",
192 key_type, g_variant_new("s",
195 key_type = "Description";
196 svalue = "Health Device";
197 g_variant_builder_add(builder, "{sv}",
198 key_type, g_variant_new("s",
201 if (role == HDP_ROLE_SOURCE) {
202 key_type = "ChannelType";
203 if (channel_type == HDP_QOS_RELIABLE)
205 else if (channel_type == HDP_QOS_STREAMING)
206 svalue = "streaming";
208 g_variant_builder_add(builder, "{sv}",
209 key_type, g_variant_new("s",
213 reply = g_dbus_proxy_call_sync(proxy, "CreateApplication",
214 g_variant_new("(a{sv})", builder),
215 G_DBUS_CALL_FLAGS_NONE, -1,
218 g_variant_builder_unref(builder);
219 g_object_unref(proxy);
222 BT_ERR(" HDP:dbus Can't create application");
224 BT_ERR("%s", err->message);
225 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
226 ret = BLUETOOTH_ERROR_PERMISSION_DEINED;
228 ret = BLUETOOTH_ERROR_INTERNAL;
234 g_variant_get(reply, "(&o)", &app_path);
235 BT_DBG("Created health application: %s", (char *)app_path);
237 ret = __bt_hdp_internal_add_filter();
239 if (ret != BLUETOOTH_ERROR_NONE) {
240 BT_ERR("Funtion failed");
244 list = g_new0(hdp_app_list_t, 1);
245 list->app_handle = (void *)g_strdup(app_path);
246 *app_handle = list->app_handle;
248 g_app_list = g_slist_append(g_app_list, list);
250 g_variant_unref(reply);
251 return BLUETOOTH_ERROR_NONE;
254 static int __bt_hdp_add_filter_subscribe_signal(GDBusConnection *conn,
257 static guint subs_add_filter_id = 0;
260 return BLUETOOTH_ERROR_INVALID_PARAM;
263 if (subs_add_filter_id == 0) {
264 subs_add_filter_id = g_dbus_connection_signal_subscribe(
265 conn, NULL, BLUEZ_HDP_DEVICE_INTERFACE,
267 __bt_hdp_internal_event_filter, NULL, NULL);
270 if (subs_add_filter_id > 0) {
271 g_dbus_connection_signal_unsubscribe(conn,
273 subs_add_filter_id = 0;
276 return BLUETOOTH_ERROR_NONE;
279 static int __bt_hdp_internal_add_filter(void)
283 /*Single process only one signal registration is required */
284 if (g_hdp_dus_conn) {
285 BT_ERR("g_hdp_dus_conn already exist");
286 return BLUETOOTH_ERROR_NONE;
289 g_hdp_dus_conn = _bt_gdbus_get_system_gconn();
290 retv_if(g_hdp_dus_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
293 return __bt_hdp_add_filter_subscribe_signal(g_hdp_dus_conn, TRUE);
296 static void __bt_hdp_internal_event_filter(GDBusConnection *connection,
297 const gchar *sender_name,
298 const gchar *object_path,
299 const gchar *interface_name,
300 const gchar *signal_name,
301 GVariant *parameters,
304 BT_DBG("Path = %s\n", object_path);
305 if (object_path == NULL || g_strcmp0(object_path, "/") == 0)
308 if (signal_name == NULL)
311 if (strcasecmp(signal_name, "ChannelConnected") == 0)
312 __bt_hdp_internal_handle_connect(parameters);
314 else if (strcasecmp(signal_name, "ChannelDeleted") == 0)
315 __bt_hdp_internal_handle_disconnect(parameters, object_path);
317 else if (strcasecmp(signal_name, "PropertyChanged") == 0)
318 __bt_hdp_internal_handle_property_changed(parameters);
323 static void __bt_hdp_internal_handle_connect(GVariant *parameters)
326 const char *obj_channel_path;
327 bt_user_info_t *user_info;
330 BT_INFO("*********Signal - ChannelConnected******\n\n");
331 g_variant_get(parameters, "(&o)", &obj_channel_path);
333 BT_INFO("Channel connected, Path = %s", obj_channel_path);
335 user_info = _bt_get_user_data(BT_COMMON);
336 if (user_info == NULL || user_info->cb == NULL)
339 ret = __bt_hdp_internal_acquire_fd(obj_channel_path);
340 if (ret != BLUETOOTH_ERROR_NONE) {
341 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
342 BLUETOOTH_ERROR_CONNECTION_ERROR, NULL,
343 user_info->cb, user_info->user_data);
348 static void __bt_hdp_internal_handle_disconnect(GVariant *parameters,
349 const gchar *object_path)
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;
358 BT_INFO("+********Signal - ChannelDeleted ******\n\n");
359 BT_DBG("Path = %s", object_path);
361 g_variant_get(parameters, "(&o)", &obj_channel_path);
363 BT_INFO("Channel Deleted, Path = %s", obj_channel_path);
365 info = __bt_hdp_internal_gslist_obj_find_using_path(obj_channel_path);
367 BT_ERR("No obj info for ob_channel_path [%s]\n", obj_channel_path);
371 /*Since bluetoothd is not sending the ChannelDeleted signal */
372 _bt_convert_device_path_to_address(object_path, address);
374 _bt_convert_addr_string_to_type(device_addr.addr, address);
376 dis_ind.channel_id = info->fd;
377 dis_ind.device_address = device_addr;
379 user_info = _bt_get_user_data(BT_COMMON);
382 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
383 BLUETOOTH_ERROR_NONE, &dis_ind,
384 user_info->cb, user_info->user_data);
387 BT_DBG(" Removed connection from list\n");
389 __bt_hdp_obj_info_free(info);
393 static void __bt_hdp_internal_handle_property_changed(GVariant *parameters)
395 char *property = NULL;
396 GVariant *value = NULL;
398 char *obj_main_channel_path = NULL;
399 GVariantIter *property_iter;
401 BT_DBG("+*******Signal - PropertyChanged*******\n");
403 g_variant_get(parameters, "(a{sv})", &property_iter);
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);
413 g_variant_iter_free(property_iter);
416 g_variant_unref(value);
417 g_free(obj_main_channel_path);
418 BT_DBG("-*************\n");
421 static int __bt_hdp_internal_acquire_fd(const char *path)
423 char address[BT_ADDRESS_STRING_SIZE] = { 0, };
424 bluetooth_device_address_t device_addr = { {0} };
425 const char *property;
426 GVariant *value = NULL;
427 char *type_qos = NULL;
429 char *app_handle = NULL;
430 hdp_app_list_t *list = NULL;
431 GDBusProxy *proxy = NULL;
432 GVariant *reply = NULL;
433 GDBusConnection *conn;
434 bt_hdp_connected_t conn_ind;
437 bt_user_info_t *user_info;
439 GUnixFDList *out_fd_list;
441 GVariantIter *property_iter;
446 conn = _bt_gdbus_get_system_gconn();
447 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
449 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
453 BLUEZ_HDP_CHANNEL_INTERFACE,
457 BT_ERR("Unable to create proxy: %s", err->message);
459 return BLUETOOTH_ERROR_INTERNAL;
462 reply = g_dbus_proxy_call_with_unix_fd_list_sync(proxy,
464 G_DBUS_CALL_FLAGS_NONE,
465 -1, NULL, &out_fd_list,
468 g_object_unref(proxy);
471 BT_ERR(" HDP:****** dbus Can't create application ****");
474 BT_ERR("%s", err->message);;
478 return BLUETOOTH_ERROR_INTERNAL;
481 g_variant_get(reply, "(h)", &index);
482 fd = g_unix_fd_list_get(out_fd_list, index, NULL);
484 g_variant_unref(reply);
486 BT_DBG("File Descriptor = %d, Dev_path = %s \n", fd, path);
488 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
492 BT_PROPERTIES_INTERFACE,
496 BT_ERR("Unable to create proxy: %s", err->message);
498 return BLUETOOTH_ERROR_INTERNAL;
501 dev_path = g_strdup(BLUEZ_HDP_CHANNEL_INTERFACE);
503 reply = g_dbus_proxy_call_sync(proxy, "GetAll",
504 g_variant_new("(s)", dev_path),
505 G_DBUS_CALL_FLAGS_NONE,
511 g_object_unref(proxy);
514 BT_ERR(" HDP:dbus Can't get the reply");
517 BT_ERR("%s", err->message);;
521 return BLUETOOTH_ERROR_INTERNAL;
524 g_variant_get(reply, "(a{sv})", &property_iter);
526 while (g_variant_iter_loop(property_iter, "{sv}", &property, &value)) {
527 BT_DBG("String received = %s\n", property);
529 if (g_strcmp0("Type", property) == 0)
530 type_qos = g_variant_dup_string(value, &len);
531 else if (g_strcmp0("Device", property) == 0)
532 device = g_variant_dup_string(value, &len);
533 else if (g_strcmp0("Application", property) == 0)
534 app_handle = g_variant_dup_string(value, &len);
536 g_variant_iter_free(property_iter);
538 g_variant_unref(reply);
539 BT_DBG("QOS = %s, Device = %s, Apphandler = %s",
540 type_qos, device, app_handle);
542 if (NULL == type_qos || NULL == app_handle) {
543 BT_ERR("Pasing failed\n");
547 list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
549 /*Only process register with app handle receive the Connected event */
551 BT_ERR("**** Could not locate the list for %s*****\n", app_handle);
555 hdp_obj_info_t *info = g_new0(hdp_obj_info_t, 1);
557 info->obj_channel_path = g_strdup(path);
558 info->watch_id = __bt_hdp_internal_watch_fd(fd, info->obj_channel_path);
559 list->obj_info = g_slist_append(list->obj_info, info);
561 _bt_convert_device_path_to_address(path, address);
563 _bt_convert_addr_string_to_type(device_addr.addr, address);
565 conn_ind.app_handle = app_handle;
566 conn_ind.channel_id = fd;
567 conn_ind.device_address = device_addr;
568 conn_ind.type = (g_strcmp0(type_qos, "Reliable") == 0) ?
569 HDP_QOS_RELIABLE : HDP_QOS_STREAMING;
571 BT_DBG("Going to give callback\n");
573 user_info = _bt_get_user_data(BT_COMMON);
576 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
577 BLUETOOTH_ERROR_NONE, &conn_ind,
578 user_info->cb, user_info->user_data);
581 BT_DBG("Updated fd in the list*\n");
587 return BLUETOOTH_ERROR_NONE;
592 return BLUETOOTH_ERROR_INTERNAL;
595 static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path)
602 gio = g_io_channel_unix_new(file_desc);
604 g_io_channel_set_close_on_unref(gio, TRUE);
606 id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
607 __bt_hdp_internal_data_received, (void *)path);
613 static void __bt_hdp_internal_handle_disconnect_cb(int sk, const char *path)
615 char address[BT_ADDRESS_STRING_SIZE] = { 0, };
616 bluetooth_device_address_t device_addr = { {0} };
617 bt_hdp_disconnected_t dis_ind;
618 hdp_obj_info_t *info;
619 bt_user_info_t *user_info;
621 BT_INFO("******** Socket Error ******\n");
623 info = __bt_hdp_internal_gslist_obj_find_using_path(path);
624 ret_if(info == NULL);
626 /*Since bluetoothd is not sending the ChannelDeleted signal */
627 _bt_convert_device_path_to_address(path, address);
629 _bt_convert_addr_string_to_type(device_addr.addr, address);
631 dis_ind.channel_id = sk;
632 dis_ind.device_address = device_addr;
634 user_info = _bt_get_user_data(BT_COMMON);
637 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
638 BLUETOOTH_ERROR_NONE, &dis_ind,
639 user_info->cb, user_info->user_data);
642 BT_DBG(" Removed connection from list\n");
644 __bt_hdp_obj_info_free(info);
647 static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
648 GIOCondition cond, gpointer data)
650 char buff[HDP_BUFFER_SIZE] = { 0, };
653 bt_hdp_data_ind_t data_ind = { 0, };
654 const char *path = (const char *)data;
655 bt_user_info_t *user_info;
659 sk = g_io_channel_unix_get_fd(gio);
661 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
662 BT_DBG("GIOCondition %d.............path = %s\n", cond, path);
663 g_io_channel_shutdown(gio, TRUE, NULL);
664 g_io_channel_unref(gio);
665 __bt_hdp_internal_handle_disconnect_cb(sk, path);
669 act_read = recv(sk, (void *)buff, sizeof(buff), 0);
672 BT_DBG("Received data of %d\n", act_read);
674 BT_ERR("Read failed.....\n");
678 data_ind.channel_id = sk;
679 data_ind.buffer = buff;
680 data_ind.size = act_read;
682 user_info = _bt_get_user_data(BT_COMMON);
685 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DATA_RECEIVED,
686 BLUETOOTH_ERROR_NONE, &data_ind,
687 user_info->cb, user_info->user_data);
695 BT_EXPORT_API int bluetooth_hdp_deactivate(const char *app_handle)
699 BT_CHECK_ENABLED(return);
700 BT_CHECK_PARAMETER(app_handle, return);
702 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_UNREGISTER_SINK_APP)
703 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
704 BT_ERR("Don't have a privilege to use this API");
705 return BLUETOOTH_ERROR_PERMISSION_DEINED;
708 return __bt_hdp_internal_destroy_application(app_handle);
711 static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle)
715 retv_if(g_app_list == NULL, NULL);
717 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
719 for (l = g_app_list; l != NULL; l = l->next) {
720 hdp_app_list_t *list = l->data;
723 if (0 == g_strcmp0((char *)list->app_handle,
731 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd)
736 retv_if(g_app_list == NULL, NULL);
738 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
740 for (l = g_app_list; l != NULL; l = l->next) {
741 hdp_app_list_t *list = l->data;
745 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
746 hdp_obj_info_t *info = iter->data;
757 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path)
761 hdp_obj_info_t *info = NULL;
763 retv_if(g_app_list == NULL, NULL);
765 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
766 for (l = g_app_list; l != NULL; l = l->next) {
767 hdp_app_list_t *list = l->data;
771 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
776 if (0 == g_strcmp0(info->obj_channel_path, obj_channel_path)) {
777 list->obj_info = g_slist_remove(list->obj_info, info);
785 static gboolean __bt_hdp_internal_destroy_application_cb(gpointer data)
787 const char *app_handle;
788 hdp_app_list_t *list = NULL;
789 app_handle = (const char *)data;
793 list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
795 BT_ERR("**** list not found for %s ******\n", app_handle);
799 g_app_list = g_slist_remove(g_app_list, list);
801 g_free(list->app_handle);
802 g_slist_foreach(list->obj_info, (GFunc)__bt_hdp_obj_info_free, NULL);
805 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
807 if (0 == g_slist_length(g_app_list))
808 __bt_hdp_internal_remove_filter();
813 static int __bt_hdp_internal_destroy_application(const char *app_handle)
815 GDBusProxy *proxy = NULL;
816 GVariant *reply = NULL;
818 GDBusConnection *conn;
819 int result = BLUETOOTH_ERROR_NONE;
821 conn = _bt_gdbus_get_system_gconn();
822 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
824 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
828 BLUEZ_HDP_MANAGER_INTERFACE,
832 BT_ERR("Unable to create proxy: %s", err->message);
834 return BLUETOOTH_ERROR_INTERNAL;
837 reply = g_dbus_proxy_call_sync(proxy, "DestroyApplication",
838 g_variant_new("o", app_handle),
839 G_DBUS_CALL_FLAGS_NONE,
844 g_object_unref(proxy);
846 BT_ERR(" HDP:dbus Can't Destroy application");
849 BT_ERR("%s", err->message);
850 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
851 result = BLUETOOTH_ERROR_ACCESS_DENIED;
853 result = BLUETOOTH_ERROR_INTERNAL;
859 g_variant_unref(reply);
861 BT_DBG("Destroyed health application: %s", (char *)app_handle);
863 g_idle_add(__bt_hdp_internal_destroy_application_cb,
864 (gpointer)app_handle);
866 return BLUETOOTH_ERROR_NONE;
869 static void __bt_hdp_internal_remove_filter(void)
873 ret_if(g_hdp_dus_conn == NULL);
875 __bt_hdp_add_filter_subscribe_signal(g_hdp_dus_conn, FALSE);
877 g_hdp_dus_conn = NULL; /*should not unref here, bcz no ++reff */
882 BT_EXPORT_API int bluetooth_hdp_send_data(unsigned int channel_id,
892 BT_CHECK_ENABLED(return);
894 if ((channel_id == 0) || (NULL == buffer) || (size == 0)) {
895 BT_ERR("Invalid arguments..\n");
896 return BLUETOOTH_ERROR_INVALID_PARAM;
899 switch (privilege_token) {
901 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_SEND_DATA);
903 if (result == BLUETOOTH_ERROR_NONE) {
904 privilege_token = 1; /* Have a permission */
905 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
906 BT_ERR("Don't have a privilege to use this API");
907 privilege_token = -1; /* Don't have a permission */
908 return BLUETOOTH_ERROR_PERMISSION_DEINED;
910 /* Just break - It is not related with permission error */
914 /* Already have a privilege */
917 return BLUETOOTH_ERROR_PERMISSION_DEINED;
919 /* Invalid privilge token value */
920 return BLUETOOTH_ERROR_INTERNAL;
923 while (wbytes < size) {
924 written = write(channel_id, buffer + wbytes, size - wbytes);
926 BT_ERR("write failed..\n");
927 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
932 return BLUETOOTH_ERROR_NONE;
935 static void __bt_hdp_connect_request_cb(GDBusProxy *hdp_proxy,
936 GAsyncResult *res, gpointer user_data)
939 char *obj_connect_path = NULL;
940 bt_hdp_connected_t *conn_ind = user_data;
941 bt_user_info_t *user_info;
942 GVariant *reply = NULL;
944 reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
946 g_object_unref(hdp_proxy);
950 BT_ERR("HDP connection Dbus Call Error: %s\n", err->message);
954 user_info = _bt_get_user_data(BT_COMMON);
957 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
958 BLUETOOTH_ERROR_CONNECTION_ERROR, conn_ind,
959 user_info->cb, user_info->user_data);
962 g_variant_get(reply, "(&o)", &obj_connect_path);
963 BT_DBG("Obj Path returned = %s\n", obj_connect_path);
965 g_free((void *)conn_ind->app_handle);
970 BT_EXPORT_API int bluetooth_hdp_connect(const char *app_handle,
971 bt_hdp_qos_type_t channel_type,
972 const bluetooth_device_address_t *device_address)
975 GDBusConnection *conn = NULL;
976 GDBusProxy *hdp_proxy = NULL;
977 bt_hdp_connected_t *param;
978 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
979 char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
980 char *dev_path = NULL;
985 BT_CHECK_ENABLED(return);
986 BT_CHECK_PARAMETER(app_handle, return);
987 BT_CHECK_PARAMETER(device_address, return);
989 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_CONNECT)
990 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
991 BT_ERR("Don't have a privilege to use this API");
992 return BLUETOOTH_ERROR_PERMISSION_DEINED;
995 if (channel_type == HDP_QOS_RELIABLE) {
997 } else if (channel_type == HDP_QOS_STREAMING) {
999 } else if (channel_type == HDP_QOS_ANY) {
1002 BT_ERR("Invalid channel_type %d", channel_type);
1003 return BLUETOOTH_ERROR_ACCESS_DENIED;
1006 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1009 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1010 g_clear_error(&err);
1011 return BLUETOOTH_ERROR_INTERNAL;
1014 /* If the adapter path is wrong, we can think the BT is not enabled. */
1015 if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1016 default_adapter_path) < 0) {
1017 BT_ERR("Could not get adapter path\n");
1018 g_object_unref(conn);
1019 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1022 _bt_convert_addr_type_to_string(address,
1023 (unsigned char *)device_address->addr);
1025 BT_DBG("create conection to %s", address);
1027 dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1029 if (dev_path == NULL) {
1030 g_object_unref(conn);
1031 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1034 g_strdelimit(dev_path, ":", '_');
1036 BT_DBG("path: %s", dev_path);
1038 hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1039 NULL, BT_BLUEZ_NAME,
1040 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1042 g_object_unref(conn);
1044 if (hdp_proxy == NULL) {
1045 BT_ERR("Failed to get the HDP server proxy\n");
1047 return BLUETOOTH_ERROR_NOT_PAIRED;
1050 BT_DBG("app path %s\n", app_handle);
1052 param = g_new0(bt_hdp_connected_t, 1);
1053 param->app_handle = g_strdup(app_handle);
1054 memcpy(¶m->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1055 param->type = channel_type;
1057 g_dbus_proxy_call(hdp_proxy, "CreateChannel",
1058 g_variant_new("(os)", app_handle, role),
1059 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1060 (GAsyncReadyCallback)__bt_hdp_connect_request_cb,
1065 return BLUETOOTH_ERROR_NONE;
1068 static void __bt_hdp_disconnect_request_cb(GDBusProxy *hdp_proxy,
1069 GAsyncResult *res, gpointer user_data)
1072 bt_hdp_disconnected_t *disconn_ind = user_data;
1073 bt_user_info_t *user_info;
1074 GVariant *reply = NULL;
1076 reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
1077 g_object_unref(hdp_proxy);
1079 user_info = _bt_get_user_data(BT_COMMON);
1080 if (user_info == NULL || user_info->cb == NULL) {
1081 g_free(disconn_ind);
1083 g_clear_error(&err);
1086 g_variant_unref(reply);
1092 BT_ERR("HDP disconnection Dbus Call Error: %s\n",
1094 g_clear_error(&err);
1097 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1098 BLUETOOTH_ERROR_CONNECTION_ERROR, disconn_ind,
1099 user_info->cb, user_info->user_data);
1101 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1102 BLUETOOTH_ERROR_NONE, disconn_ind,
1103 user_info->cb, user_info->user_data);
1104 BT_INFO("HDP disconnection Dbus Call is done\n");
1105 g_variant_unref(reply);
1108 g_free(disconn_ind);
1111 BT_EXPORT_API int bluetooth_hdp_disconnect(unsigned int channel_id,
1112 const bluetooth_device_address_t *device_address)
1115 GDBusConnection *conn = NULL;
1116 GDBusProxy *hdp_proxy = NULL;
1117 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1118 char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
1119 char *dev_path = NULL;
1120 bt_hdp_disconnected_t *param;
1124 BT_CHECK_ENABLED(return);
1125 BT_CHECK_PARAMETER(device_address, return);
1127 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_DISCONNECT)
1128 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1129 BT_ERR("Don't have a privilege to use this API");
1130 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1133 hdp_obj_info_t *info =
1134 __bt_hdp_internal_gslist_obj_find_using_fd(channel_id);
1136 BT_ERR("*** Could not locate the list for %d*****\n",
1138 return BLUETOOTH_ERROR_INVALID_PARAM;
1141 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1144 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1145 g_clear_error(&err);
1146 return BLUETOOTH_ERROR_INTERNAL;
1149 /* If the adapter path is wrong, we can think the BT is not enabled. */
1150 if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1151 default_adapter_path) < 0) {
1152 BT_ERR("Could not get adapter path\n");
1153 g_object_unref(conn);
1154 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1157 _bt_convert_addr_type_to_string(address,
1158 (unsigned char *)device_address->addr);
1160 BT_DBG("create conection to %s\n", address);
1162 dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1164 if (dev_path == NULL) {
1165 g_object_unref(conn);
1166 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1169 g_strdelimit(dev_path, ":", '_');
1171 BT_DBG("path %s\n", dev_path);
1173 hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1174 NULL, BT_BLUEZ_NAME,
1175 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1178 g_object_unref(conn);
1180 if (hdp_proxy == NULL) {
1181 BT_ERR("Failed to get the HDP proxy\n");
1183 return BLUETOOTH_ERROR_NOT_PAIRED;
1186 param = g_new0(bt_hdp_disconnected_t, 1);
1187 param->channel_id = channel_id;
1188 memcpy(¶m->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1190 g_dbus_proxy_call(hdp_proxy, "DestroyChannel",
1191 g_variant_new("o", info->obj_channel_path),
1192 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1193 (GAsyncReadyCallback)__bt_hdp_disconnect_request_cb,
1198 g_object_unref(hdp_proxy);
1200 return BLUETOOTH_ERROR_NONE;