def write_car_event_to_can_packet(car: Vehicle, event_list: CarEvent): pkt_list = [] for event in event_list: if event.ID == CarEvent.CAR_EVENT_FREE: continue data = car.dbc_data.simple_msg_encode(event.ID, event.value) # extend to 8 bytes by padding zero data = data + '00' * (8 - car.dbc_data.get_msg_length_in_byte(event.ID)) byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7 = \ int(data[0:2], 16), \ int(data[2:4], 16), \ int(data[4:6], 16), \ int(data[6:8], 16), \ int(data[8:10], 16), \ int(data[10:12], 16), \ int(data[12:14], 16), \ int(data[14:], 16) can_payload = \ struct.pack('<8B', byte0, byte1, byte2, byte3, byte4, byte5, byte6, byte7) pkt = CAN(identifier=event.ID, length=len(data), data=can_payload) pkt.time = event.timestamp pkt_list.append(pkt) wrpcap("can_packet.pcap", pkt_list) return
def recv(self): msg = self.iface.recv(timeout=1) if msg is None: raise CANSocketTimeoutElapsed frame = CAN(identifier=msg.arbitration_id, length=msg.dlc, data=bytes(msg.data)) if msg.is_error_frame: frame.flags |= 0x1 if msg.is_remote_frame: frame.flags |= 0x2 if msg.is_extended_id: frame.flags |= 0x4 frame.time = msg.timestamp return frame
def recv(self): msg = self.iface.recv(timeout=1) if msg is None: raise CANSocketTimeoutElapsed frame = CAN(identifier=msg.arbitration_id, length=msg.dlc, data=bytes(msg.data)) if msg.is_error_frame: frame.flags |= 0x1 if msg.is_remote_frame: frame.flags |= 0x2 if msg.is_extended_id: frame.flags |= 0x4 frame.time = msg.timestamp return frame
def recv(self, x=CAN_FRAME_SIZE): try: pkt, sa_ll = self.ins.recvfrom(x) except BlockingIOError: # noqa: F821 warning("Captured no data, socket in non-blocking mode.") return None except socket.timeout: warning("Captured no data, socket read timed out.") return None except OSError: # something bad happened (e.g. the interface went down) warning("Captured no data.") return None # need to change the byteoder of the first four bytes, # required by the underlying Linux SocketCAN frame format pkt = struct.pack("<I12s", *struct.unpack(">I12s", pkt)) len = pkt[4] canpkt = CAN(pkt[:len + 8]) canpkt.time = get_last_packet_timestamp(self.ins) if self.remove_padding: return canpkt else: return canpkt / Padding(pkt[len + 8:])
def recv(self, x=CAN_FRAME_SIZE): try: pkt, sa_ll = self.ins.recvfrom(x) except BlockingIOError: # noqa: F821 warning("Captured no data, socket in non-blocking mode.") return None except socket.timeout: warning("Captured no data, socket read timed out.") return None except OSError: # something bad happened (e.g. the interface went down) warning("Captured no data.") return None # need to change the byteoder of the first four bytes, # required by the underlaying Linux SocketCAN frame format pkt = struct.pack("<I12s", *struct.unpack(">I12s", pkt)) len = pkt[4] canpkt = CAN(pkt[:len + 8]) canpkt.time = get_last_packet_timestamp(self.ins) if self.remove_padding: return canpkt else: return canpkt / Padding(pkt[len + 8:])