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 #define LOG_TAG "SampleDriverLimited"
18 
19 #include <android-base/logging.h>
20 #include <android/hardware/neuralnetworks/1.3/IDevice.h>
21 #include <hidl/HidlTransportSupport.h>
22 #include <nnapi/Types.h>
23 #include <nnapi/hal/Adapter.h>
24 
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "CanonicalDevice.h"
32 #include "LimitedSupportDevice.h"
33 
34 namespace android::nn::sample {
35 namespace {
36 
main()37 int main() {
38     constexpr size_t kNumberOfThreads = 4;
39     hardware::configureRpcThreadpool(kNumberOfThreads, true);
40 
41     // Get the canonical interface objects. When developing the SL, you may want to make this
42     // "getDevices" instead.
43     const auto devices = getExampleLimitedDevices();
44 
45     // Adapt all canonical interface objects to HIDL interface objects.
46     std::vector<sp<hardware::neuralnetworks::V1_3::IDevice>> hidlDevices;
47     hidlDevices.reserve(devices.size());
48     std::transform(
49             devices.begin(), devices.end(), std::back_inserter(hidlDevices),
50             [](const auto& device) { return hardware::neuralnetworks::adapter::adapt(device); });
51 
52     // Register all HIDL interface objects.
53     CHECK_EQ(devices.size(), hidlDevices.size());
54     for (size_t i = 0; i < hidlDevices.size(); ++i) {
55         if (hidlDevices[i]->registerAsService(devices[i]->getName()) != android::OK) {
56             LOG(ERROR) << "Could not register service " << devices[i]->getName();
57             return 1;
58         }
59     }
60 
61     hardware::joinRpcThreadpool();
62     LOG(ERROR) << "Service exited!";
63     return 1;
64 }
65 
66 }  // namespace
67 }  // namespace android::nn::sample
68 
main()69 int main() {
70     return android::nn::sample::main();
71 }
72