Chapter 2. NFC and RFID

Radio frequency identification (RFID) is becoming commonplace in everyday life these days. From tap-and-go payment cards and transit passes to E-ZPass devices used on toll roads to the tags stuck on and sewn into consumer goods to manage inventory and deter theft, most of us encounter RFID tags at least a few times a week and never think about what can be done with this technology.

In the past few years, a new term has started to bubble up in connection with RFID: near field communication (NFC). Ask your average techie what it is and you’ll probably hear “Oh, it’s like RFID, only different.” Great, but how is it different? RFID and NFC are often conflated, but they’re not the same thing. Though NFC readers can read from and write to some RFID tags, NFC has more capabilities than RFID, and enables a greater range of uses. You can think of NFC as an extension of RFID, building on a few of the many RFID standards to create a wider data exchange platform.

This book aims to introduce you to NFC and its capabilities in a hands-on way. Following the exercises in these chapters, you’ll build a few NFC applications for an NFC-enabled Android device and for an Arduino microcontroller. You’ll learn where RFID and NFC overlap, and what you can do with NFC.

What’s RFID?

Imagine you’re sitting on your porch at night. You turn on the porch light, and you can see your neighbor as he passes close to your house because the light reflects off him back to your eyes. That’s passive RFID. The radio signal from a passive RFID reader reaches a tag, the tag absorbs the energy and “reflects” back its identity.

Now imagine you turn on your porch light, and your neighbor in his home sees it and flicks on his porch light so that you can see him waving hello from his porch. That’s active RFID. It can carry a longer range, because the receiver has its own power source, and can therefore generate its own radio signal rather than relying on the energy it absorbs from the sender.

RFID is a lot like those two porches. You and your neighbor know each other’s faces, but you don’t really learn a lot about each other that way. You don’t exchange any meaningful messages. RFID is not a communications technology; rather, it’s designed for identification. RFID tags can hold a small amount of data, and you can read and write to them from RFID readers, but the amount of data we’re talking about is trivial, a thousand bytes or less.

What’s NFC?

Now imagine another neighbor passes close, and when you see her, you invite her on to the porch for a chat. She accepts your invitation, and you sit together, exchange pleasantries about your lives, and develop more of a relationship. You talk with each other and you listen to each other for a few minutes. That’s NFC.

NFC is designed to build on RFID by enabling more complex exchanges between participants. You can still read passive RFID tags with an NFC reader, and you can write to their limited amount of memory. NFC also allows you to write data to certain types of RFID tags using a standard format, independent of tag type. You can also communicate with other NFC devices in a two-way, or duplex, exchange. NFC devices can exchange information about each other’s capabilities, swap records, and initiate longer term communications through other means.

For example, you might tap your NFC-enabled phone to an NFC-enabled stereo so that they can identify each other, learn that they both have WiFi capability, and exchange credentials for communication over WiFi. After that, the phone will start to stream audio over WiFi to the stereo. Why doesn’t the phone stream its audio over the NFC connection? Two reasons: first, the NFC connection is intentionally short range, generally 10cm or less. That allows it to be low-power, and to avoid interference with other radios built into devices using it. Second, it’s relatively low-speed compared to WiFi, Bluetooth, and other communications protocols. NFC is not designed to manage extended high-speed communications. It’s for short messages, exchanging credentials, and initiating relationships. Think back to the front porch for a moment. NFC is the exchange you have to open the conversation. If you want to talk at length, you invite your neighbor inside for tea. That’s WiFi, Bluetooth, and other extended communications protocols.

What’s exciting about NFC is that it allows for some sophisticated introductions and short instructions without the hassle of exchanging passwords, pairing, and all the other more complicated steps that come with those other protocols. That means that when you and your friend want to exchange address information from your phone to his, you can just tap your phones together. When you want to pay with your Google Wallet, you can just tap as you would an RFID-enabled credit card.

When you’re using NFC, your device doesn’t give the other device to which it’s speaking access to its whole memory—it just gives it the basics needed for exchange. You control what it can send and what it can’t, and to whom.

How RFID Operates

