Ejemplo n.º 1
0
    def test_invalid_checksum(self):
        """readResponse rejects frame with invalid checksum"""
        pn532 = Pn532Hsu(1)
        pn532.begin()

        # Bad length checksum
        cmd = bytearray([1])
        bad_checksum = bytearray([0, 0, 255, 4, 252, 0xD5, 2, 70, 80, 140, 0])

        MOCK_UART.read_buf = PN532_ACK + bad_checksum
        pn532.writeCommand(header=cmd, body=bytearray())
        length, resp = pn532.readResponse()
        self.assertGreaterEqual(length, -3, "readResponse did not return Invalid Frame")
Ejemplo n.º 2
0
    def test_invalid_ack(self):
        """writeCommand waits for an ack"""
        pn532 = Pn532Hsu(1)
        pn532.begin()

        # correct ack
        MOCK_UART.read_buf = PN532_ACK
        ret = pn532.writeCommand(header=bytearray([2]), body=bytearray())
        self.assertEqual(0, ret, "writeCommand failed!")

        # invalid ack
        MOCK_UART.read_buf = [1, 128, 128]
        ret = pn532.writeCommand(header=bytearray([2]), body=bytearray())
        self.assertEqual(-1, ret, "writeCommand succeeded with invalid ack!")
Ejemplo n.º 3
0
    def test_writeCommand(self):
        """pn532hsu.writeCommand writes command frames correctly"""
        pn532 = Pn532Hsu(1)
        pn532.begin()

        frames = [  #  header, body, bytes written

            (bytearray([70, 80]), bytearray(), bytearray([0, 0, 255, 3, 253, 0xD4, 70, 80, 150, 0])),
            (bytearray([60]), bytearray([2, 3]), bytearray([0, 0, 255, 4, 252, 0xD4, 60, 2, 3, 235, 0])),
            (bytearray([50]), bytearray(), bytearray([0, 0, 255, 2, 254, 0xD4, 50, 250, 0])),
        ]
        for frame in frames:
            header, body, output = frame
            MOCK_UART.reset_mock()
            MOCK_UART.read_buf = PN532_ACK
            MOCK_UART.write_buf = bytearray()
            pn532.writeCommand(header=header, body=body)
            self.assertTrue(output in MOCK_UART.write_buf, 'Invalid data written')
Ejemplo n.º 4
0
    def test_readResponse(self):
        """readResponse correctly parses a response frame"""
        pn532 = Pn532Hsu(1)
        pn532.begin()

        frames = [ # cmd    resp data    resp frame
            (bytearray([1]), bytearray([70, 80]), bytearray([0, 0, 255, 4, 252, 0xD5, 2, 70, 80, 147, 0])),
            (bytearray([2]), bytearray([60]), bytearray([0, 0, 255, 3, 253, 0xD5, 3, 60, 236, 0]))
        ]
        for frame in frames:
            cmd, resp_data, resp_frame = frame
            MOCK_UART.reset_mock()
            MOCK_UART.read_buf = PN532_ACK + resp_frame
            pn532.writeCommand(header=cmd, body=bytearray())

            length, resp = pn532.readResponse()
            self.assertGreaterEqual(length, 0, "readResponse failed!")
            self.assertEqual(len(resp), length, "length did not match response length")
            self.assertEqual(resp_data, resp, "Incorrect response")
Ejemplo n.º 5
0
from pn532pi.interfaces.pn532hsu import Pn532Hsu

keypad = serial.Serial(
    port=
    '/dev/serial/by-id/usb-Silicon_Labs_CP2104_USB_to_UART_Bridge_Controller_00897CCA-if00-port0',
    baudrate=57600)
keypadString = ''  # used to build up strings from key pad
lastKeyTime = time.time()  # initialize global variable

accessfile = open('/home/pi/pn532pi/access.list', 'r')

logfile = open(
    '/tmp/' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S') + '.log', 'w')

PN532_HSU = Pn532Hsu(
    '/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0'
)
PN532_HSU._serial.setRTS(False)  # keep RTS pin high (3.3v) when program starts
PN532_HSU._serial.setDTR(False)  # keep DTR pin high (3.3v) when program starts
nfc = Pn532(PN532_HSU)

accesslist = {}


def loadaccessfile():
    for line in accessfile:
        if line.split(' ')[-1][0:2] == "b'":
            accesslist[line.split(' ')[-1].split('\'')[1]] = line.split(' ')[
                -3:-1]  # key is text of UID, store two things before it
        else:
            logwrite('skipping accessfile line: ' +
Ejemplo n.º 6
0
 def test_wakeup(self):
     """pn532hsu.wakeup writes some data to the spi port"""
     pn532 = Pn532Hsu(Pn532Hsu.RPI_MINI_UART)
     pn532.begin()
     pn532.wakeup()  #  check bytes written/transfered
     self.assertTrue(MOCK_UART._mock_write.called, "write transaction not called")
Ejemplo n.º 7
0
    def test_begin(self):
        """pn532hsu.begin initializes with the correct parameters"""
        pn532 = Pn532Hsu(Pn532Hsu.RPI_MINI_UART)
        pn532.begin()

        modules['serial'].Serial.assert_called_once_with('/dev/serial0', baudrate=115200, timeout=100)  # Mini UART