1 /*
2  * Copyright (C) 2021 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 <android-base/logging.h>
18 #include <android-base/properties.h>
19 #include <android/binder_manager.h>
20 #include <android/binder_process.h>
21 #include <dlfcn.h>
22 
23 #include "Nfc.h"
24 
25 #if defined(ST_LIB_32)
26 #define VENDOR_LIB_PATH "/vendor/lib/"
27 #else
28 #define VENDOR_LIB_PATH "/vendor/lib64/"
29 #endif
30 #define VENDOR_LIB_EXT ".so"
31 
32 using ::aidl::android::hardware::nfc::Nfc;
33 
34 typedef int (*STEseReset)(void);
35 
main()36 int main() {
37   void* stdll = nullptr;
38   LOG(INFO) << "NFC AIDL HAL Service is starting up";
39 
40   std::string valueStr =
41       android::base::GetProperty("persist.vendor.nfc.streset", "");
42   if (valueStr.length() > 0) {
43     stdll = dlopen(valueStr.c_str(), RTLD_NOW);
44     if (!stdll) {
45       valueStr = VENDOR_LIB_PATH + valueStr + VENDOR_LIB_EXT;
46       stdll = dlopen(valueStr.c_str(), RTLD_NOW);
47     }
48     if (stdll) {
49       LOG(INFO) << "ST NFC HAL STReset starting.";
50       STEseReset fn = (STEseReset)dlsym(stdll, "boot_reset");
51       if (fn) {
52         int ret = fn();
53         LOG(INFO) << "STReset Result= " << ret;
54       }
55       LOG(INFO) << ("ST NFC HAL STReset Done.");
56     }
57   }
58   if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
59     LOG(INFO) << "failed to set thread pool max thread count";
60     return 1;
61   }
62   std::shared_ptr<Nfc> nfc_service = ndk::SharedRefBase::make<Nfc>();
63 
64   const std::string instance = std::string() + Nfc::descriptor + "/default";
65   binder_status_t status = AServiceManager_addService(
66       nfc_service->asBinder().get(), instance.c_str());
67   CHECK(status == STATUS_OK);
68   ABinderProcess_joinThreadPool();
69   return 0;
70 }
71