3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <bluetooth/l2cap.h>
32 #include <dbus-common.h>
40 #include "hdp_types.h"
45 #define ECHO_TIMEOUT 1 /* second */
46 #define HDP_ECHO_LEN 15
48 static DBusConnection *connection = NULL;
50 static GSList *applications = NULL;
51 static GSList *devices = NULL;
52 static uint8_t next_app_id = HDP_MDEP_INITIAL;
54 static GSList *adapters;
56 static gboolean update_adapter(struct hdp_adapter *adapter);
57 static struct hdp_device *create_health_device(DBusConnection *conn,
58 struct btd_device *device);
59 static void free_echo_data(struct hdp_echo_data *edata);
61 struct hdp_create_dc {
64 struct hdp_application *app;
65 struct hdp_device *dev;
69 mcap_mdl_operation_cb cb;
72 struct hdp_tmp_dc_data {
75 struct hdp_channel *hdp_chann;
77 mcap_mdl_operation_cb cb;
80 struct hdp_echo_data {
81 gboolean echo_done; /* Is a echo was already done */
82 gpointer buf; /* echo packet sent */
83 uint tid; /* echo timeout */
86 static struct hdp_channel *hdp_channel_ref(struct hdp_channel *chan)
93 DBG("health_channel_ref(%p): ref=%d", chan, chan->ref);
97 static void free_health_channel(struct hdp_channel *chan)
99 if (chan->mdep == HDP_MDEP_ECHO) {
100 free_echo_data(chan->edata);
104 mcap_mdl_unref(chan->mdl);
105 hdp_application_unref(chan->app);
106 health_device_unref(chan->dev);
111 static void hdp_channel_unref(struct hdp_channel *chan)
117 DBG("health_channel_unref(%p): ref=%d", chan, chan->ref);
122 free_health_channel(chan);
125 static void free_hdp_create_dc(struct hdp_create_dc *dc_data)
127 dbus_message_unref(dc_data->msg);
128 dbus_connection_unref(dc_data->conn);
129 hdp_application_unref(dc_data->app);
130 health_device_unref(dc_data->dev);
135 static struct hdp_create_dc *hdp_create_data_ref(struct hdp_create_dc *dc_data)
139 DBG("hdp_create_data_ref(%p): ref=%d", dc_data, dc_data->ref);
144 static void hdp_create_data_unref(struct hdp_create_dc *dc_data)
148 DBG("hdp_create_data_unref(%p): ref=%d", dc_data, dc_data->ref);
150 if (dc_data->ref > 0)
153 free_hdp_create_dc(dc_data);
156 static void free_hdp_conn_dc(struct hdp_tmp_dc_data *data)
158 dbus_message_unref(data->msg);
159 dbus_connection_unref(data->conn);
160 hdp_channel_unref(data->hdp_chann);
165 static struct hdp_tmp_dc_data *hdp_tmp_dc_data_ref(struct hdp_tmp_dc_data *data)
169 DBG("hdp_conn_data_ref(%p): ref=%d", data, data->ref);
174 static void hdp_tmp_dc_data_unref(struct hdp_tmp_dc_data *data)
178 DBG("hdp_conn_data_unref(%p): ref=%d", data, data->ref);
183 free_hdp_conn_dc(data);
186 static int cmp_app_id(gconstpointer a, gconstpointer b)
188 const struct hdp_application *app = a;
189 const uint8_t *id = b;
191 return app->id - *id;
194 static int cmp_adapter(gconstpointer a, gconstpointer b)
196 const struct hdp_adapter *hdp_adapter = a;
197 const struct btd_adapter *adapter = b;
199 if (hdp_adapter->btd_adapter == adapter)
205 static int cmp_device(gconstpointer a, gconstpointer b)
207 const struct hdp_device *hdp_device = a;
208 const struct btd_device *device = b;
210 if (hdp_device->dev == device)
216 static gint cmp_dev_addr(gconstpointer a, gconstpointer dst)
218 const struct hdp_device *device = a;
221 device_get_address(device->dev, &addr, NULL);
222 return bacmp(&addr, dst);
225 static gint cmp_dev_mcl(gconstpointer a, gconstpointer mcl)
227 const struct hdp_device *device = a;
229 if (mcl == device->mcl)
234 static gint cmp_chan_mdlid(gconstpointer a, gconstpointer b)
236 const struct hdp_channel *chan = a;
237 const uint16_t *mdlid = b;
239 return chan->mdlid - *mdlid;
242 static gint cmp_chan_path(gconstpointer a, gconstpointer b)
244 const struct hdp_channel *chan = a;
245 const char *path = b;
247 return g_ascii_strcasecmp(chan->path, path);
250 static gint cmp_chan_mdl(gconstpointer a, gconstpointer mdl)
252 const struct hdp_channel *chan = a;
254 if (chan->mdl == mdl)
259 static uint8_t get_app_id(void)
261 uint8_t id = next_app_id;
264 GSList *l = g_slist_find_custom(applications, &id, cmp_app_id);
267 next_app_id = (id % HDP_MDEP_FINAL) + 1;
270 id = (id % HDP_MDEP_FINAL) + 1;
271 } while (id != next_app_id);
273 /* No more ids available */
277 static int cmp_app(gconstpointer a, gconstpointer b)
279 const struct hdp_application *app = a;
281 return g_strcmp0(app->path, b);
284 static gboolean set_app_path(struct hdp_application *app)
286 app->id = get_app_id();
289 app->path = g_strdup_printf(MANAGER_PATH "/health_app_%d", app->id);
294 static void device_unref_mcl(struct hdp_device *hdp_device)
296 if (hdp_device->mcl == NULL)
299 mcap_close_mcl(hdp_device->mcl, FALSE);
300 mcap_mcl_unref(hdp_device->mcl);
301 hdp_device->mcl = NULL;
302 hdp_device->mcl_conn = FALSE;
305 static void free_health_device(struct hdp_device *device)
307 if (device->conn != NULL) {
308 dbus_connection_unref(device->conn);
312 if (device->dev != NULL) {
313 btd_device_unref(device->dev);
317 device_unref_mcl(device);
322 static void remove_application(struct hdp_application *app)
324 DBG("Application %s deleted", app->path);
325 hdp_application_unref(app);
327 g_slist_foreach(adapters, (GFunc) update_adapter, NULL);
330 static void client_disconnected(DBusConnection *conn, void *user_data)
332 struct hdp_application *app = user_data;
334 DBG("Client disconnected from the bus, deleting hdp application");
335 applications = g_slist_remove(applications, app);
337 app->dbus_watcher = 0; /* Watcher shouldn't be freed in this case */
338 remove_application(app);
341 static DBusMessage *manager_create_application(DBusConnection *conn,
342 DBusMessage *msg, void *user_data)
344 struct hdp_application *app;
346 DBusMessageIter iter;
349 dbus_message_iter_init(msg, &iter);
350 app = hdp_get_app_config(&iter, &err);
353 return btd_error_invalid_args(msg);
356 name = dbus_message_get_sender(msg);
358 hdp_application_unref(app);
359 return g_dbus_create_error(msg,
360 ERROR_INTERFACE ".HealthError",
361 "Can't get sender name");
364 if (!set_app_path(app)) {
365 hdp_application_unref(app);
366 return g_dbus_create_error(msg,
367 ERROR_INTERFACE ".HealthError",
368 "Can't get a valid id for the application");
371 app->oname = g_strdup(name);
372 app->conn = dbus_connection_ref(conn);
374 applications = g_slist_prepend(applications, app);
376 app->dbus_watcher = g_dbus_add_disconnect_watch(conn, name,
377 client_disconnected, app, NULL);
378 g_slist_foreach(adapters, (GFunc) update_adapter, NULL);
380 DBG("Health application created with id %s", app->path);
382 return g_dbus_create_reply(msg, DBUS_TYPE_OBJECT_PATH, &app->path,
386 static DBusMessage *manager_destroy_application(DBusConnection *conn,
387 DBusMessage *msg, void *user_data)
390 struct hdp_application *app;
393 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
395 return btd_error_invalid_args(msg);
397 l = g_slist_find_custom(applications, path, cmp_app);
400 return g_dbus_create_error(msg,
401 ERROR_INTERFACE ".InvalidArguments",
402 "Invalid arguments in method call, "
403 "no such application");
406 applications = g_slist_remove(applications, app);
408 remove_application(app);
410 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
413 static void manager_path_unregister(gpointer data)
415 g_slist_foreach(applications, (GFunc) hdp_application_unref, NULL);
417 g_slist_free(applications);
420 g_slist_foreach(adapters, (GFunc) update_adapter, NULL);
423 static const GDBusMethodTable health_manager_methods[] = {
424 { GDBUS_METHOD("CreateApplication",
425 GDBUS_ARGS({ "config", "a{sv}" }),
426 GDBUS_ARGS({ "application", "o" }),
427 manager_create_application) },
428 { GDBUS_METHOD("DestroyApplication",
429 GDBUS_ARGS({ "application", "o" }), NULL,
430 manager_destroy_application) },
434 static DBusMessage *channel_get_properties(DBusConnection *conn,
435 DBusMessage *msg, void *user_data)
437 struct hdp_channel *chan = user_data;
438 DBusMessageIter iter, dict;
443 reply = dbus_message_new_method_return(msg);
447 dbus_message_iter_init_append(reply, &iter);
449 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
450 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
451 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
452 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
454 path = device_get_path(chan->dev->dev);
455 dict_append_entry(&dict, "Device", DBUS_TYPE_OBJECT_PATH, &path);
457 path = chan->app->path;
458 dict_append_entry(&dict, "Application", DBUS_TYPE_OBJECT_PATH, &path);
460 if (chan->config == HDP_RELIABLE_DC)
461 type = g_strdup("Reliable");
463 type = g_strdup("Streaming");
465 dict_append_entry(&dict, "Type", DBUS_TYPE_STRING, &type);
469 dbus_message_iter_close_container(&iter, &dict);
474 static void hdp_tmp_dc_data_destroy(gpointer data)
476 struct hdp_tmp_dc_data *hdp_conn = data;
478 hdp_tmp_dc_data_unref(hdp_conn);
481 static void abort_mdl_cb(GError *err, gpointer data)
484 error("Aborting error: %s", err->message);
487 static void hdp_mdl_reconn_cb(struct mcap_mdl *mdl, GError *err, gpointer data)
489 struct hdp_tmp_dc_data *dc_data = data;
494 struct hdp_channel *chan = dc_data->hdp_chann;
497 error("%s", err->message);
498 reply = g_dbus_create_error(dc_data->msg,
499 ERROR_INTERFACE ".HealthError",
500 "Cannot reconnect: %s", err->message);
501 g_dbus_send_message(dc_data->conn, reply);
503 /* Send abort request because remote side */
504 /* is now in PENDING state */
505 if (!mcap_mdl_abort(chan->mdl, abort_mdl_cb, NULL, NULL,
507 error("%s", gerr->message);
513 fd = mcap_mdl_get_fd(dc_data->hdp_chann->mdl);
515 reply = g_dbus_create_error(dc_data->msg,
516 ERROR_INTERFACE ".HealthError",
517 "Cannot get file descriptor");
518 g_dbus_send_message(dc_data->conn, reply);
522 reply = g_dbus_create_reply(dc_data->msg, DBUS_TYPE_UNIX_FD,
523 &fd, DBUS_TYPE_INVALID);
524 g_dbus_send_message(dc_data->conn, reply);
526 g_dbus_emit_signal(dc_data->conn,
527 device_get_path(dc_data->hdp_chann->dev->dev),
528 HEALTH_DEVICE, "ChannelConnected",
529 DBUS_TYPE_OBJECT_PATH, &dc_data->hdp_chann->path,
533 static void hdp_get_dcpsm_cb(uint16_t dcpsm, gpointer user_data, GError *err)
535 struct hdp_tmp_dc_data *hdp_conn = user_data;
536 struct hdp_channel *hdp_chann = hdp_conn->hdp_chann;
541 hdp_conn->cb(hdp_chann->mdl, err, hdp_conn);
545 if (hdp_chann->config == HDP_RELIABLE_DC)
546 mode = L2CAP_MODE_ERTM;
548 mode = L2CAP_MODE_STREAMING;
550 if (mcap_connect_mdl(hdp_chann->mdl, mode, dcpsm, hdp_conn->cb,
551 hdp_tmp_dc_data_ref(hdp_conn),
552 hdp_tmp_dc_data_destroy, &gerr))
555 hdp_tmp_dc_data_unref(hdp_conn);
556 hdp_conn->cb(hdp_chann->mdl, err, hdp_conn);
560 static void device_reconnect_mdl_cb(struct mcap_mdl *mdl, GError *err,
563 struct hdp_tmp_dc_data *dc_data = data;
568 reply = g_dbus_create_error(dc_data->msg,
569 ERROR_INTERFACE ".HealthError",
570 "Cannot reconnect: %s", err->message);
571 g_dbus_send_message(dc_data->conn, reply);
575 dc_data->cb = hdp_mdl_reconn_cb;
577 if (hdp_get_dcpsm(dc_data->hdp_chann->dev, hdp_get_dcpsm_cb,
578 hdp_tmp_dc_data_ref(dc_data),
579 hdp_tmp_dc_data_destroy, &gerr))
582 error("%s", gerr->message);
584 reply = g_dbus_create_error(dc_data->msg,
585 ERROR_INTERFACE ".HealthError",
586 "Cannot reconnect: %s", gerr->message);
587 g_dbus_send_message(dc_data->conn, reply);
588 hdp_tmp_dc_data_unref(dc_data);
591 /* Send abort request because remote side is now in PENDING state */
592 if (!mcap_mdl_abort(mdl, abort_mdl_cb, NULL, NULL, &gerr)) {
593 error("%s", gerr->message);
598 static DBusMessage *channel_acquire_continue(struct hdp_tmp_dc_data *data,
606 return g_dbus_create_error(data->msg,
607 ERROR_INTERFACE ".HealthError",
611 fd = mcap_mdl_get_fd(data->hdp_chann->mdl);
613 return g_dbus_create_reply(data->msg, DBUS_TYPE_UNIX_FD, &fd,
616 hdp_tmp_dc_data_ref(data);
617 if (mcap_reconnect_mdl(data->hdp_chann->mdl, device_reconnect_mdl_cb,
618 data, hdp_tmp_dc_data_destroy, &gerr))
620 #ifdef __TIZEN_PATCH__
621 reply = g_dbus_create_error(data->msg, ERROR_INTERFACE ".HealthError",
622 "Cannot reconnect: %s", gerr->message);
624 hdp_tmp_dc_data_unref(data);
626 hdp_tmp_dc_data_unref(data);
627 reply = g_dbus_create_error(data->msg, ERROR_INTERFACE ".HealthError",
628 "Cannot reconnect: %s", gerr->message);
635 static void channel_acquire_cb(gpointer data, GError *err)
637 struct hdp_tmp_dc_data *dc_data = data;
640 reply = channel_acquire_continue(data, err);
643 g_dbus_send_message(dc_data->conn, reply);
646 static DBusMessage *channel_acquire(DBusConnection *conn,
647 DBusMessage *msg, void *user_data)
649 struct hdp_channel *chan = user_data;
650 struct hdp_tmp_dc_data *dc_data;
654 dc_data = g_new0(struct hdp_tmp_dc_data, 1);
655 dc_data->conn = dbus_connection_ref(conn);
656 dc_data->msg = dbus_message_ref(msg);
657 dc_data->hdp_chann = hdp_channel_ref(chan);
659 if (chan->dev->mcl_conn) {
660 reply = channel_acquire_continue(hdp_tmp_dc_data_ref(dc_data),
662 hdp_tmp_dc_data_unref(dc_data);
666 if (hdp_establish_mcl(chan->dev, channel_acquire_cb,
667 hdp_tmp_dc_data_ref(dc_data),
668 hdp_tmp_dc_data_destroy, &gerr))
671 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
672 "%s", gerr->message);
673 hdp_tmp_dc_data_unref(dc_data);
679 static void close_mdl(struct hdp_channel *hdp_chann)
683 fd = mcap_mdl_get_fd(hdp_chann->mdl);
690 static DBusMessage *channel_release(DBusConnection *conn,
691 DBusMessage *msg, void *user_data)
693 struct hdp_channel *hdp_chann = user_data;
695 close_mdl(hdp_chann);
697 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
700 static void free_echo_data(struct hdp_echo_data *edata)
706 g_source_remove(edata->tid);
708 if (edata->buf != NULL)
715 static void health_channel_destroy(void *data)
717 struct hdp_channel *hdp_chan = data;
718 struct hdp_device *dev = hdp_chan->dev;
720 DBG("Destroy Health Channel %s", hdp_chan->path);
721 if (g_slist_find(dev->channels, hdp_chan) == NULL)
724 dev->channels = g_slist_remove(dev->channels, hdp_chan);
726 if (hdp_chan->mdep != HDP_MDEP_ECHO)
727 g_dbus_emit_signal(dev->conn, device_get_path(dev->dev),
728 HEALTH_DEVICE, "ChannelDeleted",
729 DBUS_TYPE_OBJECT_PATH, &hdp_chan->path,
732 if (hdp_chan == dev->fr) {
733 hdp_channel_unref(dev->fr);
738 hdp_channel_unref(hdp_chan);
741 static const GDBusMethodTable health_channels_methods[] = {
742 { GDBUS_METHOD("GetProperties",
743 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
744 channel_get_properties) },
745 { GDBUS_ASYNC_METHOD("Acquire",
746 NULL, GDBUS_ARGS({ "fd", "h" }),
748 { GDBUS_METHOD("Release", NULL, NULL, channel_release) },
752 static struct hdp_channel *create_channel(struct hdp_device *dev,
754 struct mcap_mdl *mdl,
756 struct hdp_application *app,
759 struct hdp_channel *hdp_chann;
764 hdp_chann = g_new0(struct hdp_channel, 1);
765 hdp_chann->config = config;
766 hdp_chann->dev = health_device_ref(dev);
767 hdp_chann->mdlid = mdlid;
770 hdp_chann->mdl = mcap_mdl_ref(mdl);
773 hdp_chann->mdep = app->id;
774 hdp_chann->app = hdp_application_ref(app);
776 hdp_chann->edata = g_new0(struct hdp_echo_data, 1);
778 hdp_chann->path = g_strdup_printf("%s/chan%d",
779 device_get_path(hdp_chann->dev->dev),
782 dev->channels = g_slist_append(dev->channels,
783 hdp_channel_ref(hdp_chann));
785 if (hdp_chann->mdep == HDP_MDEP_ECHO)
786 return hdp_channel_ref(hdp_chann);
788 if (!g_dbus_register_interface(dev->conn, hdp_chann->path,
790 health_channels_methods, NULL, NULL,
791 hdp_chann, health_channel_destroy)) {
792 g_set_error(err, HDP_ERROR, HDP_UNSPECIFIED_ERROR,
793 "Can't register the channel interface");
794 health_channel_destroy(hdp_chann);
798 return hdp_channel_ref(hdp_chann);
801 static void remove_channels(struct hdp_device *dev)
803 struct hdp_channel *chan;
806 while (dev->channels != NULL) {
807 chan = dev->channels->data;
809 path = g_strdup(chan->path);
810 if (!g_dbus_unregister_interface(dev->conn, path,
812 health_channel_destroy(chan);
817 static void close_device_con(struct hdp_device *dev, gboolean cache)
819 if (dev->mcl == NULL)
822 mcap_close_mcl(dev->mcl, cache);
823 dev->mcl_conn = FALSE;
828 device_unref_mcl(dev);
829 remove_channels(dev);
831 if (!dev->sdp_present) {
834 path = device_get_path(dev->dev);
835 g_dbus_unregister_interface(dev->conn, path, HEALTH_DEVICE);
839 static int send_echo_data(int sock, const void *buf, uint32_t size)
841 const uint8_t *buf_b = buf;
844 while (sent < size) {
845 int n = write(sock, buf_b + sent, size - sent);
854 static gboolean serve_echo(GIOChannel *io_chan, GIOCondition cond,
857 struct hdp_channel *chan = data;
858 uint8_t buf[MCAP_DC_MTU];
861 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
862 hdp_channel_unref(chan);
866 if (chan->edata->echo_done)
869 chan->edata->echo_done = TRUE;
871 fd = g_io_channel_unix_get_fd(io_chan);
872 len = read(fd, buf, sizeof(buf));
874 if (send_echo_data(fd, buf, len) >= 0)
878 close_device_con(chan->dev, FALSE);
879 hdp_channel_unref(chan);
883 static gboolean check_channel_conf(struct hdp_channel *chan)
891 fd = mcap_mdl_get_fd(chan->mdl);
894 io = g_io_channel_unix_new(fd);
896 if (!bt_io_get(io, BT_IO_L2CAP, &err,
897 BT_IO_OPT_MODE, &mode,
898 BT_IO_OPT_IMTU, &imtu,
899 BT_IO_OPT_OMTU, &omtu,
900 BT_IO_OPT_INVALID)) {
901 error("Error: %s", err->message);
902 g_io_channel_unref(io);
907 g_io_channel_unref(io);
909 switch (chan->config) {
910 case HDP_RELIABLE_DC:
911 if (mode != L2CAP_MODE_ERTM)
914 case HDP_STREAMING_DC:
915 if (mode != L2CAP_MODE_STREAMING)
919 error("Error: Connected with unknown configuration");
923 DBG("MDL imtu %d omtu %d Channel imtu %d omtu %d", imtu, omtu,
924 chan->imtu, chan->omtu);
931 if (chan->imtu != imtu || chan->omtu != omtu)
937 static void hdp_mcap_mdl_connected_cb(struct mcap_mdl *mdl, void *data)
939 struct hdp_device *dev = data;
940 struct hdp_channel *chan;
942 DBG("hdp_mcap_mdl_connected_cb");
943 if (dev->ndc == NULL)
947 if (chan->mdl == NULL)
948 chan->mdl = mcap_mdl_ref(mdl);
950 if (g_slist_find(dev->channels, chan) == NULL)
951 dev->channels = g_slist_prepend(dev->channels,
952 hdp_channel_ref(chan));
954 if (!check_channel_conf(chan)) {
959 if (chan->mdep == HDP_MDEP_ECHO) {
963 fd = mcap_mdl_get_fd(chan->mdl);
967 chan->edata->echo_done = FALSE;
968 io = g_io_channel_unix_new(fd);
969 g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL | G_IO_IN,
970 serve_echo, hdp_channel_ref(chan));
971 g_io_channel_unref(io);
975 g_dbus_emit_signal(dev->conn, device_get_path(dev->dev), HEALTH_DEVICE,
977 DBUS_TYPE_OBJECT_PATH, &chan->path,
983 dev->fr = hdp_channel_ref(chan);
985 emit_property_changed(dev->conn, device_get_path(dev->dev),
986 HEALTH_DEVICE, "MainChannel",
987 DBUS_TYPE_OBJECT_PATH, &dev->fr->path);
990 hdp_channel_unref(dev->ndc);
994 static void hdp_mcap_mdl_closed_cb(struct mcap_mdl *mdl, void *data)
996 /* struct hdp_device *dev = data; */
998 DBG("hdp_mcap_mdl_closed_cb");
1003 static void hdp_mcap_mdl_deleted_cb(struct mcap_mdl *mdl, void *data)
1005 struct hdp_device *dev = data;
1006 struct hdp_channel *chan;
1010 DBG("hdp_mcap_mdl_deleted_cb");
1011 l = g_slist_find_custom(dev->channels, mdl, cmp_chan_mdl);
1017 path = g_strdup(chan->path);
1018 if (!g_dbus_unregister_interface(dev->conn, path, HEALTH_CHANNEL))
1019 health_channel_destroy(chan);
1023 static void hdp_mcap_mdl_aborted_cb(struct mcap_mdl *mdl, void *data)
1025 struct hdp_device *dev = data;
1027 DBG("hdp_mcap_mdl_aborted_cb");
1028 if (dev->ndc == NULL)
1031 dev->ndc->mdl = mcap_mdl_ref(mdl);
1033 if (g_slist_find(dev->channels, dev->ndc) == NULL)
1034 dev->channels = g_slist_prepend(dev->channels,
1035 hdp_channel_ref(dev->ndc));
1037 if (dev->ndc->mdep != HDP_MDEP_ECHO)
1038 g_dbus_emit_signal(dev->conn, device_get_path(dev->dev),
1039 HEALTH_DEVICE, "ChannelConnected",
1040 DBUS_TYPE_OBJECT_PATH, &dev->ndc->path,
1043 hdp_channel_unref(dev->ndc);
1047 static uint8_t hdp2l2cap_mode(uint8_t hdp_mode)
1049 return hdp_mode == HDP_STREAMING_DC ? L2CAP_MODE_STREAMING :
1053 static uint8_t hdp_mcap_mdl_conn_req_cb(struct mcap_mcl *mcl, uint8_t mdepid,
1054 uint16_t mdlid, uint8_t *conf, void *data)
1056 struct hdp_device *dev = data;
1057 struct hdp_application *app;
1061 DBG("Data channel request");
1063 if (mdepid == HDP_MDEP_ECHO) {
1065 case HDP_NO_PREFERENCE_DC:
1066 *conf = HDP_RELIABLE_DC;
1067 case HDP_RELIABLE_DC:
1069 case HDP_STREAMING_DC:
1070 return MCAP_CONFIGURATION_REJECTED;
1072 /* Special case defined in HDP spec 3.4. When an invalid
1073 * configuration is received we shall close the MCL when
1074 * we are still processing the callback. */
1075 close_device_con(dev, FALSE);
1076 return MCAP_CONFIGURATION_REJECTED; /* not processed */
1079 if (!mcap_set_data_chan_mode(dev->hdp_adapter->mi,
1080 L2CAP_MODE_ERTM, &err)) {
1081 error("Error: %s", err->message);
1083 return MCAP_MDL_BUSY;
1086 dev->ndc = create_channel(dev, *conf, NULL, mdlid, NULL, NULL);
1087 if (dev->ndc == NULL)
1088 return MCAP_MDL_BUSY;
1090 return MCAP_SUCCESS;
1093 l = g_slist_find_custom(applications, &mdepid, cmp_app_id);
1095 return MCAP_INVALID_MDEP;
1099 /* Check if is the first dc if so,
1100 * only reliable configuration is allowed */
1102 case HDP_NO_PREFERENCE_DC:
1103 if (app->role == HDP_SINK)
1104 return MCAP_CONFIGURATION_REJECTED;
1105 else if (dev->fr && app->chan_type_set)
1106 *conf = app->chan_type;
1108 *conf = HDP_RELIABLE_DC;
1110 case HDP_STREAMING_DC:
1111 if (!dev->fr || app->role == HDP_SOURCE)
1112 return MCAP_CONFIGURATION_REJECTED;
1113 case HDP_RELIABLE_DC:
1114 if (app->role == HDP_SOURCE)
1115 return MCAP_CONFIGURATION_REJECTED;
1118 /* Special case defined in HDP spec 3.4. When an invalid
1119 * configuration is received we shall close the MCL when
1120 * we are still processing the callback. */
1121 close_device_con(dev, FALSE);
1122 return MCAP_CONFIGURATION_REJECTED; /* not processed */
1125 l = g_slist_find_custom(dev->channels, &mdlid, cmp_chan_mdlid);
1127 struct hdp_channel *chan = l->data;
1130 path = g_strdup(chan->path);
1131 g_dbus_unregister_interface(dev->conn, path, HEALTH_CHANNEL);
1135 if (!mcap_set_data_chan_mode(dev->hdp_adapter->mi,
1136 hdp2l2cap_mode(*conf), &err)) {
1137 error("Error: %s", err->message);
1139 return MCAP_MDL_BUSY;
1142 dev->ndc = create_channel(dev, *conf, NULL, mdlid, app, NULL);
1143 if (dev->ndc == NULL)
1144 return MCAP_MDL_BUSY;
1146 return MCAP_SUCCESS;
1149 static uint8_t hdp_mcap_mdl_reconn_req_cb(struct mcap_mdl *mdl, void *data)
1151 struct hdp_device *dev = data;
1152 struct hdp_channel *chan;
1156 l = g_slist_find_custom(dev->channels, mdl, cmp_chan_mdl);
1158 return MCAP_INVALID_MDL;
1162 if (dev->fr == NULL && chan->config != HDP_RELIABLE_DC &&
1163 chan->mdep != HDP_MDEP_ECHO)
1164 return MCAP_UNSPECIFIED_ERROR;
1166 if (!mcap_set_data_chan_mode(dev->hdp_adapter->mi,
1167 hdp2l2cap_mode(chan->config), &err)) {
1168 error("Error: %s", err->message);
1170 return MCAP_MDL_BUSY;
1173 dev->ndc = hdp_channel_ref(chan);
1175 return MCAP_SUCCESS;
1178 gboolean hdp_set_mcl_cb(struct hdp_device *device, GError **err)
1182 if (device->mcl == NULL)
1185 ret = mcap_mcl_set_cb(device->mcl, device, err,
1186 MCAP_MDL_CB_CONNECTED, hdp_mcap_mdl_connected_cb,
1187 MCAP_MDL_CB_CLOSED, hdp_mcap_mdl_closed_cb,
1188 MCAP_MDL_CB_DELETED, hdp_mcap_mdl_deleted_cb,
1189 MCAP_MDL_CB_ABORTED, hdp_mcap_mdl_aborted_cb,
1190 MCAP_MDL_CB_REMOTE_CONN_REQ, hdp_mcap_mdl_conn_req_cb,
1191 MCAP_MDL_CB_REMOTE_RECONN_REQ, hdp_mcap_mdl_reconn_req_cb,
1192 MCAP_MDL_CB_INVALID);
1197 error("Can't set mcl callbacks, closing mcl");
1198 close_device_con(device, TRUE);
1203 static void mcl_connected(struct mcap_mcl *mcl, gpointer data)
1205 struct hdp_device *hdp_device;
1209 mcap_mcl_get_addr(mcl, &addr);
1210 l = g_slist_find_custom(devices, &addr, cmp_dev_addr);
1212 struct hdp_adapter *hdp_adapter = data;
1213 struct btd_device *device;
1217 device = adapter_get_device(connection,
1218 hdp_adapter->btd_adapter, str);
1221 hdp_device = create_health_device(connection, device);
1224 devices = g_slist_append(devices, hdp_device);
1226 hdp_device = l->data;
1228 hdp_device->mcl = mcap_mcl_ref(mcl);
1229 hdp_device->mcl_conn = TRUE;
1231 DBG("New mcl connected from %s", device_get_path(hdp_device->dev));
1233 hdp_set_mcl_cb(hdp_device, NULL);
1236 static void mcl_reconnected(struct mcap_mcl *mcl, gpointer data)
1238 struct hdp_device *hdp_device;
1241 l = g_slist_find_custom(devices, mcl, cmp_dev_mcl);
1245 hdp_device = l->data;
1246 hdp_device->mcl_conn = TRUE;
1248 DBG("MCL reconnected %s", device_get_path(hdp_device->dev));
1250 hdp_set_mcl_cb(hdp_device, NULL);
1253 static void mcl_disconnected(struct mcap_mcl *mcl, gpointer data)
1255 struct hdp_device *hdp_device;
1258 l = g_slist_find_custom(devices, mcl, cmp_dev_mcl);
1262 hdp_device = l->data;
1263 hdp_device->mcl_conn = FALSE;
1265 DBG("Mcl disconnected %s", device_get_path(hdp_device->dev));
1268 static void mcl_uncached(struct mcap_mcl *mcl, gpointer data)
1270 struct hdp_device *hdp_device;
1274 l = g_slist_find_custom(devices, mcl, cmp_dev_mcl);
1278 hdp_device = l->data;
1279 device_unref_mcl(hdp_device);
1281 if (hdp_device->sdp_present)
1284 /* Because remote device hasn't announced an HDP record */
1285 /* the Bluetooth daemon won't notify when the device shall */
1286 /* be removed. Then we have to remove the HealthDevice */
1287 /* interface manually */
1288 path = device_get_path(hdp_device->dev);
1289 g_dbus_unregister_interface(hdp_device->conn, path, HEALTH_DEVICE);
1290 DBG("Mcl uncached %s", path);
1293 static void check_devices_mcl(void)
1295 struct hdp_device *dev;
1296 GSList *l, *to_delete = NULL;
1298 for (l = devices; l; l = l->next) {
1300 device_unref_mcl(dev);
1302 if (!dev->sdp_present)
1303 to_delete = g_slist_append(to_delete, dev);
1305 remove_channels(dev);
1308 for (l = to_delete; l; l = l->next) {
1311 path = device_get_path(dev->dev);
1312 g_dbus_unregister_interface(dev->conn, path, HEALTH_DEVICE);
1315 g_slist_free(to_delete);
1318 static void release_adapter_instance(struct hdp_adapter *hdp_adapter)
1320 if (hdp_adapter->mi == NULL)
1323 check_devices_mcl();
1324 mcap_release_instance(hdp_adapter->mi);
1325 mcap_instance_unref(hdp_adapter->mi);
1326 hdp_adapter->mi = NULL;
1329 static gboolean update_adapter(struct hdp_adapter *hdp_adapter)
1334 if (applications == NULL) {
1335 release_adapter_instance(hdp_adapter);
1339 if (hdp_adapter->mi != NULL)
1342 adapter_get_address(hdp_adapter->btd_adapter, &addr);
1343 hdp_adapter->mi = mcap_create_instance(&addr, BT_IO_SEC_MEDIUM, 0, 0,
1344 mcl_connected, mcl_reconnected,
1345 mcl_disconnected, mcl_uncached,
1346 NULL, /* CSP is not used by now */
1349 if (hdp_adapter->mi == NULL) {
1350 error("Error creating the MCAP instance: %s", err->message);
1355 hdp_adapter->ccpsm = mcap_get_ctrl_psm(hdp_adapter->mi, &err);
1357 error("Error getting MCAP control PSM: %s", err->message);
1361 hdp_adapter->dcpsm = mcap_get_data_psm(hdp_adapter->mi, &err);
1363 error("Error getting MCAP data PSM: %s", err->message);
1368 if (hdp_update_sdp_record(hdp_adapter, applications))
1370 error("Error updating the SDP record");
1373 release_adapter_instance(hdp_adapter);
1380 int hdp_adapter_register(DBusConnection *conn, struct btd_adapter *adapter)
1382 struct hdp_adapter *hdp_adapter;
1384 hdp_adapter = g_new0(struct hdp_adapter, 1);
1385 hdp_adapter->btd_adapter = btd_adapter_ref(adapter);
1387 if(!update_adapter(hdp_adapter))
1390 adapters = g_slist_append(adapters, hdp_adapter);
1395 btd_adapter_unref(hdp_adapter->btd_adapter);
1396 g_free(hdp_adapter);
1400 void hdp_adapter_unregister(struct btd_adapter *adapter)
1402 struct hdp_adapter *hdp_adapter;
1405 l = g_slist_find_custom(adapters, adapter, cmp_adapter);
1410 hdp_adapter = l->data;
1411 adapters = g_slist_remove(adapters, hdp_adapter);
1412 if (hdp_adapter->sdp_handler > 0)
1413 remove_record_from_server(hdp_adapter->sdp_handler);
1414 release_adapter_instance(hdp_adapter);
1415 btd_adapter_unref(hdp_adapter->btd_adapter);
1416 g_free(hdp_adapter);
1419 static void delete_echo_channel_cb(GError *err, gpointer chan)
1421 if (err != NULL && err->code != MCAP_INVALID_MDL) {
1422 /* TODO: Decide if more action is required here */
1423 error("Error deleting echo channel: %s", err->message);
1427 health_channel_destroy(chan);
1430 static void delete_echo_channel(struct hdp_channel *chan)
1434 if (!chan->dev->mcl_conn) {
1435 error("Echo channel cannot be deleted: mcl closed");
1439 if (mcap_delete_mdl(chan->mdl, delete_echo_channel_cb,
1440 hdp_channel_ref(chan),
1441 (GDestroyNotify) hdp_channel_unref, &err))
1444 hdp_channel_unref(chan);
1445 error("Error deleting the echo channel: %s", err->message);
1448 /* TODO: Decide if more action is required here */
1451 static void abort_echo_channel_cb(GError *err, gpointer data)
1453 struct hdp_channel *chan = data;
1455 if (err != NULL && err->code != MCAP_ERROR_INVALID_OPERATION) {
1456 error("Aborting error: %s", err->message);
1457 if (err->code == MCAP_INVALID_MDL) {
1458 /* MDL is removed from MCAP so we can */
1459 /* free the data channel without sending */
1460 /* a MD_DELETE_MDL_REQ */
1461 /* TODO review the above comment */
1462 /* hdp_channel_unref(chan); */
1467 delete_echo_channel(chan);
1470 static void destroy_create_dc_data(gpointer data)
1472 struct hdp_create_dc *dc_data = data;
1474 hdp_create_data_unref(dc_data);
1477 static void *generate_echo_packet(void)
1482 buf = g_malloc(HDP_ECHO_LEN);
1485 for(i = 0; i < HDP_ECHO_LEN; i++)
1486 buf[i] = rand() % UINT8_MAX;
1491 static gboolean check_echo(GIOChannel *io_chan, GIOCondition cond,
1494 struct hdp_tmp_dc_data *hdp_conn = data;
1495 struct hdp_echo_data *edata = hdp_conn->hdp_chann->edata;
1496 struct hdp_channel *chan = hdp_conn->hdp_chann;
1497 uint8_t buf[MCAP_DC_MTU];
1502 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
1507 fd = g_io_channel_unix_get_fd(io_chan);
1508 len = read(fd, buf, sizeof(buf));
1510 if (len != HDP_ECHO_LEN) {
1515 value = (memcmp(buf, edata->buf, len) == 0);
1518 reply = g_dbus_create_reply(hdp_conn->msg, DBUS_TYPE_BOOLEAN, &value,
1520 g_dbus_send_message(hdp_conn->conn, reply);
1521 g_source_remove(edata->tid);
1527 close_device_con(chan->dev, FALSE);
1529 delete_echo_channel(chan);
1530 hdp_tmp_dc_data_unref(hdp_conn);
1535 static gboolean echo_timeout(gpointer data)
1537 struct hdp_channel *chan = data;
1541 error("Error: Echo request timeout");
1542 chan->edata->tid = 0;
1544 fd = mcap_mdl_get_fd(chan->mdl);
1548 io = g_io_channel_unix_new(fd);
1549 g_io_channel_shutdown(io, TRUE, NULL);
1554 static void hdp_echo_connect_cb(struct mcap_mdl *mdl, GError *err,
1557 struct hdp_tmp_dc_data *hdp_conn = data;
1558 struct hdp_echo_data *edata;
1559 GError *gerr = NULL;
1565 reply = g_dbus_create_error(hdp_conn->msg,
1566 ERROR_INTERFACE ".HealthError",
1567 "%s", err->message);
1568 g_dbus_send_message(hdp_conn->conn, reply);
1570 /* Send abort request because remote */
1571 /* side is now in PENDING state. */
1572 if (!mcap_mdl_abort(hdp_conn->hdp_chann->mdl,
1573 abort_echo_channel_cb,
1574 hdp_channel_ref(hdp_conn->hdp_chann),
1575 (GDestroyNotify) hdp_channel_unref,
1577 error("%s", gerr->message);
1579 hdp_channel_unref(hdp_conn->hdp_chann);
1584 fd = mcap_mdl_get_fd(hdp_conn->hdp_chann->mdl);
1586 reply = g_dbus_create_error(hdp_conn->msg,
1587 ERROR_INTERFACE ".HealthError",
1588 "Can't write in echo channel");
1589 g_dbus_send_message(hdp_conn->conn, reply);
1590 delete_echo_channel(hdp_conn->hdp_chann);
1594 edata = hdp_conn->hdp_chann->edata;
1595 edata->buf = generate_echo_packet();
1596 send_echo_data(fd, edata->buf, HDP_ECHO_LEN);
1598 io = g_io_channel_unix_new(fd);
1599 g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL | G_IO_IN,
1600 check_echo, hdp_tmp_dc_data_ref(hdp_conn));
1602 edata->tid = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
1603 ECHO_TIMEOUT, echo_timeout,
1604 hdp_channel_ref(hdp_conn->hdp_chann),
1605 (GDestroyNotify) hdp_channel_unref);
1607 g_io_channel_unref(io);
1610 static void delete_mdl_cb(GError *err, gpointer data)
1613 error("Deleting error: %s", err->message);
1616 static void abort_and_del_mdl_cb(GError *err, gpointer data)
1618 struct mcap_mdl *mdl = data;
1619 GError *gerr = NULL;
1622 error("%s", err->message);
1623 if (err->code == MCAP_INVALID_MDL) {
1624 /* MDL is removed from MCAP so we don't */
1625 /* need to delete it. */
1630 if (!mcap_delete_mdl(mdl, delete_mdl_cb, NULL, NULL, &gerr)) {
1631 error("%s", gerr->message);
1636 static void abort_mdl_connection_cb(GError *err, gpointer data)
1638 struct hdp_tmp_dc_data *hdp_conn = data;
1639 struct hdp_channel *hdp_chann = hdp_conn->hdp_chann;
1642 error("Aborting error: %s", err->message);
1644 /* Connection operation has failed but we have to */
1645 /* notify the channel created at MCAP level */
1646 if (hdp_chann->mdep != HDP_MDEP_ECHO)
1647 g_dbus_emit_signal(hdp_conn->conn,
1648 device_get_path(hdp_chann->dev->dev),
1651 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1655 static void hdp_mdl_conn_cb(struct mcap_mdl *mdl, GError *err, gpointer data)
1657 struct hdp_tmp_dc_data *hdp_conn = data;
1658 struct hdp_channel *hdp_chann = hdp_conn->hdp_chann;
1659 struct hdp_device *dev = hdp_chann->dev;
1661 GError *gerr = NULL;
1664 error("%s", err->message);
1665 reply = g_dbus_create_reply(hdp_conn->msg,
1666 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1668 g_dbus_send_message(hdp_conn->conn, reply);
1670 /* Send abort request because remote side */
1671 /* is now in PENDING state */
1672 if (!mcap_mdl_abort(hdp_chann->mdl, abort_mdl_connection_cb,
1673 hdp_tmp_dc_data_ref(hdp_conn),
1674 hdp_tmp_dc_data_destroy, &gerr)) {
1675 hdp_tmp_dc_data_unref(hdp_conn);
1676 error("%s", gerr->message);
1682 reply = g_dbus_create_reply(hdp_conn->msg,
1683 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1685 g_dbus_send_message(hdp_conn->conn, reply);
1687 g_dbus_emit_signal(hdp_conn->conn,
1688 device_get_path(hdp_chann->dev->dev),
1691 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1694 if (!check_channel_conf(hdp_chann)) {
1695 close_mdl(hdp_chann);
1699 if (dev->fr != NULL)
1702 dev->fr = hdp_channel_ref(hdp_chann);
1704 emit_property_changed(dev->conn, device_get_path(dev->dev),
1705 HEALTH_DEVICE, "MainChannel",
1706 DBUS_TYPE_OBJECT_PATH, &dev->fr->path);
1709 static void device_create_mdl_cb(struct mcap_mdl *mdl, uint8_t conf,
1710 GError *err, gpointer data)
1712 struct hdp_create_dc *user_data = data;
1713 struct hdp_tmp_dc_data *hdp_conn;
1714 struct hdp_channel *hdp_chan;
1715 GError *gerr = NULL;
1719 reply = g_dbus_create_error(user_data->msg,
1720 ERROR_INTERFACE ".HealthError",
1721 "%s", err->message);
1722 g_dbus_send_message(user_data->conn, reply);
1726 if (user_data->mdep != HDP_MDEP_ECHO &&
1727 user_data->config == HDP_NO_PREFERENCE_DC) {
1728 if (user_data->dev->fr == NULL && conf != HDP_RELIABLE_DC) {
1729 g_set_error(&gerr, HDP_ERROR, HDP_CONNECTION_ERROR,
1730 "Data channel aborted, first data "
1731 "channel should be reliable");
1733 } else if (conf == HDP_NO_PREFERENCE_DC ||
1734 conf > HDP_STREAMING_DC) {
1735 g_set_error(&gerr, HDP_ERROR, HDP_CONNECTION_ERROR,
1736 "Data channel aborted, "
1737 "configuration error");
1742 hdp_chan = create_channel(user_data->dev, conf, mdl,
1743 mcap_mdl_get_mdlid(mdl),
1744 user_data->app, &gerr);
1745 if (hdp_chan == NULL)
1748 hdp_conn = g_new0(struct hdp_tmp_dc_data, 1);
1749 hdp_conn->msg = dbus_message_ref(user_data->msg);
1750 hdp_conn->conn = dbus_connection_ref(user_data->conn);
1751 hdp_conn->hdp_chann = hdp_chan;
1752 hdp_conn->cb = user_data->cb;
1753 hdp_chan->mdep = user_data->mdep;
1755 if (hdp_get_dcpsm(hdp_chan->dev, hdp_get_dcpsm_cb,
1756 hdp_tmp_dc_data_ref(hdp_conn),
1757 hdp_tmp_dc_data_destroy, &gerr))
1760 error("%s", gerr->message);
1763 reply = g_dbus_create_reply(hdp_conn->msg,
1764 DBUS_TYPE_OBJECT_PATH, &hdp_chan->path,
1766 g_dbus_send_message(hdp_conn->conn, reply);
1767 hdp_tmp_dc_data_unref(hdp_conn);
1769 /* Send abort request because remote side is now in PENDING state */
1770 if (!mcap_mdl_abort(hdp_chan->mdl, abort_mdl_connection_cb,
1771 hdp_tmp_dc_data_ref(hdp_conn),
1772 hdp_tmp_dc_data_destroy, &gerr)) {
1773 hdp_tmp_dc_data_unref(hdp_conn);
1774 error("%s", gerr->message);
1781 reply = g_dbus_create_error(user_data->msg,
1782 ERROR_INTERFACE ".HealthError",
1783 "%s", gerr->message);
1784 g_dbus_send_message(user_data->conn, reply);
1787 /* Send abort request because remote side is now in PENDING */
1788 /* state. Then we have to delete it because we couldn't */
1789 /* register the HealthChannel interface */
1790 if (!mcap_mdl_abort(mdl, abort_and_del_mdl_cb, mcap_mdl_ref(mdl),
1791 (GDestroyNotify) mcap_mdl_unref, &gerr)) {
1792 error("%s", gerr->message);
1794 mcap_mdl_unref(mdl);
1798 static void device_create_dc_cb(gpointer user_data, GError *err)
1800 struct hdp_create_dc *data = user_data;
1802 GError *gerr = NULL;
1805 reply = g_dbus_create_error(data->msg,
1806 ERROR_INTERFACE ".HealthError",
1807 "%s", err->message);
1808 g_dbus_send_message(data->conn, reply);
1812 if (data->dev->mcl == NULL) {
1813 g_set_error(&gerr, HDP_ERROR, HDP_CONNECTION_ERROR,
1818 hdp_create_data_ref(data);
1820 if (mcap_create_mdl(data->dev->mcl, data->mdep, data->config,
1821 device_create_mdl_cb, data,
1822 destroy_create_dc_data, &gerr))
1824 hdp_create_data_unref(data);
1827 reply = g_dbus_create_error(data->msg, ERROR_INTERFACE ".HealthError",
1828 "%s", gerr->message);
1830 g_dbus_send_message(data->conn, reply);
1833 static DBusMessage *device_echo(DBusConnection *conn,
1834 DBusMessage *msg, void *user_data)
1836 struct hdp_device *device = user_data;
1837 struct hdp_create_dc *data;
1841 data = g_new0(struct hdp_create_dc, 1);
1842 data->dev = health_device_ref(device);
1843 data->mdep = HDP_MDEP_ECHO;
1844 data->config = HDP_RELIABLE_DC;
1845 data->msg = dbus_message_ref(msg);
1846 data->conn = dbus_connection_ref(conn);
1847 data->cb = hdp_echo_connect_cb;
1848 hdp_create_data_ref(data);
1850 if (device->mcl_conn && device->mcl != NULL) {
1851 if (mcap_create_mdl(device->mcl, data->mdep, data->config,
1852 device_create_mdl_cb, data,
1853 destroy_create_dc_data, &err))
1858 if (hdp_establish_mcl(data->dev, device_create_dc_cb,
1859 data, destroy_create_dc_data, &err))
1863 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
1864 "%s", err->message);
1866 hdp_create_data_unref(data);
1870 static void device_get_mdep_cb(uint8_t mdep, gpointer data, GError *err)
1872 struct hdp_create_dc *dc_data, *user_data = data;
1874 GError *gerr = NULL;
1877 reply = g_dbus_create_error(user_data->msg,
1878 ERROR_INTERFACE ".HealthError",
1879 "%s", err->message);
1880 g_dbus_send_message(user_data->conn, reply);
1884 dc_data = hdp_create_data_ref(user_data);
1885 dc_data->mdep = mdep;
1887 if (user_data->dev->mcl_conn) {
1888 device_create_dc_cb(dc_data, NULL);
1889 hdp_create_data_unref(dc_data);
1893 if (hdp_establish_mcl(dc_data->dev, device_create_dc_cb,
1894 dc_data, destroy_create_dc_data, &gerr))
1897 reply = g_dbus_create_error(user_data->msg,
1898 ERROR_INTERFACE ".HealthError",
1899 "%s", gerr->message);
1900 hdp_create_data_unref(dc_data);
1902 g_dbus_send_message(user_data->conn, reply);
1905 static DBusMessage *device_create_channel(DBusConnection *conn,
1906 DBusMessage *msg, void *user_data)
1908 struct hdp_device *device = user_data;
1909 struct hdp_application *app;
1910 struct hdp_create_dc *data;
1911 char *app_path, *conf;
1917 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &app_path,
1918 DBUS_TYPE_STRING, &conf,
1920 return btd_error_invalid_args(msg);
1922 l = g_slist_find_custom(applications, app_path, cmp_app);
1924 return btd_error_invalid_args(msg);
1928 if (g_ascii_strcasecmp("Reliable", conf) == 0)
1929 config = HDP_RELIABLE_DC;
1930 else if (g_ascii_strcasecmp("Streaming", conf) == 0)
1931 config = HDP_STREAMING_DC;
1932 else if (g_ascii_strcasecmp("Any", conf) == 0)
1933 config = HDP_NO_PREFERENCE_DC;
1935 return btd_error_invalid_args(msg);
1937 if (app->role == HDP_SINK && config != HDP_NO_PREFERENCE_DC)
1938 return btd_error_invalid_args(msg);
1940 if (app->role == HDP_SOURCE && config == HDP_NO_PREFERENCE_DC)
1941 return btd_error_invalid_args(msg);
1943 if (!device->fr && config == HDP_STREAMING_DC)
1944 return btd_error_invalid_args(msg);
1946 data = g_new0(struct hdp_create_dc, 1);
1947 data->dev = health_device_ref(device);
1948 data->config = config;
1949 data->app = hdp_application_ref(app);
1950 data->msg = dbus_message_ref(msg);
1951 data->conn = dbus_connection_ref(conn);
1952 data->cb = hdp_mdl_conn_cb;
1954 if (hdp_get_mdep(device, l->data, device_get_mdep_cb,
1955 hdp_create_data_ref(data),
1956 destroy_create_dc_data, &err))
1959 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
1960 "%s", err->message);
1962 hdp_create_data_unref(data);
1966 static void hdp_mdl_delete_cb(GError *err, gpointer data)
1968 struct hdp_tmp_dc_data *del_data = data;
1972 if (err != NULL && err->code != MCAP_INVALID_MDL) {
1973 reply = g_dbus_create_error(del_data->msg,
1974 ERROR_INTERFACE ".HealthError",
1975 "%s", err->message);
1976 g_dbus_send_message(del_data->conn, reply);
1980 path = g_strdup(del_data->hdp_chann->path);
1981 g_dbus_unregister_interface(del_data->conn, path, HEALTH_CHANNEL);
1984 reply = g_dbus_create_reply(del_data->msg, DBUS_TYPE_INVALID);
1985 g_dbus_send_message(del_data->conn, reply);
1988 static void hdp_continue_del_cb(gpointer user_data, GError *err)
1990 struct hdp_tmp_dc_data *del_data = user_data;
1991 GError *gerr = NULL;
1995 reply = g_dbus_create_error(del_data->msg,
1996 ERROR_INTERFACE ".HealthError",
1997 "%s", err->message);
1998 g_dbus_send_message(del_data->conn, reply);
2002 if (mcap_delete_mdl(del_data->hdp_chann->mdl, hdp_mdl_delete_cb,
2003 hdp_tmp_dc_data_ref(del_data),
2004 hdp_tmp_dc_data_destroy, &gerr))
2007 reply = g_dbus_create_error(del_data->msg,
2008 ERROR_INTERFACE ".HealthError",
2009 "%s", gerr->message);
2010 #ifdef __TIZEN_PATCH__
2011 g_dbus_send_message(del_data->conn, reply);
2013 hdp_tmp_dc_data_unref(del_data);
2015 hdp_tmp_dc_data_unref(del_data);
2017 g_dbus_send_message(del_data->conn, reply);
2021 static DBusMessage *device_destroy_channel(DBusConnection *conn,
2022 DBusMessage *msg, void *user_data)
2024 struct hdp_device *device = user_data;
2025 struct hdp_tmp_dc_data *del_data;
2026 struct hdp_channel *hdp_chan;
2032 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2033 DBUS_TYPE_INVALID)){
2034 return btd_error_invalid_args(msg);
2037 l = g_slist_find_custom(device->channels, path, cmp_chan_path);
2039 return btd_error_invalid_args(msg);
2042 del_data = g_new0(struct hdp_tmp_dc_data, 1);
2043 del_data->msg = dbus_message_ref(msg);
2044 del_data->conn = dbus_connection_ref(conn);
2045 del_data->hdp_chann = hdp_channel_ref(hdp_chan);
2047 if (device->mcl_conn) {
2048 if (mcap_delete_mdl(hdp_chan->mdl, hdp_mdl_delete_cb,
2049 hdp_tmp_dc_data_ref(del_data),
2050 hdp_tmp_dc_data_destroy, &err))
2055 if (hdp_establish_mcl(device, hdp_continue_del_cb,
2056 hdp_tmp_dc_data_ref(del_data),
2057 hdp_tmp_dc_data_destroy, &err))
2061 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
2062 "%s", err->message);
2063 hdp_tmp_dc_data_unref(del_data);
2068 static DBusMessage *device_get_properties(DBusConnection *conn,
2069 DBusMessage *msg, void *user_data)
2071 struct hdp_device *device = user_data;
2072 DBusMessageIter iter, dict;
2075 reply = dbus_message_new_method_return(msg);
2079 dbus_message_iter_init_append(reply, &iter);
2081 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
2082 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
2083 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
2084 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
2086 if (device->fr != NULL)
2087 dict_append_entry(&dict, "MainChannel", DBUS_TYPE_OBJECT_PATH,
2090 dbus_message_iter_close_container(&iter, &dict);
2095 static void health_device_destroy(void *data)
2097 struct hdp_device *device = data;
2099 DBG("Unregistered interface %s on path %s", HEALTH_DEVICE,
2100 device_get_path(device->dev));
2102 remove_channels(device);
2103 if (device->ndc != NULL) {
2104 hdp_channel_unref(device->ndc);
2108 devices = g_slist_remove(devices, device);
2109 health_device_unref(device);
2112 static const GDBusMethodTable health_device_methods[] = {
2113 { GDBUS_ASYNC_METHOD("Echo",
2114 NULL, GDBUS_ARGS({ "value", "b" }), device_echo) },
2115 { GDBUS_ASYNC_METHOD("CreateChannel",
2116 GDBUS_ARGS({ "application", "o" },
2117 { "configuration", "s" }),
2118 GDBUS_ARGS({ "channel", "o" }),
2119 device_create_channel) },
2120 { GDBUS_ASYNC_METHOD("DestroyChannel",
2121 GDBUS_ARGS({ "channel", "o" }), NULL,
2122 device_destroy_channel) },
2123 { GDBUS_METHOD("GetProperties",
2124 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
2125 device_get_properties) },
2129 static const GDBusSignalTable health_device_signals[] = {
2130 { GDBUS_SIGNAL("ChannelConnected",
2131 GDBUS_ARGS({ "channel", "o" })) },
2132 { GDBUS_SIGNAL("ChannelDeleted",
2133 GDBUS_ARGS({ "channel", "o" })) },
2134 { GDBUS_SIGNAL("PropertyChanged",
2135 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
2139 static struct hdp_device *create_health_device(DBusConnection *conn,
2140 struct btd_device *device)
2142 struct btd_adapter *adapter = device_get_adapter(device);
2143 const gchar *path = device_get_path(device);
2144 struct hdp_device *dev;
2150 dev = g_new0(struct hdp_device, 1);
2151 dev->conn = dbus_connection_ref(conn);
2152 dev->dev = btd_device_ref(device);
2153 health_device_ref(dev);
2155 l = g_slist_find_custom(adapters, adapter, cmp_adapter);
2159 dev->hdp_adapter = l->data;
2161 if (!g_dbus_register_interface(conn, path,
2163 health_device_methods,
2164 health_device_signals, NULL,
2165 dev, health_device_destroy)) {
2166 error("D-Bus failed to register %s interface", HEALTH_DEVICE);
2170 DBG("Registered interface %s on path %s", HEALTH_DEVICE, path);
2174 health_device_unref(dev);
2178 int hdp_device_register(DBusConnection *conn, struct btd_device *device)
2180 struct hdp_device *hdev;
2183 l = g_slist_find_custom(devices, device, cmp_device);
2186 hdev->sdp_present = TRUE;
2190 hdev = create_health_device(conn, device);
2194 hdev->sdp_present = TRUE;
2196 devices = g_slist_prepend(devices, hdev);
2200 void hdp_device_unregister(struct btd_device *device)
2202 struct hdp_device *hdp_dev;
2206 l = g_slist_find_custom(devices, device, cmp_device);
2211 path = device_get_path(hdp_dev->dev);
2212 g_dbus_unregister_interface(hdp_dev->conn, path, HEALTH_DEVICE);
2215 int hdp_manager_start(DBusConnection *conn)
2217 DBG("Starting Health manager");
2219 if (!g_dbus_register_interface(conn, MANAGER_PATH,
2221 health_manager_methods, NULL, NULL,
2222 NULL, manager_path_unregister)) {
2223 error("D-Bus failed to register %s interface", HEALTH_MANAGER);
2227 connection = dbus_connection_ref(conn);
2232 void hdp_manager_stop(void)
2234 g_dbus_unregister_interface(connection, MANAGER_PATH, HEALTH_MANAGER);
2236 dbus_connection_unref(connection);
2237 DBG("Stopped Health manager");
2240 struct hdp_device *health_device_ref(struct hdp_device *hdp_dev)
2244 DBG("health_device_ref(%p): ref=%d", hdp_dev, hdp_dev->ref);
2249 void health_device_unref(struct hdp_device *hdp_dev)
2253 DBG("health_device_unref(%p): ref=%d", hdp_dev, hdp_dev->ref);
2255 if (hdp_dev->ref > 0)
2258 free_health_device(hdp_dev);