Beispiel #1
0
def test_set_title():
    record = ndef.SmartposterRecord()
    assert record.titles == {}
    record.set_title("English Text", )
    record.set_title("German Text", "de")
    assert record.titles == {'en': 'English Text', 'de': 'German Text'}
    record.set_title("Deutscher Text", "de")
    assert record.titles == {'en': 'English Text', 'de': 'Deutscher Text'}
def main():
    # default to debug to catch unexpected exceptions during setup
    debug = True

    # Getting the arguments
    try:
        parser = ArgumentParser()
        parser.add_argument(
            '-d', '--debug',
            dest='debug',
            default=False,
            action='store_true',
            help='debug mode (be more verbose and print tracebacks)')
        parser.add_argument(
            '-u', '--url',
            dest='url',
            default=True,
            help='url')
        parser.add_argument(
            '-i', '--id',
            dest='uuid',
            default=True,
            help='uuid')
        parser.add_argument(
            '-t', '--title',
            dest='title',
            default=True,
            help='title')
        args = parser.parse_args()

        # Update debug
        debug = args.debug
        if debug == True:
            print args

        # Write data to the tag
        clf = nfc.ContactlessFrontend('usb:001:004')
        tag = clf.connect(rdwr={'on-connect': lambda tag: False, 'lto': 60000})
        beaconuid = str(tag.identifier).encode("hex")
        tag.ndef.records = [ndef.SmartposterRecord(args.url +"?uuid="+args.uuid, args.title)]

        # Return
        if tag.ndef.has_changed == False:
            return 0
        else:
            return 1


    except Exception as e:
        sys.stderr.write("An error occured: " + repr(e) + "\n")
        if debug:
            print_exc(e)
        return 2
Beispiel #3
0
def cmd(message, **kwargs):
    """The *smartposter* command creates an NFC Forum Smart Poster Record
    for the resource identifier. A smart poster record combines the
    uniform resource identifier with additional data such as titles
    and icons for representation and processing instructions for the
    reader application.

    A smart poster should have title text entries for the desired
    languages, added with repetitive '-t' options. An English title
    text may also be added with '-T'. The recommended action set with
    '-a' tells the reader application to either run the default action
    for the URI, save it for later or open for editing.

    A smart poster may also provide a collection of icons for
    graphical representation. An icon file is added with the '-i'
    option which may be used more than once. The icon type is
    determined from the file content and must be an 'image' or 'video'
    mime type.

    \b
    Examples:
      ndeftool smartposter http://nfcpy.org print
      ndeftool smartposter -T 'nfcpy project' http://nfcpy.org print
      ndeftool smartposter -t en 'nfcpy project' http://nfcpy.org print
      ndeftool smartposter -T 'EMERGENCY CALL 911' -a exec tel:911
      ndeftool smartposter -i nfcpy-logo-32x32.ico http://nfcpy.org

    """
    dmsg(__name__ + ' ' + str(kwargs))

    if message is None:
        message = []

    record = ndef.SmartposterRecord(kwargs['resource'])
    for lang, text in kwargs['titles']:
        record.set_title(text, lang)
    if kwargs['title']:
        record.set_title(kwargs['title'], 'en')
    if kwargs['action']:
        record.action = kwargs['action']
    for icon_file in kwargs['icons']:
        icon_data = icon_file.read()
        icon_type = magic.from_buffer(icon_data, mime=True)
        if icon_type.startswith('image/') or icon_type.startswith('video/'):
            record.add_icon(icon_type, icon_data)
        else:
            errmsg = "file %s is not a proper icon file" % icon_file.name
            raise click.ClickException(errmsg)

    message.append(record)
    return message
