bthost.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <endian.h>
  20. #include <stdbool.h>
  21. #include "lib/bluetooth.h"
  22. #include "src/shared/util.h"
  23. #include "src/shared/tester.h"
  24. #include "monitor/bt.h"
  25. #include "monitor/rfcomm.h"
  26. #include "bthost.h"
  27. #define lmp_bredr_capable(bthost) (!((bthost)->features[4] & 0x20))
  28. /* ACL handle and flags pack/unpack */
  29. #define acl_handle_pack(h, f) (uint16_t)((h & 0x0fff)|(f << 12))
  30. #define acl_handle(h) (h & 0x0fff)
  31. #define acl_flags(h) (h >> 12)
  32. #define L2CAP_FEAT_FIXED_CHAN 0x00000080
  33. #define L2CAP_FC_SIG_BREDR 0x02
  34. #define L2CAP_FC_SMP_BREDR 0x80
  35. #define L2CAP_IT_FEAT_MASK 0x0002
  36. #define L2CAP_IT_FIXED_CHAN 0x0003
  37. /* RFCOMM setters */
  38. #define RFCOMM_ADDR(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)
  39. #define RFCOMM_CTRL(type, pf) (((type & 0xef) | (pf << 4)))
  40. #define RFCOMM_LEN8(len) (((len) << 1) | 1)
  41. #define RFCOMM_LEN16(len) ((len) << 1)
  42. #define RFCOMM_MCC_TYPE(cr, type) (((type << 2) | (cr << 1) | 0x01))
  43. /* RFCOMM FCS calculation */
  44. #define CRC(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
  45. static unsigned char rfcomm_crc_table[256] = {
  46. 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
  47. 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
  48. 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
  49. 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
  50. 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
  51. 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
  52. 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
  53. 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
  54. 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
  55. 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
  56. 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
  57. 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
  58. 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
  59. 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
  60. 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
  61. 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
  62. 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
  63. 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
  64. 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
  65. 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
  66. 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
  67. 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
  68. 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
  69. 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
  70. 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
  71. 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
  72. 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
  73. 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
  74. 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
  75. 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
  76. 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
  77. 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
  78. };
  79. static uint8_t rfcomm_fcs2(uint8_t *data)
  80. {
  81. return 0xff - rfcomm_crc_table[CRC(data) ^ data[2]];
  82. }
  83. static uint8_t rfcomm_fcs(uint8_t *data)
  84. {
  85. return 0xff - CRC(data);
  86. }
  87. struct cmd {
  88. struct cmd *next;
  89. struct cmd *prev;
  90. uint8_t data[256 + sizeof(struct bt_hci_cmd_hdr)];
  91. uint16_t len;
  92. };
  93. struct cmd_queue {
  94. struct cmd *head;
  95. struct cmd *tail;
  96. };
  97. struct cid_hook {
  98. uint16_t cid;
  99. bthost_cid_hook_func_t func;
  100. void *user_data;
  101. struct cid_hook *next;
  102. };
  103. struct rfcomm_chan_hook {
  104. uint8_t channel;
  105. bthost_rfcomm_chan_hook_func_t func;
  106. void *user_data;
  107. struct rfcomm_chan_hook *next;
  108. };
  109. struct btconn {
  110. uint16_t handle;
  111. uint8_t bdaddr[6];
  112. uint8_t addr_type;
  113. uint8_t encr_mode;
  114. uint16_t next_cid;
  115. uint64_t fixed_chan;
  116. struct l2conn *l2conns;
  117. struct rcconn *rcconns;
  118. struct cid_hook *cid_hooks;
  119. struct rfcomm_chan_hook *rfcomm_chan_hooks;
  120. struct btconn *next;
  121. void *smp_data;
  122. };
  123. struct l2conn {
  124. uint16_t scid;
  125. uint16_t dcid;
  126. uint16_t psm;
  127. struct l2conn *next;
  128. };
  129. struct rcconn {
  130. uint8_t channel;
  131. uint16_t scid;
  132. struct rcconn *next;
  133. };
  134. struct l2cap_pending_req {
  135. uint8_t ident;
  136. bthost_l2cap_rsp_cb cb;
  137. void *user_data;
  138. struct l2cap_pending_req *next;
  139. };
  140. struct l2cap_conn_cb_data {
  141. uint16_t psm;
  142. bthost_l2cap_connect_cb func;
  143. bthost_l2cap_disconnect_cb disconn_func;
  144. void *user_data;
  145. struct l2cap_conn_cb_data *next;
  146. };
  147. struct rfcomm_conn_cb_data {
  148. uint8_t channel;
  149. bthost_rfcomm_connect_cb func;
  150. void *user_data;
  151. struct rfcomm_conn_cb_data *next;
  152. };
  153. struct rfcomm_connection_data {
  154. uint8_t channel;
  155. struct btconn *conn;
  156. bthost_rfcomm_connect_cb cb;
  157. void *user_data;
  158. };
  159. struct bthost {
  160. bool ready;
  161. bthost_ready_cb ready_cb;
  162. uint8_t bdaddr[6];
  163. uint8_t features[8];
  164. bthost_send_func send_handler;
  165. void *send_data;
  166. struct cmd_queue cmd_q;
  167. uint8_t ncmd;
  168. struct btconn *conns;
  169. bthost_cmd_complete_cb cmd_complete_cb;
  170. void *cmd_complete_data;
  171. bthost_new_conn_cb new_conn_cb;
  172. void *new_conn_data;
  173. struct rfcomm_connection_data *rfcomm_conn_data;
  174. struct l2cap_conn_cb_data *new_l2cap_conn_data;
  175. struct rfcomm_conn_cb_data *new_rfcomm_conn_data;
  176. struct l2cap_pending_req *l2reqs;
  177. uint8_t pin[16];
  178. uint8_t pin_len;
  179. uint8_t io_capability;
  180. uint8_t auth_req;
  181. bool reject_user_confirm;
  182. void *smp_data;
  183. bool conn_init;
  184. bool le;
  185. bool sc;
  186. bthost_debug_func_t debug_callback;
  187. bthost_destroy_func_t debug_destroy;
  188. void *debug_data;
  189. };
  190. struct bthost *bthost_create(void)
  191. {
  192. struct bthost *bthost;
  193. bthost = new0(struct bthost, 1);
  194. if (!bthost)
  195. return NULL;
  196. bthost->smp_data = smp_start(bthost);
  197. if (!bthost->smp_data) {
  198. free(bthost);
  199. return NULL;
  200. }
  201. /* Set defaults */
  202. bthost->io_capability = 0x03;
  203. return bthost;
  204. }
  205. static void l2conn_free(struct l2conn *conn)
  206. {
  207. free(conn);
  208. }
  209. static void btconn_free(struct btconn *conn)
  210. {
  211. if (conn->smp_data)
  212. smp_conn_del(conn->smp_data);
  213. while (conn->l2conns) {
  214. struct l2conn *l2conn = conn->l2conns;
  215. conn->l2conns = l2conn->next;
  216. l2conn_free(l2conn);
  217. }
  218. while (conn->cid_hooks) {
  219. struct cid_hook *hook = conn->cid_hooks;
  220. conn->cid_hooks = hook->next;
  221. free(hook);
  222. }
  223. while (conn->rcconns) {
  224. struct rcconn *rcconn = conn->rcconns;
  225. conn->rcconns = rcconn->next;
  226. free(rcconn);
  227. }
  228. while (conn->rfcomm_chan_hooks) {
  229. struct rfcomm_chan_hook *hook = conn->rfcomm_chan_hooks;
  230. conn->rfcomm_chan_hooks = hook->next;
  231. free(hook);
  232. }
  233. free(conn);
  234. }
  235. static struct btconn *bthost_find_conn(struct bthost *bthost, uint16_t handle)
  236. {
  237. struct btconn *conn;
  238. for (conn = bthost->conns; conn != NULL; conn = conn->next) {
  239. if (conn->handle == handle)
  240. return conn;
  241. }
  242. return NULL;
  243. }
  244. static struct btconn *bthost_find_conn_by_bdaddr(struct bthost *bthost,
  245. const uint8_t *bdaddr)
  246. {
  247. struct btconn *conn;
  248. for (conn = bthost->conns; conn != NULL; conn = conn->next) {
  249. if (!memcmp(conn->bdaddr, bdaddr, 6))
  250. return conn;
  251. }
  252. return NULL;
  253. }
  254. static struct l2conn *bthost_add_l2cap_conn(struct bthost *bthost,
  255. struct btconn *conn,
  256. uint16_t scid, uint16_t dcid,
  257. uint16_t psm)
  258. {
  259. struct l2conn *l2conn;
  260. l2conn = malloc(sizeof(*l2conn));
  261. if (!l2conn)
  262. return NULL;
  263. memset(l2conn, 0, sizeof(*l2conn));
  264. l2conn->psm = psm;
  265. l2conn->scid = scid;
  266. l2conn->dcid = dcid;
  267. l2conn->next = conn->l2conns;
  268. conn->l2conns = l2conn;
  269. return l2conn;
  270. }
  271. static struct rcconn *bthost_add_rfcomm_conn(struct bthost *bthost,
  272. struct btconn *conn,
  273. struct l2conn *l2conn,
  274. uint8_t channel)
  275. {
  276. struct rcconn *rcconn;
  277. rcconn = malloc(sizeof(*rcconn));
  278. if (!rcconn)
  279. return NULL;
  280. memset(rcconn, 0, sizeof(*rcconn));
  281. rcconn->channel = channel;
  282. rcconn->scid = l2conn->scid;
  283. rcconn->next = conn->rcconns;
  284. conn->rcconns = rcconn;
  285. return rcconn;
  286. }
  287. static struct rcconn *btconn_find_rfcomm_conn_by_channel(struct btconn *conn,
  288. uint8_t chan)
  289. {
  290. struct rcconn *rcconn;
  291. for (rcconn = conn->rcconns; rcconn != NULL; rcconn = rcconn->next) {
  292. if (rcconn->channel == chan)
  293. return rcconn;
  294. }
  295. return NULL;
  296. }
  297. static struct l2conn *btconn_find_l2cap_conn_by_scid(struct btconn *conn,
  298. uint16_t scid)
  299. {
  300. struct l2conn *l2conn;
  301. for (l2conn = conn->l2conns; l2conn != NULL; l2conn = l2conn->next) {
  302. if (l2conn->scid == scid)
  303. return l2conn;
  304. }
  305. return NULL;
  306. }
  307. static struct l2cap_conn_cb_data *bthost_find_l2cap_cb_by_psm(
  308. struct bthost *bthost, uint16_t psm)
  309. {
  310. struct l2cap_conn_cb_data *cb;
  311. for (cb = bthost->new_l2cap_conn_data; cb != NULL; cb = cb->next) {
  312. if (cb->psm == psm)
  313. return cb;
  314. }
  315. return NULL;
  316. }
  317. static struct rfcomm_conn_cb_data *bthost_find_rfcomm_cb_by_channel(
  318. struct bthost *bthost, uint8_t channel)
  319. {
  320. struct rfcomm_conn_cb_data *cb;
  321. for (cb = bthost->new_rfcomm_conn_data; cb != NULL; cb = cb->next) {
  322. if (cb->channel == channel)
  323. return cb;
  324. }
  325. return NULL;
  326. }
  327. void bthost_destroy(struct bthost *bthost)
  328. {
  329. if (!bthost)
  330. return;
  331. while (bthost->cmd_q.tail) {
  332. struct cmd *cmd = bthost->cmd_q.tail;
  333. bthost->cmd_q.tail = cmd->prev;
  334. free(cmd);
  335. }
  336. while (bthost->conns) {
  337. struct btconn *conn = bthost->conns;
  338. bthost->conns = conn->next;
  339. btconn_free(conn);
  340. }
  341. while (bthost->l2reqs) {
  342. struct l2cap_pending_req *req = bthost->l2reqs;
  343. bthost->l2reqs = req->next;
  344. req->cb(0, NULL, 0, req->user_data);
  345. free(req);
  346. }
  347. while (bthost->new_l2cap_conn_data) {
  348. struct l2cap_conn_cb_data *cb = bthost->new_l2cap_conn_data;
  349. bthost->new_l2cap_conn_data = cb->next;
  350. free(cb);
  351. }
  352. while (bthost->new_rfcomm_conn_data) {
  353. struct rfcomm_conn_cb_data *cb = bthost->new_rfcomm_conn_data;
  354. bthost->new_rfcomm_conn_data = cb->next;
  355. free(cb);
  356. }
  357. if (bthost->rfcomm_conn_data)
  358. free(bthost->rfcomm_conn_data);
  359. smp_stop(bthost->smp_data);
  360. free(bthost);
  361. }
  362. void bthost_set_send_handler(struct bthost *bthost, bthost_send_func handler,
  363. void *user_data)
  364. {
  365. if (!bthost)
  366. return;
  367. bthost->send_handler = handler;
  368. bthost->send_data = user_data;
  369. }
  370. static void queue_command(struct bthost *bthost, const struct iovec *iov,
  371. int iovlen)
  372. {
  373. struct cmd_queue *cmd_q = &bthost->cmd_q;
  374. struct cmd *cmd;
  375. int i;
  376. cmd = malloc(sizeof(*cmd));
  377. if (!cmd)
  378. return;
  379. memset(cmd, 0, sizeof(*cmd));
  380. for (i = 0; i < iovlen; i++) {
  381. memcpy(cmd->data + cmd->len, iov[i].iov_base, iov[i].iov_len);
  382. cmd->len += iov[i].iov_len;
  383. }
  384. if (cmd_q->tail)
  385. cmd_q->tail->next = cmd;
  386. else
  387. cmd_q->head = cmd;
  388. cmd->prev = cmd_q->tail;
  389. cmd_q->tail = cmd;
  390. }
  391. static void send_packet(struct bthost *bthost, const struct iovec *iov,
  392. int iovlen)
  393. {
  394. int i;
  395. if (!bthost->send_handler)
  396. return;
  397. for (i = 0; i < iovlen; i++) {
  398. if (!i)
  399. util_hexdump('<', iov[i].iov_base, iov[i].iov_len,
  400. bthost->debug_callback, bthost->debug_data);
  401. else
  402. util_hexdump(' ', iov[i].iov_base, iov[i].iov_len,
  403. bthost->debug_callback, bthost->debug_data);
  404. }
  405. bthost->send_handler(iov, iovlen, bthost->send_data);
  406. }
  407. static void send_iov(struct bthost *bthost, uint16_t handle, uint16_t cid,
  408. const struct iovec *iov, int iovcnt)
  409. {
  410. struct bt_hci_acl_hdr acl_hdr;
  411. struct bt_l2cap_hdr l2_hdr;
  412. uint8_t pkt = BT_H4_ACL_PKT;
  413. struct iovec pdu[3 + iovcnt];
  414. int i, len = 0;
  415. for (i = 0; i < iovcnt; i++) {
  416. pdu[3 + i].iov_base = iov[i].iov_base;
  417. pdu[3 + i].iov_len = iov[i].iov_len;
  418. len += iov[i].iov_len;
  419. }
  420. pdu[0].iov_base = &pkt;
  421. pdu[0].iov_len = sizeof(pkt);
  422. acl_hdr.handle = acl_handle_pack(handle, 0);
  423. acl_hdr.dlen = cpu_to_le16(len + sizeof(l2_hdr));
  424. pdu[1].iov_base = &acl_hdr;
  425. pdu[1].iov_len = sizeof(acl_hdr);
  426. l2_hdr.cid = cpu_to_le16(cid);
  427. l2_hdr.len = cpu_to_le16(len);
  428. pdu[2].iov_base = &l2_hdr;
  429. pdu[2].iov_len = sizeof(l2_hdr);
  430. send_packet(bthost, pdu, 3 + iovcnt);
  431. }
  432. static void send_acl(struct bthost *bthost, uint16_t handle, uint16_t cid,
  433. const void *data, uint16_t len)
  434. {
  435. struct iovec iov;
  436. iov.iov_base = (void *) data;
  437. iov.iov_len = len;
  438. send_iov(bthost, handle, cid, &iov, 1);
  439. }
  440. static uint8_t l2cap_sig_send(struct bthost *bthost, struct btconn *conn,
  441. uint8_t code, uint8_t ident,
  442. const void *data, uint16_t len)
  443. {
  444. static uint8_t next_ident = 1;
  445. struct bt_l2cap_hdr_sig hdr;
  446. uint16_t cid;
  447. struct iovec iov[2];
  448. if (!ident) {
  449. ident = next_ident++;
  450. if (!ident)
  451. ident = next_ident++;
  452. }
  453. hdr.code = code;
  454. hdr.ident = ident;
  455. hdr.len = cpu_to_le16(len);
  456. iov[0].iov_base = &hdr;
  457. iov[0].iov_len = sizeof(hdr);
  458. if (conn->addr_type == BDADDR_BREDR)
  459. cid = 0x0001;
  460. else
  461. cid = 0x0005;
  462. if (len == 0) {
  463. send_iov(bthost, conn->handle, cid, iov, 1);
  464. return ident;
  465. }
  466. iov[1].iov_base = (void *) data;
  467. iov[1].iov_len = len;
  468. send_iov(bthost, conn->handle, cid, iov, 2);
  469. return ident;
  470. }
  471. void bthost_add_cid_hook(struct bthost *bthost, uint16_t handle, uint16_t cid,
  472. bthost_cid_hook_func_t func, void *user_data)
  473. {
  474. struct cid_hook *hook;
  475. struct btconn *conn;
  476. conn = bthost_find_conn(bthost, handle);
  477. if (!conn)
  478. return;
  479. hook = malloc(sizeof(*hook));
  480. if (!hook)
  481. return;
  482. memset(hook, 0, sizeof(*hook));
  483. hook->cid = cid;
  484. hook->func = func;
  485. hook->user_data = user_data;
  486. hook->next = conn->cid_hooks;
  487. conn->cid_hooks = hook;
  488. }
  489. void bthost_send_cid(struct bthost *bthost, uint16_t handle, uint16_t cid,
  490. const void *data, uint16_t len)
  491. {
  492. struct btconn *conn;
  493. conn = bthost_find_conn(bthost, handle);
  494. if (!conn)
  495. return;
  496. send_acl(bthost, handle, cid, data, len);
  497. }
  498. void bthost_send_cid_v(struct bthost *bthost, uint16_t handle, uint16_t cid,
  499. const struct iovec *iov, int iovcnt)
  500. {
  501. struct btconn *conn;
  502. conn = bthost_find_conn(bthost, handle);
  503. if (!conn)
  504. return;
  505. send_iov(bthost, handle, cid, iov, iovcnt);
  506. }
  507. bool bthost_l2cap_req(struct bthost *bthost, uint16_t handle, uint8_t code,
  508. const void *data, uint16_t len,
  509. bthost_l2cap_rsp_cb cb, void *user_data)
  510. {
  511. struct l2cap_pending_req *req;
  512. struct btconn *conn;
  513. uint8_t ident;
  514. conn = bthost_find_conn(bthost, handle);
  515. if (!conn)
  516. return false;
  517. if (code == BT_L2CAP_PDU_CONN_REQ &&
  518. len == sizeof(struct bt_l2cap_pdu_conn_req)) {
  519. const struct bt_l2cap_pdu_conn_req *req = data;
  520. bthost_add_l2cap_conn(bthost, conn, le16_to_cpu(req->scid),
  521. le16_to_cpu(req->scid),
  522. le16_to_cpu(req->psm));
  523. }
  524. ident = l2cap_sig_send(bthost, conn, code, 0, data, len);
  525. if (!ident)
  526. return false;
  527. if (!cb)
  528. return true;
  529. req = malloc(sizeof(*req));
  530. if (!req)
  531. return false;
  532. memset(req, 0, sizeof(*req));
  533. req->ident = ident;
  534. req->cb = cb;
  535. req->user_data = user_data;
  536. req->next = bthost->l2reqs;
  537. bthost->l2reqs = req;
  538. return true;
  539. }
  540. static void send_command(struct bthost *bthost, uint16_t opcode,
  541. const void *data, uint8_t len)
  542. {
  543. struct bt_hci_cmd_hdr hdr;
  544. uint8_t pkt = BT_H4_CMD_PKT;
  545. struct iovec iov[3];
  546. bthost_debug(bthost, "command 0x%02x", opcode);
  547. iov[0].iov_base = &pkt;
  548. iov[0].iov_len = sizeof(pkt);
  549. hdr.opcode = cpu_to_le16(opcode);
  550. hdr.plen = len;
  551. iov[1].iov_base = &hdr;
  552. iov[1].iov_len = sizeof(hdr);
  553. if (len > 0) {
  554. iov[2].iov_base = (void *) data;
  555. iov[2].iov_len = len;
  556. }
  557. if (bthost->ncmd) {
  558. send_packet(bthost, iov, len > 0 ? 3 : 2);
  559. bthost->ncmd--;
  560. } else {
  561. queue_command(bthost, iov, len > 0 ? 3 : 2);
  562. }
  563. }
  564. static void next_cmd(struct bthost *bthost)
  565. {
  566. struct cmd_queue *cmd_q = &bthost->cmd_q;
  567. struct cmd *cmd = cmd_q->head;
  568. struct cmd *next;
  569. struct iovec iov;
  570. if (!cmd)
  571. return;
  572. next = cmd->next;
  573. if (!bthost->ncmd)
  574. return;
  575. iov.iov_base = cmd->data;
  576. iov.iov_len = cmd->len;
  577. send_packet(bthost, &iov, 1);
  578. bthost->ncmd--;
  579. if (next)
  580. next->prev = NULL;
  581. else
  582. cmd_q->tail = NULL;
  583. cmd_q->head = next;
  584. free(cmd);
  585. }
  586. static void read_bd_addr_complete(struct bthost *bthost, const void *data,
  587. uint8_t len)
  588. {
  589. const struct bt_hci_rsp_read_bd_addr *ev = data;
  590. if (len < sizeof(*ev))
  591. return;
  592. if (ev->status)
  593. return;
  594. memcpy(bthost->bdaddr, ev->bdaddr, 6);
  595. bthost->ready = true;
  596. if (bthost->ready_cb) {
  597. bthost->ready_cb();
  598. bthost->ready_cb = NULL;
  599. }
  600. }
  601. void bthost_notify_ready(struct bthost *bthost, bthost_ready_cb cb)
  602. {
  603. if (bthost->ready) {
  604. cb();
  605. return;
  606. }
  607. bthost->ready_cb = cb;
  608. }
  609. bool bthost_set_debug(struct bthost *bthost, bthost_debug_func_t callback,
  610. void *user_data, bthost_destroy_func_t destroy)
  611. {
  612. if (!bthost)
  613. return false;
  614. if (bthost->debug_destroy)
  615. bthost->debug_destroy(bthost->debug_data);
  616. bthost->debug_callback = callback;
  617. bthost->debug_destroy = destroy;
  618. bthost->debug_data = user_data;
  619. return true;
  620. }
  621. void bthost_debug(struct bthost *host, const char *format, ...)
  622. {
  623. va_list ap;
  624. if (!host || !format || !host->debug_callback)
  625. return;
  626. va_start(ap, format);
  627. util_debug_va(host->debug_callback, host->debug_data, format, ap);
  628. va_end(ap);
  629. }
  630. static void read_local_features_complete(struct bthost *bthost,
  631. const void *data, uint8_t len)
  632. {
  633. const struct bt_hci_rsp_read_local_features *ev = data;
  634. if (len < sizeof(*ev))
  635. return;
  636. if (ev->status)
  637. return;
  638. memcpy(bthost->features, ev->features, 8);
  639. }
  640. static void evt_cmd_complete(struct bthost *bthost, const void *data,
  641. uint8_t len)
  642. {
  643. const struct bt_hci_evt_cmd_complete *ev = data;
  644. const void *param;
  645. uint16_t opcode;
  646. if (len < sizeof(*ev))
  647. return;
  648. param = data + sizeof(*ev);
  649. bthost->ncmd = ev->ncmd;
  650. opcode = le16toh(ev->opcode);
  651. switch (opcode) {
  652. case BT_HCI_CMD_RESET:
  653. break;
  654. case BT_HCI_CMD_READ_LOCAL_FEATURES:
  655. read_local_features_complete(bthost, param, len - sizeof(*ev));
  656. break;
  657. case BT_HCI_CMD_READ_BD_ADDR:
  658. read_bd_addr_complete(bthost, param, len - sizeof(*ev));
  659. break;
  660. case BT_HCI_CMD_WRITE_SCAN_ENABLE:
  661. break;
  662. case BT_HCI_CMD_LE_SET_ADV_ENABLE:
  663. break;
  664. case BT_HCI_CMD_LE_SET_ADV_PARAMETERS:
  665. break;
  666. case BT_HCI_CMD_PIN_CODE_REQUEST_REPLY:
  667. break;
  668. case BT_HCI_CMD_PIN_CODE_REQUEST_NEG_REPLY:
  669. break;
  670. case BT_HCI_CMD_LINK_KEY_REQUEST_NEG_REPLY:
  671. break;
  672. case BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE:
  673. break;
  674. case BT_HCI_CMD_WRITE_LE_HOST_SUPPORTED:
  675. break;
  676. case BT_HCI_CMD_WRITE_SECURE_CONN_SUPPORT:
  677. break;
  678. case BT_HCI_CMD_IO_CAPABILITY_REQUEST_REPLY:
  679. break;
  680. case BT_HCI_CMD_USER_CONFIRM_REQUEST_REPLY:
  681. break;
  682. case BT_HCI_CMD_USER_CONFIRM_REQUEST_NEG_REPLY:
  683. break;
  684. case BT_HCI_CMD_LE_LTK_REQ_REPLY:
  685. break;
  686. case BT_HCI_CMD_LE_LTK_REQ_NEG_REPLY:
  687. break;
  688. case BT_HCI_CMD_LE_SET_ADV_DATA:
  689. break;
  690. case BT_HCI_CMD_LE_SET_EXT_ADV_PARAMS:
  691. break;
  692. case BT_HCI_CMD_LE_SET_EXT_ADV_DATA:
  693. break;
  694. case BT_HCI_CMD_LE_SET_EXT_ADV_ENABLE:
  695. break;
  696. default:
  697. bthost_debug(bthost, "Unhandled cmd_complete opcode 0x%04x",
  698. opcode);
  699. break;
  700. }
  701. if (bthost->cmd_complete_cb)
  702. bthost->cmd_complete_cb(opcode, 0, param, len - sizeof(*ev),
  703. bthost->cmd_complete_data);
  704. next_cmd(bthost);
  705. }
  706. static void evt_cmd_status(struct bthost *bthost, const void *data,
  707. uint8_t len)
  708. {
  709. const struct bt_hci_evt_cmd_status *ev = data;
  710. uint16_t opcode;
  711. if (len < sizeof(*ev))
  712. return;
  713. bthost->ncmd = ev->ncmd;
  714. opcode = le16toh(ev->opcode);
  715. if (ev->status && bthost->cmd_complete_cb)
  716. bthost->cmd_complete_cb(opcode, ev->status, NULL, 0,
  717. bthost->cmd_complete_data);
  718. next_cmd(bthost);
  719. }
  720. static void evt_conn_request(struct bthost *bthost, const void *data,
  721. uint8_t len)
  722. {
  723. const struct bt_hci_evt_conn_request *ev = data;
  724. struct bt_hci_cmd_accept_conn_request cmd;
  725. if (len < sizeof(*ev))
  726. return;
  727. memset(&cmd, 0, sizeof(cmd));
  728. memcpy(cmd.bdaddr, ev->bdaddr, sizeof(ev->bdaddr));
  729. send_command(bthost, BT_HCI_CMD_ACCEPT_CONN_REQUEST, &cmd,
  730. sizeof(cmd));
  731. }
  732. static void init_conn(struct bthost *bthost, uint16_t handle,
  733. const uint8_t *bdaddr, uint8_t addr_type)
  734. {
  735. struct btconn *conn;
  736. const uint8_t *ia, *ra;
  737. uint8_t ia_type, ra_type;
  738. conn = malloc(sizeof(*conn));
  739. if (!conn)
  740. return;
  741. memset(conn, 0, sizeof(*conn));
  742. conn->handle = handle;
  743. memcpy(conn->bdaddr, bdaddr, 6);
  744. conn->addr_type = addr_type;
  745. conn->next_cid = 0x0040;
  746. conn->next = bthost->conns;
  747. bthost->conns = conn;
  748. if (bthost->conn_init) {
  749. ia = bthost->bdaddr;
  750. if (addr_type == BDADDR_BREDR)
  751. ia_type = addr_type;
  752. else
  753. ia_type = BDADDR_LE_PUBLIC;
  754. ra = bdaddr;
  755. ra_type = addr_type;
  756. } else {
  757. ia = bdaddr;
  758. ia_type = addr_type;
  759. ra = bthost->bdaddr;
  760. if (addr_type == BDADDR_BREDR)
  761. ra_type = addr_type;
  762. else
  763. ra_type = BDADDR_LE_PUBLIC;
  764. }
  765. conn->smp_data = smp_conn_add(bthost->smp_data, handle, ia, ia_type,
  766. ra, ra_type, bthost->conn_init);
  767. if (bthost->new_conn_cb)
  768. bthost->new_conn_cb(conn->handle, bthost->new_conn_data);
  769. if (addr_type == BDADDR_BREDR) {
  770. struct bt_l2cap_pdu_info_req req;
  771. req.type = L2CAP_IT_FIXED_CHAN;
  772. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_INFO_REQ, 1,
  773. &req, sizeof(req));
  774. }
  775. }
  776. static void evt_conn_complete(struct bthost *bthost, const void *data,
  777. uint8_t len)
  778. {
  779. const struct bt_hci_evt_conn_complete *ev = data;
  780. if (len < sizeof(*ev))
  781. return;
  782. if (ev->status)
  783. return;
  784. init_conn(bthost, le16_to_cpu(ev->handle), ev->bdaddr, BDADDR_BREDR);
  785. }
  786. static void evt_disconn_complete(struct bthost *bthost, const void *data,
  787. uint8_t len)
  788. {
  789. const struct bt_hci_evt_disconnect_complete *ev = data;
  790. struct btconn **curr;
  791. uint16_t handle;
  792. if (len < sizeof(*ev))
  793. return;
  794. if (ev->status)
  795. return;
  796. handle = le16_to_cpu(ev->handle);
  797. for (curr = &bthost->conns; *curr;) {
  798. struct btconn *conn = *curr;
  799. if (conn->handle == handle) {
  800. *curr = conn->next;
  801. btconn_free(conn);
  802. } else {
  803. curr = &conn->next;
  804. }
  805. }
  806. }
  807. static void evt_num_completed_packets(struct bthost *bthost, const void *data,
  808. uint8_t len)
  809. {
  810. const struct bt_hci_evt_num_completed_packets *ev = data;
  811. if (len < sizeof(*ev))
  812. return;
  813. }
  814. static void evt_auth_complete(struct bthost *bthost, const void *data,
  815. uint8_t len)
  816. {
  817. const struct bt_hci_evt_auth_complete *ev = data;
  818. struct bt_hci_cmd_set_conn_encrypt cp;
  819. if (len < sizeof(*ev))
  820. return;
  821. if (ev->status)
  822. return;
  823. cp.handle = ev->handle;
  824. cp.encr_mode = 0x01;
  825. send_command(bthost, BT_HCI_CMD_SET_CONN_ENCRYPT, &cp, sizeof(cp));
  826. }
  827. static void evt_pin_code_request(struct bthost *bthost, const void *data,
  828. uint8_t len)
  829. {
  830. const struct bt_hci_evt_pin_code_request *ev = data;
  831. if (len < sizeof(*ev))
  832. return;
  833. if (bthost->pin_len > 0) {
  834. struct bt_hci_cmd_pin_code_request_reply cp;
  835. memset(&cp, 0, sizeof(cp));
  836. memcpy(cp.bdaddr, ev->bdaddr, 6);
  837. cp.pin_len = bthost->pin_len;
  838. memcpy(cp.pin_code, bthost->pin, bthost->pin_len);
  839. send_command(bthost, BT_HCI_CMD_PIN_CODE_REQUEST_REPLY,
  840. &cp, sizeof(cp));
  841. } else {
  842. struct bt_hci_cmd_pin_code_request_neg_reply cp;
  843. memcpy(cp.bdaddr, ev->bdaddr, 6);
  844. send_command(bthost, BT_HCI_CMD_PIN_CODE_REQUEST_NEG_REPLY,
  845. &cp, sizeof(cp));
  846. }
  847. }
  848. static void evt_link_key_request(struct bthost *bthost, const void *data,
  849. uint8_t len)
  850. {
  851. const struct bt_hci_evt_link_key_request *ev = data;
  852. struct bt_hci_cmd_link_key_request_neg_reply cp;
  853. if (len < sizeof(*ev))
  854. return;
  855. memset(&cp, 0, sizeof(cp));
  856. memcpy(cp.bdaddr, ev->bdaddr, 6);
  857. send_command(bthost, BT_HCI_CMD_LINK_KEY_REQUEST_NEG_REPLY,
  858. &cp, sizeof(cp));
  859. }
  860. static void evt_link_key_notify(struct bthost *bthost, const void *data,
  861. uint8_t len)
  862. {
  863. const struct bt_hci_evt_link_key_notify *ev = data;
  864. if (len < sizeof(*ev))
  865. return;
  866. }
  867. static void evt_encrypt_change(struct bthost *bthost, const void *data,
  868. uint8_t len)
  869. {
  870. const struct bt_hci_evt_encrypt_change *ev = data;
  871. struct btconn *conn;
  872. uint16_t handle;
  873. if (len < sizeof(*ev))
  874. return;
  875. handle = acl_handle(ev->handle);
  876. conn = bthost_find_conn(bthost, handle);
  877. if (!conn)
  878. return;
  879. if (ev->status)
  880. return;
  881. conn->encr_mode = ev->encr_mode;
  882. if (conn->smp_data)
  883. smp_conn_encrypted(conn->smp_data, conn->encr_mode);
  884. }
  885. static void evt_io_cap_response(struct bthost *bthost, const void *data,
  886. uint8_t len)
  887. {
  888. const struct bt_hci_evt_io_capability_response *ev = data;
  889. struct btconn *conn;
  890. if (len < sizeof(*ev))
  891. return;
  892. conn = bthost_find_conn_by_bdaddr(bthost, ev->bdaddr);
  893. if (!conn)
  894. return;
  895. }
  896. static void evt_io_cap_request(struct bthost *bthost, const void *data,
  897. uint8_t len)
  898. {
  899. const struct bt_hci_evt_io_capability_request *ev = data;
  900. struct bt_hci_cmd_io_capability_request_reply cp;
  901. struct btconn *conn;
  902. if (len < sizeof(*ev))
  903. return;
  904. conn = bthost_find_conn_by_bdaddr(bthost, ev->bdaddr);
  905. if (!conn)
  906. return;
  907. memcpy(cp.bdaddr, ev->bdaddr, 6);
  908. cp.capability = bthost->io_capability;
  909. cp.oob_data = 0x00;
  910. cp.authentication = bthost->auth_req;
  911. send_command(bthost, BT_HCI_CMD_IO_CAPABILITY_REQUEST_REPLY,
  912. &cp, sizeof(cp));
  913. }
  914. static void evt_user_confirm_request(struct bthost *bthost, const void *data,
  915. uint8_t len)
  916. {
  917. const struct bt_hci_evt_user_confirm_request *ev = data;
  918. struct btconn *conn;
  919. if (len < sizeof(*ev))
  920. return;
  921. conn = bthost_find_conn_by_bdaddr(bthost, ev->bdaddr);
  922. if (!conn)
  923. return;
  924. if (bthost->reject_user_confirm) {
  925. send_command(bthost, BT_HCI_CMD_USER_CONFIRM_REQUEST_NEG_REPLY,
  926. ev->bdaddr, 6);
  927. return;
  928. }
  929. send_command(bthost, BT_HCI_CMD_USER_CONFIRM_REQUEST_REPLY,
  930. ev->bdaddr, 6);
  931. }
  932. static void evt_simple_pairing_complete(struct bthost *bthost, const void *data,
  933. uint8_t len)
  934. {
  935. const struct bt_hci_evt_simple_pairing_complete *ev = data;
  936. if (len < sizeof(*ev))
  937. return;
  938. }
  939. static void evt_le_conn_complete(struct bthost *bthost, const void *data,
  940. uint8_t len)
  941. {
  942. const struct bt_hci_evt_le_conn_complete *ev = data;
  943. uint8_t addr_type;
  944. if (len < sizeof(*ev))
  945. return;
  946. if (ev->status)
  947. return;
  948. if (ev->peer_addr_type == 0x00)
  949. addr_type = BDADDR_LE_PUBLIC;
  950. else
  951. addr_type = BDADDR_LE_RANDOM;
  952. init_conn(bthost, le16_to_cpu(ev->handle), ev->peer_addr, addr_type);
  953. }
  954. static void evt_le_ext_conn_complete(struct bthost *bthost, const void *data,
  955. uint8_t len)
  956. {
  957. const struct bt_hci_evt_le_enhanced_conn_complete *ev = data;
  958. uint8_t addr_type;
  959. if (len < sizeof(*ev))
  960. return;
  961. if (ev->status)
  962. return;
  963. if (ev->peer_addr_type == 0x00)
  964. addr_type = BDADDR_LE_PUBLIC;
  965. else
  966. addr_type = BDADDR_LE_RANDOM;
  967. init_conn(bthost, le16_to_cpu(ev->handle), ev->peer_addr, addr_type);
  968. }
  969. static void evt_le_conn_update_complete(struct bthost *bthost, const void *data,
  970. uint8_t len)
  971. {
  972. const struct bt_hci_evt_le_conn_update_complete *ev = data;
  973. if (len < sizeof(*ev))
  974. return;
  975. if (ev->status)
  976. return;
  977. }
  978. static void evt_le_remote_features_complete(struct bthost *bthost,
  979. const void *data, uint8_t len)
  980. {
  981. const struct bt_hci_evt_le_remote_features_complete *ev = data;
  982. if (len < sizeof(*ev))
  983. return;
  984. if (ev->status)
  985. return;
  986. }
  987. static void evt_le_ltk_request(struct bthost *bthost, const void *data,
  988. uint8_t len)
  989. {
  990. const struct bt_hci_evt_le_long_term_key_request *ev = data;
  991. struct bt_hci_cmd_le_ltk_req_reply cp;
  992. struct bt_hci_cmd_le_ltk_req_neg_reply *neg_cp = (void *) &cp;
  993. uint16_t handle, ediv;
  994. uint64_t rand;
  995. struct btconn *conn;
  996. int err;
  997. if (len < sizeof(*ev))
  998. return;
  999. handle = acl_handle(ev->handle);
  1000. conn = bthost_find_conn(bthost, handle);
  1001. if (!conn)
  1002. return;
  1003. rand = le64_to_cpu(ev->rand);
  1004. ediv = le16_to_cpu(ev->ediv);
  1005. cp.handle = ev->handle;
  1006. err = smp_get_ltk(conn->smp_data, rand, ediv, cp.ltk);
  1007. if (err < 0)
  1008. send_command(bthost, BT_HCI_CMD_LE_LTK_REQ_NEG_REPLY,
  1009. neg_cp, sizeof(*neg_cp));
  1010. else
  1011. send_command(bthost, BT_HCI_CMD_LE_LTK_REQ_REPLY, &cp,
  1012. sizeof(cp));
  1013. }
  1014. static void evt_le_cis_req(struct bthost *bthost, const void *data, uint8_t len)
  1015. {
  1016. const struct bt_hci_evt_le_cis_req *ev = data;
  1017. struct bt_hci_cmd_le_accept_cis cmd;
  1018. if (len < sizeof(*ev))
  1019. return;
  1020. memset(&cmd, 0, sizeof(cmd));
  1021. cmd.handle = ev->cis_handle;
  1022. send_command(bthost, BT_HCI_CMD_LE_ACCEPT_CIS, &cmd, sizeof(cmd));
  1023. }
  1024. static void evt_le_meta_event(struct bthost *bthost, const void *data,
  1025. uint8_t len)
  1026. {
  1027. const uint8_t *event = data;
  1028. const void *evt_data = data + 1;
  1029. if (len < 1)
  1030. return;
  1031. bthost_debug(bthost, "event 0x%02x", *event);
  1032. switch (*event) {
  1033. case BT_HCI_EVT_LE_CONN_COMPLETE:
  1034. evt_le_conn_complete(bthost, evt_data, len - 1);
  1035. break;
  1036. case BT_HCI_EVT_LE_CONN_UPDATE_COMPLETE:
  1037. evt_le_conn_update_complete(bthost, evt_data, len - 1);
  1038. break;
  1039. case BT_HCI_EVT_LE_REMOTE_FEATURES_COMPLETE:
  1040. evt_le_remote_features_complete(bthost, evt_data, len - 1);
  1041. break;
  1042. case BT_HCI_EVT_LE_LONG_TERM_KEY_REQUEST:
  1043. evt_le_ltk_request(bthost, evt_data, len - 1);
  1044. break;
  1045. case BT_HCI_EVT_LE_ENHANCED_CONN_COMPLETE:
  1046. evt_le_ext_conn_complete(bthost, evt_data, len - 1);
  1047. break;
  1048. case BT_HCI_EVT_LE_CIS_REQ:
  1049. evt_le_cis_req(bthost, evt_data, len - 1);
  1050. break;
  1051. default:
  1052. bthost_debug(bthost, "Unsupported LE Meta event 0x%2.2x",
  1053. *event);
  1054. break;
  1055. }
  1056. }
  1057. static void process_evt(struct bthost *bthost, const void *data, uint16_t len)
  1058. {
  1059. const struct bt_hci_evt_hdr *hdr = data;
  1060. const void *param;
  1061. if (len < sizeof(*hdr))
  1062. return;
  1063. if (sizeof(*hdr) + hdr->plen != len)
  1064. return;
  1065. param = data + sizeof(*hdr);
  1066. bthost_debug(bthost, "event 0x%02x", hdr->evt);
  1067. switch (hdr->evt) {
  1068. case BT_HCI_EVT_CMD_COMPLETE:
  1069. evt_cmd_complete(bthost, param, hdr->plen);
  1070. break;
  1071. case BT_HCI_EVT_CMD_STATUS:
  1072. evt_cmd_status(bthost, param, hdr->plen);
  1073. break;
  1074. case BT_HCI_EVT_CONN_REQUEST:
  1075. evt_conn_request(bthost, param, hdr->plen);
  1076. break;
  1077. case BT_HCI_EVT_CONN_COMPLETE:
  1078. evt_conn_complete(bthost, param, hdr->plen);
  1079. break;
  1080. case BT_HCI_EVT_DISCONNECT_COMPLETE:
  1081. evt_disconn_complete(bthost, param, hdr->plen);
  1082. break;
  1083. case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
  1084. evt_num_completed_packets(bthost, param, hdr->plen);
  1085. break;
  1086. case BT_HCI_EVT_AUTH_COMPLETE:
  1087. evt_auth_complete(bthost, param, hdr->plen);
  1088. break;
  1089. case BT_HCI_EVT_PIN_CODE_REQUEST:
  1090. evt_pin_code_request(bthost, param, hdr->plen);
  1091. break;
  1092. case BT_HCI_EVT_LINK_KEY_REQUEST:
  1093. evt_link_key_request(bthost, param, hdr->plen);
  1094. break;
  1095. case BT_HCI_EVT_LINK_KEY_NOTIFY:
  1096. evt_link_key_notify(bthost, param, hdr->plen);
  1097. break;
  1098. case BT_HCI_EVT_ENCRYPT_CHANGE:
  1099. evt_encrypt_change(bthost, param, hdr->plen);
  1100. break;
  1101. case BT_HCI_EVT_IO_CAPABILITY_RESPONSE:
  1102. evt_io_cap_response(bthost, param, hdr->plen);
  1103. break;
  1104. case BT_HCI_EVT_IO_CAPABILITY_REQUEST:
  1105. evt_io_cap_request(bthost, param, hdr->plen);
  1106. break;
  1107. case BT_HCI_EVT_USER_CONFIRM_REQUEST:
  1108. evt_user_confirm_request(bthost, param, hdr->plen);
  1109. break;
  1110. case BT_HCI_EVT_SIMPLE_PAIRING_COMPLETE:
  1111. evt_simple_pairing_complete(bthost, param, hdr->plen);
  1112. break;
  1113. case BT_HCI_EVT_LE_META_EVENT:
  1114. evt_le_meta_event(bthost, param, hdr->plen);
  1115. break;
  1116. default:
  1117. bthost_debug(bthost, "Unsupported event 0x%2.2x", hdr->evt);
  1118. break;
  1119. }
  1120. }
  1121. static bool l2cap_cmd_rej(struct bthost *bthost, struct btconn *conn,
  1122. uint8_t ident, const void *data, uint16_t len)
  1123. {
  1124. const struct bt_l2cap_pdu_cmd_reject *rsp = data;
  1125. if (len < sizeof(*rsp))
  1126. return false;
  1127. return true;
  1128. }
  1129. static bool l2cap_conn_req(struct bthost *bthost, struct btconn *conn,
  1130. uint8_t ident, const void *data, uint16_t len)
  1131. {
  1132. const struct bt_l2cap_pdu_conn_req *req = data;
  1133. struct l2cap_conn_cb_data *cb_data;
  1134. struct bt_l2cap_pdu_conn_rsp rsp;
  1135. uint16_t psm;
  1136. if (len < sizeof(*req))
  1137. return false;
  1138. psm = le16_to_cpu(req->psm);
  1139. memset(&rsp, 0, sizeof(rsp));
  1140. rsp.scid = req->scid;
  1141. cb_data = bthost_find_l2cap_cb_by_psm(bthost, psm);
  1142. if (cb_data)
  1143. rsp.dcid = rsp.scid;
  1144. else
  1145. rsp.result = cpu_to_le16(0x0002); /* PSM Not Supported */
  1146. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_CONN_RSP, ident, &rsp,
  1147. sizeof(rsp));
  1148. if (!rsp.result) {
  1149. struct bt_l2cap_pdu_config_req conf_req;
  1150. struct l2conn *l2conn;
  1151. l2conn = bthost_add_l2cap_conn(bthost, conn,
  1152. le16_to_cpu(rsp.dcid),
  1153. le16_to_cpu(rsp.scid),
  1154. le16_to_cpu(psm));
  1155. memset(&conf_req, 0, sizeof(conf_req));
  1156. conf_req.dcid = rsp.scid;
  1157. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_CONFIG_REQ, 0,
  1158. &conf_req, sizeof(conf_req));
  1159. if (cb_data && l2conn->psm == cb_data->psm && cb_data->func)
  1160. cb_data->func(conn->handle, l2conn->dcid,
  1161. cb_data->user_data);
  1162. }
  1163. return true;
  1164. }
  1165. static void rfcomm_sabm_send(struct bthost *bthost, struct btconn *conn,
  1166. struct l2conn *l2conn, uint8_t cr, uint8_t dlci)
  1167. {
  1168. struct rfcomm_cmd cmd;
  1169. cmd.address = RFCOMM_ADDR(cr, dlci);
  1170. cmd.control = RFCOMM_CTRL(RFCOMM_SABM, 1);
  1171. cmd.length = RFCOMM_LEN8(0);
  1172. cmd.fcs = rfcomm_fcs2((uint8_t *)&cmd);
  1173. send_acl(bthost, conn->handle, l2conn->dcid, &cmd, sizeof(cmd));
  1174. }
  1175. static bool l2cap_conn_rsp(struct bthost *bthost, struct btconn *conn,
  1176. uint8_t ident, const void *data, uint16_t len)
  1177. {
  1178. const struct bt_l2cap_pdu_conn_rsp *rsp = data;
  1179. struct bt_l2cap_pdu_config_req req;
  1180. struct l2conn *l2conn;
  1181. if (len < sizeof(*rsp))
  1182. return false;
  1183. l2conn = btconn_find_l2cap_conn_by_scid(conn, le16_to_cpu(rsp->scid));
  1184. if (l2conn)
  1185. l2conn->dcid = le16_to_cpu(rsp->dcid);
  1186. else
  1187. return false;
  1188. if (rsp->result)
  1189. return true;
  1190. memset(&req, 0, sizeof(req));
  1191. req.dcid = rsp->dcid;
  1192. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_CONFIG_REQ, 0,
  1193. &req, sizeof(req));
  1194. return true;
  1195. }
  1196. static bool l2cap_config_req(struct bthost *bthost, struct btconn *conn,
  1197. uint8_t ident, const void *data, uint16_t len)
  1198. {
  1199. const struct bt_l2cap_pdu_config_req *req = data;
  1200. struct bt_l2cap_pdu_config_rsp rsp;
  1201. struct l2conn *l2conn;
  1202. uint16_t dcid;
  1203. if (len < sizeof(*req))
  1204. return false;
  1205. dcid = le16_to_cpu(req->dcid);
  1206. l2conn = btconn_find_l2cap_conn_by_scid(conn, dcid);
  1207. if (!l2conn)
  1208. return false;
  1209. memset(&rsp, 0, sizeof(rsp));
  1210. rsp.scid = cpu_to_le16(l2conn->dcid);
  1211. rsp.flags = req->flags;
  1212. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_CONFIG_RSP, ident, &rsp,
  1213. sizeof(rsp));
  1214. return true;
  1215. }
  1216. static bool l2cap_config_rsp(struct bthost *bthost, struct btconn *conn,
  1217. uint8_t ident, const void *data, uint16_t len)
  1218. {
  1219. const struct bt_l2cap_pdu_config_rsp *rsp = data;
  1220. struct l2conn *l2conn;
  1221. if (len < sizeof(*rsp))
  1222. return false;
  1223. l2conn = btconn_find_l2cap_conn_by_scid(conn, rsp->scid);
  1224. if (!l2conn)
  1225. return false;
  1226. if (l2conn->psm == 0x0003 && !rsp->result && bthost->rfcomm_conn_data)
  1227. rfcomm_sabm_send(bthost, conn, l2conn, 1, 0);
  1228. return true;
  1229. }
  1230. static bool l2cap_disconn_req(struct bthost *bthost, struct btconn *conn,
  1231. uint8_t ident, const void *data, uint16_t len)
  1232. {
  1233. const struct bt_l2cap_pdu_disconn_req *req = data;
  1234. struct l2cap_conn_cb_data *cb_data;
  1235. struct bt_l2cap_pdu_disconn_rsp rsp;
  1236. struct l2conn *l2conn;
  1237. if (len < sizeof(*req))
  1238. return false;
  1239. memset(&rsp, 0, sizeof(rsp));
  1240. rsp.dcid = req->dcid;
  1241. rsp.scid = req->scid;
  1242. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_DISCONN_RSP, ident, &rsp,
  1243. sizeof(rsp));
  1244. l2conn = btconn_find_l2cap_conn_by_scid(conn, rsp.scid);
  1245. if (!l2conn)
  1246. return true;
  1247. cb_data = bthost_find_l2cap_cb_by_psm(bthost, l2conn->psm);
  1248. if (cb_data && cb_data->disconn_func)
  1249. cb_data->disconn_func(cb_data->user_data);
  1250. return true;
  1251. }
  1252. static bool l2cap_info_req(struct bthost *bthost, struct btconn *conn,
  1253. uint8_t ident, const void *data, uint16_t len)
  1254. {
  1255. const struct bt_l2cap_pdu_info_req *req = data;
  1256. uint64_t fixed_chan;
  1257. uint16_t type;
  1258. uint8_t buf[12];
  1259. struct bt_l2cap_pdu_info_rsp *rsp = (void *) buf;
  1260. if (len < sizeof(*req))
  1261. return false;
  1262. memset(buf, 0, sizeof(buf));
  1263. rsp->type = req->type;
  1264. type = le16_to_cpu(req->type);
  1265. switch (type) {
  1266. case L2CAP_IT_FEAT_MASK:
  1267. rsp->result = 0x0000;
  1268. put_le32(L2CAP_FEAT_FIXED_CHAN, rsp->data);
  1269. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_INFO_RSP, ident,
  1270. rsp, sizeof(*rsp) + 4);
  1271. break;
  1272. case L2CAP_IT_FIXED_CHAN:
  1273. rsp->result = 0x0000;
  1274. fixed_chan = L2CAP_FC_SIG_BREDR;
  1275. if (bthost->sc && bthost->le)
  1276. fixed_chan |= L2CAP_FC_SMP_BREDR;
  1277. put_le64(fixed_chan, rsp->data);
  1278. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_INFO_RSP, ident,
  1279. rsp, sizeof(*rsp) + sizeof(fixed_chan));
  1280. break;
  1281. default:
  1282. rsp->result = cpu_to_le16(0x0001); /* Not Supported */
  1283. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_INFO_RSP, ident,
  1284. rsp, sizeof(*rsp));
  1285. break;
  1286. }
  1287. return true;
  1288. }
  1289. static bool l2cap_info_rsp(struct bthost *bthost, struct btconn *conn,
  1290. uint8_t ident, const void *data, uint16_t len)
  1291. {
  1292. const struct bt_l2cap_pdu_info_rsp *rsp = data;
  1293. uint16_t type;
  1294. if (len < sizeof(*rsp))
  1295. return false;
  1296. if (rsp->result)
  1297. return true;
  1298. type = le16_to_cpu(rsp->type);
  1299. switch (type) {
  1300. case L2CAP_IT_FIXED_CHAN:
  1301. if (len < sizeof(*rsp) + 8)
  1302. return false;
  1303. conn->fixed_chan = get_le64(rsp->data);
  1304. if (conn->smp_data && conn->encr_mode)
  1305. smp_conn_encrypted(conn->smp_data, conn->encr_mode);
  1306. break;
  1307. default:
  1308. break;
  1309. }
  1310. return true;
  1311. }
  1312. static void handle_pending_l2reqs(struct bthost *bthost, struct btconn *conn,
  1313. uint8_t ident, uint8_t code,
  1314. const void *data, uint16_t len)
  1315. {
  1316. struct l2cap_pending_req **curr;
  1317. for (curr = &bthost->l2reqs; *curr != NULL;) {
  1318. struct l2cap_pending_req *req = *curr;
  1319. if (req->ident != ident) {
  1320. curr = &req->next;
  1321. continue;
  1322. }
  1323. *curr = req->next;
  1324. req->cb(code, data, len, req->user_data);
  1325. free(req);
  1326. }
  1327. }
  1328. static bool l2cap_rsp(uint8_t code)
  1329. {
  1330. switch (code) {
  1331. case BT_L2CAP_PDU_CMD_REJECT:
  1332. case BT_L2CAP_PDU_CONN_RSP:
  1333. case BT_L2CAP_PDU_CONFIG_RSP:
  1334. case BT_L2CAP_PDU_INFO_RSP:
  1335. return true;
  1336. }
  1337. return false;
  1338. }
  1339. static void l2cap_sig(struct bthost *bthost, struct btconn *conn,
  1340. const void *data, uint16_t len)
  1341. {
  1342. const struct bt_l2cap_hdr_sig *hdr = data;
  1343. struct bt_l2cap_pdu_cmd_reject rej;
  1344. uint16_t hdr_len;
  1345. bool ret;
  1346. if (len < sizeof(*hdr))
  1347. goto reject;
  1348. hdr_len = le16_to_cpu(hdr->len);
  1349. if (sizeof(*hdr) + hdr_len != len)
  1350. goto reject;
  1351. switch (hdr->code) {
  1352. case BT_L2CAP_PDU_CMD_REJECT:
  1353. ret = l2cap_cmd_rej(bthost, conn, hdr->ident,
  1354. data + sizeof(*hdr), hdr_len);
  1355. break;
  1356. case BT_L2CAP_PDU_CONN_REQ:
  1357. ret = l2cap_conn_req(bthost, conn, hdr->ident,
  1358. data + sizeof(*hdr), hdr_len);
  1359. break;
  1360. case BT_L2CAP_PDU_CONN_RSP:
  1361. ret = l2cap_conn_rsp(bthost, conn, hdr->ident,
  1362. data + sizeof(*hdr), hdr_len);
  1363. break;
  1364. case BT_L2CAP_PDU_CONFIG_REQ:
  1365. ret = l2cap_config_req(bthost, conn, hdr->ident,
  1366. data + sizeof(*hdr), hdr_len);
  1367. break;
  1368. case BT_L2CAP_PDU_CONFIG_RSP:
  1369. ret = l2cap_config_rsp(bthost, conn, hdr->ident,
  1370. data + sizeof(*hdr), hdr_len);
  1371. break;
  1372. case BT_L2CAP_PDU_DISCONN_REQ:
  1373. ret = l2cap_disconn_req(bthost, conn, hdr->ident,
  1374. data + sizeof(*hdr), hdr_len);
  1375. break;
  1376. case BT_L2CAP_PDU_INFO_REQ:
  1377. ret = l2cap_info_req(bthost, conn, hdr->ident,
  1378. data + sizeof(*hdr), hdr_len);
  1379. break;
  1380. case BT_L2CAP_PDU_INFO_RSP:
  1381. ret = l2cap_info_rsp(bthost, conn, hdr->ident,
  1382. data + sizeof(*hdr), hdr_len);
  1383. break;
  1384. default:
  1385. bthost_debug(bthost, "Unknown L2CAP code 0x%02x", hdr->code);
  1386. ret = false;
  1387. }
  1388. if (l2cap_rsp(hdr->code))
  1389. handle_pending_l2reqs(bthost, conn, hdr->ident, hdr->code,
  1390. data + sizeof(*hdr), hdr_len);
  1391. if (ret)
  1392. return;
  1393. reject:
  1394. memset(&rej, 0, sizeof(rej));
  1395. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_CMD_REJECT, 0,
  1396. &rej, sizeof(rej));
  1397. }
  1398. static bool l2cap_conn_param_req(struct bthost *bthost, struct btconn *conn,
  1399. uint8_t ident, const void *data, uint16_t len)
  1400. {
  1401. const struct bt_l2cap_pdu_conn_param_req *req = data;
  1402. struct bt_l2cap_pdu_conn_param_rsp rsp;
  1403. struct bt_hci_cmd_le_conn_update hci_cmd;
  1404. if (len < sizeof(*req))
  1405. return false;
  1406. memset(&hci_cmd, 0, sizeof(hci_cmd));
  1407. hci_cmd.handle = cpu_to_le16(conn->handle);
  1408. hci_cmd.min_interval = req->min_interval;
  1409. hci_cmd.max_interval = req->max_interval;
  1410. hci_cmd.latency = req->latency;
  1411. hci_cmd.supv_timeout = req->timeout;
  1412. hci_cmd.min_length = cpu_to_le16(0x0001);
  1413. hci_cmd.max_length = cpu_to_le16(0x0001);
  1414. send_command(bthost, BT_HCI_CMD_LE_CONN_UPDATE,
  1415. &hci_cmd, sizeof(hci_cmd));
  1416. memset(&rsp, 0, sizeof(rsp));
  1417. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_CONN_PARAM_RSP, ident,
  1418. &rsp, sizeof(rsp));
  1419. return true;
  1420. }
  1421. static bool l2cap_conn_param_rsp(struct bthost *bthost, struct btconn *conn,
  1422. uint8_t ident, const void *data, uint16_t len)
  1423. {
  1424. const struct bt_l2cap_pdu_conn_param_req *rsp = data;
  1425. if (len < sizeof(*rsp))
  1426. return false;
  1427. return true;
  1428. }
  1429. static bool l2cap_le_conn_req(struct bthost *bthost, struct btconn *conn,
  1430. uint8_t ident, const void *data, uint16_t len)
  1431. {
  1432. const struct bt_l2cap_pdu_le_conn_req *req = data;
  1433. struct bt_l2cap_pdu_le_conn_rsp rsp;
  1434. uint16_t psm;
  1435. if (len < sizeof(*req))
  1436. return false;
  1437. psm = le16_to_cpu(req->psm);
  1438. memset(&rsp, 0, sizeof(rsp));
  1439. rsp.mtu = 23;
  1440. rsp.mps = 23;
  1441. rsp.credits = 1;
  1442. if (bthost_find_l2cap_cb_by_psm(bthost, psm))
  1443. rsp.dcid = cpu_to_le16(conn->next_cid++);
  1444. else
  1445. rsp.result = cpu_to_le16(0x0002); /* PSM Not Supported */
  1446. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_LE_CONN_RSP, ident, &rsp,
  1447. sizeof(rsp));
  1448. return true;
  1449. }
  1450. static bool l2cap_le_conn_rsp(struct bthost *bthost, struct btconn *conn,
  1451. uint8_t ident, const void *data, uint16_t len)
  1452. {
  1453. const struct bt_l2cap_pdu_le_conn_rsp *rsp = data;
  1454. if (len < sizeof(*rsp))
  1455. return false;
  1456. /* TODO add L2CAP connection before with proper PSM */
  1457. bthost_add_l2cap_conn(bthost, conn, 0, le16_to_cpu(rsp->dcid), 0);
  1458. return true;
  1459. }
  1460. static bool l2cap_ecred_conn_req(struct bthost *bthost, struct btconn *conn,
  1461. uint8_t ident, const void *data, uint16_t len)
  1462. {
  1463. const struct bt_l2cap_pdu_ecred_conn_req *req = data;
  1464. struct {
  1465. struct bt_l2cap_pdu_ecred_conn_rsp pdu;
  1466. uint16_t dcid[5];
  1467. } __attribute__ ((packed)) rsp;
  1468. uint16_t psm;
  1469. int num_scid, i = 0;
  1470. if (len < sizeof(*req))
  1471. return false;
  1472. psm = le16_to_cpu(req->psm);
  1473. memset(&rsp, 0, sizeof(rsp));
  1474. rsp.pdu.mtu = 64;
  1475. rsp.pdu.mps = 64;
  1476. rsp.pdu.credits = 1;
  1477. if (!bthost_find_l2cap_cb_by_psm(bthost, psm)) {
  1478. rsp.pdu.result = cpu_to_le16(0x0002); /* PSM Not Supported */
  1479. goto respond;
  1480. }
  1481. len -= sizeof(rsp.pdu);
  1482. num_scid = len / sizeof(*req->scid);
  1483. for (; i < num_scid; i++)
  1484. rsp.dcid[i] = cpu_to_le16(conn->next_cid++);
  1485. respond:
  1486. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_ECRED_CONN_RSP, ident, &rsp,
  1487. sizeof(rsp.pdu) + i * sizeof(*rsp.dcid));
  1488. return true;
  1489. }
  1490. static bool l2cap_ecred_conn_rsp(struct bthost *bthost, struct btconn *conn,
  1491. uint8_t ident, const void *data, uint16_t len)
  1492. {
  1493. const struct {
  1494. const struct bt_l2cap_pdu_ecred_conn_rsp *pdu;
  1495. uint16_t scid[5];
  1496. } __attribute__ ((packed)) *rsp = data;
  1497. int num_scid, i;
  1498. if (len < sizeof(*rsp))
  1499. return false;
  1500. num_scid = len / sizeof(*rsp->scid);
  1501. for (i = 0; i < num_scid; i++)
  1502. /* TODO add L2CAP connection before with proper PSM */
  1503. bthost_add_l2cap_conn(bthost, conn, 0,
  1504. le16_to_cpu(rsp->scid[i]), 0);
  1505. return true;
  1506. }
  1507. static bool l2cap_le_rsp(uint8_t code)
  1508. {
  1509. switch (code) {
  1510. case BT_L2CAP_PDU_CMD_REJECT:
  1511. case BT_L2CAP_PDU_CONN_PARAM_RSP:
  1512. case BT_L2CAP_PDU_LE_CONN_RSP:
  1513. case BT_L2CAP_PDU_ECRED_CONN_RSP:
  1514. return true;
  1515. }
  1516. return false;
  1517. }
  1518. static void l2cap_le_sig(struct bthost *bthost, struct btconn *conn,
  1519. const void *data, uint16_t len)
  1520. {
  1521. const struct bt_l2cap_hdr_sig *hdr = data;
  1522. struct bt_l2cap_pdu_cmd_reject rej;
  1523. uint16_t hdr_len;
  1524. bool ret;
  1525. if (len < sizeof(*hdr))
  1526. goto reject;
  1527. hdr_len = le16_to_cpu(hdr->len);
  1528. if (sizeof(*hdr) + hdr_len != len)
  1529. goto reject;
  1530. switch (hdr->code) {
  1531. case BT_L2CAP_PDU_CMD_REJECT:
  1532. ret = l2cap_cmd_rej(bthost, conn, hdr->ident,
  1533. data + sizeof(*hdr), hdr_len);
  1534. break;
  1535. case BT_L2CAP_PDU_DISCONN_REQ:
  1536. ret = l2cap_disconn_req(bthost, conn, hdr->ident,
  1537. data + sizeof(*hdr), hdr_len);
  1538. break;
  1539. case BT_L2CAP_PDU_CONN_PARAM_REQ:
  1540. ret = l2cap_conn_param_req(bthost, conn, hdr->ident,
  1541. data + sizeof(*hdr), hdr_len);
  1542. break;
  1543. case BT_L2CAP_PDU_CONN_PARAM_RSP:
  1544. ret = l2cap_conn_param_rsp(bthost, conn, hdr->ident,
  1545. data + sizeof(*hdr), hdr_len);
  1546. break;
  1547. case BT_L2CAP_PDU_LE_CONN_REQ:
  1548. ret = l2cap_le_conn_req(bthost, conn, hdr->ident,
  1549. data + sizeof(*hdr), hdr_len);
  1550. break;
  1551. case BT_L2CAP_PDU_LE_CONN_RSP:
  1552. ret = l2cap_le_conn_rsp(bthost, conn, hdr->ident,
  1553. data + sizeof(*hdr), hdr_len);
  1554. break;
  1555. case BT_L2CAP_PDU_ECRED_CONN_REQ:
  1556. ret = l2cap_ecred_conn_req(bthost, conn, hdr->ident,
  1557. data + sizeof(*hdr), hdr_len);
  1558. break;
  1559. case BT_L2CAP_PDU_ECRED_CONN_RSP:
  1560. ret = l2cap_ecred_conn_rsp(bthost, conn, hdr->ident,
  1561. data + sizeof(*hdr), hdr_len);
  1562. break;
  1563. default:
  1564. bthost_debug(bthost, "Unknown L2CAP code 0x%02x", hdr->code);
  1565. ret = false;
  1566. }
  1567. if (l2cap_le_rsp(hdr->code))
  1568. handle_pending_l2reqs(bthost, conn, hdr->ident, hdr->code,
  1569. data + sizeof(*hdr), hdr_len);
  1570. if (ret)
  1571. return;
  1572. reject:
  1573. memset(&rej, 0, sizeof(rej));
  1574. l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_CMD_REJECT, 0,
  1575. &rej, sizeof(rej));
  1576. }
  1577. static struct cid_hook *find_cid_hook(struct btconn *conn, uint16_t cid)
  1578. {
  1579. struct cid_hook *hook;
  1580. for (hook = conn->cid_hooks; hook != NULL; hook = hook->next) {
  1581. if (hook->cid == cid)
  1582. return hook;
  1583. }
  1584. return NULL;
  1585. }
  1586. static struct rfcomm_chan_hook *find_rfcomm_chan_hook(struct btconn *conn,
  1587. uint8_t channel)
  1588. {
  1589. struct rfcomm_chan_hook *hook;
  1590. for (hook = conn->rfcomm_chan_hooks; hook != NULL; hook = hook->next)
  1591. if (hook->channel == channel)
  1592. return hook;
  1593. return NULL;
  1594. }
  1595. static void rfcomm_ua_send(struct bthost *bthost, struct btconn *conn,
  1596. struct l2conn *l2conn, uint8_t cr, uint8_t dlci)
  1597. {
  1598. struct rfcomm_cmd cmd;
  1599. cmd.address = RFCOMM_ADDR(cr, dlci);
  1600. cmd.control = RFCOMM_CTRL(RFCOMM_UA, 1);
  1601. cmd.length = RFCOMM_LEN8(0);
  1602. cmd.fcs = rfcomm_fcs2((uint8_t *)&cmd);
  1603. send_acl(bthost, conn->handle, l2conn->dcid, &cmd, sizeof(cmd));
  1604. }
  1605. static void rfcomm_dm_send(struct bthost *bthost, struct btconn *conn,
  1606. struct l2conn *l2conn, uint8_t cr, uint8_t dlci)
  1607. {
  1608. struct rfcomm_cmd cmd;
  1609. cmd.address = RFCOMM_ADDR(cr, dlci);
  1610. cmd.control = RFCOMM_CTRL(RFCOMM_DM, 1);
  1611. cmd.length = RFCOMM_LEN8(0);
  1612. cmd.fcs = rfcomm_fcs2((uint8_t *)&cmd);
  1613. send_acl(bthost, conn->handle, l2conn->dcid, &cmd, sizeof(cmd));
  1614. }
  1615. static void rfcomm_sabm_recv(struct bthost *bthost, struct btconn *conn,
  1616. struct l2conn *l2conn, const void *data,
  1617. uint16_t len)
  1618. {
  1619. const struct rfcomm_cmd *hdr = data;
  1620. uint8_t dlci;
  1621. struct rfcomm_conn_cb_data *cb;
  1622. uint8_t chan;
  1623. if (len < sizeof(*hdr))
  1624. return;
  1625. chan = RFCOMM_GET_CHANNEL(hdr->address);
  1626. dlci = RFCOMM_GET_DLCI(hdr->address);
  1627. cb = bthost_find_rfcomm_cb_by_channel(bthost, chan);
  1628. if (!dlci || cb) {
  1629. bthost_add_rfcomm_conn(bthost, conn, l2conn, chan);
  1630. rfcomm_ua_send(bthost, conn, l2conn, 1, dlci);
  1631. if (cb && cb->func)
  1632. cb->func(conn->handle, l2conn->scid, cb->user_data,
  1633. true);
  1634. } else {
  1635. rfcomm_dm_send(bthost, conn, l2conn, 1, dlci);
  1636. }
  1637. }
  1638. static void rfcomm_disc_recv(struct bthost *bthost, struct btconn *conn,
  1639. struct l2conn *l2conn, const void *data,
  1640. uint16_t len)
  1641. {
  1642. const struct rfcomm_cmd *hdr = data;
  1643. uint8_t dlci;
  1644. if (len < sizeof(*hdr))
  1645. return;
  1646. dlci = RFCOMM_GET_DLCI(hdr->address);
  1647. rfcomm_ua_send(bthost, conn, l2conn, 0, dlci);
  1648. }
  1649. static void rfcomm_uih_send(struct bthost *bthost, struct btconn *conn,
  1650. struct l2conn *l2conn, uint8_t address,
  1651. uint8_t type, const void *data, uint16_t len)
  1652. {
  1653. struct rfcomm_hdr hdr;
  1654. struct rfcomm_mcc mcc;
  1655. uint8_t fcs;
  1656. struct iovec iov[4];
  1657. hdr.address = address;
  1658. hdr.control = RFCOMM_CTRL(RFCOMM_UIH, 0);
  1659. hdr.length = RFCOMM_LEN8(sizeof(mcc) + len);
  1660. iov[0].iov_base = &hdr;
  1661. iov[0].iov_len = sizeof(hdr);
  1662. mcc.type = type;
  1663. mcc.length = RFCOMM_LEN8(len);
  1664. iov[1].iov_base = &mcc;
  1665. iov[1].iov_len = sizeof(mcc);
  1666. iov[2].iov_base = (void *) data;
  1667. iov[2].iov_len = len;
  1668. fcs = rfcomm_fcs((uint8_t *) &hdr);
  1669. iov[3].iov_base = &fcs;
  1670. iov[3].iov_len = sizeof(fcs);
  1671. send_iov(bthost, conn->handle, l2conn->dcid, iov, 4);
  1672. }
  1673. static void rfcomm_ua_recv(struct bthost *bthost, struct btconn *conn,
  1674. struct l2conn *l2conn, const void *data,
  1675. uint16_t len)
  1676. {
  1677. const struct rfcomm_cmd *ua_hdr = data;
  1678. uint8_t channel;
  1679. struct rfcomm_connection_data *conn_data = bthost->rfcomm_conn_data;
  1680. uint8_t type;
  1681. struct rfcomm_pn pn_cmd;
  1682. if (len < sizeof(*ua_hdr))
  1683. return;
  1684. channel = RFCOMM_GET_CHANNEL(ua_hdr->address);
  1685. type = RFCOMM_GET_TYPE(ua_hdr->control);
  1686. if (channel && conn_data && conn_data->channel == channel) {
  1687. bthost_add_rfcomm_conn(bthost, conn, l2conn, channel);
  1688. if (conn_data->cb)
  1689. conn_data->cb(conn->handle, l2conn->scid,
  1690. conn_data->user_data, true);
  1691. free(bthost->rfcomm_conn_data);
  1692. bthost->rfcomm_conn_data = NULL;
  1693. return;
  1694. }
  1695. if (!conn_data || !RFCOMM_TEST_CR(type))
  1696. return;
  1697. bthost_add_rfcomm_conn(bthost, conn, l2conn, channel);
  1698. pn_cmd.dlci = conn_data->channel * 2;
  1699. pn_cmd.priority = 7;
  1700. pn_cmd.ack_timer = 0;
  1701. pn_cmd.max_retrans = 0;
  1702. pn_cmd.mtu = 667;
  1703. pn_cmd.credits = 7;
  1704. rfcomm_uih_send(bthost, conn, l2conn, RFCOMM_ADDR(1, 0),
  1705. RFCOMM_MCC_TYPE(1, RFCOMM_PN), &pn_cmd, sizeof(pn_cmd));
  1706. }
  1707. static void rfcomm_dm_recv(struct bthost *bthost, struct btconn *conn,
  1708. struct l2conn *l2conn, const void *data,
  1709. uint16_t len)
  1710. {
  1711. const struct rfcomm_cmd *hdr = data;
  1712. uint8_t channel;
  1713. struct rfcomm_connection_data *conn_data = bthost->rfcomm_conn_data;
  1714. if (len < sizeof(*hdr))
  1715. return;
  1716. channel = RFCOMM_GET_CHANNEL(hdr->address);
  1717. if (conn_data && conn_data->channel == channel) {
  1718. if (conn_data->cb)
  1719. conn_data->cb(conn->handle, l2conn->scid,
  1720. conn_data->user_data, false);
  1721. free(bthost->rfcomm_conn_data);
  1722. bthost->rfcomm_conn_data = NULL;
  1723. }
  1724. }
  1725. static void rfcomm_msc_recv(struct bthost *bthost, struct btconn *conn,
  1726. struct l2conn *l2conn, uint8_t cr,
  1727. const struct rfcomm_msc *msc)
  1728. {
  1729. struct rfcomm_msc msc_cmd;
  1730. msc_cmd.dlci = msc->dlci;
  1731. msc_cmd.v24_sig = msc->v24_sig;
  1732. rfcomm_uih_send(bthost, conn, l2conn, RFCOMM_ADDR(0, 0),
  1733. RFCOMM_MCC_TYPE(cr, RFCOMM_MSC), &msc_cmd,
  1734. sizeof(msc_cmd));
  1735. }
  1736. static void rfcomm_pn_recv(struct bthost *bthost, struct btconn *conn,
  1737. struct l2conn *l2conn, uint8_t cr,
  1738. const struct rfcomm_pn *pn)
  1739. {
  1740. struct rfcomm_pn pn_cmd;
  1741. if (!cr) {
  1742. rfcomm_sabm_send(bthost, conn, l2conn, 1,
  1743. bthost->rfcomm_conn_data->channel * 2);
  1744. return;
  1745. }
  1746. pn_cmd.dlci = pn->dlci;
  1747. pn_cmd.flow_ctrl = pn->flow_ctrl;
  1748. pn_cmd.priority = pn->priority;
  1749. pn_cmd.ack_timer = pn->ack_timer;
  1750. pn_cmd.max_retrans = pn->max_retrans;
  1751. pn_cmd.mtu = pn->mtu;
  1752. pn_cmd.credits = pn->credits;
  1753. rfcomm_uih_send(bthost, conn, l2conn, RFCOMM_ADDR(1, 0),
  1754. RFCOMM_MCC_TYPE(0, RFCOMM_PN), &pn_cmd, sizeof(pn_cmd));
  1755. }
  1756. static void rfcomm_mcc_recv(struct bthost *bthost, struct btconn *conn,
  1757. struct l2conn *l2conn, const void *data, uint16_t len)
  1758. {
  1759. const struct rfcomm_mcc *mcc = data;
  1760. const struct rfcomm_msc *msc;
  1761. const struct rfcomm_pn *pn;
  1762. if (len < sizeof(*mcc))
  1763. return;
  1764. switch (RFCOMM_GET_MCC_TYPE(mcc->type)) {
  1765. case RFCOMM_MSC:
  1766. if (len - sizeof(*mcc) < sizeof(*msc))
  1767. break;
  1768. msc = data + sizeof(*mcc);
  1769. rfcomm_msc_recv(bthost, conn, l2conn,
  1770. RFCOMM_TEST_CR(mcc->type) / 2, msc);
  1771. break;
  1772. case RFCOMM_PN:
  1773. if (len - sizeof(*mcc) < sizeof(*pn))
  1774. break;
  1775. pn = data + sizeof(*mcc);
  1776. rfcomm_pn_recv(bthost, conn, l2conn,
  1777. RFCOMM_TEST_CR(mcc->type) / 2, pn);
  1778. break;
  1779. default:
  1780. break;
  1781. }
  1782. }
  1783. #define GET_LEN8(length) ((length & 0xfe) >> 1)
  1784. #define GET_LEN16(length) ((length & 0xfffe) >> 1)
  1785. static void rfcomm_uih_recv(struct bthost *bthost, struct btconn *conn,
  1786. struct l2conn *l2conn, const void *data,
  1787. uint16_t len)
  1788. {
  1789. const struct rfcomm_hdr *hdr = data;
  1790. uint16_t hdr_len, data_len;
  1791. const void *p;
  1792. if (len < sizeof(*hdr))
  1793. return;
  1794. if (RFCOMM_TEST_EA(hdr->length)) {
  1795. data_len = (uint16_t) GET_LEN8(hdr->length);
  1796. hdr_len = sizeof(*hdr);
  1797. } else {
  1798. uint8_t ex_len = *((uint8_t *)(data + sizeof(*hdr)));
  1799. data_len = ((uint16_t) hdr->length << 8) | ex_len;
  1800. hdr_len = sizeof(*hdr) + sizeof(uint8_t);
  1801. }
  1802. if (len < hdr_len + data_len)
  1803. return;
  1804. p = data + hdr_len;
  1805. if (RFCOMM_GET_DLCI(hdr->address)) {
  1806. struct rfcomm_chan_hook *hook;
  1807. hook = find_rfcomm_chan_hook(conn,
  1808. RFCOMM_GET_CHANNEL(hdr->address));
  1809. if (hook && data_len)
  1810. hook->func(p, data_len, hook->user_data);
  1811. } else {
  1812. rfcomm_mcc_recv(bthost, conn, l2conn, p, data_len);
  1813. }
  1814. }
  1815. static void process_rfcomm(struct bthost *bthost, struct btconn *conn,
  1816. struct l2conn *l2conn, const void *data,
  1817. uint16_t len)
  1818. {
  1819. const struct rfcomm_hdr *hdr = data;
  1820. switch (RFCOMM_GET_TYPE(hdr->control)) {
  1821. case RFCOMM_SABM:
  1822. rfcomm_sabm_recv(bthost, conn, l2conn, data, len);
  1823. break;
  1824. case RFCOMM_DISC:
  1825. rfcomm_disc_recv(bthost, conn, l2conn, data, len);
  1826. break;
  1827. case RFCOMM_UA:
  1828. rfcomm_ua_recv(bthost, conn, l2conn, data, len);
  1829. break;
  1830. case RFCOMM_DM:
  1831. rfcomm_dm_recv(bthost, conn, l2conn, data, len);
  1832. break;
  1833. case RFCOMM_UIH:
  1834. rfcomm_uih_recv(bthost, conn, l2conn, data, len);
  1835. break;
  1836. default:
  1837. bthost_debug(bthost, "Unknown frame type");
  1838. break;
  1839. }
  1840. }
  1841. static void process_acl(struct bthost *bthost, const void *data, uint16_t len)
  1842. {
  1843. const struct bt_hci_acl_hdr *acl_hdr = data;
  1844. const struct bt_l2cap_hdr *l2_hdr = data + sizeof(*acl_hdr);
  1845. uint16_t handle, cid, acl_len, l2_len;
  1846. struct cid_hook *hook;
  1847. struct btconn *conn;
  1848. struct l2conn *l2conn;
  1849. const void *l2_data;
  1850. if (len < sizeof(*acl_hdr) + sizeof(*l2_hdr))
  1851. return;
  1852. acl_len = le16_to_cpu(acl_hdr->dlen);
  1853. if (len != sizeof(*acl_hdr) + acl_len)
  1854. return;
  1855. handle = acl_handle(acl_hdr->handle);
  1856. conn = bthost_find_conn(bthost, handle);
  1857. if (!conn) {
  1858. bthost_debug(bthost, "ACL data for unknown handle 0x%04x",
  1859. handle);
  1860. return;
  1861. }
  1862. l2_len = le16_to_cpu(l2_hdr->len);
  1863. if (len - sizeof(*acl_hdr) != sizeof(*l2_hdr) + l2_len)
  1864. return;
  1865. l2_data = data + sizeof(*acl_hdr) + sizeof(*l2_hdr);
  1866. cid = le16_to_cpu(l2_hdr->cid);
  1867. hook = find_cid_hook(conn, cid);
  1868. if (hook) {
  1869. hook->func(l2_data, l2_len, hook->user_data);
  1870. return;
  1871. }
  1872. switch (cid) {
  1873. case 0x0001:
  1874. l2cap_sig(bthost, conn, l2_data, l2_len);
  1875. break;
  1876. case 0x0005:
  1877. l2cap_le_sig(bthost, conn, l2_data, l2_len);
  1878. break;
  1879. case 0x0006:
  1880. smp_data(conn->smp_data, l2_data, l2_len);
  1881. break;
  1882. case 0x0007:
  1883. smp_bredr_data(conn->smp_data, l2_data, l2_len);
  1884. break;
  1885. default:
  1886. l2conn = btconn_find_l2cap_conn_by_scid(conn, cid);
  1887. if (l2conn && l2conn->psm == 0x0003)
  1888. process_rfcomm(bthost, conn, l2conn, l2_data, l2_len);
  1889. else
  1890. bthost_debug(bthost,
  1891. "Packet for unknown CID 0x%04x (%u)",
  1892. cid, cid);
  1893. break;
  1894. }
  1895. }
  1896. void bthost_receive_h4(struct bthost *bthost, const void *data, uint16_t len)
  1897. {
  1898. uint8_t pkt_type;
  1899. if (!bthost)
  1900. return;
  1901. if (len < 1)
  1902. return;
  1903. util_hexdump('>', data, len, bthost->debug_callback,
  1904. bthost->debug_data);
  1905. pkt_type = ((const uint8_t *) data)[0];
  1906. switch (pkt_type) {
  1907. case BT_H4_EVT_PKT:
  1908. process_evt(bthost, data + 1, len - 1);
  1909. break;
  1910. case BT_H4_ACL_PKT:
  1911. process_acl(bthost, data + 1, len - 1);
  1912. break;
  1913. default:
  1914. bthost_debug(bthost, "Unsupported packet 0x%2.2x", pkt_type);
  1915. break;
  1916. }
  1917. }
  1918. void bthost_set_cmd_complete_cb(struct bthost *bthost,
  1919. bthost_cmd_complete_cb cb, void *user_data)
  1920. {
  1921. bthost->cmd_complete_cb = cb;
  1922. bthost->cmd_complete_data = user_data;
  1923. }
  1924. void bthost_set_connect_cb(struct bthost *bthost, bthost_new_conn_cb cb,
  1925. void *user_data)
  1926. {
  1927. bthost->new_conn_cb = cb;
  1928. bthost->new_conn_data = user_data;
  1929. }
  1930. void bthost_hci_connect(struct bthost *bthost, const uint8_t *bdaddr,
  1931. uint8_t addr_type)
  1932. {
  1933. bthost->conn_init = true;
  1934. if (addr_type == BDADDR_BREDR) {
  1935. struct bt_hci_cmd_create_conn cc;
  1936. memset(&cc, 0, sizeof(cc));
  1937. memcpy(cc.bdaddr, bdaddr, sizeof(cc.bdaddr));
  1938. send_command(bthost, BT_HCI_CMD_CREATE_CONN, &cc, sizeof(cc));
  1939. } else {
  1940. struct bt_hci_cmd_le_create_conn cc;
  1941. memset(&cc, 0, sizeof(cc));
  1942. memcpy(cc.peer_addr, bdaddr, sizeof(cc.peer_addr));
  1943. if (addr_type == BDADDR_LE_RANDOM)
  1944. cc.peer_addr_type = 0x01;
  1945. cc.scan_interval = cpu_to_le16(0x0060);
  1946. cc.scan_window = cpu_to_le16(0x0030);
  1947. cc.min_interval = cpu_to_le16(0x0028);
  1948. cc.max_interval = cpu_to_le16(0x0038);
  1949. cc.supv_timeout = cpu_to_le16(0x002a);
  1950. send_command(bthost, BT_HCI_CMD_LE_CREATE_CONN,
  1951. &cc, sizeof(cc));
  1952. }
  1953. }
  1954. void bthost_hci_ext_connect(struct bthost *bthost, const uint8_t *bdaddr,
  1955. uint8_t addr_type)
  1956. {
  1957. struct bt_hci_cmd_le_ext_create_conn *cc;
  1958. struct bt_hci_le_ext_create_conn *cp;
  1959. uint8_t buf[sizeof(*cc) + sizeof(*cp)];
  1960. cc = (void *)buf;
  1961. cp = (void *)cc->data;
  1962. bthost->conn_init = true;
  1963. memset(cc, 0, sizeof(*cc));
  1964. memset(cp, 0, sizeof(*cp));
  1965. memcpy(cc->peer_addr, bdaddr, sizeof(cc->peer_addr));
  1966. if (addr_type == BDADDR_LE_RANDOM)
  1967. cc->peer_addr_type = 0x01;
  1968. cc->phys = 0x01;
  1969. cp->scan_interval = cpu_to_le16(0x0060);
  1970. cp->scan_window = cpu_to_le16(0x0030);
  1971. cp->min_interval = cpu_to_le16(0x0028);
  1972. cp->max_interval = cpu_to_le16(0x0038);
  1973. cp->supv_timeout = cpu_to_le16(0x002a);
  1974. send_command(bthost, BT_HCI_CMD_LE_EXT_CREATE_CONN,
  1975. buf, sizeof(buf));
  1976. }
  1977. void bthost_hci_disconnect(struct bthost *bthost, uint16_t handle,
  1978. uint8_t reason)
  1979. {
  1980. struct bt_hci_cmd_disconnect disc;
  1981. disc.handle = cpu_to_le16(handle);
  1982. disc.reason = reason;
  1983. send_command(bthost, BT_HCI_CMD_DISCONNECT, &disc, sizeof(disc));
  1984. }
  1985. void bthost_write_scan_enable(struct bthost *bthost, uint8_t scan)
  1986. {
  1987. send_command(bthost, BT_HCI_CMD_WRITE_SCAN_ENABLE, &scan, 1);
  1988. }
  1989. void bthost_set_adv_data(struct bthost *bthost, const uint8_t *data,
  1990. uint8_t len)
  1991. {
  1992. struct bt_hci_cmd_le_set_adv_data adv_cp;
  1993. memset(adv_cp.data, 0, 31);
  1994. if (len) {
  1995. adv_cp.len = len;
  1996. memcpy(adv_cp.data, data, len);
  1997. }
  1998. send_command(bthost, BT_HCI_CMD_LE_SET_ADV_DATA, &adv_cp,
  1999. sizeof(adv_cp));
  2000. }
  2001. void bthost_set_ext_adv_data(struct bthost *bthost, const uint8_t *data,
  2002. uint8_t len)
  2003. {
  2004. struct bt_hci_cmd_le_set_ext_adv_data *adv_cp;
  2005. uint8_t buf[sizeof(*adv_cp) + 31];
  2006. adv_cp = (void *)buf;
  2007. memset(adv_cp, 0, sizeof(*adv_cp));
  2008. memset(adv_cp->data, 0, 31);
  2009. adv_cp->handle = 1;
  2010. adv_cp->operation = 0x03;
  2011. adv_cp->fragment_preference = 0x01;
  2012. if (len) {
  2013. adv_cp->data_len = len;
  2014. memcpy(adv_cp->data, data, len);
  2015. }
  2016. send_command(bthost, BT_HCI_CMD_LE_SET_EXT_ADV_DATA, buf,
  2017. sizeof(buf));
  2018. }
  2019. void bthost_set_adv_enable(struct bthost *bthost, uint8_t enable)
  2020. {
  2021. struct bt_hci_cmd_le_set_adv_parameters cp;
  2022. memset(&cp, 0, sizeof(cp));
  2023. send_command(bthost, BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
  2024. &cp, sizeof(cp));
  2025. send_command(bthost, BT_HCI_CMD_LE_SET_ADV_ENABLE, &enable, 1);
  2026. }
  2027. void bthost_set_ext_adv_params(struct bthost *bthost)
  2028. {
  2029. struct bt_hci_cmd_le_set_ext_adv_params cp;
  2030. memset(&cp, 0, sizeof(cp));
  2031. cp.handle = 0x01;
  2032. cp.evt_properties = cpu_to_le16(0x0013);
  2033. send_command(bthost, BT_HCI_CMD_LE_SET_EXT_ADV_PARAMS,
  2034. &cp, sizeof(cp));
  2035. }
  2036. void bthost_set_ext_adv_enable(struct bthost *bthost, uint8_t enable)
  2037. {
  2038. struct bt_hci_cmd_le_set_ext_adv_enable *cp_enable;
  2039. struct bt_hci_cmd_ext_adv_set *cp_set;
  2040. uint8_t cp[6];
  2041. memset(cp, 0, 6);
  2042. cp_enable = (struct bt_hci_cmd_le_set_ext_adv_enable *)cp;
  2043. cp_set = (struct bt_hci_cmd_ext_adv_set *)(cp + sizeof(*cp_enable));
  2044. cp_enable->enable = enable;
  2045. cp_enable->num_of_sets = 1;
  2046. cp_set->handle = 1;
  2047. send_command(bthost, BT_HCI_CMD_LE_SET_EXT_ADV_ENABLE, cp, 6);
  2048. }
  2049. void bthost_write_ssp_mode(struct bthost *bthost, uint8_t mode)
  2050. {
  2051. send_command(bthost, BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE, &mode, 1);
  2052. }
  2053. void bthost_write_le_host_supported(struct bthost *bthost, uint8_t mode)
  2054. {
  2055. struct bt_hci_cmd_write_le_host_supported cmd;
  2056. bthost->le = mode;
  2057. memset(&cmd, 0, sizeof(cmd));
  2058. cmd.supported = mode;
  2059. send_command(bthost, BT_HCI_CMD_WRITE_LE_HOST_SUPPORTED,
  2060. &cmd, sizeof(cmd));
  2061. }
  2062. bool bthost_bredr_capable(struct bthost *bthost)
  2063. {
  2064. return lmp_bredr_capable(bthost);
  2065. }
  2066. void bthost_request_auth(struct bthost *bthost, uint16_t handle)
  2067. {
  2068. struct btconn *conn;
  2069. conn = bthost_find_conn(bthost, handle);
  2070. if (!conn)
  2071. return;
  2072. if (conn->addr_type == BDADDR_BREDR) {
  2073. struct bt_hci_cmd_auth_requested cp;
  2074. cp.handle = cpu_to_le16(handle);
  2075. send_command(bthost, BT_HCI_CMD_AUTH_REQUESTED, &cp, sizeof(cp));
  2076. } else {
  2077. uint8_t auth_req = bthost->auth_req;
  2078. if (bthost->sc)
  2079. auth_req |= 0x08;
  2080. smp_pair(conn->smp_data, bthost->io_capability, auth_req);
  2081. }
  2082. }
  2083. void bthost_le_start_encrypt(struct bthost *bthost, uint16_t handle,
  2084. const uint8_t ltk[16])
  2085. {
  2086. struct bt_hci_cmd_le_start_encrypt cmd;
  2087. memset(&cmd, 0, sizeof(cmd));
  2088. cmd.handle = htobs(handle);
  2089. memcpy(cmd.ltk, ltk, 16);
  2090. send_command(bthost, BT_HCI_CMD_LE_START_ENCRYPT, &cmd, sizeof(cmd));
  2091. }
  2092. uint64_t bthost_conn_get_fixed_chan(struct bthost *bthost, uint16_t handle)
  2093. {
  2094. struct btconn *conn;
  2095. conn = bthost_find_conn(bthost, handle);
  2096. if (!conn)
  2097. return 0;
  2098. return conn->fixed_chan;
  2099. }
  2100. void bthost_add_l2cap_server(struct bthost *bthost, uint16_t psm,
  2101. bthost_l2cap_connect_cb func,
  2102. bthost_l2cap_disconnect_cb disconn_func,
  2103. void *user_data)
  2104. {
  2105. struct l2cap_conn_cb_data *data;
  2106. data = malloc(sizeof(struct l2cap_conn_cb_data));
  2107. if (!data)
  2108. return;
  2109. data->psm = psm;
  2110. data->user_data = user_data;
  2111. data->func = func;
  2112. data->disconn_func = disconn_func;
  2113. data->next = bthost->new_l2cap_conn_data;
  2114. bthost->new_l2cap_conn_data = data;
  2115. }
  2116. void bthost_set_sc_support(struct bthost *bthost, bool enable)
  2117. {
  2118. struct bt_hci_cmd_write_secure_conn_support cmd;
  2119. bthost->sc = enable;
  2120. if (!lmp_bredr_capable(bthost))
  2121. return;
  2122. cmd.support = enable;
  2123. send_command(bthost, BT_HCI_CMD_WRITE_SECURE_CONN_SUPPORT,
  2124. &cmd, sizeof(cmd));
  2125. }
  2126. void bthost_set_pin_code(struct bthost *bthost, const uint8_t *pin,
  2127. uint8_t pin_len)
  2128. {
  2129. memcpy(bthost->pin, pin, pin_len);
  2130. bthost->pin_len = pin_len;
  2131. }
  2132. void bthost_set_io_capability(struct bthost *bthost, uint8_t io_capability)
  2133. {
  2134. bthost->io_capability = io_capability;
  2135. }
  2136. uint8_t bthost_get_io_capability(struct bthost *bthost)
  2137. {
  2138. return bthost->io_capability;
  2139. }
  2140. void bthost_set_auth_req(struct bthost *bthost, uint8_t auth_req)
  2141. {
  2142. bthost->auth_req = auth_req;
  2143. }
  2144. uint8_t bthost_get_auth_req(struct bthost *bthost)
  2145. {
  2146. uint8_t auth_req = bthost->auth_req;
  2147. if (bthost->sc)
  2148. auth_req |= 0x08;
  2149. return auth_req;
  2150. }
  2151. void bthost_set_reject_user_confirm(struct bthost *bthost, bool reject)
  2152. {
  2153. bthost->reject_user_confirm = reject;
  2154. }
  2155. bool bthost_get_reject_user_confirm(struct bthost *bthost)
  2156. {
  2157. return bthost->reject_user_confirm;
  2158. }
  2159. void bthost_add_rfcomm_server(struct bthost *bthost, uint8_t channel,
  2160. bthost_rfcomm_connect_cb func, void *user_data)
  2161. {
  2162. struct rfcomm_conn_cb_data *data;
  2163. data = malloc(sizeof(struct rfcomm_conn_cb_data));
  2164. if (!data)
  2165. return;
  2166. data->channel = channel;
  2167. data->user_data = user_data;
  2168. data->func = func;
  2169. data->next = bthost->new_rfcomm_conn_data;
  2170. bthost->new_rfcomm_conn_data = data;
  2171. }
  2172. void bthost_start(struct bthost *bthost)
  2173. {
  2174. if (!bthost)
  2175. return;
  2176. bthost->ncmd = 1;
  2177. send_command(bthost, BT_HCI_CMD_RESET, NULL, 0);
  2178. send_command(bthost, BT_HCI_CMD_READ_LOCAL_FEATURES, NULL, 0);
  2179. send_command(bthost, BT_HCI_CMD_READ_BD_ADDR, NULL, 0);
  2180. }
  2181. bool bthost_connect_rfcomm(struct bthost *bthost, uint16_t handle,
  2182. uint8_t channel, bthost_rfcomm_connect_cb func,
  2183. void *user_data)
  2184. {
  2185. struct rfcomm_connection_data *data;
  2186. struct bt_l2cap_pdu_conn_req req;
  2187. struct btconn *conn;
  2188. if (bthost->rfcomm_conn_data)
  2189. return false;
  2190. conn = bthost_find_conn(bthost, handle);
  2191. if (!conn)
  2192. return false;
  2193. data = malloc(sizeof(struct rfcomm_connection_data));
  2194. if (!data)
  2195. return false;
  2196. data->channel = channel;
  2197. data->conn = conn;
  2198. data->cb = func;
  2199. data->user_data = user_data;
  2200. bthost->rfcomm_conn_data = data;
  2201. req.psm = cpu_to_le16(0x0003);
  2202. req.scid = cpu_to_le16(conn->next_cid++);
  2203. return bthost_l2cap_req(bthost, handle, BT_L2CAP_PDU_CONN_REQ,
  2204. &req, sizeof(req), NULL, NULL);
  2205. }
  2206. void bthost_add_rfcomm_chan_hook(struct bthost *bthost, uint16_t handle,
  2207. uint8_t channel,
  2208. bthost_rfcomm_chan_hook_func_t func,
  2209. void *user_data)
  2210. {
  2211. struct rfcomm_chan_hook *hook;
  2212. struct btconn *conn;
  2213. conn = bthost_find_conn(bthost, handle);
  2214. if (!conn)
  2215. return;
  2216. hook = malloc(sizeof(*hook));
  2217. if (!hook)
  2218. return;
  2219. memset(hook, 0, sizeof(*hook));
  2220. hook->channel = channel;
  2221. hook->func = func;
  2222. hook->user_data = user_data;
  2223. hook->next = conn->rfcomm_chan_hooks;
  2224. conn->rfcomm_chan_hooks = hook;
  2225. }
  2226. void bthost_send_rfcomm_data(struct bthost *bthost, uint16_t handle,
  2227. uint8_t channel, const void *data,
  2228. uint16_t len)
  2229. {
  2230. struct btconn *conn;
  2231. struct rcconn *rcconn;
  2232. struct rfcomm_hdr *hdr;
  2233. uint8_t *uih_frame;
  2234. uint16_t uih_len;
  2235. conn = bthost_find_conn(bthost, handle);
  2236. if (!conn)
  2237. return;
  2238. rcconn = btconn_find_rfcomm_conn_by_channel(conn, channel);
  2239. if (!rcconn)
  2240. return;
  2241. if (len > 127)
  2242. uih_len = len + sizeof(struct rfcomm_cmd) + sizeof(uint8_t);
  2243. else
  2244. uih_len = len + sizeof(struct rfcomm_cmd);
  2245. uih_frame = malloc(uih_len);
  2246. if (!uih_frame)
  2247. return;
  2248. hdr = (struct rfcomm_hdr *) uih_frame;
  2249. hdr->address = RFCOMM_ADDR(1, channel * 2);
  2250. hdr->control = RFCOMM_CTRL(RFCOMM_UIH, 0);
  2251. if (len > 127) {
  2252. hdr->length = RFCOMM_LEN16(cpu_to_le16(sizeof(*hdr) + len));
  2253. memcpy(uih_frame + sizeof(*hdr) + 1, data, len);
  2254. } else {
  2255. hdr->length = RFCOMM_LEN8(sizeof(*hdr) + len);
  2256. memcpy(uih_frame + sizeof(*hdr), data, len);
  2257. }
  2258. uih_frame[uih_len - 1] = rfcomm_fcs((void *)hdr);
  2259. send_acl(bthost, handle, rcconn->scid, uih_frame, uih_len);
  2260. free(uih_frame);
  2261. }