You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/11/20 13:08:55 UTC

[plc4x] branch feature/plc4go updated (11b542e -> 6581c3a)

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a change to branch feature/plc4go
in repository https://gitbox.apache.org/repos/asf/plc4x.git.


    from 11b542e  - Secured the access to the valueCache in KNX against simultaneous access
     new b663f6b  - Fixed a number of errors in the knx mspec - Fixed a problem in the data-io-template regarding enum values
     new 6581c3a  - Added a map of "attributes" to the PLC4Go Connection's Metadata (Which allows providing protocol specific additional information)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../resources/templates/java/data-io-template.ftlh |   2 +-
 plc4go/cmd/main/drivers/knxnetip_test.go           |   6 +
 .../internal/plc4go/knxnetip/KncNetIpConnection.go | 459 -------------------
 .../internal/plc4go/knxnetip/KnxNetIpConnection.go | 488 +++++++++++++++++++++
 plc4go/internal/plc4go/modbus/ModbusConnection.go  |   5 +
 plc4go/pkg/plc4go/model/plc_connection_metadata.go |   5 +
 .../resources/protocols/knxnetip/knxnetip.mspec    |  78 +++-
 7 files changed, 560 insertions(+), 483 deletions(-)
 delete mode 100644 plc4go/internal/plc4go/knxnetip/KncNetIpConnection.go
 create mode 100644 plc4go/internal/plc4go/knxnetip/KnxNetIpConnection.go


[plc4x] 02/02: - Added a map of "attributes" to the PLC4Go Connection's Metadata (Which allows providing protocol specific additional information)

Posted by cd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/plc4go
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 6581c3ab65c5c2e094ff71ac2922969cd94bc01c
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri Nov 20 14:08:44 2020 +0100

    - Added a map of "attributes" to the PLC4Go Connection's Metadata (Which allows providing protocol specific additional information)
---
 plc4go/cmd/main/drivers/knxnetip_test.go           |   6 +
 .../internal/plc4go/knxnetip/KncNetIpConnection.go | 459 -------------------
 .../internal/plc4go/knxnetip/KnxNetIpConnection.go | 488 +++++++++++++++++++++
 plc4go/internal/plc4go/modbus/ModbusConnection.go  |   5 +
 plc4go/pkg/plc4go/model/plc_connection_metadata.go |   5 +
 5 files changed, 504 insertions(+), 459 deletions(-)

diff --git a/plc4go/cmd/main/drivers/knxnetip_test.go b/plc4go/cmd/main/drivers/knxnetip_test.go
index aaf2268..ee996cc 100644
--- a/plc4go/cmd/main/drivers/knxnetip_test.go
+++ b/plc4go/cmd/main/drivers/knxnetip_test.go
@@ -66,6 +66,12 @@ func TestKnxNetIpPlc4goDriver(t *testing.T) {
     }
     connection := connectionResult.Connection
 
+    attributes := connection.GetMetadata().GetConnectionAttributes()
+    fmt.Printf("Successfully connected to KNXnet/IP Gateway '%s' with KNX address '%s' got assigned client KNX address '%s'\n",
+        attributes["GatewayName"],
+        attributes["GatewayKnxAddress"],
+        attributes["ClientKnxAddress"])
+
     // Try to ping the remote device
     pingResultChannel := connection.Ping()
     pingResult := <-pingResultChannel
