aboutsummaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-06-21 16:58:18 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-06-21 16:58:18 +0200
commit9042b87f32bcabcc671e393376d3fb96ad858caa (patch)
treec68fe4f680fcc2a53ff3375141c9ad52cd1a028e /apps
parent4529a75f15bffd5bb941818deef03fb50a6fc9c9 (diff)
downloadble-toys-9042b87f32bcabcc671e393376d3fb96ad858caa.tar.gz
ble-toys-9042b87f32bcabcc671e393376d3fb96ad858caa.tar.bz2
ble-toys-9042b87f32bcabcc671e393376d3fb96ad858caa.tar.xz
ble-toys-9042b87f32bcabcc671e393376d3fb96ad858caa.zip
ble-scan:
o New tool to scan for devices. Requires root on linux :( Linux is also touchy if the program dies. BluetoothAdapter: o Adding getMac().
Diffstat (limited to 'apps')
-rw-r--r--apps/CMakeLists.txt1
-rw-r--r--apps/ble-inspect-device.h4
-rw-r--r--apps/ble-scan.h87
-rw-r--r--apps/sm-get-value.h4
4 files changed, 92 insertions, 4 deletions
diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt
index c9eea9e..01252cf 100644
--- a/apps/CMakeLists.txt
+++ b/apps/CMakeLists.txt
@@ -1,4 +1,5 @@
list(APPEND APPS ble-inspect-device)
+list(APPEND APPS ble-scan)
list(APPEND APPS sample-add-timestamp)
list(APPEND APPS sample-convert)
list(APPEND APPS sample-select)
diff --git a/apps/ble-inspect-device.h b/apps/ble-inspect-device.h
index 0bb70aa..346f9be 100644
--- a/apps/ble-inspect-device.h
+++ b/apps/ble-inspect-device.h
@@ -56,9 +56,9 @@ public:
try {
Mac mac = Mac::parseMac(mac_str);
- BluetoothAdapter &adapter = getAdapter(0);
+ shared_ptr<BluetoothAdapter> adapter = getAdapter(0);
- BluetoothDevice &device = adapter.getDevice(mac);
+ BluetoothDevice &device = adapter->getDevice(mac);
scan_callback(device);
diff --git a/apps/ble-scan.h b/apps/ble-scan.h
new file mode 100644
index 0000000..9a72388
--- /dev/null
+++ b/apps/ble-scan.h
@@ -0,0 +1,87 @@
+#include "ble/Bluetooth.h"
+#include "apps.h"
+
+#include <csignal>
+
+namespace trygvis {
+namespace apps {
+
+using namespace std;
+using namespace trygvis::bluetooth;
+using namespace trygvis::apps;
+
+namespace ble_scan_utils {
+
+static std::function<void(int)> onSignal;
+
+static void signal_handler(int signal) {
+ onSignal(signal);
+}
+
+}
+
+class ble_scan : public app {
+
+public:
+ ble_scan() : app("ble-scan") {
+ }
+
+ ~ble_scan() = default;
+
+ void add_options(po::options_description_easy_init &options) override {
+ options
+ ("adapter", po::value<int>()->default_value(0), "Which adapter to use.");
+ }
+
+ int main(app_execution &execution) override {
+ BluetoothSystem bluetoothSystem;
+ shared_ptr<BluetoothAdapter> adapter;
+
+ struct sigaction sigIntHandler;
+
+ ble_scan_utils::onSignal = [&](int signal) {
+ adapter->stopScan();
+ };
+
+ sigIntHandler.sa_handler = &ble_scan_utils::signal_handler;
+ sigemptyset(&sigIntHandler.sa_mask);
+ sigIntHandler.sa_flags = 0;
+
+ sigaction(SIGINT, &sigIntHandler, NULL);
+
+ try {
+ auto adapter_index = execution.vm["adapter"].as<int>();
+
+ adapter = getAdapter(adapter_index);
+
+ set<Mac> seen_devices;
+
+ cout << "Scanning with adapter #" << adapter_index << ", mac=" << adapter->getMac().str() << endl;
+
+ adapter->runScan([&](BluetoothDevice &device) {
+ auto mac = device.getMac();
+
+ cout << "Found: " << mac.str() << endl;
+
+ seen_devices.insert(mac);
+ });
+
+ cout << "Stopped. Found " << seen_devices.size() << " devices." << endl;
+
+ for_each(begin(seen_devices), end(seen_devices), [&](auto mac) {
+ cout << mac.str() << endl;
+ });
+
+ return EXIT_SUCCESS;
+ } catch (std::runtime_error ex) {
+ cout << "std::runtime_error: " << ex.what() << endl;
+ return EXIT_FAILURE;
+ } catch (std::exception ex) {
+ cout << "std::exception: " << ex.what() << endl;
+ return EXIT_FAILURE;
+ }
+ }
+};
+
+}
+}
diff --git a/apps/sm-get-value.h b/apps/sm-get-value.h
index 0073d7f..0535f68 100644
--- a/apps/sm-get-value.h
+++ b/apps/sm-get-value.h
@@ -71,9 +71,9 @@ public:
Mac mac = Mac::parseMac(MAC);
- auto &adapter = getAdapter(0);
+ auto adapter = getAdapter(0);
- auto &device = adapter.getDevice(mac);
+ auto &device = adapter->getDevice(mac);
loop = sleepTime > 0;