blob: 75a60b9b640240828ba19f36b1adc700ab6467ae [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
25Result 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
35Result 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
44Result 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
53void ParametersUtil::getParametersImpl(
54 const hidl_vec<hidl_string>& keys,
55 std::function<void(Result retval, const hidl_vec<ParameterValue>& parameters)> cb) {
56 AudioParameter halKeys;
57 for (size_t i = 0; i < keys.size(); ++i) {
58 halKeys.addKey(String8(keys[i].c_str()));
59 }
60 std::unique_ptr<AudioParameter> halValues = getParams(halKeys);
61 Result retval(Result::INVALID_ARGUMENTS);
62 hidl_vec<ParameterValue> result;
63 if (halValues->size() > 0) {
64 result.resize(halValues->size());
65 String8 halKey, halValue;
66 for (size_t i = 0; i < halValues->size(); ++i) {
67 status_t status = halValues->getAt(i, halKey, halValue);
68 if (status != OK) {
69 result.resize(0);
70 break;
71 }
72 result[i].key = halKey.string();
73 result[i].value = halValue.string();
74 }
75 if (result.size() != 0) {
76 retval = Result::OK;
77 }
78 }
79 cb(retval, result);
80}
81
82std::unique_ptr<AudioParameter> ParametersUtil::getParams(const AudioParameter& keys) {
83 String8 paramsAndValues;
84 char *halValues = halGetParameters(keys.keysToString().string());
85 if (halValues != NULL) {
86 paramsAndValues.setTo(halValues);
87 free(halValues);
88 } else {
89 paramsAndValues.clear();
90 }
91 return std::unique_ptr<AudioParameter>(new AudioParameter(paramsAndValues));
92}
93
94Result ParametersUtil::setParam(const char* name, bool value) {
95 AudioParameter param;
96 param.add(String8(name), String8(value ? AudioParameter::valueOn : AudioParameter::valueOff));
97 return setParams(param);
98}
99
100Result ParametersUtil::setParam(const char* name, int value) {
101 AudioParameter param;
102 param.addInt(String8(name), value);
103 return setParams(param);
104}
105
106Result ParametersUtil::setParam(const char* name, const char* value) {
107 AudioParameter param;
108 param.add(String8(name), String8(value));
109 return setParams(param);
110}
111
112Result ParametersUtil::setParametersImpl(const hidl_vec<ParameterValue>& parameters) {
113 AudioParameter params;
114 for (size_t i = 0; i < parameters.size(); ++i) {
115 params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str()));
116 }
117 return setParams(params);
118}
119
120Result ParametersUtil::setParams(const AudioParameter& param) {
121 int halStatus = halSetParameters(param.toString().string());
122 if (halStatus == OK)
123 return Result::OK;
124 else if (halStatus == -ENOSYS)
125 return Result::INVALID_STATE;
126 else
127 return Result::INVALID_ARGUMENTS;
128}
129
130} // namespace implementation
131} // namespace V2_0
132} // namespace audio
133} // namespace hardware
134} // namespace android