Exemplo n.º 1
0
 def _card_connect(self, options, terminate):
     timeout = options.get('timeout', 1.0)
     for target in options.get('targets'):
         activated = self.listen(target, timeout)
         if activated:
             target, command = activated
             log.debug("activated as target {0}".format(target))
             tag = nfc.tag.emulate(self, target)
             if tag is not None:
                 log.debug("connected as {0}".format(tag))
                 callback = options['on-connect']
                 if callback and callback(tag, command):
                     while not terminate():
                         response = (tag.process_command(command)
                                     if command is not None else None)
                         try:
                             command = tag.send_response(response, timeout=1)
                         except nfc.clf.TimeoutError:
                             command = None
                         except nfc.clf.DigitalProtocolError as error:
                             log.error(error)
                             break
                         else:
                             if command is None: break
                     callback = options.get('on-release', lambda tag: True)
                     return callback(tag=tag)
                 else:
                     return tag
Exemplo n.º 2
0
    def test_read_service_check_block_data(self, tag, bn, be):
        def read(block_number, rb, re):
            assert rb is True and re is True
            return BLOCK_DATA(block_number % 256)

        tag.add_service(0x000B, read, lambda: False)

        cmd_fmt = '{:02x} 06 02fe010203040506 010b00 01 {:s}'
        cmd = HEX(cmd_fmt.format(14 + len(HEX(be)), be))
        rsp = HEX('1d 07 02fe010203040506 0000 01') + BLOCK_DATA(bn)
        assert tag.process_command(cmd) == rsp
Exemplo n.º 3
0
    def test_write_service_check_block_data(self, tag, bn, be):
        def write(block_number, block_data, wb, we):
            assert block_data == BLOCK_DATA(block_number % 256)
            assert wb is True and we is True
            return True

        tag.add_service(0x0009, lambda: False, write)

        cmd = '{:02x} 08 02fe010203040506 010900 01 {:s}'
        cmd = HEX(cmd.format(14 + len(HEX(be)) + 16, be)) + BLOCK_DATA(bn)
        assert tag.process_command(cmd) == HEX('0C 09 02fe010203040506 0000')
Exemplo n.º 4
0
    def test_write_service_check_block_data(self, tag, bn, be):
        def write(block_number, block_data, wb, we):
            assert block_data == BLOCK_DATA(block_number % 256)
            assert wb is True and we is True
            return True

        tag.add_service(0x0009, lambda: False, write)

        cmd = '{:02x} 08 02fe010203040506 010900 01 {:s}'
        cmd = HEX(cmd.format(14 + len(HEX(be)) + 16, be)) + BLOCK_DATA(bn)
        assert tag.process_command(cmd) == HEX('0C 09 02fe010203040506 0000')
Exemplo n.º 5
0
    def test_read_service_check_block_data(self, tag, bn, be):
        def read(block_number, rb, re):
            assert rb is True and re is True
            return BLOCK_DATA(block_number % 256)

        tag.add_service(0x000B, read, lambda: False)

        cmd_fmt = '{:02x} 06 02fe010203040506 010b00 01 {:s}'
        cmd = HEX(cmd_fmt.format(14 + len(HEX(be)), be))
        rsp = HEX('1d 07 02fe010203040506 0000 01') + BLOCK_DATA(bn)
        assert tag.process_command(cmd) == rsp
Exemplo n.º 6
0
    def test_read_service_multiple_blocks(self, tag, nob, bel):
        def read(block_number, rb, re):
            assert rb is (True if block_number == 0 else False)
            assert re is (True if block_number == nob-1 else False)
            return BLOCK_DATA(0)

        tag.add_service(0x000B, read, lambda: False)

        cmd_fmt = '{:02x} 06 02fe010203040506 010b00 {:s}'
        rsp_fmt = '{:x}D  07 02fe010203040506 0000 {:02x} {:s}'
        cmd = HEX(cmd_fmt.format(13 + len(HEX(bel)), bel))
        rsp = HEX(rsp_fmt.format(nob, nob, nob * 16 * '00'))
        assert tag.process_command(cmd) == rsp