diff --git a/plc4go/internal/plc4go/knxnetip/KncNetIpConnection.go b/plc4go/internal/plc4go/knxnetip/KncNetIpConnection.go
deleted file mode 100644
index fcdd4d0..0000000
--- a/plc4go/internal/plc4go/knxnetip/KncNetIpConnection.go
+++ /dev/null
@@ -1,459 +0,0 @@
-//
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied.  See the License for the
-// specific language governing permissions and limitations
-// under the License.
-//
-package knxnetip
-
-import (
-	"bytes"
-	"errors"
-	"fmt"
-	driverModel "github.com/apache/plc4x/plc4go/internal/plc4go/knxnetip/readwrite/model"
-	internalModel "github.com/apache/plc4x/plc4go/internal/plc4go/model"
-	"github.com/apache/plc4x/plc4go/internal/plc4go/spi"
-	"github.com/apache/plc4x/plc4go/internal/plc4go/spi/interceptors"
-	"github.com/apache/plc4x/plc4go/internal/plc4go/transports"
-	"github.com/apache/plc4x/plc4go/internal/plc4go/transports/udp"
-	"github.com/apache/plc4x/plc4go/internal/plc4go/utils"
-	"github.com/apache/plc4x/plc4go/pkg/plc4go"
-	apiModel "github.com/apache/plc4x/plc4go/pkg/plc4go/model"
-	"net"
-	"strconv"
-	"sync"
-	"time"
-)
-
-type ConnectionMetadata struct {
-	apiModel.PlcConnectionMetadata
-}
-
-func (m ConnectionMetadata) CanRead() bool {
-	return true
-}
-
-func (m ConnectionMetadata) CanWrite() bool {
-	return true
-}
-
-func (m ConnectionMetadata) CanSubscribe() bool {
-	return true
-}
-
-type KnxNetIpConnection struct {
-	messageCodec             spi.MessageCodec
-	options                  map[string][]string
-	fieldHandler             spi.PlcFieldHandler
-	valueHandler             spi.PlcValueHandler
-	quitConnectionStateTimer chan struct{}
-	subscribers              []*KnxNetIpSubscriber
-	leve3AddressCache        map[uint16]*driverModel.KnxGroupAddress3Level
-	leve2AddressCache        map[uint16]*driverModel.KnxGroupAddress2Level
-	leve1AddressCache        map[uint16]*driverModel.KnxGroupAddressFreeLevel
-
-	valueCache      map[uint16][]int8
-	valueCacheMutex sync.RWMutex
-
-	GatewayKnxAddress      *driverModel.KnxAddress
-	GatewayName            string
-	ClientKnxAddress       *driverModel.KnxAddress
-	CommunicationChannelId uint8
-
-	requestInterceptor internalModel.RequestInterceptor
-	plc4go.PlcConnection
-}
-
-func NewKnxNetIpConnection(messageCodec spi.MessageCodec, options map[string][]string, fieldHandler spi.PlcFieldHandler) *KnxNetIpConnection {
-	return &KnxNetIpConnection{
-		messageCodec:       messageCodec,
-		options:            options,
-		fieldHandler:       fieldHandler,
-		valueHandler:       NewValueHandler(),
-		requestInterceptor: interceptors.NewSingleItemRequestInterceptor(),
-		subscribers:        []*KnxNetIpSubscriber{},
-		leve3AddressCache:  map[uint16]*driverModel.KnxGroupAddress3Level{},
-		leve2AddressCache:  map[uint16]*driverModel.KnxGroupAddress2Level{},
-		leve1AddressCache:  map[uint16]*driverModel.KnxGroupAddressFreeLevel{},
-		valueCache:         map[uint16][]int8{},
-		valueCacheMutex:    sync.RWMutex{},
-	}
-}
-
-func (m *KnxNetIpConnection) Connect() <-chan plc4go.PlcConnectionConnectResult {
-	ch := make(chan plc4go.PlcConnectionConnectResult)
-	go func() {
-		err := m.messageCodec.Connect()
-		if err != nil {
-			ch <- plc4go.NewPlcConnectionConnectResult(m, err)
-			return
-		}
-
-		transportInstanceExposer, ok := m.messageCodec.(spi.TransportInstanceExposer)
-		if !ok {
-			ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
-				"used transport, is not a TransportInstanceExposer"))
-			return
-		}
-
-		// Prepare a SearchReq
-		udpTransportInstance, ok := transportInstanceExposer.GetTransportInstance().(*udp.UdpTransportInstance)
-		if !ok {
-			ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
-				"used transport, is not a UdpTransportInstance"))
-			return
-		}
-		localAddress := driverModel.NewIPAddress(utils.ByteToInt8(udpTransportInstance.LocalAddress.IP))
-		discoveryEndpoint := driverModel.NewHPAIDiscoveryEndpoint(
-			driverModel.HostProtocolCode_IPV4_UDP, localAddress, uint16(udpTransportInstance.LocalAddress.Port))
-		searchRequest := driverModel.NewSearchRequest(discoveryEndpoint)
-		// Send the SearchReq
-		err = m.messageCodec.Send(searchRequest)
-		if err != nil {
-			ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
-				"error sending search request"))
-			return
-		}
-		// Register an expected response
-		check := func(response interface{}) (bool, bool) {
-			searchResponse := driverModel.CastSearchResponse(response)
-			return searchResponse != nil, false
-		}
-
-		// Create a channel for async execution of the connection
-		connectionResult := make(chan error)
-
-		// Register a callback to handle the response
-		searchResponseChan := m.messageCodec.Expect(check)
-		go func() {
-			select {
-			case response := <-searchResponseChan:
-				searchResponse := driverModel.CastSearchResponse(response)
-				// Check if this device supports tunneling services
-				supportsTunneling := false
-				for _, serviceId := range searchResponse.DibSuppSvcFamilies.ServiceIds {
-					_, ok := serviceId.Child.(*driverModel.KnxNetIpTunneling)
-					if ok {
-						supportsTunneling = true
-						break
-					}
-				}
-				if supportsTunneling {
-					// Save some important information
-					m.GatewayName = string(bytes.Trim(utils.Int8ToByte(
-						searchResponse.DibDeviceInfo.DeviceFriendlyName), "\x00"))
-					m.GatewayKnxAddress = searchResponse.DibDeviceInfo.KnxAddress
-
-					// As soon as we got a successful search-response back, send a connection request.
-					localAddress := m.castIpToKnxAddress(udpTransportInstance.LocalAddress.IP)
-					connectionRequest := driverModel.NewConnectionRequest(
-						driverModel.NewHPAIDiscoveryEndpoint(driverModel.HostProtocolCode_IPV4_UDP,
-							localAddress, uint16(udpTransportInstance.LocalAddress.Port)),
-						driverModel.NewHPAIDataEndpoint(driverModel.HostProtocolCode_IPV4_UDP,
-							localAddress, uint16(udpTransportInstance.LocalAddress.Port)),
-						driverModel.NewConnectionRequestInformationTunnelConnection(driverModel.KnxLayer_TUNNEL_LINK_LAYER),
-					)
-
-					// Send the connection request
-					err = m.messageCodec.Send(connectionRequest)
-					if err != nil {
-						// TODO: Different channel ...
-						ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
-							"error sending connection request"))
-						return
-					}
-					// Register an expected response
-					check := func(response interface{}) (bool, bool) {
-						connectionResponse := driverModel.CastConnectionResponse(response)
-						return connectionResponse != nil, false
-					}
-
-					// Register a callback to handle the response
-					connectionResponseChan := m.messageCodec.Expect(check)
-					go func() {
-						response := <-connectionResponseChan
-						connectionResponse := driverModel.CastConnectionResponse(response)
-						// Save the communication channel id
-						m.CommunicationChannelId = connectionResponse.CommunicationChannelId
-						if connectionResponse.Status == driverModel.Status_NO_ERROR {
-							// Register a listener for incoming tunneling requests
-							checkTunnelReq := func(response interface{}) (bool, bool) {
-								tunnelingRequest := driverModel.CastTunnelingRequest(response)
-								return (tunnelingRequest != nil) &&
-										(tunnelingRequest.TunnelingRequestDataBlock.CommunicationChannelId == m.CommunicationChannelId),
-									true
-							}
-							tunnelingRequestChan := m.messageCodec.Expect(checkTunnelReq)
-							go func() {
-								m.handleIncomingTunnelingRequest(tunnelingRequestChan)
-							}()
-
-							tunnelConnectionDataBlock := connectionResponse.ConnectionResponseDataBlock.Child.(*driverModel.ConnectionResponseDataBlockTunnelConnection)
-							// Save the KNX Address the Gateway assigned to this connection.
-							m.ClientKnxAddress = tunnelConnectionDataBlock.KnxAddress
-
-							fmt.Printf("Successfully connected to KNXnet/IP Gateway '%s' with KNX address '%d.%d.%d' got assigned client KNX address '%d.%d.%d'\n",
-								m.GatewayName,
-								m.GatewayKnxAddress.MainGroup, m.GatewayKnxAddress.MiddleGroup, m.GatewayKnxAddress.SubGroup,
-								m.ClientKnxAddress.MainGroup, m.ClientKnxAddress.MiddleGroup, m.ClientKnxAddress.SubGroup)
-
-							// Fire the "connected" event
-							ch <- plc4go.NewPlcConnectionConnectResult(m, nil)
-
-							// Start a timer that sends connection-state requests every 60 seconds
-							connectionStateTimer := time.NewTicker(60 * time.Second)
-							m.quitConnectionStateTimer = make(chan struct{})
-							go func() {
-								for {
-									select {
-									case <-connectionStateTimer.C:
-										// We're using the connection-state-request as ping operation ...
-										ping := m.Ping()
-										pingResult := <-ping
-										if pingResult.Err != nil {
-											// TODO: Do some error handling here ...
-											connectionStateTimer.Stop()
-										}
-									case <-m.quitConnectionStateTimer:
-										// TODO: Do some error handling here ...
-										connectionStateTimer.Stop()
-										return
-									}
-								}
-							}()
-						} else {
-							ch <- plc4go.NewPlcConnectionConnectResult(m,
-								errors.New("got a connection response with status "+strconv.Itoa(int(connectionResponse.Status))))
-						}
-					}()
-				} else {
-					ch <- plc4go.NewPlcConnectionConnectResult(m,
-						errors.New("this connection doesn't support tunneling"))
-				}
-			case <-time.After(1 * time.Second):
-				ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New("received timeout"))
-			}
-		}()
-		// Wait for the connection to be established
-		err = <-connectionResult
-		ch <- plc4go.NewPlcConnectionConnectResult(m, err)
-	}()
-	return ch
-}
-
-func (m *KnxNetIpConnection) Close() <-chan plc4go.PlcConnectionCloseResult {
-	// TODO: Implement ...
-	ch := make(chan plc4go.PlcConnectionCloseResult)
-	go func() {
-		ch <- plc4go.NewPlcConnectionCloseResult(m, nil)
-	}()
-	return ch
-}
-
-func (m *KnxNetIpConnection) IsConnected() bool {
-	panic("implement me")
-}
-
-func (m *KnxNetIpConnection) Ping() <-chan plc4go.PlcConnectionPingResult {
-	result := make(chan plc4go.PlcConnectionPingResult)
-	//	diagnosticRequestPdu := driverModel.NewModbusPDUDiagnosticRequest(0, 0x42)
-	go func() {
-		transportInstanceExposer, ok := m.messageCodec.(spi.TransportInstanceExposer)
-		if !ok {
-			result <- plc4go.NewPlcConnectionPingResult(errors.New(
-				"used transport, is not a TransportInstanceExposer"))
-			return
-		}
-
-		// Prepare a SearchReq
-		udpTransportInstance, ok := transportInstanceExposer.GetTransportInstance().(*udp.UdpTransportInstance)
-		if !ok {
-			result <- plc4go.NewPlcConnectionPingResult(errors.New(
-				"used transport, is not a UdpTransportInstance"))
-			return
-		}
-
-		localAddress := m.castIpToKnxAddress(udpTransportInstance.LocalAddress.IP)
-
-		connectionStateRequest := driverModel.NewConnectionStateRequest(
-			m.CommunicationChannelId,
-			driverModel.NewHPAIControlEndpoint(
-				driverModel.HostProtocolCode_IPV4_UDP,
-				localAddress, uint16(udpTransportInstance.LocalAddress.Port)))
-
-		// Send the connection state request
-		err := m.messageCodec.Send(connectionStateRequest)
-		if err != nil {
-			result <- plc4go.NewPlcConnectionPingResult(err)
-			return
-		}
-		// Register an expected response
-		check := func(response interface{}) (bool, bool) {
-			connectionStateResponse := driverModel.CastConnectionStateResponse(response)
-			return connectionStateResponse != nil, false
-		}
-
-		// Register a callback to handle the response
-		connectionStateResponseChan := m.messageCodec.Expect(check)
-		go func() {
-			response := <-connectionStateResponseChan
-			connectionStateResponse := driverModel.CastConnectionStateResponse(response)
-			if connectionStateResponse.Status != driverModel.Status_NO_ERROR {
-				result <- plc4go.NewPlcConnectionPingResult(errors.New(
-					"got a failure response code " + strconv.Itoa(int(connectionStateResponse.Status))))
-			} else {
-				result <- plc4go.NewPlcConnectionPingResult(nil)
-			}
-		}()
-	}()
-	return result
-}
-
-func (m *KnxNetIpConnection) GetMetadata() apiModel.PlcConnectionMetadata {
-	return ConnectionMetadata{}
-}
-
-func (m *KnxNetIpConnection) ReadRequestBuilder() apiModel.PlcReadRequestBuilder {
-	return internalModel.NewDefaultPlcReadRequestBuilder(
-		m.fieldHandler, NewKnxNetIpReader(m))
-}
-
-func (m *KnxNetIpConnection) WriteRequestBuilder() apiModel.PlcWriteRequestBuilder {
-	return internalModel.NewDefaultPlcWriteRequestBuilder(
-		m.fieldHandler, m.valueHandler, NewKnxNetIpWriter(m.messageCodec))
-}
-
-func (m *KnxNetIpConnection) SubscriptionRequestBuilder() apiModel.PlcSubscriptionRequestBuilder {
-	return internalModel.NewDefaultPlcSubscriptionRequestBuilder(
-		m.fieldHandler, m.valueHandler, NewKnxNetIpSubscriber(m))
-}
-
-func (m *KnxNetIpConnection) UnsubscriptionRequestBuilder() apiModel.PlcUnsubscriptionRequestBuilder {
-	return nil /*internalModel.NewDefaultPlcUnsubscriptionRequestBuilder(
-	  m.fieldHandler, m.valueHandler, NewKnxNetIpSubscriber(m.messageCodec))*/
-}
-
-func (m *KnxNetIpConnection) GetTransportInstance() transports.TransportInstance {
-	if mc, ok := m.messageCodec.(spi.TransportInstanceExposer); ok {
-		return mc.GetTransportInstance()
-	}
-	return nil
-}
-
-func (m *KnxNetIpConnection) GetPlcFieldHandler() spi.PlcFieldHandler {
-	return m.fieldHandler
-}
-
-func (m *KnxNetIpConnection) GetPlcValueHandler() spi.PlcValueHandler {
-	return m.valueHandler
-}
-
-func (m *KnxNetIpConnection) castIpToKnxAddress(ip net.IP) *driverModel.IPAddress {
-	return driverModel.NewIPAddress(utils.ByteToInt8(ip)[len(ip)-4:])
-}
-
-func (m *KnxNetIpConnection) handleIncomingTunnelingRequest(tunnelingRequestChan chan interface{}) {
-	for {
-		msg := <-tunnelingRequestChan
-		tunnelingRequest := driverModel.CastTunnelingRequest(msg)
-		// Send a response for this message
-		tunnelingResponse := driverModel.NewTunnelingResponse(driverModel.NewTunnelingResponseDataBlock(
-			m.CommunicationChannelId, tunnelingRequest.TunnelingRequestDataBlock.SequenceCounter,
-			driverModel.Status_NO_ERROR))
-		err := m.messageCodec.Send(tunnelingResponse)
-		if err != nil {
-			// TODO: Somehow react on this ...
-			break
-		}
-
-		go func() {
-			cemiDataInd := driverModel.CastCEMIDataInd(tunnelingRequest.Cemi.Child)
-			if cemiDataInd != nil {
-				addressData := uint16(cemiDataInd.CemiDataFrame.DestinationAddress[0])<<8 | (uint16(cemiDataInd.CemiDataFrame.DestinationAddress[1]) & 0xFF)
-				m.valueCacheMutex.RLock()
-				val, ok := m.valueCache[addressData]
-				m.valueCacheMutex.RUnlock()
-				changed := false
-				var payload []int8
-				payload = append(payload, cemiDataInd.CemiDataFrame.DataFirstByte)
-				payload = append(payload, cemiDataInd.CemiDataFrame.Data...)
-				if !ok || !m.sliceEqual(val, payload) {
-					m.valueCacheMutex.Lock()
-					m.valueCache[addressData] = payload
-					m.valueCacheMutex.Unlock()
-					// If this is a new value, we have to also provide the 3 different types of addresses.
-					if !ok {
-						destinationAddress := cemiDataInd.CemiDataFrame.DestinationAddress
-						arb := utils.NewReadBuffer(utils.Int8ToUint8(destinationAddress))
-						if address, err2 := driverModel.KnxGroupAddressParse(arb, 3); err2 == nil {
-							m.leve3AddressCache[addressData] = driverModel.CastKnxGroupAddress3Level(address)
-						}
-						arb.Reset()
-						if address, err2 := driverModel.KnxGroupAddressParse(arb, 2); err2 == nil {
-							m.leve2AddressCache[addressData] = driverModel.CastKnxGroupAddress2Level(address)
-						}
-						arb.Reset()
-						if address, err2 := driverModel.KnxGroupAddressParse(arb, 1); err2 == nil {
-							m.leve1AddressCache[addressData] = driverModel.CastKnxGroupAddressFreeLevel(address)
-						}
-					}
-					changed = true
-				}
-				for _, subscriber := range m.subscribers {
-					subscriber.handleValueChange(cemiDataInd.CemiDataFrame, changed)
-				}
-			}
-		}()
-	}
-}
-
-func (m *KnxNetIpConnection) getGroupAddressNumLevels() uint8 {
-	if val, ok := m.options["group-address-num-levels"]; ok {
-		groupAddressNumLevels, err := strconv.Atoi(val[0])
-		if err == nil {
-			return uint8(groupAddressNumLevels)
-		}
-	}
-	return 3
-}
-
-func (m *KnxNetIpConnection) addSubscriber(subscriber *KnxNetIpSubscriber) {
-	for _, sub := range m.subscribers {
-		if sub == subscriber {
-			return
-		}
-	}
-	m.subscribers = append(m.subscribers, subscriber)
-}
-
-func (m *KnxNetIpConnection) removeSubscriber(subscriber *KnxNetIpSubscriber) {
-	for i, sub := range m.subscribers {
-		if sub == subscriber {
-			m.subscribers = append(m.subscribers[:i], m.subscribers[i+1:]...)
-		}
-	}
-}
-
-func (m *KnxNetIpConnection) sliceEqual(a, b []int8) bool {
-	if len(a) != len(b) {
-		return false
-	}
-	for i, v := range a {
-		if v != b[i] {
-			return false
-		}
-	}
-	return true
-}
diff --git a/plc4go/internal/plc4go/knxnetip/KnxNetIpConnection.go b/plc4go/internal/plc4go/knxnetip/KnxNetIpConnection.go
new file mode 100644
index 0000000..d477016
--- /dev/null
+++ b/plc4go/internal/plc4go/knxnetip/KnxNetIpConnection.go
@@ -0,0 +1,488 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+package knxnetip
+
+import (
+    "bytes"
+    "errors"
+    driverModel "github.com/apache/plc4x/plc4go/internal/plc4go/knxnetip/readwrite/model"
+    internalModel "github.com/apache/plc4x/plc4go/internal/plc4go/model"
+    "github.com/apache/plc4x/plc4go/internal/plc4go/spi"
+    "github.com/apache/plc4x/plc4go/internal/plc4go/spi/interceptors"
+    "github.com/apache/plc4x/plc4go/internal/plc4go/transports"
+    "github.com/apache/plc4x/plc4go/internal/plc4go/transports/udp"
+    "github.com/apache/plc4x/plc4go/internal/plc4go/utils"
+    "github.com/apache/plc4x/plc4go/pkg/plc4go"
+    apiModel "github.com/apache/plc4x/plc4go/pkg/plc4go/model"
+    "net"
+    "reflect"
+    "strconv"
+    "sync"
+    "time"
+)
+
+type ConnectionMetadata struct {
+    KnxMedium         string
+    GatewayName       string
+    GatewayKnxAddress string
+    ClientKnxAddress  string
+
+    ProjectNumber          uint8
+    InstallationNumber     uint8
+    DeviceSerialNumber     []int8
+    DeviceMulticastAddress []int8
+    DeviceMacAddress       []int8
+    SupportedServices      []string
+
+    apiModel.PlcConnectionMetadata
+}
+
+func (m ConnectionMetadata) GetConnectionAttributes() map[string]string {
+    return map[string]string{
+        "GatewayName":       m.GatewayName,
+        "GatewayKnxAddress": m.GatewayKnxAddress,
+        "ClientKnxAddress":  m.ClientKnxAddress,
+    }
+}
+
+func (m ConnectionMetadata) CanRead() bool {
+    return true
+}
+
+func (m ConnectionMetadata) CanWrite() bool {
+    return true
+}
+
+func (m ConnectionMetadata) CanSubscribe() bool {
+    return true
+}
+
+type KnxNetIpConnection struct {
+    messageCodec             spi.MessageCodec
+    options                  map[string][]string
+    fieldHandler             spi.PlcFieldHandler
+    valueHandler             spi.PlcValueHandler
+    quitConnectionStateTimer chan struct{}
+    subscribers              []*KnxNetIpSubscriber
+    leve3AddressCache        map[uint16]*driverModel.KnxGroupAddress3Level
+    leve2AddressCache        map[uint16]*driverModel.KnxGroupAddress2Level
+    leve1AddressCache        map[uint16]*driverModel.KnxGroupAddressFreeLevel
+
+    valueCache      map[uint16][]int8
+    valueCacheMutex sync.RWMutex
+    metadata        *ConnectionMetadata
+
+    GatewayKnxAddress      *driverModel.KnxAddress
+    ClientKnxAddress       *driverModel.KnxAddress
+    CommunicationChannelId uint8
+
+    requestInterceptor internalModel.RequestInterceptor
+    plc4go.PlcConnection
+}
+
+func NewKnxNetIpConnection(messageCodec spi.MessageCodec, options map[string][]string, fieldHandler spi.PlcFieldHandler) *KnxNetIpConnection {
+    return &KnxNetIpConnection{
+        messageCodec:       messageCodec,
+        options:            options,
+        fieldHandler:       fieldHandler,
+        valueHandler:       NewValueHandler(),
+        requestInterceptor: interceptors.NewSingleItemRequestInterceptor(),
+        subscribers:        []*KnxNetIpSubscriber{},
+        leve3AddressCache:  map[uint16]*driverModel.KnxGroupAddress3Level{},
+        leve2AddressCache:  map[uint16]*driverModel.KnxGroupAddress2Level{},
+        leve1AddressCache:  map[uint16]*driverModel.KnxGroupAddressFreeLevel{},
+        valueCache:         map[uint16][]int8{},
+        valueCacheMutex:    sync.RWMutex{},
+        metadata:           &ConnectionMetadata{},
+    }
+}
+
+func (m *KnxNetIpConnection) Connect() <-chan plc4go.PlcConnectionConnectResult {
+    ch := make(chan plc4go.PlcConnectionConnectResult)
+    go func() {
+        err := m.messageCodec.Connect()
+        if err != nil {
+            ch <- plc4go.NewPlcConnectionConnectResult(m, err)
+            return
+        }
+
+        transportInstanceExposer, ok := m.messageCodec.(spi.TransportInstanceExposer)
+        if !ok {
+            ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
+                "used transport, is not a TransportInstanceExposer"))
+            return
+        }
+
+        // Prepare a SearchReq
+        udpTransportInstance, ok := transportInstanceExposer.GetTransportInstance().(*udp.UdpTransportInstance)
+        if !ok {
+            ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
+                "used transport, is not a UdpTransportInstance"))
+            return
+        }
+        localAddress := driverModel.NewIPAddress(utils.ByteToInt8(udpTransportInstance.LocalAddress.IP))
+        discoveryEndpoint := driverModel.NewHPAIDiscoveryEndpoint(
+            driverModel.HostProtocolCode_IPV4_UDP, localAddress, uint16(udpTransportInstance.LocalAddress.Port))
+        searchRequest := driverModel.NewSearchRequest(discoveryEndpoint)
+        // Send the SearchReq
+        err = m.messageCodec.Send(searchRequest)
+        if err != nil {
+            ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
+                "error sending search request"))
+            return
+        }
+        // Register an expected response
+        check := func(response interface{}) (bool, bool) {
+            searchResponse := driverModel.CastSearchResponse(response)
+            return searchResponse != nil, false
+        }
+
+        // Create a channel for async execution of the connection
+        connectionResult := make(chan error)
+
+        // Register a callback to handle the response
+        searchResponseChan := m.messageCodec.Expect(check)
+        go func() {
+            select {
+            case response := <-searchResponseChan:
+                searchResponse := driverModel.CastSearchResponse(response)
+                // Check if this device supports tunneling services
+                supportsTunneling := false
+                for _, serviceId := range searchResponse.DibSuppSvcFamilies.ServiceIds {
+                    _, ok := serviceId.Child.(*driverModel.KnxNetIpTunneling)
+                    if ok {
+                        supportsTunneling = true
+                        break
+                    }
+                }
+                if supportsTunneling {
+                    // Save some important information
+                    m.metadata.GatewayName = string(bytes.Trim(utils.Int8ToByte(
+                        searchResponse.DibDeviceInfo.DeviceFriendlyName), "\x00"))
+                    m.metadata.ProjectNumber = searchResponse.DibDeviceInfo.ProjectInstallationIdentifier.ProjectNumber
+                    m.metadata.InstallationNumber = searchResponse.DibDeviceInfo.ProjectInstallationIdentifier.InstallationNumber
+                    m.metadata.DeviceSerialNumber = searchResponse.DibDeviceInfo.KnxNetIpDeviceSerialNumber
+                    m.metadata.DeviceMulticastAddress = searchResponse.DibDeviceInfo.KnxNetIpDeviceMulticastAddress.Addr
+                    m.metadata.DeviceMacAddress = searchResponse.DibDeviceInfo.KnxNetIpDeviceMacAddress.Addr
+                    m.metadata.SupportedServices = []string{}
+                    for _, serviceId := range searchResponse.DibSuppSvcFamilies.ServiceIds {
+                        m.metadata.SupportedServices = append(m.metadata.SupportedServices, reflect.TypeOf(serviceId).Name())
+                    }
+                    m.GatewayKnxAddress = searchResponse.DibDeviceInfo.KnxAddress
+
+                    // As soon as we got a successful search-response back, send a connection request.
+                    localAddress := m.castIpToKnxAddress(udpTransportInstance.LocalAddress.IP)
+                    connectionRequest := driverModel.NewConnectionRequest(
+                        driverModel.NewHPAIDiscoveryEndpoint(driverModel.HostProtocolCode_IPV4_UDP,
+                            localAddress, uint16(udpTransportInstance.LocalAddress.Port)),
+                        driverModel.NewHPAIDataEndpoint(driverModel.HostProtocolCode_IPV4_UDP,
+                            localAddress, uint16(udpTransportInstance.LocalAddress.Port)),
+                        driverModel.NewConnectionRequestInformationTunnelConnection(driverModel.KnxLayer_TUNNEL_LINK_LAYER),
+                    )
+
+                    // Send the connection request
+                    err = m.messageCodec.Send(connectionRequest)
+                    if err != nil {
+                        // TODO: Different channel ...
+                        ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New(
+                            "error sending connection request"))
+                        return
+                    }
+                    // Register an expected response
+                    check := func(response interface{}) (bool, bool) {
+                        connectionResponse := driverModel.CastConnectionResponse(response)
+                        return connectionResponse != nil, false
+                    }
+
+                    // Register a callback to handle the response
+                    connectionResponseChan := m.messageCodec.Expect(check)
+                    go func() {
+                        response := <-connectionResponseChan
+                        connectionResponse := driverModel.CastConnectionResponse(response)
+                        // Save the communication channel id
+                        m.CommunicationChannelId = connectionResponse.CommunicationChannelId
+                        if connectionResponse.Status == driverModel.Status_NO_ERROR {
+                            // Register a listener for incoming tunneling requests
+                            checkTunnelReq := func(response interface{}) (bool, bool) {
+                                tunnelingRequest := driverModel.CastTunnelingRequest(response)
+                                return (tunnelingRequest != nil) &&
+                                        (tunnelingRequest.TunnelingRequestDataBlock.CommunicationChannelId == m.CommunicationChannelId),
+                                    true
+                            }
+                            tunnelingRequestChan := m.messageCodec.Expect(checkTunnelReq)
+                            go func() {
+                                m.handleIncomingTunnelingRequest(tunnelingRequestChan)
+                            }()
+
+                            tunnelConnectionDataBlock := connectionResponse.ConnectionResponseDataBlock.Child.(*driverModel.ConnectionResponseDataBlockTunnelConnection)
+                            // Save the KNX Address the Gateway assigned to this connection.
+                            m.ClientKnxAddress = tunnelConnectionDataBlock.KnxAddress
+
+                            // Fire the "connected" event
+                            ch <- plc4go.NewPlcConnectionConnectResult(m, nil)
+
+                            // Start a timer that sends connection-state requests every 60 seconds
+                            connectionStateTimer := time.NewTicker(60 * time.Second)
+                            m.quitConnectionStateTimer = make(chan struct{})
+                            go func() {
+                                for {
+                                    select {
+                                    case <-connectionStateTimer.C:
+                                        // We're using the connection-state-request as ping operation ...
+                                        ping := m.Ping()
+                                        pingResult := <-ping
+                                        if pingResult.Err != nil {
+                                            // TODO: Do some error handling here ...
+                                            connectionStateTimer.Stop()
+                                        }
+                                    case <-m.quitConnectionStateTimer:
+                                        // TODO: Do some error handling here ...
+                                        connectionStateTimer.Stop()
+                                        return
+                                    }
+                                }
+                            }()
+                        } else {
+                            ch <- plc4go.NewPlcConnectionConnectResult(m,
+                                errors.New("got a connection response with status "+strconv.Itoa(int(connectionResponse.Status))))
+                        }
+                    }()
+                } else {
+                    ch <- plc4go.NewPlcConnectionConnectResult(m,
+                        errors.New("this connection doesn't support tunneling"))
+                }
+            case <-time.After(1 * time.Second):
+                ch <- plc4go.NewPlcConnectionConnectResult(m, errors.New("request timed out"))
+            }
+        }()
+        // Wait for the connection to be established
+        err = <-connectionResult
+        ch <- plc4go.NewPlcConnectionConnectResult(m, err)
+    }()
+    return ch
+}
+
+func (m *KnxNetIpConnection) Close() <-chan plc4go.PlcConnectionCloseResult {
+    // TODO: Implement ...
+    ch := make(chan plc4go.PlcConnectionCloseResult)
+    go func() {
+        ch <- plc4go.NewPlcConnectionCloseResult(m, nil)
+    }()
+    return ch
+}
+
+func (m *KnxNetIpConnection) IsConnected() bool {
+    panic("implement me")
+}
+
+func (m *KnxNetIpConnection) Ping() <-chan plc4go.PlcConnectionPingResult {
+    result := make(chan plc4go.PlcConnectionPingResult)
+    //	diagnosticRequestPdu := driverModel.NewModbusPDUDiagnosticRequest(0, 0x42)
+    go func() {
+        transportInstanceExposer, ok := m.messageCodec.(spi.TransportInstanceExposer)
+        if !ok {
+            result <- plc4go.NewPlcConnectionPingResult(errors.New(
+                "used transport, is not a TransportInstanceExposer"))
+            return
+        }
+
+        // Prepare a SearchReq
+        udpTransportInstance, ok := transportInstanceExposer.GetTransportInstance().(*udp.UdpTransportInstance)
+        if !ok {
+            result <- plc4go.NewPlcConnectionPingResult(errors.New(
+                "used transport, is not a UdpTransportInstance"))
+            return
+        }
+
+        localAddress := m.castIpToKnxAddress(udpTransportInstance.LocalAddress.IP)
+
+        connectionStateRequest := driverModel.NewConnectionStateRequest(
+            m.CommunicationChannelId,
+            driverModel.NewHPAIControlEndpoint(
+                driverModel.HostProtocolCode_IPV4_UDP,
+                localAddress, uint16(udpTransportInstance.LocalAddress.Port)))
+
+        // Send the connection state request
+        err := m.messageCodec.Send(connectionStateRequest)
+        if err != nil {
+            result <- plc4go.NewPlcConnectionPingResult(err)
+            return
+        }
+        // Register an expected response
+        check := func(response interface{}) (bool, bool) {
+            connectionStateResponse := driverModel.CastConnectionStateResponse(response)
+            return connectionStateResponse != nil, false
+        }
+
+        // Register a callback to handle the response
+        connectionStateResponseChan := m.messageCodec.Expect(check)
+        go func() {
+            response := <-connectionStateResponseChan
+            connectionStateResponse := driverModel.CastConnectionStateResponse(response)
+            if connectionStateResponse.Status != driverModel.Status_NO_ERROR {
+                result <- plc4go.NewPlcConnectionPingResult(errors.New(
+                    "got a failure response code " + strconv.Itoa(int(connectionStateResponse.Status))))
+            } else {
+                result <- plc4go.NewPlcConnectionPingResult(nil)
+            }
+        }()
+    }()
+    return result
+}
+
+func (m *KnxNetIpConnection) GetMetadata() apiModel.PlcConnectionMetadata {
+    return m.metadata
+}
+
+func (m *KnxNetIpConnection) ReadRequestBuilder() apiModel.PlcReadRequestBuilder {
+    return internalModel.NewDefaultPlcReadRequestBuilder(
+        m.fieldHandler, NewKnxNetIpReader(m))
+}
+
+func (m *KnxNetIpConnection) WriteRequestBuilder() apiModel.PlcWriteRequestBuilder {
+    return internalModel.NewDefaultPlcWriteRequestBuilder(
+        m.fieldHandler, m.valueHandler, NewKnxNetIpWriter(m.messageCodec))
+}
+
+func (m *KnxNetIpConnection) SubscriptionRequestBuilder() apiModel.PlcSubscriptionRequestBuilder {
+    return internalModel.NewDefaultPlcSubscriptionRequestBuilder(
+        m.fieldHandler, m.valueHandler, NewKnxNetIpSubscriber(m))
+}
+
+func (m *KnxNetIpConnection) UnsubscriptionRequestBuilder() apiModel.PlcUnsubscriptionRequestBuilder {
+    return nil /*internalModel.NewDefaultPlcUnsubscriptionRequestBuilder(
+      m.fieldHandler, m.valueHandler, NewKnxNetIpSubscriber(m.messageCodec))*/
+}
+
+func (m *KnxNetIpConnection) GetTransportInstance() transports.TransportInstance {
+    if mc, ok := m.messageCodec.(spi.TransportInstanceExposer); ok {
+        return mc.GetTransportInstance()
+    }
+    return nil
+}
+
+func (m *KnxNetIpConnection) GetPlcFieldHandler() spi.PlcFieldHandler {
+    return m.fieldHandler
+}
+
+func (m *KnxNetIpConnection) GetPlcValueHandler() spi.PlcValueHandler {
+    return m.valueHandler
+}
+
+func (m *KnxNetIpConnection) castIpToKnxAddress(ip net.IP) *driverModel.IPAddress {
+    return driverModel.NewIPAddress(utils.ByteToInt8(ip)[len(ip)-4:])
+}
+
+func (m *KnxNetIpConnection) handleIncomingTunnelingRequest(tunnelingRequestChan chan interface{}) {
+    for {
+        msg := <-tunnelingRequestChan
+        tunnelingRequest := driverModel.CastTunnelingRequest(msg)
+        // Send a response for this message
+        tunnelingResponse := driverModel.NewTunnelingResponse(driverModel.NewTunnelingResponseDataBlock(
+            m.CommunicationChannelId, tunnelingRequest.TunnelingRequestDataBlock.SequenceCounter,
+            driverModel.Status_NO_ERROR))
+        err := m.messageCodec.Send(tunnelingResponse)
+        if err != nil {
+            // TODO: Somehow react on this ...
+            break
+        }
+
+        go func() {
+            cemiDataInd := driverModel.CastCEMIDataInd(tunnelingRequest.Cemi.Child)
+            if cemiDataInd != nil {
+                addressData := uint16(cemiDataInd.CemiDataFrame.DestinationAddress[0])<<8 | (uint16(cemiDataInd.CemiDataFrame.DestinationAddress[1]) & 0xFF)
+                m.valueCacheMutex.RLock()
+                val, ok := m.valueCache[addressData]
+                m.valueCacheMutex.RUnlock()
+                changed := false
+                var payload []int8
+                payload = append(payload, cemiDataInd.CemiDataFrame.DataFirstByte)
+                payload = append(payload, cemiDataInd.CemiDataFrame.Data...)
+                if !ok || !m.sliceEqual(val, payload) {
+                    m.valueCacheMutex.Lock()
+                    m.valueCache[addressData] = payload
+                    m.valueCacheMutex.Unlock()
+                    // If this is a new value, we have to also provide the 3 different types of addresses.
+                    if !ok {
+                        destinationAddress := cemiDataInd.CemiDataFrame.DestinationAddress
+                        arb := utils.NewReadBuffer(utils.Int8ToUint8(destinationAddress))
+                        if address, err2 := driverModel.KnxGroupAddressParse(arb, 3); err2 == nil {
+                            m.leve3AddressCache[addressData] = driverModel.CastKnxGroupAddress3Level(address)
+                        }
+                        arb.Reset()
+                        if address, err2 := driverModel.KnxGroupAddressParse(arb, 2); err2 == nil {
+                            m.leve2AddressCache[addressData] = driverModel.CastKnxGroupAddress2Level(address)
+                        }
+                        arb.Reset()
+                        if address, err2 := driverModel.KnxGroupAddressParse(arb, 1); err2 == nil {
+                            m.leve1AddressCache[addressData] = driverModel.CastKnxGroupAddressFreeLevel(address)
+                        }
+                    }
+                    changed = true
+                }
+                for _, subscriber := range m.subscribers {
+                    subscriber.handleValueChange(cemiDataInd.CemiDataFrame, changed)
+                }
+            }
+        }()
+    }
+}
+
+func (m *KnxNetIpConnection) getGroupAddressNumLevels() uint8 {
+    if val, ok := m.options["group-address-num-levels"]; ok {
+        groupAddressNumLevels, err := strconv.Atoi(val[0])
+        if err == nil {
+            return uint8(groupAddressNumLevels)
+        }
+    }
+    return 3
+}
+
+func (m *KnxNetIpConnection) addSubscriber(subscriber *KnxNetIpSubscriber) {
+    for _, sub := range m.subscribers {
+        if sub == subscriber {
+            return
+        }
+    }
+    m.subscribers = append(m.subscribers, subscriber)
+}
+
+func (m *KnxNetIpConnection) removeSubscriber(subscriber *KnxNetIpSubscriber) {
+    for i, sub := range m.subscribers {
+        if sub == subscriber {
+            m.subscribers = append(m.subscribers[:i], m.subscribers[i+1:]...)
+        }
+    }
+}
+
+func (m *KnxNetIpConnection) sliceEqual(a, b []int8) bool {
+    if len(a) != len(b) {
+        return false
+    }
+    for i, v := range a {
+        if v != b[i] {
+            return false
+        }
+    }
+    return true
+}
+
+func (m *KnxNetIpConnection) knxAddressToString(knxAddress *driverModel.KnxAddress) string {
+    return strconv.Itoa(int(knxAddress.MainGroup)) + "." + strconv.Itoa(int(knxAddress.MiddleGroup)) + "." + strconv.Itoa(int(knxAddress.SubGroup))
+}
diff --git a/plc4go/internal/plc4go/modbus/ModbusConnection.go b/plc4go/internal/plc4go/modbus/ModbusConnection.go
index 4fa39ef..cd776d3 100644
--- a/plc4go/internal/plc4go/modbus/ModbusConnection.go
+++ b/plc4go/internal/plc4go/modbus/ModbusConnection.go
@@ -33,6 +33,11 @@ type ConnectionMetadata struct {
     apiModel.PlcConnectionMetadata
 }
 
