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