From c83b35d6456c8a77e5b8f6a08c9262122c3cbcfc Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 19 Nov 2018 22:09:59 +0100 Subject: ByteBuffer: o Reducing silliness, no allocations by ByteBuffer. o Create StaticByteBuffer as a nice one-liner to create a buffer. LinuxBluetooth: methods that want a buffer needs to pass it in, ByteBuffer is not allocating anymore. --- apps/ble-read-characteristic.cpp | 69 +++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 11 deletions(-) (limited to 'apps/ble-read-characteristic.cpp') 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()->required(), "The MAC of the device to inspect"); options("service", po::value()->required(), "The UUID of the service to read"); options("characteristic", po::value()->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 &device, const Uuid &service_uuid, const Uuid &characteristic_uuid) { - cout << "Inspecting device: " << device->getMac().str() << endl; + int with_device(const shared_ptr &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); } -- cgit v1.2.3