1 /******************************************************************************
2  *
3  *
4  *  Copyright (C) 2015 NXP Semiconductors
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 #pragma once
20 #include <pthread.h>
21 
22 #include "ese_hal_api.h"
23 #include "hal_nxpese.h"
24 #include <utils/RefBase.h>
25 #include <android/hardware/secure_element/1.0/ISecureElement.h>
26 #include <android/hardware/secure_element/1.0/ISecureElementHalCallback.h>
27 #include <android/hardware/secure_element/1.0/types.h>
28 #include <vendor/nxp/nxpese/1.0/INxpEse.h>
29 using vendor::nxp::nxpese::V1_0::INxpEse;
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 EseAdaptation {
70  public:
71   void Initialize();
72   void InitializeHalDeviceContext();
73   virtual ~EseAdaptation();
74   static EseAdaptation& GetInstance();
75   static int HalIoctl(long arg, void* p_data);
76   tHAL_ESE_ENTRY* GetHalEntryFuncs();
77   ese_nxp_IoctlInOutData_t* mCurrentIoctlData;
78   tHAL_ESE_ENTRY mSpiHalEntryFuncs;  // function pointers for HAL entry points
79 
80  private:
81   EseAdaptation();
82   void signal();
83   static EseAdaptation* mpInstance;
84   static ThreadMutex sLock;
85   static ThreadMutex sIoctlLock;
86   ThreadCondVar mCondVar;
87   static tHAL_ESE_CBACK* mHalCallback;
88   static tHAL_ESE_DATA_CBACK* mHalDataCallback;
89   static ThreadCondVar mHalOpenCompletedEvent;
90   static ThreadCondVar mHalCloseCompletedEvent;
91   static ThreadCondVar mHalIoctlEvent;
92   static android::sp<android::hardware::secure_element::V1_0::ISecureElement>
93       mHal;
94   static android::sp<vendor::nxp::nxpese::V1_0::INxpEse> mHalNxpEse;
95 #if (NXP_EXTNS == TRUE)
96   pthread_t mThreadId;
97   static ThreadCondVar mHalCoreResetCompletedEvent;
98   static ThreadCondVar mHalCoreInitCompletedEvent;
99   static ThreadCondVar mHalInitCompletedEvent;
100 #endif
101   static uint32_t Thread(uint32_t arg);
102   static void HalDeviceContextDataCallback(uint16_t data_len, uint8_t* p_data);
103 
104   static void HalOpen(tHAL_ESE_CBACK* p_hal_cback,
105                       tHAL_ESE_DATA_CBACK* p_data_cback);
106   static void HalClose();
107   static void HalWrite(uint16_t data_len, uint8_t* p_data);
108   static void HalRead(uint16_t data_len, uint8_t* p_data);
109 };
110 tHAL_ESE_ENTRY* getInstance();
111