diff options
author | João Paulo Rechi Vita <jprvita@gmail.com> | 2013-12-11 09:42:20 -0300 |
---|---|---|
committer | João Paulo Rechi Vita <jprvita@gmail.com> | 2014-01-15 15:02:45 -0300 |
commit | 47ba774aa2c8848a090e42578f6981782a4f1c68 (patch) | |
tree | 79291525bb4d961a1d54cb708c9427a6774e581a | |
parent | b2d6b390e2435781cee678a563b204f6d7222b24 (diff) |
lib: Create API to pair and remove bond with BLE devices
-rw-r--r-- | lib/ble.c | 83 | ||||
-rw-r--r-- | lib/ble.h | 62 |
2 files changed, 144 insertions, 1 deletions
@@ -196,6 +196,87 @@ int ble_disconnect(const uint8_t *address) { return 0; } +/* Called every time the bond state with a device changes */ +static void bond_state_changed_cb(bt_status_t status, bt_bdaddr_t *bda, + bt_bond_state_t state) { + ble_bond_state_t s; + ble_device_t *dev; + + switch (state) { + case BT_BOND_STATE_NONE: + s = BLE_BOND_NONE; + break; + case BT_BOND_STATE_BONDING: + s = BLE_BOND_BONDING; + break; + case BT_BOND_STATE_BONDED: + s = BLE_BOND_BONDED; + break; + default: + return; + } + + dev = find_device_by_address(bda->address); + if (dev && data.cbs.bond_state_cb) + data.cbs.bond_state_cb(bda->address, s, status); +} + +static int ble_pair_internal(const uint8_t *address, uint8_t operation) { + ble_device_t *dev; + bt_status_t s = BT_STATUS_UNSUPPORTED; + + if (!data.btiface) + 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; + } + + switch (operation) { + case 0: /* Pair */ + s = data.btiface->create_bond(&dev->bda); + break; + case 1: /* Cancel pairing */ + s = data.btiface->cancel_bond(&dev->bda); + break; + case 2: /* Remove bond */ + s = data.btiface->remove_bond(&dev->bda); + break; + } + + if (s != BT_STATUS_SUCCESS) + return -s; + + return 0; +} + +int ble_pair(const uint8_t *address) { + return ble_pair_internal(address, 0); +} + +int ble_cancel_pairing(const uint8_t *address) { + return ble_pair_internal(address, 1); +} + +int ble_remove_bond(const uint8_t *address) { + return ble_pair_internal(address, 2); +} + /* 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) { @@ -288,7 +369,7 @@ static bt_callbacks_t btcbs = { NULL, /* discovery_state_changed_cb */ NULL, /* pin_request_cb */ NULL, /* ssp_request_cb */ - NULL, /* bond_state_changed_cb */ + bond_state_changed_cb, NULL, /* acl_state_changed_callback */ thread_event_cb, NULL, /* dut_mode_recv_callback */ @@ -41,6 +41,13 @@ * 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. @@ -84,6 +91,21 @@ 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); + +/** * List of callbacks for BLE operations. */ typedef struct ble_cbs { @@ -92,6 +114,7 @@ typedef struct ble_cbs { 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_cbs_t; /** @@ -157,4 +180,43 @@ int ble_connect(const uint8_t *address); * @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); #endif |