public static ResponseType ProcessCEMI(ref KnxDatagram datagram, byte[] cemi, bool threeLevelGroupAddressing, bool isDebug) { 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.MessageCode = cemi[0]; datagram.AdditionalInfoLength = cemi[1]; if (datagram.AdditionalInfoLength > 0) { datagram.AdditionalInfo = new byte[datagram.AdditionalInfoLength]; for (var i = 0; i < datagram.AdditionalInfoLength; i++) { datagram.AdditionalInfo[i] = cemi[2 + i]; } } datagram.ControlField1 = cemi[2 + datagram.AdditionalInfoLength]; datagram.ControlField2 = cemi[3 + datagram.AdditionalInfoLength]; datagram.SourceAddress = Networking.GetIndividualAddress(new[] { cemi[4 + datagram.AdditionalInfoLength], cemi[5 + datagram.AdditionalInfoLength] }); datagram.DestinationAddress = Networking.GetKnxDestinationAddressType(datagram.ControlField2).Equals(KnxDestinationAddressType.Individual) ? Networking.GetIndividualAddress(new[] { cemi[6 + datagram.AdditionalInfoLength], cemi[7 + datagram.AdditionalInfoLength] }) : Networking.GetGroupAddress(new[] { cemi[6 + datagram.AdditionalInfoLength], cemi[7 + datagram.AdditionalInfoLength] }, threeLevelGroupAddressing); datagram.DataLength = cemi[8 + datagram.AdditionalInfoLength]; datagram.Apdu = new byte[datagram.DataLength + 1]; for (var i = 0; i < datagram.Apdu.Length; i++) { datagram.Apdu[i] = cemi[9 + i + datagram.AdditionalInfoLength]; } datagram.Data = DataProcessing.GetData(datagram.DataLength, datagram.Apdu); if (isDebug) { System.Diagnostics.Debug.WriteLine("-----------------------------------------------------------------------------------------------------"); System.Diagnostics.Debug.WriteLine(BitConverter.ToString(cemi)); System.Diagnostics.Debug.WriteLine("Event Header Length: " + datagram.HeaderLength); System.Diagnostics.Debug.WriteLine("Event Protocol Version: " + datagram.ProtocolVersion.ToString("x")); System.Diagnostics.Debug.WriteLine("Event Service Type: 0x" + BitConverter.ToString(datagram.ServiceType).Replace("-", string.Empty)); System.Diagnostics.Debug.WriteLine("Event Total Length: " + datagram.TotalLength); System.Diagnostics.Debug.WriteLine("Event Message Code: " + datagram.MessageCode.ToString("x")); System.Diagnostics.Debug.WriteLine("Event Aditional Info Length: " + datagram.AdditionalInfoLength); if (datagram.AdditionalInfoLength > 0) { System.Diagnostics.Debug.WriteLine("Event Aditional Info: 0x" + BitConverter.ToString(datagram.AdditionalInfo).Replace("-", string.Empty)); } System.Diagnostics.Debug.WriteLine("Event Control Field 1: " + Convert.ToString(datagram.ControlField1, 2)); System.Diagnostics.Debug.WriteLine("Event Control Field 2: " + Convert.ToString(datagram.ControlField2, 2)); System.Diagnostics.Debug.WriteLine("Event Source Address: " + datagram.SourceAddress); System.Diagnostics.Debug.WriteLine("Event Destination Address: " + datagram.DestinationAddress); System.Diagnostics.Debug.WriteLine("Event Data Length: " + datagram.DataLength); System.Diagnostics.Debug.WriteLine("Event APDU: 0x" + BitConverter.ToString(datagram.Apdu).Replace("-", string.Empty)); System.Diagnostics.Debug.WriteLine("Event Data: " + datagram.Data); System.Diagnostics.Debug.WriteLine("-----------------------------------------------------------------------------------------------------"); } if (datagram.MessageCode != 0x29) { return(ResponseType.Other); } var type = datagram.Apdu[1] >> 4; switch (type) { case 8: return(ResponseType.Event); case 4: return(ResponseType.Status); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } return(ResponseType.Other); }
public static byte[] CreateActionDatagramCommon(string destinationAddress, byte[] data, byte[] header, byte actionMessageCode) { int i; var dataLength = DataProcessing.GetDataLength(data); // HEADER var datagram = new byte[dataLength + 10 + header.Length]; for (i = 0; i < header.Length; i++) { datagram[i] = header[i]; } // CEMI (start at position 6) // +--------+--------+--------+--------+----------------+----------------+--------+----------------+ // | 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[i++] = actionMessageCode != 0x00 ? actionMessageCode : (byte)0x11; datagram[i++] = 0x00; datagram[i++] = 0xAC; datagram[i++] = Networking.IsAddressIndividual(destinationAddress) ? (byte)0x50 : (byte)0xF0; datagram[i++] = 0x00; datagram[i++] = 0x00; var dst_address = Networking.GetAddress(destinationAddress); datagram[i++] = dst_address[0]; datagram[i++] = dst_address[1]; datagram[i++] = (byte)dataLength; datagram[i++] = 0x00; datagram[i] = 0x80; DataProcessing.WriteData(datagram, data, i); return(datagram); }