Skip to content

Commit 9bfd7d7

Browse files
committed
Upstream session fixes and lower BLE logging
1 parent b02950d commit 9bfd7d7

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

tesla_fleet_api/tesla/vehicle/bluetooth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def receive_data(self, data: bytearray):
110110
if self.expected_length is None and len(self.buffer) >= 2:
111111
self.expected_length = struct.unpack(">H", self.buffer[:2])[0] + 2
112112

113-
LOGGER.info(f"Buffer length: {len(self.buffer)}, Packet starts: {self.packet_starts}, Expected length: {self.expected_length}")
113+
LOGGER.debug(f"Buffer length: {len(self.buffer)}, Packet starts: {self.packet_starts}, Expected length: {self.expected_length}")
114114

115115
if self.expected_length is not None and self.expected_length > 1024:
116116
LOGGER.warning(f"Expected length too large: {self.expected_length}")
@@ -270,7 +270,7 @@ async def _send(self, msg: RoutableMessage, requires: str, timeout: int = 5) ->
270270
if resp.HasField(requires):
271271
return resp
272272
else:
273-
LOGGER.warning(f"Ignoring message since it does not contain the required field {requires}, {resp.HasField(requires)}")
273+
LOGGER.debug(f"Ignoring message since it does not contain the required field {requires}, {resp.HasField(requires)}")
274274

275275
async def query_display_name(self, max_attempts=5) -> str | None:
276276
"""Read the device name via GATT characteristic if available"""

tesla_fleet_api/tesla/vehicle/commands.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,20 @@
158158
class Session:
159159
"""A connect to a domain"""
160160

161-
domain: Domain
162-
parent: Commands
163-
key: bytes
164-
counter: int
165-
epoch: bytes
166-
delta: int
167-
sharedKey: bytes
168-
hmac: bytes
169-
publicKey: bytes
170-
lock: Lock
171-
ready: bool
172-
173161
def __init__(self, parent: Commands, domain: Domain):
174-
self.parent = parent
175-
self.domain = domain
162+
self.parent: Commands = parent
163+
self.domain: Domain = domain
164+
self.counter: int = 0
165+
self.epoch: bytes | None = None
166+
self.delta: int = 0
167+
self.sharedKey: bytes | None = None
168+
self.hmac: bytes | None = None
169+
self.publicKey: bytes | None = None
176170
self.lock = Lock()
177-
self.counter = 0
178-
self.ready = False
171+
172+
@property
173+
def ready(self) -> bool:
174+
return self.epoch is not None and self.hmac is not None and self.delta > 0
179175

180176
def update(self, sessionInfo: SessionInfo):
181177
"""Update the session with new information"""
@@ -187,14 +183,14 @@ def update(self, sessionInfo: SessionInfo):
187183
self.publicKey = sessionInfo.publicKey
188184
self.sharedKey = self.parent.shared_key(sessionInfo.publicKey)
189185
self.hmac = hmac.new(self.sharedKey, "authenticated command".encode(), hashlib.sha256).digest()
190-
self.ready = True
191186

192187
def hmac_personalized(self) -> HMAC_Personalized_Signature_Data:
193188
"""Sign a command and return session metadata"""
194189
self.counter += 1
195190
return HMAC_Personalized_Signature_Data(
196191
epoch=self.epoch,
197192
counter=self.counter,
193+
# Expire command in 10 seconds
198194
expires_at=int(time.time()) - self.delta + 10,
199195
)
200196

@@ -205,7 +201,8 @@ def aes_gcm_personalized(self) -> AES_GCM_Personalized_Signature_Data:
205201
epoch=self.epoch,
206202
nonce=randbytes(12),
207203
counter=self.counter,
208-
expires_at=int(time.time()) - self.delta + 10,
204+
# Expire command in 30 seconds (BLE can be slow)
205+
expires_at=int(time.time()) - self.delta + 30,
209206
)
210207

211208

0 commit comments

Comments
 (0)