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 "Callbacks.h"
18
19 #include "Conversions.h"
20 #include "PreparedModel.h"
21 #include "ProtectCallback.h"
22 #include "Utils.h"
23
24 #include <nnapi/IPreparedModel.h>
25 #include <nnapi/Result.h>
26 #include <nnapi/Types.h>
27
28 #include <utility>
29
30 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
31 // lifetimes across processes and for protecting asynchronous calls across AIDL.
32
33 namespace aidl::android::hardware::neuralnetworks::utils {
34 namespace {
35
36 // Converts the results of IDevice::prepareModel* to the NN canonical format. On success, this
37 // function returns with a non-null nn::SharedPreparedModel with a feature level of
38 // nn::Version::ANDROID_S. On failure, this function returns with the appropriate nn::GeneralError.
prepareModelCallback(ErrorStatus status,const std::shared_ptr<IPreparedModel> & preparedModel)39 nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
40 ErrorStatus status, const std::shared_ptr<IPreparedModel>& preparedModel) {
41 HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
42 return NN_TRY(PreparedModel::create(preparedModel));
43 }
44
45 } // namespace
46
notify(ErrorStatus status,const std::shared_ptr<IPreparedModel> & preparedModel)47 ndk::ScopedAStatus PreparedModelCallback::notify(
48 ErrorStatus status, const std::shared_ptr<IPreparedModel>& preparedModel) {
49 mData.put(prepareModelCallback(status, preparedModel));
50 return ndk::ScopedAStatus::ok();
51 }
52
notifyAsDeadObject()53 void PreparedModelCallback::notifyAsDeadObject() {
54 mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
55 }
56
get()57 PreparedModelCallback::Data PreparedModelCallback::get() {
58 return mData.take();
59 }
60
61 } // namespace aidl::android::hardware::neuralnetworks::utils
62