Beispiel #4
0
    def test_ndef_write(self, tag):
        tag.clf.exchange.side_effect = [
            HEX('1d 07 0102030405060708 0000 01') +
            HEX("10 02 02 00 03 00 00 00 00 00 01 00 00 00 00 18"),
        ]
        assert tag.ndef is not None
        assert tag.ndef.capacity == 48
        assert tag.ndef.length == 0
        assert tag.ndef.is_readable is True
        assert tag.ndef.is_writeable is True
        assert tag.ndef.octets == b''
        tag.clf.exchange.assert_called_with(
            HEX('10 06 0102030405060708 010b00 018000'), 0.3093504)

        tag.clf.exchange.reset_mock()
        tag.clf.exchange.side_effect = [
            HEX('1d 07 0102030405060708 0000 01') +
            HEX("10 02 02 00 03 00 00 00 00 00 01 00 00 00 00 18"),
            HEX('0c 09 0102030405060708 0000'),  # write block 0
            HEX('0c 09 0102030405060708 0000'),  # write block 1, 2
            HEX('0c 09 0102030405060708 0000'),  # write block 3
            HEX('0c 09 0102030405060708 0000'),  # write block 0
        ]
        records = [ndef.SmartposterRecord("http://nfc-forum.org", "NFC Forum")]
        tag.ndef.records = records
        print(tag.clf.exchange.mock_calls)
        tag.clf.exchange.assert_has_calls([
            mock.call(  # read attribute data
                HEX('10 06 0102030405060708 010b00 018000'), 0.3093504),
            mock.call(  # write attribute data (set WriteFlag)
                HEX('20 08 0102030405060708 010900 018000'
                    '1002020003000000000f010000000027'), 0.3093504),
            mock.call(  # write data blocks 1 and 2 (because Nbw is 2)
                HEX('32 08 0102030405060708 010900 0280018002'
                    'd10222537091010e55036e66632d666f'
                    '72756d2e6f726751010c5402656e4e46'), 0.46402560000000004),
            mock.call(  # write data block 3 (with zero padding)
                HEX('20 08 0102030405060708 010900 018003'
                    '4320466f72756d000000000000000000'), 0.3093504),
            mock.call(  # write attribute data (unset WriteFlag, set Ln)
                HEX('20 08 0102030405060708 010900 018000'
                    '1002020003000000000001000027003f'), 0.3093504),
        ])
Beispiel #5
0
        assert record.type_records == []
        assert record.icon_records == []


def test_set_title():
    record = ndef.SmartposterRecord()
    assert record.titles == {}
    record.set_title("English Text", )
    record.set_title("German Text", "de")
    assert record.titles == {'en': 'English Text', 'de': 'German Text'}
    record.set_title("Deutscher Text", "de")
    assert record.titles == {'en': 'English Text', 'de': 'Deutscher Text'}


smartposter_messages = [
    ('d102055370 d101015500', [ndef.SmartposterRecord('')]),
    ('d1020e5370 d1010a55036e666370792e6f7267',
     [ndef.SmartposterRecord('http://nfcpy.org')]),
    ('d1021a5370 91010a55036e666370792e6f7267 5101085402656e6e66637079',
     [ndef.SmartposterRecord('http://nfcpy.org', 'nfcpy')]),
    ('d102215370 91010a55036e666370792e6f7267 1101085402656e6e66637079'
     '51030161637400',
     [ndef.SmartposterRecord('http://nfcpy.org', 'nfcpy', 'exec')]),
]


@pytest.mark.parametrize("encoded, message", smartposter_messages + [
    ('d102085370 9101015500 500000', [ndef.SmartposterRecord('')]),
])
def test_message_decode(encoded, message):
    octets = bytes(bytearray.fromhex(encoded))
Beispiel #6
0
def run_send_link_action(args, llc):
    record = ndef.SmartposterRecord(args.uri)
    if args.title:
        record.set_title(args.title)
    send_message(args, llc, [record])
Beispiel #7
0
    print(clf)

    # ID=3700A639
    # default bitrate (106A, 106B, 212F)
    #target = clf.sense(RemoteTarget('106A'),RemoteTarget('106B'),RemoteTarget('212F'))
    #print(target)
    #tag = nfc.tag.activate(clf, target)


    def on_connect(tag):
        print(tag.type, tag.identifier.encode("hex").upper())
        print(tag)
        print("tag in on-connect: ", tag)
        print("printing records: ")
        for record in tag.ndef.records:
            print(record)
        return False

    clf.connect(rdwr={'on-connnect': on_connect})
    print("trying to write ndef record to tag:")
    uri, title = 'http://example.com', 'nfc testing'
    record = ndef.SmartposterRecord(uri, title)
    print(record)
    tag.records = [record]
    print(tag.ndef)
    if tag.ndef is not None:
        print(tag.has_changed)

finally:
    clf.close()
Beispiel #8
0
 def set_smartposter_record(self, data):
     record = ndef.SmartposterRecord(data)
     record.set_title('Home Security', 'en')
     record.resource_type = 'text/html'
     record.action = 'exec'
     return record