Esempio n. 1
0
 private void RxReset()
 {
     _nodeIdWaitResponded = 0;
     _srcNodeId           = 0;
     _reassemblingData    = null;
     _rxTimerAction       = null;
 }
Esempio n. 2
0
        public NodeInfoTask(NetworkViewPoint network, byte nodeId)
        {
            _network = network;

            _nodeInfo = new RequestNodeInfoOperation(nodeId);
            _nodeInfoSecondAttempt = new RequestNodeInfoOperation(nodeId);

            _requestEndPoints = new RequestDataOperation(0, nodeId, new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_END_POINT_GET(), _txOptions,
                                                         new COMMAND_CLASS_MULTI_CHANNEL_V4.MULTI_CHANNEL_END_POINT_REPORT(), 2, 5000);

            _requestEndPointsCapabilities = new ActionSerialGroup();

            SpecificResult.RequestNodeInfo = _nodeInfo.SpecificResult;

            Actions = new ActionBase[]
            {
                _nodeInfo,
                _nodeInfoSecondAttempt,
                _requestEndPoints,
                _requestEndPointsCapabilities,
            };
        }
Esempio n. 3
0
        private ActionSerialGroup CreateRxTimerAction()
        {
            var ret = new ActionSerialGroup(
                OnFragmentRxTimeout,
                new ExpectDataOperation(0, 0, new byte[] { COMMAND_CLASS_TRANSPORT_SERVICE_V2.ID, 0xFF }, 2,
                                        FRAGMENT_RX_TIMEOUT)
            {
                Name = "First Rx Timeout"
            },
                new SendDataOperation(_srcNodeId,
                                      null,
                                      TransportServiceManagerInfo.TxOptions)
            {
                SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
            },
                new ExpectDataOperation(0, 0, new byte[] { COMMAND_CLASS_TRANSPORT_SERVICE_V2.ID, 0xFF }, 2,
                                        FRAGMENT_RX_TIMEOUT)
            {
                Name = "Second Rx Timeout"
            });

            ret.CompletedCallback = OnRxTimerActionCompleted;
            return(ret);
        }
Esempio n. 4
0
        public InclusionTask(NetworkViewPoint network, IAddRemoveNode addRemoveNode, bool IsSmartStart)
            : base(false, null)
        {
            _network      = network;
            _isSmartStart = IsSmartStart;
            if (addRemoveNode is AddNodeOperation)
            {
                _mode = (addRemoveNode as AddNodeOperation).InitMode;
                _nodeStatusCallback = (addRemoveNode as AddNodeOperation).NodeStatusCallback;
                _timeoutMs          = (addRemoveNode as AddNodeOperation).TimeoutMs;
            }
            else
            {
                _mode      = Modes.NodeAny;
                _timeoutMs = TIMEOUT;
            }

            _peerFilter = new FilterAchOperation();
            _peerFilter.SetFilterNodeId(0xFF);
            _addNode = addRemoveNode;
            _addNode.NodeStatusCallback = OnNodeStatus;

            _memoryGetId = new MemoryGetIdOperation();

            _getSucNodeId             = new GetSucNodeIdOperation();
            _isFailedSucNodeOperation = new IsFailedNodeOperation(0);
            _getSucNodeInfo           = new RequestNodeInfoOperation(0);
            _requestSucNodeInfoGroup  = new ActionSerialGroup(OnSucNodeInfoCompletedGroup, _getSucNodeId, _isFailedSucNodeOperation, _getSucNodeInfo)
            {
                Name = "RequestSucNodeInfoGroup (InclusionController)",
                CompletedCallback = OnRequestSucNodeInfoGroupCompleted
            };

            _setupNodeLifelineTask = new SetupNodeLifelineTask(_network);

            _requestInclusionController = new InclusionController.Initiate(0, 0, _txOptions, 240000)
            {
                Name = "ReguestData (InclusionController)"
            };
            _serialApiGetInitData = new SerialApiGetInitDataOperation();

            SpecificResult.AddRemoveNode     = _addNode.SpecificResult;
            SpecificResult.MemoryGetId       = _memoryGetId.SpecificResult;
            SpecificResult.GetSucNodeId      = _getSucNodeId.SpecificResult;
            SpecificResult.NodeInfo          = _setupNodeLifelineTask.SpecificResult.NodeInfo;
            SpecificResult.SetWakeUpInterval = _setupNodeLifelineTask.SpecificResult.SetWakeUpInterval;

            _isFailedNodeOperation       = new IsFailedNodeOperation(_addNode.SpecificResult.Id);
            _removeFailedNodeIdOperation = new RemoveFailedNodeIdOperation(_addNode.SpecificResult.Id);
            if (!_isSmartStart)
            {
                _isFailedNodeOperation.Token.SetCancelled();
                _removeFailedNodeIdOperation.Token.SetCancelled();
            }

            Actions = new ActionBase[]
            {
                _peerFilter,
                new ActionSerialGroup(OnActionCompleted,
                                      _memoryGetId,
                                      _requestSucNodeInfoGroup,
                                      _serialApiGetInitData,
                                      (ActionBase)_addNode,
                                      _isFailedNodeOperation,
                                      _removeFailedNodeIdOperation,
                                      _requestInclusionController,
                                      _setupNodeLifelineTask
                                      )
                {
                    Name = "Inclusion (Group)"
                }
            };
        }
