Пример #1
0
        private void ProcessDatagramHeaders(byte[] datagram)
        {
            // HEADER
            // TODO: Might be interesting to take out these magic numbers for the datagram indices
            var knxDatagram = new KnxDatagram
            {
                header_length    = datagram[0],
                protocol_version = datagram[1],
                service_type     = new[] { datagram[2], datagram[3] },
                total_length     = datagram[4] + datagram[5]
            };

            var channelId = datagram[7];

            if (channelId != ChannelId)
            {
                return;
            }

            var sequenceNumber = datagram[8];

            _rxSequenceNumber = sequenceNumber;
            var cemi = new byte[datagram.Length - 10];

            Array.Copy(datagram, 10, cemi, 0, datagram.Length - 10);

            SendTunnelingAck(sequenceNumber);
            ProcessCEMI(knxDatagram, cemi);
        }
Пример #2
0
        private void ProcessConnectResponse(byte[] datagram)
        {
            // HEADER
            var knxDatagram = new KnxDatagram
            {
                header_length    = datagram[0],
                protocol_version = datagram[1],
                service_type     = new[] { datagram[2], datagram[3] },
                total_length     = datagram[4] + datagram[5],
                channel_id       = datagram[6],
                status           = datagram[7]
            };

            if (knxDatagram.channel_id == 0x00 && knxDatagram.status == 0x24)
            {
                Logger.Log("ProcessConnectResponse: - No more connections available");
                ErrorLog.Error("ProcessConnectResponse - No more connections available");
                Disconnect();
            }
            else
            {
                ChannelId = knxDatagram.channel_id;
                ResetSequenceNumber();
                stateRequestTimer.Reset(stateRequestTimerInterval);
                if (OnConnect != null)
                {
                    OnConnect();
                }
            }
        }
Пример #3
0
        private void ProcessConnectionStateResponse(byte[] datagram)
        {
            // HEADER
            // 06 10 02 08 00 08 -- 48 21
            var knxDatagram = new KnxDatagram
            {
                header_length    = datagram[0],
                protocol_version = datagram[1],
                service_type     = new[] { datagram[2], datagram[3] },
                total_length     = datagram[4] + datagram[5],
                channel_id       = datagram[6]
            };

            var response = datagram[7];

            if (response == 0)
            {
                Alive = true;
                return;
            }

            if (response == 0x21)
            {
                Logger.Log("ProcessConnectionStateResponse - No active connection with channel ID {0}", knxDatagram.channel_id);
            }
            else
            {
                Logger.Log("ProcessConnectionStateResponse - Error code {0}", response);
            }

            Disconnect();
        }
Пример #4
0
        private void BuildAndExecuteKnxRx(KnxDatagram datagram)
        {
            string data = string.Empty;

            if (datagram.data_length == 1)
            {
                int bitval = datagram.apdu[1] & 0x3F;
                data = bitval.ToString("X2");
            }
            else
            {
                data = BitConverter.ToString(datagram.apdu, 2).Replace("-", string.Empty);
            }

            data = datagram.destination_address + ":" +
                   datagram.data_length + ":" + data;

            Pacer.EnqueueRX(data);
        }
