Ejemplo n.º 1
0
        public byte[] GetBytesCemi()
        {
            TunnelCemiRequest builder = new TunnelCemiRequest();

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.Connect, 255);
            return(builder.GetBytes());
        }
Ejemplo n.º 2
0
        public byte[] GetBytesCemi()
        {
            if (IsExtended && Length > 256)
            {
                throw new Exception("Bei cEmi kann maximal 256 Bytes ausgelesen werden. (Angefordert waren " + Length + " bytes)[ExtendedFrame]");
            }

            if (!IsExtended && Length > 13)
            {
                throw new Exception("Bei cEmi kann maximal 13 Bytes ausgelesen werden. (Angefordert waren " + Length + " bytes)[StandardFrame]");
            }


            List <byte> data = new List <byte> {
                BitConverter.GetBytes(Length)[0]
            };

            byte[] addr = BitConverter.GetBytes(Address);
            data.Add(addr[1]);
            data.Add(addr[0]);

            Builders.TunnelRequest builder = new TunnelRequest();
            if (IsExtended)
            {
                builder.SetIsExtended();
            }
            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.MemoryRead, SequenceNumber, data.ToArray());
            data = new List <byte>()
            {
                0x11, 0x00
            };
            data.AddRange(builder.GetBytes());
            return(data.ToArray());
        }
Ejemplo n.º 3
0
        public byte[] GetBytesCemi()
        {
            TunnelCemiRequest builder = new TunnelCemiRequest();

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.DeviceDescriptorRead, SequenceNumber);
            return(builder.GetBytes());
        }
Ejemplo n.º 4
0
        public byte[] GetBytesCemi()
        {
            TunnelCemiRequest builder = new TunnelCemiRequest();

            builder.Build(UnicastAddress.FromString("0.0.0"), MulticastAddress.FromString("0/0/0"), ApciTypes.IndividualAddressRead);
            return(builder.GetBytes());
        }
Ejemplo n.º 5
0
        public byte[] GetBytesEmi1()
        {
            TunnelEmi1Request builder = new TunnelEmi1Request();

            builder.Build(UnicastAddress.FromString("0.0.0"), MulticastAddress.FromString("0/0/0"), ApciTypes.IndividualAddressRead);
            builder.SetChannelId(ChannelId);
            builder.SetSequence(SequenceCounter);
            return(builder.GetBytes());
        }
Ejemplo n.º 6
0
        public byte[] GetBytesCemi()
        {
            TunnelCemiRequest builder = new TunnelCemiRequest();

            byte[] data = { ObjectIndex, PropertyId, 0x10, 0x01 };

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.PropertyValueRead, SequenceNumber, data);
            return(builder.GetBytes());
        }
Ejemplo n.º 7
0
        public byte[] GetBytesCemi()
        {
            List <byte> data = new List <byte>()
            {
                0x11, 0x00
            };
            TunnelRequest builder = new TunnelRequest();

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.Connect, 255);
            data.AddRange(builder.GetBytes());
            return(data.ToArray());
        }
Ejemplo n.º 8
0
        public byte[] GetBytesEmi1()
        {
            List <byte> data = new List <byte>()
            {
                0x11, 0x00
            };
            TunnelRequest builder = new TunnelRequest();

            builder.Build(UnicastAddress.FromString("0.0.0"), MulticastAddress.FromString("0/0/0"), ApciTypes.IndividualAddressRead);
            data.AddRange(builder.GetBytes());
            return(data.ToArray());
        }
        private static ConnectionResponseDataBlock ParseConnectionResponseDataBlock(IEnumerable <byte> bytes)
        {
            if (bytes.Count() == 0)
            {
                return(new ConnectionResponseDataBlock(0x00, 0x01, UnicastAddress.FromString("0.0.0")));
            }

            var enumerable = bytes as byte[] ?? bytes.ToArray();

            return(new ConnectionResponseDataBlock(enumerable.ElementAt(0), enumerable.ElementAt(1),
                                                   UnicastAddress.FromByteArray(enumerable.Skip(2).ToArray())));
        }
        public byte[] GetBytesCemi()
        {
            TunnelCemiRequest builder = new TunnelCemiRequest();

            List <byte> data = new List <byte>();

            data.AddRange(Serial);

            data.AddRange(NewAddress.GetBytes());
            data.AddRange(new byte[] { 0, 0, 0, 0 });

            builder.Build(UnicastAddress.FromString("0.0.0"), MulticastAddress.FromString("0/0/0"), ApciTypes.IndividualAddressSerialNumberWrite, 255, data.ToArray());
            builder.SetPriority(Prios.System);
            return(builder.GetBytes());
        }
