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 "Service.h"
18 
19 #include <AndroidVersionUtil.h>
20 #include <aidl/android/hardware/neuralnetworks/IDevice.h>
21 #include <android/binder_auto_utils.h>
22 #include <android/binder_manager.h>
23 #include <android/binder_process.h>
24 
25 #include <nnapi/IDevice.h>
26 #include <nnapi/Result.h>
27 #include <nnapi/Types.h>
28 #include <nnapi/hal/ResilientDevice.h>
29 #include <string>
30 
31 #include "Device.h"
32 #include "Utils.h"
33 
34 namespace aidl::android::hardware::neuralnetworks::utils {
35 namespace {
36 
37 // Map the AIDL version of an IDevice to NNAPI canonical feature level.
getAidlServiceFeatureLevel(IDevice * service)38 nn::GeneralResult<nn::Version> getAidlServiceFeatureLevel(IDevice* service) {
39     CHECK(service != nullptr);
40     int aidlVersion;
41     const auto ret = service->getInterfaceVersion(&aidlVersion);
42     HANDLE_ASTATUS(ret) << "getInterfaceVersion failed";
43 
44     // For service AIDL versions greater than or equal to the AIDL library version that the runtime
45     // was built against, clamp it to the runtime AIDL library version.
46     aidlVersion = std::min(aidlVersion, IDevice::version);
47 
48     // Map stable AIDL versions to canonical versions.
49     auto version = aidlVersionToCanonicalVersion(aidlVersion);
50     if (!version.has_value()) {
51         return NN_ERROR() << "Unknown AIDL service version: " << aidlVersion;
52     }
53     return version.value();
54 }
55 
56 }  // namespace
57 
getDevice(const std::string & instanceName,::android::nn::Version::Level maxFeatureLevelAllowed)58 nn::GeneralResult<nn::SharedDevice> getDevice(
59         const std::string& instanceName, ::android::nn::Version::Level maxFeatureLevelAllowed) {
60     auto fullName = std::string(IDevice::descriptor) + "/" + instanceName;
61     hal::utils::ResilientDevice::Factory makeDevice =
62             [instanceName, name = std::move(fullName),
63              maxFeatureLevelAllowed](bool blocking) -> nn::GeneralResult<nn::SharedDevice> {
64         std::add_pointer_t<AIBinder*(const char*)> getService;
65         if (blocking) {
66             if (__builtin_available(android __NNAPI_AIDL_MIN_ANDROID_API__, *)) {
67                 getService = AServiceManager_waitForService;
68             } else {
69                 getService = AServiceManager_getService;
70             }
71         } else {
72             getService = AServiceManager_checkService;
73         }
74 
75         auto service = IDevice::fromBinder(ndk::SpAIBinder(getService(name.c_str())));
76         if (service == nullptr) {
77             return NN_ERROR()
78                    << (blocking ? "AServiceManager_waitForService (or AServiceManager_getService)"
79                                 : "AServiceManager_checkService")
80                    << " returned nullptr";
81         }
82         ABinderProcess_startThreadPool();
83         auto featureLevel = NN_TRY(getAidlServiceFeatureLevel(service.get()));
84         featureLevel.level = std::min(featureLevel.level, maxFeatureLevelAllowed);
85         return Device::create(instanceName, std::move(service), featureLevel);
86     };
87 
88     return hal::utils::ResilientDevice::create(std::move(makeDevice));
89 }
90 
91 }  // namespace aidl::android::hardware::neuralnetworks::utils
92