Skip to content

Commit 9373947

Browse files
committed
Finished corrections to Variable DID size implementation.
1 parent 2f3a603 commit 9373947

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

test/client/test_read_data_by_identifier.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def decode(self, did_payload):
2626
return did_payload
2727

2828
def __len__(self):
29-
return self.ReadAllRemainingData
29+
raise self.ReadAllRemainingData
3030

3131
class CodecWithNoLength(DidCodec):
3232

@@ -162,7 +162,7 @@ def _test_rdbi_variable_size_did(self):
162162

163163
# DID 5 read all the data up to the end. Makes no sense to read another DID after that.
164164
def test_rdbi_variable_size_did_not_last(self):
165-
pass
165+
pass
166166

167167
def _test_rdbi_variable_size_did_not_last(self):
168168
with self.assertRaises(ValueError):
@@ -257,5 +257,5 @@ def test_no_length(self):
257257
pass
258258

259259
def _test_no_length(self):
260-
with self.assertRaises(ConfigError):
260+
with self.assertRaises(NotImplementedError):
261261
self.udsclient.read_data_by_identifier(didlist=[6])

udsoncan/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class DidCodec:
4040
:type packstr: string
4141
"""
4242

43-
class ReadAllRemainingData:
43+
class ReadAllRemainingData(Exception):
4444
pass
4545

4646
def __init__(self, packstr=None):

udsoncan/services/ReadDataByIdentifier.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,28 @@ def make_request(cls, didlist, didconfig):
4545
:raises ConfigError: If didlist contains a DID not defined in didconfig
4646
"""
4747
from udsoncan import Request
48+
from udsoncan import DidCodec
49+
4850
didlist = cls.validate_didlist_input(didlist)
4951

5052
req = Request(cls)
5153
ServiceHelper.check_did_config(didlist, didconfig)
54+
55+
did_reading_all_data = None
56+
for did in didlist:
57+
if did not in didconfig: # Already checked in check_did_config. Paranoid check
58+
raise ConfigError(key=did, msg='Actual data identifier configuration contains no definition for data identifier 0x%04x' % did)
59+
60+
codec = DidCodec.from_config(didconfig[did])
61+
try:
62+
length = len(codec)
63+
if did_reading_all_data is not None:
64+
raise ValueError('Did 0x%04X is configured to read the rest of the payload (__len__ raisong ReadAllRemainingData), but a subsequent DID is requested (0x%04x)' % (did_reading_all_data, did))
65+
except DidCodec.ReadAllRemainingData:
66+
if did_reading_all_data is not None:
67+
raise ValueError('It is impossible to read 2 DIDs configured to read the rest of the payload (__len__ raising ReadAllRemainingData). Dids are : 0x%04X and 0x%04X' % (did_reading_all_data, did))
68+
did_reading_all_data = did
69+
5270
req.data = struct.pack('>'+'H'*len(didlist), *didlist) #Encode list of DID
5371

5472
return req
@@ -106,12 +124,8 @@ def interpret_response(cls, response, didlist, didconfig, tolerate_zero_padding=
106124

107125
try:
108126
payload_size = len(codec)
109-
except NotImplementedError as nie:
110-
if len(didlist) > 1:
111-
raise nie
112-
else:
113-
# we assume the remaining response data corresponds to only one DID, thus read all of it
114-
payload_size = len(response.data) - offset
127+
except DidCodec.ReadAllRemainingData:
128+
payload_size = len(response.data) - offset
115129

116130
if len(response.data) < offset+payload_size:
117131
raise InvalidResponseException(response, "Value for data identifier 0x%04x was incomplete according to definition in configuration" % did)

0 commit comments

Comments
 (0)