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 "SampleDriverAidlLimited"
18 
19 #include <aidl/android/hardware/neuralnetworks/BnDevice.h>
20 #include <aidl/android/hardware/neuralnetworks/IDevice.h>
21 #include <android-base/logging.h>
22 #include <android/binder_interface_utils.h>
23 #include <android/binder_manager.h>
24 #include <android/binder_process.h>
25 #include <nnapi/Types.h>
26 #include <nnapi/hal/aidl/Adapter.h>
27 
28 #include <algorithm>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 #include "CanonicalDevice.h"
35 #include "LimitedSupportDevice.h"
36 
37 namespace android::nn::sample {
38 namespace {
39 
40 using AidlBnDevice = ::aidl::android::hardware::neuralnetworks::BnDevice;
41 using AidlIDevice = ::aidl::android::hardware::neuralnetworks::IDevice;
42 using ::aidl::android::hardware::neuralnetworks::adapter::adapt;
43 
main()44 int main() {
45     constexpr size_t kNumberOfThreads = 4;
46     ABinderProcess_setThreadPoolMaxThreadCount(kNumberOfThreads);
47 
48     // Get the canonical interface objects. When developing the SL, you may want to make this
49     // "getDevices" instead.
50     const auto devices = getExampleLimitedDevices();
51 
52     // Adapt all canonical interface objects to AIDL interface objects.
53     std::vector<std::shared_ptr<AidlBnDevice>> aidlDevices;
54     aidlDevices.reserve(devices.size());
55     std::transform(devices.begin(), devices.end(), std::back_inserter(aidlDevices),
56                    [](const auto& device) { return adapt(device); });
57 
58     // Register all AIDL interface objects.
59     CHECK_EQ(devices.size(), aidlDevices.size());
60     for (size_t i = 0; i < aidlDevices.size(); ++i) {
61         const std::string name = devices[i]->getName();
62         if (name != "nnapi-sample_quant") {
63             continue;
64         }
65 
66         const std::string fqName = std::string(AidlIDevice::descriptor) + "/" + name;
67         const binder_status_t status =
68                 AServiceManager_addService(aidlDevices[i]->asBinder().get(), fqName.c_str());
69         if (status != STATUS_OK) {
70             LOG(ERROR) << "Could not register service " << name;
71             return 1;
72         }
73     }
74 
75     ABinderProcess_joinThreadPool();
76     LOG(ERROR) << "Service exited!";
77     return 1;
78 }
79 
80 }  // namespace
81 }  // namespace android::nn::sample
82 
main()83 int main() {
84     return android::nn::sample::main();
85 }
86