summaryrefslogtreecommitdiffstats
path: root/src/modules/bt-proximity-helper.c
blob: 3767f01cebe2f7b2e0b9a04fc67909521b9776b8 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Small SUID helper that allows us to ping a BT device. Borrows
 * heavily from bluez-utils' l2ping, which is licensed as GPL2+
 * and comes with a copyright like this:
 *
 *  Copyright (C) 2000-2001  Qualcomm Incorporated
 *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
 *  Copyright (C) 2002-2007  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#undef NDEBUG

#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/select.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>

#define PING_STRING "PulseAudio"
#define IDENT 200
#define TIMEOUT 4
#define INTERVAL 2

static void update_status(int found) {
    static int status = -1;

    if (!found && status != 0)
        printf("-");
    if (found && status <= 0)
        printf("+");

    fflush(stdout);
    status = !!found;
}

int main(int argc, char *argv[]) {
    struct sockaddr_l2 addr;
    union {
        l2cap_cmd_hdr hdr;
        uint8_t buf[L2CAP_CMD_HDR_SIZE + sizeof(PING_STRING)];
    }  packet;
    int fd = -1;
    uint8_t id = IDENT;
    int connected = 0;

    assert(argc == 2);

    for (;;) {
        fd_set fds;
        struct timeval end;
        ssize_t r;

        if (!connected) {

            if (fd >= 0)
                close(fd);

            if ((fd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP)) < 0) {
                fprintf(stderr, "socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP) failed: %s", strerror(errno));
                goto finish;
            }

            memset(&addr, 0, sizeof(addr));
            addr.l2_family = AF_BLUETOOTH;
            bacpy(&addr.l2_bdaddr, BDADDR_ANY);

            if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
                fprintf(stderr, "bind() failed: %s", strerror(errno));
                goto finish;
            }

            memset(&addr, 0, sizeof(addr));
            addr.l2_family = AF_BLUETOOTH;
            str2ba(argv[1], &addr.l2_bdaddr);

            if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {

                if (errno == EHOSTDOWN || errno == ECONNRESET || errno == ETIMEDOUT) {
                    update_status(0);
                    sleep(INTERVAL);
                    continue;
                }

                fprintf(stderr, "connect() failed: %s", strerror(errno));
                goto finish;
            }

            connected = 1;
        }

        assert(connected);

        memset(&packet, 0, sizeof(packet));
        strcpy((char*) packet.buf + L2CAP_CMD_HDR_SIZE, PING_STRING);
        packet.hdr.ident = id;
        packet.hdr.len = htobs(sizeof(PING_STRING));
        packet.hdr.code = L2CAP_ECHO_REQ;

        if ((r = send(fd, &packet, sizeof(packet), 0)) < 0) {

            if (errno == EHOSTDOWN || errno == ECONNRESET || errno == ETIMEDOUT) {
                update_status(0);
                connected = 0;
                sleep(INTERVAL);
                continue;
            }

            fprintf(stderr, "send() failed: %s", strerror(errno));
            goto finish;
        }

        assert(r == sizeof(packet));

        gettimeofday(&end, NULL);
        end.tv_sec += TIMEOUT;

        for (;;) {
            struct timeval now, delta;

            gettimeofday(&now, NULL);

            if (timercmp(&end, &now, <=)) {
                update_status(0);
                connected = 0;
                sleep(INTERVAL);
                break;
            }

            timersub(&end, &now, &delta);

            FD_ZERO(&fds);
            FD_SET(fd, &fds);

            if (select(fd+1, &fds, NULL, NULL, &delta) < 0) {
                fprintf(stderr, "select() failed: %s", strerror(errno));
                goto finish;
            }

            if ((r = recv(fd, &packet, sizeof(packet), 0)) <= 0) {

                if (errno == EHOSTDOWN || errno == ECONNRESET || errno == ETIMEDOUT) {
                    update_status(0);
                    connected = 0;
                    sleep(INTERVAL);
                    break;
                }

                fprintf(stderr, "send() failed: %s", r == 0 ? "EOF" : strerror(errno));
                goto finish;
            }

            assert(r >= L2CAP_CMD_HDR_SIZE);

            if (packet.hdr.ident != id)
                continue;

            if (packet.hdr.code == L2CAP_ECHO_RSP || packet.hdr.code == L2CAP_COMMAND_REJ) {

                if (++id >= 0xFF)
                    id = IDENT;

                update_status(1);
                sleep(INTERVAL);
                break;
            }
        }
    }

finish:

    if (fd >= 0)
        close(fd);

    return 1;
}