An RFID exchange involves two actors: a target and an initiator. The initiator, a tag reader or reader/writer device, starts the exchange by generating a radio field and listening for responses from any target in the field. The target, a tag, responds when it picks up a transmission from an initiator. It will respond with a unique identifier number (UID). RFID has two communication modes: active and passive. Passive RFID exchanges involve a reader/writer and a tag that has no power source on board. The tags get their power from the energy of the radio field itself. It’s generally a very small amount, just enough to send a signal back to the reader. Active RFID exchanges involve a target that’s an independently powered device. Because the target is powered, its reply to the reader can travel a much greater distance. E-ZPass and other traffic ID systems use active RFID.

RFID tags have a small amount of memory on board, usually less than 1 kilobyte. An initiator device can read this data, and if it’s a reader/writer device, it can write to the tag as well. This allows you to store small amounts of information associated with the card. For example, it’s sometimes used in transit systems that use RFID, to keep track of how much value is left on the card. However, since RFID systems generally are networked to a database, it’s more common to store a data record indexed by the tag’s UID in a remote database, and store all information about the tag in that remote database.

RFID Standards

Contrary to popular belief, there is no single universally interoperable RFID protocol or technology. There are dozens. RFID standards are developed by the International Standards Organization (ISO), in conjunction with major participants in the RFID market. ISO works as a mediating body to help competitors in many different industries develop interoperable standards so that even when they compete, their technologies can sometimes work together. The various RFID standards define the radio frequencies used, the data transfer rates, the data formats, and more. Some of these standards define layers of a single interoperable stack, as you’ll see with NFC. Other standards define a whole different class of applications. For example, the ISO-11784 standard was originally developed for animal tracking. It operates in frequencies between 129 and 139.4kHz, and its data format features fields suited to describing the animals to be tracked. You can also find EM4100 procotol readers and tags that operate in the 125kHz range. These are often used as proximity cards, and feature very limited information in their data protocol, usually just a UID. The ISO-14443 standards were developed for use with payment systems and smart cards. They operate at 13.56MHz. They include features in their data format for incrementing and decrementing values and for encrypting data, for example. Within the 14443 family, there are several different formats including Philips and NXP Mifare tags, Sony FeliCa tags, and NXP DESFire. ISO-14443A tags are compatible with NFC, so you’ll see a lot of them in the pages that follow.

How NFC Operates

NFC can be thought of as an extension of RFID. NFC exchanges also involve an initiator and a target like RFID. However, it can do more than just exchange UIDs and read or write data to the target. The most interesting difference between RFID and NFC is that NFC targets are often programmable devices, like mobile phones. This means that rather than just delivering static data from memory, an NFC target could actually generate unique content for each exchange and deliver it back to the initiator. For example, if you’re using NFC to exchange address data between two phones, the NFC target device could be programmed to only provide limited information if it’s never seen this particular initiator before.

NFC devices have two communications modes. If the initiator always supplies the RF energy and the target gets powered by the initiator’s field, they’re said to be engaging in passive communication mode. If both target and initiator have their own energy sources, they’re in active communication mode. These modes are the same as regular RFID communication modes.

NFC devices have three operating modes. They can be reader/writers that read data from a target and write to it. They can be card emulators, acting like RFID tags when they’re in the field of another NFC or RFID device. Or they can operate in peer-to-peer mode, in which they exchange data in both directions.

NFC Data Exchange Format (NDEF)

Data exchanged between NFC devices and tags is formatted using the NFC Data Exchange Format (NDEF). This is a term you’ll hear a lot; NDEF is one of the key advancements that NFC adds to RFID. It’s a common data format that operates across all NFC devices, regardless of the underlying tag or device technology. Every NDEF message contains one or more NDEF records. Each record has a particular record type, a unique ID, a length, and a payload of data. There are a few well-known types of NDEF records. NFC-enabled devices are expected to know what to do with each of these types:

