dlms

module
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

README

dlms

A DLMS/COSEM client for smart meters in pure Go, with HDLC, the wrapper, IEC 62056-21, DLMS over NB-IoT and LoRaWAN, OBIS and India's IS 15959 catalogue.

Go Reference test Go

The client associates with no, low or high-level security (mechanisms 2 to 7, security suites 0 to 2, every ciphering form, general signing, third-party protection, V.44 compression), reads, writes and acts with block transfer, reads profiles by range, opens pushes, reaches meters behind data concentrators, and keeps invocation counters across restarts. Below it are the IEC 62056-46 HDLC data link, the IEC 62056-42 physical layer with Hayes modems, the IEC 62056-4-7 wrapper over TCP and UDP, the gateway prefix, and a LoRaWAN bridge with SCHC. The A-XDR and xDLMS APDU encodings come from gosem (MIT); security, transports and everything above are this module's. No cgo. It was written for SigmaM2M, a oneM2M platform, where it serves its DLMS/COSEM, IEC 62056-21 and M-Bus integrations, and has no dependency on it.

Features

Area Implemented Not implemented
Client (IEC 62056-5-3:2023, Green Book) Logical name referencing, with or without the ciphering application context (applicationContext: automatic, ln or ln-ciphering); mechanisms 0 to 7; suites 0, 1 and 2; authentication, encryption or both; global and dedicated keys; service-specific, general-glo, general-ded and general ciphering (identified, wrapped, one-pass and static agreed keys); general signing; the broadcast key and unconfirmed broadcast associations; third-party protection; V.44 compression; ciphered release; replay checks; key transfer and agreement, key pairs and certificates of class 64; pushes (data-notification, general-glo, general block transfer, HDLC; encryption-only pushes only on opt-in); data concentrators in gateway and logical-device mode; profile reading by range; identity check; a crypto policy hook at association time Short name referencing; mechanism 2 is the common AES-ECB reading and is manufacturer specific (confirm per make); image transfer and key rotation as state machines (only their protocol steps are here)
HDLC (IEC 62056-46, ISO/IEC 13239) Frame format type 3; 1-, 2- and 4-octet addresses; SNRM/UA negotiation (information fields up to 2030, windows up to 7); segmentation both ways; RR polling; RNR; FRMR; DM; UI frames; the inactivity time-out; multi-drop lines on one connection Modulo 128 numbering; sending segmented UI frames (they are accepted)
Physical layer (IEC 62056-42) PH services over a TCP connection to a converter; Hayes dialling, answering, escape and hang-up; the identification service Collisions of several meters calling at once
Wrapper (IEC 62056-4-7, 62056-9-7) Header checks; whole WPDUs from TCP however they are split; one WPDU per UDP datagram; reserved wPorts
Gateway (IEC 62056-5-3 annex C) E6/E7 prefix with network ID and physical address Vendor concentrator variants; the IEC 62056-8-20 mesh
NB-IoT and LoRaWAN UDP from behind carrier NAT with reply windows, sleeping meters, a command queue; SCHC over LoRaWAN (RFC 8724, RFC 9011) with settable FPorts, rules and header; regional limits The DLMS UA LoRaWAN profile is not public: its choices are settings; network servers are the application's (NetworkServer)
IEC 62056-21 Modes A to E, programming mode, annex C formatted codes, annex B wake-ups, mode D, the mode E hand-over to HDLC; 7E1 emulated through converters Baud rates above 19200
OBIS (IEC 62056-6-1:2023, EN 13757-1:2021) Value groups A to F with every range, abstract, electricity and media 4 to 9 codes, the COSEM objects of 62056-6-2 with their classes, reduced ID codes, point names that never collide Names for manufacturer, utility and country specific codes (they keep their code)
IS 15959 (India) Parts 1 (categories A, B, C), 2 (single- and three-phase, with Amendments 1 and 2) and 3 (CT and CT/PT): objects, rights per association, profiles, events, ESW bits, push setups, associations, commands; an IS 16444 event classification The special days table

Choosing a DLMS/COSEM library

