Exemplo n.º 1
0
 public EzspGetNetworkParametersResponse(int[] inputBuffer) :
     base(inputBuffer)
 {
     _status     = deserializer.DeserializeEmberStatus();
     _nodeType   = deserializer.DeserializeEmberNodeType();
     _parameters = deserializer.DeserializeEmberNetworkParameters();
 }
        /**
         * Utility function to join an existing network as a Router
         *
         * @param networkParameters the required {@link EmberNetworkParameters}
         * @param linkKey the {@link ZigBeeKey} with the initial link key. This cannot be set to all 00 or all FF.
         */
        public void JoinNetwork(EmberNetworkParameters networkParameters, ZigBeeKey linkKey)
        {
            Log.Debug("Joining Ember network with configuration {Parameters}", networkParameters);

            // Leave the current network so we can initialise a new network
            EmberNcp ncp = new EmberNcp(_protocolHandler);

            if (CheckNetworkJoined())
            {
                ncp.LeaveNetwork();
            }

            ncp.ClearKeyTable();

            // Initialise security - no network key as we'll get that from the coordinator
            SetSecurityState(linkKey, null);

            DoJoinNetwork(networkParameters);
        }
        /**
         * Forms the ZigBee network as a coordinator
         *
         * @param networkParameters the {@link EmberNetworkParameters}
         * @return true if the network was formed successfully
         */
        private bool DoFormNetwork(EmberNetworkParameters networkParameters)
        {
            networkParameters.SetJoinMethod(EmberJoinMethod.EMBER_USE_MAC_ASSOCIATION);

            EzspFormNetworkRequest formNetwork = new EzspFormNetworkRequest();

            formNetwork.SetParameters(networkParameters);
            EzspSingleResponseTransaction transaction = new EzspSingleResponseTransaction(formNetwork, typeof(EzspFormNetworkResponse));

            _protocolHandler.SendEzspTransaction(transaction);
            EzspFormNetworkResponse formNetworkResponse = (EzspFormNetworkResponse)transaction.GetResponse();

            Log.Debug(formNetworkResponse.ToString());
            if (formNetworkResponse.GetStatus() != EmberStatus.EMBER_SUCCESS)
            {
                Log.Debug("Error forming network: {Response}", formNetworkResponse);
                return(false);
            }

            return(true);
        }
Exemplo n.º 4
0
 public void SerializeEmberNetworkParameters(EmberNetworkParameters networkParameters)
 {
     networkParameters.Serialize(this);
 }
 /// <summary>
 /// The parameters to set as <see cref="EmberNetworkParameters"/> </summary>
 public void SetParameters(EmberNetworkParameters parameters)
 {
     _parameters = parameters;
 }
        /**
         * This utility function uses emberStartScan, emberStopScan, emberScanCompleteHandler, emberEnergyScanResultHandler,
         * and emberNetworkFoundHandler to discover other networks or determine the background noise level. It then uses
         * emberFormNetwork to create a new network with a unique PAN-ID on a channel with low background noise.
         * <p>
         * Setting the PAN-ID or Extended PAN-ID to 0 will set these values to a random value.
         * <p>
         * If channel is set to 0, the quietest channel will be used.
         *
         * @param networkParameters the required {@link EmberNetworkParameters}
         * @param linkKey the {@link ZigBeeKey} with the link key. This can not be set to all 00 or all FF.
         * @param networkKey the {@link ZigBeeKey} with the network key. This can not be set to all 00 or all FF.
         */
        public void FormNetwork(EmberNetworkParameters networkParameters, ZigBeeKey linkKey, ZigBeeKey networkKey)
        {
            if (networkParameters.GetExtendedPanId() == null)
            {
                networkParameters.SetExtendedPanId(new ExtendedPanId());
            }

            Log.Debug("Initialising Ember network with configuration {NetworkParameters}", networkParameters);

            EmberNcp ncp = new EmberNcp(_protocolHandler);

            // Leave the current network so we can initialise a new network
            if (CheckNetworkJoined())
            {
                ncp.LeaveNetwork();
            }

            ncp.ClearKeyTable();

            // Perform an energy scan to find a clear channel
            int?quietestChannel = DoEnergyScan(ncp, _scanDuration);

            Log.Debug("Energy scan reports quietest channel is {QuietestChannel}", quietestChannel);

            // Check if any current networks were found and avoid those channels, PAN ID and especially Extended PAN ID
            ncp.DoActiveScan(ZigBeeChannelMask.CHANNEL_MASK_2GHZ, _scanDuration);

            // Read the current network parameters
            GetNetworkParameters();

            // Create a random PAN ID and Extended PAN ID
            if (networkParameters.GetPanId() == 0 || networkParameters.GetExtendedPanId().Equals(new ExtendedPanId()))
            {
                Random random = new Random();
                int    panId  = random.Next(65535);
                networkParameters.SetPanId(panId);
                Log.Debug("Created random PAN ID: {PanId}", panId);

                byte[] extendedPanIdBytes = new byte[8];
                random.NextBytes(extendedPanIdBytes);
                ExtendedPanId extendedPanId = new ExtendedPanId(extendedPanIdBytes);
                networkParameters.SetExtendedPanId(extendedPanId);
                Log.Debug("Created random Extended PAN ID: {ExtendedPanId}", extendedPanId.ToString());
            }

            if (networkParameters.GetRadioChannel() == 0 && quietestChannel.HasValue)
            {
                networkParameters.SetRadioChannel(quietestChannel.Value);
            }

            // If the channel set is empty, use the single channel defined above
            if (networkParameters.GetChannels() == 0)
            {
                networkParameters.SetChannels(1 << networkParameters.GetRadioChannel());
            }

            // Initialise security
            SetSecurityState(linkKey, networkKey);

            // And now form the network
            DoFormNetwork(networkParameters);
        }