Skip to content

Commit 1db7da8

Browse files
committed
NmtMaster: Delay heartbeat callbacks after state update.
Reduce the amount of code within the critical section holding the lock. Unpack and log first. Update timestamp and state while locked and notify condition waiters. Invoke callbacks after releasing the lock.
1 parent 0130eb8 commit 1db7da8

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

canopen/nmt.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,13 @@ def __init__(self, node_id: int):
120120
self._callbacks: List[Callable[[int], None]] = []
121121

122122
def on_heartbeat(self, can_id, data, timestamp):
123+
new_state, = struct.unpack_from("B", data)
124+
# Mask out toggle bit
125+
new_state &= 0x7F
126+
logger.debug("Received heartbeat can-id %d, state is %d", can_id, new_state)
127+
123128
with self.state_update:
124129
self.timestamp = timestamp
125-
new_state, = struct.unpack_from("B", data)
126-
# Mask out toggle bit
127-
new_state &= 0x7F
128-
logger.debug("Received heartbeat can-id %d, state is %d", can_id, new_state)
129-
for callback in self._callbacks:
130-
callback(new_state)
131130
if new_state == 0:
132131
# Boot-up, will go to PRE-OPERATIONAL automatically
133132
self._state = 127
@@ -136,6 +135,9 @@ def on_heartbeat(self, can_id, data, timestamp):
136135
self._state_received = new_state
137136
self.state_update.notify_all()
138137

138+
for callback in self._callbacks:
139+
callback(new_state)
140+
139141
def send_command(self, code: int):
140142
"""Send an NMT command code to the node.
141143

0 commit comments

Comments
 (0)