1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17/* 18 * Communicate with secure elements that are attached to the NFC 19 * controller. 20 */ 21#pragma once 22#include "SyncEvent.h" 23#include "DataQueue.h" 24#include "NfcJniUtil.h" 25#include "RouteDataSet.h" 26extern "C" 27{ 28 #include "nfa_ee_api.h" 29 #include "nfa_hci_api.h" 30 #include "nfa_hci_defs.h" 31 #include "nfa_ce_api.h" 32} 33 34 35class SecureElement 36{ 37public: 38 tNFA_HANDLE mActiveEeHandle; 39 40 41 /******************************************************************************* 42 ** 43 ** Function: getInstance 44 ** 45 ** Description: Get the SecureElement singleton object. 46 ** 47 ** Returns: SecureElement object. 48 ** 49 *******************************************************************************/ 50 static SecureElement& getInstance (); 51 52 53 /******************************************************************************* 54 ** 55 ** Function: initialize 56 ** 57 ** Description: Initialize all member variables. 58 ** native: Native data. 59 ** 60 ** Returns: True if ok. 61 ** 62 *******************************************************************************/ 63 bool initialize (nfc_jni_native_data* native); 64 65 66 /******************************************************************************* 67 ** 68 ** Function: finalize 69 ** 70 ** Description: Release all resources. 71 ** 72 ** Returns: None 73 ** 74 *******************************************************************************/ 75 void finalize (); 76 77 78 /******************************************************************************* 79 ** 80 ** Function: getListOfEeHandles 81 ** 82 ** Description: Get the list of handles of all execution environments. 83 ** e: Java Virtual Machine. 84 ** 85 ** Returns: List of handles of all execution environments. 86 ** 87 *******************************************************************************/ 88 jintArray getListOfEeHandles (JNIEnv* e); 89 90 91 /******************************************************************************* 92 ** 93 ** Function: activate 94 ** 95 ** Description: Turn on the secure element. 96 ** seID: ID of secure element. 97 ** 98 ** Returns: True if ok. 99 ** 100 *******************************************************************************/ 101 bool activate (jint seID); 102 103 104 /******************************************************************************* 105 ** 106 ** Function: deactivate 107 ** 108 ** Description: Turn off the secure element. 109 ** seID: ID of secure element. 110 ** 111 ** Returns: True if ok. 112 ** 113 *******************************************************************************/ 114 bool deactivate (jint seID); 115 116 117 /******************************************************************************* 118 ** 119 ** Function: connectEE 120 ** 121 ** Description: Connect to the execution environment. 122 ** 123 ** Returns: True if ok. 124 ** 125 *******************************************************************************/ 126 bool connectEE (); 127 128 129 /******************************************************************************* 130 ** 131 ** Function: disconnectEE 132 ** 133 ** Description: Disconnect from the execution environment. 134 ** seID: ID of secure element. 135 ** 136 ** Returns: True if ok. 137 ** 138 *******************************************************************************/ 139 bool disconnectEE (jint seID); 140 141 142 /******************************************************************************* 143 ** 144 ** Function: transceive 145 ** 146 ** Description: Send data to the secure element; read it's response. 147 ** xmitBuffer: Data to transmit. 148 ** xmitBufferSize: Length of data. 149 ** recvBuffer: Buffer to receive response. 150 ** recvBufferMaxSize: Maximum size of buffer. 151 ** recvBufferActualSize: Actual length of response. 152 ** timeoutMillisec: timeout in millisecond 153 ** 154 ** Returns: True if ok. 155 ** 156 *******************************************************************************/ 157 bool transceive (UINT8* xmitBuffer, INT32 xmitBufferSize, UINT8* recvBuffer, 158 INT32 recvBufferMaxSize, INT32& recvBufferActualSize, INT32 timeoutMillisec); 159 160 /******************************************************************************* 161 ** 162 ** Function: notifyListenModeState 163 ** 164 ** Description: Notify the NFC service about whether the SE was activated 165 ** in listen mode. 166 ** isActive: Whether the secure element is activated. 167 ** 168 ** Returns: None 169 ** 170 *******************************************************************************/ 171 void notifyListenModeState (bool isActivated); 172 173 /******************************************************************************* 174 ** 175 ** Function: notifyRfFieldEvent 176 ** 177 ** Description: Notify the NFC service about RF field events from the stack. 178 ** isActive: Whether any secure element is activated. 179 ** 180 ** Returns: None 181 ** 182 *******************************************************************************/ 183 void notifyRfFieldEvent (bool isActive); 184 185 186 /******************************************************************************* 187 ** 188 ** Function: storeUiccInfo 189 ** 190 ** Description: Store a copy of the execution environment information from the stack. 191 ** info: execution environment information. 192 ** 193 ** Returns: None 194 ** 195 *******************************************************************************/ 196 void storeUiccInfo (tNFA_EE_DISCOVER_REQ& info); 197 198 199 /******************************************************************************* 200 ** 201 ** Function: getUiccId 202 ** 203 ** Description: Get the ID of the secure element. 204 ** eeHandle: Handle to the secure element. 205 ** uid: Array to receive the ID. 206 ** 207 ** Returns: True if ok. 208 ** 209 *******************************************************************************/ 210 bool getUiccId (tNFA_HANDLE eeHandle, jbyteArray& uid); 211 212 213 /******************************************************************************* 214 ** 215 ** Function: getTechnologyList 216 ** 217 ** Description: Get all the technologies supported by a secure element. 218 ** eeHandle: Handle of secure element. 219 ** techList: List to receive the technologies. 220 ** 221 ** Returns: True if ok. 222 ** 223 *******************************************************************************/ 224 bool getTechnologyList (tNFA_HANDLE eeHandle, jintArray& techList); 225 226 227 /******************************************************************************* 228 ** 229 ** Function: notifyTransactionListenersOfAid 230 ** 231 ** Description: Notify the NFC service about a transaction event from secure element. 232 ** aid: Buffer contains application ID. 233 ** aidLen: Length of application ID. 234 ** 235 ** Returns: None 236 ** 237 *******************************************************************************/ 238 void notifyTransactionListenersOfAid (const UINT8* aid, UINT8 aidLen); 239 240 241 /******************************************************************************* 242 ** 243 ** Function: notifyTransactionListenersOfTlv 244 ** 245 ** Description: Notify the NFC service about a transaction event from secure element. 246 ** The type-length-value contains AID and parameter. 247 ** tlv: type-length-value encoded in Basic Encoding Rule. 248 ** tlvLen: Length tlv. 249 ** 250 ** Returns: None 251 ** 252 *******************************************************************************/ 253 void notifyTransactionListenersOfTlv (const UINT8* tlv, UINT8 tlvLen); 254 255 256 /******************************************************************************* 257 ** 258 ** Function: connectionEventHandler 259 ** 260 ** Description: Receive card-emulation related events from stack. 261 ** event: Event code. 262 ** eventData: Event data. 263 ** 264 ** Returns: None 265 ** 266 *******************************************************************************/ 267 void connectionEventHandler (UINT8 event, tNFA_CONN_EVT_DATA* eventData); 268 269 270 /******************************************************************************* 271 ** 272 ** Function: applyRoutes 273 ** 274 ** Description: Read route data from XML and apply them again 275 ** to every secure element. 276 ** 277 ** Returns: None 278 ** 279 *******************************************************************************/ 280 void applyRoutes (); 281 282 283 /******************************************************************************* 284 ** 285 ** Function: setActiveSeOverride 286 ** 287 ** Description: Specify which secure element to turn on. 288 ** activeSeOverride: ID of secure element 289 ** 290 ** Returns: None 291 ** 292 *******************************************************************************/ 293 void setActiveSeOverride (UINT8 activeSeOverride); 294 295 296 /******************************************************************************* 297 ** 298 ** Function: routeToSecureElement 299 ** 300 ** Description: Adjust controller's listen-mode routing table so transactions 301 ** are routed to the secure elements as specified in route.xml. 302 ** 303 ** Returns: True if ok. 304 ** 305 *******************************************************************************/ 306 bool routeToSecureElement (); 307 308 309 /******************************************************************************* 310 ** 311 ** Function: routeToDefault 312 ** 313 ** Description: Adjust controller's listen-mode routing table so transactions 314 ** are routed to the default destination specified in route.xml. 315 ** 316 ** Returns: True if ok. 317 ** 318 *******************************************************************************/ 319 bool routeToDefault (); 320 321 322 /******************************************************************************* 323 ** 324 ** Function: isBusy 325 ** 326 ** Description: Whether NFC controller is routing listen-mode events or a pipe is connected. 327 ** 328 ** Returns: True if either case is true. 329 ** 330 *******************************************************************************/ 331 bool isBusy (); 332 333 334 /******************************************************************************* 335 ** 336 ** Function getActualNumEe 337 ** 338 ** Description Returns number of secure elements we know about. 339 ** 340 ** Returns Number of secure elements we know about. 341 ** 342 *******************************************************************************/ 343 UINT8 getActualNumEe(); 344 345 346 /******************************************************************************* 347 ** 348 ** Function getSeVerInfo 349 ** 350 ** Description Gets version information and id for a secure element. The 351 ** seIndex parmeter is the zero based index of the secure 352 ** element to get verion info for. The version infommation 353 ** is returned as a string int the verInfo parameter. 354 ** 355 ** Returns ture on success, false on failure 356 ** 357 *******************************************************************************/ 358 bool getSeVerInfo(int seIndex, char * verInfo, int verInfoSz, UINT8 * seid); 359 360 361 /******************************************************************************* 362 ** 363 ** Function: isActivatedInListenMode 364 ** 365 ** Description: Can be used to determine if the SE is activated in listen mode 366 ** 367 ** Returns: True if the SE is activated in listen mode 368 ** 369 *******************************************************************************/ 370 bool isActivatedInListenMode(); 371 372 /******************************************************************************* 373 ** 374 ** Function: isRfFieldOn 375 ** 376 ** Description: Can be used to determine if the SE is in an RF field 377 ** 378 ** Returns: True if the SE is activated in an RF field 379 ** 380 *******************************************************************************/ 381 bool isRfFieldOn(); 382 383private: 384 static const unsigned int MAX_RESPONSE_SIZE = 1024; 385 enum RouteSelection {NoRoute, DefaultRoute, SecElemRoute}; 386 static const int MAX_NUM_EE = 5; //max number of EE's 387 static const UINT8 STATIC_PIPE_0x70 = 0x70; //Broadcom's proprietary static pipe 388 static const UINT8 STATIC_PIPE_0x71 = 0x71; //Broadcom's proprietary static pipe 389 static const UINT8 EVT_SEND_DATA = 0x10; //see specification ETSI TS 102 622 v9.0.0 (Host Controller Interface); section 9.3.3.3 390 static const tNFA_HANDLE EE_HANDLE_0xF3 = 0x4F3; //handle to secure element in slot 0 391 static const tNFA_HANDLE EE_HANDLE_0xF4 = 0x4F4; //handle to secure element in slot 1 392 static SecureElement sSecElem; 393 static const char* APP_NAME; 394 395 UINT8 mDestinationGate; //destination gate of the UICC 396 tNFA_HANDLE mNfaHciHandle; //NFA handle to NFA's HCI component 397 nfc_jni_native_data* mNativeData; 398 bool mIsInit; // whether EE is initialized 399 UINT8 mActualNumEe; // actual number of EE's reported by the stack 400 UINT8 mNumEePresent; // actual number of usable EE's 401 bool mbNewEE; 402 UINT8 mNewPipeId; 403 UINT8 mNewSourceGate; 404 UINT16 mActiveSeOverride; // active "enable" seid, 0 means activate all SEs 405 tNFA_STATUS mCommandStatus; //completion status of the last command 406 bool mIsPiping; //is a pipe connected to the controller? 407 RouteSelection mCurrentRouteSelection; 408 int mActualResponseSize; //number of bytes in the response received from secure element 409 bool mUseOberthurWarmReset; //whether to use warm-reset command 410 bool mActivatedInListenMode; // whether we're activated in listen mode 411 UINT8 mOberthurWarmResetCommand; //warm-reset command byte 412 tNFA_EE_INFO mEeInfo [MAX_NUM_EE]; //actual size stored in mActualNumEe 413 tNFA_EE_DISCOVER_REQ mUiccInfo; 414 tNFA_HCI_GET_GATE_PIPE_LIST mHciCfg; 415 SyncEvent mEeRegisterEvent; 416 SyncEvent mHciRegisterEvent; 417 SyncEvent mEeSetModeEvent; 418 SyncEvent mPipeListEvent; 419 SyncEvent mCreatePipeEvent; 420 SyncEvent mPipeOpenedEvent; 421 SyncEvent mAllocateGateEvent; 422 SyncEvent mDeallocateGateEvent; 423 SyncEvent mRoutingEvent; 424 SyncEvent mUiccInfoEvent; 425 SyncEvent mUiccListenEvent; 426 SyncEvent mAidAddRemoveEvent; 427 SyncEvent mTransceiveEvent; 428 SyncEvent mVerInfoEvent; 429 SyncEvent mRegistryEvent; 430 UINT8 mVerInfo [3]; 431 UINT8 mResponseData [MAX_RESPONSE_SIZE]; 432 RouteDataSet mRouteDataSet; //routing data 433 std::vector<std::string> mUsedAids; //AID's that are used in current routes 434 UINT8 mAidForEmptySelect[NCI_MAX_AID_LEN+1]; 435 Mutex mMutex; // protects fields below 436 bool mRfFieldIsOn; // last known RF field state 437 struct timespec mLastRfFieldToggle; // last time RF field went off 438 /******************************************************************************* 439 ** 440 ** Function: SecureElement 441 ** 442 ** Description: Initialize member variables. 443 ** 444 ** Returns: None 445 ** 446 *******************************************************************************/ 447 SecureElement (); 448 449 450 /******************************************************************************* 451 ** 452 ** Function: ~SecureElement 453 ** 454 ** Description: Release all resources. 455 ** 456 ** Returns: None 457 ** 458 *******************************************************************************/ 459 ~SecureElement (); 460 461 462 /******************************************************************************* 463 ** 464 ** Function: nfaEeCallback 465 ** 466 ** Description: Receive execution environment-related events from stack. 467 ** event: Event code. 468 ** eventData: Event data. 469 ** 470 ** Returns: None 471 ** 472 *******************************************************************************/ 473 static void nfaEeCallback (tNFA_EE_EVT event, tNFA_EE_CBACK_DATA* eventData); 474 475 476 /******************************************************************************* 477 ** 478 ** Function: nfaHciCallback 479 ** 480 ** Description: Receive Host Controller Interface-related events from stack. 481 ** event: Event code. 482 ** eventData: Event data. 483 ** 484 ** Returns: None 485 ** 486 *******************************************************************************/ 487 static void nfaHciCallback (tNFA_HCI_EVT event, tNFA_HCI_EVT_DATA* eventData); 488 489 490 /******************************************************************************* 491 ** 492 ** Function: findEeByHandle 493 ** 494 ** Description: Find information about an execution environment. 495 ** eeHandle: Handle to execution environment. 496 ** 497 ** Returns: Information about an execution environment. 498 ** 499 *******************************************************************************/ 500 tNFA_EE_INFO *findEeByHandle (tNFA_HANDLE eeHandle); 501 502 503 /******************************************************************************* 504 ** 505 ** Function: findUiccByHandle 506 ** 507 ** Description: Find information about an execution environment. 508 ** eeHandle: Handle of the execution environment. 509 ** 510 ** Returns: Information about the execution environment. 511 ** 512 *******************************************************************************/ 513 tNFA_EE_DISCOVER_INFO *findUiccByHandle (tNFA_HANDLE eeHandle); 514 515 516 /******************************************************************************* 517 ** 518 ** Function: getDefaultEeHandle 519 ** 520 ** Description: Get the handle to the execution environment. 521 ** 522 ** Returns: Handle to the execution environment. 523 ** 524 *******************************************************************************/ 525 tNFA_HANDLE getDefaultEeHandle (); 526 527 528 /******************************************************************************* 529 ** 530 ** Function: adjustRoutes 531 ** 532 ** Description: Adjust routes in the controller's listen-mode routing table. 533 ** selection: which set of routes to configure the controller. 534 ** 535 ** Returns: None 536 ** 537 *******************************************************************************/ 538 void adjustRoutes (RouteSelection selection); 539 540 541 /******************************************************************************* 542 ** 543 ** Function: adjustProtocolRoutes 544 ** 545 ** Description: Adjust default routing based on protocol in NFC listen mode. 546 ** isRouteToEe: Whether routing to EE (true) or host (false). 547 ** 548 ** Returns: None 549 ** 550 *******************************************************************************/ 551 void adjustProtocolRoutes (RouteDataSet::Database* db, RouteSelection routeSelection); 552 553 554 /******************************************************************************* 555 ** 556 ** Function: adjustTechnologyRoutes 557 ** 558 ** Description: Adjust default routing based on technology in NFC listen mode. 559 ** isRouteToEe: Whether routing to EE (true) or host (false). 560 ** 561 ** Returns: None 562 ** 563 *******************************************************************************/ 564 void adjustTechnologyRoutes (RouteDataSet::Database* db, RouteSelection routeSelection); 565 566 567 /******************************************************************************* 568 ** 569 ** Function: getEeInfo 570 ** 571 ** Description: Get latest information about execution environments from stack. 572 ** 573 ** Returns: True if at least 1 EE is available. 574 ** 575 *******************************************************************************/ 576 bool getEeInfo (); 577 578 /******************************************************************************* 579 ** 580 ** Function: eeStatusToString 581 ** 582 ** Description: Convert status code to status text. 583 ** status: Status code 584 ** 585 ** Returns: None 586 ** 587 *******************************************************************************/ 588 static const char* eeStatusToString (UINT8 status); 589 590 591 /******************************************************************************* 592 ** 593 ** Function: encodeAid 594 ** 595 ** Description: Encode AID in type-length-value using Basic Encoding Rule. 596 ** tlv: Buffer to store TLV. 597 ** tlvMaxLen: TLV buffer's maximum length. 598 ** tlvActualLen: TLV buffer's actual length. 599 ** aid: Buffer of Application ID. 600 ** aidLen: Aid buffer's actual length. 601 ** 602 ** Returns: True if ok. 603 ** 604 *******************************************************************************/ 605 bool encodeAid (UINT8* tlv, UINT16 tlvMaxLen, UINT16& tlvActualLen, const UINT8* aid, UINT8 aidLen); 606}; 607 608