CUSTOMIZED FOR WATER MONITOR
RecordURI.h@0:51d7bdf88981, 2017-12-07 (annotated)
- Committer:
- DuyLionTran
- Date:
- Thu Dec 07 17:07:22 2017 +0000
- Revision:
- 0:51d7bdf88981
CUSTOMIZED FOR WATER MONITOR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuyLionTran | 0:51d7bdf88981 | 1 | /** |
DuyLionTran | 0:51d7bdf88981 | 2 | ****************************************************************************** |
DuyLionTran | 0:51d7bdf88981 | 3 | * @file RecordURI.h |
DuyLionTran | 0:51d7bdf88981 | 4 | * @author ST / Central Labs |
DuyLionTran | 0:51d7bdf88981 | 5 | * @version V2.0.0 |
DuyLionTran | 0:51d7bdf88981 | 6 | * @date 28 Apr 2017 |
DuyLionTran | 0:51d7bdf88981 | 7 | * @brief {@link Record} that contains an URI address |
DuyLionTran | 0:51d7bdf88981 | 8 | ****************************************************************************** |
DuyLionTran | 0:51d7bdf88981 | 9 | * @attention |
DuyLionTran | 0:51d7bdf88981 | 10 | * |
DuyLionTran | 0:51d7bdf88981 | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
DuyLionTran | 0:51d7bdf88981 | 12 | * |
DuyLionTran | 0:51d7bdf88981 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
DuyLionTran | 0:51d7bdf88981 | 14 | * are permitted provided that the following conditions are met: |
DuyLionTran | 0:51d7bdf88981 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
DuyLionTran | 0:51d7bdf88981 | 16 | * this list of conditions and the following disclaimer. |
DuyLionTran | 0:51d7bdf88981 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
DuyLionTran | 0:51d7bdf88981 | 18 | * this list of conditions and the following disclaimer in the documentation |
DuyLionTran | 0:51d7bdf88981 | 19 | * and/or other materials provided with the distribution. |
DuyLionTran | 0:51d7bdf88981 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
DuyLionTran | 0:51d7bdf88981 | 21 | * may be used to endorse or promote products derived from this software |
DuyLionTran | 0:51d7bdf88981 | 22 | * without specific prior written permission. |
DuyLionTran | 0:51d7bdf88981 | 23 | * |
DuyLionTran | 0:51d7bdf88981 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
DuyLionTran | 0:51d7bdf88981 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
DuyLionTran | 0:51d7bdf88981 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
DuyLionTran | 0:51d7bdf88981 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
DuyLionTran | 0:51d7bdf88981 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
DuyLionTran | 0:51d7bdf88981 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
DuyLionTran | 0:51d7bdf88981 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
DuyLionTran | 0:51d7bdf88981 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
DuyLionTran | 0:51d7bdf88981 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
DuyLionTran | 0:51d7bdf88981 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
DuyLionTran | 0:51d7bdf88981 | 34 | * |
DuyLionTran | 0:51d7bdf88981 | 35 | ****************************************************************************** |
DuyLionTran | 0:51d7bdf88981 | 36 | */ |
DuyLionTran | 0:51d7bdf88981 | 37 | |
DuyLionTran | 0:51d7bdf88981 | 38 | #ifndef NDEFLIB_RECORDTYPE_RECORDURI_H_ |
DuyLionTran | 0:51d7bdf88981 | 39 | #define NDEFLIB_RECORDTYPE_RECORDURI_H_ |
DuyLionTran | 0:51d7bdf88981 | 40 | #include <map> |
DuyLionTran | 0:51d7bdf88981 | 41 | #include <string> |
DuyLionTran | 0:51d7bdf88981 | 42 | #include <NDefLib/Record.h> |
DuyLionTran | 0:51d7bdf88981 | 43 | |
DuyLionTran | 0:51d7bdf88981 | 44 | namespace NDefLib { |
DuyLionTran | 0:51d7bdf88981 | 45 | |
DuyLionTran | 0:51d7bdf88981 | 46 | /** |
DuyLionTran | 0:51d7bdf88981 | 47 | * {@link Record} that contains an URI address. |
DuyLionTran | 0:51d7bdf88981 | 48 | */ |
DuyLionTran | 0:51d7bdf88981 | 49 | class RecordURI: public Record { |
DuyLionTran | 0:51d7bdf88981 | 50 | |
DuyLionTran | 0:51d7bdf88981 | 51 | public: |
DuyLionTran | 0:51d7bdf88981 | 52 | |
DuyLionTran | 0:51d7bdf88981 | 53 | /** |
DuyLionTran | 0:51d7bdf88981 | 54 | * Load a record URI from a buffer. |
DuyLionTran | 0:51d7bdf88981 | 55 | * @param header Record header. |
DuyLionTran | 0:51d7bdf88981 | 56 | * @param buffer Buffer to read the tag playload from. |
DuyLionTran | 0:51d7bdf88981 | 57 | * @return record or NULL if it was not possible build it |
DuyLionTran | 0:51d7bdf88981 | 58 | * @par User is in charge of freeing the pointer returned by this function. |
DuyLionTran | 0:51d7bdf88981 | 59 | */ |
DuyLionTran | 0:51d7bdf88981 | 60 | static RecordURI* parse(const RecordHeader &header, |
DuyLionTran | 0:51d7bdf88981 | 61 | const uint8_t *buffer); |
DuyLionTran | 0:51d7bdf88981 | 62 | |
DuyLionTran | 0:51d7bdf88981 | 63 | /** |
DuyLionTran | 0:51d7bdf88981 | 64 | * If you want encode an know URI you can use this define to |
DuyLionTran | 0:51d7bdf88981 | 65 | * avoid to encode the URI type as string |
DuyLionTran | 0:51d7bdf88981 | 66 | */ |
DuyLionTran | 0:51d7bdf88981 | 67 | typedef enum { |
DuyLionTran | 0:51d7bdf88981 | 68 | UNKNOWN = 0X00, //!< UNKNOWN |
DuyLionTran | 0:51d7bdf88981 | 69 | HTTP_WWW = 0X01, //!< HTTP_WWW |
DuyLionTran | 0:51d7bdf88981 | 70 | HTTPS_WWW = 0X02, //!< HTTPS_WWW |
DuyLionTran | 0:51d7bdf88981 | 71 | HTTP = 0X03, //!< HTTP |
DuyLionTran | 0:51d7bdf88981 | 72 | HTTPS = 0X04, //!< HTTPS |
DuyLionTran | 0:51d7bdf88981 | 73 | TEL = 0x05, //!< TEL |
DuyLionTran | 0:51d7bdf88981 | 74 | MAIL = 0X06, //!< MAIL |
DuyLionTran | 0:51d7bdf88981 | 75 | FTP_ANONIMUS = 0X07,//!< FTP_ANONIMUS |
DuyLionTran | 0:51d7bdf88981 | 76 | FTP_FTP = 0X08, //!< FTP_FTP |
DuyLionTran | 0:51d7bdf88981 | 77 | FTPS = 0X09, //!< FTPS |
DuyLionTran | 0:51d7bdf88981 | 78 | SFTP = 0X0A, //!< SFTP |
DuyLionTran | 0:51d7bdf88981 | 79 | SMB = 0X0B, //!< SMB |
DuyLionTran | 0:51d7bdf88981 | 80 | NFS = 0X0C, //!< NFS |
DuyLionTran | 0:51d7bdf88981 | 81 | FTP = 0X0d, //!< FTP |
DuyLionTran | 0:51d7bdf88981 | 82 | DAV = 0X0E, //!< DAV |
DuyLionTran | 0:51d7bdf88981 | 83 | NEWS = 0X0F, //!< NEWS |
DuyLionTran | 0:51d7bdf88981 | 84 | TELNET = 0X10, //!< TELNET |
DuyLionTran | 0:51d7bdf88981 | 85 | IMAP = 0X11, //!< IMAP |
DuyLionTran | 0:51d7bdf88981 | 86 | RTSP = 0X12, //!< RTSP |
DuyLionTran | 0:51d7bdf88981 | 87 | URN = 0X13, //!< URN |
DuyLionTran | 0:51d7bdf88981 | 88 | POP = 0X14, //!< POP |
DuyLionTran | 0:51d7bdf88981 | 89 | SIP = 0X15, //!< SIP |
DuyLionTran | 0:51d7bdf88981 | 90 | SIPS = 0X016, //!< SIPS |
DuyLionTran | 0:51d7bdf88981 | 91 | TFTP = 0X017, //!< TFTP |
DuyLionTran | 0:51d7bdf88981 | 92 | BTSPP = 0x018, //!< BTSPP |
DuyLionTran | 0:51d7bdf88981 | 93 | BTL2CAP = 0x019, //!< BTL2CAP |
DuyLionTran | 0:51d7bdf88981 | 94 | BTGOEP = 0X01A, //!< BTGOEP |
DuyLionTran | 0:51d7bdf88981 | 95 | TCPOBEX = 0X1B, //!< TCPOBEX |
DuyLionTran | 0:51d7bdf88981 | 96 | IRDAOBEX = 0X1C, //!< IRDAOBEX |
DuyLionTran | 0:51d7bdf88981 | 97 | FILE = 0X1D, //!< FILE |
DuyLionTran | 0:51d7bdf88981 | 98 | URN_EPC_ID = 0X1E, //!< URN_EPC_ID |
DuyLionTran | 0:51d7bdf88981 | 99 | URN_EPC_TAG = 0X1F, //!< URN_EPC_TAG |
DuyLionTran | 0:51d7bdf88981 | 100 | URN_EPC_PAT = 0X20, //!< URN_EPC_PAT |
DuyLionTran | 0:51d7bdf88981 | 101 | URN_EPC_RAW = 0X21, //!< URN_EPC_RAW |
DuyLionTran | 0:51d7bdf88981 | 102 | URN_EPC = 0X22, //!< URN_EPC |
DuyLionTran | 0:51d7bdf88981 | 103 | URN_NFC = 0X23 //!< URN_NFC |
DuyLionTran | 0:51d7bdf88981 | 104 | } knowUriId_t; |
DuyLionTran | 0:51d7bdf88981 | 105 | |
DuyLionTran | 0:51d7bdf88981 | 106 | /** |
DuyLionTran | 0:51d7bdf88981 | 107 | * Build RecordUri with a defined URI type. |
DuyLionTran | 0:51d7bdf88981 | 108 | * @param URIId URI type. |
DuyLionTran | 0:51d7bdf88981 | 109 | * @param URIContent URI content. |
DuyLionTran | 0:51d7bdf88981 | 110 | * @par The URI prefix is removed. |
DuyLionTran | 0:51d7bdf88981 | 111 | * @par The string is copied inside the class. |
DuyLionTran | 0:51d7bdf88981 | 112 | */ |
DuyLionTran | 0:51d7bdf88981 | 113 | explicit RecordURI(knowUriId_t URIId, const std::string &URIContent=""); |
DuyLionTran | 0:51d7bdf88981 | 114 | |
DuyLionTran | 0:51d7bdf88981 | 115 | /** |
DuyLionTran | 0:51d7bdf88981 | 116 | * Build a custom URI type. |
DuyLionTran | 0:51d7bdf88981 | 117 | * @param URIType string with the URI type. |
DuyLionTran | 0:51d7bdf88981 | 118 | * @param URIContent URI content. |
DuyLionTran | 0:51d7bdf88981 | 119 | */ |
DuyLionTran | 0:51d7bdf88981 | 120 | explicit RecordURI(const std::string &URIType, const std::string &URIContent=""); |
DuyLionTran | 0:51d7bdf88981 | 121 | |
DuyLionTran | 0:51d7bdf88981 | 122 | /** |
DuyLionTran | 0:51d7bdf88981 | 123 | * Get the record type. |
DuyLionTran | 0:51d7bdf88981 | 124 | * @return TYPE_URI |
DuyLionTran | 0:51d7bdf88981 | 125 | */ |
DuyLionTran | 0:51d7bdf88981 | 126 | virtual RecordType_t get_type() const { |
DuyLionTran | 0:51d7bdf88981 | 127 | return TYPE_URI; |
DuyLionTran | 0:51d7bdf88981 | 128 | } //getType |
DuyLionTran | 0:51d7bdf88981 | 129 | |
DuyLionTran | 0:51d7bdf88981 | 130 | /** |
DuyLionTran | 0:51d7bdf88981 | 131 | * Returns the URI type. |
DuyLionTran | 0:51d7bdf88981 | 132 | * @return URI type inside this record, UNKNOWN if unknown |
DuyLionTran | 0:51d7bdf88981 | 133 | */ |
DuyLionTran | 0:51d7bdf88981 | 134 | knowUriId_t get_uri_id() const { |
DuyLionTran | 0:51d7bdf88981 | 135 | return mUriTypeId; |
DuyLionTran | 0:51d7bdf88981 | 136 | } |
DuyLionTran | 0:51d7bdf88981 | 137 | |
DuyLionTran | 0:51d7bdf88981 | 138 | virtual uint16_t get_byte_length() { |
DuyLionTran | 0:51d7bdf88981 | 139 | update_content_and_header(); |
DuyLionTran | 0:51d7bdf88981 | 140 | return mRecordHeader.get_record_length(); |
DuyLionTran | 0:51d7bdf88981 | 141 | } |
DuyLionTran | 0:51d7bdf88981 | 142 | |
DuyLionTran | 0:51d7bdf88981 | 143 | /** |
DuyLionTran | 0:51d7bdf88981 | 144 | * Returns the URI content. |
DuyLionTran | 0:51d7bdf88981 | 145 | * @return URI content |
DuyLionTran | 0:51d7bdf88981 | 146 | */ |
DuyLionTran | 0:51d7bdf88981 | 147 | std::string& get_content() { |
DuyLionTran | 0:51d7bdf88981 | 148 | update_content_and_header(); |
DuyLionTran | 0:51d7bdf88981 | 149 | return mContent; |
DuyLionTran | 0:51d7bdf88981 | 150 | } |
DuyLionTran | 0:51d7bdf88981 | 151 | |
DuyLionTran | 0:51d7bdf88981 | 152 | /** |
DuyLionTran | 0:51d7bdf88981 | 153 | * Change the URI content. |
DuyLionTran | 0:51d7bdf88981 | 154 | * @param URI new URI content. |
DuyLionTran | 0:51d7bdf88981 | 155 | */ |
DuyLionTran | 0:51d7bdf88981 | 156 | void set_content(const std::string &URI){ |
DuyLionTran | 0:51d7bdf88981 | 157 | if(mUriTypeId!=UNKNOWN) |
DuyLionTran | 0:51d7bdf88981 | 158 | store_removeing_prefix(sKnowUriPrefix[mUriTypeId],URI); |
DuyLionTran | 0:51d7bdf88981 | 159 | else |
DuyLionTran | 0:51d7bdf88981 | 160 | mContent=URI; |
DuyLionTran | 0:51d7bdf88981 | 161 | update_content_and_header(); |
DuyLionTran | 0:51d7bdf88981 | 162 | } |
DuyLionTran | 0:51d7bdf88981 | 163 | |
DuyLionTran | 0:51d7bdf88981 | 164 | /** |
DuyLionTran | 0:51d7bdf88981 | 165 | * If the URI type Id is {@code UNKNOWN} this return the user |
DuyLionTran | 0:51d7bdf88981 | 166 | * URI type. |
DuyLionTran | 0:51d7bdf88981 | 167 | * @return URI type set by the user or an empyt string |
DuyLionTran | 0:51d7bdf88981 | 168 | */ |
DuyLionTran | 0:51d7bdf88981 | 169 | const std::string& get_uri_type() const { |
DuyLionTran | 0:51d7bdf88981 | 170 | return mTypeString; |
DuyLionTran | 0:51d7bdf88981 | 171 | } |
DuyLionTran | 0:51d7bdf88981 | 172 | |
DuyLionTran | 0:51d7bdf88981 | 173 | bool operator==(const RecordURI &other) const { |
DuyLionTran | 0:51d7bdf88981 | 174 | return (mUriTypeId==other.mUriTypeId) && |
DuyLionTran | 0:51d7bdf88981 | 175 | (mTypeString==other.mTypeString) && |
DuyLionTran | 0:51d7bdf88981 | 176 | (mContent==other.mContent); |
DuyLionTran | 0:51d7bdf88981 | 177 | } |
DuyLionTran | 0:51d7bdf88981 | 178 | |
DuyLionTran | 0:51d7bdf88981 | 179 | virtual uint16_t write(uint8_t *buffer); |
DuyLionTran | 0:51d7bdf88981 | 180 | virtual ~RecordURI() { |
DuyLionTran | 0:51d7bdf88981 | 181 | }; |
DuyLionTran | 0:51d7bdf88981 | 182 | |
DuyLionTran | 0:51d7bdf88981 | 183 | protected: |
DuyLionTran | 0:51d7bdf88981 | 184 | |
DuyLionTran | 0:51d7bdf88981 | 185 | /** |
DuyLionTran | 0:51d7bdf88981 | 186 | * Record id to write to be recognizable as an URI record. |
DuyLionTran | 0:51d7bdf88981 | 187 | */ |
DuyLionTran | 0:51d7bdf88981 | 188 | static const uint8_t sNDEFUriIdCode; |
DuyLionTran | 0:51d7bdf88981 | 189 | |
DuyLionTran | 0:51d7bdf88981 | 190 | /** |
DuyLionTran | 0:51d7bdf88981 | 191 | * A subclass must implement this function to store the tag content and update the mContent |
DuyLionTran | 0:51d7bdf88981 | 192 | * variable accordingly. |
DuyLionTran | 0:51d7bdf88981 | 193 | * @par You should not call this function directly but use updateContentAndHeader that keeps the |
DuyLionTran | 0:51d7bdf88981 | 194 | * header information in sync with the content. |
DuyLionTran | 0:51d7bdf88981 | 195 | */ |
DuyLionTran | 0:51d7bdf88981 | 196 | virtual void update_content(){}; |
DuyLionTran | 0:51d7bdf88981 | 197 | |
DuyLionTran | 0:51d7bdf88981 | 198 | /** |
DuyLionTran | 0:51d7bdf88981 | 199 | * Update the tag content and update the header with the new content size. |
DuyLionTran | 0:51d7bdf88981 | 200 | */ |
DuyLionTran | 0:51d7bdf88981 | 201 | void update_content_and_header(){ |
DuyLionTran | 0:51d7bdf88981 | 202 | update_content(); |
DuyLionTran | 0:51d7bdf88981 | 203 | update_record_header(); |
DuyLionTran | 0:51d7bdf88981 | 204 | } |
DuyLionTran | 0:51d7bdf88981 | 205 | |
DuyLionTran | 0:51d7bdf88981 | 206 | /** |
DuyLionTran | 0:51d7bdf88981 | 207 | * The subclass must store in this variable the content to write to the tag. |
DuyLionTran | 0:51d7bdf88981 | 208 | * This class will ask to update the content throught the updateContent callback. |
DuyLionTran | 0:51d7bdf88981 | 209 | */ |
DuyLionTran | 0:51d7bdf88981 | 210 | std::string mContent; |
DuyLionTran | 0:51d7bdf88981 | 211 | |
DuyLionTran | 0:51d7bdf88981 | 212 | private: |
DuyLionTran | 0:51d7bdf88981 | 213 | |
DuyLionTran | 0:51d7bdf88981 | 214 | /** |
DuyLionTran | 0:51d7bdf88981 | 215 | * Set the record header flags. |
DuyLionTran | 0:51d7bdf88981 | 216 | */ |
DuyLionTran | 0:51d7bdf88981 | 217 | void set_record_header(); |
DuyLionTran | 0:51d7bdf88981 | 218 | |
DuyLionTran | 0:51d7bdf88981 | 219 | /** |
DuyLionTran | 0:51d7bdf88981 | 220 | * Set the correct size of the payload. |
DuyLionTran | 0:51d7bdf88981 | 221 | */ |
DuyLionTran | 0:51d7bdf88981 | 222 | void update_record_header(){ |
DuyLionTran | 0:51d7bdf88981 | 223 | //+1 = size of the URITypeId |
DuyLionTran | 0:51d7bdf88981 | 224 | mRecordHeader.set_payload_length(1 + mTypeString.size() + mContent.size()); |
DuyLionTran | 0:51d7bdf88981 | 225 | } |
DuyLionTran | 0:51d7bdf88981 | 226 | |
DuyLionTran | 0:51d7bdf88981 | 227 | void store_removeing_prefix(const std::string &prefix,const std::string &content){ |
DuyLionTran | 0:51d7bdf88981 | 228 | //check that the content doens't contain the prefix |
DuyLionTran | 0:51d7bdf88981 | 229 | if (content.compare(0, prefix.size(), prefix) == 0) { |
DuyLionTran | 0:51d7bdf88981 | 230 | mContent = std::string(content, prefix.size()); |
DuyLionTran | 0:51d7bdf88981 | 231 | } else |
DuyLionTran | 0:51d7bdf88981 | 232 | mContent = content; |
DuyLionTran | 0:51d7bdf88981 | 233 | } |
DuyLionTran | 0:51d7bdf88981 | 234 | |
DuyLionTran | 0:51d7bdf88981 | 235 | /** |
DuyLionTran | 0:51d7bdf88981 | 236 | * URI type used by this record |
DuyLionTran | 0:51d7bdf88981 | 237 | */ |
DuyLionTran | 0:51d7bdf88981 | 238 | const knowUriId_t mUriTypeId; |
DuyLionTran | 0:51d7bdf88981 | 239 | |
DuyLionTran | 0:51d7bdf88981 | 240 | /** |
DuyLionTran | 0:51d7bdf88981 | 241 | * In case of unknown URI type, it stores the used defined URI type. |
DuyLionTran | 0:51d7bdf88981 | 242 | */ |
DuyLionTran | 0:51d7bdf88981 | 243 | const std::string mTypeString; |
DuyLionTran | 0:51d7bdf88981 | 244 | |
DuyLionTran | 0:51d7bdf88981 | 245 | |
DuyLionTran | 0:51d7bdf88981 | 246 | /** |
DuyLionTran | 0:51d7bdf88981 | 247 | * Array of known prefix of known URI type |
DuyLionTran | 0:51d7bdf88981 | 248 | */ |
DuyLionTran | 0:51d7bdf88981 | 249 | static const std::string sKnowUriPrefix[]; |
DuyLionTran | 0:51d7bdf88981 | 250 | }; |
DuyLionTran | 0:51d7bdf88981 | 251 | |
DuyLionTran | 0:51d7bdf88981 | 252 | } /* namespace NDefLib */ |
DuyLionTran | 0:51d7bdf88981 | 253 | |
DuyLionTran | 0:51d7bdf88981 | 254 | #endif /* NDEFLIB_RECORDTYPE_RECORDURI_H_ */ |