summaryrefslogtreecommitdiff
path: root/src/modules/bluetooth/bluetooth-util.h
blob: cf97e06599ea3d2d4da7bcf82174e657c72a30b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef foobluetoothutilhfoo
#define foobluetoothutilhfoo

/***
  This file is part of PulseAudio.

  Copyright 2008-2009 Joao Paulo Rechi Vita

  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
  License along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#include <dbus/dbus.h>

#include <pulsecore/llist.h>
#include <pulsecore/macro.h>

#define PA_BLUETOOTH_ERROR_NOT_SUPPORTED "org.bluez.Error.NotSupported"

/* UUID copied from bluez/audio/device.h */
#define GENERIC_AUDIO_UUID      "00001203-0000-1000-8000-00805f9b34fb"

#define HSP_HS_UUID             "00001108-0000-1000-8000-00805f9b34fb"
#define HSP_AG_UUID             "00001112-0000-1000-8000-00805f9b34fb"

#define HFP_HS_UUID             "0000111e-0000-1000-8000-00805f9b34fb"
#define HFP_AG_UUID             "0000111f-0000-1000-8000-00805f9b34fb"

#define HFP_AUDIO_CODEC_CVSD    0x01
#define HFP_AUDIO_CODEC_MSBC    0x02

#define ADVANCED_AUDIO_UUID     "0000110d-0000-1000-8000-00805f9b34fb"

#define A2DP_SOURCE_UUID        "0000110a-0000-1000-8000-00805f9b34fb"
#define A2DP_SINK_UUID          "0000110b-0000-1000-8000-00805f9b34fb"

#define HSP_MAX_GAIN 15

typedef struct pa_bluetooth_uuid pa_bluetooth_uuid;
typedef struct pa_bluetooth_device pa_bluetooth_device;
typedef struct pa_bluetooth_discovery pa_bluetooth_discovery;
typedef struct pa_bluetooth_transport pa_bluetooth_transport;

struct userdata;

struct pa_bluetooth_uuid {
    char *uuid;
    PA_LLIST_FIELDS(pa_bluetooth_uuid);
};

enum profile {
    PROFILE_A2DP,
    PROFILE_A2DP_SOURCE,
    PROFILE_HSP,
    PROFILE_HFGW,
    PROFILE_OFF
};

#define PA_BLUETOOTH_PROFILE_COUNT PROFILE_OFF

struct pa_bluetooth_hook_uuid_data {
    pa_bluetooth_device *device;
    const char *uuid;
};

/* Hook data: pa_bluetooth_discovery pointer. */
typedef enum pa_bluetooth_hook {
    PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED, /* Call data: pa_bluetooth_device */
    PA_BLUETOOTH_HOOK_DEVICE_UUID_ADDED, /* Call data: pa_bluetooth_hook_uuid_data */
    PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED, /* Call data: pa_bluetooth_transport */
    PA_BLUETOOTH_HOOK_TRANSPORT_NREC_CHANGED, /* Call data: pa_bluetooth_transport */
    PA_BLUETOOTH_HOOK_TRANSPORT_MICROPHONE_GAIN_CHANGED, /* Call data: pa_bluetooth_transport */
    PA_BLUETOOTH_HOOK_TRANSPORT_SPEAKER_GAIN_CHANGED, /* Call data: pa_bluetooth_transport */
    PA_BLUETOOTH_HOOK_MAX
} pa_bluetooth_hook_t;

typedef enum pa_bluetooth_transport_state {
    PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED,
    PA_BLUETOOTH_TRANSPORT_STATE_IDLE, /* Connected but not playing */
    PA_BLUETOOTH_TRANSPORT_STATE_PLAYING
} pa_bluetooth_transport_state_t;

struct pa_bluetooth_transport {
    pa_bluetooth_device *device;
    char *owner;
    char *path;
    enum profile profile;
    uint8_t codec;
    uint8_t *config;
    int config_size;

    pa_bluetooth_transport_state_t state;
    bool nrec;
    uint16_t microphone_gain; /* Used for HSP/HFP */
    uint16_t speaker_gain; /* Used for HSP/HFP */
};

/* This enum is shared among Audio, Headset, AudioSink, and AudioSource, although not all values are acceptable in all profiles */
typedef enum pa_bt_audio_state {
    PA_BT_AUDIO_STATE_INVALID = -1,
    PA_BT_AUDIO_STATE_DISCONNECTED,
    PA_BT_AUDIO_STATE_CONNECTING,
    PA_BT_AUDIO_STATE_CONNECTED,
    PA_BT_AUDIO_STATE_PLAYING
} pa_bt_audio_state_t;

typedef enum pa_bluez_version {
    BLUEZ_VERSION_UNKNOWN,
    BLUEZ_VERSION_4,
    BLUEZ_VERSION_5,
} pa_bluez_version_t;

struct pa_bluetooth_device {
    pa_bluetooth_discovery *discovery;
    bool dead;

    int device_info_valid;      /* 0: no results yet; 1: good results; -1: bad results ... */

    /* Device information */
    char *path;
    pa_bluetooth_transport *transports[PA_BLUETOOTH_PROFILE_COUNT];
    int paired;
    char *alias;
    PA_LLIST_HEAD(pa_bluetooth_uuid, uuids);
    char *address;
    int class;
    int trusted;

    /* Audio state */
    pa_bt_audio_state_t audio_state;

    /* AudioSink, AudioSource, Headset and HandsfreeGateway states */
    pa_bt_audio_state_t profile_state[PA_BLUETOOTH_PROFILE_COUNT];
};

pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *core);
pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y);
void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *d);

pa_bluez_version_t pa_bluetooth_discovery_get_bluez_version(pa_bluetooth_discovery *y);
pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *d, const char* path);
pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *d, const char* address);

bool pa_bluetooth_device_any_audio_connected(const pa_bluetooth_device *d);

int pa_bluetooth_transport_acquire(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu);
void pa_bluetooth_transport_release(pa_bluetooth_transport *t);

void pa_bluetooth_transport_set_microphone_gain(pa_bluetooth_transport *t, uint16_t value);
void pa_bluetooth_transport_set_speaker_gain(pa_bluetooth_transport *t, uint16_t value);

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

typedef enum pa_bt_form_factor {
    PA_BT_FORM_FACTOR_UNKNOWN,
    PA_BT_FORM_FACTOR_HEADSET,
    PA_BT_FORM_FACTOR_HANDSFREE,
    PA_BT_FORM_FACTOR_MICROPHONE,
    PA_BT_FORM_FACTOR_SPEAKER,
    PA_BT_FORM_FACTOR_HEADPHONE,
    PA_BT_FORM_FACTOR_PORTABLE,
    PA_BT_FORM_FACTOR_CAR,
    PA_BT_FORM_FACTOR_HIFI,
    PA_BT_FORM_FACTOR_PHONE,
} pa_bt_form_factor_t;

pa_bt_form_factor_t pa_bluetooth_get_form_factor(uint32_t class);
const char *pa_bt_form_factor_to_string(pa_bt_form_factor_t ff);

char *pa_bluetooth_cleanup_name(const char *name);

bool pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid);
const char *pa_bt_profile_to_string(enum profile profile);

#endif