Skip to content

Commit d76e616

Browse files
committed
Review ZigBeeAddress classes
1 parent 01da968 commit d76e616

12 files changed

+86
-234
lines changed

libraries/ZigBeeNet/App/Discovery/ZigBeeNetworkDiscoverer.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void RediscoverNode(ushort networkAddress)
194194
{
195195
// Request basic response, start index for associated list is 0
196196
IeeeAddressRequest ieeeAddressRequest = new IeeeAddressRequest();
197-
ieeeAddressRequest.DestinationAddress = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.GetBroadcastDestination(BroadcastDestination.BROADCAST_RX_ON).Key);
197+
ieeeAddressRequest.DestinationAddress = ZigBeeEndpointAddress.BROADCAST_RX_ON;
198198
ieeeAddressRequest.RequestType = 0;
199199
ieeeAddressRequest.StartIndex = 0;
200200
ieeeAddressRequest.NwkAddrOfInterest = networkAddress;
@@ -273,7 +273,7 @@ public void RediscoverNode(IeeeAddress ieeeAddress)
273273
request.IeeeAddr = ieeeAddress;
274274
request.RequestType = 0;
275275
request.StartIndex = 0;
276-
request.DestinationAddress = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.GetBroadcastDestination(BroadcastDestination.BROADCAST_RX_ON).Key);
276+
request.DestinationAddress = ZigBeeEndpointAddress.BROADCAST_RX_ON;
277277
CommandResult response = await _networkManager.SendTransaction(request, request);
278278

279279
NetworkAddressResponse nwkAddressResponse = response.GetResponse<NetworkAddressResponse>();
@@ -439,7 +439,7 @@ private async Task<bool> GetIeeeAddress(ushort networkAddress)
439439
request.RequestType = 0;
440440
request.StartIndex = 0;
441441
request.NwkAddrOfInterest = networkAddress;
442-
request.DestinationAddress = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.GetBroadcastDestination(BroadcastDestination.BROADCAST_RX_ON).Key);
442+
request.DestinationAddress = ZigBeeEndpointAddress.BROADCAST_RX_ON;
443443
CommandResult response = await _networkManager.SendTransaction(request, request);
444444

445445
if (response.IsError())

libraries/ZigBeeNet/App/Discovery/ZigBeeNodeServiceDiscoverer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private async Task<bool> RequestNetworkAddress()
386386
networkAddressRequest.IeeeAddr = Node.IeeeAddress;
387387
networkAddressRequest.RequestType = 0x00;
388388
networkAddressRequest.StartIndex = 0;
389-
networkAddressRequest.DestinationAddress = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.GetBroadcastDestination(BroadcastDestination.BROADCAST_ALL_DEVICES).Key);
389+
networkAddressRequest.DestinationAddress = ZigBeeEndpointAddress.BROADCAST_ALL_DEVICES;
390390

391391
CommandResult response = await NetworkManager.SendTransaction(networkAddressRequest, networkAddressRequest);
392392
NetworkAddressResponse networkAddressResponse = response.GetResponse<NetworkAddressResponse>();

