1 /******************************************************************************
2  *
3  *  Copyright 2020,2022-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_2_SECUREELEMENT_H
19 #define ANDROID_HARDWARE_SECURE_ELEMENT_V1_2_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.2/ISecureElement.h>
25 #include <hardware/hardware.h>
26 #include <hidl/MQDescriptor.h>
27 #include <hidl/Status.h>
28 #include <pthread.h>
29 
30 #include <vector>
31 
32 #include "OsuHalExtn.h"
33 #include "phNxpEse_Api.h"
34 
35 class ThreadMutex {
36  public:
37   ThreadMutex();
38   virtual ~ThreadMutex();
39   void lock();
40   void unlock();
41   operator pthread_mutex_t*() { return &mMutex; }
42 
43  private:
44   pthread_mutex_t mMutex;
45 };
46 
47 class AutoThreadMutex {
48  public:
49   AutoThreadMutex(ThreadMutex& m);
50   virtual ~AutoThreadMutex();
51   operator ThreadMutex&() { return mm; }
52   operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; }
53 
54  private:
55   ThreadMutex& mm;
56 };
57 
58 namespace android {
59 namespace hardware {
60 namespace secure_element {
61 namespace V1_2 {
62 namespace implementation {
63 
64 using ::android::sp;
65 using android::base::StringPrintf;
66 using ::android::hardware::hidl_array;
67 using ::android::hardware::hidl_memory;
68 using ::android::hardware::hidl_string;
69 using ::android::hardware::hidl_vec;
70 using ::android::hardware::Return;
71 using ::android::hardware::Void;
72 using ::android::hardware::secure_element::V1_0::LogicalChannelResponse;
73 using ::android::hardware::secure_element::V1_0::SecureElementStatus;
74 using ::android::hardware::secure_element::V1_2::ISecureElement;
75 using ::android::hidl::base::V1_0::IBase;
76 
77 #ifndef MIN_APDU_LENGTH
78 #define MIN_APDU_LENGTH 0x04
79 #endif
80 #ifndef DEFAULT_BASIC_CHANNEL
81 #define DEFAULT_BASIC_CHANNEL 0x00
82 #endif
83 #ifndef MAX_AID_LENGTH
84 #define MAX_AID_LENGTH 0x10
85 #endif
86 
87 struct SecureElement : public V1_2::ISecureElement,
88                        public hidl_death_recipient {
89   SecureElement();
90   // Methods from ::android::hidl::base::V1_0::IBase follow.
91   Return<void> debug(const hidl_handle& handle,
92                      const hidl_vec<hidl_string>& options) override;
93 
94   Return<void> init(
95       const sp<
96           ::android::hardware::secure_element::V1_0::ISecureElementHalCallback>&
97           clientCallback) override;
98   Return<void> init_1_1(
99       const sp<
100           ::android::hardware::secure_element::V1_1::ISecureElementHalCallback>&
101           clientCallback) override;
102   Return<void> getAtr(getAtr_cb _hidl_cb) override;
103   Return<bool> isCardPresent() override;
104   Return<void> transmit(const hidl_vec<uint8_t>& data,
105                         transmit_cb _hidl_cb) override;
106   Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
107                                   openLogicalChannel_cb _hidl_cb) override;
108   Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
109                                 openBasicChannel_cb _hidl_cb) override;
110   Return<SecureElementStatus> closeChannel(uint8_t channelNumber) override;
111 
112   void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/);
113 
114   static void NotifySeWaitExtension(phNxpEse_wtxState state);
115   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
116   reset();
117 
118  private:
119   uint8_t mMaxChannelCount;
120   uint8_t mOpenedchannelCount = 0;
121   Mutex seHalLock;
122   bool mIsEseInitialized = false;
123   static std::vector<bool> mOpenedChannels;
124   static sp<V1_0::ISecureElementHalCallback> mCallbackV1_0;
125 
126   std::vector<sp<V1_1::ISecureElementHalCallback>> mCallbacks;
127   bool mHasPriorityAccess = false;
128 
129   void notifyClients(bool connected, std::string reason);
130 
131   void registerCallback(const sp<V1_1::ISecureElementHalCallback>& callback);
132   void unregisterCallback(const sp<IBase>& callback);
133 
134   Return<SecureElementStatus> seHalDeInit();
135   ESESTATUS seHalInit();
136   Return<SecureElementStatus> internalCloseChannel(uint8_t channelNumber);
137   uint8_t getReserveChannelCnt(const hidl_vec<uint8_t>& aid);
138   uint8_t getMaxChannelCnt();
139 };
140 
141 }  // namespace implementation
142 }  // namespace V1_2
143 }  // namespace secure_element
144 }  // namespace hardware
145 }  // namespace android
146 
147 #endif  // ANDROID_HARDWARE_SECURE_ELEMENT_V1_1_SECUREELEMENT_H
148