Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Handle case where NN AIDL callback is null in IDevice::prepareModel*
Browse files Browse the repository at this point in the history
Prior to this change, if IDevice::prepareModel* was passed a null
callback, the code would still attempt to call "notify" on the callback
to return the error to the client. This CL ensures the "notify" method
will not be invoked if the callback is null.

Bug: N/A
Test: mma
Test: presubmit
Change-Id: I4a15d02c4879a0261ec26cc0e7a47d0a4da86b8b
  • Loading branch information
Michael Butler committed Apr 21, 2022
1 parent c5fc884 commit d6f6d01
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions neuralnetworks/utils/adapter/aidl/src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,26 @@ std::shared_ptr<PreparedModel> adaptPreparedModel(nn::SharedPreparedModel prepar
return ndk::SharedRefBase::make<PreparedModel>(std::move(preparedModel));
}

void notify(IPreparedModelCallback* callback, ErrorStatus status,
const std::shared_ptr<IPreparedModel>& preparedModel) {
if (callback != nullptr) {
const auto ret = callback->notify(status, preparedModel);
if (!ret.isOk()) {
LOG(ERROR) << "IPreparedModelCallback::notify failed with " << ret.getDescription();
}
}
}

void notify(IPreparedModelCallback* callback, PrepareModelResult result) {
if (!result.has_value()) {
const auto& [message, status] = result.error();
LOG(ERROR) << message;
const auto aidlCode = utils::convert(status).value_or(ErrorStatus::GENERAL_FAILURE);
callback->notify(aidlCode, nullptr);
notify(callback, aidlCode, nullptr);
} else {
auto preparedModel = std::move(result).value();
auto aidlPreparedModel = adaptPreparedModel(std::move(preparedModel));
callback->notify(ErrorStatus::NONE, std::move(aidlPreparedModel));
notify(callback, ErrorStatus::NONE, std::move(aidlPreparedModel));
}
}

Expand Down Expand Up @@ -284,7 +294,7 @@ ndk::ScopedAStatus Device::prepareModel(const Model& model, ExecutionPreference
if (!result.has_value()) {
const auto& [message, code] = result.error();
const auto aidlCode = utils::convert(code).value_or(ErrorStatus::GENERAL_FAILURE);
callback->notify(aidlCode, nullptr);
notify(callback.get(), aidlCode, nullptr);
return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
static_cast<int32_t>(aidlCode), message.c_str());
}
Expand All @@ -300,7 +310,7 @@ ndk::ScopedAStatus Device::prepareModelFromCache(
if (!result.has_value()) {
const auto& [message, code] = result.error();
const auto aidlCode = utils::convert(code).value_or(ErrorStatus::GENERAL_FAILURE);
callback->notify(aidlCode, nullptr);
notify(callback.get(), aidlCode, nullptr);
return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
static_cast<int32_t>(aidlCode), message.c_str());
}
Expand All @@ -317,7 +327,7 @@ ndk::ScopedAStatus Device::prepareModelWithConfig(
if (!result.has_value()) {
const auto& [message, code] = result.error();
const auto aidlCode = utils::convert(code).value_or(ErrorStatus::GENERAL_FAILURE);
callback->notify(aidlCode, nullptr);
notify(callback.get(), aidlCode, nullptr);
return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
static_cast<int32_t>(aidlCode), message.c_str());
}
Expand Down

0 comments on commit d6f6d01

Please sign in to comment.