Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 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 | Result ParametersUtil::getParam(const char* name, bool* value) { |
| 26 | String8 halValue; |
| 27 | Result retval = getParam(name, &halValue); |
| 28 | *value = false; |
| 29 | if (retval == Result::OK) { |
| 30 | *value = !(halValue == AudioParameter::valueOff); |
| 31 | } |
| 32 | return retval; |
| 33 | } |
| 34 | |
| 35 | Result ParametersUtil::getParam(const char* name, int* value) { |
| 36 | const String8 halName(name); |
| 37 | AudioParameter keys; |
| 38 | keys.addKey(halName); |
| 39 | std::unique_ptr<AudioParameter> params = getParams(keys); |
| 40 | status_t halStatus = params->getInt(halName, *value); |
| 41 | return halStatus == OK ? Result::OK : Result::INVALID_ARGUMENTS; |
| 42 | } |
| 43 | |
| 44 | Result ParametersUtil::getParam(const char* name, String8* value) { |
| 45 | const String8 halName(name); |
| 46 | AudioParameter keys; |
| 47 | keys.addKey(halName); |
| 48 | std::unique_ptr<AudioParameter> params = getParams(keys); |
| 49 | status_t halStatus = params->get(halName, *value); |
| 50 | return halStatus == OK ? Result::OK : Result::INVALID_ARGUMENTS; |
| 51 | } |
| 52 | |
| 53 | void ParametersUtil::getParametersImpl( |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 54 | const hidl_vec<hidl_string>& keys, |
| 55 | std::function<void(Result retval, |
| 56 | const hidl_vec<ParameterValue>& parameters)> |
| 57 | cb) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 58 | AudioParameter halKeys; |
| 59 | for (size_t i = 0; i < keys.size(); ++i) { |
| 60 | halKeys.addKey(String8(keys[i].c_str())); |
| 61 | } |
| 62 | std::unique_ptr<AudioParameter> halValues = getParams(halKeys); |
| 63 | Result retval(Result::INVALID_ARGUMENTS); |
| 64 | hidl_vec<ParameterValue> result; |
| 65 | if (halValues->size() > 0) { |
| 66 | result.resize(halValues->size()); |
| 67 | String8 halKey, halValue; |
| 68 | for (size_t i = 0; i < halValues->size(); ++i) { |
| 69 | status_t status = halValues->getAt(i, halKey, halValue); |
| 70 | if (status != OK) { |
| 71 | result.resize(0); |
| 72 | break; |
| 73 | } |
| 74 | result[i].key = halKey.string(); |
| 75 | result[i].value = halValue.string(); |
| 76 | } |
| 77 | if (result.size() != 0) { |
| 78 | retval = Result::OK; |
| 79 | } |
| 80 | } |
| 81 | cb(retval, result); |
| 82 | } |
| 83 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 84 | std::unique_ptr<AudioParameter> ParametersUtil::getParams( |
| 85 | const AudioParameter& keys) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 86 | String8 paramsAndValues; |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 87 | char* halValues = halGetParameters(keys.keysToString().string()); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 88 | if (halValues != NULL) { |
| 89 | paramsAndValues.setTo(halValues); |
| 90 | free(halValues); |
| 91 | } else { |
| 92 | paramsAndValues.clear(); |
| 93 | } |
| 94 | return std::unique_ptr<AudioParameter>(new AudioParameter(paramsAndValues)); |
| 95 | } |
| 96 | |
| 97 | Result ParametersUtil::setParam(const char* name, bool value) { |
| 98 | AudioParameter param; |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 99 | param.add(String8(name), String8(value ? AudioParameter::valueOn |
| 100 | : AudioParameter::valueOff)); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 101 | return setParams(param); |
| 102 | } |
| 103 | |
| 104 | Result ParametersUtil::setParam(const char* name, int value) { |
| 105 | AudioParameter param; |
| 106 | param.addInt(String8(name), value); |
| 107 | return setParams(param); |
| 108 | } |
| 109 | |
| 110 | Result ParametersUtil::setParam(const char* name, const char* value) { |
| 111 | AudioParameter param; |
| 112 | param.add(String8(name), String8(value)); |
| 113 | return setParams(param); |
| 114 | } |
| 115 | |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 116 | Result ParametersUtil::setParametersImpl( |
| 117 | const hidl_vec<ParameterValue>& parameters) { |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 118 | AudioParameter params; |
| 119 | for (size_t i = 0; i < parameters.size(); ++i) { |
Kevin Rocard | 72e50e2 | 2017-05-05 14:02:55 -0700 | [diff] [blame^] | 120 | params.add(String8(parameters[i].key.c_str()), |
| 121 | String8(parameters[i].value.c_str())); |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 122 | } |
| 123 | return setParams(params); |
| 124 | } |
| 125 | |
| 126 | Result ParametersUtil::setParams(const AudioParameter& param) { |
| 127 | int halStatus = halSetParameters(param.toString().string()); |
| 128 | if (halStatus == OK) |
| 129 | return Result::OK; |
| 130 | else if (halStatus == -ENOSYS) |
| 131 | return Result::INVALID_STATE; |
| 132 | else |
| 133 | return Result::INVALID_ARGUMENTS; |
| 134 | } |
| 135 | |
| 136 | } // namespace implementation |
| 137 | } // namespace V2_0 |
| 138 | } // namespace audio |
| 139 | } // namespace hardware |
| 140 | } // namespace android |