1 /******************************************************************************
2  *
3  *  Copyright 2018,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_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 #ifndef MAX_AID_LENGTH
76 #define MAX_AID_LENGTH 0x10
77 #endif
78 
79 struct SecureElement : public ISecureElement, public hidl_death_recipient {
80   SecureElement();
81   Return<void> init(
82       const sp<
83           ::android::hardware::secure_element::V1_0::ISecureElementHalCallback>&
84           clientCallback) override;
85   Return<void> getAtr(getAtr_cb _hidl_cb) override;
86   Return<bool> isCardPresent() override;
87   Return<void> transmit(const hidl_vec<uint8_t>& data,
88                         transmit_cb _hidl_cb) override;
89   Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
90                                   openLogicalChannel_cb _hidl_cb) override;
91   Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
92                                 openBasicChannel_cb _hidl_cb) override;
93   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
94   closeChannel(uint8_t channelNumber) override;
95 
96   void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/);
97 
98  private:
99   uint8_t mMaxChannelCount;
100   uint8_t mOpenedchannelCount = 0;
101   bool mIsEseInitialized = false;
102   static std::vector<bool> mOpenedChannels;
103   static sp<V1_0::ISecureElementHalCallback> mCallbackV1_0;
104   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
105   seHalDeInit();
106   ESESTATUS seHalInit();
107   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
108   internalCloseChannel(uint8_t channelNumber);
109 };
110 
111 }  // namespace implementation
112 }  // namespace V1_0
113 }  // namespace secure_element
114 }  // namespace hardware
115 }  // namespace android
116 
117 #endif  // ANDROID_HARDWARE_SECURE_ELEMENT_V1_0_SECUREELEMENT_H
118