Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 1 | /* ALSAControl.cpp |
| 2 | ** |
| 3 | ** Copyright 2008-2009 Wind River Systems |
Duy Truong | fae1962 | 2013-11-24 02:17:54 -0800 | [diff] [blame] | 4 | ** Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 5 | ** |
| 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 Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 27 | #define LOG_TAG "ALSAControl" |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 28 | //#define LOG_NDEBUG 0 |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 29 | #define LOG_NDDEBUG 0 |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 30 | #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 | |
| 39 | namespace android_audio_legacy |
| 40 | { |
| 41 | |
| 42 | ALSAControl::ALSAControl(const char *device) |
| 43 | { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 44 | ALOGD("ALSAControl: ctor device %s", device); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 45 | mHandle = mixer_open(device); |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 46 | ALOGV("ALSAControl: ctor mixer %p", mHandle); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ALSAControl::~ALSAControl() |
| 50 | { |
| 51 | if (mHandle) mixer_close(mHandle); |
| 52 | } |
| 53 | |
| 54 | status_t ALSAControl::get(const char *name, unsigned int &value, int index) |
| 55 | { |
| 56 | struct mixer_ctl *ctl; |
| 57 | |
| 58 | if (!mHandle) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 59 | ALOGE("Control not initialized"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 60 | 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 | |
| 71 | status_t ALSAControl::set(const char *name, unsigned int value, int index) |
| 72 | { |
| 73 | struct mixer_ctl *ctl; |
| 74 | int ret = 0; |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 75 | ALOGD("set:: name %s value %d index %d", name, value, index); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 76 | if (!mHandle) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 77 | ALOGE("Control not initialized"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 78 | 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 Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 84 | ALOGE("Could not get the mixer control"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 85 | return BAD_VALUE; |
| 86 | } |
| 87 | ret = mixer_ctl_set(ctl, value); |
| 88 | return (ret < 0) ? BAD_VALUE : NO_ERROR; |
| 89 | } |
| 90 | |
| 91 | status_t ALSAControl::set(const char *name, const char *value) |
| 92 | { |
| 93 | struct mixer_ctl *ctl; |
| 94 | int ret = 0; |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 95 | ALOGD("set:: name %s value %s", name, value); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 96 | |
| 97 | if (!mHandle) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 98 | ALOGE("Control not initialized"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 99 | return NO_INIT; |
| 100 | } |
| 101 | |
| 102 | ctl = mixer_get_control(mHandle, name, 0); |
| 103 | if(ctl == NULL) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 104 | ALOGE("Could not get the mixer control"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 105 | return BAD_VALUE; |
| 106 | } |
| 107 | ret = mixer_ctl_select(ctl, value); |
| 108 | return (ret < 0) ? BAD_VALUE : NO_ERROR; |
| 109 | } |
| 110 | |
| 111 | status_t ALSAControl::setext(const char *name, int count, char **setValues) |
| 112 | { |
| 113 | struct mixer_ctl *ctl; |
| 114 | int ret = 0; |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 115 | ALOGD("setext:: name %s count %d", name, count); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 116 | if (!mHandle) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 117 | ALOGE("Control not initialized"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 118 | 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 Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 124 | ALOGE("Could not get the mixer control"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 125 | 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 |