Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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     _uri[uri_id_index] = id;
00044     memcpy(_uri + uri_field_index, uri_field.data(), uri_field.size());
00045 }
00046 
00047 URI::URI(const URI &other) :
00048     _uri(other._uri ? new uint8_t[other._uri_size] : NULL),
00049     _uri_size(other._uri_size)
00050 {
00051     memcpy(_uri, other._uri, other._uri_size);
00052 }
00053 
00054 URI::~URI()
00055 {
00056     delete[] _uri;
00057 }
00058 
00059 URI &URI::operator=(const URI &other)
00060 {
00061     delete[] _uri;
00062 
00063     if (!other._uri) {
00064         _uri = NULL;
00065         _uri_size = 0;
00066     } else {
00067         _uri = new uint8_t[other._uri_size];
00068         _uri_size = other._uri_size;
00069         memcpy(_uri, other._uri, other._uri_size);
00070     }
00071 
00072     return *this;
00073 }
00074 
00075 void URI::set_uri(
00076     uri_identifier_code_t id,
00077     const Span<const uint8_t>  &uri_field
00078 )
00079 {
00080     delete[] _uri;
00081 
00082     if (uri_field.empty()) {
00083         _uri = NULL;
00084         _uri_size = 0;
00085         return;
00086     }
00087 
00088     _uri = new uint8_t[uri_id_code_size + uri_field.size()];
00089     _uri_size = uri_id_code_size + uri_field.size();
00090     _uri[uri_id_index] = id;
00091     memcpy(_uri + uri_field_index, uri_field.data(), uri_field.size());
00092 }
00093 
00094 URI::uri_identifier_code_t URI::get_id() const
00095 {
00096     if (!_uri) {
00097         return NA;
00098     }
00099 
00100     return static_cast<uri_identifier_code_t>(_uri[uri_id_index]);
00101 }
00102 
00103 Span<const uint8_t>  URI::get_uri_field() const
00104 {
00105     if (!_uri) {
00106         return Span<const uint8_t> ();
00107     }
00108     return make_const_Span(
00109                _uri + uri_field_index,
00110                _uri_size - uri_id_code_size
00111            );
00112 }
00113 
00114 bool URI::append_as_record(MessageBuilder &message_builder, bool is_last_record) const
00115 {
00116     if (!_uri) {
00117         return false;
00118     }
00119 
00120     // Build the record type
00121     RecordType type(
00122         RecordType::well_known_type,
00123         uri_record_type_value
00124     );
00125 
00126     // build the record payload
00127     RecordPayload  payload(_uri, _uri_size);
00128 
00129     return message_builder.append_record(type, payload, is_last_record);
00130 }
00131 
00132 size_t URI::get_record_size() const
00133 {
00134     if (!_uri) {
00135         return 0;
00136     }
00137 
00138     return MessageBuilder::compute_record_size(
00139                Record(
00140                    RecordType(
00141                        RecordType::well_known_type,
00142                        uri_record_type_value
00143                    ),
00144                    RecordPayload(_uri, _uri_size),
00145                    RecordID(),
00146                    /* chunk */ false,
00147                    /* last record */ false
00148                )
00149            );
00150 }
00151 
00152 void URI::move_data(uint8_t *new_uri, size_t new_uri_size)
00153 {
00154     delete[] _uri;
00155     _uri = new_uri;
00156     _uri_size = new_uri_size;
00157 }
00158 
00159 bool URIParser::do_parse(const Record &record, URI &uri)
00160 {
00161     if (record.type.tnf != RecordType::well_known_type) {
00162         return false;
00163     }
00164 
00165     // the record type value should be equal to `U`
00166     if (record.type.value != make_const_Span(uri_record_type_value) ||
00167             record.payload.empty()
00168        ) {
00169         return false;
00170     }
00171 
00172     // create the buffer
00173     size_t uri_record_size = record.payload.size();
00174     uint8_t *uri_record = new uint8_t[uri_record_size];
00175     memcpy(uri_record, record.payload.data(), uri_record_size);
00176 
00177     uri.move_data(uri_record, uri_record_size);
00178 
00179     return true;
00180 }
00181 
00182 } // namespace common
00183 } // namespace ndef
00184 } // namespace nfc
00185 } // namespace mbed
00186 
00187