Skip to content

Commit

Permalink
Show correct help message with adapter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenart12 committed Feb 12, 2025
1 parent 055ef3f commit 7ba27d5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
11 changes: 5 additions & 6 deletions cmd/tesla-control/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/teslamotors/vehicle-command/internal/log"
"github.com/teslamotors/vehicle-command/pkg/account"
"github.com/teslamotors/vehicle-command/pkg/cli"
"github.com/teslamotors/vehicle-command/pkg/connector/ble"
"github.com/teslamotors/vehicle-command/pkg/protocol"
"github.com/teslamotors/vehicle-command/pkg/vehicle"
)
Expand Down Expand Up @@ -163,12 +164,10 @@ func main() {

acct, car, err := config.Connect(ctx)
if err != nil {
writeErr("Error: %s", err)
// Error isn't wrapped so we have to check for a substring explicitly.
if strings.Contains(err.Error(), "operation not permitted") {
// The underlying BLE package calls HCIDEVDOWN on the BLE device, presumably as a
// heavy-handed way of dealing with devices that are in a bad state.
writeErr("\nTry again after granting this application CAP_NET_ADMIN:\n\n\tsudo setcap 'cap_net_admin=eip' \"$(which %s)\"\n", os.Args[0])
if ble.IsAdapterError(err) {
writeErr("%s", ble.AdapterErrorHelpMessage(err))
} else {
writeErr("Error: %s", err)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/connector/ble/ble.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func NewConnectionToBleTarget(ctx context.Context, vin string, target *ScanResul
if err == nil {
return conn, nil
}
if !retry || strings.Contains(err.Error(), "operation not permitted") {
if !retry || IsAdapterError(err) {
return nil, err
}
log.Warning("BLE connection attempt failed: %s", err)
Expand Down
10 changes: 10 additions & 0 deletions pkg/connector/ble/device_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ import (
"tinygo.org/x/bluetooth"
)

func IsAdapterError(err error) bool {
// TODO: Add check for Darwin
return false
}

func AdapterErrorHelpMessage(err error) string {
return err.Error()
}

func newAdapter(id string) *bluetooth.Adapter {
if id != "" {
// TODO: Add support for Darwin
log.Warning("BLE adapter ID is not supported on Darwin")
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/connector/ble/device_linux.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
package ble

import (
"strings"

"tinygo.org/x/bluetooth"
)

func IsAdapterError(err error) bool {
// D-Bus not found
if strings.Contains(err.Error(), "dbus") && strings.HasSuffix(err.Error(), "no such file or directory") {
return true
}
// D-Bus is running but org.bluez is not found
if strings.Contains(err.Error(), "The name org.bluez was not provided by any .service files") {
return true
}
return false
}

func AdapterErrorHelpMessage(err error) string {
return "Failed to initialize BLE adapter: \n\t" + err.Error() + "\n" +
"Make sure bluez and dbus are installed and running.\n" +
"If running in a container, make sure the container has access to the host's D-Bus socket. (e.g. -v /var/run/dbus:/var/run/dbus)"
}

func newAdapter(id string) *bluetooth.Adapter {
if id != "" {
return bluetooth.NewAdapter(id)
Expand Down
10 changes: 10 additions & 0 deletions pkg/connector/ble/device_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ import (
"tinygo.org/x/bluetooth"
)

func IsAdapterError(err error) bool {
// TODO: Add check for Windows
return false
}

func AdapterErrorHelpMessage(err error) string {
return err.Error()
}

func newAdapter(id string) *bluetooth.Adapter {
if id != "" {
// TODO: Add support for Windows
log.Warning("BLE adapter ID is not supported on Windows")
}

Expand Down

0 comments on commit 7ba27d5

Please sign in to comment.