diff options
author | João Paulo Rechi Vita <jprvita@gmail.com> | 2013-12-19 13:52:00 -0300 |
---|---|---|
committer | João Paulo Rechi Vita <jprvita@gmail.com> | 2014-01-15 15:03:57 -0300 |
commit | ca2ff03aa45511285597e20d0c968e19004f92f9 (patch) | |
tree | dc74ce50d492bf9eb20ab7bb84fdc198544cb8bb | |
parent | d1aa8ddaf12f17bbb674e24ee1ca45d3249db438 (diff) |
lib: Create API to read the RSSI of a BLE device
-rw-r--r-- | lib/ble.c | 42 | ||||
-rw-r--r-- | lib/ble.h | 21 |
2 files changed, 62 insertions, 1 deletions
@@ -308,6 +308,46 @@ static ble_device_t *find_device_by_conn_id(int conn_id) { return dev; } +/* Called in response of a read remote RSSI operation */ +void read_remote_rssi_cb(int client_if, bt_bdaddr_t *bda, int rssi, + int status) { + ble_device_t *dev; + int conn_id = -1; + + if (!status) { + dev = find_device_by_address(bda->address); + if (dev) + conn_id = dev->conn_id; + } + + if (data.cbs.rssi_cb) + data.cbs.rssi_cb(conn_id, rssi, status); +} + +int ble_read_remote_rssi(int conn_id) { + ble_device_t *dev; + bt_status_t s; + + if (!data.client) + return -1; + + if (conn_id <= 0) + return -1; + + if (!data.gattiface) + return -1; + + dev = find_device_by_conn_id(conn_id); + if (!dev) + return -1; + + s = data.gattiface->client->read_remote_rssi(data.client, &dev->bda); + if (s != BT_STATUS_SUCCESS) + return -s; + + return 0; +} + static int find_service(ble_device_t *dev, btgatt_srvc_id_t *srvc_id) { int id; @@ -799,7 +839,7 @@ static const btgatt_client_callbacks_t gattccbs = { read_descriptor_cb, write_descriptor_cb, NULL, /* execute_write_cb */ - NULL, /* read_remote_rssi_cb */ + read_remote_rssi_cb }; /* GATT interface callbacks */ @@ -106,6 +106,17 @@ 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 inform the RSSI of the remote + * BLE device. + * + * @param conn_id The identifier of the connected remote device. + * @param rssi The RSSI of the remote device. + * @param status The status in which the read remote RSSI operation has + * finished. + */ +typedef void (*ble_rssi_cb_t)(int conn_id, int rssi, int status); + +/** * Type that represents a callback function to notify of a new GATT element * (service, characteristic or descriptor) found in a BLE device. * @@ -185,6 +196,7 @@ typedef struct ble_cbs { ble_connect_cb_t connect_cb; ble_connect_cb_t disconnect_cb; ble_bond_state_cb_t bond_state_cb; + ble_rssi_cb_t rssi_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; @@ -303,6 +315,15 @@ int ble_cancel_pairing(const uint8_t *address); int ble_remove_bond(const uint8_t *address); /** + * Read the RSSI of a remote BLE device. + * + * There should be an active connection with the device. + * + * @param conn_id The identifier of the connected remote device. + */ +int ble_read_remote_rssi(int conn_id); + +/** * Discover services in a BLE device. * * There should be an active connection with the device. |