Skip to content

Commit aa1e9ac

Browse files
committed
Simple commit wo history, review rework done, internal docs removed
1 parent 51b8d6e commit aa1e9ac

27 files changed

+3149
-1
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018-2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "SmartPoster.h"
18+
19+
#include "nfc/ndef/common/Text.h"
20+
#include "nfc/ndef/common/URI.h"
21+
#include "nfc/ndef/common/Mime.h"
22+
#include "nfc/ndef/MessageBuilder.h"
23+
#include "nfc/ndef/common/util.h"
24+
25+
using mbed::Span;
26+
27+
using mbed::nfc::ndef::MessageBuilder;
28+
using mbed::nfc::ndef::RecordType;
29+
using mbed::nfc::ndef::Record;
30+
using mbed::nfc::ndef::RecordID;
31+
using mbed::nfc::ndef::RecordPayload;
32+
using mbed::nfc::ndef::common::span_from_cstr;
33+
using mbed::nfc::ndef::common::Mime;
34+
using mbed::nfc::ndef::common::Text;
35+
using mbed::nfc::ndef::common::URI;
36+
37+
// todo: this class probably needs to be in the nfc module itself
38+
39+
namespace {
40+
static RecordType smart_poster_record_type() {
41+
return RecordType(RecordType::well_known_type, span_from_cstr("Sp"));
42+
}
43+
44+
static RecordType action_record_type() {
45+
return RecordType(RecordType::well_known_type, span_from_cstr("act"));
46+
}
47+
48+
static RecordType size_record_type() {
49+
return RecordType(RecordType::well_known_type, span_from_cstr("s"));
50+
}
51+
52+
static RecordType type_record_type() {
53+
return RecordType(RecordType::well_known_type, span_from_cstr("T"));
54+
}
55+
56+
static size_t compute_record_size(const RecordType& type,
57+
const RecordPayload& payload) {
58+
return MessageBuilder::compute_record_size(
59+
Record(type, payload, RecordID(), false, false));
60+
}
61+
62+
} // end of anonymous namespace
63+
64+
SmartPoster::SmartPoster(const URI &uri) :
65+
_uri(uri), _action(), _resource_size(0), _action_set(false), _resource_size_set(
66+
false) {
67+
}
68+
69+
void SmartPoster::set_title(const Text &text) {
70+
_title = text;
71+
}
72+
73+
void SmartPoster::set_icon(const Mime &icon) {
74+
_icon = icon;
75+
}
76+
77+
void SmartPoster::set_action(action_t action) {
78+
_action = action;
79+
_action_set = true;
80+
}
81+
82+
void SmartPoster::set_resource_size(uint32_t size) {
83+
_resource_size = size;
84+
_resource_size_set = true;
85+
}
86+
87+
void SmartPoster::set_resource_type(Span<const uint8_t> &type) {
88+
_type.set_text(Text::UTF8, Span<const uint8_t>(), type);
89+
}
90+
91+
bool SmartPoster::append_record(MessageBuilder &ndef_builder,
92+
bool is_last_record) const {
93+
if (_uri.get_uri_field().empty()) {
94+
return false;
95+
}
96+
97+
struct PayloadBuilder: MessageBuilder::PayloadBuilder {
98+
PayloadBuilder(const SmartPoster &sp) :
99+
sp(sp) {
100+
}
101+
102+
virtual size_t size() const {
103+
return sp.get_uri_record_size() + sp.get_title_record_size()
104+
+ sp.get_icon_record_size() + sp.get_action_record_size()
105+
+ sp.get_resource_size_record_size()
106+
+ sp.get_type_record_size();
107+
}
108+
109+
virtual void build(const Span<uint8_t> &buffer) const {
110+
MessageBuilder smart_poster_builder(buffer);
111+
sp.append_title(smart_poster_builder);
112+
sp.append_icon(smart_poster_builder);
113+
sp.append_resource_size(smart_poster_builder);
114+
sp.append_type(smart_poster_builder);
115+
sp.append_action(smart_poster_builder);
116+
sp.append_uri(smart_poster_builder);
117+
}
118+
119+
const SmartPoster &sp;
120+
};
121+
122+
bool result = ndef_builder.append_record(smart_poster_record_type(),
123+
PayloadBuilder(*this), is_last_record);
124+
return result;
125+
}
126+
127+
void SmartPoster::append_uri(MessageBuilder& builder) const {
128+
_uri.append_as_record(builder, true);
129+
}
130+
131+
size_t SmartPoster::get_uri_record_size() const {
132+
return _uri.get_record_size();
133+
}
134+
135+
void SmartPoster::append_title(MessageBuilder& builder) const {
136+
if (_title.get_text().empty()) {
137+
return;
138+
}
139+
_title.append_as_record(builder);
140+
}
141+
142+
size_t SmartPoster::get_title_record_size() const {
143+
if (_title.get_text().empty()) {
144+
return 0;
145+
}
146+
147+
return _title.get_record_size();
148+
}
149+
150+
void SmartPoster::append_icon(MessageBuilder& builder) const {
151+
if (_icon.get_mime_content().empty()) {
152+
return;
153+
}
154+
_icon.append_as_record(builder);
155+
}
156+
157+
size_t SmartPoster::get_icon_record_size() const {
158+
if (_icon.get_mime_content().empty()) {
159+
return 0;
160+
}
161+
162+
return _icon.get_record_size();
163+
}
164+
165+
void SmartPoster::append_action(MessageBuilder& builder) const {
166+
if (!_action_set) {
167+
return;
168+
}
169+
170+
const uint8_t action_value[1] = { _action };
171+
builder.append_record(action_record_type(), action_value);
172+
}
173+
174+
size_t SmartPoster::get_action_record_size() const {
175+
if (!_action_set) {
176+
return 0;
177+
}
178+
179+
const uint8_t action_value[1] = { _action };
180+
181+
return compute_record_size(action_record_type(), action_value);
182+
}
183+
184+
void SmartPoster::append_resource_size(MessageBuilder& builder) const {
185+
if (!_resource_size_set) {
186+
return;
187+
}
188+
189+
uint8_t value[4];
190+
std::reverse_copy(&_resource_size, &_resource_size + 4, value);
191+
192+
builder.append_record(size_record_type(), value);
193+
}
194+
195+
size_t SmartPoster::get_resource_size_record_size() const {
196+
if (!_resource_size_set) {
197+
return 0;
198+
}
199+
200+
uint8_t value[4];
201+
202+
return compute_record_size(size_record_type(), value);
203+
}
204+
205+
void SmartPoster::append_type(MessageBuilder& builder) const {
206+
if (_type.get_text().empty()) {
207+
return;
208+
}
209+
210+
builder.append_record(type_record_type(), _type.get_text());
211+
}
212+
213+
size_t SmartPoster::get_type_record_size() const {
214+
if (_type.get_text().empty()) {
215+
return 0;
216+
}
217+
218+
return compute_record_size(type_record_type(), _type.get_text());
219+
}
220+