libraries/ZigBeeNet/IZigBeeAddress.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace ZigBeeNet
66
{
7-
public interface IZigBeeAddress : IComparable<IZigBeeAddress>
7+
public interface IZigBeeAddress
88
{
99
/// <summary>
1010
/// The network address for this address.
1111
/// </summary>
12-
ushort Address { get; set; }
12+
ushort Address { get; }
1313

1414
/// <summary>
1515
/// Check whether this address is ZigBee group.

libraries/ZigBeeNet/Internal/ClusterMatcher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void CommandReceived(ZigBeeCommand command)
6969
}
7070

7171
if (matchRequest.NwkAddrOfInterest != _networkManager.LocalNwkAddress
72-
&& !ZigBeeBroadcastDestination.IsBroadcast(matchRequest.NwkAddrOfInterest))
72+
&& !ZigBeeBroadcastDestinationHelper.IsBroadcast(matchRequest.NwkAddrOfInterest))
7373
{
7474
_logger.LogDebug("{ExtPanId}: ClusterMatcher no match to local address", _networkManager.ZigBeeExtendedPanId);
7575
return;

libraries/ZigBeeNet/ZigBeeBroadcastDestination.cs

+4-62
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ZigBeeNet
1616
/// parameter to FALSE. All other flags of the TxOptions parameter shall be set based
1717
/// on the network configuration
1818
/// </summary>
19-
public enum BroadcastDestination : ushort
19+
public enum ZigBeeBroadcastDestination : ushort
2020
{
2121
/// <summary>
2222
/// All devices in PAN
@@ -45,67 +45,9 @@ public enum BroadcastDestination : ushort
4545
BROADCAST_RESERVED_FFF8 = 0xFFF8,
4646
}
4747

48-
49-
public class ZigBeeBroadcastDestination
48+
public static class ZigBeeBroadcastDestinationHelper
5049
{
51-
/// <summary>
52-
/// A mapping between the integer code and its corresponding type to
53-
/// facilitate lookup by code.
54-
/// </summary>
55-
private static Dictionary<BroadcastDestination, ZigBeeBroadcastDestination> _codeMapping;
56-
57-
public ushort Key { get; private set; }
58-
59-
public BroadcastDestination Destination { get; private set; }
60-
61-
private ZigBeeBroadcastDestination(BroadcastDestination destination)
62-
{
63-
this.Key = (ushort)destination;
64-
this.Destination = destination;
65-
}
66-
67-
private static void InitMapping()
68-
{
69-
_codeMapping = new Dictionary<BroadcastDestination, ZigBeeBroadcastDestination>
70-
{
71-
{ BroadcastDestination.BROADCAST_ALL_DEVICES, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_ALL_DEVICES) },
72-
{ BroadcastDestination.BROADCAST_RX_ON, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_RX_ON) },
73-
{ BroadcastDestination.BROADCAST_ROUTERS_AND_COORD, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_ROUTERS_AND_COORD) },
74-
{ BroadcastDestination.BROADCAST_LOW_POWER_ROUTERS, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_LOW_POWER_ROUTERS) },
75-
{ BroadcastDestination.BROADCAST_RESERVED_FFFE, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_RESERVED_FFFE) },
76-
{ BroadcastDestination.BROADCAST_RESERVED_FFFA, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_RESERVED_FFFA) },
77-
{ BroadcastDestination.BROADCAST_RESERVED_FFF9, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_RESERVED_FFF9) },
78-
{ BroadcastDestination.BROADCAST_RESERVED_FFF8, new ZigBeeBroadcastDestination(BroadcastDestination.BROADCAST_RESERVED_FFF8) },
79-
};
80-
}
81-
82-
/// <summary>
83-
/// Lookup function based on the EmberApsOption type code. Returns null
84-
/// if the code does not exist.
85-
/// </summary>
86-
public static ZigBeeBroadcastDestination GetBroadcastDestination(ushort key)
87-
{
88-
if (_codeMapping == null)
89-
{
90-
InitMapping();
91-
}
92-
93-
var i = (BroadcastDestination)key;
94-
95-
if (_codeMapping.ContainsKey(i))
96-
return _codeMapping[i];
97-
else
98-
return null;
99-
}
100-
101-
public static ZigBeeBroadcastDestination GetBroadcastDestination(BroadcastDestination dest)
102-
{
103-
return GetBroadcastDestination((ushort)dest);
104-
}
105-
106-
public static bool IsBroadcast(int address)
107-
{
108-
return (address >= (ushort)BroadcastDestination.BROADCAST_RESERVED_FFF8 && address <= (ushort)BroadcastDestination.BROADCAST_ALL_DEVICES);
109-
}
50+
public static bool IsBroadcast(this ZigBeeBroadcastDestination address) => IsBroadcast((ushort)address);
51+
public static bool IsBroadcast(ushort address) => (address >= (ushort)ZigBeeBroadcastDestination.BROADCAST_RESERVED_FFF8 && address <= (ushort)ZigBeeBroadcastDestination.BROADCAST_ALL_DEVICES);
11052
}
11153
}

libraries/ZigBeeNet/ZigBeeEndpointAddress.cs

+43-68
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66

77
namespace ZigBeeNet
88
{
9-
public class ZigBeeEndpointAddress : IZigBeeAddress
9+
public struct ZigBeeEndpointAddress : IZigBeeAddress,IComparable<ZigBeeEndpointAddress>
1010
{
11-
public byte Endpoint { get; private set; }
11+
public static ZigBeeEndpointAddress Zero {get;} = new ZigBeeEndpointAddress(0,0);
12+
public static ZigBeeEndpointAddress BROADCAST_RX_ON {get;} = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.BROADCAST_RX_ON);
13+
public static ZigBeeEndpointAddress BROADCAST_ALL_DEVICES {get;} = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.BROADCAST_ALL_DEVICES);
14+
public static ZigBeeEndpointAddress BROADCAST_LOW_POWER_ROUTERS {get;} = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.BROADCAST_LOW_POWER_ROUTERS);
15+
public static ZigBeeEndpointAddress BROADCAST_ROUTERS_AND_COORD {get;} = new ZigBeeEndpointAddress(ZigBeeBroadcastDestination.BROADCAST_ROUTERS_AND_COORD);
1216

13-
public ushort Address { get; set; }
17+
public ushort Address { get; }
18+
public byte Endpoint { get; }
1419

