1 /******************************************************************************
2  *
3  *  Copyright 2018,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 #ifndef ANDROID_HARDWARE_SECURE_ELEMENT_V1_1_SECUREELEMENT_H
19 #define ANDROID_HARDWARE_SECURE_ELEMENT_V1_1_SECUREELEMENT_H
20 
21 #include <android/hardware/secure_element/1.0/types.h>
22 #include <android/hardware/secure_element/1.1/ISecureElement.h>
23 #include <hidl/MQDescriptor.h>
24 #include <hidl/Status.h>
25 #include <string>
26 
27 #include "phNxpEse_Api.h"
28 
29 namespace android {
30 namespace hardware {
31 namespace secure_element {
32 namespace V1_1 {
33 namespace implementation {
34 
35 using ::android::sp;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::hardware::secure_element::V1_0::LogicalChannelResponse;
40 using ::android::hardware::secure_element::V1_0::SecureElementStatus;
41 using ::android::hardware::secure_element::V1_1::ISecureElement;
42 using ::android::hidl::base::V1_0::IBase;
43 
44 #ifndef MAX_LOGICAL_CHANNELS
45 #define MAX_LOGICAL_CHANNELS 0x04
46 #endif
47 #ifndef MIN_APDU_LENGTH
48 #define MIN_APDU_LENGTH 0x04
49 #endif
50 #ifndef DEFAULT_BASIC_CHANNEL
51 #define DEFAULT_BASIC_CHANNEL 0x00
52 #endif
53 #ifndef MAX_AID_LENGTH
54 #define MAX_AID_LENGTH 0x10
55 #endif
56 
57 struct SecureElement : public V1_1::ISecureElement,
58                        public hidl_death_recipient {
59   SecureElement();
60   Return<void> init(
61       const sp<V1_0::ISecureElementHalCallback>& clientCallback) override;
62   Return<void> init_1_1(
63       const sp<V1_1::ISecureElementHalCallback>& clientCallback) override;
64   Return<void> getAtr(getAtr_cb _hidl_cb) override;
65   Return<bool> isCardPresent() override;
66   Return<void> transmit(const hidl_vec<uint8_t>& data,
67                         transmit_cb _hidl_cb) override;
68   Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
69                                   openLogicalChannel_cb _hidl_cb) override;
70   Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2,
71                                 openBasicChannel_cb _hidl_cb) override;
72   Return<SecureElementStatus> closeChannel(uint8_t channelNumber) override;
73   void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) override;
74   void onStateChange(bool result, std::string reason);
75 
76  private:
77   uint8_t mOpenedchannelCount = 0;
78   bool mOpenedChannels[MAX_LOGICAL_CHANNELS];
79   static sp<V1_0::ISecureElementHalCallback> mCallbackV1_0;
80   static sp<V1_1::ISecureElementHalCallback> mCallbackV1_1;
81   Return<SecureElementStatus> seHalDeInit();
82   ESESTATUS seHalInit();
83   bool isSeInitialized();
84 };
85 
86 }  // namespace implementation
87 }  // namespace V1_1
88 }  // namespace secure_element
89 }  // namespace hardware
90 }  // namespace android
91 
92 #endif  // ANDROID_HARDWARE_SECURE_ELEMENT_V1_1_SECUREELEMENT_H
93