+func (m ConnectionMetadata) GetConnectionAttributes() map[string]string {
+    return map[string]string {
+    }
+}
+
 func (m ConnectionMetadata) CanRead() bool {
 	return true
 }
diff --git a/plc4go/pkg/plc4go/model/plc_connection_metadata.go b/plc4go/pkg/plc4go/model/plc_connection_metadata.go
index c7e6f6b..382f47d 100644
--- a/plc4go/pkg/plc4go/model/plc_connection_metadata.go
+++ b/plc4go/pkg/plc4go/model/plc_connection_metadata.go
@@ -21,10 +21,15 @@ package model
 // Information about connection capabilities.
 // This includes connection and driver specific metadata.
 type PlcConnectionMetadata interface {
+
+    // Gives access to a map of additional information the driver might be able to provide.
+    GetConnectionAttributes() map[string]string
+
 	// Indicates that the connection supports reading.
 	CanRead() bool
 	// Indicates that the connection supports writing.
 	CanWrite() bool
 	// Indicates that the connection supports subscription.
 	CanSubscribe() bool
+
 }


[plc4x] 01/02: - Fixed a number of errors in the knx mspec - Fixed a problem in the data-io-template regarding enum values

Posted by cd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/plc4go
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit b663f6b6fe539ba55d5532a5595def2b2f98293d
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri Nov 20 14:07:38 2020 +0100

    - Fixed a number of errors in the knx mspec
    - Fixed a problem in the data-io-template regarding enum values
