1 /* Spa Bluez5 Monitor
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_BLUEZ5_DEFS_H
26 #define SPA_BLUEZ5_DEFS_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <math.h>
33 
34 #include <spa/support/dbus.h>
35 #include <spa/support/log.h>
36 #include <spa/support/loop.h>
37 #include <spa/support/plugin.h>
38 #include <spa/monitor/device.h>
39 #include <spa/utils/hook.h>
40 
41 #include <dbus/dbus.h>
42 
43 #include "config.h"
44 
45 #define BLUEZ_SERVICE "org.bluez"
46 #define BLUEZ_PROFILE_MANAGER_INTERFACE BLUEZ_SERVICE ".ProfileManager1"
47 #define BLUEZ_PROFILE_INTERFACE BLUEZ_SERVICE ".Profile1"
48 #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter1"
49 #define BLUEZ_DEVICE_INTERFACE BLUEZ_SERVICE ".Device1"
50 #define BLUEZ_MEDIA_INTERFACE BLUEZ_SERVICE ".Media1"
51 #define BLUEZ_MEDIA_ENDPOINT_INTERFACE BLUEZ_SERVICE ".MediaEndpoint1"
52 #define BLUEZ_MEDIA_TRANSPORT_INTERFACE BLUEZ_SERVICE ".MediaTransport1"
53 #define BLUEZ_INTERFACE_BATTERY_PROVIDER BLUEZ_SERVICE ".BatteryProvider1"
54 #define BLUEZ_INTERFACE_BATTERY_PROVIDER_MANAGER BLUEZ_SERVICE ".BatteryProviderManager1"
55 
56 #define DBUS_INTERFACE_OBJECT_MANAGER "org.freedesktop.DBus.ObjectManager"
57 #define DBUS_SIGNAL_INTERFACES_ADDED "InterfacesAdded"
58 #define DBUS_SIGNAL_INTERFACES_REMOVED "InterfacesRemoved"
59 #define DBUS_SIGNAL_PROPERTIES_CHANGED "PropertiesChanged"
60 
61 #define PIPEWIRE_BATTERY_PROVIDER "/org/freedesktop/pipewire/battery"
62 
63 #define MIN_LATENCY	512
64 #define MAX_LATENCY	1024
65 
66 #define OBJECT_MANAGER_INTROSPECT_XML                                          \
67 	DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                                  \
68 	"<node>\n"                                                                 \
69 	" <interface name=\"org.freedesktop.DBus.ObjectManager\">\n"               \
70 	"  <method name=\"GetManagedObjects\">\n"                                  \
71 	"   <arg name=\"objects\" direction=\"out\" type=\"a{oa{sa{sv}}}\"/>\n"    \
72 	"  </method>\n"                                                            \
73 	"  <signal name=\"InterfacesAdded\">\n"                                    \
74 	"   <arg name=\"object\" type=\"o\"/>\n"                                   \
75 	"   <arg name=\"interfaces\" type=\"a{sa{sv}}\"/>\n"                       \
76 	"  </signal>\n"                                                            \
77 	"  <signal name=\"InterfacesRemoved\">\n"                                  \
78 	"   <arg name=\"object\" type=\"o\"/>\n"                                   \
79 	"   <arg name=\"interfaces\" type=\"as\"/>\n"                              \
80 	"  </signal>\n"                                                            \
81 	" </interface>\n"                                                          \
82 	" <interface name=\"org.freedesktop.DBus.Introspectable\">\n"              \
83 	"  <method name=\"Introspect\">\n"                                         \
84 	"   <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"                   \
85 	"  </method>\n"                                                            \
86 	" </interface>\n"                                                          \
87 	" <node name=\"A2DPSink\"/>\n"                                             \
88 	" <node name=\"A2DPSource\"/>\n"                                           \
89 	"</node>\n"
90 
91 #define ENDPOINT_INTROSPECT_XML                                             \
92 	DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                           \
93 	"<node>"                                                            \
94 	" <interface name=\"" BLUEZ_MEDIA_ENDPOINT_INTERFACE "\">"          \
95 	"  <method name=\"SetConfiguration\">"                              \
96 	"   <arg name=\"transport\" direction=\"in\" type=\"o\"/>"          \
97 	"   <arg name=\"properties\" direction=\"in\" type=\"ay\"/>"        \
98 	"  </method>"                                                       \
99 	"  <method name=\"SelectConfiguration\">"                           \
100 	"   <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>"      \
101 	"   <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>"    \
102 	"  </method>"                                                       \
103 	"  <method name=\"ClearConfiguration\">"                            \
104 	"   <arg name=\"transport\" direction=\"in\" type=\"o\"/>"          \
105 	"  </method>"                                                       \
106 	"  <method name=\"Release\">"                                       \
107 	"  </method>"                                                       \
108 	" </interface>"                                                     \
109 	" <interface name=\"org.freedesktop.DBus.Introspectable\">"         \
110 	"  <method name=\"Introspect\">"                                    \
111 	"   <arg name=\"data\" type=\"s\" direction=\"out\"/>"              \
112 	"  </method>"                                                       \
113 	" </interface>"                                                     \
114 	"</node>"
115 
116 #define PROFILE_INTROSPECT_XML						    \
117 	DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                           \
118 	"<node>"                                                            \
119 	" <interface name=\"" BLUEZ_PROFILE_INTERFACE "\">"                 \
120 	"  <method name=\"Release\">"                                       \
121 	"  </method>"                                                       \
122 	"  <method name=\"RequestDisconnection\">"                          \
123 	"   <arg name=\"device\" direction=\"in\" type=\"o\"/>"             \
124 	"  </method>"                                                       \
125 	"  <method name=\"NewConnection\">"                                 \
126 	"   <arg name=\"device\" direction=\"in\" type=\"o\"/>"             \
127 	"   <arg name=\"fd\" direction=\"in\" type=\"h\"/>"                 \
128 	"   <arg name=\"opts\" direction=\"in\" type=\"a{sv}\"/>"           \
129 	"  </method>"                                                       \
130 	" </interface>"                                                     \
131 	" <interface name=\"org.freedesktop.DBus.Introspectable\">"         \
132 	"  <method name=\"Introspect\">"                                    \
133 	"   <arg name=\"data\" type=\"s\" direction=\"out\"/>"              \
134 	"  </method>"                                                       \
135 	" </interface>"                                                     \
136 	"</node>"
137 
138 #define BLUEZ_ERROR_NOT_SUPPORTED "org.bluez.Error.NotSupported"
139 
140 #define SPA_BT_UUID_A2DP_SOURCE "0000110A-0000-1000-8000-00805F9B34FB"
141 #define SPA_BT_UUID_A2DP_SINK   "0000110B-0000-1000-8000-00805F9B34FB"
142 #define SPA_BT_UUID_HSP_HS      "00001108-0000-1000-8000-00805F9B34FB"
143 #define SPA_BT_UUID_HSP_HS_ALT  "00001131-0000-1000-8000-00805F9B34FB"
144 #define SPA_BT_UUID_HSP_AG      "00001112-0000-1000-8000-00805F9B34FB"
145 #define SPA_BT_UUID_HFP_HF      "0000111E-0000-1000-8000-00805F9B34FB"
146 #define SPA_BT_UUID_HFP_AG      "0000111F-0000-1000-8000-00805F9B34FB"
147 
148 #define PROFILE_HSP_AG	"/Profile/HSPAG"
149 #define PROFILE_HSP_HS	"/Profile/HSPHS"
150 #define PROFILE_HFP_AG	"/Profile/HFPAG"
151 #define PROFILE_HFP_HF	"/Profile/HFPHF"
152 
153 #define HSP_HS_DEFAULT_CHANNEL  3
154 
155 #define SOURCE_ID_BLUETOOTH	0x1	/* Bluetooth SIG */
156 #define SOURCE_ID_USB		0x2	/* USB Implementer's Forum */
157 
158 #define BUS_TYPE_USB		1
159 #define BUS_TYPE_OTHER		255
160 
161 #define HFP_AUDIO_CODEC_CVSD    0x01
162 #define HFP_AUDIO_CODEC_MSBC    0x02
163 
164 #define A2DP_OBJECT_MANAGER_PATH "/MediaEndpoint"
165 #define A2DP_SINK_ENDPOINT	A2DP_OBJECT_MANAGER_PATH "/A2DPSink"
166 #define A2DP_SOURCE_ENDPOINT	A2DP_OBJECT_MANAGER_PATH "/A2DPSource"
167 
168 #define SPA_BT_UNKNOWN_DELAY			0
169 
170 #define SPA_BT_NO_BATTERY			((uint8_t)255)
171 
172 /* HFP uses SBC encoding with precisely defined parameters. Hence, the size
173  * of the input (number of PCM samples) and output is known up front. */
174 #define MSBC_DECODED_SIZE       240
175 #define MSBC_ENCODED_SIZE       60  /* 2 bytes header + 57 mSBC payload + 1 byte padding */
176 #define MSBC_PAYLOAD_SIZE       57
177 
178 enum spa_bt_profile {
179 	SPA_BT_PROFILE_NULL =		0,
180 	SPA_BT_PROFILE_A2DP_SINK =	(1 << 0),
181 	SPA_BT_PROFILE_A2DP_SOURCE =	(1 << 1),
182 	SPA_BT_PROFILE_HSP_HS =		(1 << 2),
183 	SPA_BT_PROFILE_HSP_AG =		(1 << 3),
184 	SPA_BT_PROFILE_HFP_HF =		(1 << 4),
185 	SPA_BT_PROFILE_HFP_AG =		(1 << 5),
186 
187 	SPA_BT_PROFILE_A2DP_DUPLEX =	(SPA_BT_PROFILE_A2DP_SINK | SPA_BT_PROFILE_A2DP_SOURCE),
188 	SPA_BT_PROFILE_HEADSET_HEAD_UNIT = (SPA_BT_PROFILE_HSP_HS | SPA_BT_PROFILE_HFP_HF),
189 	SPA_BT_PROFILE_HEADSET_AUDIO_GATEWAY = (SPA_BT_PROFILE_HSP_AG | SPA_BT_PROFILE_HFP_AG),
190 	SPA_BT_PROFILE_HEADSET_AUDIO =  (SPA_BT_PROFILE_HEADSET_HEAD_UNIT | SPA_BT_PROFILE_HEADSET_AUDIO_GATEWAY),
191 };
192 
spa_bt_profile_from_uuid(const char * uuid)193 static inline enum spa_bt_profile spa_bt_profile_from_uuid(const char *uuid)
194 {
195 	if (strcasecmp(uuid, SPA_BT_UUID_A2DP_SOURCE) == 0)
196 		return SPA_BT_PROFILE_A2DP_SOURCE;
197 	else if (strcasecmp(uuid, SPA_BT_UUID_A2DP_SINK) == 0)
198 		return SPA_BT_PROFILE_A2DP_SINK;
199 	else if (strcasecmp(uuid, SPA_BT_UUID_HSP_HS) == 0)
200 		return SPA_BT_PROFILE_HSP_HS;
201 	else if (strcasecmp(uuid, SPA_BT_UUID_HSP_HS_ALT) == 0)
202 		return SPA_BT_PROFILE_HSP_HS;
203 	else if (strcasecmp(uuid, SPA_BT_UUID_HSP_AG) == 0)
204 		return SPA_BT_PROFILE_HSP_AG;
205 	else if (strcasecmp(uuid, SPA_BT_UUID_HFP_HF) == 0)
206 		return SPA_BT_PROFILE_HFP_HF;
207 	else if (strcasecmp(uuid, SPA_BT_UUID_HFP_AG) == 0)
208 		return SPA_BT_PROFILE_HFP_AG;
209 	else
210 		return 0;
211 }
212 int spa_bt_profiles_from_json_array(const char *str);
213 
214 int spa_bt_format_vendor_product_id(uint16_t source_id, uint16_t vendor_id,
215 		uint16_t product_id, char *vendor_str, int vendor_str_size,
216 		char *product_str, int product_str_size);
217 
218 enum spa_bt_hfp_ag_feature {
219 	SPA_BT_HFP_AG_FEATURE_NONE =			(0),
220 	SPA_BT_HFP_AG_FEATURE_3WAY =			(1 << 0),
221 	SPA_BT_HFP_AG_FEATURE_ECNR =			(1 << 1),
222 	SPA_BT_HFP_AG_FEATURE_VOICE_RECOG =		(1 << 2),
223 	SPA_BT_HFP_AG_FEATURE_IN_BAND_RING_TONE =	(1 << 3),
224 	SPA_BT_HFP_AG_FEATURE_ATTACH_VOICE_TAG =	(1 << 4),
225 	SPA_BT_HFP_AG_FEATURE_REJECT_CALL =		(1 << 5),
226 	SPA_BT_HFP_AG_FEATURE_ENHANCED_CALL_STATUS =	(1 << 6),
227 	SPA_BT_HFP_AG_FEATURE_ENHANCED_CALL_CONTROL =	(1 << 7),
228 	SPA_BT_HFP_AG_FEATURE_EXTENDED_RES_CODE =	(1 << 8),
229 	SPA_BT_HFP_AG_FEATURE_CODEC_NEGOTIATION =	(1 << 9),
230 	SPA_BT_HFP_AG_FEATURE_HF_INDICATORS =		(1 << 10),
231 	SPA_BT_HFP_AG_FEATURE_ESCO_S4 =			(1 << 11),
232 };
233 
234 enum spa_bt_hfp_sdp_ag_features {
235 	SPA_BT_HFP_SDP_AG_FEATURE_NONE =		(0),
236 	SPA_BT_HFP_SDP_AG_FEATURE_3WAY =		(1 << 0),
237 	SPA_BT_HFP_SDP_AG_FEATURE_ECNR =		(1 << 1),
238 	SPA_BT_HFP_SDP_AG_FEATURE_VOICE_RECOG =		(1 << 2),
239 	SPA_BT_HFP_SDP_AG_FEATURE_IN_BAND_RING_TONE =	(1 << 3),
240 	SPA_BT_HFP_SDP_AG_FEATURE_ATTACH_VOICE_TAG =	(1 << 4),
241 	SPA_BT_HFP_SDP_AG_FEATURE_WIDEBAND_SPEECH =	(1 << 5),
242 };
243 
244 enum spa_bt_hfp_hf_feature {
245 	SPA_BT_HFP_HF_FEATURE_NONE =			(0),
246 	SPA_BT_HFP_HF_FEATURE_ECNR =			(1 << 0),
247 	SPA_BT_HFP_HF_FEATURE_3WAY =			(1 << 1),
248 	SPA_BT_HFP_HF_FEATURE_CLIP =			(1 << 2),
249 	SPA_BT_HFP_HF_FEATURE_VOICE_RECOGNITION =	(1 << 3),
250 	SPA_BT_HFP_HF_FEATURE_REMOTE_VOLUME_CONTROL =	(1 << 4),
251 	SPA_BT_HFP_HF_FEATURE_ENHANCED_CALL_STATUS =	(1 << 5),
252 	SPA_BT_HFP_HF_FEATURE_ENHANCED_CALL_CONTROL =	(1 << 6),
253 	SPA_BT_HFP_HF_FEATURE_CODEC_NEGOTIATION =	(1 << 7),
254 	SPA_BT_HFP_HF_FEATURE_HF_INDICATORS =		(1 << 8),
255 	SPA_BT_HFP_HF_FEATURE_ESCO_S4 =			(1 << 9),
256 };
257 
258 /* https://btprodspecificationrefs.blob.core.windows.net/assigned-numbers/Assigned%20Number%20Types/Hands-Free%20Profile.pdf */
259 enum spa_bt_hfp_hf_indicator {
260 	SPA_BT_HFP_HF_INDICATOR_ENHANCED_SAFETY =        1,
261 	SPA_BT_HFP_HF_INDICATOR_BATTERY_LEVEL =          2,
262 };
263 
264 /* HFP Command AT+IPHONEACCEV
265  *  https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf */
266 enum spa_bt_hfp_hf_iphoneaccev_keys {
267 	SPA_BT_HFP_HF_IPHONEACCEV_KEY_BATTERY_LEVEL =    1,
268 	SPA_BT_HFP_HF_IPHONEACCEV_KEY_DOCK_STATE =       2,
269 };
270 
271 /* HFP Command AT+XAPL
272  *  https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf
273  * Bits 0, 5 and above are reserved. */
274 enum spa_bt_hfp_hf_xapl_features {
275 	SPA_BT_HFP_HF_XAPL_FEATURE_BATTERY_REPORTING =         (1 << 1),
276 	SPA_BT_HFP_HF_XAPL_FEATURE_DOCKED_OR_POWERED =         (1 << 2),
277 	SPA_BT_HFP_HF_XAPL_FEATURE_SIRI_STATUS_REPORTING =     (1 << 3),
278 	SPA_BT_HFP_HF_XAPL_FEATURE_NOISE_REDUCTION_REPORTING = (1 << 4),
279 };
280 
281 enum spa_bt_hfp_sdp_hf_features {
282 	SPA_BT_HFP_SDP_HF_FEATURE_NONE =		(0),
283 	SPA_BT_HFP_SDP_HF_FEATURE_ECNR =		(1 << 0),
284 	SPA_BT_HFP_SDP_HF_FEATURE_3WAY =		(1 << 1),
285 	SPA_BT_HFP_SDP_HF_FEATURE_CLIP =		(1 << 2),
286 	SPA_BT_HFP_SDP_HF_FEATURE_VOICE_RECOGNITION =	(1 << 3),
287 	SPA_BT_HFP_SDP_HF_FEATURE_REMOTE_VOLUME_CONTROL =	(1 << 4),
288 	SPA_BT_HFP_SDP_HF_FEATURE_WIDEBAND_SPEECH =	(1 << 5),
289 };
290 
spa_bt_profile_name(enum spa_bt_profile profile)291 static inline const char *spa_bt_profile_name (enum spa_bt_profile profile) {
292       switch (profile) {
293       case SPA_BT_PROFILE_A2DP_SOURCE:
294         return "a2dp-source";
295       case SPA_BT_PROFILE_A2DP_SINK:
296         return "a2dp-sink";
297       case SPA_BT_PROFILE_A2DP_DUPLEX:
298         return "a2dp-duplex";
299       case SPA_BT_PROFILE_HSP_HS:
300       case SPA_BT_PROFILE_HFP_HF:
301       case SPA_BT_PROFILE_HEADSET_HEAD_UNIT:
302 	return "headset-head-unit";
303       case SPA_BT_PROFILE_HSP_AG:
304       case SPA_BT_PROFILE_HFP_AG:
305       case SPA_BT_PROFILE_HEADSET_AUDIO_GATEWAY:
306 	return "headset-audio-gateway";
307       case SPA_BT_PROFILE_HEADSET_AUDIO:
308 	return "headset-audio";
309       default:
310         break;
311       }
312       return "unknown";
313 }
314 
315 struct spa_bt_monitor;
316 struct spa_bt_backend;
317 struct spa_bt_player;
318 
319 struct spa_bt_adapter {
320 	struct spa_list link;
321 	struct spa_bt_monitor *monitor;
322 	struct spa_bt_player *dummy_player;
323 	char *path;
324 	char *alias;
325 	char *address;
326 	char *name;
327 	int bus_type;
328 	uint16_t source_id;
329 	uint16_t vendor_id;
330 	uint16_t product_id;
331 	uint16_t version_id;
332 	uint32_t bluetooth_class;
333 	uint32_t profiles;
334 	int powered;
335 	unsigned int endpoints_registered:1;
336 	unsigned int application_registered:1;
337 	unsigned int player_registered:1;
338 	unsigned int has_battery_provider;
339 	unsigned int battery_provider_unavailable;
340 };
341 
342 enum spa_bt_form_factor {
343 	SPA_BT_FORM_FACTOR_UNKNOWN,
344 	SPA_BT_FORM_FACTOR_HEADSET,
345 	SPA_BT_FORM_FACTOR_HANDSFREE,
346 	SPA_BT_FORM_FACTOR_MICROPHONE,
347 	SPA_BT_FORM_FACTOR_SPEAKER,
348 	SPA_BT_FORM_FACTOR_HEADPHONE,
349 	SPA_BT_FORM_FACTOR_PORTABLE,
350 	SPA_BT_FORM_FACTOR_CAR,
351 	SPA_BT_FORM_FACTOR_HIFI,
352 	SPA_BT_FORM_FACTOR_PHONE,
353 };
354 
spa_bt_form_factor_name(enum spa_bt_form_factor ff)355 static inline const char *spa_bt_form_factor_name(enum spa_bt_form_factor ff)
356 {
357 	switch (ff) {
358 	case SPA_BT_FORM_FACTOR_HEADSET:
359 		return "headset";
360 	case SPA_BT_FORM_FACTOR_HANDSFREE:
361 		return "hands-free";
362 	case SPA_BT_FORM_FACTOR_MICROPHONE:
363 		return "microphone";
364 	case SPA_BT_FORM_FACTOR_SPEAKER:
365 		return "speaker";
366 	case SPA_BT_FORM_FACTOR_HEADPHONE:
367 		return "headphone";
368 	case SPA_BT_FORM_FACTOR_PORTABLE:
369 		return "portable";
370 	case SPA_BT_FORM_FACTOR_CAR:
371 		return "car";
372 	case SPA_BT_FORM_FACTOR_HIFI:
373 		return "hifi";
374 	case SPA_BT_FORM_FACTOR_PHONE:
375 		return "phone";
376 	case SPA_BT_FORM_FACTOR_UNKNOWN:
377 	default:
378 		return "unknown";
379 	}
380 }
381 
spa_bt_form_factor_from_class(uint32_t bluetooth_class)382 static inline enum spa_bt_form_factor spa_bt_form_factor_from_class(uint32_t bluetooth_class)
383 {
384 	uint32_t major, minor;
385 	/* See Bluetooth Assigned Numbers:
386 	 * https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm */
387 	major = (bluetooth_class >> 8) & 0x1F;
388 	minor = (bluetooth_class >> 2) & 0x3F;
389 
390 	switch (major) {
391 	case 2:
392 		return SPA_BT_FORM_FACTOR_PHONE;
393 	case 4:
394 		switch (minor) {
395 		case 1:
396 			return SPA_BT_FORM_FACTOR_HEADSET;
397 		case 2:
398 			return SPA_BT_FORM_FACTOR_HANDSFREE;
399 		case 4:
400 			return SPA_BT_FORM_FACTOR_MICROPHONE;
401 		case 5:
402 			return SPA_BT_FORM_FACTOR_SPEAKER;
403 		case 6:
404 			return SPA_BT_FORM_FACTOR_HEADPHONE;
405 		case 7:
406 			return SPA_BT_FORM_FACTOR_PORTABLE;
407 		case 8:
408 			return SPA_BT_FORM_FACTOR_CAR;
409 		case 10:
410 			return SPA_BT_FORM_FACTOR_HIFI;
411 		}
412 	}
413 	return SPA_BT_FORM_FACTOR_UNKNOWN;
414 }
415 
416 struct spa_bt_a2dp_codec_switch;
417 struct spa_bt_transport;
418 
419 struct spa_bt_device_events {
420 #define SPA_VERSION_BT_DEVICE_EVENTS	0
421 	uint32_t version;
422 
423 	/** Device connection status */
424 	void (*connected) (void *data, bool connected);
425 
426 	/** Codec switching completed */
427 	void (*codec_switched) (void *data, int status);
428 
429 	/** Profile configuration changed */
430 	void (*profiles_changed) (void *data, uint32_t prev_profiles, uint32_t prev_connected);
431 
432 	/** Device freed */
433 	void (*destroy) (void *data);
434 };
435 
436 struct spa_bt_device {
437 	struct spa_list link;
438 	struct spa_bt_monitor *monitor;
439 	struct spa_bt_adapter *adapter;
440 	uint32_t id;
441 	char *path;
442 	char *alias;
443 	char *address;
444 	char *adapter_path;
445 	char *battery_path;
446 	char *name;
447 	char *icon;
448 	uint16_t source_id;
449 	uint16_t vendor_id;
450 	uint16_t product_id;
451 	uint16_t version_id;
452 	uint32_t bluetooth_class;
453 	uint16_t appearance;
454 	uint16_t RSSI;
455 	int paired;
456 	int trusted;
457 	int connected;
458 	int blocked;
459 	uint32_t profiles;
460 	uint32_t connected_profiles;
461 	uint32_t reconnect_profiles;
462 	int reconnect_state;
463 	struct spa_source timer;
464 	struct spa_list remote_endpoint_list;
465 	struct spa_list transport_list;
466 	struct spa_list codec_switch_list;
467 	uint8_t battery;
468 	int has_battery;
469 
470 	uint32_t hw_volume_profiles;
471 	/* Even though A2DP volume is exposed on transport interface, the
472 	 * volume activation info would not be variate between transports
473 	 * under same device. So it's safe to cache activation info here. */
474 	bool a2dp_volume_active[2];
475 
476 	uint64_t last_bluez_action_time;
477 
478 	struct spa_hook_list listener_list;
479 	bool added;
480 
481 	const struct spa_dict *settings;
482 
483 	DBusPendingCall *battery_pending_call;
484 };
485 
486 struct a2dp_codec;
487 
488 struct spa_bt_device *spa_bt_device_find(struct spa_bt_monitor *monitor, const char *path);
489 struct spa_bt_device *spa_bt_device_find_by_address(struct spa_bt_monitor *monitor, const char *remote_address, const char *local_address);
490 int spa_bt_device_add_profile(struct spa_bt_device *device, enum spa_bt_profile profile);
491 int spa_bt_device_connect_profile(struct spa_bt_device *device, enum spa_bt_profile profile);
492 int spa_bt_device_check_profiles(struct spa_bt_device *device, bool force);
493 int spa_bt_device_ensure_a2dp_codec(struct spa_bt_device *device, const struct a2dp_codec * const *codecs);
494 bool spa_bt_device_supports_a2dp_codec(struct spa_bt_device *device, const struct a2dp_codec *codec);
495 const struct a2dp_codec **spa_bt_device_get_supported_a2dp_codecs(struct spa_bt_device *device, size_t *count);
496 int spa_bt_device_ensure_hfp_codec(struct spa_bt_device *device, unsigned int codec);
497 int spa_bt_device_supports_hfp_codec(struct spa_bt_device *device, unsigned int codec);
498 int spa_bt_device_release_transports(struct spa_bt_device *device);
499 int spa_bt_device_report_battery_level(struct spa_bt_device *device, uint8_t percentage);
500 
501 #define spa_bt_device_emit(d,m,v,...)			spa_hook_list_call(&(d)->listener_list, \
502 								struct spa_bt_device_events,	\
503 								m, v, ##__VA_ARGS__)
504 #define spa_bt_device_emit_connected(d,...)	        spa_bt_device_emit(d, connected, 0, __VA_ARGS__)
505 #define spa_bt_device_emit_codec_switched(d,...)	spa_bt_device_emit(d, codec_switched, 0, __VA_ARGS__)
506 #define spa_bt_device_emit_profiles_changed(d,...)	spa_bt_device_emit(d, profiles_changed, 0, __VA_ARGS__)
507 #define spa_bt_device_emit_destroy(d)			spa_bt_device_emit(d, destroy, 0)
508 #define spa_bt_device_add_listener(d,listener,events,data)           \
509 	spa_hook_list_append(&(d)->listener_list, listener, events, data)
510 
511 struct spa_bt_sco_io;
512 
513 struct spa_bt_sco_io *spa_bt_sco_io_create(struct spa_loop *data_loop, int fd, uint16_t read_mtu, uint16_t write_mtu);
514 void spa_bt_sco_io_destroy(struct spa_bt_sco_io *io);
515 void spa_bt_sco_io_set_source_cb(struct spa_bt_sco_io *io, int (*source_cb)(void *userdata, uint8_t *data, int size), void *userdata);
516 void spa_bt_sco_io_set_sink_cb(struct spa_bt_sco_io *io, int (*sink_cb)(void *userdata), void *userdata);
517 int spa_bt_sco_io_write(struct spa_bt_sco_io *io, uint8_t *data, int size);
518 
519 #define SPA_BT_VOLUME_ID_RX	0
520 #define SPA_BT_VOLUME_ID_TX	1
521 #define SPA_BT_VOLUME_ID_TERM	2
522 
523 #define SPA_BT_VOLUME_INVALID	-1
524 #define SPA_BT_VOLUME_HS_MAX	15
525 #define SPA_BT_VOLUME_A2DP_MAX	127
526 
527 enum spa_bt_transport_state {
528         SPA_BT_TRANSPORT_STATE_IDLE,
529         SPA_BT_TRANSPORT_STATE_PENDING,
530         SPA_BT_TRANSPORT_STATE_ACTIVE,
531 };
532 
533 struct spa_bt_transport_events {
534 #define SPA_VERSION_BT_TRANSPORT_EVENTS	0
535 	uint32_t version;
536 
537 	void (*destroy) (void *data);
538 	void (*delay_changed) (void *data);
539 	void (*state_changed) (void *data, enum spa_bt_transport_state old,
540 			enum spa_bt_transport_state state);
541 	void (*volume_changed) (void *data);
542 };
543 
544 struct spa_bt_transport_implementation {
545 #define SPA_VERSION_BT_TRANSPORT_IMPLEMENTATION	0
546 	uint32_t version;
547 
548 	int (*acquire) (void *data, bool optional);
549 	int (*release) (void *data);
550 	int (*set_volume) (void *data, int id, float volume);
551 	int (*destroy) (void *data);
552 };
553 
554 struct spa_bt_transport_volume {
555 	bool active;
556 	float volume;
557 	int hw_volume_max;
558 
559 	/* XXX: items below should be put to user_data */
560 	int hw_volume;
561 	int new_hw_volume;
562 };
563 
564 struct spa_bt_transport {
565 	struct spa_list link;
566 	struct spa_bt_monitor *monitor;
567 	struct spa_bt_backend *backend;
568 	char *path;
569 	struct spa_bt_device *device;
570 	struct spa_list device_link;
571 	enum spa_bt_profile profile;
572 	enum spa_bt_transport_state state;
573 	const struct a2dp_codec *a2dp_codec;
574 	unsigned int codec;
575 	void *configuration;
576 	int configuration_len;
577 
578 	uint32_t n_channels;
579 	uint32_t channels[64];
580 
581 	struct spa_bt_transport_volume volumes[SPA_BT_VOLUME_ID_TERM];
582 
583 	int acquire_refcount;
584 	int fd;
585 	uint16_t read_mtu;
586 	uint16_t write_mtu;
587 	uint16_t delay;
588 
589 	struct spa_bt_sco_io *sco_io;
590 
591 	struct spa_source volume_timer;
592 	struct spa_source release_timer;
593 
594 	struct spa_hook_list listener_list;
595 	struct spa_callbacks impl;
596 
597 	/* user_data must be the last item in the struct */
598 	void *user_data;
599 };
600 
601 struct spa_bt_transport *spa_bt_transport_create(struct spa_bt_monitor *monitor, char *path, size_t extra);
602 void spa_bt_transport_free(struct spa_bt_transport *transport);
603 void spa_bt_transport_set_state(struct spa_bt_transport *transport, enum spa_bt_transport_state state);
604 struct spa_bt_transport *spa_bt_transport_find(struct spa_bt_monitor *monitor, const char *path);
605 struct spa_bt_transport *spa_bt_transport_find_full(struct spa_bt_monitor *monitor,
606                                                     bool (*callback) (struct spa_bt_transport *t, const void *data),
607                                                     const void *data);
608 int64_t spa_bt_transport_get_delay_nsec(struct spa_bt_transport *transport);
609 bool spa_bt_transport_volume_enabled(struct spa_bt_transport *transport);
610 
611 int spa_bt_transport_acquire(struct spa_bt_transport *t, bool optional);
612 int spa_bt_transport_release(struct spa_bt_transport *t);
613 int spa_bt_transport_ensure_sco_io(struct spa_bt_transport *t, struct spa_loop *data_loop);
614 
615 #define spa_bt_transport_emit(t,m,v,...)		spa_hook_list_call(&(t)->listener_list, \
616 								struct spa_bt_transport_events,	\
617 								m, v, ##__VA_ARGS__)
618 #define spa_bt_transport_emit_destroy(t)		spa_bt_transport_emit(t, destroy, 0)
619 #define spa_bt_transport_emit_delay_changed(t)		spa_bt_transport_emit(t, delay_changed, 0)
620 #define spa_bt_transport_emit_state_changed(t,...)	spa_bt_transport_emit(t, state_changed, 0, __VA_ARGS__)
621 #define spa_bt_transport_emit_volume_changed(t)		spa_bt_transport_emit(t, volume_changed, 0)
622 
623 #define spa_bt_transport_add_listener(t,listener,events,data) \
624         spa_hook_list_append(&(t)->listener_list, listener, events, data)
625 
626 #define spa_bt_transport_set_implementation(t,_impl,_data) \
627 			(t)->impl = SPA_CALLBACKS_INIT(_impl, _data)
628 
629 #define spa_bt_transport_impl(t,m,v,...)		\
630 ({							\
631 	int res = 0;					\
632 	spa_callbacks_call_res(&(t)->impl,		\
633 		struct spa_bt_transport_implementation,	\
634 		res, m, v, ##__VA_ARGS__);		\
635 	res;						\
636 })
637 
638 #define spa_bt_transport_destroy(t)		spa_bt_transport_impl(t, destroy, 0)
639 #define spa_bt_transport_set_volume(t,...)	spa_bt_transport_impl(t, set_volume, 0, __VA_ARGS__)
640 
spa_bt_transport_state_from_string(const char * value)641 static inline enum spa_bt_transport_state spa_bt_transport_state_from_string(const char *value)
642 {
643 	if (strcasecmp("idle", value) == 0)
644 		return SPA_BT_TRANSPORT_STATE_IDLE;
645 	else if (strcasecmp("pending", value) == 0)
646 		return SPA_BT_TRANSPORT_STATE_PENDING;
647 	else if (strcasecmp("active", value) == 0)
648 		return SPA_BT_TRANSPORT_STATE_ACTIVE;
649 	else
650 		return SPA_BT_TRANSPORT_STATE_IDLE;
651 }
652 
653 #define DEFAULT_AG_VOLUME	1.0f
654 #define DEFAULT_RX_VOLUME	1.0f
655 #define DEFAULT_TX_VOLUME	0.064f /* spa_bt_volume_hw_to_linear(40, 100) */
656 
657 /* AVRCP/HSP volume is considered as percentage, so map it to pulseaudio (cubic) volume. */
spa_bt_volume_linear_to_hw(double v,uint32_t hw_volume_max)658 static inline uint32_t spa_bt_volume_linear_to_hw(double v, uint32_t hw_volume_max)
659 {
660 	if (v <= 0.0)
661 		return 0;
662 	if (v >= 1.0)
663 		return hw_volume_max;
664 	return SPA_CLAMP((uint64_t) lround(cbrt(v) * hw_volume_max),
665 			 0u, hw_volume_max);
666 }
667 
spa_bt_volume_hw_to_linear(uint32_t v,uint32_t hw_volume_max)668 static inline double spa_bt_volume_hw_to_linear(uint32_t v, uint32_t hw_volume_max)
669 {
670 	double f;
671 	if (v <= 0)
672 		return 0.0;
673 	if (v >= hw_volume_max)
674 		return 1.0;
675 	f = ((double) v / hw_volume_max);
676 	return f * f * f;
677 }
678 
679 enum spa_bt_feature {
680 	SPA_BT_FEATURE_MSBC		= (1 << 0),
681 	SPA_BT_FEATURE_MSBC_ALT1	= (1 << 1),
682 	SPA_BT_FEATURE_MSBC_ALT1_RTL	= (1 << 2),
683 	SPA_BT_FEATURE_HW_VOLUME	= (1 << 3),
684 	SPA_BT_FEATURE_HW_VOLUME_MIC	= (1 << 4),
685 	SPA_BT_FEATURE_SBC_XQ		= (1 << 5),
686 	SPA_BT_FEATURE_FASTSTREAM	= (1 << 6),
687 	SPA_BT_FEATURE_A2DP_DUPLEX	= (1 << 7),
688 };
689 
690 struct spa_bt_quirks;
691 
692 struct spa_bt_quirks *spa_bt_quirks_create(const struct spa_dict *info, struct spa_log *log);
693 int spa_bt_quirks_get_features(const struct spa_bt_quirks *quirks,
694 		const struct spa_bt_adapter *adapter,
695 		const struct spa_bt_device *device,
696 		uint32_t *features);
697 void spa_bt_quirks_destroy(struct spa_bt_quirks *quirks);
698 
699 struct spa_bt_backend_implementation {
700 #define SPA_VERSION_BT_BACKEND_IMPLEMENTATION	0
701 	uint32_t version;
702 
703 	int (*free) (void *data);
704 	int (*register_profiles) (void *data);
705 	int (*unregister_profiles) (void *data);
706 	int (*ensure_codec) (void *data, struct spa_bt_device *device, unsigned int codec);
707 	int (*supports_codec) (void *data, struct spa_bt_device *device, unsigned int codec);
708 };
709 
710 struct spa_bt_backend {
711 	struct spa_callbacks impl;
712 	const char *name;
713 	bool available;
714 	bool exclusive;
715 };
716 
717 #define spa_bt_backend_set_implementation(b,_impl,_data) \
718 			(b)->impl = SPA_CALLBACKS_INIT(_impl, _data)
719 
720 #define spa_bt_backend_impl(b,m,v,...)				\
721 ({								\
722 	int res = -ENOTSUP;					\
723 	if (b)							\
724 		spa_callbacks_call_res(&(b)->impl,		\
725 			struct spa_bt_backend_implementation,	\
726 			res, m, v, ##__VA_ARGS__);		\
727 	res;							\
728 })
729 
730 #define spa_bt_backend_free(b)			spa_bt_backend_impl(b, free, 0)
731 #define spa_bt_backend_register_profiles(b)	spa_bt_backend_impl(b, register_profiles, 0)
732 #define spa_bt_backend_unregister_profiles(b)	spa_bt_backend_impl(b, unregister_profiles, 0)
733 #define spa_bt_backend_ensure_codec(b,...)	spa_bt_backend_impl(b, ensure_codec, 0, __VA_ARGS__)
734 #define spa_bt_backend_supports_codec(b,...)	spa_bt_backend_impl(b, supports_codec, 0, __VA_ARGS__)
735 
dummy_backend_new(struct spa_bt_monitor * monitor,void * dbus_connection,const struct spa_dict * info,const struct spa_bt_quirks * quirks,const struct spa_support * support,uint32_t n_support)736 static inline struct spa_bt_backend *dummy_backend_new(struct spa_bt_monitor *monitor,
737 		void *dbus_connection,
738 		const struct spa_dict *info,
739 		const struct spa_bt_quirks *quirks,
740 		const struct spa_support *support,
741 		uint32_t n_support)
742 {
743 	return NULL;
744 }
745 
746 #ifdef HAVE_BLUEZ_5_BACKEND_NATIVE
747 struct spa_bt_backend *backend_native_new(struct spa_bt_monitor *monitor,
748 		void *dbus_connection,
749 		const struct spa_dict *info,
750 		const struct spa_bt_quirks *quirks,
751 		const struct spa_support *support,
752 		uint32_t n_support);
753 #else
754 #define backend_native_new	dummy_backend_new
755 #endif
756 
757 #define OFONO_SERVICE "org.ofono"
758 #ifdef HAVE_BLUEZ_5_BACKEND_OFONO
759 struct spa_bt_backend *backend_ofono_new(struct spa_bt_monitor *monitor,
760 		void *dbus_connection,
761 		const struct spa_dict *info,
762 		const struct spa_bt_quirks *quirks,
763 		const struct spa_support *support,
764 		uint32_t n_support);
765 #else
766 #define backend_ofono_new	dummy_backend_new
767 #endif
768 
769 #define HSPHFPD_SERVICE "org.hsphfpd"
770 #ifdef HAVE_BLUEZ_5_BACKEND_HSPHFPD
771 struct spa_bt_backend *backend_hsphfpd_new(struct spa_bt_monitor *monitor,
772 		void *dbus_connection,
773 		const struct spa_dict *info,
774 		const struct spa_bt_quirks *quirks,
775 		const struct spa_support *support,
776 		uint32_t n_support);
777 #else
778 #define backend_hsphfpd_new	dummy_backend_new
779 #endif
780 
781 #ifdef __cplusplus
782 }  /* extern "C" */
783 #endif
784 
785 #endif /* SPA_BLUEZ5_DEFS_H */
786