Simple text records
These contain whatever text string you want to send. Text messages generally don’t contain instructions for the target device. They also include metadata indicating the language and encoding scheme (e.g., UTF-8).
URIs
These contain network addresses. An NDEF target device that receives a URI record is expected to pass that record to an application that can display it, such as a web browser.
Smart Posters
These contain data you might attach to a poster to give it more information. This can include URIs, but might also contain other data, like a text message to be sent about the poster, telling your friends about it. A target device that receives a Smart Poster record might open a browser, SMS, or email application, depending on the message’s content.
Signatures
These provide a way to give trustworthy information about the origins of data contained in an NDEF record.

You can mix and match records in an NDEF message, or you can send only one record per message, as you choose.

NDEF is one of the important technical differences between RFID and NFC. Both NFC and many of the RFID protocols operate on 13.56MHz, but RFID tags do not have to format their data in NDEF format. The various RFID protocols do not share a common data format.

Think of NDEF messages like paragraphs and records like sentences: a paragraph is a discrete chunk of information that contains one or more sentences. A sentence is a smaller chunk of information that contains just one idea. For example, you might write a paragraph that indicates that you’re having a birthday party, and gives the address. The NDEF message equivalent might contain a simple text record to describe the event, a Smart Poster record containing the physical address, and a URI for more information on the Web.

The Architecture of NFC

In order to understand NFC in depth, it helps to have a mental model of the architecture. There are several layers to consider. The lowest layer is the physical, namely your CPU and the radios that are doing the communication. In the middle, there are data packetization and transport layers, then data format layers, and finally, your application code. Figure 2-1 shows the various layers of the NFC stack.

The NFC protocol stack
Figure 2-1. The NFC protocol stack

At the physical layer, NFC works on an RFID radio specification, ISO-14443-2, that describes low-power radios operating at 13.56MHz. Next comes a layer that describes the framing of data bytes sent over the radio, ISO-14443-3. Any of the radios you might use are separate hardware components, either inside your phone or tablet, or attachments to your microcontroller or personal computer. They communicate with the main processor of your device using one or more standard inter-device serial protocols: universal asynchronous receive-transmit (UART), serial peripheral interface (SPI), inter-integrated circuit communication (I2C), or universal serial bus (USB). If you’re a hardware enthusiast, you probably know the first three, but if your focus is software, you may only know USB.

Above that are several RFID command protocols, based on two specifications. The original RFID control specification that NFC tag reading and writing is built on is ISO-14443A. The Philips/NXP Semiconductors Mifare Classic and Mifare Ultralight and NXP DESFire protocols are compatible with ISO-14443A. The NFC peer-to-peer exchange is built on the ISO-18092 control protocol. Sony FeliCa RFID cards and tags, mostly available in Japan, are based on this standard as well. You can read from and write to tags using these standards and still not be using NFC, however.

The first major difference between NFC and RFID is the peer-to-peer communications mode, which is implemented using the ISO-18092 standard. There are two protocols, a logical link control protocol (LLCP) and a simple NDEF exchange protocol (SNEP) that manage peer-to-peer exchanges. These are shown in the stack alongside the control protocols, since they operate on the same standard as one of them.

The second major difference between RFID and NFC, the NFC Data Exchange Format (NDEF), sits atop these control protocols. NDEF defines a data exchange in messages, which are composed of NDEF records. There are several different record types, which you’ll learn more about in Chapter 4. NDEF makes it possible for your application code to deal with data from NFC tag reading and writing, peer-to-peer exchanges, and card emulation all the same way.

Most of this book focuses on the use of NDEF messaging. Although we’ll discuss the lower layers so that you understand them, we think that the advantage to NDEF is that it frees you from thinking about them.

NFC Tag Types

There are four types of tags defined by the NFC forum, all based on the RFID control protocols described previously. There’s a fifth that’s compatible, but not strictly part of the NFC specification. Types 1, 2, and 4 are all based on ISO-14443A, and type 3 is based on ISO-18092. You can get tags from many vendors, and you’ve probably run across them in everyday life without knowing it. Their details are as follows:

Type 1
  • Based on ISO-14443A specification.
  • Can be read-only, or read/write capable.
  • 96 bytes to 2 kilobytes of memory.
  • Communication speed 106Kb.
  • No data collision protection.
  • Examples: Innovision Topaz, Broadcom BCM20203.
