Skip to content
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

send ATT request failed: io: read/write on closed pipe every 30 seconds #253

Closed
LubosD opened this issue Jun 2, 2024 · 12 comments · Fixed by #269
Closed

send ATT request failed: io: read/write on closed pipe every 30 seconds #253

LubosD opened this issue Jun 2, 2024 · 12 comments · Fixed by #269

Comments

@LubosD
Copy link

LubosD commented Jun 2, 2024

Are there any details on how to keep the BLE connection open for long time?

I tried Ping()ing the vehicle every 5 seconds, but it seems the connection is always lost after 30 seconds:

2024/06/02 14:39:16 Trying to connect to VIN XP7YGCELXNB015325
2024/06/02 14:39:17 VIN XP7YGCELXNB015325 connected over BLE!
2024/06/02 14:39:19 Session started
2024/06/02 14:39:25 PING OK
2024/06/02 14:39:30 PING OK
2024/06/02 14:39:35 PING OK
2024/06/02 14:39:40 PING OK
2024/06/02 14:39:45 PING OK
2024/06/02 14:39:49 VIN XP7YGCELXNB015325 disconnected
2024/06/02 14:39:49 OperateConnection returned: send ATT request failed: io: read/write on closed pipe
2024/06/02 14:39:49 Connect failed: send ATT request failed: io: read/write on closed pipe
2024/06/02 14:39:50 Trying to connect to VIN XP7YGCELXNB015325
2024/06/02 14:39:50 VIN XP7YGCELXNB015325 connected over BLE!
2024/06/02 14:39:52 Session started
2024/06/02 14:39:58 PING OK
2024/06/02 14:40:03 PING OK
2024/06/02 14:40:08 PING OK
2024/06/02 14:40:13 PING OK
2024/06/02 14:40:18 PING OK
2024/06/02 14:40:22 VIN XP7YGCELXNB015325 disconnected
2024/06/02 14:40:22 OperateConnection returned: ATT request failed: input channel closed: io: read/write on closed pipe
2024/06/02 14:40:22 Connect failed: ATT request failed: input channel closed: io: read/write on closed pipe

@sethterashima
Copy link
Collaborator

If you have other BLE devices (phone keys, keyfobs) connecting to the vehicle, it's possible that vehicle BLE endpoint is dropping one connection to accommodate another.

Are you using car.Ping(ctx) with a fixed 30-second ctx? A minimal working example of the code might help with debugging.

@LubosD
Copy link
Author

LubosD commented Jun 10, 2024

Hi, I've uploaded my code here: https://github.com/LubosD/hass-tesla-vc
I don't use a 30-second ctx, I use a background ctx and then I run Ping() every 5 seconds.

I was far away from the car with my phone key, so that can't be it.

@sethterashima
Copy link
Collaborator

sethterashima commented Jun 13, 2024

I was able to run that code without issue (with the mtqq stuff gutted out). It's possible the issue is within the BLE stack itself; unfortunately, the relevant Go packages are not very well maintained. Are you able to reproduce with a different BLE device? What OS/hardware are you using?

@LubosD
Copy link
Author

LubosD commented Jun 13, 2024

I'm using a "Raspberry Pi 4 Model B Rev 1.5". I ran hcidump and I see this when the disconnect happens:

< ACL data: handle 64 flags 0x00 dlen 27
    ATT: Write req (0x12)
      handle 0x0008 value  0x00 0xb7 0x32 0x02 0x08 0x03 0x3a 0x12 0x12 0x10 0xa2 0x0f 0xa9 0xb6 0x77 0xf8 0x95 0xb3 0xe6 0xee
> HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 1
> HCI Event: Disconn Complete (0x05) plen 4
    status 0x00 handle 64 reason 0x08
    Reason: Connection Timeout

@sethterashima
Copy link
Collaborator

I'll try to repo on a Pi 4 next week, but it's looking like a BLE library/driver/hardware issue. Unfortunately I don't have the bandwidth to run those down.

@iainwhyte
Copy link

@sethterashima - what BLE hardware are you testing on and runs without issue? I also have a Pi 4 does the same thing, and so do several BT4 and BT5 dongles. Usually you get:

Error: ble: failed to enumerate device services: ATT request failed: input channel closed: io: read/write on closed pipe

With the telsa-control tool, but only every 2nd, 3rd call.

@sethterashima
Copy link
Collaborator

I've been doing my testing on a Pi 3B and a Macbook.

I was able to reproduce using a BLE dongle.

@BogdanDIA
Copy link

BogdanDIA commented Jun 28, 2024

I get the same errors from time to time and that is why my script tries each command several times (6 times max). Usually after 1 or 2 retries it gets success.

I found out that using an rpi5 with 5Ghz wifi connection make the connection better compared with rpi3/4 on 2.4Ghz wifi where both BLE and wifi share the same band.

@iainwhyte
Copy link

It does appear to be range related. If I jury-rig RPi4 very close to the car, it's a lot more reliable - only fails in 1 in 10 calls. There is no way you can have a RPi with your command EC key stationed outside the house though, might as well leave the keycard sitting on top. Im so close to done with all of this. Restricted API, no way to buy more calls, flakey BLE support. Shouldn't be this hard... WTF Tesla.

@sethterashima
Copy link
Collaborator

Work in progress: #269

Improvement on an Ubuntu box with with a BLE dongle, but needs more work and validation in different environments. Please feel free to comment on the PR on whether you see an improvement or not and, if not, what hardware and vehicle model+year you're using.

@iainwhyte
Copy link

Work in progress: #269

Improvement on an Ubuntu box with with a BLE dongle, but needs more work and validation in different environments. Please feel free to comment on the PR on whether you see an improvement or not and, if not, what hardware and vehicle model+year you're using.

Thanks @sethterashima will comment on the PR, but for this thread: seems significantly improved.

@BogdanDIA
Copy link

Tested today the new code with several adapters I have. It works a bit faster for BT 5.0 adapter but it broke the BT4.x support. That is due to using the Extended Adv features that were introduced starting with BT 5.0.

vehicle-command/pkg/connector/ble/device_linux.go

var scanParams = cmd.LESetScanParameters{
LEScanType: 1, // Active scanning
LEScanInterval: 0x10, // 10ms
LEScanWindow: 0x10, // 10ms
OwnAddressType: 0, // Static
ScanningFilterPolicy: 2, // Basic filtered <- this should be 0 for Basic Filtered.
}

Here are the possible values:
0x00 Basic unfiltered scanning filter policy
0x01 Basic filtered scanning filter policy
0x02 Extended unfiltered scanning filter policy
0x03 Extended filtered scanning filter policy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants