blob: 5cc60db9818f82ed2b1ba09871f4a375677898b0 [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);
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070081 Result retval =
82 halValues->size() == keys.size() ? Result::OK : Result::NOT_SUPPORTED;
Mikhail Naganov10548292016-10-31 10:39:47 -070083 hidl_vec<ParameterValue> result;
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070084 result.resize(halValues->size());
85 String8 halKey, halValue;
86 for (size_t i = 0; i < halValues->size(); ++i) {
87 status_t status = halValues->getAt(i, halKey, halValue);
88 if (status != OK) {
89 result.resize(0);
90 retval = getHalStatusToResult(status);
91 break;
Mikhail Naganov10548292016-10-31 10:39:47 -070092 }
Kevin Rocardfa3b4a92017-05-02 17:38:34 -070093 result[i].key = halKey.string();
94 result[i].value = halValue.string();
Mikhail Naganov10548292016-10-31 10:39:47 -070095 }
96 cb(retval, result);
97}
98
Kevin Rocard72e50e22017-05-05 14:02:55 -070099std::unique_ptr<AudioParameter> ParametersUtil::getParams(
100 const AudioParameter& keys) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700101 String8 paramsAndValues;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700102 char* halValues = halGetParameters(keys.keysToString().string());
Mikhail Naganov10548292016-10-31 10:39:47 -0700103 if (halValues != NULL) {
104 paramsAndValues.setTo(halValues);
105 free(halValues);
106 } else {
107 paramsAndValues.clear();
108 }
109 return std::unique_ptr<AudioParameter>(new AudioParameter(paramsAndValues));
110}
111
112Result ParametersUtil::setParam(const char* name, bool value) {
113 AudioParameter param;
Kevin Rocard72e50e22017-05-05 14:02:55 -0700114 param.add(String8(name), String8(value ? AudioParameter::valueOn
115 : AudioParameter::valueOff));
Mikhail Naganov10548292016-10-31 10:39:47 -0700116 return setParams(param);
117}
118
119Result ParametersUtil::setParam(const char* name, int value) {
120 AudioParameter param;
121 param.addInt(String8(name), value);
122 return setParams(param);
123}
124
125Result ParametersUtil::setParam(const char* name, const char* value) {
126 AudioParameter param;
127 param.add(String8(name), String8(value));
128 return setParams(param);
129}
130
Kevin Rocard72e50e22017-05-05 14:02:55 -0700131Result ParametersUtil::setParametersImpl(
132 const hidl_vec<ParameterValue>& parameters) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700133 AudioParameter params;
134 for (size_t i = 0; i < parameters.size(); ++i) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700135 params.add(String8(parameters[i].key.c_str()),
136 String8(parameters[i].value.c_str()));
Mikhail Naganov10548292016-10-31 10:39:47 -0700137 }
138 return setParams(params);
139}
140
141Result ParametersUtil::setParams(const AudioParameter& param) {
142 int halStatus = halSetParameters(param.toString().string());
143 if (halStatus == OK)
144 return Result::OK;
145 else if (halStatus == -ENOSYS)
146 return Result::INVALID_STATE;
147 else
148 return Result::INVALID_ARGUMENTS;
149}
150
151} // namespace implementation
152} // namespace V2_0
153} // namespace audio
154} // namespace hardware
155} // namespace android