Skip to content

Commit

Permalink
cluster: major change: remove command maps
Browse files Browse the repository at this point in the history
  • Loading branch information
zakaria-fadli-netatmo committed Jul 18, 2023
1 parent 1335f27 commit fd54977
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 317 deletions.
52 changes: 15 additions & 37 deletions include/device/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@

#include <cstdint>
#include <iomanip>
#include <memory>
#include <utility>
#include <vector>
#include <memory>

#include <zcl/clusters/on_off_cluster.hpp>

#include "nwk/pan.hpp"
#include "zcl/common/cluster.hpp"
#include "zcl/common/commands.hpp"

#include "nwk/pan.hpp"

namespace device
{

Expand Down Expand Up @@ -66,7 +64,8 @@ class MacAddress

if (size > address.size())
{
throw std::runtime_error("MacAddress constructor received too many bytes");
throw std::runtime_error(
"MacAddress constructor received too many bytes");
}

for (auto arg : {args...})
Expand All @@ -75,7 +74,6 @@ class MacAddress
}

std::cout << "MacAddress created with " << size << " bytes" << std::endl;

}

[[nodiscard]] std::string get_string() const
Expand All @@ -98,34 +96,29 @@ class MacAddress
return address == rhs.address;
}

bool operator!=(const MacAddress& rhs) const
{
return !(rhs == *this);
}
bool operator!=(const MacAddress& rhs) const { return !(rhs == *this); }
};

