1 /*****************************************************************************
2  * Copyright ©2017-2019 Gemalto – a Thales Company. All rights Reserved.
3  *
4  * This copy is licensed under the Apache License, Version 2.0 (the "License");
5  * You may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *     http://www.apache.org/licenses/LICENSE-2.0 or https://www.apache.org/licenses/LICENSE-2.0.html
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and limitations under the License.
11 
12  ****************************************************************************/
13 #ifndef ANDROID_HARDWARE_SECURE_ELEMENT_V1_2_SECUREELEMENT_H
14 #define ANDROID_HARDWARE_SECURE_ELEMENT_V1_2_SECUREELEMENT_H
15 
16 #include <android/hardware/secure_element/1.0/types.h>
17 #include <android/hardware/secure_element/1.2/ISecureElement.h>
18 #include <hidl/MQDescriptor.h>
19 #include <hidl/Status.h>
20 
21 namespace android {
22 namespace hardware {
23 namespace secure_element {
24 namespace V1_2 {
25 namespace implementation {
26 
27 using ::android::hardware::hidl_array;
28 using ::android::hardware::hidl_memory;
29 using ::android::hardware::hidl_string;
30 using ::android::hardware::hidl_vec;
31 using ::android::hardware::Return;
32 using ::android::hardware::Void;
33 using ::android::sp;
34 using ::android::hardware::secure_element::V1_0::LogicalChannelResponse;
35 using ::android::hardware::secure_element::V1_0::SecureElementStatus;
36 using ::android::hardware::secure_element::V1_2::ISecureElement;
37 
38 
39 struct SecureElement : public ISecureElement , public hidl_death_recipient {
40     SecureElement(const char* ese_name);
41     Return<void> init(const sp<V1_0::ISecureElementHalCallback>& clientCallback) override;
42     Return<void> init_1_1(const sp<V1_1::ISecureElementHalCallback>& clientCallback) override;
43     Return<void> openLogicalChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openLogicalChannel_cb _hidl_cb) override;
44     Return<void> openBasicChannel(const hidl_vec<uint8_t>& aid, uint8_t p2, openBasicChannel_cb _hidl_cb) override;
45     Return<void> getAtr(getAtr_cb _hidl_cb) override;
46     Return<void> transmit(const hidl_vec<uint8_t>& data, transmit_cb _hidl_cb) override;
47     Return<bool> isCardPresent() override;
48     Return<::android::hardware::secure_element::V1_0::SecureElementStatus> closeChannel(uint8_t channelNumber) override;
49     Return<::android::hardware::secure_element::V1_0::SecureElementStatus> reset() override;
50 
51 
52     private:
53     uint8_t nbrOpenChannel = 0;
54     bool isBasicChannelOpen = false;
55     bool checkSeUp = false;
56     uint8_t atr[32];
57     uint8_t atr_size;
58     char config_filename[100];
59     char ese_flag_name[5];
60     static sp<V1_0::ISecureElementHalCallback> internalClientCallback;
61     static sp<V1_1::ISecureElementHalCallback> internalClientCallback_v1_1;
62     int initializeSE();
63     Return<::android::hardware::secure_element::V1_0::SecureElementStatus> deinitializeSE();
64     void serviceDied(uint64_t, const wp<IBase>&) override;
65     static int run_apdu(struct se_gto_ctx *ctx, const uint8_t *apdu, uint8_t *resp, int n, int verbose);
66     static int toint(char c);
67     static void dump_bytes(const char *pf, char sep, const uint8_t *p, int n, FILE *out);
68     int resetSE();
69     int openConfigFile(int verbose);
70     int parseConfigFile(FILE *f, int verbose);
71     void notify(bool state, const char *message);
72 };
73 
74 
75 }  // namespace implementation
76 }  // namespace V1_2
77 }  // namespace secure_element
78 }  // namespace hardware
79 }  // namespace android
80 
81 #endif  // ANDROID_HARDWARE_SECURE_ELEMENT_V1_2_SECUREELEMENT_H
82