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 Record.h Source File

Record.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    Record.h
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   Generic Record 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_RECORD_H_
00039 #define NDEFLIB_RECORD_H_
00040 #include <stdint.h>
00041 
00042 #include "RecordHeader.h"
00043 
00044 namespace NDefLib {
00045 
00046 /**
00047  * Base class for a NDefRecord
00048  * @see NFC Data Exchange Format (NDEF) Technical Specification NDEF 1.0
00049  */
00050 class Record {
00051 public:
00052 
00053     /**
00054      * Enum used to identify the record type.
00055      */
00056     typedef enum {
00057         TYPE_UNKNOWN,        //!< UNKNOWN record
00058         TYPE_TEXT,           //!< TEXT
00059         TYPE_AAR,            //!< Android Archive record
00060         TYPE_MIME,           //!< generic MIME type
00061         TYPE_URI,            //!< generic URI
00062         TYPE_URI_MAIL,       //!< Email URI record
00063         TYPE_URI_SMS,        //!< SMS URI record
00064         TYPE_URI_GEOLOCATION,//!< position URI record
00065         TYPE_MIME_VCARD,     //!< VCard record
00066         TYPE_WIFI_CONF       //!< Wifi configuration
00067     } RecordType_t;
00068 
00069     Record() {
00070     }
00071 
00072     /**
00073      * Set the record as the first record in the message.
00074      */
00075     void set_as_first_record() {
00076         mRecordHeader.set_MB(true);
00077     }
00078 
00079     /**
00080      * Set the record as the last record in the message.
00081      */
00082     void set_as_last_record() {
00083         mRecordHeader.set_ME(true);
00084     }
00085 
00086     /**
00087      * Check if it is the last record in the message.
00088      * @return true if it is the last record in the message
00089      */
00090     bool is_last_record() const {
00091         return mRecordHeader.get_ME();
00092     }
00093 
00094     /**
00095      * Check if it is the first record in the message.
00096      * @return true if it is the fist record in the message
00097      */
00098     bool is_first_record() const {
00099         return mRecordHeader.get_MB();
00100     }
00101 
00102     /**
00103      * Set the record as generic (not the first one and not the last one)
00104      */
00105     void set_as_middle_record() {
00106         mRecordHeader.set_MB(false);
00107         mRecordHeader.set_ME(false);
00108     }
00109 
00110     /**
00111      * Check if the record is in the middle of a chain.
00112      * @return true if is not the fist or the last one
00113      */
00114     bool is_middle_record() const{
00115         return ! (mRecordHeader.get_MB() || mRecordHeader.get_ME());
00116     }
00117 
00118     /**
00119      * Get tag type.
00120      * @par This method should be overridden to return a valid type.
00121      * @return tag type 
00122      */
00123     virtual RecordType_t get_type() const {
00124         return TYPE_UNKNOWN;
00125     } //getType
00126 
00127 
00128     /**
00129      * Get the record header.
00130      * @return record header 
00131      */
00132     const RecordHeader& get_header() const{
00133         return mRecordHeader;
00134     }
00135 
00136     /**
00137      * Number of bytes needed to store this record.
00138      * @return size of the header + size of the record content
00139      */
00140     virtual uint16_t get_byte_length() {
00141         return mRecordHeader.get_record_length();
00142     }
00143 
00144     /**
00145      * Write the record content into a buffer.
00146      * @param[out] buffer buffer to write the record content into, the buffer size
00147      *  must be almost {@link Record#getByteLength} bytes.
00148      * @return number of written bytes
00149      */
00150     virtual uint16_t write(uint8_t *buffer)=0;
00151 
00152     virtual ~Record() {
00153     };
00154 
00155 protected:
00156     RecordHeader mRecordHeader;
00157 };
00158 
00159 } /* namespace NDefLib */
00160 
00161 #endif /* NDEFLIB_RECORD_H_ */