Radix Relay
Hybrid mesh communications with Signal Protocol encryption
Loading...
Searching...
No Matches
ble_stream.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <boost/asio.hpp>
6#include <boost/system/error_code.hpp>
7#include <simpleble/SimpleBLE.h>
8
9#include <cstddef>
10#include <functional>
11#include <memory>
12#include <optional>
13#include <span>
14#include <string_view>
15#include <vector>
16
18
20{
21 std::string_view device_address;
22 std::string_view service_uuid;
23 std::string_view characteristic_uuid;
24};
25
27{
28public:
30
31 explicit ble_stream(const std::shared_ptr<boost::asio::io_context> &io_context);
32
34 std::function<void(const boost::system::error_code &, std::size_t)> handler) -> void;
35
36 auto async_write(std::span<const std::byte> data,
37 const std::function<void(const boost::system::error_code &, std::size_t)> &handler) -> void;
38
39 auto async_read(const boost::asio::mutable_buffer &buffer,
40 const std::function<void(const boost::system::error_code &, std::size_t)> &handler) -> void;
41
42 auto async_close(std::function<void(const boost::system::error_code &, std::size_t)> handler) -> void;
43
44 [[nodiscard]] auto get_mtu() const -> std::size_t;
45
46private:
47 std::shared_ptr<boost::asio::io_context> io_context_;
48 boost::asio::strand<boost::asio::io_context::executor_type> strand_;
49 std::size_t mtu_{ 20 };
50 bool connected_{ false };
51
52 std::optional<SimpleBLE::Adapter> adapter_;
53 std::optional<SimpleBLE::Peripheral> peripheral_;
54
55 std::string tx_characteristic_uuid_;
56 std::string rx_characteristic_uuid_;
57 std::string service_uuid_;
58
59 std::vector<std::byte> read_buffer_;
60 std::function<void(const boost::system::error_code &, std::size_t)> pending_read_handler_;
61
62 static auto find_adapter() -> std::optional<SimpleBLE::Adapter>;
63 auto find_peripheral(const std::string &address) -> std::optional<SimpleBLE::Peripheral>;
64 auto setup_notification_callback() -> void;
65};
66
67static_assert(concepts::transport_stream<ble_stream>);
68
69}// namespace radix_relay::transport
auto async_close(std::function< void(const boost::system::error_code &, std::size_t)> handler) -> void
auto get_mtu() const -> std::size_t
auto async_connect(ble_connection_params params, std::function< void(const boost::system::error_code &, std::size_t)> handler) -> void
auto async_write(std::span< const std::byte > data, const std::function< void(const boost::system::error_code &, std::size_t)> &handler) -> void
auto async_read(const boost::asio::mutable_buffer &buffer, const std::function< void(const boost::system::error_code &, std::size_t)> &handler) -> void
ble_stream(const std::shared_ptr< boost::asio::io_context > &io_context)