-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (87 loc) · 2.64 KB
/
Copy pathmain.go
File metadata and controls
103 lines (87 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Example program: Use a BLE connection to unlock a vehicle and turn on the AC.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"time"
debugger "github.com/teslamotors/vehicle-command/internal/log"
"github.com/teslamotors/vehicle-command/pkg/action"
"github.com/teslamotors/vehicle-command/pkg/connector/ble"
"github.com/teslamotors/vehicle-command/pkg/protocol"
"github.com/teslamotors/vehicle-command/pkg/vehicle"
)
func main() {
logger := log.New(os.Stderr, "", 0)
status := 1
debug := false
defer func() {
os.Exit(status)
}()
// Provided through command line options
var (
privateKeyFile string
vin string
)
flag.StringVar(&privateKeyFile, "key", "", "Private key `file` for authorizing commands (PEM PKCS8 NIST-P256)")
flag.StringVar(&vin, "vin", "", "Vehicle Identification Number (`VIN`) of the car")
flag.BoolVar(&debug, "debug", false, "Enable debugging of TX/RX BLE packets")
flag.Parse()
if debug {
debugger.SetLevel(debugger.LevelDebug)
}
// For simplcity, allow 30 seconds to wake up the vehicle, connect to it,
// and unlock. In practice you'd want a fresh timeout for each command.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if vin == "" {
logger.Printf("Must specify VIN")
return
}
var err error
var privateKey protocol.ECDHPrivateKey
if privateKeyFile != "" {
if privateKey, err = protocol.LoadPrivateKey(privateKeyFile); err != nil {
logger.Printf("Failed to load private key: %s", err)
return
}
}
conn, err := ble.NewConnection(ctx, vin)
if err != nil {
logger.Printf("Failed to connect to vehicle: %s", err)
return
}
defer conn.Close()
car, err := vehicle.NewVehicle(conn, privateKey, nil)
if err != nil {
logger.Printf("Failed to connect to vehicle: %s", err)
return
}
if err := car.Connect(ctx); err != nil {
logger.Printf("Failed to connect to vehicle: %s\n", err)
return
}
defer car.Disconnect()
// Most interactions with the car require an authenticated client.
// StartSession() performs a handshake with the vehicle that allows
// subsequent commands to be authenticated.
if err := car.StartSession(ctx, nil); err != nil {
logger.Printf("Failed to perform handshake with vehicle: %s\n", err)
return
}
fmt.Println("Unlocking car...")
if err := car.ExecuteAction(ctx, action.Unlock()); err != nil {
logger.Printf("Failed to unlock vehicle: %s\n", err)
return
}
fmt.Println("Vehicle unlocked!")
fmt.Println("Turning on HVAC...")
if err := car.ExecuteAction(ctx, action.ClimateOn()); err != nil {
logger.Printf("Failed to turn on HVAC: %s\n", err)
return
}
fmt.Println("HVAC on!")
status = 0
}