[Dcm] Vehicle Diagnostic Communication Part 78 [Simulation 17]

[Dcm] Vehicle Diagnostic Communication Part 78 [Simulation 17] 車両診断通信
[Dcm] Vehicle Diagnostic Communication Part 78 [Simulation 17]

Click here for back issues.
https://www.simulationroom999.com/blog/diagnostic-communication-en-back-issue/

Introduction.

Explanation of the AUTOSAR-Dcm simulation.
In this article, we will describe the Python code for the TesterPresents simulation.

Python code for TesterPresents simulation

The following is the Python code for the TesterPresents simulation.
It is much simpler than the previous version.

import isotp
import logging
import time
import threading

from can.interfaces.vector import VectorBus

class ThreadedApp:
   def __init__(self):
      isotp_params = {
         'stmin' : 0, 
         'blocksize' : 4,
         'wftmax' : 0,
         'll_data_length' : 8,
         'tx_padding' : 0xCC,
         'rx_flowcontrol_timeout' : 1000,
         'rx_consecutive_frame_timeout' : 1000,
         'squash_stmin_requirement' : False,
         'can_fd' : False,
         'tx_data_min_length' : 8
      }
      self.exit_requested = False
      #self.bus = VectorBus(channel='0', bitrate=500000)
      self.bus = VectorBus(channel='0', bitrate=500000, fd=True)
      addr = isotp.Address(isotp.AddressingMode.NormalFixed_29bits, source_address=0xF1, target_address=0x10) 
      self.stack = isotp.CanStack(self.bus, address=addr, params=isotp_params, error_handler=self.my_error_handler)

   def start(self):
      self.exit_requested = False
      self.thread = threading.Thread(target = self.thread_task)
      self.thread.start()

   def stop(self):
      self.exit_requested = True
      if self.thread.isAlive():
         self.thread.join()
   
   def send(self, msg):
      self.stack.send(msg)
   
   def my_error_handler(self, error):
      logging.warning('IsoTp error happened : %s - %s' % (error.__class__.__name__, str(error)))

   def thread_task(self):
      while self.exit_requested == False:
         self.stack.process()                # Non-blocking
         #time.sleep(self.stack.sleep_time()) # Variable sleep time based on state machine state
         time.sleep(0.001) # Variable sleep time based on state machine state

   def shutdown(self):
      self.stop()
      self.bus.shutdown()

def sendrecv( app, msg ):
   
   if msg[0] == 0x00:
      tsleep = msg[1]*0x100 + msg[2]
      tsleep = tsleep / 1000
      print("sleep : %f [ms]" % tsleep)
      time.sleep(tsleep)
   else:
      print("Send msg : %s" % (msg.hex()))
      app.send(msg)
      t1 = time.time()
      while time.time() - t1 < 0.05:
         if app.stack.available():
            payload = app.stack.recv()
            print("Recv msg : %s" % (payload.hex()))
            break
         time.sleep(0.001)


if __name__ == '__main__':
   app = ThreadedApp()
   app.start()
   
   datas=[
      bytes([0x3E, 0x00]),
      bytes([0x3E, 0x00, 0x00]),
      bytes([0x3E, 0x01]),
      bytes([0x3E, 0x80]),
      bytes([0x3E, 0x80, 0x00]),
      bytes([0x3E, 0x81]),
      bytes([0x10, 0x03]),
      bytes([0x27, 0x13]),
      bytes([0x27, 0x14, 0xDE, 0xAD, 0xBE, 0xEF]),
      bytes([0x00, 0x13, 0x24]),
      bytes([0x3E, 0x80]),
      bytes([0x00, 0x13, 0x24]),
      bytes([0x3E, 0x80]),
      bytes([0x00, 0x13, 0x24]),
      bytes([0x27, 0x13]),
      bytes([0x00, 0x13, 0xEC]),
      bytes([0x27, 0x13]),
   ]
   
   #while True:
   for i in range(len(datas)):
      sendrecv(app, datas[i])

   print("Exiting")
   app.shutdown()

Python code explanation for simulation of TesterPresents

This time it is supposed to be TesterPresents, but in terms of messages, SessionControl and SecurityAccess are also needed.
This is because the usage scenario of TesterPresents is to maintain sessions.
Therefore, the session and security are mixed, and if it takes less than 5 seconds, the session is maintained, and if it takes more than 5 seconds, it returns to defaultSession.

The process is as follows.

(1)TesterPresents as usual
(2)Wrong message length in TesterPresents
(3)Non-existent sub-function of TesterPresents
(4)TesterPresents(suppressPosRspMsgIndicationBit exists)
(5)Wrong message length for TesterPresents(suppressPosRspMsgIndicationBit exists)
(6)Non-existent sub-function of TesterPresents (suppressPosRspMsgIndicationBit exists)
(7)Transition to extendDiagnosticSession
(8)Seed request
(9)Key sent (Key=0xdeadbeef)
(10)4.900[ms] Wait
(11)TesterPresents(suppressPosRspMsgIndicationBit exists)
(12)4.900[ms] Wait
(13)TesterPresents(suppressPosRspMsgIndicationBit exists)
(14)4.900[ms] Wait
(15)SeedRequest
(16)5.100[ms] Wait
(17)Seed request

Roughly explained as follows.

  • In the beginning, I’m trying normal and error patterns both with and without suppressPosRspMsgIndicationBit.
  • In the middle, we unlock security and throw TesterPresents just before the S3 timeout to maintain the session.
  • If the Seed is 0, the session is maintained.

Conclusion

  • I wrote a Python code to simulate TesterPresents.
  • Considering the main purpose of TesterPresents, SessionControl and SecurityAccess are also implemented.
    • The main purpose is to suppress S3 timeouts.

Click here for back issues.

コメント

タイトルとURLをコピーしました