summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoão Paulo Rechi Vita <jprvita@gmail.com>2013-12-06 13:07:15 -0300
committerJoão Paulo Rechi Vita <jprvita@gmail.com>2014-01-14 16:52:58 -0300
commit7b2f4b2501d2b26f615d4f91b840a9cd8b0d29bd (patch)
tree0ea4ec7eb75c3d9db1f5aa918fe86959f56bfe3d
parente04be1f6e3b4f0f443a411122e7928ef1eea3ad0 (diff)
lib: Create API to start and stop BLE scanning
-rw-r--r--lib/ble.c41
-rw-r--r--lib/ble.h35
2 files changed, 75 insertions, 1 deletions
diff --git a/lib/ble.c b/lib/ble.c
index a58b0a5..960b046 100644
--- a/lib/ble.c
+++ b/lib/ble.c
@@ -41,8 +41,47 @@ static struct libdata {
int client;
uint8_t adapter_state;
+ uint8_t scan_state;
} 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);
+}
+
/* 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) {
@@ -56,7 +95,7 @@ static void register_client_cb(int status, int client_if, bt_uuid_t *app_uuid) {
/* GATT client interface callbacks */
static const btgatt_client_callbacks_t gattccbs = {
register_client_cb,
- NULL, /* scan_result_cb */
+ scan_result_cb,
NULL, /* connect_cb */
NULL, /* disconnect_cb */
NULL, /* search_complete_cb */
diff --git a/lib/ble.h b/lib/ble.h
index c4e4edd..481e076 100644
--- a/lib/ble.h
+++ b/lib/ble.h
@@ -55,11 +55,26 @@ typedef void (*ble_enable_cb_t)(void);
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);
+
+/**
* 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_cbs_t;
/**
@@ -79,4 +94,24 @@ int ble_enable(ble_cbs_t cbs);
* @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();
#endif