1 /* ALSAControl.cpp 2 ** 3 ** Copyright 2008-2009 Wind River Systems 4 ** Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. 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 27 #define LOG_TAG "ALSAControl" 28 //#define LOG_NDEBUG 0 29 #define LOG_NDDEBUG 0 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 { 44 ALOGD("ALSAControl: ctor device %s", device); 45 mHandle = mixer_open(device); 46 ALOGV("ALSAControl: ctor mixer %p", mHandle); 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) { 59 ALOGE("Control not initialized"); 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; 75 ALOGD("set:: name %s value %d index %d", name, value, index); 76 if (!mHandle) { 77 ALOGE("Control not initialized"); 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) { 84 ALOGE("Could not get the mixer control"); 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; 95 ALOGD("set:: name %s value %s", name, value); 96 97 if (!mHandle) { 98 ALOGE("Control not initialized"); 99 return NO_INIT; 100 } 101 102 ctl = mixer_get_control(mHandle, name, 0); 103 if(ctl == NULL) { 104 ALOGE("Could not get the mixer control"); 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; 115 ALOGD("setext:: name %s count %d", name, count); 116 if (!mHandle) { 117 ALOGE("Control not initialized"); 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) { 124 ALOGE("Could not get the mixer control"); 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 132