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