Esempio n. 5
0
        protected override void OnHandledInternal(DataReceivedUnit ou)
        {
            byte nodeId = ReceivedAchData.SrcNodeId;

            byte[] command = ReceivedAchData.Command;
            if (command != null && command.Length > 1)
            {
                if (command[1] == COMMAND_CLASS_INCLUSION_CONTROLLER.INITIATE.ID && nodeIdToInclude == 0)
                {
                    var initiateCommand = (COMMAND_CLASS_INCLUSION_CONTROLLER.INITIATE)command;

                    if (initiateCommand.stepId == PROXY_INCLUSION || initiateCommand.stepId == PROXY_INCLUSION_REPLACE)
                    {
                        /*TO# 07220 - Delay before request Node Info*/
                        Thread.Sleep(2000);

                        //if we're not SIS then ignore
                        if (_securityManagerInfo.Network.SucNodeId == _securityManagerInfo.Network.NodeId)
                        {
                            //Start add node for example
                            nodeIdToInclude = initiateCommand.nodeId;

                            var isVirtualNodeOperation = new IsVirtualNodeOperation(nodeIdToInclude);
                            if (ReceivedAchData.CommandType != CommandTypes.CmdApplicationCommandHandler_Bridge)
                            {
                                isVirtualNodeOperation.Token.SetCancelled();
                            }
                            var stepId           = initiateCommand.stepId;
                            var sendDataComplete = new InclusionController.Complete(nodeId, TxOptions);
                            sendDataComplete.SetCommandParameters(STEP_OK, stepId);
                            var requestDataStep2 = new InclusionController.Initiate(0, nodeId, TxOptions, 20000);
                            requestDataStep2.SetCommandParameters(nodeIdToInclude, S0_INCLUSION);
                            var sendDataRejectComplete = new InclusionController.Complete(nodeId, TxOptions);
                            sendDataRejectComplete.SetCommandParameters(STEP_USER_REJECTED, stepId);

                            var addNodeOperation = new AddNodeS2Operation(_securityManagerInfo);
                            addNodeOperation.SetInclusionControllerInitiateParameters(nodeIdToInclude);

                            var setupNodeLifelineTask = new SetupNodeLifelineTask(_securityManagerInfo.Network);
                            setupNodeLifelineTask.NodeId            = _securityManagerInfo.Network.NodeId;
                            setupNodeLifelineTask.SucNodeId         = _securityManagerInfo.Network.NodeId;
                            setupNodeLifelineTask.TargetNodeId      = nodeIdToInclude;
                            setupNodeLifelineTask.CompletedCallback = OnNodeInfoCompleted;

                            var serialGroup = new ActionSerialGroup(OnInitiateFlowActionCompleted, isVirtualNodeOperation,
                                                                    new RequestNodeInfoOperation(nodeIdToInclude)
                            {
                                SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                            },
                                                                    addNodeOperation, requestDataStep2, setupNodeLifelineTask, sendDataComplete);
                            serialGroup.ActionUnitStop = new ActionUnit(sendDataRejectComplete);
                            //hack
                            serialGroup.Token.Result      = new AddRemoveNodeResult();
                            serialGroup.CompletedCallback = OnS2SerialGroupCompleted;

                            if (_inclusionControllerStatusUpdateCallback != null)
                            {
                                _inclusionControllerStatusUpdateCallback(serialGroup.Token, false);
                            }
                            ou.SetNextActionItems(serialGroup);
                        }
                    }
                    else if (initiateCommand.stepId == S0_INCLUSION)
                    {
                        //only if asked from SIS
                        if (_securityManagerInfo.Network.SucNodeId == nodeId)
                        {
                            InclusionController.Complete sendDataComplete = null;
                            //Start S0
                            nodeIdToInclude = initiateCommand.nodeId;

                            if (_securityManagerInfo.Network.HasSecurityScheme(SecuritySchemes.S0))
                            {
                                sendDataComplete = new InclusionController.Complete(nodeId, TxOptions);
                                sendDataComplete.SetCommandParameters(STEP_OK, S0_INCLUSION);

                                var addNodeOperation = new AddNodeS0Operation(_securityManagerInfo);
                                addNodeOperation.SetInclusionControllerInitiateParameters(nodeIdToInclude);

                                var nodeInfoOperation = new RequestNodeInfoOperation(nodeIdToInclude);
                                nodeInfoOperation.CompletedCallback = OnNodeInfoCompleted;

                                var serialGroup = new ActionSerialGroup(new RequestNodeInfoOperation(nodeIdToInclude)
                                {
                                    SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                                },
                                                                        addNodeOperation, nodeInfoOperation, sendDataComplete);

                                //hack
                                serialGroup.Token.Result = new AddRemoveNodeResult();

                                ou.SetNextActionItems(serialGroup);
                            }
                            else
                            {
                                sendDataComplete = new InclusionController.Complete(nodeId, TxOptions);
                                sendDataComplete.SetCommandParameters(STEP_NOT_SUPPORTED, S0_INCLUSION);
                                ou.SetNextActionItems(sendDataComplete);
                            }
                        }
                    }
                }
                else if (command[1] == COMMAND_CLASS_INCLUSION_CONTROLLER.COMPLETE.ID)
                {
                }
            }
        }
