From dad0aaa9b33a5a217ac115334a94fe299dce9e08 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Fri, 10 Oct 2025 10:01:01 +0200 Subject: Add bluetooth module Can't really do anything yet, but finds the bluetooth adapter and registers an agent to make it pairable. --- src/bt.hh | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/bt.hh (limited to 'src/bt.hh') diff --git a/src/bt.hh b/src/bt.hh new file mode 100644 index 0000000..53d3919 --- /dev/null +++ b/src/bt.hh @@ -0,0 +1,138 @@ +#ifndef BT_HH +#define BT_HH + +#include +#include +#include +#include +#include +#include + +namespace logger { +class Logger; +} // namespace logger + +namespace looper { +class Looper; +} // namespace looper + +namespace cfg { +class Config; +} // namespace cfg + +namespace bt { + +class Device; + +class Adapter { + public: + virtual ~Adapter() = default; + + [[nodiscard]] + virtual std::string const& address() const = 0; + + [[nodiscard]] + virtual std::string const& name() const = 0; + + [[nodiscard]] + virtual bool discoverable() const = 0; + + [[nodiscard]] + virtual bool pairable() const = 0; + + [[nodiscard]] + virtual bool pairing() const = 0; + + [[nodiscard]] + virtual std::vector devices() const = 0; + + protected: + Adapter() = default; + Adapter(Adapter const&) = delete; + Adapter& operator=(Adapter const&) = delete; +}; + +class Device { + public: + virtual ~Device() = default; + + [[nodiscard]] + virtual std::string const& address() const = 0; + + [[nodiscard]] + virtual std::string const& name() const = 0; + + [[nodiscard]] + virtual bool paired() const = 0; + + [[nodiscard]] + virtual bool connected() const = 0; + + protected: + Device() = default; + Device(Device const&) = delete; + Device& operator=(Device const&) = delete; +}; + +class Manager { + public: + virtual ~Manager() = default; + + class Delegate { + public: + virtual ~Delegate() = default; + + // Called when "primary" adapter changes from one to another. + virtual void new_adapter(Adapter* /* adapter */) {} + // Called when "primary" adapter changes a property. + virtual void updated_adapter(Adapter& /* adapter */) {} + + // Called when a new device is added to adapter. + virtual void added_device(Device& /* device */) {} + // Called when a device was removed from adapter. + virtual void removed_device(std::string const& /* address */) {} + // Called when device changes a property. + virtual void updated_device(Device& /* device */) {} + + virtual void agent_request_pincode( + Device& device, + std::function)> callback) = 0; + virtual void agent_display_pincode(Device& device, + std::string const& pincode) = 0; + virtual void agent_request_passkey( + Device& device, + std::function)> callback) = 0; + virtual void agent_display_passkey(Device& device, uint32_t passkey, + uint16_t entered) = 0; + virtual void agent_request_confirmation( + Device& device, uint32_t passkey, + std::function callback) = 0; + virtual void agent_request_authorization( + Device& device, std::function callback) = 0; + virtual void agent_authorize_service( + Device& device, std::string const& uuid, + std::function callback) = 0; + virtual void agent_cancel() = 0; + + protected: + Delegate() = default; + }; + + // Returns null if there are no adapters. + // Otherwise, returns the "primary" adapter. + virtual Adapter* adapter() = 0; + + protected: + Manager() = default; + Manager(Manager const&) = delete; + Manager& operator=(Manager const&) = delete; +}; + +std::unique_ptr create_manager(logger::Logger& logger, + cfg::Config const& cfg, + looper::Looper& looper, + Manager::Delegate& delegate); + +} // namespace bt + +#endif // BT_HH -- cgit v1.2.3-70-g09d2