health.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2014 Intel Corporation. All rights reserved.
  7. * Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <glib.h>
  20. #include "btio/btio.h"
  21. #include "lib/bluetooth.h"
  22. #include "lib/sdp.h"
  23. #include "lib/sdp_lib.h"
  24. #include "lib/uuid.h"
  25. #include "lib/l2cap.h"
  26. #include "src/log.h"
  27. #include "src/shared/util.h"
  28. #include "src/shared/queue.h"
  29. #include "src/uuid-helper.h"
  30. #include "src/sdp-client.h"
  31. #include "profiles/health/mcap.h"
  32. #include "hal-msg.h"
  33. #include "ipc-common.h"
  34. #include "ipc.h"
  35. #include "utils.h"
  36. #include "bluetooth.h"
  37. #include "health.h"
  38. #define SVC_HINT_HEALTH 0x00
  39. #define HDP_VERSION 0x0101
  40. #define DATA_EXCHANGE_SPEC_11073 0x01
  41. #define CHANNEL_TYPE_ANY 0x00
  42. #define CHANNEL_TYPE_RELIABLE 0x01
  43. #define CHANNEL_TYPE_STREAM 0x02
  44. #define MDEP_ECHO 0x00
  45. #define MDEP_INITIAL 0x01
  46. #define MDEP_FINAL 0x7F
  47. static bdaddr_t adapter_addr;
  48. static struct ipc *hal_ipc = NULL;
  49. static struct queue *apps = NULL;
  50. static struct mcap_instance *mcap = NULL;
  51. static uint32_t record_id = 0;
  52. static uint32_t record_state = 0;
  53. struct mdep_cfg {
  54. uint8_t role;
  55. uint16_t data_type;
  56. uint8_t channel_type;
  57. char *descr;
  58. uint8_t id; /* mdep id */
  59. };
  60. struct health_device {
  61. bdaddr_t dst;
  62. uint16_t app_id;
  63. struct mcap_mcl *mcl;
  64. struct queue *channels; /* data channels */
  65. uint16_t ccpsm;
  66. uint16_t dcpsm;
  67. };
  68. struct health_channel {
  69. uint8_t mdep_id;
  70. uint8_t type;
  71. struct health_device *dev;
  72. uint8_t remote_mdep;
  73. struct mcap_mdl *mdl;
  74. bool mdl_conn;
  75. uint16_t mdl_id; /* MDL ID */
  76. uint16_t id; /* channel id */
  77. };
  78. struct health_app {
  79. char *app_name;
  80. char *provider_name;
  81. char *service_name;
  82. char *service_descr;
  83. uint8_t num_of_mdep;
  84. struct queue *mdeps;
  85. uint16_t id; /* app id */
  86. struct queue *devices;
  87. };
  88. static void send_app_reg_notify(struct health_app *app, uint8_t state)
  89. {
  90. struct hal_ev_health_app_reg_state ev;
  91. DBG("");
  92. ev.id = app->id;
  93. ev.state = state;
  94. ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HEALTH,
  95. HAL_EV_HEALTH_APP_REG_STATE, sizeof(ev), &ev);
  96. }
  97. static void send_channel_state_notify(struct health_channel *channel,
  98. uint8_t state, int fd)
  99. {
  100. struct hal_ev_health_channel_state ev;
  101. DBG("");
  102. bdaddr2android(&channel->dev->dst, ev.bdaddr);
  103. ev.app_id = channel->dev->app_id;
  104. ev.mdep_index = channel->mdep_id - 1;
  105. ev.channel_id = channel->id;
  106. ev.channel_state = state;
  107. ipc_send_notif_with_fd(hal_ipc, HAL_SERVICE_ID_HEALTH,
  108. HAL_EV_HEALTH_CHANNEL_STATE,
  109. sizeof(ev), &ev, fd);
  110. }
  111. static void unref_mdl(struct health_channel *channel)
  112. {
  113. if (!channel || !channel->mdl)
  114. return;
  115. mcap_mdl_unref(channel->mdl);
  116. channel->mdl = NULL;
  117. channel->mdl_conn = false;
  118. }
  119. static void free_health_channel(void *data)
  120. {
  121. struct health_channel *channel = data;
  122. int fd;
  123. DBG("channel %p", channel);
  124. if (!channel)
  125. return;
  126. fd = mcap_mdl_get_fd(channel->mdl);
  127. if (fd >= 0)
  128. shutdown(fd, SHUT_RDWR);
  129. unref_mdl(channel);
  130. free(channel);
  131. }
  132. static void destroy_channel(void *data)
  133. {
  134. struct health_channel *channel = data;
  135. if (!channel)
  136. return;
  137. send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_DESTROYED, -1);
  138. queue_remove(channel->dev->channels, channel);
  139. free_health_channel(channel);
  140. }
  141. static void unref_mcl(struct health_device *dev)
  142. {
  143. if (!dev || !dev->mcl)
  144. return;
  145. mcap_close_mcl(dev->mcl, FALSE);
  146. mcap_mcl_unref(dev->mcl);
  147. dev->mcl = NULL;
  148. }
  149. static void free_health_device(void *data)
  150. {
  151. struct health_device *dev = data;
  152. if (!dev)
  153. return;
  154. unref_mcl(dev);
  155. queue_destroy(dev->channels, free_health_channel);
  156. free(dev);
  157. }
  158. static void free_mdep_cfg(void *data)
  159. {
  160. struct mdep_cfg *cfg = data;
  161. if (!cfg)
  162. return;
  163. free(cfg->descr);
  164. free(cfg);
  165. }
  166. static void free_health_app(void *data)
  167. {
  168. struct health_app *app = data;
  169. if (!app)
  170. return;
  171. free(app->app_name);
  172. free(app->provider_name);
  173. free(app->service_name);
  174. free(app->service_descr);
  175. queue_destroy(app->mdeps, free_mdep_cfg);
  176. queue_destroy(app->devices, free_health_device);
  177. free(app);
  178. }
  179. static bool match_channel_by_mdl(const void *data, const void *user_data)
  180. {
  181. const struct health_channel *channel = data;
  182. const struct mcap_mdl *mdl = user_data;
  183. return channel->mdl == mdl;
  184. }
  185. static bool match_channel_by_id(const void *data, const void *user_data)
  186. {
  187. const struct health_channel *channel = data;
  188. uint16_t channel_id = PTR_TO_INT(user_data);
  189. return channel->id == channel_id;
  190. }
  191. static bool match_dev_by_mcl(const void *data, const void *user_data)
  192. {
  193. const struct health_device *dev = data;
  194. const struct mcap_mcl *mcl = user_data;
  195. return dev->mcl == mcl;
  196. }
  197. static bool match_dev_by_addr(const void *data, const void *user_data)
  198. {
  199. const struct health_device *dev = data;
  200. const bdaddr_t *addr = user_data;
  201. return !bacmp(&dev->dst, addr);
  202. }
  203. static bool match_channel_by_mdep_id(const void *data, const void *user_data)
  204. {
  205. const struct health_channel *channel = data;
  206. uint16_t mdep_id = PTR_TO_INT(user_data);
  207. return channel->mdep_id == mdep_id;
  208. }
  209. static bool match_mdep_by_role(const void *data, const void *user_data)
  210. {
  211. const struct mdep_cfg *mdep = data;
  212. uint16_t role = PTR_TO_INT(user_data);
  213. return mdep->role == role;
  214. }
  215. static bool match_mdep_by_id(const void *data, const void *user_data)
  216. {
  217. const struct mdep_cfg *mdep = data;
  218. uint16_t mdep_id = PTR_TO_INT(user_data);
  219. return mdep->id == mdep_id;
  220. }
  221. static bool match_app_by_id(const void *data, const void *user_data)
  222. {
  223. const struct health_app *app = data;
  224. uint16_t app_id = PTR_TO_INT(user_data);
  225. return app->id == app_id;
  226. }
  227. static struct health_channel *search_channel_by_id(uint16_t id)
  228. {
  229. const struct queue_entry *apps_entry, *devices_entry;
  230. struct health_app *app;
  231. struct health_channel *channel;
  232. struct health_device *dev;
  233. DBG("");
  234. apps_entry = queue_get_entries(apps);
  235. while (apps_entry) {
  236. app = apps_entry->data;
  237. devices_entry = queue_get_entries(app->devices);
  238. while (devices_entry) {
  239. dev = devices_entry->data;
  240. channel = queue_find(dev->channels, match_channel_by_id,
  241. INT_TO_PTR(id));
  242. if (channel)
  243. return channel;
  244. devices_entry = devices_entry->next;
  245. }
  246. apps_entry = apps_entry->next;
  247. }
  248. return NULL;
  249. }
  250. static struct health_channel *search_channel_by_mdl(struct mcap_mdl *mdl)
  251. {
  252. const struct queue_entry *apps_entry, *devices_entry;
  253. struct health_app *app;
  254. struct health_channel *channel;
  255. struct health_device *dev;
  256. DBG("");
  257. apps_entry = queue_get_entries(apps);
  258. while (apps_entry) {
  259. app = apps_entry->data;
  260. devices_entry = queue_get_entries(app->devices);
  261. while (devices_entry) {
  262. dev = devices_entry->data;
  263. channel = queue_find(dev->channels,
  264. match_channel_by_mdl, mdl);
  265. if (channel)
  266. return channel;
  267. devices_entry = devices_entry->next;
  268. }
  269. apps_entry = apps_entry->next;
  270. }
  271. return NULL;
  272. }
  273. static struct health_device *search_dev_by_mcl(struct mcap_mcl *mcl)
  274. {
  275. const struct queue_entry *apps_entry;
  276. struct health_app *app;
  277. struct health_device *dev;
  278. DBG("");
  279. apps_entry = queue_get_entries(apps);
  280. while (apps_entry) {
  281. app = apps_entry->data;
  282. dev = queue_find(app->devices, match_dev_by_mcl, mcl);
  283. if (dev)
  284. return dev;
  285. apps_entry = apps_entry->next;
  286. }
  287. return NULL;
  288. }
  289. static struct health_app *search_app_by_mdepid(uint8_t mdepid)
  290. {
  291. const struct queue_entry *apps_entry;
  292. struct health_app *app;
  293. DBG("");
  294. apps_entry = queue_get_entries(apps);
  295. while (apps_entry) {
  296. app = apps_entry->data;
  297. if (queue_find(app->mdeps, match_mdep_by_id,
  298. INT_TO_PTR(mdepid)))
  299. return app;
  300. apps_entry = apps_entry->next;
  301. }
  302. return NULL;
  303. }
  304. static int register_service_protocols(sdp_record_t *rec,
  305. struct health_app *app)
  306. {
  307. uuid_t l2cap_uuid, mcap_c_uuid;
  308. sdp_list_t *l2cap_list, *proto_list = NULL, *mcap_list = NULL;
  309. sdp_list_t *access_proto_list = NULL;
  310. sdp_data_t *psm = NULL, *mcap_ver = NULL;
  311. uint32_t ccpsm;
  312. uint16_t version = MCAP_VERSION;
  313. GError *err = NULL;
  314. int ret = -1;
  315. DBG("");
  316. /* set l2cap information */
  317. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  318. l2cap_list = sdp_list_append(NULL, &l2cap_uuid);
  319. if (!l2cap_list)
  320. goto fail;
  321. ccpsm = mcap_get_ctrl_psm(mcap, &err);
  322. if (err)
  323. goto fail;
  324. psm = sdp_data_alloc(SDP_UINT16, &ccpsm);
  325. if (!psm)
  326. goto fail;
  327. if (!sdp_list_append(l2cap_list, psm))
  328. goto fail;
  329. proto_list = sdp_list_append(NULL, l2cap_list);
  330. if (!proto_list)
  331. goto fail;
  332. /* set mcap information */
  333. sdp_uuid16_create(&mcap_c_uuid, MCAP_CTRL_UUID);
  334. mcap_list = sdp_list_append(NULL, &mcap_c_uuid);
  335. if (!mcap_list)
  336. goto fail;
  337. mcap_ver = sdp_data_alloc(SDP_UINT16, &version);
  338. if (!mcap_ver)
  339. goto fail;
  340. if (!sdp_list_append(mcap_list, mcap_ver))
  341. goto fail;
  342. if (!sdp_list_append(proto_list, mcap_list))
  343. goto fail;
  344. /* attach protocol information to service record */
  345. access_proto_list = sdp_list_append(NULL, proto_list);
  346. if (!access_proto_list)
  347. goto fail;
  348. sdp_set_access_protos(rec, access_proto_list);
  349. ret = 0;
  350. fail:
  351. sdp_list_free(l2cap_list, NULL);
  352. sdp_list_free(mcap_list, NULL);
  353. sdp_list_free(proto_list, NULL);
  354. sdp_list_free(access_proto_list, NULL);
  355. if (psm)
  356. sdp_data_free(psm);
  357. if (mcap_ver)
  358. sdp_data_free(mcap_ver);
  359. if (err)
  360. g_error_free(err);
  361. return ret;
  362. }
  363. static int register_service_profiles(sdp_record_t *rec)
  364. {
  365. int ret;
  366. sdp_list_t *profile_list;
  367. sdp_profile_desc_t hdp_profile;
  368. DBG("");
  369. /* set hdp information */
  370. sdp_uuid16_create(&hdp_profile.uuid, HDP_SVCLASS_ID);
  371. hdp_profile.version = HDP_VERSION;
  372. profile_list = sdp_list_append(NULL, &hdp_profile);
  373. if (!profile_list)
  374. return -1;
  375. /* set profile descriptor list */
  376. ret = sdp_set_profile_descs(rec, profile_list);
  377. sdp_list_free(profile_list, NULL);
  378. return ret;
  379. }
  380. static int register_service_additional_protocols(sdp_record_t *rec,
  381. struct health_app *app)
  382. {
  383. int ret = -1;
  384. uuid_t l2cap_uuid, mcap_d_uuid;
  385. sdp_list_t *l2cap_list, *proto_list = NULL, *mcap_list = NULL;
  386. sdp_list_t *access_proto_list = NULL;
  387. sdp_data_t *psm = NULL;
  388. uint32_t dcpsm;
  389. GError *err = NULL;
  390. DBG("");
  391. /* set l2cap information */
  392. sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
  393. l2cap_list = sdp_list_append(NULL, &l2cap_uuid);
  394. if (!l2cap_list)
  395. goto fail;
  396. dcpsm = mcap_get_data_psm(mcap, &err);
  397. if (err)
  398. goto fail;
  399. psm = sdp_data_alloc(SDP_UINT16, &dcpsm);
  400. if (!psm)
  401. goto fail;
  402. if (!sdp_list_append(l2cap_list, psm))
  403. goto fail;
  404. proto_list = sdp_list_append(NULL, l2cap_list);
  405. if (!proto_list)
  406. goto fail;
  407. /* set mcap information */
  408. sdp_uuid16_create(&mcap_d_uuid, MCAP_DATA_UUID);
  409. mcap_list = sdp_list_append(NULL, &mcap_d_uuid);
  410. if (!mcap_list)
  411. goto fail;
  412. if (!sdp_list_append(proto_list, mcap_list))
  413. goto fail;
  414. /* attach protocol information to service record */
  415. access_proto_list = sdp_list_append(NULL, proto_list);
  416. if (!access_proto_list)
  417. goto fail;
  418. sdp_set_add_access_protos(rec, access_proto_list);
  419. ret = 0;
  420. fail:
  421. sdp_list_free(l2cap_list, NULL);
  422. sdp_list_free(mcap_list, NULL);
  423. sdp_list_free(proto_list, NULL);
  424. sdp_list_free(access_proto_list, NULL);
  425. if (psm)
  426. sdp_data_free(psm);
  427. if (err)
  428. g_error_free(err);
  429. return ret;
  430. }
  431. static sdp_list_t *mdeps_to_sdp_features(struct mdep_cfg *mdep)
  432. {
  433. sdp_data_t *mdepid, *dtype = NULL, *role = NULL, *descr = NULL;
  434. sdp_list_t *f_list = NULL;
  435. DBG("");
  436. mdepid = sdp_data_alloc(SDP_UINT8, &mdep->id);
  437. if (!mdepid)
  438. return NULL;
  439. dtype = sdp_data_alloc(SDP_UINT16, &mdep->data_type);
  440. if (!dtype)
  441. goto fail;
  442. role = sdp_data_alloc(SDP_UINT8, &mdep->role);
  443. if (!role)
  444. goto fail;
  445. if (mdep->descr) {
  446. descr = sdp_data_alloc(SDP_TEXT_STR8, mdep->descr);
  447. if (!descr)
  448. goto fail;
  449. }
  450. f_list = sdp_list_append(NULL, mdepid);
  451. if (!f_list)
  452. goto fail;
  453. if (!sdp_list_append(f_list, dtype))
  454. goto fail;
  455. if (!sdp_list_append(f_list, role))
  456. goto fail;
  457. if (descr && !sdp_list_append(f_list, descr))
  458. goto fail;
  459. return f_list;
  460. fail:
  461. sdp_list_free(f_list, NULL);
  462. if (mdepid)
  463. sdp_data_free(mdepid);
  464. if (dtype)
  465. sdp_data_free(dtype);
  466. if (role)
  467. sdp_data_free(role);
  468. if (descr)
  469. sdp_data_free(descr);
  470. return NULL;
  471. }
  472. static void free_hdp_list(void *list)
  473. {
  474. sdp_list_t *hdp_list = list;
  475. sdp_list_free(hdp_list, (sdp_free_func_t)sdp_data_free);
  476. }
  477. static void register_features(void *data, void *user_data)
  478. {
  479. struct mdep_cfg *mdep = data;
  480. sdp_list_t **sup_features = user_data;
  481. sdp_list_t *hdp_feature;
  482. DBG("");
  483. hdp_feature = mdeps_to_sdp_features(mdep);
  484. if (!hdp_feature)
  485. return;
  486. if (!*sup_features) {
  487. *sup_features = sdp_list_append(NULL, hdp_feature);
  488. if (!*sup_features)
  489. sdp_list_free(hdp_feature,
  490. (sdp_free_func_t)sdp_data_free);
  491. } else if (!sdp_list_append(*sup_features, hdp_feature)) {
  492. sdp_list_free(hdp_feature,
  493. (sdp_free_func_t)sdp_data_free);
  494. }
  495. }
  496. static int register_service_sup_features(sdp_record_t *rec,
  497. struct health_app *app)
  498. {
  499. sdp_list_t *sup_features = NULL;
  500. DBG("");
  501. queue_foreach(app->mdeps, register_features, &sup_features);
  502. if (!sup_features)
  503. return -1;
  504. if (sdp_set_supp_feat(rec, sup_features) < 0) {
  505. sdp_list_free(sup_features, free_hdp_list);
  506. return -1;
  507. }
  508. sdp_list_free(sup_features, free_hdp_list);
  509. return 0;
  510. }
  511. static int register_data_exchange_spec(sdp_record_t *rec)
  512. {
  513. sdp_data_t *spec;
  514. uint8_t data_spec = DATA_EXCHANGE_SPEC_11073;
  515. /* As of now only 11073 is supported, so we set it as default */
  516. DBG("");
  517. spec = sdp_data_alloc(SDP_UINT8, &data_spec);
  518. if (!spec)
  519. return -1;
  520. if (sdp_attr_add(rec, SDP_ATTR_DATA_EXCHANGE_SPEC, spec) < 0) {
  521. sdp_data_free(spec);
  522. return -1;
  523. }
  524. return 0;
  525. }
  526. static int register_mcap_features(sdp_record_t *rec)
  527. {
  528. sdp_data_t *mcap_proc;
  529. uint8_t mcap_sup_proc = MCAP_SUP_PROC;
  530. DBG("");
  531. mcap_proc = sdp_data_alloc(SDP_UINT8, &mcap_sup_proc);
  532. if (!mcap_proc)
  533. return -1;
  534. if (sdp_attr_add(rec, SDP_ATTR_MCAP_SUPPORTED_PROCEDURES,
  535. mcap_proc) < 0) {
  536. sdp_data_free(mcap_proc);
  537. return -1;
  538. }
  539. return 0;
  540. }
  541. static int set_sdp_services_uuid(sdp_record_t *rec, uint8_t role)
  542. {
  543. uuid_t source, sink;
  544. sdp_list_t *list = NULL;
  545. sdp_uuid16_create(&sink, HDP_SINK_SVCLASS_ID);
  546. sdp_uuid16_create(&source, HDP_SOURCE_SVCLASS_ID);
  547. sdp_get_service_classes(rec, &list);
  548. switch (role) {
  549. case HAL_HEALTH_MDEP_ROLE_SOURCE:
  550. if (!sdp_list_find(list, &source, sdp_uuid_cmp))
  551. list = sdp_list_append(list, &source);
  552. break;
  553. case HAL_HEALTH_MDEP_ROLE_SINK:
  554. if (!sdp_list_find(list, &sink, sdp_uuid_cmp))
  555. list = sdp_list_append(list, &sink);
  556. break;
  557. }
  558. if (sdp_set_service_classes(rec, list) < 0) {
  559. sdp_list_free(list, NULL);
  560. return -1;
  561. }
  562. sdp_list_free(list, NULL);
  563. return 0;
  564. }
  565. static int update_sdp_record(struct health_app *app)
  566. {
  567. sdp_record_t *rec;
  568. uint8_t role;
  569. DBG("");
  570. if (record_id > 0) {
  571. bt_adapter_remove_record(record_id);
  572. record_id = 0;
  573. }
  574. rec = sdp_record_alloc();
  575. if (!rec)
  576. return -1;
  577. role = HAL_HEALTH_MDEP_ROLE_SOURCE;
  578. if (queue_find(app->mdeps, match_mdep_by_role, INT_TO_PTR(role)))
  579. set_sdp_services_uuid(rec, role);
  580. role = HAL_HEALTH_MDEP_ROLE_SINK;
  581. if (queue_find(app->mdeps, match_mdep_by_role, INT_TO_PTR(role)))
  582. set_sdp_services_uuid(rec, role);
  583. sdp_set_info_attr(rec, app->service_name, app->provider_name,
  584. app->service_descr);
  585. if (register_service_protocols(rec, app) < 0)
  586. goto fail;
  587. if (register_service_profiles(rec) < 0)
  588. goto fail;
  589. if (register_service_additional_protocols(rec, app) < 0)
  590. goto fail;
  591. if (register_service_sup_features(rec, app) < 0)
  592. goto fail;
  593. if (register_data_exchange_spec(rec) < 0)
  594. goto fail;
  595. if (register_mcap_features(rec) < 0)
  596. goto fail;
  597. if (sdp_set_record_state(rec, record_state++) < 0)
  598. goto fail;
  599. if (bt_adapter_add_record(rec, SVC_HINT_HEALTH) < 0) {
  600. error("health: Failed to register HEALTH record");
  601. goto fail;
  602. }
  603. record_id = rec->handle;
  604. return 0;
  605. fail:
  606. sdp_record_free(rec);
  607. return -1;
  608. }
  609. static struct health_app *create_health_app(const char *app_name,
  610. const char *provider, const char *srv_name,
  611. const char *srv_descr, uint8_t mdeps)
  612. {
  613. struct health_app *app;
  614. static unsigned int app_id = 1;
  615. DBG("");
  616. app = new0(struct health_app, 1);
  617. app->id = app_id++;
  618. app->num_of_mdep = mdeps;
  619. app->app_name = strdup(app_name);
  620. if (provider) {
  621. app->provider_name = strdup(provider);
  622. if (!app->provider_name)
  623. goto fail;
  624. }
  625. if (srv_name) {
  626. app->service_name = strdup(srv_name);
  627. if (!app->service_name)
  628. goto fail;
  629. }
  630. if (srv_descr) {
  631. app->service_descr = strdup(srv_descr);
  632. if (!app->service_descr)
  633. goto fail;
  634. }
  635. app->mdeps = queue_new();
  636. app->devices = queue_new();
  637. return app;
  638. fail:
  639. free_health_app(app);
  640. return NULL;
  641. }
  642. static void bt_health_register_app(const void *buf, uint16_t len)
  643. {
  644. const struct hal_cmd_health_reg_app *cmd = buf;
  645. struct hal_rsp_health_reg_app rsp;
  646. struct health_app *app;
  647. uint16_t off;
  648. uint16_t app_name_len, provider_len, srv_name_len, srv_descr_len;
  649. char *app_name, *provider = NULL, *srv_name = NULL, *srv_descr = NULL;
  650. DBG("");
  651. if (len != sizeof(*cmd) + cmd->len ||
  652. cmd->app_name_off > cmd->provider_name_off ||
  653. cmd->provider_name_off > cmd->service_name_off ||
  654. cmd->service_name_off > cmd->service_descr_off ||
  655. cmd->service_descr_off > cmd->len) {
  656. error("health: Invalid register app command, terminating");
  657. raise(SIGTERM);
  658. return;
  659. }
  660. app_name = (char *) cmd->data;
  661. app_name_len = cmd->provider_name_off - cmd->app_name_off;
  662. off = app_name_len;
  663. provider_len = cmd->service_name_off - off;
  664. if (provider_len > 0)
  665. provider = (char *) cmd->data + off;
  666. off += provider_len;
  667. srv_name_len = cmd->service_descr_off - off;
  668. if (srv_name_len > 0)
  669. srv_name = (char *) cmd->data + off;
  670. off += srv_name_len;
  671. srv_descr_len = cmd->len - off;
  672. if (srv_descr_len > 0)
  673. srv_descr = (char *) cmd->data + off;
  674. app = create_health_app(app_name, provider, srv_name, srv_descr,
  675. cmd->num_of_mdep);
  676. if (!app)
  677. goto fail;
  678. queue_push_tail(apps, app);
  679. rsp.app_id = app->id;
  680. ipc_send_rsp_full(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_REG_APP,
  681. sizeof(rsp), &rsp, -1);
  682. return;
  683. fail:
  684. free_health_app(app);
  685. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
  686. HAL_STATUS_FAILED);
  687. }
  688. static uint8_t android2channel_type(uint8_t type)
  689. {
  690. switch (type) {
  691. case HAL_HEALTH_CHANNEL_TYPE_RELIABLE:
  692. return CHANNEL_TYPE_RELIABLE;
  693. case HAL_HEALTH_CHANNEL_TYPE_STREAMING:
  694. return CHANNEL_TYPE_STREAM;
  695. default:
  696. return CHANNEL_TYPE_ANY;
  697. }
  698. }
  699. static void bt_health_mdep_cfg_data(const void *buf, uint16_t len)
  700. {
  701. const struct hal_cmd_health_mdep *cmd = buf;
  702. struct health_app *app;
  703. struct mdep_cfg *mdep = NULL;
  704. uint8_t status;
  705. DBG("");
  706. app = queue_find(apps, match_app_by_id, INT_TO_PTR(cmd->app_id));
  707. if (!app) {
  708. status = HAL_STATUS_INVALID;
  709. goto fail;
  710. }
  711. mdep = new0(struct mdep_cfg, 1);
  712. mdep->role = cmd->role;
  713. mdep->data_type = cmd->data_type;
  714. mdep->channel_type = android2channel_type(cmd->channel_type);
  715. mdep->id = queue_length(app->mdeps) + 1;
  716. if (cmd->descr_len > 0) {
  717. mdep->descr = malloc0(cmd->descr_len);
  718. memcpy(mdep->descr, cmd->descr, cmd->descr_len);
  719. }
  720. queue_push_tail(app->mdeps, mdep);
  721. if (app->num_of_mdep != queue_length(app->mdeps))
  722. goto send_rsp;
  723. /* add sdp record from app configuration data */
  724. /*
  725. * TODO: Check what to be done if mupltple applications are trying to
  726. * register with different role and different configurations.
  727. * 1) Does device supports SOURCE and SINK at the same time ?
  728. * 2) Does it require different SDP records or one record with
  729. * multile MDEP configurations ?
  730. */
  731. if (update_sdp_record(app) < 0) {
  732. error("health: HDP SDP record preparation failed");
  733. status = HAL_STATUS_FAILED;
  734. goto fail;
  735. }
  736. send_app_reg_notify(app, HAL_HEALTH_APP_REG_SUCCESS);
  737. send_rsp:
  738. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
  739. HAL_STATUS_SUCCESS);
  740. return;
  741. fail:
  742. if (status != HAL_STATUS_SUCCESS) {
  743. free_mdep_cfg(mdep);
  744. queue_remove(apps, app);
  745. free_health_app(app);
  746. }
  747. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH, HAL_OP_HEALTH_MDEP,
  748. status);
  749. }
  750. static void bt_health_unregister_app(const void *buf, uint16_t len)
  751. {
  752. const struct hal_cmd_health_unreg_app *cmd = buf;
  753. struct health_app *app;
  754. DBG("");
  755. app = queue_remove_if(apps, match_app_by_id, INT_TO_PTR(cmd->app_id));
  756. if (!app) {
  757. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
  758. HAL_OP_HEALTH_UNREG_APP, HAL_STATUS_INVALID);
  759. return;
  760. }
  761. send_app_reg_notify(app, HAL_HEALTH_APP_DEREG_SUCCESS);
  762. if (record_id > 0) {
  763. bt_adapter_remove_record(record_id);
  764. record_id = 0;
  765. }
  766. free_health_app(app);
  767. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
  768. HAL_OP_HEALTH_UNREG_APP, HAL_STATUS_SUCCESS);
  769. }
  770. static int get_prot_desc_entry(sdp_data_t *entry, int type, guint16 *val)
  771. {
  772. sdp_data_t *iter;
  773. int proto;
  774. if (!entry || !SDP_IS_SEQ(entry->dtd))
  775. return -1;
  776. iter = entry->val.dataseq;
  777. if (!(iter->dtd & SDP_UUID_UNSPEC))
  778. return -1;
  779. proto = sdp_uuid_to_proto(&iter->val.uuid);
  780. if (proto != type)
  781. return -1;
  782. if (!val)
  783. return 0;
  784. iter = iter->next;
  785. if (iter->dtd != SDP_UINT16)
  786. return -1;
  787. *val = iter->val.uint16;
  788. return 0;
  789. }
  790. static int get_prot_desc_list(const sdp_record_t *rec, uint16_t *psm,
  791. uint16_t *version)
  792. {
  793. sdp_data_t *pdl, *p0, *p1;
  794. if (!psm && !version)
  795. return -1;
  796. pdl = sdp_data_get(rec, SDP_ATTR_PROTO_DESC_LIST);
  797. if (!pdl || !SDP_IS_SEQ(pdl->dtd))
  798. return -1;
  799. p0 = pdl->val.dataseq;
  800. if (get_prot_desc_entry(p0, L2CAP_UUID, psm) < 0)
  801. return -1;
  802. p1 = p0->next;
  803. if (get_prot_desc_entry(p1, MCAP_CTRL_UUID, version) < 0)
  804. return -1;
  805. return 0;
  806. }
  807. static int get_ccpsm(sdp_list_t *recs, uint16_t *ccpsm)
  808. {
  809. sdp_list_t *l;
  810. for (l = recs; l; l = l->next) {
  811. sdp_record_t *rec = l->data;
  812. if (!get_prot_desc_list(rec, ccpsm, NULL))
  813. return 0;
  814. }
  815. return -1;
  816. }
  817. static int get_add_prot_desc_list(const sdp_record_t *rec, uint16_t *psm)
  818. {
  819. sdp_data_t *pdl, *p0, *p1;
  820. if (!psm)
  821. return -1;
  822. pdl = sdp_data_get(rec, SDP_ATTR_ADD_PROTO_DESC_LIST);
  823. if (!pdl || pdl->dtd != SDP_SEQ8)
  824. return -1;
  825. pdl = pdl->val.dataseq;
  826. if (pdl->dtd != SDP_SEQ8)
  827. return -1;
  828. p0 = pdl->val.dataseq;
  829. if (get_prot_desc_entry(p0, L2CAP_UUID, psm) < 0)
  830. return -1;
  831. p1 = p0->next;
  832. if (get_prot_desc_entry(p1, MCAP_DATA_UUID, NULL) < 0)
  833. return -1;
  834. return 0;
  835. }
  836. static int get_dcpsm(sdp_list_t *recs, uint16_t *dcpsm)
  837. {
  838. sdp_list_t *l;
  839. for (l = recs; l; l = l->next) {
  840. sdp_record_t *rec = l->data;
  841. if (!get_add_prot_desc_list(rec, dcpsm))
  842. return 0;
  843. }
  844. return -1;
  845. }
  846. static int send_echo_data(int sock, const void *buf, uint32_t size)
  847. {
  848. const uint8_t *buf_b = buf;
  849. uint32_t sent = 0;
  850. while (sent < size) {
  851. int n = write(sock, buf_b + sent, size - sent);
  852. if (n < 0)
  853. return -1;
  854. sent += n;
  855. }
  856. return 0;
  857. }
  858. static gboolean serve_echo(GIOChannel *io, GIOCondition cond, gpointer data)
  859. {
  860. struct health_channel *channel = data;
  861. uint8_t buf[MCAP_DC_MTU];
  862. int fd, len, ret;
  863. DBG("channel %p", channel);
  864. if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
  865. DBG("Error condition on channel");
  866. return FALSE;
  867. }
  868. fd = g_io_channel_unix_get_fd(io);
  869. len = read(fd, buf, sizeof(buf));
  870. if (len < 0) {
  871. DBG("Error reading ECHO");
  872. return FALSE;
  873. }
  874. ret = send_echo_data(fd, buf, len);
  875. if (ret != len)
  876. DBG("Error sending ECHO back");
  877. return FALSE;
  878. }
  879. static void mcap_mdl_connected_cb(struct mcap_mdl *mdl, void *data)
  880. {
  881. struct health_channel *channel = data;
  882. int fd;
  883. DBG("Data channel connected: mdl %p channel %p", mdl, channel);
  884. if (!channel) {
  885. channel = search_channel_by_mdl(mdl);
  886. if (!channel) {
  887. error("health: channel data does not exist");
  888. return;
  889. }
  890. }
  891. if (!channel->mdl)
  892. channel->mdl = mcap_mdl_ref(mdl);
  893. fd = mcap_mdl_get_fd(channel->mdl);
  894. if (fd < 0) {
  895. error("health: error retrieving fd");
  896. goto fail;
  897. }
  898. if (channel->mdep_id == MDEP_ECHO) {
  899. GIOChannel *io;
  900. io = g_io_channel_unix_new(fd);
  901. g_io_add_watch(io, G_IO_ERR | G_IO_HUP | G_IO_NVAL | G_IO_IN,
  902. serve_echo, channel);
  903. g_io_channel_unref(io);
  904. return;
  905. }
  906. info("health: MDL connected");
  907. send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_CONNECTED, fd);
  908. return;
  909. fail:
  910. /* TODO: mcap_mdl_abort */
  911. destroy_channel(channel);
  912. }
  913. static void mcap_mdl_closed_cb(struct mcap_mdl *mdl, void *data)
  914. {
  915. struct health_channel *channel = data;
  916. info("health: MDL closed");
  917. if (!channel)
  918. return;
  919. channel->mdl_conn = false;
  920. }
  921. static void mcap_mdl_deleted_cb(struct mcap_mdl *mdl, void *data)
  922. {
  923. struct health_channel *channel;
  924. info("health: MDL deleted");
  925. channel = search_channel_by_mdl(mdl);
  926. if (!channel)
  927. return;
  928. DBG("channel %p mdl %p", channel, mdl);
  929. destroy_channel(channel);
  930. }
  931. static void mcap_mdl_aborted_cb(struct mcap_mdl *mdl, void *data)
  932. {
  933. DBG("Not Implemeneted");
  934. }
  935. static struct health_device *create_device(struct health_app *app,
  936. const uint8_t *addr)
  937. {
  938. struct health_device *dev;
  939. /* create device and push it to devices queue */
  940. dev = new0(struct health_device, 1);
  941. android2bdaddr(addr, &dev->dst);
  942. dev->channels = queue_new();
  943. dev->app_id = app->id;
  944. queue_push_tail(app->devices, dev);
  945. return dev;
  946. }
  947. static struct health_device *get_device(struct health_app *app,
  948. const uint8_t *addr)
  949. {
  950. struct health_device *dev;
  951. bdaddr_t bdaddr;
  952. android2bdaddr(addr, &bdaddr);
  953. dev = queue_find(app->devices, match_dev_by_addr, &bdaddr);
  954. if (dev)
  955. return dev;
  956. return create_device(app, addr);
  957. }
  958. static struct health_channel *create_channel(struct health_app *app,
  959. uint8_t mdep_index,
  960. struct health_device *dev)
  961. {
  962. struct mdep_cfg *mdep;
  963. struct health_channel *channel;
  964. static unsigned int channel_id = 1;
  965. DBG("mdep %u", mdep_index);
  966. if (!dev || !app)
  967. return NULL;
  968. mdep = queue_find(app->mdeps, match_mdep_by_id, INT_TO_PTR(mdep_index));
  969. if (!mdep) {
  970. if (mdep_index == MDEP_ECHO) {
  971. mdep = new0(struct mdep_cfg, 1);
  972. /* Leave other configuration zeroes */
  973. mdep->id = MDEP_ECHO;
  974. queue_push_tail(app->mdeps, mdep);
  975. } else {
  976. return NULL;
  977. }
  978. }
  979. /* create channel and push it to device */
  980. channel = new0(struct health_channel, 1);
  981. channel->mdep_id = mdep->id;
  982. channel->type = mdep->channel_type;
  983. channel->id = channel_id++;
  984. channel->dev = dev;
  985. queue_push_tail(dev->channels, channel);
  986. return channel;
  987. }
  988. static struct health_channel *connect_channel(struct health_app *app,
  989. struct mcap_mcl *mcl,
  990. uint8_t mdepid)
  991. {
  992. struct health_device *device;
  993. bdaddr_t addr;
  994. DBG("app %p mdepid %u", app, mdepid);
  995. mcap_mcl_get_addr(mcl, &addr);
  996. if (!app) {
  997. DBG("No app found for mdepid %u", mdepid);
  998. return NULL;
  999. }
  1000. device = get_device(app, (uint8_t *) &addr);
  1001. return create_channel(app, mdepid, device);
  1002. }
  1003. static uint8_t conf_to_l2cap(uint8_t conf)
  1004. {
  1005. return conf == CHANNEL_TYPE_STREAM ? L2CAP_MODE_STREAMING :
  1006. L2CAP_MODE_ERTM;
  1007. }
  1008. static uint8_t mcap_mdl_conn_req_cb(struct mcap_mcl *mcl, uint8_t mdepid,
  1009. uint16_t mdlid, uint8_t *conf, void *data)
  1010. {
  1011. GError *gerr = NULL;
  1012. struct health_channel *channel;
  1013. struct health_app *app;
  1014. struct mdep_cfg *mdep;
  1015. DBG("Data channel request: mdepid %u mdlid %u conf %u",
  1016. mdepid, mdlid, *conf);
  1017. if (mdepid == MDEP_ECHO)
  1018. /* For echo service take last app */
  1019. app = queue_peek_tail(apps);
  1020. else
  1021. app = search_app_by_mdepid(mdepid);
  1022. if (!app)
  1023. return MCAP_MDL_BUSY;
  1024. channel = connect_channel(app, mcl, mdepid);
  1025. if (!channel)
  1026. return MCAP_MDL_BUSY;
  1027. /* Channel is assigned here after creation */
  1028. mcl->cb->user_data = channel;
  1029. if (mdepid == MDEP_ECHO) {
  1030. switch (*conf) {
  1031. case CHANNEL_TYPE_ANY:
  1032. *conf = CHANNEL_TYPE_RELIABLE;
  1033. break;
  1034. case CHANNEL_TYPE_RELIABLE:
  1035. break;
  1036. case CHANNEL_TYPE_STREAM:
  1037. return MCAP_CONFIGURATION_REJECTED;
  1038. default:
  1039. /*
  1040. * Special case defined in HDP spec 3.4.
  1041. * When an invalid configuration is received we shall
  1042. * close the MCL when we are still processing the
  1043. * callback.
  1044. */
  1045. /* TODO close device */
  1046. return MCAP_CONFIGURATION_REJECTED; /* not processed */
  1047. }
  1048. if (!mcap_set_data_chan_mode(mcap, L2CAP_MODE_ERTM, &gerr)) {
  1049. error("Error: %s", gerr->message);
  1050. g_error_free(gerr);
  1051. return MCAP_MDL_BUSY;
  1052. }
  1053. /* TODO: Create channel */
  1054. return MCAP_SUCCESS;
  1055. }
  1056. mdep = queue_find(app->mdeps, match_mdep_by_id, INT_TO_PTR(mdepid));
  1057. if (!mdep)
  1058. return MCAP_MDL_BUSY;
  1059. switch (*conf) {
  1060. case CHANNEL_TYPE_ANY:
  1061. if (mdep->role == HAL_HEALTH_MDEP_ROLE_SINK) {
  1062. return MCAP_CONFIGURATION_REJECTED;
  1063. } else {
  1064. if (queue_length(channel->dev->channels) <= 1)
  1065. *conf = CHANNEL_TYPE_RELIABLE;
  1066. else
  1067. *conf = CHANNEL_TYPE_STREAM;
  1068. }
  1069. break;
  1070. case CHANNEL_TYPE_STREAM:
  1071. if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
  1072. return MCAP_CONFIGURATION_REJECTED;
  1073. break;
  1074. case CHANNEL_TYPE_RELIABLE:
  1075. if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
  1076. return MCAP_CONFIGURATION_REJECTED;
  1077. break;
  1078. default:
  1079. /*
  1080. * Special case defined in HDP spec 3.4. When an invalid
  1081. * configuration is received we shall close the MCL when
  1082. * we are still processing the callback.
  1083. */
  1084. /* TODO: close device */
  1085. return MCAP_CONFIGURATION_REJECTED; /* not processed */
  1086. }
  1087. if (!mcap_set_data_chan_mode(mcap, conf_to_l2cap(*conf), &gerr)) {
  1088. error("health: error setting L2CAP mode: %s", gerr->message);
  1089. g_error_free(gerr);
  1090. return MCAP_MDL_BUSY;
  1091. }
  1092. return MCAP_SUCCESS;
  1093. }
  1094. static uint8_t mcap_mdl_reconn_req_cb(struct mcap_mdl *mdl, void *data)
  1095. {
  1096. struct health_channel *channel;
  1097. GError *err = NULL;
  1098. DBG("");
  1099. channel = search_channel_by_mdl(mdl);
  1100. if (!channel) {
  1101. error("health: channel data does not exist");
  1102. return MCAP_UNSPECIFIED_ERROR;
  1103. }
  1104. if (!mcap_set_data_chan_mode(mcap,
  1105. conf_to_l2cap(channel->type), &err)) {
  1106. error("health: %s", err->message);
  1107. g_error_free(err);
  1108. return MCAP_MDL_BUSY;
  1109. }
  1110. return MCAP_SUCCESS;
  1111. }
  1112. static void connect_mdl_cb(struct mcap_mdl *mdl, GError *gerr, gpointer data)
  1113. {
  1114. struct health_channel *channel = data;
  1115. int fd;
  1116. DBG("");
  1117. if (gerr) {
  1118. error("health: error connecting to MDL %s", gerr->message);
  1119. goto fail;
  1120. }
  1121. fd = mcap_mdl_get_fd(channel->mdl);
  1122. if (fd < 0) {
  1123. error("health: error retrieving fd");
  1124. goto fail;
  1125. }
  1126. info("health: MDL connected");
  1127. channel->mdl_conn = true;
  1128. /* first data channel should be reliable data channel */
  1129. if (!queue_length(channel->dev->channels))
  1130. if (channel->type != CHANNEL_TYPE_RELIABLE)
  1131. goto fail;
  1132. send_channel_state_notify(channel, HAL_HEALTH_CHANNEL_CONNECTED, fd);
  1133. return;
  1134. fail:
  1135. /* TODO: mcap_mdl_abort */
  1136. destroy_channel(channel);
  1137. }
  1138. static void reconnect_mdl_cb(struct mcap_mdl *mdl, GError *gerr, gpointer data)
  1139. {
  1140. struct health_channel *channel = data;
  1141. uint8_t mode;
  1142. GError *err = NULL;
  1143. DBG("");
  1144. if (gerr) {
  1145. error("health: error reconnecting to MDL %s", gerr->message);
  1146. goto fail;
  1147. }
  1148. channel->mdl_id = mcap_mdl_get_mdlid(mdl);
  1149. if (channel->type == CHANNEL_TYPE_RELIABLE)
  1150. mode = L2CAP_MODE_ERTM;
  1151. else
  1152. mode = L2CAP_MODE_STREAMING;
  1153. if (!mcap_connect_mdl(channel->mdl, mode, channel->dev->dcpsm,
  1154. connect_mdl_cb, channel,
  1155. NULL, &err)) {
  1156. error("health: error connecting to mdl");
  1157. g_error_free(err);
  1158. goto fail;
  1159. }
  1160. return;
  1161. fail:
  1162. /* TODO: mcap_mdl_abort */
  1163. destroy_channel(channel);
  1164. }
  1165. static int reconnect_mdl(struct health_channel *channel)
  1166. {
  1167. GError *gerr = NULL;
  1168. DBG("");
  1169. if (!channel)
  1170. return -1;
  1171. if (!mcap_reconnect_mdl(channel->mdl, reconnect_mdl_cb, channel,
  1172. NULL, &gerr)){
  1173. error("health: reconnect failed %s", gerr->message);
  1174. destroy_channel(channel);
  1175. }
  1176. return 0;
  1177. }
  1178. static void create_mdl_cb(struct mcap_mdl *mdl, uint8_t type, GError *gerr,
  1179. gpointer data)
  1180. {
  1181. struct health_channel *channel = data;
  1182. uint8_t mode;
  1183. GError *err = NULL;
  1184. DBG("");
  1185. if (gerr) {
  1186. error("health: error creating MDL %s", gerr->message);
  1187. goto fail;
  1188. }
  1189. if (channel->type == CHANNEL_TYPE_ANY && type != CHANNEL_TYPE_ANY)
  1190. channel->type = type;
  1191. /*
  1192. * if requested channel type is not same as preferred
  1193. * channel type from remote device, then abort the connection.
  1194. */
  1195. if (channel->type != type) {
  1196. /* TODO: abort mdl */
  1197. error("health: channel type requested %d preferred %d not same",
  1198. channel->type, type);
  1199. goto fail;
  1200. }
  1201. if (!channel->mdl)
  1202. channel->mdl = mcap_mdl_ref(mdl);
  1203. channel->type = type;
  1204. channel->mdl_id = mcap_mdl_get_mdlid(mdl);
  1205. if (channel->type == CHANNEL_TYPE_RELIABLE)
  1206. mode = L2CAP_MODE_ERTM;
  1207. else
  1208. mode = L2CAP_MODE_STREAMING;
  1209. if (!mcap_connect_mdl(channel->mdl, mode, channel->dev->dcpsm,
  1210. connect_mdl_cb, channel,
  1211. NULL, &err)) {
  1212. error("health: error connecting to mdl");
  1213. g_error_free(err);
  1214. goto fail;
  1215. }
  1216. return;
  1217. fail:
  1218. destroy_channel(channel);
  1219. }
  1220. static bool check_role(uint8_t rec_role, uint8_t app_role)
  1221. {
  1222. if ((rec_role == HAL_HEALTH_MDEP_ROLE_SINK &&
  1223. app_role == HAL_HEALTH_MDEP_ROLE_SOURCE) ||
  1224. (rec_role == HAL_HEALTH_MDEP_ROLE_SOURCE &&
  1225. app_role == HAL_HEALTH_MDEP_ROLE_SINK))
  1226. return true;
  1227. return false;
  1228. }
  1229. static bool get_mdep_from_rec(const sdp_record_t *rec, uint8_t role,
  1230. uint16_t d_type, uint8_t *mdep)
  1231. {
  1232. sdp_data_t *list, *feat;
  1233. if (!mdep)
  1234. return false;
  1235. list = sdp_data_get(rec, SDP_ATTR_SUPPORTED_FEATURES_LIST);
  1236. if (!list || !SDP_IS_SEQ(list->dtd))
  1237. return false;
  1238. for (feat = list->val.dataseq; feat; feat = feat->next) {
  1239. sdp_data_t *data_type, *mdepid, *role_t;
  1240. if (!SDP_IS_SEQ(feat->dtd))
  1241. continue;
  1242. mdepid = feat->val.dataseq;
  1243. if (!mdepid)
  1244. continue;
  1245. data_type = mdepid->next;
  1246. if (!data_type)
  1247. continue;
  1248. role_t = data_type->next;
  1249. if (!role_t)
  1250. continue;
  1251. if (data_type->dtd != SDP_UINT16 || mdepid->dtd != SDP_UINT8 ||
  1252. role_t->dtd != SDP_UINT8)
  1253. continue;
  1254. if (data_type->val.uint16 != d_type ||
  1255. !check_role(role_t->val.uint8, role))
  1256. continue;
  1257. *mdep = mdepid->val.uint8;
  1258. return true;
  1259. }
  1260. return false;
  1261. }
  1262. static bool get_remote_mdep(sdp_list_t *recs, struct health_channel *channel)
  1263. {
  1264. struct health_app *app;
  1265. struct mdep_cfg *mdep;
  1266. uint8_t mdep_id;
  1267. app = queue_find(apps, match_app_by_id,
  1268. INT_TO_PTR(channel->dev->app_id));
  1269. if (!app)
  1270. return false;
  1271. mdep = queue_find(app->mdeps, match_mdep_by_id,
  1272. INT_TO_PTR(channel->mdep_id));
  1273. if (!mdep)
  1274. return false;
  1275. if (!get_mdep_from_rec(recs->data, mdep->role, mdep->data_type,
  1276. &mdep_id)) {
  1277. error("health: no matching MDEP: %u", channel->mdep_id);
  1278. return false;
  1279. }
  1280. channel->remote_mdep = mdep_id;
  1281. return true;
  1282. }
  1283. static bool create_mdl(struct health_channel *channel)
  1284. {
  1285. struct health_app *app;
  1286. struct mdep_cfg *mdep;
  1287. uint8_t type;
  1288. GError *gerr = NULL;
  1289. app = queue_find(apps, match_app_by_id,
  1290. INT_TO_PTR(channel->dev->app_id));
  1291. if (!app)
  1292. return false;
  1293. mdep = queue_find(app->mdeps, match_mdep_by_id,
  1294. INT_TO_PTR(channel->mdep_id));
  1295. if (!mdep)
  1296. return false;
  1297. if (mdep->role == HAL_HEALTH_MDEP_ROLE_SOURCE)
  1298. type = channel->type;
  1299. else
  1300. type = CHANNEL_TYPE_ANY;
  1301. if (!mcap_create_mdl(channel->dev->mcl, channel->remote_mdep,
  1302. type, create_mdl_cb, channel, NULL, &gerr)) {
  1303. error("health: error creating mdl %s", gerr->message);
  1304. g_error_free(gerr);
  1305. return false;
  1306. }
  1307. return true;
  1308. }
  1309. static bool set_mcl_cb(struct mcap_mcl *mcl, gpointer user_data, GError **err)
  1310. {
  1311. return mcap_mcl_set_cb(mcl, user_data, err,
  1312. MCAP_MDL_CB_CONNECTED, mcap_mdl_connected_cb,
  1313. MCAP_MDL_CB_CLOSED, mcap_mdl_closed_cb,
  1314. MCAP_MDL_CB_DELETED, mcap_mdl_deleted_cb,
  1315. MCAP_MDL_CB_ABORTED, mcap_mdl_aborted_cb,
  1316. MCAP_MDL_CB_REMOTE_CONN_REQ, mcap_mdl_conn_req_cb,
  1317. MCAP_MDL_CB_REMOTE_RECONN_REQ, mcap_mdl_reconn_req_cb,
  1318. MCAP_MDL_CB_INVALID);
  1319. }
  1320. static void create_mcl_cb(struct mcap_mcl *mcl, GError *err, gpointer data)
  1321. {
  1322. struct health_channel *channel = data;
  1323. gboolean ret;
  1324. GError *gerr = NULL;
  1325. DBG("");
  1326. if (err) {
  1327. error("health: error creating MCL : %s", err->message);
  1328. goto fail;
  1329. }
  1330. if (!channel->dev->mcl)
  1331. channel->dev->mcl = mcap_mcl_ref(mcl);
  1332. info("health: MCL connected");
  1333. ret = set_mcl_cb(channel->dev->mcl, channel, &gerr);
  1334. if (!ret) {
  1335. error("health: error setting mdl callbacks: %s", gerr->message);
  1336. g_error_free(gerr);
  1337. goto fail;
  1338. }
  1339. if (!create_mdl(channel))
  1340. goto fail;
  1341. return;
  1342. fail:
  1343. destroy_channel(channel);
  1344. }
  1345. static void search_cb(sdp_list_t *recs, int err, gpointer data)
  1346. {
  1347. struct health_channel *channel = data;
  1348. GError *gerr = NULL;
  1349. DBG("");
  1350. if (err < 0 || !recs) {
  1351. error("health: Error getting remote SDP records");
  1352. goto fail;
  1353. }
  1354. if (get_ccpsm(recs, &channel->dev->ccpsm) < 0) {
  1355. error("health: Can't get remote PSM for control channel");
  1356. goto fail;
  1357. }
  1358. if (get_dcpsm(recs, &channel->dev->dcpsm) < 0) {
  1359. error("health: Can't get remote PSM for data channel");
  1360. goto fail;
  1361. }
  1362. if (!get_remote_mdep(recs, channel)) {
  1363. error("health: Can't get remote MDEP data");
  1364. goto fail;
  1365. }
  1366. if (!mcap_create_mcl(mcap, &channel->dev->dst, channel->dev->ccpsm,
  1367. create_mcl_cb, channel, NULL, &gerr)) {
  1368. error("health: error creating mcl %s", gerr->message);
  1369. g_error_free(gerr);
  1370. goto fail;
  1371. }
  1372. return;
  1373. fail:
  1374. destroy_channel(channel);
  1375. }
  1376. static int connect_mcl(struct health_channel *channel)
  1377. {
  1378. uuid_t uuid;
  1379. int err;
  1380. DBG("");
  1381. bt_string2uuid(&uuid, HDP_UUID);
  1382. err = bt_search_service(&adapter_addr, &channel->dev->dst, &uuid,
  1383. search_cb, channel, NULL, 0);
  1384. if (!err)
  1385. send_channel_state_notify(channel,
  1386. HAL_HEALTH_CHANNEL_CONNECTING, -1);
  1387. return err;
  1388. }
  1389. static struct health_app *get_app(uint16_t app_id)
  1390. {
  1391. return queue_find(apps, match_app_by_id, INT_TO_PTR(app_id));
  1392. }
  1393. static struct health_channel *get_channel(struct health_app *app,
  1394. uint8_t mdep_index,
  1395. struct health_device *dev)
  1396. {
  1397. struct health_channel *channel;
  1398. uint8_t index;
  1399. if (!dev)
  1400. return NULL;
  1401. index = mdep_index + 1;
  1402. channel = queue_find(dev->channels, match_channel_by_mdep_id,
  1403. INT_TO_PTR(index));
  1404. if (channel)
  1405. return channel;
  1406. return create_channel(app, index, dev);
  1407. }
  1408. static void bt_health_connect_channel(const void *buf, uint16_t len)
  1409. {
  1410. const struct hal_cmd_health_connect_channel *cmd = buf;
  1411. struct hal_rsp_health_connect_channel rsp;
  1412. struct health_device *dev = NULL;
  1413. struct health_channel *channel = NULL;
  1414. struct health_app *app;
  1415. DBG("");
  1416. app = get_app(cmd->app_id);
  1417. if (!app)
  1418. goto send_rsp;
  1419. dev = get_device(app, cmd->bdaddr);
  1420. channel = get_channel(app, cmd->mdep_index, dev);
  1421. if (!channel)
  1422. goto send_rsp;
  1423. if (!queue_length(dev->channels)) {
  1424. if (channel->type != CHANNEL_TYPE_RELIABLE) {
  1425. error("health: first data shannel should be reliable");
  1426. goto fail;
  1427. }
  1428. }
  1429. if (!dev->mcl) {
  1430. if (connect_mcl(channel) < 0) {
  1431. error("health: error retrieving HDP SDP record");
  1432. goto fail;
  1433. }
  1434. } else {
  1435. /* data channel is already connected */
  1436. if (channel->mdl && channel->mdl_conn)
  1437. goto fail;
  1438. /* create mdl if it does not exists */
  1439. if (!channel->mdl && !create_mdl(channel))
  1440. goto fail;
  1441. /* reconnect mdl if it exists */
  1442. if (channel->mdl && !channel->mdl_conn) {
  1443. if (reconnect_mdl(channel) < 0)
  1444. goto fail;
  1445. }
  1446. }
  1447. rsp.channel_id = channel->id;
  1448. ipc_send_rsp_full(hal_ipc, HAL_SERVICE_ID_HEALTH,
  1449. HAL_OP_HEALTH_CONNECT_CHANNEL,
  1450. sizeof(rsp), &rsp, -1);
  1451. return;
  1452. fail:
  1453. queue_remove(channel->dev->channels, channel);
  1454. free_health_channel(channel);
  1455. send_rsp:
  1456. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
  1457. HAL_OP_HEALTH_CONNECT_CHANNEL, HAL_STATUS_FAILED);
  1458. }
  1459. static void channel_delete_cb(GError *gerr, gpointer data)
  1460. {
  1461. struct health_channel *channel = data;
  1462. DBG("");
  1463. if (gerr) {
  1464. error("health: channel delete failed %s", gerr->message);
  1465. return;
  1466. }
  1467. destroy_channel(channel);
  1468. }
  1469. static void bt_health_destroy_channel(const void *buf, uint16_t len)
  1470. {
  1471. const struct hal_cmd_health_destroy_channel *cmd = buf;
  1472. struct health_channel *channel;
  1473. GError *gerr = NULL;
  1474. DBG("");
  1475. channel = search_channel_by_id(cmd->channel_id);
  1476. if (!channel)
  1477. goto fail;
  1478. if (!mcap_delete_mdl(channel->mdl, channel_delete_cb, channel,
  1479. NULL, &gerr)) {
  1480. error("health: channel delete failed %s", gerr->message);
  1481. goto fail;
  1482. }
  1483. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
  1484. HAL_OP_HEALTH_DESTROY_CHANNEL, HAL_STATUS_SUCCESS);
  1485. return;
  1486. fail:
  1487. ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HEALTH,
  1488. HAL_OP_HEALTH_DESTROY_CHANNEL, HAL_STATUS_INVALID);
  1489. }
  1490. static const struct ipc_handler cmd_handlers[] = {
  1491. /* HAL_OP_HEALTH_REG_APP */
  1492. { bt_health_register_app, true,
  1493. sizeof(struct hal_cmd_health_reg_app) },
  1494. /* HAL_OP_HEALTH_MDEP */
  1495. { bt_health_mdep_cfg_data, true,
  1496. sizeof(struct hal_cmd_health_mdep) },
  1497. /* HAL_OP_HEALTH_UNREG_APP */
  1498. { bt_health_unregister_app, false,
  1499. sizeof(struct hal_cmd_health_unreg_app) },
  1500. /* HAL_OP_HEALTH_CONNECT_CHANNEL */
  1501. { bt_health_connect_channel, false,
  1502. sizeof(struct hal_cmd_health_connect_channel) },
  1503. /* HAL_OP_HEALTH_DESTROY_CHANNEL */
  1504. { bt_health_destroy_channel, false,
  1505. sizeof(struct hal_cmd_health_destroy_channel) },
  1506. };
  1507. static void mcl_connected(struct mcap_mcl *mcl, gpointer data)
  1508. {
  1509. GError *gerr = NULL;
  1510. bool ret;
  1511. DBG("");
  1512. info("health: MCL connected");
  1513. ret = set_mcl_cb(mcl, NULL, &gerr);
  1514. if (!ret) {
  1515. error("health: error setting mcl callbacks: %s", gerr->message);
  1516. g_error_free(gerr);
  1517. }
  1518. }
  1519. static void mcl_reconnected(struct mcap_mcl *mcl, gpointer data)
  1520. {
  1521. struct health_device *dev;
  1522. DBG("");
  1523. info("health: MCL reconnected");
  1524. dev = search_dev_by_mcl(mcl);
  1525. if (!dev) {
  1526. error("device data does not exists");
  1527. return;
  1528. }
  1529. }
  1530. static void mcl_disconnected(struct mcap_mcl *mcl, gpointer data)
  1531. {
  1532. struct health_device *dev;
  1533. DBG("");
  1534. info("health: MCL disconnected");
  1535. dev = search_dev_by_mcl(mcl);
  1536. unref_mcl(dev);
  1537. }
  1538. static void mcl_uncached(struct mcap_mcl *mcl, gpointer data)
  1539. {
  1540. /* mcap library maintains cache of mcls, not required here */
  1541. }
  1542. bool bt_health_register(struct ipc *ipc, const bdaddr_t *addr, uint8_t mode)
  1543. {
  1544. GError *err = NULL;
  1545. DBG("");
  1546. bacpy(&adapter_addr, addr);
  1547. mcap = mcap_create_instance(&adapter_addr, BT_IO_SEC_MEDIUM, 0, 0,
  1548. mcl_connected, mcl_reconnected,
  1549. mcl_disconnected, mcl_uncached,
  1550. NULL, /* CSP is not used right now */
  1551. NULL, &err);
  1552. if (!mcap) {
  1553. error("health: MCAP instance creation failed %s", err->message);
  1554. g_error_free(err);
  1555. return false;
  1556. }
  1557. hal_ipc = ipc;
  1558. apps = queue_new();
  1559. ipc_register(hal_ipc, HAL_SERVICE_ID_HEALTH, cmd_handlers,
  1560. G_N_ELEMENTS(cmd_handlers));
  1561. return true;
  1562. }
  1563. void bt_health_unregister(void)
  1564. {
  1565. DBG("");
  1566. mcap_instance_unref(mcap);
  1567. queue_destroy(apps, free_health_app);
  1568. ipc_unregister(hal_ipc, HAL_SERVICE_ID_HEALTH);
  1569. hal_ipc = NULL;
  1570. }