Ejemplo n.º 1
0
def _gw_test1(intfs, calibration, end_validation, sync, gctx_op, gctx_data,
              gdata, gcanid):
    print(
        "_monitor_process: TEST #1 : CAN ID FILTERING (UDS DiagnosticSessionControl) START"
    )
    for int_send, _ in intfs.items():

        # Ignore interfaces without calibration
        if not int_send in calibration:
            print("Warning, no calibration rule for interface " + int_send)
            continue

        # Init global context
        for interface, _ in gctx_op.items():
            if interface == int_send:
                gctx_op[interface] = True
            else:
                gctx_op[interface] = False

        print("_monitor_process: TEST #1 running : {}".format(int_send))
        for canid in range(canid_range):
            if end_validation.value:  # exit test
                return

            gctx_data[int_send] = [canid, 0x02, 0x10, 0x01]
            gdata[0] = [0x02, 0x10, 0x01]
            # Wait for synch for updated context, start operation
            sync.wait()

            # Wait for synch for end operation
            sync.wait()

            # Add canid to gcanid for filtering payload test (to save canid not filtered)
            for int_receive, _ in gctx_op.items():
                if int_receive != int_send:
                    if len(gctx_data[int_receive]) > 0 and not (
                            int_receive in gcanid[int_send].keys()):
                        if cal.calibration_matches(calibration, int_send,
                                                   int_receive, canid,
                                                   gdata[0]):
                            gcanid[int_send][int_receive] = canid
                            print(
                                "_monitor_process: TEST #1  add gcanid[%s][%s] = %02x"
                                % (int_send, int_receive, canid))

            # Analyze the result
            _analyze_result(int_send, gctx_op, gctx_data)

    print(
        "_monitor_process: TEST #1 : CAN ID FILTERING (UDS DiagnosticSessionControl) END"
    )
Ejemplo n.º 2
0
def _analyze_result(intf_in, gctx_op, gctx_data):
    for k2, _ in gctx_op.items():
        # For all read interfaces
        if k2 != intf_in:
            # If something has been received
            if len(gctx_data[k2]) > 0:
                if cal.calibration_matches(calibration, intf_in, k2,
                                           gctx_data[k2][0],
                                           gctx_data[k2][1:]):
                    print("{} R: {} -> {} [0x{:03x} - 0x{}]".format(
                        colored('[OK]', 'green'), intf_in, k2,
                        gctx_data[k2][0],
                        codecs.encode(bytearray(gctx_data[k2][1:]), 'hex')))

                else:
                    print("{} R: {} -> {} [0x{:03x} - 0x{}]".format(
                        colored('[ERROR]', 'red'), intf_in, k2,
                        gctx_data[k2][0],
                        codecs.encode(bytearray(gctx_data[k2][1:]), 'hex')))

                sys.stdout.flush()
                gctx_data[k2] = []
    gctx_data[intf_in] = []
