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 #pragma once
19 #include <pthread.h>
20 
21 #include <android/hardware/nfc/1.0/types.h>
22 #include <phEseStatus.h>
23 #include <utils/RefBase.h>
24 #include <vendor/nxp/nxpnfc/1.0/INxpNfc.h>
25 #include "hal_nxpese.h"
26 using vendor::nxp::nxpnfc::V1_0::INxpNfc;
27 
28 class ThreadMutex {
29  public:
30   ThreadMutex();
31   virtual ~ThreadMutex();
32   void lock();
33   void unlock();
34   operator pthread_mutex_t*() { return &mMutex; }
35 
36  private:
37   pthread_mutex_t mMutex;
38 };
39 
40 class ThreadCondVar : public ThreadMutex {
41  public:
42   ThreadCondVar();
43   virtual ~ThreadCondVar();
44   void signal();
45   void wait();
46   operator pthread_cond_t*() { return &mCondVar; }
47   operator pthread_mutex_t*() {
48     return ThreadMutex::operator pthread_mutex_t*();
49   }
50 
51  private:
52   pthread_cond_t mCondVar;
53 };
54 
55 class AutoThreadMutex {
56  public:
57   AutoThreadMutex(ThreadMutex& m);
58   virtual ~AutoThreadMutex();
59   operator ThreadMutex&() { return mm; }
60   operator pthread_mutex_t*() { return (pthread_mutex_t*)mm; }
61 
62  private:
63   ThreadMutex& mm;
64 };
65 
66 class NfcAdaptation {
67  public:
68   virtual ~NfcAdaptation();
69   void Initialize();
70   static NfcAdaptation& GetInstance();
71   static ESESTATUS HalIoctl(long data_len, void* p_data);
72   ese_nxp_IoctlInOutData_t* mCurrentIoctlData;
73 
74  private:
75   NfcAdaptation();
76   static NfcAdaptation* mpInstance;
77   static ThreadMutex sLock;
78   static ThreadMutex sIoctlLock;
79   ThreadCondVar mCondVar;
80   static ThreadCondVar mHalIoctlEvent;
81   static android::sp<INxpNfc> mHalNxpNfc;
82 };
83