8000 Rework max connections for BLE to avoid exceeding the hard limit by bdraco · Pull Request #8303 · esphome/esphome · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Rework max connections for BLE to avoid exceeding the hard limit #8303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 7, 2025

Conversation

bdraco
Copy link
Member
@bdraco bdraco commented Feb 23, 2025

What does this implement/fix?

Check that the configured total number of connections slots needed for BLE devices does not exceed the device hard limit.

Fix can be tested with 2025.2.2+

external_components:
  - source: github://pr#8303
    components: [ ble_client, bluetooth_proxy, esp32_ble_tracker ]
    refresh: 0s

This is a breaking change because previously configuration would validate even though it would run out of slots at runtime. On IDF we will do our best to mitigate that so the user does not likely have to do anything (see below) by automatically increasing the value to match. This is not possible on arduino since its hard coded to 3.

  • Allow setting max_connections for esp32_ble_tracker (total)
    • IDF: max: 9 min: 0
    • ARDUINO: max: 3 min: 3 (arduino does not allow this to be changed)
  • Allow setting connection_slots for bluetooth_proxy

IDF users that exceed the configured limit will get warning that the limit was automatically increased unless they exceed the 9 slot hard limit
WARNING esp32_ble_tracker exceeded max_connections: components attempted to consume 5 connection slot(s) out of available configured maximum 3 connection slot(s); The system automatically increased max_connections to 5 to match the number of used connection slot(s) by components: ble_client, bluetooth_proxy, bluetooth_proxy, bluetooth_proxy, bluetooth_proxy.

ARDUINO users will get an error if they exceed the 3 slot hard limit since we cannot adjust the upward limit
esp32_ble_tracker exceeded max_connections: components attempted to consume 4 connection slot(s) out of available configured maximum 3 connection slot(s); Decrease the number of BLE clients (ble_client, bluetooth_proxy, bluetooth_proxy, bluetooth_proxy) to stay under the 3 connection slot(s) limit.

If the hard limit is exceeded, we raise a validation error if the number of clients (number of ble_client + configured bluetooth_proxy connection_slots) exceeds the hard limit of the currently used framework.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

Pull request in esphome-docs with documentation (if applicable):

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL 8000 87xx

Example entry for config.yaml:

# Example config.yaml
esphome:
  name: proxy_and_ble_client

ble_client:
  - mac_address: FF:EE:DD:CC:BB:AA

esp32:
  board: esp32doit-devkit-v1
  framework:
    type: esp-idf

ethernet:
  type: IP101
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  clk_mode: GPIO0_IN
  phy_addr: 1
  power_pin: GPIO5

api:
logger:
ota:
 platform: esphome

esp32_ble_tracker:
  max_connections: 1
  scan_parameters:
    active: false

bluetooth_proxy:
  active: true
  connection_slots: 1

Exceeded max_connections: Components attempted to consume 4 slot(s) out of available configured maximum 1 connection slot(s); Decrease the number of BLE clients (ble_client,bluetooth_proxy,bluetooth_proxy,bluetooth_proxy) or increase max_connections to 4.

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

- Raise a validation error if the number of clients exceeds the max connections
  instead of failing at run time on the device
- Allow setting max_connections for esp32_ble_tracker (total)
  IDF max: 9
  arduino max is hard coded to 3 and not adjustable
- Allow setting max_connections for bluetooth_proxy

fixes esphome/issues#6553
@tomaszduda23
Copy link
Contributor

The error message is a little bit strange

  Exceeded max_connections: 4/3: Decrease the number of BLE clients or increase the maximum connections.

@tomaszduda23
Copy link
Contributor

and it does not work even if I reduce number of connectinos

  Exceeded max_connections: 4/2: Decrease the number of BLE clients or increase the maximum connections.
  scan_parameters: 
    interval: 1100ms
    window: 1100ms
    active: True
    duration: 5min
    continuous: True
  max_connections: 2

@bdraco
Copy link
Member Author
bdraco commented Feb 23, 2025

and it does not work even if I reduce number of connectinos

You need to increase the max_connections to the total value used between bluetooth_proxy and ble_client

I improved the error message


esp32_ble_tracker: [source officenewgli89proxy.yaml:66]
  
  Exceeded `max_connections`: Components attempted to consume 4 slot(s) out of available configured maximum 1 connection slot(s); Decrease the number of BLE clients or increase `max_connections` to 4.
  max_connections: 1
  scan_parameters: 
    active: False
    duration: 5min
    interval: 320ms
    window: 30ms
    continuous: True

@bdraco
Copy link
Member Author
bdraco commented Feb 23, 2025

In your case I think you will need to set max_connections: 2 for bluetooth_proxy

@bdraco
Copy link
Member Author
bdraco commented Feb 23, 2025

one more attempt to improve the error. Example with setting max_connections to 1 and than trying to use 4 connections (default of 3 for the proxy and 1 for the ble_client)

Exceeded max_connections: Components attempted to consume 4 slot(s) out of available configured maximum 1 connection slot(s); Decrease the number of BLE clients (ble_client,bluetooth_proxy,bluetooth_proxy,bluetooth_proxy) or increase max_connections to 4.

@bdraco bdraco changed the title Rework handling maximum connections for BLE Rework max connections for BLE to avoid exceeding the hard limit Mar 8, 2025
10000
@bdraco bdraco marked this pull request as ready for review March 8, 2025 22:48
@probot-esphome
Copy link
probot-esphome bot commented Mar 8, 2025

Hey there @buxtronix, @clydebarrow, mind taking a look at this pull request as it has been labeled with an integration (ble_client) you are listed as a code owner for? Thanks!
Hey there @jesserockz, mind taking a look at this pull request as it has been labeled with an integration (bluetooth_proxy) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@bdraco bdraco added this to the 2025.4.0b1 milestone Apr 6, 2025
@bdraco
Copy link
Member Author
bdraco commented Apr 6, 2025

Tagged this as next month we plan on being able to switch BLE scanners from passive to active at run time (and report current state) and I'd prefer not to have this change in the same release.

More details https://ptb.discord.com/channels/330944238910963714/1346947687718850721/1354832140247892100

@bdraco
Copy link
Member Author
bdraco commented Apr 6, 2025

Thanks

@bdraco bdraco enabled auto-merge (squash) April 6, 2025 22:32
@bdraco bdraco merged commit 23e5cdb into dev Apr 7, 2025
71 checks passed
@bdraco bdraco deleted the ble_max_connections branch April 7, 2025 00:48
@jesserockz jesserockz mentioned this pull request Apr 9, 2025
shibida pushed a commit to shibida/esphome that referenced this pull request Apr 15, 2025
herruzo99 pushed a commit to herruzo99/esphome that referenced this pull request Apr 16, 2025
@jesserockz jesserockz mentioned this pull request Apr 16, 2025
sa-crespo pushed a commit to sa-crespo/esphome that referenced this pull request May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

esp32_ble_tracker.register_client allows registering more clients than the device supports (try to allocate too many ble clients)
5 participants
0