iot_water_monitor_v2

Dependencies:   easy-connect-v16 Watchdog FP MQTTPacket RecordType-v-16 watersenor_and_temp_code

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordHeader.h Source File

RecordHeader.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    RecordHeader.h
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   Record header class 
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_RECORDHEADER_H_
00039 #define NDEFLIB_RECORDHEADER_H_
00040 
00041 #include <stdint.h>
00042 
00043 namespace NDefLib {
00044 
00045 /**
00046  * Record header class.
00047  * @see NFC Data Exchange Format (NDEF) Technical Specification NDEF 1.0
00048  */
00049 class RecordHeader {
00050 
00051 public:
00052 
00053     /**
00054      * Record type format
00055      */
00056     typedef enum TypeNameFormat {
00057         Empty = 0x00,          //!< Empty
00058         NFC_well_known = 0x01, //!< NFC_well_known
00059         Mime_media_type = 0x02,//!< Mime_media_type
00060         Absolute_URI = 0x03,   //!< Absolute_URI
00061         NFC_external = 0x04,   //!< NFC_external
00062         Unknown = 0x05,        //!< Unknown
00063         Unchanged = 0x06,      //!< Unchanged
00064         Reserved = 0x07        //!< Reserved
00065     } TypeNameFormat_t;
00066 
00067 
00068     RecordHeader() :
00069             headerFlags(0), typeLength(0), payloadLength(0) {
00070         set_SR(true);
00071     }
00072 
00073     /**
00074      * Set the 'message begin' flag.
00075      * @param value True if the record is the first within the message.
00076      */
00077     void set_MB(bool value) {
00078         if (value) {
00079             headerFlags |= 0x80;
00080         } else {
00081             headerFlags &= ~0x80;
00082         }
00083     }//set_MB
00084 
00085     /**
00086      * Check the 'message begin' flag.
00087      * @return true if it is the first record in the message
00088      */
00089     bool get_MB() const {
00090         return (headerFlags & 0x80) != 0;
00091     }//get_MB
00092 
00093     /**
00094      * Set the 'message end' flag.
00095      * @param value True if it is the last record in the message.
00096      */
00097     void set_ME(bool value) {
00098         if (value) {
00099             headerFlags |= 0x40;
00100         } else {
00101             headerFlags &= ~0x40;
00102         }
00103     }//set_ME
00104 
00105     /**
00106      * Check the 'message end' flag.
00107      * @return true if it is the last record in the message
00108      */
00109     bool get_ME() const {
00110         return (headerFlags & 0x40) != 0;
00111     }//get_ME
00112 
00113     /**
00114      * Set the 'Chunk' flag.
00115      * @param value True if the record is in the first record chunk or in a middle record
00116      * chunk of a chunked payload.
00117      */
00118     void set_CF(bool value) {
00119         if (value) {
00120             headerFlags |= 0x20;
00121         } else {
00122             headerFlags &= ~0x20;
00123         }
00124     }//set_CF
00125 
00126     /**
00127      * Check the 'Chunk' flag value.
00128      * @return true if the record is in the first record chunk or in a middle record
00129      * chunk of a chunked payload
00130      */
00131     bool get_CF() const {
00132         return (headerFlags & 0x20) != 0;
00133     }//get_CF
00134 
00135     /**
00136      * Set the 'Short record' flag value.
00137      * @param value True if the record size can be encoded with 8 bits.
00138      */
00139     void set_SR(bool value) {
00140         if (value) {
00141             headerFlags |= 0x10;
00142         } else {
00143             headerFlags &= ~0x10;
00144         }
00145     }//setSR
00146 
00147     /**
00148      * Check the 'Short record' flag.
00149      * @return true if the short range header format is set
00150      */
00151     bool get_SR() const {
00152         return (headerFlags & 0x10) != 0;
00153     }//getSR
00154 
00155     /**
00156      * Set the 'ID length' flag.
00157      * @param value True if the 'ID length' value is used.
00158      */
00159     void set_IL(bool value) {
00160         if (value) {
00161             headerFlags |= 0x08;
00162         } else {
00163             headerFlags &= ~0x08;
00164         }
00165     }//setIL
00166 
00167     /**
00168      * Check the 'ID length' flag.
00169      * @param value True if 'ID length' is set.
00170      */
00171     bool get_IL() const {
00172         return (headerFlags & 0x08) != 0;
00173     }//getIL
00174 
00175     /**
00176      * Set the type name format field.
00177      * @param value Record type name format.
00178      */
00179     void set_FNT(const TypeNameFormat_t value) {
00180         uint8_t temp = (uint8_t) value;
00181         temp &= 0x07; //keep the first 3 bits
00182         headerFlags &= 0xF8; //clean the fist 3 bits
00183         headerFlags |= temp; //set the fist 3 bits
00184     }//setFNT
00185 
00186     /**
00187      * Get the record type name.
00188      * @return type name format of the record
00189      */
00190     TypeNameFormat_t get_FNT() const {
00191         return (TypeNameFormat_t) (headerFlags & 0x07);
00192     }
00193 
00194     /**
00195      * Set the record payload length.
00196      * @par This function will update the SR field as needed.
00197      * @param length payload length
00198      */
00199     void set_payload_length(uint32_t length) {
00200         payloadLength = length;
00201         set_SR(payloadLength <= 255);
00202     }
00203 
00204     /**
00205      * Get the payload length.
00206      * @return payload length
00207      */
00208     uint32_t get_payload_length() const {
00209         return payloadLength;
00210     }
00211 
00212     /**
00213      * Set the type length.
00214      * @param size.
00215      */
00216     void set_type_length(uint8_t size) {
00217         typeLength = size;
00218     }
00219 
00220     /**
00221      * Get the type length.
00222      * @return type length
00223      */
00224     uint8_t get_type_length() const {
00225         return typeLength;
00226     }
00227 
00228     /**
00229      * Set the id length.
00230      * @param size.
00231      */
00232     void set_id_length(uint8_t size) {
00233         if (size!=0) {
00234             idLength = size;
00235             set_IL(true);
00236         } else {
00237             set_IL(false);
00238         }
00239     }
00240 
00241     /**
00242      * Get the id length.
00243      * @return id length
00244      */
00245     uint8_t get_id_length() const {
00246         return idLength;
00247     }
00248 
00249     /**
00250      * Get the number of bytes needed to store this record.
00251      * @return 3 or 6
00252      */
00253     uint16_t get_record_length() const {
00254         return (get_SR() ? 3 : 6) + (get_IL() ? idLength : 0)+typeLength + payloadLength;
00255     }
00256 
00257     /**
00258      * Store the header information in the buffer.
00259      * @param[out] outBuffer Buffer to write the header into.
00260      * @return number of write bytes
00261      */
00262     uint8_t write_header(uint8_t *outBuffer) const {
00263 
00264         uint32_t index = 0;
00265 
00266         outBuffer[index++] = headerFlags;
00267         outBuffer[index++] = typeLength;
00268         if (get_SR()) {
00269             outBuffer[index++] = (uint8_t) payloadLength;
00270         } else {
00271             outBuffer[index++] = (uint8_t) ((payloadLength & 0xFF000000)
00272                     >> 24);
00273             outBuffer[index++] = (uint8_t) ((payloadLength & 0x00FF0000)
00274                     >> 16);
00275             outBuffer[index++] = (uint8_t) ((payloadLength & 0x0000FF00)
00276                     >> 8);
00277             outBuffer[index++] = (uint8_t) (payloadLength & 0x000000FF);
00278         } //if-else
00279         if (get_IL()) {
00280             outBuffer[index++] =idLength;
00281         }
00282 
00283         return index;
00284     } //writeHeader
00285 
00286     /**
00287      * Load an header from a buffer.
00288      * @param buffer Buffer to load the header from.
00289      * @return number of read bytes
00290      */
00291     uint16_t load_header(const uint8_t * const buffer) {
00292         uint32_t index = 0;
00293         headerFlags = buffer[index++];
00294         typeLength = buffer[index++];
00295         if (get_SR()) {
00296             payloadLength = buffer[index++];
00297         } else {
00298             payloadLength = (((uint32_t) buffer[index + 0]) << 24)
00299                 | (((uint32_t) buffer[index + 1]) << 16)
00300                 | (((uint32_t) buffer[index + 2]) << 8)
00301                 | ((uint32_t) buffer[index + 3]);
00302             index += 4;
00303         } //if-else
00304         if (get_IL()) {
00305             idLength = buffer[index++];
00306         } else { 
00307             idLength = 0;
00308         }
00309 
00310         return index;
00311     } //loadHeader
00312 
00313     /**
00314      * Equal operator.
00315      * @param other Other object to compare with.
00316      * @return true if the two record headers are equal
00317      */
00318     bool operator==(const RecordHeader &other) const{
00319         return (headerFlags==other.headerFlags) &&
00320         (typeLength==other.typeLength) &&
00321         (payloadLength==other.payloadLength);
00322     }
00323 
00324     bool operator!=(const RecordHeader &other) const{
00325         return !(*this==other);
00326     }
00327 
00328 private:
00329     uint8_t idLength;
00330     uint8_t headerFlags;
00331     uint8_t typeLength;
00332     uint32_t payloadLength;
00333 };
00334 
00335 } /* namespace NDefLib */
00336 
00337 #endif /* NDEFLIB_RECORDHEADER_H_ */
00338 
00339 
00340 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/