Type 2
  • Similar to type 1 tags, type 2 tags are based on NXP/Philips Mifare Ultralight tag (ISO-14443A) specification.
  • Can be read-only, or read/write capable.
  • 96 bytes to 2 kilobytes of memory.
  • Communication speed 106Kb.
  • Anti-collision support.
  • Example: NXP Mifare Ultralight.
Type 3
  • These are based on the Sony FeliCa tags (ISO-18092 and JIS-X-6319-4), without the encryption and authentication support that FeliCa affords.
  • Configured by factory to be read-only, or read/write capable.
  • Variable memory, up to 1MB per exchange.
  • Two communication speeds, 212 or 424Kbps.
  • Anti-collision support.
  • Example: Sony FeliCa.
Type 4
  • Similar to type 1 tags, type 4 tags are based on NXP DESFire tag (ISO-14443A) specification.
  • Configured by factory to be read-only, or read/write capable.
  • 2, 4, or 8KB of memory.
  • Variable memory, up to 32KB per exchange.
  • Three communication speeds: 106, 212, or 424Kbps.
  • Anti-collision support.
  • Example: NXP DESFire, SmartMX-JCOP.

A fifth type, proprietary to NXP Semiconductors, is probably the most common in NFC use today:

Mifare Classic tag (ISO–14443A)
  • Memory options: 192, 768, or 3,584 bytes.
  • Communication speed 106Kbps
  • Anti-collision support.
  • Examples: NXP Mifare Classic 1K, Mifare Classic 4K, Mifare Classic Mini.

Where to Get Tags

Mifare and NFC tags are increasingly easy to get. To make the most of this book, you should have some NFC Forum tags and some Mifare Classic tags as well.

Many mobile phone stores now carry Samsung’s TecTiles, which work well for many of the applications in this book. They’re expensive, but convenient. The original TecTile tags are Mifare Classic, which work well on Arduino in Chapter 7 and the USB NFC reader on the BeagleBone Black and Raspberry Pi in Chapter 9. It’s not clear, but it would appear that Samsung is abandoning TecTile in favor of TecTile 2. There is a general shift among hardware vendors away from Mifare Classic and toward the official NFC Forum tag types.

If you want tags in different form factors, Adafruit has a wide variety in their NFC category. SparkFun and Seeed Studio also have plenty of Mifare tags; search for “NFC” or “Mifare” and you’ll find plenty.

You can also get tags from TagAge, but shipping can be slow from Finland, depending on your location. Customer service has been excellent for us, however.

Identive NFC can deliver tags overnight. They are a bit more expensive, but they arrive quickly and they have a cool android on them.

Device-to-Tag Type Matching

Not all NFC-compatible tags work with all NFC devices. In the course of working on this book, we’ve tested many different devices and tags, and have found some combinations work, and some don’t. Though Table 2-1 isn’t an exhaustive table, it’s intended to give you enough information to choose your devices and tags appropriately.

The most common issue you’ll run into is that some devices will read Mifare Classic tags, while others won’t. Mifare Classic is not actually an NFC Forum standard tag type, and recently, major hardware vendors like Broadcom and Samsung have started to drop support for it on their devices. You’re likely to get unpredictable results when you try to read Mifare Classic tags with some of these newer tags. For example, the Nexus 4 will read Mifare Classic as a generic tag, but the Samsung S4 (Google Edition) displays an error saying it can’t handle the tag. When possible, we recommend going with one of the official NFC Forum tag types. In the chapters ahead, we’ll make more specific recommendations where appropriate.

If you’re coming to NFC because you’re interested in using it for embedded hardware projects on the Arduino, Raspberry Pi, or BeagleBone Black (which you’ll learn more about in Chapter 7 and Chapter 9), you should make sure you have both NFC tags and Mifare Classic tags. The libraries for those platforms were developed starting with Mifare Classic support, and as of this writing, they don’t handle all the NFC Forum types. We’ve added Mifare Ultralight (NFC type 2) read support for the Arduino NDEF library (which Don wrote), and there is partial Ultralight and DESFire support (NFC types 2 and 4) in libfreefare, one of the libraries we’ll show you in Chapter 9.

