-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_fr20_ame.py
executable file
·99 lines (73 loc) · 2.94 KB
/
check_fr20_ame.py
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
#!/usr/bin/env python3.10
'''
This uses a CAN link layer to check response to an AME frame
Usage:
python3.10 check_fr20_ame.py
The -h option will display a full list of options.
'''
import sys
import logging
from openlcb.nodeid import NodeID
from openlcb.canbus.canframe import CanFrame
from openlcb.canbus.controlframe import ControlFrame
from queue import Empty
import olcbchecker.setup
def check():
# set up the infrastructure
logger = logging.getLogger("FRAME")
ownnodeid = olcbchecker.setup.configure.ownnodeid
targetnodeid = olcbchecker.setup.configure.targetnodeid
timeout = 0.3
olcbchecker.purgeFrames()
localAlias = olcbchecker.setup.canLink.localAlias # just to be shorter
###############################
# checking sequence starts here
###############################
# send the global AME frame to start the exchange
frame = CanFrame(ControlFrame.AME.value, localAlias)
olcbchecker.setup.sendCanFrame(frame)
try :
# loop for an AMD from DBC or at least not from us
while True :
# check for AMD frame
waitFor = "waiting for initial AMD frame after global AME"
frame = olcbchecker.getFrame(1.0)
if (frame.header & 0xFF_FFF_000) != 0x10_701_000 :
logger.warning ("Failure - frame was not AMD frame in first part")
return 3
# check it carries a node ID
if len(frame.data) < 6 :
logger.warning ("Failure - first part AMD frame did not carry node ID")
return 3
if targetnodeid is None :
# we'll take the first one not from us
if NodeID(frame.data) != NodeID(ownnodeid) :
break
else : # here we have a node ID to match
if NodeID(frame.data) == NodeID(targetnodeid) :
break
# loop to try again
olcbchecker.purgeFrames()
# get that node ID, create and send an AMD using it
frame = CanFrame(ControlFrame.AME.value, localAlias, frame.data)
olcbchecker.setup.sendCanFrame(frame)
# check for AMD frame
waitFor = "waiting for AMD frame after AME with address "+str(NodeID(frame.data))
frame = olcbchecker.getFrame(1.0)
if (frame.header & 0xFF_FFF_000) != 0x10_701_000 :
logger.warning ("Failure - frame was not AMD frame in second part")
return 3
# check it carries a node ID
if len(frame.data) < 6 :
logger.warning ("Failure - second AMD frame did not carry node ID")
return 3
except Empty:
logger.warning ("Failure - no frame received while "+waitFor)
return 3
logger.info("Passed")
return 0
if __name__ == "__main__":
result = check()
import olcbchecker
olcbchecker.setup.interface.close()
sys.exit(result)