Merge "Audio: setParam improve status_t to Result consistency"

This commit is contained in:
Kevin Rocard 2018-04-23 21:41:14 +00:00 committed by Gerrit Code Review
commit 74e1cbb094
4 changed files with 44 additions and 40 deletions

View file

@ -48,23 +48,7 @@ Device::~Device() {
}
Result Device::analyzeStatus(const char* funcName, int status) {
if (status != 0) {
ALOGW("Device %p %s: %s", mDevice, funcName, strerror(-status));
}
switch (status) {
case 0:
return Result::OK;
case -EINVAL:
return Result::INVALID_ARGUMENTS;
case -ENODATA:
return Result::INVALID_STATE;
case -ENODEV:
return Result::NOT_INITIALIZED;
case -ENOSYS:
return Result::NOT_SUPPORTED;
default:
return Result::INVALID_STATE;
}
return util::analyzeStatus("Device", funcName, status);
}
void Device::closeInputStream(audio_stream_in_t* stream) {

View file

@ -15,6 +15,7 @@
*/
#include "ParametersUtil.h"
#include "Util.h"
namespace android {
namespace hardware {
@ -141,12 +142,7 @@ Result ParametersUtil::setParametersImpl(
Result ParametersUtil::setParams(const AudioParameter& param) {
int halStatus = halSetParameters(param.toString().string());
if (halStatus == OK)
return Result::OK;
else if (halStatus == -ENOSYS)
return Result::INVALID_STATE;
else
return Result::INVALID_ARGUMENTS;
return util::analyzeStatus(halStatus);
}
} // namespace implementation

View file

@ -28,6 +28,7 @@
#include "Conversions.h"
#include "EffectMap.h"
#include "Stream.h"
#include "Util.h"
namespace android {
namespace hardware {
@ -45,29 +46,14 @@ Stream::~Stream() {
// static
Result Stream::analyzeStatus(const char* funcName, int status) {
static const std::vector<int> empty;
return analyzeStatus(funcName, status, empty);
return util::analyzeStatus("stream", funcName, status);
}
template <typename T>
inline bool element_in(T e, const std::vector<T>& v) {
return std::find(v.begin(), v.end(), e) != v.end();
}
// static
Result Stream::analyzeStatus(const char* funcName, int status,
const std::vector<int>& ignoreErrors) {
if (status != 0 && (ignoreErrors.empty() || !element_in(-status, ignoreErrors))) {
ALOGW("Error from HAL stream in function %s: %s", funcName, strerror(-status));
}
switch (status) {
case 0: return Result::OK;
case -EINVAL: return Result::INVALID_ARGUMENTS;
case -ENODATA: return Result::INVALID_STATE;
case -ENODEV: return Result::NOT_INITIALIZED;
case -ENOSYS: return Result::NOT_SUPPORTED;
default: return Result::INVALID_STATE;
}
return util::analyzeStatus("stream", funcName, status, ignoreErrors);
}
char* Stream::halGetParameters(const char* keys) {

View file

@ -17,6 +17,11 @@
#ifndef ANDROID_HARDWARE_AUDIO_V2_0_UTIL_H
#define ANDROID_HARDWARE_AUDIO_V2_0_UTIL_H
#include <algorithm>
#include <vector>
#include <system/audio.h>
namespace android {
namespace hardware {
namespace audio {
@ -28,6 +33,39 @@ constexpr bool isGainNormalized(float gain) {
return gain >= 0.0 && gain <= 1.0;
}
namespace util {
template <typename T>
inline bool element_in(T e, const std::vector<T>& v) {
return std::find(v.begin(), v.end(), e) != v.end();
}
static inline Result analyzeStatus(status_t status) {
switch (status) {
case 0:
return Result::OK;
case -EINVAL:
return Result::INVALID_ARGUMENTS;
case -ENODATA:
return Result::INVALID_STATE;
case -ENODEV:
return Result::NOT_INITIALIZED;
case -ENOSYS:
return Result::NOT_SUPPORTED;
default:
return Result::INVALID_STATE;
}
}
static inline Result analyzeStatus(const char* className, const char* funcName, status_t status,
const std::vector<int>& ignoreErrors = {}) {
if (status != 0 && !element_in(-status, ignoreErrors)) {
ALOGW("Error from HAL %s in function %s: %s", className, funcName, strerror(-status));
}
return analyzeStatus(status);
}
} // namespace util
} // namespace implementation
} // namespace V2_0
} // namespace audio