1 /******************************************************************************
2  *
3  *  Copyright 2023-2024 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   void handleStateOnDeath();
97 
98  private:
99   uint8_t mMaxChannelCount;
100   uint8_t mOpenedchannelCount = 0;
101   Mutex seHalLock;
102   bool mIsEseInitialized = false;
103   static std::vector<bool> mOpenedChannels;
104   Mutex seHalClientLock;
105   Mutex initLock;
106 
107   static std::shared_ptr<ISecureElementCallback> mCb;
108   static uid_t mCbClientUid;
109   bool mHasPriorityAccess = false;
110   bool isOmapi;
111 
112   ESESTATUS seHalInit();
113   int internalCloseChannel(uint8_t channelNumber);
114   uint8_t getReserveChannelCnt(const std::vector<uint8_t>& aid);
115   uint8_t getMaxChannelCnt();
116   bool isClientVts(uid_t clientUid);
117   void handleClientCbCleanup();
118   bool handleClientCallback(
119       const std::shared_ptr<ISecureElementCallback>& clientCallback);
120 };
121 
122 }  // namespace secure_element
123 }  // namespace hardware
124 }  // namespace android
125 }  // namespace aidl
126