A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
zigbee-aps-tables.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 * Authors:
7 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
8 */
9
10#include "zigbee-aps-tables.h"
11
12#include "ns3/log.h"
13#include "ns3/pointer.h"
14#include "ns3/simulator.h"
15
16#include <algorithm>
17#include <iomanip>
18
19namespace ns3
20{
21namespace zigbee
22{
23
24NS_LOG_COMPONENT_DEFINE("ZigbeeApsTables");
25
26/***********************************************************
27 * Source Binding Entry
28 ***********************************************************/
29
33
34SrcBindingEntry::SrcBindingEntry(Mac64Address address, uint8_t endPoint, uint16_t clusterId)
35{
36 m_srcAddr = address;
37 m_srcEndPoint = endPoint;
38 m_clusterId = clusterId;
39}
40
44
45void
50
51void
53{
54 m_srcEndPoint = endPoint;
55}
56
57void
59{
60 m_clusterId = clusterId;
61}
62
65{
66 return m_srcAddr;
67}
68
69uint8_t
74
75uint16_t
77{
78 return m_clusterId;
79}
80
81/***********************************************************
82 * Destination Binding Entry
83 ***********************************************************/
84
88
92
93void
98
99void
104
105void
110
111void
113{
114 m_dstEndPoint = endPoint;
115}
116
119{
120 return m_dstAddrMode;
121}
122
125{
126 return m_dstAddr16;
127}
128
131{
132 return m_dstAddr64;
133}
134
135uint8_t
137{
138 return m_dstEndPoint;
139}
140
141/***********************************************************
142 * Binding Table
143 ***********************************************************/
144
150
151bool
153{
155 {
156 // Group Addressing
157 if ((first.GetDstAddr16() == second.GetDstAddr16()) &&
158 (first.GetDstEndPoint() == second.GetDstEndPoint()))
159 {
160 return true;
161 }
162 }
164 {
165 // IEEE Addressing
166 if ((first.GetDstAddr64() == second.GetDstAddr64()) &&
167 (first.GetDstEndPoint() == second.GetDstEndPoint()))
168 {
169 return true;
170 }
171 }
172 return false;
173}
174
175bool
177{
178 return ((first.GetSrcAddress() == second.GetSrcAddress()) &&
179 (first.GetSrcEndPoint() == second.GetSrcEndPoint()) &&
180 (first.GetClusterId() == second.GetClusterId()));
181}
182
185{
186 for (auto& entry : m_bindingTable)
187 {
188 if (CompareSources(src, entry.first))
189 {
190 // The source exist, now check if the destination exist
191 for (const auto& destination : entry.second)
192 {
193 if (CompareDestinations(dst, destination))
194 {
195 NS_LOG_WARN("Entry already exist in binding table");
197 }
198 }
199 // Add the new destination bound to the source
200 if (entry.second.size() >= m_maxDstEntries)
201 {
202 NS_LOG_WARN("Binding Table full, max destination entries (" << m_maxDstEntries
203 << ") reached");
205 }
206 else
207 {
208 entry.second.emplace_back(dst);
210 }
211 }
212 }
213
214 if (m_bindingTable.size() >= m_maxSrcEntries)
215 {
216 NS_LOG_WARN("Binding Table full, max source entries (" << m_maxSrcEntries << ") reached");
218 }
219 else
220 {
221 // New source with its first destination
222 m_bindingTable.emplace_back(src, std::vector<DstBindingEntry>{dst});
224 }
225}
226
229{
230 for (auto it = m_bindingTable.begin(); it != m_bindingTable.end(); ++it)
231 {
232 if (CompareSources(src, it->first))
233 {
234 // The source exists, now check if the destination exists
235 auto& destinations = it->second;
236 for (auto destIt = destinations.begin(); destIt != destinations.end(); ++destIt)
237 {
238 if (CompareDestinations(dst, *destIt))
239 {
240 // Destination found, remove it
241 destinations.erase(destIt);
242
243 // If no destinations left, remove the source entry
244 if (destinations.empty())
245 {
246 m_bindingTable.erase(it);
247 }
248 // Successfully unbound
250 }
251 }
252 // Destination not found
253 NS_LOG_WARN("Cannot unbind, destination entry do not exist");
255 }
256 }
257 // Source not found
258 NS_LOG_WARN("Cannot unbind, source entry do not exist");
260}
261
262bool
263BindingTable::LookUpEntries(const SrcBindingEntry& src, std::vector<DstBindingEntry>& dstEntries)
264{
265 for (auto& entry : m_bindingTable)
266 {
267 if (CompareSources(src, entry.first))
268 {
269 // The source entry exist, return all the dst entries.
270 dstEntries = entry.second;
271 return true;
272 }
273 }
274 return false;
275}
276
277} // namespace zigbee
278} // namespace ns3
This class can contain 16 bit addresses.
an EUI-64 address
bool CompareSources(const SrcBindingEntry &first, const SrcBindingEntry &second)
Compare the equality of 2 source entries.
uint8_t m_maxSrcEntries
The maximum amount of source entries allowed in the table.
bool LookUpEntries(const SrcBindingEntry &src, std::vector< DstBindingEntry > &dstEntries)
Look for destination entries binded to an specific source entry portion in the binding table.
BindingTableStatus Bind(const SrcBindingEntry &src, const DstBindingEntry &dst)
Add an entry to the binding table.
BindingTableStatus Unbind(const SrcBindingEntry &src, const DstBindingEntry &dst)
Unbinds a destination entry portion of a binding table from a source entry portion.
BindingTable()
The constructor of the binding table.
std::vector< std::pair< SrcBindingEntry, std::vector< DstBindingEntry > > > m_bindingTable
The binding table object.
bool CompareDestinations(const DstBindingEntry &first, const DstBindingEntry &second)
Compare the equality of 2 destination entries.
uint8_t m_maxDstEntries
The maximum amount of destination entries allowed in the table.
Binding Table entry: Destination portion of the table.
Mac64Address m_dstAddr64
The destination IEEE address (64-bit address) in the destination entry.
Mac16Address m_dstAddr16
The destination 16-bit address in the destination entry.
~DstBindingEntry()
DstBindingEntry()
The default constructor of the destination binding entry.
ApsDstAddressModeBind m_dstAddrMode
The destination address mode used by the entry.
Mac16Address GetDstAddr16() const
Get the 16-bit address destination of the destination entry.
Mac64Address GetDstAddr64() const
Get the 64-bit address destination of the destination entry.
uint8_t GetDstEndPoint() const
Get the destination endpoint of the destination entry.
void SetDstAddr64(Mac64Address address)
Set the destination IEEE Address (64-bit address) of the destination binding entry.
ApsDstAddressModeBind GetDstAddrMode() const
Get the destination address mode used by the destination entry.
uint8_t m_dstEndPoint
The destination endpoint in the destination entry.
void SetDstAddrMode(ApsDstAddressModeBind mode)
Set the destination address mode of the destination binding entry.
void SetDstEndPoint(uint8_t endPoint)
Set the destination endppoint to the destination binding entry.
void SetDstAddr16(Mac16Address address)
Set the destination 16-bit address of the destination binding entry.
Binding Table entry: Source portion of the table.
uint16_t GetClusterId() const
Get the cluster ID from the source binding entry.
uint8_t GetSrcEndPoint() const
Get the source endpoint from the source binding entry.
SrcBindingEntry()
The default constructor of the source binding entry.
void SetSrcEndPoint(uint8_t endPoint)
Set the source endpoint of the source binding entry.
~SrcBindingEntry()
uint8_t m_srcEndPoint
The source endpoint in the source entry.
void SetClusterId(uint16_t clusterId)
Set the cluster ID of the source binding entry.
Mac64Address m_srcAddr
The source IEEE address in the source entry.
void SetSrcAddress(Mac64Address address)
Set the source IEEE address to the entry.
Mac64Address GetSrcAddress() const
Get the IEEE address from the source binding entry.
uint16_t m_clusterId
The cluster ID in the source entry.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
BindingTableStatus
The status resulting of interactions with the binding table.
ApsDstAddressModeBind
APS Destination Address Mode for Binding Zigbee Specification r22.1.0, Table 2-6 APSME-BIND....
Definition first.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.