seos_hci_h5.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <furi.h>
  5. #include <lib/toolbox/bit_buffer.h>
  6. #include <lib/bit_lib/bit_lib.h>
  7. #include "uart.h"
  8. // include/net/bluetooth/hci.h
  9. /* HCI data types */
  10. #define HCI_COMMAND_PKT 0x01
  11. #define HCI_ACLDATA_PKT 0x02
  12. #define HCI_SCODATA_PKT 0x03
  13. #define HCI_EVENT_PKT 0x04
  14. #define HCI_ISODATA_PKT 0x05
  15. #define HCI_DIAG_PKT 0xf0
  16. #define HCI_VENDOR_PKT 0xff
  17. #define HCI_3WIRE_ACK_PKT 0
  18. #define HCI_3WIRE_LINK_PKT 0x0F
  19. #define H5_TX_WIN_MAX 4
  20. typedef size_t (*SeosHciH5ReceiveCallback)(void* context, BitBuffer* frame);
  21. typedef void (*SeosHciH5InitCallback)(void* context);
  22. typedef struct {
  23. SeosUart* uart;
  24. uint8_t tx_seq; /* Next seq number to send */
  25. uint8_t tx_ack; /* Next ack number to send */
  26. uint8_t tx_win; /* Sliding window size */
  27. uint8_t rx_ack; /* Last ack number received */
  28. unsigned long flags;
  29. enum {
  30. H5_UNINITIALIZED,
  31. H5_INITIALIZED,
  32. H5_ACTIVE,
  33. } state;
  34. enum {
  35. H5_AWAKE,
  36. H5_SLEEPING,
  37. H5_WAKING_UP,
  38. } sleep;
  39. enum {
  40. STOPPED,
  41. STARTED
  42. } stage;
  43. SeosHciH5ReceiveCallback receive_callback;
  44. void* receive_callback_context;
  45. SeosHciH5InitCallback init_callback;
  46. void* init_callback_context;
  47. size_t out_of_order_count;
  48. FuriMessageQueue* messages;
  49. FuriMutex* mq_mutex;
  50. FuriThread* thread;
  51. } SeosHciH5;
  52. SeosHciH5* seos_hci_h5_alloc();
  53. void seos_hci_h5_free(SeosHciH5* seos_hci_h5);
  54. void seos_hci_h5_start(SeosHciH5* seos_hci_h5);
  55. void seos_hci_h5_stop(SeosHciH5* seos_hci_h5);
  56. void seos_hci_h5_send(SeosHciH5* seos_hci_h5, uint8_t pkt_type, BitBuffer* message);
  57. size_t seos_hci_h5_recv(void* context, uint8_t* buffer, size_t len);
  58. void seos_hci_h5_set_receive_callback(
  59. SeosHciH5* seos_hci_h5,
  60. SeosHciH5ReceiveCallback callback,
  61. void* context);
  62. void seos_hci_h5_set_init_callback(
  63. SeosHciH5* seos_hci_h5,
  64. SeosHciH5InitCallback callback,
  65. void* context);