1 /****************************************************************************** 2 * 3 * Copyright 2018-2019,2023 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_1_SECUREELEMENT_H 19 #define ANDROID_HARDWARE_SECURE_ELEMENT_V1_1_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.1/ISecureElement.h> 25 #include <hardware/hardware.h> 26 #include <hidl/MQDescriptor.h> 27 #include <hidl/Status.h> 28 #include <pthread.h> 29 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_1 { 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_1::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 #ifndef MAX_AID_LENGTH 81 #define MAX_AID_LENGTH 0x10 82 #endif 83 84 struct SecureElement : public V1_1::ISecureElement, 85 public hidl_death_recipient { 86 SecureElement(); 87 Return<void> init( 88 const sp< 89 ::android::hardware::secure_element::V1_0::ISecureElementHalCallback>& 90 clientCallback) override; 91 Return<void> init_1_1( 92 const sp< 93 ::android::hardware::secure_element::V1_1::ISecureElementHalCallback>& 94 clientCallback) override; 95 Return<void> getAtr(getAtr_cb _hidl_cb) override; 96 Return<bool> isCardPresent() override; 97 Return<void> transmit(const hidl_vec<uint8_t>& data, 98 transmit_cb _hidl_cb) override; 99 Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, 100 openLogicalChannel_cb _hidl_cb) override; 101 Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, 102 openBasicChannel_cb _hidl_cb) override; 103 Return<SecureElementStatus> closeChannel(uint8_t channelNumber) override; 104 105 void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/); 106 107 static void NotifySeWaitExtension(phNxpEse_wtxState state); 108 109 private: 110 uint8_t mMaxChannelCount; 111 uint8_t mOpenedchannelCount = 0; 112 Mutex seHalLock; 113 bool mIsEseInitialized = false; 114 static std::vector<bool> mOpenedChannels; 115 static sp<V1_0::ISecureElementHalCallback> mCallbackV1_0; 116 static sp<V1_1::ISecureElementHalCallback> mCallbackV1_1; 117 Return<SecureElementStatus> seHalDeInit(); 118 ESESTATUS seHalInit(); 119 Return<SecureElementStatus> internalCloseChannel(uint8_t channelNumber); 120 }; 121 122 } // namespace implementation 123 } // namespace V1_1 124 } // namespace secure_element 125 } // namespace hardware 126 } // namespace android 127 128 #endif // ANDROID_HARDWARE_SECURE_ELEMENT_V1_1_SECUREELEMENT_H 129