1 /******************************************************************************
2  *
3  *  Copyright 2018-2021 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 #define LOG_TAG "nxpese@1.1-service"
19 #include <android/hardware/secure_element/1.1/ISecureElement.h>
20 #include <log/log.h>
21 #include <vendor/nxp/nxpese/1.0/INxpEse.h>
22 #include "VirtualISO.h"
23 
24 #include <hidl/LegacySupport.h>
25 #include <string.h>
26 #include <regex>
27 #include "NxpEse.h"
28 #include "SecureElement.h"
29 #include "eSEClient.h"
30 
31 // Generated HIDL files
32 using android::OK;
33 using android::base::StringPrintf;
34 using android::hardware::configureRpcThreadpool;
35 using android::hardware::defaultPassthroughServiceImplementation;
36 using android::hardware::joinRpcThreadpool;
37 using android::hardware::registerPassthroughServiceImplementation;
38 using android::hardware::secure_element::V1_1::ISecureElement;
39 using android::hardware::secure_element::V1_1::implementation::SecureElement;
40 using vendor::nxp::nxpese::V1_0::INxpEse;
41 using vendor::nxp::nxpese::V1_0::implementation::NxpEse;
42 using vendor::nxp::virtual_iso::V1_0::implementation::VirtualISO;
43 
44 using android::OK;
45 using android::sp;
46 using android::status_t;
47 
main()48 int main() {
49   status_t status;
50 
51   char terminalID[5];
52   const char* SEterminal = "eSEx";
53   bool ret = false;
54 
55   android::sp<ISecureElement> se_service = nullptr;
56   android::sp<INxpEse> nxp_se_service = nullptr;
57   android::sp<ISecureElement> virtual_iso_service = nullptr;
58 
59   ALOGI("Secure Element HAL Service 1.1 is starting.");
60   try {
61     se_service = new SecureElement();
62     if (se_service == nullptr) {
63       ALOGE("Can not create an instance of Secure Element HAL Iface, exiting.");
64       goto shutdown;
65     }
66     configureRpcThreadpool(1, true /*callerWillJoin*/);
67 
68     checkEseClientUpdate();
69     ret = geteSETerminalId(terminalID);
70     ALOGI("Terminal val = %s", terminalID);
71     if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
72       ALOGI("Terminal ID found");
73       status = se_service->registerAsService(terminalID);
74 
75       if (status != OK) {
76         ALOGE("Could not register service for Secure Element HAL Iface (%d).",
77               status);
78         goto shutdown;
79       }
80       ALOGI("Secure Element Service is ready");
81 
82       ALOGI("NXP Secure Element Extn Service 1.0 is starting.");
83       nxp_se_service = new NxpEse();
84       if (nxp_se_service == nullptr) {
85         ALOGE(
86             "Can not create an instance of NXP Secure Element Extn "
87             "Iface,exiting.");
88         goto shutdown;
89       }
90       status = nxp_se_service->registerAsService();
91       if (status != OK) {
92         ALOGE(
93             "Could not register service for Power Secure Element Extn Iface "
94             "(%d).",
95             status);
96         goto shutdown;
97       }
98       ALOGI("Secure Element Service is ready");
99     }
100 
101     ALOGI("Virtual ISO HAL Service 1.0 is starting.");
102     virtual_iso_service = new VirtualISO();
103     if (virtual_iso_service == nullptr) {
104       ALOGE("Can not create an instance of Virtual ISO HAL Iface, exiting.");
105       goto shutdown;
106     }
107     ret = geteUICCTerminalId(terminalID);
108     if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
109       status = virtual_iso_service->registerAsService(terminalID);
110       if (status != OK) {
111         ALOGE("Could not register service for Virtual ISO HAL Iface (%d).",
112               status);
113         goto shutdown;
114       }
115     }
116 
117     ALOGI("Virtual ISO: Secure Element Service is ready");
118     perform_eSEClientUpdate();
119     joinRpcThreadpool();
120   } catch (const std::length_error& e) {
121     ALOGE("Length Exception occurred = %s ", e.what());
122   } catch (const std::__1::ios_base::failure& e) {
123     ALOGE("ios failure Exception occurred = %s ", e.what());
124   } catch (std::__1::regex_error& e) {
125     ALOGE("Regex Exception occurred = %s ", e.what());
126   }
127 // Should not pass this line
128 shutdown:
129   // In normal operation, we don't expect the thread pool to exit
130   ALOGE("Secure Element Service is shutting down");
131   return 1;
132 }
133