Ejemplo n.º 1
0
 protected KnxFrame(KnxConnection knx, ServiceType serviceType)
 {
     KnxConnection    = knx;
     CreationDateTime = DateTime.Now;
     ServiceType      = serviceType;
     IsSecureFrame    = false;
 }
Ejemplo n.º 2
0
 public Chenillard(KnxConnection clientFromMain)
 {
     this.client   = clientFromMain;
     this.isActive = false;
     this.speed    = 500;
     arrayLed      = new string[] { Led1, Led2, Led3, Led4 };
 }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            /*test output werte richtig anzeigen
             * testen knx connection unten abschalten
             * auf nas probiren*/
            _connection = new KnxConnectionTunneling("192.168.100.250", 3671, "192.168.100.194", 3671)
            {
                Debug = false
            };
            //_connection = new KnxConnectionTunneling("192.168.100.250", 3671, "172.17.0.2", 3672) { Debug = true };
            //_connection = new KnxConnectionTunneling("192.168.100.250", 3671, "192.168.100.90", 3672) { Debug = false }; /*DOCKER NAS*/
            //_connection = new KnxConnectionTunneling("192.168.100.250", 3671, "192.168.100.172", 3671) { Debug = false };
            _connection.KnxConnectedDelegate    += Connected;
            _connection.KnxDisconnectedDelegate += Disconnected;
            _connection.KnxEventDelegate        += Event;
            _connection.KnxStatusDelegate       += Status;
            _connection.Connect();

            Console.WriteLine("Done. Press [ENTER] to finish");
            //Console.ReadLine();
            //Console.WriteLine("send");
            //_connection.Action("2/1/3", true);
            //Console.ReadLine();
            //_connection.Action("2/1/3", false);
            Console.ReadLine();

            _connection.KnxDisconnectedDelegate -= Disconnected;
            _connection.Disconnect();
            Environment.Exit(0);
        }
Ejemplo n.º 4
0
        internal static ConnectResponseFrame CreateFrame(byte[] datagram, KnxConnection knx)
        {
            ConnectResponseFrame frame = new ConnectResponseFrame(knx);
            var knxDatagram            = new KnxDatagram(datagram)
            {
                HeaderLength    = datagram[0],
                ProtocolVersion = datagram[1],
                ServiceType     = new[] { datagram[2], datagram[3] },
                TotalLength     = datagram[4] + datagram[5],
                ChannelId       = datagram[6],
                Status          = datagram[7]
            };


            if (knxDatagram.ChannelId == 0x00 && knxDatagram.Status == 0x24)
            {
                frame.ErrorCode = "E_NO_MORE_CONNECTIONS";
                return(frame);
            }
            else if (knxDatagram.Status == 0x2B || knxDatagram.Status == 0x2A)
            {
                /*if (knx is KnxConnectionTunnelingSecure knxIpSec)
                 * {
                 *  knxIpSec.IpSecureErrorOccured?.Invoke(knx, new IpSecureEventArgs("E_NO_TUNNELLING_ADDRESS", KnxError.IpSecInvalidIndividualAddress));
                 *  knx.Disconnect();
                 * }*/
            }
            else if (knxDatagram.Status == 0x26)
            {
                frame.ErrorCode = "E_DATA_CONNECTION";
                return(frame);
            }
            else if (knxDatagram.Status == 0x27)
            {
                frame.ErrorCode = "E_KNX_CONNECTION";
                return(frame);
            }
            else
            {
                byte[] iab = new byte[2];
                Array.Copy(datagram, 18, iab, 0, 2);
                string ia = KnxHelper.GetIndividualAddress(iab);


                if (!String.IsNullOrEmpty(knx.IndividualAddress) && ia != knx.IndividualAddress)
                {
                    //    ((KnxConnectionTunnelingSecure) knx).IpSecureErrorOccured.Invoke(knx,
                    //        new IpSecureEventArgs("E_DATA_CONNECTION",
                    //            KnxError.IpSecRespondedIndividualAddressDiffersToGiven));
                    //    knx.Disconnect();
                    return(frame);
                }

                frame.IndividualAddress = ia;
                frame.ChannelId         = knxDatagram.ChannelId;
                frame.IsValid           = true;
            }
            return(frame);
        }
