Next I started with the esp-matter/example/light code and added this file:
Code: Select all
#include <esp_err.h>
#include <esp_log.h>
#include <host/ble_gap.h>
static const char *TAG = "ble_advert";
static void
print_uuid(const ble_uuid_t *uuid)
{
char buf[BLE_UUID_STR_LEN];
printf("%s", ble_uuid_to_str(uuid, buf));
}
static void
print_bytes(const uint8_t *bytes, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf("%s0x%02x", i != 0 ? ":" : "", bytes[i]);
}
}
static char *
addr_str(const void *addr)
{
static char buf[6 * 2 + 5 + 1];
const uint8_t *u8p;
u8p = (uint8_t *)addr;
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
return buf;
}
static void
print_adv_fields(const struct ble_hs_adv_fields *fields)
{
char s[BLE_HS_ADV_MAX_SZ];
const uint8_t *u8p;
int i;
printf("BLE Advert\n");
if (fields->flags != 0)
{
printf(" flags=0x%02x\n", fields->flags);
}
if (fields->uuids16 != NULL)
{
printf(" uuids16(%scomplete)=",
fields->uuids16_is_complete ? "" : "in");
for (i = 0; i < fields->num_uuids16; i++)
{
print_uuid(&fields->uuids16[i].u);
printf(" ");
}
printf("\n");
}
if (fields->uuids32 != NULL)
{
printf(" uuids32(%scomplete)=",
fields->uuids32_is_complete ? "" : "in");
for (i = 0; i < fields->num_uuids32; i++)
{
print_uuid(&fields->uuids32[i].u);
printf(" ");
}
printf("\n");
}
if (fields->uuids128 != NULL)
{
printf(" uuids128(%scomplete)=",
fields->uuids128_is_complete ? "" : "in");
for (i = 0; i < fields->num_uuids128; i++)
{
print_uuid(&fields->uuids128[i].u);
printf(" ");
}
printf("\n");
}
if (fields->name != NULL)
{
assert(fields->name_len < sizeof s - 1);
memcpy(s, fields->name, fields->name_len);
s[fields->name_len] = '\0';
printf(" name(%scomplete)=%s\n",
fields->name_is_complete ? "" : "in", s);
}
if (fields->tx_pwr_lvl_is_present)
{
printf(" tx_pwr_lvl=%d\n", fields->tx_pwr_lvl);
}
if (fields->slave_itvl_range != NULL)
{
printf(" slave_itvl_range=");
print_bytes(fields->slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN);
printf("\n");
}
if (fields->svc_data_uuid16 != NULL)
{
printf(" svc_data_uuid16=");
print_bytes(fields->svc_data_uuid16, fields->svc_data_uuid16_len);
printf("\n");
}
if (fields->public_tgt_addr != NULL)
{
printf(" public_tgt_addr=");
u8p = fields->public_tgt_addr;
for (i = 0; i < fields->num_public_tgt_addrs; i++)
{
printf("public_tgt_addr=%s ", addr_str(u8p));
u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
}
printf("\n");
}
if (fields->appearance_is_present)
{
printf(" appearance=0x%04x\n", fields->appearance);
}
if (fields->adv_itvl_is_present)
{
printf(" adv_itvl=0x%04x\n", fields->adv_itvl);
}
if (fields->svc_data_uuid32 != NULL)
{
printf(" svc_data_uuid32=");
print_bytes(fields->svc_data_uuid32, fields->svc_data_uuid32_len);
printf("\n");
}
if (fields->svc_data_uuid128 != NULL)
{
printf(" svc_data_uuid128=");
print_bytes(fields->svc_data_uuid128, fields->svc_data_uuid128_len);
printf("\n");
}
if (fields->uri != NULL)
{
printf(" uri=");
print_bytes(fields->uri, fields->uri_len);
printf("\n");
}
if (fields->mfg_data != NULL)
{
printf(" mfg_data=");
print_bytes(fields->mfg_data, fields->mfg_data_len);
printf("\n");
}
}
void ReportDevice(const struct ble_hs_adv_fields *fields, const ble_addr_t *addr, uint8_t rssi)
{
if (fields->mfg_data != NULL)
{
if ((fields->mfg_data[0] == 0x4c) && (fields->mfg_data[1] == 0xd))
{
int i = 2;
uint16_t temp, humid, battery, count;
temp = humid = battery = count = 0;
while (i < fields->mfg_data_len)
{
switch (fields->mfg_data[i])
{
case 2:
temp = *(uint16_t *)&fields->mfg_data[i + 1];
break;
case 5:
humid = *(uint16_t *)&fields->mfg_data[i + 1];
break;
case 0xfe:
count = *(uint16_t *)&fields->mfg_data[i + 1];
break;
default:
case 0xff:
battery = *(uint16_t *)&fields->mfg_data[i + 1];
break;
}
i += 3;
}
ESP_LOGI(TAG, "BLE %s Count %u RSSI %u Temp %u Humid %u Battery %u", addr_str((void *)addr), count, rssi, temp, humid, battery);
}
}
}
void RemoveDevice()
{
// TODO
}
int OnBleCentralEvent(struct ble_gap_event *event, void *arg)
{
switch (event->type)
{
case BLE_GAP_EVENT_DISC_COMPLETE:
{
return 0;
}
case BLE_GAP_EVENT_DISC:
{
/* Try to connect to the advertiser if it looks interesting. */
struct ble_hs_adv_fields fields;
ble_hs_adv_parse_fields(&fields, event->disc.data, event->disc.length_data);
ReportDevice(&fields, &event->disc.addr, event->disc.rssi);
return 0;
}
}
return 0;
}
typedef enum {
SCAN_ALL_CHANNLE,
ONLY_SCAN_CHANNEL_37,
ONLY_SCAN_CHANNEL_38,
ONLY_SCAN_CHANNEL_39,
SCAN_MODE_TYPE_ERROR,
} scan_mode_config_t;
extern "C" uint8_t esp_ble_scan_channel_setting(scan_mode_config_t scan_mode);
esp_err_t StartScan()
{
uint8_t ownAddrType;
struct ble_gap_disc_params discParams;
int rc;
/* Figure out address to use while advertising. */
rc = ble_hs_id_infer_auto(0, &ownAddrType);
if (rc != 0)
{
// ChipLogError(DeviceLayer, "ble_hs_id_infer_auto failed: %d", rc);
return ESP_ERR_INVALID_STATE;
}
/* Set up discovery parameters. */
memset(&discParams, 0, sizeof(discParams));
/* Tell the controller to filter the duplicates. */
discParams.filter_duplicates = 0;
/* Perform passive scanning. */
discParams.passive = 1;
/* Use defaults for the rest of the parameters. */
discParams.itvl = 160;
discParams.window = 160;
discParams.filter_policy = 0;
discParams.limited = 0;
esp_ble_scan_channel_setting(ONLY_SCAN_CHANNEL_39);
/* Start the discovery process. */
rc = ble_gap_disc(ownAddrType, BLE_HS_FOREVER, &discParams, OnBleCentralEvent, NULL);
if (rc != 0)
{
ESP_LOGE(TAG, "ble_gap_disc failed: %d", rc);
return ESP_ERR_INVALID_STATE;
}
return ESP_OK;
}
esp_err_t StopScan()
{
int rc = ble_gap_disc_cancel();
if (rc != 0)
{
//(DeviceLayer, "ble_gap_disc_cancel failed: %d", rc);
return ESP_ERR_INVALID_STATE;
}
// mDelegate->OnScanComplete();
return ESP_OK;
}
Starting it from app_main.cpp with this:
Code: Select all
case chip::DeviceLayer::DeviceEventType::kServerReady:
ESP_LOGI(TAG, "Server ready");
StartScan();
break;
Used menuconfig to tell esp-matter to leave ble running.
I commissioned it so that wifi would start
Code: Select all
I (574396) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6440 RSSI 196 Temp 2129 Humid 37 Battery 3371
I (574506) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6440 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (574616) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6440 RSSI 199 Temp 2129 Humid 37 Battery 3371
I (575516) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6441 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (575626) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6441 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (576436) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6442 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (576546) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6442 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (576646) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6442 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (577456) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6443 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (577566) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6443 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (577666) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6443 RSSI 200 Temp 2129 Humid 37 Battery 3371
I (583686) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6449 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (595826) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6461 RSSI 195 Temp 2129 Humid 37 Battery 3371
I (595936) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6461 RSSI 198 Temp 2129 Humid 37 Battery 3371
I (597976) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6463 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (598886) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6464 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (598996) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6464 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (599096) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6464 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (599906) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6465 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (600016) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6465 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (600116) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6465 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (600926) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6466 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (601036) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6466 RSSI 199 Temp 2130 Humid 37 Battery 3371
I (601146) ble_advert: BLE 34:f1:ae:d4:10:00 Count 6466 RSSI 199 Temp 2130 Humid 37 Battery 3371
Now look at the difference, between 6461 and 6463 it dropped ten packets in a row, and between 6449 and 6461 it dropped over 30 in a row. It is dropping about 20% of all packets. Note that Matter was not actively sending packets during this period because Matter prints when it sends. MDNS doesn't print, so it could be active.
This is 100% espressif code except for the file above and it is on a S3 WROOM dev board.
Are Matter and MDNS coordinating on wifi coexistence?