aboutsummaryrefslogtreecommitdiff
path: root/apps/ble-read-characteristic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'apps/ble-read-characteristic.cpp')
-rw-r--r--apps/ble-read-characteristic.cpp69
1 files changed, 58 insertions, 11 deletions
diff --git a/apps/ble-read-characteristic.cpp b/apps/ble-read-characteristic.cpp
index 0aa5fdc..19bbfa3 100644
--- a/apps/ble-read-characteristic.cpp
+++ b/apps/ble-read-characteristic.cpp
@@ -12,24 +12,63 @@ using namespace std;
using namespace trygvis::bluetooth;
using namespace trygvis::apps;
-class ble_inspect_device : public app {
+namespace {
+enum class op {
+ READ, NOTIFY, LISTEN
+};
+
+std::istream &operator>>(std::istream &in, op &op) {
+ std::string token;
+ in >> token;
+ if (token == "read") {
+ op = op::READ;
+ } else if (token == "notify") {
+ op = op::NOTIFY;
+ } else if (token == "listen") {
+ op = op::LISTEN;
+ } else {
+ in.setstate(std::ios_base::failbit);
+ }
+ return in;
+}
+
+std::ostream &operator<<(std::ostream &out, const op op) {
+ if (op == op::READ) {
+ out << "read";
+ } else if (op == op::NOTIFY) {
+ out << "notify";
+ } else if (op == op::LISTEN) {
+ out << "listen";
+ } else {
+ out.setstate(std::ios_base::failbit);
+ }
+ return out;
+}
+}
+
+class ble_read_characteristic : public app {
public:
- ble_inspect_device() : app("ble-inspect-device") {}
+ ble_read_characteristic() : app("ble-read-characteristic") {}
- ~ble_inspect_device() override = default;
+ ~ble_read_characteristic() override = default;
- string adapter_name;
+ string adapter_name = "0";
+ enum op op_mode = op::READ;
void add_options(po::options_description_easy_init &options) override {
- auto adapter_value = po::value<>(&adapter_name)->default_value("0");
+ auto adapter_value = po::value<>(&adapter_name)->default_value(adapter_name);
options("adapter", adapter_value, "Which adapter to use.");
options("device", po::value<string>()->required(), "The MAC of the device to inspect");
options("service", po::value<string>()->required(), "The UUID of the service to read");
options("characteristic", po::value<string>()->required(), "The UUID of the characteristic to read");
+
+ auto mode_value = po::value<>(&op_mode)->default_value(op_mode);
+ options("mode", mode_value, "Operation mode");
}
- int with_device(const shared_ptr<BluetoothDevice> &device, const Uuid &service_uuid, const Uuid &characteristic_uuid) {
- cout << "Inspecting device: " << device->getMac().str() << endl;
+ int with_device(const shared_ptr<BluetoothDevice> &device, const Uuid &service_uuid,
+ const Uuid &characteristic_uuid) {
+ cout << "Connecting to device: " << device->getMac().str() << endl;
auto gatt = device->connectGatt();
cout << "Connected, discovering services" << endl;
@@ -52,7 +91,14 @@ public:
auto characteristic = characteristicO.value();
- gatt->readValue(characteristic);
+ if (op_mode == op::READ) {
+ cout << "Reading data" << endl;
+
+ StaticByteBuffer<100> buf;
+ auto response = gatt->readValue(characteristic, buf);
+
+ cout << "Got data, size=" << response.getSize() << endl;
+ }
return EXIT_SUCCESS;
}
@@ -84,11 +130,12 @@ public:
}
}
};
-}
-}
+
+} // namespace apps
+} // namespace trygvis
int main(int argc, const char *argv[]) {
using namespace trygvis::apps;
- return real_main(new ble_inspect_device(), argc, argv);
+ return real_main(new ble_read_characteristic(), argc, argv);
}