CUSTOMIZED FOR WATER MONITOR
RecordVCard.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 RecordVCard.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 RecordMimeType} that contains a VCard data |
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_RECORDVCARD_H_ |
DuyLionTran | 0:51d7bdf88981 | 39 | #define NDEFLIB_RECORDTYPE_RECORDVCARD_H_ |
DuyLionTran | 0:51d7bdf88981 | 40 | |
DuyLionTran | 0:51d7bdf88981 | 41 | #include <map> |
DuyLionTran | 0:51d7bdf88981 | 42 | #include <string> |
DuyLionTran | 0:51d7bdf88981 | 43 | #include "RecordMimeType.h" |
DuyLionTran | 0:51d7bdf88981 | 44 | |
DuyLionTran | 0:51d7bdf88981 | 45 | namespace NDefLib { |
DuyLionTran | 0:51d7bdf88981 | 46 | |
DuyLionTran | 0:51d7bdf88981 | 47 | /** |
DuyLionTran | 0:51d7bdf88981 | 48 | * Specialize the {@link RecordMimeType} to store VCard information. |
DuyLionTran | 0:51d7bdf88981 | 49 | * This record handles the VCard version 3 format. |
DuyLionTran | 0:51d7bdf88981 | 50 | * @see https://en.wikipedia.org/wiki/VCard |
DuyLionTran | 0:51d7bdf88981 | 51 | */ |
DuyLionTran | 0:51d7bdf88981 | 52 | class RecordVCard: public RecordMimeType { |
DuyLionTran | 0:51d7bdf88981 | 53 | public: |
DuyLionTran | 0:51d7bdf88981 | 54 | |
DuyLionTran | 0:51d7bdf88981 | 55 | /** |
DuyLionTran | 0:51d7bdf88981 | 56 | * Type of information that you can store inside the tag |
DuyLionTran | 0:51d7bdf88981 | 57 | */ |
DuyLionTran | 0:51d7bdf88981 | 58 | typedef enum { |
DuyLionTran | 0:51d7bdf88981 | 59 | ADDRESS, //!< ADDRESS |
DuyLionTran | 0:51d7bdf88981 | 60 | ADDRESS_HOME, //!< ADDRESS_HOME |
DuyLionTran | 0:51d7bdf88981 | 61 | ADDRESS_WORK, //!< ADDRESS_WORK |
DuyLionTran | 0:51d7bdf88981 | 62 | AGENT, //!< AGENT |
DuyLionTran | 0:51d7bdf88981 | 63 | BIRDAY, //!< BIRDAY |
DuyLionTran | 0:51d7bdf88981 | 64 | CATEGORIES, //!< CATEGORIES |
DuyLionTran | 0:51d7bdf88981 | 65 | EMAIL, //!< EMAIL |
DuyLionTran | 0:51d7bdf88981 | 66 | EMAIL_HOME, //!< EMAIL_HOME |
DuyLionTran | 0:51d7bdf88981 | 67 | EMAIL_WORK, //!< EMAIL_WORK |
DuyLionTran | 0:51d7bdf88981 | 68 | FORMATTED_NAME,//!< FORMATTED_NAME |
DuyLionTran | 0:51d7bdf88981 | 69 | GEO, //!< GEO latitude and longitude ; separated |
DuyLionTran | 0:51d7bdf88981 | 70 | IMPP, //!< IMPP |
DuyLionTran | 0:51d7bdf88981 | 71 | PGPKEY_URL, //!< PGPKEY_URL |
DuyLionTran | 0:51d7bdf88981 | 72 | PGPGKEY_BASE64,//!< PGPGKEY_BASE64 |
DuyLionTran | 0:51d7bdf88981 | 73 | LOGO, //!< LOGO |
DuyLionTran | 0:51d7bdf88981 | 74 | LOGO_URI, //!< LOGO_URI |
DuyLionTran | 0:51d7bdf88981 | 75 | LOGO_BASE64, //!< generic hardcoded image add TYPE=XXXX:imagebyte |
DuyLionTran | 0:51d7bdf88981 | 76 | NAME, //!< NAME |
DuyLionTran | 0:51d7bdf88981 | 77 | NICKNAME, //!< NICKNAME |
DuyLionTran | 0:51d7bdf88981 | 78 | NOTE, //!< NOTE |
DuyLionTran | 0:51d7bdf88981 | 79 | ORGANIZATION, //!< ORGANIZATION |
DuyLionTran | 0:51d7bdf88981 | 80 | PHOTO_URI, //!< PHOTO_URI |
DuyLionTran | 0:51d7bdf88981 | 81 | PHOTO_BASE64, //!< generic hardcoded image add TYPE=XXXX:imagebyte |
DuyLionTran | 0:51d7bdf88981 | 82 | REVISION, //!< REVISION |
DuyLionTran | 0:51d7bdf88981 | 83 | SOURCE_URL, //!< SOURCE_URL |
DuyLionTran | 0:51d7bdf88981 | 84 | TEL, //!< TEL |
DuyLionTran | 0:51d7bdf88981 | 85 | TEL_HOME, //!< TEL_HOME |
DuyLionTran | 0:51d7bdf88981 | 86 | TEL_WORK, //!< TEL_WORK |
DuyLionTran | 0:51d7bdf88981 | 87 | TEL_MOBILE, //!< TEL_MOBILE |
DuyLionTran | 0:51d7bdf88981 | 88 | TITLE, //!< TITLE |
DuyLionTran | 0:51d7bdf88981 | 89 | URL, //!< URL |
DuyLionTran | 0:51d7bdf88981 | 90 | } VCardField_t; |
DuyLionTran | 0:51d7bdf88981 | 91 | |
DuyLionTran | 0:51d7bdf88981 | 92 | /** |
DuyLionTran | 0:51d7bdf88981 | 93 | * Type used to store the vcard information. |
DuyLionTran | 0:51d7bdf88981 | 94 | */ |
DuyLionTran | 0:51d7bdf88981 | 95 | typedef std::map<VCardField_t, std::string> VCardInfo_t; |
DuyLionTran | 0:51d7bdf88981 | 96 | |
DuyLionTran | 0:51d7bdf88981 | 97 | /** |
DuyLionTran | 0:51d7bdf88981 | 98 | * Create a RecordVCard reading the data from the buffer. |
DuyLionTran | 0:51d7bdf88981 | 99 | * @param header Record header. |
DuyLionTran | 0:51d7bdf88981 | 100 | * @param buffer Buffer to read the data from. |
DuyLionTran | 0:51d7bdf88981 | 101 | * @return an object of type RecordVCard or NULL |
DuyLionTran | 0:51d7bdf88981 | 102 | * @par User is in charge of freeing the pointer returned by this function. |
DuyLionTran | 0:51d7bdf88981 | 103 | */ |
DuyLionTran | 0:51d7bdf88981 | 104 | static RecordVCard* parse(const RecordHeader &header, |
DuyLionTran | 0:51d7bdf88981 | 105 | const uint8_t* buffer); |
DuyLionTran | 0:51d7bdf88981 | 106 | |
DuyLionTran | 0:51d7bdf88981 | 107 | /** |
DuyLionTran | 0:51d7bdf88981 | 108 | * Create a record with the specific information. |
DuyLionTran | 0:51d7bdf88981 | 109 | * @param Info optional information to store into the record. |
DuyLionTran | 0:51d7bdf88981 | 110 | */ |
DuyLionTran | 0:51d7bdf88981 | 111 | explicit RecordVCard(const VCardInfo_t &info=VCardInfo_t()); |
DuyLionTran | 0:51d7bdf88981 | 112 | |
DuyLionTran | 0:51d7bdf88981 | 113 | /** |
DuyLionTran | 0:51d7bdf88981 | 114 | * Get the specific information stored in this record. |
DuyLionTran | 0:51d7bdf88981 | 115 | * @param Type type of information to get. |
DuyLionTran | 0:51d7bdf88981 | 116 | * @return if present, the information or an empty string |
DuyLionTran | 0:51d7bdf88981 | 117 | */ |
DuyLionTran | 0:51d7bdf88981 | 118 | const std::string& operator[](const VCardField_t &type)const { |
DuyLionTran | 0:51d7bdf88981 | 119 | VCardInfo_t::const_iterator elem = mCardInfo.find(type); |
DuyLionTran | 0:51d7bdf88981 | 120 | if (elem == mCardInfo.end()) { |
DuyLionTran | 0:51d7bdf88981 | 121 | return sEmptyTagContent; |
DuyLionTran | 0:51d7bdf88981 | 122 | } |
DuyLionTran | 0:51d7bdf88981 | 123 | |
DuyLionTran | 0:51d7bdf88981 | 124 | return elem->second; |
DuyLionTran | 0:51d7bdf88981 | 125 | } |
DuyLionTran | 0:51d7bdf88981 | 126 | |
DuyLionTran | 0:51d7bdf88981 | 127 | /** |
DuyLionTran | 0:51d7bdf88981 | 128 | * Get or set/change an information associated with this record. |
DuyLionTran | 0:51d7bdf88981 | 129 | * @param type Type of information to change. |
DuyLionTran | 0:51d7bdf88981 | 130 | * @return reference to the string information |
DuyLionTran | 0:51d7bdf88981 | 131 | */ |
DuyLionTran | 0:51d7bdf88981 | 132 | std::string& operator[](const VCardField_t &type) { |
DuyLionTran | 0:51d7bdf88981 | 133 | mContentIsChange=true; |
DuyLionTran | 0:51d7bdf88981 | 134 | return mCardInfo[type]; |
DuyLionTran | 0:51d7bdf88981 | 135 | } |
DuyLionTran | 0:51d7bdf88981 | 136 | |
DuyLionTran | 0:51d7bdf88981 | 137 | /** |
DuyLionTran | 0:51d7bdf88981 | 138 | * Get the record type. |
DuyLionTran | 0:51d7bdf88981 | 139 | * @return TYPE_MIME_VCARD |
DuyLionTran | 0:51d7bdf88981 | 140 | */ |
DuyLionTran | 0:51d7bdf88981 | 141 | virtual RecordType_t get_type() const { |
DuyLionTran | 0:51d7bdf88981 | 142 | return TYPE_MIME_VCARD; |
DuyLionTran | 0:51d7bdf88981 | 143 | } //getType |
DuyLionTran | 0:51d7bdf88981 | 144 | |
DuyLionTran | 0:51d7bdf88981 | 145 | /** |
DuyLionTran | 0:51d7bdf88981 | 146 | * @return update the record content and return the number of |
DuyLionTran | 0:51d7bdf88981 | 147 | * bytes needed to store this record |
DuyLionTran | 0:51d7bdf88981 | 148 | */ |
DuyLionTran | 0:51d7bdf88981 | 149 | virtual uint16_t get_byte_length() { |
DuyLionTran | 0:51d7bdf88981 | 150 | update_content_info_string(); |
DuyLionTran | 0:51d7bdf88981 | 151 | return RecordMimeType::get_byte_length(); |
DuyLionTran | 0:51d7bdf88981 | 152 | } |
DuyLionTran | 0:51d7bdf88981 | 153 | |
DuyLionTran | 0:51d7bdf88981 | 154 | /** |
DuyLionTran | 0:51d7bdf88981 | 155 | * Update the content and write it on the buffer. |
DuyLionTran | 0:51d7bdf88981 | 156 | * @param[out] buffer buffer to write the record content into. |
DuyLionTran | 0:51d7bdf88981 | 157 | * @return number of bytes written |
DuyLionTran | 0:51d7bdf88981 | 158 | * @see Record#write |
DuyLionTran | 0:51d7bdf88981 | 159 | */ |
DuyLionTran | 0:51d7bdf88981 | 160 | virtual uint16_t write(uint8_t *buffer){ |
DuyLionTran | 0:51d7bdf88981 | 161 | update_content_info_string(); |
DuyLionTran | 0:51d7bdf88981 | 162 | return RecordMimeType::write(buffer); |
DuyLionTran | 0:51d7bdf88981 | 163 | } |
DuyLionTran | 0:51d7bdf88981 | 164 | |
DuyLionTran | 0:51d7bdf88981 | 165 | /** |
DuyLionTran | 0:51d7bdf88981 | 166 | * Compare two objects. |
DuyLionTran | 0:51d7bdf88981 | 167 | * @return true if the records have the same Vcard information |
DuyLionTran | 0:51d7bdf88981 | 168 | */ |
DuyLionTran | 0:51d7bdf88981 | 169 | bool operator==(const RecordVCard &other){ |
DuyLionTran | 0:51d7bdf88981 | 170 | return (mCardInfo==other.mCardInfo); |
DuyLionTran | 0:51d7bdf88981 | 171 | } |
DuyLionTran | 0:51d7bdf88981 | 172 | |
DuyLionTran | 0:51d7bdf88981 | 173 | virtual ~RecordVCard() { |
DuyLionTran | 0:51d7bdf88981 | 174 | } |
DuyLionTran | 0:51d7bdf88981 | 175 | |
DuyLionTran | 0:51d7bdf88981 | 176 | private: |
DuyLionTran | 0:51d7bdf88981 | 177 | VCardInfo_t mCardInfo; |
DuyLionTran | 0:51d7bdf88981 | 178 | |
DuyLionTran | 0:51d7bdf88981 | 179 | std::string mCardInfoString; ///< buffer containing the Vcard representation for this record |
DuyLionTran | 0:51d7bdf88981 | 180 | |
DuyLionTran | 0:51d7bdf88981 | 181 | bool mContentIsChange; ///< true if we have to upadte the string rappresentation of the data |
DuyLionTran | 0:51d7bdf88981 | 182 | |
DuyLionTran | 0:51d7bdf88981 | 183 | /** |
DuyLionTran | 0:51d7bdf88981 | 184 | * Generate a string representing the vcard info |
DuyLionTran | 0:51d7bdf88981 | 185 | */ |
DuyLionTran | 0:51d7bdf88981 | 186 | void update_content_info_string(); |
DuyLionTran | 0:51d7bdf88981 | 187 | |
DuyLionTran | 0:51d7bdf88981 | 188 | /** |
DuyLionTran | 0:51d7bdf88981 | 189 | * Check if the string that starts from the offset position is a valid |
DuyLionTran | 0:51d7bdf88981 | 190 | * vcard field. |
DuyLionTran | 0:51d7bdf88981 | 191 | * @param content String to search into. |
DuyLionTran | 0:51d7bdf88981 | 192 | * @param offset Search offset. |
DuyLionTran | 0:51d7bdf88981 | 193 | * @return type index of the found field or a negative number if not a valid vcard |
DuyLionTran | 0:51d7bdf88981 | 194 | */ |
DuyLionTran | 0:51d7bdf88981 | 195 | static int8_t find_VCard_field_type(const std::string &content, |
DuyLionTran | 0:51d7bdf88981 | 196 | uint16_t offset); |
DuyLionTran | 0:51d7bdf88981 | 197 | |
DuyLionTran | 0:51d7bdf88981 | 198 | /** |
DuyLionTran | 0:51d7bdf88981 | 199 | * Return the lenght of a vcard field. |
DuyLionTran | 0:51d7bdf88981 | 200 | * @param Content string to search into. |
DuyLionTran | 0:51d7bdf88981 | 201 | * @param Offset search offset. |
DuyLionTran | 0:51d7bdf88981 | 202 | * @return field length |
DuyLionTran | 0:51d7bdf88981 | 203 | * @par This function searches the first sEndVCardTag appearance. |
DuyLionTran | 0:51d7bdf88981 | 204 | */ |
DuyLionTran | 0:51d7bdf88981 | 205 | static uint16_t find_VCard_field_data_lenght(const std::string &content, |
DuyLionTran | 0:51d7bdf88981 | 206 | uint16_t offset); |
DuyLionTran | 0:51d7bdf88981 | 207 | |
DuyLionTran | 0:51d7bdf88981 | 208 | static const std::string sVcardMimeType; |
DuyLionTran | 0:51d7bdf88981 | 209 | static const std::string sStartFieldTag[]; |
DuyLionTran | 0:51d7bdf88981 | 210 | static const std::string sEndFieldTag; |
DuyLionTran | 0:51d7bdf88981 | 211 | static const std::string sStartVCardTag; |
DuyLionTran | 0:51d7bdf88981 | 212 | static const std::string sEndVCardTag; |
DuyLionTran | 0:51d7bdf88981 | 213 | static const std::string sEmptyTagContent; |
DuyLionTran | 0:51d7bdf88981 | 214 | }; |
DuyLionTran | 0:51d7bdf88981 | 215 | |
DuyLionTran | 0:51d7bdf88981 | 216 | } /* namespace NDefLib */ |
DuyLionTran | 0:51d7bdf88981 | 217 | |
DuyLionTran | 0:51d7bdf88981 | 218 | #endif /* NDEFLIB_RECORDTYPE_RECORDVCARD_H_ */ |
DuyLionTran | 0:51d7bdf88981 | 219 | |
DuyLionTran | 0:51d7bdf88981 | 220 | |
DuyLionTran | 0:51d7bdf88981 | 221 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |