1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <chrono>
18 #include <string>
19 #include <thread>
20 
21 #include <android-base/logging.h>
22 #include <hidl/HidlTransportSupport.h>
23 #include <utils/StrongPointer.h>
24 
25 #include "libese.h"
26 
27 // Select the implementation
28 #include "pn81a/pn81a.h"
29 using EseInterfaceImpl = android::esed::pn81a::EseInterface;
30 
31 using android::OK;
32 using android::sp;
33 using android::status_t;
34 using android::hardware::configureRpcThreadpool;
35 using android::hardware::joinRpcThreadpool;
36 
37 using namespace std::chrono_literals;
38 
main(int,char **)39 int main(int /* argc */, char** /* argv */) {
40     LOG(INFO) << "Starting esed...";
41 
42     // Open connection to the eSE
43     uint32_t failCount = 0;
44     while (true) {
45         EseInterfaceImpl ese;
46         ese.init();
47         if (ese.open() < 0) {
48             std::string errMsg = "Failed to open connection to eSE";
49             if (ese.error()) {
50                 errMsg += " (" + std::to_string(ese.error_code()) + "): " + ese.error_message();
51             } else {
52                 errMsg += ": reason unknown";
53             }
54             LOG(ERROR) << errMsg;
55 
56             // FIXME: this loop with sleep is a hack to avoid the process repeatedly crashing
57             ++failCount;
58             std::this_thread::sleep_for(failCount * 5s);
59             continue;
60         }
61         LOG(INFO) << "Opened connection to the eSE";
62         break;
63     }
64 
65     // This will be a single threaded daemon. This is important as libese is not
66     // thread safe so we use binder to synchronize requests for us.
67     constexpr bool thisThreadWillJoinPool = true;
68     configureRpcThreadpool(1, thisThreadWillJoinPool);
69 
70     // -- Instantiate other applet HALs here --
71 
72     joinRpcThreadpool();
73     return -1; // Should never reach here
74 }
75