le.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/socket.h>
  19. #include <sys/un.h>
  20. #include <sys/uio.h>
  21. #include <time.h>
  22. #include "lib/bluetooth.h"
  23. #include "lib/hci.h"
  24. #include "src/shared/util.h"
  25. #include "src/shared/crypto.h"
  26. #include "src/shared/ecc.h"
  27. #include "src/shared/mainloop.h"
  28. #include "monitor/bt.h"
  29. #include "phy.h"
  30. #include "le.h"
  31. #define ACCEPT_LIST_SIZE 16
  32. #define RESOLV_LIST_SIZE 16
  33. #define SCAN_CACHE_SIZE 64
  34. #define DEFAULT_TX_LEN 0x001b
  35. #define DEFAULT_TX_TIME 0x0148
  36. #define MAX_TX_LEN 0x00fb
  37. #define MAX_TX_TIME 0x0848
  38. #define MAX_RX_LEN 0x00fb
  39. #define MAX_RX_TIME 0x0848
  40. #define DEFAULT_ALL_PHYS 0x03
  41. #define DEFAULT_TX_PHYS 0x00
  42. #define DEFAULT_RX_PHYS 0x00
  43. struct bt_peer {
  44. uint8_t addr_type;
  45. uint8_t addr[6];
  46. };
  47. struct bt_le {
  48. volatile int ref_count;
  49. int vhci_fd;
  50. struct bt_phy *phy;
  51. struct bt_crypto *crypto;
  52. int adv_timeout_id;
  53. int scan_timeout_id;
  54. bool scan_window_active;
  55. uint8_t scan_chan_idx;
  56. uint8_t event_mask[16];
  57. uint16_t manufacturer;
  58. uint8_t commands[64];
  59. uint8_t features[8];
  60. uint8_t bdaddr[6];
  61. uint8_t le_event_mask[8];
  62. uint16_t le_mtu;
  63. uint8_t le_max_pkt;
  64. uint8_t le_features[8];
  65. uint8_t le_random_addr[6];
  66. uint16_t le_adv_min_interval;
  67. uint16_t le_adv_max_interval;
  68. uint8_t le_adv_type;
  69. uint8_t le_adv_own_addr_type;
  70. uint8_t le_adv_direct_addr_type;
  71. uint8_t le_adv_direct_addr[6];
  72. uint8_t le_adv_channel_map;
  73. uint8_t le_adv_filter_policy;
  74. int8_t le_adv_tx_power;
  75. uint8_t le_adv_data_len;
  76. uint8_t le_adv_data[31];
  77. uint8_t le_scan_rsp_data_len;
  78. uint8_t le_scan_rsp_data[31];
  79. uint8_t le_adv_enable;
  80. uint8_t le_scan_type;
  81. uint16_t le_scan_interval;
  82. uint16_t le_scan_window;
  83. uint8_t le_scan_own_addr_type;
  84. uint8_t le_scan_filter_policy;
  85. uint8_t le_scan_enable;
  86. uint8_t le_scan_filter_dup;
  87. uint8_t le_conn_peer_addr_type;
  88. uint8_t le_conn_peer_addr[6];
  89. uint8_t le_conn_own_addr_type;
  90. uint8_t le_conn_enable;
  91. uint8_t le_accept_list_size;
  92. uint8_t le_accept_list[ACCEPT_LIST_SIZE][7];
  93. uint8_t le_states[8];
  94. uint16_t le_default_tx_len;
  95. uint16_t le_default_tx_time;
  96. uint8_t le_local_sk256[32];
  97. uint8_t le_resolv_list[RESOLV_LIST_SIZE][39];
  98. uint8_t le_resolv_list_size;
  99. uint8_t le_resolv_enable;
  100. uint16_t le_resolv_timeout;
  101. uint8_t le_default_all_phys;
  102. uint8_t le_default_tx_phys;
  103. uint8_t le_default_rx_phys;
  104. struct bt_peer scan_cache[SCAN_CACHE_SIZE];
  105. uint8_t scan_cache_count;
  106. };
  107. static bool is_in_accept_list(struct bt_le *hci, uint8_t addr_type,
  108. const uint8_t addr[6])
  109. {
  110. int i;
  111. for (i = 0; i < hci->le_accept_list_size; i++) {
  112. if (hci->le_accept_list[i][0] == addr_type &&
  113. !memcmp(&hci->le_accept_list[i][1], addr, 6))
  114. return true;
  115. }
  116. return false;
  117. }
  118. static void clear_accept_list(struct bt_le *hci)
  119. {
  120. int i;
  121. for (i = 0; i < hci->le_accept_list_size; i++) {
  122. hci->le_accept_list[i][0] = 0xff;
  123. memset(&hci->le_accept_list[i][1], 0, 6);
  124. }
  125. }
  126. static void resolve_peer_addr(struct bt_le *hci, uint8_t peer_addr_type,
  127. const uint8_t peer_addr[6],
  128. uint8_t *addr_type, uint8_t addr[6])
  129. {
  130. int i;
  131. if (!hci->le_resolv_enable)
  132. goto done;
  133. if (peer_addr_type != 0x01)
  134. goto done;
  135. if ((peer_addr[5] & 0xc0) != 0x40)
  136. goto done;
  137. for (i = 0; i < hci->le_resolv_list_size; i++) {
  138. uint8_t local_hash[3];
  139. if (hci->le_resolv_list[i][0] == 0xff)
  140. continue;
  141. bt_crypto_ah(hci->crypto, &hci->le_resolv_list[i][7],
  142. peer_addr + 3, local_hash);
  143. if (!memcmp(peer_addr, local_hash, 3)) {
  144. switch (hci->le_resolv_list[i][0]) {
  145. case 0x00:
  146. *addr_type = 0x02;
  147. break;
  148. case 0x01:
  149. *addr_type = 0x03;
  150. break;
  151. default:
  152. continue;
  153. }
  154. memcpy(addr, &hci->le_resolv_list[i][1], 6);
  155. return;
  156. }
  157. }
  158. done:
  159. *addr_type = peer_addr_type;
  160. memcpy(addr, peer_addr, 6);
  161. }
  162. static void clear_resolv_list(struct bt_le *hci)
  163. {
  164. int i;
  165. for (i = 0; i < hci->le_resolv_list_size; i++) {
  166. hci->le_resolv_list[i][0] = 0xff;
  167. memset(&hci->le_resolv_list[i][1], 0, 38);
  168. }
  169. }
  170. static void reset_defaults(struct bt_le *hci)
  171. {
  172. memset(hci->event_mask, 0, sizeof(hci->event_mask));
  173. hci->event_mask[0] |= 0x10; /* Disconnection Complete */
  174. hci->event_mask[0] |= 0x80; /* Encryption Change */
  175. hci->event_mask[1] |= 0x08; /* Read Remote Version Information Complete */
  176. hci->event_mask[1] |= 0x20; /* Command Complete */
  177. hci->event_mask[1] |= 0x40; /* Command Status */
  178. hci->event_mask[1] |= 0x80; /* Hardware Error */
  179. hci->event_mask[2] |= 0x04; /* Number of Completed Packets */
  180. hci->event_mask[3] |= 0x02; /* Data Buffer Overflow */
  181. hci->event_mask[5] |= 0x80; /* Encryption Key Refresh Complete */
  182. //hci->event_mask[7] |= 0x20; /* LE Meta Event */
  183. hci->manufacturer = 0x003f; /* Bluetooth SIG (63) */
  184. memset(hci->commands, 0, sizeof(hci->commands));
  185. hci->commands[0] |= 0x20; /* Disconnect */
  186. //hci->commands[2] |= 0x80; /* Read Remote Version Information */
  187. hci->commands[5] |= 0x40; /* Set Event Mask */
  188. hci->commands[5] |= 0x80; /* Reset */
  189. //hci->commands[10] |= 0x04; /* Read Transmit Power Level */
  190. hci->commands[14] |= 0x08; /* Read Local Version Information */
  191. hci->commands[14] |= 0x10; /* Read Local Supported Commands */
  192. hci->commands[14] |= 0x20; /* Read Local Supported Features */
  193. hci->commands[14] |= 0x80; /* Read Buffer Size */
  194. hci->commands[15] |= 0x02; /* Read BD ADDR */
  195. //hci->commands[15] |= 0x20; /* Read RSSI */
  196. hci->commands[22] |= 0x04; /* Set Event Mask Page 2 */
  197. hci->commands[25] |= 0x01; /* LE Set Event Mask */
  198. hci->commands[25] |= 0x02; /* LE Read Buffer Size */
  199. hci->commands[25] |= 0x04; /* LE Read Local Supported Features */
  200. hci->commands[25] |= 0x10; /* LE Set Random Address */
  201. hci->commands[25] |= 0x20; /* LE Set Advertising Parameters */
  202. hci->commands[25] |= 0x40; /* LE Read Advertising Channel TX Power */
  203. hci->commands[25] |= 0x80; /* LE Set Advertising Data */
  204. hci->commands[26] |= 0x01; /* LE Set Scan Response Data */
  205. hci->commands[26] |= 0x02; /* LE Set Advertise Enable */
  206. hci->commands[26] |= 0x04; /* LE Set Scan Parameters */
  207. hci->commands[26] |= 0x08; /* LE Set Scan Enable */
  208. hci->commands[26] |= 0x10; /* LE Create Connection */
  209. hci->commands[26] |= 0x20; /* LE Create Connection Cancel */
  210. hci->commands[26] |= 0x40; /* LE Read Accept List Size */
  211. hci->commands[26] |= 0x80; /* LE Clear Accept List */
  212. hci->commands[27] |= 0x01; /* LE Add Device To Accept List */
  213. hci->commands[27] |= 0x02; /* LE Remove Device From Accept List */
  214. //hci->commands[27] |= 0x04; /* LE Connection Update */
  215. //hci->commands[27] |= 0x08; /* LE Set Host Channel Classification */
  216. //hci->commands[27] |= 0x10; /* LE Read Channel Map */
  217. //hci->commands[27] |= 0x20; /* LE Read Remote Used Features */
  218. hci->commands[27] |= 0x40; /* LE Encrypt */
  219. hci->commands[27] |= 0x80; /* LE Rand */
  220. //hci->commands[28] |= 0x01; /* LE Start Encryption */
  221. //hci->commands[28] |= 0x02; /* LE Long Term Key Request Reply */
  222. //hci->commands[28] |= 0x04; /* LE Long Term Key Request Negative Reply */
  223. hci->commands[28] |= 0x08; /* LE Read Supported States */
  224. //hci->commands[28] |= 0x10; /* LE Receiver Test */
  225. //hci->commands[28] |= 0x20; /* LE Transmitter Test */
  226. //hci->commands[28] |= 0x40; /* LE Test End */
  227. //hci->commands[33] |= 0x10; /* LE Remote Connection Parameter Request Reply */
  228. //hci->commands[33] |= 0x20; /* LE Remote Connection Parameter Request Negative Reply */
  229. hci->commands[33] |= 0x40; /* LE Set Data Length */
  230. hci->commands[33] |= 0x80; /* LE Read Suggested Default Data Length */
  231. hci->commands[34] |= 0x01; /* LE Write Suggested Default Data Length */
  232. hci->commands[34] |= 0x02; /* LE Read Local P-256 Public Key */
  233. hci->commands[34] |= 0x04; /* LE Generate DHKey */
  234. hci->commands[34] |= 0x08; /* LE Add Device To Resolving List */
  235. hci->commands[34] |= 0x10; /* LE Remove Device From Resolving List */
  236. hci->commands[34] |= 0x20; /* LE Clear Resolving List */
  237. hci->commands[34] |= 0x40; /* LE Read Resolving List Size */
  238. hci->commands[34] |= 0x80; /* LE Read Peer Resolvable Address */
  239. hci->commands[35] |= 0x01; /* LE Read Local Resolvable Address */
  240. hci->commands[35] |= 0x02; /* LE Set Address Resolution Enable */
  241. hci->commands[35] |= 0x04; /* LE Set Resolvable Private Address Timeout */
  242. hci->commands[35] |= 0x08; /* LE Read Maximum Data Length */
  243. hci->commands[35] |= 0x10; /* LE Read PHY */
  244. hci->commands[35] |= 0x20; /* LE Set Default PHY */
  245. hci->commands[35] |= 0x40; /* LE Set PHY */
  246. //hci->commands[35] |= 0x80; /* LE Enhanced Receiver Test */
  247. //hci->commands[36] |= 0x01; /* LE Enhanced Transmitter Test */
  248. //hci->commands[36] |= 0x02; /* LE Set Advertising Set Random Address */
  249. //hci->commands[36] |= 0x04; /* LE Set Extended Advertising Parameters */
  250. //hci->commands[36] |= 0x08; /* LE Set Extended Advertising Data */
  251. //hci->commands[36] |= 0x10; /* LE Set Extended Scan Response Data */
  252. //hci->commands[36] |= 0x20; /* LE Set Extended Advertising Enable */
  253. //hci->commands[36] |= 0x40; /* LE Read Maximum Advertising Data Length */
  254. //hci->commands[36] |= 0x80; /* LE Read Number of Supported Advertising Sets */
  255. //hci->commands[37] |= 0x01; /* LE Remove Advertising Set */
  256. //hci->commands[37] |= 0x02; /* LE Clear Advertising Sets */
  257. //hci->commands[37] |= 0x04; /* LE Set Periodic Advertising Parameters */
  258. //hci->commands[37] |= 0x08; /* LE Set Periodic Advertising Data */
  259. //hci->commands[37] |= 0x10; /* LE Set Periodic Advertising Enable */
  260. //hci->commands[37] |= 0x20; /* LE Set Extended Scan Parameters */
  261. //hci->commands[37] |= 0x40; /* LE Set Extended Scan Enable */
  262. //hci->commands[37] |= 0x80; /* LE Extended Create Connection */
  263. //hci->commands[38] |= 0x01; /* LE Periodic Advertising Create Sync */
  264. //hci->commands[38] |= 0x02; /* LE Periodic Advertising Create Sync Cancel */
  265. //hci->commands[38] |= 0x04; /* LE Periodic Advertising Terminate Sync */
  266. //hci->commands[38] |= 0x08; /* LE Add Device To Periodic Advertiser List */
  267. //hci->commands[38] |= 0x10; /* LE Remove Device From Periodic Advertiser List */
  268. //hci->commands[38] |= 0x20; /* LE Clear Periodic Advertiser List */
  269. //hci->commands[38] |= 0x40; /* LE Read Periodic Advertiser List Size */
  270. //hci->commands[38] |= 0x80; /* LE Read Transmit Power */
  271. //hci->commands[39] |= 0x01; /* LE Read RF Path Compensation */
  272. //hci->commands[39] |= 0x02; /* LE Write RF Path Compensation */
  273. //hci->commands[39] |= 0x04; /* LE Set Privacy Mode */
  274. memset(hci->features, 0, sizeof(hci->features));
  275. hci->features[4] |= 0x20; /* BR/EDR Not Supported */
  276. hci->features[4] |= 0x40; /* LE Supported */
  277. memset(hci->bdaddr, 0, sizeof(hci->bdaddr));
  278. memset(hci->le_event_mask, 0, sizeof(hci->le_event_mask));
  279. hci->le_event_mask[0] |= 0x01; /* LE Connection Complete */
  280. hci->le_event_mask[0] |= 0x02; /* LE Advertising Report */
  281. hci->le_event_mask[0] |= 0x04; /* LE Connection Update Complete */
  282. hci->le_event_mask[0] |= 0x08; /* LE Read Remote Used Features Complete */
  283. hci->le_event_mask[0] |= 0x10; /* LE Long Term Key Request */
  284. //hci->le_event_mask[0] |= 0x20; /* LE Remote Connection Parameter Request */
  285. //hci->le_event_mask[0] |= 0x40; /* LE Data Length Change */
  286. //hci->le_event_mask[0] |= 0x80; /* LE Read Local P-256 Public Key Complete */
  287. //hci->le_event_mask[1] |= 0x01; /* LE Generate DHKey Complete */
  288. //hci->le_event_mask[1] |= 0x02; /* LE Enhanced Connection Complete */
  289. //hci->le_event_mask[1] |= 0x04; /* LE Direct Advertising Report */
  290. //hci->le_event_mask[1] |= 0x08; /* LE PHY Update Complete */
  291. //hci->le_event_mask[1] |= 0x10; /* LE Extended Advertising Report */
  292. //hci->le_event_mask[1] |= 0x20; /* LE Periodic Advertising Sync Established */
  293. //hci->le_event_mask[1] |= 0x40; /* LE Periodic Advertising Report */
  294. //hci->le_event_mask[1] |= 0x80; /* LE Periodic Advertising Sync Lost */
  295. //hci->le_event_mask[2] |= 0x01; /* LE Extended Scan Timeout */
  296. //hci->le_event_mask[2] |= 0x02; /* LE Extended Advertising Set Terminated */
  297. //hci->le_event_mask[2] |= 0x04; /* LE Scan Request Received */
  298. //hci->le_event_mask[2] |= 0x08; /* LE Channel Selection Algorithm */
  299. hci->le_mtu = 64;
  300. hci->le_max_pkt = 1;
  301. memset(hci->le_features, 0, sizeof(hci->le_features));
  302. hci->le_features[0] |= 0x01; /* LE Encryption */
  303. //hci->le_features[0] |= 0x02; /* Connection Parameter Request Procedure */
  304. //hci->le_features[0] |= 0x04; /* Extended Reject Indication */
  305. //hci->le_features[0] |= 0x08; /* Peripheral-initd Features Exchange */
  306. hci->le_features[0] |= 0x10; /* LE Ping */
  307. hci->le_features[0] |= 0x20; /* LE Data Packet Length Extension */
  308. hci->le_features[0] |= 0x40; /* LL Privacy */
  309. hci->le_features[0] |= 0x80; /* Extended Scanner Filter Policies */
  310. hci->le_features[1] |= 0x01; /* LE 2M PHY */
  311. hci->le_features[1] |= 0x02; /* Stable Modulation Index - Transmitter */
  312. hci->le_features[1] |= 0x04; /* Stable Modulation Index - Receiver */
  313. hci->le_features[1] |= 0x08; /* LE Coded PHY */
  314. //hci->le_features[1] |= 0x10; /* LE Extended Advertising */
  315. //hci->le_features[1] |= 0x20; /* LE Periodic Advertising */
  316. hci->le_features[1] |= 0x40; /* Channel Selection Algorithm #2 */
  317. hci->le_features[1] |= 0x80; /* LE Power Class 1 */
  318. hci->le_features[2] |= 0x01; /* Minimum Number of Used Channels Procedure */
  319. memset(hci->le_random_addr, 0, sizeof(hci->le_random_addr));
  320. hci->le_adv_min_interval = 0x0800;
  321. hci->le_adv_max_interval = 0x0800;
  322. hci->le_adv_type = 0x00;
  323. hci->le_adv_own_addr_type = 0x00;
  324. hci->le_adv_direct_addr_type = 0x00;
  325. memset(hci->le_adv_direct_addr, 0, 6);
  326. hci->le_adv_channel_map = 0x07;
  327. hci->le_adv_filter_policy = 0x00;
  328. hci->le_adv_tx_power = 0;
  329. memset(hci->le_adv_data, 0, sizeof(hci->le_adv_data));
  330. hci->le_adv_data_len = 0;
  331. memset(hci->le_scan_rsp_data, 0, sizeof(hci->le_scan_rsp_data));
  332. hci->le_scan_rsp_data_len = 0;
  333. hci->le_adv_enable = 0x00;
  334. hci->le_scan_type = 0x00; /* Passive Scanning */
  335. hci->le_scan_interval = 0x0010; /* 10 ms */
  336. hci->le_scan_window = 0x0010; /* 10 ms */
  337. hci->le_scan_own_addr_type = 0x00; /* Public Device Address */
  338. hci->le_scan_filter_policy = 0x00;
  339. hci->le_scan_enable = 0x00;
  340. hci->le_scan_filter_dup = 0x00;
  341. hci->le_conn_enable = 0x00;
  342. hci->le_accept_list_size = ACCEPT_LIST_SIZE;
  343. clear_accept_list(hci);
  344. memset(hci->le_states, 0, sizeof(hci->le_states));
  345. hci->le_states[0] |= 0x01; /* Non-connectable Advertising */
  346. hci->le_states[0] |= 0x02; /* Scannable Advertising */
  347. hci->le_states[0] |= 0x04; /* Connectable Advertising */
  348. hci->le_states[0] |= 0x08; /* High Duty Cycle Directed Advertising */
  349. hci->le_states[0] |= 0x10; /* Passive Scanning */
  350. hci->le_states[0] |= 0x20; /* Active Scanning */
  351. hci->le_states[0] |= 0x40; /* Initiating + Conn (Central Role) */
  352. hci->le_states[0] |= 0x80; /* Connection (Peripheral Role) */
  353. hci->le_states[1] |= 0x01; /* Passive Scanning +
  354. * Non-connectable Advertising */
  355. hci->le_default_tx_len = DEFAULT_TX_LEN;
  356. hci->le_default_tx_time = DEFAULT_TX_TIME;
  357. memset(hci->le_local_sk256, 0, sizeof(hci->le_local_sk256));
  358. hci->le_resolv_list_size = RESOLV_LIST_SIZE;
  359. clear_resolv_list(hci);
  360. hci->le_resolv_enable = 0x00;
  361. hci->le_resolv_timeout = 0x0384; /* 900 secs or 15 minutes */
  362. hci->le_default_all_phys = DEFAULT_ALL_PHYS;
  363. hci->le_default_tx_phys = DEFAULT_TX_PHYS;
  364. hci->le_default_rx_phys = DEFAULT_RX_PHYS;
  365. }
  366. static void clear_scan_cache(struct bt_le *hci)
  367. {
  368. memset(hci->scan_cache, 0, sizeof(hci->scan_cache));
  369. hci->scan_cache_count = 0;
  370. }
  371. static bool add_to_scan_cache(struct bt_le *hci, uint8_t addr_type,
  372. const uint8_t addr[6])
  373. {
  374. int i;
  375. for (i = 0; i < hci->scan_cache_count; i++) {
  376. if (hci->scan_cache[i].addr_type == addr_type &&
  377. !memcmp(hci->scan_cache[i].addr, addr, 6))
  378. return false;
  379. }
  380. if (hci->scan_cache_count >= SCAN_CACHE_SIZE)
  381. return true;
  382. hci->scan_cache[hci->scan_cache_count].addr_type = addr_type;
  383. memcpy(hci->scan_cache[hci->scan_cache_count].addr, addr, 6);
  384. hci->scan_cache_count++;
  385. return true;
  386. }
  387. static void send_event(struct bt_le *hci, uint8_t event,
  388. void *data, uint8_t size)
  389. {
  390. uint8_t type = BT_H4_EVT_PKT;
  391. struct bt_hci_evt_hdr hdr;
  392. struct iovec iov[3];
  393. int iovcnt;
  394. hdr.evt = event;
  395. hdr.plen = size;
  396. iov[0].iov_base = &type;
  397. iov[0].iov_len = 1;
  398. iov[1].iov_base = &hdr;
  399. iov[1].iov_len = sizeof(hdr);
  400. if (size > 0) {
  401. iov[2].iov_base = data;
  402. iov[2].iov_len = size;
  403. iovcnt = 3;
  404. } else
  405. iovcnt = 2;
  406. if (writev(hci->vhci_fd, iov, iovcnt) < 0)
  407. fprintf(stderr, "Write to /dev/vhci failed (%m)\n");
  408. }
  409. static void send_adv_pkt(struct bt_le *hci, uint8_t channel)
  410. {
  411. struct bt_phy_pkt_adv pkt;
  412. memset(&pkt, 0, sizeof(pkt));
  413. pkt.chan_idx = channel;
  414. pkt.pdu_type = hci->le_adv_type;
  415. pkt.tx_addr_type = hci->le_adv_own_addr_type;
  416. switch (hci->le_adv_own_addr_type) {
  417. case 0x00:
  418. case 0x02:
  419. memcpy(pkt.tx_addr, hci->bdaddr, 6);
  420. break;
  421. case 0x01:
  422. case 0x03:
  423. memcpy(pkt.tx_addr, hci->le_random_addr, 6);
  424. break;
  425. }
  426. pkt.rx_addr_type = hci->le_adv_direct_addr_type;
  427. memcpy(pkt.rx_addr, hci->le_adv_direct_addr, 6);
  428. pkt.adv_data_len = hci->le_adv_data_len;
  429. pkt.scan_rsp_len = hci->le_scan_rsp_data_len;
  430. bt_phy_send_vector(hci->phy, BT_PHY_PKT_ADV, &pkt, sizeof(pkt),
  431. hci->le_adv_data, pkt.adv_data_len,
  432. hci->le_scan_rsp_data, pkt.scan_rsp_len);
  433. }
  434. static unsigned int get_adv_delay(void)
  435. {
  436. /* The advertising delay is a pseudo-random value with a range
  437. * of 0 ms to 10 ms generated for each advertising event.
  438. */
  439. srand(time(NULL));
  440. return (rand() % 11);
  441. }
  442. static void adv_timeout_callback(int id, void *user_data)
  443. {
  444. struct bt_le *hci = user_data;
  445. unsigned int msec, min_msec, max_msec;
  446. if (hci->le_adv_channel_map & 0x01)
  447. send_adv_pkt(hci, 37);
  448. if (hci->le_adv_channel_map & 0x02)
  449. send_adv_pkt(hci, 38);
  450. if (hci->le_adv_channel_map & 0x04)
  451. send_adv_pkt(hci, 39);
  452. min_msec = (hci->le_adv_min_interval * 625) / 1000;
  453. max_msec = (hci->le_adv_max_interval * 625) / 1000;
  454. msec = ((min_msec + max_msec) / 2) + get_adv_delay();
  455. if (mainloop_modify_timeout(id, msec) < 0) {
  456. fprintf(stderr, "Setting advertising timeout failed\n");
  457. hci->le_adv_enable = 0x00;
  458. }
  459. }
  460. static bool start_adv(struct bt_le *hci)
  461. {
  462. unsigned int msec;
  463. if (hci->adv_timeout_id >= 0)
  464. return false;
  465. msec = ((hci->le_adv_min_interval * 625) / 1000) + get_adv_delay();
  466. hci->adv_timeout_id = mainloop_add_timeout(msec, adv_timeout_callback,
  467. hci, NULL);
  468. if (hci->adv_timeout_id < 0)
  469. return false;
  470. return true;
  471. }
  472. static bool stop_adv(struct bt_le *hci)
  473. {
  474. if (hci->adv_timeout_id < 0)
  475. return false;
  476. mainloop_remove_timeout(hci->adv_timeout_id);
  477. hci->adv_timeout_id = -1;
  478. return true;
  479. }
  480. static void scan_timeout_callback(int id, void *user_data)
  481. {
  482. struct bt_le *hci = user_data;
  483. unsigned int msec;
  484. if (hci->le_scan_window == hci->le_scan_interval ||
  485. !hci->scan_window_active) {
  486. msec = (hci->le_scan_window * 625) / 1000;
  487. hci->scan_window_active = true;
  488. hci->scan_chan_idx++;
  489. if (hci->scan_chan_idx > 39)
  490. hci->scan_chan_idx = 37;
  491. } else {
  492. msec = ((hci->le_scan_interval -
  493. hci->le_scan_window) * 625) / 1000;
  494. hci->scan_window_active = false;
  495. }
  496. if (mainloop_modify_timeout(id, msec) < 0) {
  497. fprintf(stderr, "Setting scanning timeout failed\n");
  498. hci->le_scan_enable = 0x00;
  499. hci->scan_window_active = false;
  500. }
  501. }
  502. static bool start_scan(struct bt_le *hci)
  503. {
  504. unsigned int msec;
  505. if (hci->scan_timeout_id >= 0)
  506. return false;
  507. msec = (hci->le_scan_window * 625) / 1000;
  508. hci->scan_timeout_id = mainloop_add_timeout(msec, scan_timeout_callback,
  509. hci, NULL);
  510. if (hci->scan_timeout_id < 0)
  511. return false;
  512. hci->scan_window_active = true;
  513. hci->scan_chan_idx = 37;
  514. return true;
  515. }
  516. static bool stop_scan(struct bt_le *hci)
  517. {
  518. if (hci->scan_timeout_id < 0)
  519. return false;
  520. mainloop_remove_timeout(hci->scan_timeout_id);
  521. hci->scan_timeout_id = -1;
  522. hci->scan_window_active = false;
  523. return true;
  524. }
  525. static void cmd_complete(struct bt_le *hci, uint16_t opcode,
  526. const void *data, uint8_t len)
  527. {
  528. struct bt_hci_evt_cmd_complete *cc;
  529. void *pkt_data;
  530. pkt_data = alloca(sizeof(*cc) + len);
  531. if (!pkt_data)
  532. return;
  533. cc = pkt_data;
  534. cc->ncmd = 0x01;
  535. cc->opcode = cpu_to_le16(opcode);
  536. if (len > 0)
  537. memcpy(pkt_data + sizeof(*cc), data, len);
  538. send_event(hci, BT_HCI_EVT_CMD_COMPLETE, pkt_data, sizeof(*cc) + len);
  539. }
  540. static void cmd_status(struct bt_le *hci, uint8_t status, uint16_t opcode)
  541. {
  542. struct bt_hci_evt_cmd_status cs;
  543. cs.status = status;
  544. cs.ncmd = 0x01;
  545. cs.opcode = cpu_to_le16(opcode);
  546. send_event(hci, BT_HCI_EVT_CMD_STATUS, &cs, sizeof(cs));
  547. }
  548. static void le_meta_event(struct bt_le *hci, uint8_t event,
  549. void *data, uint8_t len)
  550. {
  551. void *pkt_data;
  552. if (!(hci->event_mask[7] & 0x20))
  553. return;
  554. pkt_data = alloca(1 + len);
  555. if (!pkt_data)
  556. return;
  557. ((uint8_t *) pkt_data)[0] = event;
  558. if (len > 0)
  559. memcpy(pkt_data + 1, data, len);
  560. send_event(hci, BT_HCI_EVT_LE_META_EVENT, pkt_data, 1 + len);
  561. }
  562. static void cmd_disconnect(struct bt_le *hci, const void *data, uint8_t size)
  563. {
  564. cmd_status(hci, BT_HCI_ERR_UNKNOWN_CONN_ID, BT_HCI_CMD_DISCONNECT);
  565. }
  566. static void cmd_set_event_mask(struct bt_le *hci,
  567. const void *data, uint8_t size)
  568. {
  569. const struct bt_hci_cmd_set_event_mask *cmd = data;
  570. uint8_t status;
  571. memcpy(hci->event_mask, cmd->mask, 8);
  572. status = BT_HCI_ERR_SUCCESS;
  573. cmd_complete(hci, BT_HCI_CMD_SET_EVENT_MASK, &status, sizeof(status));
  574. }
  575. static void cmd_reset(struct bt_le *hci, const void *data, uint8_t size)
  576. {
  577. uint8_t status;
  578. stop_adv(hci);
  579. stop_scan(hci);
  580. reset_defaults(hci);
  581. status = BT_HCI_ERR_SUCCESS;
  582. cmd_complete(hci, BT_HCI_CMD_RESET, &status, sizeof(status));
  583. }
  584. static void cmd_set_event_mask_page2(struct bt_le *hci,
  585. const void *data, uint8_t size)
  586. {
  587. const struct bt_hci_cmd_set_event_mask_page2 *cmd = data;
  588. uint8_t status;
  589. memcpy(hci->event_mask + 8, cmd->mask, 8);
  590. status = BT_HCI_ERR_SUCCESS;
  591. cmd_complete(hci, BT_HCI_CMD_SET_EVENT_MASK_PAGE2,
  592. &status, sizeof(status));
  593. }
  594. static void cmd_read_local_version(struct bt_le *hci,
  595. const void *data, uint8_t size)
  596. {
  597. struct bt_hci_rsp_read_local_version rsp;
  598. rsp.status = BT_HCI_ERR_SUCCESS;
  599. rsp.hci_ver = 0x09;
  600. rsp.hci_rev = cpu_to_le16(0x0000);
  601. rsp.lmp_ver = 0x09;
  602. rsp.manufacturer = cpu_to_le16(hci->manufacturer);
  603. rsp.lmp_subver = cpu_to_le16(0x0000);
  604. cmd_complete(hci, BT_HCI_CMD_READ_LOCAL_VERSION, &rsp, sizeof(rsp));
  605. }
  606. static void cmd_read_local_commands(struct bt_le *hci,
  607. const void *data, uint8_t size)
  608. {
  609. struct bt_hci_rsp_read_local_commands rsp;
  610. rsp.status = BT_HCI_ERR_SUCCESS;
  611. memcpy(rsp.commands, hci->commands, 64);
  612. cmd_complete(hci, BT_HCI_CMD_READ_LOCAL_COMMANDS, &rsp, sizeof(rsp));
  613. }
  614. static void cmd_read_local_features(struct bt_le *hci,
  615. const void *data, uint8_t size)
  616. {
  617. struct bt_hci_rsp_read_local_features rsp;
  618. rsp.status = BT_HCI_ERR_SUCCESS;
  619. memcpy(rsp.features, hci->features, 8);
  620. cmd_complete(hci, BT_HCI_CMD_READ_LOCAL_FEATURES, &rsp, sizeof(rsp));
  621. }
  622. static void cmd_read_buffer_size(struct bt_le *hci,
  623. const void *data, uint8_t size)
  624. {
  625. struct bt_hci_rsp_read_buffer_size rsp;
  626. rsp.status = BT_HCI_ERR_SUCCESS;
  627. rsp.acl_mtu = cpu_to_le16(0x0000);
  628. rsp.sco_mtu = 0x00;
  629. rsp.acl_max_pkt = cpu_to_le16(0x0000);
  630. rsp.sco_max_pkt = cpu_to_le16(0x0000);
  631. cmd_complete(hci, BT_HCI_CMD_READ_BUFFER_SIZE, &rsp, sizeof(rsp));
  632. }
  633. static void cmd_read_bd_addr(struct bt_le *hci, const void *data, uint8_t size)
  634. {
  635. struct bt_hci_rsp_read_bd_addr rsp;
  636. rsp.status = BT_HCI_ERR_SUCCESS;
  637. memcpy(rsp.bdaddr, hci->bdaddr, 6);
  638. cmd_complete(hci, BT_HCI_CMD_READ_BD_ADDR, &rsp, sizeof(rsp));
  639. }
  640. static void cmd_le_set_event_mask(struct bt_le *hci,
  641. const void *data, uint8_t size)
  642. {
  643. const struct bt_hci_cmd_le_set_event_mask *cmd = data;
  644. uint8_t status;
  645. memcpy(hci->le_event_mask, cmd->mask, 8);
  646. status = BT_HCI_ERR_SUCCESS;
  647. cmd_complete(hci, BT_HCI_CMD_LE_SET_EVENT_MASK,
  648. &status, sizeof(status));
  649. }
  650. static void cmd_le_read_buffer_size(struct bt_le *hci,
  651. const void *data, uint8_t size)
  652. {
  653. struct bt_hci_rsp_le_read_buffer_size rsp;
  654. rsp.status = BT_HCI_ERR_SUCCESS;
  655. rsp.le_mtu = cpu_to_le16(hci->le_mtu);
  656. rsp.le_max_pkt = hci->le_max_pkt;
  657. cmd_complete(hci, BT_HCI_CMD_LE_READ_BUFFER_SIZE, &rsp, sizeof(rsp));
  658. }
  659. static void cmd_le_read_local_features(struct bt_le *hci,
  660. const void *data, uint8_t size)
  661. {
  662. struct bt_hci_rsp_le_read_local_features rsp;
  663. rsp.status = BT_HCI_ERR_SUCCESS;
  664. memcpy(rsp.features, hci->le_features, 8);
  665. cmd_complete(hci, BT_HCI_CMD_LE_READ_LOCAL_FEATURES,
  666. &rsp, sizeof(rsp));
  667. }
  668. static void cmd_le_set_random_address(struct bt_le *hci,
  669. const void *data, uint8_t size)
  670. {
  671. const struct bt_hci_cmd_le_set_random_address *cmd = data;
  672. uint8_t status;
  673. memcpy(hci->le_random_addr, cmd->addr, 6);
  674. status = BT_HCI_ERR_SUCCESS;
  675. cmd_complete(hci, BT_HCI_CMD_LE_SET_RANDOM_ADDRESS,
  676. &status, sizeof(status));
  677. }
  678. static void cmd_le_set_adv_parameters(struct bt_le *hci,
  679. const void *data, uint8_t size)
  680. {
  681. const struct bt_hci_cmd_le_set_adv_parameters *cmd = data;
  682. uint16_t min_interval, max_interval;
  683. uint8_t status;
  684. if (hci->le_adv_enable == 0x01) {
  685. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  686. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  687. return;
  688. }
  689. min_interval = le16_to_cpu(cmd->min_interval);
  690. max_interval = le16_to_cpu(cmd->max_interval);
  691. /* Valid range for advertising type is 0x00 to 0x03 */
  692. switch (cmd->type) {
  693. case 0x00: /* ADV_IND */
  694. /* Range for advertising interval min is 0x0020 to 0x4000 */
  695. if (min_interval < 0x0020 || min_interval > 0x4000) {
  696. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  697. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  698. return;
  699. }
  700. /* Range for advertising interval max is 0x0020 to 0x4000 */
  701. if (max_interval < 0x0020 || max_interval > 0x4000) {
  702. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  703. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  704. return;
  705. }
  706. /* Advertising interval max shall be less or equal */
  707. if (min_interval > max_interval) {
  708. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  709. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  710. return;
  711. }
  712. break;
  713. case 0x01: /* ADV_DIRECT_IND */
  714. /* Range for direct address type is 0x00 to 0x01 */
  715. if (cmd->direct_addr_type > 0x01) {
  716. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  717. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  718. return;
  719. }
  720. break;
  721. case 0x02: /* ADV_SCAN_IND */
  722. case 0x03: /* ADV_NONCONN_IND */
  723. /* Range for advertising interval min is 0x00a0 to 0x4000 */
  724. if (min_interval < 0x00a0 || min_interval > 0x4000) {
  725. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  726. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  727. return;
  728. }
  729. /* Range for advertising interval max is 0x00a0 to 0x4000 */
  730. if (max_interval < 0x00a0 || max_interval > 0x4000) {
  731. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  732. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  733. return;
  734. }
  735. /* Advertising interval min shall be less or equal */
  736. if (min_interval > max_interval) {
  737. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  738. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  739. return;
  740. }
  741. break;
  742. default:
  743. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  744. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  745. return;
  746. }
  747. /* Valid range for own address type is 0x00 to 0x03 */
  748. if (cmd->own_addr_type > 0x03) {
  749. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  750. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  751. return;
  752. }
  753. /* Valid range for advertising channel map is 0x01 to 0x07 */
  754. if (cmd->channel_map < 0x01 || cmd->channel_map > 0x07) {
  755. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  756. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  757. return;
  758. }
  759. /* Valid range for advertising filter policy is 0x00 to 0x03 */
  760. if (cmd->filter_policy > 0x03) {
  761. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  762. BT_HCI_CMD_LE_SET_ADV_PARAMETERS);
  763. return;
  764. }
  765. hci->le_adv_min_interval = min_interval;
  766. hci->le_adv_max_interval = max_interval;
  767. hci->le_adv_type = cmd->type;
  768. hci->le_adv_own_addr_type = cmd->own_addr_type;
  769. hci->le_adv_direct_addr_type = cmd->direct_addr_type;
  770. memcpy(hci->le_adv_direct_addr, cmd->direct_addr, 6);
  771. hci->le_adv_channel_map = cmd->channel_map;
  772. hci->le_adv_filter_policy = cmd->filter_policy;
  773. status = BT_HCI_ERR_SUCCESS;
  774. cmd_complete(hci, BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
  775. &status, sizeof(status));
  776. }
  777. static void cmd_le_read_adv_tx_power(struct bt_le *hci,
  778. const void *data, uint8_t size)
  779. {
  780. struct bt_hci_rsp_le_read_adv_tx_power rsp;
  781. rsp.status = BT_HCI_ERR_SUCCESS;
  782. rsp.level = hci->le_adv_tx_power;
  783. cmd_complete(hci, BT_HCI_CMD_LE_READ_ADV_TX_POWER, &rsp, sizeof(rsp));
  784. }
  785. static void cmd_le_set_adv_data(struct bt_le *hci,
  786. const void *data, uint8_t size)
  787. {
  788. const struct bt_hci_cmd_le_set_adv_data *cmd = data;
  789. uint8_t status;
  790. /* Valid range for advertising data length is 0x00 to 0x1f */
  791. if (cmd->len > 0x1f) {
  792. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  793. BT_HCI_CMD_LE_SET_ADV_DATA);
  794. return;
  795. }
  796. hci->le_adv_data_len = cmd->len;
  797. memcpy(hci->le_adv_data, cmd->data, 31);
  798. status = BT_HCI_ERR_SUCCESS;
  799. cmd_complete(hci, BT_HCI_CMD_LE_SET_ADV_DATA, &status, sizeof(status));
  800. }
  801. static void cmd_le_set_scan_rsp_data(struct bt_le *hci,
  802. const void *data, uint8_t size)
  803. {
  804. const struct bt_hci_cmd_le_set_scan_rsp_data *cmd = data;
  805. uint8_t status;
  806. /* Valid range for scan response data length is 0x00 to 0x1f */
  807. if (cmd->len > 0x1f) {
  808. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  809. BT_HCI_CMD_LE_SET_SCAN_RSP_DATA);
  810. return;
  811. }
  812. hci->le_scan_rsp_data_len = cmd->len;
  813. memcpy(hci->le_scan_rsp_data, cmd->data, 31);
  814. status = BT_HCI_ERR_SUCCESS;
  815. cmd_complete(hci, BT_HCI_CMD_LE_SET_SCAN_RSP_DATA,
  816. &status, sizeof(status));
  817. }
  818. static void cmd_le_set_adv_enable(struct bt_le *hci,
  819. const void *data, uint8_t size)
  820. {
  821. const struct bt_hci_cmd_le_set_adv_enable *cmd = data;
  822. uint8_t status;
  823. bool result;
  824. /* Valid range for advertising enable is 0x00 to 0x01 */
  825. if (cmd->enable > 0x01) {
  826. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  827. BT_HCI_CMD_LE_SET_ADV_ENABLE);
  828. return;
  829. }
  830. if (cmd->enable == hci->le_adv_enable) {
  831. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  832. BT_HCI_CMD_LE_SET_ADV_ENABLE);
  833. return;
  834. }
  835. if (cmd->enable == 0x01)
  836. result = start_adv(hci);
  837. else
  838. result = stop_adv(hci);
  839. if (!result) {
  840. cmd_status(hci, BT_HCI_ERR_UNSPECIFIED_ERROR,
  841. BT_HCI_CMD_LE_SET_ADV_ENABLE);
  842. return;
  843. }
  844. hci->le_adv_enable = cmd->enable;
  845. status = BT_HCI_ERR_SUCCESS;
  846. cmd_complete(hci, BT_HCI_CMD_LE_SET_ADV_ENABLE,
  847. &status, sizeof(status));
  848. }
  849. static void cmd_le_set_scan_parameters(struct bt_le *hci,
  850. const void *data, uint8_t size)
  851. {
  852. const struct bt_hci_cmd_le_set_scan_parameters *cmd = data;
  853. uint16_t interval, window;
  854. uint8_t status;
  855. if (hci->le_scan_enable == 0x01) {
  856. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  857. BT_HCI_CMD_LE_SET_SCAN_PARAMETERS);
  858. return;
  859. }
  860. interval = le16_to_cpu(cmd->interval);
  861. window = le16_to_cpu(cmd->window);
  862. /* Valid range for scan type is 0x00 to 0x01 */
  863. if (cmd->type > 0x01) {
  864. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  865. BT_HCI_CMD_LE_SET_SCAN_PARAMETERS);
  866. return;
  867. }
  868. /* Valid range for scan interval is 0x0004 to 0x4000 */
  869. if (interval < 0x0004 || interval > 0x4000) {
  870. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  871. BT_HCI_CMD_LE_SET_SCAN_PARAMETERS);
  872. return;
  873. }
  874. /* Valid range for scan window is 0x0004 to 0x4000 */
  875. if (window < 0x0004 || window > 0x4000) {
  876. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  877. BT_HCI_CMD_LE_SET_SCAN_PARAMETERS);
  878. return;
  879. }
  880. /* Scan window shall be less or equal than scan interval */
  881. if (window > interval) {
  882. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  883. BT_HCI_CMD_LE_SET_SCAN_PARAMETERS);
  884. return;
  885. }
  886. /* Valid range for own address type is 0x00 to 0x03 */
  887. if (cmd->own_addr_type > 0x03) {
  888. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  889. BT_HCI_CMD_LE_SET_SCAN_PARAMETERS);
  890. return;
  891. }
  892. /* Valid range for scanning filter policy is 0x00 to 0x03 */
  893. if (cmd->filter_policy > 0x03) {
  894. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  895. BT_HCI_CMD_LE_SET_SCAN_PARAMETERS);
  896. return;
  897. }
  898. hci->le_scan_type = cmd->type;
  899. hci->le_scan_interval = interval;
  900. hci->le_scan_window = window;
  901. hci->le_scan_own_addr_type = cmd->own_addr_type;
  902. hci->le_scan_filter_policy = cmd->filter_policy;
  903. status = BT_HCI_ERR_SUCCESS;
  904. cmd_complete(hci, BT_HCI_CMD_LE_SET_SCAN_PARAMETERS,
  905. &status, sizeof(status));
  906. }
  907. static void cmd_le_set_scan_enable(struct bt_le *hci,
  908. const void *data, uint8_t size)
  909. {
  910. const struct bt_hci_cmd_le_set_scan_enable *cmd = data;
  911. uint8_t status;
  912. bool result;
  913. /* Valid range for scan enable is 0x00 to 0x01 */
  914. if (cmd->enable > 0x01) {
  915. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  916. BT_HCI_CMD_LE_SET_SCAN_ENABLE);
  917. return;
  918. }
  919. /* Valid range for filter duplicates is 0x00 to 0x01 */
  920. if (cmd->filter_dup > 0x01) {
  921. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  922. BT_HCI_CMD_LE_SET_SCAN_ENABLE);
  923. return;
  924. }
  925. if (cmd->enable == hci->le_scan_enable) {
  926. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  927. BT_HCI_CMD_LE_SET_SCAN_ENABLE);
  928. return;
  929. }
  930. clear_scan_cache(hci);
  931. if (cmd->enable == 0x01)
  932. result = start_scan(hci);
  933. else
  934. result = stop_scan(hci);
  935. if (!result) {
  936. cmd_status(hci, BT_HCI_ERR_UNSPECIFIED_ERROR,
  937. BT_HCI_CMD_LE_SET_SCAN_ENABLE);
  938. return;
  939. }
  940. hci->le_scan_enable = cmd->enable;
  941. hci->le_scan_filter_dup = cmd->filter_dup;
  942. status = BT_HCI_ERR_SUCCESS;
  943. cmd_complete(hci, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
  944. &status, sizeof(status));
  945. }
  946. static void cmd_le_create_conn(struct bt_le *hci,
  947. const void *data, uint8_t size)
  948. {
  949. const struct bt_hci_cmd_le_create_conn *cmd = data;
  950. if (hci->le_conn_enable == 0x01) {
  951. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  952. BT_HCI_CMD_LE_CREATE_CONN);
  953. return;
  954. }
  955. /* Valid range for peer address type is 0x00 to 0x03 */
  956. if (cmd->peer_addr_type > 0x03) {
  957. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  958. BT_HCI_CMD_LE_CREATE_CONN);
  959. return;
  960. }
  961. /* Valid range for own address type is 0x00 to 0x03 */
  962. if (cmd->own_addr_type > 0x03) {
  963. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  964. BT_HCI_CMD_LE_CREATE_CONN);
  965. return;
  966. }
  967. hci->le_conn_peer_addr_type = cmd->peer_addr_type;
  968. memcpy(hci->le_conn_peer_addr, cmd->peer_addr, 6);
  969. hci->le_conn_own_addr_type = cmd->own_addr_type;
  970. hci->le_conn_enable = 0x01;
  971. cmd_status(hci, BT_HCI_ERR_SUCCESS, BT_HCI_CMD_LE_CREATE_CONN);
  972. }
  973. static void cmd_le_create_conn_cancel(struct bt_le *hci,
  974. const void *data, uint8_t size)
  975. {
  976. struct bt_hci_evt_le_conn_complete evt;
  977. uint8_t status;
  978. if (hci->le_conn_enable == 0x00) {
  979. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  980. BT_HCI_CMD_LE_CREATE_CONN_CANCEL);
  981. return;
  982. }
  983. hci->le_conn_enable = 0x00;
  984. status = BT_HCI_ERR_SUCCESS;
  985. cmd_complete(hci, BT_HCI_CMD_LE_CREATE_CONN_CANCEL,
  986. &status, sizeof(status));
  987. evt.status = BT_HCI_ERR_UNKNOWN_CONN_ID;
  988. evt.handle = cpu_to_le16(0x0000);
  989. evt.role = 0x00;
  990. evt.peer_addr_type = 0x00;
  991. memset(evt.peer_addr, 0, 6);
  992. evt.interval = cpu_to_le16(0x0000);
  993. evt.latency = cpu_to_le16(0x0000);
  994. evt.supv_timeout = cpu_to_le16(0x0000);
  995. evt.clock_accuracy = 0x00;
  996. if (hci->le_event_mask[0] & 0x01)
  997. le_meta_event(hci, BT_HCI_EVT_LE_CONN_COMPLETE,
  998. &evt, sizeof(evt));
  999. }
  1000. static void cmd_le_read_accept_list_size(struct bt_le *hci,
  1001. const void *data, uint8_t size)
  1002. {
  1003. struct bt_hci_rsp_le_read_accept_list_size rsp;
  1004. rsp.status = BT_HCI_ERR_SUCCESS;
  1005. rsp.size = hci->le_accept_list_size;
  1006. cmd_complete(hci, BT_HCI_CMD_LE_READ_ACCEPT_LIST_SIZE,
  1007. &rsp, sizeof(rsp));
  1008. }
  1009. static void cmd_le_clear_accept_list(struct bt_le *hci,
  1010. const void *data, uint8_t size)
  1011. {
  1012. uint8_t status;
  1013. clear_accept_list(hci);
  1014. status = BT_HCI_ERR_SUCCESS;
  1015. cmd_complete(hci, BT_HCI_CMD_LE_CLEAR_ACCEPT_LIST,
  1016. &status, sizeof(status));
  1017. }
  1018. static void cmd_le_add_to_accept_list(struct bt_le *hci,
  1019. const void *data, uint8_t size)
  1020. {
  1021. const struct bt_hci_cmd_le_add_to_accept_list *cmd = data;
  1022. uint8_t status;
  1023. bool exists = false;
  1024. int i, pos = -1;
  1025. /* Valid range for address type is 0x00 to 0x01 */
  1026. if (cmd->addr_type > 0x01) {
  1027. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1028. BT_HCI_CMD_LE_ADD_TO_ACCEPT_LIST);
  1029. return;
  1030. }
  1031. for (i = 0; i < hci->le_accept_list_size; i++) {
  1032. if (hci->le_accept_list[i][0] == cmd->addr_type &&
  1033. !memcmp(&hci->le_accept_list[i][1],
  1034. cmd->addr, 6)) {
  1035. exists = true;
  1036. break;
  1037. } else if (pos < 0 && hci->le_accept_list[i][0] == 0xff)
  1038. pos = i;
  1039. }
  1040. if (exists) {
  1041. cmd_status(hci, BT_HCI_ERR_UNSPECIFIED_ERROR,
  1042. BT_HCI_CMD_LE_ADD_TO_ACCEPT_LIST);
  1043. return;
  1044. }
  1045. if (pos < 0) {
  1046. cmd_status(hci, BT_HCI_ERR_MEM_CAPACITY_EXCEEDED,
  1047. BT_HCI_CMD_LE_ADD_TO_ACCEPT_LIST);
  1048. return;
  1049. }
  1050. hci->le_accept_list[pos][0] = cmd->addr_type;
  1051. memcpy(&hci->le_accept_list[pos][1], cmd->addr, 6);
  1052. status = BT_HCI_ERR_SUCCESS;
  1053. cmd_complete(hci, BT_HCI_CMD_LE_ADD_TO_ACCEPT_LIST,
  1054. &status, sizeof(status));
  1055. }
  1056. static void cmd_le_remove_from_accept_list(struct bt_le *hci,
  1057. const void *data, uint8_t size)
  1058. {
  1059. const struct bt_hci_cmd_le_remove_from_accept_list *cmd = data;
  1060. uint8_t status;
  1061. int i, pos = -1;
  1062. /* Valid range for address type is 0x00 to 0x01 */
  1063. if (cmd->addr_type > 0x01) {
  1064. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1065. BT_HCI_CMD_LE_REMOVE_FROM_ACCEPT_LIST);
  1066. return;
  1067. }
  1068. for (i = 0; i < hci->le_accept_list_size; i++) {
  1069. if (hci->le_accept_list[i][0] == cmd->addr_type &&
  1070. !memcmp(&hci->le_accept_list[i][1],
  1071. cmd->addr, 6)) {
  1072. pos = i;
  1073. break;
  1074. }
  1075. }
  1076. if (pos < 0) {
  1077. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1078. BT_HCI_CMD_LE_REMOVE_FROM_ACCEPT_LIST);
  1079. return;
  1080. }
  1081. hci->le_accept_list[pos][0] = 0xff;
  1082. memset(&hci->le_accept_list[pos][1], 0, 6);
  1083. status = BT_HCI_ERR_SUCCESS;
  1084. cmd_complete(hci, BT_HCI_CMD_LE_REMOVE_FROM_ACCEPT_LIST,
  1085. &status, sizeof(status));
  1086. }
  1087. static void cmd_le_encrypt(struct bt_le *hci, const void *data, uint8_t size)
  1088. {
  1089. const struct bt_hci_cmd_le_encrypt *cmd = data;
  1090. struct bt_hci_rsp_le_encrypt rsp;
  1091. if (!bt_crypto_e(hci->crypto, cmd->key, cmd->plaintext, rsp.data)) {
  1092. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  1093. BT_HCI_CMD_LE_ENCRYPT);
  1094. return;
  1095. }
  1096. rsp.status = BT_HCI_ERR_SUCCESS;
  1097. cmd_complete(hci, BT_HCI_CMD_LE_ENCRYPT, &rsp, sizeof(rsp));
  1098. }
  1099. static void cmd_le_rand(struct bt_le *hci, const void *data, uint8_t size)
  1100. {
  1101. struct bt_hci_rsp_le_rand rsp;
  1102. uint8_t value[8];
  1103. if (!bt_crypto_random_bytes(hci->crypto, value, 8)) {
  1104. cmd_status(hci, BT_HCI_ERR_COMMAND_DISALLOWED,
  1105. BT_HCI_CMD_LE_RAND);
  1106. return;
  1107. }
  1108. rsp.status = BT_HCI_ERR_SUCCESS;
  1109. memcpy(&rsp.number, value, 8);
  1110. cmd_complete(hci, BT_HCI_CMD_LE_RAND, &rsp, sizeof(rsp));
  1111. }
  1112. static void cmd_le_read_supported_states(struct bt_le *hci,
  1113. const void *data, uint8_t size)
  1114. {
  1115. struct bt_hci_rsp_le_read_supported_states rsp;
  1116. rsp.status = BT_HCI_ERR_SUCCESS;
  1117. memcpy(rsp.states, hci->le_states, 8);
  1118. cmd_complete(hci, BT_HCI_CMD_LE_READ_SUPPORTED_STATES,
  1119. &rsp, sizeof(rsp));
  1120. }
  1121. static void cmd_le_set_data_length(struct bt_le *hci,
  1122. const void *data, uint8_t size)
  1123. {
  1124. const struct bt_hci_cmd_le_set_data_length *cmd = data;
  1125. struct bt_hci_rsp_le_set_data_length rsp;
  1126. uint16_t handle, tx_len, tx_time;
  1127. handle = le16_to_cpu(cmd->handle);
  1128. tx_len = le16_to_cpu(cmd->tx_len);
  1129. tx_time = le16_to_cpu(cmd->tx_time);
  1130. /* Valid range for connection handle is 0x0000 to 0x0eff */
  1131. if (handle > 0x0eff) {
  1132. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1133. BT_HCI_CMD_LE_SET_DATA_LENGTH);
  1134. return;
  1135. }
  1136. /* Valid range for suggested max TX octets is 0x001b to 0x00fb */
  1137. if (tx_len < 0x001b || tx_len > 0x00fb) {
  1138. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1139. BT_HCI_CMD_LE_SET_DATA_LENGTH);
  1140. return;
  1141. }
  1142. /* Valid range for suggested max TX time is 0x0148 to 0x0848 */
  1143. if (tx_time < 0x0148 || tx_time > 0x0848) {
  1144. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1145. BT_HCI_CMD_LE_SET_DATA_LENGTH);
  1146. return;
  1147. }
  1148. /* Max TX len and time shall be less or equal supported */
  1149. if (tx_len > MAX_TX_LEN || tx_time > MAX_TX_TIME) {
  1150. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1151. BT_HCI_CMD_LE_SET_DATA_LENGTH);
  1152. return;
  1153. }
  1154. rsp.status = BT_HCI_ERR_SUCCESS;
  1155. rsp.handle = cpu_to_le16(handle);
  1156. cmd_complete(hci, BT_HCI_CMD_LE_SET_DATA_LENGTH, &rsp, sizeof(rsp));
  1157. }
  1158. static void cmd_le_read_default_data_length(struct bt_le *hci,
  1159. const void *data, uint8_t size)
  1160. {
  1161. struct bt_hci_rsp_le_read_default_data_length rsp;
  1162. rsp.status = BT_HCI_ERR_SUCCESS;
  1163. rsp.tx_len = cpu_to_le16(hci->le_default_tx_len);
  1164. rsp.tx_time = cpu_to_le16(hci->le_default_tx_time);
  1165. cmd_complete(hci, BT_HCI_CMD_LE_READ_DEFAULT_DATA_LENGTH,
  1166. &rsp, sizeof(rsp));
  1167. }
  1168. static void cmd_le_write_default_data_length(struct bt_le *hci,
  1169. const void *data, uint8_t size)
  1170. {
  1171. const struct bt_hci_cmd_le_write_default_data_length *cmd = data;
  1172. uint16_t tx_len, tx_time;
  1173. uint8_t status;
  1174. tx_len = le16_to_cpu(cmd->tx_len);
  1175. tx_time = le16_to_cpu(cmd->tx_time);
  1176. /* Valid range for suggested max TX octets is 0x001b to 0x00fb */
  1177. if (tx_len < 0x001b || tx_len > 0x00fb) {
  1178. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1179. BT_HCI_CMD_LE_WRITE_DEFAULT_DATA_LENGTH);
  1180. return;
  1181. }
  1182. /* Valid range for suggested max TX time is 0x0148 to 0x0848 */
  1183. if (tx_time < 0x0148 || tx_time > 0x0848) {
  1184. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1185. BT_HCI_CMD_LE_WRITE_DEFAULT_DATA_LENGTH);
  1186. return;
  1187. }
  1188. /* Suggested max TX len and time shall be less or equal supported */
  1189. if (tx_len > MAX_TX_LEN || tx_time > MAX_TX_TIME) {
  1190. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1191. BT_HCI_CMD_LE_WRITE_DEFAULT_DATA_LENGTH);
  1192. return;
  1193. }
  1194. hci->le_default_tx_len = tx_len;
  1195. hci->le_default_tx_time = tx_time;
  1196. status = BT_HCI_ERR_SUCCESS;
  1197. cmd_complete(hci, BT_HCI_CMD_LE_WRITE_DEFAULT_DATA_LENGTH,
  1198. &status, sizeof(status));
  1199. }
  1200. static void cmd_le_read_local_pk256(struct bt_le *hci,
  1201. const void *data, uint8_t size)
  1202. {
  1203. struct bt_hci_evt_le_read_local_pk256_complete evt;
  1204. cmd_status(hci, BT_HCI_ERR_SUCCESS, BT_HCI_CMD_LE_READ_LOCAL_PK256);
  1205. evt.status = BT_HCI_ERR_SUCCESS;
  1206. ecc_make_key(evt.local_pk256, hci->le_local_sk256);
  1207. if (hci->le_event_mask[0] & 0x80)
  1208. le_meta_event(hci, BT_HCI_EVT_LE_READ_LOCAL_PK256_COMPLETE,
  1209. &evt, sizeof(evt));
  1210. }
  1211. static void cmd_le_generate_dhkey(struct bt_le *hci,
  1212. const void *data, uint8_t size)
  1213. {
  1214. const struct bt_hci_cmd_le_generate_dhkey *cmd = data;
  1215. struct bt_hci_evt_le_generate_dhkey_complete evt;
  1216. cmd_status(hci, BT_HCI_ERR_SUCCESS, BT_HCI_CMD_LE_GENERATE_DHKEY);
  1217. evt.status = BT_HCI_ERR_SUCCESS;
  1218. ecdh_shared_secret(cmd->remote_pk256, hci->le_local_sk256, evt.dhkey);
  1219. if (hci->le_event_mask[1] & 0x01)
  1220. le_meta_event(hci, BT_HCI_EVT_LE_GENERATE_DHKEY_COMPLETE,
  1221. &evt, sizeof(evt));
  1222. }
  1223. static void cmd_le_add_to_resolv_list(struct bt_le *hci,
  1224. const void *data, uint8_t size)
  1225. {
  1226. const struct bt_hci_cmd_le_add_to_resolv_list *cmd = data;
  1227. uint8_t status;
  1228. bool exists = false;
  1229. int i, pos = -1;
  1230. /* Valid range for address type is 0x00 to 0x01 */
  1231. if (cmd->addr_type > 0x01) {
  1232. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1233. BT_HCI_CMD_LE_ADD_TO_RESOLV_LIST);
  1234. return;
  1235. }
  1236. for (i = 0; i < hci->le_resolv_list_size; i++) {
  1237. if (hci->le_resolv_list[i][0] == cmd->addr_type &&
  1238. !memcmp(&hci->le_resolv_list[i][1],
  1239. cmd->addr, 6)) {
  1240. exists = true;
  1241. break;
  1242. } else if (pos < 0 && hci->le_resolv_list[i][0] == 0xff)
  1243. pos = i;
  1244. }
  1245. if (exists) {
  1246. cmd_status(hci, BT_HCI_ERR_UNSPECIFIED_ERROR,
  1247. BT_HCI_CMD_LE_ADD_TO_RESOLV_LIST);
  1248. return;
  1249. }
  1250. if (pos < 0) {
  1251. cmd_status(hci, BT_HCI_ERR_MEM_CAPACITY_EXCEEDED,
  1252. BT_HCI_CMD_LE_ADD_TO_RESOLV_LIST);
  1253. return;
  1254. }
  1255. hci->le_resolv_list[pos][0] = cmd->addr_type;
  1256. memcpy(&hci->le_resolv_list[pos][1], cmd->addr, 6);
  1257. memcpy(&hci->le_resolv_list[pos][7], cmd->peer_irk, 16);
  1258. memcpy(&hci->le_resolv_list[pos][23], cmd->local_irk, 16);
  1259. status = BT_HCI_ERR_SUCCESS;
  1260. cmd_complete(hci, BT_HCI_CMD_LE_ADD_TO_RESOLV_LIST,
  1261. &status, sizeof(status));
  1262. }
  1263. static void cmd_le_remove_from_resolv_list(struct bt_le *hci,
  1264. const void *data, uint8_t size)
  1265. {
  1266. const struct bt_hci_cmd_le_remove_from_resolv_list *cmd = data;
  1267. uint8_t status;
  1268. int i, pos = -1;
  1269. /* Valid range for address type is 0x00 to 0x01 */
  1270. if (cmd->addr_type > 0x01) {
  1271. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1272. BT_HCI_CMD_LE_REMOVE_FROM_RESOLV_LIST);
  1273. return;
  1274. }
  1275. for (i = 0; i < hci->le_resolv_list_size; i++) {
  1276. if (hci->le_resolv_list[i][0] == cmd->addr_type &&
  1277. !memcmp(&hci->le_resolv_list[i][1],
  1278. cmd->addr, 6)) {
  1279. pos = i;
  1280. break;
  1281. }
  1282. }
  1283. if (pos < 0) {
  1284. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1285. BT_HCI_CMD_LE_REMOVE_FROM_RESOLV_LIST);
  1286. return;
  1287. }
  1288. hci->le_resolv_list[pos][0] = 0xff;
  1289. memset(&hci->le_resolv_list[pos][1], 0, 38);
  1290. status = BT_HCI_ERR_SUCCESS;
  1291. cmd_complete(hci, BT_HCI_CMD_LE_REMOVE_FROM_RESOLV_LIST,
  1292. &status, sizeof(status));
  1293. }
  1294. static void cmd_le_clear_resolv_list(struct bt_le *hci,
  1295. const void *data, uint8_t size)
  1296. {
  1297. uint8_t status;
  1298. clear_resolv_list(hci);
  1299. status = BT_HCI_ERR_SUCCESS;
  1300. cmd_complete(hci, BT_HCI_CMD_LE_CLEAR_RESOLV_LIST,
  1301. &status, sizeof(status));
  1302. }
  1303. static void cmd_le_read_resolv_list_size(struct bt_le *hci,
  1304. const void *data, uint8_t size)
  1305. {
  1306. struct bt_hci_rsp_le_read_resolv_list_size rsp;
  1307. rsp.status = BT_HCI_ERR_SUCCESS;
  1308. rsp.size = hci->le_resolv_list_size;
  1309. cmd_complete(hci, BT_HCI_CMD_LE_READ_RESOLV_LIST_SIZE,
  1310. &rsp, sizeof(rsp));
  1311. }
  1312. static void cmd_le_read_peer_resolv_addr(struct bt_le *hci,
  1313. const void *data, uint8_t size)
  1314. {
  1315. const struct bt_hci_cmd_le_read_peer_resolv_addr *cmd = data;
  1316. struct bt_hci_rsp_le_read_peer_resolv_addr rsp;
  1317. /* Valid range for address type is 0x00 to 0x01 */
  1318. if (cmd->addr_type > 0x01) {
  1319. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1320. BT_HCI_CMD_LE_READ_PEER_RESOLV_ADDR);
  1321. return;
  1322. }
  1323. rsp.status = BT_HCI_ERR_UNKNOWN_CONN_ID;
  1324. memset(rsp.addr, 0, 6);
  1325. cmd_complete(hci, BT_HCI_CMD_LE_READ_PEER_RESOLV_ADDR,
  1326. &rsp, sizeof(rsp));
  1327. }
  1328. static void cmd_le_read_local_resolv_addr(struct bt_le *hci,
  1329. const void *data, uint8_t size)
  1330. {
  1331. const struct bt_hci_cmd_le_read_local_resolv_addr *cmd = data;
  1332. struct bt_hci_rsp_le_read_local_resolv_addr rsp;
  1333. /* Valid range for address type is 0x00 to 0x01 */
  1334. if (cmd->addr_type > 0x01) {
  1335. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1336. BT_HCI_CMD_LE_READ_LOCAL_RESOLV_ADDR);
  1337. return;
  1338. }
  1339. rsp.status = BT_HCI_ERR_UNKNOWN_CONN_ID;
  1340. memset(rsp.addr, 0, 6);
  1341. cmd_complete(hci, BT_HCI_CMD_LE_READ_LOCAL_RESOLV_ADDR,
  1342. &rsp, sizeof(rsp));
  1343. }
  1344. static void cmd_le_set_resolv_enable(struct bt_le *hci,
  1345. const void *data, uint8_t size)
  1346. {
  1347. const struct bt_hci_cmd_le_set_resolv_enable *cmd = data;
  1348. uint8_t status;
  1349. /* Valid range for address resolution enable is 0x00 to 0x01 */
  1350. if (cmd->enable > 0x01) {
  1351. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1352. BT_HCI_CMD_LE_SET_RESOLV_ENABLE);
  1353. return;
  1354. }
  1355. hci->le_resolv_enable = cmd->enable;
  1356. status = BT_HCI_ERR_SUCCESS;
  1357. cmd_complete(hci, BT_HCI_CMD_LE_SET_RESOLV_ENABLE,
  1358. &status, sizeof(status));
  1359. }
  1360. static void cmd_le_set_resolv_timeout(struct bt_le *hci,
  1361. const void *data, uint8_t size)
  1362. {
  1363. const struct bt_hci_cmd_le_set_resolv_timeout *cmd = data;
  1364. uint16_t timeout;
  1365. uint8_t status;
  1366. timeout = le16_to_cpu(cmd->timeout);
  1367. /* Valid range for RPA timeout is 0x0001 to 0xa1b8 */
  1368. if (timeout < 0x0001 || timeout > 0xa1b8) {
  1369. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1370. BT_HCI_CMD_LE_SET_RESOLV_TIMEOUT);
  1371. return;
  1372. }
  1373. hci->le_resolv_timeout = timeout;
  1374. status = BT_HCI_ERR_SUCCESS;
  1375. cmd_complete(hci, BT_HCI_CMD_LE_SET_RESOLV_TIMEOUT,
  1376. &status, sizeof(status));
  1377. }
  1378. static void cmd_le_read_max_data_length(struct bt_le *hci,
  1379. const void *data, uint8_t size)
  1380. {
  1381. struct bt_hci_rsp_le_read_max_data_length rsp;
  1382. rsp.status = BT_HCI_ERR_SUCCESS;
  1383. rsp.max_tx_len = cpu_to_le16(MAX_TX_LEN);
  1384. rsp.max_tx_time = cpu_to_le16(MAX_TX_TIME);
  1385. rsp.max_rx_len = cpu_to_le16(MAX_RX_LEN);
  1386. rsp.max_rx_time = cpu_to_le16(MAX_RX_TIME);
  1387. cmd_complete(hci, BT_HCI_CMD_LE_READ_MAX_DATA_LENGTH,
  1388. &rsp, sizeof(rsp));
  1389. }
  1390. static void cmd_le_read_phy(struct bt_le *hci, const void *data, uint8_t size)
  1391. {
  1392. const struct bt_hci_cmd_le_read_phy *cmd = data;
  1393. struct bt_hci_rsp_le_read_phy rsp;
  1394. rsp.status = BT_HCI_ERR_SUCCESS;
  1395. rsp.handle = cmd->handle;
  1396. rsp.tx_phy = 0x01; /* LE 1M */
  1397. rsp.rx_phy = 0x01; /* LE 1M */
  1398. cmd_complete(hci, BT_HCI_CMD_LE_READ_PHY, &rsp, sizeof(rsp));
  1399. }
  1400. static void cmd_le_set_default_phy(struct bt_le *hci,
  1401. const void *data, uint8_t size)
  1402. {
  1403. const struct bt_hci_cmd_le_set_default_phy *cmd = data;
  1404. uint8_t status, tx_phys, rx_phys;
  1405. uint8_t phys_mask;
  1406. phys_mask = (true << 0) | ((!!(hci->le_features[1] & 0x01)) << 1)
  1407. | ((!!(hci->le_features[1] & 0x08)) << 2);
  1408. if (cmd->all_phys > 0x03) {
  1409. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1410. BT_HCI_CMD_LE_SET_DEFAULT_PHY);
  1411. return;
  1412. }
  1413. /* Transmitter PHYs preferences */
  1414. if (!(cmd->all_phys & 0x01)) {
  1415. /* At least one preference bit shall be set to 1 */
  1416. if (!cmd->tx_phys) {
  1417. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1418. BT_HCI_CMD_LE_SET_DEFAULT_PHY);
  1419. return;
  1420. }
  1421. if (cmd->tx_phys & ~phys_mask) {
  1422. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1423. BT_HCI_CMD_LE_SET_DEFAULT_PHY);
  1424. return;
  1425. }
  1426. tx_phys = cmd->tx_phys;
  1427. } else
  1428. tx_phys = 0x00;
  1429. /* Transmitter PHYs preferences */
  1430. if (!(cmd->all_phys & 0x02)) {
  1431. /* At least one preference bit shall be set to 1 */
  1432. if (!cmd->rx_phys) {
  1433. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1434. BT_HCI_CMD_LE_SET_DEFAULT_PHY);
  1435. return;
  1436. }
  1437. if (cmd->rx_phys & ~phys_mask) {
  1438. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS,
  1439. BT_HCI_CMD_LE_SET_DEFAULT_PHY);
  1440. return;
  1441. }
  1442. rx_phys = cmd->rx_phys;
  1443. } else
  1444. rx_phys = 0x00;
  1445. hci->le_default_all_phys = cmd->all_phys;
  1446. hci->le_default_tx_phys = tx_phys;
  1447. hci->le_default_rx_phys = rx_phys;
  1448. status = BT_HCI_ERR_SUCCESS;
  1449. cmd_complete(hci, BT_HCI_CMD_LE_SET_DEFAULT_PHY,
  1450. &status, sizeof(status));
  1451. }
  1452. static void cmd_le_set_phy(struct bt_le *hci, const void *data, uint8_t size)
  1453. {
  1454. const struct bt_hci_cmd_le_set_phy *cmd = data;
  1455. struct bt_hci_evt_le_phy_update_complete evt;
  1456. cmd_status(hci, BT_HCI_ERR_SUCCESS, BT_HCI_CMD_LE_SET_PHY);
  1457. evt.status = BT_HCI_ERR_SUCCESS;
  1458. evt.handle = cmd->handle;
  1459. evt.tx_phy = 0x01; /* LE 1M */
  1460. evt.rx_phy = 0x01; /* LE 1M */
  1461. if (hci->le_event_mask[1] & 0x08)
  1462. le_meta_event(hci, BT_HCI_EVT_LE_PHY_UPDATE_COMPLETE,
  1463. &evt, sizeof(evt));
  1464. }
  1465. static const struct {
  1466. uint16_t opcode;
  1467. void (*func) (struct bt_le *hci, const void *data, uint8_t size);
  1468. uint8_t size;
  1469. bool fixed;
  1470. } cmd_table[] = {
  1471. { BT_HCI_CMD_DISCONNECT, cmd_disconnect, 3, true },
  1472. { BT_HCI_CMD_SET_EVENT_MASK, cmd_set_event_mask, 8, true },
  1473. { BT_HCI_CMD_RESET, cmd_reset, 0, true },
  1474. { BT_HCI_CMD_SET_EVENT_MASK_PAGE2, cmd_set_event_mask_page2, 8, true },
  1475. { BT_HCI_CMD_READ_LOCAL_VERSION, cmd_read_local_version, 0, true },
  1476. { BT_HCI_CMD_READ_LOCAL_COMMANDS, cmd_read_local_commands, 0, true },
  1477. { BT_HCI_CMD_READ_LOCAL_FEATURES, cmd_read_local_features, 0, true },
  1478. { BT_HCI_CMD_READ_BUFFER_SIZE, cmd_read_buffer_size, 0, true },
  1479. { BT_HCI_CMD_READ_BD_ADDR, cmd_read_bd_addr, 0, true },
  1480. { BT_HCI_CMD_LE_SET_EVENT_MASK,
  1481. cmd_le_set_event_mask, 8, true },
  1482. { BT_HCI_CMD_LE_READ_BUFFER_SIZE,
  1483. cmd_le_read_buffer_size, 0, true },
  1484. { BT_HCI_CMD_LE_READ_LOCAL_FEATURES,
  1485. cmd_le_read_local_features, 0, true },
  1486. { BT_HCI_CMD_LE_SET_RANDOM_ADDRESS,
  1487. cmd_le_set_random_address, 6, true },
  1488. { BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
  1489. cmd_le_set_adv_parameters, 15, true },
  1490. { BT_HCI_CMD_LE_READ_ADV_TX_POWER,
  1491. cmd_le_read_adv_tx_power, 0, true },
  1492. { BT_HCI_CMD_LE_SET_ADV_DATA,
  1493. cmd_le_set_adv_data, 32, true },
  1494. { BT_HCI_CMD_LE_SET_SCAN_RSP_DATA,
  1495. cmd_le_set_scan_rsp_data, 32, true },
  1496. { BT_HCI_CMD_LE_SET_ADV_ENABLE,
  1497. cmd_le_set_adv_enable, 1, true },
  1498. { BT_HCI_CMD_LE_SET_SCAN_PARAMETERS,
  1499. cmd_le_set_scan_parameters, 7, true },
  1500. { BT_HCI_CMD_LE_SET_SCAN_ENABLE,
  1501. cmd_le_set_scan_enable, 2, true },
  1502. { BT_HCI_CMD_LE_CREATE_CONN,
  1503. cmd_le_create_conn, 25, true },
  1504. { BT_HCI_CMD_LE_CREATE_CONN_CANCEL,
  1505. cmd_le_create_conn_cancel, 0, true },
  1506. { BT_HCI_CMD_LE_READ_ACCEPT_LIST_SIZE,
  1507. cmd_le_read_accept_list_size, 0, true },
  1508. { BT_HCI_CMD_LE_CLEAR_ACCEPT_LIST,
  1509. cmd_le_clear_accept_list, 0, true },
  1510. { BT_HCI_CMD_LE_ADD_TO_ACCEPT_LIST,
  1511. cmd_le_add_to_accept_list, 7, true },
  1512. { BT_HCI_CMD_LE_REMOVE_FROM_ACCEPT_LIST,
  1513. cmd_le_remove_from_accept_list, 7, true },
  1514. { BT_HCI_CMD_LE_ENCRYPT, cmd_le_encrypt, 32, true },
  1515. { BT_HCI_CMD_LE_RAND, cmd_le_rand, 0, true },
  1516. { BT_HCI_CMD_LE_READ_SUPPORTED_STATES,
  1517. cmd_le_read_supported_states, 0, true },
  1518. { BT_HCI_CMD_LE_SET_DATA_LENGTH,
  1519. cmd_le_set_data_length, 6, true },
  1520. { BT_HCI_CMD_LE_READ_DEFAULT_DATA_LENGTH,
  1521. cmd_le_read_default_data_length, 0, true },
  1522. { BT_HCI_CMD_LE_WRITE_DEFAULT_DATA_LENGTH,
  1523. cmd_le_write_default_data_length, 4, true },
  1524. { BT_HCI_CMD_LE_READ_LOCAL_PK256,
  1525. cmd_le_read_local_pk256, 0, true },
  1526. { BT_HCI_CMD_LE_GENERATE_DHKEY,
  1527. cmd_le_generate_dhkey, 64, true },
  1528. { BT_HCI_CMD_LE_ADD_TO_RESOLV_LIST,
  1529. cmd_le_add_to_resolv_list, 39, true },
  1530. { BT_HCI_CMD_LE_REMOVE_FROM_RESOLV_LIST,
  1531. cmd_le_remove_from_resolv_list, 7, true },
  1532. { BT_HCI_CMD_LE_CLEAR_RESOLV_LIST,
  1533. cmd_le_clear_resolv_list, 0, true },
  1534. { BT_HCI_CMD_LE_READ_RESOLV_LIST_SIZE,
  1535. cmd_le_read_resolv_list_size, 0, true },
  1536. { BT_HCI_CMD_LE_READ_PEER_RESOLV_ADDR,
  1537. cmd_le_read_peer_resolv_addr, 7, true },
  1538. { BT_HCI_CMD_LE_READ_LOCAL_RESOLV_ADDR,
  1539. cmd_le_read_local_resolv_addr, 7, true },
  1540. { BT_HCI_CMD_LE_SET_RESOLV_ENABLE,
  1541. cmd_le_set_resolv_enable, 1, true },
  1542. { BT_HCI_CMD_LE_SET_RESOLV_TIMEOUT,
  1543. cmd_le_set_resolv_timeout, 2, true },
  1544. { BT_HCI_CMD_LE_READ_MAX_DATA_LENGTH,
  1545. cmd_le_read_max_data_length, 0, true },
  1546. { BT_HCI_CMD_LE_READ_PHY,
  1547. cmd_le_read_phy, 2, true },
  1548. { BT_HCI_CMD_LE_SET_DEFAULT_PHY,
  1549. cmd_le_set_default_phy, 3, true },
  1550. { BT_HCI_CMD_LE_SET_PHY,
  1551. cmd_le_set_phy, 7, true },
  1552. { }
  1553. };
  1554. static void process_command(struct bt_le *hci, const void *data, size_t size)
  1555. {
  1556. const struct bt_hci_cmd_hdr *hdr = data;
  1557. uint16_t opcode;
  1558. unsigned int i;
  1559. if (size < sizeof(*hdr))
  1560. return;
  1561. data += sizeof(*hdr);
  1562. size -= sizeof(*hdr);
  1563. opcode = le16_to_cpu(hdr->opcode);
  1564. if (hdr->plen != size) {
  1565. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS, opcode);
  1566. return;
  1567. }
  1568. for (i = 0; cmd_table[i].func; i++) {
  1569. if (cmd_table[i].opcode != opcode)
  1570. continue;
  1571. if ((cmd_table[i].fixed && size != cmd_table[i].size) ||
  1572. size < cmd_table[i].size) {
  1573. cmd_status(hci, BT_HCI_ERR_INVALID_PARAMETERS, opcode);
  1574. return;
  1575. }
  1576. cmd_table[i].func(hci, data, size);
  1577. return;
  1578. }
  1579. cmd_status(hci, BT_HCI_ERR_UNKNOWN_COMMAND, opcode);
  1580. }
  1581. static void vhci_read_callback(int fd, uint32_t events, void *user_data)
  1582. {
  1583. struct bt_le *hci = user_data;
  1584. unsigned char buf[4096];
  1585. ssize_t len;
  1586. if (events & (EPOLLERR | EPOLLHUP))
  1587. return;
  1588. len = read(hci->vhci_fd, buf, sizeof(buf));
  1589. if (len < 1)
  1590. return;
  1591. switch (buf[0]) {
  1592. case BT_H4_CMD_PKT:
  1593. process_command(hci, buf + 1, len - 1);
  1594. break;
  1595. }
  1596. }
  1597. static void phy_recv_callback(uint16_t type, const void *data,
  1598. size_t size, void *user_data)
  1599. {
  1600. struct bt_le *hci = user_data;
  1601. switch (type) {
  1602. case BT_PHY_PKT_ADV:
  1603. if (!(hci->le_event_mask[0] & 0x02))
  1604. return;
  1605. if (hci->scan_window_active) {
  1606. const struct bt_phy_pkt_adv *pkt = data;
  1607. uint8_t buf[100];
  1608. struct bt_hci_evt_le_adv_report *evt = (void *) buf;
  1609. uint8_t tx_addr_type, tx_addr[6];
  1610. if (hci->scan_chan_idx != pkt->chan_idx)
  1611. break;
  1612. resolve_peer_addr(hci, pkt->tx_addr_type, pkt->tx_addr,
  1613. &tx_addr_type, tx_addr);
  1614. if (hci->le_scan_filter_policy == 0x01 ||
  1615. hci->le_scan_filter_policy == 0x03) {
  1616. if (!is_in_accept_list(hci, tx_addr_type,
  1617. tx_addr))
  1618. break;
  1619. }
  1620. if (hci->le_scan_filter_dup) {
  1621. if (!add_to_scan_cache(hci, tx_addr_type,
  1622. tx_addr))
  1623. break;
  1624. }
  1625. memset(buf, 0, sizeof(buf));
  1626. evt->num_reports = 0x01;
  1627. evt->event_type = pkt->pdu_type;
  1628. evt->addr_type = tx_addr_type;
  1629. memcpy(evt->addr, tx_addr, 6);
  1630. evt->data_len = pkt->adv_data_len;
  1631. memcpy(buf + sizeof(*evt), data + sizeof(*pkt),
  1632. pkt->adv_data_len);
  1633. le_meta_event(hci, BT_HCI_EVT_LE_ADV_REPORT, buf,
  1634. sizeof(*evt) + pkt->adv_data_len + 1);
  1635. if (hci->le_scan_type == 0x00)
  1636. break;
  1637. memset(buf, 0, sizeof(buf));
  1638. evt->num_reports = 0x01;
  1639. evt->event_type = 0x04;
  1640. evt->addr_type = tx_addr_type;
  1641. memcpy(evt->addr, tx_addr, 6);
  1642. evt->data_len = pkt->scan_rsp_len;
  1643. memcpy(buf + sizeof(*evt), data + sizeof(*pkt) +
  1644. pkt->adv_data_len,
  1645. pkt->scan_rsp_len);
  1646. le_meta_event(hci, BT_HCI_EVT_LE_ADV_REPORT, buf,
  1647. sizeof(*evt) + pkt->scan_rsp_len + 1);
  1648. }
  1649. break;
  1650. }
  1651. }
  1652. struct bt_le *bt_le_new(void)
  1653. {
  1654. unsigned char setup_cmd[2];
  1655. struct bt_le *hci;
  1656. hci = calloc(1, sizeof(*hci));
  1657. if (!hci)
  1658. return NULL;
  1659. hci->adv_timeout_id = -1;
  1660. hci->scan_timeout_id = -1;
  1661. hci->scan_window_active = false;
  1662. reset_defaults(hci);
  1663. hci->vhci_fd = open("/dev/vhci", O_RDWR);
  1664. if (hci->vhci_fd < 0) {
  1665. free(hci);
  1666. return NULL;
  1667. }
  1668. setup_cmd[0] = HCI_VENDOR_PKT;
  1669. setup_cmd[1] = HCI_PRIMARY;
  1670. if (write(hci->vhci_fd, setup_cmd, sizeof(setup_cmd)) < 0) {
  1671. close(hci->vhci_fd);
  1672. free(hci);
  1673. return NULL;
  1674. }
  1675. mainloop_add_fd(hci->vhci_fd, EPOLLIN, vhci_read_callback, hci, NULL);
  1676. hci->phy = bt_phy_new();
  1677. hci->crypto = bt_crypto_new();
  1678. bt_phy_register(hci->phy, phy_recv_callback, hci);
  1679. return bt_le_ref(hci);
  1680. }
  1681. struct bt_le *bt_le_ref(struct bt_le *hci)
  1682. {
  1683. if (!hci)
  1684. return NULL;
  1685. __sync_fetch_and_add(&hci->ref_count, 1);
  1686. return hci;
  1687. }
  1688. void bt_le_unref(struct bt_le *hci)
  1689. {
  1690. if (!hci)
  1691. return;
  1692. if (__sync_sub_and_fetch(&hci->ref_count, 1))
  1693. return;
  1694. stop_adv(hci);
  1695. bt_crypto_unref(hci->crypto);
  1696. bt_phy_unref(hci->phy);
  1697. mainloop_remove_fd(hci->vhci_fd);
  1698. close(hci->vhci_fd);
  1699. free(hci);
  1700. }