Skip to content

Commit 68a670a

Browse files
author
Amanda Butler
authored
Merge pull request #691 from donatieng/nfc
NFC Documentation
2 parents 91ab468 + cbb3d9e commit 68a670a

File tree

7 files changed

+250
-1
lines changed

7 files changed

+250
-1
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
### NDEF API
2+
3+
Mbed OS provide this API to construct and parse NDEF messages, which is the common data format exchange for NFC messages.
4+
5+
An NDEF message is a collection of separate NDEF records. Each of these records is defined by a type, such as URI, Mime and Text, that identifies a payload and what the record contains.
6+
7+
#### Parsing
8+
9+
The class `mbed::nfc::ndef::MessageParser` parses a buffer of data in input and produces parsing events that are forwarded to its delegate. To help you, we offer a more integrated parser (`mbed::nfc::ndef::common::SimpleMessageParser`) that parses well known NFC types records, such as Text, URI or Mime records, and produces usable objects out of the box.
10+
11+
```
12+
#include "nfc/ndef/common/SimpleMessageParser.h"
13+
14+
using mbed::nfc::ndef::Record;
15+
using mbed::nfc::ndef::RecordType;
16+
using mbed::nfc::ndef::RecordID;
17+
using mbed::nfc::ndef::MessageParser;
18+
using mbed::nfc::ndef::common::Text;
19+
using mbed::nfc::ndef::common::URI;
20+
using mbed::nfc::ndef::common::Mime;
21+
22+
using mbed::nfc::ndef::common::SimpleMessageParser;
23+
24+
void parse_ndef_message(const Span<const uint8_t> &buffer) {
25+
SimpleMessageParser parser;
26+
struct : SimpleMessageParser::Delegate {
27+
virtual void on_parsing_started() {
28+
printf("parsing started\r\n");
29+
}
30+
31+
virtual void on_text_parsed(const Text &text, const RecordID &) {
32+
printf("Text record parsed.\r\n");
33+
printf(
34+
"\tlanguage: %.*s\r\n",
35+
text.get_language_code().size(), text.get_language_code().data()
36+
);
37+
printf("\ttext: %.*s\r\n", text.get_text().size(), text.get_text().data());
38+
}
39+
40+
virtual void on_uri_parsed(const URI &uri, const RecordID &) {
41+
printf("URI parsed.\r\n");
42+
printf("\tid: %d\r\n", uri.get_id());
43+
printf("\tvalue: %.*s\r\n", uri.get_uri_field().size(), uri.get_uri_field().data());
44+
}
45+
46+
virtual void on_mime_parsed(const Mime &mime, const RecordID &) {
47+
printf("Mime object parsed.\r\n");
48+
printf("\ttype: %.*s\r\n", mime.get_mime_type().size(), mime.get_mime_type().data());
49+
printf("\tcontent size: %d\r\n", mime.get_mime_content().size());
50+
}
51+
52+
virtual void on_unknown_record_parsed(const Record &record) {
53+
printf("Unknown record parsed.\r\n");
54+
printf(
55+
"\ttype: %d, type_value: %.*s\r\n",
56+
record.type.tnf, record.type.value.size(), record.type.value.data()
57+
);
58+
printf(
59+
"\tpayload size: %d, payload: %.*s\r\n",
60+
record.payload.size(), record.payload.size(), record.payload.data()
61+
);
62+
}
63+
64+
virtual void on_parsing_terminated() {
65+
printf("parsing terminated\r\n");
66+
}
67+
68+
virtual void on_parsing_error(MessageParser::error_t error) {
69+
printf("Parsing error: %d\r\n", error);
70+
}
71+
} delegate;
72+
73+
parser.set_delegate(&delegate);
74+
parser.parse(buffer);
75+
}
76+
```
77+
78+
An application can extend capabilities of `SimpleMessageParser` by adding new record parsers (`mbed::nfc::ndef::RecordParser`) at runtime.
79+
80+
[![View code](https://www.mbed.com/embed/?type=library)](http://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1nfc_1_1ndef_1_1_message_parser.html)
81+
82+
[![View code](https://www.mbed.com/embed/?type=library)](http://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1nfc_1_1ndef_1_1common_1_1_simple_message_parser.html)
83+
84+
#### Construction
85+
86+
The class `mbed::nfc::ndef::MessageBuilder` builds an NDEF message into a user-provided buffer. `URI`, `Text` and `Mime` types can be serialized in the builder with the help of the member function `append_as_record`.
87+
88+
```
89+
#include "nfc/ndef/MessageBuilder.h"
90+
91+
using mbed::nfc::ndef::MessageBuilder;
92+
using mbed::nfc::ndef::common::Text;
93+
using mbed::nfc::ndef::common::URI;
94+
95+
size_t build_ndef_message(const Span<uint8_t> &buffer) {
96+
MessageBuilder builder(buffer);
97+
98+
URI uri(URI::HTTPS_WWW, span_from_cstr("mbed.com"));
99+
Text text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("Mbed website"));
100+
101+
uri.append_as_record(builder);
102+
text.append_as_record(builder, /* last record */ true);
103+
104+
return builder.get_message().size();
105+
}
106+
```
107+
108+
[![View code](https://www.mbed.com/embed/?type=library)](http://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1nfc_1_1ndef_1_1_message_builder.html)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## NFC controller
2+
3+
Using an NFC controller with Mbed OS allows you to emulate NFC tags that a smartphone can read, as well as generate NDEF messages on demand.
4+
5+
To use an NFC controller, you must initiate the instance with a driver instance, an event queue and a scratch buffer for NDEF messages.
6+
7+
### NFC controller class reference
8+
9+
[![View code](https://www.mbed.com/embed/?type=library)](http://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1nfc_1_1_n_f_c_controller.html)
10+
11+
### NFC controller example
12+
13+
```cpp
14+
#include "stdint.h"
15+
16+
#include "NFC.h"
17+
#include "events/EventQueue.h"
18+
#include "nfc/controllers/PN512Driver.h"
19+
#include "nfc/controllers/PN512SPITransportDriver.h"
20+
21+
static uint8_t ndef_buffer[1024] = {0};
22+
23+
int main() {
24+
mbed::nfc::PN512SPITransportDriver pn512_transport(D11, D12, D13, D10, A1, A0);
25+
mbed::nfc::PN512Driver pn512_driver(&pn512_transport);
26+
events::EventQueue queue;
27+
mbed::nfc::NFCController nfc(&pn512_driver, &queue, ndef_buffer);
28+
29+
...
30+
}
31+
```
32+
33+
A delegate mechanism handles events throughout the API.
34+
35+
For instance, a delegate for a NFC controller can look similar to this:
36+
37+
```cpp
38+
struct NFCDelegate : mbed::nfc::NFCController::Delegate {
39+
virtual void on_discovery_terminated(nfc_discovery_terminated_reason_t reason) {
40+
printf("Discovery terminated:\r\n");
41+
if(reason != nfc_discovery_terminated_completed) {
42+
nfc_ptr->start_discovery(); // Error, restart discovery
43+
}
44+
}
45+
virtual void on_nfc_initiator_discovered(const SharedPtr< mbed::nfc::NFCRemoteInitiator> &nfc_initiator) {
46+
printf("Remote inititator detected\r\n");
47+
nfc_initiator->set_delegate(nfc_initiator_delegate_ptr);
48+
nfc_initiator->connect(); // Connect to the initiator
49+
}
50+
};
51+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## NFC EEPROM
2+
3+
An NFC EEPROM can store NDEF messages. You can use a smartphone access the message through the NFC interface the EEPROM exposes.
4+
5+
To use an NFC EEPROM, you must initiate the instance with a driver instance, an event queue and a scratch buffer for NDEF messages.
6+
7+
### NFC EEPROM class reference
8+
9+
[![View code](https://www.mbed.com/embed/?type=library)](http://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/classmbed_1_1nfc_1_1_n_f_c_e_e_p_r_o_m.html)
10+
11+
### NFC EEPROM example
12+
13+
```cpp
14+
#include "stdint.h"
15+
16+
#include "NFC.h"
17+
#include "events/EventQueue.h"
18+
#include "m24sr_driver.h"
19+
20+
static uint8_t ndef_buffer[1024] = {0};
21+
22+
int main() {
23+
mbed::nfc::vendor::ST::M24srDriver m24sr_driver(p10, p11);
24+
events::EventQueue queue;
25+
mbed::nfc::NFCEEPROM nfc(&m24sr_driver, &queue, ndef_buffer);
26+
27+
...
28+
nfc.write_ndef_message();
29+
}
30+
```
Loading
17.2 KB
Loading
Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
11
<h2 id="nfc-technology">NFC</h2>
22

3-
[Placeholder for NFC technology page]
3+
NFC stands for Near-Field Communication and is a short-range radio technology. You can use it to enable use cases such as contactless payments, access control and device pairing.
4+
5+
<span class="images">![](https://s3-us-west-2.amazonaws.com/mbed-os-docs-images/n_mark.png)<span>N Mark</span></span>
6+
7+
You can find more details on NFC about the [NFC Forum's website](https://nfc-forum.org/what-is-nfc/).
8+
9+
### NFC support in Mbed OS
10+
11+
Historically, NFC encompasses multiple protocols. NFC support in Mbed OS is based on the specifications the [NFC Forum](https://nfc-forum.org/our-work/specifications-and-application-documents/specifications/nfc-forum-technical-specifications/) publishes.
12+
13+
NFC offers three modes:
14+
15+
1. NFC card emulation.
16+
1. NFC reader or writer.
17+
1. NFC peer to peer.
18+
19+
Mbed OS supports the card emulation mode, either through the use of a controller or NFC EEPROM.
20+
21+
NFC support is available for the NXP PN512 transceiver. In terms of NFC EEPROMs, a driver for the ST M24SR EEPROM series is available - we also provide a [porting guide](https://os.mbed.com/contributing/connectivity/NFCEEPROMDriver.html) to add support for other NFC EEPROMs.
22+
23+
We also provide APIs to decode and encode NDEF messages. A NDEF message is a standardized encoding that most NFC devices, such as Android and iOS smartphones, can understand.
24+
25+
An NFC EEPROM can read from and write to a NDEF message. A NFC initiator can access the message through the NFC interface the EEPROM exposes.
26+
27+
The NFC Forum defines 5 Tag specifications, which define how to transmit and receive NDEF messages on top of various NFC technologies.
28+
29+
When using a controller, the Mbed OS NFC Framework can implement the [Type 4 Tag platform](https://nfc-forum.org/our-work/specifications-and-application-documents/specifications/tag-type-technical-specifications/). This means that the initiator can gnerate NDEF messages dynamically before each read and parse it after each write.
30+
31+
### NFC controllers and NFC EEPROMs
32+
33+
These components address different use cases, and you need to consider two factors: cost and functionality.
34+
35+
Most NFC controllers implement many modes of NFC and offer flexibility; however, they are typically more expensive than NFC EEPROMs.
36+
37+
NFC controllers typically support a large subset (if not all) of NFC Forum functionality, can act as either initiators or targets and are driven by an external stack. In Mbed OS, this means that you can generate NDEF messages "on demand" when the initiator reads them.
38+
39+
NFC EEPROMs behave like NFC tags whose memory can either be addressed through a wired (such as I2C) or NFC air interface. One benefit is that these work autonomously from a microcontroller. Some of them can also use the NFC field as a power source if they are powered off at the time.
40+
41+
### Getting started with NFC and example
42+
43+
NFC examples are available on [GitHub](https://github.com/ARMmbed/mbed-os-example-nfc) and demonstrate how to create NFC tags that you can be read from and write to using a phone.
44+
45+
<span class="images">![](https://s3-us-west-2.amazonaws.com/mbed-os-docs-images/explorenfc_nucleo.png)<span>An Explore-NFC board attached to a Nucleo board</span></span>
46+
47+
### API
48+
49+
There are two entrypoints for the API, depending on whether you are using a NFC Controller or EEPROM.
50+
51+
You must initiate either entry point with a driver instance, an event queue and a scratch buffer for NDEF messages.
52+
53+
### Design
54+
55+
A detailed design document is available [within the Mbed OS source tree](https://github.com/ARMmbed/mbed-os/docs/design-documents/nfc/nfc_design.md). It details the rationale behind the API design.

docs/reference/technology/connectivity/connectivity.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ For descriptions of mesh networks, please see the [6LoWPAN Mesh](mesh-tech.html)
4343
[LoRaWAN](http://lora-alliance.org) is a long range wide-area network technology that combines long range with low power consumption. LoRaWAN is not IP based.
4444

4545
The [LoRa](lora-tech.html) section and [LoRa tutorial](/docs/development/tutorials/LoRa-tutorial.html) describe LoRA networking.
46+
47+
### NFC
48+
49+
<span class="images">![](https://s3-us-west-2.amazonaws.com/mbed-os-docs-images/n_mark.png)<span>NFC</span></span>
50+
51+
Near-field communication (NFC) is a short range (few centimeters) wireless technology standard for personal area networks. Typical uses of NFC are commissioning, easy sharing of small content and Bluetooth connection initiation/out-of-band pairing.
52+
53+
To learn how to use NFC with Mbed OS, please refer to the [NFC](/docs/development/apis/nfc.html) user API reference.

0 commit comments

Comments
 (0)