javaextensions/satsa/apdu/src.s60/apduconnectionimpl.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     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 #include "s60commonutils.h"
       
    19 #include "javajniutils.h"
       
    20 #include "com_nokia_mj_impl_satsa_APDUConnectionImpl.h"
       
    21 #include "cstsconnection.h"
       
    22 #include "cstsresphandler.h"
       
    23 #include "cstsmidletinfo.h"
       
    24 #include "javaapduconnection.h"
       
    25 #include "logger.h"
       
    26 #include "javacommonutils.h"
       
    27 
       
    28 // CONSTANTS
       
    29 
       
    30 const TInt KReturnValueArrayLength = 2;
       
    31 const TInt KHandleIndex = 0;
       
    32 const TInt KConnTypeIndex = 1;
       
    33 
       
    34 using namespace java::satsa;
       
    35 
       
    36 /**
       
    37  * JNI function.
       
    38  */
       
    39 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_satsa_APDUConnectionImpl__1doPinMethod(
       
    40     JNIEnv* aJni, jclass aPeer,jint aHandle,
       
    41     jint aPinMethodType, jint aPinID)
       
    42 {
       
    43     LOG(ESATSA, EInfo, "+ Entering doPinMethod JNI function");
       
    44     JavaApduConnection* apduConn = reinterpret_cast<JavaApduConnection *>(aHandle);
       
    45     TInt err = apduConn->DoPinMethod(*aJni, aPeer, aHandle, aPinMethodType, aPinID);
       
    46     LOG(ESATSA, EInfo, "Returning from doPinMethod JNI function");
       
    47     //return error code to caller
       
    48     return err;
       
    49 }
       
    50 
       
    51 /**
       
    52  * JNI function.
       
    53  */
       
    54 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_satsa_APDUConnectionImpl__1exchangeAPDU(
       
    55     JNIEnv *aJni, jclass aPeer,jint aHandle,
       
    56     jbyteArray aCommandAPDU)
       
    57 {
       
    58     LOG(ESATSA, EInfo, "+ Entering exchangeAPDU JNI function");
       
    59 
       
    60     JavaApduConnection* apduConn = reinterpret_cast<JavaApduConnection *>(aHandle);
       
    61     TInt err = 0;
       
    62     err = apduConn->ExchangeAPDU(*aJni, aPeer, aHandle, aCommandAPDU);
       
    63 
       
    64     LOG(ESATSA, EInfo, "-- Returning from exchnageAPDU JNI function");
       
    65     //return error code to caller
       
    66     return err;
       
    67 }
       
    68 
       
    69 /**
       
    70  * JNI function.
       
    71  */
       
    72 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_satsa_APDUConnectionImpl__1unblockPin(
       
    73     JNIEnv* aJni, jclass aPeer,jint aHandle,
       
    74     jint aBlockedPinID, jint aUnblockingPinID)
       
    75 {
       
    76     LOG(ESATSA, EInfo, "+ Entering UnblockPin JNI function");
       
    77 
       
    78     JavaApduConnection* apduConn = reinterpret_cast<JavaApduConnection *>(aHandle);
       
    79     TInt err = apduConn->UnblockPin(*aJni, aPeer, aHandle, aBlockedPinID, aUnblockingPinID);
       
    80     LOG(ESATSA, EInfo, "-- Returning from unblockPin JNI function");
       
    81     //return error code to caller
       
    82     return err;
       
    83 }
       
    84 
       
    85 /**
       
    86  * JNI function.
       
    87  */
       
    88 JNIEXPORT jbyteArray JNICALL Java_com_nokia_mj_impl_satsa_APDUConnectionImpl__1getATR(
       
    89     JNIEnv *aJni, jclass /*aPeer*/, jint aHandle,
       
    90     jintArray aErrArray)
       
    91 {
       
    92     LOG(ESATSA,  EInfo, "GetATR JNI function");
       
    93 
       
    94     JavaApduConnection* apduConn = reinterpret_cast<JavaApduConnection *>(aHandle);
       
    95     TInt err = 0;
       
    96     //err = apduConn->GetATR(*aJni, aPeer, aHandle, aCommandAPDU);
       
    97     std::wstring atrData;
       
    98     TRAP(err,
       
    99          atrData = apduConn->GetATR());
       
   100 
       
   101     // return null if resp is empty (card has been removed or the connection
       
   102     // has been closed.)
       
   103 
       
   104     jbyteArray responseAPDU = NULL;
       
   105 
       
   106     if (atrData.length()> 0)
       
   107     {
       
   108         const char* utf8 = java::util::JavaCommonUtils::wstringToUtf8(atrData);
       
   109         int len = strlen(utf8);
       
   110         responseAPDU = aJni->NewByteArray(len);
       
   111         if (responseAPDU != NULL)
       
   112         {
       
   113             aJni->SetByteArrayRegion(responseAPDU, 0, len,
       
   114                                      (jbyte *)utf8);
       
   115         }
       
   116         else
       
   117         {
       
   118             ELOG(ESATSA,"__1getATR: responseAPDU is NULL");
       
   119             err = KErrNoMemory;
       
   120         }
       
   121         delete[] utf8;
       
   122     }
       
   123 
       
   124     //error array contains only one error value
       
   125     jint javaError[ 1 ] =
       
   126         {   err};
       
   127     aJni->SetIntArrayRegion(aErrArray, 0, 1, javaError);
       
   128 
       
   129     ILOG1(ESATSA, "APDUConnectionImpl::XXX__1getATR::GetATR returned: %d", err);
       
   130     return responseAPDU;
       
   131 
       
   132 }
       
   133 
       
   134 /**
       
   135  * JNI function.
       
   136  */
       
   137 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_satsa_APDUConnectionImpl__1create
       
   138 (JNIEnv* aJni, jobject aPeer,
       
   139  jstring aURI, jstring aName, jstring aVendor,
       
   140  jstring aVersion,jstring aDomain,jstring aRootCertHash,
       
   141  jstring aSigningCertHash,
       
   142  jintArray aTypeArray)
       
   143 {
       
   144     LOG(ESATSA, EInfo, "+ Entering create JNI function");
       
   145 
       
   146     //creates array for return values, because ExecuteTrap will accept only
       
   147     //nine parameters at maximum. This array will consist of handle and
       
   148     //connectionType
       
   149     TInt returnValueArray[ KReturnValueArrayLength ] =
       
   150         {   0};
       
   151 
       
   152     //get handle to java operationComplete java method
       
   153     jmethodID handleEventMethod =
       
   154         aJni->GetMethodID(aJni->GetObjectClass(aPeer),
       
   155                           "operationComplete",
       
   156                           "(II[B)V");
       
   157 
       
   158     jobject connectionObject = aJni->NewWeakGlobalRef(aPeer);
       
   159 
       
   160     JavaApduConnection *apduConn = NULL;
       
   161     try
       
   162     {
       
   163         apduConn = JavaApduConnection::getInstance();
       
   164     }
       
   165     catch (int err)
       
   166     {
       
   167         ELOG1(ESATSA,"Caught an Exception --Value is:%d",err);
       
   168         delete apduConn;
       
   169         apduConn=NULL;
       
   170     }
       
   171 
       
   172     TInt connectionHandle = reinterpret_cast<TInt>(apduConn);
       
   173     returnValueArray[ KHandleIndex ] = connectionHandle;
       
   174 
       
   175     TInt err = apduConn->Create(*aJni, aURI, aName, aVendor,
       
   176                                 aVersion, aDomain, aRootCertHash,
       
   177                                 aSigningCertHash, returnValueArray,
       
   178                                 connectionObject,handleEventMethod);
       
   179     if (err != KErrNone)
       
   180     {
       
   181         // CreateConnectionL leaved.
       
   182         // Set error code to handle which is returned to Java
       
   183         returnValueArray[ KHandleIndex ] = err;
       
   184     }
       
   185 
       
   186     //set type of connection to the "returned" type array
       
   187     jint javaConnType[ 1 ] =
       
   188         {   returnValueArray[ KConnTypeIndex ]};
       
   189     aJni->SetIntArrayRegion(aTypeArray, 0, 1, javaConnType);
       
   190 
       
   191     ILOG1(ESATSA, "STS::APDUConnectionImpl.cpp::create return value %d", err);
       
   192     // return handle to new connection object or error code
       
   193     return returnValueArray[ KHandleIndex ];
       
   194 
       
   195 }
       
   196 
       
   197 /**
       
   198  * JNI function.
       
   199  */
       
   200 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_satsa_APDUConnectionImpl__1open(
       
   201     JNIEnv* aJni, jclass aPeer, jint aHandle)
       
   202 {
       
   203     LOG(ESATSA, EInfo, "+ Entering Open JNI function");
       
   204     JavaApduConnection* apduConn = reinterpret_cast<JavaApduConnection *>(aHandle);
       
   205     TInt err = apduConn->Open(*aJni, aPeer, aHandle);
       
   206     LOG(ESATSA, EInfo, "-- Returning from Open JNI function");
       
   207     return err;
       
   208 
       
   209 }
       
   210 
       
   211 /**
       
   212  * JNI function.
       
   213  */
       
   214 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_satsa_APDUConnectionImpl__1close(
       
   215     JNIEnv* aJni, jclass aPeer, jint aHandle)
       
   216 {
       
   217     LOG(ESATSA, EInfo, "+ Entering close JNI function");
       
   218     JavaApduConnection* apduConn = reinterpret_cast<JavaApduConnection *>(aHandle);
       
   219     TInt err = apduConn->Close(*aJni, aPeer, aHandle);
       
   220     LOG(ESATSA, EInfo, "-- Returning from close JNI function");
       
   221     return err;
       
   222 
       
   223 }