gap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /* Includes */
  7. #include "gap.h"
  8. #include "common.h"
  9. /* Private function declarations */
  10. inline static void format_addr(char *addr_str, uint8_t addr[]);
  11. static void start_advertising(void);
  12. /* Private variables */
  13. static uint8_t own_addr_type;
  14. static uint8_t addr_val[6] = {0};
  15. static uint8_t esp_uri[] = {BLE_GAP_URI_PREFIX_HTTPS, '/', '/', 'e', 's', 'p', 'r', 'e', 's', 's', 'i', 'f', '.', 'c', 'o', 'm'};
  16. /* Private functions */
  17. inline static void format_addr(char *addr_str, uint8_t addr[]) {
  18. sprintf(addr_str, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1],
  19. addr[2], addr[3], addr[4], addr[5]);
  20. }
  21. static void start_advertising(void) {
  22. /* Local variables */
  23. int rc = 0;
  24. const char *name;
  25. struct ble_hs_adv_fields adv_fields = {0};
  26. struct ble_hs_adv_fields rsp_fields = {0};
  27. struct ble_gap_adv_params adv_params = {0};
  28. /* Set advertising flags */
  29. adv_fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
  30. /* Set device name */
  31. name = ble_svc_gap_device_name();
  32. adv_fields.name = (uint8_t *)name;
  33. adv_fields.name_len = strlen(name);
  34. adv_fields.name_is_complete = 1;
  35. /* Set device tx power */
  36. adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
  37. adv_fields.tx_pwr_lvl_is_present = 1;
  38. /* Set device appearance */
  39. adv_fields.appearance = BLE_GAP_APPEARANCE_GENERIC_TAG;
  40. adv_fields.appearance_is_present = 1;
  41. /* Set device LE role */
  42. adv_fields.le_role = BLE_GAP_LE_ROLE_PERIPHERAL;
  43. adv_fields.le_role_is_present = 1;
  44. /* Set advertiement fields */
  45. rc = ble_gap_adv_set_fields(&adv_fields);
  46. if (rc != 0) {
  47. ESP_LOGE(TAG, "failed to set advertising data, error code: %d", rc);
  48. return;
  49. }
  50. /* Set device address */
  51. rsp_fields.device_addr = addr_val;
  52. rsp_fields.device_addr_type = own_addr_type;
  53. rsp_fields.device_addr_is_present = 1;
  54. /* Set URI */
  55. rsp_fields.uri = esp_uri;
  56. rsp_fields.uri_len = sizeof(esp_uri);
  57. /* Set scan response fields */
  58. rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
  59. if (rc != 0) {
  60. ESP_LOGE(TAG, "failed to set scan response data, error code: %d", rc);
  61. return;
  62. }
  63. /* Set non-connetable and general discoverable mode to be a beacon */
  64. adv_params.conn_mode = BLE_GAP_CONN_MODE_NON;
  65. adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
  66. /* Start advertising */
  67. rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params,
  68. NULL, NULL);
  69. if (rc != 0) {
  70. ESP_LOGE(TAG, "failed to start advertising, error code: %d", rc);
  71. return;
  72. }
  73. ESP_LOGI(TAG, "advertising started!");
  74. }
  75. /* Public functions */
  76. void adv_init(void) {
  77. /* Local variables */
  78. int rc = 0;
  79. char addr_str[18] = {0};
  80. /* Make sure we have proper BT identity address set */
  81. rc = ble_hs_util_ensure_addr(0);
  82. if (rc != 0) {
  83. ESP_LOGE(TAG, "device does not have any available bt address!");
  84. return;
  85. }
  86. /* Figure out BT address to use while advertising */
  87. rc = ble_hs_id_infer_auto(0, &own_addr_type);
  88. if (rc != 0) {
  89. ESP_LOGE(TAG, "failed to infer address type, error code: %d", rc);
  90. return;
  91. }
  92. /* Copy device address to addr_val */
  93. rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
  94. if (rc != 0) {
  95. ESP_LOGE(TAG, "failed to copy device address, error code: %d", rc);
  96. return;
  97. }
  98. format_addr(addr_str, addr_val);
  99. ESP_LOGI(TAG, "device address: %s", addr_str);
  100. /* Start advertising. */
  101. start_advertising();
  102. }
  103. int gap_init(void) {
  104. /* Local variables */
  105. int rc = 0;
  106. /* Initialize GAP service */
  107. ble_svc_gap_init();
  108. /* Set GAP device name */
  109. rc = ble_svc_gap_device_name_set(DEVICE_NAME);
  110. if (rc != 0) {
  111. ESP_LOGE(TAG, "failed to set device name to %s, error code: %d",
  112. DEVICE_NAME, rc);
  113. return rc;
  114. }
  115. /* Set GAP device appearance */
  116. rc = ble_svc_gap_device_appearance_set(BLE_GAP_APPEARANCE_GENERIC_TAG);
  117. if (rc != 0) {
  118. ESP_LOGE(TAG, "failed to set device appearance, error code: %d", rc);
  119. return rc;
  120. }
  121. return rc;
  122. }