bluez5-util.h 8.85 KB
Newer Older
1
2
3
4
5
6
7
#ifndef foobluez5utilhfoo
#define foobluez5utilhfoo

/***
  This file is part of PulseAudio.

  Copyright 2008-2013 João Paulo Rechi Vita
pali's avatar
pali committed
8
  Copyrigth 2018-2019 Pali Rohár <pali.rohar@gmail.com>
9
10
11
12
13
14
15
16
17
18
19
20

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
21
  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
22
23
24
25
***/

#include <pulsecore/core.h>

pali's avatar
pali committed
26
27
#include "a2dp-codec-util.h"

28
29
#define PA_BLUETOOTH_UUID_A2DP_SOURCE "0000110a-0000-1000-8000-00805f9b34fb"
#define PA_BLUETOOTH_UUID_A2DP_SINK   "0000110b-0000-1000-8000-00805f9b34fb"
30
31
32
33
34

/* There are two HSP HS UUIDs. The first one (older?) is used both as the HSP
 * profile identifier and as the HS role identifier, while the second one is
 * only used to identify the role. As far as PulseAudio is concerned, the two
 * UUIDs mean exactly the same thing. */
35
#define PA_BLUETOOTH_UUID_HSP_HS      "00001108-0000-1000-8000-00805f9b34fb"
36
37
#define PA_BLUETOOTH_UUID_HSP_HS_ALT  "00001131-0000-1000-8000-00805f9b34fb"

38
39
40
#define PA_BLUETOOTH_UUID_HSP_AG      "00001112-0000-1000-8000-00805f9b34fb"
#define PA_BLUETOOTH_UUID_HFP_HF      "0000111e-0000-1000-8000-00805f9b34fb"
#define PA_BLUETOOTH_UUID_HFP_AG      "0000111f-0000-1000-8000-00805f9b34fb"
41

42
typedef struct pa_bluetooth_transport pa_bluetooth_transport;
43
typedef struct pa_bluetooth_device pa_bluetooth_device;
44
typedef struct pa_bluetooth_adapter pa_bluetooth_adapter;
45
typedef struct pa_bluetooth_discovery pa_bluetooth_discovery;
46
typedef struct pa_bluetooth_backend pa_bluetooth_backend;
47

48
typedef enum pa_bluetooth_hook {
49
    PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED,          /* Call data: pa_bluetooth_device */
50
    PA_BLUETOOTH_HOOK_DEVICE_UNLINK,                      /* Call data: pa_bluetooth_device */
51
    PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED,            /* Call data: pa_bluetooth_transport */
52
53
    PA_BLUETOOTH_HOOK_TRANSPORT_MICROPHONE_GAIN_CHANGED,  /* Call data: pa_bluetooth_transport */
    PA_BLUETOOTH_HOOK_TRANSPORT_SPEAKER_GAIN_CHANGED,     /* Call data: pa_bluetooth_transport */
54
55
56
    PA_BLUETOOTH_HOOK_MAX
} pa_bluetooth_hook_t;

57
58
59
60
/* Profile index is used also for card profile priority. Higher number has higher priority.
 * All A2DP profiles have higher priority as all non-A2DP profiles.
 * And all A2DP sink profiles have higher priority as all A2DP source profiles. */
#define PA_BLUETOOTH_PROFILE_OFF                    0
61
62
63
64
65
#define PA_BLUETOOTH_PROFILE_HSP_AUDIO_GATEWAY      1
#define PA_BLUETOOTH_PROFILE_HFP_AUDIO_GATEWAY      2
#define PA_BLUETOOTH_PROFILE_HSP_HEAD_UNIT          3
#define PA_BLUETOOTH_PROFILE_HFP_HEAD_UNIT          4
#define PA_BLUETOOTH_PROFILE_A2DP_START_INDEX       5
66
typedef unsigned pa_bluetooth_profile_t;
67
68
69
70
71
72
73

typedef enum pa_bluetooth_transport_state {
    PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED,
    PA_BLUETOOTH_TRANSPORT_STATE_IDLE,
    PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
} pa_bluetooth_transport_state_t;

74
typedef int (*pa_bluetooth_transport_acquire_cb)(pa_bluetooth_transport *t, size_t *imtu, size_t *omtu);
75
typedef void (*pa_bluetooth_transport_release_cb)(pa_bluetooth_transport *t);
76
typedef void (*pa_bluetooth_transport_destroy_cb)(pa_bluetooth_transport *t);
77
78
typedef void (*pa_bluetooth_transport_set_speaker_gain_cb)(pa_bluetooth_transport *t, uint16_t gain);
typedef void (*pa_bluetooth_transport_set_microphone_gain_cb)(pa_bluetooth_transport *t, uint16_t gain);
79
80
81
82
83
84
85
86
87
88
89

struct pa_bluetooth_transport {
    pa_bluetooth_device *device;

    char *owner;
    char *path;
    pa_bluetooth_profile_t profile;

    uint8_t *config;
    size_t config_size;

90
    bool soft_volume;
91
92
93
    uint16_t microphone_gain;
    uint16_t speaker_gain;

94
95
96
97
    pa_bluetooth_transport_state_t state;

    pa_bluetooth_transport_acquire_cb acquire;
    pa_bluetooth_transport_release_cb release;
98
    pa_bluetooth_transport_destroy_cb destroy;
99
100
    pa_bluetooth_transport_set_speaker_gain_cb set_speaker_gain;
    pa_bluetooth_transport_set_microphone_gain_cb set_microphone_gain;
101
102
103
    void *userdata;
};

