You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.3 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #ifndef ZDATACHANNEL_H__
  2. #define ZDATACHANNEL_H__
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include "ble.h"
  6. #include "ble_link_ctx_manager.h"
  7. #include "ble_srv_common.h"
  8. #include "nrf_sdh_ble.h"
  9. #include "sdk_config.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**@brief Macro for defining a ble_nus instance.
  14. *
  15. * @param _name Name of the instance.
  16. * @param[in] _nus_max_clients Maximum number of NUS clients connected at a time.
  17. * @hideinitializer
  18. */
  19. #define ZDATACHANNEL_DEF(_name, _ble_observer_prio, _nus_max_clients) \
  20. static zdatachannel_t _name; \
  21. NRF_SDH_BLE_OBSERVER(_name##_obs, _ble_observer_prio, zdatachannel_on_ble_evt, &_name)
  22. #define OPCODE_LENGTH 1
  23. #define HANDLE_LENGTH 2
  24. /**@brief Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  25. #if defined(NRF_SDH_BLE_GATT_MAX_MTU_SIZE) && (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 0)
  26. #define ZDATACHANNEL_MAX_DATA_LEN (NRF_SDH_BLE_GATT_MAX_MTU_SIZE - OPCODE_LENGTH - HANDLE_LENGTH)
  27. #else
  28. #define ZDATACHANNEL_MAX_DATA_LEN (BLE_GATT_MTU_SIZE_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH)
  29. #warning NRF_SDH_BLE_GATT_MAX_MTU_SIZE is not defined.
  30. #endif
  31. /**@brief Nordic UART Service event types. */
  32. typedef enum {
  33. ZDATACHANNEL_EVT_RX_DATA, /**< Data received. */
  34. } zdatachannel_evt_type_t;
  35. typedef struct zdatachannel_s zdatachannel_t;
  36. typedef struct {
  37. uint8_t const* p_data; /**< A pointer to the buffer with received data. */
  38. uint16_t length; /**< Length of received data. */
  39. } zdatachannel_evt_rx_data_t;
  40. typedef struct {
  41. zdatachannel_evt_type_t type; /**< Event type. */
  42. union {
  43. zdatachannel_evt_rx_data_t rx_data; /**< @ref ZDATACHANNEL_EVT_RX_DATA event data. */
  44. } params;
  45. } zdatachannel_evt_t;
  46. typedef void (*zdatachannel_data_handler_t)(zdatachannel_evt_t* p_evt);
  47. typedef struct {
  48. zdatachannel_data_handler_t data_handler;
  49. } zdatachannel_init_t;
  50. struct zdatachannel_s {
  51. uint8_t uuid_type; /**< UUID type for Nordic UART Service Base UUID. */
  52. uint16_t service_handle; /**< Handle of Nordic UART Service (as provided by the SoftDevice). */
  53. ble_gatts_char_handles_t cmd_tx_handles; /**< ָ��;��� */
  54. ble_gatts_char_handles_t cmd_rx_handles; /**< ָ�����վ��� */
  55. ble_gatts_char_handles_t datablock_tx_handles; /**< ���ݿ鷢�;��� */
  56. zdatachannel_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
  57. // ״̬
  58. int16_t conn_handle;
  59. bool cmd_tx_channel_is_notification_enabled;
  60. bool datablock_tx_channel_is_notification_enabled;
  61. };
  62. uint32_t zdatachannel_init(zdatachannel_t* p_nus, zdatachannel_init_t const* p_nus_init);
  63. void zdatachannel_on_ble_evt(ble_evt_t const* p_ble_evt, void* p_context);
  64. bool zdatachannel_is_connected();
  65. uint32_t zdatachannel_data_send(uint8_t* p_data, uint16_t* p_length);
  66. static inline uint32_t zdatachannel_data_send2(uint8_t* p_data, uint16_t p_length){
  67. return zdatachannel_data_send(p_data, &p_length);
  68. }
  69. uint32_t zdatachannel_block_data_send(uint8_t* p_data, uint16_t* p_length);
  70. static inline uint32_t zdatachannel_block_data_send2(uint8_t* p_data, uint16_t p_length){
  71. return zdatachannel_block_data_send(p_data, &p_length);
  72. }
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif // ZDATACHANNEL_H__
  77. /** @} */