1 /* 2 * Copyright (C) 2010 NXP Semiconductors 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 * \file phFriNfc_FelicaMap.c 19 * \brief This component encapsulates read/write/check ndef/process functionalities, 20 * for the Felica Smart Card. 21 * 22 * Project: NFC-FRI 23 * 24 * $Date: Thu May 6 14:01:35 2010 $ 25 * $Author: ing07385 $ 26 * $Revision: 1.10 $ 27 * $Aliases: NFC_FRI1.1_WK1017_R34_4,NFC_FRI1.1_WK1023_R35_1 $ 28 * 29 */ 30 31 #ifndef PH_FRINFC_MAP_FELICA_DISABLED 32 33 #include <phNfcTypes.h> 34 #include <phFriNfc_OvrHal.h> 35 #include <phFriNfc_FelicaMap.h> 36 #include <phFriNfc_MapTools.h> 37 38 39 /*! \ingroup grp_file_attributes 40 * \name NDEF Mapping 41 * 42 * File: \ref phFriNfc_FelicaMap.c 43 * 44 */ 45 /*@{*/ 46 47 #define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.10 $" 48 #define PHFRINFCNDEFMAP_FILEALIASES "$Aliases: NFC_FRI1.1_WK1017_R34_4,NFC_FRI1.1_WK1023_R35_1 $" 49 50 /*@}*/ 51 52 /* Helpers for Read and updating the attribute informations*/ 53 static NFCSTATUS phFriNfc_Felica_HRdAttrInfo(phFriNfc_NdefMap_t *NdefMap); 54 static NFCSTATUS phFriNfc_Felica_HUpdateAttrInfo(phFriNfc_NdefMap_t *NdefMap); 55 static NFCSTATUS phFriNfc_Felica_HCalCheckSum(const uint8_t *TempBuffer, 56 uint8_t StartIndex, 57 uint8_t EndIndex, 58 uint16_t RecvChkSum); 59 60 /* Helpers for Poll Related Operations*/ 61 static NFCSTATUS phFriNfc_Felica_HPollCard( phFriNfc_NdefMap_t *NdefMap, 62 const uint8_t sysCode[], 63 uint8_t state); 64 65 static NFCSTATUS phFriNfc_Felica_HUpdateManufIdDetails(const phFriNfc_NdefMap_t *NdefMap); 66 67 /*Helpers for Reading Operations*/ 68 static NFCSTATUS phFriNfc_Felica_HReadData(phFriNfc_NdefMap_t *NdefMap,uint8_t offset); 69 static uint16_t phFriNfc_Felica_HGetMaximumBlksToRead(const phFriNfc_NdefMap_t *NdefMap,uint8_t NbcOrNmaxb ); 70 static void phFriNfc_Felica_HAfterRead_CopyDataToBuff(phFriNfc_NdefMap_t *NdefMap); 71 static NFCSTATUS phFriNfc_Felica_HSetTransceiveForRead(phFriNfc_NdefMap_t *NdefMap,uint16_t TrxLen,uint8_t Offset); 72 static uint16_t phFriNfc_Felica_HSetTrxLen(phFriNfc_NdefMap_t *NdefMap,uint16_t Nbc); 73 static NFCSTATUS phFriNfc_Felica_HChkApduBuff_Size( phFriNfc_NdefMap_t *NdefMap); 74 75 /* Helpers for Writing Operations*/ 76 static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap); 77 static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForRdOp(phFriNfc_NdefMap_t *NdefMap, 78 uint32_t NdefLen); 79 static NFCSTATUS phFriNfc_Felica_HUpdateAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap,uint8_t isStarted); 80 static NFCSTATUS phFriNfc_Felica_HUpdateData(phFriNfc_NdefMap_t *NdefMap); 81 static NFCSTATUS phFriNfc_Felica_HWriteDataBlk(phFriNfc_NdefMap_t *NdefMap); 82 83 /* Write Empty NDEF Message*/ 84 static NFCSTATUS phFriNfc_Felica_HWrEmptyMsg(phFriNfc_NdefMap_t *NdefMap); 85 86 /*Helpers for common checks*/ 87 static NFCSTATUS phFriNfc_Felica_HCheckManufId(const phFriNfc_NdefMap_t *NdefMap); 88 static void phFriNfc_Felica_HCrHandler(phFriNfc_NdefMap_t *NdefMap, 89 uint8_t CrIndex, 90 NFCSTATUS Status); 91 92 static void phFriNfc_Felica_HInitInternalBuf(uint8_t *Buffer); 93 94 static int phFriNfc_Felica_MemCompare ( void *s1, void *s2, unsigned int n ); 95 96 /*! 97 * \brief returns maximum number of blocks can be read from the Felica Smart Card. 98 * 99 * The function is useful in reading of NDEF information from a felica tag. 100 */ 101 102 static uint16_t phFriNfc_Felica_HGetMaximumBlksToRead(const phFriNfc_NdefMap_t *NdefMap, uint8_t NbcOrNmaxb ) 103 { 104 uint16_t BlksToRead=0; 105 uint32_t DataLen = 0; 106 /* This part of the code is useful if we take account of Nbc blks reading*/ 107 if ( NbcOrNmaxb == PH_NFCFRI_NDEFMAP_FELI_NBC ) 108 { 109 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0], 110 NdefMap->FelicaAttrInfo.LenBytes[1], 111 NdefMap->FelicaAttrInfo.LenBytes[2], 112 DataLen); 113 /* Calculate Nbc*/ 114 BlksToRead = (uint16_t) ( ((DataLen % 16) == 0) ? (DataLen >> 4) : ((DataLen >> 4) +1) ); 115 116 117 } 118 else if ( NbcOrNmaxb == PH_NFCFRI_NDEFMAP_FELI_NMAXB) 119 { 120 BlksToRead = NdefMap->FelicaAttrInfo.Nmaxb; 121 } 122 else 123 { 124 /* WARNING !!! code should not reach this point*/ 125 ; 126 } 127 return (BlksToRead); 128 } 129 130 /*! 131 * \brief Initiates Reading of NDEF information from the Felica Card. 132 * 133 * The function initiates the reading of NDEF information from a Remote Device. 134 * It performs a reset of the state and starts the action (state machine). 135 * A periodic call of the \ref phFriNfcNdefMap_Process has to be 136 * done once the action has been triggered. 137 */ 138 139 NFCSTATUS phFriNfc_Felica_RdNdef( phFriNfc_NdefMap_t *NdefMap, 140 uint8_t *PacketData, 141 uint32_t *PacketDataLength, 142 uint8_t Offset) 143 { 144 145 NFCSTATUS status = NFCSTATUS_PENDING; 146 uint32_t Nbc = 0; 147 148 NdefMap->ApduBufferSize = *PacketDataLength; 149 /*Store the packet data buffer*/ 150 NdefMap->ApduBuffer = PacketData; 151 152 NdefMap->NumOfBytesRead = PacketDataLength ; 153 *NdefMap->NumOfBytesRead = 0; 154 NdefMap->ApduBuffIndex = 0; 155 156 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_READ_OPE; 157 NdefMap->Felica.Offset = Offset; 158 159 if( ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )||( NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE)) 160 { 161 NdefMap->Felica.CurBlockNo = 0; 162 NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_RD_ATTR_RD_OP; 163 NdefMap->Felica.IntermediateCpyFlag = FALSE; 164 NdefMap->Felica.IntermediateCpyLen = 0; 165 NdefMap->Felica.Rd_NoBytesToCopy = 0; 166 NdefMap->Felica.EofCardReachedFlag= FALSE ; 167 NdefMap->Felica.LastBlkReachedFlag = FALSE; 168 NdefMap->Felica.CurrBytesRead = 0; 169 170 phFriNfc_Felica_HInitInternalBuf(NdefMap->Felica.Rd_BytesToCopyBuff); 171 172 /* send request to read attribute information*/ 173 status = phFriNfc_Felica_HRdAttrInfo(NdefMap); 174 /* handle the error in Transc function*/ 175 if ( (status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 176 { 177 /* call respective CR */ 178 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,status); 179 } 180 } 181 else 182 { 183 Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC); 184 185 /* Offset = Current, but the read has reached the End of NBC Blocks */ 186 if(( ( Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (NdefMap->Felica.CurBlockNo == Nbc)) && 187 (NdefMap->Felica.EofCardReachedFlag == FELICA_RD_WR_EOF_CARD_REACHED )) 188 { 189 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED); 190 } 191 else 192 { 193 194 NdefMap->Felica.CurrBytesRead = ((NdefMap->Felica.CurBlockNo * 16)- NdefMap->Felica.Rd_NoBytesToCopy); 195 status = phFriNfc_Felica_HReadData(NdefMap,NdefMap->Felica.Offset); 196 197 } 198 } 199 return (status); 200 } 201 202 /*Read Operation Related Helper Routines*/ 203 204 /*! 205 * \brief Used in Read Opearation.Sets the Trx Buffer Len calls Transc Cmd. 206 * After a successful read operation, function does checks the user buffer size 207 * sets the status flags. 208 */ 209 210 static NFCSTATUS phFriNfc_Felica_HReadData(phFriNfc_NdefMap_t *NdefMap,uint8_t offset) 211 { 212 NFCSTATUS status = NFCSTATUS_PENDING; 213 uint16_t Nbc=0,TranscLen=0; 214 215 Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC); 216 if( ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) > 0) && (NdefMap->Felica.CurBlockNo < Nbc )) 217 { 218 /* if data is present in the internal buffer*/ 219 if (NdefMap->Felica.Rd_NoBytesToCopy > 0 ) 220 { 221 /* copy data to external buffer*/ 222 phFriNfc_Felica_HAfterRead_CopyDataToBuff(NdefMap); 223 /*Check the size of user buffer*/ 224 status = phFriNfc_Felica_HChkApduBuff_Size(NdefMap); 225 if ( (status != NFCSTATUS_SUCCESS) && (NdefMap->Felica.IntermediateRdFlag == TRUE )) 226 { 227 /* set the transc len and call transc cmd*/ 228 TranscLen = phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc); 229 status= phFriNfc_Felica_HSetTransceiveForRead(NdefMap,TranscLen,offset); 230 } 231 else 232 { 233 /* Nothing to be done , if IntermediateRdFlag is set to zero*/ 234 ; 235 } 236 } 237 else 238 { 239 /* set the transc len and call transc cmd*/ 240 TranscLen = phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc); 241 status= phFriNfc_Felica_HSetTransceiveForRead(NdefMap,TranscLen,offset); 242 } 243 } 244 else 245 { 246 /* Chk the Buffer size*/ 247 status = phFriNfc_Felica_HChkApduBuff_Size(NdefMap); 248 if ( (status != NFCSTATUS_SUCCESS) && (NdefMap->Felica.IntermediateRdFlag == TRUE )) 249 { 250 TranscLen = phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc); 251 status= phFriNfc_Felica_HSetTransceiveForRead(NdefMap,TranscLen,offset); 252 } 253 } 254 return (status); 255 } 256 257 /*! 258 * \brief Used in Read Opearation.Sets the Trx Buffer Len. 259 */ 260 261 static uint16_t phFriNfc_Felica_HSetTrxLen(phFriNfc_NdefMap_t *NdefMap,uint16_t Nbc) 262 { 263 uint16_t TranscLen = 0,BlocksToRead=0; 264 265 if( ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)% 16) == 0) 266 { 267 BlocksToRead = (uint16_t)( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)/16 ); 268 } 269 else 270 { 271 BlocksToRead = (uint16_t)(((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)/16) +1); 272 } 273 if ( (BlocksToRead > Nbc) ||( (BlocksToRead) > ( Nbc - NdefMap->Felica.CurBlockNo)) ) 274 { 275 BlocksToRead = Nbc - NdefMap->Felica.CurBlockNo; 276 } 277 278 279 if ( BlocksToRead >= NdefMap->FelicaAttrInfo.Nbr) 280 { 281 if( NdefMap->FelicaAttrInfo.Nbr < Nbc ) 282 { 283 TranscLen = NdefMap->FelicaAttrInfo.Nbr*16; 284 } 285 else 286 { 287 TranscLen = Nbc*16; 288 NdefMap->Felica.LastBlkReachedFlag =1; 289 } 290 } 291 else 292 { 293 if (BlocksToRead <= Nbc ) 294 { 295 if ( ( BlocksToRead * 16) == ((Nbc *16) - (NdefMap->Felica.CurBlockNo * 16))) 296 { 297 NdefMap->Felica.LastBlkReachedFlag =1; 298 299 } 300 TranscLen = BlocksToRead*16; 301 302 } 303 else 304 { 305 TranscLen = Nbc*16; 306 } 307 } 308 /* As Cur Blk changes, to remember the exact len what we had set 309 in the begining of each read operation*/ 310 NdefMap->Felica.TrxLen = TranscLen; 311 return (TranscLen); 312 } 313 314 /*! 315 * \brief Used in Read Opearation.After a successful read operation, 316 * Copies the data to user buffer. 317 */ 318 319 static void phFriNfc_Felica_HAfterRead_CopyDataToBuff(phFriNfc_NdefMap_t *NdefMap) 320 { 321 uint8_t ResetFlag = FALSE, ExtrBytesToCpy = FALSE; 322 uint16_t Nbc=0; 323 uint32_t DataLen=0; 324 325 Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC ); 326 327 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0], 328 NdefMap->FelicaAttrInfo.LenBytes[1], 329 NdefMap->FelicaAttrInfo.LenBytes[2], 330 DataLen); 331 /* Internal Buffer has some old read bytes to cpy to user buffer*/ 332 if( NdefMap->Felica.Rd_NoBytesToCopy > 0 ) 333 { 334 if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) < NdefMap->Felica.Rd_NoBytesToCopy ) 335 { 336 NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 337 338 if (NdefMap->Felica.IntermediateCpyFlag == TRUE ) 339 { 340 /*Copy data from the internal buffer to user buffer*/ 341 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 342 (&(NdefMap->Felica.Rd_BytesToCopyBuff[NdefMap->Felica.IntermediateCpyLen])), 343 (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)); 344 345 346 347 /* Store number of bytes copied frm internal buffer to User Buffer */ 348 NdefMap->Felica.IntermediateCpyLen += (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 349 NdefMap->Felica.IntermediateCpyFlag = 1; 350 351 /* check do we reach len bytes any chance*/ 352 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0], 353 NdefMap->FelicaAttrInfo.LenBytes[1], 354 NdefMap->FelicaAttrInfo.LenBytes[2], 355 DataLen); 356 /* Internal buffer has zero bytes for copy operation*/ 357 if ( NdefMap->Felica.Rd_NoBytesToCopy == 0) 358 { 359 NdefMap->Felica.EofCardReachedFlag =FELICA_RD_WR_EOF_CARD_REACHED; 360 } 361 } 362 else 363 { 364 /*Copy data from the internal buffer to apdu buffer*/ 365 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 366 NdefMap->Felica.Rd_BytesToCopyBuff, 367 (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)); 368 } 369 NdefMap->ApduBuffIndex += (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 370 371 } 372 else if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) == NdefMap->Felica.Rd_NoBytesToCopy ) 373 { 374 if ( NdefMap->Felica.IntermediateCpyFlag == TRUE ) 375 { 376 /*Copy data internal buff to apdubuffer*/ 377 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 378 (&(NdefMap->Felica.Rd_BytesToCopyBuff[NdefMap->Felica.IntermediateCpyLen])), 379 NdefMap->Felica.Rd_NoBytesToCopy); 380 } 381 else 382 { 383 /*Copy data internal buff to apdubuffer*/ 384 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 385 NdefMap->Felica.Rd_BytesToCopyBuff, 386 NdefMap->Felica.Rd_NoBytesToCopy); 387 } 388 389 /*increment the index,internal buffer len*/ 390 NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy; 391 NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBuffIndex); 392 393 /* To reset the parameters*/ 394 ResetFlag = TRUE; 395 } 396 else 397 { 398 /* Extra Bytes to Copy from internal buffer to external buffer*/ 399 if ( NdefMap->Felica.IntermediateCpyFlag == TRUE ) 400 { 401 /*Copy data internal buff to apdubuffer*/ 402 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 403 (&(NdefMap->Felica.Rd_BytesToCopyBuff[NdefMap->Felica.IntermediateCpyLen])), 404 NdefMap->Felica.Rd_NoBytesToCopy); 405 } 406 else 407 { 408 /*Copy data internal buff to apdubuffer*/ 409 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 410 NdefMap->Felica.Rd_BytesToCopyBuff, 411 NdefMap->Felica.Rd_NoBytesToCopy); 412 } 413 /*increment the index*/ 414 NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy; 415 416 /* To reset the parameters*/ 417 ResetFlag = TRUE; 418 } 419 }/*End of Internal Buffer has some old read bytes to cpy to user buffer*/ 420 else 421 { 422 /* check if last block is reached*/ 423 if ( ((NdefMap->Felica.LastBlkReachedFlag == 1) && (( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= 16)) ) 424 { 425 /* greater than 16 but less than the data len size*/ 426 if (( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= DataLen) 427 { 428 NdefMap->Felica.CurrBytesRead = (uint16_t)((DataLen) - (NdefMap->Felica.CurrBytesRead + 429 NdefMap->ApduBuffIndex)); 430 431 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 432 (&(NdefMap->SendRecvBuf[13])), 433 NdefMap->Felica.CurrBytesRead); 434 435 NdefMap->ApduBuffIndex += NdefMap->Felica.CurrBytesRead; 436 if ( NdefMap->ApduBuffIndex == DataLen) 437 { 438 ResetFlag = TRUE; 439 } 440 } 441 else 442 { 443 /* need to check exact no. of bytes to copy to buffer*/ 444 if( ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) <= NdefMap->Felica.TrxLen )|| 445 ((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) <= DataLen )) 446 { 447 448 ExtrBytesToCpy = TRUE; 449 } 450 else 451 { 452 NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)(16-(( Nbc * 16) - (DataLen))); 453 454 if ( NdefMap->Felica.Rd_NoBytesToCopy > (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)) 455 { 456 /*Reduce already copied bytes from the internal buffer*/ 457 NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 458 ExtrBytesToCpy = TRUE; 459 } 460 else 461 { 462 ExtrBytesToCpy = FALSE; 463 } 464 } 465 if ( ExtrBytesToCpy == TRUE ) 466 { 467 NdefMap->Felica.CurrBytesRead = (uint16_t)((DataLen)- (NdefMap->Felica.CurrBytesRead + 468 NdefMap->ApduBuffIndex)); 469 470 if(NdefMap->Felica.CurrBytesRead < 471 (uint16_t)(NdefMap->ApduBufferSize - 472 NdefMap->ApduBuffIndex)) 473 { 474 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 475 (&(NdefMap->SendRecvBuf[13])), 476 NdefMap->Felica.CurrBytesRead); 477 } 478 else 479 { 480 (void)memcpy( (&( NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 481 (&( NdefMap->SendRecvBuf[13])), 482 (uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)); 483 } 484 485 if ( NdefMap->Felica.LastBlkReachedFlag == 1 ) 486 { 487 NdefMap->Felica.Rd_NoBytesToCopy = 488 (uint8_t)((NdefMap->Felica.CurrBytesRead > 489 (uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex))? 490 (NdefMap->Felica.CurrBytesRead - 491 (uint16_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)): 492 0); 493 494 ResetFlag = ((NdefMap->Felica.Rd_NoBytesToCopy == 0)?TRUE:FALSE); 495 496 } 497 else 498 { 499 NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)( NdefMap->Felica.TrxLen - (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)); 500 } 501 502 /* Copy remained bytes back into internal buffer*/ 503 (void)memcpy( NdefMap->Felica.Rd_BytesToCopyBuff, 504 (&(NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_RESP_HEADER_LEN+(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)])), 505 NdefMap->Felica.Rd_NoBytesToCopy); 506 507 /* set the intermediate flag : This flag remembers that there are still X no. bytes remained in 508 Internal Buffer Ex: User has given only one byte buffer,needs to cpy one byte at a time*/ 509 NdefMap->Felica.IntermediateCpyFlag = TRUE; 510 511 NdefMap->ApduBuffIndex += ((NdefMap->Felica.CurrBytesRead < 512 (uint16_t)(NdefMap->ApduBufferSize - 513 NdefMap->ApduBuffIndex))? 514 NdefMap->Felica.CurrBytesRead: 515 (uint16_t)(NdefMap->ApduBufferSize - 516 NdefMap->ApduBuffIndex)); 517 } 518 else 519 { 520 /*Copy data from the internal buffer to user buffer*/ 521 (void)memcpy( (&( NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 522 (&( NdefMap->SendRecvBuf[13])), 523 NdefMap->Felica.Rd_NoBytesToCopy); 524 525 NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy; 526 ResetFlag = TRUE; 527 528 } 529 } 530 531 } 532 else 533 { 534 if ((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) < NdefMap->Felica.TrxLen ) 535 { 536 /* Calculate exactly remained bytes to copy to internal buffer and set it*/ 537 if ( NdefMap->Felica.LastBlkReachedFlag == 1) 538 { 539 NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)(16-(( Nbc * 16) - DataLen)); 540 541 if ( NdefMap->Felica.Rd_NoBytesToCopy > (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)) 542 { 543 /*Reduce already copied bytes from the internal buffer*/ 544 NdefMap->Felica.Rd_NoBytesToCopy -= (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 545 ExtrBytesToCpy = TRUE; 546 } 547 } 548 else 549 { 550 NdefMap->Felica.Rd_NoBytesToCopy = (uint8_t)(NdefMap->Felica.TrxLen - (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)); 551 ExtrBytesToCpy = TRUE; 552 } 553 if ( ExtrBytesToCpy == TRUE ) 554 { 555 /*Copy the read data from trx buffer to apdu of size apdu*/ 556 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 557 (&(NdefMap->SendRecvBuf[13])), 558 NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 559 560 /*copy bytesToCopy to internal buffer*/ 561 (void)memcpy( NdefMap->Felica.Rd_BytesToCopyBuff, 562 (&(NdefMap->SendRecvBuf[13+(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)])), 563 NdefMap->Felica.Rd_NoBytesToCopy); 564 565 NdefMap->Felica.IntermediateCpyFlag = TRUE; 566 NdefMap->ApduBuffIndex += (uint16_t)NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex; 567 } 568 else 569 { 570 /*Copy data from the internal buffer to user buffer*/ 571 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 572 (&(NdefMap->SendRecvBuf[13])), 573 NdefMap->Felica.Rd_NoBytesToCopy); 574 575 NdefMap->ApduBuffIndex += NdefMap->Felica.Rd_NoBytesToCopy; 576 ResetFlag = TRUE; 577 578 } 579 if ( DataLen <= (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) ) 580 { 581 NdefMap->Felica.EofCardReachedFlag =FELICA_RD_WR_EOF_CARD_REACHED; 582 } 583 else 584 { 585 ; 586 } 587 } 588 else if ((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) == NdefMap->Felica.TrxLen ) 589 { 590 /*Copy exactly remained last bytes to user buffer and increment the index*/ 591 /*13 : 1+12 : 1st byte entire pkt length + 12 bytes to skip manuf details*/ 592 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 593 (&(NdefMap->SendRecvBuf[13])), 594 (NdefMap->Felica.TrxLen )); 595 596 NdefMap->ApduBuffIndex += NdefMap->Felica.TrxLen; 597 } 598 else 599 { 600 if ((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) > NdefMap->Felica.TrxLen ) 601 { 602 /*Copy the data to apdu buffer and increment the index */ 603 (void)memcpy( (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 604 (&(NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_RESP_HEADER_LEN])), 605 NdefMap->Felica.TrxLen); 606 607 NdefMap->ApduBuffIndex += (uint16_t)NdefMap->Felica.TrxLen; 608 } 609 } 610 } 611 } 612 if ( ResetFlag == TRUE ) 613 { 614 /* reset the internal buffer variables*/ 615 NdefMap->Felica.Rd_NoBytesToCopy =0; 616 NdefMap->Felica.IntermediateCpyLen =0; 617 NdefMap->Felica.IntermediateCpyFlag =FALSE; 618 } 619 return; 620 } 621 622 623 /*! 624 * \brief Used in Read Opearation.After a successful read operation, 625 Checks the relavent buffer sizes and set the status.Following function is used 626 when we read the Nmaxb blocks. Retained for future purpose. 627 */ 628 629 static NFCSTATUS phFriNfc_Felica_HChkApduBuff_Size( phFriNfc_NdefMap_t *NdefMap) 630 { 631 NFCSTATUS status = NFCSTATUS_PENDING; 632 uint8_t ResetFlag = FALSE; 633 uint32_t Nbc = 0; 634 uint32_t DataLen = 0; 635 636 Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC); 637 638 /* set status to Success : User Buffer is full and Curblk < nmaxb*/ 639 if ( (( NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0) && 640 (NdefMap->Felica.CurBlockNo < Nbc )) 641 { 642 status = PHNFCSTVAL(CID_NFC_NONE, 643 NFCSTATUS_SUCCESS); 644 /*Reset the index, internal buffer counters back to zero*/ 645 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 646 NdefMap->ApduBuffIndex = 0; 647 648 }/*if( (NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0 && NdefMap->Felica.CurBlockNo < NdefMap->FelicaAttrInfo.Nmaxb )*/ 649 else 650 { 651 if (( ( NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0) && 652 (NdefMap->Felica.CurBlockNo == Nbc )) 653 { 654 status = PHNFCSTVAL(CID_NFC_NONE, 655 NFCSTATUS_SUCCESS); 656 657 ResetFlag = ((NdefMap->Felica.Rd_NoBytesToCopy > 0 )? 658 FALSE: 659 TRUE); 660 if( ResetFlag== FALSE) 661 { 662 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 663 /*Reset the index, internal buffer counters back to zero*/ 664 NdefMap->ApduBuffIndex = 0; 665 } 666 }/*if ((NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )== 0 && NdefMap->Felica.CurBlockNo == NdefMap->FelicaAttrInfo.Nmaxb )*/ 667 else 668 { 669 /* reached reading all the blks available in the card: set EOF flag*/ 670 if ( NdefMap->ApduBuffIndex == (Nbc*16)) 671 { 672 status = PHNFCSTVAL(CID_NFC_NONE, 673 NFCSTATUS_SUCCESS); 674 ResetFlag = TRUE; 675 } 676 else 677 { 678 if ((NdefMap->ApduBufferSize-NdefMap->ApduBuffIndex )> 0 ) 679 { 680 if ( NdefMap->Felica.CurBlockNo == Nbc ) 681 { 682 /* bytes pending in internal buffer , No Space in User Buffer*/ 683 if ( NdefMap->Felica.Rd_NoBytesToCopy > 0) 684 { 685 if ( NdefMap->Felica.EofCardReachedFlag == TRUE ) 686 { 687 status = PHNFCSTVAL(CID_NFC_NONE, 688 NFCSTATUS_SUCCESS); 689 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 690 NdefMap->ApduBuffIndex=0; 691 } 692 else 693 { 694 phFriNfc_Felica_HAfterRead_CopyDataToBuff(NdefMap); 695 if( NdefMap->Felica.Rd_NoBytesToCopy > 0 ) 696 { 697 status = PHNFCSTVAL(CID_NFC_NONE, 698 NFCSTATUS_SUCCESS); 699 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 700 NdefMap->ApduBuffIndex=0; 701 } 702 else 703 { 704 /* EOF Card Reached set the internal EOF Flag*/ 705 status = PHNFCSTVAL(CID_NFC_NONE, 706 NFCSTATUS_SUCCESS); 707 708 ResetFlag = TRUE; 709 } 710 } 711 } 712 /* All bytes from internal buffer are copied and set eof flag*/ 713 else 714 { 715 status = PHNFCSTVAL(CID_NFC_NONE, 716 NFCSTATUS_SUCCESS); 717 ResetFlag = TRUE; 718 } 719 } 720 else 721 { 722 /* This flag is set to ensure that, need of Read Opearation 723 we completed coying the data from internal buffer to external buffer 724 left some more bytes,in User bufer so initiate the read operation */ 725 NdefMap->Felica.IntermediateRdFlag = TRUE; 726 } 727 } 728 else 729 { 730 status = PHNFCSTVAL(CID_NFC_NONE, 731 NFCSTATUS_SUCCESS); 732 } 733 } 734 } 735 if ( ResetFlag == TRUE) 736 { 737 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0], 738 NdefMap->FelicaAttrInfo.LenBytes[1], 739 NdefMap->FelicaAttrInfo.LenBytes[2], 740 DataLen); 741 if (NdefMap->ApduBuffIndex > DataLen) 742 { 743 *NdefMap->NumOfBytesRead = DataLen; 744 } 745 else 746 { 747 *NdefMap->NumOfBytesRead = NdefMap->ApduBuffIndex; 748 } 749 /*Reset the index, internal buffer counters back to zero*/ 750 NdefMap->ApduBuffIndex = 0; 751 NdefMap->Felica.Rd_NoBytesToCopy=0; 752 NdefMap->Felica.EofCardReachedFlag=FELICA_RD_WR_EOF_CARD_REACHED; 753 754 } 755 756 } 757 return( status); 758 } 759 760 /*! 761 * \brief Used in Read Opearation.Sets the transceive Command for read. 762 */ 763 static NFCSTATUS phFriNfc_Felica_HSetTransceiveForRead(phFriNfc_NdefMap_t *NdefMap,uint16_t TrxLen,uint8_t Offset) 764 { 765 NFCSTATUS TrxStatus = NFCSTATUS_PENDING; 766 uint16_t BufIndex=0,i=0; 767 768 /* set the felica cmd */ 769 #ifdef PH_HAL4_ENABLE 770 NdefMap->Cmd.FelCmd = phHal_eFelica_Raw; 771 #else 772 NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd; 773 #endif /* #ifdef PH_HAL4_ENABLE */ 774 775 /*Change the state to Read */ 776 NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_RD_BLOCK; 777 778 /* set the complition routines for the mifare operations */ 779 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_Felica_Process; 780 NdefMap->MapCompletionInfo.Context = NdefMap; 781 782 /*set the additional informations for the data exchange*/ 783 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0; 784 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0; 785 786 /* pkt len : updated at the end*/ 787 NdefMap->SendRecvBuf[BufIndex] = 0x00; 788 BufIndex ++; 789 790 NdefMap->SendRecvBuf[BufIndex] = 0x06; 791 BufIndex++; 792 793 /* IDm - Manufacturer Id : 8bytes*/ 794 #ifdef PH_HAL4_ENABLE 795 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 796 (void * )(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)), 797 8); 798 #else 799 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 800 (void * )(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)), 801 8); 802 #endif /* #ifdef PH_HAL4_ENABLE */ 803 804 BufIndex+=8; 805 806 /*Number of Services (n=1 ==> 0x80)*/ 807 NdefMap->SendRecvBuf[BufIndex] = 0x01; 808 BufIndex++; 809 810 /*Service Code List*/ 811 NdefMap->SendRecvBuf[BufIndex] = 0x0B; 812 BufIndex++; 813 814 NdefMap->SendRecvBuf[BufIndex] = 0x00; 815 BufIndex++; 816 817 /*Number of Blocks to read*/ 818 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(TrxLen/16); 819 BufIndex++; 820 /* Set the Blk numbers as per the offset set by the user : Block List*/ 821 if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN ) 822 { 823 for ( i=0;i<(TrxLen/16);i++) 824 { 825 /*1st Service Code list : byte 1*/ 826 NdefMap->SendRecvBuf[BufIndex] = 0x80; 827 BufIndex++; 828 829 /* No. Of Blocks*/ 830 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(i + 1); 831 BufIndex++; 832 } 833 } 834 else 835 { 836 for ( i= 1;i<=(TrxLen/16);i++) 837 { 838 /*1st Service Code list : byte 1*/ 839 NdefMap->SendRecvBuf[BufIndex] = 0x80; 840 BufIndex++; 841 842 /* No. Of Blocks*/ 843 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(NdefMap->Felica.CurBlockNo + i); 844 BufIndex++; 845 } 846 } 847 848 /* len of entire pkt*/ 849 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = (uint8_t) BufIndex; 850 851 /* Set the Pkt Len*/ 852 NdefMap->SendLength = BufIndex; 853 854 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 855 856 TrxStatus = phFriNfc_OvrHal_Transceive(NdefMap->LowerDevice, 857 &NdefMap->MapCompletionInfo, 858 NdefMap->psRemoteDevInfo, 859 NdefMap->Cmd, 860 &NdefMap->psDepAdditionalInfo, 861 NdefMap->SendRecvBuf, 862 NdefMap->SendLength, 863 NdefMap->SendRecvBuf, 864 NdefMap->SendRecvLength); 865 return (TrxStatus); 866 } 867 868 /*! 869 * \brief Initiates Writing of NDEF information to the Remote Device. 870 * 871 * The function initiates the writing of NDEF information to a Remote Device. 872 * It performs a reset of the state and starts the action (state machine). 873 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action 874 * has been triggered. 875 */ 876 877 NFCSTATUS phFriNfc_Felica_WrNdef( phFriNfc_NdefMap_t *NdefMap, 878 uint8_t *PacketData, 879 uint32_t *PacketDataLength, 880 uint8_t Offset) 881 { 882 883 NFCSTATUS status = NFCSTATUS_PENDING; 884 885 NdefMap->ApduBufferSize = *PacketDataLength; 886 /*Store the packet data buffer*/ 887 NdefMap->ApduBuffer = PacketData; 888 889 /* To Update the Acutal written bytes to context*/ 890 NdefMap->WrNdefPacketLength = PacketDataLength; 891 *NdefMap->WrNdefPacketLength = 0; 892 893 894 NdefMap->PrevOperation = PH_FRINFC_NDEFMAP_WRITE_OPE; 895 NdefMap->Felica.Offset = Offset; 896 897 NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_WR_ATTR_RD_OP; 898 status = phFriNfc_Felica_HRdAttrInfo(NdefMap); 899 /* handle the error in Transc function*/ 900 if ( (status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 901 { 902 /* call respective CR */ 903 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,status); 904 } 905 return (status); 906 } 907 908 /*! 909 * \brief Initiates Writing of Empty NDEF information to the Remote Device. 910 * 911 * The function initiates the writing empty of NDEF information to a Remote Device. 912 * It performs a reset of the state and starts the action (state machine). 913 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action 914 * has been triggered. 915 */ 916 917 NFCSTATUS phFriNfc_Felica_EraseNdef( phFriNfc_NdefMap_t *NdefMap) 918 { 919 920 NFCSTATUS status = NFCSTATUS_PENDING; 921 static uint32_t PktDtLength =0; 922 923 if ( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID ) 924 { 925 /* Card is in invalid state, cannot have any read/write 926 operations*/ 927 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 928 NFCSTATUS_INVALID_FORMAT); 929 } 930 else if ( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY ) 931 { 932 /*Can't write to the card :No Grants */ 933 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 934 NFCSTATUS_WRITE_FAILED); 935 /* set the no. bytes written is zero*/ 936 NdefMap->WrNdefPacketLength = &PktDtLength; 937 *NdefMap->WrNdefPacketLength = 0; 938 } 939 else 940 { 941 942 /* set the Operation*/ 943 NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_WR_EMPTY_MSG_OP; 944 945 status = phFriNfc_Felica_HRdAttrInfo(NdefMap); 946 } 947 return (status); 948 } 949 950 951 /*! 952 * \brief Used in Write Opearation. 953 * check the value set for the Write Flag, in first write operation(begin), sets the 954 * WR flag in attribute blck. 955 * After a successful write operation, This function sets the WR flag off and updates 956 * the LEN bytes in attribute Block. 957 */ 958 959 static NFCSTATUS phFriNfc_Felica_HUpdateAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap,uint8_t isStarted) 960 { 961 NFCSTATUS status = NFCSTATUS_PENDING; 962 963 uint16_t ChkSum=0,index=0; 964 uint8_t BufIndex=0, ErrFlag = FALSE; 965 uint32_t TotNoWrittenBytes=0; 966 967 /* Write Operation : Begin/End Check*/ 968 969 NdefMap->State = 970 (( isStarted == FELICA_WRITE_STARTED )? 971 PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN: 972 PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END); 973 974 if( ( NdefMap->State == PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN)|| 975 ( NdefMap->State == PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END) ) 976 { 977 978 /* Set the Felica Cmd*/ 979 #ifdef PH_HAL4_ENABLE 980 NdefMap->Cmd.FelCmd = phHal_eFelica_Raw; 981 #else 982 NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd; 983 #endif /* #ifdef PH_HAL4_ENABLE */ 984 985 /* 1st byte represents the length of the cmd packet*/ 986 NdefMap->SendRecvBuf[BufIndex] = 0x00; 987 BufIndex++; 988 989 /* Write/Update command code*/ 990 NdefMap->SendRecvBuf[BufIndex] = 0x08; 991 BufIndex++; 992 993 /* IDm - Manufacturer Id : 8bytes*/ 994 #ifdef PH_HAL4_ENABLE 995 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 996 (void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)), 997 8); 998 #else 999 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1000 (void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)), 1001 8); 1002 #endif /* #ifdef PH_HAL4_ENABLE */ 1003 1004 BufIndex+=8; 1005 1006 NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/ 1007 BufIndex++; 1008 1009 NdefMap->SendRecvBuf[BufIndex] = 0x09; /* Service Code List*/ 1010 BufIndex++; 1011 1012 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/ 1013 BufIndex++; 1014 1015 NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Blocks to Write*/ 1016 BufIndex++; 1017 1018 NdefMap->SendRecvBuf[BufIndex] = 0x80; /* 1st Block Element : byte 1*/ 1019 BufIndex++; 1020 1021 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* 1st Block Element : byte 2, block 1*/ 1022 BufIndex++; 1023 1024 /* Fill Attribute Blk Information*/ 1025 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Version; 1026 BufIndex++; 1027 1028 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbr; 1029 BufIndex++; 1030 1031 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbw; 1032 BufIndex++; 1033 1034 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) >> 8); 1035 BufIndex++; 1036 1037 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) & (0x00ff)); 1038 BufIndex++; 1039 1040 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1041 BufIndex++; 1042 1043 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1044 BufIndex++; 1045 1046 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1047 BufIndex++; 1048 1049 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1050 BufIndex++; 1051 1052 if (isStarted == FELICA_WRITE_STARTED ) 1053 { 1054 NdefMap->SendRecvBuf[BufIndex] = 0x0F; /* Write Flag Made On*/ 1055 BufIndex++; 1056 1057 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.RdWrFlag; /* Read write flag*/ 1058 BufIndex++; 1059 1060 /* Len Bytes*/ 1061 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.LenBytes[0]; 1062 BufIndex++; 1063 1064 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.LenBytes[1]; 1065 BufIndex++; 1066 1067 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.LenBytes[2]; 1068 BufIndex++; 1069 } 1070 else 1071 { 1072 /* Case: Previous Write Operation failed and integration context continues with write 1073 operation with offset set to Current. In this case, if we find Internal Bytes remained in the 1074 felica context is true(>0) and current block number is Zero. Then we shouldn't allow the module 1075 to write the data to card, as this is a invalid case*/ 1076 if ( (NdefMap->Felica.Wr_BytesRemained > 0) && (NdefMap->Felica.CurBlockNo == 0)) 1077 { 1078 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1079 NFCSTATUS_INVALID_PARAMETER); 1080 ErrFlag = TRUE; 1081 } 1082 else 1083 { 1084 1085 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Write Flag Made Off*/ 1086 BufIndex++; 1087 1088 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.RdWrFlag; /* Read write flag*/ 1089 BufIndex++; 1090 1091 if ( NdefMap->Felica.Wr_BytesRemained > 0 ) 1092 { 1093 TotNoWrittenBytes = ( (NdefMap->Felica.CurBlockNo *16)- (16 - (NdefMap->Felica.Wr_BytesRemained))); 1094 } 1095 else 1096 { 1097 TotNoWrittenBytes = ( NdefMap->Felica.CurBlockNo *16); 1098 1099 } 1100 1101 /* Update Len Bytes*/ 1102 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(( TotNoWrittenBytes & 0x00ff0000) >> 16); 1103 BufIndex++; 1104 1105 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((TotNoWrittenBytes & 0x0000ff00) >> 8); 1106 BufIndex++; 1107 1108 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(TotNoWrittenBytes & 0x000000ff); 1109 BufIndex++; 1110 } 1111 } 1112 1113 if ( ErrFlag != TRUE ) 1114 { 1115 /* check sum update*/ 1116 for ( index = 16 ; index < 30 ; index ++) 1117 { 1118 ChkSum += NdefMap->SendRecvBuf[index]; 1119 } 1120 1121 /* fill check sum in command pkt*/ 1122 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(ChkSum >> 8); 1123 BufIndex++; 1124 1125 NdefMap->SendRecvBuf[BufIndex] = (uint8_t )(ChkSum & 0x00ff); 1126 BufIndex++; 1127 1128 /* update length of the cmd pkt*/ 1129 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex; 1130 1131 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1132 1133 /* Update the Send Len*/ 1134 NdefMap->SendLength = BufIndex; 1135 1136 /*set the completion routines for the desfire card operations*/ 1137 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_NdefMap_Process; 1138 NdefMap->MapCompletionInfo.Context = NdefMap; 1139 1140 /*set the additional informations for the data exchange*/ 1141 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0; 1142 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0; 1143 1144 /*Call the Overlapped HAL Transceive function */ 1145 status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice, 1146 &NdefMap->MapCompletionInfo, 1147 NdefMap->psRemoteDevInfo, 1148 NdefMap->Cmd, 1149 &NdefMap->psDepAdditionalInfo, 1150 NdefMap->SendRecvBuf, 1151 NdefMap->SendLength, 1152 NdefMap->SendRecvBuf, 1153 NdefMap->SendRecvLength); 1154 } 1155 } 1156 else 1157 { 1158 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1159 NFCSTATUS_INVALID_PARAMETER); 1160 1161 } 1162 return (status); 1163 } 1164 1165 static NFCSTATUS phFriNfc_Felica_HWrEmptyMsg(phFriNfc_NdefMap_t *NdefMap) 1166 { 1167 NFCSTATUS status = NFCSTATUS_PENDING; 1168 1169 uint16_t ChkSum=0,index=0; 1170 uint8_t BufIndex=0; 1171 1172 /* Write Operation : To Erase the present NDEF Data*/ 1173 1174 NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_WR_EMPTY_MSG; 1175 1176 /* Set the Felica Cmd*/ 1177 #ifdef PH_HAL4_ENABLE 1178 NdefMap->Cmd.FelCmd = phHal_eFelica_Raw; 1179 #else 1180 NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd; 1181 #endif /* #ifdef PH_HAL4_ENABLE */ 1182 1183 /* 1st byte represents the length of the cmd packet*/ 1184 NdefMap->SendRecvBuf[BufIndex] = 0x00; 1185 BufIndex++; 1186 1187 /* Write/Update command code*/ 1188 NdefMap->SendRecvBuf[BufIndex] = 0x08; 1189 BufIndex++; 1190 1191 /* IDm - Manufacturer Id : 8bytes*/ 1192 #ifdef PH_HAL4_ENABLE 1193 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1194 (void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)), 1195 8); 1196 #else 1197 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1198 (void*)(&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)), 1199 8); 1200 #endif /* #ifdef PH_HAL4_ENABLE */ 1201 1202 1203 BufIndex+=8; 1204 1205 NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/ 1206 BufIndex++; 1207 1208 NdefMap->SendRecvBuf[BufIndex] = 0x09; /* Service Code List*/ 1209 BufIndex++; 1210 1211 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/ 1212 BufIndex++; 1213 1214 NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Blocks to Write*/ 1215 BufIndex++; 1216 1217 NdefMap->SendRecvBuf[BufIndex] = 0x80; /* 1st Block Element : byte 1*/ 1218 BufIndex++; 1219 1220 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* 1st Block Element : byte 2, block 1*/ 1221 BufIndex++; 1222 1223 /* Fill Attribute Blk Information*/ 1224 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Version; 1225 BufIndex++; 1226 1227 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbr; 1228 BufIndex++; 1229 1230 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbw; 1231 BufIndex++; 1232 1233 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) >> 8); 1234 BufIndex++; 1235 1236 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb) & (0x00ff)); 1237 BufIndex++; 1238 1239 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1240 BufIndex++; 1241 1242 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1243 BufIndex++; 1244 1245 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1246 BufIndex++; 1247 1248 NdefMap->SendRecvBuf[BufIndex] = 0x00; /*RFU*/ 1249 BufIndex++; 1250 1251 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.WriteFlag; 1252 BufIndex++; 1253 1254 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.RdWrFlag; /* Read write flag*/ 1255 BufIndex++; 1256 1257 /* Len Bytes are set to 0 : Empty Msg*/ 1258 NdefMap->SendRecvBuf[BufIndex] = 0x00; 1259 BufIndex++; 1260 1261 NdefMap->SendRecvBuf[BufIndex] = 0x00; 1262 BufIndex++; 1263 1264 NdefMap->SendRecvBuf[BufIndex] = 0x00; 1265 BufIndex++; 1266 1267 /* check sum update*/ 1268 for ( index = 16 ; index < 30 ; index ++) 1269 { 1270 ChkSum += NdefMap->SendRecvBuf[index]; 1271 } 1272 1273 /* fill check sum in command pkt*/ 1274 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)(ChkSum >> 8); 1275 BufIndex++; 1276 1277 NdefMap->SendRecvBuf[BufIndex] = (uint8_t )(ChkSum & 0x00ff); 1278 BufIndex++; 1279 1280 /* update length of the cmd pkt*/ 1281 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex; 1282 1283 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 1284 1285 /* Update the Send Len*/ 1286 NdefMap->SendLength = BufIndex; 1287 1288 /*set the completion routines for the desfire card operations*/ 1289 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_NdefMap_Process; 1290 NdefMap->MapCompletionInfo.Context = NdefMap; 1291 1292 /*set the additional informations for the data exchange*/ 1293 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0; 1294 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0; 1295 1296 /*Call the Overlapped HAL Transceive function */ 1297 status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice, 1298 &NdefMap->MapCompletionInfo, 1299 NdefMap->psRemoteDevInfo, 1300 NdefMap->Cmd, 1301 &NdefMap->psDepAdditionalInfo, 1302 NdefMap->SendRecvBuf, 1303 NdefMap->SendLength, 1304 NdefMap->SendRecvBuf, 1305 NdefMap->SendRecvLength); 1306 1307 return (status); 1308 } 1309 1310 1311 1312 1313 /*! 1314 * \brief Used in Write Opearation. 1315 * This Function is called after a successful validation and storing of attribution block 1316 * content in to context. 1317 * If the write operation is initiated with begin,function initiates the write operation with 1318 * RdWr flag. 1319 * If the Offset is set to Current, Checks for the EOF card reached status and writes data to 1320 * The Card 1321 */ 1322 1323 static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForWrOp(phFriNfc_NdefMap_t *NdefMap) 1324 { 1325 NFCSTATUS status = NFCSTATUS_PENDING; 1326 uint32_t DataLen=0; 1327 1328 /*check RW Flag Access Rights*/ 1329 /* set to read only cannot write*/ 1330 if ( NdefMap->FelicaAttrInfo.RdWrFlag == 0x00) 1331 1332 { 1333 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1334 NFCSTATUS_INVALID_DEVICE_REQUEST); 1335 } 1336 else 1337 { 1338 if ( ( NdefMap->Felica.Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || 1339 ( ( NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE) && 1340 (NdefMap->Felica.Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ) )) 1341 { 1342 /* check allready written number of bytes and apdu buffer size*/ 1343 if (NdefMap->ApduBufferSize > (uint32_t)(NdefMap->FelicaAttrInfo.Nmaxb *16)) 1344 { 1345 NdefMap->Felica.EofCardReachedFlag = FELICA_EOF_REACHED_WR_WITH_BEGIN_OFFSET; 1346 } 1347 else 1348 { 1349 NdefMap->Felica.EofCardReachedFlag = FALSE; 1350 } 1351 1352 1353 /* reset the internal variables initiate toupdate the attribute blk*/ 1354 NdefMap->Felica.Wr_BytesRemained = 0; 1355 NdefMap->Felica.CurBlockNo = 0; 1356 NdefMap->Felica.NoBlocksWritten = 0; 1357 phFriNfc_Felica_HInitInternalBuf(NdefMap->Felica.Wr_RemainedBytesBuff); 1358 status= phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_STARTED); 1359 1360 } 1361 else 1362 { 1363 if (NdefMap->Felica.Offset == PH_FRINFC_NDEFMAP_SEEK_CUR ) 1364 { 1365 /* Calculate the Allready Written No. Of Blocks*/ 1366 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0], 1367 NdefMap->FelicaAttrInfo.LenBytes[1], 1368 NdefMap->FelicaAttrInfo.LenBytes[2], 1369 DataLen); 1370 1371 if (( NdefMap->ApduBufferSize + (DataLen )) > 1372 (uint32_t)( NdefMap->FelicaAttrInfo.Nmaxb *16)) 1373 { 1374 if(( DataLen ) == (uint32_t)(NdefMap->FelicaAttrInfo.Nmaxb *16) ) 1375 { 1376 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 1377 NFCSTATUS_EOF_NDEF_CONTAINER_REACHED); 1378 } 1379 else 1380 { 1381 1382 NdefMap->Felica.EofCardReachedFlag =FELICA_EOF_REACHED_WR_WITH_CURR_OFFSET; 1383 NdefMap->ApduBuffIndex =0; 1384 NdefMap->Felica.NoBlocksWritten = 0; 1385 status= phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_STARTED); 1386 } 1387 } 1388 else 1389 { 1390 NdefMap->ApduBuffIndex =0; 1391 NdefMap->Felica.NoBlocksWritten = 0; 1392 status= phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_STARTED); 1393 } 1394 }/*if (NdefMap->Felica.Offset == PH_FRINFC_NDEFMAP_SEEK_CUR )*/ 1395 } 1396 } 1397 return (status); 1398 } 1399 1400 /*! 1401 * \brief Used in Read Opearation. 1402 * This Function is called after a successful validation and storing of attribution block 1403 * content in to context. 1404 * While Offset is set to Current, Checks for the EOF card reached status and reads data from 1405 * The Card 1406 */ 1407 1408 static NFCSTATUS phFriNfc_Felica_HChkAttrBlkForRdOp(phFriNfc_NdefMap_t *NdefMap, 1409 uint32_t NdefLen) 1410 { 1411 NFCSTATUS status = NFCSTATUS_PENDING; 1412 1413 /*check WR Flag Access Rights*/ 1414 /* set to still writing data state only cannot Read*/ 1415 if ( NdefMap->FelicaAttrInfo.WriteFlag == 0x0F ) 1416 { 1417 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_READ_FAILED); 1418 /* As we are not able to continue with reading data 1419 bytes read set to zero*/ 1420 *NdefMap->NumOfBytesRead = 0; 1421 } 1422 else 1423 { 1424 status = phFriNfc_MapTool_SetCardState( NdefMap,NdefLen); 1425 if ( status == NFCSTATUS_SUCCESS) 1426 { 1427 /* Read data From the card*/ 1428 status = phFriNfc_Felica_HReadData(NdefMap,NdefMap->Felica.Offset); 1429 } 1430 } 1431 1432 return (status); 1433 } 1434 1435 /*! 1436 * \brief Used in Write Opearation. 1437 * This function writes the data in terms of blocks. 1438 * Each write operation supports,minimum Nbw blocks of bytes. 1439 * Also checks for the EOF,>=NBW,<NBW etc 1440 */ 1441 static NFCSTATUS phFriNfc_Felica_HUpdateData(phFriNfc_NdefMap_t *NdefMap) 1442 { 1443 NFCSTATUS status = NFCSTATUS_PENDING; 1444 1445 uint8_t BufIndex=0, 1446 i=0, 1447 BlkNo=0, 1448 PadBytes=0, 1449 CurBlk=1, 1450 NoOfBlks=0, 1451 NbwCheck=0, 1452 TotNoBlks=0; 1453 1454 uint32_t BytesRemainedInCard=0, 1455 BytesRemained=0, 1456 TotNoWrittenBytes=0; 1457 1458 if( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) > 0 ) 1459 { 1460 /* Prepare the write cmd pkt for felica*/ 1461 /* 1st byte represents the length of the cmd packet*/ 1462 NdefMap->SendRecvBuf[BufIndex] = 0x00; 1463 BufIndex++; 1464 1465 /* Write/Update command code*/ 1466 NdefMap->SendRecvBuf[BufIndex] = 0x08; 1467 BufIndex++; 1468 1469 /* IDm - Manufacturer Id : 8bytes*/ 1470 #ifdef PH_HAL4_ENABLE 1471 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 1472 (&(NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm)), 1473 8); 1474 #else 1475 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 1476 (&(NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t)), 1477 8); 1478 #endif /* #ifdef PH_HAL4_ENABLE */ 1479 1480 BufIndex+=8; 1481 1482 NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/ 1483 BufIndex++; 1484 1485 NdefMap->SendRecvBuf[BufIndex] = 0x09; /* Service Code List*/ 1486 BufIndex++; 1487 1488 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/ 1489 BufIndex++; 1490 1491 if ( NdefMap->Felica.EofCardReachedFlag == FELICA_EOF_REACHED_WR_WITH_BEGIN_OFFSET) 1492 { 1493 /* check for the eof card reached flag.Need to write only mamximum bytes(memory)to card. 1494 Used when, offset set to begin case*/ 1495 BytesRemainedInCard= ( (NdefMap->FelicaAttrInfo.Nmaxb*16) - (NdefMap->Felica.CurBlockNo * 16)); 1496 } 1497 else 1498 { 1499 /* Offset : Cuurent*/ 1500 if ( NdefMap->Felica.EofCardReachedFlag == FELICA_EOF_REACHED_WR_WITH_CURR_OFFSET ) 1501 { 1502 /* caculate previously written Ndef blks*/ 1503 (void)phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC); 1504 1505 if ( NdefMap->Felica.Wr_BytesRemained ) 1506 { 1507 TotNoWrittenBytes = ( (NdefMap->Felica.CurBlockNo *16)- (16 - (NdefMap->Felica.Wr_BytesRemained))); 1508 } 1509 else 1510 { 1511 TotNoWrittenBytes = ( NdefMap->Felica.CurBlockNo *16); 1512 } 1513 /* Determine exactly, how many bytes we can write*/ 1514 BytesRemainedInCard = (NdefMap->FelicaAttrInfo.Nmaxb*16 - (TotNoWrittenBytes)); 1515 } 1516 1517 } 1518 /* Write Data Pending in the Internal Buffer*/ 1519 if(NdefMap->Felica.Wr_BytesRemained > 0) 1520 { 1521 /* update the number of blocks to write with the block list elements*/ 1522 /* Total Number of blocks to write*/ 1523 NdefMap->SendRecvBuf[BufIndex] = 0; 1524 BufIndex++; 1525 1526 /* Update this Total no. Bloks later*/ 1527 NoOfBlks = BufIndex; 1528 1529 /* As we are writing atleast one block*/ 1530 TotNoBlks = 1; 1531 1532 /* check do we have some extra bytes to write? in User Buffer*/ 1533 if ( NdefMap->ApduBufferSize >(uint32_t) (16 - NdefMap->Felica.Wr_BytesRemained)) 1534 { 1535 /* Have we reached EOF?*/ 1536 if ( NdefMap->Felica.EofCardReachedFlag ) 1537 { 1538 BytesRemained = BytesRemainedInCard; 1539 } 1540 else 1541 { 1542 /* This value tells how many extra bytes we can write other than internal buffer bytes*/ 1543 BytesRemained = (uint8_t)NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained); 1544 } 1545 1546 if ( BytesRemained ) 1547 { 1548 /* Not reached EOF*/ 1549 if (!NdefMap->Felica.EofCardReachedFlag) 1550 { 1551 /* Calculate How many blks we need to write*/ 1552 BlkNo =((uint8_t)( BytesRemained )/16); 1553 1554 /* check blocks to write exceeds nbw*/ 1555 if ( BlkNo >= NdefMap->FelicaAttrInfo.Nbw ) 1556 { 1557 BlkNo = NdefMap->FelicaAttrInfo.Nbw; 1558 /* No. Blks to write are more than Nbw*/ 1559 NbwCheck = 1; 1560 } 1561 else 1562 { 1563 if ((( BytesRemained %16) == 0)&& (BlkNo == 0 )) 1564 { 1565 BlkNo=1; 1566 } 1567 } 1568 /* check do we need pad bytes?*/ 1569 if( (!NbwCheck && (uint8_t)( BytesRemained)%16) != 0) 1570 { 1571 BlkNo++; 1572 PadBytes = (BlkNo * 16) - (uint8_t)( BytesRemained); 1573 NdefMap->Felica.PadByteFlag = TRUE; 1574 NdefMap->Felica.NoBlocksWritten = BlkNo; 1575 TotNoBlks += BlkNo; 1576 1577 } 1578 else 1579 { 1580 if ( NbwCheck ) 1581 { 1582 /* as we have to write only 8 blocks and already we have pad bytes so we have 1583 to strat from previous block*/ 1584 TotNoBlks += BlkNo - 1; 1585 NdefMap->Felica.NoBlocksWritten = TotNoBlks-1; 1586 } 1587 else 1588 { 1589 if ( !(BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained)== 0 )) 1590 { 1591 TotNoBlks += BlkNo; 1592 } 1593 else 1594 { 1595 1596 } 1597 if ( NdefMap->Felica.PadByteFlag ) 1598 { 1599 NdefMap->Felica.NoBlocksWritten = TotNoBlks-1; 1600 1601 } 1602 } 1603 } 1604 } 1605 else 1606 { 1607 /* we have reached the eof card & hv bytes to write*/ 1608 BlkNo =(uint8_t)(( BytesRemained - ((16 -NdefMap->Felica.Wr_BytesRemained)) )/16); 1609 1610 /* check are we exceeding the NBW limit, while a write?*/ 1611 if ( BlkNo >= NdefMap->FelicaAttrInfo.Nbw ) 1612 { 1613 BlkNo = NdefMap->FelicaAttrInfo.Nbw; 1614 1615 /* No. Blks to write are more than Nbw*/ 1616 NbwCheck = 1; 1617 1618 } 1619 else 1620 { 1621 if ((( BytesRemained %16) == 0)&& (BlkNo == 0 )) 1622 { 1623 BlkNo=1; 1624 } 1625 } 1626 1627 /*check Total how many blocks to write*/ 1628 if(((!NbwCheck) &&( BytesRemained- (16 - NdefMap->Felica.Wr_BytesRemained))%16) != 0) 1629 { 1630 BlkNo++; 1631 PadBytes = (BlkNo * 16) - (uint8_t)( BytesRemained); 1632 NdefMap->Felica.PadByteFlag = TRUE; 1633 NdefMap->Felica.NoBlocksWritten = BlkNo; 1634 TotNoBlks += BlkNo; 1635 1636 } 1637 else 1638 { 1639 if ( NbwCheck ) 1640 { 1641 /* as we have to write only 8 blocks and already we have pad bytes so we have 1642 to strat from previous last block*/ 1643 TotNoBlks += BlkNo - 1; 1644 NdefMap->Felica.NoBlocksWritten = TotNoBlks-1; 1645 } 1646 else 1647 { 1648 /* we need to write only one block ( bytesremanind + internal buffer size = 16)*/ 1649 if ( !(BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained)== 0 )) 1650 { 1651 TotNoBlks += BlkNo; 1652 } 1653 else 1654 { 1655 ;/* we are not incrementing the Total no. of blocks to write*/ 1656 } 1657 1658 if ( NdefMap->Felica.PadByteFlag ) 1659 { 1660 NdefMap->Felica.NoBlocksWritten = TotNoBlks -1; 1661 1662 } 1663 } 1664 } 1665 } 1666 }/*if ( BytesRemained )*/ 1667 else 1668 { 1669 ; /*Nothing to process here*/ 1670 } 1671 }/*if ( NdefMap->ApduBufferSize >(uint32_t) (16 - NdefMap->Felica.Wr_BytesRemained))*/ 1672 else 1673 { 1674 /* No new blks to write*/ 1675 NdefMap->Felica.NoBlocksWritten = 0; 1676 } 1677 /* Prepare the Blk List for Write Operation*/ 1678 /* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/ 1679 for ( i=0; i< TotNoBlks; i++) 1680 { 1681 NdefMap->SendRecvBuf[BufIndex] = 0x80; 1682 BufIndex++; 1683 /* remember the previous Blk no and continue from there*/ 1684 if ( NdefMap->Felica.PadByteFlag == TRUE ) 1685 { 1686 NdefMap->SendRecvBuf[BufIndex] = NdefMap->Felica.CurBlockNo + i; 1687 BufIndex++; 1688 } 1689 else 1690 { 1691 CurBlk = NdefMap->Felica.CurBlockNo +1; 1692 NdefMap->SendRecvBuf[BufIndex] = CurBlk + i; 1693 BufIndex++; 1694 } 1695 } 1696 /* Copy relevant data to Transc buffer*/ 1697 if((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16 - NdefMap->Felica.Wr_BytesRemained)) 1698 { 1699 1700 /*Copy the Remained bytes from the internal buffer to trxbuffer */ 1701 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1702 NdefMap->Felica.Wr_RemainedBytesBuff, 1703 NdefMap->Felica.Wr_BytesRemained); 1704 1705 /*Increment the buff index*/ 1706 BufIndex += NdefMap->Felica.Wr_BytesRemained; 1707 1708 1709 /*append copy 16-bytesToPad to trxBuffer*/ 1710 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1711 (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 1712 (16 - NdefMap->Felica.Wr_BytesRemained)); 1713 1714 /* Update Number Of Bytes Writtened*/ 1715 NdefMap->NumOfBytesWritten = 16 - NdefMap->Felica.Wr_BytesRemained; 1716 1717 /* increment the index*/ 1718 BufIndex += 16 - NdefMap->Felica.Wr_BytesRemained; 1719 1720 if ( BytesRemained ) 1721 { 1722 if (!NdefMap->Felica.EofCardReachedFlag) 1723 { 1724 /* check nbw limit*/ 1725 if ( NbwCheck != 1 ) 1726 { 1727 /* Copy Extra Bytes other than the internal buffer bytes*/ 1728 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1729 (&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])), 1730 (NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained))); 1731 1732 1733 /* Update Number Of Bytes Writtened*/ 1734 NdefMap->NumOfBytesWritten += (uint16_t)(NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained)); 1735 1736 BufIndex += (uint8_t)(NdefMap->ApduBufferSize - (16 - NdefMap->Felica.Wr_BytesRemained)); 1737 1738 if ( PadBytes ) 1739 { 1740 for(i= 0; i< PadBytes; i++) 1741 { 1742 NdefMap->SendRecvBuf[BufIndex] =0x00; 1743 BufIndex++; 1744 } 1745 /* no of bytes remained copy*/ 1746 NdefMap->Felica.Wr_BytesRemained = (uint8_t)(16 - PadBytes); 1747 1748 /*copy the data to internal buffer : Bytes remained*/ 1749 (void)memcpy( NdefMap->Felica.Wr_RemainedBytesBuff, 1750 (&( NdefMap->ApduBuffer[(NdefMap->ApduBufferSize - NdefMap->Felica.Wr_BytesRemained)])), 1751 ( NdefMap->Felica.Wr_BytesRemained)); 1752 } 1753 else 1754 { 1755 /* No Bytes in Internal buffer*/ 1756 NdefMap->Felica.Wr_BytesRemained = 0; 1757 } 1758 1759 } 1760 else 1761 { 1762 1763 /*Copy Nbw*16 bytes of data to the trx buffer*/ 1764 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1765 (&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])), 1766 (NdefMap->FelicaAttrInfo.Nbw - 1) * 16); 1767 1768 /* increment the Buffindex*/ 1769 BufIndex += ((NdefMap->FelicaAttrInfo.Nbw - 1 )*16); 1770 1771 NdefMap->Felica.Wr_BytesRemained = 0; 1772 NdefMap->NumOfBytesWritten+= ((NdefMap->FelicaAttrInfo.Nbw -1)*16); 1773 NdefMap->Felica.PadByteFlag =FALSE; 1774 } 1775 }/*if (!NdefMap->Felica.EofCardReachedFlag)*/ 1776 else 1777 { 1778 /* check nbw limit*/ 1779 if ( NbwCheck != 1 ) 1780 { 1781 /* handle EOF card reached case*/ 1782 (void)memcpy( (&(NdefMap->SendRecvBuf[BufIndex])), 1783 (&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])), 1784 ( BytesRemained - ((16 -NdefMap->Felica.Wr_BytesRemained) ))); 1785 1786 /* Update Number Of Bytes Writtened*/ 1787 NdefMap->NumOfBytesWritten += (uint16_t)( BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained)); 1788 1789 BufIndex += (uint8_t)( BytesRemained - (16 -NdefMap->Felica.Wr_BytesRemained)); 1790 1791 if ( PadBytes ) 1792 { 1793 for(i= 0; i< PadBytes; i++) 1794 { 1795 NdefMap->SendRecvBuf[BufIndex] =0x00; 1796 BufIndex++; 1797 } 1798 1799 /*no of bytes remained copy*/ 1800 NdefMap->Felica.Wr_BytesRemained = (uint8_t)(16 - PadBytes); 1801 1802 /*copy the data to internal buffer : Bytes remained*/ 1803 (void)memcpy(NdefMap->Felica.Wr_RemainedBytesBuff, 1804 (&(NdefMap->ApduBuffer[(NdefMap->ApduBufferSize - NdefMap->Felica.Wr_BytesRemained)])), 1805 (NdefMap->Felica.Wr_BytesRemained)); 1806 1807 } 1808 else 1809 { 1810 NdefMap->Felica.Wr_BytesRemained = 0; 1811 } 1812 } 1813 else 1814 { 1815 1816 /*Copy Nbw*16 bytes of data to the trx buffer*/ 1817 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 1818 (&(NdefMap->ApduBuffer[(16 - NdefMap->Felica.Wr_BytesRemained)])), 1819 (NdefMap->FelicaAttrInfo.Nbw - 1) * 16); 1820 1821 /* increment the Buffindex*/ 1822 BufIndex += ((NdefMap->FelicaAttrInfo.Nbw - 1 )*16); 1823 1824 NdefMap->Felica.Wr_BytesRemained = 0; 1825 NdefMap->NumOfBytesWritten+= ((NdefMap->FelicaAttrInfo.Nbw -1)*16); 1826 1827 NdefMap->Felica.PadByteFlag =FALSE; 1828 } 1829 } 1830 }/*if ( BytesRemained )*/ 1831 else 1832 { 1833 NdefMap->Felica.Wr_BytesRemained = 0; 1834 } 1835 /* Update Total No. of blocks writtened*/ 1836 NdefMap->SendRecvBuf[NoOfBlks -1 ]=TotNoBlks; 1837 }/*if((NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16 - NdefMap->Felica.Wr_BytesRemained))*/ 1838 else 1839 { 1840 /*copy the internal buffer data to trx buffer*/ 1841 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 1842 NdefMap->Felica.Wr_RemainedBytesBuff, 1843 (NdefMap->Felica.Wr_BytesRemained)); 1844 1845 /* increment the index*/ 1846 BufIndex+=NdefMap->Felica.Wr_BytesRemained; 1847 1848 /*append the apdusize data to the trx buffer*/ 1849 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 1850 (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 1851 NdefMap->ApduBufferSize); 1852 1853 /* Index increment*/ 1854 BufIndex+= (uint8_t)NdefMap->ApduBufferSize; 1855 1856 /* Tells how many bytes present in the internal buffer*/ 1857 BytesRemained = NdefMap->Felica.Wr_BytesRemained + NdefMap->ApduBufferSize; 1858 1859 PadBytes = (uint8_t)(16-BytesRemained); 1860 1861 /* Pad empty bytes with Zeroes to complete 16 bytes*/ 1862 for(i= 0; i< PadBytes; i++) 1863 { 1864 NdefMap->SendRecvBuf[BufIndex] =0x00; 1865 BufIndex++; 1866 } 1867 1868 /* Update Number Of Bytes Writtened*/ 1869 NdefMap->NumOfBytesWritten = (uint16_t)NdefMap->ApduBufferSize; 1870 1871 /* Flag set to understand that , we have received less no. of bytes than 1872 present in the internal buffer*/ 1873 NdefMap->Felica.IntermediateWrFlag = TRUE; 1874 1875 if ( NdefMap->Felica.PadByteFlag ) 1876 { 1877 NdefMap->Felica.NoBlocksWritten = 0; 1878 } 1879 } 1880 1881 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex; 1882 NdefMap->SendLength = BufIndex; 1883 /* Update Total No. of blocks writtened*/ 1884 NdefMap->SendRecvBuf[NoOfBlks -1 ]=TotNoBlks; 1885 } 1886 else 1887 { 1888 /*Fresh write, starting from a new block*/ 1889 if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16* NdefMap->FelicaAttrInfo.Nbw )) 1890 { 1891 /* check for the card size and write Nbw Blks*/ 1892 if ( NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo >= NdefMap->FelicaAttrInfo.Nbw) 1893 { 1894 /* update the number of blocks to write with the block list elements*/ 1895 /* Total Number of blocks to write*/ 1896 NdefMap->SendRecvBuf[BufIndex] = NdefMap->FelicaAttrInfo.Nbw; 1897 BufIndex++; 1898 1899 /* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/ 1900 for ( i=1; i<= NdefMap->FelicaAttrInfo.Nbw; i++) 1901 { 1902 NdefMap->SendRecvBuf[BufIndex] = 0x80; 1903 BufIndex++; 1904 1905 NdefMap->SendRecvBuf[BufIndex] = NdefMap->Felica.CurBlockNo + i; 1906 BufIndex++; 1907 } 1908 /*Copy Nbw*16 bytes of data to the trx buffer*/ 1909 1910 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 1911 (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 1912 NdefMap->FelicaAttrInfo.Nbw * 16); 1913 1914 /* increment the Buffindex*/ 1915 BufIndex += (NdefMap->FelicaAttrInfo.Nbw*16); 1916 1917 /* update the length*/ 1918 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex; 1919 1920 NdefMap->Felica.Wr_BytesRemained = 0; 1921 NdefMap->NumOfBytesWritten = (NdefMap->FelicaAttrInfo.Nbw*16); 1922 NdefMap->Felica.NoBlocksWritten = NdefMap->FelicaAttrInfo.Nbw; 1923 1924 /* update the Send length*/ 1925 NdefMap->SendLength = BufIndex; 1926 1927 NdefMap->Felica.PadByteFlag = FALSE; 1928 }/*if ( NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo >= NdefMap->FelicaAttrInfo.Nbw)*/ 1929 else 1930 { 1931 /* we need to write less than nbw blks*/ 1932 /* update the number of blocks to write with the block list elements*/ 1933 /* Total Number of blocks to write*/ 1934 NdefMap->SendRecvBuf[BufIndex] = (uint8_t)( NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo); 1935 BufIndex++; 1936 1937 /* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/ 1938 for ( i=1; i<= (NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo); i++) 1939 { 1940 NdefMap->SendRecvBuf[BufIndex] = 0x80; 1941 BufIndex++; 1942 NdefMap->SendRecvBuf[BufIndex] = NdefMap->Felica.CurBlockNo + i; 1943 BufIndex++; 1944 } 1945 1946 /*Copy Nbw*16 bytes of data to the trx buffer*/ 1947 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 1948 (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 1949 (NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo)*16); 1950 1951 /* increment the Buffindex*/ 1952 BufIndex += (uint8_t)((NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo )*16); 1953 1954 /* update the length*/ 1955 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex; 1956 1957 NdefMap->NumOfBytesWritten = ((NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo)*16); 1958 NdefMap->Felica.NoBlocksWritten = (uint8_t)(NdefMap->FelicaAttrInfo.Nmaxb - NdefMap->Felica.CurBlockNo); 1959 1960 /* update the Send length*/ 1961 NdefMap->SendLength = BufIndex; 1962 1963 NdefMap->Felica.PadByteFlag =FALSE; 1964 NdefMap->Felica.Wr_BytesRemained = 0; 1965 } 1966 }/* if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16* NdefMap->FelicaAttrInfo.Nbw )) */ 1967 else 1968 { 1969 /*chk eof reached*/ 1970 if ( NdefMap->Felica.EofCardReachedFlag) 1971 { 1972 BlkNo =((uint8_t)(BytesRemainedInCard )/16); 1973 if(((uint8_t)( BytesRemainedInCard )%16) != 0) 1974 { 1975 BlkNo++; 1976 PadBytes = ((BlkNo * 16) - (uint8_t)(BytesRemainedInCard )); 1977 NdefMap->Felica.PadByteFlag = TRUE; 1978 } 1979 } 1980 else 1981 { 1982 1983 BlkNo =((uint8_t)( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)/16); 1984 if(((uint8_t)( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)%16) != 0) 1985 { 1986 BlkNo++; 1987 PadBytes = (BlkNo * 16) - (uint8_t)( NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 1988 NdefMap->Felica.PadByteFlag = TRUE; 1989 1990 } 1991 1992 1993 } 1994 1995 /* update the number of blocks to write with the block list elements*/ 1996 /* Total Number of blocks to write*/ 1997 NdefMap->SendRecvBuf[BufIndex] = BlkNo; 1998 BufIndex++; 1999 2000 NdefMap->Felica.NoBlocksWritten = BlkNo; 2001 2002 /* Block List for NBw : 1st byte : two byte len list: 2nd byte is the block number*/ 2003 for ( i=0; i< BlkNo; i++) 2004 { 2005 NdefMap->SendRecvBuf[BufIndex] = 0x80; 2006 BufIndex++; 2007 { 2008 CurBlk = NdefMap->Felica.CurBlockNo +1; 2009 NdefMap->SendRecvBuf[BufIndex] = CurBlk + i; 2010 BufIndex++; 2011 } 2012 } 2013 if ( NdefMap->Felica.EofCardReachedFlag ) 2014 { 2015 /*Copy last data to the trx buffer*/ 2016 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 2017 (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 2018 BytesRemainedInCard ); 2019 2020 /* increment the bufindex and bytes written*/ 2021 BufIndex += (uint8_t )BytesRemainedInCard ; 2022 NdefMap->NumOfBytesWritten = (uint16_t)BytesRemainedInCard ; 2023 } 2024 else 2025 { 2026 /*Copy data to the trx buffer*/ 2027 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 2028 (&(NdefMap->ApduBuffer[NdefMap->ApduBuffIndex])), 2029 (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex)); 2030 2031 /* increment the bufindex and bytes written*/ 2032 BufIndex += (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 2033 NdefMap->NumOfBytesWritten = (uint8_t)(NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex); 2034 } 2035 if ( PadBytes ) 2036 { 2037 for(i= 0; i< PadBytes; i++) 2038 { 2039 NdefMap->SendRecvBuf[BufIndex] =0x00; 2040 BufIndex++; 2041 } 2042 /*no of bytes remained copy*/ 2043 NdefMap->Felica.Wr_BytesRemained = (uint8_t)(16 - PadBytes); 2044 2045 if ( NdefMap->Felica.EofCardReachedFlag ) 2046 { 2047 /*copy the data to internal buffer : Bytes remained*/ 2048 (void)memcpy(NdefMap->Felica.Wr_RemainedBytesBuff, 2049 (&(NdefMap->ApduBuffer[((BytesRemainedInCard - (BytesRemainedInCard % 16)))])), 2050 ( NdefMap->Felica.Wr_BytesRemained)); 2051 2052 } 2053 else 2054 { 2055 /*copy the data to internal buffer : Bytes remained*/ 2056 (void)memcpy( NdefMap->Felica.Wr_RemainedBytesBuff, 2057 (&(NdefMap->ApduBuffer[((NdefMap->ApduBufferSize - NdefMap->Felica.Wr_BytesRemained))])), 2058 ( NdefMap->Felica.Wr_BytesRemained)); 2059 2060 } 2061 }/*if ( PadBytes )*/ 2062 else 2063 { 2064 NdefMap->Felica.Wr_BytesRemained = 0; 2065 NdefMap->Felica.PadByteFlag = FALSE; 2066 } 2067 /* update the pkt len*/ 2068 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex; 2069 NdefMap->SendLength = BufIndex; 2070 } 2071 }/* else of if ( (NdefMap->ApduBufferSize - NdefMap->ApduBuffIndex) >= (uint32_t)(16* NdefMap->FelicaAttrInfo.Nbw )) */ 2072 status = phFriNfc_Felica_HWriteDataBlk(NdefMap); 2073 } 2074 else 2075 { 2076 /*0 represents the write operation ended*/ 2077 status = phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_ENDED); 2078 } 2079 return (status); 2080 } 2081 2082 2083 /*! 2084 * \brief Used in Write Opearation. 2085 * This function prepares and sends transcc Cmd Pkt. 2086 */ 2087 2088 static NFCSTATUS phFriNfc_Felica_HWriteDataBlk(phFriNfc_NdefMap_t *NdefMap) 2089 { 2090 NFCSTATUS status = NFCSTATUS_PENDING; 2091 2092 /*set the additional informations for the data exchange*/ 2093 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0; 2094 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0; 2095 2096 /*Set the ISO14434 command*/ 2097 #ifdef PH_HAL4_ENABLE 2098 NdefMap->Cmd.FelCmd = phHal_eFelica_Raw; 2099 #else 2100 NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd; 2101 #endif /* #ifdef PH_HAL4_ENABLE */ 2102 2103 /* set the state*/ 2104 NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_WR_BLOCK; 2105 2106 /* set send receive length*/ 2107 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 2108 2109 /*Call the Overlapped HAL Transceive function */ 2110 status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice, 2111 &NdefMap->MapCompletionInfo, 2112 NdefMap->psRemoteDevInfo, 2113 NdefMap->Cmd, 2114 &NdefMap->psDepAdditionalInfo, 2115 NdefMap->SendRecvBuf, 2116 NdefMap->SendLength, 2117 NdefMap->SendRecvBuf, 2118 NdefMap->SendRecvLength); 2119 return (status); 2120 } 2121 2122 /*! 2123 * \brief Check whether a particular Remote Device is NDEF compliant. 2124 * The function checks whether the peer device is NDEF compliant. 2125 */ 2126 2127 NFCSTATUS phFriNfc_Felica_ChkNdef( phFriNfc_NdefMap_t *NdefMap) 2128 { 2129 NFCSTATUS status = NFCSTATUS_PENDING; 2130 uint8_t sysCode[2]; 2131 2132 /* set the system code for selecting the wild card*/ 2133 sysCode[0] = 0x12; 2134 sysCode[1] = 0xFC; 2135 2136 status = phFriNfc_Felica_HPollCard( NdefMap,sysCode,PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_NDEF_APP); 2137 2138 return (status); 2139 2140 } 2141 /*! 2142 * \brief Check whether a particular Remote Device is NDEF compliant. 2143 * selects the sysCode and then NFC Forum Reference Applications 2144 */ 2145 #ifdef PH_HAL4_ENABLE 2146 static NFCSTATUS phFriNfc_Felica_HPollCard( phFriNfc_NdefMap_t *NdefMap, 2147 const uint8_t sysCode[], 2148 uint8_t state) 2149 { 2150 NFCSTATUS status = NFCSTATUS_PENDING; 2151 2152 /*Format the Poll Packet for selecting the system code passed as parameter */ 2153 NdefMap->SendRecvBuf[0] = 0x06; 2154 NdefMap->SendRecvBuf[1] = 0x00; 2155 NdefMap->SendRecvBuf[2] = sysCode[0]; 2156 NdefMap->SendRecvBuf[3] = sysCode[1]; 2157 NdefMap->SendRecvBuf[4] = 0x01; 2158 NdefMap->SendRecvBuf[5] = 0x03; 2159 2160 NdefMap->SendLength = 6; 2161 2162 /*set the completion routines for the felica card operations*/ 2163 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_Felica_Process; 2164 NdefMap->MapCompletionInfo.Context = NdefMap; 2165 2166 /*Set Ndef State*/ 2167 NdefMap->State = state; 2168 2169 /* set the felica cmd */ 2170 NdefMap->Cmd.FelCmd = phHal_eFelica_Raw; 2171 2172 /*set the additional informations for the data exchange*/ 2173 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0; 2174 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0; 2175 2176 status = phFriNfc_OvrHal_Transceive(NdefMap->LowerDevice, 2177 &NdefMap->MapCompletionInfo, 2178 NdefMap->psRemoteDevInfo, 2179 NdefMap->Cmd, 2180 &NdefMap->psDepAdditionalInfo, 2181 NdefMap->SendRecvBuf, 2182 NdefMap->SendLength, 2183 NdefMap->SendRecvBuf, 2184 NdefMap->SendRecvLength); 2185 return (status); 2186 } 2187 #endif 2188 2189 2190 #ifndef PH_HAL4_ENABLE 2191 static NFCSTATUS phFriNfc_Felica_HPollCard( phFriNfc_NdefMap_t *NdefMap, 2192 const uint8_t sysCode[], 2193 uint8_t state) 2194 { 2195 NFCSTATUS status = NFCSTATUS_PENDING; 2196 2197 /*Format the Poll Packet for selecting the wild card "0xff 0xff as system code*/ 2198 NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[0] = 0x00; 2199 NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[1] = sysCode[0]; 2200 NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[2] = sysCode[1]; 2201 NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[3] = 0x01; 2202 NdefMap->FelicaPollDetails.DevInputParam->FelicaPollPayload[4] = 0x03; 2203 2204 /* set the length to zero*/ 2205 NdefMap->FelicaPollDetails.DevInputParam->GeneralByteLength =0x00; 2206 2207 NdefMap->NoOfDevices = PH_FRINFC_NDEFMAP_FELI_NUM_DEVICE_TO_DETECT; 2208 2209 /*set the completion routines for the felica card operations*/ 2210 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_Felica_Process; 2211 NdefMap->MapCompletionInfo.Context = NdefMap; 2212 2213 /*Set Ndef State*/ 2214 NdefMap->State = state; 2215 2216 /* Harsha: This is a special case for felica. 2217 Make a copy of the remote device information and send it for 2218 polling. Return the original remote device information to the 2219 caller. The user does not need the updated results of the poll 2220 that we are going to call now. This is only used for checking 2221 whether the felica card is NDEF compliant or not. */ 2222 (void) memcpy( &NdefMap->FelicaPollDetails.psTempRemoteDevInfo, 2223 NdefMap->psRemoteDevInfo, 2224 sizeof(phHal_sRemoteDevInformation_t)); 2225 2226 /* Reset the session opened flag */ 2227 NdefMap->FelicaPollDetails.psTempRemoteDevInfo.SessionOpened = 0x00; 2228 2229 /*Call the Overlapped HAL POLL function */ 2230 status = phFriNfc_OvrHal_Poll( NdefMap->LowerDevice, 2231 &NdefMap->MapCompletionInfo, 2232 NdefMap->OpModeType, 2233 &NdefMap->FelicaPollDetails.psTempRemoteDevInfo, 2234 &NdefMap->NoOfDevices, 2235 NdefMap->FelicaPollDetails.DevInputParam); 2236 2237 return (status); 2238 } 2239 #endif /* #ifndef PH_HAL4_ENABLE */ 2240 /*! 2241 * \brief Checks validity of system code sent from the lower device, during poll operation. 2242 */ 2243 2244 static NFCSTATUS phFriNfc_Felica_HUpdateManufIdDetails(const phFriNfc_NdefMap_t *NdefMap) 2245 { 2246 NFCSTATUS status = NFCSTATUS_PENDING; 2247 2248 /* Get the details from Poll Response packet */ 2249 if (NdefMap->SendRecvLength >= 20) 2250 { 2251 (void)memcpy( (uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm, 2252 (uint8_t *)&NdefMap->SendRecvBuf[2], 8); 2253 (void)memcpy( (uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm, 2254 (uint8_t *)&NdefMap->SendRecvBuf[10], 8); 2255 NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[1] = NdefMap->SendRecvBuf[18]; 2256 NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[0] = NdefMap->SendRecvBuf[19]; 2257 NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDmLength = 8; 2258 2259 /* copy the IDm and PMm in Manufacture Details Structure*/ 2260 (void)memcpy( (uint8_t *)(NdefMap->FelicaManufDetails.ManufID), 2261 (uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm, 2262 8); 2263 (void)memcpy( (uint8_t *)(NdefMap->FelicaManufDetails.ManufParameter), 2264 (uint8_t *)NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm, 2265 8); 2266 if((NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[1] == 0x12) 2267 && (NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode[0] == 0xFC)) 2268 { 2269 status = PHNFCSTVAL(CID_NFC_NONE, NFCSTATUS_SUCCESS); 2270 } 2271 else 2272 { 2273 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2274 NFCSTATUS_NO_NDEF_SUPPORT); 2275 } 2276 } 2277 else 2278 { 2279 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2280 NFCSTATUS_NO_NDEF_SUPPORT); 2281 } 2282 2283 return (status); 2284 } 2285 2286 2287 /*! 2288 * \brief Completion Routine, Processing function, needed to avoid long blocking. 2289 * \note The lower (Overlapped HAL) layer must register a pointer to this function as a Completion 2290 * Routine in order to be able to notify the component that an I/O has finished and data are 2291 * ready to be processed. 2292 */ 2293 2294 void phFriNfc_Felica_Process(void *Context, 2295 NFCSTATUS Status) 2296 { 2297 uint8_t CRFlag = FALSE; 2298 uint16_t RecvTxLen = 0, 2299 BytesToRecv = 0, 2300 Nbc = 0; 2301 uint32_t TotNoWrittenBytes = 0, 2302 NDEFLen=0; 2303 2304 /*Set the context to Map Module*/ 2305 phFriNfc_NdefMap_t *NdefMap = (phFriNfc_NdefMap_t *)Context; 2306 2307 if ( Status == NFCSTATUS_SUCCESS ) 2308 { 2309 switch (NdefMap->State) 2310 { 2311 case PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_NDEF_APP: 2312 2313 /* check the ndef compliency with the system code reecived in the RemoteDevInfo*/ 2314 Status = phFriNfc_Felica_HUpdateManufIdDetails(NdefMap); 2315 2316 if (Status == NFCSTATUS_SUCCESS) 2317 { 2318 /* Mantis ID : 645*/ 2319 /* set the operation type to Check ndef type*/ 2320 NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_CHK_NDEF_OP; 2321 Status = phFriNfc_Felica_HRdAttrInfo(NdefMap); 2322 /* handle the error in Transc function*/ 2323 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2324 { 2325 CRFlag = TRUE; 2326 } 2327 } 2328 else 2329 { 2330 CRFlag = TRUE; 2331 } 2332 if ( CRFlag == TRUE ) 2333 { 2334 /* call respective CR */ 2335 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,Status); 2336 2337 } 2338 break; 2339 2340 case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR: 2341 /* check for the status flag1 and status flag2for the successful read operation*/ 2342 if ( NdefMap->SendRecvBuf[10] == 0x00) 2343 { 2344 /* check the Manuf Id in the receive buffer*/ 2345 Status = phFriNfc_Felica_HCheckManufId(NdefMap); 2346 if ( Status == NFCSTATUS_SUCCESS) 2347 { 2348 /* Update the Attribute Information in to the context structure*/ 2349 Status = phFriNfc_Felica_HUpdateAttrInfo(NdefMap); 2350 if ( Status == NFCSTATUS_SUCCESS ) 2351 { 2352 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0], 2353 NdefMap->FelicaAttrInfo.LenBytes[1], 2354 NdefMap->FelicaAttrInfo.LenBytes[2], 2355 NDEFLen); 2356 2357 if ( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_WR_ATTR_RD_OP ) 2358 { 2359 /* Proceed With Write Functinality*/ 2360 Status = phFriNfc_Felica_HChkAttrBlkForWrOp(NdefMap); 2361 /* handle the error in Transc function*/ 2362 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2363 { 2364 /* call respective CR */ 2365 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2366 } 2367 } 2368 else if( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_RD_ATTR_RD_OP ) 2369 { 2370 /* Proceed With Read Functinality*/ 2371 Status = phFriNfc_Felica_HChkAttrBlkForRdOp(NdefMap,NDEFLen); 2372 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2373 { 2374 /* call respective CR */ 2375 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_RD_NDEF,Status); 2376 } 2377 } 2378 else if( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_CHK_NDEF_OP ) 2379 { 2380 2381 Status = phFriNfc_MapTool_SetCardState( NdefMap, 2382 NDEFLen); 2383 /* check status value*/ 2384 NdefMap->CardType = PH_FRINFC_NDEFMAP_FELICA_SMART_CARD; 2385 /*reset the buffer index*/ 2386 NdefMap->ApduBuffIndex = 0; 2387 /* set the Next operation Flag to indicate need of reading attribute information*/ 2388 NdefMap->Felica.OpFlag = PH_FRINFC_NDEFMAP_FELI_OP_NONE; 2389 /* call respective CR */ 2390 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_CHK_NDEF,Status); 2391 } 2392 else if ( NdefMap->Felica.OpFlag == PH_FRINFC_NDEFMAP_FELI_WR_EMPTY_MSG_OP ) 2393 { 2394 /* Proceed With Write Functinality*/ 2395 Status = phFriNfc_Felica_HWrEmptyMsg(NdefMap); 2396 /* handle the error in Transc function*/ 2397 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2398 { 2399 /* call respective CR */ 2400 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_ERASE_NDEF,Status); 2401 } 2402 } 2403 else 2404 { 2405 2406 /* invalid operation occured*/ 2407 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 2408 NFCSTATUS_INVALID_DEVICE_REQUEST); 2409 CRFlag =TRUE ; 2410 } 2411 } 2412 else 2413 { 2414 CRFlag =TRUE ; 2415 } 2416 } 2417 else 2418 { 2419 CRFlag =TRUE ; 2420 } 2421 } 2422 else 2423 { 2424 CRFlag =TRUE; 2425 /*handle the Error case*/ 2426 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 2427 NFCSTATUS_READ_FAILED); 2428 } 2429 if ( CRFlag == TRUE ) 2430 { 2431 /* call respective CR */ 2432 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_RD_NDEF,Status); 2433 } 2434 break; 2435 2436 case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN: 2437 /* chk the status flags 1 and 2*/ 2438 if ( NdefMap->SendRecvBuf[10] == 0x00 ) 2439 { 2440 /* Update Data Call*/ 2441 Status =phFriNfc_Felica_HUpdateData(NdefMap); 2442 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2443 { 2444 /* call respective CR */ 2445 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2446 } 2447 } 2448 else 2449 { 2450 /*handle the Error case*/ 2451 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 2452 NFCSTATUS_WRITE_FAILED); 2453 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2454 2455 } 2456 break; 2457 case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END: 2458 2459 /* chk the status flags 1 and 2*/ 2460 if ( NdefMap->SendRecvBuf[10] == 0x00) 2461 { 2462 /* Entire Write Operation is complete*/ 2463 Status = PHNFCSTVAL(CID_NFC_NONE,\ 2464 NFCSTATUS_SUCCESS); 2465 } 2466 else 2467 { 2468 /*handle the Error case*/ 2469 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 2470 NFCSTATUS_WRITE_FAILED); 2471 } 2472 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2473 break; 2474 2475 case PH_NFCFRI_NDEFMAP_FELI_STATE_WR_EMPTY_MSG : 2476 2477 /* chk the status flags 1 and 2*/ 2478 if ( NdefMap->SendRecvBuf[10] == 0x00) 2479 { 2480 /* Entire Write Operation is complete*/ 2481 Status = PHNFCSTVAL(CID_NFC_NONE,\ 2482 NFCSTATUS_SUCCESS); 2483 } 2484 else 2485 { 2486 /*handle the Error case*/ 2487 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 2488 NFCSTATUS_WRITE_FAILED); 2489 } 2490 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2491 break; 2492 2493 case PH_NFCFRI_NDEFMAP_FELI_STATE_WR_BLOCK : 2494 if(NdefMap->SendRecvBuf[1] == PH_NFCFRI_NDEFMAP_FELI_WR_RESP_BYTE ) 2495 { 2496 /* chk the status flags 1 and 2*/ 2497 if ( NdefMap->SendRecvBuf[10] == 0x00 ) 2498 { 2499 /* This is used when we have bytes less than 16 bytes*/ 2500 if ( NdefMap->Felica.IntermediateWrFlag == TRUE ) 2501 { 2502 /* after Successful write copy the last writtened bytes back to the 2503 internal buffer*/ 2504 (void)memcpy( (&(NdefMap->Felica.Wr_RemainedBytesBuff[NdefMap->Felica.Wr_BytesRemained])), 2505 NdefMap->ApduBuffer, 2506 NdefMap->NumOfBytesWritten); 2507 2508 NdefMap->Felica.Wr_BytesRemained += 2509 (uint8_t)( NdefMap->NumOfBytesWritten); 2510 2511 /* Increment the Send Buffer index */ 2512 NdefMap->ApduBuffIndex += 2513 NdefMap->NumOfBytesWritten; 2514 2515 *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex; 2516 NdefMap->Felica.IntermediateWrFlag = FALSE; 2517 /* Call Update Data()*/ 2518 Status = phFriNfc_Felica_HUpdateData(NdefMap); 2519 } 2520 else 2521 { 2522 /* update the index and bytes writtened*/ 2523 NdefMap->ApduBuffIndex += NdefMap->NumOfBytesWritten; 2524 *NdefMap->WrNdefPacketLength = NdefMap->ApduBuffIndex; 2525 if ( NdefMap->Felica.EofCardReachedFlag ) 2526 { 2527 if ( NdefMap->Felica.CurBlockNo < NdefMap->FelicaAttrInfo.Nmaxb) 2528 { 2529 NdefMap->Felica.CurBlockNo += NdefMap->Felica.NoBlocksWritten; 2530 } 2531 if (( NdefMap->Felica.CurBlockNo == NdefMap->FelicaAttrInfo.Nmaxb) && 2532 ( NdefMap->ApduBuffIndex == (NdefMap->FelicaAttrInfo.Nmaxb*16))) 2533 { 2534 NdefMap->Felica.EofCardReachedFlag = FELICA_RD_WR_EOF_CARD_REACHED ; 2535 /*0 represents the write ended*/ 2536 Status = phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_ENDED); 2537 if( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2538 { 2539 /* call respective CR */ 2540 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2541 } 2542 } 2543 else 2544 { 2545 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES(NdefMap->FelicaAttrInfo.LenBytes[0], 2546 NdefMap->FelicaAttrInfo.LenBytes[1], 2547 NdefMap->FelicaAttrInfo.LenBytes[2], 2548 TotNoWrittenBytes); 2549 if ( ( NdefMap->Felica.CurBlockNo == NdefMap->FelicaAttrInfo.Nmaxb) && 2550 ((TotNoWrittenBytes + NdefMap->ApduBuffIndex) == (uint32_t)(NdefMap->FelicaAttrInfo.Nmaxb*16))) 2551 { 2552 NdefMap->Felica.EofCardReachedFlag =FELICA_RD_WR_EOF_CARD_REACHED; 2553 /*0 represents the write ended*/ 2554 Status = phFriNfc_Felica_HUpdateAttrBlkForWrOp(NdefMap,FELICA_WRITE_ENDED); 2555 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2556 { 2557 /* call respective CR */ 2558 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2559 } 2560 } 2561 else 2562 { 2563 /* Call Update Data()*/ 2564 Status = phFriNfc_Felica_HUpdateData(NdefMap); 2565 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2566 { 2567 /* call respective CR */ 2568 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2569 } 2570 } 2571 } 2572 }/*if ( NdefMap->Felica.EofCardReachedFlag )*/ 2573 else 2574 { 2575 NdefMap->Felica.CurBlockNo += NdefMap->Felica.NoBlocksWritten; 2576 /* Call Update Data()*/ 2577 Status = phFriNfc_Felica_HUpdateData(NdefMap); 2578 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2579 { 2580 /* call respective CR */ 2581 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2582 } 2583 } 2584 } 2585 }/*if ( NdefMap->SendRecvBuf[10] == 0x00 )*/ 2586 else 2587 { 2588 /*handle the Error case*/ 2589 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 2590 NFCSTATUS_WRITE_FAILED); 2591 CRFlag = TRUE; 2592 2593 } 2594 }/*if(NdefMap->SendRecvBuf[1] == PH_NFCFRI_NDEFMAP_FELI_WR_RESP_BYTE )*/ 2595 else 2596 { 2597 /*return Error "Invalid Write Response Code"*/ 2598 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2599 NFCSTATUS_WRITE_FAILED); 2600 CRFlag = TRUE; 2601 2602 } 2603 if ( CRFlag == TRUE ) 2604 { 2605 /* Reset following parameters*/ 2606 NdefMap->ApduBuffIndex=0; 2607 NdefMap->Felica.Wr_BytesRemained = 0; 2608 NdefMap->ApduBufferSize = 0; 2609 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_WR_NDEF,Status); 2610 } 2611 2612 break; 2613 2614 case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_BLOCK : 2615 2616 /* check the Manuf Id in the receive buffer*/ 2617 Status = phFriNfc_Felica_HCheckManufId(NdefMap); 2618 if ( Status == NFCSTATUS_SUCCESS ) 2619 { 2620 if(NdefMap->SendRecvBuf[1] == PH_NFCFRI_NDEFMAP_FELI_RD_RESP_BYTE ) 2621 { 2622 /* calculate the Nmaxb*/ 2623 Nbc = phFriNfc_Felica_HGetMaximumBlksToRead(NdefMap,PH_NFCFRI_NDEFMAP_FELI_NBC); 2624 /*get Receive length from the card for corss verifications*/ 2625 RecvTxLen= phFriNfc_Felica_HSetTrxLen(NdefMap,Nbc); 2626 BytesToRecv = NdefMap->SendRecvBuf[12]*16; 2627 2628 /* chk the status flags 1 */ 2629 if ( NdefMap->SendRecvBuf[10] == 0x00) 2630 { 2631 if ( RecvTxLen == BytesToRecv) 2632 { 2633 NdefMap->Felica.CurBlockNo += (uint8_t)(RecvTxLen/16); 2634 phFriNfc_Felica_HAfterRead_CopyDataToBuff(NdefMap); 2635 Status = phFriNfc_Felica_HReadData(NdefMap,PH_FRINFC_NDEFMAP_SEEK_CUR); 2636 /* handle the error in Transc function*/ 2637 if ( (Status & PHNFCSTBLOWER) != (NFCSTATUS_PENDING & PHNFCSTBLOWER)) 2638 { 2639 CRFlag =TRUE; 2640 } 2641 } 2642 else 2643 { 2644 CRFlag =TRUE; 2645 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2646 NFCSTATUS_INVALID_RECEIVE_LENGTH); 2647 /*set the buffer index back to zero*/ 2648 NdefMap->ApduBuffIndex = 0; 2649 NdefMap->Felica.Rd_NoBytesToCopy = 0; 2650 } 2651 } 2652 else 2653 { 2654 NdefMap->ApduBuffIndex=0; 2655 /*handle the Error case*/ 2656 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\ 2657 NFCSTATUS_READ_FAILED); 2658 CRFlag =TRUE; 2659 } 2660 } 2661 else 2662 { 2663 CRFlag =TRUE; 2664 NdefMap->ApduBuffIndex=0; 2665 /*return Error "Invalid Read Response Code"*/ 2666 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2667 NFCSTATUS_READ_FAILED); 2668 } 2669 } 2670 else 2671 { 2672 CRFlag =TRUE; 2673 } 2674 if ( CRFlag ==TRUE ) 2675 { 2676 /* call respective CR */ 2677 phFriNfc_Felica_HCrHandler(NdefMap,PH_FRINFC_NDEFMAP_CR_RD_NDEF,Status); 2678 } 2679 break; 2680 2681 default: 2682 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2683 NFCSTATUS_INVALID_DEVICE_REQUEST); 2684 phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_INVALID_OPE, Status); 2685 break; 2686 2687 2688 } 2689 } 2690 else 2691 { 2692 /* Call CR for unknown Error's*/ 2693 switch ( NdefMap->State) 2694 { 2695 case PH_FRINFC_NDEFMAP_FELI_STATE_CHK_NDEF : 2696 case PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_WILD_CARD : 2697 case PH_NFCFRI_NDEFMAP_FELI_STATE_SELECT_NDEF_APP : 2698 case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR : 2699 phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_CHK_NDEF, 2700 Status); 2701 break; 2702 case PH_NFCFRI_NDEFMAP_FELI_STATE_WR_BLOCK : 2703 case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_BEGIN : 2704 case PH_NFCFRI_NDEFMAP_FELI_STATE_ATTR_BLK_WR_END : 2705 phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_WR_NDEF, 2706 Status); 2707 break; 2708 case PH_NFCFRI_NDEFMAP_FELI_STATE_RD_BLOCK : 2709 phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_RD_NDEF, 2710 Status); 2711 break; 2712 default : 2713 /*set the invalid state*/ 2714 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST); 2715 phFriNfc_Felica_HCrHandler(NdefMap, PH_FRINFC_NDEFMAP_CR_INVALID_OPE, Status); 2716 break; 2717 } 2718 } 2719 2720 } 2721 2722 /*! 2723 * \brief Prepares Cmd Pkt for reading attribute Blk information. 2724 */ 2725 static NFCSTATUS phFriNfc_Felica_HRdAttrInfo(phFriNfc_NdefMap_t *NdefMap) 2726 { 2727 2728 NFCSTATUS status = NFCSTATUS_PENDING; 2729 uint8_t BufIndex = 0; 2730 2731 /* Set the Felica Cmd*/ 2732 #ifdef PH_HAL4_ENABLE 2733 NdefMap->Cmd.FelCmd = phHal_eFelica_Raw; 2734 #else 2735 NdefMap->Cmd.FelCmd = phHal_eFelicaCmdListFelicaCmd; 2736 #endif /* #ifdef PH_HAL4_ENABLE */ 2737 2738 /*set the additional informations for the data exchange*/ 2739 NdefMap->psDepAdditionalInfo.DepFlags.MetaChaining = 0; 2740 NdefMap->psDepAdditionalInfo.DepFlags.NADPresent = 0; 2741 2742 /* 1st byte represents the length of the cmd packet*/ 2743 NdefMap->SendRecvBuf[BufIndex] = 0x00; 2744 BufIndex++; 2745 2746 /* Read/check command code*/ 2747 NdefMap->SendRecvBuf[BufIndex] = 0x06; 2748 BufIndex++; 2749 2750 /* IDm - Manufacturer Id : 8bytes*/ 2751 #ifdef PH_HAL4_ENABLE 2752 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 2753 (void * )&NdefMap->psRemoteDevInfo->RemoteDevInfo.Felica_Info.IDm, 2754 8); 2755 #else 2756 (void)memcpy((&(NdefMap->SendRecvBuf[BufIndex])), 2757 (void * )&NdefMap->psRemoteDevInfo->RemoteDevInfo.CardInfo212_424.Startup212_424.NFCID2t, 2758 8); 2759 2760 #endif /* #ifdef PH_HAL4_ENABLE */ 2761 2762 BufIndex+=8; 2763 2764 NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Services (n=1 ==> 0x80)*/ 2765 BufIndex++; 2766 2767 NdefMap->SendRecvBuf[BufIndex] = 0x0B; /* Service Code List*/ 2768 BufIndex++; 2769 2770 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* Service Code List*/ 2771 BufIndex++; 2772 2773 NdefMap->SendRecvBuf[BufIndex] = 0x01; /* Number of Blocks to read)*/ 2774 BufIndex++; 2775 2776 NdefMap->SendRecvBuf[BufIndex] = 0x80; /* 1st Block Element : byte 1*/ 2777 BufIndex++; 2778 2779 NdefMap->SendRecvBuf[BufIndex] = 0x00; /* 1st Block Element : byte 2, block 1*/ 2780 BufIndex++; 2781 2782 NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_PKT_LEN_INDEX] = BufIndex; 2783 2784 *NdefMap->SendRecvLength = NdefMap->TempReceiveLength; 2785 2786 /* Update the Send Len*/ 2787 NdefMap->SendLength = BufIndex; 2788 2789 /* Change the state to PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR */ 2790 NdefMap->State = PH_NFCFRI_NDEFMAP_FELI_STATE_RD_ATTR; 2791 2792 /*set the completion routines for the desfire card operations*/ 2793 NdefMap->MapCompletionInfo.CompletionRoutine = phFriNfc_NdefMap_Process; 2794 NdefMap->MapCompletionInfo.Context = NdefMap; 2795 2796 /*Call the Overlapped HAL Transceive function */ 2797 status = phFriNfc_OvrHal_Transceive( NdefMap->LowerDevice, 2798 &NdefMap->MapCompletionInfo, 2799 NdefMap->psRemoteDevInfo, 2800 NdefMap->Cmd, 2801 &NdefMap->psDepAdditionalInfo, 2802 NdefMap->SendRecvBuf, 2803 NdefMap->SendLength, 2804 NdefMap->SendRecvBuf, 2805 NdefMap->SendRecvLength); 2806 return (status); 2807 2808 } 2809 2810 /*! 2811 * \brief Validated manufacturer Details, during the read/write operations. 2812 */ 2813 2814 static NFCSTATUS phFriNfc_Felica_HCheckManufId(const phFriNfc_NdefMap_t *NdefMap) 2815 { 2816 NFCSTATUS status = NFCSTATUS_PENDING; 2817 2818 uint8_t result = 0; 2819 2820 /* check the stored manufacture id with the received manufacture id*/ 2821 result = (uint8_t)(phFriNfc_Felica_MemCompare( (void *)(&(NdefMap->SendRecvBuf[2])), 2822 (void *)NdefMap->FelicaManufDetails.ManufID, 2823 8)); 2824 2825 if ( result != 0) 2826 { 2827 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_REMOTE_DEVICE); 2828 2829 } 2830 else 2831 { 2832 status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS); 2833 2834 } 2835 return (status); 2836 } 2837 2838 static NFCSTATUS phFriNfc_Felica_HCalCheckSum(const uint8_t *TempBuffer, 2839 uint8_t StartIndex, 2840 uint8_t EndIndex, 2841 uint16_t RecvChkSum) 2842 { 2843 NFCSTATUS Result = NFCSTATUS_SUCCESS; 2844 uint16_t CheckSum=0, 2845 BufIndex=0; 2846 2847 for(BufIndex = StartIndex;BufIndex <=EndIndex;BufIndex++) 2848 { 2849 CheckSum += TempBuffer[BufIndex]; 2850 } 2851 if( RecvChkSum != CheckSum ) 2852 { 2853 Result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, 2854 NFCSTATUS_INVALID_FORMAT); 2855 } 2856 return (Result); 2857 } 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 /*! 2870 * \brief On successful read attribute blk information, this function validates and stores the 2871 * Attribute informations in to the context. 2872 */ 2873 static NFCSTATUS phFriNfc_Felica_HUpdateAttrInfo(phFriNfc_NdefMap_t *NdefMap) 2874 { 2875 NFCSTATUS status = NFCSTATUS_PENDING; 2876 uint8_t CRFlag = FALSE, 2877 Nmaxb1, Nmaxb2 = 0, 2878 ChkSum1 = 0, ChkSum2=0; 2879 2880 uint16_t Nmaxblk = 0, 2881 RecvChkSum=0, 2882 NdefBlk = 0; 2883 uint32_t DataLen =0; 2884 2885 2886 /* Validate T3VNo and NFCDevVNo */ 2887 status = phFriNfc_MapTool_ChkSpcVer(NdefMap, 2888 PH_NFCFRI_NDEFMAP_FELI_VERSION_INDEX); 2889 if ( status != NFCSTATUS_SUCCESS ) 2890 { 2891 CRFlag = TRUE; 2892 } 2893 else 2894 { 2895 /* get the Nmaxb from the receive buffer*/ 2896 Nmaxb1 = NdefMap->SendRecvBuf[16]; 2897 Nmaxb2 = NdefMap->SendRecvBuf[17]; 2898 2899 Nmaxblk = (((uint16_t)Nmaxb1 << 8) | (Nmaxb2 & 0x00ff)); 2900 2901 if ( Nmaxblk != 0 ) 2902 { 2903 /* check the Nbr against the Nmaxb*/ 2904 if ( NdefMap->SendRecvBuf[14] > Nmaxblk ) 2905 { 2906 CRFlag = TRUE; 2907 } 2908 else 2909 { 2910 /*check Nbw > Nmaxb*/ 2911 /*check the write flag validity*/ 2912 /*check for the RFU bytes validity*/ 2913 if ( (NdefMap->SendRecvBuf[15] > Nmaxblk) || 2914 ((NdefMap->SendRecvBuf[22] != 0x00) && (NdefMap->SendRecvBuf[22] !=0x0f ))|| 2915 ( (NdefMap->SendRecvBuf[23] != 0x00) && (NdefMap->SendRecvBuf[23] !=0x01 ))|| 2916 ( NdefMap->SendRecvBuf[18] != 0x00) || 2917 ( NdefMap->SendRecvBuf[19] != 0x00) || 2918 ( NdefMap->SendRecvBuf[20] != 0x00) || 2919 ( NdefMap->SendRecvBuf[21] != 0x00)) 2920 2921 { 2922 CRFlag = TRUE; 2923 } 2924 else 2925 { 2926 /* check the validity of the actual ndef data len*/ 2927 PH_NFCFRI_NDEFMAP_FELI_CAL_LEN_BYTES( NdefMap->SendRecvBuf[24], 2928 NdefMap->SendRecvBuf[25], 2929 NdefMap->SendRecvBuf[26], 2930 DataLen); 2931 2932 2933 /* Calculate Nbc*/ 2934 NdefBlk = (uint16_t )((( DataLen % 16) == 0 ) ? (DataLen >> 4) : ((DataLen >> 4) +1)); 2935 2936 /* check Nbc against Nmaxb*/ 2937 if ((NdefBlk > Nmaxblk)) 2938 { 2939 CRFlag = TRUE; 2940 } 2941 else 2942 { 2943 /*Store the attribute information in phFriNfc_Felica_AttrInfo*/ 2944 NdefMap->FelicaAttrInfo.Version = NdefMap->SendRecvBuf[PH_NFCFRI_NDEFMAP_FELI_VERSION_INDEX]; 2945 NdefMap->FelicaAttrInfo.Nbr = NdefMap->SendRecvBuf[14]; 2946 NdefMap->FelicaAttrInfo.Nbw = NdefMap->SendRecvBuf[15]; 2947 2948 NdefMap->FelicaAttrInfo.Nmaxb = Nmaxblk; 2949 2950 NdefMap->FelicaAttrInfo.WriteFlag = NdefMap->SendRecvBuf[22]; 2951 NdefMap->FelicaAttrInfo.RdWrFlag = NdefMap->SendRecvBuf[23]; 2952 2953 /* Get CheckSum*/ 2954 ChkSum1 = NdefMap->SendRecvBuf[27]; 2955 ChkSum2 = NdefMap->SendRecvBuf[28]; 2956 2957 RecvChkSum = (((uint16_t)ChkSum1 << 8) | (ChkSum2 & 0x00ff)); 2958 2959 /* Check the check sum validity?*/ 2960 status = phFriNfc_Felica_HCalCheckSum(NdefMap->SendRecvBuf, 2961 PH_NFCFRI_NDEFMAP_FELI_VERSION_INDEX, 2962 26, 2963 RecvChkSum); 2964 if ( status != NFCSTATUS_SUCCESS ) 2965 { 2966 CRFlag = TRUE; 2967 } 2968 else 2969 { 2970 /*check RW Flag Access Rights*/ 2971 /* set to read only cannot write*/ 2972 if ( NdefMap->FelicaAttrInfo.RdWrFlag == 0x00 ) 2973 { 2974 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_ONLY; 2975 } 2976 else if ( NdefMap->FelicaAttrInfo.RdWrFlag == 0x01 ) // additional check for R/W access 2977 { 2978 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE; 2979 } 2980 else // otherwise invalid 2981 { 2982 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 2983 } 2984 2985 NdefMap->FelicaAttrInfo.LenBytes[0] = NdefMap->SendRecvBuf[24]; 2986 NdefMap->FelicaAttrInfo.LenBytes[1] = NdefMap->SendRecvBuf[25]; 2987 NdefMap->FelicaAttrInfo.LenBytes[2] = NdefMap->SendRecvBuf[26]; 2988 status = PHNFCSTVAL(CID_NFC_NONE,NFCSTATUS_SUCCESS); 2989 } 2990 } 2991 } 2992 } 2993 } 2994 else 2995 { 2996 CRFlag = TRUE; 2997 } 2998 } 2999 if ( (status == NFCSTATUS_INVALID_FORMAT ) && (CRFlag == TRUE )) 3000 { 3001 NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID; 3002 } 3003 if ( CRFlag == TRUE ) 3004 { 3005 /*Return Status Error Invalid Format*/ 3006 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_FORMAT); 3007 } 3008 3009 return (status); 3010 } 3011 3012 /*! 3013 * \brief this shall notify the integration software with respective 3014 * success/error status along with the completion routines. 3015 */ 3016 static void phFriNfc_Felica_HCrHandler(phFriNfc_NdefMap_t *NdefMap, 3017 uint8_t CrIndex, 3018 NFCSTATUS Status) 3019 { 3020 /* set the state back to the Reset_Init state*/ 3021 NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT; 3022 3023 /* set the completion routine*/ 3024 NdefMap->CompletionRoutine[CrIndex]. 3025 CompletionRoutine(NdefMap->CompletionRoutine->Context, Status); 3026 } 3027 3028 /*! 3029 * \brief this shall initialise the internal buffer data to zero. 3030 */ 3031 static void phFriNfc_Felica_HInitInternalBuf(uint8_t *Buffer) 3032 { 3033 uint8_t index=0; 3034 3035 for( index = 0; index< 16 ; index++) 3036 { 3037 Buffer[index] = 0; 3038 } 3039 } 3040 3041 static int phFriNfc_Felica_MemCompare ( void *s1, void *s2, unsigned int n ) 3042 { 3043 int8_t diff = 0; 3044 int8_t *char_1 =(int8_t *)s1; 3045 int8_t *char_2 =(int8_t *)s2; 3046 if(NULL == s1 || NULL == s2) 3047 { 3048 PHDBG_CRITICAL_ERROR("NULL pointer passed to memcompare"); 3049 } 3050 else 3051 { 3052 for(;((n>0)&&(diff==0));n--,char_1++,char_2++) 3053 { 3054 diff = *char_1 - *char_2; 3055 } 3056 } 3057 return (int)diff; 3058 } 3059 3060 3061 #ifdef UNIT_TEST 3062 #include <phUnitTestNfc_Felica_static.c> 3063 #endif 3064 3065 #endif /* PH_FRINFC_MAP_FELICA_DISABLED */ 3066