Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers URI.cpp Source File

URI.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <string.h>
00018 
00019 #include "nfc/ndef/common/URI.h"
00020 
00021 namespace {
00022 static const uint8_t uri_id_code_size = 1;
00023 static const uint8_t uri_id_index = 0;
00024 static const uint8_t uri_field_index = 1;
00025 static const uint8_t uri_record_type_value[] = { 'U' } ;
00026 }
00027 
00028 namespace mbed {
00029 namespace nfc {
00030 namespace ndef {
00031 namespace common {
00032 
00033 URI::URI() :
00034     _uri(NULL),
00035     _uri_size(0)
00036 {
00037 }
00038 
00039 URI::URI(uri_identifier_code_t id, const Span<const uint8_t>  &uri_field) :
00040     _uri(uri_field.size() ? new uint8_t[uri_id_code_size + uri_field.size()] : NULL),
00041     _uri_size(uri_id_code_size + uri_field.size())
00042 {
00043     if (_uri) {
00044         _uri[uri_id_index] = id;
00045         memcpy(_uri + uri_field_index, uri_field.data(), uri_field.size());
00046     }
00047 }
00048 
00049 URI::URI(const URI &other) :
00050     _uri(other._uri ? new uint8_t[other._uri_size] : NULL),
00051     _uri_size(other._uri_size)
00052 {
00053     if (_uri) {
00054         memcpy(_uri, other._uri, other._uri_size);
00055     }
00056 }
00057 
00058 URI::~URI()
00059 {
00060     delete[] _uri;
00061 }
00062 
00063 URI &URI::operator=(const URI &other)
00064 {
00065     delete[] _uri;
00066 
00067     if (!other._uri) {
00068         _uri = NULL;
00069         _uri_size = 0;
00070     } else {
00071         _uri = new uint8_t[other._uri_size];
00072         _uri_size = other._uri_size;
00073         memcpy(_uri, other._uri, other._uri_size);
00074     }
00075 
00076     return *this;
00077 }
00078 
00079 void URI::set_uri(
00080     uri_identifier_code_t id,
00081     const Span<const uint8_t>  &uri_field
00082 )
00083 {
00084     delete[] _uri;
00085 
00086     if (uri_field.empty()) {
00087         _uri = NULL;
00088         _uri_size = 0;
00089         return;
00090     }
00091 
00092     _uri = new uint8_t[uri_id_code_size + uri_field.size()];
00093     _uri_size = uri_id_code_size + uri_field.size();
00094     _uri[uri_id_index] = id;
00095     memcpy(_uri + uri_field_index, uri_field.data(), uri_field.size());
00096 }
00097 
00098 URI::uri_identifier_code_t URI::get_id() const
00099 {
00100     if (!_uri) {
00101         return NA;
00102     }
00103 
00104     return static_cast<uri_identifier_code_t>(_uri[uri_id_index]);
00105 }
00106 
00107 Span<const uint8_t>  URI::get_uri_field() const
00108 {
00109     if (!_uri) {
00110         return Span<const uint8_t> ();
00111     }
00112     return make_const_Span(
00113                _uri + uri_field_index,
00114                _uri_size - uri_id_code_size
00115            );
00116 }
00117 
00118 bool URI::append_as_record(MessageBuilder &message_builder, bool is_last_record) const
00119 {
00120     if (!_uri) {
00121         return false;
00122     }
00123 
00124     // Build the record type
00125     RecordType type(
00126         RecordType::well_known_type,
00127         uri_record_type_value
00128     );
00129 
00130     // build the record payload
00131     RecordPayload  payload(_uri, _uri_size);
00132 
00133     return message_builder.append_record(type, payload, is_last_record);
00134 }
00135 
00136 size_t URI::get_record_size() const
00137 {
00138     if (!_uri) {
00139         return 0;
00140     }
00141 
00142     return MessageBuilder::compute_record_size(
00143                Record(
00144                    RecordType(
00145                        RecordType::well_known_type,
00146                        uri_record_type_value
00147                    ),
00148                    RecordPayload(_uri, _uri_size),
00149                    RecordID(),
00150                    /* chunk */ false,
00151                    /* last record */ false
00152                )
00153            );
00154 }
00155 
00156 void URI::move_data(uint8_t *new_uri, size_t new_uri_size)
00157 {
00158     delete[] _uri;
00159     _uri = new_uri;
00160     _uri_size = new_uri_size;
00161 }
00162 
00163 bool URIParser::do_parse(const Record &record, URI &uri)
00164 {
00165     if (record.type.tnf != RecordType::well_known_type) {
00166         return false;
00167     }
00168 
00169     // the record type value should be equal to `U`
00170     if (record.type.value != make_const_Span(uri_record_type_value) ||
00171             record.payload.empty()
00172        ) {
00173         return false;
00174     }
00175 
00176     // create the buffer
00177     size_t uri_record_size = record.payload.size();
00178     uint8_t *uri_record = new uint8_t[uri_record_size];
00179     memcpy(uri_record, record.payload.data(), uri_record_size);
00180 
00181     uri.move_data(uri_record, uri_record_size);
00182 
00183     return true;
00184 }
00185 
00186 } // namespace common
00187 } // namespace ndef
00188 } // namespace nfc
00189 } // namespace mbed
00190 
00191