• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "core/default/ParametersUtil.h"
18 #include "core/default/Conversions.h"
19 #include "core/default/Util.h"
20 
21 #include <system/audio.h>
22 
23 namespace android {
24 namespace hardware {
25 namespace audio {
26 namespace CPP_VERSION {
27 namespace implementation {
28 
29 /** Converts a status_t in Result according to the rules of AudioParameter::get*
30  * Note: Static method and not private method to avoid leaking status_t dependency
31  */
getHalStatusToResult(status_t status)32 static Result getHalStatusToResult(status_t status) {
33     switch (status) {
34         case OK:
35             return Result::OK;
36         case BAD_VALUE:  // Nothing was returned, probably because the HAL does
37                          // not handle it
38             return Result::NOT_SUPPORTED;
39         case INVALID_OPERATION:  // Conversion from string to the requested type
40                                  // failed
41             return Result::INVALID_ARGUMENTS;
42         default:  // Should not happen
43             ALOGW("Unexpected status returned by getParam: %u", status);
44             return Result::INVALID_ARGUMENTS;
45     }
46 }
47 
getParam(const char * name,bool * value)48 Result ParametersUtil::getParam(const char* name, bool* value) {
49     String8 halValue;
50     Result retval = getParam(name, &halValue);
51     *value = false;
52     if (retval == Result::OK) {
53         if (halValue.empty()) {
54             return Result::NOT_SUPPORTED;
55         }
56         *value = !(halValue == AudioParameter::valueOff);
57     }
58     return retval;
59 }
60 
getParam(const char * name,int * value)61 Result ParametersUtil::getParam(const char* name, int* value) {
62     const String8 halName(name);
63     AudioParameter keys;
64     keys.addKey(halName);
65     std::unique_ptr<AudioParameter> params = getParams(keys);
66     return getHalStatusToResult(params->getInt(halName, *value));
67 }
68 
getParam(const char * name,String8 * value,AudioParameter context)69 Result ParametersUtil::getParam(const char* name, String8* value, AudioParameter context) {
70     const String8 halName(name);
71     context.addKey(halName);
72     std::unique_ptr<AudioParameter> params = getParams(context);
73     return getHalStatusToResult(params->get(halName, *value));
74 }
75 
getParametersImpl(const hidl_vec<ParameterValue> & context,const hidl_vec<hidl_string> & keys,std::function<void (Result retval,const hidl_vec<ParameterValue> & parameters)> cb)76 void ParametersUtil::getParametersImpl(
77     const hidl_vec<ParameterValue>& context, const hidl_vec<hidl_string>& keys,
78     std::function<void(Result retval, const hidl_vec<ParameterValue>& parameters)> cb) {
79     AudioParameter halKeys;
80     for (auto& pair : context) {
81         halKeys.add(String8(pair.key.c_str()), String8(pair.value.c_str()));
82     }
83     for (size_t i = 0; i < keys.size(); ++i) {
84         halKeys.addKey(String8(keys[i].c_str()));
85     }
86     std::unique_ptr<AudioParameter> halValues = getParams(halKeys);
87     Result retval =
88         (keys.size() == 0 || halValues->size() != 0) ? Result::OK : Result::NOT_SUPPORTED;
89     hidl_vec<ParameterValue> result;
90     result.resize(halValues->size());
91     String8 halKey, halValue;
92     for (size_t i = 0; i < halValues->size(); ++i) {
93         status_t status = halValues->getAt(i, halKey, halValue);
94         if (status != OK) {
95             result.resize(0);
96             retval = getHalStatusToResult(status);
97             break;
98         }
99         result[i].key = halKey.string();
100         result[i].value = halValue.string();
101     }
102     cb(retval, result);
103 }
104 
getParams(const AudioParameter & keys)105 std::unique_ptr<AudioParameter> ParametersUtil::getParams(const AudioParameter& keys) {
106     String8 paramsAndValues;
107     char* halValues = halGetParameters(keys.keysToString().string());
108     if (halValues != NULL) {
109         paramsAndValues.setTo(halValues);
110         free(halValues);
111     } else {
112         paramsAndValues.clear();
113     }
114     return std::unique_ptr<AudioParameter>(new AudioParameter(paramsAndValues));
115 }
116 
setParam(const char * name,const char * value)117 Result ParametersUtil::setParam(const char* name, const char* value) {
118     AudioParameter param;
119     param.add(String8(name), String8(value));
120     return setParams(param);
121 }
122 
setParam(const char * name,bool value)123 Result ParametersUtil::setParam(const char* name, bool value) {
124     AudioParameter param;
125     param.add(String8(name), String8(value ? AudioParameter::valueOn : AudioParameter::valueOff));
126     return setParams(param);
127 }
128 
setParam(const char * name,int value)129 Result ParametersUtil::setParam(const char* name, int value) {
130     AudioParameter param;
131     param.addInt(String8(name), value);
132     return setParams(param);
133 }
134 
setParam(const char * name,float value)135 Result ParametersUtil::setParam(const char* name, float value) {
136     AudioParameter param;
137     param.addFloat(String8(name), value);
138     return setParams(param);
139 }
140 
setParametersImpl(const hidl_vec<ParameterValue> & context,const hidl_vec<ParameterValue> & parameters)141 Result ParametersUtil::setParametersImpl(const hidl_vec<ParameterValue>& context,
142                                          const hidl_vec<ParameterValue>& parameters) {
143     AudioParameter params;
144     for (auto& pair : context) {
145         params.add(String8(pair.key.c_str()), String8(pair.value.c_str()));
146     }
147     for (size_t i = 0; i < parameters.size(); ++i) {
148         params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str()));
149     }
150     return setParams(params);
151 }
setParam(const char * name,const DeviceAddress & address)152 Result ParametersUtil::setParam(const char* name, const DeviceAddress& address) {
153     AudioParameter params(String8(deviceAddressToHal(address).c_str()));
154     params.addInt(String8(name), int(address.device));
155     return setParams(params);
156 }
157 
setParams(const AudioParameter & param)158 Result ParametersUtil::setParams(const AudioParameter& param) {
159     int halStatus = halSetParameters(param.toString().string());
160     return util::analyzeStatus(halStatus);
161 }
162 
163 }  // namespace implementation
164 }  // namespace CPP_VERSION
165 }  // namespace audio
166 }  // namespace hardware
167 }  // namespace android
168