Beispiel #1
0
        /**
         * This function updates an existing entry in the key table or adds a new one. It first searches the table for an
         * existing entry that matches the passed EUI64 address. If no entry is found, it searches for the first free entry.
         * If successful, it updates the key data and resets the associated incoming frame counter. If it fails to find an
         * existing entry and no free one exists, it returns a failure.
         *
         * @param address the {@link IeeeAddress}
         * @param key the {@link ZigBeeKey}
         * @param linkKey This indicates whether the key is a Link or a Master Key
         * @return the returned {@link EmberStatus} of the request
         */
        public EmberStatus AddOrUpdateKeyTableEntry(IeeeAddress address, ZigBeeKey key, bool linkKey)
        {
            EmberKeyData keyData = new EmberKeyData();

            keyData.SetContents(Array.ConvertAll(key.Key, c => (int)c));

            EzspAddOrUpdateKeyTableEntryRequest request = new EzspAddOrUpdateKeyTableEntryRequest();

            request.SetAddress(address);
            request.SetKeyData(keyData);
            request.SetLinkKey(linkKey);
            IEzspTransaction transaction = _protocolHandler.SendEzspTransaction(new EzspSingleResponseTransaction(request, typeof(EzspAddOrUpdateKeyTableEntryResponse)));
            EzspAddOrUpdateKeyTableEntryResponse response = (EzspAddOrUpdateKeyTableEntryResponse)transaction.GetResponse();

            return(response.GetStatus());
        }
Beispiel #2
0
        /**
         * Adds a transient link key to the NCP
         *
         * @param partner the {@link IeeeAddress} to set
         * @param transientKey the {@link ZigBeeKey} to set
         * @return the {@link EmberStatus} of the response
         */
        public EmberStatus AddTransientLinkKey(IeeeAddress partner, ZigBeeKey transientKey)
        {
            EmberKeyData emberKey = new EmberKeyData();

            emberKey.SetContents(Array.ConvertAll(transientKey.Key, c => (int)c));
            EzspAddTransientLinkKeyRequest request = new EzspAddTransientLinkKeyRequest();

            request.SetPartner(partner);
            request.SetTransientKey(emberKey);
            IEzspTransaction transaction             = _protocolHandler.SendEzspTransaction(new EzspSingleResponseTransaction(request, typeof(EzspAddTransientLinkKeyResponse)));
            EzspAddTransientLinkKeyResponse response = (EzspAddTransientLinkKeyResponse)transaction.GetResponse();

            _lastStatus = response.GetStatus();
            Log.Debug(response.ToString());
            if (response.GetStatus() != EmberStatus.EMBER_SUCCESS)
            {
                Log.Debug("Error setting transient key: {}", response);
            }

            return(response.GetStatus());
        }
        /**
         * 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);
        }