Documentation ¶
Overview ¶
Package cache allows clients to resume authenticated sessions with a Tesla vehicle.
When a client communicates with a vehicle for the first time, the protocol requires an extra round-trip to perform a handshake. Using a SessionCache allows the client to avoid that round-trip on subsequent connections. If the SessionCache is outdated (e.g., because the vehicle's security controller rebooted during a firmware update), then the first command sent by the client will fail, and the vehicle will respond with updated session information. This does not introduce more latency than redoing the handshake. Therefore clients typically benefit by using a cache and do not incur a penalty if the cached information is outdated.
A SessionCache is tied to a specific client private key. If the SessionCache is used in a connection with a different private key, authentication will fail and the vehicle will send correct session data as normal.
The same SessionCache may safely be used with different VINs.
If a SessionCache is exported using its SessionCache.Export or SessionCache.ExportToFile methods, access controls should be used to prevent third parties from reading or tampering with the data.
Example ¶
package main import ( "context" "fmt" "github.com/k-katsuda/vehicle-command/pkg/cache" "github.com/k-katsuda/vehicle-command/pkg/connector/ble" "github.com/k-katsuda/vehicle-command/pkg/protocol" "github.com/k-katsuda/vehicle-command/pkg/vehicle" ) func main() { const cacheFilename = "my_cache.json" const privateKeyFilename = "private_key.pem" conn, err := ble.NewConnection(context.Background(), "myvin123") if err != nil { panic(err) } defer conn.Close() // Try to load cache from disk if it doesn't already exist var myCache *cache.SessionCache if myCache, err = cache.ImportFromFile(cacheFilename); err != nil { myCache = cache.New(5) // Create a cache that holds sessions for up to five vehicles } privateKey, err := protocol.LoadPrivateKey(privateKeyFilename) if err != nil { panic(err) } car, err := vehicle.NewVehicle(conn, privateKey, myCache) if err != nil { panic(err) } if err := car.Connect(context.Background()); err != nil { panic(err) } defer car.Disconnect() // StartSession(...) will load from myCache when possible. if err := car.StartSession(context.Background(), nil); err != nil { panic(err) } defer func() { if err := car.UpdateCachedSessions(myCache); err != nil { fmt.Printf("Error updating session cache: %s\n", err) return } myCache.ExportToFile(cacheFilename) }() // Interact with car }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SessionCache ¶
type SessionCache struct { MaxEntries int Vehicles map[string][]dispatcher.CacheEntry `json:"vehicles"` // contains filtered or unexported fields }
func Import ¶
func Import(r io.Reader) (*SessionCache, error)
Import a SessionCache using data in r. The data should previously have been generated using SessionCache.Export.
func ImportFromFile ¶
func ImportFromFile(filename string) (*SessionCache, error)
ImportFromFile reads a SessionCache from disk.
func New ¶
func New(maxEntries int) *SessionCache
New returns a SessionCache with that holds session state for up to maxEntries vehicles. The SessionCache uses a least-recently-used (LRU) eviction strategy, with the caveat that for this purpose a session is "used" when its used to authorize a command, not when it's loaded from or saved to the SessionCache.
Set maxEntries to zero for an unbounded cache.
func (*SessionCache) Export ¶
func (c *SessionCache) Export(w io.Writer) error
Export writes a serialized SessionCache to w.
func (*SessionCache) ExportToFile ¶
func (c *SessionCache) ExportToFile(filename string) error
ExportToFile writes a SessionCache to disk.
func (*SessionCache) GetEntry ¶
func (c *SessionCache) GetEntry(vin string) ([]dispatcher.CacheEntry, bool)
GetEntry returns the sessions associated with vin. This method intended for use by the internal dispatcher package; other clients should have no use for it.
func (*SessionCache) Update ¶
func (c *SessionCache) Update(vin string, sessions []dispatcher.CacheEntry) error
Update the SessionCache's entry for a vin with current state. It's recommended that clients use the vehicle.UpdateCachedSessions method instead in order to avoid accessing the internal dispatcher package.