Ejemplo n.º 11
0
        public byte[] GetBytesCemi()
        {
            TunnelCemiRequest builder = new TunnelCemiRequest();
            List <byte>       data    = new List <byte> {
                Convert.ToByte(Data.Length)
            };

            byte[] addr = BitConverter.GetBytes(Convert.ToInt16(Address));
            Array.Reverse(addr);
            data.AddRange(addr);
            data.AddRange(Data);

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.MemoryWrite, SequenceNumber, data.ToArray());
            return(builder.GetBytes());
        }
        public byte[] GetBytesCemi()
        {
            TunnelRequest builder = new TunnelRequest();
            List <byte>   data    = new List <byte>()
            {
                ObjectIndex, PropertyId, PropertyIndex
            };

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciType, SequenceNumber, data.ToArray());
            data = new List <byte>()
            {
                0x11, 0x00
            };
            data.AddRange(builder.GetBytes());
            return(data.ToArray());
        }
Ejemplo n.º 13
0
        public byte[] GetBytesCemi()
        {
            byte[] send_data = new byte[Data.Length + 4];

            send_data[0] = ObjectIndex;
            send_data[1] = PropertyId;
            send_data[2] = 0x10;
            send_data[3] = 0x01;

            for (int i = 0; i < Data.Length; i++)
            {
                send_data[i + 4] = Data[i];
            }

            TunnelCemiRequest builder = new TunnelCemiRequest();

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.PropertyValueWrite, SequenceNumber, send_data);
            return(builder.GetBytes());
        }
Ejemplo n.º 14
0
        public byte[] GetBytesCemi()
        {
            if (Length > 256)
            {
                throw new Exception("Bei cEmi kann maximal 256 Bytes ausgelesen werden. (Angefordert waren " + Length + " bytes)");
            }


            List <byte> data = new List <byte> {
                BitConverter.GetBytes(Length)[0]
            };

            byte[] addr = BitConverter.GetBytes(Address);
            data.Add(addr[1]);
            data.Add(addr[0]);

            Builders.TunnelCemiRequest builder = new TunnelCemiRequest();
            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.MemoryRead, SequenceNumber, data.ToArray());

            return(builder.GetBytes());
        }
Ejemplo n.º 15
0
        public byte[] GetBytesCemi()
        {
            byte[] send_data = new byte[Data.Length + 4];

            send_data[0] = ObjectIndex;
            send_data[1] = PropertyId;
            send_data[2] = 0x10; //TODO check if it must be set to data.length
            send_data[3] = 0x01;

            for (int i = 0; i < Data.Length; i++)
            {
                send_data[i + 4] = Data[i];
            }

            List <byte> data = new List <byte>()
            {
                0x11, 0x00
            };
            TunnelRequest builder = new TunnelRequest();

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciTypes.PropertyValueWrite, SequenceNumber, send_data);
            data.AddRange(builder.GetBytes());
            return(data.ToArray());
        }
