Exemplo n.º 1
0
    def _generate_basic_scenario_data(self):
        # can_id = 0x0c1, ide = 0, rtr = 0, dlc = 8, data = [0x93, 0xFD, 0x07, 0xD0, 0x90, 0x2C, 0xDF, 0x40])
        self._can_frames_list[:] = []

        # Generate standard and extended data frames with different length of data
        for is_extended in [False, True]:
            for dlc in range(9):
                # Generate random frame id and data
                can_id = self._create_random_can_id(is_extended)
                can_data = self._create_random_can_data(dlc)
                self._can_frames_list.append(
                    globals.canBusFrame(can_id, dlc, can_data))

        # Generate extended data frame with CAN ID less than 11 bits.
        can_id = self._create_random_can_id(False)
        can_data = self._create_random_can_data(8)
        self._can_frames_list.append(
            globals.canBusFrame(can_id,
                                8,
                                can_data,
                                flags=globals.CAN_ID_IDE_FLAG))

        # Generate Remote frames
        can_id = self._create_random_can_id(False)
        self._can_frames_list.append(
            globals.canBusFrame(can_id,
                                8, [0] * 8,
                                flags=globals.CAN_ID_RTR_FLAG))

        can_id = self._create_random_can_id(True)
        self._can_frames_list.append(
            globals.canBusFrame(can_id,
                                0, [],
                                flags=(globals.CAN_ID_RTR_FLAG
                                       | globals.CAN_ID_IDE_FLAG)))
Exemplo n.º 2
0
    def receive(self, interface):
        """
        receive - 
        receive CAN frame
        """
        self.check_interface_range(interface)
        if self.port[interface] is None:
            raise Exception(
                "Port number {0} is not configured".format(interface))

        info = komodo_drv.km_can_info_t()
        komodo_pkt = komodo_drv.km_can_packet_t()
        data_in = array('B', '\0' * 8)
        komodo_rc = 0
        rc = 1
        self.lock.acquire()
        komodo_rc, info, komodo_pkt, data_in = komodo_drv.km_can_read(
            self.port[interface], data_in)
        self.lock.release()
        if komodo_rc:
            # pkt.can_id = komodo_pkt.id
            # pkt.dlc = komodo_pkt.dlc
            # pkt.data = data_in
            # flags = (pkt.extend_addr << EXTENDED_CAN_ID_LEN) | (pkt.remote_req << CAN_ID_RTR_BIT)
            # pkt.is_extended = pkt.extend_addr
            # pkt.rtr_f =  pkt.remote_req
            # rc = 0
            return globals.canBusFrame(
                komodo_pkt.id, komodo_pkt.dlc, data_in,
                (komodo_pkt.extend_addr << globals.EXTENDED_CAN_ID_LEN) |
                (komodo_pkt.remote_req << globals.CAN_ID_RTR_BIT))

        return str(rc)
Exemplo n.º 3
0
    def _load_can_data_file(self, file_name):
        """ This is the example of canbus data file 
            can_id = 0x0c1, ide = 0, rtr = 0, dlc = 8, data = [0x93, 0xFD, 0x07, 0xD0, 0x90, 0x2C, 0xDF, 0x40]
            can_id = 0x0ffff0c5, ide = 1, rtr = 0, dlc = 8, data = [0x93, 0xB4, 0x1E, 0xE0, 0x53, 0xEC, 0xF0, 0x3C]
        """
        flags = 0
        if not os.path.isfile(file_name):
            self._generate_can_data_file(file_name, self._frames_num)
            return

        self._can_frames_list[:] = []

        file_hwd = open(file_name, "r")
        for line in file_hwd:
            # split to 5 blocks
            base = line.split(',', 4)

            # extract can_id messages value
            can_id = int(base[0].split('=')[1].strip(), 16)

            # Extract extended flag value (=ide)
            ide = bool(int(base[1].split('=')[1].strip()))
            if ide: flags = flags | globals.CAN_ID_IDE_FLAG

            # Extract remote flag value (=rtr)
            rtr = bool(int(base[2].split('=')[1].strip()))
            if rtr: flags = flags | globals.CAN_ID_RTR_FLAG

            # extract can data length (=dlc)
            dlc = int(base[3].split('=')[1].strip())

            # Create regular expression
            regex = re.compile("0x[0-9,A-F,a-f][0-9,A-F,a-f]")
            data = regex.findall(base[4])
            can_data = [int(x, 16) for x in data]

            # Remove erroneous frames - TBD
            if dlc != len(can_data):
                continue

            if flags == 0: flags = None

            self._can_frames_list.append(
                globals.canBusFrame(can_id, dlc, can_data, flags=flags))

        file_hwd.close()
