att.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*********************************************************************************************
  2. * Filename : att.h
  3. * Description :
  4. * Author : minxian
  5. * Email : liminxian@zh-jieli.com
  6. * Last modifiled : 2020-07-01 16:33
  7. * Copyright:(c)JIELI 2011-2020 @ , All Rights Reserved.
  8. *********************************************************************************************/
  9. #ifndef _BT_ATT_H_
  10. #define _BT_ATT_H_
  11. #include "typedef.h"
  12. #include "btstack/btstack_typedef.h"
  13. #if defined __cplusplus
  14. extern "C" {
  15. #endif
  16. // Minimum/default MTU
  17. #define ATT_DEFAULT_MTU 23
  18. #define ATT_EVENT_MTU_EXCHANGE_COMPLETE 0xB5
  19. #define ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE 0xB6
  20. #define ATT_EVENT_CAN_SEND_NOW 0xB7
  21. #define ATT_TRANSACTION_MODE_NONE 0x0
  22. #define ATT_TRANSACTION_MODE_ACTIVE 0x1
  23. #define ATT_TRANSACTION_MODE_EXECUTE 0x2
  24. #define ATT_TRANSACTION_MODE_CANCEL 0x3
  25. #define ATT_TRANSACTION_MODE_VALIDATE 0x4
  26. #define ATT_PROPERTY_BROADCAST 0x01
  27. #define ATT_PROPERTY_READ 0x02
  28. #define ATT_PROPERTY_WRITE_WITHOUT_RESPONSE 0x04
  29. #define ATT_PROPERTY_WRITE 0x08
  30. #define ATT_PROPERTY_NOTIFY 0x10
  31. #define ATT_PROPERTY_INDICATE 0x20
  32. typedef enum {
  33. ATT_OP_AUTO_READ_CCC = 0, //server端 检查对方使能通知CCC来控制 notify or indicate发送
  34. ATT_OP_NOTIFY = 1, //server端 默认notify方式发送,不检查使能通知CCC (不需要对方回应答包,没有流控)
  35. ATT_OP_INDICATE = 2, //server端 默认INDICATE方式发送,不检查使能通知CCC (需要对方回应答包,有流控)
  36. ATT_OP_READ, //client端 单次读, 用于获取<=MTU 的数据包 (需要对方回应答包,有流控)
  37. ATT_OP_READ_LONG, //client端 多次读,用于支持获取>MTU 的数据包 (需要对方回应答包,有流控)
  38. ATT_OP_WRITE, //client端 写发送 (需要对方回应答包,有流控)
  39. ATT_OP_WRITE_WITHOUT_RESPOND,//client端 写发送 (不需要对方回应答包,没有流控)
  40. //add here
  41. ATT_OP_CMD_MAX = 15,
  42. } att_op_type_e;
  43. //------
  44. typedef struct {
  45. uint16_t start_group_handle;//
  46. uint16_t end_group_handle;
  47. uint16_t uuid16; //为0则是 uuid128
  48. uint8_t uuid128[16];
  49. } service_report_t; //==le_service_t
  50. typedef struct {
  51. uint16_t start_handle;
  52. uint16_t value_handle; //属性操作handle
  53. uint16_t end_handle;
  54. uint16_t properties; //属性对应 ATT_PROPERTY_XXX
  55. uint16_t uuid16; //为0则是 uuid128
  56. uint8_t uuid128[16];
  57. } charact_report_t; //==le_characteristic_t
  58. typedef struct {
  59. u16 packet_type; //数据包类型(notify,indicate,read_respone,...)
  60. u16 value_handle; //属性操作handle
  61. u16 value_offset; //包偏移
  62. u16 blob_length; //包长度
  63. u8 *blob; //包内容
  64. u16 conn_handle; //连接handle
  65. } att_data_report_t;
  66. typedef struct {
  67. service_report_t services;
  68. charact_report_t characteristic;
  69. u16 service_index;
  70. u16 characteristic_index;
  71. } search_result_t;
  72. typedef struct {
  73. u16 handle;//descriptor's handle
  74. u16 uuid16;//为0则是 uuid128
  75. u8 uuid128[16];
  76. } charact_descriptor_t;
  77. void att_ccc_config_init(void);
  78. void att_set_ccc_config(uint16_t handle, uint16_t cfg);
  79. uint16_t att_get_ccc_config(uint16_t handle);
  80. void att_server_set_exchange_mtu(u16 con_handle);
  81. void att_set_db(uint8_t const *db);//change profile_data
  82. //多机接口
  83. //初始化 CCC管理
  84. void multi_att_ccc_config_init(void);
  85. //设置CCC
  86. void multi_att_set_ccc_config(uint16_t conn_handle, uint16_t att_handle, uint16_t cfg);
  87. //获取CCC的值
  88. uint16_t multi_att_get_ccc_config(uint16_t conn_handle, uint16_t att_handle);
  89. //断开, 清链路CCC
  90. int multi_att_clear_ccc_config(uint16_t conn_handle);
  91. // ATT Client Read Callback for Dynamic Data
  92. // - if buffer == NULL, don't copy data, just return size of value
  93. // - if buffer != NULL, copy data and return number bytes copied
  94. // @param con_handle of hci le connection
  95. // @param attribute_handle to be read
  96. // @param offset defines start of attribute value
  97. // @param buffer
  98. // @param buffer_size
  99. typedef uint16_t (*att_read_callback_t)(uint16_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  100. // ATT Client Write Callback for Dynamic Data
  101. // @param con_handle of hci le connection
  102. // @param attribute_handle to be written
  103. // @param transaction - ATT_TRANSACTION_MODE_NONE for regular writes, ATT_TRANSACTION_MODE_ACTIVE for prepared writes and ATT_TRANSACTION_MODE_EXECUTE
  104. // @param offset into the value - used for queued writes and long attributes
  105. // @param buffer
  106. // @param buffer_size
  107. // @param signature used for signed write commmands
  108. // @returns 0 if write was ok, ATT_ERROR_PREPARE_QUEUE_FULL if no space in queue, ATT_ERROR_INVALID_OFFSET if offset is larger than max buffer
  109. typedef int (*att_write_callback_t)(uint16_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  110. void ble_att_server_setup_init(const u8 *profile_db, att_read_callback_t read_cbk, att_write_callback_t write_cbk);
  111. void att_server_request_can_send_now_event(hci_con_handle_t con_handle);
  112. int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len);
  113. int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len);
  114. extern void att_server_init(uint8_t const *db, att_read_callback_t read_callback, att_write_callback_t write_callback);
  115. extern void att_server_register_packet_handler(btstack_packet_handler_t handler);
  116. #endif