104
105
106
107
struct pa_bluetooth_device {
    pa_bluetooth_discovery *discovery;
    pa_bluetooth_adapter *adapter;

108
109
110
    bool properties_received;
    bool tried_to_link_with_adapter;
    bool valid;
111
    bool autodetect_mtu;
112
    bool change_a2dp_profile_in_progress;
113
114
115

    /* Device information */
    char *path;
116
    char *adapter_path;
117
118
119
    char *alias;
    char *address;
    uint32_t class_of_device;
120
    pa_hashmap *uuids; /* char* -> char* (hashmap-as-a-set) */
121
122
    pa_hashmap *a2dp_sink_endpoints; /* pa_a2dp_codec_id* -> pa_hashmap ( char* (remote endpoint) -> struct a2dp_codec_capabilities* ) */
    pa_hashmap *a2dp_source_endpoints; /* pa_a2dp_codec_id* -> pa_hashmap ( char* (remote endpoint) -> struct a2dp_codec_capabilities* ) */
123

124
    pa_bluetooth_transport **transports;
125
126

    pa_time_event *wait_for_profiles_timer;
127
128
};

129
130
131
132
struct pa_bluetooth_adapter {
    pa_bluetooth_discovery *discovery;
    char *path;
    char *address;
133
134

    bool valid;
135
    bool media_application_registered;
136
137
};

138
139
140
pa_bluetooth_backend *pa_bluetooth_hsphfpd_backend_new(pa_core *c, pa_bluetooth_discovery *y);
void pa_bluetooth_hsphfpd_backend_free(pa_bluetooth_backend *b);

141
#ifdef HAVE_BLUEZ_5_LEGACY_HSP
142
pa_bluetooth_backend *pa_bluetooth_legacy_hsp_backend_new(pa_core *c, pa_bluetooth_discovery *y, bool enable);
143
void pa_bluetooth_legacy_hsp_backend_free(pa_bluetooth_backend *b);
144
void pa_bluetooth_legacy_hsp_backend_enable(pa_bluetooth_backend *b, bool enable);
145
#else
146
static inline pa_bluetooth_backend *pa_bluetooth_legacy_hsp_backend_new(pa_core *c, pa_bluetooth_discovery *y, bool enable) {
147
148
    return NULL;
}
149
static inline void pa_bluetooth_legacy_hsp_backend_free(pa_bluetooth_backend *b) {}
150
static inline void pa_bluetooth_legacy_hsp_backend_enable(pa_bluetooth_backend *b, bool enable) {}
151
#endif
152

153
154
155
pa_bluetooth_transport *pa_bluetooth_transport_new(pa_bluetooth_device *d, const char *owner, const char *path,
                                                   pa_bluetooth_profile_t p, const uint8_t *config, size_t size);

156
void pa_bluetooth_transport_set_state(pa_bluetooth_transport *t, pa_bluetooth_transport_state_t state);
157
pa_bluetooth_transport *pa_bluetooth_transport_get(pa_bluetooth_discovery *y, const char *path);
158
pa_hashmap *pa_bluetooth_transport_get_all(pa_bluetooth_discovery *y);
159
160
161
void pa_bluetooth_transport_put(pa_bluetooth_transport *t);
void pa_bluetooth_transport_free(pa_bluetooth_transport *t);

162
163
164
size_t pa_bluetooth_device_find_a2dp_endpoints_for_codec(const pa_bluetooth_device *device, const pa_a2dp_codec *a2dp_codec, bool is_a2dp_sink, const char **endpoints, size_t endpoints_max_count);
bool pa_bluetooth_device_change_a2dp_profile(pa_bluetooth_device *d, pa_bluetooth_profile_t profile, void (*cb)(bool, void *), void *userdata);

165
166
bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d);

167
168
169
pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_discovery *y, const char *path);
pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local);

170
171
pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook);

172
173
174
175
176
unsigned pa_bluetooth_profile_count(void);
bool pa_bluetooth_profile_is_a2dp_sink(pa_bluetooth_profile_t profile);
bool pa_bluetooth_profile_is_a2dp_source(pa_bluetooth_profile_t profile);
const pa_a2dp_codec *pa_bluetooth_profile_to_a2dp_codec(pa_bluetooth_profile_t profile);
pa_bluetooth_profile_t pa_bluetooth_profile_for_a2dp_codec(const char *codec_name, bool is_a2dp_sink);
177
178
const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile);

179
180
181
182
183
184
185
186
static inline bool pa_bluetooth_profile_is_a2dp(pa_bluetooth_profile_t profile) {
    return pa_bluetooth_profile_is_a2dp_sink(profile) || pa_bluetooth_profile_is_a2dp_source(profile);
}

static inline bool pa_bluetooth_profile_support_a2dp_backchannel(pa_bluetooth_profile_t profile) {
    return pa_bluetooth_profile_to_a2dp_codec(profile)->support_backchannel;
}

187
188
189
190
static inline bool pa_bluetooth_uuid_is_hsp_hs(const char *uuid) {
    return pa_streq(uuid, PA_BLUETOOTH_UUID_HSP_HS) || pa_streq(uuid, PA_BLUETOOTH_UUID_HSP_HS_ALT);
}

191
#define HEADSET_BACKEND_HSPHFPD 0
192
#define HEADSET_BACKEND_LEGACY_HSP 1
193
194
195
#define HEADSET_BACKEND_AUTO 2

pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *core, int headset_backend);
196
197
pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y);
void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y);
198
void pa_bluetooth_discovery_legacy_hsp_backend_enable(pa_bluetooth_discovery *y, bool enable);
199
#endif