Skip to content
Snippets Groups Projects
Commit 0c8eb74c authored by Claudiu MILEA's avatar Claudiu MILEA
Browse files

Add separate sockets to ECU processes for packet sniffing

parent 49a251d8
No related branches found
No related tags found
1 merge request!3Add basic setup for sending frames via CAN Bus and Ethernet between three processes (chassis, powertrain, body)
from scapy.config import conf import time
from scapy.contrib.cansocket_python_can import CANSocket
from scapy.main import load_contrib
from can_id_sheet import VehicleSegment from can_id_sheet import VehicleSegment, CANFrame
from ecu_process import GenericECU from ecu_process import GenericECU
from scapy.config import conf
from scapy.contrib.cansocket_python_can import CANSocket
from scapy.main import load_contrib
from time import sleep from time import sleep
import multiprocessing as mp import multiprocessing as mp
import can import can
...@@ -30,6 +30,12 @@ def coordinate_python_can(): ...@@ -30,6 +30,12 @@ def coordinate_python_can():
sleep(5) sleep(5)
def get_filtered_can_socket(vehicle_segment: VehicleSegment):
filters = [{'can_id': f.value, 'can_mask': 0x7FF} for f in CANFrame if
f.execution == vehicle_segment]
return CANSocket(bustype='virtual', channel='vcan0', bitrate=250000, can_filters=filters)
if __name__ == '__main__': if __name__ == '__main__':
# CAN related setup # CAN related setup
# load_layer("can") # load_layer("can")
...@@ -38,15 +44,20 @@ if __name__ == '__main__': ...@@ -38,15 +44,20 @@ if __name__ == '__main__':
conf.contribs['CANSocket'] = {'use-python-can': True} conf.contribs['CANSocket'] = {'use-python-can': True}
load_contrib('cansocket') load_contrib('cansocket')
socket = CANSocket(bustype='virtual', channel='vcan0', bitrate=250000) socket = CANSocket(bustype='virtual', channel='vcan0', bitrate=250000)
# socket = CANSocket(channel='vcan0') # socket = CANSocket(channel='vcan0')
# multiprocessing setup # multiprocessing setup
mp.set_start_method('fork') mp.set_start_method('fork')
processes = [ processes = [
GenericECU(socket=socket, vehicle_segment=VehicleSegment.Chassis), GenericECU(tx_socket=socket,
GenericECU(socket=socket, vehicle_segment=VehicleSegment.PowerTrain), rx_socket=get_filtered_can_socket(VehicleSegment.Chassis),
GenericECU(socket=socket, vehicle_segment=VehicleSegment.Body), vehicle_segment=VehicleSegment.Chassis),
GenericECU(tx_socket=socket,
rx_socket=get_filtered_can_socket(VehicleSegment.PowerTrain),
vehicle_segment=VehicleSegment.PowerTrain),
GenericECU(tx_socket=socket,
rx_socket=get_filtered_can_socket(VehicleSegment.Body),
vehicle_segment=VehicleSegment.Body)
] ]
for p in processes: for p in processes:
......
...@@ -11,28 +11,36 @@ from scapy.sendrecv import * ...@@ -11,28 +11,36 @@ from scapy.sendrecv import *
class GenericECU(Process): class GenericECU(Process):
def __init__(self, def __init__(self,
socket, # type: CANSocket tx_socket, # type: CANSocket
rx_socket, # type: CANSocket
vehicle_segment: VehicleSegment vehicle_segment: VehicleSegment
): ):
def sniff_function(pkt):
print(pkt.__repr__())
Process.__init__(self) Process.__init__(self)
self.socket = socket self.tx_socket = tx_socket
self.rx_socket = rx_socket
self.frames_to_send = [frame for frame in CANFrame if frame.source == vehicle_segment] self.frames_to_send = [frame for frame in CANFrame if frame.source == vehicle_segment]
self.name = vehicle_segment.name self.name = vehicle_segment.name
# split frames of this ECU by sending frequency and schedule them on separate cyclic threads
tasks = []
ecu_work_description = f'Work description for {self.name} ECU:\n' ecu_work_description = f'Work description for {self.name} ECU:\n'
tasks = [AsyncSniffer(opened_socket=self.rx_socket, prn=sniff_function)]
# split frames of this ECU by sending frequency and schedule them on separate cyclic threads
for freq in Frequency: for freq in Frequency:
frames_for_freq = list(filter(lambda x: x.interval == freq, self.frames_to_send)) frames_for_freq = list(filter(lambda x: x.interval == freq, self.frames_to_send))
if not frames_for_freq: if not frames_for_freq:
continue continue
tasks.append(PeriodicSendingTask(socket=self.socket, frames=frames_for_freq, interval_s=freq.value)) tasks.append(PeriodicSendingTask(socket=self.tx_socket, frames=frames_for_freq, interval_s=freq.value))
ecu_work_description += f' - Created cyclic thread which will send {frames_for_freq} every {freq.value}s.\n' ecu_work_description += f' - Created cyclic thread which will send {frames_for_freq} every {freq.value}s.\n'
self.tasks = tasks self.tasks = tasks
print(ecu_work_description) print(ecu_work_description)
def run(self): def run(self):
print(f'Hello from ECU with segment: {self.name} and socket: {self.socket}.') print(f'Hello from ECU with segment: {self.name} and socket: {self.tx_socket}.')
for t in self.tasks: for t in self.tasks:
t.start() t.start()
......
...@@ -28,7 +28,6 @@ class PeriodicSendingTask(Thread): ...@@ -28,7 +28,6 @@ class PeriodicSendingTask(Thread):
msg_due_time_ns = time.perf_counter_ns() msg_due_time_ns = time.perf_counter_ns()
while not self.stopped: while not self.stopped:
# to be replaced with sending
send(self.packets, verbose=0, socket=self.socket) send(self.packets, verbose=0, socket=self.socket)
print(f'Sent {len(self.packets)} packets at {time.time()}, frequency = {self.interval_s}s') print(f'Sent {len(self.packets)} packets at {time.time()}, frequency = {self.interval_s}s')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment