Skip to content

Commit

Permalink
started work on issue #46
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Markus committed Jun 3, 2019
1 parent a17eb3a commit e005f2f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 64 deletions.
3 changes: 2 additions & 1 deletion libraries/ZigBeeNet.Hardware.Digi.XBee/ZigBeeDongleXBee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using ZigBeeNet.Hardware.Digi.XBee.Internal.Protocol;
using ZigBeeNet.Security;
using ZigBeeNet.Transport;
using System.Threading.Tasks;

namespace ZigBeeNet.Hardware.Digi.XBee
{
Expand Down Expand Up @@ -199,7 +200,7 @@ public ZigBeeStatus Initialize()
return ZigBeeStatus.SUCCESS;
}

public ZigBeeStatus Startup(bool reinitialize)
public async Task<ZigBeeStatus> Startup(bool reinitialize)
{
Log.Debug("XBee dongle startup.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<PackageReference Include="Serilog" Version="2.8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZigBeeNet\ZigBeeNet.csproj" />
<ProjectReference Include="..\ZigBeeNet\ZigBeeNet.csproj">
<Private>
</Private>
</ProjectReference>
</ItemGroup>
</Project>
34 changes: 28 additions & 6 deletions libraries/ZigBeeNet.Hardware.TI.CC2531/Network/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace ZigBeeNet.Hardware.TI.CC2531.Network
{
public class NetworkManager
public class NetworkManager : IAsynchronousCommandListener
{
private const int DEFAULT_TIMEOUT = 8000;
private const string TIMEOUT_KEY = "zigbee.driver.cc2531.timeout";
Expand Down Expand Up @@ -77,15 +77,15 @@ public class NetworkManager

private static ManualResetEventSlim _hardwareSync = new ManualResetEventSlim(false);

public event EventHandler<DriverStatus> OnStateChanged;

private byte[] _ep;
private byte[] _prof;
private byte[] _dev;
private byte[] _ver;
private ushort[][] _inp;
private ushort[][] _out;

private NetworkStateListener _announceListenerFilter = new NetworkStateListener();

private List<IApplicationFrameworkMessageListener> _messageListeners = new List<IApplicationFrameworkMessageListener>();
private AFMessageListenerFilter _afMessageListenerFilter;

Expand All @@ -97,9 +97,8 @@ public class NetworkManager
private object _hardwareWaitSync = new object();
private object _networkSync = new object();

public NetworkManager(ICommandInterface commandInterface, NetworkMode mode, long timeout)
public NetworkManager(ICommandInterface commandInterface, NetworkMode mode)
{
_announceListenerFilter.OnStateChanged += (object sender, DriverStatus status) => SetState(status);
_afMessageListenerFilter = new AFMessageListenerFilter(_messageListeners);

_mode = mode;
Expand All @@ -113,6 +112,29 @@ public NetworkManager(ICommandInterface commandInterface, NetworkMode mode, long
_state = DriverStatus.CLOSED;
}

public void ReceivedAsynchronousCommand(ZToolPacket packet)
{
if (packet is ZDO_STATE_CHANGE_IND stateInd)
{
switch (stateInd.Status)
{
case DeviceState.Started_as_ZigBee_Coordinator:
Log.Debug("Started as Zigbee Coordinator");
SetState(DriverStatus.NETWORK_READY);
OnStateChanged?.Invoke(this, DriverStatus.NETWORK_READY);
break;
default:
break;
}
}
}

public void ReceivedUnclaimedSynchronousCommandResponse(ZToolPacket packet)
{
// Processing not requiered
throw new NotImplementedException(); // TODO: Realy throwing an exception ?
}

/// <summary>
/// Different hardware may use a different "Magic Number" to skip waiting in the bootloader. Otherwise
/// the dongle may wait in the bootloader for 60 seconds after it's powered on or reset.
Expand Down Expand Up @@ -424,7 +446,7 @@ private void PostHardwareEnabled()
_commandInterface.AddAsynchronousCommandListener(_afMessageListenerFilter);
//}
// if (!announceListeners.contains(announceListenerFilter)) {
_commandInterface.AddAsynchronousCommandListener(_announceListenerFilter);
_commandInterface.AddAsynchronousCommandListener(this);
// }
}

Expand Down

This file was deleted.

64 changes: 48 additions & 16 deletions libraries/ZigBeeNet.Hardware.TI.CC2531/ZigBeeDongleTiCc2531.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using ZigBeeNet.Transport;
using ZigBeeNet.ZCL;
using static ZigBeeNet.ZigBeeNetworkManager;
using System.Threading.Tasks;

namespace ZigBeeNet.Hardware.TI.CC2531
{
Expand All @@ -27,6 +28,8 @@ public class ZigBeeDongleTiCc2531 : IZigBeeTransportTransmit, IApplicationFramew
private List<ushort> _supportedInputClusters = new List<ushort>();
private List<ushort> _supportedOutputClusters = new List<ushort>();

private TaskCompletionSource<ZigBeeStatus> _startupTask;

public string VersionString { get; set; }

public IeeeAddress IeeeAddress { get; set; }
Expand Down Expand Up @@ -76,7 +79,18 @@ public ZigBeeKey TcLinkKey

public ZigBeeDongleTiCc2531(IZigBeePort serialPort)
{
_networkManager = new NetworkManager(new CommandInterfaceImpl(serialPort), NetworkMode.Coordinator, 2500);
_networkManager = new NetworkManager(new CommandInterfaceImpl(serialPort), NetworkMode.Coordinator);
_networkManager.OnStateChanged += NetworkManager_OnStateChanged;
}

private void NetworkManager_OnStateChanged(object sender, DriverStatus e)
{
if (e == DriverStatus.NETWORK_READY)
{
CreateEndpoint(1, 0x104);

_startupTask?.SetResult(ZigBeeStatus.SUCCESS);
}
}

public void SetMagicNumber(byte magicNumber)
Expand Down Expand Up @@ -216,7 +230,7 @@ public void Shutdown()
_networkManager.Shutdown();
}

public ZigBeeStatus Startup(bool reinitialize)
public async Task<ZigBeeStatus> Startup(bool reinitialize)
{
Log.Debug("CC2531 transport startup");

Expand All @@ -229,25 +243,43 @@ public ZigBeeStatus Startup(bool reinitialize)
return ZigBeeStatus.INVALID_STATE;
}

// TODO: ugly ugly ugly
// See: https://github.com/zigbeenet/ZigbeeNet/issues/46
while (true)
_startupTask = new TaskCompletionSource<ZigBeeStatus>();

var t = _startupTask.Task;
var timeoutTask = Task.Delay(10000);

if (t == await Task.WhenAny(t, timeoutTask).ConfigureAwait(false))
{
if (_networkManager.GetDriverStatus() == DriverStatus.NETWORK_READY)
{
break;
}
if (_networkManager.GetDriverStatus() == DriverStatus.CLOSED)
{
return ZigBeeStatus.BAD_RESPONSE;
}
return ZigBeeStatus.SUCCESS;
}
else
{
/* Timeout */
Log.Debug("Startup timeout");

Thread.Sleep(50);
return ZigBeeStatus.BAD_RESPONSE;
}

CreateEndpoint(1, 0x104);

return ZigBeeStatus.SUCCESS;
//// TODO: ugly ugly ugly
//// See: https://github.com/zigbeenet/ZigbeeNet/issues/46
//while (true)
//{
// if (_networkManager.GetDriverStatus() == DriverStatus.NETWORK_READY)
// {
// break;
// }
// if (_networkManager.GetDriverStatus() == DriverStatus.CLOSED)
// {
// return ZigBeeStatus.BAD_RESPONSE;
// }

// Thread.Sleep(50);
//}

//CreateEndpoint(1, 0x104);

//return ZigBeeStatus.SUCCESS;
}

private byte GetSendingEndpoint(ushort profileId)
Expand Down
3 changes: 2 additions & 1 deletion libraries/ZigBeeNet/Transport/IZigBeeTransportTransmit.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBeeNet.Security;
using ZigBeeNet.ZCL;

Expand Down Expand Up @@ -28,7 +29,7 @@ public interface IZigBeeTransportTransmit
/// </summary>
/// <param name="reinitialize"></param>
/// <returns>true if startup was success</returns>
ZigBeeStatus Startup(bool reinitialize);
Task<ZigBeeStatus> Startup(bool reinitialize);

/// <summary>
/// Shuts down a transport interface.
Expand Down
4 changes: 2 additions & 2 deletions libraries/ZigBeeNet/ZigBeeNetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public ZigBeeStatus SetZigBeeInstallKey(ZigBeeKey key)
///
/// <returns><see cref="ZigBeeStatus"> with the status of function</returns>
/// </summary>
public ZigBeeStatus Startup(bool reinitialize)
public async Task<ZigBeeStatus> Startup(bool reinitialize)
{
lock (_networkStateSync)
{
Expand All @@ -480,7 +480,7 @@ public ZigBeeStatus Startup(bool reinitialize)
}
}

ZigBeeStatus status = Transport.Startup(reinitialize);
ZigBeeStatus status = await Transport.Startup(reinitialize);

if (status != ZigBeeStatus.SUCCESS)
{
Expand Down
2 changes: 1 addition & 1 deletion samples/ZigBeeNet.PlayGround/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static async Task Main(string[] args)
((ZigBeeDongleTiCc2531)dongle).SetLedMode(2, false); // red led
}

ZigBeeStatus startupSucceded = networkManager.Startup(false);
ZigBeeStatus startupSucceded = await networkManager.Startup(false);

if (startupSucceded == ZigBeeStatus.SUCCESS)
{
Expand Down

0 comments on commit e005f2f

Please sign in to comment.