Ejemplo n.º 16
0
        public byte[] GetBytesCemi()
        {
            TunnelRequest builder = new TunnelRequest();


            List <byte> data = new List <byte>()
            {
            };

            data.Add(0x00);
            data.Add((byte)(Key >> 24));
            data.Add((byte)(Key >> 16));
            data.Add((byte)(Key >> 8));
            data.Add((byte)Key);

            builder.Build(UnicastAddress.FromString("0.0.0"), DestinationAddress, ApciType, SequenceNumber, data.ToArray());

            data = new List <byte>()
            {
                0x11, 0x00
            };
            data.AddRange(builder.GetBytes());
            return(data.ToArray());
        }
Ejemplo n.º 17
0
        private void ProcessReceivingMessages(UdpClient _udpClient)
        {
            Debug.WriteLine("Höre jetzt auf: " + (_udpClient.Client.LocalEndPoint as IPEndPoint).Port);
            Task.Run(async() =>
            {
                int rofl = 0;
                try
                {
                    while (!StopProcessing)
                    {
                        rofl++;
                        var result      = await _udpClient.ReceiveAsync();
                        var knxResponse = _receiveParserDispatcher.Build(result.Buffer);

                        //if(!(knxResponse is SearchResponse))
                        //    Debug.WriteLine("Telegram angekommen: " + knxResponse?.ToString());


                        switch (knxResponse)
                        {
                        case ConnectStateResponse connectStateResponse:
                            Debug.WriteLine("Connection State Response: " + connectStateResponse.Status.ToString());
                            switch (connectStateResponse.Status)
                            {
                            case 0x00:
                                IsConnected = true;
                                ConnectionChanged?.Invoke(IsConnected);
                                break;

                            default:
                                Debug.WriteLine("Connection State: Fehler: " + connectStateResponse.Status.ToString());
                                LastError   = ConnectionErrors.NotConnectedToBus;
                                IsConnected = false;
                                ConnectionChanged?.Invoke(IsConnected);
                                break;
                            }
                            break;

                        case ConnectResponse connectResponse:
                            _flagCRRecieved = true;
                            switch (connectResponse.Status)
                            {
                            case 0x00:
                                _sequenceCounter      = 0;
                                _communicationChannel = connectResponse.CommunicationChannel;
                                IsConnected           = true;
                                ConnectionChanged?.Invoke(IsConnected);
                                PhysicalAddress = connectResponse.ConnectionResponseDataBlock.KnxAddress;
                                Debug.WriteLine("Connected: Eigene Adresse: " + PhysicalAddress.ToString());
                                break;

                            default:
                                Debug.WriteLine("Connected: Fehler: " + connectResponse.Status.ToString());
                                LastError   = ConnectionErrors.Undefined;
                                IsConnected = false;
                                ConnectionChanged?.Invoke(IsConnected);
                                break;
                            }
                            break;

                        case Builders.TunnelResponse tunnelResponse:
                            if (tunnelResponse.IsRequest && tunnelResponse.DestinationAddress != PhysicalAddress)
                            {
                                Debug.WriteLine("Telegram erhalten das nicht mit der Adresse selbst zu tun hat!");
                                Debug.WriteLine("Typ: " + tunnelResponse.APCI);
                                Debug.WriteLine("Eigene Adresse: " + PhysicalAddress.ToString());
                                break;
                            }

                            _sendMessages.Add(new Responses.TunnelResponse(0x06, 0x10, 0x0A, 0x04, _communicationChannel, tunnelResponse.SequenceCounter, 0x00).GetBytes());

                            //Debug.WriteLine("Telegram APCI: " + tunnelResponse.APCI.ToString());

                            if (tunnelResponse.APCI.ToString().EndsWith("Response"))
                            {
                                List <byte> data = new List <byte>()
                                {
                                    0x11, 0x00
                                };
                                TunnelRequest builder = new TunnelRequest();
                                builder.Build(UnicastAddress.FromString("0.0.0"), tunnelResponse.SourceAddress, ApciTypes.Ack, tunnelResponse.SequenceNumber);
                                data.AddRange(builder.GetBytes());
                                _ = Send(data.ToArray(), _sequenceCounter);
                                _sequenceCounter++;
                                //Debug.WriteLine("Got Response " + tunnelResponse.SequenceCounter + " . " + tunnelResponse.SequenceNumber);
                            }
                            else if (tunnelResponse.APCI == ApciTypes.Ack)
                            {
                                OnTunnelAck?.Invoke(new MsgAckRes()
                                {
                                    ChannelId          = tunnelResponse.CommunicationChannel,
                                    SequenceCounter    = tunnelResponse.SequenceCounter,
                                    SequenceNumber     = tunnelResponse.SequenceNumber,
                                    SourceAddress      = tunnelResponse.SourceAddress,
                                    DestinationAddress = tunnelResponse.DestinationAddress
                                });
                                break;
                            }


                            List <string> temp = new List <string>();
                            var q = from t in Assembly.GetExecutingAssembly().GetTypes()
                                    where t.IsClass && t.IsNested == false && (t.Namespace == "Kaenx.Konnect.Messages.Response" || t.Namespace == "Kaenx.Konnect.Messages.Request")
                                    select t;

                            IMessage message = null;

                            foreach (Type t in q.ToList())
                            {
                                IMessage resp = (IMessage)Activator.CreateInstance(t);

                                if (resp.ApciType == tunnelResponse.APCI)
                                {
                                    message = resp;
                                    break;
                                }
                            }


                            if (message == null)
                            {
                                //throw new Exception("Kein MessageParser für den APCI " + tunnelResponse.APCI);
                                message = new MsgDefaultRes()
                                {
                                    ApciType = tunnelResponse.APCI
                                };
                                Debug.WriteLine("Kein MessageParser für den APCI " + tunnelResponse.APCI);
                            }

                            message.Raw                = tunnelResponse.Data;
                            message.ChannelId          = tunnelResponse.CommunicationChannel;
                            message.SequenceCounter    = tunnelResponse.SequenceCounter;
                            message.SequenceNumber     = tunnelResponse.SequenceNumber;
                            message.SourceAddress      = tunnelResponse.SourceAddress;
                            message.DestinationAddress = tunnelResponse.DestinationAddress;

                            switch (CurrentType)
                            {
                            case ProtocolTypes.cEmi:
                                message.ParseDataCemi();
                                break;

                            case ProtocolTypes.Emi1:
                                message.ParseDataEmi1();
                                break;

                            case ProtocolTypes.Emi2:
                                message.ParseDataEmi2();
                                break;

                            default:
                                throw new NotImplementedException("Unbekanntes Protokoll - TunnelResponse KnxIpTunneling");
                            }


                            if (tunnelResponse.APCI.ToString().EndsWith("Response"))
                            {
                                OnTunnelResponse?.Invoke(message as IMessageResponse);
                            }
                            else
                            {
                                OnTunnelRequest?.Invoke(message as IMessageRequest);
                            }

                            break;

                        case SearchResponse searchResponse:
                            MsgSearchRes msg = new MsgSearchRes(searchResponse.responseBytes);
                            switch (CurrentType)
                            {
                            case ProtocolTypes.cEmi:
                                msg.ParseDataCemi();
                                break;

                            case ProtocolTypes.Emi1:
                                msg.ParseDataEmi1();
                                break;

                            case ProtocolTypes.Emi2:
                                msg.ParseDataEmi2();
                                break;

                            default:
                                throw new NotImplementedException("Unbekanntes Protokoll - SearchResponse KnxIpTunneling");
                            }
                            OnSearchResponse?.Invoke(msg);
                            break;

                        case TunnelAckResponse tunnelAck:
                            //Do nothing
                            break;

                        case DisconnectResponse disconnectResponse:
                            IsConnected           = false;
                            _communicationChannel = 0;
                            ConnectionChanged?.Invoke(IsConnected);
                            break;
                        }
                    }

                    Debug.WriteLine("Stopped Processing Messages " + _udpClient.Client.LocalEndPoint.ToString());
                    _udpClient.Close();
                    _udpClient.Dispose();
                }
                catch
                {
                }
            });
        }