Esempio n. 1
0
    def recv(self, timeout=None):
        log.debug("Trying to read a msg")

        if timeout is None or len(
                select.select([self.socket], [], [], timeout)[0]) > 0:
            packet = capturePacket(self.socket)
        else:
            # socket wasn't readable or timeout occurred
            return None

        arbitration_id = ArbitrationID()
        arbitration_id.can_id = packet['CAN ID']

        # Flags: EXT, RTR, ERR
        flags = (packet['CAN ID'] & MSK_FLAGS) >> 29

        rx_msg = SecureMessage(timestamp=packet['Timestamp'],
                               arbitration_id=arbitration_id,
                               data=packet['Data'][:-2],
                               MACs=packet['Data'][-2:])

        for dest in rx_msg.destinations:
            if dest not in self.nodes:
                self.nodes.append(Node(bus=self, address=dest))

        self.local_node.on_message_received(rx_msg)

        log.debug("Local node address at receiver: %x",
                  self.local_node.address)

        log.debug("arbID at receiver: %s", rx_msg.arbitration_id)

        log.debug("ID table entry: %s", self.local_node.id_table)

        return rx_msg
Esempio n. 2
0
 def testSend(self):
     bus = Bus(channel=can_interface, claimed_addresses=[0, 1])
     print "send addr: {0}".format(bus.local_node.address)
     arb = ArbitrationID(priority=5,
                         destination_addresses=[0],
                         source_address=bus.local_node.address)
     msg = SecureMessage(data=[0, 245, 134], arbitration_id=arb)
     bus.send(msg)
     bus.recv()
Esempio n. 3
0
class SecureBusTest(unittest.TestCase):

    # def testCreateBus(self):
    #     self.bus = secure.Bus(channel=can_interface)
    #     self.bus.shutdown()

    # def testArbitrationID(self):
    #     self.arbitration_id = ArbitrationID(priority=5, destination_addresses = [14, 6], source_address=20)
    #     #check that the number of destinations was correctly set to 2
    #     self.assertEqual(bin(self.arbitration_id.can_id)[11:13], "10")

    def testSend(self):
        bus = Bus(channel=can_interface, claimed_addresses=[0, 1])
        print "send addr: {0}".format(bus.local_node.address)
        arb = ArbitrationID(priority=5,
                            destination_addresses=[0],
                            source_address=bus.local_node.address)
        msg = SecureMessage(data=[0, 245, 134], arbitration_id=arb)
        bus.send(msg)
        bus.recv()

    def testRecv(self):
        bus = Bus(channel=can_interface)
        print "recv addr: {0}".format(bus.local_node.address)
        print bus.recv(timeout=2)

    # def testMACs(self):
    #     bus = Bus(channel=can_interface, claimed_addresses=[0,1])
    #     msg = SecureMessage(data=[0, 245, 134, 156])
    #     msg.destinations.append(1)
    #     bus.compute_MACs(msg)
    #     node = bus.get_node(1)
    #     node.on_message_received(msg)
    #     self.assertTrue(node.id_table.message_ids)

    def testMessage(self):
        self.msg = SecureMessage()
        another_msg = SecureMessage(arbitration_id=ArbitrationID(priority=7))
        self.assertTrue(
            self.msg.check_equality(another_msg, ["arbitration_id"]))
Esempio n. 4
0
class SecureBusTest(unittest.TestCase):

    # def testCreateBus(self):
    #     self.bus = secure.Bus(channel=can_interface)
    #     self.bus.shutdown()

    # def testArbitrationID(self):
    #     self.arbitration_id = ArbitrationID(priority=5, destination_addresses = [14, 6], source_address=20)
    #     #check that the number of destinations was correctly set to 2
    #     self.assertEqual(bin(self.arbitration_id.can_id)[11:13], "10")

    def testSend(self):
        bus = Bus(channel=can_interface, claimed_addresses=[0, 1])
        print "send addr: {0}".format(bus.local_node.address)
        arb = ArbitrationID(priority=5, destination_addresses = [0], source_address=bus.local_node.address)
        msg = SecureMessage(data=[0, 245, 134], arbitration_id=arb)
        bus.send(msg)
        bus.recv()

    def testRecv(self):
        bus = Bus(channel=can_interface)
        print "recv addr: {0}".format(bus.local_node.address)
        print bus.recv(timeout=2)


    # def testMACs(self):
    #     bus = Bus(channel=can_interface, claimed_addresses=[0,1])
    #     msg = SecureMessage(data=[0, 245, 134, 156])
    #     msg.destinations.append(1)
    #     bus.compute_MACs(msg)
    #     node = bus.get_node(1)
    #     node.on_message_received(msg)
    #     self.assertTrue(node.id_table.message_ids)

    def testMessage(self):
        self.msg = SecureMessage()
        another_msg = SecureMessage(arbitration_id=ArbitrationID(priority=7))
        self.assertTrue(self.msg.check_equality(another_msg, ["arbitration_id"]))
Esempio n. 5
0
 def testMessage(self):
     self.msg = SecureMessage()
     another_msg = SecureMessage(arbitration_id=ArbitrationID(priority=7))
     self.assertTrue(self.msg.check_equality(another_msg, ["arbitration_id"]))
Esempio n. 6
0
 def testMessage(self):
     self.msg = SecureMessage()
     another_msg = SecureMessage(arbitration_id=ArbitrationID(priority=7))
     self.assertTrue(
         self.msg.check_equality(another_msg, ["arbitration_id"]))
Esempio n. 7
0
"""

import can
import time
from can.protocols.secure.securemessage import SecureMessage
from can.protocols.secure.arbitrationid import ArbitrationID
from can.protocols.secure import Bus

interface = 'vcan0'

NO_OF_MSGS = 100
USEFUL_BITS = 40

bus = Bus(channel=interface, claimed_addresses=[0, 1])
arb = ArbitrationID(priority=5,
                    destination_addresses=[1],
                    source_address=bus.local_node.address)
sum = 0

for i in range(NO_OF_MSGS - 1, 0, -1):
    start_time = time.time()
    msg = SecureMessage(data=[i], arbitration_id=arb)
    bus.send(msg)
    timer = time.time() - start_time
    sum += timer

time_per_msg = sum / NO_OF_MSGS

print "latency: ", time_per_msg
print "throughput: ", USEFUL_BITS / time_per_msg