protocomm_ble.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <protocomm.h>
  8. #include "esp_event.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. ESP_EVENT_DECLARE_BASE(PROTOCOMM_TRANSPORT_BLE_EVENT);
  13. /**
  14. * @brief Events generated by BLE transport
  15. *
  16. * These events are generated when the BLE transport is paired
  17. * and disconnected.
  18. */
  19. typedef enum {
  20. PROTOCOMM_TRANSPORT_BLE_CONNECTED,
  21. PROTOCOMM_TRANSPORT_BLE_DISCONNECTED,
  22. } protocomm_transport_ble_event_t;
  23. /**
  24. * BLE device name cannot be larger than this value
  25. * 31 bytes (max scan response size) - 1 byte (length) - 1 byte (type) = 29 bytes
  26. */
  27. #define MAX_BLE_DEVNAME_LEN 29
  28. #define BLE_UUID128_VAL_LENGTH 16
  29. /**
  30. * Theoretically, the limit for max manufacturer length remains same as BLE
  31. * device name i.e. 31 bytes (max scan response size) - 1 byte (length) - 1
  32. * byte (type) = 29 bytes
  33. * However, manufacturer data goes along with BLE device name in scan response.
  34. * So, it is important to understand the actual length should be smaller than
  35. * (29 - (BLE device name length) - 2). */
  36. #define MAX_BLE_MANUFACTURER_DATA_LEN 29
  37. /**
  38. * @brief This structure maps handler required by protocomm layer to
  39. * UUIDs which are used to uniquely identify BLE characteristics
  40. * from a smartphone or a similar client device.
  41. */
  42. typedef struct name_uuid {
  43. /**
  44. * Name of the handler, which is passed to protocomm layer
  45. */
  46. const char *name;
  47. /**
  48. * UUID to be assigned to the BLE characteristic which is
  49. * mapped to the handler
  50. */
  51. uint16_t uuid;
  52. } protocomm_ble_name_uuid_t;
  53. /**
  54. * @brief Structure for BLE events in Protocomm.
  55. */
  56. typedef struct {
  57. /**
  58. * This field indicates the type of BLE event that occurred.
  59. */
  60. uint16_t evt_type;
  61. /**
  62. * The handle of the relevant connection.
  63. */
  64. uint16_t conn_handle;
  65. union {
  66. /**
  67. * The status of the connection attempt;
  68. * o 0: the connection was successfully established.
  69. * o BLE host error code: the connection attempt failed for
  70. * the specified reason.
  71. */
  72. uint16_t conn_status;
  73. /**
  74. * Return code indicating the reason for the disconnect.
  75. */
  76. uint16_t disconnect_reason;
  77. };
  78. } protocomm_ble_event_t;
  79. /**
  80. * @brief Config parameters for protocomm BLE service
  81. */
  82. typedef struct protocomm_ble_config {
  83. /**
  84. * BLE device name being broadcast at the time of provisioning
  85. */
  86. char device_name[MAX_BLE_DEVNAME_LEN + 1];
  87. /**
  88. * 128 bit UUID of the provisioning service
  89. */
  90. uint8_t service_uuid[BLE_UUID128_VAL_LENGTH];
  91. /**
  92. * BLE device manufacturer data pointer in advertisement
  93. */
  94. uint8_t *manufacturer_data;
  95. /**
  96. * BLE device manufacturer data length in advertisement
  97. */
  98. ssize_t manufacturer_data_len;
  99. /**
  100. * Number of entries in the Name-UUID lookup table
  101. */
  102. ssize_t nu_lookup_count;
  103. /**
  104. * Pointer to the Name-UUID lookup table
  105. */
  106. protocomm_ble_name_uuid_t *nu_lookup;
  107. /**
  108. * BLE bonding
  109. */
  110. unsigned ble_bonding:1;
  111. /**
  112. * BLE security flag
  113. */
  114. unsigned ble_sm_sc:1;
  115. /**
  116. * BLE security flag
  117. */
  118. unsigned ble_link_encryption:1;
  119. } protocomm_ble_config_t;
  120. /**
  121. * @brief Start Bluetooth Low Energy based transport layer for provisioning
  122. *
  123. * Initialize and start required BLE service for provisioning. This includes
  124. * the initialization for characteristics/service for BLE.
  125. *
  126. * @param[in] pc Protocomm instance pointer obtained from protocomm_new()
  127. * @param[in] config Pointer to config structure for initializing BLE
  128. *
  129. * @return
  130. * - ESP_OK : Success
  131. * - ESP_FAIL : Simple BLE start error
  132. * - ESP_ERR_NO_MEM : Error allocating memory for internal resources
  133. * - ESP_ERR_INVALID_STATE : Error in ble config
  134. * - ESP_ERR_INVALID_ARG : Null arguments
  135. */
  136. esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *config);
  137. /**
  138. * @brief Stop Bluetooth Low Energy based transport layer for provisioning
  139. *
  140. * Stops service/task responsible for BLE based interactions for provisioning
  141. *
  142. * @note You might want to optionally reclaim memory from Bluetooth.
  143. * Refer to the documentation of `esp_bt_mem_release` in that case.
  144. *
  145. * @param[in] pc Same protocomm instance that was passed to protocomm_ble_start()
  146. *
  147. * @return
  148. * - ESP_OK : Success
  149. * - ESP_FAIL : Simple BLE stop error
  150. * - ESP_ERR_INVALID_ARG : Null / incorrect protocomm instance
  151. */
  152. esp_err_t protocomm_ble_stop(protocomm_t *pc);
  153. #ifdef __cplusplus
  154. }
  155. #endif