ST / NDefLib

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of NDefLib by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordURI.h Source File

RecordURI.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    RecordURI.h
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   {@link Record} that contains an URI address
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 #ifndef NDEFLIB_RECORDTYPE_RECORDURI_H_
00039 #define NDEFLIB_RECORDTYPE_RECORDURI_H_
00040 #include <map>
00041 #include <string>
00042 #include <NDefLib/Record.h>
00043 
00044 namespace NDefLib {
00045 
00046 /**
00047  * {@link Record} that contains an URI address.
00048  */
00049 class RecordURI: public Record {
00050 
00051 public:
00052 
00053     /**
00054      * Load a record URI from a buffer.
00055      * @param header Record header.
00056      * @param buffer Buffer to read the tag playload from.
00057      * @return record or NULL if it was not possible build it
00058      * @par User is in charge of freeing the pointer returned by this function.
00059      */
00060     static RecordURI* parse(const RecordHeader &header,
00061             const uint8_t *buffer);
00062 
00063     /**
00064      * If you want encode an know URI you can use this define to
00065      * avoid to encode the URI type as string
00066      */
00067     typedef enum {
00068         UNKNOWN = 0X00,     //!< UNKNOWN
00069         HTTP_WWW = 0X01,    //!< HTTP_WWW
00070         HTTPS_WWW = 0X02,   //!< HTTPS_WWW
00071         HTTP = 0X03,        //!< HTTP
00072         HTTPS = 0X04,       //!< HTTPS
00073         TEL = 0x05,         //!< TEL
00074         MAIL = 0X06,        //!< MAIL
00075         FTP_ANONIMUS = 0X07,//!< FTP_ANONIMUS
00076         FTP_FTP = 0X08,     //!< FTP_FTP
00077         FTPS = 0X09,        //!< FTPS
00078         SFTP = 0X0A,        //!< SFTP
00079         SMB = 0X0B,         //!< SMB
00080         NFS = 0X0C,         //!< NFS
00081         FTP = 0X0d,         //!< FTP
00082         DAV = 0X0E,         //!< DAV
00083         NEWS = 0X0F,        //!< NEWS
00084         TELNET = 0X10,      //!< TELNET
00085         IMAP = 0X11,        //!< IMAP
00086         RTSP = 0X12,        //!< RTSP
00087         URN = 0X13,         //!< URN
00088         POP = 0X14,         //!< POP
00089         SIP = 0X15,         //!< SIP
00090         SIPS = 0X016,       //!< SIPS
00091         TFTP = 0X017,       //!< TFTP
00092         BTSPP = 0x018,      //!< BTSPP
00093         BTL2CAP = 0x019,    //!< BTL2CAP
00094         BTGOEP = 0X01A,     //!< BTGOEP
00095         TCPOBEX = 0X1B,     //!< TCPOBEX
00096         IRDAOBEX = 0X1C,    //!< IRDAOBEX
00097         FILE = 0X1D,        //!< FILE
00098         URN_EPC_ID = 0X1E,  //!< URN_EPC_ID
00099         URN_EPC_TAG = 0X1F, //!< URN_EPC_TAG
00100         URN_EPC_PAT = 0X20, //!< URN_EPC_PAT
00101         URN_EPC_RAW = 0X21, //!< URN_EPC_RAW
00102         URN_EPC = 0X22,     //!< URN_EPC
00103         URN_NFC = 0X23      //!< URN_NFC
00104     } knowUriId_t;
00105 
00106     /**
00107      * Build RecordUri with a defined URI type.
00108      * @param URIId URI type.
00109      * @param URIContent URI content.
00110      * @par The URI prefix is removed.
00111      * @par The string is copied inside the class.
00112      */
00113     explicit RecordURI(knowUriId_t URIId, const std::string &URIContent="");
00114 
00115     /**
00116      * Build a custom URI type.
00117      * @param URIType string with the URI type.
00118      * @param URIContent URI content.
00119      */
00120     explicit RecordURI(const std::string &URIType, const std::string &URIContent="");
00121 
00122     /**
00123      * Get the record type.
00124      * @return TYPE_URI
00125      */
00126     virtual RecordType_t get_type() const {
00127         return TYPE_URI;
00128     } //getType
00129 
00130     /**
00131      * Returns the URI type.
00132      * @return URI type inside this record, UNKNOWN if unknown
00133      */
00134     knowUriId_t get_uri_id() const {
00135         return mUriTypeId;
00136     }
00137 
00138     virtual uint16_t get_byte_length() {
00139         update_content_and_header();
00140         return mRecordHeader.get_record_length();
00141     }
00142 
00143     /**
00144      * Returns the URI content.
00145      * @return URI content
00146      */
00147     std::string& get_content() {
00148         update_content_and_header();
00149         return mContent;
00150     }
00151 
00152     /**
00153      * Change the URI content.
00154      * @param URI new URI content.
00155      */
00156     void set_content(const std::string &URI){
00157         if(mUriTypeId!=UNKNOWN)
00158             store_removeing_prefix(sKnowUriPrefix[mUriTypeId],URI);
00159         else
00160             mContent=URI;
00161         update_content_and_header();
00162     }
00163 
00164     /**
00165      * If the URI type Id is {@code UNKNOWN} this return the user
00166      * URI type.
00167      * @return URI type set by the user or an empyt string
00168      */
00169     const std::string& get_uri_type() const {
00170         return mTypeString;
00171     }
00172 
00173     bool operator==(const RecordURI &other) const {
00174         return  (mUriTypeId==other.mUriTypeId) &&
00175                 (mTypeString==other.mTypeString) &&
00176                 (mContent==other.mContent);
00177     }
00178 
00179     virtual uint16_t write(uint8_t *buffer);
00180     virtual ~RecordURI() {
00181     };
00182 
00183 protected:
00184 
00185     /**
00186      * Record id to write to be recognizable as an URI record.
00187      */
00188     static const uint8_t sNDEFUriIdCode;
00189 
00190     /**
00191      * A subclass must implement this function to store the tag content and update the mContent
00192      * variable accordingly.
00193      * @par You should not call this function directly but use updateContentAndHeader that keeps the
00194      * header information in sync with the content.
00195      */
00196     virtual void update_content(){};
00197 
00198     /**
00199      * Update the tag content and update the header with the new content size.
00200      */
00201     void update_content_and_header(){
00202         update_content();
00203         update_record_header();
00204     }
00205 
00206     /**
00207      * The subclass must store in this variable the content to write to the tag.
00208      * This class will ask to update the content throught the updateContent callback.
00209      */
00210     std::string mContent;
00211 
00212 private:
00213 
00214     /**
00215      * Set the record header flags.
00216      */
00217     void set_record_header();
00218 
00219     /**
00220      * Set the correct size of the payload.
00221      */
00222     void update_record_header(){
00223         //+1 = size of the URITypeId
00224         mRecordHeader.set_payload_length(1 + mTypeString.size() + mContent.size());
00225     }
00226 
00227     void store_removeing_prefix(const std::string &prefix,const std::string &content){
00228         //check that the content doens't contain the prefix
00229         if (content.compare(0, prefix.size(), prefix) == 0) {
00230             mContent = std::string(content, prefix.size());
00231         } else
00232             mContent = content;
00233     }
00234 
00235     /**
00236      * URI type used by this record
00237      */
00238     const knowUriId_t mUriTypeId;
00239 
00240     /**
00241      * In case of unknown URI type, it stores the used defined URI type.
00242      */
00243     const std::string mTypeString;
00244 
00245 
00246     /**
00247      * Array of known prefix of known URI type
00248      */
00249     static const std::string sKnowUriPrefix[];
00250 };
00251 
00252 } /* namespace NDefLib */
00253 
00254 #endif /* NDEFLIB_RECORDTYPE_RECORDURI_H_ */