1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * NFC Ndef Mapping For Different Smart Cards. 19 * 20 */ 21 22 #ifndef PHFRINFC_NDEFMAP_H 23 #define PHFRINFC_NDEFMAP_H 24 25 /*include files*/ 26 #include <phFriNfc.h> 27 #include <phNfcStatus.h> 28 #include <phNfcTypes.h> 29 #include <phNfcTypes_Mapping.h> 30 31 /* NDEF Mapping Component 32 * 33 * This component implements the read/write/check NDEF functions for remote 34 * devices. NDEF data, as defined by the NFC Forum NDEF specification are 35 * written to or read from a remote device that can be a smart- or memory card. 36 * Please notice that the NDEF mapping command sequence must 37 * be contiguous (after correct initialization) 38 * 39 */ 40 41 /* 42 * NDEF Mapping - specifies the different card types 43 * These are the only recognized card types in this version. 44 * 45 */ 46 47 #define PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD 7 /* Mifare Standard */ 48 #define PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD 8 /* Mifare Standard */ 49 #define PH_FRINFC_NDEFMAP_MIFARE_STD_2K_CARD 11 /*internal Mifare Standard */ 50 #define PH_FRINFC_NDEFMAP_EMPTY_NDEF_MSG \ 51 { 0xD0, 0x00, 0x00 } /* Empty ndef message */ 52 #define PH_FRINFC_NDEFMAP_MFUL_4BYTES_BUF 4 /* To store 4 bytes after write */ 53 54 /* Enum represents the different card state*/ 55 typedef enum { 56 PH_NDEFMAP_CARD_STATE_INITIALIZED, 57 PH_NDEFMAP_CARD_STATE_READ_ONLY, 58 PH_NDEFMAP_CARD_STATE_READ_WRITE, 59 PH_NDEFMAP_CARD_STATE_INVALID 60 } phNDEF_CARD_STATE; 61 62 /* 63 * NDEF Mapping - specifies the Compliant Blocks in the Mifare 1k and 4k card 64 * types 65 * 66 */ 67 #define PH_FRINFC_NDEFMAP_MIFARESTD_1KNDEF_COMPBLOCK \ 68 45 /* Total Ndef Compliant blocks Mifare 1k */ 69 #define PH_FRINFC_NDEFMAP_MIFARESTD_2KNDEF_COMPBLOCK \ 70 90 /* Total Ndef Compliant blocks Mifare 2k */ 71 #define PH_FRINFC_NDEFMAP_MIFARESTD_4KNDEF_COMPBLOCK \ 72 210 /* Total Ndef Compliant blocks Mifare 4k */ 73 #define PH_FRINFC_NDEFMAP_MIFARESTD_RDWR_SIZE \ 74 16 /* Bytes read/write for one read/write operation*/ 75 #define PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK \ 76 40 /* Total number of sectors in Mifare 4k */ 77 #define PH_FRINFC_NDEFMAP_MIFARESTD_ST15_BYTES \ 78 15 /* To store 15 bytes after reading a block */ 79 80 /* 81 * Completion Routine Indices 82 * 83 * These are the indices of the completion routine pointers within the component 84 * context. Completion routines belong to upper components. 85 * 86 */ 87 #define PH_FRINFC_NDEFMAP_CR_CHK_NDEF 0 /* */ 88 #define PH_FRINFC_NDEFMAP_CR_RD_NDEF 1 /* */ 89 #define PH_FRINFC_NDEFMAP_CR_WR_NDEF 2 /* */ 90 #define PH_FRINFC_NDEFMAP_CR_ERASE_NDEF 3 /* */ 91 #define PH_FRINFC_NDEFMAP_CR_INVALID_OPE 4 /* */ 92 #define PH_FRINFC_NDEFMAP_CR 5 /* */ 93 94 /* 95 * File Offset Attributes 96 * 97 * Following values are used to determine the offset value for Read/Write. This 98 * specifies whether the Read/Write operation needs to be restarted/continued 99 * from the last offset set. 100 * 101 */ 102 /* Read/Write operation shall start from the last offset set */ 103 #define PH_FRINFC_NDEFMAP_SEEK_CUR 0 /* */ 104 /* Read/Write operation shall start from the beginning of the file/card */ 105 #define PH_FRINFC_NDEFMAP_SEEK_BEGIN 1 /* */ 106 107 /* Read operation invalid */ 108 #define PH_FRINFC_NDEFMAP_SEEK_INVALID 0xFF /* */ 109 110 /* 111 * Buffer Size Definitions 112 * 113 */ 114 /* Minimum size of the TRX buffer required */ 115 #define PH_FRINFC_NDEFMAP_MAX_SEND_RECV_BUF_SIZE 252 /* */ 116 /* The size of s MIFARE block */ 117 #define PH_FRINFC_NDEFMAP_MF_READ_BLOCK_SIZE 16 /* */ 118 119 typedef struct phFriNfc_MifareStdCont { 120 /* to store bytes that will be used in the next write/read operation, if any 121 */ 122 uint8_t internalBuf[PH_FRINFC_NDEFMAP_MIFARESTD_ST15_BYTES]; 123 /* to Store the length of the internalBuf */ 124 uint16_t internalLength; 125 /* holds the block number which is presently been used */ 126 uint8_t currentBlock; 127 /* the number of Ndef compliant blocks written/read */ 128 uint8_t NdefBlocks; 129 /* Total Number of Ndef compliant Blocks */ 130 uint16_t NoOfNdefCompBlocks; 131 /* used in write ndef, to know that internal byte are accessed */ 132 uint8_t internalBufFlag; 133 /* used in write ndef, to know that last 16 bytes are used to write*/ 134 uint8_t RemainingBufFlag; 135 /* indicates that Read has reached the end of the card */ 136 uint8_t ReadWriteCompleteFlag; 137 /* indicates that Read has reached the end of the card */ 138 uint8_t ReadCompleteFlag; 139 /* indicates that Write is possible or not */ 140 uint8_t WriteFlag; 141 /* indicates that Write is possible or not */ 142 uint8_t ReadFlag; 143 /* indicates that Write is possible or not */ 144 uint8_t RdBeforeWrFlag; 145 /* Authentication Flag indicating that a particular sector is authenticated or 146 * not */ 147 uint8_t AuthDone; 148 /* to store the last Sector ID in Check Ndef */ 149 uint8_t SectorIndex; 150 /* to read the access bits of each sector */ 151 uint8_t ReadAcsBitFlag; 152 /* Flag to check if Acs bit was written in this call */ 153 uint8_t WriteAcsBitFlag; 154 /* Buffer to store 16 bytes */ 155 uint8_t Buffer[PH_FRINFC_NDEFMAP_MIFARESTD_RDWR_SIZE]; 156 /* to store the AIDs of Mifare 1k or 4k */ 157 uint8_t aid[PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK]; 158 /* flag to write with offset begin */ 159 uint8_t WrNdefFlag; 160 /* flag to read with offset begin */ 161 uint8_t ReadNdefFlag; 162 /* flag to check with offset begin */ 163 uint8_t ChkNdefFlag; 164 /* To store the remaining size of the Mifare 1k or 4k card */ 165 uint16_t remainingSize; 166 /* To update the remaining size when writing to the Mifare 1k or 4k card */ 167 uint8_t remSizeUpdFlag; 168 /* The flag is to know that there is a different AID apart from NFC forum 169 * sector AID */ 170 uint16_t aidCompleteFlag; 171 /* The flag is to know that there is a a NFC forum sector exists in the card 172 */ 173 uint16_t NFCforumSectFlag; 174 /* The flag is to know that the particular sector is a proprietary NFC forum 175 * sector */ 176 uint16_t ProprforumSectFlag; 177 /* The flag is set after reading the MAD sectors */ 178 uint16_t ChkNdefCompleteFlag; 179 /* Flag to store the current block */ 180 uint8_t TempBlockNo; 181 /* Completion routine index */ 182 uint8_t CRIndex; 183 /* Bytes remaining to write for one write procedure */ 184 uint16_t WrLength; 185 /* Flag to read after write */ 186 uint8_t RdAfterWrFlag; 187 /* Flag to say that poll is required before write ndef (authentication) */ 188 uint8_t PollFlag; 189 /* Flag is to know that this is first time the read has been called. This 190 is required when read is called after write (especially for the card formatted 191 with the 2nd configuration) */ 192 uint8_t FirstReadFlag; 193 /* Flag is to know that this is first time the write has been called. This 194 is required when the card formatted with the 3rd configuration */ 195 uint8_t FirstWriteFlag; 196 /* Indicates the sector trailor id for which the convert 197 to read only is currently in progress*/ 198 uint8_t ReadOnlySectorIndex; 199 /* Indicates the total number of sectors on the card */ 200 uint8_t TotalNoSectors; 201 /* Indicates the block number of the sector trailor on the card */ 202 uint8_t SectorTrailerBlockNo; 203 /* Secret key B to given by the application */ 204 uint8_t UserScrtKeyB[6]; 205 } phFriNfc_MifareStdCont_t; 206 207 /* 208 * NDEF TLV structure which details the different variables used for TLV. 209 * 210 */ 211 typedef struct phFriNfc_NDEFTLVCont { 212 /* Flag is to know that the TLV Type Found */ 213 uint8_t NdefTLVFoundFlag; 214 /* Sector number of the next/present available TLV */ 215 uint8_t NdefTLVSector; 216 /* Following two variables are used to store the 217 T byte and the Block number in which the T is 218 found in Tag */ 219 /* Byte number of the next/present available TLV */ 220 uint16_t NdefTLVByte; 221 /* Block number of the next/present available TLV */ 222 uint8_t NdefTLVBlock; 223 /* Authentication flag for NDEF TLV Block */ 224 uint8_t NdefTLVAuthFlag; 225 /* if the 16th byte of the last read is type (T) of TLV 226 and next read contains length (L) bytes of TLV. This flag 227 is set when the type (T) of TLV is found in the last read */ 228 uint8_t TcheckedinTLVFlag; 229 /* if the 16th byte of the last read is Length (L) of TLV 230 and next read contains length (L) bytes of TLV. This flag 231 is set when the Length (L) of TLV is found in the last read */ 232 uint8_t LcheckedinTLVFlag; 233 /* This flag is set, if Terminator TLV is already written 234 and next read contains value (V) bytes of TLV. This flag 235 is set when the value (V) of TLV is found in the last read */ 236 uint8_t SetTermTLVFlag; 237 /* To know the number of Length (L) field is present in the 238 next block */ 239 uint8_t NoLbytesinTLV; 240 /* The value of 3 bytes length(L) field in TLV. In 3 bytes 241 length field, 2 bytes are in one block and other 1 byte 242 is in the next block. To store the former block length 243 field value, this variable is used */ 244 uint16_t prevLenByteValue; 245 /* The value of length(L) field in TLV. */ 246 uint16_t BytesRemainLinTLV; 247 /* Actual size to read and write. This will be always equal to the 248 length (L) of TLV as there is only one NDEF TLV . */ 249 uint16_t ActualSize; 250 /* Flag is to write the length (L) field of the TLV */ 251 uint8_t WrLenFlag; 252 /* Flag is to write the length (L) field of the TLV */ 253 uint16_t NULLTLVCount; 254 /* Buffer to store 4 bytes of data which is written to a block */ 255 uint8_t NdefTLVBuffer[PH_FRINFC_NDEFMAP_MFUL_4BYTES_BUF]; 256 /* Buffer to store 4 bytes of data which is written to a next block */ 257 uint8_t NdefTLVBuffer1[PH_FRINFC_NDEFMAP_MFUL_4BYTES_BUF]; 258 } phFriNfc_NDEFTLVCont_t; 259 260 /* 261 * Lock Control TLV structure which stores the Position, Size and PageCntrl 262 * details. 263 */ 264 265 typedef struct phFriNfc_LockCntrlTLVCont { 266 /* Specifies the Byte Position of the lock cntrl tlv 267 in the card memory*/ 268 uint16_t ByteAddr; 269 270 /* Specifies the Size of the lock area in terms of 271 bits/bytes*/ 272 uint16_t Size; 273 274 /* Specifies the Bytes per Page*/ 275 uint8_t BytesPerPage; 276 277 /* Specifies the BytesLockedPerLockBit */ 278 uint8_t BytesLockedPerLockBit; 279 280 /* Specifies the index of Lock cntrl TLV*/ 281 uint8_t LockTlvBuffIdx; 282 283 /* Store the content of Lock cntrl TLV*/ 284 uint8_t LockTlvBuff[8]; 285 286 /* Specifies the Block number Lock cntrl TLV*/ 287 uint16_t BlkNum; 288 289 /* Specifies the Byte Number position of Lock cntrl TLV*/ 290 uint16_t ByteNum; 291 292 } phFriNfc_LockCntrlTLVCont_t; 293 294 /* 295 * Memory Control TLV structure which stores the Position, Size and PageCntrl 296 * details of the reserved byte area. 297 */ 298 299 typedef struct phFriNfc_ResMemCntrlTLVCont { 300 /* Specifies the Byte Position of the lock cntrl tlv 301 in the card memory */ 302 uint16_t ByteAddr; 303 304 /* Specifies the Size of the lock area in terms of 305 bits/bytes*/ 306 uint16_t Size; 307 308 /* Store the content of Memory cntrl TLV*/ 309 uint8_t MemCntrlTlvBuff[8]; 310 311 /* Specifies the Bytes per Page*/ 312 uint8_t BytesPerPage; 313 314 /* Specifies the index of Mem cntrl TLV*/ 315 uint8_t MemTlvBuffIdx; 316 317 /* Specifies the Block number Lock cntrl TLV*/ 318 uint16_t BlkNum; 319 320 /* Specifies the Byte Number position of Lock cntrl TLV*/ 321 uint16_t ByteNum; 322 323 } phFriNfc_ResMemCntrlTLVCont_t; 324 325 /* 326 * NFC NDEF Mapping Component Context Structure 327 * 328 * This structure is used to store the current context information of the 329 * instance. 330 * 331 */ 332 typedef struct phFriNfc_NdefMap { 333 /* The state of the operation. */ 334 uint8_t State; 335 336 /* Completion Routine Context. */ 337 phFriNfc_CplRt_t CompletionRoutine[PH_FRINFC_NDEFMAP_CR]; 338 339 phNfc_sTransceiveInfo_t* pTransceiveInfo; 340 341 /*Holds the completion routine informations of the Map Layer*/ 342 phFriNfc_CplRt_t MapCompletionInfo; 343 344 /* Pointer to the Remote Device Information */ 345 phLibNfc_sRemoteDevInformation_t* psRemoteDevInfo; 346 347 /*Holds the Command Type(read/write)*/ 348 phNfc_uCmdList_t Cmd; 349 350 /* Pointer to a temporary buffer. Could be 351 used for read/write purposes */ 352 uint8_t* ApduBuffer; 353 354 /* Size allocated to the ApduBuffer. */ 355 uint32_t ApduBufferSize; 356 357 /* Index to the APDU Buffer. Used for internal calculations */ 358 uint16_t ApduBuffIndex; 359 360 /* Pointer to the user-provided Data Size to be written trough WrNdef 361 * function. */ 362 uint32_t* WrNdefPacketLength; 363 364 /* Holds the length of the received data. */ 365 uint16_t* SendRecvLength; 366 367 /*Holds the ack of some initial commands*/ 368 uint8_t* SendRecvBuf; 369 370 /* Holds the length of the data to be sent. */ 371 uint16_t SendLength; 372 373 /* Data Byte Count, which gives the offset to the integration.*/ 374 uint16_t* DataCount; 375 376 /* Holds the previous operation on the card*/ 377 uint8_t PrevOperation; 378 379 /* Holds the previous read mode*/ 380 uint8_t bPrevReadMode; 381 382 /* Holds the current read mode*/ 383 uint8_t bCurrReadMode; 384 385 /* Holds the previous state on the card*/ 386 uint8_t PrevState; 387 388 /* Stores the type of the smart card. */ 389 uint8_t CardType; 390 391 /* Stores the card state. */ 392 uint8_t CardState; 393 394 /* Stores the memory size of the card */ 395 uint16_t CardMemSize; 396 397 /*to Store the page offset on the mifare ul card*/ 398 uint8_t Offset; 399 400 /* specifies the desired operation to be performed*/ 401 uint8_t DespOpFlag; 402 403 /* Used to remember how many bytes were written, to update 404 the dataCount and the BufferIndex */ 405 uint16_t NumOfBytesWritten; 406 407 /*used to remember number of L byte Remaining to be written */ 408 uint16_t NumOfLReminWrite; 409 410 /* Pointer Used to remember and return how many bytes were read, 411 to update the PacketDataLength in case of Read operation */ 412 /* Fix for 0000238: [gk] MAP: Number of bytes actually read out is 413 not returned. */ 414 uint32_t* NumOfBytesRead; 415 416 /* Flag used to tell the process function that WRITE has 417 requested for an internal READ.*/ 418 uint8_t ReadingForWriteOperation; 419 420 /* Buffer of 5 bytes used for the write operation for the 421 Mifare UL card.*/ 422 uint8_t BufferForWriteOp[5]; 423 424 /* Temporary Receive Length to update the Receive Length 425 when every time the Overlapped HAL is called. */ 426 uint16_t TempReceiveLength; 427 428 uint8_t NoOfDevices; 429 430 /* stores operating mode type of the felica smart tag */ 431 /* phHal_eOpModes_t OpModeType[2]; */ 432 433 /* stores the type of the TLV found */ 434 uint8_t TLVFoundFlag; 435 436 /* stores the TLV structure related informations */ 437 phFriNfc_NDEFTLVCont_t TLVStruct; 438 439 /* stores the Lock Contrl Tlv related informations */ 440 phFriNfc_LockCntrlTLVCont_t LockTlv; 441 442 /* stores the Mem Contrl Tlv related informations */ 443 phFriNfc_ResMemCntrlTLVCont_t MemTlv; 444 445 /* Pointer to the Mifare Standard capability Container Structure. */ 446 phFriNfc_MifareStdCont_t StdMifareContainer; 447 448 } phFriNfc_NdefMap_t; 449 450 /* 451 * States of the FSM. 452 */ 453 #define PH_FRINFC_NDEFMAP_STATE_RESET_INIT 0 /* Initial state */ 454 #define PH_FRINFC_NDEFMAP_STATE_CR_REGISTERED 1 /* CR has been registered */ 455 #define PH_FRINFC_NDEFMAP_STATE_EOF_CARD 2 /* EOF card reached */ 456 457 /* Following values specify the previous operation on the card. This value is 458 assigned to the context structure variable: PrevOperation. */ 459 460 /* Previous operation is check */ 461 #define PH_FRINFC_NDEFMAP_CHECK_OPE 1 462 /* Previous operation is read */ 463 #define PH_FRINFC_NDEFMAP_READ_OPE 2 464 /* Previous operation is write */ 465 #define PH_FRINFC_NDEFMAP_WRITE_OPE 3 466 /* Previous operation is Actual size */ 467 #define PH_FRINFC_NDEFMAP_GET_ACTSIZE_OPE 4 468 469 /* This flag is set when there is a need of write operation on the odd positions 470 ex: 35,5 etc. This is used with MfUlOp Flag */ 471 #define PH_FRINFC_MFUL_INTERNAL_READ 3 /* Read/Write control*/ 472 473 #endif /* PHFRINFC_NDEFMAP_H */ 474