Request and Response¶
Messages exchanged by the client and the server are represented by a Request
and a Response
.
The client sends Requests
to the server that include a service number, an optional subfunction and some data. The server processes the request and answers with a Response
that contains an echo of the service number, a response code and some additional data.
The following classes provides the necessary interface to manipulate UDS requests and responses.
Request¶
req = Request(service=ECUReset, subfunction=1, data=b'\x99\x88')
payload = req.get_payload()
print(payload) # b'\x11\x01\x99\x88'
req2 = Request.from_payload(payload)
print(req2) # <Request: [ECUReset] (subfunction=1) - 2 data bytes at 0x12345678>
-
class
udsoncan.
Request
(service: typing.Union[udsoncan.BaseService.BaseService, typing.Type[udsoncan.BaseService.BaseService], NoneType] = None, subfunction: typing.Union[int, NoneType] = None, suppress_positive_response: bool = False, data: typing.Union[bytes, NoneType] = None)[source]¶ Represents a UDS Request.
Parameters: - service (class) – The service for which to make the request. This parameter must be a class that extends
udsoncan.services.BaseService
- subfunction (int or None) – The service subfunction. This value may be ignored if the given service does not supports subfunctions
- suppress_positive_response (bool) – Indicates that the server should not send a response if the response code is positive. This parameter has effect only when the given service supports subfunctions
- data (bytes) – The service data appended after service ID and payload
- service (class) – The service for which to make the request. This parameter must be a class that extends
-
Request.
get_payload
(suppress_positive_response: typing.Union[bool, NoneType] = None) → bytes[source]¶ Generates a payload to be given to the underlying protocol. This method is meant to be used by a UDS client
Returns: A payload to be sent through the underlying protocol Return type: bytes
-
classmethod
Request.
from_payload
(payload: bytes) → udsoncan.Request.Request[source]¶ Creates a
Request
object from a payload coming from the underlying protocols. This method is meant to be used by a UDS serverParameters: payload (bytes) – The payload of data to parse Returns: A Request object with populated fields Return type: Request
Response¶
response = Response(service=ECUReset, code=Response.Code.PositiveResponse, data=b'\x11\x22')
payload = response.get_payload()
print(payload) # b'\x51\x11\x22'
response2 = Response.from_payload(payload)
print(response2) # <PositiveResponse: [ECUReset] - 2 data bytes at 0x7f9367e619b0>
-
class
udsoncan.
Response
(service: typing.Union[udsoncan.BaseService.BaseService, typing.Type[udsoncan.BaseService.BaseService], NoneType] = None, code: typing.Union[int, NoneType] = None, data: typing.Union[bytes, NoneType] = None)[source]¶ Represents a server Response to a client Request
Parameters: - service (class) – The service implied by this response.
- code (int) – The response code
- data (bytes) – The response data encoded after the service and response code
-
valid
¶ (boolean) True if the response content is valid. Only
invalid_reason
is guaranteed to have a meaningful value if this value is False
-
invalid_reason
¶ (string) String explaining why the response is invalid.
-
positive
¶ (boolean) True if the response code is 0 (PositiveResponse), False otherwise
-
code
¶ (int) The response code.
-
code_name
¶ (string) The response code name.
-
data
¶ (bytes) The response data. All the payload content, except the service number and the response code
-
service_data
¶ (object) The content of
data
interpreted by a service; can be any type of content.
-
original_payload
¶ (bytes) When the response is built with Response.from_payload, this property contains a copy of the payload used. None otherwise.
-
Response.
get_payload
() → bytes[source]¶ Generates a payload to be given to the underlying protocol. This method is meant to be used by a UDS server
Returns: A payload to be sent through the underlying protocol Return type: bytes
-
classmethod
Response.
from_payload
(payload: bytes) → udsoncan.Response.Response[source]¶ Creates a
Response
object from a payload coming from the underlying protocol. This method is meant to be used by a UDS clientParameters: payload (bytes) – The payload of data to parse Returns: A Response object with populated fields Return type: Response