CUSTOMIZED FOR WATER MONITOR
RecordMimeType.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 RecordMimeType.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 data encoded as MimeType. |
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_RECORDMIMETYPE_H_ |
DuyLionTran | 0:51d7bdf88981 | 39 | #define NDEFLIB_RECORDTYPE_RECORDMIMETYPE_H_ |
DuyLionTran | 0:51d7bdf88981 | 40 | |
DuyLionTran | 0:51d7bdf88981 | 41 | #include <cstring> |
DuyLionTran | 0:51d7bdf88981 | 42 | #include <string> |
DuyLionTran | 0:51d7bdf88981 | 43 | |
DuyLionTran | 0:51d7bdf88981 | 44 | #include "NDefLib/Record.h" |
DuyLionTran | 0:51d7bdf88981 | 45 | |
DuyLionTran | 0:51d7bdf88981 | 46 | namespace NDefLib { |
DuyLionTran | 0:51d7bdf88981 | 47 | |
DuyLionTran | 0:51d7bdf88981 | 48 | /** |
DuyLionTran | 0:51d7bdf88981 | 49 | * {@link Record} that contains data encoded as MimeType. |
DuyLionTran | 0:51d7bdf88981 | 50 | */ |
DuyLionTran | 0:51d7bdf88981 | 51 | class RecordMimeType: public Record { |
DuyLionTran | 0:51d7bdf88981 | 52 | public: |
DuyLionTran | 0:51d7bdf88981 | 53 | |
DuyLionTran | 0:51d7bdf88981 | 54 | /** |
DuyLionTran | 0:51d7bdf88981 | 55 | * Create an RecordMimeType reading the data from the buffer. |
DuyLionTran | 0:51d7bdf88981 | 56 | * @param header Record header. |
DuyLionTran | 0:51d7bdf88981 | 57 | * @param buffer Buffer to read the data from. |
DuyLionTran | 0:51d7bdf88981 | 58 | * @return an object of type RecordMimeType or NULL |
DuyLionTran | 0:51d7bdf88981 | 59 | * @par User is in charge of freeing the pointer returned by this function. |
DuyLionTran | 0:51d7bdf88981 | 60 | */ |
DuyLionTran | 0:51d7bdf88981 | 61 | static RecordMimeType* parse(const RecordHeader &header, |
DuyLionTran | 0:51d7bdf88981 | 62 | const uint8_t* buffer); |
DuyLionTran | 0:51d7bdf88981 | 63 | |
DuyLionTran | 0:51d7bdf88981 | 64 | /** |
DuyLionTran | 0:51d7bdf88981 | 65 | * Create a new record with a specific MIME type. |
DuyLionTran | 0:51d7bdf88981 | 66 | * @param mimeType MIME type. |
DuyLionTran | 0:51d7bdf88981 | 67 | * @param data Content. |
DuyLionTran | 0:51d7bdf88981 | 68 | * @param nDataLenght Content length in bytes. |
DuyLionTran | 0:51d7bdf88981 | 69 | * @par Data content is copied inside the class, unless NULL pointer is passed and |
DuyLionTran | 0:51d7bdf88981 | 70 | * @{link setMimeDataPointer} is used to set the data pointer. |
DuyLionTran | 0:51d7bdf88981 | 71 | */ |
DuyLionTran | 0:51d7bdf88981 | 72 | RecordMimeType(const std::string &mimeType, const uint8_t *data=NULL, |
DuyLionTran | 0:51d7bdf88981 | 73 | uint32_t nDataLenght=0); |
DuyLionTran | 0:51d7bdf88981 | 74 | |
DuyLionTran | 0:51d7bdf88981 | 75 | /** |
DuyLionTran | 0:51d7bdf88981 | 76 | * Create a new RecordMimeType. |
DuyLionTran | 0:51d7bdf88981 | 77 | * @param mimeType MIME type. |
DuyLionTran | 0:51d7bdf88981 | 78 | * @param data String that contains the data. |
DuyLionTran | 0:51d7bdf88981 | 79 | * @par The string is copied inside the class. |
DuyLionTran | 0:51d7bdf88981 | 80 | */ |
DuyLionTran | 0:51d7bdf88981 | 81 | RecordMimeType(const std::string &mimeType, const std::string &data); |
DuyLionTran | 0:51d7bdf88981 | 82 | |
DuyLionTran | 0:51d7bdf88981 | 83 | /** |
DuyLionTran | 0:51d7bdf88981 | 84 | * Return the mime type of the content. |
DuyLionTran | 0:51d7bdf88981 | 85 | * @return the mime type of the content. |
DuyLionTran | 0:51d7bdf88981 | 86 | */ |
DuyLionTran | 0:51d7bdf88981 | 87 | const std::string& get_mime_type() const { |
DuyLionTran | 0:51d7bdf88981 | 88 | return mMimeType; |
DuyLionTran | 0:51d7bdf88981 | 89 | } |
DuyLionTran | 0:51d7bdf88981 | 90 | |
DuyLionTran | 0:51d7bdf88981 | 91 | /** |
DuyLionTran | 0:51d7bdf88981 | 92 | * Change the data pointer used by this record. |
DuyLionTran | 0:51d7bdf88981 | 93 | * @param data Pointer used by this record. |
DuyLionTran | 0:51d7bdf88981 | 94 | * @param dataLength Number of byte to write in this record. |
DuyLionTran | 0:51d7bdf88981 | 95 | * @par The buffer is not copied or freed by this class. |
DuyLionTran | 0:51d7bdf88981 | 96 | * With this function only the data pointer is copied, so it must not be freed before the object is deallocated. |
DuyLionTran | 0:51d7bdf88981 | 97 | */ |
DuyLionTran | 0:51d7bdf88981 | 98 | void set_mime_data_pointer(uint8_t* data, uint32_t dataLength){ |
DuyLionTran | 0:51d7bdf88981 | 99 | delete_mime_data(); |
DuyLionTran | 0:51d7bdf88981 | 100 | mData = data; |
DuyLionTran | 0:51d7bdf88981 | 101 | mDataLength=dataLength; |
DuyLionTran | 0:51d7bdf88981 | 102 | mRecordHeader.set_payload_length(dataLength); |
DuyLionTran | 0:51d7bdf88981 | 103 | } |
DuyLionTran | 0:51d7bdf88981 | 104 | |
DuyLionTran | 0:51d7bdf88981 | 105 | /** |
DuyLionTran | 0:51d7bdf88981 | 106 | * Change the data linked with this record, with this function the |
DuyLionTran | 0:51d7bdf88981 | 107 | * data are copied inside the object. |
DuyLionTran | 0:51d7bdf88981 | 108 | * @param data Pointer to the data buffer. |
DuyLionTran | 0:51d7bdf88981 | 109 | * @param dataLength Number of bytes to write. |
DuyLionTran | 0:51d7bdf88981 | 110 | */ |
DuyLionTran | 0:51d7bdf88981 | 111 | void copy_mime_data(const uint8_t* data, uint32_t dataLength); |
DuyLionTran | 0:51d7bdf88981 | 112 | |
DuyLionTran | 0:51d7bdf88981 | 113 | /** |
DuyLionTran | 0:51d7bdf88981 | 114 | * @return number of bytes used to store the content data |
DuyLionTran | 0:51d7bdf88981 | 115 | */ |
DuyLionTran | 0:51d7bdf88981 | 116 | uint32_t get_mime_data_lenght() const { |
DuyLionTran | 0:51d7bdf88981 | 117 | return mDataLength; |
DuyLionTran | 0:51d7bdf88981 | 118 | } |
DuyLionTran | 0:51d7bdf88981 | 119 | |
DuyLionTran | 0:51d7bdf88981 | 120 | /** |
DuyLionTran | 0:51d7bdf88981 | 121 | * @return pointer to the content data |
DuyLionTran | 0:51d7bdf88981 | 122 | * @par DO NOT free this pointer, it is managed by the class. |
DuyLionTran | 0:51d7bdf88981 | 123 | */ |
DuyLionTran | 0:51d7bdf88981 | 124 | uint8_t const* get_mime_data() const { |
DuyLionTran | 0:51d7bdf88981 | 125 | return mData; |
DuyLionTran | 0:51d7bdf88981 | 126 | } |
DuyLionTran | 0:51d7bdf88981 | 127 | |
DuyLionTran | 0:51d7bdf88981 | 128 | /** |
DuyLionTran | 0:51d7bdf88981 | 129 | * Get the record type. |
DuyLionTran | 0:51d7bdf88981 | 130 | * @return TYPE_MIME |
DuyLionTran | 0:51d7bdf88981 | 131 | */ |
DuyLionTran | 0:51d7bdf88981 | 132 | virtual RecordType_t get_type() const { |
DuyLionTran | 0:51d7bdf88981 | 133 | return TYPE_MIME; |
DuyLionTran | 0:51d7bdf88981 | 134 | } //getType |
DuyLionTran | 0:51d7bdf88981 | 135 | |
DuyLionTran | 0:51d7bdf88981 | 136 | |
DuyLionTran | 0:51d7bdf88981 | 137 | virtual uint16_t write(uint8_t *buffer); |
DuyLionTran | 0:51d7bdf88981 | 138 | |
DuyLionTran | 0:51d7bdf88981 | 139 | /** |
DuyLionTran | 0:51d7bdf88981 | 140 | * If needed free the memory used to store the content data. |
DuyLionTran | 0:51d7bdf88981 | 141 | */ |
DuyLionTran | 0:51d7bdf88981 | 142 | virtual ~RecordMimeType() { |
DuyLionTran | 0:51d7bdf88981 | 143 | delete_mime_data(); |
DuyLionTran | 0:51d7bdf88981 | 144 | }; |
DuyLionTran | 0:51d7bdf88981 | 145 | |
DuyLionTran | 0:51d7bdf88981 | 146 | /** |
DuyLionTran | 0:51d7bdf88981 | 147 | * Compare two objects |
DuyLionTran | 0:51d7bdf88981 | 148 | * @return true if the 2 records have the same mime type, length and data. |
DuyLionTran | 0:51d7bdf88981 | 149 | */ |
DuyLionTran | 0:51d7bdf88981 | 150 | bool operator==(const RecordMimeType &other) const { |
DuyLionTran | 0:51d7bdf88981 | 151 | return mMimeType==other.mMimeType && |
DuyLionTran | 0:51d7bdf88981 | 152 | mDataLength == other.mDataLength && |
DuyLionTran | 0:51d7bdf88981 | 153 | (std::memcmp(mData,other.mData,mDataLength)==0); |
DuyLionTran | 0:51d7bdf88981 | 154 | } |
DuyLionTran | 0:51d7bdf88981 | 155 | |
DuyLionTran | 0:51d7bdf88981 | 156 | private: |
DuyLionTran | 0:51d7bdf88981 | 157 | |
DuyLionTran | 0:51d7bdf88981 | 158 | void initialize_header_data(); |
DuyLionTran | 0:51d7bdf88981 | 159 | |
DuyLionTran | 0:51d7bdf88981 | 160 | /** |
DuyLionTran | 0:51d7bdf88981 | 161 | * Delete the mData buffer if it is allocated by this class. |
DuyLionTran | 0:51d7bdf88981 | 162 | */ |
DuyLionTran | 0:51d7bdf88981 | 163 | void delete_mime_data(){ |
DuyLionTran | 0:51d7bdf88981 | 164 | if(mDataToFree && mData!=NULL){ |
DuyLionTran | 0:51d7bdf88981 | 165 | mDataToFree=false; |
DuyLionTran | 0:51d7bdf88981 | 166 | delete [] mData; |
DuyLionTran | 0:51d7bdf88981 | 167 | mData=NULL; |
DuyLionTran | 0:51d7bdf88981 | 168 | }//if |
DuyLionTran | 0:51d7bdf88981 | 169 | }//deleteMimeData |
DuyLionTran | 0:51d7bdf88981 | 170 | |
DuyLionTran | 0:51d7bdf88981 | 171 | const std::string mMimeType; |
DuyLionTran | 0:51d7bdf88981 | 172 | uint32_t mDataLength; |
DuyLionTran | 0:51d7bdf88981 | 173 | uint8_t *mData; |
DuyLionTran | 0:51d7bdf88981 | 174 | bool mDataToFree; |
DuyLionTran | 0:51d7bdf88981 | 175 | |
DuyLionTran | 0:51d7bdf88981 | 176 | }; |
DuyLionTran | 0:51d7bdf88981 | 177 | |
DuyLionTran | 0:51d7bdf88981 | 178 | } /* namespace NDefLib */ |
DuyLionTran | 0:51d7bdf88981 | 179 | |
DuyLionTran | 0:51d7bdf88981 | 180 | #endif /* NDEFLIB_RECORDTYPE_RECORDMIMETYPE_H_ */ |