Class: Tinkerforge::BrickletNFCRFID

Inherits:
Device
  • Object
show all
Defined in:
lib/tinkerforge/bricklet_nfc_rfid.rb

Overview

Reads and writes NFC and RFID tags

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

246
DEVICE_DISPLAY_NAME =

:nodoc:

'NFC/RFID Bricklet'
CALLBACK_STATE_CHANGED =

This callback is called if the state of the NFC/RFID Bricklet changes. See BrickletNFCRFID#get_state for more information about the possible states.

8
FUNCTION_REQUEST_TAG_ID =

:nodoc:

1
FUNCTION_GET_TAG_ID =

:nodoc:

2
FUNCTION_GET_STATE =

:nodoc:

3
FUNCTION_AUTHENTICATE_MIFARE_CLASSIC_PAGE =

:nodoc:

4
FUNCTION_WRITE_PAGE =

:nodoc:

5
FUNCTION_REQUEST_PAGE =

:nodoc:

6
FUNCTION_GET_PAGE =

:nodoc:

7
FUNCTION_GET_IDENTITY =

:nodoc:

255
TAG_TYPE_MIFARE_CLASSIC =

:nodoc:

0
TAG_TYPE_TYPE1 =

:nodoc:

1
TAG_TYPE_TYPE2 =

:nodoc:

2
STATE_INITIALIZATION =

:nodoc:

0
STATE_IDLE =

:nodoc:

128
STATE_ERROR =

:nodoc:

192
STATE_REQUEST_TAG_ID =

:nodoc:

2
STATE_REQUEST_TAG_ID_READY =

:nodoc:

130
STATE_REQUEST_TAG_ID_ERROR =

:nodoc:

194
STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE =

:nodoc:

3
STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_READY =

:nodoc:

131
STATE_AUTHENTICATING_MIFARE_CLASSIC_PAGE_ERROR =

:nodoc:

195
STATE_WRITE_PAGE =

:nodoc:

4
STATE_WRITE_PAGE_READY =

:nodoc:

132
STATE_WRITE_PAGE_ERROR =

:nodoc:

196
STATE_REQUEST_PAGE =

:nodoc:

5
STATE_REQUEST_PAGE_READY =

:nodoc:

133
STATE_REQUEST_PAGE_ERROR =

:nodoc:

197
KEY_A =

:nodoc:

0
KEY_B =

:nodoc:

1

Constants inherited from Device

Device::DEVICE_IDENTIFIER_CHECK_MATCH, Device::DEVICE_IDENTIFIER_CHECK_MISMATCH, Device::DEVICE_IDENTIFIER_CHECK_PENDING, Device::RESPONSE_EXPECTED_ALWAYS_TRUE, Device::RESPONSE_EXPECTED_FALSE, Device::RESPONSE_EXPECTED_INVALID_FUNCTION_ID, Device::RESPONSE_EXPECTED_TRUE

Instance Attribute Summary

Attributes inherited from Device

#callback_formats, #expected_response_function_id, #expected_response_sequence_number, #high_level_callbacks, #registered_callbacks, #replaced, #uid

Instance Method Summary collapse

Methods inherited from Device

#check_validity, #dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletNFCRFID

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 56

def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_REQUEST_TAG_ID] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_TAG_ID] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_AUTHENTICATE_MIFARE_CLASSIC_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_REQUEST_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_PAGE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_STATE_CHANGED] = [10, 'C ?']

  @ipcon.add_device self
end

Instance Method Details

#authenticate_mifare_classic_page(page, key_number, key) ⇒ Object

Mifare Classic tags use authentication. If you want to read from or write to a Mifare Classic page you have to authenticate it beforehand. Each page can be authenticated with two keys: A (“key_number“ = 0) and B (“key_number“ = 1). A new Mifare Classic tag that has not yet been written to can be accessed with key A and the default key “[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]“.

The approach to read or write a Mifare Classic page is as follows:

  1. Call BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFCRFID#get_tag_id and check if the expected tag was found, if it was not found go back to step 1

  4. Call BrickletNFCRFID#authenticate_mifare_classic_page with page and key for the page

  5. Wait for state to change to AuthenticatingMifareClassicPageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  6. Call BrickletNFCRFID#request_page or BrickletNFCRFID#write_page to read/write page



164
165
166
167
168
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 164

def authenticate_mifare_classic_page(page, key_number, key)
  check_validity

  send_request FUNCTION_AUTHENTICATE_MIFARE_CLASSIC_PAGE, [page, key_number, key], 'S C C6', 8, ''
end

#get_identityObject

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ or ‘h’ (Bricklet Port). A Bricklet connected to an :ref:‘Isolator Bricklet <isolator_bricklet>` is always at position ’z’.

The device identifier numbers can be found :ref:‘here <device_identifier>`. |device_identifier_constant|



