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