Provisioning BLE: Add API to set manufacturer data in scan response

- Add `wifi_prov_scheme_ble_set_mfg_data` API to set custom manufacturer data
  in BLE advertisements.
- Run format.sh script on modified files.
- Fix few typos in `protocomm_nimble.c`.

- Incorporate suggestion to remove extra check on protocomm_ble_mfg_data_len

- Remove few unnecessary comments.
This commit is contained in:
Prasad Alatkar
2021-06-24 21:43:28 +05:30
parent 44c175649a
commit c8f4153d4f
5 changed files with 157 additions and 25 deletions

View File

@@ -29,6 +29,9 @@ extern const wifi_prov_scheme_t wifi_prov_scheme_ble;
static uint8_t *custom_service_uuid;
static uint8_t *custom_manufacturer_data;
static size_t custom_manufacturer_data_len;
static esp_err_t prov_start(protocomm_t *pc, void *config)
{
if (!pc) {
@@ -60,6 +63,23 @@ esp_err_t wifi_prov_scheme_ble_set_service_uuid(uint8_t *uuid128)
return ESP_OK;
}
esp_err_t wifi_prov_scheme_ble_set_mfg_data(uint8_t *mfg_data, ssize_t mfg_data_len)
{
if (!mfg_data || !mfg_data_len) {
return ESP_ERR_INVALID_ARG;
}
custom_manufacturer_data = (uint8_t *) malloc(mfg_data_len);
if (custom_manufacturer_data == NULL) {
ESP_LOGE(TAG, "Error allocating memory for mfg_data");
return ESP_ERR_NO_MEM;
}
custom_manufacturer_data_len = mfg_data_len;
memcpy(custom_manufacturer_data, mfg_data, mfg_data_len);
return ESP_OK;
}
static void *new_config(void)
{
protocomm_ble_config_t *ble_config = calloc(1, sizeof(protocomm_ble_config_t));
@@ -114,6 +134,26 @@ static esp_err_t set_config_service(void *config, const char *service_name, cons
if (custom_service_uuid) {
memcpy(ble_config->service_uuid, custom_service_uuid, sizeof(ble_config->service_uuid));
}
/* Set manufacturer data if it is provided by app */
if (custom_manufacturer_data) {
size_t mfg_data_len = custom_manufacturer_data_len;
/* Manufacturer Data Length + 2 Byte header + BLE Device name + 2 Byte
* header <= 31 Bytes */
if (mfg_data_len > (MAX_BLE_MANUFACTURER_DATA_LEN - sizeof(ble_config->device_name) - 2)) {
ESP_LOGE(TAG, "Manufacturer data length is more than the max allowed size; expect truncated mfg_data ");
/* XXX Does it even make any sense to set truncated mfg_data ? The
* only reason to not return failure from here is provisioning
* should continue as it is with error prints for mfg_data length */
mfg_data_len = MAX_BLE_MANUFACTURER_DATA_LEN - sizeof(ble_config->device_name) - 2;
}
ble_config->manufacturer_data = custom_manufacturer_data;
ble_config->manufacturer_data_len = mfg_data_len;
} else {
ble_config->manufacturer_data = NULL;
ble_config->manufacturer_data_len = 0;
}
return ESP_OK;
}