Пример #5
0
        protected void ProcessCEMI(KnxDatagram datagram, byte[] cemi)
        {
            try
            {
                // CEMI
                // +--------+--------+--------+--------+----------------+----------------+--------+----------------+
                // |  Msg   |Add.Info| Ctrl 1 | Ctrl 2 | Source Address | Dest. Address  |  Data  |      APDU      |
                // | Code   | Length |        |        |                |                | Length |                |
                // +--------+--------+--------+--------+----------------+----------------+--------+----------------+
                //   1 byte   1 byte   1 byte   1 byte      2 bytes          2 bytes       1 byte      2 bytes
                //
                //  Message Code    = 0x11 - a L_Data.req primitive
                //      COMMON EMI MESSAGE CODES FOR DATA LINK LAYER PRIMITIVES
                //          FROM NETWORK LAYER TO DATA LINK LAYER
                //          +---------------------------+--------------+-------------------------+---------------------+------------------+
                //          | Data Link Layer Primitive | Message Code | Data Link Layer Service | Service Description | Common EMI Frame |
                //          +---------------------------+--------------+-------------------------+---------------------+------------------+
                //          |        L_Raw.req          |    0x10      |                         |                     |                  |
                //          +---------------------------+--------------+-------------------------+---------------------+------------------+
                //          |                           |              |                         | Primitive used for  | Sample Common    |
                //          |        L_Data.req         |    0x11      |      Data Service       | transmitting a data | EMI frame        |
                //          |                           |              |                         | frame               |                  |
                //          +---------------------------+--------------+-------------------------+---------------------+------------------+
                //          |        L_Poll_Data.req    |    0x13      |    Poll Data Service    |                     |                  |
                //          +---------------------------+--------------+-------------------------+---------------------+------------------+
                //          |        L_Raw.req          |    0x10      |                         |                     |                  |
                //          +---------------------------+--------------+-------------------------+---------------------+------------------+
                //          FROM DATA LINK LAYER TO NETWORK LAYER
                //          +---------------------------+--------------+-------------------------+---------------------+
                //          | Data Link Layer Primitive | Message Code | Data Link Layer Service | Service Description |
                //          +---------------------------+--------------+-------------------------+---------------------+
                //          |        L_Poll_Data.con    |    0x25      |    Poll Data Service    |                     |
                //          +---------------------------+--------------+-------------------------+---------------------+
                //          |                           |              |                         | Primitive used for  |
                //          |        L_Data.ind         |    0x29      |      Data Service       | receiving a data    |
                //          |                           |              |                         | frame               |
                //          +---------------------------+--------------+-------------------------+---------------------+
                //          |        L_Busmon.ind       |    0x2B      |   Bus Monitor Service   |                     |
                //          +---------------------------+--------------+-------------------------+---------------------+
                //          |        L_Raw.ind          |    0x2D      |                         |                     |
                //          +---------------------------+--------------+-------------------------+---------------------+
                //          |                           |              |                         | Primitive used for  |
                //          |                           |              |                         | local confirmation  |
                //          |        L_Data.con         |    0x2E      |      Data Service       | that a frame was    |
                //          |                           |              |                         | sent (does not mean |
                //          |                           |              |                         | successful receive) |
                //          +---------------------------+--------------+-------------------------+---------------------+
                //          |        L_Raw.con          |    0x2F      |                         |                     |
                //          +---------------------------+--------------+-------------------------+---------------------+

                //  Add.Info Length = 0x00 - no additional info
                //  Control Field 1 = see the bit structure above
                //  Control Field 2 = see the bit structure above
                //  Source Address  = 0x0000 - filled in by router/gateway with its source address which is
                //                    part of the KNX subnet
                //  Dest. Address   = KNX group or individual address (2 byte)
                //  Data Length     = Number of bytes of data in the APDU excluding the TPCI/APCI bits
                //  APDU            = Application Protocol Data Unit - the actual payload including transport
                //                    protocol control information (TPCI), application protocol control
                //                    information (APCI) and data passed as an argument from higher layers of
                //                    the KNX communication stack
                //
                datagram.message_code          = cemi[0];
                datagram.aditional_info_length = cemi[1];

                if (datagram.aditional_info_length > 0)
                {
                    datagram.aditional_info = new byte[datagram.aditional_info_length];
                    for (var i = 0; i < datagram.aditional_info_length; i++)
                    {
                        datagram.aditional_info[i] = cemi[2 + i];
                    }
                }

                datagram.control_field_1 = cemi[2 + datagram.aditional_info_length];
                datagram.control_field_2 = cemi[3 + datagram.aditional_info_length];
                datagram.source_address  = KnxHelper.GetIndividualAddress(new[] { cemi[4 + datagram.aditional_info_length], cemi[5 + datagram.aditional_info_length] });

                datagram.destination_address =
                    KnxHelper.GetKnxDestinationAddressType(datagram.control_field_2).Equals(KnxHelper.KnxDestinationAddressType.INDIVIDUAL)
                        ? KnxHelper.GetIndividualAddress(new[] { cemi[6 + datagram.aditional_info_length], cemi[7 + datagram.aditional_info_length] })
                        : KnxHelper.GetGroupAddress(new[] { cemi[6 + datagram.aditional_info_length], cemi[7 + datagram.aditional_info_length] },
                                                    true);

                datagram.data_length = cemi[8 + datagram.aditional_info_length];
                datagram.apdu        = new byte[datagram.data_length + 1];

                for (var i = 0; i < datagram.apdu.Length; i++)
                {
                    datagram.apdu[i] = cemi[9 + i + datagram.aditional_info_length];
                }

                datagram.data = KnxHelper.GetData(datagram.data_length, datagram.apdu);

                Logger.Log("-----------------------------------------------------------------------------------------------------");
                Logger.Log(BitConverter.ToString(cemi));
                Logger.Log("Event Header Length: " + datagram.header_length);
                Logger.Log("Event Protocol Version: " + datagram.protocol_version.ToString("x"));
                Logger.Log("Event Service Type: 0x" + BitConverter.ToString(datagram.service_type).Replace("-", string.Empty));
                Logger.Log("Event Total Length: " + datagram.total_length);

                Logger.Log("Event Message Code: " + datagram.message_code.ToString("x"));
                Logger.Log("Event Aditional Info Length: " + datagram.aditional_info_length);

                if (datagram.aditional_info_length > 0)
                {
                    Logger.Log("Event Aditional Info: 0x" + BitConverter.ToString(datagram.aditional_info).Replace("-", string.Empty));
                }

                Logger.Log("Event Control Field 1: " + Convert.ToString(datagram.control_field_1, 2));
                Logger.Log("Event Control Field 2: " + Convert.ToString(datagram.control_field_2, 2));
                Logger.Log("Event Source Address: " + datagram.source_address);
                Logger.Log("Event Destination Address: " + datagram.destination_address);
                Logger.Log("Event Data Length: " + datagram.data_length);
                Logger.Log("Event APDU: 0x" + BitConverter.ToString(datagram.apdu).Replace("-", string.Empty));
                Logger.Log("-----------------------------------------------------------------------------------------------------");

                if (datagram.message_code != 0x29)
                {
                    return;
                }

                var type = datagram.apdu[1] >> 4;

                switch (type)
                {
                case 8:
                    BuildAndExecuteKnxRx(datagram);
                    Logger.Log("{0}", datagram.source_address);
                    break;

                case 4:
                    BuildAndExecuteKnxRx(datagram);
                    Logger.Log("Device {0} status {1}", datagram.source_address, datagram.destination_address);
                    break;
                }
            }
            catch (Exception e)
            {
                Logger.Log("ProcessCEMI: {0}", e.Message);
            }
        }