blob: 9bb83f09bcc9b4fb34eab113a9b33629cf223def [file] [log] [blame]
Andrew Scull17398422017-03-01 14:43:39 +00001/*
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>
Paul Crowleyde5b1722017-06-26 13:53:07 -070022#include <android-base/properties.h>
Andrew Scull17398422017-03-01 14:43:39 +000023#include <hidl/HidlTransportSupport.h>
24#include <utils/StrongPointer.h>
25
Andrew Scull17398422017-03-01 14:43:39 +000026// Select the implementation
Andrew Sculle416c0e2017-04-16 10:38:33 +010027#include <esecpp/NxpPn80tNqNci.h>
28using EseInterfaceImpl = android::NxpPn80tNqNci;
Andrew Scull17398422017-03-01 14:43:39 +000029
Andrew Scull6024d162017-04-17 18:58:12 +010030#include "Weaver.h"
31
Andrew Scull17398422017-03-01 14:43:39 +000032using android::OK;
33using android::sp;
34using android::status_t;
35using android::hardware::configureRpcThreadpool;
36using android::hardware::joinRpcThreadpool;
37
38using namespace std::chrono_literals;
39
Andrew Scull6024d162017-04-17 18:58:12 +010040// HALs
41using android::esed::Weaver;
42
Andrew Scull17398422017-03-01 14:43:39 +000043int main(int /* argc */, char** /* argv */) {
Paul Crowleyde5b1722017-06-26 13:53:07 -070044 LOG(INFO) << "Waiting for property...";
Jaekyun Seok25b37062017-09-14 16:12:03 +090045 android::base::WaitForProperty("init.svc.vendor.ese_load", "stopped");
Andrew Scull17398422017-03-01 14:43:39 +000046 LOG(INFO) << "Starting esed...";
47
48 // Open connection to the eSE
Andrew Scull6024d162017-04-17 18:58:12 +010049 EseInterfaceImpl ese;
Andrew Scull17398422017-03-01 14:43:39 +000050 uint32_t failCount = 0;
51 while (true) {
Andrew Scull17398422017-03-01 14:43:39 +000052 ese.init();
53 if (ese.open() < 0) {
54 std::string errMsg = "Failed to open connection to eSE";
55 if (ese.error()) {
56 errMsg += " (" + std::to_string(ese.error_code()) + "): " + ese.error_message();
57 } else {
58 errMsg += ": reason unknown";
59 }
60 LOG(ERROR) << errMsg;
61
62 // FIXME: this loop with sleep is a hack to avoid the process repeatedly crashing
63 ++failCount;
64 std::this_thread::sleep_for(failCount * 5s);
65 continue;
66 }
67 LOG(INFO) << "Opened connection to the eSE";
Andrew Scullb5071292017-03-23 11:44:27 +000068 break;
Andrew Scull17398422017-03-01 14:43:39 +000069 }
Will Drewrye47ba832017-04-18 17:08:09 -050070 // Close it until use.
71 ese.close();
72
Andrew Scull17398422017-03-01 14:43:39 +000073
74 // This will be a single threaded daemon. This is important as libese is not
75 // thread safe so we use binder to synchronize requests for us.
76 constexpr bool thisThreadWillJoinPool = true;
77 configureRpcThreadpool(1, thisThreadWillJoinPool);
78
Andrew Scull6024d162017-04-17 18:58:12 +010079 // Create Weaver HAL instance
80 sp<Weaver> weaver = new Weaver{ese};
Andrew Scull173657e2017-05-24 13:49:44 +010081 const status_t status = weaver->registerAsService();
Andrew Scull6024d162017-04-17 18:58:12 +010082 if (status != OK) {
83 LOG(ERROR) << "Failed to register Weaver as a service (status: " << status << ")";
84 }
85
Andrew Scull173657e2017-05-24 13:49:44 +010086
Andrew Scull17398422017-03-01 14:43:39 +000087 joinRpcThreadpool();
88 return -1; // Should never reach here
89}