mbed OS 5 example application using X-NUCLEO-NFC02A1

Dependencies:   NDefLib X_NUCLEO_NFC02A1

Fork of HelloWorld_NFC02A1 by ST

X_NUCLEO_NFC02A1/m24lr/NDefNfcTagM24LR.h

Committer:
rosarium
Date:
2016-08-30
Revision:
1:11ae12d41082
Child:
2:6f1b1f7f8d12

File content as of revision 1:11ae12d41082:

/**
  ******************************************************************************
  * @file       NdefNfcTagM24LR.h
  * @author     AMG Central Lab
  * @version    V1.0.0
  * @date       30 Aug 2016
  * @brief      M24LR specific NDefLib derived class
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

#ifndef X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_
#define X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_

#include <cstdlib>
#include <stdint.h>

#include "NDefNfcTag.h"
#include "Nfc_class.h"

//#include "m24lr.h"

#define NFCT5_MAGICNUMBER_E1_CCFILE       0xE1
#define NFCT5_MAGICNUMBER_E2_CCFILE       0xE2
#define NFCT5_EXTENDED_CCFILE             0xFF
#define NFCT5_VERSION_V1_0                0x40
#define NFCT5_READ_ACCESS                 0x0C
#define NFCT5_WRITE_ACCESS                0x03

#define NFCT5_NDEF_MSG_TLV                0x03
#define NFCT5_PROPRIETARY_TLV             0xFD
#define NFCT5_TERMINATOR_TLV              0xFE
#define NFCT5_3_BYTES_L_TLV               0xFF
    
#define NFC_OK                   0
#define NDEF_OK                     0
#define NDEF_ERROR                  1
#define NDEF_ERROR_MEMORY_TAG       2
#define NDEF_ERROR_MEMORY_INTERNAL  3
#define NDEF_ERROR_LOCKED           4
#define NDEF_ERROR_NOT_FORMATED     5

#define NDEF_MAX_SIZE               NFC_DEVICE_MAX_NDEFMEMORY

#define NDEF_SIZE_OFFSET            0
#define FIRST_RECORD_OFFSET         2

#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif

#define NFCTAG_4M_SIZE            0x200
#define NFCTAG_16M_SIZE           0x800
#define NFCTAG_64M_SIZE           0x2000

    
#define MAX_NDEF_MEM                 0x200
#define M24LR_MAX_SIZE               NFCTAG_4M_SIZE
#define M24LR_NDEF_MAX_SIZE          MIN(M24LR_MAX_SIZE,MAX_NDEF_MEM)
#define NFC_DEVICE_MAX_NDEFMEMORY    M24LR_NDEF_MAX_SIZE
    


typedef enum
{
  TT5_NO_NDEF = 0,
  TT5_INITIALIZED,
  TT5_READ_WRITE,
  TT5_READ
} TT5_State;

/**
  * @brief  CCfile structure
  */
typedef struct
{
  uint8_t MagicNumber;  /* Magic Number should be E1h or E2h */
  uint8_t Version;
  uint8_t MemorySize;
  uint8_t TT5Tag;
  uint8_t rsved1;
  uint8_t rsved2;
  uint16_t ExtMemorySize;
  TT5_State State;
  uint32_t NDEF_offset;
}sCCFileInfo;


/**
 * Helper class to use the NDefLib
 */


class NDefNfcTagM24LR: public NDefLib::NDefNfcTag {

public:

    /**
     *
     * @param device device to use
     */
    NDefNfcTagM24LR(Nfc &device) :
        NDefLib::NDefNfcTag(),
        mDevice(device),mIsSessionOpen(false),
        mMaxReadBytes(0xFF), mMaxWriteBytes(0xFF){}

    virtual bool openSession(bool force = false);

    virtual bool closeSession();

    virtual bool isSessionOpen(){
        return mIsSessionOpen;
    }
        uint16_t ReadData( uint16_t Offset , uint16_t DataSize , uint8_t* pData );
        uint16_t WriteData( uint16_t Offset , uint32_t DataSize , uint8_t *pData );
        uint16_t NfcType5_WriteCCFile( const uint8_t * const pCCBuffer );
        uint16_t NfcType5_ReadCCFile( uint8_t * const pCCBuffer );
        uint16_t NfcType5_TT5Init( void );
        uint16_t NfcType5_NDEFDetection( void );
    /**
    * Close the open session.
    */
    virtual ~NDefNfcTagM24LR(){
        if(isSessionOpen())
            closeSession();
    }//~NDefNfcTagM24SR

    protected:

        virtual bool writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
                byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);

        virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
                uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
                bool NDefWriteByte(const uint8_t *buffer, uint16_t length,uint16_t offset);


                bool NDefReadByte(const uint16_t byteOffset, const uint16_t length, uint8_t *buffer);

    private:

        Nfc &mDevice;

        /**
         * true if the session is open
         */
        bool mIsSessionOpen;

        /**
        * Max length for a read operation
        */
        uint16_t mMaxReadBytes;

        /**
        * Max length for a write operation
        */
        uint16_t mMaxWriteBytes;
                
                sCCFileInfo CCFileStruct;

    };

#endif /* X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_ */