15-
public bool IsGroup
16-
{
17-
get { return false; }
18-
}
20+
public bool IsGroup => false;
1921

2022
/// <summary>
2123
/// Constructor for ZDO ZigBee devices where only the address is defined
@@ -26,8 +28,14 @@ public bool IsGroup
2628
/// </summary>
2729
public ZigBeeEndpointAddress(ushort address)
2830
{
29-
this.Address = address;
30-
this.Endpoint = 0;
31+
Address = address;
32+
Endpoint = 0;
33+
}
34+
35+
public ZigBeeEndpointAddress(ZigBeeBroadcastDestination address)
36+
{
37+
Address = (ushort)address;
38+
Endpoint = 0;
3139
}
3240

3341
/// <summary>
@@ -40,83 +48,50 @@ public ZigBeeEndpointAddress(ushort address)
4048
/// </summary>
4149
public ZigBeeEndpointAddress(ushort address, byte endpoint)
4250
{
43-
this.Address = address;
44-
this.Endpoint = endpoint;
51+
Address = address;
52+
Endpoint = endpoint;
4553
}
4654

4755
public ZigBeeEndpointAddress(string address)
4856
{
49-
if (address.Contains("/"))
50-
{
51-
var splits = address.Split('/');
52-
if (splits.Length > 2)
53-
{
54-
throw new ArgumentException(nameof(address));
55-
}
56-
this.Address = ushort.Parse(splits[0]);
57-
this.Endpoint = byte.Parse(splits[1]);
58-
}
57+
if (TryParse(address,out (ushort a,byte e) result))
58+
(Address,Endpoint)=result;
5959
else
60-
{
61-
this.Address = ushort.Parse(address);
62-
this.Endpoint = 0;
63-
}
60+
throw new ArgumentException(nameof(address));
6461
}
6562

66-
public override int GetHashCode()
63+
private static bool TryParse(string address,out (ushort addr,byte endpt) result )
6764
{
68-
byte[] hash = new byte[3];
69-
70-
hash[0] = Address.GetByte(0);
71-
hash[1] = Address.GetByte(1);
72-
hash[2] = Endpoint;
73-
74-
return Hash.CalcHashCode(hash);
75-
}
76-
77-
public override bool Equals(object obj)
78-
{
79-
if (obj == null)
80-
{
81-
return false;
82-
}
83-
84-
if (!typeof(ZigBeeEndpointAddress).IsAssignableFrom(obj.GetType()))
85-
{
65+
result=(0,0);
66+
if (string.IsNullOrWhiteSpace(address))
8667
return false;
87-
}
88-
89-
ZigBeeEndpointAddress other = (ZigBeeEndpointAddress)obj;
90-
91-
return (other.Address == Address && other.Endpoint == Endpoint);
68+
string[] splits = address.Split('/');
69+
return (splits.Length==1 && ushort.TryParse(address,out result.addr))
70+
|| (splits.Length==2 && ushort.TryParse(splits[0],out result.addr) && byte.TryParse(splits[1],out result.endpt));
9271
}
9372

94-
public int CompareTo(IZigBeeAddress that)
73+
public static bool TryParse(string address,out ZigBeeEndpointAddress result)
9574
{
96-
if (this == that)
75+
if (TryParse(address,out (ushort addr,byte endpt) r))
9776
{
98-
return 0;
77+
result=new ZigBeeEndpointAddress(r.addr,r.endpt);
78+
return true;
9979
}
100-
101-
ZigBeeEndpointAddress thatAddr = (ZigBeeEndpointAddress)that;
102-
103-
if (thatAddr.Address == Address && thatAddr.Endpoint == Endpoint)
104-
{
105-
return 0;
106-
}
107-
108-
if (thatAddr.Address == Address)
109-
{
110-
return Endpoint - thatAddr.Endpoint;
111-
}
112-
113-
return Address - thatAddr.Address;
80+
result=default;
81+
return false;
11482
}
11583

116-
public override string ToString()
84+
public override int GetHashCode() => Endpoint<<16 | Address;
85+
86+
public override bool Equals(object obj)
11787
{
118-
return Address + "/" + Endpoint;
88+
return !(obj is null)
89+
&& (obj is ZigBeeEndpointAddress objAddr)
90+
&& objAddr.Address == Address && objAddr.Endpoint == Endpoint;
11991
}
12092

93+
public int CompareTo(ZigBeeEndpointAddress other) => (Address == other.Address) ? (int)Endpoint - (int)other.Endpoint: (int)Address - (int)other.Address;
94+
95+
public override string ToString() =>Address + "/" + Endpoint;
12196
}
12297
}

0 commit comments

Comments
 (0)