|
| 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 | + |
0 commit comments