wrapper

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wrapper is our DLMS/COSEM transport layer for IP networks (IEC 62056-4-7:2015, profile IEC 62056-9-7:2013): the wrapper protocol data unit (version, source and destination wPort, length) over TCP, where a WPDU may arrive in any number of TCP packets, and over UDP, where it fills one datagram. Transport is a gosem transport for the DLMS client; ReadWPDU and ParseDatagram serve the push listener and the meter simulator. Clause numbers are those of IEC 62056-4-7 unless stated.

Index

Examples

Constants

View Source
const (
	NoStation        = 0x0000 // client and server
	ClientManagement = 0x0001 // client management process
	PublicClient     = 0x0010 // public client
	ManagementDevice = 0x0001 // management logical device
	AllStation       = 0x007F // server all-station (broadcast)

)

Reserved wPorts (Table 1).

View Source
const HeaderLen = 8

HeaderLen is the wrapper header: four 16-bit fields (5.3.2, Figure 4).

View Source
const MaxAPDU = 0xFFFF

MaxAPDU is the most the 16-bit length field can carry (TCP).

View Source
const MaxUDPAPDU = 65507 - HeaderLen

MaxUDPAPDU is the longest APDU in one UDP datagram over IPv4 (65 507 octets of UDP data less the wrapper header; 5.2.1, Figure 4 NOTE).

View Source
const Port = 4059

Port is the IANA port of DLMS/COSEM, TCP and UDP (IEC 62056-9-7 clause 6 NOTE 1).

View Source
const Version = 0x0001

Version is the wrapper version (5.3.2).

Variables

View Source
var ErrVersion = errors.New("not a wrapper version 1 header")

ErrVersion is returned for a WPDU of another wrapper version, whose header may have another layout (5.3.2).

Functions

func CheckClient

func CheckClient(p int) error

CheckClient reports a client wPort Table 1 does not open: 1 (client management), 0x10 (public) and 0x02 to 0xFF.

func CheckServer

func CheckServer(p int) error

CheckServer reports a server wPort that does not name one logical device: 1 (management logical device) or 0x10 to 0x7E (Table 1).

func Encode

func Encode(src, dst uint16, apdu []byte) ([]byte, error)

Encode prefixes an APDU with the wrapper header.

Example

A GET of the clock (class 8, 0.0.1.0.0.255, attribute 2) from client wPort 16 to the meter's management logical device (wPort 1), framed and read back.

package main

import (
	"bytes"
	"fmt"

	"github.com/cB-Sigma/dlms/wrapper"
)

func main() {
	get := []byte{0xC0, 0x01, 0xC1, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0xFF, 0x02, 0x00}
	wpdu, err := wrapper.Encode(16, 1, get)
	if err != nil {
		panic(err)
	}
	fmt.Printf("header % X\n", wpdu[:wrapper.HeaderLen])
	h, apdu, err := wrapper.ReadWPDU(bytes.NewReader(wpdu))
	fmt.Println(h.Src, h.Dst, len(apdu), err)
}
Output:
header 00 01 00 10 00 01 00 0D
16 1 13 <nil>

Types

type Header struct {
	Version, Src, Dst, Length uint16
}

Header is a wrapper header.

func ParseDatagram

func ParseDatagram(b []byte) (Header, []byte, error)

ParseDatagram reads the WPDU a UDP datagram carries; the APDU fills the rest of the datagram exactly (5.2.1, 5.3.3).

func ReadWPDU

func ReadWPDU(r io.Reader) (Header, []byte, error)

ReadWPDU reads one WPDU from a TCP stream, however the octets arrive: the header, then as many reads as the length needs (6.3.5.4, A.4). A WPDU of another version is read past (its length field is trusted) and returned with ErrVersion.

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport is the client side of the wrapper over TCP or UDP, a gosem transport: each Send is one WPDU; each WPDU received from the server's wPort to ours is delivered whole. WPDUs of another version or for another wPort are discarded (5.3.5, 6.2.5.2).

func New

func New(network, address string, client, server int, timeout time.Duration) (*Transport, error)

New builds a transport to address ("host:port") over "tcp" or "udp".

func (*Transport) Close

func (t *Transport) Close()

Close disconnects and closes the reception channel.

func (*Transport) Connect

func (t *Transport) Connect() error

Connect opens the TCP connection (or binds the UDP socket).

func (*Transport) Discarded

func (t *Transport) Discarded() int

Discarded counts WPDUs dropped for their version, length or wPorts.

func (*Transport) Disconnect

func (t *Transport) Disconnect() error

Disconnect closes the connection and waits for the reader.

func (*Transport) IsConnected

func (t *Transport) IsConnected() bool

IsConnected reports an open connection.

func (*Transport) Send

func (t *Transport) Send(apdu []byte) error

Send sends one APDU in one WPDU. Over TCP the whole WPDU is written however many writes it takes (6.3.5.4); over UDP it must fit one datagram (5.2.1).

func (*Transport) SendBroadcast

func (t *Transport) SendBroadcast(apdu []byte) error

SendBroadcast sends an unconfirmed APDU; over IP it is an ordinary WPDU (IEC 62056-9-7 9.1, the invoke-id's service class bit says it).

func (*Transport) SetAddress

func (t *Transport) SetAddress(client, server int)

SetAddress sets our (client) and the server's wPort.

func (*Transport) SetLogger

func (t *Transport) SetLogger(l *log.Logger)

SetLogger logs APDUs.

func (*Transport) SetReception

func (t *Transport) SetReception(dc gdlms.DataChannel)

SetReception sets where APDUs go.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL