A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-group-table.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Tokushima University, Japan
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author:
7 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
8 */
9
10#include "zigbee-group-table.h"
11
12#include "ns3/log.h"
13
14namespace ns3
15{
16namespace zigbee
17{
18
19NS_LOG_COMPONENT_DEFINE("ZigbeeGroupTable");
20
22
24
25bool
26ZigbeeGroupTable::AddEntry(uint16_t groupId, uint8_t endPoint)
27{
28 NS_LOG_FUNCTION(this << groupId << static_cast<uint16_t>(endPoint));
29
31 {
32 NS_LOG_WARN("Group table is full, cannot add group ID " << groupId);
33 return false; // too many groups
34 }
35
36 // Check the group entry existence, if it does not exist, create it.
37 auto& group = m_groupTable[groupId];
38 if (group[endPoint])
39 {
40 NS_LOG_INFO("Endpoint " << static_cast<uint16_t>(endPoint) << " already exists in group "
41 << groupId);
42 return false; // endPoint already in group
43 }
44
45 NS_LOG_INFO("Adding endpoint " << static_cast<uint16_t>(endPoint) << " to group " << groupId);
46 group.set(endPoint);
47 return true;
48}
49
50bool
51ZigbeeGroupTable::RemoveEntry(uint16_t groupId, uint8_t endPoint)
52{
53 NS_LOG_FUNCTION(this << groupId << static_cast<uint16_t>(endPoint));
54
55 auto group = m_groupTable.find(groupId);
56 auto endPoints = group->second;
57
58 if (group == m_groupTable.end())
59 {
60 NS_LOG_WARN("Group ID " << groupId << " does not exist, cannot remove endpoint "
61 << static_cast<uint16_t>(endPoint));
62 return false; // group doesn't exist
63 }
64
65 if (!endPoints[endPoint])
66 {
67 NS_LOG_WARN("Endpoint " << static_cast<uint16_t>(endPoint) << " not found in group "
68 << groupId);
69 return false; // endpoint not in this group
70 }
71 else
72 {
73 endPoints.reset(endPoint); // Turn bit in bitset off (0)
74 NS_LOG_INFO("Removed endpoint " << static_cast<uint16_t>(endPoint) << " from group "
75 << groupId);
76
77 if (endPoints.none())
78 {
79 m_groupTable.erase(group); // remove group if no endpoints left
80 }
81 return true; // endpoint removed successfully
82 }
83}
84
85bool
87{
88 NS_LOG_FUNCTION(this << static_cast<uint16_t>(endPoint));
89
90 bool removed = false;
91 for (auto group = m_groupTable.begin(); group != m_groupTable.end(); ++group)
92 {
93 if (group->second[endPoint])
94 {
95 group->second.reset(endPoint); // Turn bit in bitset off (0)
96 NS_LOG_INFO("Removed endpoint " << static_cast<uint16_t>(endPoint) << " from group "
97 << group->first);
98
99 if (group->second.none())
100 {
101 m_groupTable.erase(group); // remove group if no endpoints left
102 }
103 removed = true; // endpoint removed successfully
104 }
105 }
106 return removed;
107}
108
109bool
110ZigbeeGroupTable::IsGroupMember(uint16_t groupId) const
111{
112 return m_groupTable.find(groupId) != m_groupTable.end();
113}
114
115bool
116ZigbeeGroupTable::LookUpEndPoints(uint16_t groupId, std::vector<uint8_t>& endPoints) const
117{
118 bool hasEndPoints = false;
119 auto group = m_groupTable.find(groupId);
120
121 if (group == m_groupTable.end())
122 {
123 NS_LOG_WARN("Group ID " << groupId << " does not exist, cannot look up endpoints");
124 return false; // group doesn't exist
125 }
126
127 for (auto endpoint = 0; endpoint < MAX_ENDPOINT_ENTRIES; ++endpoint)
128 {
129 if (group->second[endpoint])
130 {
131 endPoints.push_back(endpoint);
132 hasEndPoints = true;
133 }
134 }
135 return hasEndPoints;
136}
137
138} // namespace zigbee
139} // namespace ns3
bool RemoveEntry(uint16_t groupId, uint8_t endPoint)
Remove endpoint from a group.
bool IsGroupMember(uint16_t groupId) const
Indicates whether the group ID exists in the group table.
std::unordered_map< uint16_t, std::bitset< MAX_ENDPOINT_ENTRIES > > m_groupTable
The group table object.
static constexpr int MAX_ENDPOINT_ENTRIES
The maximum amount of endpoints allowed per group id entry.
bool LookUpEndPoints(uint16_t groupId, std::vector< uint8_t > &endPoints) const
Look up the endpoints associated with a given group ID.
ZigbeeGroupTable()
Constructor for Zigbee group table.
bool RemoveMembership(uint8_t endPoint)
Remove the endPoint from all groups.
~ZigbeeGroupTable()
Destructor for Zigbee group table.
static constexpr int MAX_GROUP_ID_ENTRIES
The maximum amount of group ID entries allowed in the table.
bool AddEntry(uint16_t groupId, uint8_t endPoint)
Add a group ID and its related endpoint.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Every class exported by the ns3 library is enclosed in the ns3 namespace.