-
Notifications
You must be signed in to change notification settings - Fork 79
/
vehicle.go
261 lines (226 loc) · 8.38 KB
/
vehicle.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package vehicle
import (
"context"
"crypto/ecdh"
"errors"
"time"
"google.golang.org/protobuf/proto"
"github.com/teslamotors/vehicle-command/internal/authentication"
"github.com/teslamotors/vehicle-command/internal/dispatcher"
"github.com/teslamotors/vehicle-command/pkg/cache"
"github.com/teslamotors/vehicle-command/pkg/connector"
"github.com/teslamotors/vehicle-command/pkg/protocol"
"github.com/teslamotors/vehicle-command/pkg/protocol/protobuf/signatures"
universal "github.com/teslamotors/vehicle-command/pkg/protocol/protobuf/universalmessage"
)
var (
// ErrNoFleetAPIConnection indicates the client attempted to send a command that terminates on
// Tesla's backend (rather than a vehicle), but the Vehicle Connection does not use connector/inet.
ErrNoFleetAPIConnection = errors.New("not connected to Fleet API")
// ErrVehicleStateUnknown indicates the client attempt to determine if a vehicle supported a
// feature before calling vehicle.GetState.
ErrVehicleStateUnknown = errors.New("could not determine vehicle state")
)
// sender provides an interface that handles the RoutableMessage protocol layer.
type sender interface {
// Start causes the sender to listen for messages from the vehicle in a
// separate go routine. Returns an error if ctx expires before the sender
// is ready to receive messages.
Start(ctx context.Context) error
// Stop the goroutine launched by Start.
Stop()
// Send transmits message to the vehicle using the provided authMethod.
// If err is not nil, the caller must invoke recv.Close() after handling any responses.
// The client must call StartSessions before calling with auth set to authMethodMAC.
Send(ctx context.Context, message *universal.RoutableMessage, auth connector.AuthMethod) (recv protocol.Receiver, err error)
// StartSessions performs handshakes with the vehicle security controller
// and infotainment to allow subsequent commands to be authenticated.
StartSessions(ctx context.Context, domains []universal.Domain) error
Cache() []dispatcher.CacheEntry
LoadCache(entries []dispatcher.CacheEntry) error
// Returns the recommended retransmission interval for the Connector
RetryInterval() time.Duration
}
// A Vehicle represents a Tesla vehicle.
type Vehicle struct {
dispatcher sender
Flags uint32
vin string
conn connector.Connector
authMethod connector.AuthMethod
keyAvailable bool
}
// NewVehicle creates a new Vehicle. The privateKey and sessionCache may be nil.
func NewVehicle(conn connector.Connector, privateKey authentication.ECDHPrivateKey, sessionCache *cache.SessionCache) (*Vehicle, error) {
dispatch, err := dispatcher.New(conn, privateKey)
if err != nil {
return nil, err
}
vin := conn.VIN()
vehicle := &Vehicle{
dispatcher: dispatch,
vin: vin,
conn: conn,
authMethod: conn.PreferredAuthMethod(),
keyAvailable: privateKey != nil,
}
if sessionCache != nil {
if sessions, ok := sessionCache.GetEntry(vin); ok {
if err := dispatch.LoadCache(sessions); err != nil {
return nil, err
}
}
}
return vehicle, nil
}
func (v *Vehicle) VIN() string {
return v.vin
}
func (v *Vehicle) PrivateKeyAvailable() bool {
return v.keyAvailable
}
// Connect opens a connection to the vehicle.
func (v *Vehicle) Connect(ctx context.Context) error {
return v.dispatcher.Start(ctx)
}
func (v *Vehicle) SessionInfo(ctx context.Context, publicKey *ecdh.PublicKey, domain universal.Domain) (*signatures.SessionInfo, error) {
request := dispatcher.SessionInfoRequest(domain, publicKey.Bytes())
recv, err := v.dispatcher.Send(ctx, request, connector.AuthMethodNone)
if err != nil {
return nil, err
}
select {
case reply := <-recv.Recv():
if err := protocol.GetError(reply); err != nil {
return nil, err
}
if infoBytes := reply.GetSessionInfo(); infoBytes != nil {
var info signatures.SessionInfo
if err := proto.Unmarshal(infoBytes, &info); err != nil {
return nil, err
}
return &info, nil
}
return nil, protocol.ErrBadResponse
case <-ctx.Done():
return nil, ctx.Err()
}
}
// StartSession performs a handshake with the vehicle that allows the client to begin sending
// authenticated commands. This will fail if the client's public key has not been paired with the
// vehicle. If domains is nil, then the client will establish connections with all supported vehicle
// subsystems. The client may specify a subset of domains if it does not need to connect to all of
// them; for example, a client that only interacts with VCSEC can avoid waking infotainment.
func (v *Vehicle) StartSession(ctx context.Context, domains []universal.Domain) error {
for {
err := v.dispatcher.StartSessions(ctx, domains)
if err == nil {
return nil
}
if !protocol.ShouldRetry(err) {
return err
}
select {
case <-time.After(v.dispatcher.RetryInterval()):
continue
case <-ctx.Done():
return ctx.Err()
}
}
}
// Disconnect closes the connection to v.
// Calling this method invokes the underlying [connector.Connector.Close] method. The
// [connector.Connector] interface definition requires that multiple calls to Close() are safe, and so
// it is safe to defer both this method and the Connector's Close() method; however, Disconnect must
// be invoked first.
func (v *Vehicle) Disconnect() {
v.dispatcher.Stop()
if v.conn != nil {
v.conn.Close()
}
}
func (v *Vehicle) getReceiver(ctx context.Context, domain universal.Domain, payload []byte, auth connector.AuthMethod) (protocol.Receiver, error) {
message := universal.RoutableMessage{
ToDestination: &universal.Destination{
SubDestination: &universal.Destination_Domain{
Domain: domain,
},
},
Payload: &universal.RoutableMessage_ProtobufMessageAsBytes{
ProtobufMessageAsBytes: payload,
},
Flags: v.Flags,
}
pendingResponse, err := v.dispatcher.Send(ctx, &message, auth)
if err != nil {
return nil, err
}
return pendingResponse, nil
}
func (v *Vehicle) trySend(ctx context.Context, domain universal.Domain, payload []byte, auth connector.AuthMethod) ([]byte, error) {
recv, err := v.getReceiver(ctx, domain, payload, auth)
if err != nil {
return nil, err
}
defer recv.Close()
select {
case response := <-recv.Recv():
return response.GetProtobufMessageAsBytes(), protocol.GetError(response)
case <-ctx.Done():
return nil, &protocol.CommandError{Err: ctx.Err(), PossibleSuccess: true, PossibleTemporary: true}
}
}
// SendMessage sends a routable message to the vehicle.
//
// This interface is intended to be used when proxying commands that were authorized by a different
// entity, notably when using cardless key pairing over BLE. In most cases, you'll want to use Send
// instead, which automatically resynchronises session state and tries again when encountering
// certain types of errors.
//
// The SendMessage method only retries on errors for which retransmission of the same message
// (without modifying anti-replay counters, etc.) is safe and might resolve a transient error.
func (v *Vehicle) SendMessage(ctx context.Context, message *universal.RoutableMessage) (protocol.Receiver, error) {
return v.dispatcher.Send(ctx, message, connector.AuthMethodNone)
}
// Send a payload to a Vehicle. This is a low-level method that most clients will not need.
//
// The method retries until vehicle responds with a terminal result (success or non-transient
// failure) or the provided context expires.
//
// The domain controls what vehicle subsystem receives the message, and auth controls how the
// message is authenticated (if it all).
func (v *Vehicle) Send(ctx context.Context, domain universal.Domain, payload []byte, auth connector.AuthMethod) ([]byte, error) {
payloadCopy := make([]byte, len(payload))
copy(payloadCopy, payload)
for {
response, err := v.trySend(ctx, domain, payloadCopy, auth)
if err == nil {
return response, nil
}
if !protocol.ShouldRetry(err) {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(v.dispatcher.RetryInterval()):
continue
}
}
}
func (v *Vehicle) Wakeup(ctx context.Context) error {
if oapi, ok := v.conn.(connector.FleetAPIConnector); ok {
return oapi.Wakeup(ctx)
} else {
return v.wakeupRKE(ctx)
}
}
func (v *Vehicle) UpdateCachedSessions(c *cache.SessionCache) error {
return c.Update(v.vin, v.dispatcher.Cache())
}
func (v *Vehicle) LoadCachedSessions(c *cache.SessionCache) error {
if data, ok := c.GetEntry(v.vin); ok {
return v.dispatcher.LoadCache(data)
}
return errors.New("VIN not in cache")
}