00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <utf.h>
00031 #include <nfctype1connection.h>
00032 #include <nfctype2connection.h>
00033 #include <nfctype3connection.h>
00034 #include <mifareclassicconnection.h>
00035 #include <iso14443connection.h>
00036 #include <nfctype1address.h>
00037 #include <nfctype2address.h>
00038 #include <nfctype3address.h>
00039
00040 #include <QString>
00041
00042 #include "activewait.h"
00043 #include "nfctagsdiscovery_symbian.h"
00044 #include "nfctagsdiscovery.h"
00045
00046 _LIT8(KSendText,"Sending data to tag");
00047
00048
00049 NfcTagsDiscoveryPrivate* NfcTagsDiscoveryPrivate::NewL(NfcTagsDiscovery *aPublicAPI)
00050 {
00051 NfcTagsDiscoveryPrivate* self = NewLC(aPublicAPI);
00052 CleanupStack::Pop( self );
00053 return self;
00054 }
00055
00056
00057 NfcTagsDiscoveryPrivate* NfcTagsDiscoveryPrivate::NewLC(NfcTagsDiscovery *aPublicAPI)
00058 {
00059 NfcTagsDiscoveryPrivate* self = new (ELeave) NfcTagsDiscoveryPrivate(aPublicAPI);
00060 CleanupStack::PushL( self );
00061 self->ConstructL(aPublicAPI);
00062 return self;
00063 }
00064
00065
00066 NfcTagsDiscoveryPrivate::~NfcTagsDiscoveryPrivate()
00067 {
00068 iWait->AsyncStop();
00069 delete iPublicAPI;
00070 delete iWait;
00071 iNfcTagDiscovery->RemoveTagConnectionListener();
00072 iNfcTagDiscovery->RemoveTagSubscription();
00073 delete iNfcTagDiscovery ;
00074 delete iSubscription;
00075 delete iNfcType1Connection;
00076 delete iNfcType2Connection;
00077 delete iNfcType3Connection;
00078 delete iMifareClassicConnection;
00079 delete iIso14443Connection;
00080 delete iMifareClassicDataSector;
00081 iWriteBuffer.Close();
00082 iReadBuffer.Close();
00083 iServer.Close();
00084 delete iReadWait;
00085 delete iWriteWait;
00086 iNdefDiscovery->RemoveNdefMessageListener();
00087 iNdefDiscovery->RemoveAllNdefSubscription();
00088 delete iNdefDiscovery;
00089 }
00090
00101 void NfcTagsDiscoveryPrivate::StartTagDiscovery()
00102 {
00103
00104 emit iPublicAPI->clearText();
00105
00106
00107 emit iPublicAPI->changeMenu(NfcTagsDiscoveryPrivate::ETagDiscovery);
00108
00109
00110 TInt err = iNfcTagDiscovery->AddTagConnectionListener( *this );
00111 if(err == KErrNone)
00112 {
00113 emit iPublicAPI->showMessage("Added tag connection listener\n");
00114 }
00115
00116
00117 iSubscription = CNfcTagSubscription::NewL();
00118 iSubscription->AddConnectionModeL(TNfcConnectionInfo::ENfcType1);
00119 iSubscription->AddConnectionModeL(TNfcConnectionInfo::ENfcType2);
00120 iSubscription->AddConnectionModeL(TNfcConnectionInfo::ENfcType3);
00121 iSubscription->AddConnectionModeL(TNfcConnectionInfo::ENfcMifareStd);
00122 iSubscription->AddConnectionModeL(TNfcConnectionInfo::ENfc14443P4);
00123 TRAP(err,iNfcTagDiscovery->AddTagSubscriptionL( *iSubscription ));
00124
00125 if(err == KErrNone)
00126 {
00127 emit iPublicAPI->showMessage("Added all tag connections to subscription.\n");
00128 iWait->Start();
00129 }
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 void NfcTagsDiscoveryPrivate::StartNdefDiscovery()
00141 {
00142
00143 emit iPublicAPI->clearText();
00144
00145 emit iPublicAPI->changeMenu(NfcTagsDiscoveryPrivate::ENdefDiscovery);
00146 TInt err = iNdefDiscovery->AddNdefMessageListener( *this );
00147
00148
00149 _LIT8( KSmartPoster, "Sp" );
00150 _LIT8( KTextRTD, "T" );
00151 _LIT8( KUriRTD, "U" );
00152
00153
00154 err = iNdefDiscovery->AddNdefSubscription( CNdefRecord::ENfcWellKnown, KUriRTD);
00155 err = iNdefDiscovery->AddNdefSubscription( CNdefRecord::ENfcWellKnown, KSmartPoster);
00156 err = iNdefDiscovery->AddNdefSubscription( CNdefRecord::ENfcWellKnown, KTextRTD);
00157 if(err == KErrNone)
00158 {
00159 emit iPublicAPI->showMessage("Added NDEF subscriptions successfully.\n");
00160 iWait->Start();
00161 }
00162 }
00163
00164
00165 void NfcTagsDiscoveryPrivate::StopTagDiscovery()
00166 {
00167 iWait->AsyncStop();
00168 iNfcTagDiscovery->RemoveTagConnectionListener();
00169 iNfcTagDiscovery->RemoveTagSubscription();
00170 }
00171
00172
00173 void NfcTagsDiscoveryPrivate::StopNdefDiscovery()
00174 {
00175 iWait->AsyncStop();
00176 iNdefDiscovery->RemoveNdefMessageListener();
00177 iNdefDiscovery->RemoveAllNdefSubscription();
00178 }
00179
00184 void NfcTagsDiscoveryPrivate::TagOperationCompleted()
00185 {
00186 if(iTagOperation ==
00187 NfcTagsDiscoveryPrivate::EExchangeDataWithIso14443Tag){
00188 emit iPublicAPI->showMessage("Exchange data with Iso14443 "
00189 "tag is successfull.\n");
00190 ShowTagInformation();
00191 }
00192 else if(iTagOperation ==
00193 NfcTagsDiscoveryPrivate::EReadFromClass1Tag){
00194 emit iPublicAPI->showMessage("Read from Type1 tag "
00195 "completed.\n");
00196 ShowTagInformation();
00197 }
00198 else if(iTagOperation ==
00199 NfcTagsDiscoveryPrivate::EWriteToClass1Tag){
00200 emit iPublicAPI->showMessage("Write to Type1 tag "
00201 "completed.\n");
00202 ReadFromClass1Tag();
00203 }
00204 else if(iTagOperation ==
00205 NfcTagsDiscoveryPrivate::EReadFromClass2Tag){
00206 emit iPublicAPI->showMessage("Read from Type2 tag "
00207 "completed.\n");
00208 ShowTagInformation();
00209 }
00210 else if(iTagOperation ==
00211 NfcTagsDiscoveryPrivate::EWriteToClass2Tag){
00212 emit iPublicAPI->showMessage("Write to Type2 tag "
00213 "completed.\n");
00214 ReadFromClass2Tag();
00215 }
00216 else if(iTagOperation ==
00217 NfcTagsDiscoveryPrivate::EUpdateClass3Tag){
00218 emit iPublicAPI->showMessage("Update Type3 tag "
00219 "completed.\n");
00220 CheckClass3Tag();
00221 }
00222 else if(iTagOperation ==
00223 NfcTagsDiscoveryPrivate::ECheckClass3Tag){
00224 emit iPublicAPI->showMessage("Check Type3 tag "
00225 "completed.\n");
00226 ShowTagInformation();
00227 }
00228 else if(iTagOperation ==
00229 NfcTagsDiscoveryPrivate::EWriteToMifareTag){
00230 emit iPublicAPI->showMessage("Write to mifare "
00231 "tag completed.\n");
00232 ReadFromMifareTag();
00233 }
00234 else if(iTagOperation ==
00235 NfcTagsDiscoveryPrivate::EReadFromMifareTag){
00236 emit iPublicAPI->showMessage("Read from mifare tag "
00237 "completed.\n");
00238 ShowTagInformation();
00239 }
00240 }
00241
00242
00243 NfcTagsDiscoveryPrivate::NfcTagsDiscoveryPrivate(
00244 NfcTagsDiscovery* aPublicAPI): iPublicAPI(aPublicAPI)
00245 {
00246
00247 }
00248
00249
00250 void NfcTagsDiscoveryPrivate::ConstructL(NfcTagsDiscovery
00251 *aPublicAPI)
00252 {
00253 iWriteWait = CActiveWait::NewL(this);
00254 iReadWait = CActiveWait::NewL(this);
00255 iWait = new (ELeave) CActiveSchedulerWait();
00256
00257
00258 iServer.Open();
00259
00260
00261 TRAPD(err,iNfcTagDiscovery = CNfcTagDiscovery::NewL( iServer ));
00262
00263
00264 TRAP(err,iNdefDiscovery = CNdefDiscovery::NewL(iServer ));
00265
00266
00267 TRAP(err,iNfcType1Connection =
00268 CNfcType1Connection::NewL( iServer ));
00269 TRAP(err,iNfcType2Connection =
00270 CNfcType2Connection::NewL( iServer ));
00271 TRAP(err,iNfcType3Connection =
00272 CNfcType3Connection::NewL( iServer ));
00273 TRAP(err,iMifareClassicConnection =
00274 CMifareClassicConnection::NewL( iServer ));
00275 TRAP(err,iIso14443Connection =
00276 CIso14443Connection::NewL( iServer ));
00277
00278
00279 iWriteBuffer.ReAlloc(KSendText().Size());
00280 iWriteBuffer = KSendText;
00281 iReadBuffer.ReAlloc(KSendText().Size());
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291 void NfcTagsDiscoveryPrivate::TagDetected(MNfcTag* aTag)
00292 {
00293 emit iPublicAPI->showMessage("\nA new tag is detected\n\n");
00294
00295
00296 if ( aTag->HasConnectionMode(
00297 TNfcConnectionInfo::ENfc14443P4 ) )
00298 {
00299
00300 emit iPublicAPI->showMessage("Iso14443 tag.\n");
00301 TInt error = aTag->OpenConnection( *iIso14443Connection );
00302 if(error == KErrNone)
00303 {
00304 emit iPublicAPI->showMessage("Connection mode is "
00305 "successfully opened.\n");
00306 ExchangeDataWithIso14443Tag();
00307 }
00308 }
00309 else if ( aTag->HasConnectionMode(
00310 TNfcConnectionInfo::ENfcType1 ) )
00311 {
00312
00313 emit iPublicAPI->showMessage("Type1 tag.\n");
00314 TInt error = aTag->
00315 OpenConnection( *iNfcType1Connection );
00316 if(error == KErrNone)
00317 {
00318 emit iPublicAPI->showMessage("Connection mode"
00319 " is successfully opened.\n");
00320 WriteToClass1Tag();
00321 }
00322 }
00323 else if ( aTag->HasConnectionMode(
00324 TNfcConnectionInfo::ENfcType2 ) )
00325 {
00326
00327 emit iPublicAPI->showMessage("Type2 tag.\n");
00328 TInt error = aTag->
00329 OpenConnection( *iNfcType2Connection );
00330 if(error == KErrNone)
00331 {
00332 emit iPublicAPI->showMessage("Connection mode "
00333 "is successfully opened.\n");
00334 WriteToClass2Tag();
00335 }
00336 }
00337 else if ( aTag->HasConnectionMode( TNfcConnectionInfo::ENfcType3 ) )
00338 {
00339
00340 emit iPublicAPI->showMessage("Type3 tag.\n");
00341 TInt error = aTag->
00342 OpenConnection( *iNfcType3Connection );
00343 if(error == KErrNone)
00344 {
00345 emit iPublicAPI->showMessage("Connection mode "
00346 "is successfully opened.\n");
00347 UpdateClass3Tag();
00348 }
00349 }
00350 else if ( aTag->
00351 HasConnectionMode( TNfcConnectionInfo::ENfcMifareStd ) )
00352 {
00353
00354 emit iPublicAPI->showMessage("Mifare classic tag.\n");
00355 TInt error = aTag->
00356 OpenConnection( *iMifareClassicConnection );
00357 if(error == KErrNone)
00358 {
00359 emit iPublicAPI->showMessage("Connection mode "
00360 "is successfully opened.\n");
00361 WriteToMifareTag();
00362 }
00363 }
00364 delete aTag;
00365 }
00366
00367
00368 void NfcTagsDiscoveryPrivate::TagLost()
00369 {
00370 if(iIso14443Connection->IsActivated())
00371 {
00372 iIso14443Connection->Deactivate();
00373 }
00374 emit iPublicAPI->clearText();
00375 emit iPublicAPI->showMessage("Tag is lost.\n");
00376 emit iPublicAPI->showMessage("Detected nfc tags information is \n"
00377 "displayed here.");
00378 }
00379
00380
00381 void NfcTagsDiscoveryPrivate::MessageDetected( CNdefMessage* aMessage )
00382 {
00383 if ( aMessage )
00384 {
00385 emit iPublicAPI->showMessage("NDEF Message received is :\n");
00386 TBuf8<200> buf(0);
00387 aMessage->ExportRawDataL(buf, 0);
00388 HBufC16* recData = HBufC16::NewL(80);
00389 recData = CnvUtfConverter::ConvertToUnicodeFromUtf8L(buf);
00390 emit iPublicAPI->showMessage(QString::fromUtf16(recData->Ptr(),recData->Length()));
00391 delete recData;
00392 delete aMessage;
00393 }
00394 }
00395
00396
00397 void NfcTagsDiscoveryPrivate::ExchangeDataWithIso14443Tag()
00398 {
00399 iTagOperation =
00400 NfcTagsDiscoveryPrivate::EExchangeDataWithIso14443Tag;
00401 iIso14443Connection->
00402 ExchangeData(iWriteWait->iStatus, iWriteBuffer, iReadBuffer);
00403 if(!iWriteWait->IsActive()){
00404 emit iPublicAPI->showMessage("Exchange data started\n");
00405 iWriteWait->SetActive();
00406 }
00407 }
00408
00409
00410 void NfcTagsDiscoveryPrivate::WriteToClass1Tag()
00411 {
00412 iTagOperation = NfcTagsDiscoveryPrivate::EWriteToClass1Tag;
00413
00414
00415 TInt block = 1;
00416
00417
00418 TInt byte = 6;
00419 TNfcType1Address address(byte, block);
00420
00421
00422 iNfcType1Connection->
00423 Write( iWriteWait->iStatus, iWriteBuffer, address );
00424 if(!iWriteWait->IsActive()){
00425 emit iPublicAPI->showMessage("Write to class1 tag "
00426 "started.\n");
00427 iWriteWait->SetActive();
00428 }
00429 }
00430
00431
00432 void NfcTagsDiscoveryPrivate::ReadFromClass1Tag()
00433 {
00434 iTagOperation = NfcTagsDiscoveryPrivate::EReadFromClass1Tag;
00435
00436
00437 TInt block = 1;
00438
00439
00440 TInt byte = 6;
00441 TNfcType1Address address( byte, block );
00442
00443
00444 iNfcType1Connection->Read( iReadWait->iStatus, iReadBuffer,KSendText().Size(), address );
00445 if(!iReadWait->IsActive()){
00446 emit iPublicAPI->showMessage("Read from class1 tag started\n");
00447 iReadWait->SetActive();
00448 }
00449 }
00450
00451
00452 void NfcTagsDiscoveryPrivate::WriteToClass2Tag()
00453 {
00454 iTagOperation = NfcTagsDiscoveryPrivate::EWriteToClass2Tag;
00455 TInt block = 4;
00456 iNfcType2Connection->Write( iWriteWait->iStatus, iWriteBuffer, block );
00457 if(!iWriteWait->IsActive()){
00458 emit iPublicAPI->showMessage("Write to class2 tag started.\n");
00459 iWriteWait->SetActive();
00460 }
00461 }
00462
00463
00464 void NfcTagsDiscoveryPrivate::ReadFromClass2Tag()
00465 {
00466 iTagOperation = NfcTagsDiscoveryPrivate::EReadFromClass2Tag;
00467 TInt block = 4;
00468 TInt byte = 0;
00469 TNfcType2Address address( byte, block );
00470 iNfcType2Connection->
00471 Read( iReadWait->iStatus,
00472 iReadBuffer, KSendText().Length(), address);
00473 if(!iReadWait->IsActive()){
00474 emit iPublicAPI->showMessage("Read from class2 "
00475 "tag started\n");
00476 iReadWait->SetActive();
00477 }
00478 }
00479
00480
00481 void NfcTagsDiscoveryPrivate::UpdateClass3Tag()
00482 {
00483 iTagOperation = NfcTagsDiscoveryPrivate::EUpdateClass3Tag;
00484 TInt byte = 1;
00485 TInt block = 1;
00486 TInt service = 11;
00487 TNfcType3Address address( byte, block, service );
00488 iNfcType3Connection->
00489 Update( iWriteWait->iStatus, iWriteBuffer, address );
00490 if(!iWriteWait->IsActive()){
00491 emit iPublicAPI->showMessage("Update class3 tag "
00492 "started.\n");
00493 iWriteWait->SetActive();
00494 }
00495 }
00496
00497
00498 void NfcTagsDiscoveryPrivate::CheckClass3Tag()
00499 {
00500 iTagOperation = NfcTagsDiscoveryPrivate::ECheckClass3Tag;
00501 TInt byte = 1;
00502 TInt block = 1;
00503 TInt service = 11;
00504 TNfcType3Address address( byte, block, service );
00505 iNfcType3Connection->
00506 Check( iReadWait->iStatus, iReadBuffer,
00507 KSendText().Size(), address );
00508 if(!iReadWait->IsActive()){
00509 emit iPublicAPI->showMessage("Check class3 tag "
00510 "started.\n");
00511 iReadWait->SetActive();
00512 }
00513 }
00514
00515
00516 void NfcTagsDiscoveryPrivate::WriteToMifareTag()
00517 {
00518 iTagOperation = NfcTagsDiscoveryPrivate::EWriteToMifareTag;
00519
00520 RBuf8 keyA;
00521 CleanupClosePushL( keyA );
00522 keyA.CreateL(100);
00523 RBuf8 keyB;
00524 keyB.CreateL(100);
00525 CleanupClosePushL( keyB );
00526
00527 TInt sect = 1;
00528 if(iMifareClassicDataSector)
00529 {
00530 delete iMifareClassicDataSector;
00531 iMifareClassicDataSector = NULL;
00532 }
00533 iMifareClassicDataSector = CMifareClassicSector::NewL(
00534 CMifareClassicSector::E1KSector);
00535
00536 iMifareClassicConnection->WriteSector(
00537 iWriteWait->iStatus,
00538 *iMifareClassicDataSector,
00539 sect);
00540
00541 iWriteWait->SetActive();
00542
00543 CleanupStack::PopAndDestroy();
00544 CleanupStack::PopAndDestroy();
00545 }
00546
00547
00548 void NfcTagsDiscoveryPrivate::ReadFromMifareTag()
00549 {
00550 iTagOperation = NfcTagsDiscoveryPrivate::EReadFromMifareTag;
00551
00552 RBuf8 keyA;
00553 CleanupClosePushL( keyA );
00554 keyA.CreateL(100);
00555 RBuf8 keyB;
00556 keyB.CreateL(100);
00557 CleanupClosePushL( keyB );
00558
00559 TInt sect = 1;
00560 if(iMifareClassicDataSector)
00561 {
00562 delete iMifareClassicDataSector;
00563 iMifareClassicDataSector = NULL;
00564 }
00565 iMifareClassicDataSector = CMifareClassicSector::NewL(
00566 CMifareClassicSector::E1KSector);
00567
00568 iMifareClassicConnection->ReadSector(
00569 iReadWait->iStatus,
00570 *iMifareClassicDataSector,
00571 sect,
00572 keyA,
00573 keyB);
00574
00575 iReadWait->SetActive();
00576
00577 CleanupStack::PopAndDestroy();
00578 CleanupStack::PopAndDestroy();
00579
00580 }
00581
00582
00583 void NfcTagsDiscoveryPrivate::ShowTagInformation()
00584 {
00585
00586 if(iTagOperation == NfcTagsDiscoveryPrivate::EReadFromMifareTag)
00587 {
00588 HBufC8* recData8 = iMifareClassicDataSector->ExportDataLC();
00589 emit iPublicAPI->showMessage("Information read from "
00590 "the tag:\n");
00591 emit iPublicAPI->showMessage(QString::fromUtf16(
00592 CnvUtfConverter::ConvertToUnicodeFromUtf8L(
00593 *recData8)->Ptr(),
00594 CnvUtfConverter::ConvertToUnicodeFromUtf8L(
00595 *recData8)->Length()));
00596 CleanupStack::PopAndDestroy();
00597 }
00598 else{
00599 emit iPublicAPI->showMessage("Information read from "
00600 "the tag:\n");
00601 emit iPublicAPI->showMessage(
00602 QString::fromUtf16(
00603 CnvUtfConverter::ConvertToUnicodeFromUtf8L(
00604 iReadBuffer)->Ptr(),
00605 CnvUtfConverter::ConvertToUnicodeFromUtf8L(
00606 iReadBuffer)->Length()));
00607 }
00608 }