Ejemplo n.º 5
0
 public KnxBinding(KnxConnection knxConnection, MessageBus messageBus, IMessageBusAddress sendingAddress)
 {
     _knxConnection = knxConnection;
     _messageBus    = messageBus;
     Address        = sendingAddress;
     _busInlet      = _messageBus.CreateInletFor(this);
     _knxConnection.KnxEventReceived += _knxConnection_KnxEventReceived;
 }
Ejemplo n.º 6
0
        internal static DisconnectRequestFrame Parse(KnxConnection knx, byte[] data)
        {
            DisconnectRequestFrame frame = new DisconnectRequestFrame(knx);

            if (data.Length >= 6 && data[6] == knx.ChannelId)
            {
                frame.Disconnect = true;
            }

            return(frame);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Connect to the remote host using the specified port.
        /// </summary>
        public KnxClientHelper Connect()
        {
            if (knxClient != null)
            {
                knxClient.Disconnect();
            }
            if (knxEndPoint == null)
            {
                knxClient = new KnxConnectionRouting();
            }
            else
            {
                if (knxEndPoint.RemoteIp != null && knxEndPoint.LocalIp != null)
                {
                    if (knxClient == null)
                    {
                        knxClient = new KnxConnectionTunneling(knxEndPoint.RemoteIp, knxEndPoint.RemotePort, knxEndPoint.LocalIp, knxEndPoint.LocalPort);
                    }
                }
                else if (knxEndPoint.LocalIp != null && knxEndPoint.LocalPort > 0)
                {
                    if (knxClient == null)
                    {
                        knxClient = new KnxConnectionRouting(knxEndPoint.LocalIp, knxEndPoint.LocalPort);
                    }
                }
                else if (knxEndPoint.LocalIp != null && knxEndPoint.LocalPort == 0)
                {
                    if (knxClient == null)
                    {
                        knxClient = new KnxConnectionRouting(knxEndPoint.LocalIp);
                    }
                }
                else if (knxEndPoint.LocalPort > 0)
                {
                    if (knxClient == null)
                    {
                        knxClient = new KnxConnectionRouting(knxEndPoint.LocalPort);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(actionMessageCode))
            {
                knxClient.ActionMessageCode = (byte)new ByteConverter().ConvertFromString(actionMessageCode);
            }

            knxClient.Connect();
            knxClient.KnxConnectedDelegate    += knxClient_Connected;
            knxClient.KnxDisconnectedDelegate += knxClient_Disconnected;
            knxClient.KnxEventDelegate        += knxClient_EventReceived;
            knxClient.KnxStatusDelegate       += knxClient_StatusReceived;
            return(this);
        }
Ejemplo n.º 8
0
        private static void Disconnected(KnxConnection connection)
        {
            Console.WriteLine("Disconnected! Reconnecting");
            if (connection == null)
            {
                return;
            }

            Thread.Sleep(1000);
            connection.Connect();
        }
Ejemplo n.º 9
0
        private static void LightOnOff(KnxConnection connection)
        {
            Console.WriteLine("Press [ENTER] to send command ({0}) - false", LightOnOffAddress);
            Console.ReadLine();
            connection.Action(KnxGroupAddress.Parse(LightOnOffAddress), false);
            Thread.Sleep(200);

            Console.WriteLine("Press [ENTER] to send command ({0}) - true", LightOnOffAddress);
            Console.ReadLine();
            connection.Action(KnxGroupAddress.Parse(LightOnOffAddress), true);
            Thread.Sleep(200);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Disconnects from the remote host.
 /// </summary>
 public KnxClientHelper Disconnect()
 {
     if (knxClient != null)
     {
         knxClient.KnxConnectedDelegate    -= knxClient_Connected;
         knxClient.KnxDisconnectedDelegate -= knxClient_Disconnected;
         knxClient.KnxEventDelegate        -= knxClient_EventReceived;
         knxClient.KnxStatusDelegate       -= knxClient_StatusReceived;
         try { knxClient.Disconnect(); } catch { }
         knxClient = null;
     }
     return(this);
 }
 internal TunnelingConnectRequestFrameExt(KnxConnection knx, byte[] ia)
     : base(knx, KnxHelper.ServiceType.ConnectRequest)
 {
     if (ia == null)
     {
         throw new ArgumentException(nameof(ia));
     }
     if (ia.Length != 2)
     {
         throw new ArgumentException($"{nameof(ia)} must contain 2 bytes");
     }
     mIndividualAddress__ = ia;
 }
Ejemplo n.º 12
0
 public override Task <bool> Stop()
 {
     lock (_lock)
     {
         KnxHelper.Logger.LogInformation($"Stopping KNX driver...");
         if (_tunneling != null)
         {
             _tunneling.Stop();
             _callbackMap.Clear();
             _tunneling = null;
         }
     }
     return(base.Stop());
 }
Ejemplo n.º 13
0
 public bool Connect()
 {
     _connection = new KnxConnectionTunneling("192.168.10.101", 3671, "192.168.10.140", 3671)
     {
         Debug = false
     };
     _connection.KnxConnectedDelegate    += Connected;
     _connection.KnxDisconnectedDelegate += Disconnected;
     _connection.KnxEventDelegate        += Event;
     _connection.KnxStatusDelegate       += Status;
     _connection.Connect();
     OnInterfaceModulesChanged(this.GetDomain());
     return(true);
 }
        internal static ConnectionStateResponseFrame CreateFrame(byte[] datagram, KnxConnection knx)
        {
            ConnectionStateResponseFrame frame = new ConnectionStateResponseFrame(knx)
            {
                DisconnectSession = false
            };
            var response = datagram[7];

            if (response != 0x21)
            {
                return(frame);
            }

            frame.DisconnectSession = true;
            return(frame);
        }
Ejemplo n.º 15
0
        private void _knxConnection_KnxEventReceived(KnxConnection arg1, CemiFrame arg2)
        {
            ImmutableList <IMessageBusAddress> entityAddresses;

            if (_mapping.Mapping2.TryGetValue(arg2.DestinationAddress, out entityAddresses))
            {
                KnxAddressBindingTypes bindingType;
                if (_bindingTypes.TryGetValue(arg2.DestinationAddress, out bindingType))
                {
                    var message = CreateBusMessage(bindingType, arg2);
                    foreach (var messageBusAddress in entityAddresses)
                    {
                        _busInlet.Send(messageBusAddress, message);
                    }
                }
            }
        }
Ejemplo n.º 16
0
 public void Shutdown()
 {
     lock (_runLock)
     {
         if (!_run)
         {
             return;
         }
         _run = false;
     }
     _connection.Disconnect();
     Task.Delay(10000).GetAwaiter().GetResult();
     _connection.KnxDisconnectedDelegate -= Disconnected;
     _connection.KnxConnectedDelegate    -= Connected;
     _connection.KnxStatusDelegate       -= Status;
     _connection.KnxEventDelegate        -= Event;
     _connection = null;
 }
Ejemplo n.º 17
0
        private static void Main()
        {
            _connection = new KnxConnectionRouting {
                Debug = false, ActionMessageCode = 0x29
            };
            _connection.KnxConnectedDelegate    += Connected;
            _connection.KnxDisconnectedDelegate += Disconnected;
            _connection.KnxEventDelegate        += Event;
            _connection.KnxStatusDelegate       += Status;
            _connection.Connect();

            LightOnOff();


            Console.WriteLine("Done. Press [ENTER] to finish");
            Console.Read();
            Environment.Exit(0);
        }
Ejemplo n.º 18
0
        internal override byte[] ToFrame()
        {
            int cemi_start_pos = 10;
            var datagram       = new byte[21];

            datagram[00] = 0x06;
            datagram[01] = 0x10;
            datagram[02] = 0x04;
            datagram[03] = 0x20;
            datagram[04] = 0x00;
            datagram[05] = 0x15;

            datagram[06] = 0x04;
            datagram[07] = KnxConnection.ChannelId;
            datagram[08] = KnxConnection.GenerateSequenceNumber();
            datagram[09] = 0x00;

            var i = 0;

            datagram[cemi_start_pos + i++] =
                KnxConnection.ActionMessageCode != 0x00
                    ? KnxConnection.ActionMessageCode
                    : (byte)0x11;

            datagram[cemi_start_pos + i++] = 0x00;
            datagram[cemi_start_pos + i++] = 0xAC;

            datagram[cemi_start_pos + i++] =
                KnxHelper.IsAddressIndividual(DestinationAddress)
                    ? (byte)0x50
                    : (byte)0xF0;

            datagram[cemi_start_pos + i++] = 0x00;
            datagram[cemi_start_pos + i++] = 0x00;
            byte[] dstAddress = KnxHelper.GetAddress(DestinationAddress);
            datagram[cemi_start_pos + i++] = dstAddress[0];
            datagram[cemi_start_pos + i++] = dstAddress[1];

            datagram[cemi_start_pos + i++] = 0x01;
            datagram[cemi_start_pos + i++] = 0x00;
            datagram[cemi_start_pos + i]   = 0x00;

            return(datagram);
        }
Ejemplo n.º 19
0
        public override bool Init()
        {
            var ipAddress = GetProperty("knx-ip").ValueString;
            var useNat    = GetProperty("knx-use-nat").ValueBool;
            var port      = GetPropertyValueInt("knx-port");

            if (String.IsNullOrEmpty(ipAddress))
            {
                DriverContext.Logger.LogError($"IP Address cannot be empty!");
                return(false);
            }

            try
            {
                var remoteIp = IPAddress.Parse(ipAddress);
                if (_secureDriver)
                {
                    throw new NotImplementedException();
                }
                else
                {
                    _tunneling = new KnxConnectionTunneling(this, remoteIp, port,
                                                            IPAddress.Parse(NetworkHelper.GetActiveIp()), NetworkHelper.GetFreeTcpPort());

                    if (useNat == null)
                    {
                        _tunneling.UseNat = false;
                    }
                    else
                    {
                        _tunneling.UseNat = useNat.Value;
                    }
                }
            }
            catch (Exception e)
            {
                KnxHelper.Logger.LogError($"Could not init knx driver {e}");
                return(false);
            }

            KnxHelper.Logger.LogInformation($"Init done...");

            return(true);
        }
Ejemplo n.º 20
0
        private static void Main()
        {
            _connection = new KnxConnectionTunneling("10.0.250.20", 3671, "10.0.253.5", 3671)
            {
                Debug = false
            };
            _connection.KnxConnectedDelegate    += Connected;
            _connection.KnxDisconnectedDelegate += Disconnected;
            _connection.KnxEventDelegate        += Event;
            _connection.KnxStatusDelegate       += Status;
            _connection.Connect();

            Console.WriteLine("Done. Press [ENTER] to finish");
            Console.Read();

            _connection.KnxDisconnectedDelegate -= Disconnected;
            _connection.Disconnect();
            Environment.Exit(0);
        }
Ejemplo n.º 21
0
        public override bool Init()
        {
            var ipAddress = GetProperty("knx-ip").ValueString;
            var useNat    = GetProperty("knx-use-nat").ValueBool;
            var port      = GetPropertyValueInt("knx-port");

            try
            {
                var remoteIp = IPAddress.Parse(ipAddress);
                if (_secureDriver)
                {
                    throw new NotImplementedException();
                }
                else
                {
                    _tunneling = new KnxConnectionTunneling(remoteIp, port,
                                                            IPAddress.Parse(NetworkHelper.GetActiveIp()), NetworkHelper.GetFreeTcpPort());

                    if (useNat == null)
                    {
                        _tunneling.UseNat = false;
                    }
                    else
                    {
                        _tunneling.UseNat = useNat.Value;
                    }
                }

                _tunneling.OnDatagramReceived += KnxEventDelegate;
                _tunneling.OnConnected        += KnxConnectedEvent;
                _tunneling.OnDisconnected     += KnxDisconnectedEvent;
            }
            catch (Exception e)
            {
                KnxHelper.Logger.LogError($"Could not init knx driver {e}");
                return(false);
            }

            KnxHelper.Logger.LogInformation($"Init done...");

            return(true);
        }
Ejemplo n.º 22
0
        private static Task CreateNewTask(CancellationTokenSource cts, KnxConnection connection)
        {
            return(new Task(async() =>

            {
                try
                {
                    while (true)
                    {
                        connection.Write("7/2/0",
                                         DptTranslator.Instance.ToDataPoint("16.001", "7141.61$"));
                        await Task.Delay(1000);
                    }
                }
                catch (OperationCanceledException)
                {
                    return;
                }
            }, cts.Token));;
        }
Ejemplo n.º 23
0
        internal override byte[] ToFrame()
        {
            var datagram = new byte[16];

            datagram[00] = 0x06;
            datagram[01] = 0x10;
            datagram[02] = 0x02;
            datagram[03] = 0x09;
            datagram[04] = 0x00;
            datagram[05] = 0x10;


            datagram[06] = KnxConnection.ChannelId;
            datagram[07] = 0x00;

            if (KnxConnection.GetType() == typeof(KnxConnectionTunneling))
            {
                datagram[08] = 0x08;
                datagram[09] = 0x01;
                datagram[10] = KnxConnection.LocalEndpoint.Address.GetAddressBytes()[0];
                datagram[11] = KnxConnection.LocalEndpoint.Address.GetAddressBytes()[1];
                datagram[12] = KnxConnection.LocalEndpoint.Address.GetAddressBytes()[2];
                datagram[13] = KnxConnection.LocalEndpoint.Address.GetAddressBytes()[3];
                datagram[14] = (byte)(KnxConnection.LocalEndpoint.Port >> 8);
                datagram[15] = (byte)KnxConnection.LocalEndpoint.Port;
            }
            else
            {
                datagram[08] = 0x08;
                datagram[09] = 0x02;
                datagram[10] = 0x00;
                datagram[11] = 0x00;
                datagram[12] = 0x00;
                datagram[13] = 0x00;
                datagram[14] = 0x00;
                datagram[15] = 0x00;
            }
            return(datagram);
        }
Ejemplo n.º 24
0
        internal override byte[] ToFrame()
        {
            var datagram = new byte[10 + mCemiBytes__.Length];

            datagram[00] = 0x06;
            datagram[01] = 0x10;
            datagram[02] = 0x04;
            datagram[03] = 0x20;

            var totalLength = BitConverter.GetBytes(mCemiBytes__.Length + 10);

            datagram[04] = totalLength[1];
            datagram[05] = totalLength[0];

            datagram[06] = 0x04;
            datagram[07] = KnxConnection.ChannelId;
            datagram[08] = KnxConnection.GenerateSequenceNumber();
            datagram[09] = 0x00;

            Array.Copy(mCemiBytes__, 0, datagram, 10, mCemiBytes__.Length);

            return(datagram);
        }
Ejemplo n.º 25
0
        private static void Main()
        {
            _logFile = new StreamWriter("telegrams.txt");

            _connection = new KnxConnectionRouting {
                Debug = false, ActionMessageCode = 0x29
            };
            _connection.KnxConnectedDelegate    += Connected;
            _connection.KnxDisconnectedDelegate += Disconnected;
            _connection.KnxEventDelegate        += Event;
            _connection.KnxStatusDelegate       += Status;
            _connection.Connect();

            //LightOnOff();
            //BlindUpDown1();
            //BlindUpDown2();
            //TemperatureSetpoint();

            Console.WriteLine("Done. Press [ENTER] to finish");
            Console.Read();
            _logFile.Dispose();
            Environment.Exit(0);
        }
Ejemplo n.º 26
0
        internal static TunnelingRequestFrame CreateFrame(byte[] datagram, KnxConnection knx, int rxSequenceNumber, bool isFirstRequest)
        {
            TunnelingRequestFrame frame = new TunnelingRequestFrame(knx);


            var knxDatagram = new KnxDatagram(datagram)
            {
                HeaderLength    = datagram[0],
                ProtocolVersion = datagram[1],
                ServiceType     = new[] { datagram[2], datagram[3] },
                TotalLength     = datagram[4] + datagram[5]
            };

            var channelId = datagram[7];

            if (channelId != knx.ChannelId)
            {
                return(frame);
            }

            var sequenceNumber = datagram[8];
            var isValid        = isFirstRequest || (sequenceNumber >= rxSequenceNumber);

            frame.FrameSequenceNumber = sequenceNumber;
            frame.IsValid             = isValid;
            if (isValid)
            {
                var cemi = new byte[datagram.Length - 10];
                Array.Copy(datagram, 10, cemi, 0, datagram.Length - 10);

                frame.CemiPacket = cemi;
                frame.IsValid    = true;
                frame.Datagram   = knxDatagram;
            }

            return(frame);
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Connect to the remote host using the specified port.
 /// </summary>
 /// <param name="port">Port number.</param>
 public KnxClientHelper Connect()
 {
     if (knxClient != null)
     {
         knxClient.Disconnect();
     }
     if (knxEndPoint == null)
     {
         knxClient = new KnxConnectionRouting();
     }
     else
     {
         if (knxEndPoint.RemoteIp != null && knxEndPoint.LocalIp != null)
         {
             knxClient = new KnxConnectionTunneling(knxEndPoint.RemoteIp, knxEndPoint.RemotePort, knxEndPoint.LocalIp, knxEndPoint.LocalPort);
         }
         else if (knxEndPoint.LocalIp != null && knxEndPoint.LocalPort > 0)
         {
             knxClient = new KnxConnectionRouting(knxEndPoint.LocalIp, knxEndPoint.LocalPort);
         }
         else if (knxEndPoint.LocalIp != null && knxEndPoint.LocalPort == 0)
         {
             knxClient = new KnxConnectionRouting(knxEndPoint.LocalIp);
         }
         else if (knxEndPoint.LocalPort > 0)
         {
             knxClient = new KnxConnectionRouting(knxEndPoint.LocalPort);
         }
     }
     knxClient.Connect();
     knxClient.KnxConnectedDelegate    += knxClient_Connected;
     knxClient.KnxDisconnectedDelegate += knxClient_Disconnected;
     knxClient.KnxEventDelegate        += knxClient_EventReceived;
     knxClient.KnxStatusDelegate       += knxClient_StatusReceived;
     return(this);
 }
Ejemplo n.º 28
0
        internal override byte[] ToFrame()
        {
            var dataLength = KnxHelper.GetDataLength(_data);

            // HEADER
            var datagram = new byte[10];

            datagram[00] = 0x06;
            datagram[01] = 0x10;
            datagram[02] = 0x04;
            datagram[03] = 0x20;

            var totalLength = BitConverter.GetBytes(dataLength + 20);

            datagram[04] = totalLength[1];
            datagram[05] = totalLength[0];

            datagram[06] = 0x04;
            datagram[07] = KnxConnection.ChannelId;
            datagram[08] = KnxConnection.GenerateSequenceNumber();
            datagram[09] = 0x00;

            return(CreateActionDatagramCommon(_ia, _data, datagram));
        }
Ejemplo n.º 29
0
 public KnxTunnelingSender(KnxConnection connection, UdpClient client) : base(connection)
 {
     _client = client;
 }
Ejemplo n.º 30
0
 private RawDataFrame(KnxConnection knx, byte[] data)
     : base(knx, KnxHelper.ServiceType.Unknown)
 {
     mData__ = data;
 }
Ejemplo n.º 31
0
 protected KnxSender(KnxConnection connection)
 {
     KnxConnection = connection;
 }