1 /*
2 * Copyright 2019 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 #define LOG_TAG "BTAudioHw"
18
19 #include <android-base/logging.h>
20 #include <hardware/hardware.h>
21 #include <malloc.h>
22 #include <string.h>
23 #include <system/audio.h>
24
25 #include "stream_apis.h"
26 #include "utils.h"
27
28 using ::android::bluetooth::audio::utils::GetAudioParamString;
29 using ::android::bluetooth::audio::utils::ParseAudioParams;
30
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)31 static int adev_set_parameters(struct audio_hw_device* dev,
32 const char* kvpairs) {
33 LOG(VERBOSE) << __func__ << ": kevpairs=[" << kvpairs << "]";
34 std::unordered_map<std::string, std::string> params =
35 ParseAudioParams(kvpairs);
36 if (params.empty()) return 0;
37
38 LOG(VERBOSE) << __func__ << ": ParamsMap=[" << GetAudioParamString(params)
39 << "]";
40 if (params.find("A2dpSuspended") == params.end() &&
41 params.find("LeAudioSuspended") == params.end()) {
42 return -ENOSYS;
43 }
44
45 auto* bluetooth_device = reinterpret_cast<BluetoothAudioDevice*>(dev);
46 std::lock_guard<std::mutex> guard(bluetooth_device->mutex_);
47 for (auto sout : bluetooth_device->opened_stream_outs_) {
48 if (sout->stream_out_.common.set_parameters != nullptr) {
49 sout->stream_out_.common.set_parameters(&sout->stream_out_.common,
50 kvpairs);
51 }
52 }
53 return 0;
54 }
55
adev_get_parameters(const struct audio_hw_device * dev,const char * keys)56 static char* adev_get_parameters(const struct audio_hw_device* dev,
57 const char* keys) {
58 LOG(VERBOSE) << __func__ << ": keys=[" << keys << "]";
59 return strdup("");
60 }
61
adev_init_check(const struct audio_hw_device * dev)62 static int adev_init_check(const struct audio_hw_device* dev) { return 0; }
63
adev_set_voice_volume(struct audio_hw_device * dev,float volume)64 static int adev_set_voice_volume(struct audio_hw_device* dev, float volume) {
65 LOG(VERBOSE) << __func__ << ": volume=" << volume;
66 return -ENOSYS;
67 }
68
adev_set_master_volume(struct audio_hw_device * dev,float volume)69 static int adev_set_master_volume(struct audio_hw_device* dev, float volume) {
70 LOG(VERBOSE) << __func__ << ": volume=" << volume;
71 return -ENOSYS;
72 }
73
adev_get_master_volume(struct audio_hw_device * dev,float * volume)74 static int adev_get_master_volume(struct audio_hw_device* dev, float* volume) {
75 return -ENOSYS;
76 }
77
adev_set_master_mute(struct audio_hw_device * dev,bool muted)78 static int adev_set_master_mute(struct audio_hw_device* dev, bool muted) {
79 LOG(VERBOSE) << __func__ << ": mute=" << muted;
80 return -ENOSYS;
81 }
82
adev_get_master_mute(struct audio_hw_device * dev,bool * muted)83 static int adev_get_master_mute(struct audio_hw_device* dev, bool* muted) {
84 return -ENOSYS;
85 }
86
adev_set_mode(struct audio_hw_device * dev,audio_mode_t mode)87 static int adev_set_mode(struct audio_hw_device* dev, audio_mode_t mode) {
88 LOG(VERBOSE) << __func__ << ": mode=" << mode;
89 return 0;
90 }
91
adev_set_mic_mute(struct audio_hw_device * dev,bool state)92 static int adev_set_mic_mute(struct audio_hw_device* dev, bool state) {
93 LOG(VERBOSE) << __func__ << ": state=" << state;
94 return -ENOSYS;
95 }
96
adev_get_mic_mute(const struct audio_hw_device * dev,bool * state)97 static int adev_get_mic_mute(const struct audio_hw_device* dev, bool* state) {
98 return -ENOSYS;
99 }
100
adev_create_audio_patch(struct audio_hw_device * device,unsigned int num_sources,const struct audio_port_config * sources,unsigned int num_sinks,const struct audio_port_config * sinks,audio_patch_handle_t * handle)101 static int adev_create_audio_patch(struct audio_hw_device* device,
102 unsigned int num_sources,
103 const struct audio_port_config* sources,
104 unsigned int num_sinks,
105 const struct audio_port_config* sinks,
106 audio_patch_handle_t* handle) {
107 if (device == nullptr || sources == nullptr || sinks == nullptr ||
108 handle == nullptr || num_sources != 1 || num_sinks == 0 ||
109 num_sinks > AUDIO_PATCH_PORTS_MAX) {
110 return -EINVAL;
111 }
112 if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
113 if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
114 return -EINVAL;
115 }
116 } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
117 for (unsigned int i = 0; i < num_sinks; i++) {
118 if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
119 return -EINVAL;
120 }
121 }
122 } else {
123 return -EINVAL;
124 }
125
126 auto* bluetooth_device = reinterpret_cast<BluetoothAudioDevice*>(device);
127 std::lock_guard<std::mutex> guard(bluetooth_device->mutex_);
128 if (*handle == AUDIO_PATCH_HANDLE_NONE) {
129 *handle = ++bluetooth_device->next_unique_id;
130 }
131
132 LOG(INFO) << __func__ << ": device=" << std::hex << sinks[0].ext.device.type
133 << " handle: " << *handle;
134 return 0;
135 }
136
adev_release_audio_patch(struct audio_hw_device * device,audio_patch_handle_t patch_handle)137 static int adev_release_audio_patch(struct audio_hw_device* device,
138 audio_patch_handle_t patch_handle) {
139 if (device == nullptr) {
140 return -EINVAL;
141 }
142 LOG(INFO) << __func__ << ": patch_handle=" << patch_handle;
143 return 0;
144 }
145
adev_get_audio_port_v7(struct audio_hw_device * device,struct audio_port_v7 * port)146 static int adev_get_audio_port_v7(struct audio_hw_device* device,
147 struct audio_port_v7* port) {
148 if (device == nullptr || port == nullptr) {
149 return -EINVAL;
150 }
151 return -ENOSYS;
152 }
153
adev_get_audio_port(struct audio_hw_device * device,struct audio_port * port)154 static int adev_get_audio_port(struct audio_hw_device* device,
155 struct audio_port* port) {
156 if (device == nullptr || port == nullptr) {
157 return -EINVAL;
158 }
159 return -ENOSYS;
160 }
161
adev_dump(const audio_hw_device_t * device,int fd)162 static int adev_dump(const audio_hw_device_t* device, int fd) { return 0; }
163
adev_close(hw_device_t * device)164 static int adev_close(hw_device_t* device) {
165 auto* bluetooth_device = reinterpret_cast<BluetoothAudioDevice*>(device);
166 delete bluetooth_device;
167 return 0;
168 }
169
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)170 static int adev_open(const hw_module_t* module, const char* name,
171 hw_device_t** device) {
172 LOG(VERBOSE) << __func__ << ": name=[" << name << "]";
173 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
174
175 auto bluetooth_audio_device = new BluetoothAudioDevice{};
176 struct audio_hw_device* adev = &bluetooth_audio_device->audio_device_;
177 if (!adev) return -ENOMEM;
178
179 adev->common.tag = HARDWARE_DEVICE_TAG;
180 adev->common.version = AUDIO_DEVICE_API_VERSION_3_2;
181 adev->common.module = (struct hw_module_t*)module;
182 adev->common.close = adev_close;
183
184 adev->init_check = adev_init_check;
185 adev->set_voice_volume = adev_set_voice_volume;
186 adev->set_master_volume = adev_set_master_volume;
187 adev->get_master_volume = adev_get_master_volume;
188 adev->set_mode = adev_set_mode;
189 adev->set_mic_mute = adev_set_mic_mute;
190 adev->get_mic_mute = adev_get_mic_mute;
191 adev->set_parameters = adev_set_parameters;
192 adev->get_parameters = adev_get_parameters;
193 adev->get_input_buffer_size = adev_get_input_buffer_size;
194 adev->open_output_stream = adev_open_output_stream;
195 adev->close_output_stream = adev_close_output_stream;
196 adev->open_input_stream = adev_open_input_stream;
197 adev->close_input_stream = adev_close_input_stream;
198 adev->dump = adev_dump;
199 adev->set_master_mute = adev_set_master_mute;
200 adev->get_master_mute = adev_get_master_mute;
201 adev->create_audio_patch = adev_create_audio_patch;
202 adev->release_audio_patch = adev_release_audio_patch;
203 adev->get_audio_port_v7 = adev_get_audio_port_v7;
204 adev->get_audio_port = adev_get_audio_port;
205
206 *device = &adev->common;
207 return 0;
208 }
209
210 static struct hw_module_methods_t hal_module_methods = {
211 .open = adev_open,
212 };
213
214 struct audio_module HAL_MODULE_INFO_SYM = {
215 .common =
216 {
217 .tag = HARDWARE_MODULE_TAG,
218 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
219 .hal_api_version = HARDWARE_HAL_API_VERSION,
220 .id = AUDIO_HARDWARE_MODULE_ID,
221 .name = "Bluetooth Audio HW HAL",
222 .author = "The Android Open Source Project",
223 .methods = &hal_module_methods,
224 },
225 };
226