---
 .../resources/templates/java/data-io-template.ftlh |  2 +-
 .../resources/protocols/knxnetip/knxnetip.mspec    | 78 +++++++++++++++-------
 2 files changed, 56 insertions(+), 24 deletions(-)

diff --git a/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh b/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh
index 2a123a8..0f36832 100644
--- a/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh
+++ b/build-utils/language-java/src/main/resources/templates/java/data-io-template.ftlh
@@ -161,7 +161,7 @@ public class ${type.name}IO {
                     <#case "enum">
 
             // Enum field (${field.name})
-            ${helper.getNonPrimitiveLanguageTypeNameForField(field)} ${field.name} = ${helper.getNonPrimitiveLanguageTypeNameForField(field)}.valueOf(${helper.getReadBufferReadMethodCall(helper.getEnumBaseTypeReference(field.type))});
+            ${helper.getNonPrimitiveLanguageTypeNameForField(field)} ${field.name} = ${helper.getNonPrimitiveLanguageTypeNameForField(field)}.enumForValue(${helper.getReadBufferReadMethodCall(helper.getEnumBaseTypeReference(field.type))});
                         <#if field.name == "value">
                             <#assign valueDefined=true>
                         </#if>
diff --git a/protocols/knxnetip/src/main/resources/protocols/knxnetip/knxnetip.mspec b/protocols/knxnetip/src/main/resources/protocols/knxnetip/knxnetip.mspec
index a4c5ca0..ae59941 100644
--- a/protocols/knxnetip/src/main/resources/protocols/knxnetip/knxnetip.mspec
+++ b/protocols/knxnetip/src/main/resources/protocols/knxnetip/knxnetip.mspec
@@ -106,7 +106,7 @@
 [type 'DIBDeviceInfo'
     [implicit uint 8       'structureLength' 'lengthInBytes']
     [simple   uint 8       'descriptionType']
-    [simple   uint 8       'knxMedium']
+    [enum     KnxMedium    'knxMedium']
     [simple   DeviceStatus 'deviceStatus']
     [simple   KnxAddress   'knxAddress']
     [simple   ProjectInstallationIdentifier 'projectInstallationIdentifier']
@@ -230,7 +230,7 @@
     ]
 ]
 
