Skip to content

Commit 30ddfa4

Browse files
committed
Add type hint for can message
Use make_can_msg
1 parent 50a0e4e commit 30ddfa4

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

selfdrive/car/ecu_addrs.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#!/usr/bin/env python3
2+
import capnp
23
import time
34
import traceback
4-
from typing import Any, Set
5+
from typing import Set
56

67
import cereal.messaging as messaging
78
from panda.python.uds import SERVICE_TYPE
9+
from selfdrive.car import make_can_msg
810
from selfdrive.boardd.boardd import can_list_to_can_capnp
911
from selfdrive.swaglog import cloudlog
1012

11-
# TODO: figure out type annotation for CanData
12-
def is_tester_present_response(msg: Any) -> bool:
13+
14+
def is_tester_present_response(msg: capnp.lib.capnp._DynamicStructReader) -> bool:
1315
# ISO-TP messages are always padded to 8 bytes
1416
# tester present response is always a single frame
15-
if len(msg.dat) == 8 and msg.dat[0] >= 1 and msg.dat[0] <= 7:
17+
if len(msg.dat) == 8 and 1 <= msg.dat[0] <= 7:
1618
# success response
1719
if msg.dat[1] == (SERVICE_TYPE.TESTER_PRESENT + 0x40):
1820
return True
@@ -21,12 +23,13 @@ def is_tester_present_response(msg: Any) -> bool:
2123
return True
2224
return False
2325

24-
def get_ecu_addrs(logcan: messaging.SubSocket, sendcan: messaging.PubSocket, bus: int, timeout: float=1, debug: bool=True) -> Set[int]:
26+
27+
def get_ecu_addrs(logcan: messaging.SubSocket, sendcan: messaging.PubSocket, bus: int, timeout: float = 1, debug: bool = True) -> Set[int]:
2528
ecu_addrs = set()
2629
try:
2730
addr_list = [0x700 + i for i in range(256)] + [0x18da00f1 + (i << 8) for i in range(256)]
2831
tester_present = bytes([0x02, SERVICE_TYPE.TESTER_PRESENT, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0])
29-
msgs = [[addr, 0, tester_present, bus] for addr in addr_list]
32+
msgs = [make_can_msg(addr, tester_present, bus) for addr in addr_list]
3033

3134
messaging.drain_sock(logcan)
3235
sendcan.send(can_list_to_can_capnp(msgs, msgtype='sendcan'))
@@ -45,6 +48,7 @@ def get_ecu_addrs(logcan: messaging.SubSocket, sendcan: messaging.PubSocket, bus
4548
cloudlog.warning(f"ECU addr scan exception: {traceback.format_exc()}")
4649
return ecu_addrs
4750

51+
4852
if __name__ == "__main__":
4953
import argparse
5054

0 commit comments

Comments
 (0)