diff --git a/c2usb/i2c/base.hpp b/c2usb/i2c/base.hpp index 839511d..d2fea07 100644 --- a/c2usb/i2c/base.hpp +++ b/c2usb/i2c/base.hpp @@ -39,6 +39,8 @@ class address bool is_10bit() const { return (_code & MODE_MASK) == static_cast(mode::_10BIT); } uint16_t raw() const { return _code; } + constexpr bool operator==(const address& rhs) const = default; + // special reserved addresses constexpr static address general_call() { return address(0); } constexpr static address start_byte() { return address(1); } diff --git a/c2usb/i2c/hid/device.cpp b/c2usb/i2c/hid/device.cpp index a358ef9..0a808c9 100644 --- a/c2usb/i2c/hid/device.cpp +++ b/c2usb/i2c/hid/device.cpp @@ -17,13 +17,13 @@ using namespace i2c::hid; device::device(application& app, const product_info& pinfo, i2c::slave& slave, i2c::address address, uint16_t hid_descriptor_reg_address) - : app_(app), + : module(address), + app_(app), pinfo_(pinfo), slave_(slave), - bus_address_(address), hid_descriptor_reg_(hid_descriptor_reg_address) { - slave_.register_module(this, address); + slave_.register_module(*this); } device::~device() @@ -32,7 +32,7 @@ device::~device() slave().set_pin_interrupt(false); // disable I2C - slave().unregister_module(this); + slave().unregister_module(*this); // clear context get_report_.clear(); diff --git a/c2usb/i2c/hid/device.hpp b/c2usb/i2c/hid/device.hpp index 28c3f3b..681e010 100644 --- a/c2usb/i2c/hid/device.hpp +++ b/c2usb/i2c/hid/device.hpp @@ -68,7 +68,6 @@ class device : public slave::module, public ::hid::transport private: void link_reset(); - i2c::address bus_address() const { return bus_address_; } uint16_t hid_descriptor_reg_address() const { return hid_descriptor_reg_; } void get_hid_descriptor(descriptor& desc) const; result send_report(const std::span& data, ::hid::report::type type) override; @@ -112,7 +111,6 @@ class device : public slave::module, public ::hid::transport power_event_delegate power_event_delegate_{}; ::hid::reports_receiver rx_buffers_{}; i2c::slave& slave_; - i2c::address bus_address_; uint16_t hid_descriptor_reg_; single_elem_queue> in_queue_{}; uint8_t stage_{}; diff --git a/c2usb/i2c/slave.hpp b/c2usb/i2c/slave.hpp index 295f800..31aaf07 100644 --- a/c2usb/i2c/slave.hpp +++ b/c2usb/i2c/slave.hpp @@ -19,44 +19,54 @@ namespace i2c class slave : public polymorphic { public: - /// @brief The module class is a callback interface for I2C slave functionality + /// @brief The module class is an abstract base for I2C slave functionality /// at a specific address. - class module : public interface + class module : public polymorphic { public: + constexpr module(i2c::address slave_addr) + : slave_addr_(slave_addr) + {} virtual bool on_start(direction dir, size_t data_length) = 0; virtual void on_stop(direction dir, size_t data_length) = 0; + i2c::address address() const { return slave_addr_; } + + private: + i2c::address slave_addr_; }; - bool has_module([[maybe_unused]] address slave_addr) const { return module_ != nullptr; } + bool has_module() const { return module_ != nullptr; } + bool has_module([[maybe_unused]] address slave_addr) const + { + return has_module() and (module_->address() == slave_addr); + } /// @brief Registers a slave module on the I2C bus. - /// @param m: module pointer - /// @param slave_addr: the I2C slave address to listen to + /// @param m: module reference /// @return true if module registered - bool register_module(module* m, address slave_addr) + bool register_module(module& m) { // single module design at the moment if (module_ != nullptr) { return false; } - module_ = m; - start_listen(slave_addr); + module_ = &m; + start_listen(module_->address()); return true; } /// @brief Unregisters the active module from the I2C bus interface. - /// @param m: module pointer + /// @param m: module reference /// @return true if module unregistered - bool unregister_module(module* m) + bool unregister_module(module& m) { // single module design at the moment - if (module_ != m) + if (module_ != &m) { return false; } - stop_listen(); + stop_listen(module_->address()); module_ = nullptr; return true; } @@ -118,7 +128,8 @@ class slave : public polymorphic virtual void start_listen(address slave_addr) = 0; /// @brief Stop listening to I2C transfers - virtual void stop_listen() = 0; + /// @param slave_addr: the I2C address to stop listening on + virtual void stop_listen(address slave_addr) = 0; /// @brief Call the module to handle I2C (re)start events. /// @param dir: the transfer's direction diff --git a/c2usb/port/zephyr/bluetooth/hid.hpp b/c2usb/port/zephyr/bluetooth/hid.hpp index 867eb9f..3a179c4 100644 --- a/c2usb/port/zephyr/bluetooth/hid.hpp +++ b/c2usb/port/zephyr/bluetooth/hid.hpp @@ -285,6 +285,7 @@ template <::hid::report_protocol_properties REPORT_PROPS, auto BOOT = boot_proto class service_instance : public service { friend class service; + public: service_instance(::hid::application& app, security sec, flags f = (flags)((uint8_t)flags::REMOTE_WAKE | diff --git a/c2usb/usb/df/class/cdc.hpp b/c2usb/usb/df/class/cdc.hpp index 44dbe76..faf9cc9 100644 --- a/c2usb/usb/df/class/cdc.hpp +++ b/c2usb/usb/df/class/cdc.hpp @@ -14,7 +14,6 @@ #include "usb/class/cdc.hpp" #include "usb/df/function.hpp" - namespace usb::standard::descriptor { struct interface; diff --git a/c2usb/usb/df/config.hpp b/c2usb/usb/df/config.hpp index f37ce23..0183524 100644 --- a/c2usb/usb/df/config.hpp +++ b/c2usb/usb/df/config.hpp @@ -51,8 +51,8 @@ class power constexpr auto max_power_mA() const { return value_ >> 7; } constexpr bool valid() const { return value_ != 0; } - friend auto operator<<(standard::descriptor::configuration* desc, const power& p) - -> standard::descriptor::configuration*; + friend auto operator<<(standard::descriptor::configuration* desc, + const power& p) -> standard::descriptor::configuration*; protected: constexpr power(source src = source::BUS, uint16_t max_current_mA = 100,