Exemplo n.º 4
0
    def _generate_can_data_file(self, file_name, frames_num):
        # Save CAN data frames to a file in the following pattern
        # can_id = 0x0c1, ide = 0, rtr = 0, dlc = 8, data = [0x93, 0xFD, 0x07, 0xD0, 0x90, 0x2C, 0xDF, 0x40])

        self._can_frames_list[:] = []
        frm_cnt = 0

        while len(self._can_frames_list) < frames_num:
            # Generate random can_id and data with different length of data
            for is_extended in [False, True]:
                for dlc in range(0, 9):
                    can_id = self._create_random_can_id(is_extended)

                    # Change one extended can_id to be less than 11 bits.
                    if is_extended and dlc == 4:
                        can_id = self._create_random_can_id(False)

                    can_data = self._create_random_can_data(dlc)
                    for idx in range(0, 10):
                        flags = None
                        if len(self._can_frames_list) >= frames_num:
                            break

                        self._can_frames_list.append(
                            globals.canBusFrame(can_id,
                                                dlc,
                                                can_data,
                                                flags=globals.CAN_ID_IDE_FLAG
                                                if is_extended else None))

                    ## Add Remote frame - TBD
                    #self._can_frames_list.append(CanFrameAT((can_id, is_extended, True), 0, []))

        file_hwd = open(file_name, "w")
        for frame in self._can_frames_list:
            file_hwd.write(str(frame) + '\n')

        file_hwd.close()
Exemplo n.º 5
0
    def receive(self, port=None):

        event_count = ctypes.c_uint(1)
        #print phandle, event_count
        event_list = canlib_xl.XLevent()

        self.lock.acquire()
        ok = self.driver.receive(self.phandle, event_count, event_list)
        self.lock.release()

        if ok:
            rec_string = self.driver.get_error_string(ok)
        else:
            rec_string = self.driver.get_event_string(event_list)

            dlc = event_list.tagData.msg.dlc
            data = []
            for i in range(0, dlc):
                data.append(event_list.tagData.msg.data[i])

            flags = 0
            # Verify Remote
            if event_list.tagData.msg.flags & canlib_xl.XL_CAN_MSG_FLAG_REMOTE_FRAME:
                flags = flags | globals.CAN_ID_RTR_FLAG

            # Verify Extednded flags
            if (event_list.tagData.msg.id & globals.CAN_ID_IDE_FLAG):
                flags = flags | globals.CAN_ID_IDE_FLAG

            # initileize for default
            if flags == 0:
                flags = None

            return globals.canBusFrame(event_list.tagData.msg.id, dlc, data,
                                       flags)

        return rec_string
Exemplo n.º 6
0
    def test_can_device():

        can = vectorCanDevice()

        can.open_device([1, 2])

        can.channel_open(1)
        can.channel_open(2)

        can_frame = globals.canBusFrame(0x402,
                                        8, [0] * 8,
                                        flags=globals.CAN_ID_RTR_FLAG)

        i = 0
        while 1:

            can.transmit(1, can_frame)
            ddd = can.receive()
            if type(ddd) is globals.canBusFrame:
                i += 1
                print "Recevied frame {}, can_id {}, dlc {}".format(
                    i, ddd.can_id, ddd.dlc)

        # can_frame = globals.canBusFrame( 0x1C01, 8 , [0]*8 )
        # can.transmit(2  , can_frame )
        rate = 500
        rate_fps_ms = 500 / (60.0 * 1000.0)
        """
        try:
            ddd = can.receive()
        except Exception as e:
            pass
        """

        frames = 100000
        for i in xrange(frames):

            start = time.time()
            if i % 2 == 0:
                can_frame = globals.canBusFrame(0x402,
                                                8, [0] * 8,
                                                flags=globals.CAN_ID_RTR_FLAG)
            else:
                can_frame = globals.canBusFrame(
                    0x401,
                    8, [0] * 8,
                    flags=(globals.CAN_ID_RTR_FLAG | globals.CAN_ID_IDE_FLAG))

            can.transmit(1, can_frame)

            sleep_time = rate_fps_ms - (time.time() - start)
            time.sleep(sleep_time if sleep_time > 0 else 0)

        print "Total frames {}, time {}".format(frames, time.time() - start)

        try:

            can.transmit(2, can_frame)
            time.sleep(0.5)
            ddd = can.receive()

        except Exception as e:
            raise e
        finally:
            can.channel_close(1)
            can.channel_close(2)
            can.device_close()
