hm_hci_transport_h4.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef HM_TRANS_H4_H
  2. #define HM_TRANS_H4_H
  3. #include "hm_error.h"
  4. #include "hm_hci_transport_h4_uart.h"
  5. #include <stdint.h>
  6. #include <stddef.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #define HCI_TRANS_H4_TYPE_CMD (0x01)
  11. #define HCI_TRANS_H4_TYPE_ACL (0x02)
  12. #define HCI_TRANS_H4_TYPE_SCO (0x03)
  13. #define HCI_TRANS_H4_TYPE_EVT (0x04)
  14. #define HCI_TRANS_H4_TYPE_ISO (0x05)
  15. /**
  16. * @param n The count for the block.
  17. * @param block_size One block size in memory pool.
  18. */
  19. #define MEMPOOL_SIZE(n, block_size) (RT_ALIGN(((block_size) + 4), RT_ALIGN_SIZE) * (n))
  20. struct hm_hci_cmd {
  21. uint16_t opcode;
  22. uint8_t length;
  23. uint8_t data[0];
  24. } __attribute__((packed));
  25. struct hm_hci_acl {
  26. uint16_t handle; // Include PB, BC flag.
  27. uint16_t length;
  28. uint8_t data[0];
  29. } __attribute__((packed));
  30. struct hm_hci_sco {
  31. uint16_t handle; // Include Package Status flag, RFU.
  32. uint8_t length;
  33. uint8_t data[0];
  34. } __attribute__((packed));
  35. struct hm_hci_evt {
  36. uint8_t evt_code;
  37. uint8_t length;
  38. uint8_t data[0];
  39. } __attribute__((packed));
  40. #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
  41. struct hci_trans_h4_config {
  42. struct hci_trans_h4_uart_config uart_config;
  43. };
  44. extern void hci_trans_h4_init(struct hci_trans_h4_config *config);
  45. extern int hci_trans_h4_open(void);
  46. extern int hci_trans_h4_close(void);
  47. /**
  48. * @brief HCI transport h4 receive a byte from uart, used for hci_transport_h4_uart.c .
  49. *
  50. * @param byte A byte coming from uart.
  51. *
  52. * @return int
  53. * @retval HM_SUCCESS Receive byte success.
  54. * @retval -HM_NOT_SUPPORT H4 sync loss, or this package not support now.
  55. */
  56. extern int hci_trans_h4_recv_byte(uint8_t byte);
  57. /**
  58. * @brief Alloc a enough memory to send data.
  59. *
  60. * @param type The H4 package type, HCI_TRANS_H4_TYPE_CMD, HCI_TRANS_H4_TYPE_ACL, ...
  61. *
  62. * @return void*
  63. * @retval Non-NULL A memory to storage HCI data.
  64. * @retval NULL Alloc fail. Memory pool is mot enough or this type package not support send_alloc.
  65. *
  66. * @note If memory alloc success, need to free it use `hci_trans_h4_send_free` .
  67. */
  68. extern void *hci_trans_h4_send_alloc(uint8_t type);
  69. /**
  70. * @brief Free memory buffer alloc by `hci_trans_h4_send_alloc` .
  71. *
  72. * @param buf Memory buffer.
  73. */
  74. extern void hci_trans_h4_send_free(uint8_t *buf);
  75. /**
  76. * @brief HCI transport h4 send package to uart.
  77. *
  78. * @param type The H4 package type, HCI_TRANS_H4_TYPE_CMD, HCI_TRANS_H4_TYPE_ACL, ...
  79. * @param data HCI package data.
  80. *
  81. * @return int
  82. * @retval HM_SUCCESS Send success.
  83. * @retval -HM_NOT_SUPPORT This type package not support now.
  84. */
  85. extern int hci_trans_h4_send(uint8_t type, uint8_t *data);
  86. /**
  87. * @brief HCI transport h4 receive a packet, which type is not limited.
  88. *
  89. * @param buf A pointer to hci packet buffer when receive successfully.
  90. * @param ms Waitting time in ms. Specially, RT_WAITING_NO means no wait,
  91. * @param type A pointer to restore hci packet type.
  92. *
  93. * @return int
  94. * @retval HM_SUCCESS Read hci acl packet success.
  95. * @retval -HM_TIMEOUT Timeout.
  96. *
  97. * @note If this function return successfully, `buf` should be
  98. * freed with `hci_trans_h4_recv_free()` when it's not needed.
  99. *
  100. * @note ms shouldn't be RT_WAITING_FOREVER.
  101. */
  102. int hci_trans_h4_recv_all(uint8_t **buf, int ms, uint8_t *type);
  103. /**
  104. * @brief Free memory buffer, which is alloc by `hci_trans_h4_recv_*` API.
  105. *
  106. * @param p Memory buffer.
  107. */
  108. void hci_trans_h4_recv_free(uint8_t *p);
  109. #ifdef __cplusplus
  110. }
  111. #endif
  112. #endif /* HM_TRANS_H4_H */