How to use the udsoncan.Request function in udsoncan

To help you get started, we’ve selected a few udsoncan examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pylessard / python-udsoncan / test / test_request.py View on Github external
def test_suppress_positive_response(self):
        req = Request(DummyServiceNormal, subfunction=0x44, suppress_positive_response=True)
        payload = req.get_payload()
        self.assertEqual(b"\x13\xC4", payload)	# Subfunction bit 7 is set
github pylessard / python-udsoncan / testserver.py View on Github external
from udsoncan import sessions, services, Request, Response, Connection
from udsoncan.server import Server
import struct


import time
conn = Connection('vcan0', rxid=0x456, txid=0x123)
with conn.open():
	while True:
		payload = conn.wait_frame(timeout=None)
		if payload is not None:
			print("Received: " + ''.join(['%02X' % b for b in payload]))
			req = Request.from_payload(payload)
			response = Response(req.service, Response.Code.GeneralReject)

			## DiagnosticSessionControl
			if req.service == services.DiagnosticSessionControl:
				if sessions.from_id(req.subfunction) == sessions.ExtendedDiagnosticSession:
					response = Response(req.service, Response.Code.PositiveResponse)
				else:
					response = Response(req.service, Response.Code.SubFunctionNotSupported)
			
			## SecurityAccess
			elif req.service == services.SecurityAccess:
				if req.subfunction == 3:
					response = Response(req.service, Response.Code.PositiveResponse, data=b"\x12\x34\x56\x78")
				elif req.subfunction == 4:
					if req.data == b"\xed\xcb\xa9\x87":
						response = Response(req.service, Response.Code.PositiveResponse)
github pylessard / python-udsoncan / test / test_request.py View on Github external
def test_make_payload_basic(self):
        req = Request(DummyServiceNormal, subfunction=0x44)
        payload = req.get_payload()
        self.assertEqual(b"\x13\x44", payload)
github pylessard / python-udsoncan / udsoncan / services / TransferData.py View on Github external
Allowed values are from 0 to 0xFF
        :type sequence_number: int

        :param data: Optional additional data to send to the server
        :type data: bytes

        :raises ValueError: If parameters are out of range, missing or wrong type
        """		
        from udsoncan import Request, MemoryLocation

        ServiceHelper.validate_int(sequence_number, min=0, max=0xFF, name='Block sequence counter')	# Not a subfunction!

        if data is not None and not isinstance(data, bytes):
            raise ValueError('data must be a bytes object')

        request = Request(service=cls)
        request.data = struct.pack('B', sequence_number)

        if data is not None:
            request.data += data
        return request

github pylessard / python-udsoncan / udsoncan / services / ReadMemoryByAddress.py View on Github external
def make_request(cls, memory_location):
        """
        Generates a request for ReadMemoryByAddress

        :param memory_location: The address and the size of the memory block to read.
        :type memory_location: :ref:`MemoryLocation `

        :raises ValueError: If parameters are out of range, missing or wrong type
        """		
        from udsoncan import Request, MemoryLocation

        if not isinstance(memory_location, MemoryLocation):
            raise ValueError('Given memory location must be an instance of MemoryLocation')

        request =  Request(service=cls)
        request.data = b''
        request.data += memory_location.alfid.get_byte() # AddressAndLengthFormatIdentifier
        request.data += memory_location.get_address_bytes()
        request.data += memory_location.get_memorysize_bytes()

        return request
github pylessard / python-udsoncan / udsoncan / services / RequestDownload.py View on Github external
:type memory_location: :ref:`MemoryLocation `

        :param dfi: Optional :ref:`DataFormatIdentifier ` defining the compression and encryption scheme of the data. 
                If not specified, the default value of 00 will be used, specifying no encryption and no compression
        :type dfi: :ref:`DataFormatIdentifier `	

        :raises ValueError: If parameters are out of range, missing or wrong type
        """				
        from udsoncan import Request, MemoryLocation

        dfi = cls.normalize_data_format_identifier(dfi)

        if not isinstance(memory_location, MemoryLocation):
            raise ValueError('memory_location must be an instance of MemoryLocation')

        request = Request(service=cls)
        request.data=b""
        request.data += dfi.get_byte()	# Data Format Identifier
        request.data += memory_location.alfid.get_byte()	# AddressAndLengthFormatIdentifier
        request.data += memory_location.get_address_bytes()
        request.data += memory_location.get_memorysize_bytes()

        return request

github pylessard / python-udsoncan / udsoncan / services / InputOutputControlByIdentifier.py View on Github external
if not isinstance(values, IOValues):
                raise ValueError("values must be an instance of IOValues")

        if masks is not None:
            if isinstance(masks, list):
                masks = IOMasks(*masks)
            if isinstance(masks, dict):
                masks = IOMasks(**masks)

            if not isinstance(masks, IOMasks) and not isinstance(masks, bool):
                raise ValueError("masks must be an instance of IOMask or a boolean value")

        if values is None and masks is not None:
            raise ValueError('An IOValue must be given if a IOMask is provided.')	

        request = Request(service=cls)		

        request.data = b''
        ioconfig = ServiceHelper.check_io_config(did, ioconfig)	# IO dids are defined in client config.
        request.data += struct.pack('>H', did)

        # This parameter is optional according to standard
        if control_param is not None:
            request.data += struct.pack('B', control_param)

        codec = DidCodec.from_config(ioconfig[did])	# Get IO codec from config

        if values is not None:
            request.data += codec.encode(*values.args, **values.kwargs)

        if masks is not None: # Skip the masks byte if none is given.
            if isinstance(masks, bool):
github pylessard / python-udsoncan / udsoncan / services / DiagnosticSessionControl.py View on Github external
def make_request(cls, session):
        """
        Generates a request for DiagnosticSessionControl service

        :param session: Service subfunction. Allowed values are from 0 to 0x7F
        :type session: int

        :raises ValueError: If parameters are out of range, missing or wrong type
        """

        from udsoncan import Request
        ServiceHelper.validate_int(session, min=0, max=0x7F, name='Session number')
        return Request(service=cls, subfunction=session)
github pylessard / python-udsoncan / udsoncan / services / SecurityAccess.py View on Github external
If the even/odd constraint is not respected, the level value will be corrected to properly set the LSB.
        :type level: int

        :param mode: Type of request to perform. ``SecurityAccess.Mode.RequestSeed`` or ``SecurityAccess.Mode.SendKey`` 
        :type mode: SecurityAccess.Mode, int

        :param key: When mode=``SendKey``, this value must be provided.
        :type key: bytes

        :raises ValueError: If parameters are out of range, missing or wrong type
        """		
        from udsoncan import Request
        cls.validate_mode(mode)

        ServiceHelper.validate_int(level, min=0, max=0x7F, name='Security level')
        req = Request(service=cls, subfunction=cls.normalize_level(mode=mode, level=level))

        if mode == cls.Mode.SendKey:
            if not isinstance(key, bytes):
                raise ValueError('key must be a valid bytes object')
            req.data = key

        return req

github pylessard / python-udsoncan / udsoncan / services / RequestTransferExit.py View on Github external
def make_request(cls, data=None):
        """
        Generates a request for RequestTransferExit

        :param data: Additional optional data to send to the server
        :type data: bytes

        :raises ValueError: If parameters are out of range, missing or wrong type
        """			
        from udsoncan import Request, MemoryLocation

        if data is not None and not isinstance(data, bytes):
            raise ValueError('data must be a bytes object')

        request = Request(service=cls, data=data)
        return request