~binli/ubuntu/vivid/pulseaudio/fix-atoi

« back to all changes in this revision

Viewing changes to src/modules/bluetooth/module-bluez4-discover.c

  • Committer: Bin Li
  • Date: 2016-07-05 03:39:49 UTC
  • Revision ID: bin.li@canonical.com-20160705033949-rz4p9x4hbi2danxk
first version based on pulseaudio_6.0-0ubuntu9.27

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***
 
2
  This file is part of PulseAudio.
 
3
 
 
4
  Copyright 2008-2013 João Paulo Rechi Vita
 
5
 
 
6
  PulseAudio is free software; you can redistribute it and/or modify
 
7
  it under the terms of the GNU Lesser General Public License as
 
8
  published by the Free Software Foundation; either version 2.1 of the
 
9
  License, or (at your option) any later version.
 
10
 
 
11
  PulseAudio is distributed in the hope that it will be useful, but
 
12
  WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
14
  General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU Lesser General Public
 
17
  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
 
18
***/
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include <config.h>
 
22
#endif
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
 
 
27
#include <pulse/xmalloc.h>
 
28
#include <pulsecore/module.h>
 
29
#include <pulsecore/core-util.h>
 
30
#include <pulsecore/modargs.h>
 
31
#include <pulsecore/macro.h>
 
32
#include <pulsecore/core-util.h>
 
33
#include <pulsecore/dbus-shared.h>
 
34
 
 
35
#include "module-bluez4-discover-symdef.h"
 
36
#include "bluez4-util.h"
 
37
 
 
38
PA_MODULE_AUTHOR("João Paulo Rechi Vita");
 
39
PA_MODULE_DESCRIPTION("Detect available BlueZ 4 Bluetooth audio devices and load BlueZ 4 Bluetooth audio drivers");
 
40
PA_MODULE_VERSION(PACKAGE_VERSION);
 
41
PA_MODULE_USAGE("profile=<a2dp|hsp|hfgw> "
 
42
                "sco_sink=<name of sink> "
 
43
                "sco_source=<name of source> ");
 
44
PA_MODULE_LOAD_ONCE(true);
 
45
 
 
46
static const char* const valid_modargs[] = {
 
47
    "profile",
 
48
    "sco_sink",
 
49
    "sco_source",
 
50
    "async", /* deprecated */
 
51
    NULL
 
52
};
 
53
 
 
54
struct userdata {
 
55
    pa_module *module;
 
56
    pa_modargs *modargs;
 
57
    pa_core *core;
 
58
    pa_bluez4_discovery *discovery;
 
59
    pa_hook_slot *slot;
 
60
    pa_hashmap *hashmap;
 
61
};
 
62
 
 
63
struct module_info {
 
64
    char *path;
 
65
    uint32_t module;
 
66
};
 
67
 
 
68
static pa_hook_result_t load_module_for_device(pa_bluez4_discovery *y, const pa_bluez4_device *d, struct userdata *u) {
 
69
    struct module_info *mi;
 
70
 
 
71
    pa_assert(u);
 
72
    pa_assert(d);
 
73
 
 
74
    mi = pa_hashmap_get(u->hashmap, d->path);
 
75
 
 
76
    if (pa_bluez4_device_any_audio_connected(d)) {
 
77
 
 
78
        if (!mi) {
 
79
            pa_module *m = NULL;
 
80
            char *args;
 
81
 
 
82
            /* Oh, awesome, a new device has shown up and been connected! */
 
83
 
 
84
            args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
 
85
 
 
86
            if (pa_modargs_get_value(u->modargs, "profile", NULL)) {
 
87
                char *profile;
 
88
 
 
89
                profile = pa_sprintf_malloc("%s profile=\"%s\"", args,
 
90
                                        pa_modargs_get_value(u->modargs, "profile", NULL));
 
91
                pa_xfree(args);
 
92
                args = profile;
 
93
            }
 
94
 
 
95
            if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
 
96
                pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
 
97
                char *tmp;
 
98
 
 
99
                tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
 
100
                                        pa_modargs_get_value(u->modargs, "sco_sink", NULL),
 
101
                                        pa_modargs_get_value(u->modargs, "sco_source", NULL));
 
102
                pa_xfree(args);
 
103
                args = tmp;
 
104
            }
 
105
 
 
106
            pa_log_debug("Loading module-bluez4-device %s", args);
 
107
            m = pa_module_load(u->module->core, "module-bluez4-device", args);
 
108
            pa_xfree(args);
 
109
 
 
110
            if (m) {
 
111
                mi = pa_xnew(struct module_info, 1);
 
112
                mi->module = m->index;
 
113
                mi->path = pa_xstrdup(d->path);
 
114
 
 
115
                pa_hashmap_put(u->hashmap, mi->path, mi);
 
116
            } else
 
117
                pa_log_debug("Failed to load module for device %s", d->path);
 
118
        }
 
