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 #define LOG_TAG "ShellServiceSample"
18 
19 #include <NeuralNetworksShim.h>
20 #include <aidl/android/hardware/neuralnetworks/BnDevice.h>
21 #include <android-base/logging.h>
22 #include <android-base/scopeguard.h>
23 #include <android/binder_enums.h>
24 #include <android/binder_manager.h>
25 #include <android/binder_process.h>
26 #include <dlfcn.h>
27 #include <nnapi/hal/aidl/InvalidDevice.h>
28 
29 #include <algorithm>
30 #include <limits>
31 #include <string>
32 #include <type_traits>
33 #include <unordered_map>
34 #include <utility>
35 #include <vector>
36 
37 typedef struct NnApiSLDriverImpl NnApiSLDriverImpl;
38 
39 namespace aidl::android::hardware::neuralnetworks {
40 namespace {
41 
42 struct Names {
43     std::string driverName;
44     std::string serviceName;
45 };
46 
registerInvalidDevices(const std::vector<Names> & names)47 void registerInvalidDevices(const std::vector<Names>& names) {
48     for (const auto& [_, name] : names) {
49         const auto invalidDevice = InvalidDevice::create();
50         const std::string instance = std::string(IDevice::descriptor) + "/" + name;
51         LOG(INFO) << "Attempting service registration for " << instance;
52         const auto status = AServiceManager_registerLazyService(invalidDevice->asBinder().get(),
53                                                                 instance.c_str());
54         if (status != STATUS_OK) {
55             LOG(ERROR) << "AServiceManager_registerLazyService failed for " << name
56                        << ", error code " << status;
57             return;
58         }
59     }
60     ABinderProcess_setThreadPoolMaxThreadCount(15);
61     ABinderProcess_joinThreadPool();
62 }
63 
registerDevices(const std::string & driverPath,const std::vector<Names> & devices)64 int registerDevices(const std::string& driverPath, const std::vector<Names>& devices) {
65     // Load support library.
66     void* libHandle = dlopen(driverPath.c_str(), RTLD_LAZY | RTLD_LOCAL | RTLD_NODELETE);
67     if (libHandle == nullptr) {
68         LOG(ERROR) << "Failed to load sample SL driver: " << driverPath;
69         registerInvalidDevices(devices);
70         return EXIT_FAILURE;
71     }
72 
73     // Load support library entry point.
74     using GetSlDriverImplFn = std::add_pointer_t<NnApiSLDriverImpl*()>;
75     GetSlDriverImplFn getSlDriverImpl = reinterpret_cast<GetSlDriverImplFn>(
76             dlsym(libHandle, "ANeuralNetworks_getSLDriverImpl"));
77     if (getSlDriverImpl == nullptr) {
78         LOG(ERROR) << "Failed to find ANeuralNetworks_getSLDriverImpl symbol in: " << driverPath;
79         registerInvalidDevices(devices);
80         return EXIT_FAILURE;
81     }
82 
83     // Call support library entry point to obtain functionality.
84     NnApiSLDriverImpl* impl = getSlDriverImpl();
85     if (impl == nullptr) {
86         LOG(ERROR) << "ANeuralNetworks_getSLDriverImpl returned nullptr: " << driverPath;
87         registerInvalidDevices(devices);
88         return EXIT_FAILURE;
89     }
90 
91     ANeuralNetworksShimRegistrationParams* params;
92     ANeuralNetworksShimRegistrationParams_create(impl, &params);
93     const auto guardParams = ::android::base::make_scope_guard(
94             [params] { ANeuralNetworksShimRegistrationParams_free(params); });
95 
96     // The default is 15, use more only if there's more devices exposed.
97     ANeuralNetworksShimRegistrationParams_setNumberOfListenerThreads(params, 15);
98     ANeuralNetworksShimRegistrationParams_registerAsLazyService(params, /*asLazy=*/true);
99     ANeuralNetworksShimRegistrationParams_fallbackToMinimumSupportDevice(params, /*fallback=*/true);
100 
101     for (const auto& device : devices) {
102         ANeuralNetworksShimDeviceInfo* deviceInfo;
103         ANeuralNetworksShimDeviceInfo_create(&deviceInfo, device.driverName.c_str(),
104                                              device.serviceName.c_str());
105         const auto guardDeviceInfo = ::android::base::make_scope_guard(
106                 [deviceInfo] { ANeuralNetworksShimDeviceInfo_free(deviceInfo); });
107 
108         ANeuralNetworksShimRegistrationParams_addDeviceInfo(params, deviceInfo);
109     }
110 
111     // Register the support library as a binderized AIDL service.
112     auto result = ANeuralNetworksShim_registerSupportLibraryService(params);
113     LOG(ERROR) << "ANeuralNetworksShim_registerSupportLibraryService returned with error status: "
114                << result;
115 
116     return EXIT_FAILURE;
117 }
118 
119 }  // namespace
120 }  // namespace aidl::android::hardware::neuralnetworks
121 
122 using aidl::android::hardware::neuralnetworks::Names;
123 using aidl::android::hardware::neuralnetworks::registerDevices;
124 
main()125 int main() {
126     const std::string driverPath = "/vendor/lib64/neuralnetworks_sample_sl_driver_prebuilt.so";
127 
128     const std::vector<Names> devicesToRegister = {
129             {.driverName = "nnapi-sample_sl", .serviceName = "nnapi-sample_sl_updatable"},
130     };
131 
132     return registerDevices(driverPath, devicesToRegister);
133 }
134