1 /*
2 * Copyright (C) 2018 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 #ifndef ANDROID_HARDWARE_AUDIO_UTIL_H
18 #define ANDROID_HARDWARE_AUDIO_UTIL_H
19
20 // clang-format off
21 #include PATH(android/hardware/audio/CORE_TYPES_FILE_VERSION/types.h)
22 // clang-format on
23
24 #include <algorithm>
25 #include <vector>
26
27 #include <system/audio.h>
28
29 namespace android {
30 namespace hardware {
31 namespace audio {
32 namespace CORE_TYPES_CPP_VERSION {
33 namespace implementation {
34
35 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
36 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION;
37
38 namespace util {
39
40 /** @return true if gain is between 0 and 1 included. */
isGainNormalized(float gain)41 constexpr bool isGainNormalized(float gain) {
42 return gain >= 0.0 && gain <= 1.0;
43 }
44
45 template <typename T>
element_in(T e,const std::vector<T> & v)46 inline bool element_in(T e, const std::vector<T>& v) {
47 return std::find(v.begin(), v.end(), e) != v.end();
48 }
49
analyzeStatus(status_t status)50 static inline Result analyzeStatus(status_t status) {
51 switch (status) {
52 case 0:
53 return Result::OK;
54 case -EINVAL:
55 return Result::INVALID_ARGUMENTS;
56 case -ENODATA:
57 return Result::INVALID_STATE;
58 case -ENODEV:
59 return Result::NOT_INITIALIZED;
60 case -ENOSYS:
61 return Result::NOT_SUPPORTED;
62 default:
63 return Result::INVALID_STATE;
64 }
65 }
66
67 static inline Result analyzeStatus(const char* className, const char* funcName, status_t status,
68 const std::vector<int>& ignoreErrors = {}) {
69 if (status != 0 && !element_in(-status, ignoreErrors)) {
70 ALOGW("Error from HAL %s in function %s: %s", className, funcName, strerror(-status));
71 }
72 return analyzeStatus(status);
73 }
74
75 } // namespace util
76 } // namespace implementation
77 } // namespace CORE_TYPES_CPP_VERSION
78 } // namespace audio
79 } // namespace hardware
80 } // namespace android
81
82 #endif // ANDROID_HARDWARE_AUDIO_UTIL_H
83