119
 
 
120
    } else {
 
121
 
 
122
        if (mi) {
 
123
 
 
124
            /* Hmm, disconnection? Then the module unloads itself */
 
125
 
 
126
            pa_log_debug("Unregistering module for %s", d->path);
 
127
            pa_hashmap_remove(u->hashmap, mi->path);
 
128
            pa_xfree(mi->path);
 
129
            pa_xfree(mi);
 
130
        }
 
131
    }
 
132
 
 
133
    return PA_HOOK_OK;
 
134
}
 
135
 
 
136
int pa__init(pa_module* m) {
 
137
    struct userdata *u;
 
138
    pa_modargs *ma = NULL;
 
139
 
 
140
    pa_assert(m);
 
141
 
 
142
    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
 
143
        pa_log("Failed to parse module arguments");
 
144
        goto fail;
 
145
    }
 
146
 
 
147
    if (pa_modargs_get_value(ma, "async", NULL))
 
148
        pa_log_warn("The 'async' argument is deprecated and does nothing.");
 
149
 
 
150
    m->userdata = u = pa_xnew0(struct userdata, 1);
 
151
    u->module = m;
 
152
    u->core = m->core;
 
153
    u->modargs = ma;
 
154
    ma = NULL;
 
155
    u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
156
 
 
157
    if (!(u->discovery = pa_bluez4_discovery_get(u->core)))
 
158
        goto fail;
 
159
 
 
160
    u->slot = pa_hook_connect(pa_bluez4_discovery_hook(u->discovery, PA_BLUEZ4_HOOK_DEVICE_CONNECTION_CHANGED),
 
161
                              PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
 
162
 
 
163
    return 0;
 
164
 
 
165
fail:
 
166
    pa__done(m);
 
167
 
 
168
    if (ma)
 
169
        pa_modargs_free(ma);
 
170
 
 
171
    return -1;
 
172
}
 
173
 
 
174
void pa__done(pa_module* m) {
 
175
    struct userdata *u;
 
176
 
 
177
    pa_assert(m);
 
178
 
 
179
    if (!(u = m->userdata))
 
180
        return;
 
181
 
 
182
    if (u->slot)
 
183
        pa_hook_slot_free(u->slot);
 
184
 
 
185
    if (u->discovery)
 
186
        pa_bluez4_discovery_unref(u->discovery);
 
187
 
 
188
    if (u->hashmap) {
 
189
        struct module_info *mi;
 
190
 
 
191
        while ((mi = pa_hashmap_steal_first(u->hashmap))) {
 
192
            pa_xfree(mi->path);
 
193
            pa_xfree(mi);
 
194
        }
 
195
 
 
196
        pa_hashmap_free(u->hashmap);
 
197
    }
 
198
 
 
199
    if (u->modargs)
 
200
        pa_modargs_free(u->modargs);
 
201
 
 
202
    pa_xfree(u);
 
203
}