diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2025-10-10 10:01:01 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2025-10-19 00:13:47 +0200 |
| commit | dad0aaa9b33a5a217ac115334a94fe299dce9e08 (patch) | |
| tree | baeffb1e3ef6a6e100bc2f255581d77c4b9a45d3 /src/bt.hh | |
| parent | 86ec0b5386fc2078891a829026844d2ec21ea7db (diff) | |
Add bluetooth module
Can't really do anything yet, but finds the bluetooth adapter
and registers an agent to make it pairable.
Diffstat (limited to 'src/bt.hh')
| -rw-r--r-- | src/bt.hh | 138 |
1 files changed, 138 insertions, 0 deletions
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 <cstdint> +#include <functional> +#include <memory> +#include <optional> +#include <string> +#include <vector> + +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<Device*> 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<void(std::optional<std::string>)> callback) = 0; + virtual void agent_display_pincode(Device& device, + std::string const& pincode) = 0; + virtual void agent_request_passkey( + Device& device, + std::function<void(std::optional<uint32_t>)> 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<void(bool)> callback) = 0; + virtual void agent_request_authorization( + Device& device, std::function<void(bool)> callback) = 0; + virtual void agent_authorize_service( + Device& device, std::string const& uuid, + std::function<void(bool)> 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<Manager> create_manager(logger::Logger& logger, + cfg::Config const& cfg, + looper::Looper& looper, + Manager::Delegate& delegate); + +} // namespace bt + +#endif // BT_HH |