While we hope to see additions to those libraries to support all of the NFC Forum tag types, it’s not there yet. Growing pains like these are common with emerging technologies, but so far, signs point to the industry moving toward a standardized set of tag types.

Table 2-1. Device-to-tag compatibility
Device Type 1 Type 2 (Mifare Ultralight) Type 3 (Sony FeliCa) Type 4 (Mifare DESFire) Mifare Classic

Samsung Galaxy S

Yes

Yes

Yes

Yes

Yes

Google Nexus S

Yes

Yes

Yes

Yes

Yes

Google Galaxy Nexus

Yes

Yes

Yes

Yes

Yes

Google Nexus 7 version 1

Yes

Yes

Yes

Yes

Yes

Google Nexus 7 version 2

Yes

Yes

Yes

Yes

No

Google Nexus 4 (phone)

Yes

Yes

Yes

Yes

No

Google Nexus 10 (tablet)

Yes

Yes

Yes

Yes

No

Samsung Galaxy S4 (tablet)

Yes

Yes

Yes

Yes

No

Samsung Galaxy SIII

Yes

Yes

Yes

Yes

Yes

Adafruit NFC Shield

No

Partial

No

No

Yes

Seeed Studio NFC Shield for Arduino

No

Partial

No

No

Yes

NFC USB Dongle (libnfc)

No

Partial

No

Partial

Yes

What You Can Do with NFC

There are many possible applications for NFC. Its tag emulation functionality is showing up in monetary transactions like Google Wallet, and in payment and ticketing systems for public transport. There are several mobile phone apps that allow you to save configuration data for your phone to a tag, and use the tag to automatically change the context of your phone (e.g., set the quiet settings for meetings, turn on WiFi and configure for a given network by tapping a tag). Home audio equipment is coming on the market that allows you to automatically pair your phone or tablet with your audio player or TV, to use the former as a remote control for the latter. Healthcare systems have implemented patient ID and record linking using NFC, and the NFC Forum recently released a Personal Health Device Communication (PHDC) technical specification to aid in that development. Inventory management can be improved by using NDEF records written to the goods being shipped, about their journey, shipping times, and so forth. Fleet tracking can also be enhanced using NFC tags and readers by storing route information, travel times, mileage, maintenance, and fuel information on tags, or transferring it from vehicle to mobile device through peer-to-peer.

In the course of working on this book, we’ve seen NFC-compatible tags in many locations, from New York City’s Citi Bike key fobs to metro passes in China and Norway to hotel room keys, and more. Although not all of these applications are using the tags to the full potential NFC affords, they all could be enhanced or simplified through some of its features.

The combination of peer-to-peer messaging between devices with minimal negotiation, and a common data format between device and tag messages makes NFC a potentially powerful tool for data-intensive applications in the physical world.

Conclusion

Near field communication adds some promising new functionality to RFID technologies. Most notable of these is the NFC Data Exchange Format, NDEF, which provides a common data format across the four different NFC tag technologies. NDEF can be used for both tag-to-device data exchange and for device-to-device data exchange. This distinguishes NFC as more than just an identification technology; it is also a useful technology for short data exchanges. The method by which NFC devices and tags interact—by means of a single tap—affords a spontaneity of interaction between users.

There are four physical NFC tag types. These are based on existing RFID standards; three of the tag types are based on the ISO-14443A standard, and the fourth is based on the ISO-18092 standard. This makes NFC tags at least partially compatible with many existing Mifare and FeliCa RFID systems. Although these older systems do not support NDEF, they can still identify those NFC Tags that are compatible with them. An RFID reader that can read Mifare Ultralight tags, for example, would still be able to read the UID of an NFC type 2 tag, even though it couldn’t read any NDEF data encoded on the tag.

In its current state, there are some incompatibilities between some NFC devices and older Mifare Classic tags. Some NFC radios support Mifare Classic as an unofficial fifth tag type, while others don’t. As NFC matures and more devices come on the market, these incompatibilities will hopefully come to affect users less. For now, however, it’s best to check the compatibility between the readers and tags you plan to use in a given application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset