Mistake on this page?
Report an issue in GitHub or email us
URI.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 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 NFC_COMMON_URI_H_
18 #define NFC_COMMON_URI_H_
19 
20 #include <stdint.h>
21 #include <string.h>
22 
23 #include "platform/Span.h"
24 
25 #include "nfc/ndef/RecordParser.h"
26 #include "nfc/ndef/MessageBuilder.h"
27 
28 namespace mbed {
29 namespace nfc {
30 namespace ndef {
31 namespace common {
32 
33 /**
34  * @addtogroup nfc
35  * @{
36  */
37 
38 /**
39  * Model the well known type URI.
40  */
41 class URI {
42 public:
43  /**
44  * Identifier codes
45  */
47  NA = 0x00, /// Not applicable
48  HTTP_WWW = 0x01, /// http://www.
49  HTTPS_WWW = 0x02, /// https://www.
50  HTTP = 0x03, /// http://
51  HTTPS = 0x04, /// https://
52  TEL = 0x05, /// tel:
53  MAILTO = 0x06, /// mailto:
54  FTP_ANONYMOUS = 0x07, /// ftp://anonymous:anonymous@
55  FTP_FTP = 0x08, /// ftp://ftp.
56  FTPS = 0x09, /// ftps://
57  SFTP = 0x0A, /// sftp://
58  SMB = 0x0B, /// smb://
59  NFS = 0x0C, /// nfs://
60  FTP = 0x0D, /// ftp://
61  DAV = 0x0E, /// dav://
62  NEWS = 0x0F, /// news:
63  TELNET = 0x10, /// telnet://
64  IMAP = 0x11, /// imap:
65  RSTP = 0x12, /// rstp://
66  URN = 0x13, /// urn:
67  POP = 0x14, /// pop:
68  SIP = 0x15, /// sip:
69  SIPS = 0x16, /// sips:
70  TFTP = 0x17, /// tftp:
71  BTSPP = 0x18, /// btspp://
72  BTL2CAP = 0x19, /// btl2cap://
73  BTGOEP = 0x1A, /// btgoep://
74  TCPOBEX = 0x1B, /// tcpobex://
75  IRDAOBEX = 0x1C, /// irdaobex://
76  FILE = 0x1D, /// file://
77  URN_EPC_ID = 0x1E, /// urn:epc:id:
78  URN_EPC_TAG = 0x1F, /// urn:epc:tag:
79  URN_EPC_PAT = 0x20, /// urn:epc:pat:
80  URN_EPC_RAW = 0x21, /// urn:epc:raw:
81  URN_EPC = 0x22, /// urn:epc:
82  URN_NFC = 0x23, /// urn:nfc:
83  };
84 
85  /**
86  * Construct an empty URI object.
87  */
88  URI();
89 
90  /**
91  * Construct a URI from an id and a uri field.
92  *
93  * @param id The code of the URI prefix.
94  * @param uri_field The URI itself.
95  *
96  * @note To remove the NULL terminator of the C-string of the uri_field
97  * parameter, you can use the utility function span_from_cstr.
98  */
99  URI(uri_identifier_code_t id, const Span<const uint8_t> &uri_field);
100 
101  /**
102  * Construct a URI from another URI.
103  * @param to_copy The uri copied.
104  */
105  URI(const URI &to_copy);
106 
107  /**
108  * Destroy a URI object.
109  */
110  ~URI();
111 
112  /**
113  * Replace the content by the one of an existing URI.
114  * @param to_copy The URI to copy.
115  * @return a reference to this object
116  */
117  URI &operator=(const URI &to_copy);
118 
119  /**
120  * Replace the value of the URI.
121  *
122  * @param id The ID of the URI
123  * @param uri_field A buffer containing the value of the URI field.
124  *
125  * @note To remove the NULL terminator of the C-string of the uri_field
126  * parameter, you can use the utility function span_from_cstr.
127  */
128  void set_uri(
130  const Span<const uint8_t> &uri_field
131  );
132 
133  /**
134  * Return the id of the uri.
135  * @return The id of the uri.
136  */
138 
139  /**
140  * Return the current value of the uri field.
141  * @return The value of the uri field.
142  */
144 
145  /**
146  * Append into a message builder
147  */
148  bool append_as_record(
149  MessageBuilder &message_builder,
150  bool is_last_record = false
151  ) const;
152 
153  /**
154  * Compute the size of this object in a ndef record.
155  *
156  * @return The size of the ndef record required to store this object.
157  */
158  size_t get_record_size() const;
159 
160  /**
161  * Equal operator between two URIs
162  * @param lhs The URI on the left hand side
163  * @param rhs The URI on the right hand side
164  * @return true if lhs equals rhs or false.
165  */
166  friend bool operator==(const URI &lhs, const URI &rhs)
167  {
168  if (lhs._uri_size != rhs._uri_size) {
169  return false;
170  }
171 
172  return memcmp(lhs._uri, rhs._uri, lhs._uri_size) == 0;
173  }
174 
175  friend bool operator!=(const URI &lhs, const URI &rhs)
176  {
177  return !(lhs == rhs);
178  }
179 
180 private:
181  friend class URIParser;
182 
183  void move_data(uint8_t *text, size_t size);
184 
185  uint8_t *_uri;
186  size_t _uri_size;
187 };
188 
189 /**
190  * Parser of a URI.
191  */
192 class URIParser : public GenericRecordParser<URIParser, URI> {
193 public:
194  bool do_parse(const Record &record, URI &uri);
195 };
196 
197 /**
198  * @}
199  */
200 
201 } // namespace common
202 } // namespace ndef
203 } // namespace nfc
204 } // namespace mbed
205 
206 #endif /* NFC_COMMON_URI_H_ */
Construct a NDEF Message.
bool append_as_record(MessageBuilder &message_builder, bool is_last_record=false) const
Append into a message builder.
Model the well known type URI.
Definition: URI.h:41
Span< const uint8_t > get_uri_field() const
Return the current value of the uri field.
size_t get_record_size() const
Compute the size of this object in a ndef record.
ftp://anonymous:anonymous@
Definition: URI.h:55
void set_uri(uri_identifier_code_t id, const Span< const uint8_t > &uri_field)
Replace the value of the URI.
Represent a record.
Definition: Record.h:148
friend bool operator==(const URI &lhs, const URI &rhs)
Equal operator between two URIs.
Definition: URI.h:166
Parser of a URI.
Definition: URI.h:192
URI & operator=(const URI &to_copy)
Replace the content by the one of an existing URI.
uri_identifier_code_t
Identifier codes.
Definition: URI.h:46
URI()
Construct an empty URI object.
uri_identifier_code_t get_id() const
Return the id of the uri.
~URI()
Destroy a URI object.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.