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