blob: 2140885f20b9a0cdfc7675576ddb1775f543ed52 [file] [log] [blame]
Mikhail Naganov10548292016-10-31 10:39:47 -07001/*
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
19namespace android {
20namespace hardware {
21namespace audio {
22namespace V2_0 {
23namespace implementation {
24
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070025// Static method and not private method to avoid leaking status_t dependency
26static 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
Mikhail Naganov10548292016-10-31 10:39:47 -070042Result ParametersUtil::getParam(const char* name, bool* value) {
43 String8 halValue;
44 Result retval = getParam(name, &halValue);
45 *value = false;
46 if (retval == Result::OK) {
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070047 if (halValue.empty()) {
48 return Result::NOT_SUPPORTED;
49 }
Mikhail Naganov10548292016-10-31 10:39:47 -070050 *value = !(halValue == AudioParameter::valueOff);
51 }
52 return retval;
53}
54
55Result 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);
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070060 return getHalStatusToResult(params->getInt(halName, *value));
Mikhail Naganov10548292016-10-31 10:39:47 -070061}
62
63Result 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);
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070068 return getHalStatusToResult(params->get(halName, *value));
Mikhail Naganov10548292016-10-31 10:39:47 -070069}
70
71void ParametersUtil::getParametersImpl(
Kevin Rocard72e50e22017-05-05 14:02:55 -070072 const hidl_vec<hidl_string>& keys,
73 std::function<void(Result retval,
74 const hidl_vec<ParameterValue>& parameters)>
75 cb) {
Mikhail Naganov10548292016-10-31 10:39:47 -070076 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);
Eric Laurent45d40322017-05-17 18:32:36 -070081 Result retval = (keys.size() == 0 || halValues->size() != 0)
82 ? Result::OK
83 : Result::NOT_SUPPORTED;
Mikhail Naganov10548292016-10-31 10:39:47 -070084 hidl_vec<ParameterValue> result;
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070085 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;
Mikhail Naganov10548292016-10-31 10:39:47 -070093 }
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070094 result[i].key = halKey.string();
95 result[i].value = halValue.string();
Mikhail Naganov10548292016-10-31 10:39:47 -070096 }
97 cb(retval, result);
98}
99
Kevin Rocard72e50e22017-05-05 14:02:55 -0700100std::unique_ptr<AudioParameter> ParametersUtil::getParams(
101 const AudioParameter& keys) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700102 String8 paramsAndValues;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700103 char* halValues = halGetParameters(keys.keysToString().string());
Mikhail Naganov10548292016-10-31 10:39:47 -0700104 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
113Result ParametersUtil::setParam(const char* name, bool value) {
114 AudioParameter param;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700115 param.add(String8(name), String8(value ? AudioParameter::valueOn
116 : AudioParameter::valueOff));
Mikhail Naganov10548292016-10-31 10:39:47 -0700117 return setParams(param);
118}
119
120Result ParametersUtil::setParam(const char* name, int value) {
121 AudioParameter param;
122 param.addInt(String8(name), value);
123 return setParams(param);
124}
125
126Result ParametersUtil::setParam(const char* name, const char* value) {
127 AudioParameter param;
128 param.add(String8(name), String8(value));
129 return setParams(param);
130}
131
Kevin Rocard72e50e22017-05-05 14:02:55 -0700132Result ParametersUtil::setParametersImpl(
133 const hidl_vec<ParameterValue>& parameters) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700134 AudioParameter params;
135 for (size_t i = 0; i < parameters.size(); ++i) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700136 params.add(String8(parameters[i].key.c_str()),
137 String8(parameters[i].value.c_str()));
Mikhail Naganov10548292016-10-31 10:39:47 -0700138 }
139 return setParams(params);
140}
141
142Result 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