Skip to content

Commit

Permalink
sfos: bluez5: Add RING feature to native backend.
Browse files Browse the repository at this point in the history
Only does anything when headset_head_unit profile
is active.
  • Loading branch information
jusa committed Mar 22, 2019
1 parent d6dda9c commit 50d23a6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/modules/bluetooth/backend-native.c
Expand Up @@ -26,6 +26,8 @@
#include <pulsecore/core-util.h>
#include <pulsecore/dbus-shared.h>
#include <pulsecore/log.h>
#include <pulse/timeval.h>
#include <pulse/rtclock.h>

#include <errno.h>
#include <sys/types.h>
Expand All @@ -46,6 +48,9 @@ struct pa_bluetooth_backend {
};

struct transport_data {
pa_core *core;
pa_bluetooth_transport *hsp_ag_transport;
pa_time_event *ring_time_event;
int rfcomm_fd;
pa_io_event *rfcomm_io;
int sco_fd;
Expand Down Expand Up @@ -553,14 +558,20 @@ static DBusMessage *profile_new_connection(DBusConnection *conn, DBusMessage *m,
t->set_microphone_gain = set_microphone_gain;

trd = pa_xnew0(struct transport_data, 1);
trd->core = b->core;
trd->rfcomm_fd = fd;
trd->mainloop = b->core->mainloop;
trd->rfcomm_io = trd->mainloop->io_new(b->core->mainloop, fd, PA_IO_EVENT_INPUT,
rfcomm_io_callback, t);
t->native = true;
t->userdata = trd;

sco_listen(t);

if (t->profile == PA_BLUETOOTH_PROFILE_HEADSET_HEAD_UNIT) {
trd->hsp_ag_transport = t;
}

pa_bluetooth_transport_put(t);

pa_log_debug("Transport %s available for profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
Expand Down Expand Up @@ -718,3 +729,60 @@ void pa_bluetooth_native_backend_free(pa_bluetooth_backend *backend) {

pa_xfree(backend);
}

#define RING_WAIT_TIME ((pa_usec_t) (3 * PA_USEC_PER_SEC))

static void ring_time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
struct transport_data *trd;
const char *buf = "\r\nRING\r\n";
const ssize_t len = strlen(buf);
ssize_t written;

trd = userdata;

if (trd->rfcomm_fd < 0)
return;

pa_log_debug("RFCOMM >> RING");
written = write(trd->rfcomm_fd, buf, len);

if (written != len)
pa_log_error("RFCOMM write error: %s", pa_cstrerror(errno));

pa_core_rttime_restart(trd->core, trd->ring_time_event, pa_rtclock_now() + RING_WAIT_TIME);
}

static void ring_start(struct transport_data *trd) {
pa_assert(trd);

if (!trd->ring_time_event)
trd->ring_time_event = pa_core_rttime_new(trd->core,
pa_rtclock_now(),
ring_time_cb, trd);
}

static void ring_stop(struct transport_data *trd) {
pa_assert(trd);

if (trd->ring_time_event) {
trd->core->mainloop->time_free(trd->ring_time_event);
trd->ring_time_event = NULL;
}
}

void pa_bluetooth_native_backend_ring(pa_bluetooth_transport *t, bool active) {
struct transport_data *trd;

if (!t || !t->native)
return;

pa_assert_se((trd = t->userdata));

if (trd->hsp_ag_transport != t)
return;

if (active)
ring_start(t->userdata);
else
ring_stop(t->userdata);
}
4 changes: 4 additions & 0 deletions src/modules/bluetooth/bluez5-util.h
Expand Up @@ -87,6 +87,7 @@ struct pa_bluetooth_transport {

pa_bluetooth_transport_state_t state;

bool native;
pa_bluetooth_transport_acquire_cb acquire;
pa_bluetooth_transport_release_cb release;
pa_bluetooth_transport_destroy_cb destroy;
Expand Down Expand Up @@ -150,6 +151,9 @@ static inline void pa_bluetooth_native_backend_enable_hs_role(pa_bluetooth_backe
bool pa_bluetooth_droid_backend(pa_bluetooth_discovery *y);
void pa_bluetooth_droid_volume_control_acquire(pa_bluetooth_discovery *y, pa_bluetooth_transport *t);
void pa_bluetooth_droid_volume_control_release(pa_bluetooth_discovery *y);
#ifdef HAVE_BLUEZ_5_NATIVE_HEADSET
void pa_bluetooth_native_backend_ring(pa_bluetooth_transport *t, bool active);
#endif

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);
Expand Down

0 comments on commit 50d23a6

Please sign in to comment.