Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / android / health.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2014  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
7  *
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdint.h>
30 #include <stdbool.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <glib.h>
34
35 #include "btio/btio.h"
36 #include "lib/bluetooth.h"
37 #include "lib/sdp.h"
38 #include "lib/sdp_lib.h"
39 #include "lib/uuid.h"
40 #include "lib/l2cap.h"
41 #include "src/log.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"
47
48 #include "hal-msg.h"
49 #include "ipc-common.h"
50 #include "ipc.h"
51 #include "utils.h"
52 #include "bluetooth.h"
53 #include "health.h"
54
55 #define SVC_HINT_HEALTH                 0x00
56 #define HDP_VERSION                     0x0101
57 #define DATA_EXCHANGE_SPEC_11073        0x01
58
59 #define CHANNEL_TYPE_ANY       0x00
60 #define CHANNEL_TYPE_RELIABLE  0x01
61 #define CHANNEL_TYPE_STREAM    0x02
62
63 #define MDEP_ECHO               0x00
64 #define MDEP_INITIAL            0x01
65 #define MDEP_FINAL              0x7F
66
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;
73
74 struct mdep_cfg {
75         uint8_t role;
76         uint16_t data_type;
77         uint8_t channel_type;
78         char *descr;
79
80         uint8_t id; /* mdep id */
81 };
82
83 struct health_device {
84         bdaddr_t dst;
85         uint16_t app_id;
86
87         struct mcap_mcl *mcl;
88
89         struct queue *channels;     /* data channels */
90
91         uint16_t ccpsm;
92         uint16_t dcpsm;
93 };
94
95 struct health_channel {
96         uint8_t mdep_id;
97         uint8_t type;
98
99         struct health_device *dev;
100
101         uint8_t remote_mdep;
102         struct mcap_mdl *mdl;
103         bool mdl_conn;
104         uint16_t mdl_id; /* MDL ID */
105
106         uint16_t id; /* channel id */
107 };
108
109 struct health_app {
110         char *app_name;
111         char *provider_name;
112         char *service_name;
113         char *service_descr;
114         uint8_t num_of_mdep;
115         struct queue *mdeps;
116
117         uint16_t id; /* app id */
118         struct queue *devices;
119 };
120
121 static void send_app_reg_notify(struct health_app *app, uint8_t state)
122 {
123         struct hal_ev_health_app_reg_state ev;
124
125         DBG("");
126
127         ev.id = app->id;
128         ev.state = state;
129
130         ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HEALTH,
131                                 HAL_EV_HEALTH_APP_REG_STATE, sizeof(ev), &ev);
132 }
133
134 static void send_channel_state_notify(struct health_channel *channel,
135                                                 uint8_t state, int fd)
136 {
137         struct hal_ev_health_channel_state ev;
138
139         DBG("");
140
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;
146
147         ipc_send_notif_with_fd(hal_ipc, HAL_SERVICE_ID_HEALTH,
148                                         HAL_EV_HEALTH_CHANNEL_STATE,
149                                         sizeof(ev), &ev, fd);
150 }
151
152 static void unref_mdl(struct health_channel *channel)
153 {
154         if (!channel || !channel->mdl)
155                 return;
156
157         mcap_mdl_unref(channel->mdl);
158         channel->mdl = NULL;
159         channel->mdl_conn = false;
160 }
161
162 static void free_health_channel(void *data)
163 {
164         struct health_channel *channel = data;
165         int fd;
166
167         DBG("channel %p", channel);
168
169         if (!channel)
170                 return;
171
172         fd = mcap_mdl_get_fd(channel->mdl);
173         if (fd >= 0)
174                 shutdown(fd, SHUT_RDWR);
175
176         unref_mdl(channel);
177         free(channel);
178 }
179
180 static void destroy_channel(void *data)
181 {
182         struct health_channel *channel = data;
183
184         if (!channel)
185                 return;
186
187         send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_DESTROYED, -1);
188         queue_remove(channel->dev->channels, channel);
189         free_health_channel(channel);
190 }
191
192 static void unref_mcl(struct health_device *dev)
193 {
194         if (!dev || !dev->mcl)
195                 return;
196
197         mcap_close_mcl(dev->mcl, FALSE);
198         mcap_mcl_unref(dev->mcl);
199         dev->mcl = NULL;
200 }
201
202 static void free_health_device(void *data)
203 {
204         struct health_device *dev = data;
205
206         if (!dev)
207                 return;
208
209         unref_mcl(dev);
210         queue_destroy(dev->channels, free_health_channel);
211         free(dev);
212 }
213
214 static void free_mdep_cfg(void *data)
215 {
216         struct mdep_cfg *cfg = data;
217
218         if (!cfg)
219                 return;
220
221         free(cfg->descr);
222         free(cfg);
223 }
224
225 static void free_health_app(void *data)
226 {
227         struct health_app *app = data;
228
229         if (!app)
230                 return;
231
232         free(app->app_name);
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);
238         free(app);
239 }
240
241 static bool match_channel_by_mdl(const void *data, const void *user_data)
242 {
243         const struct health_channel *channel = data;
244         const struct mcap_mdl *mdl = user_data;
245
246         return channel->mdl == mdl;
247 }
248
249 static bool match_channel_by_id(const void *data, const void *user_data)
250 {
251         const struct health_channel *channel = data;
252         uint16_t channel_id = PTR_TO_INT(user_data);
253
254         return channel->id == channel_id;
255 }
256
257 static bool match_dev_by_mcl(const void *data, const void *user_data)
258 {
259         const struct health_device *dev = data;
260         const struct mcap_mcl *mcl = user_data;
261
262         return dev->mcl == mcl;
263 }
264
265 static bool match_dev_by_addr(const void *data, const void *user_data)
266 {
267         const struct health_device *dev = data;
268         const bdaddr_t *addr = user_data;
269
270         return !bacmp(&dev->dst, addr);
271 }
272
273 static bool match_channel_by_mdep_id(const void *data, const void *user_data)
274 {
275         const struct health_channel *channel = data;
276         uint16_t mdep_id = PTR_TO_INT(user_data);
277
278         return channel->mdep_id == mdep_id;
279 }
280
281 static bool match_mdep_by_role(const void *data, const void *user_data)
282 {
283         const struct mdep_cfg *mdep = data;
284         uint16_t role = PTR_TO_INT(user_data);
285
286         return mdep->role == role;
287 }
288
289 static bool match_mdep_by_id(const void *data, const void *user_data)
290 {
291         const struct mdep_cfg *mdep = data;
292         uint16_t mdep_id = PTR_TO_INT(user_data);
293
294         return mdep->id == mdep_id;
295 }
296
297 static bool match_app_by_id(const void *data, const void *user_data)
298 {
299         const struct health_app *app = data;
300         uint16_t app_id = PTR_TO_INT(user_data);
301
302         return app->id == app_id;
303 }
304
305 static struct health_channel *search_channel_by_id(uint16_t id)
306 {
307         const struct queue_entry *apps_entry, *devices_entry;
308         struct health_app *app;
309         struct health_channel *channel;
310         struct health_device *dev;
311
312         DBG("");
313
314         apps_entry = queue_get_entries(apps);
315         while (apps_entry) {
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,
321                                                                 INT_TO_PTR(id));
322
323                         if (channel)
324                                 return channel;
325
326                         devices_entry = devices_entry->next;
327                 }
328
329                 apps_entry = apps_entry->next;
330         }
331
332         return NULL;
333 }
334
335 static struct health_channel *search_channel_by_mdl(struct mcap_mdl *mdl)
336 {
337         const struct queue_entry *apps_entry, *devices_entry;
338         struct health_app *app;
339         struct health_channel *channel;
340         struct health_device *dev;
341
342         DBG("");
343
344         apps_entry = queue_get_entries(apps);
345         while (apps_entry) {
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);
352
353                         if (channel)
354                                 return channel;
355
356                         devices_entry = devices_entry->next;
357                 }
358
359                 apps_entry = apps_entry->next;
360         }
361
362         return NULL;
363 }
364
365 static struct health_device *search_dev_by_mcl(struct mcap_mcl *mcl)
366 {
367         const struct queue_entry *apps_entry;
368         struct health_app *app;
369         struct health_device *dev;
370
371         DBG("");
372
373         apps_entry = queue_get_entries(apps);
374         while (apps_entry) {
375                 app = apps_entry->data;
376
377                 dev = queue_find(app->devices, match_dev_by_mcl, mcl);
378
379                 if (dev)
380                         return dev;
381
382                 apps_entry = apps_entry->next;
383         }
384
385         return NULL;
386 }
387
388 static struct health_app *search_app_by_mdepid(uint8_t mdepid)
389 {
390         const struct queue_entry *apps_entry;
391         struct health_app *app;
392
393         DBG("");
394
395         apps_entry = queue_get_entries(apps);
396         while (apps_entry) {
397                 app = apps_entry->data;
398
399                 if (queue_find(app->mdeps, match_mdep_by_id,
400                                                         INT_TO_PTR(mdepid)))
401                         return app;
402
403                 apps_entry = apps_entry->next;
404         }
405
406         return NULL;
407 }
408
409 static int register_service_protocols(sdp_record_t *rec,
410                                         struct health_app *app)
411 {
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;
416         uint32_t ccpsm;
417         uint16_t version = MCAP_VERSION;
418         GError *err = NULL;
419         int ret = -1;
420
421         DBG("");
422
423         /* set l2cap information */
424         sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
425         l2cap_list = sdp_list_append(NULL, &l2cap_uuid);
426         if (!l2cap_list)
427                 goto fail;
428
429         ccpsm = mcap_get_ctrl_psm(mcap, &err);
430         if (err)
431                 goto fail;
432
433         psm = sdp_data_alloc(SDP_UINT16, &ccpsm);
434         if (!psm)
435                 goto fail;
436
437         if (!sdp_list_append(l2cap_list, psm))
438                 goto fail;
439
440         proto_list = sdp_list_append(NULL, l2cap_list);
441         if (!proto_list)
442                 goto fail;
443
444         /* set mcap information */
445         sdp_uuid16_create(&mcap_c_uuid, MCAP_CTRL_UUID);
446         mcap_list = sdp_list_append(NULL, &mcap_c_uuid);
447         if (!mcap_list)
448                 goto fail;
449
450         mcap_ver = sdp_data_alloc(SDP_UINT16, &version);
451         if (!mcap_ver)
452                 goto fail;
453
454         if (!sdp_list_append(mcap_list, mcap_ver))
455                 goto fail;
456
457         if (!sdp_list_append(proto_list, mcap_list))
458                 goto fail;
459
460         /* attach protocol information to service record */
461         access_proto_list = sdp_list_append(NULL, proto_list);
462         if (!access_proto_list)
463                 goto fail;
464
465         sdp_set_access_protos(rec, access_proto_list);
466         ret = 0;
467
468 fail:
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);
473
474         if (psm)
475                 sdp_data_free(psm);
476
477         if (mcap_ver)
478                 sdp_data_free(mcap_ver);
479
480         if (err)
481                 g_error_free(err);
482
483         return ret;
484 }
485
486 static int register_service_profiles(sdp_record_t *rec)
487 {
488         int ret;
489         sdp_list_t *profile_list;
490         sdp_profile_desc_t hdp_profile;
491
492         DBG("");
493
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);
498         if (!profile_list)
499                 return -1;
500
501         /* set profile descriptor list */
502         ret = sdp_set_profile_descs(rec, profile_list);
503         sdp_list_free(profile_list, NULL);
504
505         return ret;
506 }
507
508 static int register_service_additional_protocols(sdp_record_t *rec,
509                                                 struct health_app *app)
510 {
511         int ret = -1;
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;
516         uint32_t dcpsm;
517         GError *err = NULL;
518
519         DBG("");
520
521         /* set l2cap information */
522         sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
523         l2cap_list = sdp_list_append(NULL, &l2cap_uuid);
524         if (!l2cap_list)
525                 goto fail;
526
527         dcpsm = mcap_get_data_psm(mcap, &err);
528         if (err)
529                 goto fail;
530
531         psm = sdp_data_alloc(SDP_UINT16, &dcpsm);
532         if (!psm)
533                 goto fail;
534
535         if (!sdp_list_append(l2cap_list, psm))
536                 goto fail;
537
538         proto_list = sdp_list_append(NULL, l2cap_list);
539         if (!proto_list)
540                 goto fail;
541
542         /* set mcap information */
543         sdp_uuid16_create(&mcap_d_uuid, MCAP_DATA_UUID);
544         mcap_list = sdp_list_append(NULL, &mcap_d_uuid);
545         if (!mcap_list)
546                 goto fail;
547
548         if (!sdp_list_append(proto_list, mcap_list))
549                 goto fail;
550
551         /* attach protocol information to service record */
552         access_proto_list = sdp_list_append(NULL, proto_list);
553         if (!access_proto_list)
554                 goto fail;
555
556         sdp_set_add_access_protos(rec, access_proto_list);
557         ret = 0;
558
559 fail:
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);
564
565         if (psm)
566                 sdp_data_free(psm);
567
568         if (err)
569                 g_error_free(err);
570
571         return ret;
572 }
573
574 static sdp_list_t *mdeps_to_sdp_features(struct mdep_cfg *mdep)
575 {
576         sdp_data_t *mdepid, *dtype = NULL, *role = NULL, *descr = NULL;
577         sdp_list_t *f_list = NULL;
578
579         DBG("");
580
581         mdepid = sdp_data_alloc(SDP_UINT8, &mdep->id);
582         if (!mdepid)
583                 return NULL;
584
585         dtype = sdp_data_alloc(SDP_UINT16, &mdep->data_type);
586         if (!dtype)
587                 goto fail;
588
589         role = sdp_data_alloc(SDP_UINT8, &mdep->role);
590         if (!role)
591                 goto fail;
592
593         if (mdep->descr) {
594                 descr = sdp_data_alloc(SDP_TEXT_STR8, mdep->descr);
595                 if (!descr)
596                         goto fail;
597         }
598
599         f_list = sdp_list_append(NULL, mdepid);
600         if (!f_list)
601                 goto fail;
602
603         if (!sdp_list_append(f_list, dtype))
604                 goto fail;
605
606         if (!sdp_list_append(f_list, role))
607                 goto fail;
608
609         if (descr && !sdp_list_append(f_list, descr))
610                 goto fail;
611
612         return f_list;
613
614 fail:
615         sdp_list_free(f_list, NULL);
616
617         if (mdepid)
618                 sdp_data_free(mdepid);
619
620         if (dtype)
621                 sdp_data_free(dtype);
622
623         if (role)
624                 sdp_data_free(role);
625
626         if (descr)
627                 sdp_data_free(descr);
628
629         return NULL;
630 }
631
632 static void free_hdp_list(void *list)
633 {
634         sdp_list_t *hdp_list = list;
635
636         sdp_list_free(hdp_list, (sdp_free_func_t)sdp_data_free);
637 }
638
639 static void register_features(void *data, void *user_data)
640 {
641         struct mdep_cfg *mdep = data;
642         sdp_list_t **sup_features = user_data;
643         sdp_list_t *hdp_feature;
644
645         DBG("");
646
647         hdp_feature = mdeps_to_sdp_features(mdep);
648         if (!hdp_feature)
649                 return;
650
651         if (!*sup_features) {
652                 *sup_features = sdp_list_append(NULL, hdp_feature);
653                 if (!*sup_features)
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);
659         }
660 }
661
662 static int register_service_sup_features(sdp_record_t *rec,
663                                                 struct health_app *app)
664 {
665         sdp_list_t *sup_features = NULL;
666
667         DBG("");
668
669         queue_foreach(app->mdeps, register_features, &sup_features);
670         if (!sup_features)
671                 return -1;
672
673         if (sdp_set_supp_feat(rec, sup_features) < 0) {
674                 sdp_list_free(sup_features, free_hdp_list);
675                 return -1;
676         }
677
678         sdp_list_free(sup_features, free_hdp_list);
679         return 0;
680 }
681
682 static int register_data_exchange_spec(sdp_record_t *rec)
683 {
684         sdp_data_t *spec;
685         uint8_t data_spec = DATA_EXCHANGE_SPEC_11073;
686         /* As of now only 11073 is supported, so we set it as default */
687
688         DBG("");
689
690         spec = sdp_data_alloc(SDP_UINT8, &data_spec);
691         if (!spec)
692                 return -1;
693
694         if (sdp_attr_add(rec, SDP_ATTR_DATA_EXCHANGE_SPEC, spec) < 0) {
695                 sdp_data_free(spec);
696                 return -1;
697         }
698
699         return 0;
700 }
701
702 static int register_mcap_features(sdp_record_t *rec)
703 {
704         sdp_data_t *mcap_proc;
705         uint8_t mcap_sup_proc = MCAP_SUP_PROC;
706
707         DBG("");
708
709         mcap_proc = sdp_data_alloc(SDP_UINT8, &mcap_sup_proc);
710         if (!mcap_proc)
711                 return -1;
712
713         if (sdp_attr_add(rec, SDP_ATTR_MCAP_SUPPORTED_PROCEDURES,
714                                                         mcap_proc) < 0) {
715                 sdp_data_free(mcap_proc);
716                 return -1;
717         }
718
719         return 0;
720 }
721
722 static int set_sdp_services_uuid(sdp_record_t *rec, uint8_t role)
723 {
724         uuid_t source, sink;
725         sdp_list_t *list = NULL;
726
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);
730
731         switch (role) {
732         case HAL_HEALTH_MDEP_ROLE_SOURCE:
733                 if (!sdp_list_find(list, &source, sdp_uuid_cmp))
734                         list = sdp_list_append(list, &source);
735                 break;
736         case HAL_HEALTH_MDEP_ROLE_SINK:
737                 if (!sdp_list_find(list, &sink, sdp_uuid_cmp))
738                         list = sdp_list_append(list, &sink);
739                 break;
740         }
741
742         if (sdp_set_service_classes(rec, list) < 0) {
743                 sdp_list_free(list, NULL);
744                 return -1;
745         }
746
747         sdp_list_free(list, NULL);
748
749         return 0;
750 }
751
752 static int update_sdp_record(struct health_app *app)
753 {
754         sdp_record_t *rec;
755         uint8_t role;
756
757         DBG("");
758
759         if (record_id > 0) {
760                 bt_adapter_remove_record(record_id);
761                 record_id = 0;
762         }
763
764         rec = sdp_record_alloc();
765         if (!rec)
766                 return -1;
767
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);
771
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);
775
776         sdp_set_info_attr(rec, app->service_name, app->provider_name,
777                                                         app->service_descr);
778
779         if (register_service_protocols(rec, app) < 0)
780                 goto fail;
781
782         if (register_service_profiles(rec) < 0)
783                 goto fail;
784
785         if (register_service_additional_protocols(rec, app) < 0)
786                 goto fail;
787
788         if (register_service_sup_features(rec, app) < 0)
789                 goto fail;
790
791         if (register_data_exchange_spec(rec) < 0)
792                 goto fail;
793
794         if (register_mcap_features(rec) < 0)
795                 goto fail;
796
797         if (sdp_set_record_state(rec, record_state++) < 0)
798                 goto fail;
799
800         if (bt_adapter_add_record(rec, SVC_HINT_HEALTH) < 0) {
801                 error("health: Failed to register HEALTH record");
802                 goto fail;
803         }
804
805         record_id = rec->handle;
806
807         return 0;
808
809 fail:
810         sdp_record_free(rec);
811
812         return -1;
813 }
814
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)
818 {
819         struct health_app *app;
820         static unsigned int app_id = 1;
821
822         DBG("");
823
824         app = new0(struct health_app, 1);
825         app->id = app_id++;
826         app->num_of_mdep = mdeps;
827         app->app_name = strdup(app_name);
828
829         if (provider) {
830                 app->provider_name = strdup(provider);
831                 if (!app->provider_name)
832                         goto fail;
833         }
834
835         if (srv_name) {
836                 app->service_name = strdup(srv_name);
837                 if (!app->service_name)
838                         goto fail;
839         }
840
841         if (srv_descr) {
842                 app->service_descr = strdup(srv_descr);
843                 if (!app->service_descr)
844                         goto fail;
845         }
846
847         app->mdeps = queue_new();
848         app->devices = queue_new();
849
850         return app;
851
852 fail:
853         free_health_app(app);
854         return NULL;
855 }
856
857 static void bt_health_register_app(const void *buf, uint16_t len)
858 {
859         const struct hal_cmd_health_reg_app *cmd = buf;
860         struct hal_rsp_health_reg_app rsp;
861         struct health_app *app;
862         uint16_t off;
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;
865
866         DBG("");
867
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");
874                 raise(SIGTERM);
875                 return;
876         }
877
878         app_name = (char *) cmd->data;
879         app_name_len = cmd->provider_name_off - cmd->app_name_off;
880
881         off = app_name_len;
882         provider_len = cmd->service_name_off - off;
883         if (provider_len > 0)
884                 provider = (char *) cmd->data + off;
885
886         off += provider_len;
887         srv_name_len = cmd->service_descr_off - off;
888         if (srv_name_len > 0)
889                 srv_name = (char *) cmd->data + off;
890
891         off += srv_name_len;
892         srv_descr_len = cmd->len - off;
893         if (srv_descr_len > 0)
894                 srv_descr = (char *) cmd->data + off;
895
896         app = create_health_app(app_name, provider, srv_name, srv_descr,
897                                                         cmd->num_of_mdep);
898         if (!app)
899                 goto fail;
900
901         queue_push_tail(apps, app);
902
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);
906         return;
907
908 fail:
909         free_health_app(app);
910         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
911                                                         HAL_STATUS_FAILED);
912 }
913
914 static uint8_t android2channel_type(uint8_t type)
915 {
916         switch (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;
921         default:
922                 return CHANNEL_TYPE_ANY;
923         }
924 }
925
926 static void bt_health_mdep_cfg_data(const void *buf, uint16_t len)
927 {
928         const struct hal_cmd_health_mdep *cmd = buf;
929         struct health_app *app;
930         struct mdep_cfg *mdep = NULL;
931         uint8_t status;
932
933         DBG("");
934
935         app = queue_find(apps, match_app_by_id, INT_TO_PTR(cmd->app_id));
936         if (!app) {
937                 status = HAL_STATUS_INVALID;
938                 goto fail;
939         }
940
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;
946
947         if (cmd->descr_len > 0) {
948                 mdep->descr = malloc0(cmd->descr_len);
949                 memcpy(mdep->descr, cmd->descr, cmd->descr_len);
950         }
951
952         queue_push_tail(app->mdeps, mdep);
953
954         if (app->num_of_mdep != queue_length(app->mdeps))
955                 goto send_rsp;
956
957         /* add sdp record from app configuration data */
958         /*
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 ?
964          */
965         if (update_sdp_record(app) < 0) {
966                 error("health: HDP SDP record preparation failed");
967                 status = HAL_STATUS_FAILED;
968                 goto fail;
969         }
970
971         send_app_reg_notify(app, HAL_HEALTH_APP_REG_SUCCESS);
972
973 send_rsp:
974         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
975                                                         HAL_STATUS_SUCCESS);
976         return;
977
978 fail:
979         if (status != HAL_STATUS_SUCCESS) {
980                 free_mdep_cfg(mdep);
981                 queue_remove(apps, app);
982                 free_health_app(app);
983         }
984
985         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
986                                                                 status);
987 }
988
989 static void bt_health_unregister_app(const void *buf, uint16_t len)
990 {
991         const struct hal_cmd_health_unreg_app *cmd = buf;
992         struct health_app *app;
993
994         DBG("");
995
996         app = queue_remove_if(apps, match_app_by_id, INT_TO_PTR(cmd->app_id));
997         if (!app) {
998                 ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
999                                 HAL_OP_HEALTH_UNREG_APP, HAL_STATUS_INVALID);
1000                 return;
1001         }
1002
1003         send_app_reg_notify(app, HAL_HEALTH_APP_DEREG_SUCCESS);
1004
1005         if (record_id > 0) {
1006                 bt_adapter_remove_record(record_id);
1007                 record_id = 0;
1008         }
1009
1010         free_health_app(app);
1011         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1012                                 HAL_OP_HEALTH_UNREG_APP, HAL_STATUS_SUCCESS);
1013 }
1014
1015 static int get_prot_desc_entry(sdp_data_t *entry, int type, guint16 *val)
1016 {
1017         sdp_data_t *iter;
1018         int proto;
1019
1020         if (!entry || !SDP_IS_SEQ(entry->dtd))
1021                 return -1;
1022
1023         iter = entry->val.dataseq;
1024         if (!(iter->dtd & SDP_UUID_UNSPEC))
1025                 return -1;
1026
1027         proto = sdp_uuid_to_proto(&iter->val.uuid);
1028         if (proto != type)
1029                 return -1;
1030
1031         if (!val)
1032                 return 0;
1033
1034         iter = iter->next;
1035         if (iter->dtd != SDP_UINT16)
1036                 return -1;
1037
1038         *val = iter->val.uint16;
1039
1040         return 0;
1041 }
1042
1043 static int get_prot_desc_list(const sdp_record_t *rec, uint16_t *psm,
1044                                                         uint16_t *version)
1045 {
1046         sdp_data_t *pdl, *p0, *p1;
1047
1048         if (!psm && !version)
1049                 return -1;
1050
1051         pdl = sdp_data_get(rec, SDP_ATTR_PROTO_DESC_LIST);
1052         if (!pdl || !SDP_IS_SEQ(pdl->dtd))
1053                 return -1;
1054
1055         p0 = pdl->val.dataseq;
1056         if (get_prot_desc_entry(p0, L2CAP_UUID, psm) < 0)
1057                 return -1;
1058
1059         p1 = p0->next;
1060         if (get_prot_desc_entry(p1, MCAP_CTRL_UUID, version) < 0)
1061                 return -1;
1062
1063         return 0;
1064 }
1065
1066 static int get_ccpsm(sdp_list_t *recs, uint16_t *ccpsm)
1067 {
1068         sdp_list_t *l;
1069
1070         for (l = recs; l; l = l->next) {
1071                 sdp_record_t *rec = l->data;
1072
1073                 if (!get_prot_desc_list(rec, ccpsm, NULL))
1074                         return 0;
1075         }
1076
1077         return -1;
1078 }
1079
1080 static int get_add_prot_desc_list(const sdp_record_t *rec, uint16_t *psm)
1081 {
1082         sdp_data_t *pdl, *p0, *p1;
1083
1084         if (!psm)
1085                 return -1;
1086
1087         pdl = sdp_data_get(rec, SDP_ATTR_ADD_PROTO_DESC_LIST);
1088         if (!pdl || pdl->dtd != SDP_SEQ8)
1089                 return -1;
1090
1091         pdl = pdl->val.dataseq;
1092         if (pdl->dtd != SDP_SEQ8)
1093                 return -1;
1094
1095         p0 = pdl->val.dataseq;
1096
1097         if (get_prot_desc_entry(p0, L2CAP_UUID, psm) < 0)
1098                 return -1;
1099
1100         p1 = p0->next;
1101         if (get_prot_desc_entry(p1, MCAP_DATA_UUID, NULL) < 0)
1102                 return -1;
1103
1104         return 0;
1105 }
1106
1107 static int get_dcpsm(sdp_list_t *recs, uint16_t *dcpsm)
1108 {
1109         sdp_list_t *l;
1110
1111         for (l = recs; l; l = l->next) {
1112                 sdp_record_t *rec = l->data;
1113
1114                 if (!get_add_prot_desc_list(rec, dcpsm))
1115                         return 0;
1116         }
1117
1118         return -1;
1119 }
1120
1121 static int send_echo_data(int sock, const void *buf, uint32_t size)
1122 {
1123         const uint8_t *buf_b = buf;
1124         uint32_t sent = 0;
1125
1126         while (sent < size) {
1127                 int n = write(sock, buf_b + sent, size - sent);
1128                 if (n < 0)
1129                         return -1;
1130                 sent += n;
1131         }
1132
1133         return 0;
1134 }
1135
1136 static gboolean serve_echo(GIOChannel *io, GIOCondition cond, gpointer data)
1137 {
1138         struct health_channel *channel = data;
1139         uint8_t buf[MCAP_DC_MTU];
1140         int fd, len, ret;
1141
1142         DBG("channel %p", channel);
1143
1144         if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
1145                 DBG("Error condition on channel");
1146                 return FALSE;
1147         }
1148
1149         fd = g_io_channel_unix_get_fd(io);
1150
1151         len = read(fd, buf, sizeof(buf));
1152         if (len < 0) {
1153                 DBG("Error reading ECHO");
1154                 return FALSE;
1155         }
1156
1157         ret = send_echo_data(fd, buf, len);
1158         if (ret != len)
1159                 DBG("Error sending ECHO back");
1160
1161         return FALSE;
1162 }
1163
1164 static void mcap_mdl_connected_cb(struct mcap_mdl *mdl, void *data)
1165 {
1166         struct health_channel *channel = data;
1167         int fd;
1168
1169         DBG("Data channel connected: mdl %p channel %p", mdl, channel);
1170
1171         if (!channel) {
1172                 channel = search_channel_by_mdl(mdl);
1173                 if (!channel) {
1174                         error("health: channel data does not exist");
1175                         return;
1176                 }
1177         }
1178
1179         if (!channel->mdl)
1180                 channel->mdl = mcap_mdl_ref(mdl);
1181
1182         fd = mcap_mdl_get_fd(channel->mdl);
1183         if (fd < 0) {
1184                 error("health: error retrieving fd");
1185                 goto fail;
1186         }
1187
1188         if (channel->mdep_id == MDEP_ECHO) {
1189                 GIOChannel *io;
1190
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);
1195
1196                 return;
1197         }
1198
1199         info("health: MDL connected");
1200         send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_CONNECTED, fd);
1201
1202         return;
1203 fail:
1204         /* TODO: mcap_mdl_abort */
1205         destroy_channel(channel);
1206 }
1207
1208 static void mcap_mdl_closed_cb(struct mcap_mdl *mdl, void *data)
1209 {
1210         struct health_channel *channel = data;
1211
1212         info("health: MDL closed");
1213
1214         if (!channel)
1215                 return;
1216
1217         channel->mdl_conn = false;
1218 }
1219
1220 static void mcap_mdl_deleted_cb(struct mcap_mdl *mdl, void *data)
1221 {
1222         struct health_channel *channel;
1223
1224         info("health: MDL deleted");
1225
1226         channel = search_channel_by_mdl(mdl);
1227         if (!channel)
1228                 return;
1229
1230         DBG("channel %p mdl %p", channel, mdl);
1231         destroy_channel(channel);
1232 }
1233
1234 static void mcap_mdl_aborted_cb(struct mcap_mdl *mdl, void *data)
1235 {
1236         DBG("Not Implemeneted");
1237 }
1238
1239 static struct health_device *create_device(struct health_app *app,
1240                                                         const uint8_t *addr)
1241 {
1242         struct health_device *dev;
1243
1244         /* create device and push it to devices queue */
1245         dev = new0(struct health_device, 1);
1246
1247         android2bdaddr(addr, &dev->dst);
1248         dev->channels = queue_new();
1249         dev->app_id = app->id;
1250
1251         queue_push_tail(app->devices, dev);
1252
1253         return dev;
1254 }
1255
1256 static struct health_device *get_device(struct health_app *app,
1257                                                         const uint8_t *addr)
1258 {
1259         struct health_device *dev;
1260         bdaddr_t bdaddr;
1261
1262         android2bdaddr(addr, &bdaddr);
1263         dev = queue_find(app->devices, match_dev_by_addr, &bdaddr);
1264         if (dev)
1265                 return dev;
1266
1267         return create_device(app, addr);
1268 }
1269
1270 static struct health_channel *create_channel(struct health_app *app,
1271                                                 uint8_t mdep_index,
1272                                                 struct health_device *dev)
1273 {
1274         struct mdep_cfg *mdep;
1275         struct health_channel *channel;
1276         static unsigned int channel_id = 1;
1277
1278         DBG("mdep %u", mdep_index);
1279
1280         if (!dev || !app)
1281                 return NULL;
1282
1283         mdep = queue_find(app->mdeps, match_mdep_by_id, INT_TO_PTR(mdep_index));
1284         if (!mdep) {
1285                 if (mdep_index == MDEP_ECHO) {
1286                         mdep = new0(struct mdep_cfg, 1);
1287
1288                         /* Leave other configuration zeroes */
1289                         mdep->id = MDEP_ECHO;
1290
1291                         queue_push_tail(app->mdeps, mdep);
1292                 } else {
1293                         return NULL;
1294                 }
1295         }
1296
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++;
1302         channel->dev = dev;
1303
1304         queue_push_tail(dev->channels, channel);
1305
1306         return channel;
1307 }
1308
1309 static struct health_channel *connect_channel(struct health_app *app,
1310                                                         struct mcap_mcl *mcl,
1311                                                         uint8_t mdepid)
1312 {
1313         struct health_device *device;
1314         bdaddr_t addr;
1315
1316         DBG("app %p mdepid %u", app, mdepid);
1317
1318         mcap_mcl_get_addr(mcl, &addr);
1319
1320         if (!app) {
1321                 DBG("No app found for mdepid %u", mdepid);
1322                 return NULL;
1323         }
1324
1325         device = get_device(app, (uint8_t *) &addr);
1326
1327         return create_channel(app, mdepid, device);
1328 }
1329
1330 static uint8_t conf_to_l2cap(uint8_t conf)
1331 {
1332         return conf == CHANNEL_TYPE_STREAM ? L2CAP_MODE_STREAMING :
1333                                                                 L2CAP_MODE_ERTM;
1334 }
1335
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)
1338 {
1339         GError *gerr = NULL;
1340         struct health_channel *channel;
1341         struct health_app *app;
1342         struct mdep_cfg *mdep;
1343
1344         DBG("Data channel request: mdepid %u mdlid %u conf %u",
1345                                                         mdepid, mdlid, *conf);
1346
1347         if (mdepid == MDEP_ECHO)
1348                 /* For echo service take last app */
1349                 app = queue_peek_tail(apps);
1350         else
1351                 app = search_app_by_mdepid(mdepid);
1352
1353         if (!app)
1354                 return MCAP_MDL_BUSY;
1355
1356         channel = connect_channel(app, mcl, mdepid);
1357         if (!channel)
1358                 return MCAP_MDL_BUSY;
1359
1360         /* Channel is assigned here after creation */
1361         mcl->cb->user_data = channel;
1362
1363         if (mdepid == MDEP_ECHO) {
1364                 switch (*conf) {
1365                 case CHANNEL_TYPE_ANY:
1366                         *conf = CHANNEL_TYPE_RELIABLE;
1367                         break;
1368                 case CHANNEL_TYPE_RELIABLE:
1369                         break;
1370                 case CHANNEL_TYPE_STREAM:
1371                         return MCAP_CONFIGURATION_REJECTED;
1372                 default:
1373                         /*
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
1377                          * callback.
1378                          */
1379                         /* TODO close device */
1380                         return MCAP_CONFIGURATION_REJECTED; /* not processed */
1381                 }
1382
1383                 if (!mcap_set_data_chan_mode(mcap, L2CAP_MODE_ERTM, &gerr)) {
1384                         error("Error: %s", gerr->message);
1385                         g_error_free(gerr);
1386                         return MCAP_MDL_BUSY;
1387                 }
1388
1389                 /* TODO: Create channel */
1390
1391                 return MCAP_SUCCESS;
1392         }
1393
1394         mdep = queue_find(app->mdeps, match_mdep_by_id, INT_TO_PTR(mdepid));
1395         if (!mdep)
1396                 return MCAP_MDL_BUSY;
1397
1398         switch (*conf) {
1399         case CHANNEL_TYPE_ANY:
1400                 if (mdep->role == HAL_HEALTH_MDEP_ROLE_SINK) {
1401                         return MCAP_CONFIGURATION_REJECTED;
1402                 } else {
1403                         if (queue_length(channel->dev->channels) <= 1)
1404                                 *conf = CHANNEL_TYPE_RELIABLE;
1405                         else
1406                                 *conf = CHANNEL_TYPE_STREAM;
1407                 }
1408                 break;
1409         case CHANNEL_TYPE_STREAM:
1410                 if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
1411                         return MCAP_CONFIGURATION_REJECTED;
1412                 break;
1413         case CHANNEL_TYPE_RELIABLE:
1414                 if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
1415                         return MCAP_CONFIGURATION_REJECTED;
1416                 break;
1417         default:
1418                 /*
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.
1422                  */
1423                 /* TODO: close device */
1424                 return MCAP_CONFIGURATION_REJECTED; /* not processed */
1425         }
1426
1427         if (!mcap_set_data_chan_mode(mcap, conf_to_l2cap(*conf), &gerr)) {
1428                 error("health: error setting L2CAP mode: %s", gerr->message);
1429                 g_error_free(gerr);
1430                 return MCAP_MDL_BUSY;
1431         }
1432
1433         return MCAP_SUCCESS;
1434 }
1435
1436 static uint8_t mcap_mdl_reconn_req_cb(struct mcap_mdl *mdl, void *data)
1437 {
1438         struct health_channel *channel;
1439         GError *err = NULL;
1440
1441         DBG("");
1442
1443         channel = search_channel_by_mdl(mdl);
1444         if (!channel) {
1445                 error("health: channel data does not exist");
1446                 return MCAP_UNSPECIFIED_ERROR;
1447         }
1448
1449         if (!mcap_set_data_chan_mode(mcap,
1450                         conf_to_l2cap(channel->type), &err)) {
1451                 error("health: %s", err->message);
1452                 g_error_free(err);
1453                 return MCAP_MDL_BUSY;
1454         }
1455
1456         return MCAP_SUCCESS;
1457 }
1458
1459 static void connect_mdl_cb(struct mcap_mdl *mdl, GError *gerr, gpointer data)
1460 {
1461         struct health_channel *channel = data;
1462         int fd;
1463
1464         DBG("");
1465
1466         if (gerr) {
1467                 error("health: error connecting to MDL %s", gerr->message);
1468                 goto fail;
1469         }
1470
1471         fd = mcap_mdl_get_fd(channel->mdl);
1472         if (fd < 0) {
1473                 error("health: error retrieving fd");
1474                 goto fail;
1475         }
1476
1477         info("health: MDL connected");
1478         channel->mdl_conn = true;
1479
1480         /* first data channel should be reliable data channel */
1481         if (!queue_length(channel->dev->channels))
1482                 if (channel->type != CHANNEL_TYPE_RELIABLE)
1483                         goto fail;
1484
1485         send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_CONNECTED, fd);
1486
1487         return;
1488
1489 fail:
1490         /* TODO: mcap_mdl_abort */
1491         destroy_channel(channel);
1492 }
1493
1494 static void reconnect_mdl_cb(struct mcap_mdl *mdl, GError *gerr, gpointer data)
1495 {
1496         struct health_channel *channel = data;
1497         uint8_t mode;
1498         GError *err = NULL;
1499
1500         DBG("");
1501
1502         if (gerr) {
1503                 error("health: error reconnecting to MDL %s", gerr->message);
1504                 goto fail;
1505         }
1506
1507         channel->mdl_id = mcap_mdl_get_mdlid(mdl);
1508
1509         if (channel->type == CHANNEL_TYPE_RELIABLE)
1510                 mode = L2CAP_MODE_ERTM;
1511         else
1512                 mode = L2CAP_MODE_STREAMING;
1513
1514         if (!mcap_connect_mdl(channel->mdl, mode, channel->dev->dcpsm,
1515                                                 connect_mdl_cb, channel,
1516                                                 NULL, &err)) {
1517                 error("health: error connecting to mdl");
1518                 g_error_free(err);
1519                 goto fail;
1520         }
1521
1522         return;
1523
1524 fail:
1525         /* TODO: mcap_mdl_abort */
1526         destroy_channel(channel);
1527 }
1528
1529 static int reconnect_mdl(struct health_channel *channel)
1530 {
1531         GError *gerr = NULL;
1532
1533         DBG("");
1534
1535         if (!channel)
1536                 return -1;
1537
1538         if (!mcap_reconnect_mdl(channel->mdl, reconnect_mdl_cb, channel,
1539                                                                 NULL, &gerr)){
1540                 error("health: reconnect failed %s", gerr->message);
1541                 destroy_channel(channel);
1542         }
1543
1544         return 0;
1545 }
1546
1547 static void create_mdl_cb(struct mcap_mdl *mdl, uint8_t type, GError *gerr,
1548                                                                 gpointer data)
1549 {
1550         struct health_channel *channel = data;
1551         uint8_t mode;
1552         GError *err = NULL;
1553
1554         DBG("");
1555         if (gerr) {
1556                 error("health: error creating MDL %s", gerr->message);
1557                 goto fail;
1558         }
1559
1560         if (channel->type == CHANNEL_TYPE_ANY && type != CHANNEL_TYPE_ANY)
1561                 channel->type = type;
1562
1563         /*
1564          * if requested channel type is not same as preferred
1565          * channel type from remote device, then abort the connection.
1566          */
1567         if (channel->type != type) {
1568                 /* TODO: abort mdl */
1569                 error("health: channel type requested %d preferred %d not same",
1570                                                         channel->type, type);
1571                 goto fail;
1572         }
1573
1574         if (!channel->mdl)
1575                 channel->mdl = mcap_mdl_ref(mdl);
1576
1577         channel->type = type;
1578         channel->mdl_id = mcap_mdl_get_mdlid(mdl);
1579
1580         if (channel->type == CHANNEL_TYPE_RELIABLE)
1581                 mode = L2CAP_MODE_ERTM;
1582         else
1583                 mode = L2CAP_MODE_STREAMING;
1584
1585         if (!mcap_connect_mdl(channel->mdl, mode, channel->dev->dcpsm,
1586                                                 connect_mdl_cb, channel,
1587                                                 NULL, &err)) {
1588                 error("health: error connecting to mdl");
1589                 g_error_free(err);
1590                 goto fail;
1591         }
1592
1593         return;
1594
1595 fail:
1596         destroy_channel(channel);
1597 }
1598
1599 static bool check_role(uint8_t rec_role, uint8_t app_role)
1600 {
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))
1605                 return true;
1606
1607         return false;
1608 }
1609
1610 static bool get_mdep_from_rec(const sdp_record_t *rec, uint8_t role,
1611                                                 uint16_t d_type, uint8_t *mdep)
1612 {
1613         sdp_data_t *list, *feat;
1614
1615         if (!mdep)
1616                 return false;
1617
1618         list = sdp_data_get(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST);
1619         if (!list || !SDP_IS_SEQ(list->dtd))
1620                 return false;
1621
1622         for (feat = list->val.dataseq; feat; feat = feat->next) {
1623                 sdp_data_t *data_type, *mdepid, *role_t;
1624
1625                 if (!SDP_IS_SEQ(feat->dtd))
1626                         continue;
1627
1628                 mdepid = feat->val.dataseq;
1629                 if (!mdepid)
1630                         continue;
1631
1632                 data_type = mdepid->next;
1633                 if (!data_type)
1634                         continue;
1635
1636                 role_t = data_type->next;
1637                 if (!role_t)
1638                         continue;
1639
1640                 if (data_type->dtd != SDP_UINT16 || mdepid->dtd != SDP_UINT8 ||
1641                                                 role_t->dtd != SDP_UINT8)
1642                         continue;
1643
1644                 if (data_type->val.uint16 != d_type ||
1645                                         !check_role(role_t->val.uint8, role))
1646                         continue;
1647
1648                 *mdep = mdepid->val.uint8;
1649
1650                 return true;
1651         }
1652
1653         return false;
1654 }
1655
1656 static bool get_remote_mdep(sdp_list_t *recs, struct health_channel *channel)
1657 {
1658         struct health_app *app;
1659         struct mdep_cfg *mdep;
1660         uint8_t mdep_id;
1661
1662         app = queue_find(apps, match_app_by_id,
1663                                         INT_TO_PTR(channel->dev->app_id));
1664         if (!app)
1665                 return false;
1666
1667         mdep = queue_find(app->mdeps, match_mdep_by_id,
1668                                                 INT_TO_PTR(channel->mdep_id));
1669         if (!mdep)
1670                 return false;
1671
1672         if (!get_mdep_from_rec(recs->data, mdep->role, mdep->data_type,
1673                                                                 &mdep_id)) {
1674                 error("health: no matching MDEP: %u", channel->mdep_id);
1675                 return false;
1676         }
1677
1678         channel->remote_mdep = mdep_id;
1679         return true;
1680 }
1681
1682 static bool create_mdl(struct health_channel *channel)
1683 {
1684         struct health_app *app;
1685         struct mdep_cfg *mdep;
1686         uint8_t type;
1687         GError *gerr = NULL;
1688
1689         app = queue_find(apps, match_app_by_id,
1690                                         INT_TO_PTR(channel->dev->app_id));
1691         if (!app)
1692                 return false;
1693
1694         mdep = queue_find(app->mdeps, match_mdep_by_id,
1695                                                 INT_TO_PTR(channel->mdep_id));
1696         if (!mdep)
1697                 return false;
1698
1699         if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
1700                 type = channel->type;
1701         else
1702                 type = CHANNEL_TYPE_ANY;
1703
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);
1707                 g_error_free(gerr);
1708                 return false;
1709         }
1710
1711         return true;
1712 }
1713
1714 static bool set_mcl_cb(struct mcap_mcl *mcl, gpointer user_data, GError **err)
1715 {
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);
1724 }
1725
1726 static void create_mcl_cb(struct mcap_mcl *mcl, GError *err, gpointer data)
1727 {
1728         struct health_channel *channel = data;
1729         gboolean ret;
1730         GError *gerr = NULL;
1731
1732         DBG("");
1733
1734         if (err) {
1735                 error("health: error creating MCL : %s", err->message);
1736                 goto fail;
1737         }
1738
1739         if (!channel->dev->mcl)
1740                 channel->dev->mcl = mcap_mcl_ref(mcl);
1741
1742         info("health: MCL connected");
1743
1744         ret = set_mcl_cb(channel->dev->mcl, channel, &gerr);
1745         if (!ret) {
1746                 error("health: error setting mdl callbacks: %s", gerr->message);
1747                 g_error_free(gerr);
1748                 goto fail;
1749         }
1750
1751         if (!create_mdl(channel))
1752                 goto fail;
1753
1754         return;
1755
1756 fail:
1757         destroy_channel(channel);
1758 }
1759
1760 static void search_cb(sdp_list_t *recs, int err, gpointer data)
1761 {
1762         struct health_channel *channel = data;
1763         GError *gerr = NULL;
1764
1765         DBG("");
1766
1767         if (err < 0 || !recs) {
1768                 error("health: Error getting remote SDP records");
1769                 goto fail;
1770         }
1771
1772         if (get_ccpsm(recs, &channel->dev->ccpsm) < 0) {
1773                 error("health: Can't get remote PSM for control channel");
1774                 goto fail;
1775         }
1776
1777         if (get_dcpsm(recs, &channel->dev->dcpsm) < 0) {
1778                 error("health: Can't get remote PSM for data channel");
1779                 goto fail;
1780         }
1781
1782         if (!get_remote_mdep(recs, channel)) {
1783                 error("health: Can't get remote MDEP data");
1784                 goto fail;
1785         }
1786
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);
1790                 g_error_free(gerr);
1791                 goto fail;
1792         }
1793
1794         return;
1795
1796 fail:
1797         destroy_channel(channel);
1798 }
1799
1800 static int connect_mcl(struct health_channel *channel)
1801 {
1802         uuid_t uuid;
1803         int err;
1804
1805         DBG("");
1806
1807         bt_string2uuid(&uuid, HDP_UUID);
1808
1809         err = bt_search_service(&adapter_addr, &channel->dev->dst, &uuid,
1810                                                 search_cb, channel, NULL, 0);
1811         if (!err)
1812                 send_channel_state_notify(channel,
1813                                         HAL_HEALTH_CHANNEL_CONNECTING, -1);
1814
1815         return err;
1816 }
1817
1818 static struct health_app *get_app(uint16_t app_id)
1819 {
1820         return queue_find(apps, match_app_by_id, INT_TO_PTR(app_id));
1821 }
1822
1823 static struct health_channel *get_channel(struct health_app *app,
1824                                                 uint8_t mdep_index,
1825                                                 struct health_device *dev)
1826 {
1827         struct health_channel *channel;
1828         uint8_t index;
1829
1830         if (!dev)
1831                 return NULL;
1832
1833         index = mdep_index + 1;
1834         channel = queue_find(dev->channels, match_channel_by_mdep_id,
1835                                                         INT_TO_PTR(index));
1836         if (channel)
1837                 return channel;
1838
1839         return create_channel(app, index, dev);
1840 }
1841
1842 static void bt_health_connect_channel(const void *buf, uint16_t len)
1843 {
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;
1849
1850         DBG("");
1851
1852         app = get_app(cmd->app_id);
1853         if (!app)
1854                 goto send_rsp;
1855
1856         dev = get_device(app, cmd->bdaddr);
1857
1858         channel = get_channel(app, cmd->mdep_index, dev);
1859         if (!channel)
1860                 goto send_rsp;
1861
1862         if (!queue_length(dev->channels)) {
1863                 if (channel->type != CHANNEL_TYPE_RELIABLE) {
1864                         error("health: first data shannel should be reliable");
1865                         goto fail;
1866                 }
1867         }
1868
1869         if (!dev->mcl) {
1870                 if (connect_mcl(channel) < 0) {
1871                         error("health: error retrieving HDP SDP record");
1872                         goto fail;
1873                 }
1874         } else {
1875                 /* data channel is already connected */
1876                 if (channel->mdl && channel->mdl_conn)
1877                         goto fail;
1878
1879                 /* create mdl if it does not exists */
1880                 if (!channel->mdl && !create_mdl(channel))
1881                         goto fail;
1882
1883                 /* reconnect mdl if it exists */
1884                 if (channel->mdl && !channel->mdl_conn) {
1885                         if (reconnect_mdl(channel) < 0)
1886                                 goto fail;
1887                 }
1888
1889         }
1890
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);
1895         return;
1896
1897 fail:
1898         queue_remove(channel->dev->channels, channel);
1899         free_health_channel(channel);
1900
1901 send_rsp:
1902         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1903                         HAL_OP_HEALTH_CONNECT_CHANNEL, HAL_STATUS_FAILED);
1904 }
1905
1906 static void channel_delete_cb(GError *gerr, gpointer data)
1907 {
1908         struct health_channel *channel = data;
1909
1910         DBG("");
1911
1912         if (gerr) {
1913                 error("health: channel delete failed %s", gerr->message);
1914                 return;
1915         }
1916
1917         destroy_channel(channel);
1918 }
1919
1920 static void bt_health_destroy_channel(const void *buf, uint16_t len)
1921 {
1922         const struct hal_cmd_health_destroy_channel *cmd = buf;
1923         struct health_channel *channel;
1924         GError *gerr = NULL;
1925
1926         DBG("");
1927
1928         channel = search_channel_by_id(cmd->channel_id);
1929         if (!channel)
1930                 goto fail;
1931
1932         if (!mcap_delete_mdl(channel->mdl, channel_delete_cb, channel,
1933                                                         NULL, &gerr)) {
1934                 error("health: channel delete failed %s", gerr->message);
1935                 goto fail;
1936         }
1937
1938         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1939                         HAL_OP_HEALTH_DESTROY_CHANNEL, HAL_STATUS_SUCCESS);
1940
1941         return;
1942
1943 fail:
1944         ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
1945                         HAL_OP_HEALTH_DESTROY_CHANNEL, HAL_STATUS_INVALID);
1946 }
1947
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) },
1964 };
1965
1966 static void mcl_connected(struct mcap_mcl *mcl, gpointer data)
1967 {
1968         GError *gerr = NULL;
1969         bool ret;
1970
1971         DBG("");
1972
1973         info("health: MCL connected");
1974         ret = set_mcl_cb(mcl, NULL, &gerr);
1975         if (!ret) {
1976                 error("health: error setting mcl callbacks: %s", gerr->message);
1977                 g_error_free(gerr);
1978         }
1979 }
1980
1981 static void mcl_reconnected(struct mcap_mcl *mcl, gpointer data)
1982 {
1983         struct health_device *dev;
1984
1985         DBG("");
1986
1987         info("health: MCL reconnected");
1988         dev = search_dev_by_mcl(mcl);
1989         if (!dev) {
1990                 error("device data does not exists");
1991                 return;
1992         }
1993 }
1994
1995 static void mcl_disconnected(struct mcap_mcl *mcl, gpointer data)
1996 {
1997         struct health_device *dev;
1998
1999         DBG("");
2000
2001         info("health: MCL disconnected");
2002         dev = search_dev_by_mcl(mcl);
2003         unref_mcl(dev);
2004 }
2005
2006 static void mcl_uncached(struct mcap_mcl *mcl, gpointer data)
2007 {
2008         /* mcap library maintains cache of mcls, not required here */
2009 }
2010
2011 bool bt_health_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
2012 {
2013         GError *err = NULL;
2014
2015         DBG("");
2016
2017         bacpy(&adapter_addr, addr);
2018
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 */
2023                                         NULL, &err);
2024         if (!mcap) {
2025                 error("health: MCAP instance creation failed %s", err->message);
2026                 g_error_free(err);
2027                 return false;
2028         }
2029
2030         hal_ipc = ipc;
2031         apps = queue_new();
2032
2033         ipc_register(hal_ipc, HAL_SERVICE_ID_HEALTH, cmd_handlers,
2034                                                 G_N_ELEMENTS(cmd_handlers));
2035
2036         return true;
2037 }
2038
2039 void bt_health_unregister(void)
2040 {
2041         DBG("");
2042
2043         mcap_instance_unref(mcap);
2044         queue_destroy(apps, free_health_app);
2045         ipc_unregister(hal_ipc, HAL_SERVICE_ID_HEALTH);
2046         hal_ipc = NULL;
2047 }