Skip to content

Commit

Permalink
Merge pull request #88 from khauersp/configure-dm-timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
snideto authored Sep 22, 2024
2 parents 2811674 + 1aa2c26 commit 2eef93c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
6 changes: 5 additions & 1 deletion j1939/Dm14Query.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def read(
object_byte_size: int = 1,
signed: bool = False,
return_raw_bytes: bool = False,
max_timeout: int = 1,
) -> list:
"""
Send a read query to dest_address, requesting data at address
Expand All @@ -223,6 +224,7 @@ def read(
:param int object_byte_size: size of each object in bytes
:param bool signed: whether the data is signed
:param bool return_raw_bytes: whether to return raw bytes or values
:param int max_timeout: max timeout for transaction
"""
assert object_count > 0
self._dest_address = dest_address
Expand Down Expand Up @@ -255,6 +257,7 @@ def write(
address: int,
values: list,
object_byte_size: int = 1,
max_timeout: int = 1,
) -> None:
"""
Send a write query to dest_address, requesting to write values at address
Expand All @@ -263,6 +266,7 @@ def write(
:param int address: address of the message
:param list values: values to be written
:param int object_byte_size: size of each object in bytes
:param int max_timeout: max timeout for transaction
"""
self._dest_address = dest_address
self.direct = direct
Expand All @@ -276,7 +280,7 @@ def write(
self.state = QueryState.WAIT_FOR_SEED
# wait for operation completed DM15 message
try:
self.data_queue.get(block=True, timeout=1)
self.data_queue.get(block=True, timeout=max_timeout)
for _ in range(self.exception_queue.qsize()):
raise self.exception_queue.get(block=False, timeout=1)
except queue.Empty:
Expand Down
4 changes: 3 additions & 1 deletion j1939/Dm14Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,15 @@ def respond(
data=None,
error: int = 0xFFFFFF,
edcp: int = 0xFF,
max_timeout: int = 3,
) -> list:
"""
Respond to DM14 query with the requested data or confimation of operation is good to proceed
:param bool proceed: whether the operation is good to proceed
:param list data: data to be sent to device
:param int error: error code to be sent to device
:param int edcp: value for edcp extension
:param int max_timeout: max time for transaction
"""
if data is None:
data = []
Expand All @@ -366,5 +368,5 @@ def respond(
self._wait_for_data()
mem_data = None
if self.state == ResponseState.WAIT_FOR_DM16:
mem_data = self.data_queue.get(block=True, timeout=3)
mem_data = self.data_queue.get(block=True, timeout=max_timeout)
return mem_data
19 changes: 16 additions & 3 deletions j1939/memory_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,27 @@ def _listen_for_dm14(
pass

def respond(
self, proceed: bool, data: list = None, error: int = 0xFFFFFF, edcp: int = 0xFF
self,
proceed: bool,
data: list = None,
error: int = 0xFFFFFF,
edcp: int = 0xFF,
max_timeout: int = 3,
) -> list:
"""
Responds with requested data and error code, if applicable, to a read request
:param bool proceed: whether the operation is good to proceed
:param list data: data to be sent to device
:param int error: error code to be sent to device
:param int edcp: value for edcp extension
:param int max_timeout: max timeout for transaction
"""
if data is None:
data = []
if self.state is DMState.WAIT_RESPONSE:
self._ca.unsubscribe(self._listen_for_dm14)
self.state = DMState.IDLE
return_data = self.server.respond(proceed, data, error, edcp)
return_data = self.server.respond(proceed, data, error, edcp, max_timeout)
self._ca.subscribe(self._listen_for_dm14)
return return_data
else:
Expand All @@ -157,6 +163,7 @@ def read(
object_byte_size: int = 1,
signed: bool = False,
return_raw_bytes: bool = False,
max_timeout: int = 1,
) -> list:
"""
Make a dm14 read Query
Expand All @@ -167,6 +174,7 @@ def read(
:param int object_byte_size: size of each object in bytes
:param bool signed: whether the data is signed
:param bool return_raw_bytes: whether to return raw bytes or values
:param int max_timeout: max timeout for transaction
"""
if self.state == DMState.IDLE:
self.state = DMState.WAIT_QUERY
Expand All @@ -179,6 +187,7 @@ def read(
object_byte_size,
signed,
return_raw_bytes,
max_timeout,
)
self.state = DMState.IDLE
return data
Expand All @@ -192,6 +201,7 @@ def write(
address: int,
values: list,
object_byte_size: int = 1,
max_timeout: int = 1,
) -> None:
"""
Send a write query to dest_address, requesting to write values at address
Expand All @@ -200,11 +210,14 @@ def write(
:param int address: address of the message
:param list values: values to be written
:param int object_byte_size: size of each object in bytes
:param int max_timeout: max timeout for transaction
"""
if self.state == DMState.IDLE:
self.state = DMState.WAIT_QUERY
self.address = dest_address
self.query.write(dest_address, direct, address, values, object_byte_size)
self.query.write(
dest_address, direct, address, values, object_byte_size, max_timeout
)
self.state = DMState.IDLE

def set_seed_generator(self, seed_generator: callable) -> None:
Expand Down

0 comments on commit 2eef93c

Please sign in to comment.