Skip to content

Commit 8cc5f08

Browse files
committed
Add SimpleMessageParser example
1 parent e7c8205 commit 8cc5f08

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## SimpleMessageParser Example
2+
3+
This SimpleMessageParser example shows how to parse NDEF messages.

APIs_NFC/SimpleMessageParser/main.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "mbed.h"
2+
#include "nfc/ndef/MessageBuilder.h"
3+
#include "nfc/ndef/common/URI.h"
4+
#include "nfc/ndef/common/Text.h"
5+
#include "nfc/ndef/common/util.h"
6+
#include "nfc/ndef/common/SimpleMessageParser.h"
7+
8+
using mbed::nfc::ndef::Record;
9+
using mbed::nfc::ndef::RecordType;
10+
using mbed::nfc::ndef::RecordID;
11+
using mbed::nfc::ndef::MessageParser;
12+
using mbed::nfc::ndef::common::Text;
13+
using mbed::nfc::ndef::common::URI;
14+
using mbed::nfc::ndef::common::Mime;
15+
using mbed::nfc::ndef::MessageBuilder;
16+
using mbed::nfc::ndef::common::span_from_cstr;
17+
using mbed::nfc::ndef::common::SimpleMessageParser;
18+
19+
int main()
20+
{
21+
uint8_t message_data[512];
22+
const Span<uint8_t, 512> buffer(message_data, sizeof(message_data));
23+
24+
MessageBuilder builder(buffer);
25+
26+
Text text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("Mbed website"));
27+
URI uri(URI::HTTPS_WWW, span_from_cstr("mbed.com"));
28+
29+
uri.append_as_record(builder);
30+
text.append_as_record(builder, /* last record */ true);
31+
32+
SimpleMessageParser parser;
33+
struct : SimpleMessageParser::Delegate {
34+
virtual void on_parsing_started()
35+
{
36+
printf("parsing started\r\n");
37+
}
38+
39+
virtual void on_text_parsed(const Text &text, const RecordID &)
40+
{
41+
printf("Text record parsed.\r\n");
42+
printf(
43+
"\tlanguage: %.*s\r\n",
44+
text.get_language_code().size(), text.get_language_code().data()
45+
);
46+
printf("\ttext: %.*s\r\n", text.get_text().size(), text.get_text().data());
47+
}
48+
49+
virtual void on_uri_parsed(const URI &uri, const RecordID &)
50+
{
51+
printf("URI parsed.\r\n");
52+
printf("\tid: %d\r\n", uri.get_id());
53+
printf("\tvalue: %.*s\r\n", uri.get_uri_field().size(), uri.get_uri_field().data());
54+
}
55+
56+
virtual void on_mime_parsed(const Mime &mime, const RecordID &)
57+
{
58+
printf("Mime object parsed.\r\n");
59+
printf("\ttype: %.*s\r\n", mime.get_mime_type().size(), mime.get_mime_type().data());
60+
printf("\tcontent size: %d\r\n", mime.get_mime_content().size());
61+
}
62+
63+
virtual void on_unknown_record_parsed(const Record &record)
64+
{
65+
printf("Unknown record parsed.\r\n");
66+
printf(
67+
"\ttype: %d, type_value: %.*s\r\n",
68+
record.type.tnf, record.type.value.size(), record.type.value.data()
69+
);
70+
printf(
71+
"\tpayload size: %d, payload: %.*s\r\n",
72+
record.payload.size(), record.payload.size(), record.payload.data()
73+
);
74+
}
75+
76+
virtual void on_parsing_terminated()
77+
{
78+
printf("parsing terminated\r\n");
79+
}
80+
81+
virtual void on_parsing_error(MessageParser::error_t error)
82+
{
83+
printf("Parsing error: %d\r\n", error);
84+
}
85+
} delegate;
86+
87+
parser.set_delegate(&delegate);
88+
parser.parse(buffer);
89+
}

0 commit comments

Comments
 (0)