protocomm_ble.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * BLE device name cannot be larger than this value
  13. * 31 bytes (max scan response size) - 1 byte (length) - 1 byte (type) = 29 bytes
  14. */
  15. #define MAX_BLE_DEVNAME_LEN 29
  16. #define BLE_UUID128_VAL_LENGTH 16
  17. /**
  18. * Theoretically, the limit for max manufacturer length remains same as BLE
  19. * device name i.e. 31 bytes (max scan response size) - 1 byte (length) - 1
  20. * byte (type) = 29 bytes
  21. * However, manufacturer data goes along with BLE device name in scan response.
  22. * So, it is important to understand the actual length should be smaller than
  23. * (29 - (BLE device name length) - 2). */
  24. #define MAX_BLE_MANUFACTURER_DATA_LEN 29
  25. /**
  26. * @brief This structure maps handler required by protocomm layer to
  27. * UUIDs which are used to uniquely identify BLE characteristics
  28. * from a smartphone or a similar client device.
  29. */
  30. typedef struct name_uuid {
  31. /**
  32. * Name of the handler, which is passed to protocomm layer
  33. */
  34. const char *name;
  35. /**
  36. * UUID to be assigned to the BLE characteristic which is
  37. * mapped to the handler
  38. */
  39. uint16_t uuid;
  40. } protocomm_ble_name_uuid_t;
  41. /**
  42. * @brief Config parameters for protocomm BLE service
  43. */
  44. typedef struct protocomm_ble_config {
  45. /**
  46. * BLE device name being broadcast at the time of provisioning
  47. */
  48. char device_name[MAX_BLE_DEVNAME_LEN];
  49. /**
  50. * 128 bit UUID of the provisioning service
  51. */
  52. uint8_t service_uuid[BLE_UUID128_VAL_LENGTH];
  53. /**
  54. * BLE device manufacturer data pointer in advertisement
  55. */
  56. uint8_t *manufacturer_data;
  57. /**
  58. * BLE device manufacturer data length in advertisement
  59. */
  60. ssize_t manufacturer_data_len;
  61. /**
  62. * Number of entries in the Name-UUID lookup table
  63. */
  64. ssize_t nu_lookup_count;
  65. /**
  66. * Pointer to the Name-UUID lookup table
  67. */
  68. protocomm_ble_name_uuid_t *nu_lookup;
  69. /**
  70. * BLE bonding
  71. */
  72. unsigned ble_bonding:1;
  73. /**
  74. * BLE security flag
  75. */
  76. unsigned ble_sm_sc:1;
  77. } protocomm_ble_config_t;
  78. /**
  79. * @brief Start Bluetooth Low Energy based transport layer for provisioning
  80. *
  81. * Initialize and start required BLE service for provisioning. This includes
  82. * the initialization for characteristics/service for BLE.
  83. *
  84. * @param[in] pc Protocomm instance pointer obtained from protocomm_new()
  85. * @param[in] config Pointer to config structure for initializing BLE
  86. *
  87. * @return
  88. * - ESP_OK : Success
  89. * - ESP_FAIL : Simple BLE start error
  90. * - ESP_ERR_NO_MEM : Error allocating memory for internal resources
  91. * - ESP_ERR_INVALID_STATE : Error in ble config
  92. * - ESP_ERR_INVALID_ARG : Null arguments
  93. */
  94. esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *config);
  95. /**
  96. * @brief Stop Bluetooth Low Energy based transport layer for provisioning
  97. *
  98. * Stops service/task responsible for BLE based interactions for provisioning
  99. *
  100. * @note You might want to optionally reclaim memory from Bluetooth.
  101. * Refer to the documentation of `esp_bt_mem_release` in that case.
  102. *
  103. * @param[in] pc Same protocomm instance that was passed to protocomm_ble_start()
  104. *
  105. * @return
  106. * - ESP_OK : Success
  107. * - ESP_FAIL : Simple BLE stop error
  108. * - ESP_ERR_INVALID_ARG : Null / incorrect protocomm instance
  109. */
  110. esp_err_t protocomm_ble_stop(protocomm_t *pc);
  111. #ifdef __cplusplus
  112. }
  113. #endif