Compared from each project's source at the versions named, in October 2026; "tested" means our interop tests ran it (see below). Cells marked "not checked" were not verified in the source. Check the projects themselves before relying on a row.

this module gosem v1.3.5 (Go) Gurux.DLMS.Java (gurux.dlms 4.0.95) Gurux.DLMS.Python (gurux-dlms 1.0.203)
Licence Apache-2.0 MIT GPL-2.0 or a commercial licence GPL-2.0 or a commercial licence
Client / server client (simulated meters in dlms-sim) client both (tested as a server) both (tested as a client)
Referencing logical name logical name logical and short name logical and short name
Transports HDLC (with modems and multi-drop), wrapper over TCP and UDP, serial ports, IEC 62056-21 mode E, the gateway prefix, NB-IoT and LoRaWAN with SCHC HDLC, wrapper, TCP, serial port HDLC and wrapper (tested); others not checked HDLC and wrapper (tested); others not checked
Low-level security, HLS mechanism 2 yes (tested both ways) low-level only: no HLS passes 3 and 4 in its client yes (tested) yes (tested)
HLS-GMAC (mechanism 5), suite 0 ciphering yes (tested both ways) ciphering of suite 0 APDUs; no GMAC pass 3 and 4 yes (tested) yes (tested)
Mechanisms 3, 4, 6, 7; suites 1 and 2; general ciphering and signing; third party; compression yes no not checked not checked
Block transfer, profile by range yes (tested) yes yes (tested) yes (tested)
Pushes, data concentrators, invocation counters kept across restarts yes data notifications received not checked not checked
OBIS model and IS 15959 catalogue yes no object model of the classes; no IS 15959 catalogue object model of the classes; no IS 15959 catalogue
cgo / runtime none, pure Go none, pure Go JVM Python

This module builds on gosem for the A-XDR and xDLMS APDU encodings and adds everything above them. Pick it for a Go head end that must talk to Indian (IS 15959) or other secured meters over HDLC, TCP, UDP, NB-IoT or LoRaWAN without a JVM or a GPL dependency. Pick Gurux when you need a DLMS server as well as a client, short name referencing, or the most widely deployed implementation with commercial support; pick gosem alone for unsecured or low-level-security reads where a small dependency matters most.

Install

go get github.com/cB-Sigma/dlms@latest

Requires Go 1.25 or later.

Quick start