Exemplo n.º 7
0
    def test_read_service_multiple_blocks(self, tag, nob, bel):
        def read(block_number, rb, re):
            assert rb is (True if block_number == 0 else False)
            assert re is (True if block_number == nob - 1 else False)
            return BLOCK_DATA(0)

        tag.add_service(0x000B, read, lambda: False)

        cmd_fmt = '{:02x} 06 02fe010203040506 010b00 {:s}'
        rsp_fmt = '{:x}D  07 02fe010203040506 0000 {:02x} {:s}'
        cmd = HEX(cmd_fmt.format(13 + len(HEX(bel)), bel))
        rsp = HEX(rsp_fmt.format(nob, nob, nob * 16 * '00'))
        assert tag.process_command(cmd) == rsp
Exemplo n.º 8
0
    def test_write_service_multiple_blocks(self, tag, nob, bel):
        def write(block_number, block_data, wb, we):
            assert block_data == BLOCK_DATA(0)
            assert wb is (True if block_number == 0 else False)
            assert we is (True if block_number == nob-1 else False)
            return True

        tag.add_service(0x0009, lambda: False, write)

        cmd_fmt = '{:02x} 08 02fe010203040506 010900 {:s} {:s}'
        rsp_fmt = '{:02x} 09 02fe010203040506 0000'
        cmd = HEX(cmd_fmt.format(13+len(HEX(bel))+nob*16, bel, nob*16*'00'))
        rsp = HEX(rsp_fmt.format(12))
        assert tag.process_command(cmd) == rsp
Exemplo n.º 9
0
    def test_write_service_multiple_blocks(self, tag, nob, bel):
        def write(block_number, block_data, wb, we):
            assert block_data == BLOCK_DATA(0)
            assert wb is (True if block_number == 0 else False)
            assert we is (True if block_number == nob - 1 else False)
            return True

        tag.add_service(0x0009, lambda: False, write)

        cmd_fmt = '{:02x} 08 02fe010203040506 010900 {:s} {:s}'
        rsp_fmt = '{:02x} 09 02fe010203040506 0000'
        cmd = HEX(
            cmd_fmt.format(13 + len(HEX(bel)) + nob * 16, bel,
                           nob * 16 * '00'))
        rsp = HEX(rsp_fmt.format(12))
        assert tag.process_command(cmd) == rsp
Exemplo n.º 10
0
 def test_process_unknown_command(self, tag):
     assert tag.process_command(HEX('0A FF 02FE010203040506')) is None
Exemplo n.º 11
0
 def test_request_response(self, tag):
     rsp = tag.process_command(HEX('0A 04 02FE010203040506'))
     assert rsp == HEX('0B 05 02FE010203040506 00')
Exemplo n.º 12
0
 def test_request_response(self, tag):
     rsp = tag.process_command(HEX('0A 04 02FE010203040506'))
     assert rsp == HEX('0B 05 02FE010203040506 00')
