Skip to content

Commit

Permalink
Support parsing of node attributes in J2602 LDF file (#130)
Browse files Browse the repository at this point in the history
* Add support to handle J2602_*_1 version LDF files
* Default J2602 attributes to None for non-J2602 files. Add attribute to version class to indicate J2602 status.
* Update changelog
* Bump version number
  • Loading branch information
nuts4coffee authored Jan 30, 2024
1 parent 63ae6cc commit 31b4fa6
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.22.0] - 2024-01-30

### Added

- Support for J2602 slave node attributes:
- `wakeup_time`, `poweron_time`

## [0.21.0] - 2023-09-13

### Fixes
Expand Down
6 changes: 6 additions & 0 deletions ldfparser/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ def node_definition_configurable_frames_21(self, tree):
def node_definition_response_tolerance(self, tree):
return ("response_tolerance", tree[0] * 0.01)

def node_definition_wakeup_time(self, tree):
return ("wakeup_time", tree[0] * 0.001)

def node_definition_poweron_time(self, tree):
return ("poweron_time", tree[0] * 0.001)

def schedule_tables(self, tree):
return ("schedule_tables", tree)

Expand Down
5 changes: 3 additions & 2 deletions ldfparser/grammars/ldf.lark
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ diagnostic_address: ldf_identifier ":" ldf_integer ";"
// LIN 2.1 Specification, section 9.2.2.2
// response_tolerance: SAE J2602_1_201001, section 7.2.1
node_attributes: "Node_attributes" "{" (node_definition*) "}"
node_definition: ldf_identifier "{" (node_definition_protocol | node_definition_configured_nad | node_definition_initial_nad | node_definition_product_id | node_definition_response_error | node_definition_fault_state_signals | node_definition_p2_min | node_definition_st_min | node_definition_n_as_timeout | node_definition_n_cr_timeout | node_definition_configurable_frames | node_definition_response_tolerance)* "}"
node_definition: ldf_identifier "{" (node_definition_protocol | node_definition_configured_nad | node_definition_initial_nad | node_definition_product_id | node_definition_response_error | node_definition_fault_state_signals | node_definition_p2_min | node_definition_st_min | node_definition_n_as_timeout | node_definition_n_cr_timeout | node_definition_configurable_frames | node_definition_response_tolerance | node_definition_wakeup_time | node_definition_poweron_time)* "}"
node_definition_protocol: "LIN_protocol" "=" (["\"" ldf_version "\""] | ldf_version) ";"
node_definition_configured_nad: "configured_NAD" "=" ldf_integer ";"
node_definition_initial_nad: "initial_NAD" "=" ldf_integer ";"
Expand All @@ -84,7 +84,8 @@ node_definition_configurable_frames: node_definition_configurable_frames_20 | no
node_definition_configurable_frames_20: "configurable_frames" "{" (ldf_identifier "=" ldf_integer ";")+ "}"
node_definition_configurable_frames_21: "configurable_frames" "{" (ldf_identifier ";")+ "}"
node_definition_response_tolerance: "response_tolerance" "=" ldf_float "%" ";"

node_definition_wakeup_time: "wakeup_time" "=" ldf_float "ms" ";"
node_definition_poweron_time: "poweron_time" "=" ldf_float "ms" ";"

// LIN 2.1 Specification, section 9.2.5
schedule_tables: "Schedule_tables" "{" (schedule_table_definition+) "}"
Expand Down
8 changes: 8 additions & 0 deletions ldfparser/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ class LinSlave(LinNode):
:param response_tolerance: The value between 0.0 - 1.0 that represents the
percentage of the frame response tolerance. For example, 0.4 for 40%.
:type response_tolerance: float
:param wakeup_time: The time in seconds a responder-node requires to recover from LIN sleep to normal
communication state
:type wakeup_time: float
:param poweron_time: The time in seconds a responder-node requires to recover from power down to LIN
normal communication state
:type poweron_time: float
"""

def __init__(self, name: str) -> None:
Expand All @@ -143,6 +149,8 @@ def __init__(self, name: str) -> None:
self.n_cr_timeout: float = 1
self.configurable_frames = {}
self.response_tolerance: float = None
self.wakeup_time: float = None
self.poweron_time: float = None

class LinNodeCompositionConfiguration:

Expand Down
8 changes: 7 additions & 1 deletion ldfparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,14 @@ def _create_ldf2x_node(node: dict, language_version: float):
slave.n_as_timeout = node.get('N_As_timeout', 1)
slave.n_cr_timeout = node.get('N_Cr_timeout', 1)
is_j2602_protocol = isinstance(slave.lin_protocol, J2602Version)
default_resp_tolerance = 0.4 if is_j2602_protocol else None
if is_j2602_protocol:
default_resp_tolerance, default_wakeup_time, default_poweron_time = 0.4, 0.1, 0.1
else:
default_resp_tolerance, default_wakeup_time, default_poweron_time = None, None, None

slave.response_tolerance = node.get('response_tolerance', default_resp_tolerance)
slave.wakeup_time = node.get('wakeup_time', default_wakeup_time)
slave.poweron_time = node.get('poweron_time', default_poweron_time)

return slave

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[metadata]
version = 0.21.0
version = 0.22.0
2 changes: 2 additions & 0 deletions tests/ldf/j2602_1.ldf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Node_attributes {
LIN_protocol = 2.0;
configured_NAD = 0x01;
response_tolerance = 38 % ;
wakeup_time = 50 ms;
poweron_time = 60 ms;
}
}

Expand Down
12 changes: 8 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,19 @@ def test_load_j2602_attributes():
assert ldf.master.max_header_length == 24
assert ldf.master.response_tolerance == 0.3
assert list(ldf.slaves)[0].response_tolerance == 0.38
assert list(ldf.slaves)[0].wakeup_time == 0.05
assert list(ldf.slaves)[0].poweron_time == 0.06

@pytest.mark.unit
@pytest.mark.parametrize(
'file, max_header_length, master_response_tolerance, slave_response_tolerance',
'file, max_header_length, master_response_tolerance, slave_response_tolerance, slave_wakeup_time, slave_poweron_time',
[
("lin20.ldf", None, None, None),
("j2602_1_no_values.ldf", 48, 0.4, 0.4)
("lin20.ldf", None, None, None, None, None),
("j2602_1_no_values.ldf", 48, 0.4, 0.4, 0.1, 0.1)
]
)
def test_j2602_attributes_default(
file, max_header_length, master_response_tolerance, slave_response_tolerance):
file, max_header_length, master_response_tolerance, slave_response_tolerance, slave_wakeup_time, slave_poweron_time):
"""
Should not set default value for J2602 attributes if protocol is not J2602
"""
Expand All @@ -242,3 +244,5 @@ def test_j2602_attributes_default(
assert ldf.master.max_header_length == max_header_length
assert ldf.master.response_tolerance == master_response_tolerance
assert list(ldf.slaves)[0].response_tolerance == slave_response_tolerance
assert list(ldf.slaves)[0].wakeup_time == slave_wakeup_time
assert list(ldf.slaves)[0].poweron_time == slave_poweron_time

0 comments on commit 31b4fa6

Please sign in to comment.