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