-/* The CEMI part is described in the document "03_06_03 EMI_IMI v01.03.03 AS" */
+// The CEMI part is described in the document "03_06_03 EMI_IMI v01.03.03 AS"
 [discriminatedType 'CEMI' [uint 8 'size']
     [discriminator uint 8 'messageCode']
     [typeSwitch 'messageCode'
@@ -326,8 +326,8 @@
     [array         int 8        'data' count 'dataLength - 1']
 ]
 
-/* The CEMI part is described in the document "03_06_03 EMI_IMI v01.03.03 AS" Page 73
-"03_02_02 Communication Medium TP1 v01.02.02 AS" Page 27 */
+// The CEMI part is described in the document "03_06_03 EMI_IMI v01.03.03 AS" Page 73
+// "03_02_02 Communication Medium TP1 v01.02.02 AS" Page 27
 [discriminatedType 'CEMIFrame'
     [discriminator bit          'standardFrame']
     [discriminator bit          'polling']
@@ -440,9 +440,10 @@
             [reserved uint 8  '0x0']
             [simple   uint 16 'value']
         ]
-        ['V16' INT]
+        ['V16' INT
             [reserved uint 8  '0x0']
             [simple   int 16 'value']
+        ]
         ['F16' REAL
             [reserved uint 8     '0x0']
             [simple   float 4.11 'value']
@@ -461,6 +462,7 @@
             [reserved uint 3 '0x00']
             [simple   uint 5 'day']
             [reserved uint 4 '0x00']
+            [reserved uint 4 '0x00']
             [simple   uint 4 'month']
             [reserved uint 1 '0x00']
             [simple   uint 7 'year']
@@ -1020,12 +1022,11 @@
             [simple   uint 8  'statusOrCommand']
         ]
         ['U16U32U8N8' Struct
-            [reserved uint 8  '0x0']
-            [simple   uint 16  'manufacturerId']
-            [simple   uint 32  'identNumber']
-            [simple   uint 8   'version']
-            // TODO This should be an enum
-            [simple   int  8   'medium']
+            [reserved uint 8                    '0x0']
+            [simple   uint 16                   'manufacturerId']
+            [simple   uint 32                   'identNumber']
+            [simple   uint 8                    'version']
+            [enum     SupportedPhysicalMedia    'medium']
         ]
         ['A8A8A8A8' Struct
             [reserved uint 8  '0x0']
@@ -1498,24 +1499,31 @@
     ['0x02' IPV4_TCP]
 ]
 
-/*
- The mode in which the connection should be established:
- TUNNEL_LINK_LAYER: The gateway assigns a unique KNX address to the client.
-                    The client can then actively participate in communicating
-                    with other KNX devices.
- TUNNEL_RAW:        The gateway will just pass along the packets and not
-                    automatically generate Ack frames for the packets it
-                    receives for a given client.
- TUNNEL_BUSMONITOR: The client becomes a passive participant and all frames
-                    on the KNX bus get forwarded to the client. Only one
-                    Busmonitor connection is allowed at any given time.
-*/
+// The mode in which the connection should be established:
+// TUNNEL_LINK_LAYER The gateway assigns a unique KNX address to the client.
+//                   The client can then actively participate in communicating
+//                   with other KNX devices.
+// TUNNEL_RAW        The gateway will just pass along the packets and not
+//                   automatically generate Ack frames for the packets it
+//                   receives for a given client.
+// TUNNEL_BUSMONITOR The client becomes a passive participant and all frames
+//                   on the KNX bus get forwarded to the client. Only one
+//                   Busmonitor connection is allowed at any given time.
 [enum uint 8 'KnxLayer'
     ['0x02' TUNNEL_LINK_LAYER]
     ['0x04' TUNNEL_RAW]
     ['0x80' TUNNEL_BUSMONITOR]
 ]
 
+[enum uint 8 'KnxMedium'
+    ['0x01' MEDIUM_RESERVED_1]
+    ['0x02' MEDIUM_TP1]
+    ['0x04' MEDIUM_PL110]
+    ['0x08' MEDIUM_RESERVED_2]
+    ['0x10' MEDIUM_RF]
+    ['0x20' MEDIUM_KNX_IP]
+]
+
 [enum uint 2 'TPCI'
     ['0x0' UNNUMBERED_DATA_PACKET]
     ['0x1' UNNUMBERED]
@@ -1542,4 +1550,28 @@
     ['0xF' OTHER_PDU]
 ]
 
+[enum uint 8 'SupportedPhysicalMedia' [string 'description',                                                    bit 'knxSupport']
+    ['0x00' OTHER                     ['used_for_undefined_physical_medium',                                    'true']]
+    ['0x01' OIL_METER                 ['measures_volume_of_oil',                                                'true']]
+    ['0x02' ELECTRICITY_METER         ['measures_electric_energy',                                              'true']]
+    ['0x03' GAS_METER                 ['measures_volume_of_gaseous_energy',                                     'true']]
+    ['0x04' HEAT_METER                ['heat_energy_measured_in_outlet_pipe',                                   'true']]
+    ['0x05' STEAM_METER               ['measures_weight_of_hot_steam',                                          'true']]
+    ['0x06' WARM_WATER_METER          ['measured_heated_water_volume',                                          'true']]
+    ['0x07' WATER_METER               ['measured_water_volume',                                                 'true']]
+    ['0x08' HEAT_COST_ALLOCATOR       ['measured_relative_cumulated_heat_consumption',                          'true']]
+    ['0x09' COMPRESSED_AIR            ['measures_weight_of_compressed_air',                                     'false']]
+    ['0x0A' COOLING_LOAD_METER_INLET  ['cooling_energy_measured_in_inlet_pipe',                                 'true']]
+    ['0x0B' COOLING_LOAD_METER_OUTLET ['cooling_energy_measured_in_outlet_pipe',                                'true']]
+    ['0x0C' HEAT_INLET                ['heat_energy_measured_in_inlet_pipe',                                    'true']]
+    ['0x0D' HEAT_AND_COOL             ['measures_both_heat_and_cool',                                           'true']]
+    ['0x0E' BUS_OR_SYSTEM             ['no_meter',                                                              'false']]
+    ['0x0F' UNKNOWN_DEVICE_TYPE       ['used_for_undefined_physical_medium',                                    'false']]
+    ['0x20' BREAKER                   ['status_of_electric_energy_supply',                                      'true']]
+    ['0x21' VALVE                     ['status_of_supply_of_Gas_or_water',                                      'true']]
+    ['0x28' WASTE_WATER_METER         ['measured_volume_of_disposed_water',                                     'true']]
+    ['0x29' GARBAGE                   ['measured_weight_of_disposed_rubbish',                                   'true']]
+    ['0x37' RADIO_CONVERTER           ['enables_the_radio_transmission_of_a_meter_without_a_radio_interface',   'false']]
+]
+