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