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 #pragma once
19 #include <aidl/vendor/nxp/nxpnfc_aidl/INxpNfc.h>
20 #include <android/hardware/nfc/1.0/types.h>
21 #include <phEseStatus.h>
22 #include <phNxpEseFeatures.h>
23 #include <pthread.h>
24 #include <utils/RefBase.h>
25 #include <vendor/nxp/nxpnfc/2.0/INxpNfc.h>
26 
27 #include "hal_nxpese.h"
28 using INxpNfc = vendor::nxp::nxpnfc::V2_0::INxpNfc;
29 using INxpNfcAidl = ::aidl::vendor::nxp::nxpnfc_aidl::INxpNfc;
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 ThreadCondVar : public ThreadMutex {
44  public:
45   ThreadCondVar();
46   virtual ~ThreadCondVar();
47   void signal();
48   void wait();
49   operator pthread_cond_t*() { return &mCondVar; }
50   operator pthread_mutex_t*() {
51     return ThreadMutex::operator pthread_mutex_t*();
52   }
53 
54  private:
55   pthread_cond_t mCondVar;
56 };
57 
58 class AutoThreadMutex {
59  public:
60   AutoThreadMutex(ThreadMutex& m);
61   virtual ~AutoThreadMutex();
62   operator ThreadMutex&() { return mm; }
63   operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; }
64 
65  private:
66   ThreadMutex& mm;
67 };
68 
69 class NfcAdaptation {
70  public:
71   virtual ~NfcAdaptation();
72   static NfcAdaptation& GetInstance();
73   static ESESTATUS resetEse(uint64_t level);
74   void Initialize();
75 #ifdef NXP_BOOTTIME_UPDATE
76   static ESESTATUS setEseUpdateState(void* p_data);
77 #endif
78   ese_nxp_IoctlInOutData_t* mCurrentIoctlData;
79 
80  private:
81   NfcAdaptation();
82   static NfcAdaptation* mpInstance;
83   static ThreadMutex sLock;
84   static ThreadMutex sIoctlLock;
85   ThreadCondVar mCondVar;
86   static ThreadCondVar mHalIoctlEvent;
87   static android::sp<INxpNfc> mHalNxpNfc;
88   static std::shared_ptr<INxpNfcAidl> mAidlHalNxpNfc;
89 };
90