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 #include <aidl/android/hardware/nfc/INfc.h>
19 #include <android/binder_manager.h>
20 #include <android/binder_process.h>
21 #include <android/hardware/nfc/1.2/INfc.h>
22
23 #include "SecureElement.h"
24 #include "VirtualISO.h"
25 #include "phNxpConfig.h"
26
27 #define MAX_NFC_GET_RETRY 30
28 #define NFC_GET_SERVICE_DELAY_MS 100
29 #define TERMINAL_LEN 5
30 #define NAME_NXP_SPI_SE_TERMINAL_NUM "NXP_SPI_SE_TERMINAL_NUM"
31 #define NAME_NXP_VISO_SE_TERMINAL_NUM "NXP_VISO_SE_TERMINAL_NUM"
32
33 using INfc = android::hardware::nfc::V1_2::INfc;
34 using INfcAidl = ::aidl::android::hardware::nfc::INfc;
35 using ::aidl::android::hardware::secure_element::SecureElement;
36 using VirtualISO = aidl::vendor::nxp::virtual_iso::VirtualISO;
37
38 using android::OK;
39 using android::sp;
40 using android::status_t;
41
42 std::string NFC_AIDL_HAL_SERVICE_NAME = "android.hardware.nfc.INfc/default";
43
waitForNFCHAL()44 static inline void waitForNFCHAL() {
45 int retry = 0;
46 android::sp<INfc> nfc_service = nullptr;
47 std::shared_ptr<INfcAidl> nfc_aidl_service = nullptr;
48
49 ALOGI("Waiting for NFC HAL .. ");
50 do {
51 ::ndk::SpAIBinder binder(
52 AServiceManager_checkService(NFC_AIDL_HAL_SERVICE_NAME.c_str()));
53 nfc_aidl_service = INfcAidl::fromBinder(binder);
54 if (nfc_aidl_service != nullptr) {
55 ALOGI("NFC HAL service is registered");
56 break;
57 }
58 /* Wait for 100 MS for HAL RETRY*/
59 usleep(NFC_GET_SERVICE_DELAY_MS * 1000);
60 } while (retry++ < MAX_NFC_GET_RETRY);
61
62 if (nfc_aidl_service == nullptr) {
63 ALOGE("Failed to get NFC AIDLHAL Service, trying to get HIDL service");
64 nfc_service = INfc::tryGetService();
65 if (nfc_service != nullptr) {
66 ALOGI("NFC HAL service is registered");
67 } else {
68 ALOGE("Failed to get NFC HAL Service");
69 }
70 }
71 }
72
main()73 int main() {
74 char terminalID[5] = "eSE1";
75 const char* SEterminal = "eSEx";
76 bool ret = false;
77
78 ALOGI("Secure Element AIDL HAL Service starting up");
79 if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
80 ALOGE("failed to set thread pool max thread count");
81 return EXIT_FAILURE;
82 }
83
84 waitForNFCHAL();
85 ALOGI("Secure Element AIDL HAL Service starting up");
86 std::shared_ptr<SecureElement> se_service =
87 ndk::SharedRefBase::make<SecureElement>();
88 std::shared_ptr<VirtualISO> virtual_iso_service = nullptr;
89
90 if (GetNxpStrValue(NAME_NXP_SPI_SE_TERMINAL_NUM, terminalID, TERMINAL_LEN)) {
91 LOG(ERROR) << "eSETerminalId found";
92 ALOGE("eSETerminalId found val = %s ", terminalID);
93
94 ret = true;
95 }
96 ALOGI("Terminal val = %s", terminalID);
97 if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
98 ALOGI("Terminal ID found");
99 const std::string seInstName =
100 std::string() + SecureElement::descriptor + "/" + terminalID;
101 binder_status_t status = AServiceManager_addService(
102 se_service->asBinder().get(), seInstName.c_str());
103 if (status != OK) {
104 ALOGE("Could not register service for Secure Element HAL Iface (%d).",
105 status);
106 goto shutdown;
107 }
108 ALOGI("Secure Element Service is ready");
109 }
110 #ifdef NXP_VISO_ENABLE
111 ALOGI("Virtual ISO HAL Service 1.0 is starting.");
112 virtual_iso_service = ndk::SharedRefBase::make<VirtualISO>();
113
114 ret = false;
115 if (GetNxpStrValue(NAME_NXP_VISO_SE_TERMINAL_NUM, terminalID, TERMINAL_LEN)) {
116 ALOGE("eUICCTerminalId found val = %s ", terminalID);
117 ret = true;
118 }
119 if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
120 const std::string vISO_InstName =
121 std::string() + SecureElement::descriptor + "/" + terminalID;
122 binder_status_t status = AServiceManager_addService(
123 virtual_iso_service->asBinder().get(), vISO_InstName.c_str());
124
125 if (status != OK) {
126 ALOGE("Could not register service for Virtual ISO HAL Iface (%d).",
127 status);
128 goto shutdown;
129 }
130 }
131
132 ALOGI("Virtual ISO: Secure Element Service is ready");
133 #endif
134 ABinderProcess_joinThreadPool();
135 // Should not pass this line
136 shutdown:
137 // In normal operation, we don't expect the thread pool to exit
138 ALOGE("Secure Element Service is shutting down");
139 return EXIT_FAILURE;
140 }