Esempio n. 6
0
        protected override void OnHandledInternal(DataReceivedUnit ou)
        {
            byte nodeId = ReceivedAchData.SrcNodeId;

            byte[] cmd = ReceivedAchData.Command;
            if (handlingRequestFromNode != nodeId || !handlingRequest.SequenceEqual(cmd))
            {
                handlingRequestFromNode = nodeId;
                handlingRequest         = cmd;
                if (ReceiveCallback != null)
                {
                    byte[] data = ReceiveCallback(ReceivedAchData.Options, ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, ReceivedAchData.Command);
                    if (data != null && data.Length > 0)
                    {
                        Data = new List <byte[]>();
                        Data.Add(data);
                    }
                    else
                    {
                        Data = null;
                    }
                }
                else if (ReceiveExCallback != null)
                {
                    Data = ReceiveExCallback(ReceivedAchData.Options, ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, ReceivedAchData.Command);
                }
                ou.SetNextActionItems();
                List <ActionBase> nextOperations = new List <ActionBase>();
                if (Data != null)
                {
                    var scheme = IsSecuritySchemeSpecified ? SecurityScheme : (SecuritySchemes)ReceivedAchData.SecurityScheme;
                    foreach (var command in Data)
                    {
                        bool isSuportedScheme = IsSupportedScheme(_network, command, scheme);
                        if (command != null && command.Length > 1 && isSuportedScheme)
                        {
                            CallbackApiOperation operation = null;
                            operation = new SendDataExOperation(ReceivedAchData.DestNodeId, ReceivedAchData.SrcNodeId, command, TxOptions, TxSecOptions, scheme, TxOptions2);
                            operation.SubstituteSettings = new SubstituteSettings(SubstituteSettings.SubstituteFlags, SubstituteSettings.MaxBytesPerFrameSize);
                            if (ReceivedAchData.SubstituteIncomingFlags.HasFlag(SubstituteIncomingFlags.Crc16Encap))
                            {
                                operation.SubstituteSettings.SetFlag(SubstituteFlags.UseCrc16Encap);
                            }
                            nextOperations.Add(operation);
                        }
                    }
                }
                if (nextOperations.Count > 0)
                {
                    var next = new ActionSerialGroup(nextOperations.ToArray());
                    next.CompletedCallback = (x) =>
                    {
                        var action = x as ActionBase;
                        if (action != null)
                        {
                            handlingRequestFromNode = 0;
                            handlingRequest         = emptyArray;
                            SpecificResult.TotalCount++;
                            if (action.Result.State != ActionStates.Completed)
                            {
                                SpecificResult.FailCount++;
                            }
                        }
                    };
                    ou.SetNextActionItems(next);
                }
                else
                {
                    handlingRequestFromNode = 0;
                    handlingRequest         = emptyArray;
                }
            }
        }
