1 /****************************************************************************** 2 * 3 * Copyright 2020 NXP 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 #ifndef ANDROID_HARDWARE_SECURE_ELEMENT_V1_2_SECUREELEMENT_H 19 #define ANDROID_HARDWARE_SECURE_ELEMENT_V1_2_SECUREELEMENT_H 20 21 #include <SyncEvent.h> 22 #include <android-base/stringprintf.h> 23 #include <android/hardware/secure_element/1.0/types.h> 24 #include <android/hardware/secure_element/1.2/ISecureElement.h> 25 #include <hardware/hardware.h> 26 #include <hidl/MQDescriptor.h> 27 #include <hidl/Status.h> 28 #include <pthread.h> 29 #include "OsuHalExtn.h" 30 #include "phNxpEse_Api.h" 31 32 class ThreadMutex { 33 public: 34 ThreadMutex(); 35 virtual ~ThreadMutex(); 36 void lock(); 37 void unlock(); 38 operator pthread_mutex_t*() { return &mMutex; } 39 40 private: 41 pthread_mutex_t mMutex; 42 }; 43 44 class AutoThreadMutex { 45 public: 46 AutoThreadMutex(ThreadMutex& m); 47 virtual ~AutoThreadMutex(); 48 operator ThreadMutex&() { return mm; } 49 operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; } 50 51 private: 52 ThreadMutex& mm; 53 }; 54 55 namespace android { 56 namespace hardware { 57 namespace secure_element { 58 namespace V1_2 { 59 namespace implementation { 60 61 using ::android::sp; 62 using android::base::StringPrintf; 63 using ::android::hardware::hidl_array; 64 using ::android::hardware::hidl_memory; 65 using ::android::hardware::hidl_string; 66 using ::android::hardware::hidl_vec; 67 using ::android::hardware::Return; 68 using ::android::hardware::Void; 69 using ::android::hardware::secure_element::V1_0::LogicalChannelResponse; 70 using ::android::hardware::secure_element::V1_0::SecureElementStatus; 71 using ::android::hardware::secure_element::V1_2::ISecureElement; 72 using ::android::hidl::base::V1_0::IBase; 73 74 #ifndef MIN_APDU_LENGTH 75 #define MIN_APDU_LENGTH 0x04 76 #endif 77 #ifndef DEFAULT_BASIC_CHANNEL 78 #define DEFAULT_BASIC_CHANNEL 0x00 79 #endif 80 81 struct SecureElement : public V1_2::ISecureElement, 82 public hidl_death_recipient { 83 SecureElement(); 84 Return<void> init( 85 const sp< 86 ::android::hardware::secure_element::V1_0::ISecureElementHalCallback>& 87 clientCallback) override; 88 Return<void> init_1_1( 89 const sp< 90 ::android::hardware::secure_element::V1_1::ISecureElementHalCallback>& 91 clientCallback) override; 92 Return<void> getAtr(getAtr_cb _hidl_cb) override; 93 Return<bool> isCardPresent() override; 94 Return<void> transmit(const hidl_vec<uint8_t>& data, 95 transmit_cb _hidl_cb) override; 96 Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, 97 openLogicalChannel_cb _hidl_cb) override; 98 Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, 99 openBasicChannel_cb _hidl_cb) override; 100 Return<SecureElementStatus> closeChannel(uint8_t channelNumber) override; 101 102 void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/); 103 104 static void NotifySeWaitExtension(phNxpEse_wtxState state); 105 Return<::android::hardware::secure_element::V1_0::SecureElementStatus> 106 reset(); 107 108 private: 109 uint8_t mMaxChannelCount; 110 uint8_t mOpenedchannelCount = 0; 111 Mutex seHalLock; 112 bool mIsEseInitialized = false; 113 static std::vector<bool> mOpenedChannels; 114 static sp<V1_0::ISecureElementHalCallback> mCallbackV1_0; 115 static sp<V1_1::ISecureElementHalCallback> mCallbackV1_1; 116 Return<SecureElementStatus> seHalDeInit(); 117 ESESTATUS seHalInit(); 118 Return<SecureElementStatus> internalCloseChannel(uint8_t channelNumber); 119 }; 120 121 } // namespace implementation 122 } // namespace V1_2 123 } // namespace secure_element 124 } // namespace hardware 125 } // namespace android 126 127 #endif // ANDROID_HARDWARE_SECURE_ELEMENT_V1_1_SECUREELEMENT_H 128