/// <summary>
 /// The state to set as <see cref="EmberInitialSecurityState"/> </summary>
 public void SetState(EmberInitialSecurityState state)
 {
     _state = state;
 }
Ejemplo n.º 2
0
 public void SerializeEmberInitialSecurityState(EmberInitialSecurityState securityState)
 {
     securityState.Serialize(this);
 }
        /**
         * Sets the initial security state
         *
         * @param linkKey the initial {@link ZigBeeKey}
         * @param networkKey the initial {@link ZigBeeKey}
         * @return true if the security state was set successfully
         */
        private bool SetSecurityState(ZigBeeKey linkKey, ZigBeeKey networkKey)
        {
            EzspSetInitialSecurityStateRequest securityState = new EzspSetInitialSecurityStateRequest();
            EmberInitialSecurityState          state         = new EmberInitialSecurityState();

            state.AddBitmask(EmberInitialSecurityBitmask.EMBER_TRUST_CENTER_GLOBAL_LINK_KEY);

            EmberKeyData networkKeyData = new EmberKeyData();

            if (networkKey != null)
            {
                networkKeyData.SetContents(Array.ConvertAll(networkKey.Key, c => (int)c));
                state.AddBitmask(EmberInitialSecurityBitmask.EMBER_HAVE_NETWORK_KEY);
                if (networkKey.SequenceNumber.HasValue)
                {
                    state.SetNetworkKeySequenceNumber(networkKey.SequenceNumber.Value);
                }
            }
            state.SetNetworkKey(networkKeyData);

            EmberKeyData linkKeyData = new EmberKeyData();

            if (linkKey != null)
            {
                linkKeyData.SetContents(Array.ConvertAll(linkKey.Key, c => (int)c));
                state.AddBitmask(EmberInitialSecurityBitmask.EMBER_HAVE_PRECONFIGURED_KEY);
                state.AddBitmask(EmberInitialSecurityBitmask.EMBER_REQUIRE_ENCRYPTED_KEY);
            }
            state.SetPreconfiguredKey(linkKeyData);

            state.SetPreconfiguredTrustCenterEui64(new IeeeAddress());

            securityState.SetState(state);
            EzspSingleResponseTransaction transaction = new EzspSingleResponseTransaction(securityState, typeof(EzspSetInitialSecurityStateResponse));

            _protocolHandler.SendEzspTransaction(transaction);
            EzspSetInitialSecurityStateResponse securityStateResponse = (EzspSetInitialSecurityStateResponse)transaction.GetResponse();

            Log.Debug(securityStateResponse.ToString());
            if (securityStateResponse.GetStatus() != EmberStatus.EMBER_SUCCESS)
            {
                Log.Debug("Error during retrieval of network parameters: {Response}", securityStateResponse);
                return(false);
            }

            EmberNcp ncp = new EmberNcp(_protocolHandler);

            if (networkKey != null && networkKey.OutgoingFrameCounter.HasValue)
            {
                EzspSerializer serializer = new EzspSerializer();
                serializer.SerializeUInt32(networkKey.OutgoingFrameCounter.Value);
                if (ncp.SetValue(EzspValueId.EZSP_VALUE_NWK_FRAME_COUNTER, serializer.GetPayload()) != EzspStatus.EZSP_SUCCESS)
                {
                    return(false);
                }
            }
            if (linkKey != null && linkKey.OutgoingFrameCounter.HasValue)
            {
                EzspSerializer serializer = new EzspSerializer();
                serializer.SerializeUInt32(linkKey.OutgoingFrameCounter.Value);
                if (ncp.SetValue(EzspValueId.EZSP_VALUE_APS_FRAME_COUNTER, serializer.GetPayload()) != EzspStatus.EZSP_SUCCESS)
                {
                    return(false);
                }
            }

            return(true);
        }