1 /******************************************************************************
2  *
3  *  Copyright 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 #pragma once
19 
20 #include <SyncEvent.h>
21 #include <aidl/android/hardware/secure_element/BnSecureElement.h>
22 #include <aidl/android/hardware/secure_element/ISecureElementCallback.h>
23 #include <android-base/logging.h>
24 #include <log/log.h>
25 
26 #include "OsuHalExtn.h"
27 #include "phNxpEse_Api.h"
28 
29 #define SESTATUS_SUCCESS 0
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 aidl {
55 namespace android {
56 namespace hardware {
57 namespace secure_element {
58 
59 using ::ndk::ICInterface;
60 using ndk::ScopedAStatus;
61 
62 #ifndef MIN_APDU_LENGTH
63 #define MIN_APDU_LENGTH 0x04
64 #endif
65 #ifndef DEFAULT_BASIC_CHANNEL
66 #define DEFAULT_BASIC_CHANNEL 0x00
67 #endif
68 #ifndef MAX_AID_LENGTH
69 #define MAX_AID_LENGTH 0x10
70 #endif
71 
72 struct SecureElement : public BnSecureElement {
73  public:
74   SecureElement();
75   ::ndk::ScopedAStatus closeChannel(int8_t in_channelNumber) override;
76   ::ndk::ScopedAStatus getAtr(std::vector<uint8_t>* _aidl_return) override;
77   ::ndk::ScopedAStatus init(
78       const std::shared_ptr<
79           ::aidl::android::hardware::secure_element::ISecureElementCallback>&
80           in_clientCallback) override;
81   ::ndk::ScopedAStatus isCardPresent(bool* _aidl_return) override;
82   ::ndk::ScopedAStatus openBasicChannel(
83       const std::vector<uint8_t>& in_aid, int8_t in_p2,
84       std::vector<uint8_t>* _aidl_return) override;
85   ::ndk::ScopedAStatus openLogicalChannel(
86       const std::vector<uint8_t>& in_aid, int8_t in_p2,
87       ::aidl::android::hardware::secure_element::LogicalChannelResponse*
88           _aidl_return) override;
89   ::ndk::ScopedAStatus reset() override;
90   ::ndk::ScopedAStatus transmit(const std::vector<uint8_t>& in_data,
91                                 std::vector<uint8_t>* _aidl_return) override;
92 
93   static void NotifySeWaitExtension(phNxpEse_wtxState state);
94   void updateSeHalInitState(bool);
95   int seHalDeInit();
96 
97  private:
98   uint8_t mMaxChannelCount;
99   uint8_t mOpenedchannelCount = 0;
100   Mutex seHalLock;
101   bool mIsEseInitialized = false;
102   static std::vector<bool> mOpenedChannels;
103 
104   static std::shared_ptr<ISecureElementCallback> mCb;
105   bool mHasPriorityAccess = false;
106 
107   ESESTATUS seHalInit();
108   int internalCloseChannel(uint8_t channelNumber);
109   uint8_t getReserveChannelCnt(const std::vector<uint8_t>& aid);
110   uint8_t getMaxChannelCnt();
111 };
112 
113 }  // namespace secure_element
114 }  // namespace hardware
115 }  // namespace android
116 }  // namespace aidl