seos_hci.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. #include "seos_hci_i.h"
  2. #define TAG "SeosHci"
  3. #define OGF_LINK_CTL 0x01
  4. #define OCF_DISCONNECT 0x0006
  5. #define OGF_HOST_CTL 0x03
  6. #define OCF_SET_EVENT_MASK 0x0001
  7. #define OCF_RESET 0x0003
  8. #define OCF_READ_LE_HOST_SUPPORTED 0x006c
  9. #define OCF_WRITE_LE_HOST_SUPPORTED 0x006d
  10. #define OGF_INFO_PARAM 0x04
  11. #define OCF_READ_LOCAL_VERSION 0x0001
  12. #define OCF_READ_BUFFER_SIZE 0x0005
  13. #define OCF_READ_BD_ADDR 0x0009
  14. #define OGF_STATUS_PARAM 0x05
  15. #define OCF_READ_RSSI 0x0005
  16. #define OGF_LE_CTL 0x08
  17. #define OCF_LE_SET_EVENT_MASK 0x0001
  18. #define OCF_LE_READ_BUFFER_SIZE 0x0002
  19. #define OCF_LE_READ_LOCAL_SUPPORTED_FEATURES 0x0003
  20. #define OCF_LE_SET_RANDOM_ADDRESS 0x0005
  21. #define OCF_LE_SET_ADVERTISING_PARAMETERS 0x0006
  22. #define OCF_LE_SET_ADVERTISING_DATA 0x0008
  23. #define OCF_LE_SET_SCAN_RESPONSE_DATA 0x0009
  24. #define OCF_LE_SET_ADVERTISE_ENABLE 0x000a
  25. #define OCF_LE_SET_SCAN_PARAMETERS 0x000b
  26. #define OCF_LE_SET_SCAN_ENABLE 0x000c
  27. #define OCF_LE_CREATE_CONNECTION 0x000d
  28. #define OGF_VENDOR_CTL 0x3F
  29. #define OCF_LE_LTK_NEG_REPLY 0x001B
  30. /* Obtain OGF from OpCode */
  31. #define BT_OGF(opcode) (((opcode) >> 10) & 0x3f)
  32. /* Obtain OCF from OpCode */
  33. #define BT_OCF(opcode) ((opcode) & 0x3FF)
  34. #define BT_OP(ogf, ocf) ((ocf) | ((ogf) << 10))
  35. #define BT_HCI_EVT_DISCONN_COMPLETE 0x05 // HCI_Disconnection_Complete
  36. #define BT_HCI_EVT_QOS_SETUP_COMPLETE 0x0d
  37. #define BT_HCI_EVT_CMD_COMPLETE 0x0e
  38. #define BT_HCI_EVT_CMD_STATUS 0x0f
  39. #define BT_HCI_EVT_NUM_COMPLETED_PACKETS 0x13
  40. #define BT_HCI_EVT_LE_META 0x3e // HCI_LE_Connection_Complete
  41. #define HCI_LE_CONNECTION_COMPLETE 0x01
  42. #define HCI_LE_ADVERTISING_REPORT 0x02
  43. // Consider making this an enum that shifts a bit in the apropriate amount
  44. #define CAP_TWIST_AND_GO 0x02
  45. #define CAP_ALLOW_TAP 0x04
  46. #define CAP_APP_SPECIFIC 0x08
  47. #define CAP_ENHANCED_TAP 0x40
  48. static uint8_t seos_reader_service_backwards[] =
  49. {0x02, 0x00, 0x00, 0x7a, 0x17, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x98, 0x00, 0x00};
  50. static uint8_t seos_cred_service_backwards[] =
  51. {0x02, 0x00, 0x00, 0x7a, 0x17, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x01, 0x98, 0x00, 0x00};
  52. // Occationally scan stop's completion doesn't get caught.
  53. // Use the timer callback to call it again
  54. void seos_hci_timer(void* context) {
  55. FURI_LOG_I(TAG, "RUN TIMER");
  56. SeosHci* seos_hci = (SeosHci*)context;
  57. if(seos_hci->mode == BLE_PERIPHERAL) {
  58. seos_hci_enable_advertising(seos_hci, seos_hci->adv_status);
  59. } else if(seos_hci->mode == BLE_CENTRAL) {
  60. seos_hci_set_scan(seos_hci, seos_hci->scan_status);
  61. }
  62. }
  63. SeosHci* seos_hci_alloc(Seos* seos) {
  64. SeosHci* seos_hci = malloc(sizeof(SeosHci));
  65. memset(seos_hci, 0, sizeof(SeosHci));
  66. seos_hci->device_found = false;
  67. seos_hci->connection_handle = 0;
  68. seos_hci->seos = seos;
  69. seos_hci->seos_hci_h5 = seos_hci_h5_alloc();
  70. seos_hci->timer = furi_timer_alloc(seos_hci_timer, FuriTimerTypeOnce, seos_hci);
  71. seos_hci_h5_set_init_callback(seos_hci->seos_hci_h5, seos_hci_init, seos_hci);
  72. seos_hci_h5_set_receive_callback(seos_hci->seos_hci_h5, seos_hci_recv, seos_hci);
  73. return seos_hci;
  74. }
  75. void seos_hci_free(SeosHci* seos_hci) {
  76. furi_assert(seos_hci);
  77. furi_timer_free(seos_hci->timer);
  78. seos_hci_h5_set_init_callback(seos_hci->seos_hci_h5, NULL, NULL);
  79. seos_hci_h5_set_receive_callback(seos_hci->seos_hci_h5, NULL, NULL);
  80. seos_hci_h5_free(seos_hci->seos_hci_h5);
  81. free(seos_hci);
  82. }
  83. void seos_hci_start(SeosHci* seos_hci, BleMode mode, FlowMode flow_mode) {
  84. seos_hci->device_found = false;
  85. seos_hci->connection_handle = 0;
  86. seos_hci->flow_mode = flow_mode;
  87. seos_hci->mode = mode;
  88. seos_hci_h5_start(seos_hci->seos_hci_h5);
  89. }
  90. void seos_hci_stop(SeosHci* seos_hci) {
  91. if(seos_hci->connection_handle > 0) {
  92. uint16_t opcode = BT_OP(OGF_LINK_CTL, OCF_DISCONNECT);
  93. BitBuffer* disconnect = bit_buffer_alloc(5);
  94. bit_buffer_append_bytes(disconnect, (uint8_t*)&opcode, sizeof(opcode));
  95. bit_buffer_append_bytes(
  96. disconnect,
  97. (uint8_t*)&seos_hci->connection_handle,
  98. sizeof(seos_hci->connection_handle));
  99. bit_buffer_append_byte(disconnect, 0x00);
  100. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, disconnect);
  101. }
  102. seos_hci->device_found = false;
  103. seos_hci->connection_handle = 0;
  104. seos_hci_h5_stop(seos_hci->seos_hci_h5);
  105. if(seos_hci->mode == BLE_PERIPHERAL) {
  106. if(seos_hci->adv_status) {
  107. seos_hci_enable_advertising(seos_hci, false);
  108. }
  109. } else if(seos_hci->mode == BLE_CENTRAL) {
  110. if(seos_hci->scan_status) {
  111. seos_hci_set_scan(seos_hci, false);
  112. }
  113. }
  114. if(furi_timer_is_running(seos_hci->timer)) {
  115. FURI_LOG_D(TAG, "clear timer");
  116. furi_timer_stop(seos_hci->timer);
  117. }
  118. }
  119. void seos_hci_handle_event_cmd_complete_ogf_host(SeosHci* seos_hci, uint16_t OCF, BitBuffer* frame) {
  120. UNUSED(frame);
  121. BitBuffer* message = bit_buffer_alloc(128);
  122. switch(OCF) {
  123. case OCF_RESET:
  124. uint8_t le_read_local_supported_features[] = {0x03, 0x20, 0x00};
  125. bit_buffer_append_bytes(
  126. message, le_read_local_supported_features, sizeof(le_read_local_supported_features));
  127. break;
  128. case OCF_SET_EVENT_MASK:
  129. uint8_t set_le_event_mask[] = {
  130. 0x01, 0x20, 0x08, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  131. bit_buffer_append_bytes(message, set_le_event_mask, sizeof(set_le_event_mask));
  132. break;
  133. default:
  134. FURI_LOG_W(TAG, "Unhandled OCF %04x", OCF);
  135. break;
  136. }
  137. if(bit_buffer_get_size_bytes(message) > 0) {
  138. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  139. }
  140. bit_buffer_free(message);
  141. }
  142. void seos_hci_handle_event_cmd_complete_ogf_info(SeosHci* seos_hci, uint16_t OCF, BitBuffer* frame) {
  143. UNUSED(frame);
  144. BitBuffer* message = bit_buffer_alloc(128);
  145. switch(OCF) {
  146. case OCF_READ_LOCAL_VERSION:
  147. // uint8_t write_LE_host_supported[] = { 0x01, 0x6d, 0x0c, 0x02, 0x01, 0x00};
  148. uint8_t read_bd_addr[] = {0x09, 0x10, 0x00};
  149. bit_buffer_append_bytes(message, read_bd_addr, sizeof(read_bd_addr));
  150. break;
  151. case OCF_READ_BD_ADDR:
  152. uint8_t le_read_buffer_size[] = {0x02, 0x20, 0x00};
  153. bit_buffer_append_bytes(message, le_read_buffer_size, sizeof(le_read_buffer_size));
  154. break;
  155. default:
  156. FURI_LOG_W(TAG, "Unhandled OCF %04x", OCF);
  157. break;
  158. }
  159. if(bit_buffer_get_size_bytes(message) > 0) {
  160. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  161. }
  162. bit_buffer_free(message);
  163. }
  164. void seos_hci_handle_event_cmd_complete_ogf_le(SeosHci* seos_hci, uint16_t OCF, BitBuffer* frame) {
  165. UNUSED(frame);
  166. BitBuffer* message = bit_buffer_alloc(128);
  167. switch(OCF) {
  168. case OCF_LE_SET_EVENT_MASK:
  169. uint8_t read_local_version[] = {0x01, 0x10, 0x00};
  170. bit_buffer_append_bytes(message, read_local_version, sizeof(read_local_version));
  171. break;
  172. case OCF_LE_READ_BUFFER_SIZE:
  173. uint8_t le_set_random_address[] = {0x05, 0x20, 0x06, 0xCA, 0xFE, 0x00, 0x00, 0x00, 0x03};
  174. bit_buffer_append_bytes(message, le_set_random_address, sizeof(le_set_random_address));
  175. break;
  176. case OCF_LE_SET_ADVERTISING_DATA:
  177. seos_hci_enable_advertising(seos_hci, true);
  178. break;
  179. case OCF_LE_SET_SCAN_RESPONSE_DATA:
  180. uint8_t flow_mode_byte = seos_hci->flow_mode == FLOW_READER ? 0x00 : 0x01;
  181. // TODO: Use seos_reader_service_backwards
  182. uint8_t adv_data[] = {0x08, 0x20, 0x20, 0x15, 0x02, 0x01, 0x06, 0x11, 0x07,
  183. 0x02, 0x00, 0x00, 0x7a, 0x17, 0x00, 0x00, 0x80, 0x00,
  184. 0x10, 0x00, 0x00, flow_mode_byte, 0x98, 0x00, 0x00, 0x00, 0x00,
  185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  186. bit_buffer_append_bytes(message, adv_data, sizeof(adv_data));
  187. break;
  188. case OCF_LE_SET_ADVERTISE_ENABLE:
  189. if(furi_timer_is_running(seos_hci->timer)) {
  190. FURI_LOG_D(TAG, "clear timer");
  191. furi_timer_stop(seos_hci->timer);
  192. }
  193. uint8_t status = bit_buffer_get_byte(frame, 6);
  194. if(status == 0) {
  195. if(seos_hci->adv_status) {
  196. FURI_LOG_I(TAG, "*** Advertising enabled ***");
  197. view_dispatcher_send_custom_event(
  198. seos_hci->seos->view_dispatcher, SeosCustomEventAdvertising);
  199. } else {
  200. FURI_LOG_I(TAG, "*** Advertising disabled ***");
  201. }
  202. } else {
  203. FURI_LOG_W(TAG, "Advertising enabled FAILED");
  204. }
  205. break;
  206. case OCF_LE_SET_SCAN_PARAMETERS:
  207. seos_hci_set_scan(seos_hci, true);
  208. break;
  209. case OCF_LE_SET_SCAN_ENABLE:
  210. if(furi_timer_is_running(seos_hci->timer)) {
  211. FURI_LOG_D(TAG, "clear timer");
  212. furi_timer_stop(seos_hci->timer);
  213. }
  214. if(seos_hci->scan_status) { // enabled
  215. FURI_LOG_I(TAG, "Scan enable complete. new state: %d", seos_hci->scan_status);
  216. view_dispatcher_send_custom_event(
  217. seos_hci->seos->view_dispatcher, SeosCustomEventScan);
  218. } else if(seos_hci->device_found) {
  219. // Scanning stopped, try to connect
  220. seos_hci_connect(seos_hci);
  221. }
  222. break;
  223. case OCF_LE_READ_LOCAL_SUPPORTED_FEATURES:
  224. // FURI_LOG_D(TAG, "Local Supported Features");
  225. uint8_t set_event_mask[] = {
  226. 0x01, 0x0c, 0x08, 0xff, 0xff, 0xfb, 0xff, 0x07, 0xf8, 0xbf, 0x3d};
  227. bit_buffer_append_bytes(message, set_event_mask, sizeof(set_event_mask));
  228. break;
  229. case OCF_LE_SET_RANDOM_ADDRESS:
  230. // FURI_LOG_D(TAG, "opcode = %04x", BT_OP(0x3f, 0x0006)); <--- reverse this in byte array
  231. uint8_t vendor_set_addr[] = {0x06, 0xfc, 0x06, 0x0, 0x0, 0x1, 0x2, 0x21, 0xAD};
  232. bit_buffer_append_bytes(message, vendor_set_addr, sizeof(vendor_set_addr));
  233. break;
  234. case OCF_LE_SET_ADVERTISING_PARAMETERS:
  235. // TODO: make this more dynamic
  236. uint8_t capabilities = CAP_TWIST_AND_GO | CAP_ALLOW_TAP | CAP_APP_SPECIFIC |
  237. CAP_ENHANCED_TAP;
  238. int8_t tap_rssi = -75;
  239. int8_t twist_rssi = -75;
  240. int8_t seamless_rssi = -75;
  241. int8_t app_rssi = -75;
  242. uint8_t mfg_data[] = {0x14, 0xff, 0x2e, 0x01, 0x15, capabilities,
  243. tap_rssi, twist_rssi, seamless_rssi, app_rssi, 0x2a, 0x46,
  244. 0x4c, 0x30, 0x4b, 0x37, 0x5a, 0x30,
  245. 0x31, 0x55, 0x31, 0x00, 0x00};
  246. uint8_t device_name[] = {
  247. 0x09, 0x20, 0x20, 0x1e, 0x08, 0x09, 0x46, 0x6c, 0x69, 0x70, 0x70, 0x65, 0x72};
  248. bit_buffer_append_bytes(message, device_name, sizeof(device_name));
  249. bit_buffer_append_bytes(message, mfg_data, sizeof(mfg_data));
  250. break;
  251. default:
  252. FURI_LOG_W(TAG, "Unhandled OCF %04x", OCF);
  253. break;
  254. }
  255. if(bit_buffer_get_size_bytes(message) > 0) {
  256. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  257. }
  258. bit_buffer_free(message);
  259. }
  260. void seos_hci_handle_event_cmd_complete_ogf_vendor(
  261. SeosHci* seos_hci,
  262. uint16_t OCF,
  263. BitBuffer* frame) {
  264. UNUSED(frame);
  265. BitBuffer* message = bit_buffer_alloc(128);
  266. switch(OCF) {
  267. case 0x0006:
  268. if(seos_hci->mode == BLE_PERIPHERAL) {
  269. // Flipper as Reader
  270. uint8_t adv_param[] = {
  271. 0x06,
  272. 0x20,
  273. 0x0f,
  274. 0xa0,
  275. 0x00,
  276. 0xa0,
  277. 0x00,
  278. 0x00,
  279. 0x00,
  280. 0x01,
  281. 0xDE,
  282. 0xAF,
  283. 0xBE,
  284. 0xEF,
  285. 0xCA,
  286. 0xFE,
  287. 0x07,
  288. 0x00};
  289. bit_buffer_append_bytes(message, adv_param, sizeof(adv_param));
  290. } else if(seos_hci->mode == BLE_CENTRAL) {
  291. // Flipper as device/credential
  292. seos_hci_send_scan_params(seos_hci);
  293. }
  294. break;
  295. default:
  296. FURI_LOG_W(TAG, "Unhandled OCF %04x", OCF);
  297. break;
  298. }
  299. if(bit_buffer_get_size_bytes(message) > 0) {
  300. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  301. }
  302. bit_buffer_free(message);
  303. }
  304. void seos_hci_handle_event_cmd_complete(SeosHci* seos_hci, BitBuffer* frame) {
  305. BitBuffer* message = bit_buffer_alloc(128);
  306. const uint8_t* data = bit_buffer_get_data(frame);
  307. uint8_t event_type = data[0];
  308. uint8_t sub_event_type = data[1];
  309. uint8_t ncmd = data[3];
  310. uint16_t cmd = data[5] << 8 | data[4];
  311. uint8_t status = data[6];
  312. if(status == 0) {
  313. /*
  314. FURI_LOG_D(
  315. TAG,
  316. "event %d sub event %d ncmd %d cmd %d status %d",
  317. event_type,
  318. sub_event_type,
  319. ncmd,
  320. cmd,
  321. status);
  322. */
  323. } else {
  324. FURI_LOG_W(
  325. TAG,
  326. "event %d sub event %d ncmd %d cmd %d status %d",
  327. event_type,
  328. sub_event_type,
  329. ncmd,
  330. cmd,
  331. status);
  332. bit_buffer_free(message);
  333. return;
  334. }
  335. uint16_t OGF = BT_OGF(cmd);
  336. uint16_t OCF = BT_OCF(cmd);
  337. // FURI_LOG_D(TAG, "OGF = %04x OCF = %04x", OGF, OCF);
  338. switch(OGF) {
  339. case OGF_HOST_CTL:
  340. seos_hci_handle_event_cmd_complete_ogf_host(seos_hci, OCF, frame);
  341. break;
  342. case OGF_INFO_PARAM:
  343. seos_hci_handle_event_cmd_complete_ogf_info(seos_hci, OCF, frame);
  344. break;
  345. case OGF_LE_CTL:
  346. seos_hci_handle_event_cmd_complete_ogf_le(seos_hci, OCF, frame);
  347. break;
  348. case OGF_VENDOR_CTL:
  349. seos_hci_handle_event_cmd_complete_ogf_vendor(seos_hci, OCF, frame);
  350. break;
  351. default:
  352. FURI_LOG_W(TAG, "Unhandled OGF %04x", OGF);
  353. break;
  354. }
  355. bit_buffer_free(message);
  356. }
  357. void seos_hci_enable_advertising(SeosHci* seos_hci, bool enable) {
  358. seos_hci->adv_status = enable;
  359. FURI_LOG_I(TAG, "Enable Advertising: %s", enable ? "true" : "false");
  360. uint8_t adv_enable[] = {0x0a, 0x20, 0x01, enable ? 0x01 : 0x00};
  361. BitBuffer* message = bit_buffer_alloc(sizeof(adv_enable));
  362. bit_buffer_append_bytes(message, adv_enable, sizeof(adv_enable));
  363. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  364. bit_buffer_free(message);
  365. FURI_LOG_I(TAG, "Start timer to make sure adv change ran");
  366. size_t delay = 100 /*ms*/ / (1000.0f / furi_kernel_get_tick_frequency());
  367. furi_check(furi_timer_start(seos_hci->timer, delay) == FuriStatusOk);
  368. }
  369. void seos_hci_send_scan_params(SeosHci* seos_hci) {
  370. uint8_t LE_Scan_Type = 0x00;
  371. uint8_t Scanning_Filter_Policy = 0x00;
  372. uint16_t opcode = BT_OP(OGF_LE_CTL, OCF_LE_SET_SCAN_PARAMETERS);
  373. uint8_t scan_param[] = {
  374. 0xff, 0xff, 0x07, LE_Scan_Type, 0x10, 0x00, 0x10, 0x00, 0x00, Scanning_Filter_Policy};
  375. BitBuffer* message = bit_buffer_alloc(sizeof(scan_param));
  376. memcpy(scan_param, (uint8_t*)&opcode, sizeof(opcode));
  377. bit_buffer_append_bytes(message, scan_param, sizeof(scan_param));
  378. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  379. bit_buffer_free(message);
  380. }
  381. void seos_hci_set_scan(SeosHci* seos_hci, bool enable) {
  382. FURI_LOG_I(TAG, "Start Scan: %s", enable ? "true" : "false");
  383. seos_hci->scan_status = enable;
  384. uint16_t opcode = BT_OP(OGF_LE_CTL, OCF_LE_SET_SCAN_ENABLE);
  385. uint8_t set_scan[] = {0xff, 0xff, 0x02, enable ? 0x01 : 0x00, 0x00};
  386. memcpy(set_scan, (uint8_t*)&opcode, sizeof(opcode));
  387. BitBuffer* message = bit_buffer_alloc(sizeof(set_scan));
  388. bit_buffer_append_bytes(message, set_scan, sizeof(set_scan));
  389. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  390. bit_buffer_free(message);
  391. FURI_LOG_I(TAG, "Start timer to make sure set scan ran");
  392. size_t delay = 100 /*ms*/ / (1000.0f / furi_kernel_get_tick_frequency());
  393. furi_check(furi_timer_start(seos_hci->timer, delay) == FuriStatusOk);
  394. }
  395. // TODO: test this: hci create le conn - writing: 010d 2019 6000 3000 00 01 2db88ee137c3 000600120000002a0004000600
  396. void seos_hci_connect(SeosHci* seos_hci) {
  397. FURI_LOG_I(TAG, "seos_hci_connect");
  398. uint16_t opcode = BT_OP(OGF_LE_CTL, OCF_LE_CREATE_CONNECTION);
  399. // Values arbitrarily copied from https://stackoverflow.com/questions/71250571/how-to-send-le-extended-create-connection-in-ble-with-raspberry-pi
  400. uint8_t connect[] = {
  401. 0xff,
  402. 0xff, //opcode
  403. 0x19, // length
  404. 0x60,
  405. 0x00, // LE_Scan_Interval
  406. 0x60,
  407. 0x00, // LE_Scan_Window
  408. 0x00, // Initiator_Filter_Policy
  409. seos_hci->address_type, // Peer_Address_Type
  410. 0xFF,
  411. 0xFF,
  412. 0xFF,
  413. 0xFF,
  414. 0xFF,
  415. 0xFF, // Peer_Address
  416. 0x01, // Own_Address_Type
  417. 0x18,
  418. 0x00, // Connection_Interval_Min
  419. 0x28,
  420. 0x00, // Connection_Interval_Max
  421. 0x00,
  422. 0x00, // Max_Latency
  423. 0x90,
  424. 0x00, // Supervision_Timeout
  425. 0x00,
  426. 0x00, // Min_CE_Length
  427. 0x00,
  428. 0x00, // Max_CE_Length
  429. };
  430. memcpy(connect, (uint8_t*)&opcode, sizeof(opcode));
  431. memcpy(connect + 9, seos_hci->address, 6);
  432. BitBuffer* message = bit_buffer_alloc(sizeof(connect));
  433. bit_buffer_append_bytes(message, connect, sizeof(connect));
  434. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  435. bit_buffer_free(message);
  436. }
  437. void seos_hci_handle_event_le_meta(SeosHci* seos_hci, BitBuffer* frame) {
  438. const uint8_t* data = bit_buffer_get_data(frame);
  439. // uint8_t length = data[2];
  440. uint8_t subevent_code = data[3];
  441. switch(subevent_code) {
  442. case HCI_LE_CONNECTION_COMPLETE:
  443. uint8_t status = data[4];
  444. if(status != 0x00) {
  445. FURI_LOG_W(TAG, "Connection complete with non-zero status");
  446. return;
  447. }
  448. seos_hci->connection_handle = data[6] << 8 | data[5];
  449. uint8_t role = data[7];
  450. uint8_t peer_address_type = data[8];
  451. // and more...
  452. FURI_LOG_D(
  453. TAG,
  454. "connection complete: handle %04x role %d peer_address_type %d",
  455. seos_hci->connection_handle,
  456. role,
  457. peer_address_type);
  458. view_dispatcher_send_custom_event(
  459. seos_hci->seos->view_dispatcher, SeosCustomEventConnected);
  460. if(role == 0x00) { // I'm a central!
  461. if(seos_hci->central_connection_callback) {
  462. seos_hci->central_connection_callback(seos_hci->central_connection_context);
  463. } else {
  464. FURI_LOG_W(TAG, "No central_connection_callback defined");
  465. }
  466. } else if(role == 0x01) { // I'm a peripheral!
  467. }
  468. break;
  469. case HCI_LE_ADVERTISING_REPORT:
  470. // Prevent interruptions to handling a device by a second advertisement
  471. if(seos_hci->device_found) {
  472. break;
  473. }
  474. // TODO: Support single packet with multiple reports
  475. uint8_t num_reports = data[4];
  476. uint8_t Event_Type = data[5];
  477. uint8_t Address_Type = data[6];
  478. const uint8_t* Address = data + 7;
  479. uint8_t Data_Length = data[13];
  480. const uint8_t* adv_data = data + 14;
  481. char name[20];
  482. memset(name, 0, sizeof(name));
  483. if(Event_Type != 0 || Data_Length < sizeof(seos_reader_service_backwards)) {
  484. break;
  485. }
  486. FURI_LOG_D(
  487. TAG,
  488. "Adv %d reports: event type %d address type %d data len %d",
  489. num_reports,
  490. Event_Type,
  491. Address_Type,
  492. Data_Length);
  493. // seos_log_buffer(TAG, "ADV_IND", (uint8_t*)adv_data, Data_Length);
  494. uint8_t i = 0;
  495. do {
  496. uint8_t l = adv_data[i++];
  497. uint8_t t = adv_data[i++];
  498. const uint8_t* val = adv_data + i;
  499. i += l - 1; // subtract one so we don't overcount the type byte
  500. switch(t) {
  501. case 0x07:
  502. if(seos_hci->flow_mode == FLOW_CRED) {
  503. // You're acting like a credential, looking for readers to connect and send to
  504. if(memcmp(
  505. val,
  506. seos_reader_service_backwards,
  507. sizeof(seos_reader_service_backwards)) == 0) {
  508. seos_hci->device_found = true;
  509. }
  510. } else if(seos_hci->flow_mode == FLOW_READER) {
  511. if(memcmp(
  512. val,
  513. seos_cred_service_backwards,
  514. sizeof(seos_cred_service_backwards)) == 0) {
  515. seos_hci->device_found = true;
  516. }
  517. } else if(seos_hci->flow_mode == FLOW_READER_SCANNER) {
  518. // Reader scanner looks for readers, it doesn't act like a reader (as in FLOW_READER)
  519. if(memcmp(
  520. val,
  521. seos_reader_service_backwards,
  522. sizeof(seos_reader_service_backwards)) == 0) {
  523. // TODO: handle duplicates
  524. notification_message(
  525. seos_hci->seos->notifications, &sequence_single_vibro);
  526. }
  527. } else if(seos_hci->flow_mode == FLOW_CRED_SCANNER) {
  528. // Cred scanner looks for devices advertising credential service, it doesn't act like a credential (as in FLOW_CRED)
  529. if(memcmp(
  530. val,
  531. seos_cred_service_backwards,
  532. sizeof(seos_cred_service_backwards)) == 0) {
  533. // TODO: handle duplicates
  534. notification_message(
  535. seos_hci->seos->notifications, &sequence_single_vibro);
  536. }
  537. }
  538. break;
  539. case 0x08: // Short device name
  540. case 0x09: // full device name
  541. memcpy(name, val, l - 1);
  542. break;
  543. }
  544. } while(i < Data_Length - 1);
  545. seos_hci->adv_report_count += num_reports;
  546. if(seos_hci->device_found) {
  547. FURI_LOG_I(TAG, "Matched Seos Reader Service: %s", name);
  548. seos_hci->address_type = Address_Type;
  549. memcpy(seos_hci->address, Address, sizeof(seos_hci->address));
  550. seos_hci_set_scan(seos_hci, false);
  551. view_dispatcher_send_custom_event(
  552. seos_hci->seos->view_dispatcher, SeosCustomEventFound);
  553. }
  554. break;
  555. default:
  556. FURI_LOG_W(TAG, "LE Meta event with unknown subevent code");
  557. break;
  558. }
  559. }
  560. void seos_hci_event_handler(SeosHci* seos_hci, BitBuffer* frame) {
  561. const uint8_t* data = bit_buffer_get_data(frame);
  562. uint8_t sub_event_type = data[1];
  563. // uint8_t length = data[2];
  564. if(sub_event_type == BT_HCI_EVT_CMD_STATUS) {
  565. struct bt_hci_evt_cmd_status {
  566. uint8_t status;
  567. uint8_t ncmd;
  568. uint16_t opcode;
  569. } __packed;
  570. struct bt_hci_evt_cmd_status* status = (struct bt_hci_evt_cmd_status*)(data + 3);
  571. if(status->status == 0) {
  572. /*
  573. FURI_LOG_D(
  574. TAG,
  575. "Status: status %d ncmd 0x%02x opcode %04x",
  576. status->status,
  577. status->ncmd,
  578. status->opcode);
  579. */
  580. } else {
  581. // Unknown HCI command (0x01)
  582. FURI_LOG_W(
  583. TAG,
  584. "Status: status %d ncmd 0x%02x opcode %04x",
  585. status->status,
  586. status->ncmd,
  587. status->opcode);
  588. }
  589. } else if(sub_event_type == BT_HCI_EVT_CMD_COMPLETE) {
  590. seos_hci_handle_event_cmd_complete(seos_hci, frame);
  591. } else if(sub_event_type == BT_HCI_EVT_LE_META) {
  592. seos_hci_handle_event_le_meta(seos_hci, frame);
  593. } else if(sub_event_type == BT_HCI_EVT_DISCONN_COMPLETE) {
  594. seos_hci->connection_handle = 0;
  595. if(seos_hci->mode == BLE_PERIPHERAL) {
  596. FURI_LOG_W(TAG, "Disconnect. Restart Advertising");
  597. seos_hci_enable_advertising(seos_hci, true);
  598. } else if(seos_hci->mode == BLE_CENTRAL) {
  599. FURI_LOG_W(TAG, "Disconnect. Scan again");
  600. seos_hci->device_found = false;
  601. seos_hci_set_scan(seos_hci, true);
  602. }
  603. } else if(sub_event_type == BT_HCI_EVT_NUM_COMPLETED_PACKETS) {
  604. struct bt_hci_evt_num_completed_packets {
  605. uint8_t num_handles;
  606. uint16_t handle;
  607. uint16_t count;
  608. } __attribute__((packed));
  609. struct bt_hci_evt_num_completed_packets* evt =
  610. (struct bt_hci_evt_num_completed_packets*)(data + 3);
  611. if(evt->num_handles == 1) {
  612. // FURI_LOG_D(TAG, "Number of completed packets for %04x: %d", evt->handle, evt->count);
  613. } else {
  614. FURI_LOG_D(TAG, "Number of completed packets for multiple handles");
  615. }
  616. if(seos_hci->completed_packets_callback) {
  617. seos_hci->completed_packets_callback(seos_hci->completed_packets_context);
  618. }
  619. } else {
  620. FURI_LOG_W(TAG, "Unhandled event subtype %02x", sub_event_type);
  621. }
  622. }
  623. void seos_hci_acldata_send(SeosHci* seos_hci, uint8_t flags, BitBuffer* tx) {
  624. // seos_log_buffer("seos_hci_acldata_send", tx);
  625. uint16_t tx_len = bit_buffer_get_size_bytes(tx);
  626. uint16_t handle = seos_hci->connection_handle | (flags << 12);
  627. BitBuffer* response = bit_buffer_alloc(tx_len + sizeof(handle) + sizeof(tx_len));
  628. bit_buffer_append_bytes(response, (uint8_t*)&handle, sizeof(handle));
  629. bit_buffer_append_bytes(response, (uint8_t*)&tx_len, sizeof(tx_len));
  630. // tx
  631. bit_buffer_append_bytes(response, bit_buffer_get_data(tx), tx_len);
  632. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_ACLDATA_PKT, response);
  633. bit_buffer_free(response);
  634. }
  635. void seos_hci_acldata_handler(SeosHci* seos_hci, BitBuffer* frame) {
  636. const uint8_t* data = bit_buffer_get_data(frame);
  637. // 0 is 0x02 for ACL DATA
  638. uint16_t handle = (data[2] << 8 | data[1]) & 0x0FFF;
  639. uint8_t flags = data[2] >> 4;
  640. uint16_t length = data[4] << 8 | data[3];
  641. /*
  642. uint8_t Broadcast_Flag = flags >> 2;
  643. uint8_t Packet_Boundary_Flag = flags & 0x03;
  644. FURI_LOG_D(
  645. TAG,
  646. "ACLDATA handle %04x Broadcast_Flag %02x Packet_Boundary_Flag %02x length %d",
  647. handle,
  648. Broadcast_Flag,
  649. Packet_Boundary_Flag,
  650. length);
  651. */
  652. if(handle != seos_hci->connection_handle) {
  653. FURI_LOG_W(TAG, "Mismatched handle values");
  654. }
  655. BitBuffer* pdu = bit_buffer_alloc(length);
  656. bit_buffer_append_bytes(pdu, data + 1 /*ACL DATA */ + sizeof(handle) + sizeof(length), length);
  657. if(seos_hci->receive_callback) {
  658. seos_hci->receive_callback(seos_hci->receive_callback_context, handle, flags, pdu);
  659. }
  660. bit_buffer_free(pdu);
  661. }
  662. size_t seos_hci_recv(void* context, BitBuffer* frame) {
  663. SeosHci* seos_hci = (SeosHci*)context;
  664. // seos_log_buffer("HCI Frame", frame);
  665. const uint8_t* data = bit_buffer_get_data(frame);
  666. uint8_t event_type = data[0];
  667. // TODO: consider `bit_buffer_starts_with_byte`
  668. switch(event_type) {
  669. case HCI_EVENT_PKT:
  670. seos_hci_event_handler(seos_hci, frame);
  671. break;
  672. case HCI_ACLDATA_PKT:
  673. seos_hci_acldata_handler(seos_hci, frame);
  674. break;
  675. default:
  676. FURI_LOG_W(TAG, "Haven't added support for other HCI commands yet");
  677. break;
  678. }
  679. return 0;
  680. }
  681. // TODO: Consider making this a general "when the state changes" callback which would check if H5 is active (or needs to be reset)
  682. void seos_hci_init(void* context) {
  683. SeosHci* seos_hci = (SeosHci*)context;
  684. BitBuffer* message = bit_buffer_alloc(128);
  685. uint8_t reset[] = {0x03, 0x0c, 0x00};
  686. bit_buffer_append_bytes(message, reset, sizeof(reset));
  687. seos_hci_h5_send(seos_hci->seos_hci_h5, HCI_COMMAND_PKT, message);
  688. view_dispatcher_send_custom_event(seos_hci->seos->view_dispatcher, SeosCustomEventHCIInit);
  689. bit_buffer_free(message);
  690. }
  691. void seos_hci_set_receive_callback(
  692. SeosHci* seos_hci,
  693. SeosHciReceiveCallback callback,
  694. void* context) {
  695. seos_hci->receive_callback = callback;
  696. seos_hci->receive_callback_context = context;
  697. }
  698. void seos_hci_set_completed_packets_callback(
  699. SeosHci* seos_hci,
  700. SeosHciCompletedPacketsCallback callback,
  701. void* context) {
  702. seos_hci->completed_packets_callback = callback;
  703. seos_hci->completed_packets_context = context;
  704. }
  705. void seos_hci_set_central_connection_callback(
  706. SeosHci* seos_hci,
  707. SeosHciCentralConnectionCallback callback,
  708. void* context) {
  709. seos_hci->central_connection_callback = callback;
  710. seos_hci->central_connection_context = context;
  711. }