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