def _canread_process(intf, intfs, calibration, end_process):
    intf_phy = intf
    intf_virt = intfs[intf]['channel']

    print("canread_process: START [physical={} virtual={}]".format(
        intf_phy, intf_virt))

    # Create the read context
    try:
        ctx_read = context.create_ctx(channel=intf_virt,
                                      bustype=BusType.SOCKETCAN,
                                      bitrate=intfs[intf]['bitrate'],
                                      canid_recv=0,
                                      canid_send=0,
                                      timeout=2,
                                      trace=0)
    except:
        print("Error creating context on interface {}".format(intf_virt))
        return

    while not end_process.value:
        ret, msg = vcan.read(ctx_read)
        if msg is not None:
            canid = msg.arbitration_id

            if len(msg.data) == 0:
                print("R: {} [0x{:03x} - empty]".format(
                    intf_phy, msg.arbitration_id))
                continue

            # Check if GW write the same message on the same interface recently
            found = False
            for e in write_cache:
                # Clean very old cache
                try:
                    if e['time'] > msg.timestamp + CACHE_TIMEOUT:
                        write_cache.remove(e)
                    elif e['msg']['intf'] == intf_phy and \
                         e['msg']['canid'] == msg.arbitration_id and \
                         e['msg']['data'] == msg.data:
                        found = True
                        write_cache.remove(e)
                        break
                except ValueError:
                    break

            if found:
                if verbose:
                    print("R: {} [0x{:03x} - 0x{}] (from GW)".format(
                        intf_phy, msg.arbitration_id,
                        codecs.encode(msg.data, 'hex')))
                continue

            print("R: {} [0x{:03x} - 0x{}]".format(
                intf_phy, msg.arbitration_id, codecs.encode(msg.data, 'hex')))
            # for each output interface
            for k, v in intfs.items():

                # Ignore the same interface
                if k == intf_phy:
                    continue

                # Check rules
                if cal.calibration_matches(calibration, intf_phy, k,
                                           msg.arbitration_id, list(msg.data)):
                    out = "    F: {} -> {} [0x{:03x} - 0x{}]".format(
                        intf_phy, k, msg.arbitration_id,
                        codecs.encode(msg.data, 'hex'))
                    print(colored(out, 'green'))
                    forward_queue.put({
                        'intf': k,
                        'canid': msg.arbitration_id,
                        'data': msg.data
                    })
 def test_10(self):
     self.assertEqual(
         False,
         cal.calibration_matches(calibration_data, "dlc", "pt", 0xA0,
                                 [0x10, 0x10, 0xC0]))
Ejemplo n.º 5
0
if __name__ == "__main__":

    # Load the input data file
    if len(sys.argv) != 2:
        print("missing data input parameters (JSON files)")
        print("usage : {} cal.json".format(sys.argv[0]))
        print("    cal.json is the gateway calibration profil in JSON format")
        sys.exit(1)

    # Load the calibration file and the mapping interfaces
    with open(sys.argv[1]) as f:
        calibration = json.load(f)

    while True:
        rx = input("RX interface (input CAN interface) ? ")
        tx = input("TX interface (output CAN interface) ? ")

        canid = input("CAN ID (hexadecimal value without 0x) ? ")
        canid = int(canid, 16)

        payload = input("payload (hexadecimal value without 0x) ? ")
        payload = [
            int(payload[i:i + 2], 16) for i in range(0, len(payload), 2)
        ]

        if cal.calibration_matches(calibration, rx, tx, canid, payload):
            print("-> OK")
        else:
            print("-> NOK")
        print()
 def test_07(self):
     self.assertEqual(
         False,
         cal.calibration_matches(calibration_data, "dlc", "ext", 0x704,
                                 [0x02, 0x10, 0x01]))
 def test_08(self):
     self.assertEqual(
         True,
         cal.calibration_matches(calibration_data, "dlc", "pt", 0xA0,
                                 [0x02, 0x10, 0xC0]))
 def test_06(self):
     self.assertEqual(
         False,
         cal.calibration_matches(calibration_data, "chassis", "v1", 0x704,
                                 [0x02, 0x10, 0x01]))
 def test_05(self):
     self.assertEqual(
         True,
         cal.calibration_matches(calibration_data, "chassis", "dlc", 0x704,
                                 [0x02, 0x10, 0x01]))
 def test_03(self):
     self.assertEqual(
         False,
         cal.calibration_matches(calibration_data, "dlc", "v1", 0x020,
                                 [0x02, 0x10, 0x01]))
 def test_02(self):
     self.assertEqual(
         False,
         cal.calibration_matches(calibration_data, "ext", "v2", 0x020,
                                 [0x02, 0x10, 0x01]))
 def test_01(self):
     self.assertEqual(
         True,
         cal.calibration_matches(calibration_data, "ext", "v1", 0x020,
                                 [0x02, 0x10, 0x01]))