summaryrefslogtreecommitdiff
path: root/lib/ble.c
blob: 5a9161beb91b8fff6f2ba1a8fd25c3e548f3fdce (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 *  Android BLE Library -- Provides utility functions to control BLE features
 *
 *  Copyright (C) 2013 João Paulo Rechi Vita
 *
 *  This program 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
 *  the Free Software Foundation; either version 2 of the License, or
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser 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
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <hardware/bluetooth.h>
#include <hardware/bt_gatt.h>
#include <hardware/bt_gatt_client.h>
#include <hardware/hardware.h>

#include "ble.h"

/* Internal representation of a BLE device */
typedef struct ble_device ble_device_t;
struct ble_device {
    bt_bdaddr_t bda;
    int conn_id;

    ble_device_t *next;
};

/* Data that have to be acessable by the callbacks */
static struct libdata {
    ble_cbs_t cbs;

    uint8_t btiface_initialized;
    const bt_interface_t *btiface;
    const btgatt_interface_t *gattiface;
    int client;

    uint8_t adapter_state;
    uint8_t scan_state;
    ble_device_t *devices;
} data;

/* Called every time an advertising report is seen */
static void scan_result_cb(bt_bdaddr_t *bda, int rssi, uint8_t *adv_data) {
    if (data.cbs.scan_cb)
        data.cbs.scan_cb(bda->address, rssi, adv_data);
}

static int ble_scan(uint8_t start) {
    bt_status_t s;

    if (!data.client)
        return -1;

    if (data.gattiface == NULL)
        return -1;

    if (!data.adapter_state)
        return -1;

    if (data.scan_state == start)
        return 1;

    s = data.gattiface->client->scan(data.client, start);
    if (s != BT_STATUS_SUCCESS)
        return -s;

    data.scan_state = start;

    return 0;
}

int ble_start_scan() {
    return ble_scan(1);
}

int ble_stop_scan() {
    return ble_scan(0);
}

static ble_device_t *find_device_by_address(const uint8_t *address) {
    ble_device_t *dev;

    for (dev = data.devices; dev ; dev = dev->next)
        if (!memcmp(dev->bda.address, address, sizeof(dev->bda.address)))
            break;

    return dev;
}

/* Called every time a device gets connected */
static void connect_cb(int conn_id, int status, int client_if,
                       bt_bdaddr_t *bda) {
    ble_device_t *dev;

    dev = find_device_by_address(bda->address);
    if (!dev)
        return;

    dev->conn_id = conn_id;

    if (data.cbs.connect_cb)
        data.cbs.connect_cb(bda->address, conn_id, status);
}

int ble_connect(const uint8_t *address) {
    ble_device_t *dev;
    bt_status_t s;

    if (!data.client)
        return -1;

    if (!data.gattiface)
        return -1;

    if (!data.adapter_state)
        return -1;

    dev = find_device_by_address(address);
    if (!dev) {
        int i;

        dev = calloc(1, sizeof(ble_device_t));
        if (!dev)
            return -1;

        /* TODO: memcpy() */
        for (i = 0; i < 6; i++)
            dev->bda.address[i] = address[i];

        dev->next = data.devices;
        data.devices = dev;
    }

    s = data.gattiface->client->connect(data.client, &dev->bda, true);
    if (s != BT_STATUS_SUCCESS)
        return -s;

    return 0;
}

/* Called every time a device gets disconnected */
static void disconnect_cb(int conn_id, int status, int client_if,
                          bt_bdaddr_t *bda) {
    ble_device_t *dev;

    dev = find_device_by_address(bda->address);
    if (!dev)
        return;

    dev->conn_id = 0;

    if (data.cbs.disconnect_cb)
        data.cbs.disconnect_cb(bda->address, conn_id, status);
}

int ble_disconnect(const uint8_t *address) {
    ble_device_t *dev;
    bt_status_t s;

    if (!data.client)
        return -1;

    if (!data.gattiface)
        return -1;

    if (!data.adapter_state)
        return -1;

    if (!address)
        return -1;

    dev = find_device_by_address(address);
    if (!dev)
        return -1;

    s = data.gattiface->client->disconnect(data.client, &dev->bda,
                                           dev->conn_id);
    if (s != BT_STATUS_SUCCESS)
        return -s;

    return 0;
}

/* Called when the client registration is finished */
static void register_client_cb(int status, int client_if, bt_uuid_t *app_uuid) {
    if (status == BT_STATUS_SUCCESS) {
        data.client = client_if;
        if (data.cbs.enable_cb)
            data.cbs.enable_cb();
    } else
        data.client = 0;
}

/* GATT client interface callbacks */
static const btgatt_client_callbacks_t gattccbs = {
    register_client_cb,
    scan_result_cb,
    connect_cb,
    disconnect_cb,
    NULL, /* search_complete_cb */
    NULL, /* search_result_cb */
    NULL, /* get_characteristic_cb */
    NULL, /* get_descriptor_cb */
    NULL, /* get_included_service_cb */
    NULL, /* register_for_notification_cb */
    NULL, /* notify_cb */
    NULL, /* read_characteristic_cb */
    NULL, /* write_characteristic_cb */
    NULL, /* read_descriptor_cb */
    NULL, /* write_descriptor_cb */
    NULL, /* execute_write_cb */
    NULL, /* read_remote_rssi_cb */
};

/* GATT interface callbacks */
static const btgatt_callbacks_t gattcbs = {
    sizeof(btgatt_callbacks_t),
    &gattccbs,
    NULL  /* btgatt_server_callbacks_t */
};

/* Called every time the adapter state changes */
static void adapter_state_changed_cb(bt_state_t state) {
    /* Arbitrary UUID used to identify this application with the GATT profile */
    bt_uuid_t app_uuid = { .uu = { 0x1b, 0x1c, 0xb9, 0x2e, 0x0d, 0x2e, 0x4c,
                                   0x45, 0xbb, 0xb8, 0xf4, 0x1b, 0x46, 0x39,
                                   0x23, 0x36 } };

    data.adapter_state = state == BT_STATE_ON ? 1 : 0;
    if (data.cbs.adapter_state_cb)
        data.cbs.adapter_state_cb(data.adapter_state);

    if (data.adapter_state) {
        bt_status_t s = data.gattiface->client->register_client(&app_uuid);
        if (s != BT_STATUS_SUCCESS)
            data.btiface->disable();
    } else
        /* Cleanup the Bluetooth interface */
        data.btiface->cleanup();
}

/* This callback is called when the stack finishes initialization / shutdown */
static void thread_event_cb(bt_cb_thread_evt event) {
    bt_status_t s;

    if (event == DISASSOCIATE_JVM) {
        data.btiface_initialized = 0;
        return;
    }

    data.btiface_initialized = 1;

    data.gattiface = data.btiface->get_profile_interface(BT_PROFILE_GATT_ID);
    if (data.gattiface != NULL) {
        s = data.gattiface->init(&gattcbs);
        if (s != BT_STATUS_SUCCESS)
            data.gattiface = NULL;
    }

    /* Enable the adapter */
    s = data.btiface->enable();
    if (s != BT_STATUS_SUCCESS)
        data.btiface->cleanup();
}

/* Bluetooth interface callbacks */
static bt_callbacks_t btcbs = {
    sizeof(bt_callbacks_t),
    adapter_state_changed_cb,
    NULL, /* adapter_properties_cb */
    NULL, /* remote_device_properties_callback */
    NULL, /* device_found_cb */
    NULL, /* discovery_state_changed_cb */
    NULL, /* pin_request_cb */
    NULL, /* ssp_request_cb */
    NULL, /* bond_state_changed_cb */
    NULL, /* acl_state_changed_callback */
    thread_event_cb,
    NULL, /* dut_mode_recv_callback */
    NULL, /* le_test_mode_callback */
};

int ble_enable(ble_cbs_t cbs) {
    int status;
    bt_status_t s;
    hw_module_t *module;
    hw_device_t *hwdev;
    bluetooth_device_t *btdev;

    memset(&data, 0, sizeof(data));

    /* Get the Bluetooth module from libhardware */
    status = hw_get_module(BT_STACK_MODULE_ID, (hw_module_t const**) &module);
    if (status < 0)
        return status;

    /* Get the Bluetooth hardware device */
    status = module->methods->open(module, BT_STACK_MODULE_ID, &hwdev);
    if (status < 0)
        return status;

    /* Get the Bluetooth interface */
    btdev = (bluetooth_device_t *) hwdev;
    data.btiface = btdev->get_bluetooth_interface();
    if (data.btiface == NULL)
        return -1;

    /* Init the Bluetooth interface, setting a callback for each operation */
    s = data.btiface->init(&btcbs);
    if (s != BT_STATUS_SUCCESS && s != BT_STATUS_DONE)
        return -s;

    /* Store the user callbacks */
    data.cbs = cbs;

    return 0;
}

static void remove_all_devices() {
    ble_device_t *dev, *next;

    dev = data.devices;
    while (dev) {
        next = dev->next;
        free(dev);
        dev = next;
    }
}

int ble_disable() {
    bt_status_t s;

    if (!data.adapter_state)
        return -1;

    if (!data.btiface)
        return -1;

    if (!data.client)
        return -1;

    s = data.gattiface->client->unregister_client(data.client);
    if (s != BT_STATUS_SUCCESS)
        return -s;

    s = data.btiface->disable();
    if (s != BT_STATUS_SUCCESS)
        return -s;

    return 0;
}