Exemplo n.º 13
0
 def test_write_wrong_service_list_index(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('20 08 02fe010203040506 010b00 018100') + bytearray(16)
     rsp = HEX('0C 09 02fe010203040506 01A3')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 14
0
 def test_write_insufficient_block_data(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('1F 08 02fe010203040506 010b00 018000') + bytearray(15)
     rsp = HEX('0C 09 02fe010203040506 FFA2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 15
0
 def test_read_wrong_service_list_index(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('10 06 02fe010203040506 010b00 018100')
     rsp = HEX('0C 07 02fe010203040506 01A3')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 16
0
 def test_read_more_blocks_than_possible(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('2E 06 02fe010203040506 010b00 10' + 16 * '8000')
     rsp = HEX('0C 07 02fe010203040506 FFA2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 17
0
 def test_read_from_unknown_service(self, tag):
     cmd = HEX('10 06 02fe010203040506 010b00 018000')
     rsp = HEX('0C 07 02fe010203040506 FFA1')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 18
0
 def test_process_command_idm_error(self, tag):
     assert tag.process_command(HEX('0A 0C F2FE010203040506')) is None
Exemplo n.º 19
0
 def test_process_command_length_error(self, tag):
     assert tag.process_command(HEX('0B 0C 02FE010203040506')) is None
Exemplo n.º 20
0
 def test_process_unknown_command(self, tag):
     assert tag.process_command(HEX('0A FF 02FE010203040506')) is None
Exemplo n.º 21
0
 def test_request_system_code(self, tag):
     rsp = tag.process_command(HEX('0A 0C 02FE010203040506'))
     assert rsp == HEX('0D 0D 02FE010203040506 01 12FC')
Exemplo n.º 22
0
 def test_read_more_blocks_than_possible(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('2E 06 02fe010203040506 010b00 10' + 16 * '8000')
     rsp = HEX('0C 07 02fe010203040506 FFA2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 23
0
 def test_process_command_idm_error(self, tag):
     assert tag.process_command(HEX('0A 0C F2FE010203040506')) is None
Exemplo n.º 24
0
 def test_request_system_code(self, tag):
     rsp = tag.process_command(HEX('0A 0C 02FE010203040506'))
     assert rsp == HEX('0D 0D 02FE010203040506 01 12FC')
Exemplo n.º 25
0
 def test_read_from_non_existing_block(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('10 06 02fe010203040506 010b00 018000')
     rsp = HEX('0C 07 02fe010203040506 01A2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 26
0
 def test_process_command_length_error(self, tag):
     assert tag.process_command(HEX('0B 0C 02FE010203040506')) is None
Exemplo n.º 27
0
 def test_read_wrong_service_list_index(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('10 06 02fe010203040506 010b00 018100')
     rsp = HEX('0C 07 02fe010203040506 01A3')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 28
0
 def test_read_from_unknown_service(self, tag):
     cmd = HEX('10 06 02fe010203040506 010b00 018000')
     rsp = HEX('0C 07 02fe010203040506 FFA1')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 29
0
 def test_write_to_unknown_service(self, tag):
     cmd = HEX('20 08 02fe010203040506 010b00 018000') + bytearray(16)
     rsp = HEX('0C 09 02fe010203040506 FFA1')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 30
0
 def test_read_from_non_existing_block(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('10 06 02fe010203040506 010b00 018000')
     rsp = HEX('0C 07 02fe010203040506 01A2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 31
0
 def test_write_insufficient_block_data(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('1F 08 02fe010203040506 010b00 018000') + bytearray(15)
     rsp = HEX('0C 09 02fe010203040506 FFA2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 32
0
 def test_write_to_unknown_service(self, tag):
     cmd = HEX('20 08 02fe010203040506 010b00 018000') + bytearray(16)
     rsp = HEX('0C 09 02fe010203040506 FFA1')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 33
0
 def test_write_to_non_existing_block(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('20 08 02fe010203040506 010b00 018000') + bytearray(16)
     rsp = HEX('0C 09 02fe010203040506 01A2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 34
0
 def test_write_to_non_existing_block(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('20 08 02fe010203040506 010b00 018000') + bytearray(16)
     rsp = HEX('0C 09 02fe010203040506 01A2')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 35
0
 def test_write_wrong_service_list_index(self, tag):
     tag.add_service(0x000B, None, None)
     cmd = HEX('20 08 02fe010203040506 010b00 018100') + bytearray(16)
     rsp = HEX('0C 09 02fe010203040506 01A3')
     assert tag.process_command(cmd) == rsp
Exemplo n.º 36
0
 def test_polling(self, tag):
     rsp = tag.process_command(HEX('06 00 12FC0003'))
     assert rsp == HEX('12 01 02FE010203040506 FFFFFFFFFFFFFFFF')
     rsp = tag.process_command(HEX('06 0012FC0103'))
     assert rsp == HEX('14 01 02FE010203040506 FFFFFFFFFFFFFFFF 12FC')
Exemplo n.º 37
0
 def test_polling(self, tag):
     rsp = tag.process_command(HEX('06 00 12FC0003'))
     assert rsp == HEX('12 01 02FE010203040506 FFFFFFFFFFFFFFFF')
     rsp = tag.process_command(HEX('06 0012FC0103'))
     assert rsp == HEX('14 01 02FE010203040506 FFFFFFFFFFFFFFFF 12FC')