import (
	gdlms "gitlab.com/circutor-library/gosem/pkg/dlms"

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

sec := cosem.SecurityConfig{Name: "M1", Authentication: "hls-gmac", Security: "authenticated-encrypted",
	BlockCipherKey: "...", AuthenticationKey: "...", DedicatedKey: true}
hls, _, err := sec.Build()
r := &cosem.ICReservation{Store: myStore, Name: "M1"} // an iotsec.Store: counters survive a restart
c := cosem.NewClient(cosem.ClientConfig{
	Transport: func(client int) (gdlms.Transport, error) {
		return wrapper.New("tcp", "192.0.2.10:4059", client, 1, 10*time.Second)
	},
	ClientAddress:     48,
	HLS:               hls,
	InvocationCounter: &cosem.FrameCounterObject{OBIS: "0.0.43.1.3.255"},
	Reserve: func(ic uint32, first bool) (uint32, error) { return r.Reserve(ctx, ic, first) },
})
err = c.Connect()
v, err := c.Get(3, "1.0.32.7.0.255", 2) // voltage L1

The examples in the documentation that run and check their output are cosem.ExampleProtect (the Green Book's suite 0 example), cosem.ExampleICReservation, obis.ExampleDescribe, is15959.ExampleGet, hdlc.ExampleFrame, wrapper.ExampleEncode, gateway.ExampleEncode, phy.ExampleIdentifyRequest, iec6205621.ExamplePointName and lpwan.ExampleLookupRegion. cosem.ExampleClient and iec6205621.ExampleOpenSession need a meter, so they compile but do not run here; dlms-sim runs the same steps against its simulated meters.

Documentation

API reference: https://pkg.go.dev/github.com/cB-Sigma/dlms

Package What it does
cosem The client: SecurityConfig (settings as text) and Associate/Client (AARQ/AARE, HLS mechanisms 2 to 7, the frame counter read through the public client); SecureTransport (suites 0 to 2, every ciphering form, general signing, the broadcast key, third-party layers, compression, ciphered release); ICReservation, Counter and PushCounter over an iotsec.Store; SecuritySetup (class 64); pushes (OpenProtectedPush, MapPush, ServeHDLCPushes, general block transfer); InboundConn (meter-initiated connections); Gateway (data concentrators); profile reading by range; NewPortClient over a port another protocol opened; serial lines with the IEC 62056-21 mode E opening; DLMS APDUs inside M-Bus (Seal, Open, Request, Exchange)
hdlc Frames, SNRM/UA negotiation, a client transport for gosem (Client), a server Station, a multi-drop Bus, a frame Scanner
phy The physical layer over any net.Conn: PH services, Hayes modem procedures, the identification service, a simulated modem
wrapper The wrapper header and a gosem transport over TCP or UDP
gateway The gateway prefix for meters behind a data concentrator
lpwan DLMS over NB-IoT (UDP reply windows, sleeping meters, a command queue) and LoRaWAN (a SCHC bridge to a NetworkServer, an in-memory MemNS), each meter as a net.Conn of wrapper frames
iec6205621 IEC 62056-21 sessions over serial ports or converters, programming mode, annex C, wake-ups, mode D, mode E; reduced-ID point names
obis OBIS codes: parse (full, short and reduced forms), validate, ranges, what a standard code means with its classes, camel-case point names (1.0.21.7.0.255 is activePowerImportL1)
is15959 IS 15959 Parts 1 to 3 as data under one point name per OBIS code; EventInfo

Testing

go test -race ./...

The tests use the worked examples of the standards (the IEC 62056-46 FCS table and frames, the IEC 62056-4-7 annex A.4 split, the IEC 62056-5-3:2023 Table 30 and annex E ciphering and release examples, the Green Book's suite 0 example), RFC 3394 key wrap and NIST CAVS ECDSA vectors, and fuzzing of the ciphered and general APDU decoders and of IEC 62056-21 messages. Tests carry // Covers: SIG-... comments: requirement IDs of the SigmaM2M project's compliance report, which mean nothing else here.

The client's tests against a simulated meter (every mechanism, suite and ciphering form, third-party layers, compression, data concentrators, the LoRaWAN bridge, IEC 62056-21 sessions) are in dlms-sim, so this module does not depend on its simulator.

Interop tested against

Against Gurux, written independently of this module, on 2026-10-11 (interop/, a nested test-only module that starts the peer in Docker; cd interop && SIGMA_DLMS_INTEROP=1 go test -v ./...). Peer: the server example of Gurux.DLMS.Java at commit 709c3a0 (gurux.dlms 4.0.95 from Maven Central), built in maven:3.9.9-eclipse-temurin-17, logical name referencing.

Check (our client against the Gurux server) Wrapper HDLC
No security, public client: logical device name, invocation counter pass pass
Low-level security: clock, register with scaler and unit, data object pass pass
Low-level security with a wrong password: refused, reported as such pass
HLS mechanism 2 (Gurux's AES reading of it) pass
HLS mechanism 2 with authenticated and encrypted APDUs (suite 0) pass
HLS-GMAC, suite 0, authenticated and encrypted APDUs: clock, register, profile generic by range, the whole 10 002-row buffer in 942 blocks pass
HLS-GMAC with Gurux's default settings (no ciphering context, plain APDUs): our automatic context is refused with diagnostic 2; applicationContext: "ln" associates and reads clock, register and profile by range pass
Profile generic: the whole buffer (10 002 rows, 858 blocks) and by range pass pass

Gurux's published unit-test vectors (Gurux.DLMS.Android at commit d3489a4, fetched at run time, not kept here): the AES-GCM authentication, encryption and authenticated encryption of a GET request match Protect and Unprotect, and the AES key wrap vectors match WrapKey and UnwrapKey. Its ACCESS service vectors are not decoded: this client does not implement the ACCESS service.

These runs found and fixed two things: our HLS mechanism 2 challenge is now one AES block long, the form both our ECB reading and Gurux's CBC reading accept, and the application context is now selectable (SecurityConfig.ApplicationContext) for meters that take HLS-GMAC only without the ciphering context. The Gurux client against our simulator is in dlms-sim. Also tested: the worked examples of the standards (above), and gosem v1.3.4's APDU encoders against the simulator.

Not yet: a real meter or data concentrator, short name referencing, the ACCESS service, suites 1 and 2 and general ciphering against another implementation (only against our own simulator).

  • dlms-sim: simulated DLMS/COSEM meters, a data concentrator and an IEC 62056-21 meter.
  • mbus: wired and wireless M-Bus, which carries DLMS APDUs with this module's cosem.
  • schc: SCHC and V.44, used for LoRaWAN and compression.
  • iotsec: the store and crypto policy seams.

Status

v0.x: the API may change before v1.

Contributing

See CONTRIBUTING.md.

Security

See SECURITY.md to report a vulnerability privately.

Licence

Apache-2.0 (see LICENSE and NOTICE). Copyright 2026 Abhinav Gautam.

Directories

Path Synopsis
Package cosem is a DLMS/COSEM client (logical name referencing): the application association (AARQ, AARE, RLRQ, RLRE) with low-level and high-level security authentication (mechanisms 1 to 7), the security suites 0 to 2 with service-specific, general-glo, general-ded and general ciphering, general signing, third-party protection and V.44 compression (IEC 62056-5-3:2023, the DLMS UA Green Book), get, set and action with block transfer, the security setup object (class 64: key transfer and agreement, certificates), push reception (data-notification, general block transfer, HDLC pushes), the gateway protocol of IEC 62056-5-3 annex C for meters behind a data concentrator, profile reading by range, meter-initiated connections, local serial lines with the IEC 62056-21 mode E opening, and invocation counters kept across restarts.
Package cosem is a DLMS/COSEM client (logical name referencing): the application association (AARQ, AARE, RLRQ, RLRE) with low-level and high-level security authentication (mechanisms 1 to 7), the security suites 0 to 2 with service-specific, general-glo, general-ded and general ciphering, general signing, third-party protection and V.44 compression (IEC 62056-5-3:2023, the DLMS UA Green Book), get, set and action with block transfer, the security setup object (class 64: key transfer and agreement, certificates), push reception (data-notification, general block transfer, HDLC pushes), the gateway protocol of IEC 62056-5-3 annex C for meters behind a data concentrator, profile reading by range, meter-initiated connections, local serial lines with the IEC 62056-21 mode E opening, and invocation counters kept across restarts.
Package gateway is the DLMS/COSEM gateway protocol of IEC 62056-5-3:2023 annex C: a COSEM APDU exchanged with an end device behind a gateway (a data concentrator unit, DCU) is prefixed with a header octet, the network ID of the gateway's local network, the length of the physical device address and that address (C.2, Figure C.2).
Package gateway is the DLMS/COSEM gateway protocol of IEC 62056-5-3:2023 annex C: a COSEM APDU exchanged with an end device behind a gateway (a data concentrator unit, DCU) is prefixed with a header octet, the network ID of the gateway's local network, the length of the physical device address and that address (C.2, Figure C.2).
Package hdlc is our IEC 62056-46 (Ed 1.1, 2002+A1:2006) data link layer for DLMS/COSEM, the 3-layer connection-oriented profile of IEC 62056-7-6 (Ed 1.0, 2013): HDLC frame format type 3, extended addressing with upper (logical device) and lower (physical device) server addresses, the UNC repertoire with UI frames, SNRM/UA parameter negotiation, segmentation, windows of up to seven frames, and the 8802-2 LLC header COSEM uses.
Package hdlc is our IEC 62056-46 (Ed 1.1, 2002+A1:2006) data link layer for DLMS/COSEM, the 3-layer connection-oriented profile of IEC 62056-7-6 (Ed 1.0, 2013): HDLC frame format type 3, extended addressing with upper (logical device) and lower (physical device) server addresses, the UNC repertoire with UI frames, SNRM/UA parameter negotiation, segmentation, windows of up to seven frames, and the 8802-2 LLC header COSEM uses.
Package iec6205621 is the IEC 62056-21 protocol (the local readout of meters formerly known as IEC 1107) from the client's side: messages and their block check character (clause 6.3), identification and option select, modes A to E (6.4), programming mode with the R1 to R6, W1 to W5, E2 and B0/B1 commands and partial blocks (6.4.7), the annex C formatted codes, the annex B wake-up and fast wake-up of battery meters, mode D pushes, and the mode E switch to the binary HDLC phase, after which the line is handed to a DLMS/COSEM client (Session.BinaryPort with cosem.NewPortClient).
Package iec6205621 is the IEC 62056-21 protocol (the local readout of meters formerly known as IEC 1107) from the client's side: messages and their block check character (clause 6.3), identification and option select, modes A to E (6.4), programming mode with the R1 to R6, W1 to W5, E2 and B0/B1 commands and partial blocks (6.4.7), the annex C formatted codes, the annex B wake-up and fast wake-up of battery meters, mode D pushes, and the mode E switch to the binary HDLC phase, after which the line is handed to a DLMS/COSEM client (Session.BinaryPort with cosem.NewPortClient).
Package is15959 is the object catalogue of the Indian DLMS/COSEM companion standards, as data: IS 15959 Part 1:2011 (meter categories), Part 2:2016 with Amendments 1 and 2 (2017) for single- and three-phase whole-current smart meters, and Part 3:2017 for CT and CT/PT operated smart meters.
Package is15959 is the object catalogue of the Indian DLMS/COSEM companion standards, as data: IS 15959 Part 1:2011 (meter categories), Part 2:2016 with Amendments 1 and 2 (2017) for single- and three-phase whole-current smart meters, and Part 3:2017 for CT and CT/PT operated smart meters.
Package lpwan carries DLMS/COSEM over low-power wide-area networks: NB-IoT (and LTE-M) meters that call in over UDP from behind carrier NAT and sleep between calls (udp.go, queue.go, wake.go), and LoRaWAN meters reached through a network server with SCHC compression and fragmentation (RFC 8724, RFC 9011; bridge.go, ns.go, regional.go).
Package lpwan carries DLMS/COSEM over low-power wide-area networks: NB-IoT (and LTE-M) meters that call in over UDP from behind carrier NAT and sleep between calls (udp.go, queue.go, wake.go), and LoRaWAN meters reached through a network server with SCHC compression and fragmentation (RFC 8724, RFC 9011; bridge.go, ns.go, regional.go).
Package obis is the Object Identification System of IEC 62056-6-1:2023 (EN IEC 62056-6-1:2024 is the same text) as a model: it validates an OBIS code, says which range each value group is in (standard, manufacturer, utility, consortia, country, reserved), describes what a standard code identifies, and derives a point name for it.
Package obis is the Object Identification System of IEC 62056-6-1:2023 (EN IEC 62056-6-1:2024 is the same text) as a model: it validates an OBIS code, says which range each value group is in (standard, manufacturer, utility, consortia, country, reserved), describes what a standard code identifies, and derives a point name for it.
Package phy is our physical layer for the IEC 62056-7-6 profile, after IEC 62056-42 (Ed 1.0, 2002): the PH-CONNECT, PH-ABORT and PH-DATA services over a byte stream (a serial-to-Ethernet converter, a direct line or a Hayes modem on the PSTN), the Hayes procedures of its annex A for dialling, answering and hanging up, and the optional identification service of 6.3.3 on both sides.
Package phy is our physical layer for the IEC 62056-7-6 profile, after IEC 62056-42 (Ed 1.0, 2002): the PH-CONNECT, PH-ABORT and PH-DATA services over a byte stream (a serial-to-Ethernet converter, a direct line or a Hayes modem on the PSTN), the Hayes procedures of its annex A for dialling, answering and hanging up, and the optional identification service of 6.3.3 on both sides.
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.
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.

Jump to

Keyboard shortcuts

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