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