Repository navigation
Fix connecting to printers on Bluetooth LE - #386
Merged
Merged
Conversation
A printer set up by its address was connected through any adapter, as no search had heard it. With a second adapter, e.g. a dongle on USB/IP out of the printer's reach, the kernel may take that one, and every connection timed out ("Host is down"). Where no search heard the printer and the kernel lists more than one adapter, it is now searched for `FIND_WAIT` seconds first and connected through the adapter that heard it, which is kept as after any search.
Signed-off-by: DL6ER <dl6er@dl6er.de>
The first status query of a B1 after the server started could fail with "[Errno 38] Function not implemented" while the search for printers ran, and the label designer then showed for a few seconds that the printer does not answer. Linux tells ENOSYS where the adapter could not set up an LE connection for a reason it has no error number for, e.g. as a search on the adapter ended. Such a connection is now set up once more. Signed-off-by: DL6ER <dl6er@dl6er.de>
Signed-off-by: DL6ER <dl6er@dl6er.de>
…nded
A print on a Niimbot B1 right after the server started ran into the search for printers, which also searched classic Bluetooth for 11 s. The LE link of its status query came up, but the connection failed only as the search ended ("Host is down"), and the job connected right after got no answer to its end of page, so nothing came out. While classic Bluetooth is searched, see `devices.classic_search`, a connection on Bluetooth LE now waits for the search to end, for at most `CLASSIC_SEARCH_WAIT` seconds.
Signed-off-by: DL6ER <dl6er@dl6er.de>
A printer no search heard, e.g. a B1 that is off, is asked again every 10 s while the label designer follows it, and with more than one adapter each try searched every adapter for `FIND_WAIT` seconds. `adapter_reaching()` now searches for an address once, and again only after the label designer looked for printers since, see `devices.looked`. Signed-off-by: DL6ER <dl6er@dl6er.de>
Signed-off-by: DL6ER <dl6er@dl6er.de>
There was a problem hiding this comment.
🟡 Changes recommended
A status query can hold the printer lock while waiting for discovery, causing concurrent prints to time out and fail as busy.
3 open issues
What changed in this PR
Improves Bluetooth LE printer connections by selecting a reachable adapter, retrying setup failures, and waiting for classic Bluetooth discovery.
Changes:
- Searches for unheard printers when multiple adapters are available.
- Retries
ENOSYSconnection failures once and waits for classic discovery. - Adds regression tests and isolates tests from host Bluetooth adapters.
| File | Description |
|---|---|
| tests/printing/test_niimbot_ble.py | Tests adapter selection, search caching, retries, and discovery waits. |
| tests/conftest.py | Isolates adapter discovery and resets search state. |
| app/labeldesigner/drivers/transports.py | Adds LE connection retries and discovery waits. |
| app/labeldesigner/bluetooth.py | Finds reachable adapters and caches search attempts. |
Review effort: Balanced
…e printer A status query of a B1 or PUQU on Bluetooth LE waited for a search of classic Bluetooth while it held the printer's lock, so a print that came meanwhile could give up after `DEVICE_LOCK_TIMEOUT` and fail as busy. The status queries now wait in `transports.after_classic_search()` before they lock the printer; a job still waits there with the lock, as it holds it anyway. The wait also lasted only `CLASSIC_SEARCH` plus 2 s, while a search that resolves names goes on for `NAME_WAIT` more. It now lasts `CLASSIC_SEARCH + NAME_WAIT + SEARCH_ENDS` at most. Signed-off-by: DL6ER <dl6er@dl6er.de>
Two status queries of printers on Bluetooth LE that no search heard could each start a search, and the kernel refused the second as busy: that one returned before the first knew the adapter, so its printer was reached through any adapter and not searched for again until the next look. `discover()` now runs one search at a time, also the label designer's, and `adapter_reaching()` searches only where the search it waited for did not hear the printer. Signed-off-by: DL6ER <dl6er@dl6er.de>
Review points on the waits for a search: 1. A status query of a printer on Bluetooth LE searches for its adapter and waits for a search of classic Bluetooth in `transports.ready_le()`, which `bluetooth_status()` runs before the query locks the printer where the driver tells `le`, so that a print meanwhile does not wait for the lock. The drivers no longer call the wait themselves. 2. Within the lock, the wait for a search of classic Bluetooth comes right before the connection is set up, after the search for the adapter, as a search of classic Bluetooth may begin meanwhile. 3. The wait lasts the longest search from the time that search began, `bluetooth.classic_began`, as it may first wait for another search. 4. A search waits until the adapters stopped searching, as Linux fails a connection on Bluetooth LE set up before. 5. A search for an adapter counts only where an adapter searched: one refused as busy is tried again at the next connection. 6. The adapters counted are those on with Bluetooth LE, read on the management channel as for a search, not the entries in /sys/class/bluetooth; where they cannot be read, the log says so. Signed-off-by: DL6ER <dl6er@dl6er.de>
…s lock Our own review of the adapter search: 1. A search with no adapter on with Bluetooth LE ends at once, not after its timeout. 2. A connection in the printer's lock never waits for a search that runs; a status query waits for one at most `SEARCH_WAIT` seconds before it locks the printer, and searches for no adapter while classic Bluetooth is searched, as that search searches Bluetooth LE too. 3. The adapters on are counted again after `COUNT_KEPT` seconds or a look, and `discover()` counts them with its own reading, so that a followed status query does not ask the kernel, nor log that it cannot, every 2 s. 4. A status query of a printer whose connection is kept gets ready for none, as none is set up. 5. A connection that failed through the adapter that heard the printer forgets it, so that the printer is searched for again, e.g. after a dongle was removed. 6. A management reply too short for what it tells is left out, and a connection that failed for another reason closes its socket. 7. `discover()` tells when its search of classic Bluetooth ends at the latest, from its own timeout and waits, and the wait for it follows that. 8. A search the adapters refused is tried again `REFUSED_AGAIN` seconds later, not at each status query. 9. The searches are kept apart by a plain lock, as none takes it twice. Signed-off-by: DL6ER <dl6er@dl6er.de>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Three fixes for printers on Bluetooth LE, found with a Niimbot B1:
The wait was tried on a B1. With a second adapter attached, its connection still drops at times.
No pictures: the label designer looks as before.