243
244
245
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 243

def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end

#get_pageObject

Returns 16 bytes of data from an internal buffer. To fill the buffer with specific pages you have to call BrickletNFCRFID#request_page beforehand.



227
228
229
230
231
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 227

def get_page
  check_validity

  send_request FUNCTION_GET_PAGE, [], '', 24, 'C16'
end

#get_stateObject

Returns the current state of the NFC/RFID Bricklet.

On startup the Bricklet will be in the Initialization state. The initialization will only take about 20ms. After that it changes to Idle.

The functions of this Bricklet can be called in the Idle state and all of the Ready and Error states.

Example: If you call BrickletNFCRFID#request_page, the state will change to RequestPage until the reading of the page is finished. Then it will change to either RequestPageReady if it worked or to RequestPageError if it didn’t. If the request worked you can get the page by calling BrickletNFCRFID#get_page.

The same approach is used analogously for the other API functions.



140
141
142
143
144
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 140

def get_state
  check_validity

  send_request FUNCTION_GET_STATE, [], '', 10, 'C ?'
end

#get_tag_idObject

Returns the tag type, tag ID and the length of the tag ID (4 or 7 bytes are possible length). This function can only be called if the NFC/RFID is currently in one of the Ready states. The returned ID is the ID that was saved through the last call of BrickletNFCRFID#request_tag_id.

To get the tag ID of a tag the approach is as follows:

  1. Call BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  3. Call BrickletNFCRFID#get_tag_id



120
121
122
123
124
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 120

def get_tag_id
  check_validity

  send_request FUNCTION_GET_TAG_ID, [], '', 17, 'C C C7'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



248
249
250
251
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 248

def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end

#request_page(page) ⇒ Object

Reads 16 bytes starting from the given page and stores them into a buffer. The buffer can then be read out with BrickletNFCRFID#get_page. How many pages are read depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte (one page is read)

  • NFC Forum Type 1 page size: 8 byte (two pages are read)

  • NFC Forum Type 2 page size: 4 byte (four pages are read)

The general approach for reading a tag is as follows:

  1. Call BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFCRFID#get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFCRFID#request_page with page number

  5. Wait for state to change to RequestPageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  6. Call BrickletNFCRFID#get_page to retrieve the page from the buffer

If you use a Mifare Classic tag you have to authenticate a page before you can read it. See BrickletNFCRFID#authenticate_mifare_classic_page.



219
220
221
222
223
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 219

def request_page(page)
  check_validity

  send_request FUNCTION_REQUEST_PAGE, [page], 'S', 8, ''
end

#request_tag_id(tag_type) ⇒ Object

To read or write a tag that is in proximity of the NFC/RFID Bricklet you first have to call this function with the expected tag type as parameter. It is no problem if you don’t know the tag type. You can cycle through the available tag types until the tag gives an answer to the request.

Currently the following tag types are supported:

  • Mifare Classic

  • NFC Forum Type 1

  • NFC Forum Type 2

After you call BrickletNFCRFID#request_tag_id the NFC/RFID Bricklet will try to read the tag ID from the tag. After this process is done the state will change. You can either register the CALLBACK_STATE_CHANGED callback or you can poll BrickletNFCRFID#get_state to find out about the state change.

If the state changes to RequestTagIDError it means that either there was no tag present or that the tag is of an incompatible type. If the state changes to RequestTagIDReady it means that a compatible tag was found and that the tag ID could be read out. You can now get the tag ID by calling BrickletNFCRFID#get_tag_id.

If two tags are in the proximity of the NFC/RFID Bricklet, this function will cycle through the tags. To select a specific tag you have to call BrickletNFCRFID#request_tag_id until the correct tag id is found.

In case of any Error state the selection is lost and you have to start again by calling BrickletNFCRFID#request_tag_id.



103
104
105
106
107
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 103

def request_tag_id(tag_type)
  check_validity

  send_request FUNCTION_REQUEST_TAG_ID, [tag_type], 'C', 8, ''
end

#write_page(page, data) ⇒ Object

Writes 16 bytes starting from the given page. How many pages are written depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte (one page is written)

  • NFC Forum Type 1 page size: 8 byte (two pages are written)

  • NFC Forum Type 2 page size: 4 byte (four pages are written)

The general approach for writing to a tag is as follows:

  1. Call BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFCRFID#get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFCRFID#write_page with page number and data

  5. Wait for state to change to WritePageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

If you use a Mifare Classic tag you have to authenticate a page before you can write to it. See BrickletNFCRFID#authenticate_mifare_classic_page.



190
191
192
193
194
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 190

def write_page(page, data)
  check_validity

  send_request FUNCTION_WRITE_PAGE, [page, data], 'S C16', 8, ''
end