summaryrefslogtreecommitdiff
path: root/lib/ble.h
blob: 10945bbf16e7e51dec5d1896c30374dd004a0150 (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
#ifndef __BLE_H__
#define __BLE_H__

/*
 *  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
 *
 */

/** @file
 *
 * \mainpage
 *
 * \section intro_sec Introduction
 *
 * This document describes the libble API, a BLE library for Android which sits
 * on top of libhardware. The objective of this library is to abstract away
 * some libhardware specifics, exposing only what is necessary to create BLE
 * bindings to higher-level languages.
 *
 * Due to Bluedroid and/or libhardware architectural restrictions no more than
 * one process can access the Bluetooth resources on the system at the same
 * time. That means that only one program that makes use of libble can run on
 * the system at a certain time and that Bluetooth should be disabled in the
 * Android GUI (if running).
 */

/** BLE device bond state. */
typedef enum {
    BLE_BOND_NONE,     /**< There is no bond with the remote device. */
    BLE_BOND_BONDING,  /**< Pairing with the remote device is ongoing. */
    BLE_BOND_BONDED    /**< The remote device is bonded. */
} ble_bond_state_t;

/**
 * Type that represents a callback function to inform that BLE is enabled and
 * available to be used.
 */
typedef void (*ble_enable_cb_t)(void);

/**
 * Type that represents a callback function to inform an adapter state change.
 *
 * @param state The new state of the adapter: 0 disabled, 1 enabled.
 */
typedef void (*ble_adapter_state_cb_t)(uint8_t state);

/**
 * Type that represents a callback function to notify of a new found device
 * during a scanning session.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the found device, where the
 *                most-significant byte is on position 0 and the
 *                least-sifnificant byte is on position 5.
 * @param rssi The RSSI of the found device.
 * @param adv_data A pointer to the advertising data of the found device.
 */
typedef void (*ble_scan_cb_t)(const uint8_t *address, int rssi,
                              const uint8_t *adv_data);

/**
 * Type that represents a callback function to notify of a new connection with
 * a BLE device or a disconnection from a device.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the remote device, where the
 *                most-significant byte is on position 0 and the
 *                least-sifnificant byte is on position 5.
 * @param conn_id An identifier of the connected remote device. If this is
 *                nonpositive it means the device is not connected.
 * @param status The status in which the connect operation has finished.
 */
typedef void (*ble_connect_cb_t)(const uint8_t *address, int conn_id,
                                 int status);

/**
 * Type that represents a callback function to notify when the bond state with a
 * BLE device changes.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the remote device, where the
 *                most-significant byte is on position 0 and the
 *                least-sifnificant byte is on position 5.
 * @param state The new bonding state.
 * @param status The status in which the pair or remove bond operation has
 *               finished.
 */
typedef void (*ble_bond_state_cb_t)(const uint8_t *address,
                                    ble_bond_state_t state, int status);

/**
 * Type that represents a callback function to notify of a new GATT element
 * (service, characteristic or descriptor) found in a BLE device.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param id ID of the service, characteristic or descriptor.
 * @param uuid A pointer to a 16 element array representing each part of the
 *             service, characteristic or descriptor UUID.
 * @param props Additional element properties. For services, whether the service
 *              is primary or not: 1 primary, 0 included. Always zero for other
 *              GATT elements.
 */
typedef void (*ble_gatt_found_cb_t)(int conn_id, int id, const uint8_t *uuid,
                                    int props);

/**
 * Type that represents a callback function to notify that a GATT operation has
 * finished.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param status The status in which the operation has finished.
 */
typedef void (*ble_gatt_finished_cb_t)(int conn_id, int status);

/**
 * Type that represents a callback function to forward a GATT operation
 * response.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param id ID of the GATT element that the operation was executed uppon.
 * @param value The value in the operation response.
 * @param value_len The length of the data pointed by the value parameter.
 * @param value_type The type of the data pointed by the value parameter.
 * @param status The status in which the operation has finished.
 *
 * TODO: Document the semantics of value_type
 */
typedef void (*ble_gatt_response_cb_t)(int conn_id, int id,
                                       const uint8_t *value, uint16_t value_len,
                                       uint16_t value_type, int status);

/**
 * List of callbacks for BLE operations.
 */
typedef struct ble_cbs {
    ble_enable_cb_t enable_cb;
    ble_adapter_state_cb_t adapter_state_cb;
    ble_scan_cb_t scan_cb;
    ble_connect_cb_t connect_cb;
    ble_connect_cb_t disconnect_cb;
    ble_bond_state_cb_t bond_state_cb;
    ble_gatt_found_cb_t srvc_found_cb;
    ble_gatt_finished_cb_t srvc_finished_cb;
    ble_gatt_found_cb_t char_found_cb;
    ble_gatt_finished_cb_t char_finished_cb;
    ble_gatt_found_cb_t desc_found_cb;
    ble_gatt_finished_cb_t desc_finished_cb;
    ble_gatt_response_cb_t char_read_cb;
    ble_gatt_response_cb_t desc_read_cb;
} ble_cbs_t;

/**
 * Initialize the BLE stack and necessary interfaces and power on the adapter.
 *
 * @param cbs List of callbacks for BLE operations.
 *
 * @return 0 on success.
 * @return Negative value on failure.
 */
int ble_enable(ble_cbs_t cbs);

/**
 * Power off the adapter, cleanup the BLE features and shutdown the stack.
 *
 * @return 0 on success.
 * @return Negative value on failure.
 */
int ble_disable();

/**
 * Starts a LE scan procedure on the adapter.
 *
 * Scan will run indefinitelly until ble_scan_stop() is called.
 *
 * @return 0 if scan has been started.
 * @return 1 if adapter is already scanning.
 * @return -1 if failed to request scan start.
 */
int ble_start_scan();

/**
 * Stops a running LE scan procedure on the adapter.
 *
 * @return 0 if scan has been stopped.
 * @return 1 if adapter is not scanning.
 * @return -1 if failed to request scan stop.
 */
int ble_stop_scan();

/**
 * Connects to a BLE device.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the remote device a connection should be
 *                requested, where the most-significant byte is on position 0
 *                and the least-sifnificant byte is on position 5.
 *
 * @return 0 if connection has been successfully requested.
 * @return -1 if failed to request connection.
 */
int ble_connect(const uint8_t *address);

/**
 * Disconnects from a BLE device.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the remote device a disconnection should
 *                be requested, where the most-significant byte is on position
 *                0 and the least-sifnificant byte is on position 5.
 *
 * @return 0 if disconnection has been successfully requested.
 * @return -1 if failed to request disconnection.
 */
int ble_disconnect(const uint8_t *address);

/**
 * Pairs with a BLE device.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the remote device a pairing should be
 *                requested, where the most-significant byte is on position 0
 *                and the least-sifnificant byte is on position 5. *
 *
 * @return 0 if pairing has been successfully requested.
 * @return -1 if failed to pair.
 */
int ble_pair(const uint8_t *address);

/**
 * Cancel pairing with a BLE device.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the remote device which the bonding
 *                should be removed, where the most-significant byte is on
 *                position 0 and the least-sifnificant byte is on position 5.
 *
 * @return 0 if pairing has been successfully cancelled.
 * @return -1 if failed to cancel pairing.
 */
int ble_cancel_pairing(const uint8_t *address);

/**
 * Remove bond with a BLE device.
 *
 * @param address A pointer to a 6 element array representing each part of the
 *                Bluetooth address of the remote device which the bonding
 *                should be removed, where the most-significant byte is on
 *                position 0 and the least-sifnificant byte is on position 5.
 *
 * @return 0 if bond has been successfully removed.
 * @return -1 if failed to remove bond.
 */
int ble_remove_bond(const uint8_t *address);

/**
 * Discover services in a BLE device.
 *
 * There should be an active connection with the device.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param uuid Service UUID: if not NULL then only services with this UUID will
 *             be notified.
 *
 * @return 0 if service discovery has been successfully requested.
 * @return -1 if failed to request service discovery.
 */
int ble_gatt_discover_services(int conn_id, const uint8_t *uuid);

/**
 * Discover characteristics in a service of a BLE device.
 *
 * There should be an active connection with the device.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param service_id The identifier of the remote service in which the
 *                   characteristic discovery will be run.
 *
 * @return 0 if characteristic discovery has been successfully requested.
 * @return -1 if failed to request characteristic discovery.
 */
int ble_gatt_discover_characteristics(int conn_id, int service_id);

/**
 * Discover descriptors of a characteristic of a BLE device.
 *
 * There should be an active connection with the device.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param char_id The identifier of the characteristic in which the descriptor
 *                discovery will be run.
 *
 * @return 0 if descriptor discovery has been successfully requested.
 * @return -1 if failed to request descriptor discovery.
 */
int ble_gatt_discover_descriptors(int conn_id, int char_id);

/**
 * Read the value of a characteristic.
 *
 * There should be an active connection with the device.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param char_id The identifier of the characteristic to be read.
 * @param auth Whether or not link authentication should be requested before
 *             trying to read the characteristic: 1 request, 0 do not request.
 *
 * @return 0 if characteristic read has been successfully requested.
 * @return -1 if failed to request characteristic read.
 */
int ble_gatt_read_char(int conn_id, int char_id, int auth);

/**
 * Read the value of a characteristic descriptor.
 *
 * There should be an active connection with the device.
 *
 * @param conn_id The identifier of the connected remote device.
 * @param desc_id The identifier of the descriptor to be read.
 * @param auth Whether or not link authentication should be requested before
 *             trying to read the characteristic: 1 request, 0 do not request.
 *
 * @return 0 if characteristic read has been successfully requested.
 * @return -1 if failed to request characteristic read.
 */
int ble_gatt_read_desc(int conn_id, int desc_id, int auth);
#endif