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 #define LOG_TAG "nxpese@1.2-service"
19 #include <aidl/android/hardware/nfc/INfc.h>
20 #include <android/hardware/nfc/1.2/INfc.h>
21 #include <android/hardware/secure_element/1.2/ISecureElement.h>
22 #include <hidl/LegacySupport.h>
23 #include <log/log.h>
24 #include <string.h>
25 #include <vendor/nxp/nxpese/1.0/INxpEse.h>
26
27 #include <regex>
28
29 #include "NxpEse.h"
30 #include "SecureElement.h"
31 #include "VirtualISO.h"
32 #ifdef NXP_BOOTTIME_UPDATE
33 #include "eSEClient.h"
34 #endif
35
36 #define MAX_NFC_GET_RETRY 30
37 #define NFC_GET_SERVICE_DELAY_MS 100
38
39 // Generated HIDL files
40 using android::OK;
41 using android::base::StringPrintf;
42 using android::hardware::configureRpcThreadpool;
43 using android::hardware::defaultPassthroughServiceImplementation;
44 using android::hardware::joinRpcThreadpool;
45 using android::hardware::registerPassthroughServiceImplementation;
46 using INfc = android::hardware::nfc::V1_2::INfc;
47 using android::hardware::secure_element::V1_2::ISecureElement;
48 using android::hardware::secure_element::V1_2::implementation::SecureElement;
49 using vendor::nxp::nxpese::V1_0::INxpEse;
50 using vendor::nxp::nxpese::V1_0::implementation::NxpEse;
51 using vendor::nxp::virtual_iso::V1_0::implementation::VirtualISO;
52 using INfcAidl = ::aidl::android::hardware::nfc::INfc;
53
54 using android::OK;
55 using android::sp;
56 using android::status_t;
57
58 std::string NFC_AIDL_HAL_SERVICE_NAME = "android.hardware.nfc.INfc/default";
59
waitForNFCHAL()60 static inline void waitForNFCHAL() {
61 int retry = 0;
62 android::sp<INfc> nfc_service = nullptr;
63 std::shared_ptr<INfcAidl> nfc_aidl_service = nullptr;
64
65 ALOGI("Waiting for NFC HAL .. ");
66 do {
67 ::ndk::SpAIBinder binder(
68 AServiceManager_checkService(NFC_AIDL_HAL_SERVICE_NAME.c_str()));
69 nfc_aidl_service = INfcAidl::fromBinder(binder);
70 if (nfc_aidl_service != nullptr) {
71 ALOGI("NFC HAL service is registered");
72 break;
73 }
74 /* Wait for 100 MS for HAL RETRY*/
75 usleep(NFC_GET_SERVICE_DELAY_MS * 1000);
76 } while (retry++ < MAX_NFC_GET_RETRY);
77
78 if (nfc_aidl_service == nullptr) {
79 ALOGE("Failed to get NFC AIDLHAL Service, trying to get HIDL service");
80 nfc_service = INfc::tryGetService();
81 if (nfc_service != nullptr) {
82 ALOGI("NFC HAL service is registered");
83 } else {
84 ALOGE("Failed to get NFC HAL Service");
85 }
86 }
87 }
88
main()89 int main() {
90 status_t status;
91
92 char terminalID[5] = "eSE1";
93 const char* SEterminal = "eSEx";
94 bool ret = false;
95
96 android::sp<ISecureElement> se_service = nullptr;
97 android::sp<INxpEse> nxp_se_service = nullptr;
98 android::sp<ISecureElement> virtual_iso_service = nullptr;
99
100 try {
101 waitForNFCHAL();
102 ALOGI("Secure Element HAL Service 1.2 is starting.");
103 se_service = new SecureElement();
104 if (se_service == nullptr) {
105 ALOGE("Can not create an instance of Secure Element HAL Iface, exiting.");
106 goto shutdown;
107 }
108 configureRpcThreadpool(1, true /*callerWillJoin*/);
109
110 #ifdef NXP_BOOTTIME_UPDATE
111 checkEseClientUpdate();
112 ret = geteSETerminalId(terminalID);
113 #else
114 ret = true;
115 #endif
116 ALOGI("Terminal val = %s", terminalID);
117 if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
118 ALOGI("Terminal ID found");
119 status = se_service->registerAsService(terminalID);
120
121 if (status != OK) {
122 ALOGE("Could not register service for Secure Element HAL Iface (%d).",
123 status);
124 goto shutdown;
125 }
126 ALOGI("Secure Element Service is ready");
127
128 ALOGI("NXP Secure Element Extn Service 1.0 is starting.");
129 nxp_se_service = new NxpEse();
130 if (nxp_se_service == nullptr) {
131 ALOGE(
132 "Can not create an instance of NXP Secure Element Extn "
133 "Iface,exiting.");
134 goto shutdown;
135 }
136 status = nxp_se_service->registerAsService();
137 if (status != OK) {
138 ALOGE(
139 "Could not register service for Power Secure Element Extn Iface "
140 "(%d).",
141 status);
142 goto shutdown;
143 }
144 ALOGI("Secure Element Service is ready");
145 }
146
147 #ifdef NXP_VISO_ENABLE
148 ALOGI("Virtual ISO HAL Service 1.0 is starting.");
149 virtual_iso_service = new VirtualISO();
150 if (virtual_iso_service == nullptr) {
151 ALOGE("Can not create an instance of Virtual ISO HAL Iface, exiting.");
152 goto shutdown;
153 }
154 #ifdef NXP_BOOTTIME_UPDATE
155 ret = geteUICCTerminalId(terminalID);
156 #else
157 strncpy(terminalID, "eSE2", 4);
158 ret = true;
159 #endif
160 if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
161 status = virtual_iso_service->registerAsService(terminalID);
162 if (status != OK) {
163 ALOGE("Could not register service for Virtual ISO HAL Iface (%d).",
164 status);
165 goto shutdown;
166 }
167 }
168
169 ALOGI("Virtual ISO: Secure Element Service is ready");
170 #endif
171 #ifdef NXP_BOOTTIME_UPDATE
172 perform_eSEClientUpdate();
173 #endif
174 joinRpcThreadpool();
175 } catch (const std::length_error& e) {
176 ALOGE("Length Exception occurred = %s ", e.what());
177 } catch (const std::__1::ios_base::failure& e) {
178 ALOGE("ios failure Exception occurred = %s ", e.what());
179 } catch (std::__1::regex_error& e) {
180 ALOGE("Regex Exception occurred = %s ", e.what());
181 }
182 // Should not pass this line
183 shutdown:
184 // In normal operation, we don't expect the thread pool to exit
185 ALOGE("Secure Element Service is shutting down");
186 return 1;
187 }
188