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