Skip to content

Commit c1e5624

Browse files
author
Donatien Garnier
committed
Move some content to technology section, add doc for specific classes
1 parent 58a5874 commit c1e5624

File tree

5 files changed

+241
-213
lines changed

5 files changed

+241
-213
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
### NDEF API
2+
3+
We provide API to construct and parse NDEF messages which is the common data format exchange for NFC messages.
4+
5+
A NDEF message is a collection of separate NDEF records where each of these records is defined by a type such as URI, Mime, Text that identifies what is contained in the record and a payload.
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 developers 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/FIXME)
81+
82+
#### Construction
83+
84+
The class `mbed::nfc::ndef::MessageBuilder` builds a NDEF message into a user provided buffer. `URI`, `Text` and `Mime` type can be serialized in the builder with the help of the member function `append_as_record`.
85+
86+
```
87+
#include "nfc/ndef/MessageBuilder.h"
88+
89+
using mbed::nfc::ndef::MessageBuilder;
90+
using mbed::nfc::ndef::common::Text;
91+
using mbed::nfc::ndef::common::URI;
92+
93+
size_t build_ndef_message(const Span<uint8_t> &buffer) {
94+
MessageBuilder builder(buffer);
95+
96+
URI uri(URI::HTTPS_WWW, span_from_cstr("mbed.com"));
97+
Text text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("Mbed website"));
98+
99+
uri.append_as_record(builder);
100+
text.append_as_record(builder, /* last record */ true);
101+
102+
return builder.get_message().size();
103+
}
104+
```
105+
106+
[![View code](https://www.mbed.com/embed/?type=library)](http://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/FIXME)

docs/reference/api/connectivity/nfc/NFC.md

Lines changed: 0 additions & 213 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## NFC Controller
2+
3+
To use a NFC controller, you must initiate the instance with a driver instance, an event queue and a scratch buffer for NDEF messages.
4+
5+
### NFC Controller Class Reference
6+
7+
[![View code](https://www.mbed.com/embed/?type=library)](http://os-doc-builder.test.mbed.com/docs/development/mbed-os-api-doxy/FIXME)
8+
9+
### NFC Controller Example
10+
11+
```cpp
12+
#include "stdint.h"
13+
14+
#include "NFC.h"
15+
#include "events/EventQueue.h"
16+
#include "nfc/controllers/PN512Driver.h"
17+
#include "nfc/controllers/PN512SPITransportDriver.h"
18+
19+
static uint8_t ndef_buffer[1024] = {0};
20+
21+
int main() {
22+
mbed::nfc::PN512SPITransportDriver pn512_transport(D11, D12, D13, D10, A1, A0);
23+
mbed::nfc::PN512Driver pn512_driver(&pn512_transport);
24+
events::EventQueue queue;
25+
mbed::nfc::NFCController nfc(&pn512_driver, &queue, ndef_buffer);
26+
27+
...
28+
}
29+
```
30+
31+
A delegate mechanism handles events throughout the API.
32+
33+
For instance, a delegate for a NFC controller can look similar to this:
34+
35+
```cpp
36+
struct NFCDelegate : mbed::nfc::NFCController::Delegate {
37+
virtual void on_discovery_terminated(nfc_discovery_terminated_reason_t reason) {
38+
printf("Discovery terminated:\r\n");
39+
if(reason != nfc_discovery_terminated_completed) {
40+
nfc_ptr->start_discovery(); // Error, restart discovery
41+
}
42+
}
43+
virtual void on_nfc_initiator_discovered(const SharedPtr< mbed::nfc::NFCRemoteInitiator> &nfc_initiator) {
44+
printf("Remote inititator detected\r\n");
45+
nfc_initiator->set_delegate(nfc_initiator_delegate_ptr);
46+
nfc_initiator->connect(); // Connect to the initiator
47+
}
48+
};
49+
```

0 commit comments

Comments
 (0)