Class: Tinkerforge::BrickletNFCRFID
- 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::RESPONSE_EXPECTED_ALWAYS_FALSE, 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, #registered_callbacks, #uid
Instance Method Summary collapse
-
#authenticate_mifare_classic_page(page, key_number, key) ⇒ Object
Mifare Classic tags use authentication.
-
#get_identity ⇒ Object
Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.
-
#get_page ⇒ Object
Returns 16 bytes of data from an internal buffer.
-
#get_state ⇒ Object
Returns the current state of the NFC/RFID Bricklet.
-
#get_tag_id ⇒ Object
Returns the tag type, tag ID and the length of the tag ID (4 or 7 bytes are possible length).
-
#initialize(uid, ipcon) ⇒ BrickletNFCRFID
constructor
Creates an object with the unique device ID
uidand adds it to the IP Connectionipcon. -
#register_callback(id, &block) ⇒ Object
Registers a callback with ID
idto the blockblock. -
#request_page(page) ⇒ Object
Reads 16 bytes starting from the given page and stores them into a buffer.
-
#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.
-
#write_page(page, data) ⇒ Object
Writes 16 bytes starting from the given page.
Methods inherited from Device
#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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 54 def initialize(uid, ipcon) super uid, ipcon @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[CALLBACK_STATE_CHANGED] = RESPONSE_EXPECTED_ALWAYS_FALSE @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE @callback_formats[CALLBACK_STATE_CHANGED] = 'C ?' 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 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:
-
Call BrickletNFCRFID#request_tag_id
-
Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED)
-
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
-
Call BrickletNFCRFID#authenticate_mifare_classic_page with page and key for the page
-
Wait for state to change to AuthenticatingMifareClassicPageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED)
-
Call BrickletNFCRFID#request_page or BrickletNFCRFID#write_page to read/write page
155 156 157 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 155 def authenticate_mifare_classic_page(page, key_number, key) send_request(FUNCTION_AUTHENTICATE_MIFARE_CLASSIC_PAGE, [page, key_number, key], 'S C C6', 0, '') end |
#get_identity ⇒ Object
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’ or ‘d’.
The device identifier numbers can be found :ref:‘here <device_identifier>`. |device_identifier_constant|
224 225 226 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 224 def get_identity send_request(FUNCTION_GET_IDENTITY, [], '', 25, 'Z8 Z8 k C3 C3 S') end |
#get_page ⇒ Object
Returns 16 bytes of data from an internal buffer. To fill the buffer with specific pages you have to call BrickletNFCRFID#request_page beforehand.
212 213 214 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 212 def get_page send_request(FUNCTION_GET_PAGE, [], '', 16, 'C16') end |
#get_state ⇒ Object
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.
133 134 135 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 133 def get_state send_request(FUNCTION_GET_STATE, [], '', 2, 'C ?') end |
#get_tag_id ⇒ Object
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:
-
Call BrickletNFCRFID#request_tag_id
-
Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED)
-
Call BrickletNFCRFID#get_tag_id
115 116 117 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 115 def get_tag_id send_request(FUNCTION_GET_TAG_ID, [], '', 9, 'C C C7') end |
#register_callback(id, &block) ⇒ Object
Registers a callback with ID id to the block block.
229 230 231 232 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 229 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:
-
Call BrickletNFCRFID#request_tag_id
-
Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED)
-
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
-
Call BrickletNFCRFID#request_page with page number
-
Wait for state to change to RequestPageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED)
-
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.
206 207 208 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 206 def request_page(page) send_request(FUNCTION_REQUEST_PAGE, [page], 'S', 0, '') 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.
Current 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.
100 101 102 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 100 def request_tag_id(tag_type) send_request(FUNCTION_REQUEST_TAG_ID, [tag_type], 'C', 0, '') 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:
-
Call BrickletNFCRFID#request_tag_id
-
Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED)
-
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
-
Call BrickletNFCRFID#write_page with page number and data
-
Wait for state to change to WritePageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED)
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.
179 180 181 |
# File 'lib/tinkerforge/bricklet_nfc_rfid.rb', line 179 def write_page(page, data) send_request(FUNCTION_WRITE_PAGE, [page, data], 'S C16', 0, '') end |