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