1 /******************************************************************************
2  *
3  *  Copyright 2018 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.0-service"
19 #include <android-base/stringprintf.h>
20 #include <android/hardware/secure_element/1.0/ISecureElement.h>
21 #include <base/logging.h>
22 #include <vendor/nxp/nxpese/1.0/INxpEse.h>
23 #include "VirtualISO.h"
24 
25 #include <hidl/LegacySupport.h>
26 #include <string.h>
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_0::ISecureElement;
39 using android::hardware::secure_element::V1_0::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.0 is starting.");
60   se_service = new SecureElement();
61   if (se_service == nullptr) {
62     LOG(ERROR) << StringPrintf(
63         "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       LOG(ERROR) << StringPrintf(
77           "Could not register service for Secure Element HAL Iface (%d).",
78           status);
79       goto shutdown;
80     }
81     ALOGI("Secure Element Service is ready");
82 
83     ALOGI("NXP Secure Element Extn Service 1.0 is starting.");
84     nxp_se_service = new NxpEse();
85     if (nxp_se_service == nullptr) {
86       LOG(ERROR) << StringPrintf(
87           "Can not create an instance of NXP Secure Element Extn "
88           "Iface,exiting.");
89       goto shutdown;
90     }
91     status = nxp_se_service->registerAsService();
92     if (status != OK) {
93       LOG(ERROR) << StringPrintf(
94           "Could not register service for Power Secure Element Extn Iface "
95           "(%d).",
96           status);
97       goto shutdown;
98     }
99     ALOGI("Secure Element Service is ready");
100   }
101 
102   ALOGI("Virtual ISO HAL Service 1.0 is starting.");
103   virtual_iso_service = new VirtualISO();
104   if (virtual_iso_service == nullptr) {
105     LOG(ERROR) << StringPrintf(
106         "Can not create an instance of Virtual ISO HAL Iface, exiting.");
107     goto shutdown;
108   }
109   ret = geteUICCTerminalId(terminalID);
110   if ((ret) && (strncmp(SEterminal, terminalID, 3) == 0)) {
111     status = virtual_iso_service->registerAsService(terminalID);
112     if (status != OK) {
113       LOG(ERROR) << StringPrintf(
114           "Could not register service for Virtual ISO HAL Iface (%d).", status);
115       goto shutdown;
116     }
117   }
118 
119   ALOGI("Virtual ISO: Secure Element Service is ready");
120   perform_eSEClientUpdate();
121   joinRpcThreadpool();
122 // Should not pass this line
123 shutdown:
124   // In normal operation, we don't expect the thread pool to exit
125   LOG(ERROR) << StringPrintf("Secure Element Service is shutting down");
126   return 1;
127 }
128