1 /*
2  * Copyright (C) 2020 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 "Service.h"
18 
19 #include <nnapi/IDevice.h>
20 #include <nnapi/Result.h>
21 #include <nnapi/Types.h>
22 #include <nnapi/hal/ResilientDevice.h>
23 #include <string>
24 #include "Device.h"
25 
26 namespace android::hardware::neuralnetworks::V1_3::utils {
27 
28 nn::GeneralResult<nn::SharedDevice> getDevice(const std::string& name) {
29     hal::utils::ResilientDevice::Factory makeDevice =
30             [name](bool blocking) -> nn::GeneralResult<nn::SharedDevice> {
31         auto service = blocking ? IDevice::getService(name) : IDevice::tryGetService(name);
32         if (service == nullptr) {
33             return NN_ERROR() << (blocking ? "getService" : "tryGetService") << " returned nullptr";
34         }
35         return Device::create(name, std::move(service));
36     };
37 
38     return hal::utils::ResilientDevice::create(std::move(makeDevice));
39 }
40 
41 }  // namespace android::hardware::neuralnetworks::V1_3::utils
42