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 result = __bt_hdp_internal_create_application(data_type, role,
124 channel_type, app_handle);
129 static void __bt_hdp_obj_info_free(hdp_obj_info_t *info)
132 g_source_remove(info->watch_id);
134 g_free(info->obj_channel_path);
139 static int __bt_hdp_internal_create_application(unsigned int data_type,
141 bt_hdp_qos_type_t channel_type,
144 GDBusProxy *proxy = NULL;
145 GVariant *reply = NULL;
146 GVariantBuilder *builder;
148 const char *key_type;
150 hdp_app_list_t *list;
153 GDBusConnection *conn;
154 int ret = BLUETOOTH_ERROR_NONE;
158 conn = _bt_gdbus_get_system_gconn();
159 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
161 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
165 BLUEZ_HDP_MANAGER_INTERFACE,
169 BT_ERR("Unable to create proxy: %s", err->message);
171 return BLUETOOTH_ERROR_INTERNAL;
174 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
176 key_type = "DataType";
177 value = (guint16) data_type;
178 g_variant_builder_add(builder, "{sv}",
179 key_type, g_variant_new("q",
184 svalue = (role == HDP_ROLE_SINK) ? "Sink" : "Source";
185 g_variant_builder_add(builder, "{sv}",
186 key_type, g_variant_new("s",
189 key_type = "Description";
190 svalue = "Health Device";
191 g_variant_builder_add(builder, "{sv}",
192 key_type, g_variant_new("s",
195 if (role == HDP_ROLE_SOURCE) {
196 key_type = "ChannelType";
197 if (channel_type == HDP_QOS_RELIABLE)
199 else if (channel_type == HDP_QOS_STREAMING)
200 svalue = "streaming";
202 g_variant_builder_add(builder, "{sv}",
203 key_type, g_variant_new("s",
207 reply = g_dbus_proxy_call_sync(proxy, "CreateApplication",
208 g_variant_new("(a{sv})", builder),
209 G_DBUS_CALL_FLAGS_NONE, -1,
212 g_variant_builder_unref(builder);
213 g_object_unref(proxy);
216 BT_ERR(" HDP:dbus Can't create application");
218 BT_ERR("%s", err->message);
219 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
220 ret = BLUETOOTH_ERROR_ACCESS_DENIED;
222 ret = BLUETOOTH_ERROR_INTERNAL;
228 g_variant_get(reply, "(&o)", &app_path);
229 BT_DBG("Created health application: %s", (char *)app_path);
231 ret = __bt_hdp_internal_add_filter();
233 if (ret != BLUETOOTH_ERROR_NONE) {
234 BT_ERR("Funtion failed");
238 list = g_new0(hdp_app_list_t, 1);
239 list->app_handle = (void *)g_strdup(app_path);
240 *app_handle = list->app_handle;
242 g_app_list = g_slist_append(g_app_list, list);
244 g_variant_unref(reply);
245 return BLUETOOTH_ERROR_NONE;
248 static int __bt_hdp_add_filter_subscribe_signal(GDBusConnection *conn,
251 static guint subs_add_filter_id = 0;
254 return BLUETOOTH_ERROR_INVALID_PARAM;
257 if (subs_add_filter_id == 0) {
258 subs_add_filter_id = g_dbus_connection_signal_subscribe(
259 conn, NULL, BLUEZ_HDP_DEVICE_INTERFACE,
261 __bt_hdp_internal_event_filter, NULL, NULL);
264 if (subs_add_filter_id > 0) {
265 g_dbus_connection_signal_unsubscribe(conn,
267 subs_add_filter_id = 0;
270 return BLUETOOTH_ERROR_NONE;
273 static int __bt_hdp_internal_add_filter(void)
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;
283 g_hdp_dus_conn = _bt_gdbus_get_system_gconn();
284 retv_if(g_hdp_dus_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
287 return __bt_hdp_add_filter_subscribe_signal(g_hdp_dus_conn, TRUE);
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,
300 BT_DBG("Path = %s\n", object_path);
301 if (object_path == NULL || g_strcmp0(object_path, "/") == 0)
304 if (signal_name == NULL)
307 if (strcasecmp(signal_name, "ChannelConnected") == 0)
308 __bt_hdp_internal_handle_connect(parameters);
310 else if (strcasecmp(signal_name, "ChannelDeleted") == 0)
311 __bt_hdp_internal_handle_disconnect(parameters, object_path);
313 else if (strcasecmp(signal_name, "PropertyChanged") == 0)
314 __bt_hdp_internal_handle_property_changed(parameters);
319 static void __bt_hdp_internal_handle_connect(GVariant *parameters)
322 const char *obj_channel_path;
323 bt_user_info_t *user_info;
326 BT_INFO("*********Signal - ChannelConnected******\n\n");
327 g_variant_get(parameters, "(&o)", &obj_channel_path);
329 BT_INFO("Channel connected, Path = %s", obj_channel_path);
331 user_info = _bt_get_user_data(BT_COMMON);
332 if (user_info == NULL || user_info->cb == NULL)
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);
344 static void __bt_hdp_internal_handle_disconnect(GVariant *parameters,
345 const gchar *object_path)
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;
354 BT_INFO("+********Signal - ChannelDeleted ******\n\n");
355 BT_DBG("Path = %s", object_path);
357 g_variant_get(parameters, "(&o)", &obj_channel_path);
359 BT_INFO("Channel Deleted, Path = %s", obj_channel_path);
361 info = __bt_hdp_internal_gslist_obj_find_using_path(obj_channel_path);
363 BT_ERR("No obj info for ob_channel_path [%s]\n", obj_channel_path);
367 /*Since bluetoothd is not sending the ChannelDeleted signal */
368 _bt_convert_device_path_to_address(object_path, address);
370 _bt_convert_addr_string_to_type(device_addr.addr, address);
372 dis_ind.channel_id = info->fd;
373 dis_ind.device_address = device_addr;
375 user_info = _bt_get_user_data(BT_COMMON);
378 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
379 BLUETOOTH_ERROR_NONE, &dis_ind,
380 user_info->cb, user_info->user_data);
383 BT_DBG(" Removed connection from list\n");
385 __bt_hdp_obj_info_free(info);
389 static void __bt_hdp_internal_handle_property_changed(GVariant *parameters)
391 char *property = NULL;
392 GVariant *value = NULL;
394 char *obj_main_channel_path = NULL;
395 GVariantIter *property_iter;
397 BT_DBG("+*******Signal - PropertyChanged*******\n");
399 g_variant_get (parameters, "(a{sv})", &property_iter);
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);
409 g_variant_iter_free(property_iter);
412 g_variant_unref(value);
413 g_free(obj_main_channel_path);
414 BT_DBG("-*************\n");
417 static int __bt_hdp_internal_acquire_fd(const char *path)
419 char address[BT_ADDRESS_STRING_SIZE] = { 0, };
420 bluetooth_device_address_t device_addr = { {0} };
421 const char *property;
422 GVariant *value = NULL;
423 char *type_qos = NULL;
425 char *app_handle = NULL;
426 hdp_app_list_t *list = NULL;
427 GDBusProxy *proxy = NULL;
428 GVariant *reply = NULL;
429 GDBusConnection *conn;
430 bt_hdp_connected_t conn_ind;
433 bt_user_info_t *user_info;
435 GUnixFDList *out_fd_list;
437 GVariantIter *property_iter;
442 conn = _bt_gdbus_get_system_gconn();
443 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
445 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
449 BLUEZ_HDP_CHANNEL_INTERFACE,
453 BT_ERR("Unable to create proxy: %s", err->message);
455 return BLUETOOTH_ERROR_INTERNAL;
458 reply = g_dbus_proxy_call_with_unix_fd_list_sync (proxy,
461 G_DBUS_CALL_FLAGS_NONE,
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);
537 g_variant_iter_free(property_iter);
539 g_variant_unref(reply);
540 BT_DBG("QOS = %s, Device = %s, Apphandler = %s",
541 type_qos, device, app_handle);
543 if (NULL == type_qos || NULL == app_handle) {
544 BT_ERR("Pasing failed\n");
548 list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
550 /*Only process register with app handle receive the Connected event */
552 BT_ERR("**** Could not locate the list for %s*****\n", app_handle);
556 hdp_obj_info_t *info = g_new0(hdp_obj_info_t, 1);
558 info->obj_channel_path = g_strdup(path);
559 info->watch_id = __bt_hdp_internal_watch_fd(fd, info->obj_channel_path);
560 list->obj_info = g_slist_append(list->obj_info, info);
562 _bt_convert_device_path_to_address(path, address);
564 _bt_convert_addr_string_to_type(device_addr.addr, address);
566 conn_ind.app_handle = app_handle;
567 conn_ind.channel_id = fd;
568 conn_ind.device_address = device_addr;
569 conn_ind.type = (g_strcmp0(type_qos, "Reliable") == 0) ?
570 HDP_QOS_RELIABLE : HDP_QOS_STREAMING;
572 BT_DBG("Going to give callback\n");
574 user_info = _bt_get_user_data(BT_COMMON);
577 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
578 BLUETOOTH_ERROR_NONE, &conn_ind,
579 user_info->cb, user_info->user_data);
582 BT_DBG("Updated fd in the list*\n");
588 return BLUETOOTH_ERROR_NONE;
593 return BLUETOOTH_ERROR_INTERNAL;
596 static guint __bt_hdp_internal_watch_fd(int file_desc, const char *path)
603 gio = g_io_channel_unix_new(file_desc);
605 g_io_channel_set_close_on_unref(gio, TRUE);
607 id = g_io_add_watch(gio, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
608 __bt_hdp_internal_data_received, (void *)path);
614 static void __bt_hdp_internal_handle_disconnect_cb(int sk, const char *path)
616 char address[BT_ADDRESS_STRING_SIZE] = { 0, };
617 bluetooth_device_address_t device_addr = { {0} };
618 bt_hdp_disconnected_t dis_ind;
619 hdp_obj_info_t *info;
620 bt_user_info_t *user_info;
622 BT_INFO("******** Socket Error ******\n");
624 info = __bt_hdp_internal_gslist_obj_find_using_path(path);
625 ret_if(info == NULL);
627 /*Since bluetoothd is not sending the ChannelDeleted signal */
628 _bt_convert_device_path_to_address(path, address);
630 _bt_convert_addr_string_to_type(device_addr.addr, address);
632 dis_ind.channel_id = sk;
633 dis_ind.device_address = device_addr;
635 user_info = _bt_get_user_data(BT_COMMON);
638 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
639 BLUETOOTH_ERROR_NONE, &dis_ind,
640 user_info->cb, user_info->user_data);
643 BT_DBG(" Removed connection from list\n");
645 __bt_hdp_obj_info_free(info);
648 static gboolean __bt_hdp_internal_data_received(GIOChannel *gio,
649 GIOCondition cond, gpointer data)
651 char buff[HDP_BUFFER_SIZE] = { 0, };
654 bt_hdp_data_ind_t data_ind = { 0, };
655 const char *path = (const char *)data;
656 bt_user_info_t *user_info;
660 sk = g_io_channel_unix_get_fd(gio);
662 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
663 BT_DBG("GIOCondition %d.............path = %s\n", cond, path);
664 g_io_channel_shutdown(gio, TRUE, NULL);
665 g_io_channel_unref(gio);
666 __bt_hdp_internal_handle_disconnect_cb(sk, path);
670 act_read = recv(sk, (void *)buff, sizeof(buff), 0);
673 BT_DBG("Received data of %d\n", act_read);
675 BT_ERR("Read failed.....\n");
679 data_ind.channel_id = sk;
680 data_ind.buffer = buff;
681 data_ind.size = act_read;
683 user_info = _bt_get_user_data(BT_COMMON);
686 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DATA_RECEIVED,
687 BLUETOOTH_ERROR_NONE, &data_ind,
688 user_info->cb, user_info->user_data);
696 BT_EXPORT_API int bluetooth_hdp_deactivate(const char *app_handle)
700 BT_CHECK_ENABLED(return);
701 BT_CHECK_PARAMETER(app_handle, return);
703 return __bt_hdp_internal_destroy_application(app_handle);
706 static hdp_app_list_t *__bt_hdp_internal_gslist_find_app_handler(void *app_handle)
710 retv_if(g_app_list == NULL, NULL);
712 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
714 for (l = g_app_list; l != NULL; l = l->next) {
715 hdp_app_list_t *list = l->data;
718 if (0 == g_strcmp0((char *)list->app_handle,
726 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_fd(int fd)
731 retv_if(g_app_list == NULL, NULL);
733 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
735 for (l = g_app_list; l != NULL; l = l->next) {
736 hdp_app_list_t *list = l->data;
740 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
741 hdp_obj_info_t *info = iter->data;
752 static hdp_obj_info_t *__bt_hdp_internal_gslist_obj_find_using_path(const char *obj_channel_path)
756 hdp_obj_info_t *info = NULL;
758 retv_if(g_app_list == NULL, NULL);
760 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
761 for (l = g_app_list; l != NULL; l = l->next) {
762 hdp_app_list_t *list = l->data;
766 for (iter = list->obj_info; iter != NULL; iter = iter->next) {
771 if (0 == g_strcmp0(info->obj_channel_path, obj_channel_path)) {
772 list->obj_info = g_slist_remove(list->obj_info, info);
780 static gboolean __bt_hdp_internal_destroy_application_cb(gpointer data)
782 const char *app_handle;
783 hdp_app_list_t *list = NULL;
784 app_handle = (const char *)data;
788 list = __bt_hdp_internal_gslist_find_app_handler((void *)app_handle);
790 BT_ERR("**** list not found for %s ******\n", app_handle);
794 g_app_list = g_slist_remove(g_app_list, list);
796 g_free(list->app_handle);
797 g_slist_foreach(list->obj_info, (GFunc)__bt_hdp_obj_info_free, NULL);
800 BT_DBG("List length = %d\n", g_slist_length(g_app_list));
802 if (0 == g_slist_length(g_app_list))
803 __bt_hdp_internal_remove_filter();
808 static int __bt_hdp_internal_destroy_application(const char *app_handle)
810 GDBusProxy *proxy = NULL;
811 GVariant *reply = NULL;
813 GDBusConnection *conn;
814 int result = BLUETOOTH_ERROR_NONE;
816 conn = _bt_gdbus_get_system_gconn();
817 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
819 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
823 BLUEZ_HDP_MANAGER_INTERFACE,
827 BT_ERR("Unable to create proxy: %s", err->message);
829 return BLUETOOTH_ERROR_INTERNAL;
832 reply = g_dbus_proxy_call_sync(proxy, "DestroyApplication",
833 g_variant_new("o", app_handle),
834 G_DBUS_CALL_FLAGS_NONE,
839 g_object_unref(proxy);
841 BT_ERR(" HDP:dbus Can't Destroy application");
844 BT_ERR("%s", err->message);
845 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
846 result = BLUETOOTH_ERROR_ACCESS_DENIED;
848 result = BLUETOOTH_ERROR_INTERNAL;
854 g_variant_unref(reply);
856 BT_DBG("Destroyed health application: %s", (char *)app_handle);
858 g_idle_add(__bt_hdp_internal_destroy_application_cb,
859 (gpointer)app_handle);
861 return BLUETOOTH_ERROR_NONE;
864 static void __bt_hdp_internal_remove_filter(void)
868 ret_if(g_hdp_dus_conn == NULL);
870 __bt_hdp_add_filter_subscribe_signal(g_hdp_dus_conn, FALSE);
872 g_hdp_dus_conn = NULL; /*should not unref here, bcz no ++reff */
877 BT_EXPORT_API int bluetooth_hdp_send_data(unsigned int channel_id,
887 BT_CHECK_ENABLED(return);
889 if ((channel_id == 0) || (NULL == buffer) || (size == 0)) {
890 BT_ERR("Invalid arguments..\n");
891 return BLUETOOTH_ERROR_INVALID_PARAM;
894 switch (privilege_token) {
896 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_SEND_DATA);
898 if (result == BLUETOOTH_ERROR_NONE) {
899 privilege_token = 1; /* Have a permission */
900 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
901 BT_ERR("Don't have a privilege to use this API");
902 privilege_token = -1; /* Don't have a permission */
903 return BLUETOOTH_ERROR_PERMISSION_DEINED;
905 /* Just break - It is not related with permission error */
909 /* Already have a privilege */
912 return BLUETOOTH_ERROR_PERMISSION_DEINED;
914 /* Invalid privilge token value */
915 return BLUETOOTH_ERROR_INTERNAL;
918 while (wbytes < size) {
919 written = write(channel_id, buffer + wbytes, size - wbytes);
921 BT_ERR("write failed..\n");
922 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
927 return BLUETOOTH_ERROR_NONE;
930 static void __bt_hdp_connect_request_cb(GDBusProxy *hdp_proxy,
931 GAsyncResult *res, gpointer user_data)
934 char *obj_connect_path = NULL;
935 bt_hdp_connected_t *conn_ind = user_data;
936 bt_user_info_t *user_info;
937 GVariant *reply = NULL;
939 reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
941 g_object_unref(hdp_proxy);
945 BT_ERR("HDP connection Dbus Call Error: %s\n", err->message);
949 user_info = _bt_get_user_data(BT_COMMON);
952 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_CONNECTED,
953 BLUETOOTH_ERROR_CONNECTION_ERROR, conn_ind,
954 user_info->cb, user_info->user_data);
957 g_variant_get(reply, "(&o)", &obj_connect_path);
958 BT_DBG("Obj Path returned = %s\n", obj_connect_path);
960 g_free((void *)conn_ind->app_handle);
965 BT_EXPORT_API int bluetooth_hdp_connect(const char *app_handle,
966 bt_hdp_qos_type_t channel_type,
967 const bluetooth_device_address_t *device_address)
970 GDBusConnection *conn = NULL;
971 GDBusProxy *hdp_proxy = NULL;
972 bt_hdp_connected_t *param;
973 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
974 char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
975 char *dev_path = NULL;
980 BT_CHECK_ENABLED(return);
981 BT_CHECK_PARAMETER(app_handle, return);
982 BT_CHECK_PARAMETER(device_address, return);
984 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_CONNECT)
985 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
986 BT_ERR("Don't have a privilege to use this API");
987 return BLUETOOTH_ERROR_PERMISSION_DEINED;
990 if (channel_type == HDP_QOS_RELIABLE) {
992 } else if (channel_type == HDP_QOS_STREAMING) {
994 } else if (channel_type == HDP_QOS_ANY) {
997 BT_ERR("Invalid channel_type %d", channel_type);
998 return BLUETOOTH_ERROR_ACCESS_DENIED;
1001 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1004 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1005 g_clear_error(&err);
1006 return BLUETOOTH_ERROR_INTERNAL;
1009 /* If the adapter path is wrong, we can think the BT is not enabled. */
1010 if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1011 default_adapter_path) < 0) {
1012 BT_ERR("Could not get adapter path\n");
1013 g_object_unref(conn);
1014 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1017 _bt_convert_addr_type_to_string(address,
1018 (unsigned char *)device_address->addr);
1020 BT_DBG("create conection to %s", address);
1022 dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1024 if (dev_path == NULL) {
1025 g_object_unref(conn);
1026 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1029 g_strdelimit(dev_path, ":", '_');
1031 BT_DBG("path: %s", dev_path);
1033 hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1034 NULL, BT_BLUEZ_NAME,
1035 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1037 g_object_unref(conn);
1039 if (hdp_proxy == NULL) {
1040 BT_ERR("Failed to get the HDP server proxy\n");
1042 return BLUETOOTH_ERROR_NOT_PAIRED;
1045 BT_DBG("app path %s\n", app_handle);
1047 param = g_new0(bt_hdp_connected_t, 1);
1048 param->app_handle = g_strdup(app_handle);
1049 memcpy(¶m->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1050 param->type = channel_type;
1052 g_dbus_proxy_call(hdp_proxy, "CreateChannel",
1053 g_variant_new("(os)", app_handle, role),
1054 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1055 (GAsyncReadyCallback)__bt_hdp_connect_request_cb,
1060 return BLUETOOTH_ERROR_NONE;
1063 static void __bt_hdp_disconnect_request_cb(GDBusProxy *hdp_proxy,
1064 GAsyncResult *res, gpointer user_data)
1067 bt_hdp_disconnected_t *disconn_ind = user_data;
1068 bt_user_info_t *user_info;
1069 GVariant *reply = NULL;
1071 reply = g_dbus_proxy_call_finish(hdp_proxy, res, &err);
1072 g_object_unref(hdp_proxy);
1074 user_info = _bt_get_user_data(BT_COMMON);
1075 if (user_info == NULL || user_info->cb == NULL) {
1076 g_free(disconn_ind);
1078 g_clear_error(&err);
1081 g_variant_unref(reply);
1087 BT_ERR("HDP disconnection Dbus Call Error: %s\n",
1089 g_clear_error(&err);
1092 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1093 BLUETOOTH_ERROR_CONNECTION_ERROR, disconn_ind,
1094 user_info->cb, user_info->user_data);
1096 _bt_common_event_cb(BLUETOOTH_EVENT_HDP_DISCONNECTED,
1097 BLUETOOTH_ERROR_NONE, disconn_ind,
1098 user_info->cb, user_info->user_data);
1099 BT_INFO("HDP disconnection Dbus Call is done\n");
1100 g_variant_unref(reply);
1103 g_free(disconn_ind);
1106 BT_EXPORT_API int bluetooth_hdp_disconnect(unsigned int channel_id,
1107 const bluetooth_device_address_t *device_address)
1110 GDBusConnection *conn = NULL;
1111 GDBusProxy *hdp_proxy = NULL;
1112 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1113 char default_adapter_path[BT_ADAPTER_OBJECT_PATH_MAX + 1] = { 0 };
1114 char *dev_path = NULL;
1115 bt_hdp_disconnected_t *param;
1119 BT_CHECK_ENABLED(return);
1120 BT_CHECK_PARAMETER(device_address, return);
1122 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_HDP_DISCONNECT)
1123 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
1124 BT_ERR("Don't have a privilege to use this API");
1125 return BLUETOOTH_ERROR_PERMISSION_DEINED;
1128 hdp_obj_info_t *info =
1129 __bt_hdp_internal_gslist_obj_find_using_fd(channel_id);
1131 BT_ERR("*** Could not locate the list for %d*****\n",
1133 return BLUETOOTH_ERROR_INVALID_PARAM;
1136 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1139 BT_ERR("ERROR: Can't get on system bus [%s]", err->message);
1140 g_clear_error(&err);
1141 return BLUETOOTH_ERROR_INTERNAL;
1144 /* If the adapter path is wrong, we can think the BT is not enabled. */
1145 if (_bt_get_adapter_path(_bt_gdbus_get_system_gconn(),
1146 default_adapter_path) < 0) {
1147 BT_ERR("Could not get adapter path\n");
1148 g_object_unref(conn);
1149 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1152 _bt_convert_addr_type_to_string(address,
1153 (unsigned char *)device_address->addr);
1155 BT_DBG("create conection to %s\n", address);
1157 dev_path = g_strdup_printf("%s/dev_%s", default_adapter_path, address);
1159 if (dev_path == NULL) {
1160 g_object_unref(conn);
1161 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1164 g_strdelimit(dev_path, ":", '_');
1166 BT_DBG("path %s\n", dev_path);
1168 hdp_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1169 NULL, BT_BLUEZ_NAME,
1170 dev_path, BLUEZ_HDP_DEVICE_INTERFACE,
1173 g_object_unref(conn);
1175 if (hdp_proxy == NULL) {
1176 BT_ERR("Failed to get the HDP proxy\n");
1178 return BLUETOOTH_ERROR_NOT_PAIRED;
1181 param = g_new0(bt_hdp_disconnected_t, 1);
1182 param->channel_id = channel_id;
1183 memcpy(¶m->device_address, device_address, BLUETOOTH_ADDRESS_LENGTH);
1185 g_dbus_proxy_call(hdp_proxy, "DestroyChannel",
1186 g_variant_new("o", info->obj_channel_path),
1187 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
1188 (GAsyncReadyCallback)__bt_hdp_disconnect_request_cb,
1193 g_object_unref(hdp_proxy);
1195 return BLUETOOTH_ERROR_NONE;