esp_spp_api.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_SPP_API_H__
  14. #define __ESP_SPP_API_H__
  15. #include "esp_err.h"
  16. #include "esp_bt_defs.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. typedef enum {
  21. ESP_SPP_SUCCESS = 0, /*!< Successful operation. */
  22. ESP_SPP_FAILURE, /*!< Generic failure. */
  23. ESP_SPP_BUSY, /*!< Temporarily can not handle this request. */
  24. ESP_SPP_NO_DATA, /*!< no data. */
  25. ESP_SPP_NO_RESOURCE /*!< No more set pm control block */
  26. } esp_spp_status_t;
  27. /* Security Setting Mask */
  28. #define ESP_SPP_SEC_NONE 0x0000 /*!< No security. relate to BTA_SEC_NONE in bta_api.h */
  29. #define ESP_SPP_SEC_AUTHORIZE 0x0001 /*!< Authorization required (only needed for out going connection ) relate to BTA_SEC_AUTHORIZE in bta_api.h*/
  30. #define ESP_SPP_SEC_AUTHENTICATE 0x0012 /*!< Authentication required. relate to BTA_SEC_AUTHENTICATE in bta_api.h*/
  31. #define ESP_SPP_SEC_ENCRYPT 0x0024 /*!< Encryption required. relate to BTA_SEC_ENCRYPT in bta_api.h*/
  32. #define ESP_SPP_SEC_MODE4_LEVEL4 0x0040 /*!< Mode 4 level 4 service, i.e. incoming/outgoing MITM and P-256 encryption relate to BTA_SEC_MODE4_LEVEL4 in bta_api.h*/
  33. #define ESP_SPP_SEC_MITM 0x3000 /*!< Man-In-The_Middle protection relate to BTA_SEC_MITM in bta_api.h*/
  34. #define ESP_SPP_SEC_IN_16_DIGITS 0x4000 /*!< Min 16 digit for pin code relate to BTA_SEC_IN_16_DIGITS in bta_api.h*/
  35. typedef uint16_t esp_spp_sec_t;
  36. typedef enum {
  37. ESP_SPP_ROLE_MASTER = 0, /*!< Role: master */
  38. ESP_SPP_ROLE_SLAVE = 1, /*!< Role: slave */
  39. } esp_spp_role_t;
  40. typedef enum {
  41. ESP_SPP_MODE_CB = 0, /*!< When data is coming, a callback will come with data */
  42. ESP_SPP_MODE_VFS = 1, /*!< Use VFS to write/read data */
  43. } esp_spp_mode_t;
  44. #define ESP_SPP_MAX_MTU (3*330) /*!< SPP max MTU */
  45. #define ESP_SPP_MAX_SCN 31 /*!< SPP max SCN */
  46. /**
  47. * @brief SPP callback function events
  48. */
  49. typedef enum {
  50. ESP_SPP_INIT_EVT = 0, /*!< When SPP is inited, the event comes */
  51. ESP_SPP_DISCOVERY_COMP_EVT = 8, /*!< When SDP discovery complete, the event comes */
  52. ESP_SPP_OPEN_EVT = 26, /*!< When SPP Client connection open, the event comes */
  53. ESP_SPP_CLOSE_EVT = 27, /*!< When SPP connection closed, the event comes */
  54. ESP_SPP_START_EVT = 28, /*!< When SPP server started, the event comes */
  55. ESP_SPP_CL_INIT_EVT = 29, /*!< When SPP client initiated a connection, the event comes */
  56. ESP_SPP_DATA_IND_EVT = 30, /*!< When SPP connection received data, the event comes */
  57. ESP_SPP_CONG_EVT = 31, /*!< When SPP connection congestion status changed, the event comes */
  58. ESP_SPP_WRITE_EVT = 33, /*!< When SPP write operation completes, the event comes */
  59. ESP_SPP_SRV_OPEN_EVT = 34, /*!< When SPP Server connection open, the event comes */
  60. } esp_spp_cb_event_t;
  61. /**
  62. * @brief SPP callback parameters union
  63. */
  64. typedef union {
  65. /**
  66. * @brief SPP_INIT_EVT
  67. */
  68. struct spp_init_evt_param {
  69. esp_spp_status_t status; /*!< status */
  70. } init; /*!< SPP callback param of SPP_INIT_EVT */
  71. /**
  72. * @brief SPP_DISCOVERY_COMP_EVT
  73. */
  74. struct spp_discovery_comp_evt_param {
  75. esp_spp_status_t status; /*!< status */
  76. uint8_t scn_num; /*!< The num of scn_num */
  77. uint8_t scn[ESP_SPP_MAX_SCN]; /*!< channel # */
  78. } disc_comp; /*!< SPP callback param of SPP_DISCOVERY_COMP_EVT */
  79. /**
  80. * @brief ESP_SPP_OPEN_EVT
  81. */
  82. struct spp_open_evt_param {
  83. esp_spp_status_t status; /*!< status */
  84. uint32_t handle; /*!< The connection handle */
  85. esp_bd_addr_t rem_bda; /*!< The peer address */
  86. } open; /*!< SPP callback param of ESP_SPP_OPEN_EVT */
  87. /**
  88. * @brief ESP_SPP_SRV_OPEN_EVT
  89. */
  90. struct spp_srv_open_evt_param {
  91. esp_spp_status_t status; /*!< status */
  92. uint32_t handle; /*!< The connection handle */
  93. uint32_t new_listen_handle; /*!< The new listen handle */
  94. esp_bd_addr_t rem_bda; /*!< The peer address */
  95. } srv_open; /*!< SPP callback param of ESP_SPP_SRV_OPEN_EVT */
  96. /**
  97. * @brief ESP_SPP_CLOSE_EVT
  98. */
  99. struct spp_close_evt_param {
  100. esp_spp_status_t status; /*!< status */
  101. uint32_t port_status; /*!< PORT status */
  102. uint32_t handle; /*!< The connection handle */
  103. bool async; /*!< FALSE, if local initiates disconnect */
  104. } close; /*!< SPP callback param of ESP_SPP_CLOSE_EVT */
  105. /**
  106. * @brief ESP_SPP_START_EVT
  107. */
  108. struct spp_start_evt_param {
  109. esp_spp_status_t status; /*!< status */
  110. uint32_t handle; /*!< The connection handle */
  111. uint8_t sec_id; /*!< security ID used by this server */
  112. bool use_co; /*!< TRUE to use co_rfc_data */
  113. } start; /*!< SPP callback param of ESP_SPP_START_EVT */
  114. /**
  115. * @brief ESP_SPP_CL_INIT_EVT
  116. */
  117. struct spp_cl_init_evt_param {
  118. esp_spp_status_t status; /*!< status */
  119. uint32_t handle; /*!< The connection handle */
  120. uint8_t sec_id; /*!< security ID used by this server */
  121. bool use_co; /*!< TRUE to use co_rfc_data */
  122. } cl_init; /*!< SPP callback param of ESP_SPP_CL_INIT_EVT */
  123. /**
  124. * @brief ESP_SPP_WRITE_EVT
  125. */
  126. struct spp_write_evt_param {
  127. esp_spp_status_t status; /*!< status */
  128. uint32_t handle; /*!< The connection handle */
  129. uint32_t req_id; /*!< The req_id in the associated BTA_JvRfcommWrite() */
  130. int len; /*!< The length of the data written. */
  131. bool cong; /*!< congestion status */
  132. } write; /*!< SPP callback param of ESP_SPP_WRITE_EVT */
  133. /**
  134. * @brief ESP_SPP_DATA_IND_EVT
  135. */
  136. struct spp_data_ind_evt_param {
  137. esp_spp_status_t status; /*!< status */
  138. uint32_t handle; /*!< The connection handle */
  139. uint16_t len; /*!< The length of data */
  140. uint8_t *data; /*!< The data recived */
  141. } data_ind; /*!< SPP callback param of ESP_SPP_DATA_IND_EVT */
  142. /**
  143. * @brief ESP_SPP_CONG_EVT
  144. */
  145. struct spp_cong_evt_param {
  146. esp_spp_status_t status; /*!< status */
  147. uint32_t handle; /*!< The connection handle */
  148. bool cong; /*!< TRUE, congested. FALSE, uncongested */
  149. } cong; /*!< SPP callback param of ESP_SPP_CONG_EVT */
  150. } esp_spp_cb_param_t; /*!< SPP callback parameter union type */
  151. /**
  152. * @brief SPP callback function type
  153. * @param event: Event type
  154. * @param param: Point to callback parameter, currently is union type
  155. */
  156. typedef void (esp_spp_cb_t)(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
  157. /**
  158. * @brief This function is called to init callbacks
  159. * with SPP module.
  160. *
  161. * @param[in] callback: pointer to the init callback function.
  162. *
  163. * @return
  164. * - ESP_OK: success
  165. * - other: failed
  166. */
  167. esp_err_t esp_spp_register_callback(esp_spp_cb_t callback);
  168. /**
  169. * @brief This function is called to init SPP.
  170. *
  171. * @param[in] mode: Choose the mode of SPP, ESP_SPP_MODE_CB or ESP_SPP_MODE_CB.
  172. * Now only supports ESP_SPP_MODE_CB mode, we will continue to update.
  173. *
  174. * @return
  175. * - ESP_OK: success
  176. * - other: failed
  177. */
  178. esp_err_t esp_spp_init(esp_spp_mode_t mode);
  179. /**
  180. * @brief This function is called to uninit SPP.
  181. *
  182. * @return
  183. * - ESP_OK: success
  184. * - other: failed
  185. */
  186. esp_err_t esp_spp_deinit();
  187. /**
  188. * @brief This function is called to performs service discovery for
  189. * the services provided by the given peer device. When the
  190. * operation is complete the callback function will be called
  191. * with a ESP_SPP_DISCOVERY_COMP_EVT.
  192. *
  193. * @param[in] bd_addr: Remote device bluetooth device address.
  194. *
  195. * @return
  196. * - ESP_OK: success
  197. * - other: failed
  198. */
  199. esp_err_t esp_spp_start_discovery(esp_bd_addr_t bd_addr);
  200. /**
  201. * @brief This function makes an SPP conection to a remote BD Address.
  202. * When the connection is initiated or failed to initiate,
  203. * the callback is called with ESP_SPP_CL_INIT_EVT.
  204. * When the connection is established or failed,
  205. * the callback is called with ESP_SPP_OPEN_EVT.
  206. *
  207. * @param[in] sec_mask: Security Setting Mask .
  208. * @param[in] role: Msater or slave.
  209. * @param[in] remote_scn: Remote device bluetooth device SCN.
  210. * @param[in] peer_bd_addr: Remote device bluetooth device address.
  211. *
  212. * @return
  213. * - ESP_OK: success
  214. * - other: failed
  215. */
  216. esp_err_t esp_spp_connect(esp_spp_sec_t sec_mask,
  217. esp_spp_role_t role, uint8_t remote_scn, esp_bd_addr_t peer_bd_addr);
  218. /**
  219. * @brief This function closes an SPP connection.
  220. *
  221. * @param[in] handle: The connection handle.
  222. *
  223. * @return
  224. * - ESP_OK: success
  225. * - other: failed
  226. */
  227. esp_err_t esp_spp_disconnect(uint32_t handle);
  228. /**
  229. * @brief This function create a SPP server and starts listening for an
  230. * SPP connection request from a remote Bluetooth device.
  231. * When the server is started successfully, the callback is called
  232. * with ESP_SPP_START_EVT.
  233. * When the connection is established, the callback is called
  234. * with ESP_SPP_SRV_OPEN_EVT.
  235. *
  236. * @param[in] sec_mask: Security Setting Mask .
  237. * @param[in] role: Msater or slave.
  238. * @param[in] local_scn: The specific channel you want to get.
  239. * If channel is 0, means get any channel.
  240. * @param[in] name: Server's name.
  241. *
  242. * @return
  243. * - ESP_OK: success
  244. * - other: failed
  245. */
  246. esp_err_t esp_spp_start_srv(esp_spp_sec_t sec_mask,
  247. esp_spp_role_t role, uint8_t local_scn, const char *name);
  248. /**
  249. * @brief This function closes an SPP connection.
  250. *
  251. * @param[in] handle: The connection handle.
  252. * @param[in] len: The length of the data written.
  253. * @param[in] p_data: The data written.
  254. *
  255. * @return
  256. * - ESP_OK: success
  257. * - other: failed
  258. */
  259. esp_err_t esp_spp_write(uint32_t handle, int len, uint8_t *p_data);
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif ///__ESP_SPP_API_H__