Exemplo n.º 7
0
    def main(self):
        try:
            self._errors = []

            # Generate CAN ID more than 11 bits and extended format bit off
            can_id = self._create_random_can_id(True)
            can_data = self._create_random_can_data(8)

            can_frame = globals.canBusFrame(can_id, 8, can_data)
            can_frame.ide_f = False
            can_frame.can_id_and_flags = can_id & globals.STANDARD_CAN_ID_MASK
            expected_frame = globals.canBusFrame(
                can_id & globals.STANDARD_CAN_ID_MASK, 8, can_data)
            self._test_can_frame(can_frame, None, expected_frame, self.can_cli,
                                 self.uut_can_if[0].sim_port)

            # Generate Data frame with data not empty and remote bit on
            can_id = self._create_random_can_id(False)
            can_data = self._create_random_can_data(8)

            can_frame = globals.canBusFrame(can_id,
                                            8,
                                            can_data,
                                            flags=globals.CAN_ID_RTR_FLAG)
            expected_frame = globals.canBusFrame(can_id,
                                                 8, [],
                                                 flags=globals.CAN_ID_RTR_FLAG)
            self._test_can_frame(can_frame, expected_frame, expected_frame,
                                 self.can_cli, self.uut_can_if[0].sim_port)

            # Generate frame with data length code and actual data size not equal
            can_id = self._create_random_can_id(False)
            can_data = self._create_random_can_data(8)
            can_frame = globals.canBusFrame(can_id, 4, can_data)
            expected_frame = globals.canBusFrame(can_id, 4, can_data[0:4])

            self._test_can_frame(can_frame, expected_frame, expected_frame,
                                 self.can_cli, self.uut_can_if[0].sim_port)

            can_id = self._create_random_can_id(False)
            can_data = self._create_random_can_data(4)
            can_frame = globals.canBusFrame(can_id, 8, can_data)
            komodo_expected_frame = globals.canBusFrame(
                can_id, 4, can_data[0:4])
            ################ TO CHECK ###############
            self._test_can_frame(can_frame, can_frame, komodo_expected_frame,
                                 self.can_cli, self.uut_can_if[0].sim_port)

            # This Test is useless in vector mode, since vector is not allowing this status.
            # Generate Data frame with data length code < 0
            # can_id = self._create_random_can_id(False)
            # can_frame = globals.canBusFrame( can_id, -2, [] )
            # komodo_expected_frame = globals.canBusFrame( can_id, 0, [])
            # self._test_can_frame(can_frame, None, komodo_expected_frame, self.can_cli, self.uut_can_if[0].sim_port)

            # Generate Data frame with data and data length code > 8
            can_id = self._create_random_can_id(False)
            can_data = self._create_random_can_data(10)
            can_frame = globals.canBusFrame(can_id, 10, can_data)
            expected_frame = globals.canBusFrame(can_id, 8, can_data[0:8])
            self._test_can_frame(can_frame, None, expected_frame, self.can_cli,
                                 self.uut_can_if[0].sim_port)

            if len(self._errors):
                log.debug("TC_CAN_API - Test errors :")
                for error in self._errors:
                    log.debug(error)

        except Exception as e:
            raise e
Exemplo n.º 8
0
    def _test_can_frame(self, can_frame, expected_frame1, expected_frame2, cli,
                        sim_port):

        self._frames_num += 1
        sim_rx_frame = None

        # CAN CLI transmit
        data_str = "".join(("%0.2x" % x) for x in can_frame.data)
        log.info("TC_CAN_API: Can tx frame - {}".format(can_frame))
        ret = cli.can.transmit(can_id=can_frame.can_id_and_flags,
                               can_data=data_str,
                               data_size=can_frame.dlc)

        if "ERROR : can_send:" in ret:
            log.debug("TC_CAN_API: Can tx frame received error")
            sim_rx_frame = globals.canBusFrame(0, 0, [])

        else:
            # CAN simulator receive
            try:
                # sim_rx_frame = self.can_sim.get_frame(sim_port, 10)
                sim_rx_frame = self.can_sim.receive(sim_port)
            except Exception as e:
                log.error("TC_CAN_API: Simulator rx failed, {}".format(e))

            self.stats.can_sim_rx[sim_port] += 1

            if ((sim_rx_frame is None) or (isinstance(sim_rx_frame, str))):
                # sim_rx_frame = CanFrameAT((sim_rx_frame["can_id"], sim_rx_frame["ide_f"], sim_rx_frame["rtr_f"]), sim_rx_frame["dlc"], sim_rx_frame["data"])
                sim_rx_frame = globals.canBusFrame(0, 0, [])

            log.debug(
                "TC_CAN_API: Simulator received - {}".format(sim_rx_frame))

        try:
            is_equal = (expected_frame1 == sim_rx_frame)
        except Exception as e:
            is_equal = False

        log.info("TC_CAN_API: canbus tx == simulator rx - {}".format(is_equal))
        if not is_equal:
            log.error(
                'Failed can cli tx --> simulator rx: id={0} dlc={1}'.format(
                    sim_rx_frame.can_id, sim_rx_frame.dlc))
            self.stats.failed_can_cli2sim[sim_port] += 1

        # CAN Simulator transmit
        #self.can_sim.send_frame(sim_port, can_frame)
        self.can_sim.transmit(sim_port, can_frame)

        log.info("Can Simulator transmit frame : {}".format(can_frame))
        self.stats.can_sim_tx[sim_port] += 1

        # CAN CLI receive
        rx_id, rx_dlc, rx_data = cli.can.receive(frames=1, print_frame=1)
        can_frame_rx = globals.canBusFrame(rx_id, rx_dlc, rx_data)
        #CanFrameAT(rx_id, rx_dlc, rx_data)
        log.debug("TC_CAN_API: Canbus received - {}".format(can_frame_rx))

        is_equal = (expected_frame2 == can_frame_rx)

        log.info("TC_CAN_API: simulator tx == canbus rx - {}".format(is_equal))
        if not is_equal:
            log.error(
                'Failed simulator tx --> can cli rx: id={0} dlc={1}'.format(
                    can_frame_rx.can_id, can_frame_rx.dlc))
            self.stats.failed_can_sim2cli[sim_port] += 1