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 #ifndef VENDOR_NXP_VIRTUAL_ISO_V1_0_SECUREELEMENT_H
19 #define VENDOR_NXP_VIRTUAL_ISO_V1_0_SECUREELEMENT_H
20 
21 #include <android-base/stringprintf.h>
22 #include <android/hardware/secure_element/1.0/ISecureElement.h>
23 #include <hardware/hardware.h>
24 #include <hidl/MQDescriptor.h>
25 #include <hidl/Status.h>
26 #include "phNxpEse_Api.h"
27 
28 namespace vendor {
29 namespace nxp {
30 namespace virtual_iso {
31 namespace V1_0 {
32 namespace implementation {
33 
34 using ::android::sp;
35 using ::android::wp;
36 using android::base::StringPrintf;
37 using ::android::hardware::hidl_array;
38 using ::android::hardware::hidl_death_recipient;
39 using ::android::hardware::hidl_memory;
40 using ::android::hardware::hidl_string;
41 using ::android::hardware::hidl_vec;
42 using ::android::hardware::Return;
43 using ::android::hardware::Void;
44 using ::android::hardware::secure_element::V1_0::ISecureElement;
45 using ::android::hardware::secure_element::V1_0::LogicalChannelResponse;
46 using ::android::hardware::secure_element::V1_0::SecureElementStatus;
47 using ::android::hidl::base::V1_0::IBase;
48 
49 #ifndef DEFAULT_BASIC_CHANNEL
50 #define DEFAULT_BASIC_CHANNEL 0x00
51 #endif
52 
53 struct VirtualISO : public ISecureElement, public hidl_death_recipient {
54   VirtualISO();
55   Return<void> init(
56       const sp<
57           ::android::hardware::secure_element::V1_0::ISecureElementHalCallback>&
58           clientCallback) override;
59   Return<void> getAtr(getAtr_cb _hidl_cb) override;
60   Return<bool> isCardPresent() override;
61   Return<void> transmit(const hidl_vec<uint8_t>& data,
62                         transmit_cb _hidl_cb) override;
63   Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
64                                   openLogicalChannel_cb _hidl_cb) override;
65   Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
66                                 openBasicChannel_cb _hidl_cb) override;
67   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
68   closeChannel(uint8_t channelNumber) override;
69 
serviceDiedVirtualISO70   virtual void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
71     // TODO: Implement graceful closure, to close ongoing tx-rx and deinit
72     // T=1 stack
73     // close(0);
74   }
75 
76  private:
77   uint8_t mMaxChannelCount;
78   uint8_t mOpenedchannelCount = 0;
79   bool mIsEseInitialized = false;
80   static std::vector<bool> mOpenedChannels;
81   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
82   seHalDeInit();
83   ESESTATUS seHalInit();
84   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
85   internalCloseChannel(uint8_t channelNumber);
86 };
87 
88 }  // namespace implementation
89 }  // namespace V1_0
90 }  // namespace virtual_iso
91 }  // namespace nxp
92 }  // namespace vendor
93 
94 #endif  // VENDOR_NXP_VIRTUAL_ISO_V1_0_SECUREELEMENT_H
95