Esempio n. 7
0
        protected override CustomDataFrame SubstituteIncomingInternal(CustomDataFrame packet, byte destNodeId, byte srcNodeId, byte[] cmdData, int lenIndex, out ActionBase additionalAction, out ActionBase completeAction)
        {
            CustomDataFrame ret = packet;

            additionalAction = null;
            completeAction   = null;
            if (IsActive)
            {
                if (cmdData.Length > 1 && cmdData[0] == COMMAND_CLASS_TRANSPORT_SERVICE_V2.ID && ValidateCRC16Checksum(cmdData))
                {
                    byte id = (byte)(cmdData[1] & COMMAND_CLASS_TRANSPORT_SERVICE_V2.COMMAND_FIRST_SEGMENT.ID_MASK);

                    if (id == (COMMAND_CLASS_TRANSPORT_SERVICE_V2.COMMAND_FIRST_SEGMENT.ID) &&
                        TransportServiceManagerInfo.TestNeedToIgnoreFirstSegment.CanBeUsed &&
                        TransportServiceManagerInfo.TestNeedToIgnoreFirstSegment.PullValue().Value
                        )
                    {
                        return(ret);
                    }

                    if (id == (COMMAND_CLASS_TRANSPORT_SERVICE_V2.COMMAND_SUBSEQUENT_SEGMENT.ID) &&
                        TransportServiceManagerInfo.TestNeedToIgnoreSubsequentSegment.CanBeUsed &&
                        TransportServiceManagerInfo.TestOffset.CanBeUsed
                        )
                    {
                        var datagramOffset = SegmentsContainer.GetSegmentOffset(cmdData);
                        var payloadLength  = SegmentsContainer.GetSegmentPayloadLength(cmdData);
                        if (TransportServiceManagerInfo.TestOffset.PullValue(
                                val =>
                        {
                            return(val >= datagramOffset && val <= (datagramOffset + payloadLength));
                        }) != null &&
                            TransportServiceManagerInfo.TestNeedToIgnoreSubsequentSegment.PullValue().Value
                            )
                        {
                            return(ret);
                        }
                    }

                    lock (_lockObject)
                    {
                        if (_reassemblingData == null || _reassemblingData.SegmentsContainer.IsCompleted)
                        {
                            if (id == (COMMAND_CLASS_TRANSPORT_SERVICE_V2.COMMAND_FIRST_SEGMENT.ID))
                            {
                                RxReset();
                                _srcNodeId = srcNodeId;

                                var cmdHeader = new byte[lenIndex];
                                Array.Copy(packet.Data, 0, cmdHeader, 0, cmdHeader.Length);

                                var cmdFooter = new byte[packet.Data.Length - cmdHeader.Length - cmdData.Length - 1];
                                Array.Copy(packet.Data, lenIndex + packet.Data[lenIndex] + 1, cmdFooter, 0, cmdFooter.Length);

                                _reassemblingData = new ReassemblingData
                                {
                                    SegmentsContainer  = new SegmentsContainer(cmdData),
                                    CompletedCmdHeader = cmdHeader,
                                    CompletedCmdFooter = cmdFooter
                                };

                                if (_reassemblingData.SegmentsContainer.IsCompleted)
                                {
                                    additionalAction = new SendDataOperation(srcNodeId, CreateSegmentCompletedCmd(), TransportServiceManagerInfo.TxOptions)
                                    {
                                        SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                                    };
                                    ret = CreateDataFrameOnReassemblingCompleted(packet);
                                }
                                else
                                {
                                    // Start fragment rx timer.
                                    _rxTimerAction   = CreateRxTimerAction();
                                    additionalAction = _rxTimerAction;
                                }
                            }
                            else if (id == (COMMAND_CLASS_TRANSPORT_SERVICE_V2.COMMAND_SUBSEQUENT_SEGMENT.ID))
                            {
                                if (_reassemblingData != null && _reassemblingData.SegmentsContainer.CheckForLastSegment(cmdData))
                                {
                                    additionalAction = new SendDataOperation(srcNodeId, CreateSegmentCompletedCmd(), TransportServiceManagerInfo.TxOptions)
                                    {
                                        SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                                    };
                                }
                                else if (srcNodeId != _nodeIdWaitResponded)
                                {
                                    _nodeIdWaitResponded = srcNodeId;
                                    additionalAction     = new SendDataOperation(srcNodeId, CreateSegmentWaitCmd(1), TransportServiceManagerInfo.TxOptions)
                                    {
                                        SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                                    };
                                }
                            }
                        }
                        else if (_srcNodeId == srcNodeId && id == (COMMAND_CLASS_TRANSPORT_SERVICE_V2.COMMAND_SUBSEQUENT_SEGMENT.ID))
                        {
                            if (_rxTimerAction != null)
                            {
                                _rxTimerAction.Actions[0].Token.Reset(FRAGMENT_RX_TIMEOUT); // Reset fragment rx timer.
                            }
                            else
                            {
                                _rxTimerAction   = CreateRxTimerAction();
                                additionalAction = _rxTimerAction;
                            }

                            _reassemblingData.SegmentsContainer.AddSegment(cmdData);
                            if (_reassemblingData.SegmentsContainer.IsLastSegmentReceived)
                            {
                                if (_reassemblingData.SegmentsContainer.IsCompleted)
                                {
                                    _rxTimerAction.Token.SetCompleted(); // Complete fragment rx timer.
                                    completeAction = _rxTimerAction;
                                    _rxTimerAction = null;

                                    additionalAction = new SendDataOperation(srcNodeId, CreateSegmentCompletedCmd(), TransportServiceManagerInfo.TxOptions)
                                    {
                                        SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                                    };
                                    ret = CreateDataFrameOnReassemblingCompleted(packet);
                                }
                                else
                                {
                                    ushort missingOffset = _reassemblingData.SegmentsContainer.GetFirstMissingFragmentOffset();
                                    additionalAction = new SendDataOperation(srcNodeId,
                                                                             CreateSegmentRequestCmd(missingOffset),
                                                                             TransportServiceManagerInfo.TxOptions)
                                    {
                                        SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                                    };
                                }
                            }
                        }
                        else
                        {
                            if (srcNodeId != _nodeIdWaitResponded)
                            {
                                _nodeIdWaitResponded = srcNodeId;
                                additionalAction     = new SendDataOperation(srcNodeId,
                                                                             CreateSegmentWaitCmd(_reassemblingData.SegmentsContainer.PendingSegmentsCount),
                                                                             TransportServiceManagerInfo.TxOptions)
                                {
                                    SubstituteSettings = new SubstituteSettings(SubstituteFlags.DenySecurity, 0)
                                };
                            }
                        }
                    }
                }
            }
            return(ret);
        }
