1 /******************************************************************************
2  *
3  *  Copyright 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 #pragma once
19 
20 #include <aidl/android/hardware/secure_element/BnSecureElement.h>
21 #include <aidl/android/hardware/secure_element/ISecureElement.h>
22 #include <aidl/android/hardware/secure_element/ISecureElementCallback.h>
23 #include <android-base/logging.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26 #include <hardware/hardware.h>
27 #include <log/log.h>
28 
29 #include "phNxpEse_Api.h"
30 
31 #define SESTATUS_SUCCESS 0
32 
33 namespace aidl {
34 namespace vendor {
35 namespace nxp {
36 namespace virtual_iso {
37 
38 using ::aidl::android::hardware::secure_element::ISecureElementCallback;
39 using ::aidl::android::hardware::secure_element::LogicalChannelResponse;
40 using ::ndk::ICInterface;
41 using ndk::ScopedAStatus;
42 
43 #ifndef DEFAULT_BASIC_CHANNEL
44 #define DEFAULT_BASIC_CHANNEL 0x00
45 #endif
46 
47 struct VirtualISO
48     : public ::aidl::android::hardware::secure_element::BnSecureElement {
49   VirtualISO();
50   ::ndk::ScopedAStatus closeChannel(int8_t in_channelNumber) override;
51   ::ndk::ScopedAStatus getAtr(std::vector<uint8_t>* _aidl_return) override;
52   ::ndk::ScopedAStatus init(
53       const std::shared_ptr<
54           ::aidl::android::hardware::secure_element::ISecureElementCallback>&
55           in_clientCallback) override;
56   ::ndk::ScopedAStatus isCardPresent(bool* _aidl_return) override;
57   ::ndk::ScopedAStatus openBasicChannel(
58       const std::vector<uint8_t>& in_aid, int8_t in_p2,
59       std::vector<uint8_t>* _aidl_return) override;
60   ::ndk::ScopedAStatus openLogicalChannel(
61       const std::vector<uint8_t>& in_aid, int8_t in_p2,
62       ::aidl::android::hardware::secure_element::LogicalChannelResponse*
63           _aidl_return) override;
64   ::ndk::ScopedAStatus reset() override;
65   ::ndk::ScopedAStatus transmit(const std::vector<uint8_t>& in_data,
66                                 std::vector<uint8_t>* _aidl_return) override;
67 
68   static void NotifySeWaitExtension(phNxpEse_wtxState state);
69 
70  private:
71   uint8_t mMaxChannelCount;
72   uint8_t mOpenedchannelCount = 0;
73   bool mIsEseInitialized = false;
74   static std::vector<bool> mOpenedChannels;
75 
76   static std::shared_ptr<ISecureElementCallback> mCallback;
77 
78   int seHalDeInit();
79   ESESTATUS seHalInit();
80   int internalCloseChannel(uint8_t channelNumber);
81 };
82 
83 }  // namespace virtual_iso
84 }  // namespace nxp
85 }  // namespace vendor
86 }  // namespace aidl
87