STMicroelectronics' M24SR NFC Dynamic Tag Library.

Dependencies:   ST_INTERFACES

Dependents:   X_NUCLEO_NFC01A1

Fork of M24SR by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NDefNfcTagM24SR.h Source File

NDefNfcTagM24SR.h

00001 /**
00002   ******************************************************************************
00003   * @file       Type4NfcTagM24SR.h
00004   * @author     ST Central Labs
00005   * @version    V2.0.0
00006   * @date       28 Apr 2017
00007   * @brief      M24SR specific NDefLib derived 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 X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_
00039 #define X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_
00040 
00041 #include <cstdlib>
00042 #include <stdint.h>
00043 
00044 #include "NDefLib/NDefNfcTag.h"
00045 
00046 #include "M24SR.h"
00047 
00048 /**
00049  * Helper class to use the NDefLib
00050  */
00051 class NDefNfcTagM24SR: public NDefLib::NDefNfcTag {
00052 
00053 public:
00054 
00055     /**
00056      *
00057      * @param device device to use
00058      */
00059     NDefNfcTagM24SR (M24SR &device):
00060         NDefLib::NDefNfcTag(),
00061         mDevice(device),mIsSessionOpen(false),
00062         mMaxReadBytes(0xFF), mMaxWriteBytes(0xFF),
00063         mOpenSessionCallback(*this),
00064         mCloseSessionCallback(*this),
00065         mWriteByteCallback(*this),
00066         mReadByteCallback(*this){}
00067 
00068     virtual bool open_session(bool force = false);
00069 
00070     virtual bool close_session();
00071 
00072     virtual bool is_session_open(){
00073         return mIsSessionOpen;
00074     }
00075 
00076     /**
00077     * Close the open session.
00078     */
00079     virtual ~NDefNfcTagM24SR() {
00080         if (is_session_open()) {
00081             close_session();
00082         }
00083     }//~NDefNfcTagM24SR
00084 
00085     protected:
00086 
00087         virtual bool writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
00088             byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
00089 
00090         virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
00091             uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
00092 
00093     private:
00094 
00095         M24SR &mDevice;
00096 
00097         /**
00098          * true if the session is open
00099          */
00100         bool mIsSessionOpen;
00101 
00102         /**
00103         * Max length for a read operation
00104         */
00105         uint16_t mMaxReadBytes;
00106 
00107         /**
00108         * Max length for a write operation
00109         */
00110         uint16_t mMaxWriteBytes;
00111 
00112         /**
00113          * Class containing the callback needed to open a session and read the max
00114          * read/write size
00115          */
00116         class OpenSessionCallBack: public M24SR::Callbacks{
00117             public:
00118                 OpenSessionCallBack(NDefNfcTagM24SR &sender);
00119 
00120                 virtual void on_session_open(M24SR *nfc,M24SR::StatusTypeDef status);
00121                 virtual void on_selected_application(M24SR *nfc,M24SR::StatusTypeDef status);
00122                 virtual void on_selected_CC_file(M24SR *nfc,M24SR::StatusTypeDef status);
00123                 virtual void on_read_byte(M24SR *nfc,M24SR::StatusTypeDef status,uint16_t offset,
00124                         uint8_t *readByte, uint16_t nReadByte);
00125                 virtual void on_selected_NDEF_file(M24SR *nfc,M24SR::StatusTypeDef status);
00126 
00127             private:
00128                 /**
00129                  * Object that send the open session callback
00130                  */
00131                 NDefNfcTagM24SR &mSender;
00132 
00133                 /**
00134                  * number of trials done for open the session
00135                  */
00136                 uint32_t mNTrials;
00137 
00138                 /**
00139                  * buffer where read the CC file
00140                  */
00141                 uint8_t CCFile[15];
00142         };
00143 
00144         OpenSessionCallBack mOpenSessionCallback;
00145         friend class OpenSessionCallBack;
00146 
00147         /**
00148          * Class containing the callback needed to close a session
00149          */
00150         class CloseSessionCallBack : public M24SR::Callbacks{
00151             public:
00152                 CloseSessionCallBack(NDefNfcTagM24SR &sender):
00153                     mSender(sender){}
00154 
00155                 virtual void on_deselect(M24SR *nfc,M24SR::StatusTypeDef status){
00156                     (void)nfc;
00157                     if (status==M24SR::M24SR_SUCCESS) {
00158                         mSender.mIsSessionOpen = false;
00159                         mSender.mCallBack->on_session_close(&mSender,true);
00160                     } else {
00161                         mSender.mCallBack->on_session_close(&mSender,false);
00162                     }//if-else
00163                 }
00164             private:
00165 
00166                 /**
00167                  * Object that send the open session callback
00168                  */
00169                 NDefNfcTagM24SR &mSender;
00170         };
00171 
00172         CloseSessionCallBack mCloseSessionCallback;
00173         friend class OpenSessionCallBack;
00174 
00175         /**
00176          * Class containing the callback needed to write a buffer
00177          */
00178         class WriteByteCallback : public M24SR::Callbacks{
00179             public:
00180 
00181                 /**
00182                  *
00183                  * @param sender tag where write the buffer
00184                  */
00185                 WriteByteCallback(NDefNfcTagM24SR &sender):
00186                                   mByteToWrite(NULL),
00187                                   mNByteToWrite(0),
00188                                   mByteWrote(0),
00189                                   mCallback(NULL),
00190                                   mCallbackParam(NULL),
00191                                   mSender(sender){}
00192 
00193                 /**
00194                  * Set the buffer to write and the function to call when finish
00195                  * @param buffer Buffer to write.
00196                  * @param nByte Number of bytes to write.
00197                  * @param callback Function to call when the write ends.
00198                  * @param param Parameter to pass to the callback function.
00199                  */
00200                 void set_task(const uint8_t *buffer,uint16_t nByte,
00201                     byteOperationCallback_t callback,CallbackStatus_t *param) {
00202                     mByteToWrite=buffer;
00203                     mNByteToWrite=nByte;
00204                     mByteWrote=0;
00205                     mCallback = callback;
00206                     mCallbackParam = param;
00207                 }
00208 
00209                 virtual void on_updated_binary(M24SR *nfc,M24SR::StatusTypeDef status,
00210                     uint16_t startOffset,uint8_t *writeByte,uint16_t nWriteByte);
00211 
00212 
00213             private:
00214 
00215                 /** buffer to write */
00216                 const uint8_t *mByteToWrite;
00217                 /** length of the buffer */
00218                 uint16_t mNByteToWrite;
00219                 /** number of byte already wrote */
00220                 uint16_t mByteWrote;
00221 
00222                 /** function to call when all the bytes are write */
00223                 byteOperationCallback_t mCallback;
00224                 /** parameter to pass to the callback function*/
00225                 CallbackStatus_t* mCallbackParam;
00226 
00227                 /** tag where write the buffer*/
00228                 NDefNfcTagM24SR &mSender;
00229         };
00230 
00231         WriteByteCallback mWriteByteCallback;
00232         friend class WriteByteCallback;
00233 
00234         /**
00235          * Class containing the callback needed to read a buffer
00236          */
00237         class ReadByteCallback : public M24SR::Callbacks{
00238             public:
00239 
00240                 /**
00241                  *
00242                  * @param Sender tag where read the buffer
00243                  */
00244                 ReadByteCallback(NDefNfcTagM24SR &sender):
00245                                  mBuffer(NULL),
00246                                  mNByteToRead(0),
00247                                  mByteRead(0),
00248                                  mCallback(NULL),
00249                                  mCallbackParam(NULL),
00250                                  mSender(sender){}
00251 
00252                 /**
00253                  * Set the buffer where read the data and the function to call when finish
00254                  * @param buffer Buffer read
00255                  * @param nByte Number of bytes to read
00256                  * @param callback Function to call when the read ends
00257                  * @param param Parameter to pass to the callback function
00258                  */
00259                 void set_task(uint8_t *buffer,uint16_t nByte,
00260                     byteOperationCallback_t callback,CallbackStatus_t *param){
00261                     mBuffer=buffer;
00262                     mNByteToRead=nByte;
00263                     mByteRead=0;
00264                     mCallback = callback;
00265                     mCallbackParam = param;
00266                 }
00267 
00268                 virtual void on_read_byte(M24SR *nfc,M24SR::StatusTypeDef status,
00269                     uint16_t offset,uint8_t *readByte, uint16_t nReadByte);
00270 
00271 
00272             private:
00273 
00274                 /**
00275                  * Buffer where read the data
00276                  */
00277                 uint8_t *mBuffer;
00278 
00279                 /**
00280                  * Number of bytes to read
00281                  */
00282                 uint16_t mNByteToRead;
00283 
00284                 /**
00285                  * Number of bytes already read
00286                  */
00287                 uint16_t mByteRead;
00288 
00289                 /**
00290                  * Function to call when all the bytes are read
00291                  */
00292                 byteOperationCallback_t mCallback;
00293 
00294                 /**
00295                  * Parameter to pass to the callback function
00296                  */
00297                 CallbackStatus_t* mCallbackParam;
00298 
00299                 /**
00300                  * Tag where the data are read
00301                  */
00302                 NDefNfcTagM24SR &mSender;
00303         };
00304 
00305         ReadByteCallback mReadByteCallback;
00306         friend class ReadByteCallback;
00307 
00308 
00309 };
00310 
00311 #endif /* X_NUCLEO_NFC01A1_M24SR_NDEFNFCTAGM24SR_H_ */