bluez5-util.h 9.14 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 91 92 93 94 95
    bool microphone_soft_volume;
    bool speaker_soft_volume;
    pa_volume_t microphone_gain;
    pa_volume_t speaker_gain;
    pa_volume_t max_microphone_gain;
    pa_volume_t max_speaker_gain;
96

97 98 99 100
    pa_bluetooth_transport_state_t state;

    pa_bluetooth_transport_acquire_cb acquire;
    pa_bluetooth_transport_release_cb release;
101
    pa_bluetooth_transport_destroy_cb destroy;
102 103
    pa_bluetooth_transport_set_speaker_gain_cb set_speaker_gain;
    pa_bluetooth_transport_set_microphone_gain_cb set_microphone_gain;
104 105 106
    void *userdata;
};

107 108 109 110
struct pa_bluetooth_device {
    pa_bluetooth_discovery *discovery;
    pa_bluetooth_adapter *adapter;

111 112 113
    bool properties_received;
    bool tried_to_link_with_adapter;
    bool valid;
114
    bool autodetect_mtu;
115
    bool change_a2dp_profile_in_progress;
116 117 118

    /* Device information */
    char *path;
119
    char *adapter_path;
120 121 122
    char *alias;
    char *address;
    uint32_t class_of_device;
123
    pa_hashmap *uuids; /* char* -> char* (hashmap-as-a-set) */
124 125
    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* ) */
126

127
    pa_bluetooth_transport **transports;
128 129

    pa_time_event *wait_for_profiles_timer;
130 131
};

132 133 134 135
struct pa_bluetooth_adapter {
    pa_bluetooth_discovery *discovery;
    char *path;
    char *address;
136 137

    bool valid;
138
    bool media_application_registered;
139 140
};

141 142 143
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);

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

156 157 158
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);

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

165 166 167
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);

168
bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d);
169 170
bool pa_bluetooth_device_a2dp_sink_transport_connected(const pa_bluetooth_device *d);
bool pa_bluetooth_device_a2dp_source_transport_connected(const pa_bluetooth_device *d);
171

172 173 174
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);

175 176
pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook);

177 178 179 180 181
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);
182 183
const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile);

184 185 186 187 188 189 190 191
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;
}

192 193 194 195
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);
}

196
#define HEADSET_BACKEND_HSPHFPD 0
197
#define HEADSET_BACKEND_LEGACY_HSP 1
198 199 200
#define HEADSET_BACKEND_AUTO 2

pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *core, int headset_backend);
201 202
pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y);
void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y);
203
void pa_bluetooth_discovery_legacy_hsp_backend_enable(pa_bluetooth_discovery *y, bool enable);
204
#endif