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))
621 hdp_tmp_dc_data_unref(data);
622 reply = g_dbus_create_error(data->msg, ERROR_INTERFACE ".HealthError",
623 "Cannot reconnect: %s", gerr->message);
629 static void channel_acquire_cb(gpointer data, GError *err)
631 struct hdp_tmp_dc_data *dc_data = data;
634 reply = channel_acquire_continue(data, err);
637 g_dbus_send_message(dc_data->conn, reply);
640 static DBusMessage *channel_acquire(DBusConnection *conn,
641 DBusMessage *msg, void *user_data)
643 struct hdp_channel *chan = user_data;
644 struct hdp_tmp_dc_data *dc_data;
648 dc_data = g_new0(struct hdp_tmp_dc_data, 1);
649 dc_data->conn = dbus_connection_ref(conn);
650 dc_data->msg = dbus_message_ref(msg);
651 dc_data->hdp_chann = hdp_channel_ref(chan);
653 if (chan->dev->mcl_conn) {
654 reply = channel_acquire_continue(hdp_tmp_dc_data_ref(dc_data),
656 hdp_tmp_dc_data_unref(dc_data);
660 if (hdp_establish_mcl(chan->dev, channel_acquire_cb,
661 hdp_tmp_dc_data_ref(dc_data),
662 hdp_tmp_dc_data_destroy, &gerr))
665 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
666 "%s", gerr->message);
667 hdp_tmp_dc_data_unref(dc_data);
673 static void close_mdl(struct hdp_channel *hdp_chann)
677 fd = mcap_mdl_get_fd(hdp_chann->mdl);
684 static DBusMessage *channel_release(DBusConnection *conn,
685 DBusMessage *msg, void *user_data)
687 struct hdp_channel *hdp_chann = user_data;
689 close_mdl(hdp_chann);
691 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
694 static void free_echo_data(struct hdp_echo_data *edata)
700 g_source_remove(edata->tid);
702 if (edata->buf != NULL)
709 static void health_channel_destroy(void *data)
711 struct hdp_channel *hdp_chan = data;
712 struct hdp_device *dev = hdp_chan->dev;
714 DBG("Destroy Health Channel %s", hdp_chan->path);
715 if (g_slist_find(dev->channels, hdp_chan) == NULL)
718 dev->channels = g_slist_remove(dev->channels, hdp_chan);
720 if (hdp_chan->mdep != HDP_MDEP_ECHO)
721 g_dbus_emit_signal(dev->conn, device_get_path(dev->dev),
722 HEALTH_DEVICE, "ChannelDeleted",
723 DBUS_TYPE_OBJECT_PATH, &hdp_chan->path,
726 if (hdp_chan == dev->fr) {
727 hdp_channel_unref(dev->fr);
732 hdp_channel_unref(hdp_chan);
735 static const GDBusMethodTable health_channels_methods[] = {
736 { GDBUS_METHOD("GetProperties",
737 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
738 channel_get_properties) },
739 { GDBUS_ASYNC_METHOD("Acquire",
740 NULL, GDBUS_ARGS({ "fd", "h" }),
742 { GDBUS_METHOD("Release", NULL, NULL, channel_release) },
746 static struct hdp_channel *create_channel(struct hdp_device *dev,
748 struct mcap_mdl *mdl,
750 struct hdp_application *app,
753 struct hdp_channel *hdp_chann;
758 hdp_chann = g_new0(struct hdp_channel, 1);
759 hdp_chann->config = config;
760 hdp_chann->dev = health_device_ref(dev);
761 hdp_chann->mdlid = mdlid;
764 hdp_chann->mdl = mcap_mdl_ref(mdl);
767 hdp_chann->mdep = app->id;
768 hdp_chann->app = hdp_application_ref(app);
770 hdp_chann->edata = g_new0(struct hdp_echo_data, 1);
772 hdp_chann->path = g_strdup_printf("%s/chan%d",
773 device_get_path(hdp_chann->dev->dev),
776 dev->channels = g_slist_append(dev->channels,
777 hdp_channel_ref(hdp_chann));
779 if (hdp_chann->mdep == HDP_MDEP_ECHO)
780 return hdp_channel_ref(hdp_chann);
782 if (!g_dbus_register_interface(dev->conn, hdp_chann->path,
784 health_channels_methods, NULL, NULL,
785 hdp_chann, health_channel_destroy)) {
786 g_set_error(err, HDP_ERROR, HDP_UNSPECIFIED_ERROR,
787 "Can't register the channel interface");
788 health_channel_destroy(hdp_chann);
792 return hdp_channel_ref(hdp_chann);
795 static void remove_channels(struct hdp_device *dev)
797 struct hdp_channel *chan;
800 while (dev->channels != NULL) {
801 chan = dev->channels->data;
803 path = g_strdup(chan->path);
804 if (!g_dbus_unregister_interface(dev->conn, path,
806 health_channel_destroy(chan);
811 static void close_device_con(struct hdp_device *dev, gboolean cache)
813 if (dev->mcl == NULL)
816 mcap_close_mcl(dev->mcl, cache);
817 dev->mcl_conn = FALSE;
822 device_unref_mcl(dev);
823 remove_channels(dev);
825 if (!dev->sdp_present) {
828 path = device_get_path(dev->dev);
829 g_dbus_unregister_interface(dev->conn, path, HEALTH_DEVICE);
833 static int send_echo_data(int sock, const void *buf, uint32_t size)
835 const uint8_t *buf_b = buf;
838 while (sent < size) {
839 int n = write(sock, buf_b + sent, size - sent);
848 static gboolean serve_echo(GIOChannel *io_chan, GIOCondition cond,
851 struct hdp_channel *chan = data;
852 uint8_t buf[MCAP_DC_MTU];
855 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
856 hdp_channel_unref(chan);
860 if (chan->edata->echo_done)
863 chan->edata->echo_done = TRUE;
865 fd = g_io_channel_unix_get_fd(io_chan);
866 len = read(fd, buf, sizeof(buf));
868 if (send_echo_data(fd, buf, len) >= 0)
872 close_device_con(chan->dev, FALSE);
873 hdp_channel_unref(chan);
877 static gboolean check_channel_conf(struct hdp_channel *chan)
885 fd = mcap_mdl_get_fd(chan->mdl);
888 io = g_io_channel_unix_new(fd);
890 if (!bt_io_get(io, BT_IO_L2CAP, &err,
891 BT_IO_OPT_MODE, &mode,
892 BT_IO_OPT_IMTU, &imtu,
893 BT_IO_OPT_OMTU, &omtu,
894 BT_IO_OPT_INVALID)) {
895 error("Error: %s", err->message);
896 g_io_channel_unref(io);
901 g_io_channel_unref(io);
903 switch (chan->config) {
904 case HDP_RELIABLE_DC:
905 if (mode != L2CAP_MODE_ERTM)
908 case HDP_STREAMING_DC:
909 if (mode != L2CAP_MODE_STREAMING)
913 error("Error: Connected with unknown configuration");
917 DBG("MDL imtu %d omtu %d Channel imtu %d omtu %d", imtu, omtu,
918 chan->imtu, chan->omtu);
925 if (chan->imtu != imtu || chan->omtu != omtu)
931 static void hdp_mcap_mdl_connected_cb(struct mcap_mdl *mdl, void *data)
933 struct hdp_device *dev = data;
934 struct hdp_channel *chan;
936 DBG("hdp_mcap_mdl_connected_cb");
937 if (dev->ndc == NULL)
941 if (chan->mdl == NULL)
942 chan->mdl = mcap_mdl_ref(mdl);
944 if (g_slist_find(dev->channels, chan) == NULL)
945 dev->channels = g_slist_prepend(dev->channels,
946 hdp_channel_ref(chan));
948 if (!check_channel_conf(chan)) {
953 if (chan->mdep == HDP_MDEP_ECHO) {
957 fd = mcap_mdl_get_fd(chan->mdl);
961 chan->edata->echo_done = FALSE;
962 io = g_io_channel_unix_new(fd);
963 g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL | G_IO_IN,
964 serve_echo, hdp_channel_ref(chan));
965 g_io_channel_unref(io);
969 g_dbus_emit_signal(dev->conn, device_get_path(dev->dev), HEALTH_DEVICE,
971 DBUS_TYPE_OBJECT_PATH, &chan->path,
977 dev->fr = hdp_channel_ref(chan);
979 emit_property_changed(dev->conn, device_get_path(dev->dev),
980 HEALTH_DEVICE, "MainChannel",
981 DBUS_TYPE_OBJECT_PATH, &dev->fr->path);
984 hdp_channel_unref(dev->ndc);
988 static void hdp_mcap_mdl_closed_cb(struct mcap_mdl *mdl, void *data)
990 /* struct hdp_device *dev = data; */
992 DBG("hdp_mcap_mdl_closed_cb");
997 static void hdp_mcap_mdl_deleted_cb(struct mcap_mdl *mdl, void *data)
999 struct hdp_device *dev = data;
1000 struct hdp_channel *chan;
1004 DBG("hdp_mcap_mdl_deleted_cb");
1005 l = g_slist_find_custom(dev->channels, mdl, cmp_chan_mdl);
1011 path = g_strdup(chan->path);
1012 if (!g_dbus_unregister_interface(dev->conn, path, HEALTH_CHANNEL))
1013 health_channel_destroy(chan);
1017 static void hdp_mcap_mdl_aborted_cb(struct mcap_mdl *mdl, void *data)
1019 struct hdp_device *dev = data;
1021 DBG("hdp_mcap_mdl_aborted_cb");
1022 if (dev->ndc == NULL)
1025 dev->ndc->mdl = mcap_mdl_ref(mdl);
1027 if (g_slist_find(dev->channels, dev->ndc) == NULL)
1028 dev->channels = g_slist_prepend(dev->channels,
1029 hdp_channel_ref(dev->ndc));
1031 if (dev->ndc->mdep != HDP_MDEP_ECHO)
1032 g_dbus_emit_signal(dev->conn, device_get_path(dev->dev),
1033 HEALTH_DEVICE, "ChannelConnected",
1034 DBUS_TYPE_OBJECT_PATH, &dev->ndc->path,
1037 hdp_channel_unref(dev->ndc);
1041 static uint8_t hdp2l2cap_mode(uint8_t hdp_mode)
1043 return hdp_mode == HDP_STREAMING_DC ? L2CAP_MODE_STREAMING :
1047 static uint8_t hdp_mcap_mdl_conn_req_cb(struct mcap_mcl *mcl, uint8_t mdepid,
1048 uint16_t mdlid, uint8_t *conf, void *data)
1050 struct hdp_device *dev = data;
1051 struct hdp_application *app;
1055 DBG("Data channel request");
1057 if (mdepid == HDP_MDEP_ECHO) {
1059 case HDP_NO_PREFERENCE_DC:
1060 *conf = HDP_RELIABLE_DC;
1061 case HDP_RELIABLE_DC:
1063 case HDP_STREAMING_DC:
1064 return MCAP_CONFIGURATION_REJECTED;
1066 /* Special case defined in HDP spec 3.4. When an invalid
1067 * configuration is received we shall close the MCL when
1068 * we are still processing the callback. */
1069 close_device_con(dev, FALSE);
1070 return MCAP_CONFIGURATION_REJECTED; /* not processed */
1073 if (!mcap_set_data_chan_mode(dev->hdp_adapter->mi,
1074 L2CAP_MODE_ERTM, &err)) {
1075 error("Error: %s", err->message);
1077 return MCAP_MDL_BUSY;
1080 dev->ndc = create_channel(dev, *conf, NULL, mdlid, NULL, NULL);
1081 if (dev->ndc == NULL)
1082 return MCAP_MDL_BUSY;
1084 return MCAP_SUCCESS;
1087 l = g_slist_find_custom(applications, &mdepid, cmp_app_id);
1089 return MCAP_INVALID_MDEP;
1093 /* Check if is the first dc if so,
1094 * only reliable configuration is allowed */
1096 case HDP_NO_PREFERENCE_DC:
1097 if (app->role == HDP_SINK)
1098 return MCAP_CONFIGURATION_REJECTED;
1099 else if (dev->fr && app->chan_type_set)
1100 *conf = app->chan_type;
1102 *conf = HDP_RELIABLE_DC;
1104 case HDP_STREAMING_DC:
1105 if (!dev->fr || app->role == HDP_SOURCE)
1106 return MCAP_CONFIGURATION_REJECTED;
1107 case HDP_RELIABLE_DC:
1108 if (app->role == HDP_SOURCE)
1109 return MCAP_CONFIGURATION_REJECTED;
1112 /* Special case defined in HDP spec 3.4. When an invalid
1113 * configuration is received we shall close the MCL when
1114 * we are still processing the callback. */
1115 close_device_con(dev, FALSE);
1116 return MCAP_CONFIGURATION_REJECTED; /* not processed */
1119 l = g_slist_find_custom(dev->channels, &mdlid, cmp_chan_mdlid);
1121 struct hdp_channel *chan = l->data;
1124 path = g_strdup(chan->path);
1125 g_dbus_unregister_interface(dev->conn, path, HEALTH_CHANNEL);
1129 if (!mcap_set_data_chan_mode(dev->hdp_adapter->mi,
1130 hdp2l2cap_mode(*conf), &err)) {
1131 error("Error: %s", err->message);
1133 return MCAP_MDL_BUSY;
1136 dev->ndc = create_channel(dev, *conf, NULL, mdlid, app, NULL);
1137 if (dev->ndc == NULL)
1138 return MCAP_MDL_BUSY;
1140 return MCAP_SUCCESS;
1143 static uint8_t hdp_mcap_mdl_reconn_req_cb(struct mcap_mdl *mdl, void *data)
1145 struct hdp_device *dev = data;
1146 struct hdp_channel *chan;
1150 l = g_slist_find_custom(dev->channels, mdl, cmp_chan_mdl);
1152 return MCAP_INVALID_MDL;
1156 if (dev->fr == NULL && chan->config != HDP_RELIABLE_DC &&
1157 chan->mdep != HDP_MDEP_ECHO)
1158 return MCAP_UNSPECIFIED_ERROR;
1160 if (!mcap_set_data_chan_mode(dev->hdp_adapter->mi,
1161 hdp2l2cap_mode(chan->config), &err)) {
1162 error("Error: %s", err->message);
1164 return MCAP_MDL_BUSY;
1167 dev->ndc = hdp_channel_ref(chan);
1169 return MCAP_SUCCESS;
1172 gboolean hdp_set_mcl_cb(struct hdp_device *device, GError **err)
1176 if (device->mcl == NULL)
1179 ret = mcap_mcl_set_cb(device->mcl, device, err,
1180 MCAP_MDL_CB_CONNECTED, hdp_mcap_mdl_connected_cb,
1181 MCAP_MDL_CB_CLOSED, hdp_mcap_mdl_closed_cb,
1182 MCAP_MDL_CB_DELETED, hdp_mcap_mdl_deleted_cb,
1183 MCAP_MDL_CB_ABORTED, hdp_mcap_mdl_aborted_cb,
1184 MCAP_MDL_CB_REMOTE_CONN_REQ, hdp_mcap_mdl_conn_req_cb,
1185 MCAP_MDL_CB_REMOTE_RECONN_REQ, hdp_mcap_mdl_reconn_req_cb,
1186 MCAP_MDL_CB_INVALID);
1191 error("Can't set mcl callbacks, closing mcl");
1192 close_device_con(device, TRUE);
1197 static void mcl_connected(struct mcap_mcl *mcl, gpointer data)
1199 struct hdp_device *hdp_device;
1203 mcap_mcl_get_addr(mcl, &addr);
1204 l = g_slist_find_custom(devices, &addr, cmp_dev_addr);
1206 struct hdp_adapter *hdp_adapter = data;
1207 struct btd_device *device;
1211 device = adapter_get_device(connection,
1212 hdp_adapter->btd_adapter, str);
1215 hdp_device = create_health_device(connection, device);
1218 devices = g_slist_append(devices, hdp_device);
1220 hdp_device = l->data;
1222 hdp_device->mcl = mcap_mcl_ref(mcl);
1223 hdp_device->mcl_conn = TRUE;
1225 DBG("New mcl connected from %s", device_get_path(hdp_device->dev));
1227 hdp_set_mcl_cb(hdp_device, NULL);
1230 static void mcl_reconnected(struct mcap_mcl *mcl, gpointer data)
1232 struct hdp_device *hdp_device;
1235 l = g_slist_find_custom(devices, mcl, cmp_dev_mcl);
1239 hdp_device = l->data;
1240 hdp_device->mcl_conn = TRUE;
1242 DBG("MCL reconnected %s", device_get_path(hdp_device->dev));
1244 hdp_set_mcl_cb(hdp_device, NULL);
1247 static void mcl_disconnected(struct mcap_mcl *mcl, gpointer data)
1249 struct hdp_device *hdp_device;
1252 l = g_slist_find_custom(devices, mcl, cmp_dev_mcl);
1256 hdp_device = l->data;
1257 hdp_device->mcl_conn = FALSE;
1259 DBG("Mcl disconnected %s", device_get_path(hdp_device->dev));
1262 static void mcl_uncached(struct mcap_mcl *mcl, gpointer data)
1264 struct hdp_device *hdp_device;
1268 l = g_slist_find_custom(devices, mcl, cmp_dev_mcl);
1272 hdp_device = l->data;
1273 device_unref_mcl(hdp_device);
1275 if (hdp_device->sdp_present)
1278 /* Because remote device hasn't announced an HDP record */
1279 /* the Bluetooth daemon won't notify when the device shall */
1280 /* be removed. Then we have to remove the HealthDevice */
1281 /* interface manually */
1282 path = device_get_path(hdp_device->dev);
1283 g_dbus_unregister_interface(hdp_device->conn, path, HEALTH_DEVICE);
1284 DBG("Mcl uncached %s", path);
1287 static void check_devices_mcl(void)
1289 struct hdp_device *dev;
1290 GSList *l, *to_delete = NULL;
1292 for (l = devices; l; l = l->next) {
1294 device_unref_mcl(dev);
1296 if (!dev->sdp_present)
1297 to_delete = g_slist_append(to_delete, dev);
1299 remove_channels(dev);
1302 for (l = to_delete; l; l = l->next) {
1305 path = device_get_path(dev->dev);
1306 g_dbus_unregister_interface(dev->conn, path, HEALTH_DEVICE);
1309 g_slist_free(to_delete);
1312 static void release_adapter_instance(struct hdp_adapter *hdp_adapter)
1314 if (hdp_adapter->mi == NULL)
1317 check_devices_mcl();
1318 mcap_release_instance(hdp_adapter->mi);
1319 mcap_instance_unref(hdp_adapter->mi);
1320 hdp_adapter->mi = NULL;
1323 static gboolean update_adapter(struct hdp_adapter *hdp_adapter)
1328 if (applications == NULL) {
1329 release_adapter_instance(hdp_adapter);
1333 if (hdp_adapter->mi != NULL)
1336 adapter_get_address(hdp_adapter->btd_adapter, &addr);
1337 hdp_adapter->mi = mcap_create_instance(&addr, BT_IO_SEC_MEDIUM, 0, 0,
1338 mcl_connected, mcl_reconnected,
1339 mcl_disconnected, mcl_uncached,
1340 NULL, /* CSP is not used by now */
1343 if (hdp_adapter->mi == NULL) {
1344 error("Error creating the MCAP instance: %s", err->message);
1349 hdp_adapter->ccpsm = mcap_get_ctrl_psm(hdp_adapter->mi, &err);
1351 error("Error getting MCAP control PSM: %s", err->message);
1355 hdp_adapter->dcpsm = mcap_get_data_psm(hdp_adapter->mi, &err);
1357 error("Error getting MCAP data PSM: %s", err->message);
1362 if (hdp_update_sdp_record(hdp_adapter, applications))
1364 error("Error updating the SDP record");
1367 release_adapter_instance(hdp_adapter);
1374 int hdp_adapter_register(DBusConnection *conn, struct btd_adapter *adapter)
1376 struct hdp_adapter *hdp_adapter;
1378 hdp_adapter = g_new0(struct hdp_adapter, 1);
1379 hdp_adapter->btd_adapter = btd_adapter_ref(adapter);
1381 if(!update_adapter(hdp_adapter))
1384 adapters = g_slist_append(adapters, hdp_adapter);
1389 btd_adapter_unref(hdp_adapter->btd_adapter);
1390 g_free(hdp_adapter);
1394 void hdp_adapter_unregister(struct btd_adapter *adapter)
1396 struct hdp_adapter *hdp_adapter;
1399 l = g_slist_find_custom(adapters, adapter, cmp_adapter);
1404 hdp_adapter = l->data;
1405 adapters = g_slist_remove(adapters, hdp_adapter);
1406 if (hdp_adapter->sdp_handler > 0)
1407 remove_record_from_server(hdp_adapter->sdp_handler);
1408 release_adapter_instance(hdp_adapter);
1409 btd_adapter_unref(hdp_adapter->btd_adapter);
1410 g_free(hdp_adapter);
1413 static void delete_echo_channel_cb(GError *err, gpointer chan)
1415 if (err != NULL && err->code != MCAP_INVALID_MDL) {
1416 /* TODO: Decide if more action is required here */
1417 error("Error deleting echo channel: %s", err->message);
1421 health_channel_destroy(chan);
1424 static void delete_echo_channel(struct hdp_channel *chan)
1428 if (!chan->dev->mcl_conn) {
1429 error("Echo channel cannot be deleted: mcl closed");
1433 if (mcap_delete_mdl(chan->mdl, delete_echo_channel_cb,
1434 hdp_channel_ref(chan),
1435 (GDestroyNotify) hdp_channel_unref, &err))
1438 hdp_channel_unref(chan);
1439 error("Error deleting the echo channel: %s", err->message);
1442 /* TODO: Decide if more action is required here */
1445 static void abort_echo_channel_cb(GError *err, gpointer data)
1447 struct hdp_channel *chan = data;
1449 if (err != NULL && err->code != MCAP_ERROR_INVALID_OPERATION) {
1450 error("Aborting error: %s", err->message);
1451 if (err->code == MCAP_INVALID_MDL) {
1452 /* MDL is removed from MCAP so we can */
1453 /* free the data channel without sending */
1454 /* a MD_DELETE_MDL_REQ */
1455 /* TODO review the above comment */
1456 /* hdp_channel_unref(chan); */
1461 delete_echo_channel(chan);
1464 static void destroy_create_dc_data(gpointer data)
1466 struct hdp_create_dc *dc_data = data;
1468 hdp_create_data_unref(dc_data);
1471 static void *generate_echo_packet(void)
1476 buf = g_malloc(HDP_ECHO_LEN);
1479 for(i = 0; i < HDP_ECHO_LEN; i++)
1480 buf[i] = rand() % UINT8_MAX;
1485 static gboolean check_echo(GIOChannel *io_chan, GIOCondition cond,
1488 struct hdp_tmp_dc_data *hdp_conn = data;
1489 struct hdp_echo_data *edata = hdp_conn->hdp_chann->edata;
1490 struct hdp_channel *chan = hdp_conn->hdp_chann;
1491 uint8_t buf[MCAP_DC_MTU];
1496 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
1501 fd = g_io_channel_unix_get_fd(io_chan);
1502 len = read(fd, buf, sizeof(buf));
1504 if (len != HDP_ECHO_LEN) {
1509 value = (memcmp(buf, edata->buf, len) == 0);
1512 reply = g_dbus_create_reply(hdp_conn->msg, DBUS_TYPE_BOOLEAN, &value,
1514 g_dbus_send_message(hdp_conn->conn, reply);
1515 g_source_remove(edata->tid);
1521 close_device_con(chan->dev, FALSE);
1523 delete_echo_channel(chan);
1524 hdp_tmp_dc_data_unref(hdp_conn);
1529 static gboolean echo_timeout(gpointer data)
1531 struct hdp_channel *chan = data;
1535 error("Error: Echo request timeout");
1536 chan->edata->tid = 0;
1538 fd = mcap_mdl_get_fd(chan->mdl);
1542 io = g_io_channel_unix_new(fd);
1543 g_io_channel_shutdown(io, TRUE, NULL);
1548 static void hdp_echo_connect_cb(struct mcap_mdl *mdl, GError *err,
1551 struct hdp_tmp_dc_data *hdp_conn = data;
1552 struct hdp_echo_data *edata;
1553 GError *gerr = NULL;
1559 reply = g_dbus_create_error(hdp_conn->msg,
1560 ERROR_INTERFACE ".HealthError",
1561 "%s", err->message);
1562 g_dbus_send_message(hdp_conn->conn, reply);
1564 /* Send abort request because remote */
1565 /* side is now in PENDING state. */
1566 if (!mcap_mdl_abort(hdp_conn->hdp_chann->mdl,
1567 abort_echo_channel_cb,
1568 hdp_channel_ref(hdp_conn->hdp_chann),
1569 (GDestroyNotify) hdp_channel_unref,
1571 error("%s", gerr->message);
1573 hdp_channel_unref(hdp_conn->hdp_chann);
1578 fd = mcap_mdl_get_fd(hdp_conn->hdp_chann->mdl);
1580 reply = g_dbus_create_error(hdp_conn->msg,
1581 ERROR_INTERFACE ".HealthError",
1582 "Can't write in echo channel");
1583 g_dbus_send_message(hdp_conn->conn, reply);
1584 delete_echo_channel(hdp_conn->hdp_chann);
1588 edata = hdp_conn->hdp_chann->edata;
1589 edata->buf = generate_echo_packet();
1590 send_echo_data(fd, edata->buf, HDP_ECHO_LEN);
1592 io = g_io_channel_unix_new(fd);
1593 g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL | G_IO_IN,
1594 check_echo, hdp_tmp_dc_data_ref(hdp_conn));
1596 edata->tid = g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
1597 ECHO_TIMEOUT, echo_timeout,
1598 hdp_channel_ref(hdp_conn->hdp_chann),
1599 (GDestroyNotify) hdp_channel_unref);
1601 g_io_channel_unref(io);
1604 static void delete_mdl_cb(GError *err, gpointer data)
1607 error("Deleting error: %s", err->message);
1610 static void abort_and_del_mdl_cb(GError *err, gpointer data)
1612 struct mcap_mdl *mdl = data;
1613 GError *gerr = NULL;
1616 error("%s", err->message);
1617 if (err->code == MCAP_INVALID_MDL) {
1618 /* MDL is removed from MCAP so we don't */
1619 /* need to delete it. */
1624 if (!mcap_delete_mdl(mdl, delete_mdl_cb, NULL, NULL, &gerr)) {
1625 error("%s", gerr->message);
1630 static void abort_mdl_connection_cb(GError *err, gpointer data)
1632 struct hdp_tmp_dc_data *hdp_conn = data;
1633 struct hdp_channel *hdp_chann = hdp_conn->hdp_chann;
1636 error("Aborting error: %s", err->message);
1638 /* Connection operation has failed but we have to */
1639 /* notify the channel created at MCAP level */
1640 if (hdp_chann->mdep != HDP_MDEP_ECHO)
1641 g_dbus_emit_signal(hdp_conn->conn,
1642 device_get_path(hdp_chann->dev->dev),
1645 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1649 static void hdp_mdl_conn_cb(struct mcap_mdl *mdl, GError *err, gpointer data)
1651 struct hdp_tmp_dc_data *hdp_conn = data;
1652 struct hdp_channel *hdp_chann = hdp_conn->hdp_chann;
1653 struct hdp_device *dev = hdp_chann->dev;
1655 GError *gerr = NULL;
1658 error("%s", err->message);
1659 reply = g_dbus_create_reply(hdp_conn->msg,
1660 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1662 g_dbus_send_message(hdp_conn->conn, reply);
1664 /* Send abort request because remote side */
1665 /* is now in PENDING state */
1666 if (!mcap_mdl_abort(hdp_chann->mdl, abort_mdl_connection_cb,
1667 hdp_tmp_dc_data_ref(hdp_conn),
1668 hdp_tmp_dc_data_destroy, &gerr)) {
1669 hdp_tmp_dc_data_unref(hdp_conn);
1670 error("%s", gerr->message);
1676 reply = g_dbus_create_reply(hdp_conn->msg,
1677 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1679 g_dbus_send_message(hdp_conn->conn, reply);
1681 g_dbus_emit_signal(hdp_conn->conn,
1682 device_get_path(hdp_chann->dev->dev),
1685 DBUS_TYPE_OBJECT_PATH, &hdp_chann->path,
1688 if (!check_channel_conf(hdp_chann)) {
1689 close_mdl(hdp_chann);
1693 if (dev->fr != NULL)
1696 dev->fr = hdp_channel_ref(hdp_chann);
1698 emit_property_changed(dev->conn, device_get_path(dev->dev),
1699 HEALTH_DEVICE, "MainChannel",
1700 DBUS_TYPE_OBJECT_PATH, &dev->fr->path);
1703 static void device_create_mdl_cb(struct mcap_mdl *mdl, uint8_t conf,
1704 GError *err, gpointer data)
1706 struct hdp_create_dc *user_data = data;
1707 struct hdp_tmp_dc_data *hdp_conn;
1708 struct hdp_channel *hdp_chan;
1709 GError *gerr = NULL;
1713 reply = g_dbus_create_error(user_data->msg,
1714 ERROR_INTERFACE ".HealthError",
1715 "%s", err->message);
1716 g_dbus_send_message(user_data->conn, reply);
1720 if (user_data->mdep != HDP_MDEP_ECHO &&
1721 user_data->config == HDP_NO_PREFERENCE_DC) {
1722 if (user_data->dev->fr == NULL && conf != HDP_RELIABLE_DC) {
1723 g_set_error(&gerr, HDP_ERROR, HDP_CONNECTION_ERROR,
1724 "Data channel aborted, first data "
1725 "channel should be reliable");
1727 } else if (conf == HDP_NO_PREFERENCE_DC ||
1728 conf > HDP_STREAMING_DC) {
1729 g_set_error(&gerr, HDP_ERROR, HDP_CONNECTION_ERROR,
1730 "Data channel aborted, "
1731 "configuration error");
1736 hdp_chan = create_channel(user_data->dev, conf, mdl,
1737 mcap_mdl_get_mdlid(mdl),
1738 user_data->app, &gerr);
1739 if (hdp_chan == NULL)
1742 hdp_conn = g_new0(struct hdp_tmp_dc_data, 1);
1743 hdp_conn->msg = dbus_message_ref(user_data->msg);
1744 hdp_conn->conn = dbus_connection_ref(user_data->conn);
1745 hdp_conn->hdp_chann = hdp_chan;
1746 hdp_conn->cb = user_data->cb;
1747 hdp_chan->mdep = user_data->mdep;
1749 if (hdp_get_dcpsm(hdp_chan->dev, hdp_get_dcpsm_cb,
1750 hdp_tmp_dc_data_ref(hdp_conn),
1751 hdp_tmp_dc_data_destroy, &gerr))
1754 error("%s", gerr->message);
1757 reply = g_dbus_create_reply(hdp_conn->msg,
1758 DBUS_TYPE_OBJECT_PATH, &hdp_chan->path,
1760 g_dbus_send_message(hdp_conn->conn, reply);
1761 hdp_tmp_dc_data_unref(hdp_conn);
1763 /* Send abort request because remote side is now in PENDING state */
1764 if (!mcap_mdl_abort(hdp_chan->mdl, abort_mdl_connection_cb,
1765 hdp_tmp_dc_data_ref(hdp_conn),
1766 hdp_tmp_dc_data_destroy, &gerr)) {
1767 hdp_tmp_dc_data_unref(hdp_conn);
1768 error("%s", gerr->message);
1775 reply = g_dbus_create_error(user_data->msg,
1776 ERROR_INTERFACE ".HealthError",
1777 "%s", gerr->message);
1778 g_dbus_send_message(user_data->conn, reply);
1781 /* Send abort request because remote side is now in PENDING */
1782 /* state. Then we have to delete it because we couldn't */
1783 /* register the HealthChannel interface */
1784 if (!mcap_mdl_abort(mdl, abort_and_del_mdl_cb, mcap_mdl_ref(mdl),
1785 (GDestroyNotify) mcap_mdl_unref, &gerr)) {
1786 error("%s", gerr->message);
1788 mcap_mdl_unref(mdl);
1792 static void device_create_dc_cb(gpointer user_data, GError *err)
1794 struct hdp_create_dc *data = user_data;
1796 GError *gerr = NULL;
1799 reply = g_dbus_create_error(data->msg,
1800 ERROR_INTERFACE ".HealthError",
1801 "%s", err->message);
1802 g_dbus_send_message(data->conn, reply);
1806 if (data->dev->mcl == NULL) {
1807 g_set_error(&gerr, HDP_ERROR, HDP_CONNECTION_ERROR,
1812 hdp_create_data_ref(data);
1814 if (mcap_create_mdl(data->dev->mcl, data->mdep, data->config,
1815 device_create_mdl_cb, data,
1816 destroy_create_dc_data, &gerr))
1818 hdp_create_data_unref(data);
1821 reply = g_dbus_create_error(data->msg, ERROR_INTERFACE ".HealthError",
1822 "%s", gerr->message);
1824 g_dbus_send_message(data->conn, reply);
1827 static DBusMessage *device_echo(DBusConnection *conn,
1828 DBusMessage *msg, void *user_data)
1830 struct hdp_device *device = user_data;
1831 struct hdp_create_dc *data;
1835 data = g_new0(struct hdp_create_dc, 1);
1836 data->dev = health_device_ref(device);
1837 data->mdep = HDP_MDEP_ECHO;
1838 data->config = HDP_RELIABLE_DC;
1839 data->msg = dbus_message_ref(msg);
1840 data->conn = dbus_connection_ref(conn);
1841 data->cb = hdp_echo_connect_cb;
1842 hdp_create_data_ref(data);
1844 if (device->mcl_conn && device->mcl != NULL) {
1845 if (mcap_create_mdl(device->mcl, data->mdep, data->config,
1846 device_create_mdl_cb, data,
1847 destroy_create_dc_data, &err))
1852 if (hdp_establish_mcl(data->dev, device_create_dc_cb,
1853 data, destroy_create_dc_data, &err))
1857 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
1858 "%s", err->message);
1860 hdp_create_data_unref(data);
1864 static void device_get_mdep_cb(uint8_t mdep, gpointer data, GError *err)
1866 struct hdp_create_dc *dc_data, *user_data = data;
1868 GError *gerr = NULL;
1871 reply = g_dbus_create_error(user_data->msg,
1872 ERROR_INTERFACE ".HealthError",
1873 "%s", err->message);
1874 g_dbus_send_message(user_data->conn, reply);
1878 dc_data = hdp_create_data_ref(user_data);
1879 dc_data->mdep = mdep;
1881 if (user_data->dev->mcl_conn) {
1882 device_create_dc_cb(dc_data, NULL);
1883 hdp_create_data_unref(dc_data);
1887 if (hdp_establish_mcl(dc_data->dev, device_create_dc_cb,
1888 dc_data, destroy_create_dc_data, &gerr))
1891 reply = g_dbus_create_error(user_data->msg,
1892 ERROR_INTERFACE ".HealthError",
1893 "%s", gerr->message);
1894 hdp_create_data_unref(dc_data);
1896 g_dbus_send_message(user_data->conn, reply);
1899 static DBusMessage *device_create_channel(DBusConnection *conn,
1900 DBusMessage *msg, void *user_data)
1902 struct hdp_device *device = user_data;
1903 struct hdp_application *app;
1904 struct hdp_create_dc *data;
1905 char *app_path, *conf;
1911 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &app_path,
1912 DBUS_TYPE_STRING, &conf,
1914 return btd_error_invalid_args(msg);
1916 l = g_slist_find_custom(applications, app_path, cmp_app);
1918 return btd_error_invalid_args(msg);
1922 if (g_ascii_strcasecmp("Reliable", conf) == 0)
1923 config = HDP_RELIABLE_DC;
1924 else if (g_ascii_strcasecmp("Streaming", conf) == 0)
1925 config = HDP_STREAMING_DC;
1926 else if (g_ascii_strcasecmp("Any", conf) == 0)
1927 config = HDP_NO_PREFERENCE_DC;
1929 return btd_error_invalid_args(msg);
1931 if (app->role == HDP_SINK && config != HDP_NO_PREFERENCE_DC)
1932 return btd_error_invalid_args(msg);
1934 if (app->role == HDP_SOURCE && config == HDP_NO_PREFERENCE_DC)
1935 return btd_error_invalid_args(msg);
1937 if (!device->fr && config == HDP_STREAMING_DC)
1938 return btd_error_invalid_args(msg);
1940 data = g_new0(struct hdp_create_dc, 1);
1941 data->dev = health_device_ref(device);
1942 data->config = config;
1943 data->app = hdp_application_ref(app);
1944 data->msg = dbus_message_ref(msg);
1945 data->conn = dbus_connection_ref(conn);
1946 data->cb = hdp_mdl_conn_cb;
1948 if (hdp_get_mdep(device, l->data, device_get_mdep_cb,
1949 hdp_create_data_ref(data),
1950 destroy_create_dc_data, &err))
1953 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
1954 "%s", err->message);
1956 hdp_create_data_unref(data);
1960 static void hdp_mdl_delete_cb(GError *err, gpointer data)
1962 struct hdp_tmp_dc_data *del_data = data;
1966 if (err != NULL && err->code != MCAP_INVALID_MDL) {
1967 reply = g_dbus_create_error(del_data->msg,
1968 ERROR_INTERFACE ".HealthError",
1969 "%s", err->message);
1970 g_dbus_send_message(del_data->conn, reply);
1974 path = g_strdup(del_data->hdp_chann->path);
1975 g_dbus_unregister_interface(del_data->conn, path, HEALTH_CHANNEL);
1978 reply = g_dbus_create_reply(del_data->msg, DBUS_TYPE_INVALID);
1979 g_dbus_send_message(del_data->conn, reply);
1982 static void hdp_continue_del_cb(gpointer user_data, GError *err)
1984 struct hdp_tmp_dc_data *del_data = user_data;
1985 GError *gerr = NULL;
1989 reply = g_dbus_create_error(del_data->msg,
1990 ERROR_INTERFACE ".HealthError",
1991 "%s", err->message);
1992 g_dbus_send_message(del_data->conn, reply);
1996 if (mcap_delete_mdl(del_data->hdp_chann->mdl, hdp_mdl_delete_cb,
1997 hdp_tmp_dc_data_ref(del_data),
1998 hdp_tmp_dc_data_destroy, &gerr))
2001 reply = g_dbus_create_error(del_data->msg,
2002 ERROR_INTERFACE ".HealthError",
2003 "%s", gerr->message);
2004 hdp_tmp_dc_data_unref(del_data);
2006 g_dbus_send_message(del_data->conn, reply);
2009 static DBusMessage *device_destroy_channel(DBusConnection *conn,
2010 DBusMessage *msg, void *user_data)
2012 struct hdp_device *device = user_data;
2013 struct hdp_tmp_dc_data *del_data;
2014 struct hdp_channel *hdp_chan;
2020 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
2021 DBUS_TYPE_INVALID)){
2022 return btd_error_invalid_args(msg);
2025 l = g_slist_find_custom(device->channels, path, cmp_chan_path);
2027 return btd_error_invalid_args(msg);
2030 del_data = g_new0(struct hdp_tmp_dc_data, 1);
2031 del_data->msg = dbus_message_ref(msg);
2032 del_data->conn = dbus_connection_ref(conn);
2033 del_data->hdp_chann = hdp_channel_ref(hdp_chan);
2035 if (device->mcl_conn) {
2036 if (mcap_delete_mdl(hdp_chan->mdl, hdp_mdl_delete_cb,
2037 hdp_tmp_dc_data_ref(del_data),
2038 hdp_tmp_dc_data_destroy, &err))
2043 if (hdp_establish_mcl(device, hdp_continue_del_cb,
2044 hdp_tmp_dc_data_ref(del_data),
2045 hdp_tmp_dc_data_destroy, &err))
2049 reply = g_dbus_create_error(msg, ERROR_INTERFACE ".HealthError",
2050 "%s", err->message);
2051 hdp_tmp_dc_data_unref(del_data);
2056 static DBusMessage *device_get_properties(DBusConnection *conn,
2057 DBusMessage *msg, void *user_data)
2059 struct hdp_device *device = user_data;
2060 DBusMessageIter iter, dict;
2063 reply = dbus_message_new_method_return(msg);
2067 dbus_message_iter_init_append(reply, &iter);
2069 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
2070 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
2071 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
2072 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
2074 if (device->fr != NULL)
2075 dict_append_entry(&dict, "MainChannel", DBUS_TYPE_OBJECT_PATH,
2078 dbus_message_iter_close_container(&iter, &dict);
2083 static void health_device_destroy(void *data)
2085 struct hdp_device *device = data;
2087 DBG("Unregistered interface %s on path %s", HEALTH_DEVICE,
2088 device_get_path(device->dev));
2090 remove_channels(device);
2091 if (device->ndc != NULL) {
2092 hdp_channel_unref(device->ndc);
2096 devices = g_slist_remove(devices, device);
2097 health_device_unref(device);
2100 static const GDBusMethodTable health_device_methods[] = {
2101 { GDBUS_ASYNC_METHOD("Echo",
2102 NULL, GDBUS_ARGS({ "value", "b" }), device_echo) },
2103 { GDBUS_ASYNC_METHOD("CreateChannel",
2104 GDBUS_ARGS({ "application", "o" },
2105 { "configuration", "s" }),
2106 GDBUS_ARGS({ "channel", "o" }),
2107 device_create_channel) },
2108 { GDBUS_ASYNC_METHOD("DestroyChannel",
2109 GDBUS_ARGS({ "channel", "o" }), NULL,
2110 device_destroy_channel) },
2111 { GDBUS_METHOD("GetProperties",
2112 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
2113 device_get_properties) },
2117 static const GDBusSignalTable health_device_signals[] = {
2118 { GDBUS_SIGNAL("ChannelConnected",
2119 GDBUS_ARGS({ "channel", "o" })) },
2120 { GDBUS_SIGNAL("ChannelDeleted",
2121 GDBUS_ARGS({ "channel", "o" })) },
2122 { GDBUS_SIGNAL("PropertyChanged",
2123 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
2127 static struct hdp_device *create_health_device(DBusConnection *conn,
2128 struct btd_device *device)
2130 struct btd_adapter *adapter = device_get_adapter(device);
2131 const gchar *path = device_get_path(device);
2132 struct hdp_device *dev;
2138 dev = g_new0(struct hdp_device, 1);
2139 dev->conn = dbus_connection_ref(conn);
2140 dev->dev = btd_device_ref(device);
2141 health_device_ref(dev);
2143 l = g_slist_find_custom(adapters, adapter, cmp_adapter);
2147 dev->hdp_adapter = l->data;
2149 if (!g_dbus_register_interface(conn, path,
2151 health_device_methods,
2152 health_device_signals, NULL,
2153 dev, health_device_destroy)) {
2154 error("D-Bus failed to register %s interface", HEALTH_DEVICE);
2158 DBG("Registered interface %s on path %s", HEALTH_DEVICE, path);
2162 health_device_unref(dev);
2166 int hdp_device_register(DBusConnection *conn, struct btd_device *device)
2168 struct hdp_device *hdev;
2171 l = g_slist_find_custom(devices, device, cmp_device);
2174 hdev->sdp_present = TRUE;
2178 hdev = create_health_device(conn, device);
2182 hdev->sdp_present = TRUE;
2184 devices = g_slist_prepend(devices, hdev);
2188 void hdp_device_unregister(struct btd_device *device)
2190 struct hdp_device *hdp_dev;
2194 l = g_slist_find_custom(devices, device, cmp_device);
2199 path = device_get_path(hdp_dev->dev);
2200 g_dbus_unregister_interface(hdp_dev->conn, path, HEALTH_DEVICE);
2203 int hdp_manager_start(DBusConnection *conn)
2205 DBG("Starting Health manager");
2207 if (!g_dbus_register_interface(conn, MANAGER_PATH,
2209 health_manager_methods, NULL, NULL,
2210 NULL, manager_path_unregister)) {
2211 error("D-Bus failed to register %s interface", HEALTH_MANAGER);
2215 connection = dbus_connection_ref(conn);
2220 void hdp_manager_stop(void)
2222 g_dbus_unregister_interface(connection, MANAGER_PATH, HEALTH_MANAGER);
2224 dbus_connection_unref(connection);
2225 DBG("Stopped Health manager");
2228 struct hdp_device *health_device_ref(struct hdp_device *hdp_dev)
2232 DBG("health_device_ref(%p): ref=%d", hdp_dev, hdp_dev->ref);
2237 void health_device_unref(struct hdp_device *hdp_dev)
2241 DBG("health_device_unref(%p): ref=%d", hdp_dev, hdp_dev->ref);
2243 if (hdp_dev->ref > 0)
2246 free_health_device(hdp_dev);