|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
@@ -79,6 +79,7 @@ static struct ble_hs_adv_fields adv_data, resp_data; |
79 | 79 | static uint8_t *protocomm_ble_mfg_data; |
80 | 80 | static size_t protocomm_ble_mfg_data_len; |
81 | 81 |
|
| 82 | +static uint8_t *protocomm_ble_addr; |
82 | 83 | /********************************************************************** |
83 | 84 | * Maintain database of uuid_name addresses to free memory afterwards * |
84 | 85 | **********************************************************************/ |
@@ -130,6 +131,8 @@ typedef struct { |
130 | 131 | unsigned ble_sm_sc:1; |
131 | 132 | /** BLE Link Encryption flag */ |
132 | 133 | unsigned ble_link_encryption:1; |
| 134 | + /** BLE address */ |
| 135 | + uint8_t *ble_addr; |
133 | 136 | } simple_ble_cfg_t; |
134 | 137 |
|
135 | 138 | static simple_ble_cfg_t *ble_cfg_p; |
@@ -461,10 +464,25 @@ simple_ble_on_sync(void) |
461 | 464 | { |
462 | 465 | int rc; |
463 | 466 |
|
464 | | - rc = ble_hs_util_ensure_addr(0); |
465 | | - if (rc != 0) { |
466 | | - ESP_LOGE(TAG, "Error loading address"); |
467 | | - return; |
| 467 | + if (protocomm_ble_addr) { |
| 468 | + rc = ble_hs_id_set_rnd(protocomm_ble_addr); |
| 469 | + if (rc != 0) { |
| 470 | + ESP_LOGE(TAG,"Error in setting address"); |
| 471 | + return; |
| 472 | + } |
| 473 | + |
| 474 | + rc = ble_hs_util_ensure_addr(1); |
| 475 | + if (rc != 0) { |
| 476 | + ESP_LOGE(TAG,"Error loading address"); |
| 477 | + return; |
| 478 | + } |
| 479 | + } |
| 480 | + else { |
| 481 | + rc = ble_hs_util_ensure_addr(0); |
| 482 | + if (rc != 0) { |
| 483 | + ESP_LOGE(TAG, "Error loading address"); |
| 484 | + return; |
| 485 | + } |
468 | 486 | } |
469 | 487 |
|
470 | 488 | /* Figure out address to use while advertising (no privacy for now) */ |
@@ -727,7 +745,7 @@ ble_gatt_add_primary_svcs(struct ble_gatt_svc_def *gatt_db_svcs, int char_count) |
727 | 745 | gatt_db_svcs->type = BLE_GATT_SVC_TYPE_PRIMARY; |
728 | 746 |
|
729 | 747 | /* Allocate (number of characteristics + 1) memory for characteristics, the |
730 | | - * addtional characteristic consist of all 0s indicating end of |
| 748 | + * additional characteristic consist of all 0s indicating end of |
731 | 749 | * characteristics */ |
732 | 750 | gatt_db_svcs->characteristics = (struct ble_gatt_chr_def *) calloc((char_count + 1), |
733 | 751 | sizeof(struct ble_gatt_chr_def)); |
@@ -781,7 +799,7 @@ populate_gatt_db(struct ble_gatt_svc_def **gatt_db_svcs, const protocomm_ble_con |
781 | 799 | rc = ble_gatt_add_char_dsc((void *) (*gatt_db_svcs)->characteristics, |
782 | 800 | i, BLE_GATT_UUID_CHAR_DSC); |
783 | 801 | if (rc != 0) { |
784 | | - ESP_LOGE(TAG, "Error adding GATT Discriptor !!"); |
| 802 | + ESP_LOGE(TAG, "Error adding GATT Descriptor !!"); |
785 | 803 | return rc; |
786 | 804 | } |
787 | 805 | } |
@@ -813,6 +831,11 @@ static void protocomm_ble_cleanup(void) |
813 | 831 | protocomm_ble_mfg_data = NULL; |
814 | 832 | protocomm_ble_mfg_data_len = 0; |
815 | 833 | } |
| 834 | + |
| 835 | + if (protocomm_ble_addr) { |
| 836 | + free(protocomm_ble_addr); |
| 837 | + protocomm_ble_addr = NULL; |
| 838 | + } |
816 | 839 | } |
817 | 840 |
|
818 | 841 | static void free_gatt_ble_misc_memory(simple_ble_cfg_t *ble_config) |
@@ -970,6 +993,10 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con |
970 | 993 | ble_config->ble_bonding = config->ble_bonding; |
971 | 994 | ble_config->ble_sm_sc = config->ble_sm_sc; |
972 | 995 |
|
| 996 | + if (config->ble_addr != NULL) { |
| 997 | + protocomm_ble_addr = config->ble_addr; |
| 998 | + } |
| 999 | + |
973 | 1000 | if (populate_gatt_db(&ble_config->gatt_db, config) != 0) { |
974 | 1001 | ESP_LOGE(TAG, "Error populating GATT Database"); |
975 | 1002 | free_gatt_ble_misc_memory(ble_config); |
|
0 commit comments