TEST_APPS/device/nfcapp/SmartPoster.h

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018-2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef SMARTPOSTER_H_
18+
#define SMARTPOSTER_H_
19+
20+
#include "nfc/ndef/common/Text.h"
21+
#include "nfc/ndef/common/URI.h"
22+
#include "nfc/ndef/common/Mime.h"
23+
#include "nfc/ndef/MessageBuilder.h"
24+
25+
/**
26+
* Smart poster object.
27+
*
28+
* A smart poster is one of the basic use case of NFC. It encapsulates a URI to
29+
* a resource and meta-data of the resource.
30+
*
31+
* Meta-data are optional, they can be:
32+
* - title: name of the resource
33+
* - icon: image/media associated to the resource
34+
* - action: Action the peer should execute upon reception of the smart poster
35+
* - size: The size of the resource.
36+
* - type: Mime type of the resource.
37+
*
38+
* @note It obeys to value semantic and can be copied around.
39+
*/
40+
class SmartPoster {
41+
public:
42+
typedef mbed::nfc::ndef::common::Mime Mime;
43+
typedef mbed::nfc::ndef::common::Text Text;
44+
typedef mbed::nfc::ndef::common::URI URI;
45+
typedef mbed::nfc::ndef::MessageBuilder MessageBuilder;
46+
47+
/**
48+
* Type of actions that should be executed upon smart poster reception.
49+
*/
50+
enum action_t {
51+
EXECUTE, //!< EXECUTE
52+
SAVE, //!< SAVE
53+
EDIT //!< EDIT
54+
};
55+
56+
/**
57+
* Construct a smart poster.
58+
*
59+
* @param uri The URI to the resource.
60+
*/
61+
SmartPoster(const URI &uri);
62+
63+
/**
64+
* Set the title of the resource.
65+
*
66+
* @param text The title of the resource to set.
67+
*/
68+
void set_title(const Text &text);
69+
70+
/**
71+
* Set the icon of the resource.
72+
*
73+
* @param icon The icon to set.
74+
*/
75+
void set_icon(const Mime &icon);
76+
77+
/**
78+
* Set the action to trigger upon smart poster reception.
79+
*
80+
* @param action The action to do upon reception.
81+
*/
82+
void set_action(action_t action);
83+
84+
/**
85+
* Set the size of the resource.
86+
*
87+
* @param size The size of the resource.
88+
*/
89+
void set_resource_size(uint32_t size);
90+
91+
/**
92+
* Set the type of the resource.
93+
*
94+
* @param resource_type The type of the resource pointed by the URI.
95+
*/
96+
void set_resource_type(mbed::Span<const uint8_t> &resource_type);
97+
98+
/**
99+
* Append the smart poster as a ndef record.
100+
*
101+
* @param ndef_builder The message builder where the record is appended.
102+
* @param is_last_record Indicates if this message is the last one.
103+
*
104+
* @return true if the message has been appended to the builder or false
105+
* otherwise.
106+
*/
107+
bool append_record(MessageBuilder &ndef_builder, bool is_last_record) const;
108+
109+
private:
110+
void append_uri(MessageBuilder &builder) const;
111+
size_t get_uri_record_size() const;
112+
113+
void append_title(MessageBuilder &builder) const;
114+
size_t get_title_record_size() const;
115+
116+
void append_icon(MessageBuilder &builder) const;
117+
size_t get_icon_record_size() const;
118+
119+
void append_action(MessageBuilder &builder) const;
120+
size_t get_action_record_size() const;
121+
122+
void append_resource_size(MessageBuilder &builder) const;
123+
size_t get_resource_size_record_size() const;
124+
125+
void append_type(MessageBuilder &builder) const;
126+
size_t get_type_record_size() const;
127+
128+
URI _uri;
129+
Text _title;
130+
Mime _icon;
131+
action_t _action;
132+
uint32_t _resource_size;
133+
Text _type;
134+
135+
bool _action_set :1;
136+
bool _resource_size_set :1;
137+
};
138+
139+
#endif /* SMARTPOSTER_H_ */

0 commit comments

Comments
 (0)