mbed OS 5 example application using X-NUCLEO-NFC02A1

Dependencies:   NDefLib X_NUCLEO_NFC02A1

Fork of HelloWorld_NFC02A1 by ST

Committer:
rosarium
Date:
Tue Aug 30 09:18:50 2016 +0000
Revision:
1:11ae12d41082
Child:
2:6f1b1f7f8d12
NDefLib middle-ware library ported on HelloWorld_NFC02A1. Now NFC02A1 using the same middle-ware library as NFC01A1.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rosarium 1:11ae12d41082 1 /**
rosarium 1:11ae12d41082 2 ******************************************************************************
rosarium 1:11ae12d41082 3 * @file NdefNfcTagM24LR.h
rosarium 1:11ae12d41082 4 * @author AMG Central Lab
rosarium 1:11ae12d41082 5 * @version V1.0.0
rosarium 1:11ae12d41082 6 * @date 30 Aug 2016
rosarium 1:11ae12d41082 7 * @brief M24LR specific NDefLib derived class
rosarium 1:11ae12d41082 8 ******************************************************************************
rosarium 1:11ae12d41082 9 * @attention
rosarium 1:11ae12d41082 10 *
rosarium 1:11ae12d41082 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
rosarium 1:11ae12d41082 12 *
rosarium 1:11ae12d41082 13 * Redistribution and use in source and binary forms, with or without modification,
rosarium 1:11ae12d41082 14 * are permitted provided that the following conditions are met:
rosarium 1:11ae12d41082 15 * 1. Redistributions of source code must retain the above copyright notice,
rosarium 1:11ae12d41082 16 * this list of conditions and the following disclaimer.
rosarium 1:11ae12d41082 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
rosarium 1:11ae12d41082 18 * this list of conditions and the following disclaimer in the documentation
rosarium 1:11ae12d41082 19 * and/or other materials provided with the distribution.
rosarium 1:11ae12d41082 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
rosarium 1:11ae12d41082 21 * may be used to endorse or promote products derived from this software
rosarium 1:11ae12d41082 22 * without specific prior written permission.
rosarium 1:11ae12d41082 23 *
rosarium 1:11ae12d41082 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
rosarium 1:11ae12d41082 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
rosarium 1:11ae12d41082 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
rosarium 1:11ae12d41082 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
rosarium 1:11ae12d41082 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
rosarium 1:11ae12d41082 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
rosarium 1:11ae12d41082 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
rosarium 1:11ae12d41082 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
rosarium 1:11ae12d41082 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
rosarium 1:11ae12d41082 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
rosarium 1:11ae12d41082 34 *
rosarium 1:11ae12d41082 35 ******************************************************************************
rosarium 1:11ae12d41082 36 */
rosarium 1:11ae12d41082 37
rosarium 1:11ae12d41082 38 #ifndef X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_
rosarium 1:11ae12d41082 39 #define X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_
rosarium 1:11ae12d41082 40
rosarium 1:11ae12d41082 41 #include <cstdlib>
rosarium 1:11ae12d41082 42 #include <stdint.h>
rosarium 1:11ae12d41082 43
rosarium 1:11ae12d41082 44 #include "NDefNfcTag.h"
rosarium 1:11ae12d41082 45 #include "Nfc_class.h"
rosarium 1:11ae12d41082 46
rosarium 1:11ae12d41082 47 //#include "m24lr.h"
rosarium 1:11ae12d41082 48
rosarium 1:11ae12d41082 49 #define NFCT5_MAGICNUMBER_E1_CCFILE 0xE1
rosarium 1:11ae12d41082 50 #define NFCT5_MAGICNUMBER_E2_CCFILE 0xE2
rosarium 1:11ae12d41082 51 #define NFCT5_EXTENDED_CCFILE 0xFF
rosarium 1:11ae12d41082 52 #define NFCT5_VERSION_V1_0 0x40
rosarium 1:11ae12d41082 53 #define NFCT5_READ_ACCESS 0x0C
rosarium 1:11ae12d41082 54 #define NFCT5_WRITE_ACCESS 0x03
rosarium 1:11ae12d41082 55
rosarium 1:11ae12d41082 56 #define NFCT5_NDEF_MSG_TLV 0x03
rosarium 1:11ae12d41082 57 #define NFCT5_PROPRIETARY_TLV 0xFD
rosarium 1:11ae12d41082 58 #define NFCT5_TERMINATOR_TLV 0xFE
rosarium 1:11ae12d41082 59 #define NFCT5_3_BYTES_L_TLV 0xFF
rosarium 1:11ae12d41082 60
rosarium 1:11ae12d41082 61 #define NFC_OK 0
rosarium 1:11ae12d41082 62 #define NDEF_OK 0
rosarium 1:11ae12d41082 63 #define NDEF_ERROR 1
rosarium 1:11ae12d41082 64 #define NDEF_ERROR_MEMORY_TAG 2
rosarium 1:11ae12d41082 65 #define NDEF_ERROR_MEMORY_INTERNAL 3
rosarium 1:11ae12d41082 66 #define NDEF_ERROR_LOCKED 4
rosarium 1:11ae12d41082 67 #define NDEF_ERROR_NOT_FORMATED 5
rosarium 1:11ae12d41082 68
rosarium 1:11ae12d41082 69 #define NDEF_MAX_SIZE NFC_DEVICE_MAX_NDEFMEMORY
rosarium 1:11ae12d41082 70
rosarium 1:11ae12d41082 71 #define NDEF_SIZE_OFFSET 0
rosarium 1:11ae12d41082 72 #define FIRST_RECORD_OFFSET 2
rosarium 1:11ae12d41082 73
rosarium 1:11ae12d41082 74 #ifndef MIN
rosarium 1:11ae12d41082 75 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
rosarium 1:11ae12d41082 76 #endif
rosarium 1:11ae12d41082 77
rosarium 1:11ae12d41082 78 #define NFCTAG_4M_SIZE 0x200
rosarium 1:11ae12d41082 79 #define NFCTAG_16M_SIZE 0x800
rosarium 1:11ae12d41082 80 #define NFCTAG_64M_SIZE 0x2000
rosarium 1:11ae12d41082 81
rosarium 1:11ae12d41082 82
rosarium 1:11ae12d41082 83 #define MAX_NDEF_MEM 0x200
rosarium 1:11ae12d41082 84 #define M24LR_MAX_SIZE NFCTAG_4M_SIZE
rosarium 1:11ae12d41082 85 #define M24LR_NDEF_MAX_SIZE MIN(M24LR_MAX_SIZE,MAX_NDEF_MEM)
rosarium 1:11ae12d41082 86 #define NFC_DEVICE_MAX_NDEFMEMORY M24LR_NDEF_MAX_SIZE
rosarium 1:11ae12d41082 87
rosarium 1:11ae12d41082 88
rosarium 1:11ae12d41082 89
rosarium 1:11ae12d41082 90 typedef enum
rosarium 1:11ae12d41082 91 {
rosarium 1:11ae12d41082 92 TT5_NO_NDEF = 0,
rosarium 1:11ae12d41082 93 TT5_INITIALIZED,
rosarium 1:11ae12d41082 94 TT5_READ_WRITE,
rosarium 1:11ae12d41082 95 TT5_READ
rosarium 1:11ae12d41082 96 } TT5_State;
rosarium 1:11ae12d41082 97
rosarium 1:11ae12d41082 98 /**
rosarium 1:11ae12d41082 99 * @brief CCfile structure
rosarium 1:11ae12d41082 100 */
rosarium 1:11ae12d41082 101 typedef struct
rosarium 1:11ae12d41082 102 {
rosarium 1:11ae12d41082 103 uint8_t MagicNumber; /* Magic Number should be E1h or E2h */
rosarium 1:11ae12d41082 104 uint8_t Version;
rosarium 1:11ae12d41082 105 uint8_t MemorySize;
rosarium 1:11ae12d41082 106 uint8_t TT5Tag;
rosarium 1:11ae12d41082 107 uint8_t rsved1;
rosarium 1:11ae12d41082 108 uint8_t rsved2;
rosarium 1:11ae12d41082 109 uint16_t ExtMemorySize;
rosarium 1:11ae12d41082 110 TT5_State State;
rosarium 1:11ae12d41082 111 uint32_t NDEF_offset;
rosarium 1:11ae12d41082 112 }sCCFileInfo;
rosarium 1:11ae12d41082 113
rosarium 1:11ae12d41082 114
rosarium 1:11ae12d41082 115 /**
rosarium 1:11ae12d41082 116 * Helper class to use the NDefLib
rosarium 1:11ae12d41082 117 */
rosarium 1:11ae12d41082 118
rosarium 1:11ae12d41082 119
rosarium 1:11ae12d41082 120 class NDefNfcTagM24LR: public NDefLib::NDefNfcTag {
rosarium 1:11ae12d41082 121
rosarium 1:11ae12d41082 122 public:
rosarium 1:11ae12d41082 123
rosarium 1:11ae12d41082 124 /**
rosarium 1:11ae12d41082 125 *
rosarium 1:11ae12d41082 126 * @param device device to use
rosarium 1:11ae12d41082 127 */
rosarium 1:11ae12d41082 128 NDefNfcTagM24LR(Nfc &device) :
rosarium 1:11ae12d41082 129 NDefLib::NDefNfcTag(),
rosarium 1:11ae12d41082 130 mDevice(device),mIsSessionOpen(false),
rosarium 1:11ae12d41082 131 mMaxReadBytes(0xFF), mMaxWriteBytes(0xFF){}
rosarium 1:11ae12d41082 132
rosarium 1:11ae12d41082 133 virtual bool openSession(bool force = false);
rosarium 1:11ae12d41082 134
rosarium 1:11ae12d41082 135 virtual bool closeSession();
rosarium 1:11ae12d41082 136
rosarium 1:11ae12d41082 137 virtual bool isSessionOpen(){
rosarium 1:11ae12d41082 138 return mIsSessionOpen;
rosarium 1:11ae12d41082 139 }
rosarium 1:11ae12d41082 140 uint16_t ReadData( uint16_t Offset , uint16_t DataSize , uint8_t* pData );
rosarium 1:11ae12d41082 141 uint16_t WriteData( uint16_t Offset , uint32_t DataSize , uint8_t *pData );
rosarium 1:11ae12d41082 142 uint16_t NfcType5_WriteCCFile( const uint8_t * const pCCBuffer );
rosarium 1:11ae12d41082 143 uint16_t NfcType5_ReadCCFile( uint8_t * const pCCBuffer );
rosarium 1:11ae12d41082 144 uint16_t NfcType5_TT5Init( void );
rosarium 1:11ae12d41082 145 uint16_t NfcType5_NDEFDetection( void );
rosarium 1:11ae12d41082 146 /**
rosarium 1:11ae12d41082 147 * Close the open session.
rosarium 1:11ae12d41082 148 */
rosarium 1:11ae12d41082 149 virtual ~NDefNfcTagM24LR(){
rosarium 1:11ae12d41082 150 if(isSessionOpen())
rosarium 1:11ae12d41082 151 closeSession();
rosarium 1:11ae12d41082 152 }//~NDefNfcTagM24SR
rosarium 1:11ae12d41082 153
rosarium 1:11ae12d41082 154 protected:
rosarium 1:11ae12d41082 155
rosarium 1:11ae12d41082 156 virtual bool writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset,
rosarium 1:11ae12d41082 157 byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
rosarium 1:11ae12d41082 158
rosarium 1:11ae12d41082 159 virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
rosarium 1:11ae12d41082 160 uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus);
rosarium 1:11ae12d41082 161 bool NDefWriteByte(const uint8_t *buffer, uint16_t length,uint16_t offset);
rosarium 1:11ae12d41082 162
rosarium 1:11ae12d41082 163
rosarium 1:11ae12d41082 164 bool NDefReadByte(const uint16_t byteOffset, const uint16_t length, uint8_t *buffer);
rosarium 1:11ae12d41082 165
rosarium 1:11ae12d41082 166 private:
rosarium 1:11ae12d41082 167
rosarium 1:11ae12d41082 168 Nfc &mDevice;
rosarium 1:11ae12d41082 169
rosarium 1:11ae12d41082 170 /**
rosarium 1:11ae12d41082 171 * true if the session is open
rosarium 1:11ae12d41082 172 */
rosarium 1:11ae12d41082 173 bool mIsSessionOpen;
rosarium 1:11ae12d41082 174
rosarium 1:11ae12d41082 175 /**
rosarium 1:11ae12d41082 176 * Max length for a read operation
rosarium 1:11ae12d41082 177 */
rosarium 1:11ae12d41082 178 uint16_t mMaxReadBytes;
rosarium 1:11ae12d41082 179
rosarium 1:11ae12d41082 180 /**
rosarium 1:11ae12d41082 181 * Max length for a write operation
rosarium 1:11ae12d41082 182 */
rosarium 1:11ae12d41082 183 uint16_t mMaxWriteBytes;
rosarium 1:11ae12d41082 184
rosarium 1:11ae12d41082 185 sCCFileInfo CCFileStruct;
rosarium 1:11ae12d41082 186
rosarium 1:11ae12d41082 187 };
rosarium 1:11ae12d41082 188
rosarium 1:11ae12d41082 189 #endif /* X_NUCLEO_NFC02A1_M24LR_NDEFNFCTAGM24LR_H_ */