class Device
{
MacAddress mac_address;
MacAddress mac_address;
endpoint_list_t endpoints;

// Attributes depending on the network
std::weak_ptr<nwk::Pan> pan{};
short_address_t short_address{};
bool reacheable{true};
short_address_t short_address{};
bool reacheable{true};

public:
Device() = delete;
Device(const MacAddress& mac_address, endpoint_list_t endpoints)
: mac_address(mac_address),
endpoints(std::move(endpoints))
: mac_address(mac_address), endpoints(std::move(endpoints))
{
std::cout << "Device created with MAC: " << mac_address.get_string()
<< " and " << endpoints.size() << " endpoints"
<< std::endl;
std::cout << "Device created with MAC: " << mac_address.get_string()
<< " and " << endpoints.size() << " endpoints" << std::endl;
}

[[nodiscard]] MacAddress get_mac_address() const { return mac_address; }
[[nodiscard]] MacAddress get_mac_address() const { return mac_address; }

[[nodiscard]] short_address_t get_short_address() const
{
Expand All @@ -144,7 +137,8 @@ class Device
Endpoint& get_endpoint(const endpoint_id_t ep_id)
{
auto ept = std::find_if(endpoints.begin(), endpoints.end(),
[ep_id](const Endpoint& ept) { return ept.get_endpoint_id() == ep_id; });
[ep_id](const Endpoint& ept)
{ return ept.get_endpoint_id() == ep_id; });

if (ept == endpoints.end())
{
Expand All @@ -154,19 +148,6 @@ class Device
return *ept;
}

template <typename... Args>
zcl::ZclStatus execute_cluster_command(const endpoint_id_t ep_id,
const zcl::cluster_id_t cluster_id,
const zcl::command_id_t command_id,
bool is_common, Args... args)
{
Endpoint ept = get_endpoint(ep_id);
zcl::Cluster cluster = ept.get_cluster(cluster_id);

return cluster.execute_cluster_command<Args...>(command_id, is_common,
args...);
}

bool join_pan(const std::shared_ptr<nwk::Pan>& pan)
{
if (!pan->is_permit_joining())
Expand All @@ -179,10 +160,7 @@ class Device
return true;
}

void leave_pan()
{
pan.reset();
}
void leave_pan() { pan.reset(); }
};

class OnOffDevice : public Device
Expand Down
7 changes: 3 additions & 4 deletions include/device_pool/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#define DEVICE_POOL_POOL_HPP

#include <device/device.hpp>
#include <nwk/pan.hpp>

#include <memory>
#include <nwk/pan.hpp>

namespace device
{
Expand All @@ -14,15 +13,15 @@ class Pool
device_list devices = {};

// Each pool has its own PAN
std::shared_ptr<nwk::Pan> pan;
std::shared_ptr<nwk::Pan> pan;

public:
Pool() = delete;

explicit Pool(nwk::pan_id_t pan_id) : pan(std::make_shared<nwk::Pan>(pan_id))
{
}

Pool(device_list devices, nwk::pan_id_t pan_id)
: devices(std::move(devices)), pan(std::make_shared<nwk::Pan>(pan_id))
{
Expand Down
62 changes: 36 additions & 26 deletions include/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,64 @@

#include <vector>

namespace event {
namespace event
{

// Base event class
class Event {
public:
virtual ~Event() = default;
class Event
{
public:
virtual ~Event() = default;
};

// Device announcement event
class DeviceAnnouncementEvent : public Event {
class DeviceAnnouncementEvent : public Event
{
Device& device;

public:
public:
explicit DeviceAnnouncementEvent(Device& device) : device(device) {}

[[nodiscard]] Device& get_device() const { return device; }
};

// Reporting event
class ReportingEvent : public Event {
class ReportingEvent : public Event
{
};

// Event handler interface
class IEventHandler {
public:
virtual void handleEvent(const Event& event) = 0;
virtual ~IEventHandler() = default;
class IEventHandler
{
public:
virtual void handleEvent(const Event& event) = 0;
virtual ~IEventHandler() = default;
};

// Zigbee device mocker
class ZigbeeDeviceMocker {
private:
std::vector<IEventHandler*> eventHandlers; // Event handler interface pointers

public:
// Register an event handler
void registerEventHandler(IEventHandler* handler) {
eventHandlers.push_back(handler);
}
class ZigbeeDeviceMocker
{
private:
std::vector<IEventHandler*>
eventHandlers; // Event handler interface pointers

public:
// Register an event handler
void registerEventHandler(IEventHandler* handler)
{
eventHandlers.push_back(handler);
}

// Emit an event and notify all registered event handlers
void emitEvent(const Event& event) {
for (auto handler : eventHandlers) {
handler->handleEvent(event);
}
// Emit an event and notify all registered event handlers
void emitEvent(const Event& event)
{
for (auto handler : eventHandlers)
{
handler->handleEvent(event);
}
}
};

} // namespace event

#endif // EVENTS_HPP
#endif // EVENTS_HPP
27 changes: 9 additions & 18 deletions include/nwk/nwk_mgt_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,31 @@ namespace nwk
{

// Request NWK address command
const zcl::CommandDescriptor requestNwkAddressCmdDescriptor{
const zcl::CommandDescriptor requestNwkAddressCommandDescriptor{
/*cmd_id=*/0,
/*is_common=*/true,
/*is_mandatory=*/true,
/*description=*/"Request NWK address"};

zcl::ZclStatus requestNwkAddressExecuter(
zcl::ZclStatus requestNwkAddressCommand(
device::Pool& pool, const device::MacAddress& input_mac_address,
device::short_address_t& output_nwk_address);

const zcl::Command requestNwkAddressCommand{/*exec=*/requestNwkAddressExecuter};

// Request mac address command
const zcl::CommandDescriptor requestMacAddressCmdDescriptor{
const zcl::CommandDescriptor requestMacAddressCommandDescriptor{
/*cmd_id=*/1,
/*is_common=*/true,
/*is_mandatory=*/true,
/*description=*/"Request MAC address"};

zcl::ZclStatus requestMacAddressExecuter(
zcl::ZclStatus requestMacAddressCommand(
device::Pool& pool, const device::short_address_t& input_nwk_address,
device::MacAddress& output_mac_address);

const zcl::Command requestMacAddressCommand{/*exec=*/requestMacAddressExecuter};

// Leave command
const zcl::CommandDescriptor leaveCmdDescriptor{/*cmd_id=*/0x34,
/*is_common=*/true,
/*is_mandatory=*/true,
/*description=*/"leave"};

zcl::ZclStatus leaveExecuter(device::Device& device);

const zcl::Command leaveCommand{/*exec=*/leaveExecuter};
const zcl::CommandDescriptor leaveCommandDescriptor{/*cmd_id=*/0x34,
/*is_common=*/true,
/*is_mandatory=*/true,
/*description=*/"leave"};
zcl::ZclStatus leaveCommand(device::Device& device);

} // namespace nwk

Expand Down
11 changes: 7 additions & 4 deletions include/nwk/pan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ using pan_id_t = uint16_t;
class Pan
{
pan_id_t pan_id{};
bool permit_joining{false};
bool permit_joining{false};

public:
explicit Pan(pan_id_t pan_id) : pan_id(pan_id) {}

[[nodiscard]] pan_id_t get_pan_id() const { return pan_id; }
[[nodiscard]] bool is_permit_joining() const { return permit_joining; }
[[nodiscard]] bool is_permit_joining() const { return permit_joining; }

void set_permit_joining(bool permit_joining) { this->permit_joining = permit_joining; }
void set_permit_joining(bool permit_joining)
{
this->permit_joining = permit_joining;
}
};

} // namespace nwk

#endif // NWK_PAN_HPP
#endif // NWK_PAN_HPP
21 changes: 0 additions & 21 deletions include/zcl/clusters/global_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,27 @@ const CommandDescriptor read_attribute_command_descriptor{
/*is_common*/ true,
/*is_mandatory*/ true,
/*description*/ "Read attribute"};
ZclStatus readAttributeCommandExecuter(Cluster& cluster,
const attr_id_t& attribute_id,
attr_value_t& value);
const Command readAttributeCommand{/*exec=*/readAttributeCommandExecuter};

// Write attribute command
const CommandDescriptor write_attribute_command_descriptor{
/*id*/ 0x02,
/*is_common*/ true,
/*is_mandatory*/ true,
/*description*/ "Write attribute"};
ZclStatus writeAttributeCommandExecuter(Cluster& cluster,
const attr_id_t& attribute_id,
const attr_value_t& value);
const Command writeAttributeCommand{/*exec=*/writeAttributeCommandExecuter};

// Configure reporting command
const CommandDescriptor configure_reporting_command_descriptor{
/*id*/ 0x06,
/*is_common*/ true,
/*is_mandatory*/ true,
/*description*/ "Configure reporting"};
ZclStatus configureReportingCommandExecuter(
Cluster& cluster, const attr_id_t& attribute_id,
const ReportingConfiguration& reporting_configuration);
const Command configureReportingCommand{
/*exec=*/configureReportingCommandExecuter};

// Discover attributes command
const CommandDescriptor discover_attributes_command_descriptor{
/*id*/ 0x0C,
/*is_common*/ true,
/*is_mandatory*/ true,
/*description*/ "Discover attributes"};
ZclStatus discoverAttributesCommandExecuter(
Cluster& cluster,
std::vector<attribute_descriptor_t>& attribute_descriptors);
const Command discoverAttributesCommand{
/*exec=*/discoverAttributesCommandExecuter};

// FIXME This command requires a special handling
// since it is send by the device
Expand All @@ -68,9 +50,6 @@ const CommandDescriptor report_attributes_command_descriptor{
/*is_common*/ true,
/*is_mandatory*/ true,
/*description*/ "Report attributes"};
ZclStatus reportAttributesCommandExecuter(Cluster& cluster,
const Attribute& attribute);
const Command reportAttributesCommand{/*exec=*/reportAttributesCommandExecuter};

} // namespace zcl

Expand Down
Loading

0 comments on commit fd54977

Please sign in to comment.