aboutsummaryrefslogtreecommitdiff
path: root/apps/ble-scan.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-07-19 21:39:28 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-07-19 21:49:03 +0200
commitae2d05eee4ffcec4c0611d907779ce8ef61d3a6e (patch)
tree6b86d64d03dfda4efc4a41e5814a229507289cb9 /apps/ble-scan.cpp
parent0374af511d7efdb856af372f126e66e5a78841d7 (diff)
downloadble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.tar.gz
ble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.tar.bz2
ble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.tar.xz
ble-toys-ae2d05eee4ffcec4c0611d907779ce8ef61d3a6e.zip
o Going back to a bunch of cpp files instead of launcher+bunch of header files. This ends up with an easier build file and faster builds with CMake's "OBJECT" library type.
Diffstat (limited to 'apps/ble-scan.cpp')
-rw-r--r--apps/ble-scan.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/apps/ble-scan.cpp b/apps/ble-scan.cpp
new file mode 100644
index 0000000..839a9fa
--- /dev/null
+++ b/apps/ble-scan.cpp
@@ -0,0 +1,85 @@
+#include "ble/Bluetooth.h"
+#include "apps.h"
+
+#include <csignal>
+#include "apps.h"
+
+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;
+ }
+ }
+};
+}
+}
+
+int main(int argc, const char *argv[]) {
+ using namespace trygvis::apps;
+
+ return real_main(new ble_scan(), argc, argv);
+}