blob: 5b2cf31415fa61e7889dfb0d43e98259c191a3dc [file] [log] [blame]
Iliyan Malchev4765c432012-06-11 14:36:16 -07001/* ALSAControl.cpp
2 **
3 ** Copyright 2008-2009 Wind River Systems
Duy Truongfae19622013-11-24 02:17:54 -08004 ** Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
Iliyan Malchev4765c432012-06-11 14:36:16 -07005 **
6 ** Licensed under the Apache License, Version 2.0 (the "License");
7 ** you may not use this file except in compliance with the License.
8 ** You may obtain a copy of the License at
9 **
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 **
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 */
18
19#include <errno.h>
20#include <stdarg.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <dlfcn.h>
26
Ajay Dudani9746c472012-06-18 16:01:16 -070027#define LOG_TAG "ALSAControl"
Iliyan Malchev4765c432012-06-11 14:36:16 -070028//#define LOG_NDEBUG 0
Ajay Dudani9746c472012-06-18 16:01:16 -070029#define LOG_NDDEBUG 0
Iliyan Malchev4765c432012-06-11 14:36:16 -070030#include <utils/Log.h>
31#include <utils/String8.h>
32
33#include <cutils/properties.h>
34#include <media/AudioRecord.h>
35#include <hardware_legacy/power.h>
36
37#include "AudioHardwareALSA.h"
38
39namespace android_audio_legacy
40{
41
42ALSAControl::ALSAControl(const char *device)
43{
Iliyan Malchev4113f342012-06-11 14:39:47 -070044 ALOGD("ALSAControl: ctor device %s", device);
Iliyan Malchev4765c432012-06-11 14:36:16 -070045 mHandle = mixer_open(device);
Iliyan Malchev4113f342012-06-11 14:39:47 -070046 ALOGV("ALSAControl: ctor mixer %p", mHandle);
Iliyan Malchev4765c432012-06-11 14:36:16 -070047}
48
49ALSAControl::~ALSAControl()
50{
51 if (mHandle) mixer_close(mHandle);
52}
53
54status_t ALSAControl::get(const char *name, unsigned int &value, int index)
55{
56 struct mixer_ctl *ctl;
57
58 if (!mHandle) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070059 ALOGE("Control not initialized");
Iliyan Malchev4765c432012-06-11 14:36:16 -070060 return NO_INIT;
61 }
62
63 ctl = mixer_get_control(mHandle, name, index);
64 if (!ctl)
65 return BAD_VALUE;
66
67 mixer_ctl_get(ctl, &value);
68 return NO_ERROR;
69}
70
71status_t ALSAControl::set(const char *name, unsigned int value, int index)
72{
73 struct mixer_ctl *ctl;
74 int ret = 0;
Iliyan Malchev4113f342012-06-11 14:39:47 -070075 ALOGD("set:: name %s value %d index %d", name, value, index);
Iliyan Malchev4765c432012-06-11 14:36:16 -070076 if (!mHandle) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070077 ALOGE("Control not initialized");
Iliyan Malchev4765c432012-06-11 14:36:16 -070078 return NO_INIT;
79 }
80
81 // ToDo: Do we need to send index here? Right now it works with 0
82 ctl = mixer_get_control(mHandle, name, 0);
83 if(ctl == NULL) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070084 ALOGE("Could not get the mixer control");
Iliyan Malchev4765c432012-06-11 14:36:16 -070085 return BAD_VALUE;
86 }
87 ret = mixer_ctl_set(ctl, value);
88 return (ret < 0) ? BAD_VALUE : NO_ERROR;
89}
90
91status_t ALSAControl::set(const char *name, const char *value)
92{
93 struct mixer_ctl *ctl;
94 int ret = 0;
Iliyan Malchev4113f342012-06-11 14:39:47 -070095 ALOGD("set:: name %s value %s", name, value);
Iliyan Malchev4765c432012-06-11 14:36:16 -070096
97 if (!mHandle) {
Iliyan Malchev4113f342012-06-11 14:39:47 -070098 ALOGE("Control not initialized");
Iliyan Malchev4765c432012-06-11 14:36:16 -070099 return NO_INIT;
100 }
101
102 ctl = mixer_get_control(mHandle, name, 0);
103 if(ctl == NULL) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700104 ALOGE("Could not get the mixer control");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700105 return BAD_VALUE;
106 }
107 ret = mixer_ctl_select(ctl, value);
108 return (ret < 0) ? BAD_VALUE : NO_ERROR;
109}
110
111status_t ALSAControl::setext(const char *name, int count, char **setValues)
112{
113 struct mixer_ctl *ctl;
114 int ret = 0;
Iliyan Malchev4113f342012-06-11 14:39:47 -0700115 ALOGD("setext:: name %s count %d", name, count);
Iliyan Malchev4765c432012-06-11 14:36:16 -0700116 if (!mHandle) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700117 ALOGE("Control not initialized");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700118 return NO_INIT;
119 }
120
121 // ToDo: Do we need to send index here? Right now it works with 0
122 ctl = mixer_get_control(mHandle, name, 0);
123 if(ctl == NULL) {
Iliyan Malchev4113f342012-06-11 14:39:47 -0700124 ALOGE("Could not get the mixer control");
Iliyan Malchev4765c432012-06-11 14:36:16 -0700125 return BAD_VALUE;
126 }
127 ret = mixer_ctl_set_value(ctl, count, setValues);
128 return (ret < 0) ? BAD_VALUE : NO_ERROR;
129}
130
131}; // namespace android