3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2014 Intel Corporation. All rights reserved.
6 * Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "btio/btio.h"
36 #include "lib/bluetooth.h"
38 #include "lib/sdp_lib.h"
40 #include "lib/l2cap.h"
42 #include "src/shared/util.h"
43 #include "src/shared/queue.h"
44 #include "src/uuid-helper.h"
45 #include "src/sdp-client.h"
46 #include "profiles/health/mcap.h"
49 #include "ipc-common.h"
52 #include "bluetooth.h"
55 #define SVC_HINT_HEALTH 0x00
56 #define HDP_VERSION 0x0101
57 #define DATA_EXCHANGE_SPEC_11073 0x01
59 #define CHANNEL_TYPE_ANY 0x00
60 #define CHANNEL_TYPE_RELIABLE 0x01
61 #define CHANNEL_TYPE_STREAM 0x02
63 #define MDEP_ECHO 0x00
64 #define MDEP_INITIAL 0x01
65 #define MDEP_FINAL 0x7F
67 static bdaddr_t adapter_addr;
68 static struct ipc *hal_ipc = NULL;
69 static struct queue *apps = NULL;
70 static struct mcap_instance *mcap = NULL;
71 static uint32_t record_id = 0;
72 static uint32_t record_state = 0;
80 uint8_t id; /* mdep id */
83 struct health_device {
89 struct queue *channels; /* data channels */
95 struct health_channel {
99 struct health_device *dev;
102 struct mcap_mdl *mdl;
104 uint16_t mdl_id; /* MDL ID */
106 uint16_t id; /* channel id */
117 uint16_t id; /* app id */
118 struct queue *devices;
121 static void send_app_reg_notify(struct health_app *app, uint8_t state)
123 struct hal_ev_health_app_reg_state ev;
130 ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HEALTH,
131 HAL_EV_HEALTH_APP_REG_STATE, sizeof(ev), &ev);
134 static void send_channel_state_notify(struct health_channel *channel,
135 uint8_t state, int fd)
137 struct hal_ev_health_channel_state ev;
141 bdaddr2android(&channel->dev->dst, ev.bdaddr);
142 ev.app_id = channel->dev->app_id;
143 ev.mdep_index = channel->mdep_id - 1;
144 ev.channel_id = channel->id;
145 ev.channel_state = state;
147 ipc_send_notif_with_fd(hal_ipc, HAL_SERVICE_ID_HEALTH,
148 HAL_EV_HEALTH_CHANNEL_STATE,
149 sizeof(ev), &ev, fd);
152 static void unref_mdl(struct health_channel *channel)
154 if (!channel || !channel->mdl)
157 mcap_mdl_unref(channel->mdl);
159 channel->mdl_conn = false;
162 static void free_health_channel(void *data)
164 struct health_channel *channel = data;
167 DBG("channel %p", channel);
172 fd = mcap_mdl_get_fd(channel->mdl);
174 shutdown(fd, SHUT_RDWR);
180 static void destroy_channel(void *data)
182 struct health_channel *channel = data;
187 send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_DESTROYED, -1);
188 queue_remove(channel->dev->channels, channel);
189 free_health_channel(channel);
192 static void unref_mcl(struct health_device *dev)
194 if (!dev || !dev->mcl)
197 mcap_close_mcl(dev->mcl, FALSE);
198 mcap_mcl_unref(dev->mcl);
202 static void free_health_device(void *data)
204 struct health_device *dev = data;
210 queue_destroy(dev->channels, free_health_channel);
214 static void free_mdep_cfg(void *data)
216 struct mdep_cfg *cfg = data;
225 static void free_health_app(void *data)
227 struct health_app *app = data;
233 free(app->provider_name);
234 free(app->service_name);
235 free(app->service_descr);
236 queue_destroy(app->mdeps, free_mdep_cfg);
237 queue_destroy(app->devices, free_health_device);
241 static bool match_channel_by_mdl(const void *data, const void *user_data)
243 const struct health_channel *channel = data;
244 const struct mcap_mdl *mdl = user_data;
246 return channel->mdl == mdl;
249 static bool match_channel_by_id(const void *data, const void *user_data)
251 const struct health_channel *channel = data;
252 uint16_t channel_id = PTR_TO_INT(user_data);
254 return channel->id == channel_id;
257 static bool match_dev_by_mcl(const void *data, const void *user_data)
259 const struct health_device *dev = data;
260 const struct mcap_mcl *mcl = user_data;
262 return dev->mcl == mcl;
265 static bool match_dev_by_addr(const void *data, const void *user_data)
267 const struct health_device *dev = data;
268 const bdaddr_t *addr = user_data;
270 return !bacmp(&dev->dst, addr);
273 static bool match_channel_by_mdep_id(const void *data, const void *user_data)
275 const struct health_channel *channel = data;
276 uint16_t mdep_id = PTR_TO_INT(user_data);
278 return channel->mdep_id == mdep_id;
281 static bool match_mdep_by_role(const void *data, const void *user_data)
283 const struct mdep_cfg *mdep = data;
284 uint16_t role = PTR_TO_INT(user_data);
286 return mdep->role == role;
289 static bool match_mdep_by_id(const void *data, const void *user_data)
291 const struct mdep_cfg *mdep = data;
292 uint16_t mdep_id = PTR_TO_INT(user_data);
294 return mdep->id == mdep_id;
297 static bool match_app_by_id(const void *data, const void *user_data)
299 const struct health_app *app = data;
300 uint16_t app_id = PTR_TO_INT(user_data);
302 return app->id == app_id;
305 static struct health_channel *search_channel_by_id(uint16_t id)
307 const struct queue_entry *apps_entry, *devices_entry;
308 struct health_app *app;
309 struct health_channel *channel;
310 struct health_device *dev;
314 apps_entry = queue_get_entries(apps);
316 app = apps_entry->data;
317 devices_entry = queue_get_entries(app->devices);
318 while (devices_entry) {
319 dev = devices_entry->data;
320 channel = queue_find(dev->channels, match_channel_by_id,
326 devices_entry = devices_entry->next;
329 apps_entry = apps_entry->next;
335 static struct health_channel *search_channel_by_mdl(struct mcap_mdl *mdl)
337 const struct queue_entry *apps_entry, *devices_entry;
338 struct health_app *app;
339 struct health_channel *channel;
340 struct health_device *dev;
344 apps_entry = queue_get_entries(apps);
346 app = apps_entry->data;
347 devices_entry = queue_get_entries(app->devices);
348 while (devices_entry) {
349 dev = devices_entry->data;
350 channel = queue_find(dev->channels,
351 match_channel_by_mdl, mdl);
356 devices_entry = devices_entry->next;
359 apps_entry = apps_entry->next;
365 static struct health_device *search_dev_by_mcl(struct mcap_mcl *mcl)
367 const struct queue_entry *apps_entry;
368 struct health_app *app;
369 struct health_device *dev;
373 apps_entry = queue_get_entries(apps);
375 app = apps_entry->data;
377 dev = queue_find(app->devices, match_dev_by_mcl, mcl);
382 apps_entry = apps_entry->next;
388 static struct health_app *search_app_by_mdepid(uint8_t mdepid)
390 const struct queue_entry *apps_entry;
391 struct health_app *app;
395 apps_entry = queue_get_entries(apps);
397 app = apps_entry->data;
399 if (queue_find(app->mdeps, match_mdep_by_id,
403 apps_entry = apps_entry->next;
409 static int register_service_protocols(sdp_record_t *rec,
410 struct health_app *app)
412 uuid_t l2cap_uuid, mcap_c_uuid;
413 sdp_list_t *l2cap_list, *proto_list = NULL, *mcap_list = NULL;
414 sdp_list_t *access_proto_list = NULL;
415 sdp_data_t *psm = NULL, *mcap_ver = NULL;
417 uint16_t version = MCAP_VERSION;
423 /* set l2cap information */
424 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
425 l2cap_list = sdp_list_append(NULL, &l2cap_uuid);
429 ccpsm = mcap_get_ctrl_psm(mcap, &err);
433 psm = sdp_data_alloc(SDP_UINT16, &ccpsm);
437 if (!sdp_list_append(l2cap_list, psm))
440 proto_list = sdp_list_append(NULL, l2cap_list);
444 /* set mcap information */
445 sdp_uuid16_create(&mcap_c_uuid, MCAP_CTRL_UUID);
446 mcap_list = sdp_list_append(NULL, &mcap_c_uuid);
450 mcap_ver = sdp_data_alloc(SDP_UINT16, &version);
454 if (!sdp_list_append(mcap_list, mcap_ver))
457 if (!sdp_list_append(proto_list, mcap_list))
460 /* attach protocol information to service record */
461 access_proto_list = sdp_list_append(NULL, proto_list);
462 if (!access_proto_list)
465 sdp_set_access_protos(rec, access_proto_list);
469 sdp_list_free(l2cap_list, NULL);
470 sdp_list_free(mcap_list, NULL);
471 sdp_list_free(proto_list, NULL);
472 sdp_list_free(access_proto_list, NULL);
478 sdp_data_free(mcap_ver);
486 static int register_service_profiles(sdp_record_t *rec)
489 sdp_list_t *profile_list;
490 sdp_profile_desc_t hdp_profile;
494 /* set hdp information */
495 sdp_uuid16_create(&hdp_profile.uuid, HDP_SVCLASS_ID);
496 hdp_profile.version = HDP_VERSION;
497 profile_list = sdp_list_append(NULL, &hdp_profile);
501 /* set profile descriptor list */
502 ret = sdp_set_profile_descs(rec, profile_list);
503 sdp_list_free(profile_list, NULL);
508 static int register_service_additional_protocols(sdp_record_t *rec,
509 struct health_app *app)
512 uuid_t l2cap_uuid, mcap_d_uuid;
513 sdp_list_t *l2cap_list, *proto_list = NULL, *mcap_list = NULL;
514 sdp_list_t *access_proto_list = NULL;
515 sdp_data_t *psm = NULL;
521 /* set l2cap information */
522 sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
523 l2cap_list = sdp_list_append(NULL, &l2cap_uuid);
527 dcpsm = mcap_get_data_psm(mcap, &err);
531 psm = sdp_data_alloc(SDP_UINT16, &dcpsm);
535 if (!sdp_list_append(l2cap_list, psm))
538 proto_list = sdp_list_append(NULL, l2cap_list);
542 /* set mcap information */
543 sdp_uuid16_create(&mcap_d_uuid, MCAP_DATA_UUID);
544 mcap_list = sdp_list_append(NULL, &mcap_d_uuid);
548 if (!sdp_list_append(proto_list, mcap_list))
551 /* attach protocol information to service record */
552 access_proto_list = sdp_list_append(NULL, proto_list);
553 if (!access_proto_list)
556 sdp_set_add_access_protos(rec, access_proto_list);
560 sdp_list_free(l2cap_list, NULL);
561 sdp_list_free(mcap_list, NULL);
562 sdp_list_free(proto_list, NULL);
563 sdp_list_free(access_proto_list, NULL);
574 static sdp_list_t *mdeps_to_sdp_features(struct mdep_cfg *mdep)
576 sdp_data_t *mdepid, *dtype = NULL, *role = NULL, *descr = NULL;
577 sdp_list_t *f_list = NULL;
581 mdepid = sdp_data_alloc(SDP_UINT8, &mdep->id);
585 dtype = sdp_data_alloc(SDP_UINT16, &mdep->data_type);
589 role = sdp_data_alloc(SDP_UINT8, &mdep->role);
594 descr = sdp_data_alloc(SDP_TEXT_STR8, mdep->descr);
599 f_list = sdp_list_append(NULL, mdepid);
603 if (!sdp_list_append(f_list, dtype))
606 if (!sdp_list_append(f_list, role))
609 if (descr && !sdp_list_append(f_list, descr))
615 sdp_list_free(f_list, NULL);
618 sdp_data_free(mdepid);
621 sdp_data_free(dtype);
627 sdp_data_free(descr);
632 static void free_hdp_list(void *list)
634 sdp_list_t *hdp_list = list;
636 sdp_list_free(hdp_list, (sdp_free_func_t)sdp_data_free);
639 static void register_features(void *data, void *user_data)
641 struct mdep_cfg *mdep = data;
642 sdp_list_t **sup_features = user_data;
643 sdp_list_t *hdp_feature;
647 hdp_feature = mdeps_to_sdp_features(mdep);
651 if (!*sup_features) {
652 *sup_features = sdp_list_append(NULL, hdp_feature);
654 sdp_list_free(hdp_feature,
655 (sdp_free_func_t)sdp_data_free);
656 } else if (!sdp_list_append(*sup_features, hdp_feature)) {
657 sdp_list_free(hdp_feature,
658 (sdp_free_func_t)sdp_data_free);
662 static int register_service_sup_features(sdp_record_t *rec,
663 struct health_app *app)
665 sdp_list_t *sup_features = NULL;
669 queue_foreach(app->mdeps, register_features, &sup_features);
673 if (sdp_set_supp_feat(rec, sup_features) < 0) {
674 sdp_list_free(sup_features, free_hdp_list);
678 sdp_list_free(sup_features, free_hdp_list);
682 static int register_data_exchange_spec(sdp_record_t *rec)
685 uint8_t data_spec = DATA_EXCHANGE_SPEC_11073;
686 /* As of now only 11073 is supported, so we set it as default */
690 spec = sdp_data_alloc(SDP_UINT8, &data_spec);
694 if (sdp_attr_add(rec, SDP_ATTR_DATA_EXCHANGE_SPEC, spec) < 0) {
702 static int register_mcap_features(sdp_record_t *rec)
704 sdp_data_t *mcap_proc;
705 uint8_t mcap_sup_proc = MCAP_SUP_PROC;
709 mcap_proc = sdp_data_alloc(SDP_UINT8, &mcap_sup_proc);
713 if (sdp_attr_add(rec, SDP_ATTR_MCAP_SUPPORTED_PROCEDURES,
715 sdp_data_free(mcap_proc);
722 static int set_sdp_services_uuid(sdp_record_t *rec, uint8_t role)
725 sdp_list_t *list = NULL;
727 sdp_uuid16_create(&sink, HDP_SINK_SVCLASS_ID);
728 sdp_uuid16_create(&source, HDP_SOURCE_SVCLASS_ID);
729 sdp_get_service_classes(rec, &list);
732 case HAL_HEALTH_MDEP_ROLE_SOURCE:
733 if (!sdp_list_find(list, &source, sdp_uuid_cmp))
734 list = sdp_list_append(list, &source);
736 case HAL_HEALTH_MDEP_ROLE_SINK:
737 if (!sdp_list_find(list, &sink, sdp_uuid_cmp))
738 list = sdp_list_append(list, &sink);
742 if (sdp_set_service_classes(rec, list) < 0) {
743 sdp_list_free(list, NULL);
747 sdp_list_free(list, NULL);
752 static int update_sdp_record(struct health_app *app)
760 bt_adapter_remove_record(record_id);
764 rec = sdp_record_alloc();
768 role = HAL_HEALTH_MDEP_ROLE_SOURCE;
769 if (queue_find(app->mdeps, match_mdep_by_role, INT_TO_PTR(role)))
770 set_sdp_services_uuid(rec, role);
772 role = HAL_HEALTH_MDEP_ROLE_SINK;
773 if (queue_find(app->mdeps, match_mdep_by_role, INT_TO_PTR(role)))
774 set_sdp_services_uuid(rec, role);
776 sdp_set_info_attr(rec, app->service_name, app->provider_name,
779 if (register_service_protocols(rec, app) < 0)
782 if (register_service_profiles(rec) < 0)
785 if (register_service_additional_protocols(rec, app) < 0)
788 if (register_service_sup_features(rec, app) < 0)
791 if (register_data_exchange_spec(rec) < 0)
794 if (register_mcap_features(rec) < 0)
797 if (sdp_set_record_state(rec, record_state++) < 0)
800 if (bt_adapter_add_record(rec, SVC_HINT_HEALTH) < 0) {
801 error("health: Failed to register HEALTH record");
805 record_id = rec->handle;
810 sdp_record_free(rec);
815 static struct health_app *create_health_app(const char *app_name,
816 const char *provider, const char *srv_name,
817 const char *srv_descr, uint8_t mdeps)
819 struct health_app *app;
820 static unsigned int app_id = 1;
824 app = new0(struct health_app, 1);
826 app->num_of_mdep = mdeps;
827 app->app_name = strdup(app_name);
830 app->provider_name = strdup(provider);
831 if (!app->provider_name)
836 app->service_name = strdup(srv_name);
837 if (!app->service_name)
842 app->service_descr = strdup(srv_descr);
843 if (!app->service_descr)
847 app->mdeps = queue_new();
848 app->devices = queue_new();
853 free_health_app(app);
857 static void bt_health_register_app(const void *buf, uint16_t len)
859 const struct hal_cmd_health_reg_app *cmd = buf;
860 struct hal_rsp_health_reg_app rsp;
861 struct health_app *app;
863 uint16_t app_name_len, provider_len, srv_name_len, srv_descr_len;
864 char *app_name, *provider = NULL, *srv_name = NULL, *srv_descr = NULL;
868 if (len != sizeof(*cmd) + cmd->len ||
869 cmd->app_name_off > cmd->provider_name_off ||
870 cmd->provider_name_off > cmd->service_name_off ||
871 cmd->service_name_off > cmd->service_descr_off ||
872 cmd->service_descr_off > cmd->len) {
873 error("health: Invalid register app command, terminating");
878 app_name = (char *) cmd->data;
879 app_name_len = cmd->provider_name_off - cmd->app_name_off;
882 provider_len = cmd->service_name_off - off;
883 if (provider_len > 0)
884 provider = (char *) cmd->data + off;
887 srv_name_len = cmd->service_descr_off - off;
888 if (srv_name_len > 0)
889 srv_name = (char *) cmd->data + off;
892 srv_descr_len = cmd->len - off;
893 if (srv_descr_len > 0)
894 srv_descr = (char *) cmd->data + off;
896 app = create_health_app(app_name, provider, srv_name, srv_descr,
901 queue_push_tail(apps, app);
903 rsp.app_id = app->id;
904 ipc_send_rsp_full(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_REG_APP,
905 sizeof(rsp), &rsp, -1);
909 free_health_app(app);
910 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
914 static uint8_t android2channel_type(uint8_t type)
917 case HAL_HEALTH_CHANNEL_TYPE_RELIABLE:
918 return CHANNEL_TYPE_RELIABLE;
919 case HAL_HEALTH_CHANNEL_TYPE_STREAMING:
920 return CHANNEL_TYPE_STREAM;
922 return CHANNEL_TYPE_ANY;
926 static void bt_health_mdep_cfg_data(const void *buf, uint16_t len)
928 const struct hal_cmd_health_mdep *cmd = buf;
929 struct health_app *app;
930 struct mdep_cfg *mdep = NULL;
935 app = queue_find(apps, match_app_by_id, INT_TO_PTR(cmd->app_id));
937 status = HAL_STATUS_INVALID;
941 mdep = new0(struct mdep_cfg, 1);
942 mdep->role = cmd->role;
943 mdep->data_type = cmd->data_type;
944 mdep->channel_type = android2channel_type(cmd->channel_type);
945 mdep->id = queue_length(app->mdeps) + 1;
947 if (cmd->descr_len > 0) {
948 mdep->descr = malloc0(cmd->descr_len);
949 memcpy(mdep->descr, cmd->descr, cmd->descr_len);
952 queue_push_tail(app->mdeps, mdep);
954 if (app->num_of_mdep != queue_length(app->mdeps))
957 /* add sdp record from app configuration data */
959 * TODO: Check what to be done if mupltple applications are trying to
960 * register with different role and different configurations.
961 * 1) Does device supports SOURCE and SINK at the same time ?
962 * 2) Does it require different SDP records or one record with
963 * multile MDEP configurations ?
965 if (update_sdp_record(app) < 0) {
966 error("health: HDP SDP record preparation failed");
967 status = HAL_STATUS_FAILED;
971 send_app_reg_notify(app, HAL_HEALTH_APP_REG_SUCCESS);
974 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
979 if (status != HAL_STATUS_SUCCESS) {
981 queue_remove(apps, app);
982 free_health_app(app);
985 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
989 static void bt_health_unregister_app(const void *buf, uint16_t len)
991 const struct hal_cmd_health_unreg_app *cmd = buf;
992 struct health_app *app;
996 app = queue_remove_if(apps, match_app_by_id, INT_TO_PTR(cmd->app_id));
998 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
999 HAL_OP_HEALTH_UNREG_APP, HAL_STATUS_INVALID);
1003 send_app_reg_notify(app, HAL_HEALTH_APP_DEREG_SUCCESS);
1005 if (record_id > 0) {
1006 bt_adapter_remove_record(record_id);
1010 free_health_app(app);
1011 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1012 HAL_OP_HEALTH_UNREG_APP, HAL_STATUS_SUCCESS);
1015 static int get_prot_desc_entry(sdp_data_t *entry, int type, guint16 *val)
1020 if (!entry || !SDP_IS_SEQ(entry->dtd))
1023 iter = entry->val.dataseq;
1024 if (!(iter->dtd & SDP_UUID_UNSPEC))
1027 proto = sdp_uuid_to_proto(&iter->val.uuid);
1035 if (iter->dtd != SDP_UINT16)
1038 *val = iter->val.uint16;
1043 static int get_prot_desc_list(const sdp_record_t *rec, uint16_t *psm,
1046 sdp_data_t *pdl, *p0, *p1;
1048 if (!psm && !version)
1051 pdl = sdp_data_get(rec, SDP_ATTR_PROTO_DESC_LIST);
1052 if (!pdl || !SDP_IS_SEQ(pdl->dtd))
1055 p0 = pdl->val.dataseq;
1056 if (get_prot_desc_entry(p0, L2CAP_UUID, psm) < 0)
1060 if (get_prot_desc_entry(p1, MCAP_CTRL_UUID, version) < 0)
1066 static int get_ccpsm(sdp_list_t *recs, uint16_t *ccpsm)
1070 for (l = recs; l; l = l->next) {
1071 sdp_record_t *rec = l->data;
1073 if (!get_prot_desc_list(rec, ccpsm, NULL))
1080 static int get_add_prot_desc_list(const sdp_record_t *rec, uint16_t *psm)
1082 sdp_data_t *pdl, *p0, *p1;
1087 pdl = sdp_data_get(rec, SDP_ATTR_ADD_PROTO_DESC_LIST);
1088 if (!pdl || pdl->dtd != SDP_SEQ8)
1091 pdl = pdl->val.dataseq;
1092 if (pdl->dtd != SDP_SEQ8)
1095 p0 = pdl->val.dataseq;
1097 if (get_prot_desc_entry(p0, L2CAP_UUID, psm) < 0)
1101 if (get_prot_desc_entry(p1, MCAP_DATA_UUID, NULL) < 0)
1107 static int get_dcpsm(sdp_list_t *recs, uint16_t *dcpsm)
1111 for (l = recs; l; l = l->next) {
1112 sdp_record_t *rec = l->data;
1114 if (!get_add_prot_desc_list(rec, dcpsm))
1121 static int send_echo_data(int sock, const void *buf, uint32_t size)
1123 const uint8_t *buf_b = buf;
1126 while (sent < size) {
1127 int n = write(sock, buf_b + sent, size - sent);
1136 static gboolean serve_echo(GIOChannel *io, GIOCondition cond, gpointer data)
1138 struct health_channel *channel = data;
1139 uint8_t buf[MCAP_DC_MTU];
1142 DBG("channel %p", channel);
1144 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
1145 DBG("Error condition on channel");
1149 fd = g_io_channel_unix_get_fd(io);
1151 len = read(fd, buf, sizeof(buf));
1153 DBG("Error reading ECHO");
1157 ret = send_echo_data(fd, buf, len);
1159 DBG("Error sending ECHO back");
1164 static void mcap_mdl_connected_cb(struct mcap_mdl *mdl, void *data)
1166 struct health_channel *channel = data;
1169 DBG("Data channel connected: mdl %p channel %p", mdl, channel);
1172 channel = search_channel_by_mdl(mdl);
1174 error("health: channel data does not exist");
1180 channel->mdl = mcap_mdl_ref(mdl);
1182 fd = mcap_mdl_get_fd(channel->mdl);
1184 error("health: error retrieving fd");
1188 if (channel->mdep_id == MDEP_ECHO) {
1191 io = g_io_channel_unix_new(fd);
1192 g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL | G_IO_IN,
1193 serve_echo, channel);
1194 g_io_channel_unref(io);
1199 info("health: MDL connected");
1200 send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_CONNECTED, fd);
1204 /* TODO: mcap_mdl_abort */
1205 destroy_channel(channel);
1208 static void mcap_mdl_closed_cb(struct mcap_mdl *mdl, void *data)
1210 struct health_channel *channel = data;
1212 info("health: MDL closed");
1217 channel->mdl_conn = false;
1220 static void mcap_mdl_deleted_cb(struct mcap_mdl *mdl, void *data)
1222 struct health_channel *channel;
1224 info("health: MDL deleted");
1226 channel = search_channel_by_mdl(mdl);
1230 DBG("channel %p mdl %p", channel, mdl);
1231 destroy_channel(channel);
1234 static void mcap_mdl_aborted_cb(struct mcap_mdl *mdl, void *data)
1236 DBG("Not Implemeneted");
1239 static struct health_device *create_device(struct health_app *app,
1240 const uint8_t *addr)
1242 struct health_device *dev;
1244 /* create device and push it to devices queue */
1245 dev = new0(struct health_device, 1);
1247 android2bdaddr(addr, &dev->dst);
1248 dev->channels = queue_new();
1249 dev->app_id = app->id;
1251 queue_push_tail(app->devices, dev);
1256 static struct health_device *get_device(struct health_app *app,
1257 const uint8_t *addr)
1259 struct health_device *dev;
1262 android2bdaddr(addr, &bdaddr);
1263 dev = queue_find(app->devices, match_dev_by_addr, &bdaddr);
1267 return create_device(app, addr);
1270 static struct health_channel *create_channel(struct health_app *app,
1272 struct health_device *dev)
1274 struct mdep_cfg *mdep;
1275 struct health_channel *channel;
1276 static unsigned int channel_id = 1;
1278 DBG("mdep %u", mdep_index);
1283 mdep = queue_find(app->mdeps, match_mdep_by_id, INT_TO_PTR(mdep_index));
1285 if (mdep_index == MDEP_ECHO) {
1286 mdep = new0(struct mdep_cfg, 1);
1288 /* Leave other configuration zeroes */
1289 mdep->id = MDEP_ECHO;
1291 queue_push_tail(app->mdeps, mdep);
1297 /* create channel and push it to device */
1298 channel = new0(struct health_channel, 1);
1299 channel->mdep_id = mdep->id;
1300 channel->type = mdep->channel_type;
1301 channel->id = channel_id++;
1304 queue_push_tail(dev->channels, channel);
1309 static struct health_channel *connect_channel(struct health_app *app,
1310 struct mcap_mcl *mcl,
1313 struct health_device *device;
1316 DBG("app %p mdepid %u", app, mdepid);
1318 mcap_mcl_get_addr(mcl, &addr);
1321 DBG("No app found for mdepid %u", mdepid);
1325 device = get_device(app, (uint8_t *) &addr);
1327 return create_channel(app, mdepid, device);
1330 static uint8_t conf_to_l2cap(uint8_t conf)
1332 return conf == CHANNEL_TYPE_STREAM ? L2CAP_MODE_STREAMING :
1336 static uint8_t mcap_mdl_conn_req_cb(struct mcap_mcl *mcl, uint8_t mdepid,
1337 uint16_t mdlid, uint8_t *conf, void *data)
1339 GError *gerr = NULL;
1340 struct health_channel *channel;
1341 struct health_app *app;
1342 struct mdep_cfg *mdep;
1344 DBG("Data channel request: mdepid %u mdlid %u conf %u",
1345 mdepid, mdlid, *conf);
1347 if (mdepid == MDEP_ECHO)
1348 /* For echo service take last app */
1349 app = queue_peek_tail(apps);
1351 app = search_app_by_mdepid(mdepid);
1354 return MCAP_MDL_BUSY;
1356 channel = connect_channel(app, mcl, mdepid);
1358 return MCAP_MDL_BUSY;
1360 /* Channel is assigned here after creation */
1361 mcl->cb->user_data = channel;
1363 if (mdepid == MDEP_ECHO) {
1365 case CHANNEL_TYPE_ANY:
1366 *conf = CHANNEL_TYPE_RELIABLE;
1368 case CHANNEL_TYPE_RELIABLE:
1370 case CHANNEL_TYPE_STREAM:
1371 return MCAP_CONFIGURATION_REJECTED;
1374 * Special case defined in HDP spec 3.4.
1375 * When an invalid configuration is received we shall
1376 * close the MCL when we are still processing the
1379 /* TODO close device */
1380 return MCAP_CONFIGURATION_REJECTED; /* not processed */
1383 if (!mcap_set_data_chan_mode(mcap, L2CAP_MODE_ERTM, &gerr)) {
1384 error("Error: %s", gerr->message);
1386 return MCAP_MDL_BUSY;
1389 /* TODO: Create channel */
1391 return MCAP_SUCCESS;
1394 mdep = queue_find(app->mdeps, match_mdep_by_id, INT_TO_PTR(mdepid));
1396 return MCAP_MDL_BUSY;
1399 case CHANNEL_TYPE_ANY:
1400 if (mdep->role == HAL_HEALTH_MDEP_ROLE_SINK) {
1401 return MCAP_CONFIGURATION_REJECTED;
1403 if (queue_length(channel->dev->channels) <= 1)
1404 *conf = CHANNEL_TYPE_RELIABLE;
1406 *conf = CHANNEL_TYPE_STREAM;
1409 case CHANNEL_TYPE_STREAM:
1410 if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
1411 return MCAP_CONFIGURATION_REJECTED;
1413 case CHANNEL_TYPE_RELIABLE:
1414 if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
1415 return MCAP_CONFIGURATION_REJECTED;
1419 * Special case defined in HDP spec 3.4. When an invalid
1420 * configuration is received we shall close the MCL when
1421 * we are still processing the callback.
1423 /* TODO: close device */
1424 return MCAP_CONFIGURATION_REJECTED; /* not processed */
1427 if (!mcap_set_data_chan_mode(mcap, conf_to_l2cap(*conf), &gerr)) {
1428 error("health: error setting L2CAP mode: %s", gerr->message);
1430 return MCAP_MDL_BUSY;
1433 return MCAP_SUCCESS;
1436 static uint8_t mcap_mdl_reconn_req_cb(struct mcap_mdl *mdl, void *data)
1438 struct health_channel *channel;
1443 channel = search_channel_by_mdl(mdl);
1445 error("health: channel data does not exist");
1446 return MCAP_UNSPECIFIED_ERROR;
1449 if (!mcap_set_data_chan_mode(mcap,
1450 conf_to_l2cap(channel->type), &err)) {
1451 error("health: %s", err->message);
1453 return MCAP_MDL_BUSY;
1456 return MCAP_SUCCESS;
1459 static void connect_mdl_cb(struct mcap_mdl *mdl, GError *gerr, gpointer data)
1461 struct health_channel *channel = data;
1467 error("health: error connecting to MDL %s", gerr->message);
1471 fd = mcap_mdl_get_fd(channel->mdl);
1473 error("health: error retrieving fd");
1477 info("health: MDL connected");
1478 channel->mdl_conn = true;
1480 /* first data channel should be reliable data channel */
1481 if (!queue_length(channel->dev->channels))
1482 if (channel->type != CHANNEL_TYPE_RELIABLE)
1485 send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_CONNECTED, fd);
1490 /* TODO: mcap_mdl_abort */
1491 destroy_channel(channel);
1494 static void reconnect_mdl_cb(struct mcap_mdl *mdl, GError *gerr, gpointer data)
1496 struct health_channel *channel = data;
1503 error("health: error reconnecting to MDL %s", gerr->message);
1507 channel->mdl_id = mcap_mdl_get_mdlid(mdl);
1509 if (channel->type == CHANNEL_TYPE_RELIABLE)
1510 mode = L2CAP_MODE_ERTM;
1512 mode = L2CAP_MODE_STREAMING;
1514 if (!mcap_connect_mdl(channel->mdl, mode, channel->dev->dcpsm,
1515 connect_mdl_cb, channel,
1517 error("health: error connecting to mdl");
1525 /* TODO: mcap_mdl_abort */
1526 destroy_channel(channel);
1529 static int reconnect_mdl(struct health_channel *channel)
1531 GError *gerr = NULL;
1538 if (!mcap_reconnect_mdl(channel->mdl, reconnect_mdl_cb, channel,
1540 error("health: reconnect failed %s", gerr->message);
1541 destroy_channel(channel);
1547 static void create_mdl_cb(struct mcap_mdl *mdl, uint8_t type, GError *gerr,
1550 struct health_channel *channel = data;
1556 error("health: error creating MDL %s", gerr->message);
1560 if (channel->type == CHANNEL_TYPE_ANY && type != CHANNEL_TYPE_ANY)
1561 channel->type = type;
1564 * if requested channel type is not same as preferred
1565 * channel type from remote device, then abort the connection.
1567 if (channel->type != type) {
1568 /* TODO: abort mdl */
1569 error("health: channel type requested %d preferred %d not same",
1570 channel->type, type);
1575 channel->mdl = mcap_mdl_ref(mdl);
1577 channel->type = type;
1578 channel->mdl_id = mcap_mdl_get_mdlid(mdl);
1580 if (channel->type == CHANNEL_TYPE_RELIABLE)
1581 mode = L2CAP_MODE_ERTM;
1583 mode = L2CAP_MODE_STREAMING;
1585 if (!mcap_connect_mdl(channel->mdl, mode, channel->dev->dcpsm,
1586 connect_mdl_cb, channel,
1588 error("health: error connecting to mdl");
1596 destroy_channel(channel);
1599 static bool check_role(uint8_t rec_role, uint8_t app_role)
1601 if ((rec_role == HAL_HEALTH_MDEP_ROLE_SINK &&
1602 app_role == HAL_HEALTH_MDEP_ROLE_SOURCE) ||
1603 (rec_role == HAL_HEALTH_MDEP_ROLE_SOURCE &&
1604 app_role == HAL_HEALTH_MDEP_ROLE_SINK))
1610 static bool get_mdep_from_rec(const sdp_record_t *rec, uint8_t role,
1611 uint16_t d_type, uint8_t *mdep)
1613 sdp_data_t *list, *feat;
1618 list = sdp_data_get(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST);
1619 if (!list || !SDP_IS_SEQ(list->dtd))
1622 for (feat = list->val.dataseq; feat; feat = feat->next) {
1623 sdp_data_t *data_type, *mdepid, *role_t;
1625 if (!SDP_IS_SEQ(feat->dtd))
1628 mdepid = feat->val.dataseq;
1632 data_type = mdepid->next;
1636 role_t = data_type->next;
1640 if (data_type->dtd != SDP_UINT16 || mdepid->dtd != SDP_UINT8 ||
1641 role_t->dtd != SDP_UINT8)
1644 if (data_type->val.uint16 != d_type ||
1645 !check_role(role_t->val.uint8, role))
1648 *mdep = mdepid->val.uint8;
1656 static bool get_remote_mdep(sdp_list_t *recs, struct health_channel *channel)
1658 struct health_app *app;
1659 struct mdep_cfg *mdep;
1662 app = queue_find(apps, match_app_by_id,
1663 INT_TO_PTR(channel->dev->app_id));
1667 mdep = queue_find(app->mdeps, match_mdep_by_id,
1668 INT_TO_PTR(channel->mdep_id));
1672 if (!get_mdep_from_rec(recs->data, mdep->role, mdep->data_type,
1674 error("health: no matching MDEP: %u", channel->mdep_id);
1678 channel->remote_mdep = mdep_id;
1682 static bool create_mdl(struct health_channel *channel)
1684 struct health_app *app;
1685 struct mdep_cfg *mdep;
1687 GError *gerr = NULL;
1689 app = queue_find(apps, match_app_by_id,
1690 INT_TO_PTR(channel->dev->app_id));
1694 mdep = queue_find(app->mdeps, match_mdep_by_id,
1695 INT_TO_PTR(channel->mdep_id));
1699 if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
1700 type = channel->type;
1702 type = CHANNEL_TYPE_ANY;
1704 if (!mcap_create_mdl(channel->dev->mcl, channel->remote_mdep,
1705 type, create_mdl_cb, channel, NULL, &gerr)) {
1706 error("health: error creating mdl %s", gerr->message);
1714 static bool set_mcl_cb(struct mcap_mcl *mcl, gpointer user_data, GError **err)
1716 return mcap_mcl_set_cb(mcl, user_data, err,
1717 MCAP_MDL_CB_CONNECTED, mcap_mdl_connected_cb,
1718 MCAP_MDL_CB_CLOSED, mcap_mdl_closed_cb,
1719 MCAP_MDL_CB_DELETED, mcap_mdl_deleted_cb,
1720 MCAP_MDL_CB_ABORTED, mcap_mdl_aborted_cb,
1721 MCAP_MDL_CB_REMOTE_CONN_REQ, mcap_mdl_conn_req_cb,
1722 MCAP_MDL_CB_REMOTE_RECONN_REQ, mcap_mdl_reconn_req_cb,
1723 MCAP_MDL_CB_INVALID);
1726 static void create_mcl_cb(struct mcap_mcl *mcl, GError *err, gpointer data)
1728 struct health_channel *channel = data;
1730 GError *gerr = NULL;
1735 error("health: error creating MCL : %s", err->message);
1739 if (!channel->dev->mcl)
1740 channel->dev->mcl = mcap_mcl_ref(mcl);
1742 info("health: MCL connected");
1744 ret = set_mcl_cb(channel->dev->mcl, channel, &gerr);
1746 error("health: error setting mdl callbacks: %s", gerr->message);
1751 if (!create_mdl(channel))
1757 destroy_channel(channel);
1760 static void search_cb(sdp_list_t *recs, int err, gpointer data)
1762 struct health_channel *channel = data;
1763 GError *gerr = NULL;
1767 if (err < 0 || !recs) {
1768 error("health: Error getting remote SDP records");
1772 if (get_ccpsm(recs, &channel->dev->ccpsm) < 0) {
1773 error("health: Can't get remote PSM for control channel");
1777 if (get_dcpsm(recs, &channel->dev->dcpsm) < 0) {
1778 error("health: Can't get remote PSM for data channel");
1782 if (!get_remote_mdep(recs, channel)) {
1783 error("health: Can't get remote MDEP data");
1787 if (!mcap_create_mcl(mcap, &channel->dev->dst, channel->dev->ccpsm,
1788 create_mcl_cb, channel, NULL, &gerr)) {
1789 error("health: error creating mcl %s", gerr->message);
1797 destroy_channel(channel);
1800 static int connect_mcl(struct health_channel *channel)
1807 bt_string2uuid(&uuid, HDP_UUID);
1809 err = bt_search_service(&adapter_addr, &channel->dev->dst, &uuid,
1810 search_cb, channel, NULL, 0);
1812 send_channel_state_notify(channel,
1813 HAL_HEALTH_CHANNEL_CONNECTING, -1);
1818 static struct health_app *get_app(uint16_t app_id)
1820 return queue_find(apps, match_app_by_id, INT_TO_PTR(app_id));
1823 static struct health_channel *get_channel(struct health_app *app,
1825 struct health_device *dev)
1827 struct health_channel *channel;
1833 index = mdep_index + 1;
1834 channel = queue_find(dev->channels, match_channel_by_mdep_id,
1839 return create_channel(app, index, dev);
1842 static void bt_health_connect_channel(const void *buf, uint16_t len)
1844 const struct hal_cmd_health_connect_channel *cmd = buf;
1845 struct hal_rsp_health_connect_channel rsp;
1846 struct health_device *dev = NULL;
1847 struct health_channel *channel = NULL;
1848 struct health_app *app;
1852 app = get_app(cmd->app_id);
1856 dev = get_device(app, cmd->bdaddr);
1858 channel = get_channel(app, cmd->mdep_index, dev);
1862 if (!queue_length(dev->channels)) {
1863 if (channel->type != CHANNEL_TYPE_RELIABLE) {
1864 error("health: first data shannel should be reliable");
1870 if (connect_mcl(channel) < 0) {
1871 error("health: error retrieving HDP SDP record");
1875 /* data channel is already connected */
1876 if (channel->mdl && channel->mdl_conn)
1879 /* create mdl if it does not exists */
1880 if (!channel->mdl && !create_mdl(channel))
1883 /* reconnect mdl if it exists */
1884 if (channel->mdl && !channel->mdl_conn) {
1885 if (reconnect_mdl(channel) < 0)
1891 rsp.channel_id = channel->id;
1892 ipc_send_rsp_full(hal_ipc, HAL_SERVICE_ID_HEALTH,
1893 HAL_OP_HEALTH_CONNECT_CHANNEL,
1894 sizeof(rsp), &rsp, -1);
1898 queue_remove(channel->dev->channels, channel);
1899 free_health_channel(channel);
1902 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1903 HAL_OP_HEALTH_CONNECT_CHANNEL, HAL_STATUS_FAILED);
1906 static void channel_delete_cb(GError *gerr, gpointer data)
1908 struct health_channel *channel = data;
1913 error("health: channel delete failed %s", gerr->message);
1917 destroy_channel(channel);
1920 static void bt_health_destroy_channel(const void *buf, uint16_t len)
1922 const struct hal_cmd_health_destroy_channel *cmd = buf;
1923 struct health_channel *channel;
1924 GError *gerr = NULL;
1928 channel = search_channel_by_id(cmd->channel_id);
1932 if (!mcap_delete_mdl(channel->mdl, channel_delete_cb, channel,
1934 error("health: channel delete failed %s", gerr->message);
1938 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1939 HAL_OP_HEALTH_DESTROY_CHANNEL, HAL_STATUS_SUCCESS);
1944 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1945 HAL_OP_HEALTH_DESTROY_CHANNEL, HAL_STATUS_INVALID);
1948 static const struct ipc_handler cmd_handlers[] = {
1949 /* HAL_OP_HEALTH_REG_APP */
1950 { bt_health_register_app, true,
1951 sizeof(struct hal_cmd_health_reg_app) },
1952 /* HAL_OP_HEALTH_MDEP */
1953 { bt_health_mdep_cfg_data, true,
1954 sizeof(struct hal_cmd_health_mdep) },
1955 /* HAL_OP_HEALTH_UNREG_APP */
1956 { bt_health_unregister_app, false,
1957 sizeof(struct hal_cmd_health_unreg_app) },
1958 /* HAL_OP_HEALTH_CONNECT_CHANNEL */
1959 { bt_health_connect_channel, false,
1960 sizeof(struct hal_cmd_health_connect_channel) },
1961 /* HAL_OP_HEALTH_DESTROY_CHANNEL */
1962 { bt_health_destroy_channel, false,
1963 sizeof(struct hal_cmd_health_destroy_channel) },
1966 static void mcl_connected(struct mcap_mcl *mcl, gpointer data)
1968 GError *gerr = NULL;
1973 info("health: MCL connected");
1974 ret = set_mcl_cb(mcl, NULL, &gerr);
1976 error("health: error setting mcl callbacks: %s", gerr->message);
1981 static void mcl_reconnected(struct mcap_mcl *mcl, gpointer data)
1983 struct health_device *dev;
1987 info("health: MCL reconnected");
1988 dev = search_dev_by_mcl(mcl);
1990 error("device data does not exists");
1995 static void mcl_disconnected(struct mcap_mcl *mcl, gpointer data)
1997 struct health_device *dev;
2001 info("health: MCL disconnected");
2002 dev = search_dev_by_mcl(mcl);
2006 static void mcl_uncached(struct mcap_mcl *mcl, gpointer data)
2008 /* mcap library maintains cache of mcls, not required here */
2011 bool bt_health_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
2017 bacpy(&adapter_addr, addr);
2019 mcap = mcap_create_instance(&adapter_addr, BT_IO_SEC_MEDIUM, 0, 0,
2020 mcl_connected, mcl_reconnected,
2021 mcl_disconnected, mcl_uncached,
2022 NULL, /* CSP is not used right now */
2025 error("health: MCAP instance creation failed %s", err->message);
2033 ipc_register(hal_ipc, HAL_SERVICE_ID_HEALTH, cmd_handlers,
2034 G_N_ELEMENTS(cmd_handlers));
2039 void bt_health_unregister(void)
2043 mcap_instance_unref(mcap);
2044 queue_destroy(apps, free_health_app);
2045 ipc_unregister(hal_ipc, HAL_SERVICE_ID_HEALTH);