ble.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef __BLE_C_H
  2. #define __BLE_C_H
  3. #include "bluetooth.h"
  4. #ifndef UNUSED
  5. #define UNUSED(x) (void)(sizeof(x))
  6. #endif
  7. void ble_init(void *init);
  8. void ble_mainloop(void);
  9. typedef struct {
  10. uint16_t (*att_read_callback)(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
  11. int (*att_write_callback)(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  12. void (*packet_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  13. const uint8_t *ble_device_db;
  14. }blestack_init_t;
  15. /**
  16. * State of BTstack
  17. */
  18. typedef enum {
  19. HCI_STATE_OFF = 0,
  20. HCI_STATE_INITIALIZING,
  21. HCI_STATE_WORKING,
  22. HCI_STATE_HALTING,
  23. HCI_STATE_SLEEPING,
  24. HCI_STATE_FALLING_ASLEEP
  25. } HCI_STATE;
  26. #define BTSTACK_EVENT_STATE 0x60
  27. #define BTSTACK_EVENT_NR_CONNECTIONS_CHANGED 0x61
  28. #define DAEMON_EVENT_VERSION 0x63
  29. #define L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE 0x77
  30. #define ATT_EVENT_MTU_EXCHANGE_COMPLETE 0xB5
  31. #define ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE 0xB6
  32. #define ATT_EVENT_CAN_SEND_NOW 0xB7
  33. /**
  34. * @brief Sets a fixed random address for advertising
  35. * @param addr
  36. * @note Sets random address mode to type off
  37. */
  38. void gap_random_address_set(bd_addr_t addr);
  39. /**
  40. * @brief Set Advertisement Data
  41. * @param advertising_data_length
  42. * @param advertising_data (max 31 octets)
  43. * @note data is not copied, pointer has to stay valid
  44. * @note '00:00:00:00:00:00' in advertising_data will be replaced with actual bd addr
  45. */
  46. void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data);
  47. /**
  48. * @brief Set Advertisement Paramters
  49. * @param adv_int_min
  50. * @param adv_int_max
  51. * @param adv_type
  52. * @param direct_address_type
  53. * @param direct_address
  54. * @param channel_map
  55. * @param filter_policy
  56. * @note own_address_type is used from gap_random_address_set_mode
  57. */
  58. void gap_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
  59. uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy);
  60. /**
  61. * @brief Enable/Disable Advertisements. OFF by default.
  62. * @param enabled
  63. */
  64. void gap_advertisements_enable(int enabled);
  65. /**
  66. * @brief Set Scan Response Data
  67. *
  68. * @note For scan response data, scannable undirected advertising (ADV_SCAN_IND) need to be used
  69. *
  70. * @param advertising_data_length
  71. * @param advertising_data (max 31 octets)
  72. * @note data is not copied, pointer has to stay valid
  73. * @note '00:00:00:00:00:00' in scan_response_data will be replaced with actual bd addr
  74. */
  75. void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data);
  76. static inline uint16_t little_endian_read_16(const uint8_t * buffer, int pos){
  77. return (uint16_t)(((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8));
  78. }
  79. /**
  80. * @brief Get event type
  81. * @param event
  82. * @return type of event
  83. */
  84. static inline uint8_t hci_event_packet_get_type(const uint8_t * event){
  85. return event[0];
  86. }
  87. /**
  88. * @brief Get field state from event BTSTACK_EVENT_STATE
  89. * @param event packet
  90. * @return state
  91. * @note: btstack_type 1
  92. */
  93. static inline uint8_t btstack_event_state_get_state(const uint8_t * event){
  94. return event[2];
  95. }
  96. /***
  97. * @brief Get subevent code for le event
  98. * @param event packet
  99. * @return subevent_code
  100. */
  101. static inline uint8_t hci_event_le_meta_get_subevent_code(const uint8_t * event){
  102. return event[2];
  103. }
  104. /**
  105. * @brief Get field status from event HCI_EVENT_CONNECTION_COMPLETE
  106. * @param event packet
  107. * @return status
  108. * @note: btstack_type 1
  109. */
  110. static inline uint8_t hci_event_connection_complete_get_status(const uint8_t * event){
  111. return event[2];
  112. }
  113. /**
  114. * @brief Get field connection_handle from event HCI_EVENT_CONNECTION_COMPLETE
  115. * @param event packet
  116. * @return connection_handle
  117. * @note: btstack_type 2
  118. */
  119. static inline uint16_t hci_event_connection_complete_get_connection_handle(const uint8_t * event){
  120. return little_endian_read_16(event, 3);
  121. }
  122. /**
  123. * @brief Get field connection_handle from event HCI_EVENT_DISCONNECTION_COMPLETE
  124. * @param event packet
  125. * @return connection_handle
  126. * @note: btstack_type 2
  127. */
  128. static inline uint16_t hci_event_disconnection_complete_get_connection_handle(const uint8_t * event){
  129. return little_endian_read_16(event, 3);
  130. }
  131. /**
  132. * @brief Get field connection_handle from event HCI_SUBEVENT_LE_CONNECTION_COMPLETE
  133. * @param event packet
  134. * @return connection_handle
  135. * @note: btstack_type H
  136. */
  137. static inline hci_con_handle_t hci_subevent_le_connection_complete_get_connection_handle(const uint8_t * event){
  138. return little_endian_read_16(event, 4);
  139. }
  140. /**
  141. * @brief Get field result from event L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE
  142. * @param event packet
  143. * @return result
  144. * @note: btstack_type 2
  145. */
  146. static inline uint16_t l2cap_event_connection_parameter_update_response_get_result(const uint8_t * event){
  147. return little_endian_read_16(event, 4);
  148. }
  149. /**
  150. * @brief Get field reason from event HCI_EVENT_DISCONNECTION_COMPLETE
  151. * @param event packet
  152. * @return reason
  153. * @note: btstack_type 1
  154. */
  155. static inline uint8_t hci_event_disconnection_complete_get_reason(const uint8_t * event){
  156. return event[5];
  157. }
  158. /**
  159. * @brief Get field MTU from event ATT_EVENT_MTU_EXCHANGE_COMPLETE
  160. * @param event packet
  161. * @return MTU
  162. * @note: btstack_type 2
  163. */
  164. static inline uint16_t att_event_mtu_exchange_complete_get_MTU(const uint8_t * event){
  165. return little_endian_read_16(event, 4);
  166. }
  167. /**
  168. * @brief Request an update of the connection parameter for a given LE connection
  169. * @param handle
  170. * @param conn_interval_min (unit: 1.25ms)
  171. * @param conn_interval_max (unit: 1.25ms)
  172. * @param conn_latency
  173. * @param supervision_timeout (unit: 10ms)
  174. * @returns 0 if ok
  175. */
  176. int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
  177. uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout);
  178. /*
  179. * @brief notify client about attribute value change
  180. * @param con_handle
  181. * @param attribute_handle
  182. * @param value
  183. * @param value_len
  184. * @return 0 if ok, error otherwise
  185. */
  186. int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len);
  187. /*
  188. * @brief indicate value change to client. client is supposed to reply with an indication_response
  189. * @param con_handle
  190. * @param attribute_handle
  191. * @param value
  192. * @param value_len
  193. * @return 0 if ok, error otherwise
  194. */
  195. int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len);
  196. /**
  197. * @brief Get field status from event ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE
  198. * @param event packet
  199. * @return status
  200. * @note: btstack_type 1
  201. */
  202. static inline uint8_t att_event_handle_value_indication_complete_get_status(const uint8_t * event){
  203. return event[2];
  204. }
  205. #endif