seos_hci_h5.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #include "seos_hci_h5_i.h"
  2. #define TAG "SeosHciH5"
  3. #define MAX_OUT_OF_ORDER 3
  4. /* Convenience macros for reading Three-wire header values */
  5. #define H5_HDR_SEQ(hdr) ((hdr)[0] & 0x07)
  6. #define H5_HDR_ACK(hdr) (((hdr)[0] >> 3) & 0x07)
  7. #define H5_HDR_CRC(hdr) (((hdr)[0] >> 6) & 0x01)
  8. #define H5_HDR_RELIABLE(hdr) (((hdr)[0] >> 7) & 0x01)
  9. #define H5_HDR_PKT_TYPE(hdr) ((hdr)[1] & 0x0f)
  10. #define H5_HDR_LEN(hdr) ((((hdr)[1] >> 4) & 0x0f) + ((hdr)[2] << 4))
  11. #define SLIP_DELIMITER 0xc0
  12. #define SLIP_ESC 0xdb
  13. #define SLIP_ESC_DELIM 0xdc
  14. #define SLIP_ESC_ESC 0xdd
  15. #define MESSAGE_QUEUE_SIZE 10
  16. /* H5 state flags */
  17. enum {
  18. H5_RX_ESC, /* SLIP escape mode */
  19. H5_TX_ACK_REQ, /* Pending ack to send */
  20. H5_WAKEUP_DISABLE, /* Device cannot wake host */
  21. H5_HW_FLOW_CONTROL, /* Use HW flow control */
  22. };
  23. typedef struct {
  24. size_t len;
  25. uint8_t buf[SEOS_UART_RX_BUF_SIZE];
  26. } HCI_MESSAGE;
  27. int32_t seos_hci_h5_task(void* context);
  28. void seos_hci_h5_link_control(SeosHciH5* seos_hci_h5, uint8_t* data, size_t len);
  29. void seos_hci_h5_sync(SeosHciH5* seos_hci_h5) {
  30. seos_hci_h5->out_of_order_count = 0;
  31. uint8_t sync[] = {0x01, 0x7e};
  32. seos_hci_h5_link_control(seos_hci_h5, sync, sizeof(sync));
  33. }
  34. SeosHciH5* seos_hci_h5_alloc() {
  35. SeosHciH5* seos_hci_h5 = malloc(sizeof(SeosHciH5));
  36. memset(seos_hci_h5, 0, sizeof(SeosHciH5));
  37. seos_hci_h5->tx_win = H5_TX_WIN_MAX;
  38. seos_hci_h5->stage = STOPPED;
  39. seos_hci_h5->uart = seos_uart_alloc();
  40. seos_uart_set_receive_callback(seos_hci_h5->uart, seos_hci_h5_recv, seos_hci_h5);
  41. seos_hci_h5->thread =
  42. furi_thread_alloc_ex("SeosHciH5Worker", 5 * 1024, seos_hci_h5_task, seos_hci_h5);
  43. seos_hci_h5->messages = furi_message_queue_alloc(MESSAGE_QUEUE_SIZE, sizeof(HCI_MESSAGE));
  44. seos_hci_h5->mq_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  45. furi_thread_start(seos_hci_h5->thread);
  46. // Give the UART threads a little time to get started
  47. furi_delay_ms(3);
  48. return seos_hci_h5;
  49. }
  50. void seos_hci_h5_free(SeosHciH5* seos_hci_h5) {
  51. furi_assert(seos_hci_h5);
  52. furi_message_queue_free(seos_hci_h5->messages);
  53. furi_mutex_free(seos_hci_h5->mq_mutex);
  54. furi_thread_free(seos_hci_h5->thread);
  55. seos_uart_free(seos_hci_h5->uart);
  56. free(seos_hci_h5);
  57. }
  58. void seos_hci_h5_start(SeosHciH5* seos_hci_h5) {
  59. seos_hci_h5->stage = STARTED;
  60. seos_hci_h5->out_of_order_count = 0;
  61. /* Send initial sync request */
  62. seos_hci_h5_sync(seos_hci_h5);
  63. }
  64. void seos_hci_h5_stop(SeosHciH5* seos_hci_h5) {
  65. seos_hci_h5->stage = STOPPED;
  66. furi_thread_flags_set(furi_thread_get_id(seos_hci_h5->thread), WorkerEvtStop);
  67. furi_thread_join(seos_hci_h5->thread);
  68. }
  69. void seos_hci_h5_peer_reset(SeosHciH5* seos_hci_h5) {
  70. seos_hci_h5->state = H5_UNINITIALIZED;
  71. seos_hci_h5->tx_seq = 0;
  72. seos_hci_h5->tx_ack = 0;
  73. }
  74. void seos_hci_h5_reset_rx(SeosHciH5* seos_hci_h5) {
  75. bit_lib_set_bit((uint8_t*)&seos_hci_h5->flags, H5_RX_ESC, false);
  76. }
  77. void seos_hci_h5_link_control(SeosHciH5* seos_hci_h5, uint8_t* data, size_t len) {
  78. BitBuffer* message = bit_buffer_alloc(len);
  79. bit_buffer_append_bytes(message, data, len);
  80. seos_hci_h5_send(seos_hci_h5, HCI_3WIRE_LINK_PKT, message);
  81. bit_buffer_free(message);
  82. }
  83. void seos_hci_h5_handle_internal_rx(SeosHciH5* seos_hci_h5, BitBuffer* rx_skb) {
  84. uint8_t sync_req[] = {0x01, 0x7e};
  85. uint8_t sync_rsp[] = {0x02, 0x7d};
  86. uint8_t conf_req[3] = {0x03, 0xfc};
  87. uint8_t conf_rsp[] = {0x04, 0x7b};
  88. uint8_t wakeup_req[] = {0x05, 0xfa};
  89. uint8_t woken_req[] = {0x06, 0xf9};
  90. uint8_t sleep_req[] = {0x07, 0x78};
  91. const uint8_t* header = bit_buffer_get_data(rx_skb);
  92. const uint8_t* data = bit_buffer_get_data(rx_skb) + 4;
  93. if(H5_HDR_PKT_TYPE(header) != HCI_3WIRE_LINK_PKT) return;
  94. if(H5_HDR_LEN(header) < 2) return;
  95. conf_req[2] = seos_hci_h5->tx_win & 0x07;
  96. if(memcmp(data, sync_req, 2) == 0) {
  97. if(seos_hci_h5->state == H5_ACTIVE) seos_hci_h5_peer_reset(seos_hci_h5);
  98. seos_hci_h5_link_control(seos_hci_h5, sync_rsp, 2);
  99. } else if(memcmp(data, sync_rsp, 2) == 0) {
  100. if(seos_hci_h5->state == H5_ACTIVE) seos_hci_h5_peer_reset(seos_hci_h5);
  101. seos_hci_h5->state = H5_INITIALIZED;
  102. seos_hci_h5_link_control(seos_hci_h5, conf_req, 3);
  103. } else if(memcmp(data, conf_req, 2) == 0) {
  104. seos_hci_h5_link_control(seos_hci_h5, conf_rsp, 2);
  105. seos_hci_h5_link_control(seos_hci_h5, conf_req, 3);
  106. } else if(memcmp(data, conf_rsp, 2) == 0) {
  107. if(H5_HDR_LEN(header) > 2) seos_hci_h5->tx_win = (data[2] & 0x07);
  108. FURI_LOG_D(TAG, "---- Three-wire init complete. tx_win %u ----", seos_hci_h5->tx_win);
  109. seos_hci_h5->state = H5_ACTIVE;
  110. if(seos_hci_h5->init_callback) {
  111. seos_hci_h5->init_callback(seos_hci_h5->init_callback_context);
  112. }
  113. return;
  114. } else if(memcmp(data, sleep_req, 2) == 0) {
  115. FURI_LOG_D(TAG, "Peer went to sleep");
  116. seos_hci_h5->sleep = H5_SLEEPING;
  117. return;
  118. } else if(memcmp(data, woken_req, 2) == 0) {
  119. FURI_LOG_D(TAG, "Peer woke up");
  120. seos_hci_h5->sleep = H5_AWAKE;
  121. } else if(memcmp(data, wakeup_req, 2) == 0) {
  122. FURI_LOG_D(TAG, "Peer requested wakeup");
  123. seos_hci_h5_link_control(seos_hci_h5, woken_req, 2);
  124. seos_hci_h5->sleep = H5_AWAKE;
  125. } else {
  126. FURI_LOG_D(TAG, "Link Control: 0x%02hhx 0x%02hhx", data[0], data[1]);
  127. return;
  128. }
  129. // hci_uart_tx_wakeup(hu);
  130. }
  131. static void seos_hci_h5_slip_delim(BitBuffer* skb) {
  132. const char delim = SLIP_DELIMITER;
  133. bit_buffer_append_byte(skb, delim);
  134. }
  135. static void seos_hci_h5_slip_one_byte(BitBuffer* skb, uint8_t c) {
  136. const char esc_delim[2] = {SLIP_ESC, SLIP_ESC_DELIM};
  137. const char esc_esc[2] = {SLIP_ESC, SLIP_ESC_ESC};
  138. switch(c) {
  139. case SLIP_DELIMITER:
  140. bit_buffer_append_byte(skb, esc_delim[0]);
  141. bit_buffer_append_byte(skb, esc_delim[1]);
  142. break;
  143. case SLIP_ESC:
  144. bit_buffer_append_byte(skb, esc_esc[0]);
  145. bit_buffer_append_byte(skb, esc_esc[1]);
  146. break;
  147. default:
  148. bit_buffer_append_byte(skb, c);
  149. }
  150. }
  151. static void seos_hci_h5_unslip_one_byte(SeosHciH5* seos_hci_h5, BitBuffer* skb, uint8_t c) {
  152. const uint8_t delim = SLIP_DELIMITER, esc = SLIP_ESC;
  153. uint8_t byte = c;
  154. bool test = bit_lib_get_bit((uint8_t*)&seos_hci_h5->flags, H5_RX_ESC);
  155. if(!test && c == SLIP_ESC) {
  156. bit_lib_set_bit((uint8_t*)&seos_hci_h5->flags, H5_RX_ESC, true);
  157. return;
  158. }
  159. if(test) {
  160. bit_lib_set_bit((uint8_t*)&seos_hci_h5->flags, H5_RX_ESC, false);
  161. switch(c) {
  162. case SLIP_ESC_DELIM:
  163. byte = delim;
  164. break;
  165. case SLIP_ESC_ESC:
  166. byte = esc;
  167. break;
  168. default:
  169. FURI_LOG_W(TAG, "Invalid esc byte 0x%02hhx", c);
  170. seos_hci_h5_reset_rx(seos_hci_h5);
  171. return;
  172. }
  173. }
  174. bit_buffer_append_byte(skb, byte);
  175. }
  176. void seos_hci_h5_send(SeosHciH5* seos_hci_h5, uint8_t pkt_type, BitBuffer* message) {
  177. SeosUart* seos_uart = seos_hci_h5->uart;
  178. const uint8_t* message_buf = message ? bit_buffer_get_data(message) : NULL;
  179. size_t message_len = message ? bit_buffer_get_size_bytes(message) : 0;
  180. if(message_len > 0) {
  181. // seos_log_buffer("seos_hci_h5_send", message);
  182. }
  183. uint8_t header[4];
  184. /*
  185. * Max len of packet: (original len + 4 (H5 hdr) + 2 (crc)) * 2
  186. * (because bytes 0xc0 and 0xdb are escaped, worst case is when
  187. * the packet is all made of 0xc0 and 0xdb) + 2 (0xc0
  188. * delimiters at start and end).
  189. */
  190. size_t max_packet_len = (message_len + 6) * 2 + 2;
  191. BitBuffer* nskb = bit_buffer_alloc(max_packet_len);
  192. seos_hci_h5_slip_delim(nskb);
  193. bit_lib_set_bit((uint8_t*)&seos_hci_h5->flags, H5_TX_ACK_REQ, false);
  194. header[0] = seos_hci_h5->tx_ack << 3;
  195. /* Reliable packet? */
  196. if(pkt_type == HCI_ACLDATA_PKT || pkt_type == HCI_COMMAND_PKT) {
  197. header[0] |= 1 << 7;
  198. header[0] |= seos_hci_h5->tx_seq;
  199. seos_hci_h5->tx_seq = (seos_hci_h5->tx_seq + 1) % 8;
  200. }
  201. header[1] = pkt_type | ((message_len & 0x0f) << 4);
  202. header[2] = message_len >> 4;
  203. header[3] = ~((header[0] + header[1] + header[2]) & 0xff);
  204. /*
  205. FURI_LOG_D(
  206. TAG,
  207. "tx: seq %u ack %u crc %u rel %u type %u len %u",
  208. H5_HDR_SEQ(header),
  209. H5_HDR_ACK(header),
  210. H5_HDR_CRC(header),
  211. H5_HDR_RELIABLE(header),
  212. H5_HDR_PKT_TYPE(header),
  213. H5_HDR_LEN(header));
  214. */
  215. for(size_t i = 0; i < sizeof(header); i++) {
  216. seos_hci_h5_slip_one_byte(nskb, header[i]);
  217. }
  218. for(size_t i = 0; i < message_len; i++) {
  219. seos_hci_h5_slip_one_byte(nskb, message_buf[i]);
  220. }
  221. seos_hci_h5_slip_delim(nskb);
  222. seos_uart_send(
  223. seos_uart, (uint8_t*)bit_buffer_get_data(nskb), bit_buffer_get_size_bytes(nskb));
  224. bit_buffer_free(nskb);
  225. }
  226. size_t seos_hci_h5_recv(void* context, uint8_t* buffer, size_t len) {
  227. SeosHciH5* seos_hci_h5 = (SeosHciH5*)context;
  228. // Must get start byte + 4 byte header + end byte to even give a shit
  229. if(len < 6) {
  230. return 0;
  231. }
  232. if(buffer[0] != SLIP_DELIMITER) {
  233. // FURI_LOG_E(TAG, "UART didn't start with SLIP_DELIMITER (%02x)", buffer[0]);
  234. return 1;
  235. }
  236. BitBuffer* rx_skb = bit_buffer_alloc(len);
  237. // i = 1 -> Skip first c0 byte
  238. for(size_t i = 1; i < len; i++) {
  239. uint8_t c = buffer[i];
  240. seos_hci_h5_unslip_one_byte(seos_hci_h5, rx_skb, c);
  241. }
  242. // seos_log_buffer("HCI H5 Recv", rx_skb);
  243. // From h5_rx_3wire_hdr
  244. const uint8_t* header = bit_buffer_get_data(rx_skb);
  245. /*
  246. FURI_LOG_D(
  247. TAG,
  248. "rx: seq %u ack %u crc %u rel %u type %u len %u",
  249. H5_HDR_SEQ(header),
  250. H5_HDR_ACK(header),
  251. H5_HDR_CRC(header),
  252. H5_HDR_RELIABLE(header),
  253. H5_HDR_PKT_TYPE(header),
  254. H5_HDR_LEN(header));
  255. */
  256. if(((header[0] + header[1] + header[2] + header[3]) & 0xff) != 0xff) {
  257. FURI_LOG_W(TAG, "Invalid header checksum");
  258. bit_buffer_free(rx_skb);
  259. return len;
  260. }
  261. if(len < (size_t)(1 + 4 + H5_HDR_LEN(header) + 1)) {
  262. // FURI_LOG_W(TAG, "Incomplete packet (%d), wait for more data", len);
  263. bit_buffer_free(rx_skb);
  264. return 0;
  265. }
  266. if(H5_HDR_RELIABLE(header) && H5_HDR_SEQ(header) != seos_hci_h5->tx_ack) {
  267. FURI_LOG_W(
  268. TAG, "Out-of-order packet arrived (%u != %u)", H5_HDR_SEQ(header), seos_hci_h5->tx_ack);
  269. bit_lib_set_bit((uint8_t*)&seos_hci_h5->flags, H5_TX_ACK_REQ, true);
  270. seos_hci_h5_reset_rx(seos_hci_h5);
  271. seos_hci_h5->out_of_order_count++;
  272. if(seos_hci_h5->out_of_order_count >= MAX_OUT_OF_ORDER) {
  273. seos_hci_h5_sync(seos_hci_h5);
  274. }
  275. bit_buffer_free(rx_skb);
  276. return len;
  277. }
  278. // When a packet isn't out of order, reset the count
  279. seos_hci_h5->out_of_order_count = 0;
  280. if(seos_hci_h5->state != H5_ACTIVE && H5_HDR_PKT_TYPE(header) != HCI_3WIRE_LINK_PKT) {
  281. FURI_LOG_W(TAG, "Non-link packet received in non-active state");
  282. seos_hci_h5_reset_rx(seos_hci_h5);
  283. bit_buffer_free(rx_skb);
  284. return len;
  285. }
  286. if(H5_HDR_CRC(header)) {
  287. // TODO: Check header
  288. }
  289. // From h5_complete_rx_pkt
  290. if(H5_HDR_RELIABLE(header)) {
  291. seos_hci_h5->tx_ack = (seos_hci_h5->tx_ack + 1) % 8;
  292. bit_lib_set_bit((uint8_t*)&seos_hci_h5->flags, H5_TX_ACK_REQ, true);
  293. //hci_uart_tx_wakeup(hu);
  294. }
  295. seos_hci_h5->rx_ack = H5_HDR_ACK(header);
  296. size_t payload_len = H5_HDR_LEN(header);
  297. // Determine amount of data we consumed
  298. BitBuffer* tmp = bit_buffer_alloc(len);
  299. for(size_t i = 0; i < 4 + payload_len; i++) {
  300. seos_hci_h5_slip_one_byte(tmp, bit_buffer_get_byte(rx_skb, i));
  301. }
  302. size_t consumed = 1 + bit_buffer_get_size_bytes(tmp) + 1;
  303. if(consumed > len) {
  304. // At one point I was double processing the header, and not processing all the paylaod, and ended up with consumed > len.
  305. // This is to track if there are edge cases I missed.
  306. FURI_LOG_W(TAG, "Consumed %d > len %d", consumed, len);
  307. consumed = len;
  308. }
  309. bit_buffer_free(tmp);
  310. // Remove from send queue?
  311. // h5_pkt_cull(h5);
  312. switch(H5_HDR_PKT_TYPE(header)) {
  313. case HCI_EVENT_PKT:
  314. case HCI_ACLDATA_PKT:
  315. case HCI_SCODATA_PKT:
  316. case HCI_ISODATA_PKT:
  317. uint32_t space = furi_message_queue_get_space(seos_hci_h5->messages);
  318. if(space > 0) {
  319. HCI_MESSAGE message = {};
  320. message.len = 4 + payload_len;
  321. if(message.len > sizeof(message.buf)) {
  322. FURI_LOG_W(TAG, "Too big to queue");
  323. return len;
  324. }
  325. memcpy(message.buf, bit_buffer_get_data(rx_skb), message.len);
  326. if(furi_mutex_acquire(seos_hci_h5->mq_mutex, FuriWaitForever) == FuriStatusOk) {
  327. furi_message_queue_put(seos_hci_h5->messages, &message, FuriWaitForever);
  328. furi_mutex_release(seos_hci_h5->mq_mutex);
  329. }
  330. if(space < MESSAGE_QUEUE_SIZE / 2) {
  331. FURI_LOG_D(TAG, "Queue message. %ld remaining", space);
  332. }
  333. } else {
  334. if(seos_hci_h5->stage != STOPPED) {
  335. FURI_LOG_E(TAG, "No space in message queue");
  336. }
  337. }
  338. break;
  339. default:
  340. seos_hci_h5_handle_internal_rx(seos_hci_h5, rx_skb);
  341. break;
  342. }
  343. if(bit_lib_get_bit((uint8_t*)&seos_hci_h5->flags, H5_TX_ACK_REQ)) {
  344. seos_hci_h5_send(seos_hci_h5, HCI_3WIRE_ACK_PKT, NULL);
  345. }
  346. seos_hci_h5_reset_rx(seos_hci_h5);
  347. bit_buffer_free(rx_skb);
  348. // FURI_LOG_D(TAG, "%d consumed", consumed);
  349. return consumed;
  350. }
  351. int32_t seos_hci_h5_task(void* context) {
  352. SeosHciH5* seos_hci_h5 = (SeosHciH5*)context;
  353. bool running = true;
  354. while(running) {
  355. uint32_t events = furi_thread_flags_get();
  356. if(events & WorkerEvtStop) {
  357. running = false;
  358. break;
  359. }
  360. if(furi_mutex_acquire(seos_hci_h5->mq_mutex, 1) == FuriStatusOk) {
  361. uint32_t count = furi_message_queue_get_count(seos_hci_h5->messages);
  362. if(count > 0) {
  363. if(count > MESSAGE_QUEUE_SIZE / 2) {
  364. FURI_LOG_I(TAG, "Dequeue message [%ld messages]", count);
  365. }
  366. HCI_MESSAGE message = {};
  367. FuriStatus status =
  368. furi_message_queue_get(seos_hci_h5->messages, &message, FuriWaitForever);
  369. if(status != FuriStatusOk) {
  370. FURI_LOG_W(TAG, "furi_message_queue_get fail %d", status);
  371. }
  372. if(seos_hci_h5->receive_callback) {
  373. size_t payload_len = H5_HDR_LEN(message.buf);
  374. BitBuffer* frame = bit_buffer_alloc(payload_len + 1);
  375. bit_buffer_append_byte(frame, H5_HDR_PKT_TYPE(message.buf));
  376. bit_buffer_append_bytes(frame, message.buf + 4, payload_len);
  377. seos_hci_h5->receive_callback(seos_hci_h5->receive_callback_context, frame);
  378. bit_buffer_free(frame);
  379. }
  380. }
  381. furi_mutex_release(seos_hci_h5->mq_mutex);
  382. } else {
  383. FURI_LOG_W(TAG, "Failed to acquire mutex");
  384. }
  385. // A beat for event flags
  386. // furi_delay_ms(1);
  387. }
  388. return 0;
  389. }
  390. void seos_hci_h5_set_receive_callback(
  391. SeosHciH5* seos_hci_h5,
  392. SeosHciH5ReceiveCallback callback,
  393. void* context) {
  394. seos_hci_h5->receive_callback = callback;
  395. seos_hci_h5->receive_callback_context = context;
  396. }
  397. void seos_hci_h5_set_init_callback(
  398. SeosHciH5* seos_hci_h5,
  399. SeosHciH5InitCallback callback,
  400. void* context) {
  401. seos_hci_h5->init_callback = callback;
  402. seos_hci_h5->init_callback_context = context;
  403. }