bluetooth.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #ifndef _BLUETOOTH_H_
  2. #define _BLUETOOTH_H_
  3. #include "typedef.h"
  4. //LE
  5. #include "le/ble_data_types.h"
  6. #include "le/ble_api.h"
  7. #include "le/le_user.h"
  8. #include "le/att.h"
  9. #include "le/gatt.h"
  10. #include "le/sm.h"
  11. //Classic
  12. //Common
  13. #include "btstack_event.h"
  14. #define HCI_COMMAND_DATA_PACKET 0x01
  15. #define HCI_ACL_DATA_PACKET 0x02
  16. #define HCI_SCO_DATA_PACKET 0x03
  17. #define HCI_EVENT_PACKET 0x04
  18. // OGFs
  19. #define OGF_LINK_CONTROL 0x01
  20. #define OGF_LINK_POLICY 0x02
  21. #define OGF_CONTROLLER_BASEBAND 0x03
  22. #define OGF_INFORMATIONAL_PARAMETERS 0x04
  23. #define OGF_STATUS_PARAMETERS 0x05
  24. #define OGF_TESTING 0x06
  25. #define OGF_LE_CONTROLLER 0x08
  26. #define OGF_VENDOR_LE_CONTROLLER 0x3e
  27. #define OGF_VENDOR 0x3f
  28. // Events from host controller to host
  29. /**
  30. * @format 1
  31. * @param status
  32. */
  33. #define HCI_EVENT_INQUIRY_COMPLETE 0x01
  34. /**
  35. * @format 1B11132
  36. * @param num_responses
  37. * @param bd_addr
  38. * @param page_scan_repetition_mode
  39. * @param reserved1
  40. * @param reserved2
  41. * @param class_of_device
  42. * @param clock_offset
  43. */
  44. #define HCI_EVENT_INQUIRY_RESULT 0x02
  45. /**
  46. * @format 12B11
  47. * @param status
  48. * @param connection_handle
  49. * @param bd_addr
  50. * @param link_type
  51. * @param encryption_enabled
  52. */
  53. #define HCI_EVENT_CONNECTION_COMPLETE 0x03
  54. /**
  55. * @format B31
  56. * @param bd_addr
  57. * @param class_of_device
  58. * @param link_type
  59. */
  60. #define HCI_EVENT_CONNECTION_REQUEST 0x04
  61. /**
  62. * @format 121
  63. * @param status
  64. * @param connection_handle
  65. * @param reason
  66. */
  67. #define HCI_EVENT_DISCONNECTION_COMPLETE 0x05
  68. /**
  69. * @format 12
  70. * @param status
  71. * @param connection_handle
  72. */
  73. #define HCI_EVENT_AUTHENTICATION_COMPLETE 0x06
  74. /**
  75. * @format 1BN
  76. * @param status
  77. * @param bd_addr
  78. * @param remote_name
  79. */
  80. #define HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07
  81. /**
  82. * @format 121
  83. * @param status
  84. * @param connection_handle
  85. * @param encryption_enabled
  86. */
  87. #define HCI_EVENT_ENCRYPTION_CHANGE 0x08
  88. /**
  89. * @format 12
  90. * @param status
  91. * @param connection_handle
  92. */
  93. #define HCI_EVENT_CHANGE_CONNECTION_LINK_KEY_COMPLETE 0x09
  94. /**
  95. * @format 121
  96. * @param status
  97. * @param connection_handle
  98. * @param key_flag
  99. */
  100. #define HCI_EVENT_MASTER_LINK_KEY_COMPLETE 0x0A
  101. #define HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE 0x0B
  102. #define HCI_EVENT_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
  103. #define HCI_EVENT_QOS_SETUP_COMPLETE 0x0D
  104. /**
  105. * @format 12R
  106. * @param num_hci_command_packets
  107. * @param command_opcode
  108. * @param return_parameters
  109. */
  110. #define HCI_EVENT_COMMAND_COMPLETE 0x0E
  111. /**
  112. * @format 112
  113. * @param status
  114. * @param num_hci_command_packets
  115. * @param command_opcode
  116. */
  117. #define HCI_EVENT_COMMAND_STATUS 0x0F
  118. /**
  119. * @format 1
  120. * @param hardware_code
  121. */
  122. #define HCI_EVENT_HARDWARE_ERROR 0x10
  123. #define HCI_EVENT_FLUSH_OCCURRED 0x11
  124. /**
  125. * @format 1B1
  126. * @param status
  127. * @param bd_addr
  128. * @param role
  129. */
  130. #define HCI_EVENT_ROLE_CHANGE 0x12
  131. // TODO: number_of_handles 1, connection_handle[H*i], hc_num_of_completed_packets[2*i]
  132. #define HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS 0x13
  133. /**
  134. * @format 1H12
  135. * @param status
  136. * @param handle
  137. * @param mode
  138. * @param interval
  139. */
  140. #define HCI_EVENT_MODE_CHANGE_EVENT 0x14
  141. // TODO: num_keys, bd_addr[B*i], link_key[16 octets * i]
  142. #define HCI_EVENT_RETURN_LINK_KEYS 0x15
  143. /**
  144. * @format B
  145. * @param bd_addr
  146. */
  147. #define HCI_EVENT_PIN_CODE_REQUEST 0x16
  148. /**
  149. * @format B
  150. * @param bd_addr
  151. */
  152. #define HCI_EVENT_LINK_KEY_REQUEST 0x17
  153. // TODO: bd_addr B, link_key 16octets, key_type 1
  154. #define HCI_EVENT_LINK_KEY_NOTIFICATION 0x18
  155. /**
  156. * @format 1
  157. * @param link_type
  158. */
  159. #define HCI_EVENT_DATA_BUFFER_OVERFLOW 0x1A
  160. /**
  161. * @format H1
  162. * @param handle
  163. * @param lmp_max_slots
  164. */
  165. #define HCI_EVENT_MAX_SLOTS_CHANGED 0x1B
  166. /**
  167. * @format 1H2
  168. * @param status
  169. * @param handle
  170. * @param clock_offset
  171. */
  172. #define HCI_EVENT_READ_CLOCK_OFFSET_COMPLETE 0x1C
  173. /**
  174. * @format 1H2
  175. * @param status
  176. * @param handle
  177. * @param packet_types
  178. * @pnote packet_type is in plural to avoid clash with Java binding Packet.getPacketType()
  179. */
  180. #define HCI_EVENT_CONNECTION_PACKET_TYPE_CHANGED 0x1D
  181. #define HCI_EVENT_PAGE_SCAN_REPETITION_MODE_CHANGE 0x20
  182. /**
  183. * @format 1B11321
  184. * @param num_responses
  185. * @param bd_addr
  186. * @param page_scan_repetition_mode
  187. * @param reserved
  188. * @param class_of_device
  189. * @param clock_offset
  190. * @param rssi
  191. */
  192. #define HCI_EVENT_INQUIRY_RESULT_WITH_RSSI 0x22
  193. #define HCI_EVENT_READ_REMOTE_EXTERNED_FEATURES_COMPLETE 0x23
  194. /**
  195. * @format 1HB111221
  196. * @param status
  197. * @param handle
  198. * @param bd_addr
  199. * @param link_type
  200. * @param transmission_interval
  201. * @param retransmission_interval
  202. * @param rx_packet_length
  203. * @param tx_packet_length
  204. * @param air_mode
  205. */
  206. #define HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE 0x2C
  207. // TODO: serialize extended_inquiry_response and provide parser
  208. /**
  209. * @format 1B11321
  210. * @param num_responses
  211. * @param bd_addr
  212. * @param page_scan_repetition_mode
  213. * @param reserved
  214. * @param class_of_device
  215. * @param clock_offset
  216. * @param rssi
  217. */
  218. #define HCI_EVENT_EXTENDED_INQUIRY_RESPONSE 0x2F
  219. #define HCI_EVENT_EXTENDED_INQUIRY_RESULT 0x2F
  220. /**
  221. * @format 1H
  222. * @param status
  223. * @param handle
  224. */
  225. #define HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE 0x30
  226. #define HCI_EVENT_IO_CAPABILITY_REQUEST 0x31
  227. #define HCI_EVENT_IO_CAPABILITY_RESPONSE 0x32
  228. /**
  229. * @format B4
  230. * @param bd_addr
  231. * @param numeric_value
  232. */
  233. #define HCI_EVENT_USER_CONFIRMATION_REQUEST 0x33
  234. /**
  235. * @format B
  236. * @param bd_addr
  237. */
  238. #define HCI_EVENT_USER_PASSKEY_REQUEST 0x34
  239. /**
  240. * @format B
  241. * @param bd_addr
  242. */
  243. #define HCI_EVENT_REMOTE_OOB_DATA_REQUEST 0x35
  244. /**
  245. * @format 1B
  246. * @param status
  247. * @param bd_addr
  248. */
  249. #define HCI_EVENT_SIMPLE_PAIRING_COMPLETE 0x36
  250. #define HCI_EVENT_LINK_SUPPERVISION_TIMEOUT_CHANGE_EVENT 0x38
  251. #define HCI_EVENT_USER_PRESSKEY_NOTIFICATION 0x3B
  252. #define HCI_EVENT_REMOTE_KEYPRESS_NOTIFICATION 0x3C
  253. #define HCI_EVENT_REMOTE_SUPPORTED_FEATURES_NOTIFICATION 0x3D
  254. #define HCI_EVENT_LE_META 0x3E
  255. // last used HCI_EVENT in 2.1 is 0x3d
  256. // last used HCI_EVENT in 4.1 is 0x57
  257. #define HCI_EVENT_VENDOR_CONNECTION_COMPLETE 0xEF
  258. //event definition for new vendor sub event
  259. #define HCI_EVENT_VENDOR_META 0xF5
  260. #define HCI_SUBEVENT_VENDOR_TEST_MODE_CFG 0x01
  261. #define HCI_EVENT_VENDOR_FRE_OFFSET_TRIM 0xF6
  262. #define HCI_EVENT_VENDOR_ENCRY_COMPLETE 0xF7
  263. #define HCI_EVENT_VENDOR_NO_RECONN_ADDR 0xF8
  264. #define HCI_EVENT_VENDOR_SETUP_COMPLETE 0xF9
  265. #define HCI_EVENT_VENDOR_DUT 0xFA
  266. #define HCI_EVENT_VENDOR_OSC_INTERNAL 0xFB
  267. #define HCI_EVENT_VENDOR_FAST_TEST 0xFC
  268. #define HCI_EVENT_VENDOR_REMOTE_UPDATE 0xFD
  269. #define HCI_EVENT_VENDOR_REMOTE_TEST 0xFE
  270. #define HCI_EVENT_VENDOR_SPECIFIC 0xFF
  271. #define BTSTACK_EVENT_HCI_CONNECTIONS_DELETE 0x6D
  272. /**
  273. * @format 11H11B2221
  274. * @param subevent_code
  275. * @param status
  276. * @param connection_handle
  277. * @param role
  278. * @param peer_address_type
  279. * @param peer_address
  280. * @param conn_interval
  281. * @param conn_latency
  282. * @param supervision_timeout
  283. * @param master_clock_accuracy
  284. */
  285. #define HCI_SUBEVENT_LE_CONNECTION_COMPLETE 0x01
  286. // array of advertisements, not handled by event accessor generator
  287. #define HCI_SUBEVENT_LE_ADVERTISING_REPORT 0x02
  288. /**
  289. * @format 11H222
  290. * @param subevent_code
  291. * @param status
  292. * @param connection_handle
  293. * @param conn_interval
  294. * @param conn_latency
  295. * @param supervision_timeout
  296. */
  297. #define HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE 0x03
  298. /**
  299. * @format 1HD2
  300. * @param subevent_code
  301. * @param connection_handle
  302. * @param random_number
  303. * @param encryption_diversifier
  304. */
  305. #define HCI_SUBEVENT_LE_READ_REMOTE_USED_FEATURES_COMPLETE 0x04
  306. /**
  307. * @format 1HD2
  308. * @param subevent_code
  309. * @param connection_handle
  310. * @param random_number
  311. * @param encryption_diversifier
  312. */
  313. #define HCI_SUBEVENT_LE_LONG_TERM_KEY_REQUEST 0x05
  314. /**
  315. * @format 1H2222
  316. * @param subevent_code
  317. * @param connection_handle
  318. * @param interval_min
  319. * @param interval_max
  320. * @param latency
  321. * @param timeout
  322. */
  323. #define HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST 0x06
  324. /**
  325. * @format 1H2222
  326. * @param subevent_code
  327. * @param connection_handle
  328. * @param max_tx_octets
  329. * @param max_tx_time
  330. * @param max_rx_octets
  331. * @param max_rx_time
  332. */
  333. #define HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE 0x07
  334. /**
  335. * @format 11QQ
  336. * @param subevent_code
  337. * @param status
  338. * @param dhkey_x x coordinate of P256 public key
  339. * @param dhkey_y y coordinate of P256 public key
  340. */
  341. #define HCI_SUBEVENT_LE_READ_LOCAL_P256_PUBLIC_KEY_COMPLETE 0x08
  342. /**
  343. * @format 11Q
  344. * @param subevent_code
  345. * @param status
  346. * @param dhkey Diffie-Hellman key
  347. */
  348. #define HCI_SUBEVENT_LE_GENERATE_DHKEY_COMPLETE 0x09
  349. /**
  350. * @format 11H11BBB2221
  351. * @param subevent_code
  352. * @param status
  353. * @param connection_handle
  354. * @param role
  355. * @param peer_address_type
  356. * @param perr_addresss
  357. * @param local_resolvable_private_addres
  358. * @param peer_resolvable_private_addres
  359. * @param conn_interval
  360. * @param conn_latency
  361. * @param supervision_timeout
  362. * @param master_clock_accuracy
  363. */
  364. #define HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE 0x0A
  365. // array of advertisements, not handled by event accessor generator
  366. #define HCI_SUBEVENT_LE_DIRECT_ADVERTISING_REPORT 0x0B
  367. /**
  368. * @format 11211
  369. * @param subevent_code
  370. * @param status
  371. * @param connection_handle
  372. * @param TX_PHY
  373. * @param RX_PHY
  374. */
  375. #define HCI_SUBEVENT_LE_PHY_UPDATE_COMPLETE 0x0C
  376. // array of advertisements, not handled by event accessor generator
  377. #define HCI_SUBEVENT_LE_EXTENDED_ADVERTISING_REPORT 0x0D
  378. #define HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHED 0x0E
  379. /**
  380. * @format 1211111B
  381. * @param subevent_code
  382. * @param sync_handle
  383. * @param tx_power
  384. * @param rssi
  385. * @param unused
  386. * @param data_status
  387. * @param data_length
  388. * @param data
  389. */
  390. #define HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_REPORT 0x0F
  391. /**
  392. * @format 2
  393. * @param sync_handle
  394. */
  395. #define HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_LOST 0x10
  396. /**
  397. * @format
  398. */
  399. #define HCI_SUBEVENT_LE_SCAN_TIMEOUT 0x11
  400. /**
  401. * @format 1121
  402. * @param subevent_code
  403. * @param status
  404. * @param advertising_handle
  405. * @param connection_handle
  406. * @param num_completed_extended_advertising_events
  407. */
  408. #define HCI_SUBEVENT_LE_ADVERTISING_SET_TERMINATED 0x12
  409. /**
  410. * @format 1116
  411. * @param subevent_code
  412. * @param advertising_handle
  413. * @param scanner_address_type
  414. * @param scanner_address
  415. */
  416. #define HCI_SUBEVENT_LE_SCAN_REQUEST_RECEIVED 0x13
  417. /**
  418. * @format 21
  419. * @param subevent_code
  420. * @param connection_handle
  421. * @param channel_selection_algorithm
  422. */
  423. #define HCI_SUBEVENT_LE_CHANNEL_SELECTION_ALGORITHM 0x14
  424. #define HCI_SUBEVENT_LE_VENDOR_INTERVAL_COMPLETE 0xF0
  425. /**
  426. * @format
  427. */
  428. #define HCI_EVENT_ANCS_META 0xEA
  429. #define HCI_EVENT_AMS_META 0xF2
  430. typedef enum {
  431. AMS_RemoteCommandIDPlay = 0,
  432. AMS_RemoteCommandIDPause,
  433. AMS_RemoteCommandIDTogglePlayPause,
  434. AMS_RemoteCommandIDNextTrack,
  435. AMS_RemoteCommandIDPreviousTrack,
  436. AMS_RemoteCommandIDVolumeUp,
  437. AMS_RemoteCommandIDVolumeDown,
  438. AMS_RemoteCommandIDAdvanceRepeatMode,
  439. AMS_RemoteCommandIDAdvanceShuffleMode,
  440. AMS_RemoteCommandIDSkipForward,
  441. AMS_RemoteCommandIDSkipBackward,
  442. AMS_RemoteCommandIDLikeTrack,
  443. AMS_RemoteCommandIDDislikeTrack,
  444. AMS_RemoteCommandIDBookmarkTrack,
  445. } ams_request_cmd_e;
  446. bool ams_send_request_command(ams_request_cmd_e cmd_id);
  447. /*
  448. config_map:
  449. bit0--IDPlayer,bit1--IDQueue,bit2--IDTrack
  450. */
  451. #define AMS_IDPlayer_ENABLE BIT(0)
  452. #define AMS_IDQueue_ENABLE BIT(1)
  453. #define AMS_IDTrack_ENABLE BIT(2)
  454. bool ams_entity_attribute_config(int config_map);
  455. /**
  456. * compact HCI Command packet description
  457. */
  458. typedef struct {
  459. uint16_t opcode;
  460. const char *format;
  461. } hci_cmd_t;
  462. int hci_send_cmd(const hci_cmd_t *cmd, ...);
  463. extern const hci_cmd_t hci_reset;
  464. extern const hci_cmd_t hci_read_bd_addr;
  465. extern const hci_cmd_t hci_read_local_supported_features;
  466. extern const hci_cmd_t hci_read_buffer_size;
  467. extern const hci_cmd_t hci_read_local_supported_commands;
  468. extern const hci_cmd_t hci_read_local_version_information;
  469. extern const hci_cmd_t hci_read_le_host_supported;
  470. extern const hci_cmd_t hci_read_local_name;
  471. extern const hci_cmd_t hci_write_class_of_device;
  472. extern const hci_cmd_t hci_write_local_name;
  473. extern const hci_cmd_t hci_write_scan_enable;
  474. extern const hci_cmd_t hci_set_event_mask;
  475. extern const hci_cmd_t hci_le_add_device_to_white_list;
  476. extern const hci_cmd_t hci_le_clear_white_list;
  477. extern const hci_cmd_t hci_le_connection_update;
  478. extern const hci_cmd_t hci_le_create_connection;
  479. extern const hci_cmd_t hci_le_create_connection_cancel;
  480. extern const hci_cmd_t hci_le_encrypt;
  481. extern const hci_cmd_t hci_le_generate_dhkey;
  482. extern const hci_cmd_t hci_le_long_term_key_negative_reply;
  483. extern const hci_cmd_t hci_le_long_term_key_request_reply;
  484. extern const hci_cmd_t hci_le_rand;
  485. extern const hci_cmd_t hci_le_read_advertising_channel_tx_power;
  486. extern const hci_cmd_t hci_le_read_buffer_size;
  487. extern const hci_cmd_t hci_le_read_channel_map;
  488. extern const hci_cmd_t hci_le_read_local_p256_public_key;
  489. extern const hci_cmd_t hci_le_read_maximum_data_length;
  490. extern const hci_cmd_t hci_le_read_remote_used_features;
  491. extern const hci_cmd_t hci_le_read_suggested_default_data_length;
  492. extern const hci_cmd_t hci_le_read_supported_features;
  493. extern const hci_cmd_t hci_le_read_supported_states;
  494. extern const hci_cmd_t hci_le_read_white_list_size;
  495. extern const hci_cmd_t hci_le_receiver_test;
  496. extern const hci_cmd_t hci_le_remove_device_from_white_list;
  497. extern const hci_cmd_t hci_le_set_advertise_enable;
  498. extern const hci_cmd_t hci_le_set_advertising_data;
  499. extern const hci_cmd_t hci_le_set_advertising_parameters;
  500. extern const hci_cmd_t hci_le_set_data_length;
  501. extern const hci_cmd_t hci_le_set_event_mask;
  502. extern const hci_cmd_t hci_le_set_host_channel_classification;
  503. extern const hci_cmd_t hci_le_set_random_address;
  504. extern const hci_cmd_t hci_le_set_scan_enable;
  505. extern const hci_cmd_t hci_le_set_scan_parameters;
  506. extern const hci_cmd_t hci_le_set_scan_response_data;
  507. extern const hci_cmd_t hci_le_start_encryption;
  508. extern const hci_cmd_t hci_le_test_end;
  509. extern const hci_cmd_t hci_le_transmitter_test;
  510. extern const hci_cmd_t hci_le_write_suggested_default_data_length;
  511. extern const hci_cmd_t hci_le_set_phy;
  512. extern const hci_cmd_t hci_le_set_ext_advertising_parameters;
  513. extern const hci_cmd_t hci_le_set_ext_advertising_data;
  514. extern const hci_cmd_t hci_le_set_ext_advertise_enable;
  515. extern const hci_cmd_t hci_le_set_ext_scan_parameters;
  516. extern const hci_cmd_t hci_le_set_ext_scan_enable;
  517. enum VENDOR_REMOTE_TEST_VALUE {
  518. VENDOR_TEST_DISCONNECTED = 0,
  519. VENDOR_TEST_LEGACY_CONNECTED_BY_BT_CLASSIC,
  520. VENDOR_TEST_LEGACY_CONNECTED_BY_BLE,
  521. VENDOR_TEST_CONNECTED_WITH_TWS,
  522. };
  523. #endif