blob: 0511557da32f8679485ca83d2d1c36f239a75270 [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(
Kevin Rocard72e50e22017-05-05 14:02:55 -070054 const hidl_vec<hidl_string>& keys,
55 std::function<void(Result retval,
56 const hidl_vec<ParameterValue>& parameters)>
57 cb) {
Mikhail Naganov10548292016-10-31 10:39:47 -070058 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 Rocard72e50e22017-05-05 14:02:55 -070084std::unique_ptr<AudioParameter> ParametersUtil::getParams(
85 const AudioParameter& keys) {
Mikhail Naganov10548292016-10-31 10:39:47 -070086 String8 paramsAndValues;
Kevin Rocard72e50e22017-05-05 14:02:55 -070087 char* halValues = halGetParameters(keys.keysToString().string());
Mikhail Naganov10548292016-10-31 10:39:47 -070088 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
97Result ParametersUtil::setParam(const char* name, bool value) {
98 AudioParameter param;
Kevin Rocard72e50e22017-05-05 14:02:55 -070099 param.add(String8(name), String8(value ? AudioParameter::valueOn
100 : AudioParameter::valueOff));
Mikhail Naganov10548292016-10-31 10:39:47 -0700101 return setParams(param);
102}
103
104Result ParametersUtil::setParam(const char* name, int value) {
105 AudioParameter param;
106 param.addInt(String8(name), value);
107 return setParams(param);
108}
109
110Result 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 Rocard72e50e22017-05-05 14:02:55 -0700116Result ParametersUtil::setParametersImpl(
117 const hidl_vec<ParameterValue>& parameters) {
Mikhail Naganov10548292016-10-31 10:39:47 -0700118 AudioParameter params;
119 for (size_t i = 0; i < parameters.size(); ++i) {
Kevin Rocard72e50e22017-05-05 14:02:55 -0700120 params.add(String8(parameters[i].key.c_str()),
121 String8(parameters[i].value.c_str()));
Mikhail Naganov10548292016-10-31 10:39:47 -0700122 }
123 return setParams(params);
124}
125
126Result 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