javaextensions/satsa/apdu/src.s60/cstsapduexchanger.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include "cstsapduexchanger.h"
       
    21 #include "stsapduconstants.h"
       
    22 #include "cstscmdapdu.h"
       
    23 #include "cstsrespapdu.h"
       
    24 #include "cstscardoperationsfacade.h"
       
    25 #include "mstsresphandler.h"
       
    26 #include "cstsgetresponse.h"
       
    27 #include "fs_methodcall.h"
       
    28 #include "logger.h"
       
    29 
       
    30 namespace java
       
    31 {
       
    32 namespace satsa
       
    33 {
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CSTSApduExchanger::CSTSApduExchanger
       
    39 // C++ default constructor can NOT contain any code, that
       
    40 // might leave.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 
       
    44 CSTSApduExchanger::CSTSApduExchanger(CSTSCardOperationsFacade* aCOFacade) :
       
    45         CActive(EPriorityNormal)
       
    46 {
       
    47     CActiveScheduler::Add(this);
       
    48     iCOFacade = aCOFacade;
       
    49 }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CSTSApduExchanger::ConstructL
       
    53 // Symbian 2nd phase constructor can leave.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void CSTSApduExchanger::ConstructL()
       
    57 {
       
    58     iWait = new(ELeave) CActiveSchedulerWait();
       
    59     iRespBuf = HBufC8::NewL(KSTSResponseApduMaxLength);
       
    60     iRespApdu = CSTSRespApdu::NewL(KSTSResponseApduMaxLength);
       
    61     iGetResponseApdu = CSTSGetResponse::NewL(0x00, //empty at this point
       
    62                        CSTSApdu::ESTSUICC);
       
    63 }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CSTSApduExchanger::NewL
       
    67 // Two-phased constructor.
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CSTSApduExchanger* CSTSApduExchanger::NewL(JNIEnv* aJni, jobject aPeer,
       
    71         CSTSCardOperationsFacade* aCOFacade)
       
    72 {
       
    73     CSTSApduExchanger* self = new(ELeave) CSTSApduExchanger(aCOFacade);
       
    74     self->mJni = aJni;
       
    75     self->mPeer = aPeer;
       
    76 
       
    77     CleanupStack::PushL(self);
       
    78     self->ConstructL();
       
    79     CleanupStack::Pop(self);
       
    80     return self;
       
    81 }
       
    82 
       
    83 // Destructor
       
    84 CSTSApduExchanger::~CSTSApduExchanger()
       
    85 {
       
    86     Cancel();
       
    87     delete iRespBuf;
       
    88     delete iWait;
       
    89     delete iRespApdu;
       
    90     delete iGetResponseApdu;
       
    91 }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CSTSApduExchanger::DoExchange
       
    95 // Set channel, apdu and empties response buffer. Exchanges apdu message
       
    96 // asynchronously
       
    97 // (other items were commented in a header).
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CSTSApduExchanger::DoExchange(CSTSCmdApdu& aCmdApdu)
       
   101 {
       
   102     LOG(ESATSA, EInfo, "+ CSTSApduExchanger::DoExchange");
       
   103     aCmdApdu.SetChannel(iChannel);
       
   104 
       
   105     iCmdApdu = &aCmdApdu;
       
   106     //response buffer is "reusable" so empty length before next use
       
   107     iRespBuf->Des().SetLength(0);
       
   108     //send apdu to card
       
   109     LOG(ESATSA, EInfo, "CSTSApduExchanger::DoExchange: send apdu to card");
       
   110     iCOFacade->SendAPDUReq(iStatus, (TUint8) iReader, &aCmdApdu.ApduBytes());
       
   111     SetActive();
       
   112 }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CSTSApduExchanger::ExchangeApduL
       
   116 // Exchanges apdu message synchronously
       
   117 // (other items were commented in a header).
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 TInt CSTSApduExchanger::ExchangeApdu(CSTSCmdApdu& aCmdApdu,
       
   121                                      CSTSRespApdu& aRespApdu)
       
   122 {
       
   123     DoExchange(aCmdApdu);
       
   124     if (!iWait->IsStarted())
       
   125     {
       
   126         iWait->Start();
       
   127     }
       
   128     //save response
       
   129     aRespApdu.ResponseBytes() = iRespBuf->Des();
       
   130     return iStatus.Int();
       
   131 
       
   132 }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CSTSApduExchanger::ExchangeApdu
       
   136 // Exchanges apdu message asynchronously
       
   137 // Sets iClientStatus information for RunL purposes.
       
   138 // (other items were commented in a header).
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 void CSTSApduExchanger::ExchangeApduAsync(CSTSCmdApdu& aCmdApdu,
       
   142         MSTSRespHandler* aRespHandler)
       
   143 {
       
   144     LOG(ESATSA, EInfo, "+ CSTSApduExchanger::ExchangeApduAsync 1");
       
   145 
       
   146     iRespHandler = aRespHandler;
       
   147     LOG(ESATSA, EInfo, "Calling DoExchnage()");
       
   148     DoExchange(aCmdApdu);
       
   149 }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CSTSApduExchanger::ExchangeApdu
       
   153 // Exchanges apdu message asynchronously
       
   154 // Response must be get separately after asynchronous call is returned
       
   155 // When using this method for sending apdu and GetResponseBytes method for
       
   156 // getting response bytes there is no checked is resend needed or get response
       
   157 // needed.
       
   158 // (other items were commented in a header).
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CSTSApduExchanger::ExchangeApduAsync(TRequestStatus& aStatus,
       
   162         CSTSCmdApdu& aCmdApdu)
       
   163 {
       
   164     aCmdApdu.SetChannel(iChannel);
       
   165     // send apdu to card
       
   166     iCOFacade->SendAPDUReq(aStatus, (TUint8) iReader, &aCmdApdu.ApduBytes());
       
   167 }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CSTSApduExchanger::GetResponseBytes
       
   171 
       
   172 // (other items were commented in a header).
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 void CSTSApduExchanger::GetResponseBytes(CSTSRespApdu& aRespApdu)
       
   176 {
       
   177     iCOFacade->GetResponseBytes(&aRespApdu.ResponseBytes());
       
   178 }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CSTSApduExchanger::SetChannel
       
   182 //
       
   183 // (other items were commented in a header).
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void CSTSApduExchanger::SetChannel(TInt aChannel)
       
   187 {
       
   188     LOG(ESATSA, EInfo, "+ CSTSApduExchanger::SetChannel");
       
   189     iChannel = aChannel;
       
   190 }
       
   191 
       
   192 // -----------------------------------------------------------------------------
       
   193 // CSTSApduExchanger::SetReader
       
   194 //
       
   195 // (other items were commented in a header).
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CSTSApduExchanger::SetReader(TInt aReader)
       
   199 {
       
   200     iReader = aReader;
       
   201 }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CSTSApduExchanger::SetConnectionType
       
   205 // Sets connection target to response adpu
       
   206 // (other items were commented in a header).
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void CSTSApduExchanger::SetConnectionType(TBool aSatConnection)
       
   210 {
       
   211     if (aSatConnection)
       
   212     {
       
   213         iRespApdu->SetConnectionTarget(CSTSRespApdu::ESTSUSAT);
       
   214     }
       
   215     else
       
   216     {
       
   217         iRespApdu->SetConnectionTarget(CSTSRespApdu::ESTSAID);
       
   218     }
       
   219 }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CSTSApduExchanger::CancelExchange
       
   223 // Can be used, if J2ME application calls CLOSE method in another thread for
       
   224 // closing connection and in other thread is pending apdu send at the same time.
       
   225 // (other items were commented in a header).
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 void CSTSApduExchanger::CancelExchange()
       
   229 {
       
   230     Cancel();
       
   231 }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CSTSApduExchanger::RunL
       
   235 // If apdu send was called synchronously, CActiveSchedulerWait is used to continue
       
   236 // in ExchangeApduL method. If apdu send was called synchronously, we notify
       
   237 // and deliver response to java side
       
   238 // (other items were commented in a header).
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CSTSApduExchanger::RunL()
       
   242 {
       
   243     LOG(ESATSA, EInfo, "+ CSTSApduExchanger::RunL");
       
   244     if (iStatus == KErrNone)
       
   245     {
       
   246         LOG(ESATSA, EInfo, "CSTSApduExchanger::RunL:Status is KErrNone.");
       
   247         iRespApdu->ResponseBytes().SetLength(0);
       
   248         iCOFacade->GetResponseBytes(&iRespApdu->ResponseBytes());
       
   249 
       
   250         //append new response after old response
       
   251         iRespBuf->Des().Append(iRespApdu->ResponseBytes());
       
   252 
       
   253         TUint8 newRespLength = 0x00;
       
   254         if (iRespApdu->GetResponseNeeded(newRespLength))
       
   255         {
       
   256             LOG(ESATSA, EInfo, "CSTSApduExchanger::RunL:GetResponseNeeded");
       
   257             //remove sw1 and sw2, because more data will be appended
       
   258             iRespBuf->Des().SetLength(iRespBuf->Length() - 2);
       
   259             //we got new length from earlier called method
       
   260             iGetResponseApdu->SetExpectedResponseLength(newRespLength);
       
   261             iGetResponseApdu->SetChannel(iChannel);
       
   262 
       
   263             //send get response apdu to card
       
   264             LOG(ESATSA, EInfo, "send get response apdu to card");
       
   265             iCOFacade->SendAPDUReq(iStatus, (TUint8) iReader,
       
   266                                    &iGetResponseApdu->ApduBytes());
       
   267             SetActive();
       
   268         }
       
   269         else if (iRespApdu->ResendNeeded(newRespLength))
       
   270         {
       
   271             LOG(ESATSA, EInfo, "CSTSApduExchanger::RunL: Resend needed ");
       
   272             //Resend apdu with correct length
       
   273             iCmdApdu->SetLe(newRespLength);
       
   274             iRespBuf->Des().SetLength(0); //empty old response
       
   275 
       
   276             LOG(ESATSA, EInfo, "send same apdu to card again (resend)");
       
   277             //send same apdu to card again (resend)
       
   278             iCOFacade->SendAPDUReq(iStatus, (TUint8) iReader,
       
   279                                    &iCmdApdu->ApduBytes());
       
   280             SetActive();
       
   281         }
       
   282         else
       
   283         {
       
   284             //if synchronous ExchangeApdu was called
       
   285             if (iWait->IsStarted())
       
   286             {
       
   287                 LOG(ESATSA, EInfo, "Synch Exchng called!");
       
   288                 iWait->AsyncStop();
       
   289             }
       
   290             //if asynchronous ExchangeApduAsync was called
       
   291             else
       
   292             {
       
   293                 LOG(ESATSA, EInfo, "calling response handler to complete operation");
       
   294                 iRespHandler->OperationComplete(mJni, mPeer, iRespBuf, KErrNone);
       
   295             }
       
   296         }
       
   297     }
       
   298     else //if the iStatus is not KErrNone
       
   299     {
       
   300         LOG(ESATSA, EInfo, "CSTSApduExchanger::RunL:iStatus is not KErrNone");
       
   301         //if synchronous ExchangeApdu was called
       
   302         if (iWait->IsStarted())
       
   303         {
       
   304             iWait->AsyncStop();
       
   305         }
       
   306         else
       
   307         {
       
   308             LOG(ESATSA, EInfo, "calling response handler to complete operation");
       
   309             iRespHandler->OperationComplete(mJni, mPeer, NULL, KSTSErrIO);
       
   310         }
       
   311     }
       
   312 }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CSTSApduExchanger::DoCancel
       
   316 //
       
   317 // (other items were commented in a header).
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 void CSTSApduExchanger::DoCancel()
       
   321 {
       
   322 
       
   323     if (iWait->IsStarted())
       
   324     {
       
   325         //synchronous call can not be cancelled
       
   326 
       
   327     }
       
   328     else
       
   329     {
       
   330         iCOFacade->CancelAPDUReq();
       
   331         iRespHandler->OperationComplete(mJni, mPeer, NULL, KSTSErrInterruptedIO
       
   332                                         + KSTSErrIIConnClosedDuring);
       
   333     }
       
   334 }
       
   335 
       
   336 } // namespace satsa
       
   337 } // namespace java
       
   338 //  End of File
       
   339