1 /****************************************************************************** 2 * 3 * Copyright 2018 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_0_SECUREELEMENT_H 19 #define ANDROID_HARDWARE_SECURE_ELEMENT_V1_0_SECUREELEMENT_H 20 21 #include <android-base/stringprintf.h> 22 #include <android/hardware/secure_element/1.0/ISecureElement.h> 23 #include <hardware/hardware.h> 24 #include <hidl/MQDescriptor.h> 25 #include <hidl/Status.h> 26 #include <pthread.h> 27 28 #include "phNxpEse_Api.h" 29 30 class ThreadMutex { 31 public: 32 ThreadMutex(); 33 virtual ~ThreadMutex(); 34 void lock(); 35 void unlock(); 36 operator pthread_mutex_t*() { return &mMutex; } 37 38 private: 39 pthread_mutex_t mMutex; 40 }; 41 42 class AutoThreadMutex { 43 public: 44 AutoThreadMutex(ThreadMutex& m); 45 virtual ~AutoThreadMutex(); 46 operator ThreadMutex&() { return mm; } 47 operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; } 48 49 private: 50 ThreadMutex& mm; 51 }; 52 53 namespace android { 54 namespace hardware { 55 namespace secure_element { 56 namespace V1_0 { 57 namespace implementation { 58 59 using ::android::sp; 60 using android::base::StringPrintf; 61 using ::android::hardware::hidl_array; 62 using ::android::hardware::hidl_memory; 63 using ::android::hardware::hidl_string; 64 using ::android::hardware::hidl_vec; 65 using ::android::hardware::Return; 66 using ::android::hardware::Void; 67 using ::android::hidl::base::V1_0::IBase; 68 69 #ifndef MIN_APDU_LENGTH 70 #define MIN_APDU_LENGTH 0x04 71 #endif 72 #ifndef DEFAULT_BASIC_CHANNEL 73 #define DEFAULT_BASIC_CHANNEL 0x00 74 #endif 75 76 struct SecureElement : public ISecureElement, public hidl_death_recipient { 77 SecureElement(); 78 Return<void> init( 79 const sp< 80 ::android::hardware::secure_element::V1_0::ISecureElementHalCallback>& 81 clientCallback) override; 82 Return<void> getAtr(getAtr_cb _hidl_cb) override; 83 Return<bool> isCardPresent() override; 84 Return<void> transmit(const hidl_vec<uint8_t>& data, 85 transmit_cb _hidl_cb) override; 86 Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, 87 openLogicalChannel_cb _hidl_cb) override; 88 Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, 89 openBasicChannel_cb _hidl_cb) override; 90 Return<::android::hardware::secure_element::V1_0::SecureElementStatus> 91 closeChannel(uint8_t channelNumber) override; 92 93 void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/); 94 95 private: 96 uint8_t mMaxChannelCount; 97 uint8_t mOpenedchannelCount = 0; 98 bool mIsEseInitialized = false; 99 static std::vector<bool> mOpenedChannels; 100 static sp<V1_0::ISecureElementHalCallback> mCallbackV1_0; 101 Return<::android::hardware::secure_element::V1_0::SecureElementStatus> 102 seHalDeInit(); 103 ESESTATUS seHalInit(); 104 Return<::android::hardware::secure_element::V1_0::SecureElementStatus> 105 internalCloseChannel(uint8_t channelNumber); 106 }; 107 108 } // namespace implementation 109 } // namespace V1_0 110 } // namespace secure_element 111 } // namespace hardware 112 } // namespace android 113 114 #endif // ANDROID_HARDWARE_SECURE_ELEMENT_V1_0_SECUREELEMENT_H 115