Esempio n. 8
0
        private void OnAddNodeCompleted(StartActionUnit tu)
        {
            SecurityManagerInfo.IsInclusion = true;
            if ((ParentAction as ActionSerialGroup) != null)
            {
                ActionSerialGroup actionGroup = (ActionSerialGroup)ParentAction;
                if (_initiateNodeId > 0)
                {
                    OnAddNodeCompletedInternal(tu);
                }
                else
                {
                    ActionResult agRes = ParentAction.Result;
                    AddTraceLogItems(agRes.InnerResults[0].TraceLog);
                    if (agRes.InnerResults[0].State == ActionStates.Completed)
                    {
                        AddRemoveNodeResult arnRes = agRes.InnerResults[0] as AddRemoveNodeResult;
                        SpecificResult.Id                  = arnRes.Id;
                        SpecificResult.Basic               = arnRes.Basic;
                        SpecificResult.Generic             = arnRes.Generic;
                        SpecificResult.Specific            = arnRes.Specific;
                        SpecificResult.AddRemoveNodeStatus = arnRes.AddRemoveNodeStatus;
                        if (arnRes.CommandClasses == null)
                        {
                            var rniRes = agRes.FindInnerResults <RequestNodeInfoResult>();
                            if (rniRes != null && rniRes.Count > 0)
                            {
                                SpecificResult.CommandClasses = rniRes[0].CommandClasses;
                                SpecificResult.IsSlave        = rniRes[0].Basic > 2;
                            }
                        }
                        else
                        {
                            SpecificResult.CommandClasses = arnRes.CommandClasses;
                            SpecificResult.IsSlave        = arnRes.IsSlave;
                        }

                        SecurityManagerInfo.Network.SetCommandClasses(new NodeTag(SpecificResult.Id), SpecificResult.CommandClasses);

                        if (actionGroup.Actions != null && actionGroup.Actions.Length > 0 && actionGroup.Actions[0] is ReplaceFailedNodeOperation)
                        {
                            SecurityManagerInfo.Network.ResetSecuritySchemes(SpecificResult.Id);
                        }

                        if (SpecificResult.SubstituteStatus == SubstituteStatuses.None &&
                            (!SecurityManagerInfo.CheckIfSupportSecurityCC ||
                             (SpecificResult.CommandClasses != null && SpecificResult.CommandClasses.Contains(COMMAND_CLASS_SECURITY.ID))))
                        {
                            if (SecurityManagerInfo.Network.HasSecurityScheme(SpecificResult.Id, SecuritySchemes.S0) ||
                                arnRes.AddRemoveNodeStatus == AddRemoveNodeStatuses.Replicated)
                            {
                                SetStateCompleted(tu);
                                SpecificResult.SubstituteStatus = SubstituteStatuses.Done;
                            }
                            else
                            {
                                if (SpecificResult.tmpSkipS0)
                                {
                                    SetStateCompleted(tu);
                                }
                                else
                                {
                                    SpecificResult.SubstituteStatus = SubstituteStatuses.Failed;
                                    requestScheme.DestNodeId        = SpecificResult.Id;
                                    if (SecurityManagerInfo.DelaysS0.ContainsKey(SecurityS0Delays.SchemeGet))
                                    {
                                        Thread.Sleep(SecurityManagerInfo.DelaysS0[SecurityS0Delays.SchemeGet]);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (SpecificResult.SubstituteStatus != SubstituteStatuses.Done &&
                                SecurityManagerInfo.CheckIfSupportSecurityCC &&
                                SpecificResult.CommandClasses != null &&
                                !SpecificResult.CommandClasses.Contains(COMMAND_CLASS_SECURITY.ID))
                            {
                                SecurityManagerInfo.Network.ResetSecuritySchemes(SpecificResult.Id);
                            }
                            SetStateCompleted(tu);
                        }
                    }
                    else
                    {
                        SetStateFailed(tu);
                    }
                }
            }
            else
            {
                SetStateFailed(tu);
            }
        }